ffexample.c (lzlib-1.12.tar.lz) | : | ffexample.c (lzlib-1.13.tar.lz) | ||
---|---|---|---|---|
/* File to file example - Test program for the library lzlib | /* File to file example - Test program for the library lzlib | |||
Copyright (C) 2010-2021 Antonio Diaz Diaz. | Copyright (C) 2010-2022 Antonio Diaz Diaz. | |||
This program is free software: you have unlimited permission | This program is free software: you have unlimited permission | |||
to copy, distribute, and modify it. | to copy, distribute, and modify it. | |||
Try 'ffexample -h' for usage information. | Try 'ffexample -h' for usage information. | |||
This program is an example of how file-to-file | This program is an example of how file-to-file | |||
compression/decompression can be implemented using lzlib. | compression/decompression can be implemented using lzlib. | |||
*/ | */ | |||
#define _FILE_OFFSET_BITS 64 | #define _FILE_OFFSET_BITS 64 | |||
#include <errno.h> | #include <errno.h> | |||
#include <limits.h> | #include <limits.h> | |||
#include <stdbool.h> | #include <stdbool.h> | |||
#include <stdint.h> | #include <stdint.h> | |||
#include <stdio.h> | #include <stdio.h> | |||
#include <stdlib.h> | #include <stdlib.h> | |||
#include <string.h> | #include <string.h> | |||
#include <unistd.h> | #include <unistd.h> | |||
#if defined(__MSVCRT__) || defined(__OS2__) || defined(__DJGPP__) | #if defined __MSVCRT__ || defined __OS2__ || defined __DJGPP__ | |||
#include <fcntl.h> | #include <fcntl.h> | |||
#include <io.h> | #include <io.h> | |||
#endif | #endif | |||
#include "lzlib.h" | #include "lzlib.h" | |||
#ifndef min | #ifndef min | |||
#define min(x,y) ((x) <= (y) ? (x) : (y)) | #define min(x,y) ((x) <= (y) ? (x) : (y)) | |||
#endif | #endif | |||
skipping to change at line 175 | skipping to change at line 175 | |||
if( LZ_compress_member_finished( encoder ) == 1 ) | if( LZ_compress_member_finished( encoder ) == 1 ) | |||
{ | { | |||
if( LZ_compress_finished( encoder ) == 1 ) { done = true; break; } | if( LZ_compress_finished( encoder ) == 1 ) { done = true; break; } | |||
if( LZ_compress_restart_member( encoder, member_size ) < 0 ) break; | if( LZ_compress_restart_member( encoder, member_size ) < 0 ) break; | |||
} | } | |||
} | } | |||
if( LZ_compress_close( encoder ) < 0 ) done = false; | if( LZ_compress_close( encoder ) < 0 ) done = false; | |||
return done; | return done; | |||
} | } | |||
/* Compresses 'infile' to 'outfile' as a multimember stream with one member | /* Compress 'infile' to 'outfile' as a multimember stream with one member | |||
for each line of text terminated by a newline character or by EOF. | for each line of text terminated by a newline character or by EOF. | |||
Returns 0 if success, 1 if error. | Return 0 if success, 1 if error. | |||
*/ | */ | |||
int fflfcompress( struct LZ_Encoder * const encoder, | int fflfcompress( struct LZ_Encoder * const encoder, | |||
FILE * const infile, FILE * const outfile ) | FILE * const infile, FILE * const outfile ) | |||
{ | { | |||
enum { buffer_size = 16384 }; | enum { buffer_size = 16384 }; | |||
uint8_t buffer[buffer_size]; | uint8_t buffer[buffer_size]; | |||
while( true ) | while( true ) | |||
{ | { | |||
int len, ret; | int len, ret; | |||
int size = min( buffer_size, LZ_compress_write_size( encoder ) ); | int size = min( buffer_size, LZ_compress_write_size( encoder ) ); | |||
skipping to change at line 215 | skipping to change at line 215 | |||
if( len < ret ) break; | if( len < ret ) break; | |||
if( LZ_compress_member_finished( encoder ) == 1 ) | if( LZ_compress_member_finished( encoder ) == 1 ) | |||
{ | { | |||
if( feof( infile ) && LZ_compress_finished( encoder ) == 1 ) return 0; | if( feof( infile ) && LZ_compress_finished( encoder ) == 1 ) return 0; | |||
if( LZ_compress_restart_member( encoder, INT64_MAX ) < 0 ) break; | if( LZ_compress_restart_member( encoder, INT64_MAX ) < 0 ) break; | |||
} | } | |||
} | } | |||
return 1; | return 1; | |||
} | } | |||
/* Decompresses 'infile' to 'outfile' with automatic resynchronization to | /* Decompress 'infile' to 'outfile' with automatic resynchronization to | |||
next member in case of data error, including the automatic removal of | next member in case of data error, including the automatic removal of | |||
leading garbage. | leading garbage. | |||
*/ | */ | |||
int ffrsdecompress( struct LZ_Decoder * const decoder, | int ffrsdecompress( struct LZ_Decoder * const decoder, | |||
FILE * const infile, FILE * const outfile ) | FILE * const infile, FILE * const outfile ) | |||
{ | { | |||
enum { buffer_size = 16384 }; | enum { buffer_size = 16384 }; | |||
uint8_t buffer[buffer_size]; | uint8_t buffer[buffer_size]; | |||
while( true ) | while( true ) | |||
{ | { | |||
skipping to change at line 252 | skipping to change at line 252 | |||
} | } | |||
len = fwrite( buffer, 1, ret, outfile ); | len = fwrite( buffer, 1, ret, outfile ); | |||
if( len < ret ) break; | if( len < ret ) break; | |||
if( LZ_decompress_finished( decoder ) == 1 ) return 0; | if( LZ_decompress_finished( decoder ) == 1 ) return 0; | |||
} | } | |||
return 1; | return 1; | |||
} | } | |||
int main( const int argc, const char * const argv[] ) | int main( const int argc, const char * const argv[] ) | |||
{ | { | |||
#if defined(__MSVCRT__) || defined(__OS2__) || defined(__DJGPP__) | #if defined __MSVCRT__ || defined __OS2__ || defined __DJGPP__ | |||
setmode( STDIN_FILENO, O_BINARY ); | setmode( STDIN_FILENO, O_BINARY ); | |||
setmode( STDOUT_FILENO, O_BINARY ); | setmode( STDOUT_FILENO, O_BINARY ); | |||
#endif | #endif | |||
struct LZ_Encoder * const encoder = LZ_compress_open( 65535, 16, INT64_MAX ); | struct LZ_Encoder * const encoder = LZ_compress_open( 65535, 16, INT64_MAX ); | |||
struct LZ_Decoder * const decoder = LZ_decompress_open(); | struct LZ_Decoder * const decoder = LZ_decompress_open(); | |||
FILE * const infile = ( argc >= 3 && strcmp( argv[2], "-" ) != 0 ) ? | FILE * const infile = ( argc >= 3 && strcmp( argv[2], "-" ) != 0 ) ? | |||
fopen( argv[2], "rb" ) : stdin; | fopen( argv[2], "rb" ) : stdin; | |||
FILE * const outfile = ( argc >= 4 && strcmp( argv[3], "-" ) != 0 ) ? | FILE * const outfile = ( argc >= 4 && strcmp( argv[3], "-" ) != 0 ) ? | |||
fopen( argv[3], "wb" ) : stdout; | fopen( argv[3], "wb" ) : stdout; | |||
End of changes. 6 change blocks. | ||||
6 lines changed or deleted | 6 lines changed or added |