diff --git a/include/inflate.h b/include/inflate.h index 9cb5705806447ff4864568316c847224277be278..1afc34a6721e194a06d779df6a38d443373684fe 100644 --- a/include/inflate.h +++ b/include/inflate.h @@ -1,5 +1,5 @@ -#ifndef _INFLATE_C_ -#define _INFLATE_C_ +#ifndef _INFLATE_H_ +#define _INFLATE_H_ /* Maximum length for any Huffman code */ #define MAX_LENGTH 15 @@ -22,6 +22,9 @@ typedef struct huffman { } huffman_t; +extern huffman_t HUFFMAN_FIXED_DISTS; +extern huffman_t HUFFMAN_FIXED; + /* Reset the position state variable */ void reset_pos(); diff --git a/src/inflate/inflate.c b/src/inflate/inflate.c index 5e2fa372e3d7535215c9e2a958c55a183c5a76f1..c75a2fa8e459c9979bd54134f51841cc29ce2aca 100644 --- a/src/inflate/inflate.c +++ b/src/inflate/inflate.c @@ -7,6 +7,37 @@ #include <inflate.h> +/* Conversion tables for lengths + * To index into lengths, use (value_read - LENGTH_OFFSET) + * We may have to read additional bits; check LEN_ADDITIONAL for how many + */ +#define LENGTH_OFFSET 257 +int LEN_TABLE[29] = { + 3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31, 35, 43, 51, + 59, 67, 83, 99, 115, 131, 163, 195, 227, 258 +}; + +int LEN_ADDITIONAL[29] = { + 0, 0, 0, 0, 0, 0, 0, 0, + 1, 1, 1, 1, 2, 2, 2, 2, + 3, 3, 3, 3, 4, 4, 4, 4, + 5, 5, 5, 5, 0 +}; + +/* Conversion tables for distance codes + * This can be indexed into directly with the distance code. + * Again, we may have to read additional bits to get the distance. + */ +int DIST_TABLE[30] = { + 1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193, 257, 385, 513, + 769, 1025, 1537, 2049, 3073, 4097, 6145, 8193, 12289, 16385, 24577 +}; +int DIST_ADDITIONAL[30] = { + 0, 0, 0, 0, 1, 1, 2, 2, + 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, + 9, 9, 10, 10, 11, 11, 12, 12, 13, 13 +}; + /* Types of huffman codes */ #define FIXED 1 #define DYNAMIC 2 @@ -48,8 +79,7 @@ int _FIXED_9[112] = {144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 221, 222, 223, 224, 225, 226, 227, 228, 229, 230, 231, 232, 233, 234, 235, 236, 237, 238, 239, 240, 241, 242, 243, 244, 245, 246, 247, 248, 249, 250, 251, 252, 253, - 254, 255 -}; + 254, 255}; /* Note that we only have codes of length 7, 8, 9 */ huffman_t HUFFMAN_FIXED = { @@ -87,37 +117,6 @@ huffman_t HUFFMAN_FIXED_DISTS = { NO_CODE, NO_CODE, NO_CODE, NO_CODE} }; -/* Conversion tables for lengths - * To index into lengths, use (value_read - LENGTH_OFFSET) - * We may have to read additional bits; check LEN_ADDITIONAL for how many - */ -#define LENGTH_OFFSET 257 -int LEN_TABLE[29] = { - 3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31, 35, 43, 51, - 59, 67, 83, 99, 115, 131, 163, 195, 227, 258 -}; - -int LEN_ADDITIONAL[29] = { - 0, 0, 0, 0, 0, 0, 0, 0, - 1, 1, 1, 1, 2, 2, 2, 2, - 3, 3, 3, 3, 4, 4, 4, 4, - 5, 5, 5, 5, 0 -}; - -/* Conversion tables for distance codes - * This can be indexed into directly with the distance code. - * Again, we may have to read additional bits to get the distance. - */ -int DIST_TABLE[30] = { - 1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193, 257, 385, 513, - 769, 1025, 1537, 2049, 3073, 4097, 6145, 8193, 12289, 16385, 24577 -}; -int DIST_ADDITIONAL[30] = { - 0, 0, 0, 0, 1, 1, 2, 2, - 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, - 9, 9, 10, 10, 11, 11, 12, 12, 13, 13 -}; - /* * Constants for DYNAMIC code type */ diff --git a/tests/inflate_test.cpp b/tests/inflate_test.cpp index 53fbf797d3aba71d0fd761a03a274ac20bffa92b..9b69af07ef7e0028daaaa9de896380bd850cb1c3 100644 --- a/tests/inflate_test.cpp +++ b/tests/inflate_test.cpp @@ -147,11 +147,62 @@ TEST (HuffmanTests, TestMakeHuffmanZeroLens) { destroy_huffman(hf); } -// TODO -TEST (HuffmanTests, TestReadChunk) { +/* + * Test reading a sequence of literals/distances from a buffer. + * In this test, the FIXED Huffman codes are used. + */ +TEST (HuffmanTests, TestReadChunkFixed) { + + /* + * This is the mapping of literal or length -> code. + * The DEFLATE specification defines how to map the literals + * (0, 1, ..., 287) to their codes. However, these codes are to + * be interpreted in stream order. The numbers below are the actual + * memory representation of each code, not their stream-order + * numerical value. + */ + int codes[288] = {12, 140, 76, 204, 44, 172, 108, 236, 28, 156, 92, 220, + 60, 188, 124, 252, 2, 130, 66, 194, 34, 162, 98, 226, 18, + 146, 82, 210, 50, 178, 114, 242, 10, 138, 74, 202, 42, + 170, 106, 234, 26, 154, 90, 218, 58, 186, 122, 250, 6, + 134, 70, 198, 38, 166, 102, 230, 22, 150, 86, 214, 54, + 182, 118, 246, 14, 142, 78, 206, 46, 174, 110, 238, 30, + 158, 94, 222, 62, 190, 126, 254, 1, 129, 65, 193, 33, + 161, 97, 225, 17, 145, 81, 209, 49, 177, 113, 241, 9, + 137, 73, 201, 41, 169, 105, 233, 25, 153, 89, 217, 57, + 185, 121, 249, 5, 133, 69, 197, 37, 165, 101, 229, 21, + 149, 85, 213, 53, 181, 117, 245, 13, 141, 77, 205, 45, + 173, 109, 237, 29, 157, 93, 221, 61, 189, 125, 253, 19, + 275, 147, 403, 83, 339, 211, 467, 51, 307, 179, 435, 115, + 371, 243, 499, 11, 267, 139, 395, 75, 331, 203, 459, 43, + 299, 171, 427, 107, 363, 235, 491, 27, 283, 155, 411, 91, + 347, 219, 475, 59, 315, 187, 443, 123, 379, 251, 507, 7, + 263, 135, 391, 71, 327, 199, 455, 39, 295, 167, 423, 103, + 359, 231, 487, 23, 279, 151, 407, 87, 343, 215, 471, 55, + 311, 183, 439, 119, 375, 247, 503, 15, 271, 143, 399, 79, + 335, 207, 463, 47, 303, 175, 431, 111, 367, 239, 495, 31, + 287, 159, 415, 95, 351, 223, 479, 63, 319, 191, 447, 127, + 383, 255, 511, 0, 64, 32, 96, 16, 80, 48, 112, 8, 72, 40, + 104, 24, 88, 56, 120, 4, 68, 36, 100, 20, 84, 52, 116, + 3, 131, 67, 195, 35, 163, 99, 227}; + + /* Test reading all possible literal-lengths */ + for (int i = 0; i < 288; i++) { + reset_pos(); + + int code = codes[i]; + + unsigned char buf[2]; + buf[0] = code & 255; + buf[1] = code >> 8; + + ASSERT_EQ (read_chunk((char *) buf, HUFFMAN_FIXED), i); + } + // TODO test fixed distance codes as well? } +// TODO TEST (HuffmanTests, TestReadLens) {} int main(int argc, char **argv) {