You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

deflate.h 7.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. #ifndef _IPXE_DEFLATE_H
  2. #define _IPXE_DEFLATE_H
  3. /** @file
  4. *
  5. * DEFLATE decompression algorithm
  6. *
  7. */
  8. FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
  9. #include <stdint.h>
  10. #include <string.h>
  11. #include <ipxe/uaccess.h>
  12. /** Compression formats */
  13. enum deflate_format {
  14. /** Raw DEFLATE data (no header or footer) */
  15. DEFLATE_RAW,
  16. /** ZLIB header and footer */
  17. DEFLATE_ZLIB,
  18. };
  19. /** Block header length (in bits) */
  20. #define DEFLATE_HEADER_BITS 3
  21. /** Block header final block flags bit */
  22. #define DEFLATE_HEADER_BFINAL_BIT 0
  23. /** Block header type LSB */
  24. #define DEFLATE_HEADER_BTYPE_LSB 1
  25. /** Block header type mask */
  26. #define DEFLATE_HEADER_BTYPE_MASK 0x03
  27. /** Block header type: literal data */
  28. #define DEFLATE_HEADER_BTYPE_LITERAL 0
  29. /** Block header type: static Huffman alphabet */
  30. #define DEFLATE_HEADER_BTYPE_STATIC 1
  31. /** Block header type: dynamic Huffman alphabet */
  32. #define DEFLATE_HEADER_BTYPE_DYNAMIC 2
  33. /** Literal header LEN/NLEN field length (in bits) */
  34. #define DEFLATE_LITERAL_LEN_BITS 16
  35. /** Dynamic header length (in bits) */
  36. #define DEFLATE_DYNAMIC_BITS 14
  37. /** Dynamic header HLIT field LSB */
  38. #define DEFLATE_DYNAMIC_HLIT_LSB 0
  39. /** Dynamic header HLIT field mask */
  40. #define DEFLATE_DYNAMIC_HLIT_MASK 0x1f
  41. /** Dynamic header HDIST field LSB */
  42. #define DEFLATE_DYNAMIC_HDIST_LSB 5
  43. /** Dynamic header HDIST field mask */
  44. #define DEFLATE_DYNAMIC_HDIST_MASK 0x1f
  45. /** Dynamic header HCLEN field LSB */
  46. #define DEFLATE_DYNAMIC_HCLEN_LSB 10
  47. /** Dynamic header HCLEN field mask */
  48. #define DEFLATE_DYNAMIC_HCLEN_MASK 0x0f
  49. /** Dynamic header code length length (in bits) */
  50. #define DEFLATE_CODELEN_BITS 3
  51. /** Maximum length of a Huffman symbol (in bits) */
  52. #define DEFLATE_HUFFMAN_BITS 15
  53. /** Quick lookup length for a Huffman symbol (in bits)
  54. *
  55. * This is a policy decision.
  56. */
  57. #define DEFLATE_HUFFMAN_QL_BITS 7
  58. /** Quick lookup shift */
  59. #define DEFLATE_HUFFMAN_QL_SHIFT ( 16 - DEFLATE_HUFFMAN_QL_BITS )
  60. /** Literal/length end of block code */
  61. #define DEFLATE_LITLEN_END 256
  62. /** Maximum value of a literal/length code */
  63. #define DEFLATE_LITLEN_MAX_CODE 287
  64. /** Maximum value of a distance code */
  65. #define DEFLATE_DISTANCE_MAX_CODE 31
  66. /** Maximum value of a code length code */
  67. #define DEFLATE_CODELEN_MAX_CODE 18
  68. /** ZLIB header length (in bits) */
  69. #define ZLIB_HEADER_BITS 16
  70. /** ZLIB header compression method LSB */
  71. #define ZLIB_HEADER_CM_LSB 0
  72. /** ZLIB header compression method mask */
  73. #define ZLIB_HEADER_CM_MASK 0x0f
  74. /** ZLIB header compression method: DEFLATE */
  75. #define ZLIB_HEADER_CM_DEFLATE 8
  76. /** ZLIB header preset dictionary flag bit */
  77. #define ZLIB_HEADER_FDICT_BIT 13
  78. /** ZLIB ADLER32 length (in bits) */
  79. #define ZLIB_ADLER32_BITS 32
  80. /** A Huffman-coded set of symbols of a given length */
  81. struct deflate_huf_symbols {
  82. /** Length of Huffman-coded symbols */
  83. uint8_t bits;
  84. /** Shift to normalise symbols of this length to 16 bits */
  85. uint8_t shift;
  86. /** Number of Huffman-coded symbols having this length */
  87. uint16_t freq;
  88. /** First symbol of this length (normalised to 16 bits)
  89. *
  90. * Stored as a 32-bit value to allow the value 0x10000 to be
  91. * used for empty sets of symbols longer than the maximum
  92. * utilised length.
  93. */
  94. uint32_t start;
  95. /** Raw symbols having this length */
  96. uint16_t *raw;
  97. };
  98. /** A Huffman-coded alphabet */
  99. struct deflate_alphabet {
  100. /** Huffman-coded symbol set for each length */
  101. struct deflate_huf_symbols huf[DEFLATE_HUFFMAN_BITS];
  102. /** Quick lookup table */
  103. uint8_t lookup[ 1 << DEFLATE_HUFFMAN_QL_BITS ];
  104. /** Raw symbols
  105. *
  106. * Ordered by Huffman-coded symbol length, then by symbol
  107. * value. This field has a variable length.
  108. */
  109. uint16_t raw[0];
  110. };
  111. /** A static Huffman alphabet length pattern */
  112. struct deflate_static_length_pattern {
  113. /** Length pair */
  114. uint8_t fill;
  115. /** Repetition count */
  116. uint8_t count;
  117. } __attribute__ (( packed ));
  118. /** Decompressor */
  119. struct deflate {
  120. /** Resume point
  121. *
  122. * Used as the target of a computed goto to jump to the
  123. * appropriate point within the state machine.
  124. */
  125. void *resume;
  126. /** Format */
  127. enum deflate_format format;
  128. /** Accumulator */
  129. uint32_t accumulator;
  130. /** Bit-reversed accumulator
  131. *
  132. * Don't ask.
  133. */
  134. uint32_t rotalumucca;
  135. /** Number of bits within the accumulator */
  136. unsigned int bits;
  137. /** Current block header */
  138. unsigned int header;
  139. /** Remaining length of data (e.g. within a literal block) */
  140. size_t remaining;
  141. /** Current length index within a set of code lengths */
  142. unsigned int length_index;
  143. /** Target length index within a set of code lengths */
  144. unsigned int length_target;
  145. /** Current length within a set of code lengths */
  146. unsigned int length;
  147. /** Number of extra bits required */
  148. unsigned int extra_bits;
  149. /** Length of a duplicated string */
  150. size_t dup_len;
  151. /** Distance of a duplicated string */
  152. size_t dup_distance;
  153. /** Literal/length Huffman alphabet */
  154. struct deflate_alphabet litlen;
  155. /** Literal/length raw symbols
  156. *
  157. * Must immediately follow the literal/length Huffman alphabet.
  158. */
  159. uint16_t litlen_raw[ DEFLATE_LITLEN_MAX_CODE + 1 ];
  160. /** Number of symbols in the literal/length Huffman alphabet */
  161. unsigned int litlen_count;
  162. /** Distance and code length Huffman alphabet
  163. *
  164. * The code length Huffman alphabet has a maximum Huffman
  165. * symbol length of 7 and a maximum code value of 18, and is
  166. * thus strictly smaller than the distance Huffman alphabet.
  167. * Since we never need both alphabets simultaneously, we can
  168. * reuse the storage space for the distance alphabet to
  169. * temporarily hold the code length alphabet.
  170. */
  171. struct deflate_alphabet distance_codelen;
  172. /** Distance and code length raw symbols
  173. *
  174. * Must immediately follow the distance and code length
  175. * Huffman alphabet.
  176. */
  177. uint16_t distance_codelen_raw[ DEFLATE_DISTANCE_MAX_CODE + 1 ];
  178. /** Number of symbols in the distance Huffman alphabet */
  179. unsigned int distance_count;
  180. /** Huffman code lengths
  181. *
  182. * The literal/length and distance code lengths are
  183. * constructed as a single set of lengths.
  184. *
  185. * The code length Huffman alphabet has a maximum code value
  186. * of 18 and the set of lengths is thus strictly smaller than
  187. * the combined literal/length and distance set of lengths.
  188. * Since we never need both alphabets simultaneously, we can
  189. * reuse the storage space for the literal/length and distance
  190. * code lengths to temporarily hold the code length code
  191. * lengths.
  192. */
  193. uint8_t lengths[ ( ( DEFLATE_LITLEN_MAX_CODE + 1 ) +
  194. ( DEFLATE_DISTANCE_MAX_CODE + 1 ) +
  195. 1 /* round up */ ) / 2 ];
  196. };
  197. /** A chunk of data */
  198. struct deflate_chunk {
  199. /** Data */
  200. userptr_t data;
  201. /** Current offset */
  202. size_t offset;
  203. /** Length of data */
  204. size_t len;
  205. };
  206. /**
  207. * Initialise chunk of data
  208. *
  209. * @v chunk Chunk of data to initialise
  210. * @v data Data
  211. * @v offset Starting offset
  212. * @v len Length
  213. */
  214. static inline __attribute__ (( always_inline )) void
  215. deflate_chunk_init ( struct deflate_chunk *chunk, userptr_t data,
  216. size_t offset, size_t len ) {
  217. chunk->data = data;
  218. chunk->offset = offset;
  219. chunk->len = len;
  220. }
  221. /**
  222. * Check if decompression has finished
  223. *
  224. * @v deflate Decompressor
  225. * @ret finished Decompression has finished
  226. */
  227. static inline int deflate_finished ( struct deflate *deflate ) {
  228. return ( deflate->resume == NULL );
  229. }
  230. extern void deflate_init ( struct deflate *deflate,
  231. enum deflate_format format );
  232. extern int deflate_inflate ( struct deflate *deflate,
  233. struct deflate_chunk *in,
  234. struct deflate_chunk *out );
  235. #endif /* _IPXE_DEFLATE_H */