annotate src/dmzlib.c @ 1071:dd101fda037d

Use size_t here.
author Matti Hamalainen <ccr@tnsp.org>
date Mon, 02 Mar 2015 03:54:41 +0200
parents 2ee8d6b765f9
children 33c5f2bd0ed3
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
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
95 memset(sizes, 0, sizeof(sizes));
958
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
96 memset(ctx->fast, 0, sizeof(ctx->fast));
949
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
97
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
98 for (i = 0; i < num; i++)
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
99 sizes[sizelist[i]]++;
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
100
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
101 sizes[0] = 0;
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
102 for (i = 1; i < 16; i++)
954
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;
949
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
117 code = (code + sizes[i]);
954
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 {
958
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
160 if (ctx->zbuffer >= ctx->zbufferEnd)
949
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
161 return 0;
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
162
958
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
163 return *ctx->zbuffer++;
949
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
164 }
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
165
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
166
969
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];
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
221
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
222 return DMERR_OK;
949
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
223 }
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
224
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
225
969
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
969
14b82bd5a408 Rename some functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 968
diff changeset
242 return dmZLibHuffmanDecodeSlow(ctx, huff, val);
949
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
243 }
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
244
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
245
969
14b82bd5a408 Rename some functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 968
diff changeset
246 static int dmZLibExpand(DMZLibContext * ctx, Uint8 *zout, size_t n)
949
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
247 {
958
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
248 Uint8 *newBuf;
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
249 size_t cur, limit;
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
250
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
251 ctx->zout = zout;
949
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
252
958
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
253 if (!ctx->expandable)
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
254 {
1046
7e54b2d08ce7 Add special debug error message function/macro that can be
Matti Hamalainen <ccr@tnsp.org>
parents: 1004
diff changeset
255 return dmErrorDBG(DMERR_BOUNDS,
958
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
256 "Output buffer limit hit, and is not expandable.\n");
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
257 }
949
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
258
969
14b82bd5a408 Rename some functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 968
diff changeset
259 cur = ctx->zout - ctx->zoutStart;
958
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
260 limit = ctx->zoutEnd - ctx->zoutStart;
949
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
261
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
262 while (cur + n > limit)
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
263 limit *= 2;
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
264
958
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
265 if ((newBuf = (Uint8 *) dmRealloc(ctx->zoutStart, limit)) == NULL)
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
266 {
1046
7e54b2d08ce7 Add special debug error message function/macro that can be
Matti Hamalainen <ccr@tnsp.org>
parents: 1004
diff changeset
267 return dmErrorDBG(DMERR_MALLOC,
958
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
268 "Could not reallocate buffer.\n");
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
269 }
949
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
270
958
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
271 ctx->zoutStart = newBuf;
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
272 ctx->zout = newBuf + cur;
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
273 ctx->zoutEnd = newBuf + limit;
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
274
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
275 return DMERR_OK;
949
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
276 }
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
277
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
278
1002
2da97be2aa1f Rename some variables.
Matti Hamalainen <ccr@tnsp.org>
parents: 1001
diff changeset
279 static const int dm_zlib_length_base[31] =
949
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
280 {
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
281 3, 4, 5, 6, 7, 8, 9, 10, 11, 13,
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
282 15, 17, 19, 23, 27, 31, 35, 43, 51, 59,
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
283 67, 83, 99, 115, 131, 163, 195, 227, 258, 0, 0
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
284 };
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
285
1002
2da97be2aa1f Rename some variables.
Matti Hamalainen <ccr@tnsp.org>
parents: 1001
diff changeset
286 static const int dm_zlib_length_extra[31] =
949
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
287 {
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
288 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4,
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
289 5, 5, 5, 5, 0, 0, 0
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
290 };
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
291
1002
2da97be2aa1f Rename some variables.
Matti Hamalainen <ccr@tnsp.org>
parents: 1001
diff changeset
292 static const int dm_zlib_dist_base[32] =
949
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
293 {
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
294 1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193,
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
295 257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145, 8193, 12289,
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
296 16385, 24577, 0, 0
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
297 };
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
298
1002
2da97be2aa1f Rename some variables.
Matti Hamalainen <ccr@tnsp.org>
parents: 1001
diff changeset
299 static const int dm_zlib_dist_extra[32] =
949
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
300 {
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
301 0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 10,
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
302 10, 11, 11, 12, 12, 13, 13
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
303 };
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
304
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
305
1003
7a0c2fe22e60 Rename a function argument.
Matti Hamalainen <ccr@tnsp.org>
parents: 1002
diff changeset
306 static int dmZLibParseHuffmanBlock(DMZLibContext * ctx)
949
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
307 {
1003
7a0c2fe22e60 Rename a function argument.
Matti Hamalainen <ccr@tnsp.org>
parents: 1002
diff changeset
308 Uint8 *zout = ctx->zout;
949
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
309 for (;;)
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
310 {
958
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
311 int z, ret;
1003
7a0c2fe22e60 Rename a function argument.
Matti Hamalainen <ccr@tnsp.org>
parents: 1002
diff changeset
312 if ((ret = dmZLibHuffmanDecode(ctx, &ctx->zlength, &z)) != DMERR_OK)
958
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
313 return ret;
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
314
949
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
315 if (z < 256)
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
316 {
1003
7a0c2fe22e60 Rename a function argument.
Matti Hamalainen <ccr@tnsp.org>
parents: 1002
diff changeset
317 if (zout >= ctx->zoutEnd)
958
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
318 {
1003
7a0c2fe22e60 Rename a function argument.
Matti Hamalainen <ccr@tnsp.org>
parents: 1002
diff changeset
319 if ((ret = dmZLibExpand(ctx, zout, 1)) != DMERR_OK)
958
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
320 return ret;
949
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
321
1003
7a0c2fe22e60 Rename a function argument.
Matti Hamalainen <ccr@tnsp.org>
parents: 1002
diff changeset
322 zout = ctx->zout;
949
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
323 }
958
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
324 *zout++ = (Uint8) z;
949
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
325 }
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
326 else
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
327 {
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
328 Uint8 *p;
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
329 int len, dist;
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
330 if (z == 256)
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
331 {
1003
7a0c2fe22e60 Rename a function argument.
Matti Hamalainen <ccr@tnsp.org>
parents: 1002
diff changeset
332 ctx->zout = zout;
958
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
333 return DMERR_OK;
949
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
334 }
1069
b20922f4746f Comments, cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1062
diff changeset
335
949
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
336 z -= 257;
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
337
1002
2da97be2aa1f Rename some variables.
Matti Hamalainen <ccr@tnsp.org>
parents: 1001
diff changeset
338 len = dm_zlib_length_base[z];
2da97be2aa1f Rename some variables.
Matti Hamalainen <ccr@tnsp.org>
parents: 1001
diff changeset
339 if (dm_zlib_length_extra[z])
1003
7a0c2fe22e60 Rename a function argument.
Matti Hamalainen <ccr@tnsp.org>
parents: 1002
diff changeset
340 len += dmZReceive(ctx, dm_zlib_length_extra[z]);
949
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
341
1003
7a0c2fe22e60 Rename a function argument.
Matti Hamalainen <ccr@tnsp.org>
parents: 1002
diff changeset
342 if ((ret = dmZLibHuffmanDecode(ctx, &ctx->zdistance, &z)) != DMERR_OK)
958
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
343 return ret;
949
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
344
1002
2da97be2aa1f Rename some variables.
Matti Hamalainen <ccr@tnsp.org>
parents: 1001
diff changeset
345 dist = dm_zlib_dist_base[z];
2da97be2aa1f Rename some variables.
Matti Hamalainen <ccr@tnsp.org>
parents: 1001
diff changeset
346 if (dm_zlib_dist_extra[z])
1003
7a0c2fe22e60 Rename a function argument.
Matti Hamalainen <ccr@tnsp.org>
parents: 1002
diff changeset
347 dist += dmZReceive(ctx, dm_zlib_dist_extra[z]);
949
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
348
1003
7a0c2fe22e60 Rename a function argument.
Matti Hamalainen <ccr@tnsp.org>
parents: 1002
diff changeset
349 if (zout - ctx->zoutStart < dist)
958
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
350 {
1046
7e54b2d08ce7 Add special debug error message function/macro that can be
Matti Hamalainen <ccr@tnsp.org>
parents: 1004
diff changeset
351 return dmErrorDBG(DMERR_DATA_ERROR,
958
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
352 "Bad Huffman block distance.\n");
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
353 }
949
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
354
1003
7a0c2fe22e60 Rename a function argument.
Matti Hamalainen <ccr@tnsp.org>
parents: 1002
diff changeset
355 if (zout + len > ctx->zoutEnd)
949
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
356 {
1003
7a0c2fe22e60 Rename a function argument.
Matti Hamalainen <ccr@tnsp.org>
parents: 1002
diff changeset
357 if ((ret = dmZLibExpand(ctx, zout, len)) != DMERR_OK)
1001
198156b930d7 Fix error checking and propagation. 10L.
Matti Hamalainen <ccr@tnsp.org>
parents: 1000
diff changeset
358 return ret;
1003
7a0c2fe22e60 Rename a function argument.
Matti Hamalainen <ccr@tnsp.org>
parents: 1002
diff changeset
359 zout = ctx->zout;
949
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
360 }
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
361 p = (Uint8 *) (zout - dist);
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
362 if (dist == 1)
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
363 { // run of one byte; common in images.
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
364 Uint8 v = *p;
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
365 do { *zout++ = v; } while (--len);
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
366 }
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
367 else
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
368 {
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
369 do { *zout++ = *p++; } while (--len);
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
370 }
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
371 }
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
372 }
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
373 }
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
374
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
375
1054
d98fcb10df6a Work on dmzlib.
Matti Hamalainen <ccr@tnsp.org>
parents: 1053
diff changeset
376 #define DM_ZLIB_NCODELENGTH_SIZES 19
d98fcb10df6a Work on dmzlib.
Matti Hamalainen <ccr@tnsp.org>
parents: 1053
diff changeset
377
d98fcb10df6a Work on dmzlib.
Matti Hamalainen <ccr@tnsp.org>
parents: 1053
diff changeset
378 static const Uint8 dm_zlib_length_dezigzag[DM_ZLIB_NCODELENGTH_SIZES] =
949
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
379 {
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
380 16, 17, 18, 0, 8, 7, 9,
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
381 6 , 10, 5 , 11, 4, 12, 3,
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
382 13, 2 , 14, 1 , 15
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
383 };
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
384
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
385
999
b3b8794c4915 Rename a function argument.
Matti Hamalainen <ccr@tnsp.org>
parents: 998
diff changeset
386 static int dmZLibComputeHuffmanCodes(DMZLibContext * ctx)
949
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
387 {
1069
b20922f4746f Comments, cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1062
diff changeset
388 DMZHuffmanContext codeLengthCtx;
1054
d98fcb10df6a Work on dmzlib.
Matti Hamalainen <ccr@tnsp.org>
parents: 1053
diff changeset
389 Uint8 codeLengthSizes[DM_ZLIB_NCODELENGTH_SIZES];
1069
b20922f4746f Comments, cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1062
diff changeset
390 Uint8 codeLengths[288 + 32 + 137]; // padding for maximum single op
958
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
391 int i, n, ret;
949
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
392
1069
b20922f4746f Comments, cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1062
diff changeset
393 // Get table sizes
b20922f4746f Comments, cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1062
diff changeset
394 int hlit = dmZReceive(ctx, 5) + 257; // 288 = X
b20922f4746f Comments, cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1062
diff changeset
395 int hdist = dmZReceive(ctx, 5) + 1; // 32 = X
b20922f4746f Comments, cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1062
diff changeset
396 int hclen = dmZReceive(ctx, 4) + 4; // 19 = DM_ZLIB_NCODELENGTH_SIZES
949
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
397
1069
b20922f4746f Comments, cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1062
diff changeset
398 if (hlit > 286 || hdist > 30)
b20922f4746f Comments, cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1062
diff changeset
399 {
b20922f4746f Comments, cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1062
diff changeset
400 return dmErrorDBG(DMERR_INVALID_DATA,
b20922f4746f Comments, cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1062
diff changeset
401 "Too many Huffman length or distance symbols (%d, %d)",
b20922f4746f Comments, cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1062
diff changeset
402 hlit, hdist);
b20922f4746f Comments, cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1062
diff changeset
403 }
b20922f4746f Comments, cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1062
diff changeset
404
b20922f4746f Comments, cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1062
diff changeset
405 // Get lengths table (uninitialized entries should be set to 0)
960
1832ac20edb2 Clean up dmzlib and use it in stb_image.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
406 memset(codeLengthSizes, 0, sizeof(codeLengthSizes));
949
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
407 for (i = 0; i < hclen; i++)
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
408 {
999
b3b8794c4915 Rename a function argument.
Matti Hamalainen <ccr@tnsp.org>
parents: 998
diff changeset
409 int s = dmZReceive(ctx, 3);
1000
5df750e47721 Rename a variable.
Matti Hamalainen <ccr@tnsp.org>
parents: 999
diff changeset
410 codeLengthSizes[dm_zlib_length_dezigzag[i]] = (Uint8) s;
949
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
411 }
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
412
1069
b20922f4746f Comments, cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1062
diff changeset
413 // Inflate the code lengths table
b20922f4746f Comments, cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1062
diff changeset
414 if ((ret = dmZLibBuildHuffmanTables(&codeLengthCtx,
b20922f4746f Comments, cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1062
diff changeset
415 codeLengthSizes, DM_ZLIB_NCODELENGTH_SIZES)) != DMERR_OK)
b20922f4746f Comments, cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1062
diff changeset
416 {
b20922f4746f Comments, cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1062
diff changeset
417 return dmErrorDBG(ret,
b20922f4746f Comments, cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1062
diff changeset
418 "Invalid code lengths set.\n");
b20922f4746f Comments, cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1062
diff changeset
419 }
955
6b2f41844580 Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 954
diff changeset
420
949
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
421 n = 0;
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
422 while (n < hlit + hdist)
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
423 {
958
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
424 int c;
1069
b20922f4746f Comments, cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1062
diff changeset
425 if ((ret = dmZLibHuffmanDecode(ctx, &codeLengthCtx, &c)) != DMERR_OK)
958
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
426 return ret;
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
427
1058
e51ec592bfb6 Rename a macro.
Matti Hamalainen <ccr@tnsp.org>
parents: 1054
diff changeset
428 DMZLIB_ASSERT(c >= 0 && c < DM_ZLIB_NCODELENGTH_SIZES);
955
6b2f41844580 Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 954
diff changeset
429
949
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
430 if (c < 16)
960
1832ac20edb2 Clean up dmzlib and use it in stb_image.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
431 codeLengths[n++] = (Uint8) c;
949
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
432 else
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
433 if (c == 16)
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
434 {
1069
b20922f4746f Comments, cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1062
diff changeset
435 int bv = dmZReceive(ctx, 2) + 3;
b20922f4746f Comments, cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1062
diff changeset
436 if (n == 0)
b20922f4746f Comments, cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1062
diff changeset
437 {
b20922f4746f Comments, cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1062
diff changeset
438 return dmErrorDBG(DMERR_INVALID_DATA,
b20922f4746f Comments, cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1062
diff changeset
439 "Invalid bit length repeat.\n");
b20922f4746f Comments, cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1062
diff changeset
440 }
b20922f4746f Comments, cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1062
diff changeset
441 memset(codeLengths + n, codeLengths[n - 1], bv);
b20922f4746f Comments, cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1062
diff changeset
442 n += bv;
949
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
443 }
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
444 else
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
445 if (c == 17)
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
446 {
1069
b20922f4746f Comments, cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1062
diff changeset
447 int bv = dmZReceive(ctx, 3) + 3;
b20922f4746f Comments, cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1062
diff changeset
448 memset(codeLengths + n, 0, bv);
b20922f4746f Comments, cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1062
diff changeset
449 n += bv;
949
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
450 }
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
451 else
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
452 {
1069
b20922f4746f Comments, cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1062
diff changeset
453 int bv = dmZReceive(ctx, 7) + 11;
b20922f4746f Comments, cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1062
diff changeset
454 memset(codeLengths + n, 0, bv);
b20922f4746f Comments, cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1062
diff changeset
455 n += bv;
949
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
456 }
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
457 }
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
458
1069
b20922f4746f Comments, cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1062
diff changeset
459 if (codeLengths[256] == 0)
b20922f4746f Comments, cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1062
diff changeset
460 {
b20922f4746f Comments, cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1062
diff changeset
461 return dmErrorDBG(DMERR_DATA_ERROR,
b20922f4746f Comments, cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1062
diff changeset
462 "Invalid code -- missing end-of-block.\n");
b20922f4746f Comments, cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1062
diff changeset
463 }
b20922f4746f Comments, cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1062
diff changeset
464
949
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
465 if (n != hlit + hdist)
958
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
466 {
1046
7e54b2d08ce7 Add special debug error message function/macro that can be
Matti Hamalainen <ccr@tnsp.org>
parents: 1004
diff changeset
467 return dmErrorDBG(DMERR_DATA_ERROR,
958
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
468 "Bad huffman codelengths.\n");
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
469 }
949
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
470
1069
b20922f4746f Comments, cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1062
diff changeset
471 // Build the code tables
999
b3b8794c4915 Rename a function argument.
Matti Hamalainen <ccr@tnsp.org>
parents: 998
diff changeset
472 if ((ret = dmZLibBuildHuffmanTables(&ctx->zlength, codeLengths, hlit)) != DMERR_OK)
1070
2ee8d6b765f9 Improve error handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 1069
diff changeset
473 {
2ee8d6b765f9 Improve error handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 1069
diff changeset
474 return dmErrorDBG(DMERR_DATA_ERROR,
2ee8d6b765f9 Improve error handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 1069
diff changeset
475 "Invalid literal/lengths set.\n");
2ee8d6b765f9 Improve error handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 1069
diff changeset
476 }
949
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
477
999
b3b8794c4915 Rename a function argument.
Matti Hamalainen <ccr@tnsp.org>
parents: 998
diff changeset
478 if ((ret = dmZLibBuildHuffmanTables(&ctx->zdistance, codeLengths + hlit, hdist)) != DMERR_OK)
1070
2ee8d6b765f9 Improve error handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 1069
diff changeset
479 {
2ee8d6b765f9 Improve error handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 1069
diff changeset
480 return dmErrorDBG(DMERR_DATA_ERROR,
2ee8d6b765f9 Improve error handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 1069
diff changeset
481 "Invalid literal distances set.\n");
2ee8d6b765f9 Improve error handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 1069
diff changeset
482 }
958
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
483
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
484 return DMERR_OK;
949
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
485 }
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
486
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
487
990
76f3961a044b Fix one function name.
Matti Hamalainen <ccr@tnsp.org>
parents: 969
diff changeset
488 static int dmZLibParseUncompressedBlock(DMZLibContext * ctx)
949
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
489 {
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
490 Uint8 header[4];
1071
dd101fda037d Use size_t here.
Matti Hamalainen <ccr@tnsp.org>
parents: 1070
diff changeset
491 size_t len, nlen, k, ret;
949
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
492
997
cd0e0270e1ce Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 992
diff changeset
493 // "Any bits of input up to the next byte boundary are ignored."
965
df8d2ad98f7d Rename a function argument.
Matti Hamalainen <ccr@tnsp.org>
parents: 964
diff changeset
494 if (ctx->numBits & 7)
997
cd0e0270e1ce Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 992
diff changeset
495 dmZReceive(ctx, ctx->numBits & 7);
949
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
496
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
497 // drain the bit-packed data into header
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
498 k = 0;
965
df8d2ad98f7d Rename a function argument.
Matti Hamalainen <ccr@tnsp.org>
parents: 964
diff changeset
499 while (ctx->numBits > 0)
949
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
500 {
965
df8d2ad98f7d Rename a function argument.
Matti Hamalainen <ccr@tnsp.org>
parents: 964
diff changeset
501 header[k++] = (Uint8) (ctx->codeBuffer & 255); // suppress MSVC run-time check
df8d2ad98f7d Rename a function argument.
Matti Hamalainen <ccr@tnsp.org>
parents: 964
diff changeset
502 ctx->codeBuffer >>= 8;
df8d2ad98f7d Rename a function argument.
Matti Hamalainen <ccr@tnsp.org>
parents: 964
diff changeset
503 ctx->numBits -= 8;
949
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
504 }
1058
e51ec592bfb6 Rename a macro.
Matti Hamalainen <ccr@tnsp.org>
parents: 1054
diff changeset
505 DMZLIB_ASSERT(ctx->numBits == 0);
949
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
506
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
507 // now fill header the normal way
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
508 while (k < 4)
965
df8d2ad98f7d Rename a function argument.
Matti Hamalainen <ccr@tnsp.org>
parents: 964
diff changeset
509 header[k++] = dmZGet8(ctx);
949
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
510
958
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
511 len = (header[1] << 8) | header[0];
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
512 nlen = (header[3] << 8) | header[2];
949
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
513
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
514 if (nlen != (len ^ 0xffff))
958
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
515 {
1046
7e54b2d08ce7 Add special debug error message function/macro that can be
Matti Hamalainen <ccr@tnsp.org>
parents: 1004
diff changeset
516 return dmErrorDBG(DMERR_DATA_ERROR,
992
929e43afbdb1 Improve error message slightly.
Matti Hamalainen <ccr@tnsp.org>
parents: 991
diff changeset
517 "Compressed data corrupt %04x :: %04x [%04x].\n",
929e43afbdb1 Improve error message slightly.
Matti Hamalainen <ccr@tnsp.org>
parents: 991
diff changeset
518 nlen, len, len ^ 0xffff);
958
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
519 }
949
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
520
965
df8d2ad98f7d Rename a function argument.
Matti Hamalainen <ccr@tnsp.org>
parents: 964
diff changeset
521 if (ctx->zbuffer + len > ctx->zbufferEnd)
958
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
522 {
1046
7e54b2d08ce7 Add special debug error message function/macro that can be
Matti Hamalainen <ccr@tnsp.org>
parents: 1004
diff changeset
523 return dmErrorDBG(DMERR_BOUNDS,
958
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
524 "Read past buffer, probably corrupt compressed data.\n");
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
525 }
949
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
526
968
b522067e2beb Fix error message.
Matti Hamalainen <ccr@tnsp.org>
parents: 965
diff changeset
527 if (ctx->zout + len > ctx->zoutEnd &&
969
14b82bd5a408 Rename some functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 968
diff changeset
528 (ret = dmZLibExpand(ctx, ctx->zout, len)) != DMERR_OK)
958
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
529 {
1046
7e54b2d08ce7 Add special debug error message function/macro that can be
Matti Hamalainen <ccr@tnsp.org>
parents: 1004
diff changeset
530 return dmErrorDBG(DMERR_DATA_ERROR,
997
cd0e0270e1ce Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 992
diff changeset
531 "Could not expand output buffer: %d, %s\n",
968
b522067e2beb Fix error message.
Matti Hamalainen <ccr@tnsp.org>
parents: 965
diff changeset
532 ret, dmErrorStr(ret));
958
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
533 }
949
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
534
965
df8d2ad98f7d Rename a function argument.
Matti Hamalainen <ccr@tnsp.org>
parents: 964
diff changeset
535 memcpy(ctx->zout, ctx->zbuffer, len);
df8d2ad98f7d Rename a function argument.
Matti Hamalainen <ccr@tnsp.org>
parents: 964
diff changeset
536 ctx->zbuffer += len;
df8d2ad98f7d Rename a function argument.
Matti Hamalainen <ccr@tnsp.org>
parents: 964
diff changeset
537 ctx->zout += len;
958
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
538
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
539 return DMERR_OK;
949
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
540 }
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
541
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
542
958
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
543 int dmZLibDecode(DMZLibContext * ctx)
949
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
544 {
958
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
545 int final, type, ret;
949
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
546
958
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
547 ctx->numBits = 0;
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
548 ctx->codeBuffer = 0;
949
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
549 do
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
550 {
960
1832ac20edb2 Clean up dmzlib and use it in stb_image.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
551 final = dmZReceive(ctx, 1);
1832ac20edb2 Clean up dmzlib and use it in stb_image.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
552 type = dmZReceive(ctx, 2);
949
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
553 if (type == 0)
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
554 {
990
76f3961a044b Fix one function name.
Matti Hamalainen <ccr@tnsp.org>
parents: 969
diff changeset
555 if ((ret = dmZLibParseUncompressedBlock(ctx)) != DMERR_OK)
958
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
556 return ret;
949
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
557 }
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
558 else
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
559 if (type == 3)
1001
198156b930d7 Fix error checking and propagation. 10L.
Matti Hamalainen <ccr@tnsp.org>
parents: 1000
diff changeset
560 return DMERR_INVALID_DATA;
949
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
561 else
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
562 {
1062
d36bf7514614 Add some constants and make initialization one-time only.
Matti Hamalainen <ccr@tnsp.org>
parents: 1060
diff changeset
563 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
564 {
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
565 // use fixed code lengths
1062
d36bf7514614 Add some constants and make initialization one-time only.
Matti Hamalainen <ccr@tnsp.org>
parents: 1060
diff changeset
566 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
567 return ret;
949
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
568
1062
d36bf7514614 Add some constants and make initialization one-time only.
Matti Hamalainen <ccr@tnsp.org>
parents: 1060
diff changeset
569 if ((ret = dmZLibBuildHuffmanTables(&ctx->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
570 return ret;
949
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
571 }
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
572 else
960
1832ac20edb2 Clean up dmzlib and use it in stb_image.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
573 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
574 return ret;
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
575
960
1832ac20edb2 Clean up dmzlib and use it in stb_image.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
576 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
577 return ret;
949
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
578 }
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
579 }
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
580 while (!final);
958
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
581
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
582 return DMERR_OK;
949
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
583 }
997
cd0e0270e1ce Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 992
diff changeset
584
cd0e0270e1ce Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 992
diff changeset
585
cd0e0270e1ce Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 992
diff changeset
586 int dmZLibParseHeader(DMZLibContext * ctx, BOOL checkPNG)
cd0e0270e1ce Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 992
diff changeset
587 {
cd0e0270e1ce Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 992
diff changeset
588 // See http://tools.ietf.org/html/rfc1950
cd0e0270e1ce Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 992
diff changeset
589 int cmf = dmZGet8(ctx); // Compression method and flags
cd0e0270e1ce Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 992
diff changeset
590 int flags = dmZGet8(ctx); // Flags
cd0e0270e1ce Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 992
diff changeset
591 int cmethod = (cmf & 15);
cd0e0270e1ce Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 992
diff changeset
592 int cinfo = (cmf >> 4) & 15;
cd0e0270e1ce Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 992
diff changeset
593 ctx->window = 1 << (8 + cinfo); // Window size (not used at the moment)
cd0e0270e1ce Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 992
diff changeset
594
cd0e0270e1ce Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 992
diff changeset
595 // "The FCHECK value must be such that CMF and FLG, when viewed as
cd0e0270e1ce Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 992
diff changeset
596 // a 16-bit unsigned integer stored in MSB order (CMF*256 + FLG),
cd0e0270e1ce Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 992
diff changeset
597 // is a multiple of 31."
cd0e0270e1ce Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 992
diff changeset
598 if ((cmf * 256 + flags) % 31 != 0)
cd0e0270e1ce Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 992
diff changeset
599 {
1046
7e54b2d08ce7 Add special debug error message function/macro that can be
Matti Hamalainen <ccr@tnsp.org>
parents: 1004
diff changeset
600 return dmErrorDBG(DMERR_INVALID_DATA,
997
cd0e0270e1ce Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 992
diff changeset
601 "Bad zlib header.");
cd0e0270e1ce Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 992
diff changeset
602 }
cd0e0270e1ce Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 992
diff changeset
603
cd0e0270e1ce Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 992
diff changeset
604 // We only support compression method 8
cd0e0270e1ce Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 992
diff changeset
605 if (cmethod != 8)
cd0e0270e1ce Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 992
diff changeset
606 {
1046
7e54b2d08ce7 Add special debug error message function/macro that can be
Matti Hamalainen <ccr@tnsp.org>
parents: 1004
diff changeset
607 return dmErrorDBG(DMERR_INVALID_DATA,
997
cd0e0270e1ce Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 992
diff changeset
608 "Bad or unsupported zlib compression method %d.\n",
cd0e0270e1ce Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 992
diff changeset
609 cmethod);
cd0e0270e1ce Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 992
diff changeset
610 }
cd0e0270e1ce Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 992
diff changeset
611
cd0e0270e1ce Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 992
diff changeset
612 if (checkPNG && (flags & 32))
cd0e0270e1ce Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 992
diff changeset
613 {
cd0e0270e1ce Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 992
diff changeset
614 // 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
615 return dmErrorDBG(DMERR_NOT_SUPPORTED,
997
cd0e0270e1ce Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 992
diff changeset
616 "Preset dictionary not allowed in PNG.\n");
cd0e0270e1ce Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 992
diff changeset
617 }
cd0e0270e1ce Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 992
diff changeset
618 return DMERR_OK;
cd0e0270e1ce Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 992
diff changeset
619 }
cd0e0270e1ce Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 992
diff changeset
620