|
| 1 | +/* Buffer to buffer example - Test program for the lzlib library |
| 2 | + Copyright (C) 2010-2017 Antonio Diaz Diaz. |
| 3 | +
|
| 4 | + This program is free software: you have unlimited permission |
| 5 | + to copy, distribute and modify it. |
| 6 | +
|
| 7 | + Usage is: |
| 8 | + bbexample filename |
| 9 | +
|
| 10 | + This program is an example of how buffer-to-buffer |
| 11 | + compression/decompression can be implemented using lzlib. |
| 12 | +*/ |
| 13 | + |
| 14 | +#include <errno.h> |
| 15 | +#include <limits.h> |
| 16 | +#ifndef __cplusplus |
| 17 | +#include <stdbool.h> |
| 18 | +#endif |
| 19 | +#include <stdint.h> |
| 20 | +#include <stdio.h> |
| 21 | +#include <stdlib.h> |
| 22 | +#include <string.h> |
| 23 | +#include <unistd.h> |
| 24 | + |
| 25 | +#include "../lzlib-1.11/lzlib.h" |
| 26 | + |
| 27 | +#if 0 |
| 28 | +/* Returns the address of a malloc'd buffer containing the file data and |
| 29 | + its size in '*size'. |
| 30 | + In case of error, returns 0 and does not modify '*size'. |
| 31 | +*/ |
| 32 | +uint8_t * read_file( const char * const name, long * const size ) |
| 33 | + { |
| 34 | + long buffer_size = 1 << 20, file_size; |
| 35 | + uint8_t * buffer, * tmp; |
| 36 | + FILE * const f = fopen( name, "rb" ); |
| 37 | + if( !f ) |
| 38 | + { |
| 39 | + fprintf( stderr, "bbexample: Can't open input file '%s': %s\n", |
| 40 | + name, strerror( errno ) ); |
| 41 | + return 0; |
| 42 | + } |
| 43 | + |
| 44 | + buffer = (uint8_t *)malloc( buffer_size ); |
| 45 | + if( !buffer ) |
| 46 | + { fputs( "bbexample: Not enough memory.\n", stderr ); return 0; } |
| 47 | + file_size = fread( buffer, 1, buffer_size, f ); |
| 48 | + while( file_size >= buffer_size ) |
| 49 | + { |
| 50 | + if( buffer_size >= LONG_MAX ) |
| 51 | + { |
| 52 | + fprintf( stderr, "bbexample: Input file '%s' is too large.\n", name ); |
| 53 | + free( buffer ); return 0; |
| 54 | + } |
| 55 | + buffer_size = ( buffer_size <= LONG_MAX / 2 ) ? 2 * buffer_size : LONG_MAX; |
| 56 | + tmp = (uint8_t *)realloc( buffer, buffer_size ); |
| 57 | + if( !tmp ) |
| 58 | + { fputs( "bbexample: Not enough memory.\n", stderr ); |
| 59 | + free( buffer ); return 0; } |
| 60 | + buffer = tmp; |
| 61 | + file_size += fread( buffer + file_size, 1, buffer_size - file_size, f ); |
| 62 | + } |
| 63 | + if( ferror( f ) || !feof( f ) ) |
| 64 | + { |
| 65 | + fprintf( stderr, "bbexample: Error reading file '%s': %s\n", |
| 66 | + name, strerror( errno ) ); |
| 67 | + free( buffer ); return 0; |
| 68 | + } |
| 69 | + fclose( f ); |
| 70 | + *size = file_size; |
| 71 | + return buffer; |
| 72 | + } |
| 73 | +#endif |
| 74 | + |
| 75 | +/* Compresses 'size' bytes from 'data'. Returns the address of a |
| 76 | + malloc'd buffer containing the compressed data and its size in |
| 77 | + '*out_sizep'. |
| 78 | + In case of error, returns 0 and does not modify '*out_sizep'. |
| 79 | +*/ |
| 80 | +uint8_t * bbcompress( const uint8_t * const data, const long size, uint8_t * new_data, |
| 81 | + long * const out_sizep, int dictionary_size, int match_len_limit ) |
| 82 | + { |
| 83 | + #if 0 |
| 84 | + struct Lzma_options |
| 85 | + { |
| 86 | + int dictionary_size; /* 4 KiB .. 512 MiB */ |
| 87 | + int match_len_limit; /* 5 .. 273 */ |
| 88 | + }; |
| 89 | + /* Mapping from gzip/bzip2 style 1..9 compression modes |
| 90 | + to the corresponding LZMA compression modes. */ |
| 91 | + const struct Lzma_options option_mapping[] = |
| 92 | + { |
| 93 | + { 65535, 16 }, /* -0 (65535,16 chooses fast encoder) */ |
| 94 | + { 1 << 20, 5 }, /* -1 */ |
| 95 | + { 3 << 19, 6 }, /* -2 */ |
| 96 | + { 1 << 21, 8 }, /* -3 */ |
| 97 | + { 3 << 20, 12 }, /* -4 */ |
| 98 | + { 1 << 22, 20 }, /* -5 */ |
| 99 | + { 1 << 23, 36 }, /* -6 */ |
| 100 | + { 1 << 24, 68 }, /* -7 */ |
| 101 | + { 3 << 23, 132 }, /* -8 */ |
| 102 | + { 1 << 25, 273 } }; /* -9 */ |
| 103 | + struct Lzma_options encoder_options; |
| 104 | + #endif |
| 105 | + const unsigned long long member_size = 0x7FFFFFFFFFFFFFFFULL; /* INT64_MAX */ |
| 106 | + struct LZ_Encoder * encoder; |
| 107 | + //const long delta_size = ( size / 4 ) + 64; /* size may be zero */ |
| 108 | + long new_data_size = size; //delta_size; /* initial size */ |
| 109 | + long new_pos = 0; |
| 110 | + long written = 0; |
| 111 | + bool error = false; |
| 112 | + |
| 113 | + //if( level < 0 || level > 9 ) return 0; |
| 114 | + //encoder_options = option_mapping[level]; |
| 115 | + |
| 116 | + if( dictionary_size > size /*&& level != 0*/ ) |
| 117 | + dictionary_size = size; /* saves memory */ |
| 118 | + if( dictionary_size < LZ_min_dictionary_size() ) |
| 119 | + dictionary_size = LZ_min_dictionary_size(); |
| 120 | + encoder = LZ_compress_open( dictionary_size, |
| 121 | + match_len_limit, member_size ); |
| 122 | + if( !encoder || LZ_compress_errno( encoder ) != LZ_ok ) |
| 123 | + { LZ_compress_close( encoder ); return 0; } |
| 124 | + |
| 125 | + /*new_data = (uint8_t *)malloc( new_data_size ); |
| 126 | + if( !new_data ) |
| 127 | + { LZ_compress_close( encoder ); return 0; }*/ |
| 128 | + |
| 129 | + while( true ) |
| 130 | + { |
| 131 | + int rd; |
| 132 | + if( LZ_compress_write_size( encoder ) > 0 ) |
| 133 | + { |
| 134 | + if( written < size ) |
| 135 | + { |
| 136 | + const int wr = LZ_compress_write( encoder, data + written, |
| 137 | + size - written ); |
| 138 | + if( wr < 0 ) { error = true; break; } |
| 139 | + written += wr; |
| 140 | + } |
| 141 | + if( written >= size ) LZ_compress_finish( encoder ); |
| 142 | + } |
| 143 | + rd = LZ_compress_read( encoder, new_data + new_pos, |
| 144 | + new_data_size - new_pos ); |
| 145 | + if( rd < 0 ) { error = true; break; } |
| 146 | + new_pos += rd; |
| 147 | + if( LZ_compress_finished( encoder ) == 1 ) break; |
| 148 | + /*if( new_pos >= new_data_size ) |
| 149 | + { |
| 150 | + uint8_t * tmp; |
| 151 | + if( new_data_size > LONG_MAX - delta_size ) { error = true; break; } |
| 152 | + new_data_size += delta_size; |
| 153 | + tmp = (uint8_t *)realloc( new_data, new_data_size ); |
| 154 | + if( !tmp ) { error = true; break; } |
| 155 | + new_data = tmp; |
| 156 | + }*/ |
| 157 | + } |
| 158 | + |
| 159 | + if( LZ_compress_close( encoder ) < 0 ) error = true; |
| 160 | + if( error ) { /*free( new_data );*/ return 0; } |
| 161 | + *out_sizep = new_pos; |
| 162 | + return new_data; |
| 163 | + } |
| 164 | + |
| 165 | + |
| 166 | +/* Decompresses 'size' bytes from 'data'. Returns the address of a |
| 167 | + malloc'd buffer containing the decompressed data and its size in |
| 168 | + '*out_sizep'. |
| 169 | + In case of error, returns 0 and does not modify '*out_sizep'. |
| 170 | +*/ |
| 171 | +uint8_t * bbdecompress( const uint8_t * const data, const long size, uint8_t * new_data, |
| 172 | + long * const out_sizep ) |
| 173 | + { |
| 174 | + struct LZ_Decoder * const decoder = LZ_decompress_open(); |
| 175 | + //uint8_t * new_data; |
| 176 | + const long delta_size = size; /* size must be > zero */ |
| 177 | + long new_data_size = delta_size; /* initial size */ |
| 178 | + long new_pos = 0; |
| 179 | + long written = 0; |
| 180 | + bool error = false; |
| 181 | + if( !decoder || LZ_decompress_errno( decoder ) != LZ_ok ) |
| 182 | + { LZ_decompress_close( decoder ); return 0; } |
| 183 | + |
| 184 | + //new_data = (uint8_t *)malloc( new_data_size ); |
| 185 | + //if( !new_data ) |
| 186 | + // { LZ_decompress_close( decoder ); return 0; } |
| 187 | + |
| 188 | + while( true ) |
| 189 | + { |
| 190 | + int rd; |
| 191 | + if( LZ_decompress_write_size( decoder ) > 0 ) |
| 192 | + { |
| 193 | + if( written < size ) |
| 194 | + { |
| 195 | + const int wr = LZ_decompress_write( decoder, data + written, |
| 196 | + size - written ); |
| 197 | + if( wr < 0 ) { error = true; break; } |
| 198 | + written += wr; |
| 199 | + } |
| 200 | + if( written >= size ) LZ_decompress_finish( decoder ); |
| 201 | + } |
| 202 | + rd = LZ_decompress_read( decoder, new_data + new_pos, |
| 203 | + new_data_size - new_pos ); |
| 204 | + if( rd < 0 ) { error = true; break; } |
| 205 | + new_pos += rd; |
| 206 | + if( LZ_decompress_finished( decoder ) == 1 ) break; |
| 207 | + /*if( new_pos >= new_data_size ) |
| 208 | + { |
| 209 | + uint8_t * tmp; |
| 210 | + if( new_data_size > LONG_MAX - delta_size ) { error = true; break; } |
| 211 | + new_data_size += delta_size; |
| 212 | + tmp = (uint8_t *)realloc( new_data, new_data_size ); |
| 213 | + if( !tmp ) { error = true; break; } |
| 214 | + new_data = tmp; |
| 215 | + }*/ |
| 216 | + } |
| 217 | + |
| 218 | + if( LZ_decompress_close( decoder ) < 0 ) error = true; |
| 219 | + if( error ) { /*free( new_data );*/ return 0; } |
| 220 | + *out_sizep = new_pos; |
| 221 | + return new_data; |
| 222 | + } |
| 223 | + |
| 224 | +#if 0 |
| 225 | +int main( const int argc, const char * const argv[] ) |
| 226 | + { |
| 227 | + uint8_t * in_buffer; |
| 228 | + long in_size = 0; |
| 229 | + int level; |
| 230 | + |
| 231 | + if( argc < 2 ) |
| 232 | + { |
| 233 | + fputs( "Usage: bbexample filename\n", stderr ); |
| 234 | + return 1; |
| 235 | + } |
| 236 | + |
| 237 | + in_buffer = read_file( argv[1], &in_size ); |
| 238 | + if( !in_buffer ) return 1; |
| 239 | + |
| 240 | + for( level = 0; level <= 9; ++level ) |
| 241 | + { |
| 242 | + uint8_t * mid_buffer, * out_buffer; |
| 243 | + long mid_size = 0, out_size = 0; |
| 244 | + |
| 245 | + mid_buffer = bbcompress( in_buffer, in_size, level, &mid_size ); |
| 246 | + if( !mid_buffer ) |
| 247 | + { |
| 248 | + fputs( "bbexample: Not enough memory or compress error.\n", stderr ); |
| 249 | + return 1; |
| 250 | + } |
| 251 | + |
| 252 | + out_buffer = bbdecompress( mid_buffer, mid_size, &out_size ); |
| 253 | + if( !out_buffer ) |
| 254 | + { |
| 255 | + fputs( "bbexample: Not enough memory or decompress error.\n", stderr ); |
| 256 | + return 1; |
| 257 | + } |
| 258 | + |
| 259 | + if( in_size != out_size || |
| 260 | + ( in_size > 0 && memcmp( in_buffer, out_buffer, in_size ) != 0 ) ) |
| 261 | + { |
| 262 | + fputs( "bbexample: Decompressed data differs from original.\n", stderr ); |
| 263 | + return 1; |
| 264 | + } |
| 265 | + |
| 266 | + free( out_buffer ); |
| 267 | + free( mid_buffer ); |
| 268 | + } |
| 269 | + free( in_buffer ); |
| 270 | + return 0; |
| 271 | + } |
| 272 | +#endif |
0 commit comments