Mercurial > hg > dmlib
annotate src/dmzlib.c @ 1072:33c5f2bd0ed3
Error message improvement.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Mon, 02 Mar 2015 04:01:05 +0200 |
parents | dd101fda037d |
children | f317e34246db |
rev | line source |
---|---|
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
1 /* |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
2 * DMLib |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
3 * -- ZLib implementation |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
4 * Public domain zlib decode v0.2 by Sean Barrett 2006-11-18 |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
5 * Modified and reformatted for DMLib by Matti 'ccr' Hamalainen |
1053
a726c1b9a41e
Add some clarifying comments.
Matti Hamalainen <ccr@tnsp.org>
parents:
1046
diff
changeset
|
6 * |
a726c1b9a41e
Add some clarifying comments.
Matti Hamalainen <ccr@tnsp.org>
parents:
1046
diff
changeset
|
7 * For more information, refer to following RFCs: |
a726c1b9a41e
Add some clarifying comments.
Matti Hamalainen <ccr@tnsp.org>
parents:
1046
diff
changeset
|
8 * http://tools.ietf.org/html/rfc1950 - ZLIB compressed data format |
a726c1b9a41e
Add some clarifying comments.
Matti Hamalainen <ccr@tnsp.org>
parents:
1046
diff
changeset
|
9 * http://tools.ietf.org/html/rfc1951 - DEFLATE format |
a726c1b9a41e
Add some clarifying comments.
Matti Hamalainen <ccr@tnsp.org>
parents:
1046
diff
changeset
|
10 * |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
11 */ |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
12 #include "dmzlib.h" |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
13 |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
14 |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
15 #define DM_ZLIB_TMPBUF_SIZE (16 * 1024) |
1058 | 16 #define DMZLIB_ASSERT(x) //do { printf("%s: %d\n", # x, x); } while (0) // dummy |
951
1723ebe6771c
Add kludge stbi__err() implementation temporarily until the error handling
Matti Hamalainen <ccr@tnsp.org>
parents:
949
diff
changeset
|
17 |
1723ebe6771c
Add kludge stbi__err() implementation temporarily until the error handling
Matti Hamalainen <ccr@tnsp.org>
parents:
949
diff
changeset
|
18 |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
19 // @TODO: should statically initialize these for optimal thread safety |
1054 | 20 static Uint8 *dm_zdefault_length = NULL, |
21 *dm_zdefault_distance = NULL; | |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
22 |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
23 |
1054 | 24 int dmZLibInit() |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
25 { |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
26 int i; |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
27 |
1053
a726c1b9a41e
Add some clarifying comments.
Matti Hamalainen <ccr@tnsp.org>
parents:
1046
diff
changeset
|
28 // Use <= to match clearly with DEFLATE spec |
1062
d36bf7514614
Add some constants and make initialization one-time only.
Matti Hamalainen <ccr@tnsp.org>
parents:
1060
diff
changeset
|
29 if ((dm_zdefault_length = dmMalloc(DM_ZLIB_HUFF_CODES)) == NULL || |
d36bf7514614
Add some constants and make initialization one-time only.
Matti Hamalainen <ccr@tnsp.org>
parents:
1060
diff
changeset
|
30 (dm_zdefault_distance = dmMalloc(DM_ZLIB_HUFF_DIST)) == NULL) |
1054 | 31 { |
32 return dmErrorDBG(DMERR_MALLOC, | |
33 "Failed to allocate zlib decompression tables.\n"); | |
34 } | |
1053
a726c1b9a41e
Add some clarifying comments.
Matti Hamalainen <ccr@tnsp.org>
parents:
1046
diff
changeset
|
35 |
a726c1b9a41e
Add some clarifying comments.
Matti Hamalainen <ccr@tnsp.org>
parents:
1046
diff
changeset
|
36 // Literals 0 ..143: 8 bits, codes 00110000 .. 10111111 |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
37 for (i = 0; i <= 143; i++) |
960
1832ac20edb2
Clean up dmzlib and use it in stb_image.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
38 dm_zdefault_length[i] = 8; |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
39 |
1053
a726c1b9a41e
Add some clarifying comments.
Matti Hamalainen <ccr@tnsp.org>
parents:
1046
diff
changeset
|
40 // Literals 144..255: 9 bits, codes 110010000 .. 111111111 |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
41 for (; i <= 255; i++) |
960
1832ac20edb2
Clean up dmzlib and use it in stb_image.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
42 dm_zdefault_length[i] = 9; |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
43 |
1053
a726c1b9a41e
Add some clarifying comments.
Matti Hamalainen <ccr@tnsp.org>
parents:
1046
diff
changeset
|
44 // Literals 256..279: 7 bits, codes 0000000 .. 0010111 |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
45 for (; i <= 279; i++) |
960
1832ac20edb2
Clean up dmzlib and use it in stb_image.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
46 dm_zdefault_length[i] = 7; |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
47 |
1053
a726c1b9a41e
Add some clarifying comments.
Matti Hamalainen <ccr@tnsp.org>
parents:
1046
diff
changeset
|
48 // Literals 280..287: 8 bits, codes 11000000 .. 11000111 |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
49 for (; i <= 287; i++) |
960
1832ac20edb2
Clean up dmzlib and use it in stb_image.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
50 dm_zdefault_length[i] = 8; |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
51 |
1053
a726c1b9a41e
Add some clarifying comments.
Matti Hamalainen <ccr@tnsp.org>
parents:
1046
diff
changeset
|
52 // Default distances |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
53 for (i = 0; i <= 31; i++) |
960
1832ac20edb2
Clean up dmzlib and use it in stb_image.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
54 dm_zdefault_distance[i] = 5; |
1054 | 55 |
56 return DMERR_OK; | |
57 } | |
58 | |
59 | |
60 void dmZLibClose() | |
61 { | |
62 dmFree(dm_zdefault_length); | |
63 dm_zdefault_length = NULL; | |
1058 | 64 |
1054 | 65 dmFree(dm_zdefault_distance); |
66 dm_zdefault_distance = NULL; | |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
67 } |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
68 |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
69 |
998
4a68f8d0adc5
Rename bitreverse functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
997
diff
changeset
|
70 static inline int dmBitReverse16(int n) |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
71 { |
1053
a726c1b9a41e
Add some clarifying comments.
Matti Hamalainen <ccr@tnsp.org>
parents:
1046
diff
changeset
|
72 // "Reverse" a 16bit word through bitshifts |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
73 n = ((n & 0xAAAA) >> 1) | ((n & 0x5555) << 1); |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
74 n = ((n & 0xCCCC) >> 2) | ((n & 0x3333) << 2); |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
75 n = ((n & 0xF0F0) >> 4) | ((n & 0x0F0F) << 4); |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
76 n = ((n & 0xFF00) >> 8) | ((n & 0x00FF) << 8); |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
77 return n; |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
78 } |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
79 |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
80 |
1053
a726c1b9a41e
Add some clarifying comments.
Matti Hamalainen <ccr@tnsp.org>
parents:
1046
diff
changeset
|
81 // To bit reverse N bits, reverse 16 and shift |
a726c1b9a41e
Add some clarifying comments.
Matti Hamalainen <ccr@tnsp.org>
parents:
1046
diff
changeset
|
82 // e.g. 11 bits, bit reverse and shift away 5 |
a726c1b9a41e
Add some clarifying comments.
Matti Hamalainen <ccr@tnsp.org>
parents:
1046
diff
changeset
|
83 static inline int dmBitReverseN(int v, const int bits) |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
84 { |
1058 | 85 DMZLIB_ASSERT(bits <= 16); |
998
4a68f8d0adc5
Rename bitreverse functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
997
diff
changeset
|
86 return dmBitReverse16(v) >> (16 - bits); |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
87 } |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
88 |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
89 |
960
1832ac20edb2
Clean up dmzlib and use it in stb_image.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
90 static int dmZLibBuildHuffmanTables(DMZHuffmanContext * ctx, const Uint8 * sizelist, const int num) |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
91 { |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
92 int i, k = 0; |
1059 | 93 int code, nextCode[16], sizes[17]; |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
94 |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
95 memset(sizes, 0, sizeof(sizes)); |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
96 memset(ctx->fast, 0, sizeof(ctx->fast)); |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
97 |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
98 for (i = 0; i < num; i++) |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
99 sizes[sizelist[i]]++; |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
100 |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
101 sizes[0] = 0; |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
102 for (i = 1; i < 16; i++) |
954 | 103 { |
104 if (sizes[i] > (1 << i)) | |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
105 { |
1046
7e54b2d08ce7
Add special debug error message function/macro that can be
Matti Hamalainen <ccr@tnsp.org>
parents:
1004
diff
changeset
|
106 return dmErrorDBG(DMERR_INTERNAL, |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
107 "Sizes assert failed while building Huffman codes.\n"); |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
108 } |
954 | 109 } |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
110 |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
111 code = 0; |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
112 for (i = 1; i < 16; i++) |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
113 { |
1059 | 114 nextCode[i] = code; |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
115 ctx->firstCode[i] = (Uint16) code; |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
116 ctx->firstSymbol[i] = (Uint16) k; |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
117 code = (code + sizes[i]); |
954 | 118 |
119 if (sizes[i] && code - 1 >= (1 << i)) | |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
120 { |
1046
7e54b2d08ce7
Add special debug error message function/macro that can be
Matti Hamalainen <ccr@tnsp.org>
parents:
1004
diff
changeset
|
121 return dmErrorDBG(DMERR_INVALID_DATA, |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
122 "Bad Huffman code lengths.\n"); |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
123 } |
954 | 124 |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
125 ctx->maxCode[i] = code << (16 - i); // preshift for inner loop |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
126 code <<= 1; |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
127 k += sizes[i]; |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
128 } |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
129 |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
130 ctx->maxCode[16] = 0x10000; // sentinel |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
131 for (i = 0; i < num; i++) |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
132 { |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
133 int s = sizelist[i]; |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
134 if (s) |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
135 { |
1059 | 136 int c = nextCode[s] - ctx->firstCode[s] + ctx->firstSymbol[s]; |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
137 Uint16 fastv = (Uint16) ((s << 9) | i); |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
138 ctx->size[c] = (Uint8) s; |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
139 ctx->value[c] = (Uint16) i; |
1060 | 140 if (s <= DM_ZLIB_HFAST_BITS) |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
141 { |
1060 | 142 int k = dmBitReverseN(nextCode[s], s); |
143 while (k < DM_ZLIB_HFAST_SIZE) | |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
144 { |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
145 ctx->fast[k] = fastv; |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
146 k += (1 << s); |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
147 } |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
148 } |
1059 | 149 nextCode[s]++; |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
150 } |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
151 } |
964 | 152 |
1062
d36bf7514614
Add some constants and make initialization one-time only.
Matti Hamalainen <ccr@tnsp.org>
parents:
1060
diff
changeset
|
153 ctx->initialized = TRUE; |
964 | 154 return DMERR_OK; |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
155 } |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
156 |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
157 |
960
1832ac20edb2
Clean up dmzlib and use it in stb_image.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
158 static inline Uint8 dmZGet8(DMZLibContext * ctx) |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
159 { |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
160 if (ctx->zbuffer >= ctx->zbufferEnd) |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
161 return 0; |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
162 |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
163 return *ctx->zbuffer++; |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
164 } |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
165 |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
166 |
969 | 167 static void dmZFillBits(DMZLibContext * ctx) |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
168 { |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
169 do |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
170 { |
1058 | 171 DMZLIB_ASSERT(ctx->codeBuffer < (1U << ctx->numBits)); |
960
1832ac20edb2
Clean up dmzlib and use it in stb_image.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
172 ctx->codeBuffer |= dmZGet8(ctx) << ctx->numBits; |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
173 ctx->numBits += 8; |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
174 } |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
175 while (ctx->numBits <= 24); |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
176 } |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
177 |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
178 |
960
1832ac20edb2
Clean up dmzlib and use it in stb_image.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
179 static inline unsigned int dmZReceive(DMZLibContext * ctx, int n) |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
180 { |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
181 unsigned int val; |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
182 |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
183 if (ctx->numBits < n) |
969 | 184 dmZFillBits(ctx); |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
185 |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
186 val = ctx->codeBuffer & ((1 << n) - 1); |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
187 ctx->codeBuffer >>= n; |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
188 ctx->numBits -= n; |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
189 |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
190 return val; |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
191 } |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
192 |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
193 |
969 | 194 static int dmZLibHuffmanDecodeSlow(DMZLibContext * ctx, DMZHuffmanContext * huff, int *val) |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
195 { |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
196 int b, s, k; |
960
1832ac20edb2
Clean up dmzlib and use it in stb_image.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
197 *val = 0; |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
198 |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
199 // not resolved by fast table, so compute it the slow way |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
200 // use jpeg approach, which requires MSbits at top |
998
4a68f8d0adc5
Rename bitreverse functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
997
diff
changeset
|
201 k = dmBitReverse16(ctx->codeBuffer); |
1060 | 202 for (s = DM_ZLIB_HFAST_BITS + 1; ; s++) |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
203 { |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
204 if (k < huff->maxCode[s]) |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
205 break; |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
206 } |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
207 |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
208 if (s == 16) |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
209 { |
1046
7e54b2d08ce7
Add special debug error message function/macro that can be
Matti Hamalainen <ccr@tnsp.org>
parents:
1004
diff
changeset
|
210 return dmErrorDBG(DMERR_DATA_ERROR, |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
211 "Bad Huffman code.\n"); |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
212 } |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
213 |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
214 // code size is s, so: |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
215 b = (k >> (16 - s)) - huff->firstCode[s] + huff->firstSymbol[s]; |
1058 | 216 DMZLIB_ASSERT(huff->size[b] == s); |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
217 |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
218 ctx->codeBuffer >>= s; |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
219 ctx->numBits -= s; |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
220 *val = huff->value[b]; |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
221 |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
222 return DMERR_OK; |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
223 } |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
224 |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
225 |
969 | 226 static inline int dmZLibHuffmanDecode(DMZLibContext * ctx, DMZHuffmanContext * huff, int *val) |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
227 { |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
228 int b; |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
229 if (ctx->numBits < 16) |
969 | 230 dmZFillBits(ctx); |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
231 |
1060 | 232 b = huff->fast[ctx->codeBuffer & DM_ZLIB_HFAST_MASK]; |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
233 if (b) |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
234 { |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
235 int s = b >> 9; |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
236 ctx->codeBuffer >>= s; |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
237 ctx->numBits -= s; |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
238 *val = b & 511; |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
239 return DMERR_OK; |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
240 } |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
241 |
969 | 242 return dmZLibHuffmanDecodeSlow(ctx, huff, val); |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
243 } |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
244 |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
245 |
969 | 246 static int dmZLibExpand(DMZLibContext * ctx, Uint8 *zout, size_t n) |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
247 { |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
248 Uint8 *newBuf; |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
249 size_t cur, limit; |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
250 |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
251 ctx->zout = zout; |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
252 |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
253 if (!ctx->expandable) |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
254 { |
1046
7e54b2d08ce7
Add special debug error message function/macro that can be
Matti Hamalainen <ccr@tnsp.org>
parents:
1004
diff
changeset
|
255 return dmErrorDBG(DMERR_BOUNDS, |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
256 "Output buffer limit hit, and is not expandable.\n"); |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
257 } |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
258 |
969 | 259 cur = ctx->zout - ctx->zoutStart; |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
260 limit = ctx->zoutEnd - ctx->zoutStart; |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
261 |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
262 while (cur + n > limit) |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
263 limit *= 2; |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
264 |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
265 if ((newBuf = (Uint8 *) dmRealloc(ctx->zoutStart, limit)) == NULL) |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
266 { |
1046
7e54b2d08ce7
Add special debug error message function/macro that can be
Matti Hamalainen <ccr@tnsp.org>
parents:
1004
diff
changeset
|
267 return dmErrorDBG(DMERR_MALLOC, |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
268 "Could not reallocate buffer.\n"); |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
269 } |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
270 |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
271 ctx->zoutStart = newBuf; |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
272 ctx->zout = newBuf + cur; |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
273 ctx->zoutEnd = newBuf + limit; |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
274 |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
275 return DMERR_OK; |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
276 } |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
277 |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
278 |
1002 | 279 static const int dm_zlib_length_base[31] = |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
280 { |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
281 3, 4, 5, 6, 7, 8, 9, 10, 11, 13, |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
282 15, 17, 19, 23, 27, 31, 35, 43, 51, 59, |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
283 67, 83, 99, 115, 131, 163, 195, 227, 258, 0, 0 |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
284 }; |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
285 |
1002 | 286 static const int dm_zlib_length_extra[31] = |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
287 { |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
288 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
289 5, 5, 5, 5, 0, 0, 0 |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
290 }; |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
291 |
1002 | 292 static const int dm_zlib_dist_base[32] = |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
293 { |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
294 1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193, |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
295 257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145, 8193, 12289, |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
296 16385, 24577, 0, 0 |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
297 }; |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
298 |
1002 | 299 static const int dm_zlib_dist_extra[32] = |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
300 { |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
301 0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 10, |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
302 10, 11, 11, 12, 12, 13, 13 |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
303 }; |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
304 |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
305 |
1003
7a0c2fe22e60
Rename a function argument.
Matti Hamalainen <ccr@tnsp.org>
parents:
1002
diff
changeset
|
306 static int dmZLibParseHuffmanBlock(DMZLibContext * ctx) |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
307 { |
1003
7a0c2fe22e60
Rename a function argument.
Matti Hamalainen <ccr@tnsp.org>
parents:
1002
diff
changeset
|
308 Uint8 *zout = ctx->zout; |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
309 for (;;) |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
310 { |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
311 int z, ret; |
1003
7a0c2fe22e60
Rename a function argument.
Matti Hamalainen <ccr@tnsp.org>
parents:
1002
diff
changeset
|
312 if ((ret = dmZLibHuffmanDecode(ctx, &ctx->zlength, &z)) != DMERR_OK) |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
313 return ret; |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
314 |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
315 if (z < 256) |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
316 { |
1003
7a0c2fe22e60
Rename a function argument.
Matti Hamalainen <ccr@tnsp.org>
parents:
1002
diff
changeset
|
317 if (zout >= ctx->zoutEnd) |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
318 { |
1003
7a0c2fe22e60
Rename a function argument.
Matti Hamalainen <ccr@tnsp.org>
parents:
1002
diff
changeset
|
319 if ((ret = dmZLibExpand(ctx, zout, 1)) != DMERR_OK) |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
320 return ret; |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
321 |
1003
7a0c2fe22e60
Rename a function argument.
Matti Hamalainen <ccr@tnsp.org>
parents:
1002
diff
changeset
|
322 zout = ctx->zout; |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
323 } |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
324 *zout++ = (Uint8) z; |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
325 } |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
326 else |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
327 { |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
328 Uint8 *p; |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
329 int len, dist; |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
330 if (z == 256) |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
331 { |
1003
7a0c2fe22e60
Rename a function argument.
Matti Hamalainen <ccr@tnsp.org>
parents:
1002
diff
changeset
|
332 ctx->zout = zout; |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
333 return DMERR_OK; |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
334 } |
1069 | 335 |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
336 z -= 257; |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
337 |
1002 | 338 len = dm_zlib_length_base[z]; |
339 if (dm_zlib_length_extra[z]) | |
1003
7a0c2fe22e60
Rename a function argument.
Matti Hamalainen <ccr@tnsp.org>
parents:
1002
diff
changeset
|
340 len += dmZReceive(ctx, dm_zlib_length_extra[z]); |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
341 |
1003
7a0c2fe22e60
Rename a function argument.
Matti Hamalainen <ccr@tnsp.org>
parents:
1002
diff
changeset
|
342 if ((ret = dmZLibHuffmanDecode(ctx, &ctx->zdistance, &z)) != DMERR_OK) |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
343 return ret; |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
344 |
1002 | 345 dist = dm_zlib_dist_base[z]; |
346 if (dm_zlib_dist_extra[z]) | |
1003
7a0c2fe22e60
Rename a function argument.
Matti Hamalainen <ccr@tnsp.org>
parents:
1002
diff
changeset
|
347 dist += dmZReceive(ctx, dm_zlib_dist_extra[z]); |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
348 |
1003
7a0c2fe22e60
Rename a function argument.
Matti Hamalainen <ccr@tnsp.org>
parents:
1002
diff
changeset
|
349 if (zout - ctx->zoutStart < dist) |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
350 { |
1046
7e54b2d08ce7
Add special debug error message function/macro that can be
Matti Hamalainen <ccr@tnsp.org>
parents:
1004
diff
changeset
|
351 return dmErrorDBG(DMERR_DATA_ERROR, |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
352 "Bad Huffman block distance.\n"); |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
353 } |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
354 |
1003
7a0c2fe22e60
Rename a function argument.
Matti Hamalainen <ccr@tnsp.org>
parents:
1002
diff
changeset
|
355 if (zout + len > ctx->zoutEnd) |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
356 { |
1003
7a0c2fe22e60
Rename a function argument.
Matti Hamalainen <ccr@tnsp.org>
parents:
1002
diff
changeset
|
357 if ((ret = dmZLibExpand(ctx, zout, len)) != DMERR_OK) |
1001
198156b930d7
Fix error checking and propagation. 10L.
Matti Hamalainen <ccr@tnsp.org>
parents:
1000
diff
changeset
|
358 return ret; |
1003
7a0c2fe22e60
Rename a function argument.
Matti Hamalainen <ccr@tnsp.org>
parents:
1002
diff
changeset
|
359 zout = ctx->zout; |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
360 } |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
361 p = (Uint8 *) (zout - dist); |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
362 if (dist == 1) |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
363 { // run of one byte; common in images. |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
364 Uint8 v = *p; |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
365 do { *zout++ = v; } while (--len); |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
366 } |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
367 else |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
368 { |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
369 do { *zout++ = *p++; } while (--len); |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
370 } |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
371 } |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
372 } |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
373 } |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
374 |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
375 |
1054 | 376 #define DM_ZLIB_NCODELENGTH_SIZES 19 |
377 | |
378 static const Uint8 dm_zlib_length_dezigzag[DM_ZLIB_NCODELENGTH_SIZES] = | |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
379 { |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
380 16, 17, 18, 0, 8, 7, 9, |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
381 6 , 10, 5 , 11, 4, 12, 3, |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
382 13, 2 , 14, 1 , 15 |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
383 }; |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
384 |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
385 |
999
b3b8794c4915
Rename a function argument.
Matti Hamalainen <ccr@tnsp.org>
parents:
998
diff
changeset
|
386 static int dmZLibComputeHuffmanCodes(DMZLibContext * ctx) |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
387 { |
1069 | 388 DMZHuffmanContext codeLengthCtx; |
1054 | 389 Uint8 codeLengthSizes[DM_ZLIB_NCODELENGTH_SIZES]; |
1069 | 390 Uint8 codeLengths[288 + 32 + 137]; // padding for maximum single op |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
391 int i, n, ret; |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
392 |
1069 | 393 // Get table sizes |
394 int hlit = dmZReceive(ctx, 5) + 257; // 288 = X | |
395 int hdist = dmZReceive(ctx, 5) + 1; // 32 = X | |
396 int hclen = dmZReceive(ctx, 4) + 4; // 19 = DM_ZLIB_NCODELENGTH_SIZES | |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
397 |
1069 | 398 if (hlit > 286 || hdist > 30) |
399 { | |
400 return dmErrorDBG(DMERR_INVALID_DATA, | |
401 "Too many Huffman length or distance symbols (%d, %d)", | |
402 hlit, hdist); | |
403 } | |
404 | |
405 // Get lengths table (uninitialized entries should be set to 0) | |
960
1832ac20edb2
Clean up dmzlib and use it in stb_image.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
406 memset(codeLengthSizes, 0, sizeof(codeLengthSizes)); |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
407 for (i = 0; i < hclen; i++) |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
408 { |
999
b3b8794c4915
Rename a function argument.
Matti Hamalainen <ccr@tnsp.org>
parents:
998
diff
changeset
|
409 int s = dmZReceive(ctx, 3); |
1000 | 410 codeLengthSizes[dm_zlib_length_dezigzag[i]] = (Uint8) s; |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
411 } |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
412 |
1069 | 413 // Inflate the code lengths table |
414 if ((ret = dmZLibBuildHuffmanTables(&codeLengthCtx, | |
415 codeLengthSizes, DM_ZLIB_NCODELENGTH_SIZES)) != DMERR_OK) | |
416 { | |
417 return dmErrorDBG(ret, | |
418 "Invalid code lengths set.\n"); | |
419 } | |
955 | 420 |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
421 n = 0; |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
422 while (n < hlit + hdist) |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
423 { |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
424 int c; |
1069 | 425 if ((ret = dmZLibHuffmanDecode(ctx, &codeLengthCtx, &c)) != DMERR_OK) |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
426 return ret; |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
427 |
1058 | 428 DMZLIB_ASSERT(c >= 0 && c < DM_ZLIB_NCODELENGTH_SIZES); |
955 | 429 |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
430 if (c < 16) |
960
1832ac20edb2
Clean up dmzlib and use it in stb_image.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
431 codeLengths[n++] = (Uint8) c; |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
432 else |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
433 if (c == 16) |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
434 { |
1069 | 435 int bv = dmZReceive(ctx, 2) + 3; |
436 if (n == 0) | |
437 { | |
438 return dmErrorDBG(DMERR_INVALID_DATA, | |
439 "Invalid bit length repeat.\n"); | |
440 } | |
441 memset(codeLengths + n, codeLengths[n - 1], bv); | |
442 n += bv; | |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
443 } |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
444 else |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
445 if (c == 17) |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
446 { |
1069 | 447 int bv = dmZReceive(ctx, 3) + 3; |
448 memset(codeLengths + n, 0, bv); | |
449 n += bv; | |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
450 } |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
451 else |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
452 { |
1069 | 453 int bv = dmZReceive(ctx, 7) + 11; |
454 memset(codeLengths + n, 0, bv); | |
455 n += bv; | |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
456 } |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
457 } |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
458 |
1069 | 459 if (codeLengths[256] == 0) |
460 { | |
461 return dmErrorDBG(DMERR_DATA_ERROR, | |
462 "Invalid code -- missing end-of-block.\n"); | |
463 } | |
464 | |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
465 if (n != hlit + hdist) |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
466 { |
1046
7e54b2d08ce7
Add special debug error message function/macro that can be
Matti Hamalainen <ccr@tnsp.org>
parents:
1004
diff
changeset
|
467 return dmErrorDBG(DMERR_DATA_ERROR, |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
468 "Bad huffman codelengths.\n"); |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
469 } |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
470 |
1069 | 471 // Build the code tables |
999
b3b8794c4915
Rename a function argument.
Matti Hamalainen <ccr@tnsp.org>
parents:
998
diff
changeset
|
472 if ((ret = dmZLibBuildHuffmanTables(&ctx->zlength, codeLengths, hlit)) != DMERR_OK) |
1070 | 473 { |
474 return dmErrorDBG(DMERR_DATA_ERROR, | |
475 "Invalid literal/lengths set.\n"); | |
476 } | |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
477 |
999
b3b8794c4915
Rename a function argument.
Matti Hamalainen <ccr@tnsp.org>
parents:
998
diff
changeset
|
478 if ((ret = dmZLibBuildHuffmanTables(&ctx->zdistance, codeLengths + hlit, hdist)) != DMERR_OK) |
1070 | 479 { |
480 return dmErrorDBG(DMERR_DATA_ERROR, | |
481 "Invalid literal distances set.\n"); | |
482 } | |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
483 |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
484 return DMERR_OK; |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
485 } |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
486 |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
487 |
990 | 488 static int dmZLibParseUncompressedBlock(DMZLibContext * ctx) |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
489 { |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
490 Uint8 header[4]; |
1071 | 491 size_t len, nlen, k, ret; |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
492 |
997 | 493 // "Any bits of input up to the next byte boundary are ignored." |
965
df8d2ad98f7d
Rename a function argument.
Matti Hamalainen <ccr@tnsp.org>
parents:
964
diff
changeset
|
494 if (ctx->numBits & 7) |
997 | 495 dmZReceive(ctx, ctx->numBits & 7); |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
496 |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
497 // drain the bit-packed data into header |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
498 k = 0; |
965
df8d2ad98f7d
Rename a function argument.
Matti Hamalainen <ccr@tnsp.org>
parents:
964
diff
changeset
|
499 while (ctx->numBits > 0) |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
500 { |
965
df8d2ad98f7d
Rename a function argument.
Matti Hamalainen <ccr@tnsp.org>
parents:
964
diff
changeset
|
501 header[k++] = (Uint8) (ctx->codeBuffer & 255); // suppress MSVC run-time check |
df8d2ad98f7d
Rename a function argument.
Matti Hamalainen <ccr@tnsp.org>
parents:
964
diff
changeset
|
502 ctx->codeBuffer >>= 8; |
df8d2ad98f7d
Rename a function argument.
Matti Hamalainen <ccr@tnsp.org>
parents:
964
diff
changeset
|
503 ctx->numBits -= 8; |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
504 } |
1058 | 505 DMZLIB_ASSERT(ctx->numBits == 0); |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
506 |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
507 // now fill header the normal way |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
508 while (k < 4) |
965
df8d2ad98f7d
Rename a function argument.
Matti Hamalainen <ccr@tnsp.org>
parents:
964
diff
changeset
|
509 header[k++] = dmZGet8(ctx); |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
510 |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
511 len = (header[1] << 8) | header[0]; |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
512 nlen = (header[3] << 8) | header[2]; |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
513 |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
514 if (nlen != (len ^ 0xffff)) |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
515 { |
1046
7e54b2d08ce7
Add special debug error message function/macro that can be
Matti Hamalainen <ccr@tnsp.org>
parents:
1004
diff
changeset
|
516 return dmErrorDBG(DMERR_DATA_ERROR, |
992
929e43afbdb1
Improve error message slightly.
Matti Hamalainen <ccr@tnsp.org>
parents:
991
diff
changeset
|
517 "Compressed data corrupt %04x :: %04x [%04x].\n", |
929e43afbdb1
Improve error message slightly.
Matti Hamalainen <ccr@tnsp.org>
parents:
991
diff
changeset
|
518 nlen, len, len ^ 0xffff); |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
519 } |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
520 |
965
df8d2ad98f7d
Rename a function argument.
Matti Hamalainen <ccr@tnsp.org>
parents:
964
diff
changeset
|
521 if (ctx->zbuffer + len > ctx->zbufferEnd) |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
522 { |
1046
7e54b2d08ce7
Add special debug error message function/macro that can be
Matti Hamalainen <ccr@tnsp.org>
parents:
1004
diff
changeset
|
523 return dmErrorDBG(DMERR_BOUNDS, |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
524 "Read past buffer, probably corrupt compressed data.\n"); |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
525 } |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
526 |
968 | 527 if (ctx->zout + len > ctx->zoutEnd && |
969 | 528 (ret = dmZLibExpand(ctx, ctx->zout, len)) != DMERR_OK) |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
529 { |
1046
7e54b2d08ce7
Add special debug error message function/macro that can be
Matti Hamalainen <ccr@tnsp.org>
parents:
1004
diff
changeset
|
530 return dmErrorDBG(DMERR_DATA_ERROR, |
997 | 531 "Could not expand output buffer: %d, %s\n", |
968 | 532 ret, dmErrorStr(ret)); |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
533 } |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
534 |
965
df8d2ad98f7d
Rename a function argument.
Matti Hamalainen <ccr@tnsp.org>
parents:
964
diff
changeset
|
535 memcpy(ctx->zout, ctx->zbuffer, len); |
df8d2ad98f7d
Rename a function argument.
Matti Hamalainen <ccr@tnsp.org>
parents:
964
diff
changeset
|
536 ctx->zbuffer += len; |
df8d2ad98f7d
Rename a function argument.
Matti Hamalainen <ccr@tnsp.org>
parents:
964
diff
changeset
|
537 ctx->zout += len; |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
538 |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
539 return DMERR_OK; |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
540 } |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
541 |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
542 |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
543 int dmZLibDecode(DMZLibContext * ctx) |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
544 { |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
545 int final, type, ret; |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
546 |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
547 ctx->numBits = 0; |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
548 ctx->codeBuffer = 0; |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
549 do |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
550 { |
960
1832ac20edb2
Clean up dmzlib and use it in stb_image.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
551 final = dmZReceive(ctx, 1); |
1832ac20edb2
Clean up dmzlib and use it in stb_image.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
552 type = dmZReceive(ctx, 2); |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
553 if (type == 0) |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
554 { |
990 | 555 if ((ret = dmZLibParseUncompressedBlock(ctx)) != DMERR_OK) |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
556 return ret; |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
557 } |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
558 else |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
559 if (type == 3) |
1072
33c5f2bd0ed3
Error message improvement.
Matti Hamalainen <ccr@tnsp.org>
parents:
1071
diff
changeset
|
560 { |
33c5f2bd0ed3
Error message improvement.
Matti Hamalainen <ccr@tnsp.org>
parents:
1071
diff
changeset
|
561 return dmError(DMERR_INVALID_DATA, |
33c5f2bd0ed3
Error message improvement.
Matti Hamalainen <ccr@tnsp.org>
parents:
1071
diff
changeset
|
562 "Invalid zlib block type.\n"); |
33c5f2bd0ed3
Error message improvement.
Matti Hamalainen <ccr@tnsp.org>
parents:
1071
diff
changeset
|
563 } |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
564 else |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
565 { |
1062
d36bf7514614
Add some constants and make initialization one-time only.
Matti Hamalainen <ccr@tnsp.org>
parents:
1060
diff
changeset
|
566 if (type == 1 && !ctx->zlength.initialized) |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
567 { |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
568 // use fixed code lengths |
1062
d36bf7514614
Add some constants and make initialization one-time only.
Matti Hamalainen <ccr@tnsp.org>
parents:
1060
diff
changeset
|
569 if ((ret = dmZLibBuildHuffmanTables(&ctx->zlength, dm_zdefault_length, DM_ZLIB_HUFF_CODES)) != DMERR_OK) |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
570 return ret; |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
571 |
1062
d36bf7514614
Add some constants and make initialization one-time only.
Matti Hamalainen <ccr@tnsp.org>
parents:
1060
diff
changeset
|
572 if ((ret = dmZLibBuildHuffmanTables(&ctx->zdistance, dm_zdefault_distance, DM_ZLIB_HUFF_DIST)) != DMERR_OK) |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
573 return ret; |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
574 } |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
575 else |
960
1832ac20edb2
Clean up dmzlib and use it in stb_image.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
576 if ((ret = dmZLibComputeHuffmanCodes(ctx)) != DMERR_OK) |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
577 return ret; |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
578 |
960
1832ac20edb2
Clean up dmzlib and use it in stb_image.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
579 if ((ret = dmZLibParseHuffmanBlock(ctx)) != DMERR_OK) |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
580 return ret; |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
581 } |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
582 } |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
583 while (!final); |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
584 |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
585 return DMERR_OK; |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
586 } |
997 | 587 |
588 | |
589 int dmZLibParseHeader(DMZLibContext * ctx, BOOL checkPNG) | |
590 { | |
591 // See http://tools.ietf.org/html/rfc1950 | |
592 int cmf = dmZGet8(ctx); // Compression method and flags | |
593 int flags = dmZGet8(ctx); // Flags | |
594 int cmethod = (cmf & 15); | |
595 int cinfo = (cmf >> 4) & 15; | |
596 ctx->window = 1 << (8 + cinfo); // Window size (not used at the moment) | |
597 | |
598 // "The FCHECK value must be such that CMF and FLG, when viewed as | |
599 // a 16-bit unsigned integer stored in MSB order (CMF*256 + FLG), | |
600 // is a multiple of 31." | |
601 if ((cmf * 256 + flags) % 31 != 0) | |
602 { | |
1046
7e54b2d08ce7
Add special debug error message function/macro that can be
Matti Hamalainen <ccr@tnsp.org>
parents:
1004
diff
changeset
|
603 return dmErrorDBG(DMERR_INVALID_DATA, |
997 | 604 "Bad zlib header."); |
605 } | |
606 | |
607 // We only support compression method 8 | |
608 if (cmethod != 8) | |
609 { | |
1046
7e54b2d08ce7
Add special debug error message function/macro that can be
Matti Hamalainen <ccr@tnsp.org>
parents:
1004
diff
changeset
|
610 return dmErrorDBG(DMERR_INVALID_DATA, |
997 | 611 "Bad or unsupported zlib compression method %d.\n", |
612 cmethod); | |
613 } | |
614 | |
615 if (checkPNG && (flags & 32)) | |
616 { | |
617 // preset dictionary not allowed in png | |
1046
7e54b2d08ce7
Add special debug error message function/macro that can be
Matti Hamalainen <ccr@tnsp.org>
parents:
1004
diff
changeset
|
618 return dmErrorDBG(DMERR_NOT_SUPPORTED, |
997 | 619 "Preset dictionary not allowed in PNG.\n"); |
620 } | |
621 return DMERR_OK; | |
622 } |