diff options
| author | Thomas Schmucker <ts@its1.de> | 2026-07-16 22:02:49 +0200 |
|---|---|---|
| committer | Thomas Schmucker <ts@its1.de> | 2026-07-16 22:02:49 +0200 |
| commit | 6eb05f5bd007561fac9dcd3328d888d06d2a3712 (patch) | |
| tree | 2399fc77777ce6b8692b05640e41462792b434bc | |
| parent | f010c2032f4054f2eae544166814d95c8c5ff3d9 (diff) | |
| download | use-zlib-6eb05f5bd007561fac9dcd3328d888d06d2a3712.tar.gz use-zlib-6eb05f5bd007561fac9dcd3328d888d06d2a3712.tar.bz2 use-zlib-6eb05f5bd007561fac9dcd3328d888d06d2a3712.zip | |
Fehlerbehandlung hinzugefügt
| -rw-r--r-- | src/simple.c | 75 |
1 files changed, 55 insertions, 20 deletions
diff --git a/src/simple.c b/src/simple.c index 36f7e48..da79562 100644 --- a/src/simple.c +++ b/src/simple.c | |||
| @@ -1,6 +1,7 @@ | |||
| 1 | // Standard C | 1 | // Standard C |
| 2 | #include <stdio.h> | 2 | #include <stdio.h> |
| 3 | #include <stdlib.h> | 3 | #include <stdlib.h> |
| 4 | #include <string.h> | ||
| 4 | 5 | ||
| 5 | // System | 6 | // System |
| 6 | #include <unistd.h> | 7 | #include <unistd.h> |
| @@ -12,38 +13,61 @@ static void | |||
| 12 | usage(void) | 13 | usage(void) |
| 13 | { | 14 | { |
| 14 | (void) fprintf(stderr, "compress [-d] infile outfile\n"); | 15 | (void) fprintf(stderr, "compress [-d] infile outfile\n"); |
| 16 | } | ||
| 17 | |||
| 18 | static void | ||
| 19 | error(const char *msg) | ||
| 20 | { | ||
| 21 | perror(msg); | ||
| 15 | exit(EXIT_FAILURE); | 22 | exit(EXIT_FAILURE); |
| 16 | } | 23 | } |
| 17 | 24 | ||
| 18 | unsigned long | 25 | size_t |
| 19 | get_filesize(FILE *file) | 26 | get_filesize(FILE *file) |
| 20 | { | 27 | { |
| 21 | (void) fseek(file, 0, SEEK_END); | 28 | long current_pos = ftell(file); |
| 22 | unsigned long size = ftell(file); | 29 | if ( current_pos == -1 ) { |
| 23 | (void) fseek(file, 0, SEEK_SET); | 30 | error("ftell() failed"); |
| 24 | return size; | 31 | } |
| 32 | |||
| 33 | if ( fseek(file, 0, SEEK_END) != 0 ) { | ||
| 34 | error("fseek() failed"); | ||
| 35 | } | ||
| 36 | |||
| 37 | long size = ftell(file); | ||
| 38 | if ( size == -1 ) { | ||
| 39 | error("ftell() failed"); | ||
| 40 | } | ||
| 41 | |||
| 42 | if ( fseek(file, current_pos, SEEK_SET) != 0 ) { | ||
| 43 | error("fseek() failed"); | ||
| 44 | } | ||
| 45 | |||
| 46 | return (size_t) size; | ||
| 25 | } | 47 | } |
| 26 | 48 | ||
| 27 | void | 49 | void |
| 28 | do_compress(FILE *in_file, FILE *out_file, int compress_level) | 50 | do_compress(FILE *in_file, FILE *out_file, int compress_level) |
| 29 | { | 51 | { |
| 30 | const unsigned long in_size = get_filesize(in_file); | 52 | size_t in_size = get_filesize(in_file); |
| 31 | 53 | ||
| 32 | (void) fwrite(&in_size, sizeof in_size, 1, out_file); | 54 | (void) fwrite(&in_size, sizeof in_size, 1, out_file); |
| 33 | 55 | ||
| 34 | unsigned char *in_data = malloc(in_size); | 56 | unsigned char *in_data = malloc(in_size); |
| 35 | (void) fread(in_data, 1, in_size, in_file); | 57 | (void) fread(in_data, 1, in_size, in_file); |
| 36 | 58 | ||
| 37 | unsigned long out_size = compressBound(in_size); | 59 | size_t out_size = compressBound(in_size); |
| 38 | unsigned char *out_data = malloc(out_size); | 60 | unsigned char *out_data = malloc(out_size); |
| 39 | 61 | ||
| 40 | const int error = compress2(out_data, &out_size, in_data, in_size, compress_level); | 62 | const int error = compress2(out_data, &out_size, |
| 63 | in_data, in_size, | ||
| 64 | compress_level); | ||
| 41 | 65 | ||
| 42 | if ( !error ) { | 66 | if ( !error ) { |
| 43 | (void) fwrite(out_data, 1, out_size, out_file); | 67 | (void) fwrite(out_data, 1, out_size, out_file); |
| 44 | } | 68 | } |
| 45 | else { | 69 | else { |
| 46 | printf("Error: %d\n", error); | 70 | (void) fprintf(stderr, "Error: %s\n", zError(error)); |
| 47 | } | 71 | } |
| 48 | 72 | ||
| 49 | free(out_data); | 73 | free(out_data); |
| @@ -53,26 +77,32 @@ do_compress(FILE *in_file, FILE *out_file, int compress_level) | |||
| 53 | void | 77 | void |
| 54 | do_uncompress(FILE *in_file, FILE *out_file) | 78 | do_uncompress(FILE *in_file, FILE *out_file) |
| 55 | { | 79 | { |
| 56 | const unsigned long in_size = get_filesize(in_file); | 80 | size_t in_size = get_filesize(in_file); |
| 57 | 81 | ||
| 58 | unsigned char *in_data = malloc(in_size); | 82 | unsigned char *in_data = malloc(in_size); |
| 59 | (void) fread(in_data, 1, in_size, in_file); | 83 | (void) fread(in_data, 1, in_size, in_file); |
| 60 | 84 | ||
| 61 | unsigned long out_size = *(unsigned long *) in_data; | 85 | size_t out_size = 0; |
| 86 | memcpy(&out_size, in_data, sizeof out_size); | ||
| 87 | |||
| 62 | unsigned char *out_data = malloc(out_size); | 88 | unsigned char *out_data = malloc(out_size); |
| 63 | 89 | ||
| 64 | unsigned long tmp = in_size - sizeof(unsigned long); | 90 | const size_t compressed_data_size = in_size - sizeof(size_t); |
| 91 | size_t consumed_size = compressed_data_size; | ||
| 65 | 92 | ||
| 66 | const int error = uncompress2(out_data, | 93 | const int error = uncompress2(out_data, &out_size, |
| 67 | &out_size, | 94 | in_data + sizeof(size_t), |
| 68 | in_data + sizeof(unsigned long), | 95 | &consumed_size); |
| 69 | &tmp); | ||
| 70 | 96 | ||
| 71 | if ( !error ) { | 97 | if ( !error ) { |
| 98 | if ( compressed_data_size != consumed_size ) { | ||
| 99 | (void) fputs("Warning: Trailing data detected.\n", stderr); | ||
| 100 | } | ||
| 101 | |||
| 72 | (void) fwrite(out_data, 1, out_size, out_file); | 102 | (void) fwrite(out_data, 1, out_size, out_file); |
| 73 | } | 103 | } |
| 74 | else { | 104 | else { |
| 75 | printf("Error: %d\n", error); | 105 | (void) fprintf(stderr, "Error: %s\n", zError(error)); |
| 76 | } | 106 | } |
| 77 | 107 | ||
| 78 | free(out_data); | 108 | free(out_data); |
| @@ -82,8 +112,13 @@ do_uncompress(FILE *in_file, FILE *out_file) | |||
| 82 | int | 112 | int |
| 83 | main(int argc, char *argv[]) | 113 | main(int argc, char *argv[]) |
| 84 | { | 114 | { |
| 115 | enum { | ||
| 116 | COMPRESSION_STD = 5, | ||
| 117 | COMPRESSION_BEST = 9 | ||
| 118 | }; | ||
| 119 | |||
| 85 | int decompress = 0; | 120 | int decompress = 0; |
| 86 | int compress_level = 5; | 121 | int compress_level = COMPRESSION_STD; |
| 87 | 122 | ||
| 88 | int opt = 0; | 123 | int opt = 0; |
| 89 | while ( (opt = getopt(argc, argv, "dl")) != -1 ) { | 124 | while ( (opt = getopt(argc, argv, "dl")) != -1 ) { |
| @@ -93,13 +128,13 @@ main(int argc, char *argv[]) | |||
| 93 | break; | 128 | break; |
| 94 | 129 | ||
| 95 | case 'l': | 130 | case 'l': |
| 96 | compress_level = 9; | 131 | compress_level = COMPRESSION_BEST; |
| 97 | break; | 132 | break; |
| 98 | 133 | ||
| 99 | case '?': | 134 | case '?': |
| 100 | default: | 135 | default: |
| 101 | usage(); | 136 | usage(); |
| 102 | break; | 137 | return EXIT_SUCCESS; |
| 103 | } | 138 | } |
| 104 | } | 139 | } |
| 105 | argc -= optind; | 140 | argc -= optind; |
