lzrw3.h
上传用户:lgb322
上传日期:2013-02-24
资源大小:30529k
文件大小:16k
源码类别:

嵌入式Linux

开发平台:

Unix_Linux

  1. #ifndef _LZRW3_H
  2. #define _LZRW3_H
  3. /*
  4.  * $Source: /homes/cvs/ftape-stacked/ftape/compressor/lzrw3.h,v $
  5.  * $Revision: 1.1 $
  6.  * $Date: 1997/10/05 19:12:30 $
  7.  *
  8.  *  include files for lzrw3. Only slighty modified from the original
  9.  *  version. Assembles the three include files compress.h, port.h and
  10.  *  fastcopy.h from the original lzrw3 package.
  11.  *
  12.  */
  13. #include <linux/types.h>
  14. #include <linux/string.h>
  15. /******************************************************************************/
  16. /*                                                                            */
  17. /*                                 COMPRESS.H                                 */
  18. /*                                                                            */
  19. /******************************************************************************/
  20. /*                                                                            */
  21. /* Author : Ross Williams.                                                    */
  22. /* Date   : December 1989.                                                    */
  23. /*                                                                            */
  24. /* This header file defines the interface to a set of functions called        */
  25. /* 'compress', each member of which implements a particular data compression  */
  26. /* algorithm.                                                                 */
  27. /*                                                                            */
  28. /* Normally in C programming, for each .H file, there is a corresponding .C   */
  29. /* file that implements the functions promised in the .H file.                */
  30. /* Here, there are many .C files corresponding to this header file.           */
  31. /* Each comforming implementation file contains a single function             */
  32. /* called 'compress' that implements a single data compression                */
  33. /* algorithm that conforms with the interface specified in this header file.  */
  34. /* Only one algorithm can be linked in at a time in this organization.        */
  35. /*                                                                            */
  36. /******************************************************************************/
  37. /*                                                                            */
  38. /*                    DEFINITION OF FUNCTION COMPRESS                         */
  39. /*                    ===============================                         */
  40. /*                                                                            */
  41. /* Summary of Function Compress                                               */
  42. /* ----------------------------                                               */
  43. /* The action that 'compress' takes depends on its first argument called      */
  44. /* 'action'.  The function provides three actions:                            */
  45. /*                                                                            */
  46. /*    - Return information about the algorithm.                               */
  47. /*    - Compress   a block of memory.                                         */
  48. /*    - Decompress a block of memory.                                         */
  49. /*                                                                            */
  50. /* Parameters                                                                 */
  51. /* ----------                                                                 */
  52. /* See the formal C definition later for a description of the parameters.     */
  53. /*                                                                            */
  54. /* Constants                                                                  */
  55. /* ---------                                                                  */
  56. /* COMPRESS_OVERRUN: The constant COMPRESS_OVERRUN defines by how many bytes  */
  57. /* an algorithm is allowed to expand a block during a compression operation.  */
  58. /*                                                                            */
  59. /* Although compression algorithms usually compress data, there will always   */
  60. /* be data that a given compressor will expand (this can be proven).          */
  61. /* Fortunately, the degree of expansion can be limited to a single bit, by    */
  62. /* copying over the input data if the data gets bigger during compression.    */
  63. /* To allow for this possibility, the first bit of a compressed               */
  64. /* representation can be used as a flag indicating whether the                */
  65. /* input data was copied over, or truly compressed. In practice, the first    */
  66. /* byte would be used to store this bit so as to maintain byte alignment.     */
  67. /*                                                                            */
  68. /* Unfortunately, in general, the only way to tell if an algorithm will       */
  69. /* expand a particular block of data is to run the algorithm on the data.     */
  70. /* If the algorithm does not continuously monitor how many output bytes it    */
  71. /* has written, it might write an output block far larger than the input      */
  72. /* block before realizing that it has done so.                                */
  73. /* On the other hand, continuous checks on output length are inefficient.     */
  74. /*                                                                            */
  75. /* To cater for all these problems, this interface definition:                */
  76. /* > Allows a compression algorithm to return an output block that is up to   */
  77. /*   COMPRESS_OVERRUN bytes longer than the input block.                      */
  78. /* > Allows a compression algorithm to write up to COMPRESS_OVERRUN bytes     */
  79. /*   more than the length of the input block to the memory of the output      */
  80. /*   block regardless of the length of the output block eventually returned.  */
  81. /*   This allows an algorithm to overrun the length of the input block in the */
  82. /*   output block by up to COMPRESS_OVERRUN bytes between expansion checks.   */
  83. /*                                                                            */
  84. /* The problem does not arise for decompression.                              */
  85. /*                                                                            */
  86. /* Identity Action                                                            */
  87. /* ---------------                                                            */
  88. /* > action must be COMPRESS_ACTION_IDENTITY.                                 */
  89. /* > p_dst_len must point to a longword to receive a longword address.        */
  90. /* > The value of the other parameters does not matter.                       */
  91. /* > After execution, the longword that p_dst_len points to will be a pointer */
  92. /*   to a structure of type compress_identity.                                */
  93. /*   Thus, for example, after the call, (*p_dst_len)->memory will return the  */
  94. /*   number of bytes of working memory that the algorithm requires to run.    */
  95. /* > The values of the identity structure returned are fixed constant         */
  96. /*   attributes of the algorithm and must not vary from call to call.         */
  97. /*                                                                            */
  98. /* Common Requirements for Compression and Decompression Actions              */
  99. /* -------------------------------------------------------------              */
  100. /* > wrk_mem must point to an unused block of memory of a length specified in */
  101. /*   the algorithm's identity block. The identity block can be obtained by    */
  102. /*   making a separate call to compress, specifying the identity action.      */
  103. /* > The INPUT BLOCK is defined to be Memory[src_addr,src_addr+src_len-1].    */
  104. /* > dst_len will be used to denote *p_dst_len.                               */
  105. /* > dst_len is not read by compress, only written.                           */
  106. /* > The value of dst_len is defined only upon termination.                   */
  107. /* > The OUTPUT BLOCK is defined to be Memory[dst_addr,dst_addr+dst_len-1].   */
  108. /*                                                                            */
  109. /* Compression Action                                                         */
  110. /* ------------------                                                         */
  111. /* > action must be COMPRESS_ACTION_COMPRESS.                                 */
  112. /* > src_len must be in the range [0,COMPRESS_MAX_ORG].                       */
  113. /* > The OUTPUT ZONE is defined to be                                         */
  114. /*      Memory[dst_addr,dst_addr+src_len-1+COMPRESS_OVERRUN].                 */
  115. /* > The function can modify any part of the output zone regardless of the    */
  116. /*   final length of the output block.                                        */
  117. /* > The input block and the output zone must not overlap.                    */
  118. /* > dst_len will be in the range [0,src_len+COMPRESS_OVERRUN].               */
  119. /* > dst_len will be in the range [0,COMPRESS_MAX_COM] (from prev fact).      */
  120. /* > The output block will consist of a representation of the input block.    */
  121. /*                                                                            */
  122. /* Decompression Action                                                       */
  123. /* --------------------                                                       */
  124. /* > action must be COMPRESS_ACTION_DECOMPRESS.                               */
  125. /* > The input block must be the result of an earlier compression operation.  */
  126. /* > If the previous fact is true, the following facts must also be true:     */
  127. /*   > src_len will be in the range [0,COMPRESS_MAX_COM].                     */
  128. /*   > dst_len will be in the range [0,COMPRESS_MAX_ORG].                     */
  129. /* > The input and output blocks must not overlap.                            */
  130. /* > Only the output block is modified.                                       */
  131. /* > Upon termination, the output block will consist of the bytes contained   */
  132. /*   in the input block passed to the earlier compression operation.          */
  133. /*                                                                            */
  134. /******************************************************************************/
  135. /******************************************************************************/
  136. /*                                                                            */
  137. /*                                    PORT.H                                  */
  138. /*                                                                            */
  139. /******************************************************************************/
  140. /*                                                                            */
  141. /* This module contains macro definitions and types that are likely to        */
  142. /* change between computers.                                                  */
  143. /*                                                                            */
  144. /******************************************************************************/
  145. #ifndef DONE_PORT       /* Only do this if not previously done.               */
  146.    #ifdef THINK_C
  147.       #define UBYTE unsigned char      /* Unsigned byte                       */
  148.       #define UWORD unsigned int       /* Unsigned word (2 bytes)             */
  149.       #define ULONG unsigned long      /* Unsigned word (4 bytes)             */
  150.       #define BOOL  unsigned char      /* Boolean                             */
  151.       #define FOPEN_BINARY_READ  "rb"  /* Mode string for binary reading.     */
  152.       #define FOPEN_BINARY_WRITE "wb"  /* Mode string for binary writing.     */
  153.       #define FOPEN_TEXT_APPEND  "a"   /* Mode string for text appending.     */
  154.       #define REAL double              /* USed for floating point stuff.      */
  155.    #endif
  156.    #if defined(LINUX) || defined(linux)
  157.       #define UBYTE __u8               /* Unsigned byte                       */
  158.       #define UWORD __u16              /* Unsigned word (2 bytes)             */
  159.       #define ULONG __u32              /* Unsigned word (4 bytes)             */
  160.       #define LONG  __s32              /* Signed   word (4 bytes)             */
  161.       #define BOOL  is not used here   /* Boolean                             */
  162.       #define FOPEN_BINARY_READ  not used  /* Mode string for binary reading. */
  163.       #define FOPEN_BINARY_WRITE not used  /* Mode string for binary writing. */
  164.       #define FOPEN_TEXT_APPEND  not used  /* Mode string for text appending. */
  165.       #define REAL not used                /* USed for floating point stuff.  */
  166.       #ifndef TRUE
  167.       #define TRUE 1
  168.       #endif
  169.    #endif
  170.    #define DONE_PORT                   /* Don't do all this again.            */
  171.    #define MALLOC_FAIL NULL            /* Failure status from malloc()        */
  172.    #define LOCAL static                /* For non-exported routines.          */
  173.    #define EXPORT                      /* Signals exported function.          */
  174.    #define then                        /* Useful for aligning ifs.            */
  175. #endif
  176. /******************************************************************************/
  177. /*                              End of PORT.H                                 */
  178. /******************************************************************************/
  179. #define COMPRESS_ACTION_IDENTITY   0
  180. #define COMPRESS_ACTION_COMPRESS   1
  181. #define COMPRESS_ACTION_DECOMPRESS 2
  182. #define COMPRESS_OVERRUN 1024
  183. #define COMPRESS_MAX_COM 0x70000000
  184. #define COMPRESS_MAX_ORG (COMPRESS_MAX_COM-COMPRESS_OVERRUN)
  185. #define COMPRESS_MAX_STRLEN 255
  186. /* The following structure provides information about the algorithm.         */
  187. /* > The top bit of id must be zero. The remaining bits must be chosen by    */
  188. /*   the author of the algorithm by tossing a coin 31 times.                 */
  189. /* > The amount of memory requested by the algorithm is specified in bytes   */
  190. /*   and must be in the range [0,0x70000000].                                */
  191. /* > All strings s must be such that strlen(s)<=COMPRESS_MAX_STRLEN.         */
  192. struct compress_identity
  193.   {
  194.    ULONG id;           /* Identifying number of algorithm.            */
  195.    ULONG memory;       /* Number of bytes of working memory required. */
  196.    char  *name;        /* Name of algorithm.                          */
  197.    char  *version;     /* Version number.                             */
  198.    char  *date;        /* Date of release of this version.            */
  199.    char  *copyright;   /* Copyright message.                          */
  200.    char  *author;      /* Author of algorithm.                        */
  201.    char  *affiliation; /* Affiliation of author.                      */
  202.    char  *vendor;      /* Where the algorithm can be obtained.        */
  203.   };
  204. void  lzrw3_compress(        /* Single function interface to compression algorithm. */
  205. UWORD     action,      /* Action to be performed.                             */
  206. UBYTE   *wrk_mem,      /* Working memory temporarily given to routine to use. */
  207. UBYTE   *src_adr,      /* Address of input  data.                             */
  208. LONG     src_len,      /* Length  of input  data.                             */
  209. UBYTE   *dst_adr,      /* Address of output data.                             */
  210. void  *p_dst_len       /* Pointer to a longword where routine will write:     */
  211.                        /*    If action=..IDENTITY   => Adr of id structure.   */
  212.                        /*    If action=..COMPRESS   => Length of output data. */
  213.                        /*    If action=..DECOMPRESS => Length of output data. */
  214. );
  215. /******************************************************************************/
  216. /*                             End of COMPRESS.H                              */
  217. /******************************************************************************/
  218. /******************************************************************************/
  219. /*                                  fast_copy.h                               */
  220. /******************************************************************************/
  221. /* This function copies a block of memory very quickly.                       */
  222. /* The exact speed depends on the relative alignment of the blocks of memory. */
  223. /* PRE  : 0<=src_len<=(2^32)-1 .                                              */
  224. /* PRE  : Source and destination blocks must not overlap.                     */
  225. /* POST : MEM[dst_adr,dst_adr+src_len-1]=MEM[src_adr,src_adr+src_len-1].      */
  226. /* POST : MEM[dst_adr,dst_adr+src_len-1] is the only memory changed.          */
  227. #define fast_copy(src,dst,len) memcpy(dst,src,len)
  228. /******************************************************************************/
  229. /*                               End of fast_copy.h                           */
  230. /******************************************************************************/
  231. #endif