| 495 | } |
| 496 | |
| 497 | bool TinyDictZStream::readChunk( unsigned n ) |
| 498 | { |
| 499 | if ( n >= chunkCount ) |
| 500 | return false; |
| 501 | if ( !unp_buffer ) { |
| 502 | unp_buffer = (unsigned char *)malloc( sizeof(unsigned char)*chunkLength ); |
| 503 | unp_buffer_size = chunkLength; |
| 504 | } |
| 505 | unp_buffer_start = n * chunkLength; |
| 506 | |
| 507 | if ( fseek( f, offsets[ n ], SEEK_SET ) ) { |
| 508 | printf( "cannot seek to %d position\n", offsets[n] ); |
| 509 | return false; |
| 510 | } |
| 511 | unsigned packsz = chunks[n]; |
| 512 | unsigned char * tmp = (unsigned char *)malloc( sizeof(unsigned char) * packsz ); |
| 513 | |
| 514 | crc.reset(); |
| 515 | unsigned int bytesRead = readBytes( tmp, packsz ); |
| 516 | unsigned crc1 = crc.get(); |
| 517 | unsigned crc2 = readU32(); |
| 518 | if ( bytesRead != packsz || error ) { |
| 519 | printf( "error reading packed data\n" ); |
| 520 | free( tmp ); |
| 521 | return false; |
| 522 | } |
| 523 | if ( crc1!=crc2 ) { |
| 524 | printf( "CRC error: real: %08x expected: %08x\n", crc1, crc2 ); |
| 525 | //free( tmp ); |
| 526 | //return false; |
| 527 | } |
| 528 | zclose(); |
| 529 | if ( !zinit(tmp, packsz, unp_buffer, unp_buffer_size) ) { |
| 530 | printf("cannot init deflater\n"); |
| 531 | return false; |
| 532 | } |
| 533 | printf("unpacking %d bytes\n", packsz); |
| 534 | int err = inflate( &zStream, Z_PARTIAL_FLUSH ); |
| 535 | printf("inflate result: %d\n", err); |
| 536 | if ( err != Z_OK ) { |
| 537 | printf("Inflate error %s (%d). avail_in=%d, avail_out=%d \n", zStream.msg, err, (int)zStream.avail_in, (int)zStream.avail_out); |
| 538 | free( tmp ); |
| 539 | return false; |
| 540 | } |
| 541 | if ( zStream.avail_in ) { |
| 542 | printf("Inflate: not all data read, still %d bytes available\n", (int)zStream.avail_in ); |
| 543 | free( tmp ); |
| 544 | return false; |
| 545 | } |
| 546 | unp_buffer_len = unp_buffer_size - zStream.avail_out; |
| 547 | |
| 548 | printf("freeing tmp\n"); |
| 549 | free( tmp ); |
| 550 | printf("done\n"); |
| 551 | |
| 552 | |
| 553 | |
| 554 | if ( n < chunkCount-1 && unp_buffer_len!=chunkLength ) { |