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