annotate src/dmzlib.c @ 1272:acae5f8ebc67

Fix build process.
author Matti Hamalainen <ccr@tnsp.org>
date Thu, 19 May 2016 12:08:16 +0300
parents 848a88ce7a57
children 749f8f808531
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
e51ec592bfb6 Rename a macro.
Matti Hamalainen <ccr@tnsp.org>
parents: 1054
diff changeset
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
d98fcb10df6a Work on dmzlib.
Matti Hamalainen <ccr@tnsp.org>
parents: 1053
diff changeset
20 static Uint8 *dm_zdefault_length = NULL,
d98fcb10df6a Work on dmzlib.
Matti Hamalainen <ccr@tnsp.org>
parents: 1053
diff changeset
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
d98fcb10df6a Work on dmzlib.
Matti Hamalainen <ccr@tnsp.org>
parents: 1053
diff changeset
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
d98fcb10df6a Work on dmzlib.
Matti Hamalainen <ccr@tnsp.org>
parents: 1053
diff changeset
31 {
d98fcb10df6a Work on dmzlib.
Matti Hamalainen <ccr@tnsp.org>
parents: 1053
diff changeset
32 return dmErrorDBG(DMERR_MALLOC,
d98fcb10df6a Work on dmzlib.
Matti Hamalainen <ccr@tnsp.org>
parents: 1053
diff changeset
33 "Failed to allocate zlib decompression tables.\n");
d98fcb10df6a Work on dmzlib.
Matti Hamalainen <ccr@tnsp.org>
parents: 1053
diff changeset
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
d98fcb10df6a Work on dmzlib.
Matti Hamalainen <ccr@tnsp.org>
parents: 1053
diff changeset
55
d98fcb10df6a Work on dmzlib.
Matti Hamalainen <ccr@tnsp.org>
parents: 1053
diff changeset
56 return DMERR_OK;
d98fcb10df6a Work on dmzlib.
Matti Hamalainen <ccr@tnsp.org>
parents: 1053
diff changeset
57 }
d98fcb10df6a Work on dmzlib.
Matti Hamalainen <ccr@tnsp.org>
parents: 1053
diff changeset
58
d98fcb10df6a Work on dmzlib.
Matti Hamalainen <ccr@tnsp.org>
parents: 1053
diff changeset
59
d98fcb10df6a Work on dmzlib.
Matti Hamalainen <ccr@tnsp.org>
parents: 1053
diff changeset
60 void dmZLibClose()
d98fcb10df6a Work on dmzlib.
Matti Hamalainen <ccr@tnsp.org>
parents: 1053
diff changeset
61 {
d98fcb10df6a Work on dmzlib.
Matti Hamalainen <ccr@tnsp.org>
parents: 1053
diff changeset
62 dmFree(dm_zdefault_length);
d98fcb10df6a Work on dmzlib.
Matti Hamalainen <ccr@tnsp.org>
parents: 1053
diff changeset
63 dm_zdefault_length = NULL;
1058
e51ec592bfb6 Rename a macro.
Matti Hamalainen <ccr@tnsp.org>
parents: 1054
diff changeset
64
1054
d98fcb10df6a Work on dmzlib.
Matti Hamalainen <ccr@tnsp.org>
parents: 1053
diff changeset
65 dmFree(dm_zdefault_distance);
d98fcb10df6a Work on dmzlib.
Matti Hamalainen <ccr@tnsp.org>
parents: 1053
diff changeset
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
e51ec592bfb6 Rename a macro.
Matti Hamalainen <ccr@tnsp.org>
parents: 1054
diff changeset
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
df35244490e8 Rename a variable.
Matti Hamalainen <ccr@tnsp.org>
parents: 1058
diff changeset
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
848a88ce7a57 Use dmMemset().
Matti Hamalainen <ccr@tnsp.org>
parents: 1134
diff changeset
95 dmMemset(sizes, 0, sizeof(sizes));
848a88ce7a57 Use dmMemset().
Matti Hamalainen <ccr@tnsp.org>
parents: 1134
diff changeset
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
88cbea0ee9b5 Cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 951
diff changeset
103 {
88cbea0ee9b5 Cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 951
diff changeset
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
88cbea0ee9b5 Cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 951
diff changeset
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
df35244490e8 Rename a variable.
Matti Hamalainen <ccr@tnsp.org>
parents: 1058
diff changeset
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
7bf421248190 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 1075
diff changeset
117 code += sizes[i];
954
88cbea0ee9b5 Cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 951
diff changeset
118
88cbea0ee9b5 Cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 951
diff changeset
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
88cbea0ee9b5 Cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 951
diff changeset
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
df35244490e8 Rename a variable.
Matti Hamalainen <ccr@tnsp.org>
parents: 1058
diff changeset
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
5006cfe66250 Rename constants.
Matti Hamalainen <ccr@tnsp.org>
parents: 1059
diff changeset
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
5006cfe66250 Rename constants.
Matti Hamalainen <ccr@tnsp.org>
parents: 1059
diff changeset
142 int k = dmBitReverseN(nextCode[s], s);
5006cfe66250 Rename constants.
Matti Hamalainen <ccr@tnsp.org>
parents: 1059
diff changeset
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
df35244490e8 Rename a variable.
Matti Hamalainen <ccr@tnsp.org>
parents: 1058
diff changeset
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
76ac0d5c89b3 Oops, bugfix.
Matti Hamalainen <ccr@tnsp.org>
parents: 960
diff changeset
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
76ac0d5c89b3 Oops, bugfix.
Matti Hamalainen <ccr@tnsp.org>
parents: 960
diff changeset
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
14b82bd5a408 Rename some functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 968
diff changeset
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
e51ec592bfb6 Rename a macro.
Matti Hamalainen <ccr@tnsp.org>
parents: 1054
diff changeset
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
14b82bd5a408 Rename some functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 968
diff changeset
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
14b82bd5a408 Rename some functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 968
diff changeset
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
5006cfe66250 Rename constants.
Matti Hamalainen <ccr@tnsp.org>
parents: 1059
diff changeset
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
e51ec592bfb6 Rename a macro.
Matti Hamalainen <ccr@tnsp.org>
parents: 1054
diff changeset
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
14b82bd5a408 Rename some functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 968
diff changeset
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
14b82bd5a408 Rename some functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 968
diff changeset
230 dmZFillBits(ctx);
949
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
231
1060
5006cfe66250 Rename constants.
Matti Hamalainen <ccr@tnsp.org>
parents: 1059
diff changeset
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
7bf421248190 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 1075
diff changeset
242 // Not resolved by fast table, so compute it the slow way
969
14b82bd5a408 Rename some functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 968
diff changeset
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
2da97be2aa1f Rename some variables.
Matti Hamalainen <ccr@tnsp.org>
parents: 1001
diff changeset
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
2da97be2aa1f Rename some variables.
Matti Hamalainen <ccr@tnsp.org>
parents: 1001
diff changeset
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 {
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
289 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
290 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
291 };
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
292
1002
2da97be2aa1f Rename some variables.
Matti Hamalainen <ccr@tnsp.org>
parents: 1001
diff changeset
293 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
294 {
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
295 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
296 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
297 16385, 24577, 0, 0
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
298 };
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
299
1002
2da97be2aa1f Rename some variables.
Matti Hamalainen <ccr@tnsp.org>
parents: 1001
diff changeset
300 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
301 {
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
302 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
303 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
304 };
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
1003
7a0c2fe22e60 Rename a function argument.
Matti Hamalainen <ccr@tnsp.org>
parents: 1002
diff changeset
307 static int dmZLibParseHuffmanBlock(DMZLibContext * ctx)
949
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
308 {
1074
e98bc627ad08 Rename dmzlib structure members.
Matti Hamalainen <ccr@tnsp.org>
parents: 1073
diff changeset
309 Uint8 *outBuffer = ctx->outBuffer;
949
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
310 for (;;)
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
311 {
958
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
312 int z, ret;
1003
7a0c2fe22e60 Rename a function argument.
Matti Hamalainen <ccr@tnsp.org>
parents: 1002
diff changeset
313 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
314 return ret;
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
315
949
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
316 if (z < 256)
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
317 {
1074
e98bc627ad08 Rename dmzlib structure members.
Matti Hamalainen <ccr@tnsp.org>
parents: 1073
diff changeset
318 if (outBuffer >= ctx->outBufferEnd)
958
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
319 {
1074
e98bc627ad08 Rename dmzlib structure members.
Matti Hamalainen <ccr@tnsp.org>
parents: 1073
diff changeset
320 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
321 return ret;
949
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 outBuffer = ctx->outBuffer;
949
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
324 }
1074
e98bc627ad08 Rename dmzlib structure members.
Matti Hamalainen <ccr@tnsp.org>
parents: 1073
diff changeset
325 *outBuffer++ = (Uint8) z;
949
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
326 }
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
327 else
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
328 {
1075
97ccd6d972ff Comments, cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 1074
diff changeset
329 Uint8 *ptr;
949
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
330 int len, dist;
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
331 if (z == 256)
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
332 {
1074
e98bc627ad08 Rename dmzlib structure members.
Matti Hamalainen <ccr@tnsp.org>
parents: 1073
diff changeset
333 ctx->outBuffer = outBuffer;
958
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
334 return DMERR_OK;
949
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
335 }
1069
b20922f4746f Comments, cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1062
diff changeset
336
949
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
337 z -= 257;
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
338
1002
2da97be2aa1f Rename some variables.
Matti Hamalainen <ccr@tnsp.org>
parents: 1001
diff changeset
339 len = dm_zlib_length_base[z];
2da97be2aa1f Rename some variables.
Matti Hamalainen <ccr@tnsp.org>
parents: 1001
diff changeset
340 if (dm_zlib_length_extra[z])
1003
7a0c2fe22e60 Rename a function argument.
Matti Hamalainen <ccr@tnsp.org>
parents: 1002
diff changeset
341 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
342
1003
7a0c2fe22e60 Rename a function argument.
Matti Hamalainen <ccr@tnsp.org>
parents: 1002
diff changeset
343 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
344 return ret;
949
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
345
1002
2da97be2aa1f Rename some variables.
Matti Hamalainen <ccr@tnsp.org>
parents: 1001
diff changeset
346 dist = dm_zlib_dist_base[z];
2da97be2aa1f Rename some variables.
Matti Hamalainen <ccr@tnsp.org>
parents: 1001
diff changeset
347 if (dm_zlib_dist_extra[z])
1003
7a0c2fe22e60 Rename a function argument.
Matti Hamalainen <ccr@tnsp.org>
parents: 1002
diff changeset
348 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
349
1074
e98bc627ad08 Rename dmzlib structure members.
Matti Hamalainen <ccr@tnsp.org>
parents: 1073
diff changeset
350 if (outBuffer - ctx->outBufferStart < dist)
958
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
351 {
1046
7e54b2d08ce7 Add special debug error message function/macro that can be
Matti Hamalainen <ccr@tnsp.org>
parents: 1004
diff changeset
352 return dmErrorDBG(DMERR_DATA_ERROR,
958
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
353 "Bad Huffman block distance.\n");
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
354 }
949
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
355
1075
97ccd6d972ff Comments, cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 1074
diff changeset
356 // Check if output buffer needs to expand
1074
e98bc627ad08 Rename dmzlib structure members.
Matti Hamalainen <ccr@tnsp.org>
parents: 1073
diff changeset
357 if (outBuffer + len > ctx->outBufferEnd)
949
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
358 {
1074
e98bc627ad08 Rename dmzlib structure members.
Matti Hamalainen <ccr@tnsp.org>
parents: 1073
diff changeset
359 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
360 return ret;
1074
e98bc627ad08 Rename dmzlib structure members.
Matti Hamalainen <ccr@tnsp.org>
parents: 1073
diff changeset
361 outBuffer = ctx->outBuffer;
949
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
362 }
1075
97ccd6d972ff Comments, cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 1074
diff changeset
363
97ccd6d972ff Comments, cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 1074
diff changeset
364 ptr = outBuffer - dist;
949
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
365 if (dist == 1)
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
366 { // run of one byte; common in images.
1075
97ccd6d972ff Comments, cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 1074
diff changeset
367 Uint8 v = *ptr;
1074
e98bc627ad08 Rename dmzlib structure members.
Matti Hamalainen <ccr@tnsp.org>
parents: 1073
diff changeset
368 do { *outBuffer++ = v; } while (--len);
949
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
369 }
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
370 else
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
371 {
1075
97ccd6d972ff Comments, cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 1074
diff changeset
372 do { *outBuffer++ = *ptr++; } while (--len);
949
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
373 }
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
374 }
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
375 }
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
376 }
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
377
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
378
1054
d98fcb10df6a Work on dmzlib.
Matti Hamalainen <ccr@tnsp.org>
parents: 1053
diff changeset
379 #define DM_ZLIB_NCODELENGTH_SIZES 19
d98fcb10df6a Work on dmzlib.
Matti Hamalainen <ccr@tnsp.org>
parents: 1053
diff changeset
380
d98fcb10df6a Work on dmzlib.
Matti Hamalainen <ccr@tnsp.org>
parents: 1053
diff changeset
381 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
382 {
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
383 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
384 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
385 13, 2 , 14, 1 , 15
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
386 };
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
999
b3b8794c4915 Rename a function argument.
Matti Hamalainen <ccr@tnsp.org>
parents: 998
diff changeset
389 static int dmZLibComputeHuffmanCodes(DMZLibContext * ctx)
949
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
390 {
1069
b20922f4746f Comments, cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1062
diff changeset
391 DMZHuffmanContext codeLengthCtx;
1054
d98fcb10df6a Work on dmzlib.
Matti Hamalainen <ccr@tnsp.org>
parents: 1053
diff changeset
392 Uint8 codeLengthSizes[DM_ZLIB_NCODELENGTH_SIZES];
1069
b20922f4746f Comments, cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1062
diff changeset
393 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
394 int i, n, ret;
949
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
395
1069
b20922f4746f Comments, cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1062
diff changeset
396 // Get table sizes
b20922f4746f Comments, cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1062
diff changeset
397 int hlit = dmZReceive(ctx, 5) + 257; // 288 = X
b20922f4746f Comments, cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1062
diff changeset
398 int hdist = dmZReceive(ctx, 5) + 1; // 32 = X
b20922f4746f Comments, cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1062
diff changeset
399 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
400
1069
b20922f4746f Comments, cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1062
diff changeset
401 if (hlit > 286 || hdist > 30)
b20922f4746f Comments, cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1062
diff changeset
402 {
b20922f4746f Comments, cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1062
diff changeset
403 return dmErrorDBG(DMERR_INVALID_DATA,
b20922f4746f Comments, cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1062
diff changeset
404 "Too many Huffman length or distance symbols (%d, %d)",
b20922f4746f Comments, cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1062
diff changeset
405 hlit, hdist);
b20922f4746f Comments, cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1062
diff changeset
406 }
b20922f4746f Comments, cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1062
diff changeset
407
b20922f4746f Comments, cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1062
diff changeset
408 // 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
409 codeLengths[256] = 0;
1167
848a88ce7a57 Use dmMemset().
Matti Hamalainen <ccr@tnsp.org>
parents: 1134
diff changeset
410 dmMemset(codeLengthSizes, 0, sizeof(codeLengthSizes));
949
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
411 for (i = 0; i < hclen; i++)
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
412 {
999
b3b8794c4915 Rename a function argument.
Matti Hamalainen <ccr@tnsp.org>
parents: 998
diff changeset
413 int s = dmZReceive(ctx, 3);
1000
5df750e47721 Rename a variable.
Matti Hamalainen <ccr@tnsp.org>
parents: 999
diff changeset
414 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
415 }
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
416
1069
b20922f4746f Comments, cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1062
diff changeset
417 // Inflate the code lengths table
b20922f4746f Comments, cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1062
diff changeset
418 if ((ret = dmZLibBuildHuffmanTables(&codeLengthCtx,
b20922f4746f Comments, cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1062
diff changeset
419 codeLengthSizes, DM_ZLIB_NCODELENGTH_SIZES)) != DMERR_OK)
b20922f4746f Comments, cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1062
diff changeset
420 {
b20922f4746f Comments, cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1062
diff changeset
421 return dmErrorDBG(ret,
b20922f4746f Comments, cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1062
diff changeset
422 "Invalid code lengths set.\n");
b20922f4746f Comments, cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1062
diff changeset
423 }
955
6b2f41844580 Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 954
diff changeset
424
949
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
425 n = 0;
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
426 while (n < hlit + hdist)
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
427 {
958
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
428 int c;
1069
b20922f4746f Comments, cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1062
diff changeset
429 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
430 return ret;
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
431
1058
e51ec592bfb6 Rename a macro.
Matti Hamalainen <ccr@tnsp.org>
parents: 1054
diff changeset
432 DMZLIB_ASSERT(c >= 0 && c < DM_ZLIB_NCODELENGTH_SIZES);
955
6b2f41844580 Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 954
diff changeset
433
949
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
434 if (c < 16)
960
1832ac20edb2 Clean up dmzlib and use it in stb_image.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
435 codeLengths[n++] = (Uint8) c;
949
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
436 else
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
437 if (c == 16)
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
438 {
1069
b20922f4746f Comments, cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1062
diff changeset
439 int bv = dmZReceive(ctx, 2) + 3;
b20922f4746f Comments, cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1062
diff changeset
440 if (n == 0)
b20922f4746f Comments, cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1062
diff changeset
441 {
b20922f4746f Comments, cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1062
diff changeset
442 return dmErrorDBG(DMERR_INVALID_DATA,
b20922f4746f Comments, cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1062
diff changeset
443 "Invalid bit length repeat.\n");
b20922f4746f Comments, cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1062
diff changeset
444 }
1167
848a88ce7a57 Use dmMemset().
Matti Hamalainen <ccr@tnsp.org>
parents: 1134
diff changeset
445 dmMemset(codeLengths + n, codeLengths[n - 1], bv);
1069
b20922f4746f Comments, cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1062
diff changeset
446 n += bv;
949
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
447 }
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
448 else
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
449 if (c == 17)
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
450 {
1069
b20922f4746f Comments, cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1062
diff changeset
451 int bv = dmZReceive(ctx, 3) + 3;
1167
848a88ce7a57 Use dmMemset().
Matti Hamalainen <ccr@tnsp.org>
parents: 1134
diff changeset
452 dmMemset(codeLengths + n, 0, bv);
1069
b20922f4746f Comments, cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1062
diff changeset
453 n += bv;
949
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
454 }
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
455 else
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
456 {
1069
b20922f4746f Comments, cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1062
diff changeset
457 int bv = dmZReceive(ctx, 7) + 11;
1167
848a88ce7a57 Use dmMemset().
Matti Hamalainen <ccr@tnsp.org>
parents: 1134
diff changeset
458 dmMemset(codeLengths + n, 0, bv);
1069
b20922f4746f Comments, cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1062
diff changeset
459 n += bv;
949
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
460 }
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
461 }
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
462
1069
b20922f4746f Comments, cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1062
diff changeset
463 if (codeLengths[256] == 0)
b20922f4746f Comments, cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1062
diff changeset
464 {
b20922f4746f Comments, cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1062
diff changeset
465 return dmErrorDBG(DMERR_DATA_ERROR,
b20922f4746f Comments, cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1062
diff changeset
466 "Invalid code -- missing end-of-block.\n");
b20922f4746f Comments, cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1062
diff changeset
467 }
b20922f4746f Comments, cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1062
diff changeset
468
949
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
469 if (n != hlit + hdist)
958
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
470 {
1046
7e54b2d08ce7 Add special debug error message function/macro that can be
Matti Hamalainen <ccr@tnsp.org>
parents: 1004
diff changeset
471 return dmErrorDBG(DMERR_DATA_ERROR,
958
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
472 "Bad huffman codelengths.\n");
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
473 }
949
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
474
1069
b20922f4746f Comments, cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1062
diff changeset
475 // Build the code tables
999
b3b8794c4915 Rename a function argument.
Matti Hamalainen <ccr@tnsp.org>
parents: 998
diff changeset
476 if ((ret = dmZLibBuildHuffmanTables(&ctx->zlength, codeLengths, hlit)) != DMERR_OK)
1070
2ee8d6b765f9 Improve error handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 1069
diff changeset
477 {
2ee8d6b765f9 Improve error handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 1069
diff changeset
478 return dmErrorDBG(DMERR_DATA_ERROR,
2ee8d6b765f9 Improve error handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 1069
diff changeset
479 "Invalid literal/lengths set.\n");
2ee8d6b765f9 Improve error handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 1069
diff changeset
480 }
949
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
481
999
b3b8794c4915 Rename a function argument.
Matti Hamalainen <ccr@tnsp.org>
parents: 998
diff changeset
482 if ((ret = dmZLibBuildHuffmanTables(&ctx->zdistance, codeLengths + hlit, hdist)) != DMERR_OK)
1070
2ee8d6b765f9 Improve error handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 1069
diff changeset
483 {
2ee8d6b765f9 Improve error handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 1069
diff changeset
484 return dmErrorDBG(DMERR_DATA_ERROR,
2ee8d6b765f9 Improve error handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 1069
diff changeset
485 "Invalid literal distances set.\n");
2ee8d6b765f9 Improve error handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 1069
diff changeset
486 }
958
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
487
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
488 return DMERR_OK;
949
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
489 }
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
490
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
491
990
76f3961a044b Fix one function name.
Matti Hamalainen <ccr@tnsp.org>
parents: 969
diff changeset
492 static int dmZLibParseUncompressedBlock(DMZLibContext * ctx)
949
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
493 {
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
494 Uint8 header[4];
1071
dd101fda037d Use size_t here.
Matti Hamalainen <ccr@tnsp.org>
parents: 1070
diff changeset
495 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
496
997
cd0e0270e1ce Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 992
diff changeset
497 // "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
498 if (ctx->numBits & 7)
997
cd0e0270e1ce Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 992
diff changeset
499 dmZReceive(ctx, ctx->numBits & 7);
949
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
500
1074
e98bc627ad08 Rename dmzlib structure members.
Matti Hamalainen <ccr@tnsp.org>
parents: 1073
diff changeset
501 // 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
502 k = 0;
965
df8d2ad98f7d Rename a function argument.
Matti Hamalainen <ccr@tnsp.org>
parents: 964
diff changeset
503 while (ctx->numBits > 0)
949
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
504 {
965
df8d2ad98f7d Rename a function argument.
Matti Hamalainen <ccr@tnsp.org>
parents: 964
diff changeset
505 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
506 ctx->codeBuffer >>= 8;
df8d2ad98f7d Rename a function argument.
Matti Hamalainen <ccr@tnsp.org>
parents: 964
diff changeset
507 ctx->numBits -= 8;
949
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
508 }
1058
e51ec592bfb6 Rename a macro.
Matti Hamalainen <ccr@tnsp.org>
parents: 1054
diff changeset
509 DMZLIB_ASSERT(ctx->numBits == 0);
949
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 // now fill header the normal way
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
512 while (k < 4)
965
df8d2ad98f7d Rename a function argument.
Matti Hamalainen <ccr@tnsp.org>
parents: 964
diff changeset
513 header[k++] = dmZGet8(ctx);
949
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
514
958
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
515 len = (header[1] << 8) | header[0];
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
516 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
517
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
518 if (nlen != (len ^ 0xffff))
958
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
519 {
1046
7e54b2d08ce7 Add special debug error message function/macro that can be
Matti Hamalainen <ccr@tnsp.org>
parents: 1004
diff changeset
520 return dmErrorDBG(DMERR_DATA_ERROR,
992
929e43afbdb1 Improve error message slightly.
Matti Hamalainen <ccr@tnsp.org>
parents: 991
diff changeset
521 "Compressed data corrupt %04x :: %04x [%04x].\n",
929e43afbdb1 Improve error message slightly.
Matti Hamalainen <ccr@tnsp.org>
parents: 991
diff changeset
522 nlen, len, len ^ 0xffff);
958
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
523 }
949
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
524
1073
f317e34246db No need to handle literal copy of length zero.
Matti Hamalainen <ccr@tnsp.org>
parents: 1072
diff changeset
525 if (len > 0)
f317e34246db No need to handle literal copy of length zero.
Matti Hamalainen <ccr@tnsp.org>
parents: 1072
diff changeset
526 {
1074
e98bc627ad08 Rename dmzlib structure members.
Matti Hamalainen <ccr@tnsp.org>
parents: 1073
diff changeset
527 if (ctx->inBuffer + len > ctx->inBufferEnd)
e98bc627ad08 Rename dmzlib structure members.
Matti Hamalainen <ccr@tnsp.org>
parents: 1073
diff changeset
528 {
e98bc627ad08 Rename dmzlib structure members.
Matti Hamalainen <ccr@tnsp.org>
parents: 1073
diff changeset
529 return dmErrorDBG(DMERR_BOUNDS,
e98bc627ad08 Rename dmzlib structure members.
Matti Hamalainen <ccr@tnsp.org>
parents: 1073
diff changeset
530 "Read past buffer, probably corrupt compressed data.\n");
e98bc627ad08 Rename dmzlib structure members.
Matti Hamalainen <ccr@tnsp.org>
parents: 1073
diff changeset
531 }
949
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
532
1075
97ccd6d972ff Comments, cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 1074
diff changeset
533 // Check if output buffer needs to expand
1074
e98bc627ad08 Rename dmzlib structure members.
Matti Hamalainen <ccr@tnsp.org>
parents: 1073
diff changeset
534 if (ctx->outBuffer + len > ctx->outBufferEnd &&
e98bc627ad08 Rename dmzlib structure members.
Matti Hamalainen <ccr@tnsp.org>
parents: 1073
diff changeset
535 (ret = dmZLibExpand(ctx, ctx->outBuffer, len)) != DMERR_OK)
e98bc627ad08 Rename dmzlib structure members.
Matti Hamalainen <ccr@tnsp.org>
parents: 1073
diff changeset
536 {
e98bc627ad08 Rename dmzlib structure members.
Matti Hamalainen <ccr@tnsp.org>
parents: 1073
diff changeset
537 return dmErrorDBG(DMERR_DATA_ERROR,
e98bc627ad08 Rename dmzlib structure members.
Matti Hamalainen <ccr@tnsp.org>
parents: 1073
diff changeset
538 "Could not expand output buffer: %d, %s\n",
e98bc627ad08 Rename dmzlib structure members.
Matti Hamalainen <ccr@tnsp.org>
parents: 1073
diff changeset
539 ret, dmErrorStr(ret));
e98bc627ad08 Rename dmzlib structure members.
Matti Hamalainen <ccr@tnsp.org>
parents: 1073
diff changeset
540 }
949
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
541
1075
97ccd6d972ff Comments, cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 1074
diff changeset
542 // Copy uncompressed data
1074
e98bc627ad08 Rename dmzlib structure members.
Matti Hamalainen <ccr@tnsp.org>
parents: 1073
diff changeset
543 memcpy(ctx->outBuffer, ctx->inBuffer, len);
e98bc627ad08 Rename dmzlib structure members.
Matti Hamalainen <ccr@tnsp.org>
parents: 1073
diff changeset
544 ctx->inBuffer += len;
e98bc627ad08 Rename dmzlib structure members.
Matti Hamalainen <ccr@tnsp.org>
parents: 1073
diff changeset
545 ctx->outBuffer += len;
1073
f317e34246db No need to handle literal copy of length zero.
Matti Hamalainen <ccr@tnsp.org>
parents: 1072
diff changeset
546 }
958
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
547
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
548 return DMERR_OK;
949
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
549 }
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
550
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
551
1121
043b5942fdb6 Rename some dmzlib functions and add context init/close functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 1102
diff changeset
552 int dmZLibInitInflate(DMZLibContext *ctx)
043b5942fdb6 Rename some dmzlib functions and add context init/close functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 1102
diff changeset
553 {
043b5942fdb6 Rename some dmzlib functions and add context init/close functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 1102
diff changeset
554 if (ctx == NULL)
043b5942fdb6 Rename some dmzlib functions and add context init/close functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 1102
diff changeset
555 return DMERR_NULLPTR;
043b5942fdb6 Rename some dmzlib functions and add context init/close functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 1102
diff changeset
556
1167
848a88ce7a57 Use dmMemset().
Matti Hamalainen <ccr@tnsp.org>
parents: 1134
diff changeset
557 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
558 return DMERR_OK;
043b5942fdb6 Rename some dmzlib functions and add context init/close functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 1102
diff changeset
559 }
043b5942fdb6 Rename some dmzlib functions and add context init/close functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 1102
diff changeset
560
043b5942fdb6 Rename some dmzlib functions and add context init/close functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 1102
diff changeset
561
043b5942fdb6 Rename some dmzlib functions and add context init/close functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 1102
diff changeset
562 void dmZLibCloseInflate(DMZLibContext *ctx)
043b5942fdb6 Rename some dmzlib functions and add context init/close functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 1102
diff changeset
563 {
043b5942fdb6 Rename some dmzlib functions and add context init/close functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 1102
diff changeset
564 if (ctx != NULL)
043b5942fdb6 Rename some dmzlib functions and add context init/close functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 1102
diff changeset
565 {
1167
848a88ce7a57 Use dmMemset().
Matti Hamalainen <ccr@tnsp.org>
parents: 1134
diff changeset
566 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
567 }
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
043b5942fdb6 Rename some dmzlib functions and add context init/close functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 1102
diff changeset
570
043b5942fdb6 Rename some dmzlib functions and add context init/close functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 1102
diff changeset
571 int dmZLibInflate(DMZLibContext * ctx)
949
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
572 {
958
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
573 int final, type, ret;
949
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
574
958
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
575 ctx->numBits = 0;
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
576 ctx->codeBuffer = 0;
949
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
577 do
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
578 {
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
579 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
580 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
581
960
1832ac20edb2 Clean up dmzlib and use it in stb_image.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
582 final = dmZReceive(ctx, 1);
1832ac20edb2 Clean up dmzlib and use it in stb_image.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
583 type = dmZReceive(ctx, 2);
949
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
584 if (type == 0)
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
585 {
990
76f3961a044b Fix one function name.
Matti Hamalainen <ccr@tnsp.org>
parents: 969
diff changeset
586 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
587 return ret;
949
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
588 }
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
589 else
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
590 if (type == 3)
1072
33c5f2bd0ed3 Error message improvement.
Matti Hamalainen <ccr@tnsp.org>
parents: 1071
diff changeset
591 {
1082
dec84b805399 Make one error message debug.
Matti Hamalainen <ccr@tnsp.org>
parents: 1078
diff changeset
592 return dmErrorDBG(DMERR_INVALID_DATA,
1072
33c5f2bd0ed3 Error message improvement.
Matti Hamalainen <ccr@tnsp.org>
parents: 1071
diff changeset
593 "Invalid zlib block type.\n");
33c5f2bd0ed3 Error message improvement.
Matti Hamalainen <ccr@tnsp.org>
parents: 1071
diff changeset
594 }
949
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
595 else
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
596 {
1062
d36bf7514614 Add some constants and make initialization one-time only.
Matti Hamalainen <ccr@tnsp.org>
parents: 1060
diff changeset
597 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
598 {
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
599 // use fixed code lengths
1062
d36bf7514614 Add some constants and make initialization one-time only.
Matti Hamalainen <ccr@tnsp.org>
parents: 1060
diff changeset
600 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
601 return ret;
949
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
602
1062
d36bf7514614 Add some constants and make initialization one-time only.
Matti Hamalainen <ccr@tnsp.org>
parents: 1060
diff changeset
603 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
604 return ret;
949
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
605 }
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
606 else
960
1832ac20edb2 Clean up dmzlib and use it in stb_image.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
607 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
608 return ret;
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
609
960
1832ac20edb2 Clean up dmzlib and use it in stb_image.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
610 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
611 return ret;
949
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
612 }
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
613 }
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
614 while (!final);
958
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
615
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
616 return DMERR_OK;
949
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
617 }
997
cd0e0270e1ce Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 992
diff changeset
618
cd0e0270e1ce Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 992
diff changeset
619
cd0e0270e1ce Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 992
diff changeset
620 int dmZLibParseHeader(DMZLibContext * ctx, BOOL checkPNG)
cd0e0270e1ce Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 992
diff changeset
621 {
cd0e0270e1ce Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 992
diff changeset
622 // See http://tools.ietf.org/html/rfc1950
cd0e0270e1ce Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 992
diff changeset
623 int cmf = dmZGet8(ctx); // Compression method and flags
cd0e0270e1ce Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 992
diff changeset
624 int flags = dmZGet8(ctx); // Flags
cd0e0270e1ce Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 992
diff changeset
625 int cmethod = (cmf & 15);
cd0e0270e1ce Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 992
diff changeset
626 int cinfo = (cmf >> 4) & 15;
cd0e0270e1ce Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 992
diff changeset
627 ctx->window = 1 << (8 + cinfo); // Window size (not used at the moment)
cd0e0270e1ce Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 992
diff changeset
628
cd0e0270e1ce Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 992
diff changeset
629 // "The FCHECK value must be such that CMF and FLG, when viewed as
cd0e0270e1ce Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 992
diff changeset
630 // a 16-bit unsigned integer stored in MSB order (CMF*256 + FLG),
cd0e0270e1ce Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 992
diff changeset
631 // is a multiple of 31."
cd0e0270e1ce Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 992
diff changeset
632 if ((cmf * 256 + flags) % 31 != 0)
cd0e0270e1ce Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 992
diff changeset
633 {
1046
7e54b2d08ce7 Add special debug error message function/macro that can be
Matti Hamalainen <ccr@tnsp.org>
parents: 1004
diff changeset
634 return dmErrorDBG(DMERR_INVALID_DATA,
997
cd0e0270e1ce Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 992
diff changeset
635 "Bad zlib header.");
cd0e0270e1ce Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 992
diff changeset
636 }
cd0e0270e1ce Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 992
diff changeset
637
cd0e0270e1ce Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 992
diff changeset
638 // We only support compression method 8
cd0e0270e1ce Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 992
diff changeset
639 if (cmethod != 8)
cd0e0270e1ce Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 992
diff changeset
640 {
1046
7e54b2d08ce7 Add special debug error message function/macro that can be
Matti Hamalainen <ccr@tnsp.org>
parents: 1004
diff changeset
641 return dmErrorDBG(DMERR_INVALID_DATA,
997
cd0e0270e1ce Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 992
diff changeset
642 "Bad or unsupported zlib compression method %d.\n",
cd0e0270e1ce Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 992
diff changeset
643 cmethod);
cd0e0270e1ce Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 992
diff changeset
644 }
cd0e0270e1ce Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 992
diff changeset
645
cd0e0270e1ce Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 992
diff changeset
646 if (checkPNG && (flags & 32))
cd0e0270e1ce Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 992
diff changeset
647 {
cd0e0270e1ce Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 992
diff changeset
648 // 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
649 return dmErrorDBG(DMERR_NOT_SUPPORTED,
997
cd0e0270e1ce Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 992
diff changeset
650 "Preset dictionary not allowed in PNG.\n");
cd0e0270e1ce Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 992
diff changeset
651 }
cd0e0270e1ce Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 992
diff changeset
652 return DMERR_OK;
cd0e0270e1ce Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 992
diff changeset
653 }