Mercurial > hg > dmlib
annotate src/dmzlib.c @ 1058:e51ec592bfb6
Rename a macro.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Sun, 01 Mar 2015 23:28:40 +0200 |
parents | d98fcb10df6a |
children | df35244490e8 |
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 |
1054 | 29 if ((dm_zdefault_length = dmMalloc(288)) == NULL || |
30 (dm_zdefault_distance = dmMalloc(32)) == NULL) | |
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; |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
93 int code, next_code[16], sizes[17]; |
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 { |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
114 next_code[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 { |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
136 int c = next_code[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; |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
140 if (s <= STBI__ZFAST_BITS) |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
141 { |
998
4a68f8d0adc5
Rename bitreverse functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
997
diff
changeset
|
142 int k = dmBitReverseN(next_code[s], s); |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
143 while (k < STBI__ZFAST_SIZE) |
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 } |
997 | 149 next_code[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 |
153 return DMERR_OK; | |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
154 } |
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 |
960
1832ac20edb2
Clean up dmzlib and use it in stb_image.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
157 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
|
158 { |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
159 if (ctx->zbuffer >= ctx->zbufferEnd) |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
160 return 0; |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
161 |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
162 return *ctx->zbuffer++; |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
163 } |
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 |
969 | 166 static void dmZFillBits(DMZLibContext * ctx) |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
167 { |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
168 do |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
169 { |
1058 | 170 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
|
171 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
|
172 ctx->numBits += 8; |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
173 } |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
174 while (ctx->numBits <= 24); |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
175 } |
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 |
960
1832ac20edb2
Clean up dmzlib and use it in stb_image.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
178 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
|
179 { |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
180 unsigned int val; |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
181 |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
182 if (ctx->numBits < n) |
969 | 183 dmZFillBits(ctx); |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
184 |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
185 val = ctx->codeBuffer & ((1 << n) - 1); |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
186 ctx->codeBuffer >>= n; |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
187 ctx->numBits -= n; |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
188 |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
189 return val; |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
190 } |
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 |
969 | 193 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
|
194 { |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
195 int b, s, k; |
960
1832ac20edb2
Clean up dmzlib and use it in stb_image.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
196 *val = 0; |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
197 |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
198 // 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
|
199 // use jpeg approach, which requires MSbits at top |
998
4a68f8d0adc5
Rename bitreverse functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
997
diff
changeset
|
200 k = dmBitReverse16(ctx->codeBuffer); |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
201 for (s = STBI__ZFAST_BITS + 1; ; s++) |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
202 { |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
203 if (k < huff->maxCode[s]) |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
204 break; |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
205 } |
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 if (s == 16) |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
208 { |
1046
7e54b2d08ce7
Add special debug error message function/macro that can be
Matti Hamalainen <ccr@tnsp.org>
parents:
1004
diff
changeset
|
209 return dmErrorDBG(DMERR_DATA_ERROR, |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
210 "Bad Huffman code.\n"); |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
211 } |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
212 |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
213 // code size is s, so: |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
214 b = (k >> (16 - s)) - huff->firstCode[s] + huff->firstSymbol[s]; |
1058 | 215 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
|
216 |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
217 ctx->codeBuffer >>= s; |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
218 ctx->numBits -= s; |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
219 *val = huff->value[b]; |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
220 |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
221 return DMERR_OK; |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
222 } |
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 |
969 | 225 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
|
226 { |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
227 int b; |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
228 if (ctx->numBits < 16) |
969 | 229 dmZFillBits(ctx); |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
230 |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
231 b = huff->fast[ctx->codeBuffer & STBI__ZFAST_MASK]; |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
232 if (b) |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
233 { |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
234 int s = b >> 9; |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
235 ctx->codeBuffer >>= s; |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
236 ctx->numBits -= s; |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
237 *val = b & 511; |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
238 return DMERR_OK; |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
239 } |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
240 |
969 | 241 return dmZLibHuffmanDecodeSlow(ctx, huff, val); |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
242 } |
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 |
969 | 245 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
|
246 { |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
247 Uint8 *newBuf; |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
248 size_t cur, limit; |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
249 |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
250 ctx->zout = zout; |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
251 |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
252 if (!ctx->expandable) |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
253 { |
1046
7e54b2d08ce7
Add special debug error message function/macro that can be
Matti Hamalainen <ccr@tnsp.org>
parents:
1004
diff
changeset
|
254 return dmErrorDBG(DMERR_BOUNDS, |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
255 "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
|
256 } |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
257 |
969 | 258 cur = ctx->zout - ctx->zoutStart; |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
259 limit = ctx->zoutEnd - ctx->zoutStart; |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
260 |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
261 while (cur + n > limit) |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
262 limit *= 2; |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
263 |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
264 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
|
265 { |
1046
7e54b2d08ce7
Add special debug error message function/macro that can be
Matti Hamalainen <ccr@tnsp.org>
parents:
1004
diff
changeset
|
266 return dmErrorDBG(DMERR_MALLOC, |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
267 "Could not reallocate buffer.\n"); |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
268 } |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
269 |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
270 ctx->zoutStart = newBuf; |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
271 ctx->zout = newBuf + cur; |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
272 ctx->zoutEnd = newBuf + limit; |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
273 |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
274 return DMERR_OK; |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
275 } |
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 |
1002 | 278 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
|
279 { |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
280 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
|
281 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
|
282 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
|
283 }; |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
284 |
1002 | 285 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
|
286 { |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
287 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
|
288 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
|
289 }; |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
290 |
1002 | 291 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
|
292 { |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
293 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
|
294 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
|
295 16385, 24577, 0, 0 |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
296 }; |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
297 |
1002 | 298 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
|
299 { |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
300 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
|
301 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
|
302 }; |
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 |
1003
7a0c2fe22e60
Rename a function argument.
Matti Hamalainen <ccr@tnsp.org>
parents:
1002
diff
changeset
|
305 static int dmZLibParseHuffmanBlock(DMZLibContext * ctx) |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
306 { |
1003
7a0c2fe22e60
Rename a function argument.
Matti Hamalainen <ccr@tnsp.org>
parents:
1002
diff
changeset
|
307 Uint8 *zout = ctx->zout; |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
308 for (;;) |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
309 { |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
310 int z, ret; |
1003
7a0c2fe22e60
Rename a function argument.
Matti Hamalainen <ccr@tnsp.org>
parents:
1002
diff
changeset
|
311 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
|
312 return ret; |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
313 |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
314 if (z < 256) |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
315 { |
1003
7a0c2fe22e60
Rename a function argument.
Matti Hamalainen <ccr@tnsp.org>
parents:
1002
diff
changeset
|
316 if (zout >= ctx->zoutEnd) |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
317 { |
1003
7a0c2fe22e60
Rename a function argument.
Matti Hamalainen <ccr@tnsp.org>
parents:
1002
diff
changeset
|
318 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
|
319 return ret; |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
320 |
1003
7a0c2fe22e60
Rename a function argument.
Matti Hamalainen <ccr@tnsp.org>
parents:
1002
diff
changeset
|
321 zout = ctx->zout; |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
322 } |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
323 *zout++ = (Uint8) z; |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
324 } |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
325 else |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
326 { |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
327 Uint8 *p; |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
328 int len, dist; |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
329 if (z == 256) |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
330 { |
1003
7a0c2fe22e60
Rename a function argument.
Matti Hamalainen <ccr@tnsp.org>
parents:
1002
diff
changeset
|
331 ctx->zout = zout; |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
332 return DMERR_OK; |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
333 } |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
334 z -= 257; |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
335 |
1002 | 336 len = dm_zlib_length_base[z]; |
337 if (dm_zlib_length_extra[z]) | |
1003
7a0c2fe22e60
Rename a function argument.
Matti Hamalainen <ccr@tnsp.org>
parents:
1002
diff
changeset
|
338 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
|
339 |
1003
7a0c2fe22e60
Rename a function argument.
Matti Hamalainen <ccr@tnsp.org>
parents:
1002
diff
changeset
|
340 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
|
341 return ret; |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
342 |
1002 | 343 dist = dm_zlib_dist_base[z]; |
344 if (dm_zlib_dist_extra[z]) | |
1003
7a0c2fe22e60
Rename a function argument.
Matti Hamalainen <ccr@tnsp.org>
parents:
1002
diff
changeset
|
345 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
|
346 |
1003
7a0c2fe22e60
Rename a function argument.
Matti Hamalainen <ccr@tnsp.org>
parents:
1002
diff
changeset
|
347 if (zout - ctx->zoutStart < dist) |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
348 { |
1046
7e54b2d08ce7
Add special debug error message function/macro that can be
Matti Hamalainen <ccr@tnsp.org>
parents:
1004
diff
changeset
|
349 return dmErrorDBG(DMERR_DATA_ERROR, |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
350 "Bad Huffman block distance.\n"); |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
351 } |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
352 |
1003
7a0c2fe22e60
Rename a function argument.
Matti Hamalainen <ccr@tnsp.org>
parents:
1002
diff
changeset
|
353 if (zout + len > ctx->zoutEnd) |
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 ((ret = dmZLibExpand(ctx, zout, len)) != DMERR_OK) |
1001
198156b930d7
Fix error checking and propagation. 10L.
Matti Hamalainen <ccr@tnsp.org>
parents:
1000
diff
changeset
|
356 return ret; |
1003
7a0c2fe22e60
Rename a function argument.
Matti Hamalainen <ccr@tnsp.org>
parents:
1002
diff
changeset
|
357 zout = ctx->zout; |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
358 } |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
359 p = (Uint8 *) (zout - dist); |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
360 if (dist == 1) |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
361 { // 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
|
362 Uint8 v = *p; |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
363 do { *zout++ = v; } while (--len); |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
364 } |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
365 else |
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 do { *zout++ = *p++; } while (--len); |
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 } |
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 |
1054 | 374 #define DM_ZLIB_NCODELENGTH_SIZES 19 |
375 | |
376 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
|
377 { |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
378 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
|
379 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
|
380 13, 2 , 14, 1 , 15 |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
381 }; |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
382 |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
383 |
999
b3b8794c4915
Rename a function argument.
Matti Hamalainen <ccr@tnsp.org>
parents:
998
diff
changeset
|
384 static int dmZLibComputeHuffmanCodes(DMZLibContext * ctx) |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
385 { |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
386 DMZHuffmanContext z_codelength; |
1054 | 387 Uint8 codeLengths[288 + 32 + 137]; // padding for maximum single op |
388 Uint8 codeLengthSizes[DM_ZLIB_NCODELENGTH_SIZES]; | |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
389 int i, n, ret; |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
390 |
999
b3b8794c4915
Rename a function argument.
Matti Hamalainen <ccr@tnsp.org>
parents:
998
diff
changeset
|
391 int hlit = dmZReceive(ctx, 5) + 257; |
b3b8794c4915
Rename a function argument.
Matti Hamalainen <ccr@tnsp.org>
parents:
998
diff
changeset
|
392 int hdist = dmZReceive(ctx, 5) + 1; |
b3b8794c4915
Rename a function argument.
Matti Hamalainen <ccr@tnsp.org>
parents:
998
diff
changeset
|
393 int hclen = dmZReceive(ctx, 4) + 4; |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
394 |
960
1832ac20edb2
Clean up dmzlib and use it in stb_image.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
395 memset(codeLengthSizes, 0, sizeof(codeLengthSizes)); |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
396 |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
397 for (i = 0; i < hclen; i++) |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
398 { |
999
b3b8794c4915
Rename a function argument.
Matti Hamalainen <ccr@tnsp.org>
parents:
998
diff
changeset
|
399 int s = dmZReceive(ctx, 3); |
1000 | 400 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
|
401 } |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
402 |
960
1832ac20edb2
Clean up dmzlib and use it in stb_image.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
403 if ((ret = dmZLibBuildHuffmanTables(&z_codelength, codeLengthSizes, 19)) != DMERR_OK) |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
404 return ret; |
955 | 405 |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
406 n = 0; |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
407 while (n < hlit + hdist) |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
408 { |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
409 int c; |
999
b3b8794c4915
Rename a function argument.
Matti Hamalainen <ccr@tnsp.org>
parents:
998
diff
changeset
|
410 if ((ret = dmZLibHuffmanDecode(ctx, &z_codelength, &c)) != DMERR_OK) |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
411 return ret; |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
412 |
1058 | 413 DMZLIB_ASSERT(c >= 0 && c < DM_ZLIB_NCODELENGTH_SIZES); |
955 | 414 |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
415 if (c < 16) |
960
1832ac20edb2
Clean up dmzlib and use it in stb_image.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
416 codeLengths[n++] = (Uint8) c; |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
417 else |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
418 if (c == 16) |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
419 { |
999
b3b8794c4915
Rename a function argument.
Matti Hamalainen <ccr@tnsp.org>
parents:
998
diff
changeset
|
420 c = dmZReceive(ctx, 2) + 3; |
960
1832ac20edb2
Clean up dmzlib and use it in stb_image.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
421 memset(codeLengths + n, codeLengths[n - 1], c); |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
422 n += c; |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
423 } |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
424 else |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
425 if (c == 17) |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
426 { |
999
b3b8794c4915
Rename a function argument.
Matti Hamalainen <ccr@tnsp.org>
parents:
998
diff
changeset
|
427 c = dmZReceive(ctx, 3) + 3; |
960
1832ac20edb2
Clean up dmzlib and use it in stb_image.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
428 memset(codeLengths + n, 0, c); |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
429 n += c; |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
430 } |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
431 else |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
432 { |
999
b3b8794c4915
Rename a function argument.
Matti Hamalainen <ccr@tnsp.org>
parents:
998
diff
changeset
|
433 c = dmZReceive(ctx, 7) + 11; |
960
1832ac20edb2
Clean up dmzlib and use it in stb_image.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
434 memset(codeLengths + n, 0, c); |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
435 n += c; |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
436 } |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
437 } |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
438 |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
439 if (n != hlit + hdist) |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
440 { |
1046
7e54b2d08ce7
Add special debug error message function/macro that can be
Matti Hamalainen <ccr@tnsp.org>
parents:
1004
diff
changeset
|
441 return dmErrorDBG(DMERR_DATA_ERROR, |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
442 "Bad huffman codelengths.\n"); |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
443 } |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
444 |
999
b3b8794c4915
Rename a function argument.
Matti Hamalainen <ccr@tnsp.org>
parents:
998
diff
changeset
|
445 if ((ret = dmZLibBuildHuffmanTables(&ctx->zlength, codeLengths, hlit)) != DMERR_OK) |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
446 return ret; |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
447 |
999
b3b8794c4915
Rename a function argument.
Matti Hamalainen <ccr@tnsp.org>
parents:
998
diff
changeset
|
448 if ((ret = dmZLibBuildHuffmanTables(&ctx->zdistance, codeLengths + hlit, hdist)) != DMERR_OK) |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
449 return ret; |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
450 |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
451 return DMERR_OK; |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
452 } |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
453 |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
454 |
990 | 455 static int dmZLibParseUncompressedBlock(DMZLibContext * ctx) |
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 Uint8 header[4]; |
968 | 458 int len, nlen, k, ret; |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
459 |
997 | 460 // "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
|
461 if (ctx->numBits & 7) |
997 | 462 dmZReceive(ctx, ctx->numBits & 7); |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
463 |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
464 // 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
|
465 k = 0; |
965
df8d2ad98f7d
Rename a function argument.
Matti Hamalainen <ccr@tnsp.org>
parents:
964
diff
changeset
|
466 while (ctx->numBits > 0) |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
467 { |
965
df8d2ad98f7d
Rename a function argument.
Matti Hamalainen <ccr@tnsp.org>
parents:
964
diff
changeset
|
468 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
|
469 ctx->codeBuffer >>= 8; |
df8d2ad98f7d
Rename a function argument.
Matti Hamalainen <ccr@tnsp.org>
parents:
964
diff
changeset
|
470 ctx->numBits -= 8; |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
471 } |
1058 | 472 DMZLIB_ASSERT(ctx->numBits == 0); |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
473 |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
474 // now fill header the normal way |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
475 while (k < 4) |
965
df8d2ad98f7d
Rename a function argument.
Matti Hamalainen <ccr@tnsp.org>
parents:
964
diff
changeset
|
476 header[k++] = dmZGet8(ctx); |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
477 |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
478 len = (header[1] << 8) | header[0]; |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
479 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
|
480 |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
481 if (nlen != (len ^ 0xffff)) |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
482 { |
1046
7e54b2d08ce7
Add special debug error message function/macro that can be
Matti Hamalainen <ccr@tnsp.org>
parents:
1004
diff
changeset
|
483 return dmErrorDBG(DMERR_DATA_ERROR, |
992
929e43afbdb1
Improve error message slightly.
Matti Hamalainen <ccr@tnsp.org>
parents:
991
diff
changeset
|
484 "Compressed data corrupt %04x :: %04x [%04x].\n", |
929e43afbdb1
Improve error message slightly.
Matti Hamalainen <ccr@tnsp.org>
parents:
991
diff
changeset
|
485 nlen, len, len ^ 0xffff); |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
486 } |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
487 |
965
df8d2ad98f7d
Rename a function argument.
Matti Hamalainen <ccr@tnsp.org>
parents:
964
diff
changeset
|
488 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
|
489 { |
1046
7e54b2d08ce7
Add special debug error message function/macro that can be
Matti Hamalainen <ccr@tnsp.org>
parents:
1004
diff
changeset
|
490 return dmErrorDBG(DMERR_BOUNDS, |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
491 "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
|
492 } |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
493 |
968 | 494 if (ctx->zout + len > ctx->zoutEnd && |
969 | 495 (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
|
496 { |
1046
7e54b2d08ce7
Add special debug error message function/macro that can be
Matti Hamalainen <ccr@tnsp.org>
parents:
1004
diff
changeset
|
497 return dmErrorDBG(DMERR_DATA_ERROR, |
997 | 498 "Could not expand output buffer: %d, %s\n", |
968 | 499 ret, dmErrorStr(ret)); |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
500 } |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
501 |
965
df8d2ad98f7d
Rename a function argument.
Matti Hamalainen <ccr@tnsp.org>
parents:
964
diff
changeset
|
502 memcpy(ctx->zout, ctx->zbuffer, len); |
df8d2ad98f7d
Rename a function argument.
Matti Hamalainen <ccr@tnsp.org>
parents:
964
diff
changeset
|
503 ctx->zbuffer += len; |
df8d2ad98f7d
Rename a function argument.
Matti Hamalainen <ccr@tnsp.org>
parents:
964
diff
changeset
|
504 ctx->zout += len; |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
505 |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
506 return DMERR_OK; |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
507 } |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
508 |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
509 |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
510 int dmZLibDecode(DMZLibContext * ctx) |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
511 { |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
512 int final, type, ret; |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
513 |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
514 ctx->numBits = 0; |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
515 ctx->codeBuffer = 0; |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
516 do |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
517 { |
960
1832ac20edb2
Clean up dmzlib and use it in stb_image.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
518 final = dmZReceive(ctx, 1); |
1832ac20edb2
Clean up dmzlib and use it in stb_image.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
519 type = dmZReceive(ctx, 2); |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
520 if (type == 0) |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
521 { |
990 | 522 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
|
523 return ret; |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
524 } |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
525 else |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
526 if (type == 3) |
1001
198156b930d7
Fix error checking and propagation. 10L.
Matti Hamalainen <ccr@tnsp.org>
parents:
1000
diff
changeset
|
527 return DMERR_INVALID_DATA; |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
528 else |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
529 { |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
530 if (type == 1) |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
531 { |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
532 // use fixed code lengths |
960
1832ac20edb2
Clean up dmzlib and use it in stb_image.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
533 if ((ret = dmZLibBuildHuffmanTables(&ctx->zlength, dm_zdefault_length, 288)) != DMERR_OK) |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
534 return ret; |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
535 |
960
1832ac20edb2
Clean up dmzlib and use it in stb_image.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
536 if ((ret = dmZLibBuildHuffmanTables(&ctx->zdistance, dm_zdefault_distance, 32)) != DMERR_OK) |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
537 return ret; |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
538 } |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
539 else |
960
1832ac20edb2
Clean up dmzlib and use it in stb_image.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
540 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
|
541 return ret; |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
542 |
960
1832ac20edb2
Clean up dmzlib and use it in stb_image.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
543 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
|
544 return ret; |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
545 } |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
546 } |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
547 while (!final); |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
548 |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
549 return DMERR_OK; |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
550 } |
997 | 551 |
552 | |
553 int dmZLibParseHeader(DMZLibContext * ctx, BOOL checkPNG) | |
554 { | |
555 // See http://tools.ietf.org/html/rfc1950 | |
556 int cmf = dmZGet8(ctx); // Compression method and flags | |
557 int flags = dmZGet8(ctx); // Flags | |
558 int cmethod = (cmf & 15); | |
559 int cinfo = (cmf >> 4) & 15; | |
560 ctx->window = 1 << (8 + cinfo); // Window size (not used at the moment) | |
561 | |
562 // "The FCHECK value must be such that CMF and FLG, when viewed as | |
563 // a 16-bit unsigned integer stored in MSB order (CMF*256 + FLG), | |
564 // is a multiple of 31." | |
565 if ((cmf * 256 + flags) % 31 != 0) | |
566 { | |
1046
7e54b2d08ce7
Add special debug error message function/macro that can be
Matti Hamalainen <ccr@tnsp.org>
parents:
1004
diff
changeset
|
567 return dmErrorDBG(DMERR_INVALID_DATA, |
997 | 568 "Bad zlib header."); |
569 } | |
570 | |
571 // We only support compression method 8 | |
572 if (cmethod != 8) | |
573 { | |
1046
7e54b2d08ce7
Add special debug error message function/macro that can be
Matti Hamalainen <ccr@tnsp.org>
parents:
1004
diff
changeset
|
574 return dmErrorDBG(DMERR_INVALID_DATA, |
997 | 575 "Bad or unsupported zlib compression method %d.\n", |
576 cmethod); | |
577 } | |
578 | |
579 if (checkPNG && (flags & 32)) | |
580 { | |
581 // 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
|
582 return dmErrorDBG(DMERR_NOT_SUPPORTED, |
997 | 583 "Preset dictionary not allowed in PNG.\n"); |
584 } | |
585 return DMERR_OK; | |
586 } | |
587 |