Mercurial > hg > dmlib
comparison src/dmzlib.c @ 1074:e98bc627ad08
Rename dmzlib structure members.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Mon, 02 Mar 2015 04:13:56 +0200 |
parents | f317e34246db |
children | 97ccd6d972ff |
comparison
equal
deleted
inserted
replaced
1073:f317e34246db | 1074:e98bc627ad08 |
---|---|
155 } | 155 } |
156 | 156 |
157 | 157 |
158 static inline Uint8 dmZGet8(DMZLibContext * ctx) | 158 static inline Uint8 dmZGet8(DMZLibContext * ctx) |
159 { | 159 { |
160 if (ctx->zbuffer >= ctx->zbufferEnd) | 160 if (ctx->inBuffer >= ctx->inBufferEnd) |
161 return 0; | 161 return 0; |
162 | 162 |
163 return *ctx->zbuffer++; | 163 return *ctx->inBuffer++; |
164 } | 164 } |
165 | 165 |
166 | 166 |
167 static void dmZFillBits(DMZLibContext * ctx) | 167 static void dmZFillBits(DMZLibContext * ctx) |
168 { | 168 { |
241 | 241 |
242 return dmZLibHuffmanDecodeSlow(ctx, huff, val); | 242 return dmZLibHuffmanDecodeSlow(ctx, huff, val); |
243 } | 243 } |
244 | 244 |
245 | 245 |
246 static int dmZLibExpand(DMZLibContext * ctx, Uint8 *zout, size_t n) | 246 static int dmZLibExpand(DMZLibContext * ctx, Uint8 *outBuffer, size_t n) |
247 { | 247 { |
248 Uint8 *newBuf; | 248 Uint8 *newBuf; |
249 size_t cur, limit; | 249 size_t cur, limit; |
250 | 250 |
251 ctx->zout = zout; | 251 ctx->outBuffer = outBuffer; |
252 | 252 |
253 if (!ctx->expandable) | 253 if (!ctx->expandable) |
254 { | 254 { |
255 return dmErrorDBG(DMERR_BOUNDS, | 255 return dmErrorDBG(DMERR_BOUNDS, |
256 "Output buffer limit hit, and is not expandable.\n"); | 256 "Output buffer limit hit, and is not expandable.\n"); |
257 } | 257 } |
258 | 258 |
259 cur = ctx->zout - ctx->zoutStart; | 259 cur = ctx->outBuffer - ctx->outBufferStart; |
260 limit = ctx->zoutEnd - ctx->zoutStart; | 260 limit = ctx->outBufferEnd - ctx->outBufferStart; |
261 | 261 |
262 while (cur + n > limit) | 262 while (cur + n > limit) |
263 limit *= 2; | 263 limit *= 2; |
264 | 264 |
265 if ((newBuf = (Uint8 *) dmRealloc(ctx->zoutStart, limit)) == NULL) | 265 if ((newBuf = (Uint8 *) dmRealloc(ctx->outBufferStart, limit)) == NULL) |
266 { | 266 { |
267 return dmErrorDBG(DMERR_MALLOC, | 267 return dmErrorDBG(DMERR_MALLOC, |
268 "Could not reallocate buffer.\n"); | 268 "Could not reallocate buffer.\n"); |
269 } | 269 } |
270 | 270 |
271 ctx->zoutStart = newBuf; | 271 ctx->outBufferStart = newBuf; |
272 ctx->zout = newBuf + cur; | 272 ctx->outBuffer = newBuf + cur; |
273 ctx->zoutEnd = newBuf + limit; | 273 ctx->outBufferEnd = newBuf + limit; |
274 | 274 |
275 return DMERR_OK; | 275 return DMERR_OK; |
276 } | 276 } |
277 | 277 |
278 | 278 |
303 }; | 303 }; |
304 | 304 |
305 | 305 |
306 static int dmZLibParseHuffmanBlock(DMZLibContext * ctx) | 306 static int dmZLibParseHuffmanBlock(DMZLibContext * ctx) |
307 { | 307 { |
308 Uint8 *zout = ctx->zout; | 308 Uint8 *outBuffer = ctx->outBuffer; |
309 for (;;) | 309 for (;;) |
310 { | 310 { |
311 int z, ret; | 311 int z, ret; |
312 if ((ret = dmZLibHuffmanDecode(ctx, &ctx->zlength, &z)) != DMERR_OK) | 312 if ((ret = dmZLibHuffmanDecode(ctx, &ctx->zlength, &z)) != DMERR_OK) |
313 return ret; | 313 return ret; |
314 | 314 |
315 if (z < 256) | 315 if (z < 256) |
316 { | 316 { |
317 if (zout >= ctx->zoutEnd) | 317 if (outBuffer >= ctx->outBufferEnd) |
318 { | 318 { |
319 if ((ret = dmZLibExpand(ctx, zout, 1)) != DMERR_OK) | 319 if ((ret = dmZLibExpand(ctx, outBuffer, 1)) != DMERR_OK) |
320 return ret; | 320 return ret; |
321 | 321 |
322 zout = ctx->zout; | 322 outBuffer = ctx->outBuffer; |
323 } | 323 } |
324 *zout++ = (Uint8) z; | 324 *outBuffer++ = (Uint8) z; |
325 } | 325 } |
326 else | 326 else |
327 { | 327 { |
328 Uint8 *p; | 328 Uint8 *p; |
329 int len, dist; | 329 int len, dist; |
330 if (z == 256) | 330 if (z == 256) |
331 { | 331 { |
332 ctx->zout = zout; | 332 ctx->outBuffer = outBuffer; |
333 return DMERR_OK; | 333 return DMERR_OK; |
334 } | 334 } |
335 | 335 |
336 z -= 257; | 336 z -= 257; |
337 | 337 |
344 | 344 |
345 dist = dm_zlib_dist_base[z]; | 345 dist = dm_zlib_dist_base[z]; |
346 if (dm_zlib_dist_extra[z]) | 346 if (dm_zlib_dist_extra[z]) |
347 dist += dmZReceive(ctx, dm_zlib_dist_extra[z]); | 347 dist += dmZReceive(ctx, dm_zlib_dist_extra[z]); |
348 | 348 |
349 if (zout - ctx->zoutStart < dist) | 349 if (outBuffer - ctx->outBufferStart < dist) |
350 { | 350 { |
351 return dmErrorDBG(DMERR_DATA_ERROR, | 351 return dmErrorDBG(DMERR_DATA_ERROR, |
352 "Bad Huffman block distance.\n"); | 352 "Bad Huffman block distance.\n"); |
353 } | 353 } |
354 | 354 |
355 if (zout + len > ctx->zoutEnd) | 355 if (outBuffer + len > ctx->outBufferEnd) |
356 { | 356 { |
357 if ((ret = dmZLibExpand(ctx, zout, len)) != DMERR_OK) | 357 if ((ret = dmZLibExpand(ctx, outBuffer, len)) != DMERR_OK) |
358 return ret; | 358 return ret; |
359 zout = ctx->zout; | 359 outBuffer = ctx->outBuffer; |
360 } | 360 } |
361 p = (Uint8 *) (zout - dist); | 361 p = (Uint8 *) (outBuffer - dist); |
362 if (dist == 1) | 362 if (dist == 1) |
363 { // run of one byte; common in images. | 363 { // run of one byte; common in images. |
364 Uint8 v = *p; | 364 Uint8 v = *p; |
365 do { *zout++ = v; } while (--len); | 365 do { *outBuffer++ = v; } while (--len); |
366 } | 366 } |
367 else | 367 else |
368 { | 368 { |
369 do { *zout++ = *p++; } while (--len); | 369 do { *outBuffer++ = *p++; } while (--len); |
370 } | 370 } |
371 } | 371 } |
372 } | 372 } |
373 } | 373 } |
374 | 374 |
492 | 492 |
493 // "Any bits of input up to the next byte boundary are ignored." | 493 // "Any bits of input up to the next byte boundary are ignored." |
494 if (ctx->numBits & 7) | 494 if (ctx->numBits & 7) |
495 dmZReceive(ctx, ctx->numBits & 7); | 495 dmZReceive(ctx, ctx->numBits & 7); |
496 | 496 |
497 // drain the bit-packed data into header | 497 // Get the bit-packed data into header |
498 k = 0; | 498 k = 0; |
499 while (ctx->numBits > 0) | 499 while (ctx->numBits > 0) |
500 { | 500 { |
501 header[k++] = (Uint8) (ctx->codeBuffer & 255); // suppress MSVC run-time check | 501 header[k++] = (Uint8) (ctx->codeBuffer & 255); // suppress MSVC run-time check |
502 ctx->codeBuffer >>= 8; | 502 ctx->codeBuffer >>= 8; |
518 nlen, len, len ^ 0xffff); | 518 nlen, len, len ^ 0xffff); |
519 } | 519 } |
520 | 520 |
521 if (len > 0) | 521 if (len > 0) |
522 { | 522 { |
523 if (ctx->zbuffer + len > ctx->zbufferEnd) | 523 if (ctx->inBuffer + len > ctx->inBufferEnd) |
524 { | 524 { |
525 return dmErrorDBG(DMERR_BOUNDS, | 525 return dmErrorDBG(DMERR_BOUNDS, |
526 "Read past buffer, probably corrupt compressed data.\n"); | 526 "Read past buffer, probably corrupt compressed data.\n"); |
527 } | 527 } |
528 | 528 |
529 if (ctx->zout + len > ctx->zoutEnd && | 529 if (ctx->outBuffer + len > ctx->outBufferEnd && |
530 (ret = dmZLibExpand(ctx, ctx->zout, len)) != DMERR_OK) | 530 (ret = dmZLibExpand(ctx, ctx->outBuffer, len)) != DMERR_OK) |
531 { | 531 { |
532 return dmErrorDBG(DMERR_DATA_ERROR, | 532 return dmErrorDBG(DMERR_DATA_ERROR, |
533 "Could not expand output buffer: %d, %s\n", | 533 "Could not expand output buffer: %d, %s\n", |
534 ret, dmErrorStr(ret)); | 534 ret, dmErrorStr(ret)); |
535 } | 535 } |
536 | 536 |
537 memcpy(ctx->zout, ctx->zbuffer, len); | 537 memcpy(ctx->outBuffer, ctx->inBuffer, len); |
538 ctx->zbuffer += len; | 538 ctx->inBuffer += len; |
539 ctx->zout += len; | 539 ctx->outBuffer += len; |
540 } | 540 } |
541 | 541 |
542 return DMERR_OK; | 542 return DMERR_OK; |
543 } | 543 } |
544 | 544 |