home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / cmprss.zip / Compress.h < prev    next >
C/C++ Source or Header  |  1994-05-31  |  21KB  |  419 lines

  1. /****************************************************************************
  2.  * COMPRESS.H
  3.  *
  4.  *      This header file contains the function and constant declarations for
  5.  *      the data compression functions in the COMPRESS.DLL dynamic link
  6.  *      library.
  7.  *
  8.  *      Modifications : 05-Mar-94 : Initial version.
  9.  *                      20-Mar-94 : Added programming notes comments.
  10.  *                                  Changed PBYTE to const BYTE * in
  11.  *                                  CompressObject function declaration.
  12.  *                                  Changed PCOMPRESSED_DATA to
  13.  *                                  const COMPRESSED_DATA * in ExpandObject
  14.  *                                  function declaration.
  15.  *                                  Eliminated dependencies on OS2 toolkit
  16.  *                                  headers.
  17.  *                      10-Apr-94 : Fixed problem with CompressObject
  18.  *                                  and zero length input data blocks.
  19.  *                      14-May-94 : Added ReadAndExpand and CompressAndWrite
  20.  *                                  APIs.
  21.  *                      31-May-94 : Fixed OS2 common definitions section of
  22.  *                                  header file so that it is compatible
  23.  *                                  with the GNU C++ compiler.
  24.  *
  25.  *      (c)Copyright 1994 Rick Yoder
  26.  ****************************************************************************/
  27.  
  28. /*****************************************************************************
  29.  * Programming notes
  30.  *
  31.  * 1. All entry points into this DLL are implemented using the same calling
  32.  *    convention as the OS2 system APIs so it should be compiler independent.
  33.  *
  34.  * 2. The compression functions are fully reentrant.
  35.  *
  36.  * 3. The 32-bit CRCs used for data integrity checking are built using the same
  37.  *    32-bit generator polynomial used by the ZMODEM protocol: 0xEDB88320. The
  38.  *    CRC is initialized to 0xFFFFFFFF and the final calculated value is inverted.
  39.  *
  40.  * 3. Currently, the only compression method available is METHOD_DEFLATE, which
  41.  *    compresses your data using the pkzip 2.04 deflate method.
  42.  *
  43.  * 4. You should never receive a protection violation exception generated from
  44.  *    within the library functions. The compression functions are designed to
  45.  *    (hopefully) fail gracefully and return the appropriate error code if
  46.  *    they are given bad data or some sort of internal error occurs. If one of
  47.  *    the library functions returns COMPRESS_INTERNAL_ERROR or
  48.  *    EXPAND_INTERNAL_ERROR please report this to the author as this means
  49.  *    there is probably a bug in the library.
  50.  *****************************************************************************/
  51.  
  52. #if !defined(_COMPRESS_INCLUDED)
  53. #define _COMPRESS_INCLUDED
  54.  
  55. #ifdef __cplusplus
  56. extern "C" {
  57. #endif
  58.  
  59. /*****************************************************************************
  60.  * If the OS2 common definitions header from the OS2 toolkit (OS2DEF.H) has
  61.  * not been included, define the types from it that we need.
  62.  *****************************************************************************/
  63. #if !defined(OS2DEF_INCLUDED) && !defined(_OS2EMX_H)
  64.     #if defined(__EMX__)
  65.         #define EXPENTRY
  66.     #else
  67.         #define EXPENTRY _System
  68.     #endif
  69.     typedef unsigned long APIRET;
  70.  
  71.     typedef unsigned short USHORT;
  72.     typedef unsigned long  ULONG;
  73.     typedef char BYTE;
  74.     typedef BYTE *PBYTE;
  75. #endif
  76.  
  77. #if !(defined(INCL_DOSINCLUDED) || defined(_OS2EMX_H)) || !(defined (INCL_DOSFILEMGR) || !defined (INCL_NOCOMMON))
  78.     typedef unsigned long  HFILE;
  79. #endif
  80. /*****************************************************************************/
  81.  
  82. /*****************************************************************************/
  83. /* Definition of compression constants                                       */
  84. /*****************************************************************************/
  85. #define METHOD_STORE    0           /* store object with no compression */
  86. #define METHOD_DEFLATE  1           /* use ZIP style deflation */
  87.  
  88. #define METHOD_BEST     255         /* use best available compression method */
  89.                                     /*   (currently METHOD_DEFLATE)          */
  90. /*****************************************************************************/
  91.  
  92. /*****************************************************************************/
  93. /* Error constants                                                           */
  94. /*****************************************************************************/
  95. #define COMPRESS_INVALID_METHOD     ((APIRET)0xF0000000)
  96. #define COMPRESS_INVALID_LEVEL      ((APIRET)0xF0000001)
  97. #define COMPRESS_INVALID_PARAMETER  ((APIRET)0xF0000002)
  98. #define COMPRESS_INTERNAL_ERROR     ((APIRET)0xF0000003)
  99.  
  100. #define EXPAND_INVALID_PARAMETER    ((APIRET)0xFF000000)
  101. #define EXPAND_INVALID_METHOD       ((APIRET)0xFF000001)
  102. #define EXPAND_INTERNAL_ERROR       ((APIRET)0xFF000002)
  103. #define EXPAND_CRC                  ((APIRET)0xFF000003)
  104.  
  105. #ifndef ERROR_NOT_ENOUGH_MEMORY
  106.     #define ERROR_NOT_ENOUGH_MEMORY 8
  107. #endif
  108. #ifndef ERROR_INVALID_PARAMETER
  109.     #define ERROR_INVALID_PARAMETER 87
  110. #endif
  111. #ifndef ERROR_INTERRUPT
  112.     #define ERROR_INTERRUPT 95
  113. #endif
  114. #ifndef ERROR_INVALID_ADDRESS
  115.     #define ERROR_INVALID_ADDRESS 487
  116. #endif
  117. #ifndef ERROR_ACCESS_DENIED
  118.     #define ERROR_ACCESS_DENIED 5
  119. #endif
  120. #ifndef ERROR_LOCKED
  121.     #define ERROR_LOCKED 212
  122. #endif
  123. #ifndef ERROR_CROSSES_OBJECT_BOUNDARY
  124.     #define ERROR_CROSSES_OBJECT_BOUNDARY 32798
  125. #endif
  126. #ifndef ERROR_INVALID_HANDLE
  127.     #define ERROR_INVALID_HANDLE 6
  128. #endif
  129. #ifndef ERROR_NOT_DOS_DISK
  130.     #define ERROR_NOT_DOS_DISK 26
  131. #endif
  132. #ifndef ERROR_LOCK_VIOLATION
  133.     #define ERROR_LOCK_VIOLATION 33
  134. #endif
  135. #ifndef ERROR_WRITE_PROTECT
  136.     #define ERROR_WRITE_PROTECT 19
  137. #endif
  138. #ifndef ERROR_WRITE_FAULT
  139.     #define ERROR_WRITE_FAULT 29
  140. #endif
  141. #ifndef ERROR_BROKEN_PIPE
  142.     #define ERROR_BROKEN_PIPE 109
  143. #endif
  144. #ifndef ERROR_MORE_DATA
  145.     #define ERROR_MORE_DATA 234
  146. #endif
  147.  
  148. /*****************************************************************************/
  149.  
  150. /*****************************************************************************/
  151. /* Definition of compressed data object data structure                       */
  152. /*****************************************************************************/
  153. #pragma pack(1)
  154. typedef struct _COMPRESSED_DATA {
  155.     ULONG  crc;                 /* 32 bit CRC of rest of structure + compressed data */
  156.     ULONG  ulCompressedSize;    /* compressed size of data */
  157.     ULONG  crcOriginal;         /* 32 bit CRC of the original uncompressed data */
  158.     ULONG  ulOriginalSize;      /* original uncompressed size of data */
  159.     USHORT version;             /* version of COMPRESS.DLL used to compress data */
  160.     BYTE   method;              /* compression method */
  161.     BYTE   reserved;            /* currently unused, set to 0 */
  162.     BYTE   data[1];             /* compressed data starts here */
  163.     } COMPRESSED_DATA;
  164.  
  165. typedef COMPRESSED_DATA * PCOMPRESSED_DATA;
  166. #pragma pack()
  167.  
  168. #define COMPRESSED_HEADER_SIZE  ( sizeof(COMPRESSED_DATA) - 1 )     /* 20 bytes */
  169. /*****************************************************************************/
  170.  
  171. /*****************************************************************************
  172.  * Function declarations
  173.  *****************************************************************************/
  174. extern APIRET EXPENTRY CompressObject( const BYTE * pData,
  175.                                        ULONG dataSize,
  176.                                        PCOMPRESSED_DATA * ppCompressed,
  177.                                        BYTE method,
  178.                                        BYTE level );
  179. /* Description          This function compresses the designated memory object.
  180.  *
  181.  * Parameters           pData points to the data object that is to be compressed.
  182.  *                      This parameter may be set to NULL if dataSize is zero.
  183.  *
  184.  *                      dataSize contains the size in bytes of the input data object.
  185.  *                      If dataSize is zero, the METHOD_STORE compression
  186.  *                      method will always be used.
  187.  *
  188.  *                      ppCompressed points to the location where the pointer to
  189.  *                      the compressed data object created by CompressObject is to
  190.  *                      be stored. The memory used to store the compressed data
  191.  *                      object is sparse allocated by the DosAllocMem function.
  192.  *
  193.  *                      method contains the compression method to be used to
  194.  *                      compress the data object. This parameter can be set to
  195.  *                      one of the following values:
  196.  *
  197.  *                          METHOD_STORE    ..... store with no compression.
  198.  *                          METHOD_DEFLATE  ..... use ZIP style deflation.
  199.  *                          METHOD_BEST     ..... use best available method
  200.  *                                                  (currently METHOD_DEFLATE).
  201.  *
  202.  *                      level contains a value from 0 - 9 indicating the compression
  203.  *                      level to be used. A value of 0 forces the data to be STORED.
  204.  *                      A value of 1 gives fast but minimal compression. A value of
  205.  *                      9 gives slow but maximal compression. Usually a compression
  206.  *                      level of 4 or 5 provides a decent speed/compression tradeoff.
  207.  *
  208.  * Return Value         A return value of 0 indicates that no error occured, otherwise
  209.  *                      CompressObject returns one of the following error codes:
  210.  *
  211.  *                      Possible OS/2 errors
  212.  *                      --------------------
  213.  *                      ERROR_NOT_ENOUGH_MEMORY
  214.  *                      ERROR_INVALID_PARAMETER
  215.  *                      ERROR_INTERRUPT
  216.  *                      ERROR_INVALID_ADDRESS
  217.  *                      ERROR_ACCESS_DENIED
  218.  *                      ERROR_LOCKED
  219.  *                      ERROR_CROSSES_OBJECT_BOUNDARY
  220.  *
  221.  *                      Compression library specific errors
  222.  *                      -----------------------------------
  223.  *                      COMPRESS_INVALID_METHOD     ..... The specified compression method
  224.  *                                                        is not supported by this version of
  225.  *                                                        the library.
  226.  *
  227.  *                      COMPRESS_INVALID_LEVEL      ..... The compression level parameter
  228.  *                                                        contained an invalid value.
  229.  *
  230.  *                      COMPRESS_INVALID_PARAMETER  ..... The pData, dataSize, or ppCompressed
  231.  *                                                        parameter contained an invalid
  232.  *                                                        value.
  233.  *
  234.  *                      COMPRESS_INTERNAL_ERROR     ..... An internal error occured in the
  235.  *                                                        compression function.
  236.  */
  237.  
  238. extern APIRET EXPENTRY ExpandObject( const COMPRESSED_DATA * pData,
  239.                                      PBYTE * ppExpanded );
  240. /* Purpose              This function decompresses the designated compressed data
  241.  *                      object.
  242.  *
  243.  * Parameters           pData points to the compressed data object to be
  244.  *                      expanded.
  245.  *
  246.  *                      ppExpanded points to the location where the pointer to the
  247.  *                      expanded data object created by ExpandObject is to be stored.
  248.  *                      The memory used to store the expanded data object is allocated
  249.  *                      by the DosAllocMem function.
  250.  *
  251.  * Return Value         A return value of 0 indicates that no error occured, otherwise
  252.  *                      ExpandObject returns one of the following error codes:
  253.  *
  254.  *                      Possible OS/2 errors
  255.  *                      --------------------
  256.  *                      ERROR_NOT_ENOUGH_MEMORY
  257.  *                      ERROR_INVALID_PARAMETER
  258.  *                      ERROR_INTERRUPT
  259.  *                      ERROR_ACCESS_DENIED
  260.  *
  261.  *                      Compression library specific errors
  262.  *                      -----------------------------------
  263.  *                      EXPAND_INVALID_PARAMETER    ..... The pData or ppExpanded parameters
  264.  *                                                        contain a pointer to an invalid
  265.  *                                                        address.
  266.  *
  267.  *                      EXPAND_INVALID_METHOD       ..... The compressed data object was
  268.  *                                                        compressed using a method not
  269.  *                                                        supported by this version of the
  270.  *                                                        library.
  271.  *
  272.  *                      EXPAND_CRC                  ..... The contents of the input compressed
  273.  *                                                        data object appear to have been
  274.  *                                                        corrupted.
  275.  *
  276.  *                      EXPAND_INTERNAL_ERROR       ..... An internal error occurred in the
  277.  *                                                        data expansion function.
  278.  */
  279.  
  280. extern USHORT EXPENTRY CompressVersion( void );
  281. /* Purpose              This function retrieves the currently installed version
  282.  *                      of the data compression library.
  283.  *
  284.  * Return Value         The major version number is stored in the high order
  285.  *                      byte. The minor version number is stored in the low
  286.  *                      order byte.
  287.  */
  288.  
  289. extern APIRET EXPENTRY ReadAndExpand( HFILE hfile,PBYTE * ppExpanded );
  290. /* Purpose              This function reads a compressed data object
  291.  *                      from a disk file, expands the compressed
  292.  *                      data, and returns the expanded data to
  293.  *                      the caller.
  294.  *
  295.  * Parameters           hfile contains the handle to the file
  296.  *                      being read. ReadAndExpand assumes that
  297.  *                      the file pointer has already been set
  298.  *                      to the start of the compressed data
  299.  *                      object.
  300.  *
  301.  *                      ppExpanded points to the location where the pointer to the
  302.  *                      expanded data object created by ExpandObject is to be stored.
  303.  *                      The memory used to store the expanded data object is allocated
  304.  *                      by the DosAllocMem function.
  305.  *
  306.  * Return Value         A return value of 0 indicates that no error occured, otherwise
  307.  *                      ReadAndExpand returns one of the following error codes:
  308.  *
  309.  *                      Possible OS/2 errors
  310.  *                      --------------------
  311.  *                      ERROR_NOT_ENOUGH_MEMORY
  312.  *                      ERROR_INVALID_PARAMETER
  313.  *                      ERROR_INTERRUPT
  314.  *                      ERROR_ACCESS_DENIED
  315.  *                      ERROR_INVALID_HANDLE
  316.  *                      ERROR_NOT_DOS_DISK
  317.  *                      ERROR_LOCK_VIOLATION
  318.  *                      ERROR_BROKEN_PIPE
  319.  *                      ERROR_MORE_DATA
  320.  *
  321.  *                      Compression library specific errors
  322.  *                      -----------------------------------
  323.  *                      EXPAND_INVALID_PARAMETER    ..... The pData or ppExpanded parameters
  324.  *                                                        contain a pointer to an invalid
  325.  *                                                        address.
  326.  *
  327.  *                      EXPAND_INVALID_METHOD       ..... The compressed data object was
  328.  *                                                        compressed using a method not
  329.  *                                                        supported by this version of the
  330.  *                                                        library.
  331.  *
  332.  *                      EXPAND_CRC                  ..... The contents of the input compressed
  333.  *                                                        data object appear to have been
  334.  *                                                        corrupted.
  335.  *
  336.  *                      EXPAND_INTERNAL_ERROR       ..... An internal error occurred in the
  337.  *                                                        data expansion function.
  338.  */
  339.  
  340. extern APIRET EXPENTRY CompressAndWrite( HFILE hfile,
  341.                                          const BYTE * pData,
  342.                                          ULONG dataSize,
  343.                                          BYTE method,
  344.                                          BYTE level );
  345. /* Description          This function compresses the designated memory object
  346.  *                      and then writes the compressed data to the specified
  347.  *                      disk file.
  348.  *
  349.  * Parameters           hfile contains the handle to the file being
  350.  *                      written. CompressAndWrite assumes that the
  351.  *                      file pointer has already been set to the
  352.  *                      location where the compressed object is to
  353.  *                      be stored.
  354.  *
  355.  *                      pData points to the data object that is to be compressed.
  356.  *                      This parameter may be set to NULL if dataSize is zero.
  357.  *
  358.  *                      dataSize contains the size in bytes of the input data object.
  359.  *                      If dataSize is zero, the METHOD_STORE compression
  360.  *                      method will always be used.
  361.  *
  362.  *                      method contains the compression method to be used to
  363.  *                      compress the data object. This parameter can be set to
  364.  *                      one of the following values:
  365.  *
  366.  *                          METHOD_STORE    ..... store with no compression.
  367.  *                          METHOD_DEFLATE  ..... use ZIP style deflation.
  368.  *                          METHOD_BEST     ..... use best available method
  369.  *                                                  (currently METHOD_DEFLATE).
  370.  *
  371.  *                      level contains a value from 0 - 9 indicating the compression
  372.  *                      level to be used. A value of 0 forces the data to be STORED.
  373.  *                      A value of 1 gives fast but minimal compression. A value of
  374.  *                      9 gives slow but maximal compression. Usually a compression
  375.  *                      level of 4 or 5 provides a decent speed/compression tradeoff.
  376.  *
  377.  * Return Value         A return value of 0 indicates that no error occured, otherwise
  378.  *                      CompressAndWrite returns one of the following error codes:
  379.  *
  380.  *                      Possible OS/2 errors
  381.  *                      --------------------
  382.  *                      ERROR_NOT_ENOUGH_MEMORY
  383.  *                      ERROR_INVALID_PARAMETER
  384.  *                      ERROR_INTERRUPT
  385.  *                      ERROR_INVALID_ADDRESS
  386.  *                      ERROR_ACCESS_DENIED
  387.  *                      ERROR_LOCKED
  388.  *                      ERROR_CROSSES_OBJECT_BOUNDARY
  389.  *                      ERROR_INVALID_HANDLE
  390.  *                      ERROR_WRITE_PROTECT
  391.  *                      ERROR_NOT_DOS_DISK
  392.  *                      ERROR_WRITE_FAULT
  393.  *                      ERROR_LOCK_VIOLATION
  394.  *                      ERROR_BROKEN_PIPE
  395.  *
  396.  *                      Compression library specific errors
  397.  *                      -----------------------------------
  398.  *                      COMPRESS_INVALID_METHOD     ..... The specified compression method
  399.  *                                                        is not supported by this version of
  400.  *                                                        the library.
  401.  *
  402.  *                      COMPRESS_INVALID_LEVEL      ..... The compression level parameter
  403.  *                                                        contained an invalid value.
  404.  *
  405.  *                      COMPRESS_INVALID_PARAMETER  ..... The pData, dataSize, or ppCompressed
  406.  *                                                        parameter contained an invalid
  407.  *                                                        value.
  408.  *
  409.  *                      COMPRESS_INTERNAL_ERROR     ..... An internal error occured in the
  410.  *                                                        compression function.
  411.  */
  412. /*****************************************************************************/
  413.  
  414. #ifdef __cplusplus
  415. }
  416. #endif
  417.  
  418. #endif /* _COMPRESS_INCLUDED */
  419.