// Program name: cftypes.cpp // Programmed by: Anthony Barbachan // Programmed in: C++ (Turbo C++ 3.0 Compatable) // Purpose: source file for cabinet file types and constants. // Version: 1.00 // Last modified on: 12/4/1998 // Last modified on: 1/5/1998 // Changes: Added more error strings. #ifndef __CFTYPES_CPP__ #define __CFTYPES_CPP__ #include #include #include #include "zlib.h" #include "cftypes.h" #ifndef MAXSHORT #define BITSPERBYTE 8 #define BITS(type) (BITSPERBYTE * (int)sizeof(type)) #define SHORTBITS BITS(int16_t) #define MINSHORT ((int16_t)(1 << (SHORTBITS - 1))) #define MAXSHORT ((int16_t)~MINSHORT) #endif /******************************************************************************/ int convert_z_error_code(int code) { switch(code) { case Z_OK: return COMPRESSOR_Z_OK; case Z_STREAM_END: return COMPRESSOR_Z_STREAM_END; case Z_NEED_DICT: return COMPRESSOR_Z_NEED_DICT; case Z_ERRNO: return COMPRESSOR_Z_ERRNO; case Z_STREAM_ERROR: return COMPRESSOR_Z_STREAM_ERROR; case Z_DATA_ERROR: return COMPRESSOR_Z_DATA_ERROR; case Z_MEM_ERROR: return COMPRESSOR_Z_MEM_ERROR; case Z_BUF_ERROR: return COMPRESSOR_Z_BUF_ERROR; case Z_VERSION_ERROR: return COMPRESSOR_Z_VERSION_ERROR; default: return COMPRESSOR_Z_UNKNOWN_ERROR; } } /******************************************************************************/ const char* get_cabinet_error_string(int err) { switch(err) { case OK: return "Operation completed successfully"; case CLOSED_CABINET: return "Operation on a closed cabinet object"; case NO_MEMORY: return "Failed because we are out of memory"; case READ_ERROR: return "Error encountered during a read"; case WRITE_ERROR: return "Error encountered during a write"; case INVALID_DATA: return "Data either invalid or in bad format"; case UNABLE_TO_OPEN: return "Unable to open a file stream"; case INVALID_HEADER: return "If this cabinet's header is currupt"; case INVALID_FOLDERNO: return "Invalid folder number"; case SEEK_ERROR: return "A seek operation has failed"; case MISSING_FILE: return "Expected file not found in blocks"; case CHECKSUM_ERROR: return "A checksum error has been encountered"; case UNSUPPORTED_COMPRESSION: return "An unsupported compression method choosen"; case DECOMPRESSION_ERROR: return "An error has occured during decompression"; case UNEXPECTED_EOF: return "An unexpected EOF was encountered"; case NO_FOLDERS: return "Cabinet is lacking any folder entries"; case FILE_NOT_FOUND: return "Requested file was not found"; case NO_FILENAME: return "No filename specified"; case GETCWD_FAILURE: return "The getcwd function call failed"; case OUT_OF_MEMORY: return "An allocation operation has run out of memory"; case CHDIR_FAILURE: return "A chdir operation failed"; case MKDIR_FAILURE: return "A mkdir operation failed"; case ACCESS_DENIED: return "Access was denied"; case SETTIME_FAILURE: return "Operation failed to restore the date or time"; case SETATTRIB_FAILURE: return "Operation failed to restore the attributes"; case OPEN_TEMP_FAILURE: return "Unable to open temporary file stream"; case FOLDER_CREATOR_FROZEN: return "An attempt was made to add a file to a frozen folder creator"; case FOLDER_LIMIT_REACHED: return "No more folders can be added to this cabinet"; case FILE_LIMIT_REACHED: return "No more files can be added to this folder"; case NO_FOLDER: return "No folder exists"; case FILE_OPEN_FAILURE: return "Unable to open a file"; case FILE_CLOSE_FAILURE: return "Unable to close a file"; case COMPRESSOR_OUT_OF_MEMORY: return "The compressor ran out of memory"; case COMPRESSOR_BUF_TO_SMALL: return "To small of a buffer was passed to the compressor"; case COMPRESSOR_UNKNOWN_ERROR: return "An unknown compressor error has occured"; case FSTAT_FAILURE: return "A call to fstat failed"; case GETATTRIB_FAILURE: return "The call to retrieve the file's attributes failed"; case GETTIME_FAILURE: return "The call to retrieve the file's date/time failed"; case UNKNOWN_ERROR: return "An unknown error has occured"; default: return "Unknown error code"; } } /******************************************************************************/ //*****************************************************************************/ int io_read(istream& in, byte* buf, word len) { // replaced MAXINT with MAXSHORT, since len is of type // word == unsigned short int // // TODO: please review this, since IMO the while-loop is // never entered while (len > MAXSHORT) { if(in.read((char*)buf, MAXSHORT).bad()) return (in.fail()) ? READ_ERROR : UNEXPECTED_EOF; len -= (word) MAXSHORT; buf += (word) MAXSHORT; } return (in.read((char*)buf, (int) len).bad()) ? (in.fail()) ? READ_ERROR : UNEXPECTED_EOF : OK; } //*****************************************************************************/ int io_write(ostream& out, const byte* buf, word len) { // replaced MAXINT with MAXSHORT, since len is of type // word == unsigned short int // // TODO: please review this, since IMO the while-loop is // never entered while(len > MAXSHORT) { if(out.write((char*)buf, MAXSHORT).fail()) return WRITE_ERROR; len -= (word) MAXSHORT; buf += (word) MAXSHORT; } return (out.write((char*)buf, (int) len).fail()) ? WRITE_ERROR : OK; } //*****************************************************************************/ #endif