annotate src/dmzlib.c @ 958:985225a93aeb

Add error code parameter to dmError() and dmErrorVA().
author Matti Hamalainen <ccr@tnsp.org>
date Fri, 27 Feb 2015 03:58:25 +0200
parents 6b2f41844580
children 1832ac20edb2
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
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
6 */
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
7 #include "dmzlib.h"
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
8
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
9
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
10 #define DM_ZLIB_TMPBUF_SIZE (16 * 1024)
958
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
11 #define DMSTBI_ASSERT(x) // dummy
951
1723ebe6771c Add kludge stbi__err() implementation temporarily until the error handling
Matti Hamalainen <ccr@tnsp.org>
parents: 949
diff changeset
12
1723ebe6771c Add kludge stbi__err() implementation temporarily until the error handling
Matti Hamalainen <ccr@tnsp.org>
parents: 949
diff changeset
13
949
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
14 // @TODO: should statically initialize these for optimal thread safety
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
15 static Uint8 stbi__zdefault_length[288], stbi__zdefault_distance[32];
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
16
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
17
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
18 void dmZLibInit()
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
19 {
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
20 int i;
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
21
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
22 // use <= to match clearly with spec
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
23 for (i = 0; i <= 143; i++)
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
24 stbi__zdefault_length[i] = 8;
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 for (; i <= 255; i++)
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
27 stbi__zdefault_length[i] = 9;
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
28
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
29 for (; i <= 279; i++)
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
30 stbi__zdefault_length[i] = 7;
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
31
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
32 for (; i <= 287; i++)
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
33 stbi__zdefault_length[i] = 8;
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
34
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
35 for (i = 0; i <= 31; i++)
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
36 stbi__zdefault_distance[i] = 5;
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
37 }
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
38
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
39
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
40 static inline int stbi__bit_reverse_16(int n)
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
41 {
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
42 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
43 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
44 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
45 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
46 return n;
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
47 }
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
48
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
49
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
50 static inline int stbi__bit_reverse_n(int v, int bits)
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
51 {
958
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
52 DMSTBI_ASSERT(bits <= 16);
949
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
53 // to bit reverse n bits, reverse 16 and shift
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
54 // e.g. 11 bits, bit reverse and shift away 5
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
55 return stbi__bit_reverse_16(v) >> (16 - bits);
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
56 }
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
57
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
58
958
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
59 static int stbi__zbuild_huffman(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
60 {
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
61 int i, k = 0;
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
62 int code, next_code[16], sizes[17];
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
63
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
64 // DEFLATE spec for generating codes
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
65 memset(sizes, 0, sizeof(sizes));
958
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
66 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
67
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
68 for (i = 0; i < num; i++)
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
69 sizes[sizelist[i]]++;
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
70
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
71 sizes[0] = 0;
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
72 for (i = 1; i < 16; i++)
954
88cbea0ee9b5 Cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 951
diff changeset
73 {
88cbea0ee9b5 Cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 951
diff changeset
74 if (sizes[i] > (1 << i))
958
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
75 {
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
76 return dmError(DMERR_INTERNAL,
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
77 "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
78 }
954
88cbea0ee9b5 Cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 951
diff changeset
79 }
949
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
80
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
81 code = 0;
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
82 for (i = 1; i < 16; i++)
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
83 {
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
84 next_code[i] = code;
958
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
85 ctx->firstCode[i] = (Uint16) code;
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
86 ctx->firstSymbol[i] = (Uint16) k;
949
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
87 code = (code + sizes[i]);
954
88cbea0ee9b5 Cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 951
diff changeset
88
88cbea0ee9b5 Cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 951
diff changeset
89 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
90 {
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
91 return dmError(DMERR_INVALID_DATA,
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
92 "Bad Huffman code lengths.\n");
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
93 }
954
88cbea0ee9b5 Cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 951
diff changeset
94
958
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
95 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
96 code <<= 1;
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
97 k += sizes[i];
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
98 }
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
99
958
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
100 ctx->maxCode[16] = 0x10000; // sentinel
949
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
101 for (i = 0; i < num; i++)
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
102 {
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
103 int s = sizelist[i];
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
104 if (s)
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
105 {
958
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
106 int c = next_code[s] - ctx->firstCode[s] + ctx->firstSymbol[s];
949
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
107 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
108 ctx->size[c] = (Uint8) s;
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
109 ctx->value[c] = (Uint16) i;
949
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
110 if (s <= STBI__ZFAST_BITS)
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
111 {
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
112 int k = stbi__bit_reverse_n(next_code[s], s);
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
113 while (k < STBI__ZFAST_SIZE)
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
114 {
958
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
115 ctx->fast[k] = fastv;
949
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
116 k += (1 << s);
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
117 }
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
118 }
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
119 ++next_code[s];
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
120 }
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
121 }
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
122 return 1;
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
123 }
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
124
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
125
958
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
126 static inline Uint8 stbi__zget8(DMZLibContext * ctx)
949
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
127 {
958
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
128 if (ctx->zbuffer >= ctx->zbufferEnd)
949
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
129 return 0;
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
130
958
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
131 return *ctx->zbuffer++;
949
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
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
134
958
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
135 static void stbi__fill_bits(DMZLibContext * ctx)
949
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
136 {
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
137 do
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
138 {
958
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
139 DMSTBI_ASSERT(ctx->codeBuffer < (1U << ctx->numBits));
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
140 ctx->codeBuffer |= stbi__zget8(ctx) << ctx->numBits;
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
141 ctx->numBits += 8;
949
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
142 }
958
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
143 while (ctx->numBits <= 24);
949
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
144 }
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
145
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
146
958
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
147 static inline unsigned int stbi__zreceive(DMZLibContext * ctx, int n)
949
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
148 {
958
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
149 unsigned int val;
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
150
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
151 if (ctx->numBits < n)
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
152 stbi__fill_bits(ctx);
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
153
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
154 val = ctx->codeBuffer & ((1 << n) - 1);
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
155 ctx->codeBuffer >>= n;
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
156 ctx->numBits -= n;
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
157
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
158 return val;
949
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
159 }
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
160
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
161
958
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
162 static int stbi__zhuffman_decode_slowpath(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
163 {
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
164 int b, s, k;
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 // 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
167 // use jpeg approach, which requires MSbits at top
958
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
168 k = stbi__bit_reverse_16(ctx->codeBuffer);
949
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
169 for (s = STBI__ZFAST_BITS + 1; ; s++)
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
170 {
958
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
171 if (k < huff->maxCode[s])
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
172 break;
949
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
173 }
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
174
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
175 if (s == 16)
958
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
176 {
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
177 return dmError(DMERR_DATA_ERROR,
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
178 "Bad Huffman code.\n");
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
179 }
949
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
180
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
181 // code size is s, so:
958
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
182 b = (k >> (16 - s)) - huff->firstCode[s] + huff->firstSymbol[s];
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
183 DMSTBI_ASSERT(huff->size[b] == s);
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
184
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
185 ctx->codeBuffer >>= s;
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
186 ctx->numBits -= s;
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
187 *val = huff->value[b];
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
188
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
189 return DMERR_OK;
949
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
190 }
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
191
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
192
958
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
193 static inline int stbi__zhuffman_decode(DMZLibContext * ctx, DMZHuffmanContext * huff, int *val)
949
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
194 {
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
195 int b;
958
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
196 if (ctx->numBits < 16)
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
197 stbi__fill_bits(ctx);
949
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
198
958
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
199 b = huff->fast[ctx->codeBuffer & STBI__ZFAST_MASK];
949
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
200 if (b)
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
201 {
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
202 int s = b >> 9;
958
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
203 ctx->codeBuffer >>= s;
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
204 ctx->numBits -= s;
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
205 *val = b & 511;
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
206 return DMERR_OK;
949
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
207 }
958
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
208
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
209 return stbi__zhuffman_decode_slowpath(ctx, huff, val);
949
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
210 }
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
211
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
212
958
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
213 static int stbi__zexpand(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
214 {
958
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
215 Uint8 *newBuf;
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
216 size_t cur, limit;
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->zout = zout;
949
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
219
958
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
220 if (!ctx->expandable)
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 dmError(DMERR_BOUNDS,
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
223 "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
224 }
949
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
225
958
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
226 cur = ctx->zout - ctx->zoutStart;
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
227 limit = ctx->zoutEnd - ctx->zoutStart;
949
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
228
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
229 while (cur + n > limit)
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
230 limit *= 2;
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
231
958
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
232 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
233 {
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
234 return dmError(DMERR_MALLOC,
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
235 "Could not reallocate buffer.\n");
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
236 }
949
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
237
958
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
238 ctx->zoutStart = newBuf;
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
239 ctx->zout = newBuf + cur;
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
240 ctx->zoutEnd = newBuf + limit;
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
241
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
242 return DMERR_OK;
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
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
246 static const int stbi__zlength_base[31] =
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
247 {
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
248 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
249 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
250 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
251 };
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
252
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
253 static const int stbi__zlength_extra[31] =
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
254 {
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
255 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
256 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
257 };
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
258
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
259 static const int stbi__zdist_base[32] =
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
260 {
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
261 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
262 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
263 16385, 24577, 0, 0
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
264 };
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
265
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
266 static const int stbi__zdist_extra[32] =
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
267 {
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
268 0, 0, 0, 0, 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
269 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
270 };
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
271
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
272
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
273 static int stbi__parse_huffman_block(DMZLibContext * a)
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
274 {
958
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
275 Uint8 *zout = a->zout;
949
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
276 for (;;)
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
277 {
958
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
278 int z, ret;
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
279 if ((ret = stbi__zhuffman_decode(a, &a->zlength, &z)) != DMERR_OK)
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
280 return ret;
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
281
949
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
282 if (z < 256)
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
283 {
958
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
284 if (zout >= a->zoutEnd)
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
285 {
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
286 if ((ret = stbi__zexpand(a, zout, 1)) != DMERR_OK)
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
287 return ret;
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 zout = a->zout;
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
290 }
958
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
291 *zout++ = (Uint8) z;
949
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
292 }
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
293 else
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 Uint8 *p;
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
296 int len, dist;
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
297 if (z == 256)
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 a->zout = zout;
958
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
300 return DMERR_OK;
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 z -= 257;
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 len = stbi__zlength_base[z];
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
305 if (stbi__zlength_extra[z])
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
306 len += stbi__zreceive(a, stbi__zlength_extra[z]);
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
307
958
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
308 if ((ret = stbi__zhuffman_decode(a, &a->zdistance, &z)) != DMERR_OK)
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
309 return ret;
949
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
310
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
311 dist = stbi__zdist_base[z];
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
312 if (stbi__zdist_extra[z])
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
313 dist += stbi__zreceive(a, stbi__zdist_extra[z]);
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
314
958
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
315 if (zout - a->zoutStart < dist)
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
316 {
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
317 return dmError(DMERR_DATA_ERROR,
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
318 "Bad Huffman block distance.\n");
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
319 }
949
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
320
958
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
321 if (zout + len > a->zoutEnd)
949
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
322 {
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
323 if (!stbi__zexpand(a, zout, len))
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
324 return 0;
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
325 zout = a->zout;
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 p = (Uint8 *) (zout - dist);
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
328 if (dist == 1)
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
329 { // 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
330 Uint8 v = *p;
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
331 do { *zout++ = v; } while (--len);
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
332 }
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
333 else
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
334 {
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
335 do { *zout++ = *p++; } while (--len);
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
336 }
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
337 }
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
338 }
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
339 }
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
340
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
341
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
342 static const Uint8 length_dezigzag[19] =
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
343 {
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
344 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
345 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
346 13, 2 , 14, 1 , 15
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
347 };
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
348
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
349
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
350 static int stbi__compute_huffman_codes(DMZLibContext * a)
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
351 {
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
352 DMZHuffmanContext z_codelength;
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
353 Uint8 lencodes[286 + 32 + 137]; //padding for maximum single op
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
354 Uint8 codelength_sizes[19];
958
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
355 int i, n, ret;
949
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
356
958
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
357 int hlit = stbi__zreceive(a, 5) + 257;
949
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
358 int hdist = stbi__zreceive(a, 5) + 1;
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
359 int hclen = stbi__zreceive(a, 4) + 4;
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 memset(codelength_sizes, 0, sizeof(codelength_sizes));
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
362
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
363 for (i = 0; i < hclen; i++)
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
364 {
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
365 int s = stbi__zreceive(a, 3);
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
366 codelength_sizes[length_dezigzag[i]] = (Uint8) s;
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
367 }
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
368
958
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
369 if ((ret = stbi__zbuild_huffman(&z_codelength, codelength_sizes, 19)) != DMERR_OK)
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
370 return ret;
955
6b2f41844580 Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 954
diff changeset
371
949
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
372 n = 0;
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
373 while (n < hlit + hdist)
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
374 {
958
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
375 int c;
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
376 if ((ret = stbi__zhuffman_decode(a, &z_codelength, &c)) != DMERR_OK)
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
377 return ret;
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
378
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
379 DMSTBI_ASSERT(c >= 0 && c < 19);
955
6b2f41844580 Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 954
diff changeset
380
949
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
381 if (c < 16)
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
382 lencodes[n++] = (Uint8) c;
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
383 else
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
384 if (c == 16)
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
385 {
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
386 c = stbi__zreceive(a, 2) + 3;
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
387 memset(lencodes + n, lencodes[n - 1], c);
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
388 n += c;
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
389 }
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
390 else
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
391 if (c == 17)
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
392 {
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
393 c = stbi__zreceive(a, 3) + 3;
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
394 memset(lencodes + n, 0, c);
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
395 n += c;
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
396 }
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
397 else
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
398 {
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
399 c = stbi__zreceive(a, 7) + 11;
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
400 memset(lencodes + n, 0, c);
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
401 n += c;
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
402 }
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
403 }
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
404
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
405 if (n != hlit + hdist)
958
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
406 {
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
407 return dmError(DMERR_DATA_ERROR,
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
408 "Bad huffman codelengths.\n");
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
409 }
949
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
410
958
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
411 if ((ret = stbi__zbuild_huffman(&a->zlength, lencodes, hlit)) != DMERR_OK)
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
412 return ret;
949
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
413
958
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
414 if ((ret = stbi__zbuild_huffman(&a->zdistance, lencodes + hlit, hdist)) != DMERR_OK)
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
415 return ret;
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
416
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
417 return DMERR_OK;
949
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
418 }
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
419
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
420
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
421 static int stbi__parse_uncompressed_block(DMZLibContext * a)
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
422 {
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
423 Uint8 header[4];
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
424 int len, nlen, k;
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
425
958
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
426 if (a->numBits & 7)
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
427 stbi__zreceive(a, a->numBits & 7); // discard
949
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
428
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
429 // 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
430 k = 0;
958
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
431 while (a->numBits > 0)
949
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
432 {
958
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
433 header[k++] = (Uint8) (a->codeBuffer & 255); // suppress MSVC run-time check
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
434 a->codeBuffer >>= 8;
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
435 a->numBits -= 8;
949
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
436 }
958
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
437 DMSTBI_ASSERT(a->numBits == 0);
949
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
438
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
439 // now fill header the normal way
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
440 while (k < 4)
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
441 header[k++] = stbi__zget8(a);
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
442
958
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
443 len = (header[1] << 8) | header[0];
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
444 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
445
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
446 if (nlen != (len ^ 0xffff))
958
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
447 {
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
448 return dmError(DMERR_DATA_ERROR,
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
449 "Compressed data corrupt.\n");
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
450 }
949
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
451
958
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
452 if (a->zbuffer + len > a->zbufferEnd)
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
453 {
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
454 return dmError(DMERR_BOUNDS,
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
455 "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
456 }
949
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
457
958
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
458 if (a->zout + len > a->zoutEnd && !stbi__zexpand(a, a->zout, len))
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
459 {
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
460 return dmError(DMERR_DATA_ERROR,
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
461 "XXXX TODO");
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
462 }
949
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
463
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
464 memcpy(a->zout, a->zbuffer, len);
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
465 a->zbuffer += len;
958
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
466 a->zout += len;
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
467
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
468 return DMERR_OK;
949
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
469 }
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
470
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
471
958
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
472 int dmZLibParseHeader(DMZLibContext * ctx, BOOL checkPNG)
949
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
473 {
958
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
474 int cmf = stbi__zget8(ctx);
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
475 int flags = stbi__zget8(ctx);
949
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
476 int cm = cmf & 15;
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
477 // int cinfo = cmf >> 4;
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
478
958
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
479 if ((cmf * 256 + flags) % 31 != 0)
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
480 {
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
481 return dmError(DMERR_INVALID_DATA,
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
482 "Bad zlib header.");
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
483 }
949
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
484
958
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
485 if (checkPNG && (flags & 32))
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
486 {
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
487 // preset dictionary not allowed in png
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
488 return dmError(DMERR_NOT_SUPPORTED,
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
489 "Preset dictionary not allowed in PNG.\n");
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
490 }
949
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
491
958
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
492 if (checkPNG && cm != 8)
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
493 {
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
494 return dmError(DMERR_INVALID_DATA,
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
495 "Bad or unsupported compression type.\n");
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
496 }
949
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
497
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
498 // window = 1 << (8 + cinfo)... but who cares, we fully buffer output
958
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
499 return DMERR_OK;
949
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
500 }
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
501
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
502
958
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
503 int dmZLibDecode(DMZLibContext * ctx)
949
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
504 {
958
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
505 int final, type, ret;
949
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
506
958
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
507 ctx->numBits = 0;
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
508 ctx->codeBuffer = 0;
949
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
509 do
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 final = stbi__zreceive(ctx, 1);
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
512 type = stbi__zreceive(ctx, 2);
949
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
513 if (type == 0)
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 if ((ret = stbi__parse_uncompressed_block(ctx)) != DMERR_OK)
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
516 return ret;
949
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
517 }
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
518 else
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
519 if (type == 3)
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
520 return 0;
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
521 else
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
522 {
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
523 if (type == 1)
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
524 {
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
525 // use fixed code lengths
958
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
526 if ((ret = stbi__zbuild_huffman(&ctx->zlength, stbi__zdefault_length, 288)) != DMERR_OK)
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
527 return ret;
949
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
528
958
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
529 if ((ret = stbi__zbuild_huffman(&ctx->zdistance, stbi__zdefault_distance, 32)) != DMERR_OK)
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
530 return ret;
949
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
531 }
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
532 else
958
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
533 if ((ret = stbi__compute_huffman_codes(ctx)) != DMERR_OK)
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
534 return ret;
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
535
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
536 if ((ret = stbi__parse_huffman_block(ctx)) != DMERR_OK)
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
537 return ret;
949
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
538 }
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
539 }
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
540 while (!final);
958
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
541
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
542 return DMERR_OK;
949
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
543 }
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
544
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
545
958
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
546 Uint8 *stbi_zlib_decode_malloc_guesssize_headerflag(
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
547 const Uint8 *buffer, const size_t len,
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
548 const size_t initialSize, size_t *outLen,
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
549 BOOL parseHeader)
949
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
550 {
958
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
551 DMZLibContext ctx;
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
552 Uint8 *outBuf;
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
553 int ret;
949
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
554
958
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
555 if ((outBuf = dmMalloc(initialSize)) == NULL)
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
556 return NULL;
949
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
557
958
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
558 ctx.zbuffer = (Uint8 *) buffer;
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
559 ctx.zbufferEnd = (Uint8 *) buffer + len;
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
560 ctx.zout = outBuf;
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
561 ctx.zoutStart = outBuf;
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
562 ctx.zoutEnd = outBuf + initialSize;
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
563 ctx.expandable = TRUE;
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
564
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
565 if (parseHeader && (ret = dmZLibParseHeader(&ctx, TRUE)) != DMERR_OK)
949
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
566 {
958
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
567 return dmError(ret,
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
568 "Failed to parse zlib header data.\n");
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
569 }
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
570
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
571 if ((ret = dmZLibDecode(&ctx)) != DMERR_OK)
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
572 {
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
573 if (outLen)
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
574 *outLen = ctx.zout - ctx.zoutStart;
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
575
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
576 return ctx.zoutStart;
949
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
577 }
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
578 else
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
579 {
958
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
580 dmFree(ctx.zoutStart);
949
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
581 return NULL;
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
582 }
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
583 }
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
584
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
585
958
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
586 #if 0
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
587
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
588 Uint8 *stbi_zlib_decode_malloc(Uint8 const *buffer, int len, int *outlen)
949
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
589 {
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
590 return stbi_zlib_decode_malloc_guesssize(buffer, len, DM_ZLIB_TMPBUF_SIZE, outlen);
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
591 }
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
592
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
593
958
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
594 int stbi_zlib_decode_buffer(Uint8 *obuffer, size_t olen, Uint8 const *ibuffer, size_t ilen, size_t *res)
949
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
595 {
958
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
596 DMZLibContext ctx;
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
597 int ret;
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
598
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
599 ctx.zbuffer = (Uint8 *) ibuffer;
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
600 ctx.zbufferEnd = (Uint8 *) ibuffer + ilen;
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
601
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
602 if ((ret = stbi__do_zlib(&a, obuffer, olen, 0, 1)) != DMERR_OK)
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
603 {
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
604 *res = a.zout - a.zoutStart;
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
605 return DMERR_OK;
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
606 }
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
607 else
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 }
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
610
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
611
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
612 Uint8 *stbi_zlib_decode_noheader_malloc(Uint8 const *buffer, int len, size_t *outlen)
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
613 {
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
614 DMZLibContext ctx;
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
615 Uint8 *p = (Uint8 *) dmMalloc(DM_ZLIB_TMPBUF_SIZE);
949
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
616 if (p == NULL)
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
617 return NULL;
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
618
958
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
619 ctx.zbuffer = (Uint8 *) buffer;
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
620 ctx.zbufferEnd = (Uint8 *) buffer + len;
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
621
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
622 if (stbi__do_zlib(&ctx, p, DM_ZLIB_TMPBUF_SIZE, 1, 0))
949
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
623 {
958
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
624 if (outlen != NULL)
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
625 *outlen = (int) (ctx.zout - a.zoutStart);
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
626 return a.zoutStart;
949
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
627 }
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
628 else
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
629 {
958
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
630 dmFree(a.zoutStart);
949
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
631 return NULL;
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
632 }
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
633 }
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
634
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
635
958
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
636 int stbi_zlib_decode_noheader_buffer(Uint8 *obuffer, int olen, const Uint8 *ibuffer, int ilen)
949
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
637 {
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
638 DMZLibContext a;
958
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
639
949
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
640 a.zbuffer = (Uint8 *) ibuffer;
958
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
641 a.zbufferEnd = (Uint8 *) ibuffer + ilen;
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
642
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
643 if (stbi__do_zlib(&a, obuffer, olen, 0, 0))
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
644 return (int) (a.zout - a.zoutStart);
949
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
645 else
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
646 return -1;
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
647 }
6ed9465f3913 Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
648
958
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 955
diff changeset
649 #endif