diff options
| author | Dirk Engling <erdgeist@erdgeist.org> | 2024-08-18 12:52:07 +0200 |
|---|---|---|
| committer | Dirk Engling <erdgeist@erdgeist.org> | 2024-08-18 12:52:07 +0200 |
| commit | a31fae7960ed0ada95813cd5e0160c194cb81ebb (patch) | |
| tree | d6b1d7b0210eeb8f1bb3e99ad9dd0d335608e53f | |
| parent | f6fc0fc424a35c67d257012c9574bc60537f63b4 (diff) | |
Make zlib wrapper in v4 exporter more robust and easier to read
| -rw-r--r-- | src/export/extract_version_4.c | 40 |
1 files changed, 26 insertions, 14 deletions
diff --git a/src/export/extract_version_4.c b/src/export/extract_version_4.c index 1dbaf40..e014e23 100644 --- a/src/export/extract_version_4.c +++ b/src/export/extract_version_4.c | |||
| @@ -7,44 +7,56 @@ | |||
| 7 | #include "mystdlib.h" | 7 | #include "mystdlib.h" |
| 8 | 8 | ||
| 9 | #define XORLEN (29) | 9 | #define XORLEN (29) |
| 10 | #define HUGEINBLOCK (128*1024) | ||
| 10 | #define HUGEBLOCK (4*1024*1024) | 11 | #define HUGEBLOCK (4*1024*1024) |
| 11 | 12 | ||
| 12 | int main(int argc, char **argv) { | 13 | int main(int argc, char **argv) { |
| 13 | unsigned const char xorkey [XORLEN] = "Just for Fun. Linus Torvalds."; | 14 | unsigned const char xorkey [XORLEN] = "Just for Fun. Linus Torvalds."; |
| 14 | unsigned char input [XORLEN]; | 15 | unsigned char input [HUGEINBLOCK]; |
| 15 | unsigned char output [HUGEBLOCK]; | 16 | unsigned char output [HUGEBLOCK]; |
| 16 | char respath[32]; /* file_XXXXX\0 */ | 17 | char respath[32]; /* file_XXXXX\0 */ |
| 17 | int zres = 0, filenum = 0, resfile; | 18 | int zres = 0, filenum = 0, resfile; |
| 18 | size_t i, offs = 0, reported = 0; | 19 | size_t offs = 0, reported = 0; |
| 19 | ssize_t temp = 0; | 20 | ssize_t temp = 0; |
| 20 | MAP in; | 21 | MAP in; |
| 21 | 22 | ||
| 22 | if( argc != 2 ) exit(111); | 23 | if( argc != 2 ) exit(111); |
| 23 | in = map_file( argv[1], 1 ); | 24 | in = map_file( argv[1], 1 ); |
| 24 | 25 | ||
| 25 | z_stream z; memset( &z, 0, sizeof(z)); | 26 | z_stream z; |
| 26 | 27 | ||
| 27 | while( offs < in->size ) { | 28 | while( offs < in->size ) { |
| 28 | size_t inlen = offs + XORLEN < in->size ? XORLEN : in->size - offs; | 29 | /* std::min(remain, HUGEINBLOCK) */ |
| 29 | for( i=0; i<inlen; ++i ) input[i] = in->addr[offs+i] ^ xorkey[i]; | 30 | size_t inlen = in->size - offs; |
| 31 | if (inlen > sizeof(input)) | ||
| 32 | inlen = sizeof(input); | ||
| 33 | |||
| 34 | /* Copy in block large enough */ | ||
| 35 | memcpy(input, in->addr + offs, inlen); | ||
| 36 | |||
| 37 | /* De-"crypt" */ | ||
| 38 | for (size_t i = 0; i < sizeof(xorkey); ++i ) | ||
| 39 | input[i] ^= xorkey[i]; | ||
| 40 | |||
| 41 | /* Prepare decompression struct */ | ||
| 42 | memset( &z, 0, sizeof(z)); | ||
| 30 | z.next_in = input; z.avail_in = inlen; | 43 | z.next_in = input; z.avail_in = inlen; |
| 31 | z.next_out = output; z.avail_out = HUGEBLOCK; | 44 | z.next_out = output; z.avail_out = HUGEBLOCK; |
| 32 | inflateInit( &z ); zres = inflate( &z, Z_NO_FLUSH ); | 45 | inflateInit( &z ); |
| 33 | if( (zres != Z_OK) && (zres != Z_STREAM_END) ) | 46 | zres = Z_OK; |
| 34 | goto error_continue; | ||
| 35 | 47 | ||
| 36 | z.next_in = in->addr + offs + inlen; | 48 | while( zres == Z_OK ) |
| 37 | z.avail_in = (unsigned int)(in->size - offs - inlen); | 49 | zres = inflate( &z, Z_NO_FLUSH ); |
| 38 | while( zres == Z_OK ) zres = inflate( &z, Z_NO_FLUSH ); | ||
| 39 | 50 | ||
| 40 | if( zres != Z_STREAM_END ) { | 51 | if( zres != Z_STREAM_END ) { |
| 41 | error_continue: | 52 | inflateEnd(&z); |
| 42 | inflateEnd(&z); memset( &z, 0, sizeof(z)); | ||
| 43 | offs++; | 53 | offs++; |
| 44 | continue; | 54 | continue; |
| 45 | } | 55 | } |
| 46 | 56 | ||
| 47 | sprintf( respath, "file_%05X", filenum++ ); | 57 | // fprintf( stderr, "%08X\n", (unsigned int)(offs)); |
| 58 | //old_offs = offs; | ||
| 59 | snprintf( respath, sizeof(respath), "file_%05X", filenum++ ); | ||
| 48 | 60 | ||
| 49 | resfile = open( respath, O_RDWR | O_CREAT, 0644 ); | 61 | resfile = open( respath, O_RDWR | O_CREAT, 0644 ); |
| 50 | if( resfile < 0 ) { | 62 | if( resfile < 0 ) { |
