summaryrefslogtreecommitdiffstats
path: root/bbexample.cc
blob: df9130095358af705f85436438e88b8a8c7d904c (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
/*  Buff to buff example - A test program for the lzlib library
    Copyright (C) 2010, 2011 Antonio Diaz Diaz.

    This program is free software: you have unlimited permission
    to copy, distribute and modify it.

    Usage is:
      bbexample filename

    This program is an example of how buffer-to-buffer
    compression/decompression can be implemented using lzlib.
*/

#ifndef __cplusplus
#include <stdbool.h>
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <unistd.h>

#include "lzlib.h"

#ifndef LLONG_MAX
#define LLONG_MAX  0x7FFFFFFFFFFFFFFFLL
#endif
#ifndef LLONG_MIN
#define LLONG_MIN  (-LLONG_MAX - 1LL)
#endif
#ifndef ULLONG_MAX
#define ULLONG_MAX 0xFFFFFFFFFFFFFFFFULL
#endif


// Compresses `size' bytes from `data'. Returns the address of a
// malloc'd buffer containing the compressed data and its size in
// `*out_sizep'.
// In case of error, returns 0 and does not modify `*out_sizep'.

uint8_t * bbcompress( const uint8_t * const data, const int size,
                      int * const out_sizep )
  {
  int dict_size = 8 << 20;				// 8 MiB
  const int match_len_limit = 36;
  const long long member_size = LLONG_MAX;
  if( dict_size > size ) dict_size = size;
  if( dict_size < LZ_min_dictionary_size() )
    dict_size = LZ_min_dictionary_size();
  struct LZ_Encoder * encoder =
    LZ_compress_open( dict_size, match_len_limit, member_size );
  if( !encoder || LZ_compress_errno( encoder ) != LZ_ok )
    { LZ_compress_close( encoder ); return 0; }

  const int delta_size = (size < 256) ? 64 : size / 4;	// size may be zero
  int new_data_size = delta_size;			// initial size
  uint8_t * new_data = (uint8_t *)malloc( new_data_size );
  if( !new_data )
    { LZ_compress_close( encoder ); return 0; }

  int new_pos = 0;
  int written = 0;
  bool error = false;
  while( true )
    {
    if( LZ_compress_write_size( encoder ) > 0 )
      {
      if( written < size )
        {
        const int wr = LZ_compress_write( encoder, data + written,
                                          size - written );
        if( wr < 0 ) { error = true; break; }
        written += wr;
        }
      if( written >= size ) LZ_compress_finish( encoder );
      }
    const int rd = LZ_compress_read( encoder, new_data + new_pos,
                                     new_data_size - new_pos );
    if( rd < 0 ) { error = true; break; }
    new_pos += rd;
    if( LZ_compress_finished( encoder ) == 1 ) break;
    if( new_pos >= new_data_size )
      {
      void * const tmp = realloc( new_data, new_data_size + delta_size );
      if( !tmp ) { error = true; break; }
      new_data = (uint8_t *)tmp;
      new_data_size += delta_size;
      }
    }

  if( LZ_compress_close( encoder ) < 0 ) error = true;
  if( error ) { free( new_data ); return 0; }
  *out_sizep = new_pos;
  return new_data;
  }


// Decompresses `size' bytes from `data'. Returns the address of a
// malloc'd buffer containing the decompressed data and its size in
// `*out_sizep'.
// In case of error, returns 0 and does not modify `*out_sizep'.

uint8_t * bbdecompress( const uint8_t * const data, const int size,
                        int * const out_sizep )
  {
  struct LZ_Decoder * decoder = LZ_decompress_open();
  if( !decoder || LZ_decompress_errno( decoder ) != LZ_ok )
    { LZ_decompress_close( decoder ); return 0; }

  const int delta_size = size;
  int new_data_size = delta_size;			// initial size
  uint8_t * new_data = (uint8_t *)malloc( new_data_size );
  if( !new_data )
    { LZ_decompress_close( decoder ); return 0; }

  int new_pos = 0;
  int written = 0;
  bool error = false;
  while( true )
    {
    if( LZ_decompress_write_size( decoder ) > 0 )
      {
      if( written < size )
        {
        const int wr = LZ_decompress_write( decoder, data + written,
                                            size - written );
        if( wr < 0 ) { error = true; break; }
        written += wr;
        }
      if( written >= size ) LZ_decompress_finish( decoder );
      }
    const int rd = LZ_decompress_read( decoder, new_data + new_pos,
                                       new_data_size - new_pos );
    if( rd < 0 ) { error = true; break; }
    new_pos += rd;
    if( LZ_decompress_finished( decoder ) == 1 ) break;
    if( new_pos >= new_data_size )
      {
      void * const tmp = realloc( new_data, new_data_size + delta_size );
      if( !tmp ) { error = true; break; }
      new_data = (uint8_t *)tmp;
      new_data_size += delta_size;
      }
    }

  if( LZ_decompress_close( decoder ) < 0 ) error = true;
  if( error ) { free( new_data ); return 0; }
  *out_sizep = new_pos;
  return new_data;
  }


int main( const int argc, const char * const argv[] )
  {
  if( argc < 2 )
    {
    fprintf( stderr, "Usage: bbexample filename\n" );
    return 1;
    }

  FILE *file = fopen( argv[1], "rb" );
  if( !file )
    {
    fprintf( stderr, "bbexample: Can't open file `%s' for reading\n", argv[1] );
    return 1;
    }

  const int in_buffer_size = 1 << 20;
  uint8_t * const in_buffer = (uint8_t *)malloc( in_buffer_size );
  if( !in_buffer )
    {
    fprintf( stderr, "bbexample: Not enough memory.\n" );
    return 1;
    }
  const int in_size = fread( in_buffer, 1, in_buffer_size, file );
  if( in_size >= in_buffer_size )
    {
    fprintf( stderr, "bbexample: Input file `%s' is too big.\n", argv[1] );
    return 1;
    }
  fclose( file );

  int mid_size = 0;
  uint8_t * const mid_buffer = bbcompress( in_buffer, in_size, &mid_size );
  if( !mid_buffer )
    {
    fprintf( stderr, "bbexample: Not enough memory or compress error.\n" );
    return 1;
    }

  int out_size = 0;
  uint8_t * const out_buffer = bbdecompress( mid_buffer, mid_size, &out_size );
  if( !out_buffer )
    {
    fprintf( stderr, "bbexample: Not enough memory or decompress error.\n" );
    return 1;
    }

  if( in_size != out_size ||
      ( out_size > 0 && memcmp( in_buffer, out_buffer, out_size ) ) )
    {
    fprintf( stderr, "bbexample: Decompressed data differs from original.\n" );
    return 1;
    }

  free( out_buffer );
  free( mid_buffer );
  free( in_buffer );
  return 0;
  }