File: lzcheck.c

package info (click to toggle)
lzlib 1.11-4
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 580 kB
  • sloc: ansic: 4,707; sh: 518; makefile: 169
file content (254 lines) | stat: -rw-r--r-- 8,399 bytes parent folder | download | duplicates (2)
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
/*  Lzcheck - Test program for the lzlib library
    Copyright (C) 2009-2019 Antonio Diaz Diaz.

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

    Usage is:
      lzcheck filename.txt...

    This program reads each specified text file and then compresses it,
    line by line, to test the flushing mechanism and the member
    restart/reset/sync functions.
*/

#define _FILE_OFFSET_BITS 64

#include <ctype.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include "lzlib.h"

#ifndef min
  #define min(x,y) ((x) <= (y) ? (x) : (y))
#endif


enum { buffer_size = 32768 };
uint8_t in_buffer[buffer_size];
uint8_t mid_buffer[buffer_size];
uint8_t out_buffer[buffer_size];


void show_line( const uint8_t * const buffer, const int size )
  {
  int i;
  for( i = 0; i < size; ++i )
    fputc( isprint( buffer[i] ) ? buffer[i] : '.', stderr );
  fputc( '\n', stderr );
  }


int lzcheck( FILE * const file, const int dictionary_size )
  {
  const int match_len_limit = 16;
  const unsigned long long member_size = 0x7FFFFFFFFFFFFFFFULL;	/* INT64_MAX */
  struct LZ_Encoder * encoder;
  struct LZ_Decoder * decoder;
  int retval = 0;

  encoder = LZ_compress_open( dictionary_size, match_len_limit, member_size );
  if( !encoder || LZ_compress_errno( encoder ) != LZ_ok )
    {
    const bool mem_error = ( LZ_compress_errno( encoder ) == LZ_mem_error );
    LZ_compress_close( encoder );
    if( mem_error )
      {
      fputs( "lzcheck: Not enough memory.\n", stderr );
      return 1;
      }
    fputs( "lzcheck: internal error: Invalid argument to encoder.\n", stderr );
    return 3;
    }

  decoder = LZ_decompress_open();
  if( !decoder || LZ_decompress_errno( decoder ) != LZ_ok )
    {
    LZ_decompress_close( decoder );
    fputs( "lzcheck: Not enough memory.\n", stderr );
    return 1;
    }

  while( retval <= 1 )
    {
    int l, r;
    const int read_size = fread( in_buffer, 1, buffer_size, file );
    if( read_size <= 0 ) break;			/* end of file */

    for( l = 0, r = 1; r <= read_size; l = r, ++r )
      {
      int in_size, mid_size, out_size;
      while( r < read_size && in_buffer[r-1] != '\n' ) ++r;
      in_size = LZ_compress_write( encoder, in_buffer + l, r - l );
      if( in_size < r - l ) r = l + in_size;
      LZ_compress_sync_flush( encoder );
      mid_size = LZ_compress_read( encoder, mid_buffer, buffer_size );
      if( mid_size < 0 )
        {
        fprintf( stderr, "lzcheck: LZ_compress_read error: %s\n",
                 LZ_strerror( LZ_compress_errno( encoder ) ) );
        retval = 3; break;
        }
      LZ_decompress_write( decoder, mid_buffer, mid_size );
      out_size = LZ_decompress_read( decoder, out_buffer, buffer_size );
      if( out_size < 0 )
        {
        fprintf( stderr, "lzcheck: LZ_decompress_read error: %s\n",
                 LZ_strerror( LZ_decompress_errno( decoder ) ) );
        retval = 3; break;
        }

      if( out_size != in_size || memcmp( in_buffer + l, out_buffer, out_size ) )
        {
        fprintf( stderr, "lzcheck: Sync error at pos %d  in_size = %d, "
                         "out_size = %d\n",
                 l, in_size, out_size );
        show_line( in_buffer + l, in_size );
        show_line( out_buffer, out_size );
        retval = 1;
        }
      }
    }

  if( retval <= 1 )
    {
    rewind( file );
    if( LZ_compress_finish( encoder ) < 0 ||
        LZ_compress_finish( encoder ) < 0 ||
        LZ_decompress_write( decoder, mid_buffer,
          LZ_compress_read( encoder, mid_buffer, buffer_size ) ) < 0 ||
        LZ_decompress_read( decoder, out_buffer, buffer_size ) != 0 ||
        LZ_compress_finish( encoder ) < 0 ||
        LZ_compress_restart_member( encoder, member_size ) < 0 )
      {
      fprintf( stderr, "lzcheck: Can't finish member: %s\n",
               LZ_strerror( LZ_decompress_errno( decoder ) ) );
      retval = 3;
      }
    }

  while( retval <= 1 )
    {
    int l, r, size;
    const int read_size = fread( in_buffer, 1, buffer_size / 2, file );
    if( read_size <= 0 ) break;			/* end of file */

    for( l = 0, r = 1; r <= read_size; l = r, ++r )
      {
      int leading_garbage, in_size, mid_size, out_size;
      while( r < read_size && in_buffer[r-1] != '\n' ) ++r;
      leading_garbage = (l == 0) ? min( r, read_size ) / 2 : 0;
      in_size = LZ_compress_write( encoder, in_buffer + l, r - l );
      if( in_size < r - l ) r = l + in_size;
      LZ_compress_sync_flush( encoder );
      if( leading_garbage )
        memset( mid_buffer, in_buffer[0], leading_garbage );
      mid_size = LZ_compress_read( encoder, mid_buffer + leading_garbage,
                                   buffer_size - leading_garbage );
      if( mid_size < 0 )
        {
        fprintf( stderr, "lzcheck: LZ_compress_read error: %s\n",
                 LZ_strerror( LZ_compress_errno( encoder ) ) );
        retval = 3; break;
        }
      LZ_decompress_write( decoder, mid_buffer, mid_size + leading_garbage );
      out_size = LZ_decompress_read( decoder, out_buffer, buffer_size );
      if( out_size < 0 )
        {
        if( LZ_decompress_errno( decoder ) == LZ_header_error ||
            LZ_decompress_errno( decoder ) == LZ_data_error )
          {
          LZ_decompress_sync_to_member( decoder );  /* remove leading garbage */
          out_size = LZ_decompress_read( decoder, out_buffer, buffer_size );
          }
        if( out_size < 0 )
          {
          fprintf( stderr, "lzcheck: LZ_decompress_read error: %s\n",
                   LZ_strerror( LZ_decompress_errno( decoder ) ) );
          retval = 3; break;
          }
        }

      if( out_size != in_size || memcmp( in_buffer + l, out_buffer, out_size ) )
        {
        fprintf( stderr, "lzcheck: Sync error at pos %d  in_size = %d, "
                         "out_size = %d, leading garbage = %d\n",
                 l, in_size, out_size, leading_garbage );
        show_line( in_buffer + l, in_size );
        show_line( out_buffer, out_size );
        retval = 1;
        }
      }
    if( retval >= 3 ) break;

    if( LZ_compress_finish( encoder ) < 0 ||
        LZ_decompress_write( decoder, mid_buffer,
          LZ_compress_read( encoder, mid_buffer, buffer_size ) ) < 0 ||
        LZ_decompress_read( decoder, out_buffer, buffer_size ) != 0 ||
        LZ_decompress_reset( decoder ) < 0 ||
        LZ_compress_restart_member( encoder, member_size ) < 0 )
      {
      fprintf( stderr, "lzcheck: Can't restart member: %s\n",
               LZ_strerror( LZ_decompress_errno( decoder ) ) );
      retval = 3; break;
      }

    size = min( 100, read_size );
    if( LZ_compress_write( encoder, in_buffer, size ) != size ||
        LZ_compress_finish( encoder ) < 0 ||
        LZ_decompress_write( decoder, mid_buffer,
          LZ_compress_read( encoder, mid_buffer, buffer_size ) ) < 0 ||
        LZ_decompress_read( decoder, out_buffer, 0 ) != 0 ||
        LZ_decompress_sync_to_member( decoder ) < 0 ||
        LZ_compress_restart_member( encoder, member_size ) < 0 )
      {
      fprintf( stderr, "lzcheck: Can't seek to next member: %s\n",
               LZ_strerror( LZ_decompress_errno( decoder ) ) );
      retval = 3; break;
      }
    }

  LZ_decompress_close( decoder );
  LZ_compress_close( encoder );
  return retval;
  }


int main( const int argc, const char * const argv[] )
  {
  int retval = 0, i;
  int open_failures = 0;
  const bool verbose = ( argc > 2 );

  if( argc < 2 )
    {
    fputs( "Usage: lzcheck filename.txt...\n", stderr );
    return 1;
    }

  for( i = 1; i < argc && retval == 0; ++ i )
    {
    FILE * file = fopen( argv[i], "rb" );
    if( !file )
      {
      fprintf( stderr, "lzcheck: Can't open file '%s' for reading.\n", argv[i] );
      ++open_failures; continue;
      }
    if( verbose ) fprintf( stderr, "  Testing file '%s'\n", argv[i] );

    retval = lzcheck( file, 65535 );	/* 65535,16 chooses fast encoder */
    if( retval == 0 )
      { rewind( file ); retval = lzcheck( file, 1 << 20 ); }
    fclose( file );
    }
  if( open_failures > 0 && verbose )
    fprintf( stderr, "lzcheck: warning: %d %s failed to open.\n",
             open_failures, ( open_failures == 1 ) ? "file" : "files" );
  if( retval == 0 && open_failures ) retval = 1;
  return retval;
  }