Mercurial > hg > dmlib
comparison src/dmzlib.c @ 969:14b82bd5a408
Rename some functions.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Fri, 27 Feb 2015 05:32:07 +0200 |
parents | b522067e2beb |
children | 76f3961a044b |
comparison
equal
deleted
inserted
replaced
968:b522067e2beb | 969:14b82bd5a408 |
---|---|
131 | 131 |
132 return *ctx->zbuffer++; | 132 return *ctx->zbuffer++; |
133 } | 133 } |
134 | 134 |
135 | 135 |
136 static void stbi__fill_bits(DMZLibContext * ctx) | 136 static void dmZFillBits(DMZLibContext * ctx) |
137 { | 137 { |
138 do | 138 do |
139 { | 139 { |
140 DMSTBI_ASSERT(ctx->codeBuffer < (1U << ctx->numBits)); | 140 DMSTBI_ASSERT(ctx->codeBuffer < (1U << ctx->numBits)); |
141 ctx->codeBuffer |= dmZGet8(ctx) << ctx->numBits; | 141 ctx->codeBuffer |= dmZGet8(ctx) << ctx->numBits; |
148 static inline unsigned int dmZReceive(DMZLibContext * ctx, int n) | 148 static inline unsigned int dmZReceive(DMZLibContext * ctx, int n) |
149 { | 149 { |
150 unsigned int val; | 150 unsigned int val; |
151 | 151 |
152 if (ctx->numBits < n) | 152 if (ctx->numBits < n) |
153 stbi__fill_bits(ctx); | 153 dmZFillBits(ctx); |
154 | 154 |
155 val = ctx->codeBuffer & ((1 << n) - 1); | 155 val = ctx->codeBuffer & ((1 << n) - 1); |
156 ctx->codeBuffer >>= n; | 156 ctx->codeBuffer >>= n; |
157 ctx->numBits -= n; | 157 ctx->numBits -= n; |
158 | 158 |
159 return val; | 159 return val; |
160 } | 160 } |
161 | 161 |
162 | 162 |
163 static int stbi__zhuffman_decode_slowpath(DMZLibContext * ctx, DMZHuffmanContext * huff, int *val) | 163 static int dmZLibHuffmanDecodeSlow(DMZLibContext * ctx, DMZHuffmanContext * huff, int *val) |
164 { | 164 { |
165 int b, s, k; | 165 int b, s, k; |
166 *val = 0; | 166 *val = 0; |
167 | 167 |
168 // not resolved by fast table, so compute it the slow way | 168 // not resolved by fast table, so compute it the slow way |
190 | 190 |
191 return DMERR_OK; | 191 return DMERR_OK; |
192 } | 192 } |
193 | 193 |
194 | 194 |
195 static inline int stbi__zhuffman_decode(DMZLibContext * ctx, DMZHuffmanContext * huff, int *val) | 195 static inline int dmZLibHuffmanDecode(DMZLibContext * ctx, DMZHuffmanContext * huff, int *val) |
196 { | 196 { |
197 int b; | 197 int b; |
198 if (ctx->numBits < 16) | 198 if (ctx->numBits < 16) |
199 stbi__fill_bits(ctx); | 199 dmZFillBits(ctx); |
200 | 200 |
201 b = huff->fast[ctx->codeBuffer & STBI__ZFAST_MASK]; | 201 b = huff->fast[ctx->codeBuffer & STBI__ZFAST_MASK]; |
202 if (b) | 202 if (b) |
203 { | 203 { |
204 int s = b >> 9; | 204 int s = b >> 9; |
206 ctx->numBits -= s; | 206 ctx->numBits -= s; |
207 *val = b & 511; | 207 *val = b & 511; |
208 return DMERR_OK; | 208 return DMERR_OK; |
209 } | 209 } |
210 | 210 |
211 return stbi__zhuffman_decode_slowpath(ctx, huff, val); | 211 return dmZLibHuffmanDecodeSlow(ctx, huff, val); |
212 } | 212 } |
213 | 213 |
214 | 214 |
215 static int stbi__zexpand(DMZLibContext * ctx, Uint8 *zout, size_t n) | 215 static int dmZLibExpand(DMZLibContext * ctx, Uint8 *zout, size_t n) |
216 { | 216 { |
217 Uint8 *newBuf; | 217 Uint8 *newBuf; |
218 size_t cur, limit; | 218 size_t cur, limit; |
219 | 219 |
220 ctx->zout = zout; | 220 ctx->zout = zout; |
223 { | 223 { |
224 return dmError(DMERR_BOUNDS, | 224 return dmError(DMERR_BOUNDS, |
225 "Output buffer limit hit, and is not expandable.\n"); | 225 "Output buffer limit hit, and is not expandable.\n"); |
226 } | 226 } |
227 | 227 |
228 cur = ctx->zout - ctx->zoutStart; | 228 cur = ctx->zout - ctx->zoutStart; |
229 limit = ctx->zoutEnd - ctx->zoutStart; | 229 limit = ctx->zoutEnd - ctx->zoutStart; |
230 | 230 |
231 while (cur + n > limit) | 231 while (cur + n > limit) |
232 limit *= 2; | 232 limit *= 2; |
233 | 233 |
276 { | 276 { |
277 Uint8 *zout = a->zout; | 277 Uint8 *zout = a->zout; |
278 for (;;) | 278 for (;;) |
279 { | 279 { |
280 int z, ret; | 280 int z, ret; |
281 if ((ret = stbi__zhuffman_decode(a, &a->zlength, &z)) != DMERR_OK) | 281 if ((ret = dmZLibHuffmanDecode(a, &a->zlength, &z)) != DMERR_OK) |
282 return ret; | 282 return ret; |
283 | 283 |
284 if (z < 256) | 284 if (z < 256) |
285 { | 285 { |
286 if (zout >= a->zoutEnd) | 286 if (zout >= a->zoutEnd) |
287 { | 287 { |
288 if ((ret = stbi__zexpand(a, zout, 1)) != DMERR_OK) | 288 if ((ret = dmZLibExpand(a, zout, 1)) != DMERR_OK) |
289 return ret; | 289 return ret; |
290 | 290 |
291 zout = a->zout; | 291 zout = a->zout; |
292 } | 292 } |
293 *zout++ = (Uint8) z; | 293 *zout++ = (Uint8) z; |
305 | 305 |
306 len = stbi__zlength_base[z]; | 306 len = stbi__zlength_base[z]; |
307 if (stbi__zlength_extra[z]) | 307 if (stbi__zlength_extra[z]) |
308 len += dmZReceive(a, stbi__zlength_extra[z]); | 308 len += dmZReceive(a, stbi__zlength_extra[z]); |
309 | 309 |
310 if ((ret = stbi__zhuffman_decode(a, &a->zdistance, &z)) != DMERR_OK) | 310 if ((ret = dmZLibHuffmanDecode(a, &a->zdistance, &z)) != DMERR_OK) |
311 return ret; | 311 return ret; |
312 | 312 |
313 dist = stbi__zdist_base[z]; | 313 dist = stbi__zdist_base[z]; |
314 if (stbi__zdist_extra[z]) | 314 if (stbi__zdist_extra[z]) |
315 dist += dmZReceive(a, stbi__zdist_extra[z]); | 315 dist += dmZReceive(a, stbi__zdist_extra[z]); |
320 "Bad Huffman block distance.\n"); | 320 "Bad Huffman block distance.\n"); |
321 } | 321 } |
322 | 322 |
323 if (zout + len > a->zoutEnd) | 323 if (zout + len > a->zoutEnd) |
324 { | 324 { |
325 if (!stbi__zexpand(a, zout, len)) | 325 if (!dmZLibExpand(a, zout, len)) |
326 return 0; | 326 return 0; |
327 zout = a->zout; | 327 zout = a->zout; |
328 } | 328 } |
329 p = (Uint8 *) (zout - dist); | 329 p = (Uint8 *) (zout - dist); |
330 if (dist == 1) | 330 if (dist == 1) |
373 | 373 |
374 n = 0; | 374 n = 0; |
375 while (n < hlit + hdist) | 375 while (n < hlit + hdist) |
376 { | 376 { |
377 int c; | 377 int c; |
378 if ((ret = stbi__zhuffman_decode(a, &z_codelength, &c)) != DMERR_OK) | 378 if ((ret = dmZLibHuffmanDecode(a, &z_codelength, &c)) != DMERR_OK) |
379 return ret; | 379 return ret; |
380 | 380 |
381 DMSTBI_ASSERT(c >= 0 && c < 19); | 381 DMSTBI_ASSERT(c >= 0 && c < 19); |
382 | 382 |
383 if (c < 16) | 383 if (c < 16) |
456 return dmError(DMERR_BOUNDS, | 456 return dmError(DMERR_BOUNDS, |
457 "Read past buffer, probably corrupt compressed data.\n"); | 457 "Read past buffer, probably corrupt compressed data.\n"); |
458 } | 458 } |
459 | 459 |
460 if (ctx->zout + len > ctx->zoutEnd && | 460 if (ctx->zout + len > ctx->zoutEnd && |
461 (ret = stbi__zexpand(ctx, ctx->zout, len)) != DMERR_OK) | 461 (ret = dmZLibExpand(ctx, ctx->zout, len)) != DMERR_OK) |
462 { | 462 { |
463 return dmError(DMERR_DATA_ERROR, | 463 return dmError(DMERR_DATA_ERROR, |
464 "Failed to decompress enough data: %d, %s\n", | 464 "Failed to decompress enough data: %d, %s\n", |
465 ret, dmErrorStr(ret)); | 465 ret, dmErrorStr(ret)); |
466 } | 466 } |