| | 698 | /** |
| | 699 | Decompress data into intialized array. |
| | 700 | |
| | 701 | \return Non-zero if successful. |
| | 702 | */ |
| | 703 | int ur_decompress( const void* data, int len, UArray* arr ) |
| | 704 | { |
| | 705 | #define BUF_LOW 32 |
| | 706 | bz_stream strm; |
| | 707 | int ok; |
| | 708 | |
| | 709 | strm.bzalloc = 0; |
| | 710 | strm.bzfree = 0; |
| | 711 | strm.opaque = 0; |
| | 712 | |
| | 713 | ok = BZ2_bzDecompressInit( &strm, 0, 0 ); |
| | 714 | if( ok != BZ_OK ) |
| | 715 | { |
| | 716 | //fprintf( stderr, "BZ2_bzDecompressInit failure (%d)", ok ); |
| | 717 | return 0; |
| | 718 | } |
| | 719 | |
| | 720 | strm.next_in = (char*) data; |
| | 721 | strm.avail_in = len; |
| | 722 | |
| | 723 | ur_arrayReserve( arr, 1, (len < BUF_LOW) ? BUF_LOW : len ); |
| | 724 | strm.next_out = arr->ptr.c; |
| | 725 | strm.avail_out = arr->avail; |
| | 726 | |
| | 727 | do |
| | 728 | { |
| | 729 | if( strm.avail_out < BUF_LOW ) |
| | 730 | { |
| | 731 | ur_arrayReserve( arr, 1, arr->avail + (2 * BUF_LOW) ); |
| | 732 | strm.next_out = arr->ptr.c + strm.total_out_lo32; |
| | 733 | strm.avail_out = arr->avail - strm.total_out_lo32; |
| | 734 | } |
| | 735 | |
| | 736 | ok = BZ2_bzDecompress( &strm ); |
| | 737 | arr->used = strm.total_out_lo32; |
| | 738 | } |
| | 739 | while( ok == BZ_OK ); |
| | 740 | |
| | 741 | BZ2_bzDecompressEnd( &strm ); |
| | 742 | |
| | 743 | return (ok == BZ_STREAM_END) ? 1 : 0; |
| | 744 | } |
| | 745 | |
| | 746 | |
| 714 | | strm.bzalloc = 0; |
| 715 | | strm.bzfree = 0; |
| 716 | | strm.opaque = 0; |
| 717 | | |
| 718 | | ok = BZ2_bzDecompressInit( &strm, 0, 0 ); |
| 719 | | if( ok == BZ_OK ) |
| 720 | | { |
| 721 | | len = cpB - cpA; |
| 722 | | strm.next_in = cpA; |
| 723 | | strm.avail_in = len; |
| 724 | | |
| 725 | | strN = ur_makeBinary( (len < BUF_LOW) ? BUF_LOW : len, &str ); |
| 726 | | strm.next_out = str->ptr.c; |
| 727 | | strm.avail_out = str->avail; |
| 728 | | |
| 729 | | while( ok == BZ_OK ) |
| 730 | | { |
| 731 | | if( strm.avail_out < BUF_LOW ) |
| 732 | | { |
| 733 | | ur_arrayReserve( str, 1, str->avail + (2 * BUF_LOW) ); |
| 734 | | strm.next_out = str->ptr.c + strm.total_out_lo32; |
| 735 | | strm.avail_out = str->avail - strm.total_out_lo32; |
| 736 | | } |
| 737 | | |
| 738 | | ok = BZ2_bzDecompress( &strm ); |
| 739 | | str->used = strm.total_out_lo32; |
| 740 | | } |
| 741 | | |
| 742 | | BZ2_bzDecompressEnd( &strm ); |
| 743 | | |
| 744 | | if( ok == BZ_STREAM_END ) |
| 745 | | { |
| 746 | | ur_initType( tos, UT_STRING ); |
| 747 | | ur_setSeries( tos, strN, 0 ); |
| 748 | | return; |
| 749 | | } |
| 750 | | ur_throwErr( UR_ERR_INTERNAL, "decompress failure (%d)", ok ); |
| 751 | | } |
| 752 | | ur_throwErr( UR_ERR_INTERNAL, "decompress init failure (%d)", ok ); |
| | 758 | strN = ur_makeBinary( 0, &str ); |
| | 759 | |
| | 760 | if( ur_decompress( cpA, cpB - cpA, str ) ) |
| | 761 | { |
| | 762 | ur_initString( tos, strN, 0 ); |
| | 763 | } |
| | 764 | else |
| | 765 | { |
| | 766 | ur_throwErr( UR_ERR_INTERNAL, "decompress failure" ); |
| | 767 | } |