Mercurial > hg > dmlib
annotate src/dmzlib.c @ 1053:a726c1b9a41e
Add some clarifying comments.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Sun, 01 Mar 2015 22:27:08 +0200 |
parents | 7e54b2d08ce7 |
children | d98fcb10df6a |
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) |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
16 #define DMSTBI_ASSERT(x) // 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 |
960
1832ac20edb2
Clean up dmzlib and use it in stb_image.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
20 static Uint8 dm_zdefault_length[288], dm_zdefault_distance[32]; |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
21 |
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 void dmZLibInit() |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
24 { |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
25 int i; |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
26 |
1053
a726c1b9a41e
Add some clarifying comments.
Matti Hamalainen <ccr@tnsp.org>
parents:
1046
diff
changeset
|
27 // Use <= to match clearly with DEFLATE spec |
a726c1b9a41e
Add some clarifying comments.
Matti Hamalainen <ccr@tnsp.org>
parents:
1046
diff
changeset
|
28 |
a726c1b9a41e
Add some clarifying comments.
Matti Hamalainen <ccr@tnsp.org>
parents:
1046
diff
changeset
|
29 // 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
|
30 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
|
31 dm_zdefault_length[i] = 8; |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
32 |
1053
a726c1b9a41e
Add some clarifying comments.
Matti Hamalainen <ccr@tnsp.org>
parents:
1046
diff
changeset
|
33 // 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
|
34 for (; i <= 255; i++) |
960
1832ac20edb2
Clean up dmzlib and use it in stb_image.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
35 dm_zdefault_length[i] = 9; |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
36 |
1053
a726c1b9a41e
Add some clarifying comments.
Matti Hamalainen <ccr@tnsp.org>
parents:
1046
diff
changeset
|
37 // 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
|
38 for (; i <= 279; i++) |
960
1832ac20edb2
Clean up dmzlib and use it in stb_image.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
39 dm_zdefault_length[i] = 7; |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
40 |
1053
a726c1b9a41e
Add some clarifying comments.
Matti Hamalainen <ccr@tnsp.org>
parents:
1046
diff
changeset
|
41 // 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
|
42 for (; i <= 287; i++) |
960
1832ac20edb2
Clean up dmzlib and use it in stb_image.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
43 dm_zdefault_length[i] = 8; |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
44 |
1053
a726c1b9a41e
Add some clarifying comments.
Matti Hamalainen <ccr@tnsp.org>
parents:
1046
diff
changeset
|
45 // Default distances |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
46 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
|
47 dm_zdefault_distance[i] = 5; |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
48 } |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
49 |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
50 |
998
4a68f8d0adc5
Rename bitreverse functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
997
diff
changeset
|
51 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
|
52 { |
1053
a726c1b9a41e
Add some clarifying comments.
Matti Hamalainen <ccr@tnsp.org>
parents:
1046
diff
changeset
|
53 // "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
|
54 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
|
55 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
|
56 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
|
57 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
|
58 return n; |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
59 } |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
60 |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
61 |
1053
a726c1b9a41e
Add some clarifying comments.
Matti Hamalainen <ccr@tnsp.org>
parents:
1046
diff
changeset
|
62 // To bit reverse N bits, reverse 16 and shift |
a726c1b9a41e
Add some clarifying comments.
Matti Hamalainen <ccr@tnsp.org>
parents:
1046
diff
changeset
|
63 // e.g. 11 bits, bit reverse and shift away 5 |
a726c1b9a41e
Add some clarifying comments.
Matti Hamalainen <ccr@tnsp.org>
parents:
1046
diff
changeset
|
64 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
|
65 { |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
66 DMSTBI_ASSERT(bits <= 16); |
998
4a68f8d0adc5
Rename bitreverse functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
997
diff
changeset
|
67 return dmBitReverse16(v) >> (16 - bits); |
949
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 |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
70 |
960
1832ac20edb2
Clean up dmzlib and use it in stb_image.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
71 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
|
72 { |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
73 int i, k = 0; |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
74 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
|
75 |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
76 memset(sizes, 0, sizeof(sizes)); |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
77 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
|
78 |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
79 for (i = 0; i < num; i++) |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
80 sizes[sizelist[i]]++; |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
81 |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
82 sizes[0] = 0; |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
83 for (i = 1; i < 16; i++) |
954 | 84 { |
85 if (sizes[i] > (1 << i)) | |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
86 { |
1046
7e54b2d08ce7
Add special debug error message function/macro that can be
Matti Hamalainen <ccr@tnsp.org>
parents:
1004
diff
changeset
|
87 return dmErrorDBG(DMERR_INTERNAL, |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
88 "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
|
89 } |
954 | 90 } |
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 code = 0; |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
93 for (i = 1; i < 16; i++) |
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 next_code[i] = code; |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
96 ctx->firstCode[i] = (Uint16) code; |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
97 ctx->firstSymbol[i] = (Uint16) k; |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
98 code = (code + sizes[i]); |
954 | 99 |
100 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
|
101 { |
1046
7e54b2d08ce7
Add special debug error message function/macro that can be
Matti Hamalainen <ccr@tnsp.org>
parents:
1004
diff
changeset
|
102 return dmErrorDBG(DMERR_INVALID_DATA, |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
103 "Bad Huffman code lengths.\n"); |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
104 } |
954 | 105 |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
106 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
|
107 code <<= 1; |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
108 k += sizes[i]; |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
109 } |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
110 |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
111 ctx->maxCode[16] = 0x10000; // sentinel |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
112 for (i = 0; i < num; 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 int s = sizelist[i]; |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
115 if (s) |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
116 { |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
117 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
|
118 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
|
119 ctx->size[c] = (Uint8) s; |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
120 ctx->value[c] = (Uint16) i; |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
121 if (s <= STBI__ZFAST_BITS) |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
122 { |
998
4a68f8d0adc5
Rename bitreverse functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
997
diff
changeset
|
123 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
|
124 while (k < STBI__ZFAST_SIZE) |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
125 { |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
126 ctx->fast[k] = fastv; |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
127 k += (1 << s); |
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 } |
997 | 130 next_code[s]++; |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
131 } |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
132 } |
964 | 133 |
134 return DMERR_OK; | |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
135 } |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
136 |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
137 |
960
1832ac20edb2
Clean up dmzlib and use it in stb_image.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
138 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
|
139 { |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
140 if (ctx->zbuffer >= ctx->zbufferEnd) |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
141 return 0; |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
142 |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
143 return *ctx->zbuffer++; |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
144 } |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
145 |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
146 |
969 | 147 static void dmZFillBits(DMZLibContext * ctx) |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
148 { |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
149 do |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
150 { |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
151 DMSTBI_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
|
152 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
|
153 ctx->numBits += 8; |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
154 } |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
155 while (ctx->numBits <= 24); |
949
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 |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
158 |
960
1832ac20edb2
Clean up dmzlib and use it in stb_image.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
159 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
|
160 { |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
161 unsigned int val; |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
162 |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
163 if (ctx->numBits < n) |
969 | 164 dmZFillBits(ctx); |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
165 |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
166 val = ctx->codeBuffer & ((1 << n) - 1); |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
167 ctx->codeBuffer >>= n; |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
168 ctx->numBits -= n; |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
169 |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
170 return val; |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
171 } |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
172 |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
173 |
969 | 174 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
|
175 { |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
176 int b, s, k; |
960
1832ac20edb2
Clean up dmzlib and use it in stb_image.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
177 *val = 0; |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
178 |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
179 // 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
|
180 // use jpeg approach, which requires MSbits at top |
998
4a68f8d0adc5
Rename bitreverse functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
997
diff
changeset
|
181 k = dmBitReverse16(ctx->codeBuffer); |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
182 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
|
183 { |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
184 if (k < huff->maxCode[s]) |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
185 break; |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
186 } |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
187 |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
188 if (s == 16) |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
189 { |
1046
7e54b2d08ce7
Add special debug error message function/macro that can be
Matti Hamalainen <ccr@tnsp.org>
parents:
1004
diff
changeset
|
190 return dmErrorDBG(DMERR_DATA_ERROR, |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
191 "Bad Huffman code.\n"); |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
192 } |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
193 |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
194 // code size is s, so: |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
195 b = (k >> (16 - s)) - huff->firstCode[s] + huff->firstSymbol[s]; |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
196 DMSTBI_ASSERT(huff->size[b] == s); |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
197 |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
198 ctx->codeBuffer >>= s; |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
199 ctx->numBits -= s; |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
200 *val = huff->value[b]; |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
201 |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
202 return DMERR_OK; |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
203 } |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
204 |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
205 |
969 | 206 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
|
207 { |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
208 int b; |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
209 if (ctx->numBits < 16) |
969 | 210 dmZFillBits(ctx); |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
211 |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
212 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
|
213 if (b) |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
214 { |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
215 int s = b >> 9; |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
216 ctx->codeBuffer >>= s; |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
217 ctx->numBits -= s; |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
218 *val = b & 511; |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
219 return DMERR_OK; |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
220 } |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
221 |
969 | 222 return dmZLibHuffmanDecodeSlow(ctx, huff, val); |
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 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
|
227 { |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
228 Uint8 *newBuf; |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
229 size_t cur, limit; |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
230 |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
231 ctx->zout = zout; |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
232 |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
233 if (!ctx->expandable) |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
234 { |
1046
7e54b2d08ce7
Add special debug error message function/macro that can be
Matti Hamalainen <ccr@tnsp.org>
parents:
1004
diff
changeset
|
235 return dmErrorDBG(DMERR_BOUNDS, |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
236 "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
|
237 } |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
238 |
969 | 239 cur = ctx->zout - ctx->zoutStart; |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
240 limit = ctx->zoutEnd - ctx->zoutStart; |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
241 |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
242 while (cur + n > limit) |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
243 limit *= 2; |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
244 |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
245 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
|
246 { |
1046
7e54b2d08ce7
Add special debug error message function/macro that can be
Matti Hamalainen <ccr@tnsp.org>
parents:
1004
diff
changeset
|
247 return dmErrorDBG(DMERR_MALLOC, |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
248 "Could not reallocate buffer.\n"); |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
249 } |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
250 |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
251 ctx->zoutStart = newBuf; |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
252 ctx->zout = newBuf + cur; |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
253 ctx->zoutEnd = newBuf + limit; |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
254 |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
255 return DMERR_OK; |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
256 } |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
257 |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
258 |
1002 | 259 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
|
260 { |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
261 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
|
262 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
|
263 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
|
264 }; |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
265 |
1002 | 266 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
|
267 { |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
268 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
|
269 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
|
270 }; |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
271 |
1002 | 272 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
|
273 { |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
274 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
|
275 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
|
276 16385, 24577, 0, 0 |
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_dist_extra[32] = |
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 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
|
282 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
|
283 }; |
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 |
1003
7a0c2fe22e60
Rename a function argument.
Matti Hamalainen <ccr@tnsp.org>
parents:
1002
diff
changeset
|
286 static int dmZLibParseHuffmanBlock(DMZLibContext * ctx) |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
287 { |
1003
7a0c2fe22e60
Rename a function argument.
Matti Hamalainen <ccr@tnsp.org>
parents:
1002
diff
changeset
|
288 Uint8 *zout = ctx->zout; |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
289 for (;;) |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
290 { |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
291 int z, ret; |
1003
7a0c2fe22e60
Rename a function argument.
Matti Hamalainen <ccr@tnsp.org>
parents:
1002
diff
changeset
|
292 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
|
293 return ret; |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
294 |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
295 if (z < 256) |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
296 { |
1003
7a0c2fe22e60
Rename a function argument.
Matti Hamalainen <ccr@tnsp.org>
parents:
1002
diff
changeset
|
297 if (zout >= ctx->zoutEnd) |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
298 { |
1003
7a0c2fe22e60
Rename a function argument.
Matti Hamalainen <ccr@tnsp.org>
parents:
1002
diff
changeset
|
299 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
|
300 return ret; |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
301 |
1003
7a0c2fe22e60
Rename a function argument.
Matti Hamalainen <ccr@tnsp.org>
parents:
1002
diff
changeset
|
302 zout = ctx->zout; |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
303 } |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
304 *zout++ = (Uint8) z; |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
305 } |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
306 else |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
307 { |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
308 Uint8 *p; |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
309 int len, dist; |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
310 if (z == 256) |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
311 { |
1003
7a0c2fe22e60
Rename a function argument.
Matti Hamalainen <ccr@tnsp.org>
parents:
1002
diff
changeset
|
312 ctx->zout = zout; |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
313 return DMERR_OK; |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
314 } |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
315 z -= 257; |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
316 |
1002 | 317 len = dm_zlib_length_base[z]; |
318 if (dm_zlib_length_extra[z]) | |
1003
7a0c2fe22e60
Rename a function argument.
Matti Hamalainen <ccr@tnsp.org>
parents:
1002
diff
changeset
|
319 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
|
320 |
1003
7a0c2fe22e60
Rename a function argument.
Matti Hamalainen <ccr@tnsp.org>
parents:
1002
diff
changeset
|
321 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
|
322 return ret; |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
323 |
1002 | 324 dist = dm_zlib_dist_base[z]; |
325 if (dm_zlib_dist_extra[z]) | |
1003
7a0c2fe22e60
Rename a function argument.
Matti Hamalainen <ccr@tnsp.org>
parents:
1002
diff
changeset
|
326 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
|
327 |
1003
7a0c2fe22e60
Rename a function argument.
Matti Hamalainen <ccr@tnsp.org>
parents:
1002
diff
changeset
|
328 if (zout - ctx->zoutStart < dist) |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
329 { |
1046
7e54b2d08ce7
Add special debug error message function/macro that can be
Matti Hamalainen <ccr@tnsp.org>
parents:
1004
diff
changeset
|
330 return dmErrorDBG(DMERR_DATA_ERROR, |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
331 "Bad Huffman block distance.\n"); |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
332 } |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
333 |
1003
7a0c2fe22e60
Rename a function argument.
Matti Hamalainen <ccr@tnsp.org>
parents:
1002
diff
changeset
|
334 if (zout + len > ctx->zoutEnd) |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
335 { |
1003
7a0c2fe22e60
Rename a function argument.
Matti Hamalainen <ccr@tnsp.org>
parents:
1002
diff
changeset
|
336 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
|
337 return ret; |
1003
7a0c2fe22e60
Rename a function argument.
Matti Hamalainen <ccr@tnsp.org>
parents:
1002
diff
changeset
|
338 zout = ctx->zout; |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
339 } |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
340 p = (Uint8 *) (zout - dist); |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
341 if (dist == 1) |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
342 { // 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
|
343 Uint8 v = *p; |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
344 do { *zout++ = v; } while (--len); |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
345 } |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
346 else |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
347 { |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
348 do { *zout++ = *p++; } while (--len); |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
349 } |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
350 } |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
351 } |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
352 } |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
353 |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
354 |
1000 | 355 static const Uint8 dm_zlib_length_dezigzag[19] = |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
356 { |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
357 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
|
358 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
|
359 13, 2 , 14, 1 , 15 |
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 |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
362 |
999
b3b8794c4915
Rename a function argument.
Matti Hamalainen <ccr@tnsp.org>
parents:
998
diff
changeset
|
363 static int dmZLibComputeHuffmanCodes(DMZLibContext * ctx) |
949
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 DMZHuffmanContext z_codelength; |
1004 | 366 Uint8 codeLengths[286 + 32 + 137]; // padding for maximum single op |
960
1832ac20edb2
Clean up dmzlib and use it in stb_image.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
367 Uint8 codeLengthSizes[19]; |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
368 int i, n, ret; |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
369 |
999
b3b8794c4915
Rename a function argument.
Matti Hamalainen <ccr@tnsp.org>
parents:
998
diff
changeset
|
370 int hlit = dmZReceive(ctx, 5) + 257; |
b3b8794c4915
Rename a function argument.
Matti Hamalainen <ccr@tnsp.org>
parents:
998
diff
changeset
|
371 int hdist = dmZReceive(ctx, 5) + 1; |
b3b8794c4915
Rename a function argument.
Matti Hamalainen <ccr@tnsp.org>
parents:
998
diff
changeset
|
372 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
|
373 |
960
1832ac20edb2
Clean up dmzlib and use it in stb_image.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
374 memset(codeLengthSizes, 0, sizeof(codeLengthSizes)); |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
375 |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
376 for (i = 0; i < hclen; i++) |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
377 { |
999
b3b8794c4915
Rename a function argument.
Matti Hamalainen <ccr@tnsp.org>
parents:
998
diff
changeset
|
378 int s = dmZReceive(ctx, 3); |
1000 | 379 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
|
380 } |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
381 |
960
1832ac20edb2
Clean up dmzlib and use it in stb_image.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
382 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
|
383 return ret; |
955 | 384 |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
385 n = 0; |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
386 while (n < hlit + hdist) |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
387 { |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
388 int c; |
999
b3b8794c4915
Rename a function argument.
Matti Hamalainen <ccr@tnsp.org>
parents:
998
diff
changeset
|
389 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
|
390 return ret; |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
391 |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
392 DMSTBI_ASSERT(c >= 0 && c < 19); |
955 | 393 |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
394 if (c < 16) |
960
1832ac20edb2
Clean up dmzlib and use it in stb_image.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
395 codeLengths[n++] = (Uint8) c; |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
396 else |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
397 if (c == 16) |
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 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
|
400 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
|
401 n += c; |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
402 } |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
403 else |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
404 if (c == 17) |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
405 { |
999
b3b8794c4915
Rename a function argument.
Matti Hamalainen <ccr@tnsp.org>
parents:
998
diff
changeset
|
406 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
|
407 memset(codeLengths + n, 0, c); |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
408 n += c; |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
409 } |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
410 else |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
411 { |
999
b3b8794c4915
Rename a function argument.
Matti Hamalainen <ccr@tnsp.org>
parents:
998
diff
changeset
|
412 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
|
413 memset(codeLengths + n, 0, c); |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
414 n += c; |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
415 } |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
416 } |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
417 |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
418 if (n != hlit + hdist) |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
419 { |
1046
7e54b2d08ce7
Add special debug error message function/macro that can be
Matti Hamalainen <ccr@tnsp.org>
parents:
1004
diff
changeset
|
420 return dmErrorDBG(DMERR_DATA_ERROR, |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
421 "Bad huffman codelengths.\n"); |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
422 } |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
423 |
999
b3b8794c4915
Rename a function argument.
Matti Hamalainen <ccr@tnsp.org>
parents:
998
diff
changeset
|
424 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
|
425 return ret; |
949
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 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
|
428 return ret; |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
429 |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
430 return DMERR_OK; |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
431 } |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
432 |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
433 |
990 | 434 static int dmZLibParseUncompressedBlock(DMZLibContext * ctx) |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
435 { |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
436 Uint8 header[4]; |
968 | 437 int len, nlen, k, ret; |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
438 |
997 | 439 // "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
|
440 if (ctx->numBits & 7) |
997 | 441 dmZReceive(ctx, ctx->numBits & 7); |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
442 |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
443 // 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
|
444 k = 0; |
965
df8d2ad98f7d
Rename a function argument.
Matti Hamalainen <ccr@tnsp.org>
parents:
964
diff
changeset
|
445 while (ctx->numBits > 0) |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
446 { |
965
df8d2ad98f7d
Rename a function argument.
Matti Hamalainen <ccr@tnsp.org>
parents:
964
diff
changeset
|
447 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
|
448 ctx->codeBuffer >>= 8; |
df8d2ad98f7d
Rename a function argument.
Matti Hamalainen <ccr@tnsp.org>
parents:
964
diff
changeset
|
449 ctx->numBits -= 8; |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
450 } |
965
df8d2ad98f7d
Rename a function argument.
Matti Hamalainen <ccr@tnsp.org>
parents:
964
diff
changeset
|
451 DMSTBI_ASSERT(ctx->numBits == 0); |
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 // now fill header the normal way |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
454 while (k < 4) |
965
df8d2ad98f7d
Rename a function argument.
Matti Hamalainen <ccr@tnsp.org>
parents:
964
diff
changeset
|
455 header[k++] = dmZGet8(ctx); |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
456 |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
457 len = (header[1] << 8) | header[0]; |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
458 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
|
459 |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
460 if (nlen != (len ^ 0xffff)) |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
461 { |
1046
7e54b2d08ce7
Add special debug error message function/macro that can be
Matti Hamalainen <ccr@tnsp.org>
parents:
1004
diff
changeset
|
462 return dmErrorDBG(DMERR_DATA_ERROR, |
992
929e43afbdb1
Improve error message slightly.
Matti Hamalainen <ccr@tnsp.org>
parents:
991
diff
changeset
|
463 "Compressed data corrupt %04x :: %04x [%04x].\n", |
929e43afbdb1
Improve error message slightly.
Matti Hamalainen <ccr@tnsp.org>
parents:
991
diff
changeset
|
464 nlen, len, len ^ 0xffff); |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
465 } |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
466 |
965
df8d2ad98f7d
Rename a function argument.
Matti Hamalainen <ccr@tnsp.org>
parents:
964
diff
changeset
|
467 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
|
468 { |
1046
7e54b2d08ce7
Add special debug error message function/macro that can be
Matti Hamalainen <ccr@tnsp.org>
parents:
1004
diff
changeset
|
469 return dmErrorDBG(DMERR_BOUNDS, |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
470 "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
|
471 } |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
472 |
968 | 473 if (ctx->zout + len > ctx->zoutEnd && |
969 | 474 (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
|
475 { |
1046
7e54b2d08ce7
Add special debug error message function/macro that can be
Matti Hamalainen <ccr@tnsp.org>
parents:
1004
diff
changeset
|
476 return dmErrorDBG(DMERR_DATA_ERROR, |
997 | 477 "Could not expand output buffer: %d, %s\n", |
968 | 478 ret, dmErrorStr(ret)); |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
479 } |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
480 |
965
df8d2ad98f7d
Rename a function argument.
Matti Hamalainen <ccr@tnsp.org>
parents:
964
diff
changeset
|
481 memcpy(ctx->zout, ctx->zbuffer, len); |
df8d2ad98f7d
Rename a function argument.
Matti Hamalainen <ccr@tnsp.org>
parents:
964
diff
changeset
|
482 ctx->zbuffer += len; |
df8d2ad98f7d
Rename a function argument.
Matti Hamalainen <ccr@tnsp.org>
parents:
964
diff
changeset
|
483 ctx->zout += len; |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
484 |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
485 return DMERR_OK; |
949
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 |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
488 |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
489 int dmZLibDecode(DMZLibContext * ctx) |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
490 { |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
491 int final, type, ret; |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
492 |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
493 ctx->numBits = 0; |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
494 ctx->codeBuffer = 0; |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
495 do |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
496 { |
960
1832ac20edb2
Clean up dmzlib and use it in stb_image.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
497 final = dmZReceive(ctx, 1); |
1832ac20edb2
Clean up dmzlib and use it in stb_image.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
498 type = dmZReceive(ctx, 2); |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
499 if (type == 0) |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
500 { |
990 | 501 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
|
502 return ret; |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
503 } |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
504 else |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
505 if (type == 3) |
1001
198156b930d7
Fix error checking and propagation. 10L.
Matti Hamalainen <ccr@tnsp.org>
parents:
1000
diff
changeset
|
506 return DMERR_INVALID_DATA; |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
507 else |
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 if (type == 1) |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
510 { |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
511 // use fixed code lengths |
960
1832ac20edb2
Clean up dmzlib and use it in stb_image.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
512 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
|
513 return ret; |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
514 |
960
1832ac20edb2
Clean up dmzlib and use it in stb_image.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
515 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
|
516 return ret; |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
517 } |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
518 else |
960
1832ac20edb2
Clean up dmzlib and use it in stb_image.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
519 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
|
520 return ret; |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
521 |
960
1832ac20edb2
Clean up dmzlib and use it in stb_image.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
522 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
|
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 } |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
526 while (!final); |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
527 |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
528 return DMERR_OK; |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
529 } |
997 | 530 |
531 | |
532 int dmZLibParseHeader(DMZLibContext * ctx, BOOL checkPNG) | |
533 { | |
534 // See http://tools.ietf.org/html/rfc1950 | |
535 int cmf = dmZGet8(ctx); // Compression method and flags | |
536 int flags = dmZGet8(ctx); // Flags | |
537 int cmethod = (cmf & 15); | |
538 int cinfo = (cmf >> 4) & 15; | |
539 ctx->window = 1 << (8 + cinfo); // Window size (not used at the moment) | |
540 | |
541 // "The FCHECK value must be such that CMF and FLG, when viewed as | |
542 // a 16-bit unsigned integer stored in MSB order (CMF*256 + FLG), | |
543 // is a multiple of 31." | |
544 if ((cmf * 256 + flags) % 31 != 0) | |
545 { | |
1046
7e54b2d08ce7
Add special debug error message function/macro that can be
Matti Hamalainen <ccr@tnsp.org>
parents:
1004
diff
changeset
|
546 return dmErrorDBG(DMERR_INVALID_DATA, |
997 | 547 "Bad zlib header."); |
548 } | |
549 | |
550 // We only support compression method 8 | |
551 if (cmethod != 8) | |
552 { | |
1046
7e54b2d08ce7
Add special debug error message function/macro that can be
Matti Hamalainen <ccr@tnsp.org>
parents:
1004
diff
changeset
|
553 return dmErrorDBG(DMERR_INVALID_DATA, |
997 | 554 "Bad or unsupported zlib compression method %d.\n", |
555 cmethod); | |
556 } | |
557 | |
558 if (checkPNG && (flags & 32)) | |
559 { | |
560 // 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
|
561 return dmErrorDBG(DMERR_NOT_SUPPORTED, |
997 | 562 "Preset dictionary not allowed in PNG.\n"); |
563 } | |
564 return DMERR_OK; | |
565 } | |
566 |