summaryrefslogtreecommitdiffstats
path: root/lzlib.cc
blob: c64ed434458b5039b54d31aec6267037c334884e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
/*  Lzlib - A compression library for lzip files
    Copyright (C) 2009 Antonio Diaz Diaz.

    This library is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.

    As a special exception, you may use this file as part of a free
    software library without restriction.  Specifically, if other files
    instantiate templates or use macros or inline functions from this
    file, or you compile this file and link it with other files to
    produce an executable, this file does not by itself cause the
    resulting executable to be covered by the GNU General Public
    License.  This exception does not however invalidate any other
    reasons why the executable file might be covered by the GNU General
    Public License.
*/

#include <algorithm>
#include <cstring>
#include <vector>
#include <stdint.h>

#include "lzlib.h"
#include "lzip.h"
#include "decoder.h"
#include "encoder.h"


namespace {

struct Encoder
  {
  long long partial_in_size;
  long long partial_out_size;
  Matchfinder * matchfinder;
  LZ_encoder * lz_encoder;
  LZ_errno lz_errno;
  bool flush_pending;
  const File_header member_header;

  Encoder( const File_header & header ) throw()
    :
    partial_in_size( 0 ),
    partial_out_size( 0 ),
    matchfinder( 0 ),
    lz_encoder( 0 ),
    lz_errno( LZ_ok ),
    flush_pending( false ),
    member_header( header )
    {}
  };


bool verify_encoder( void * const encoder )
  {
  if( !encoder ) return false;
  Encoder & e = *(Encoder *)encoder;
  if( !e.matchfinder || !e.lz_encoder )
    { e.lz_errno = LZ_bad_argument; return false; }
  return true;
  }


struct Decoder
  {
  long long partial_in_size;
  long long partial_out_size;
  Input_buffer * ibuf;
  LZ_decoder * lz_decoder;
  LZ_errno lz_errno;

  Decoder() throw()
    :
    partial_in_size( 0 ),
    partial_out_size( 0 ),
    ibuf( 0 ),
    lz_decoder( 0 ),
    lz_errno( LZ_ok )
    {}
  };


bool verify_decoder( void * const decoder )
  {
  if( !decoder ) return false;
  if( !((Decoder *)decoder)->ibuf )
    { ((Decoder *)decoder)->lz_errno = LZ_bad_argument; return false; }
  return true;
  }

} // end namespace




const char * LZ_version() { return LZ_version_string; }


void * LZ_compress_open( const int dictionary_size, const int match_len_limit,
                         const long long member_size )
  {
  File_header header;
  header.set_magic();
  const bool error = ( !header.dictionary_size( dictionary_size ) ||
                       match_len_limit < 5 || match_len_limit > max_match_len );

  Encoder * encoder = new( std::nothrow ) Encoder( header );
  if( !encoder ) return 0;
  Encoder & e = *encoder;
  if( error ) e.lz_errno = LZ_bad_argument;
  else
    {
    try {
      e.matchfinder = new Matchfinder( header.dictionary_size(), match_len_limit );
      }
    catch( std::bad_alloc ) { e.matchfinder = 0; }
    if( e.matchfinder )
      {
      try {
        e.lz_encoder = new LZ_encoder( *e.matchfinder, header, member_size );
        }
      catch( std::bad_alloc )
        {
        delete e.matchfinder;
        e.matchfinder = 0;
        e.lz_encoder = 0;
        }
      }
    if( !e.lz_encoder ) e.lz_errno = LZ_mem_error;
    }
  return encoder;
  }


int LZ_compress_restart_member( void * const encoder,
                                const long long member_size )
  {
  if( !verify_encoder( encoder ) ) return -1;
  Encoder & e = *(Encoder *)encoder;
  if( !e.lz_encoder->member_finished() )
    { e.lz_errno = LZ_sequence_error; return -1; }

  e.partial_in_size += e.matchfinder->data_position();
  e.partial_out_size += e.lz_encoder->member_position();
  e.matchfinder->reset();

  delete e.lz_encoder;
  try {
    e.lz_encoder = new LZ_encoder( *e.matchfinder, e.member_header, member_size );
    }
  catch( std::bad_alloc )
    { e.lz_encoder = 0; e.lz_errno = LZ_mem_error; return -1; }
  return 0;
  }


int LZ_compress_close( void * const encoder )
  {
  if( !encoder ) return -1;
  Encoder & e = *(Encoder *)encoder;
  if( e.lz_encoder ) delete e.lz_encoder;
  if( e.matchfinder ) delete e.matchfinder;
  delete (Encoder *)encoder;
  return 0;
  }


int LZ_compress_finish( void * const encoder )
  {
  if( !verify_encoder( encoder ) ) return -1;
  Encoder & e = *(Encoder *)encoder;
  e.matchfinder->flushing( true );
  e.flush_pending = false;
  return 0;
  }


int LZ_compress_sync_flush( void * const encoder )
  {
  if( !verify_encoder( encoder ) ) return -1;
  Encoder & e = *(Encoder *)encoder;
  if( !e.flush_pending && !e.matchfinder->at_stream_end() )
    {
    e.flush_pending = true;
    e.matchfinder->flushing( true );
    if( !e.lz_encoder->encode_member( false ) )
      { e.lz_errno = LZ_library_error; return -1; }
    if( e.lz_encoder->sync_flush() )
      { e.matchfinder->flushing( false ); e.flush_pending = false; }
    }
  return 0;
  }


int LZ_compress_read( void * const encoder, uint8_t * const buffer,
                      const int size )
  {
  if( !verify_encoder( encoder ) ) return -1;
  Encoder & e = *(Encoder *)encoder;
  if( !e.lz_encoder->encode_member( !e.flush_pending ) )
    { e.lz_errno = LZ_library_error; return -1; }
  if( e.flush_pending && e.lz_encoder->sync_flush() )
    { e.matchfinder->flushing( false ); e.flush_pending = false; }
  return e.lz_encoder->read_data( buffer, size );
  }


int LZ_compress_write( void * const encoder, uint8_t * const buffer,
                       const int size )
  {
  if( !verify_encoder( encoder ) ) return -1;
  Encoder & e = *(Encoder *)encoder;
  if( e.flush_pending ) return 0;
  return e.matchfinder->write_data( buffer, size );
  }


int LZ_compress_write_size( void * const encoder )
  {
  if( !verify_encoder( encoder ) ) return -1;
  Encoder & e = *(Encoder *)encoder;
  if( e.flush_pending ) return 0;
  return e.matchfinder->free_bytes();
  }


enum LZ_errno LZ_compress_errno( void * const encoder )
  {
  if( !encoder ) return LZ_bad_argument;
  return ((Encoder *)encoder)->lz_errno;
  }


int LZ_compress_finished( void * const encoder )
  {
  if( !verify_encoder( encoder ) ) return -1;
  Encoder & e = *(Encoder *)encoder;
  return ( !e.flush_pending && e.matchfinder->finished() &&
           e.lz_encoder->member_finished() );
  }


int LZ_compress_member_finished( void * const encoder )
  {
  if( !verify_encoder( encoder ) ) return -1;
  return ((Encoder *)encoder)->lz_encoder->member_finished();
  }


long long LZ_compress_data_position( void * const encoder )
  {
  if( !verify_encoder( encoder ) ) return -1;
  return ((Encoder *)encoder)->matchfinder->data_position();
  }


long long LZ_compress_member_position( void * const encoder )
  {
  if( !verify_encoder( encoder ) ) return -1;
  return ((Encoder *)encoder)->lz_encoder->member_position();
  }


long long LZ_compress_total_in_size( void * const encoder )
  {
  if( !verify_encoder( encoder ) ) return -1;
  Encoder & e = *(Encoder *)encoder;
  return e.partial_in_size + e.matchfinder->data_position();
  }


long long LZ_compress_total_out_size( void * const encoder )
  {
  if( !verify_encoder( encoder ) ) return -1;
  Encoder & e = *(Encoder *)encoder;
  return e.partial_out_size + e.lz_encoder->member_position();
  }


void * LZ_decompress_open()
  {
  Decoder * decoder = new( std::nothrow ) Decoder();
  if( !decoder ) return 0;

  try { decoder->ibuf = new Input_buffer(); }
  catch( std::bad_alloc )
    { decoder->ibuf = 0; decoder->lz_errno = LZ_mem_error; }
  return decoder;
  }


int LZ_decompress_close( void * const decoder )
  {
  if( !decoder ) return -1;
  Decoder & d = *(Decoder *)decoder;
  if( d.lz_decoder ) delete d.lz_decoder;
  if( d.ibuf ) delete d.ibuf;
  delete (Decoder *)decoder;
  return 0;
  }


int LZ_decompress_finish( void * const decoder )
  {
  if( !verify_decoder( decoder ) ) return -1;
  ((Decoder *)decoder)->ibuf->finish();
  return 0;
  }


int LZ_decompress_read( void * const decoder, uint8_t * const buffer,
                        const int size )
  {
  if( !verify_decoder( decoder ) ) return -1;
  Decoder & d = *(Decoder *)decoder;
  if( d.lz_decoder && d.lz_decoder->member_finished() )
    {
    d.partial_in_size += d.lz_decoder->member_position();
    d.partial_out_size += d.lz_decoder->data_position();
    delete d.lz_decoder;
    d.lz_decoder = 0;
    }
  if( !d.lz_decoder )
    {
    if( d.ibuf->used_bytes() < 5 + (int)sizeof( File_header ) )
      {
      if( !d.ibuf->at_stream_end() || d.ibuf->finished() ) return 0;
      d.ibuf->purge();
      d.lz_errno = LZ_header_error;
      return -1;
      }
    File_header header;
    for( unsigned int i = 0; i < sizeof header; ++i )
      ((uint8_t *)&header)[i] = d.ibuf->get_byte();
    if( !header.verify_magic() || !header.verify_version() ||
        header.dictionary_size() < min_dictionary_size ||
        header.dictionary_size() > max_dictionary_size )
      {
      d.ibuf->purge();
      d.lz_errno = LZ_header_error;
      return -1;
      }
    try { d.lz_decoder = new LZ_decoder( header, *d.ibuf ); }
    catch( std::bad_alloc )
      {
      d.ibuf->purge();
      d.lz_decoder = 0;
      d.lz_errno = LZ_mem_error;
      return -1;
      }
    }
  const int result = d.lz_decoder->decode_member();
  if( result != 0 )
    {
    if( result == 2 ) d.lz_errno = LZ_unexpected_eof;
    else d.lz_errno = LZ_data_error;
    return -1;
    }
  return d.lz_decoder->read_data( buffer, size );
  }


int LZ_decompress_write( void * const decoder, uint8_t * const buffer,
                         const int size )
  {
  if( !verify_decoder( decoder ) ) return -1;
  return ((Decoder *)decoder)->ibuf->write_data( buffer, size );
  }


enum LZ_errno LZ_decompress_errno( void * const decoder )
  {
  if( !decoder ) return LZ_bad_argument;
  return ((Decoder *)decoder)->lz_errno;
  }


int LZ_decompress_finished( void * const decoder )
  {
  if( !verify_decoder( decoder ) ) return -1;
  Decoder & d = *(Decoder *)decoder;
  return ( d.ibuf->finished() &&
           ( !d.lz_decoder || d.lz_decoder->member_finished() ) );
  }


long long LZ_decompress_data_position( void * const decoder )
  {
  if( !verify_decoder( decoder ) ) return -1;
  if( ((Decoder *)decoder)->lz_decoder )
    return ((Decoder *)decoder)->lz_decoder->data_position();
  else return 0;
  }


long long LZ_decompress_member_position( void * const decoder )
  {
  if( !verify_decoder( decoder ) ) return -1;
  if( ((Decoder *)decoder)->lz_decoder )
    return ((Decoder *)decoder)->lz_decoder->member_position();
  else return 0;
  }


long long LZ_decompress_total_in_size( void * const decoder )
  {
  if( !verify_decoder( decoder ) ) return -1;
  Decoder & d = *(Decoder *)decoder;
  if( d.lz_decoder )
    return d.partial_in_size + d.lz_decoder->member_position();
  return d.partial_in_size;
  }


long long LZ_decompress_total_out_size( void * const decoder )
  {
  if( !verify_decoder( decoder ) ) return -1;
  Decoder & d = *(Decoder *)decoder;
  if( d.lz_decoder )
    return d.partial_out_size + d.lz_decoder->data_position();
  return d.partial_out_size;
  }