diff options
| author | Thomas Schmucker <ts@its1.de> | 2026-07-17 20:32:44 +0200 |
|---|---|---|
| committer | Thomas Schmucker <ts@its1.de> | 2026-07-17 20:32:44 +0200 |
| commit | e378fd8cbb69892d0af4110a7e3968d074700769 (patch) | |
| tree | 93eb9a22bf9dedc87c0645af2edeb46e07b9871c | |
| parent | c73b06b03a3f43256fb1e071553af91fa6d6a825 (diff) | |
| download | use-zlib-master.tar.gz use-zlib-master.tar.bz2 use-zlib-master.zip | |
| -rw-r--r-- | makefile | 10 | ||||
| -rw-r--r-- | src/gzip.c | 125 |
2 files changed, 133 insertions, 2 deletions
| @@ -6,10 +6,13 @@ STREAM_OBJ_FILES=obj/stream.o | |||
| 6 | SIMPLE=bin/simple | 6 | SIMPLE=bin/simple |
| 7 | SIMPLE_OBJ_FILES=obj/simple.o | 7 | SIMPLE_OBJ_FILES=obj/simple.o |
| 8 | 8 | ||
| 9 | release: $(STREAM) $(SIMPLE) | 9 | GZIP=bin/gzip |
| 10 | GZIP_OBJ_FILES=obj/gzip.o | ||
| 11 | |||
| 12 | release: $(STREAM) $(SIMPLE) $(GZIP) | ||
| 10 | release: CFLAGS+=-DNDEBUG -O2 | 13 | release: CFLAGS+=-DNDEBUG -O2 |
| 11 | 14 | ||
| 12 | debug: $(STREAM) $(SIMPLE) | 15 | debug: $(STREAM) $(SIMPLE) $(GZIP) |
| 13 | debug: CFLAGS+=-g | 16 | debug: CFLAGS+=-g |
| 14 | 17 | ||
| 15 | $(STREAM): $(STREAM_OBJ_FILES) | bin | 18 | $(STREAM): $(STREAM_OBJ_FILES) | bin |
| @@ -18,6 +21,9 @@ $(STREAM): $(STREAM_OBJ_FILES) | bin | |||
| 18 | $(SIMPLE): $(SIMPLE_OBJ_FILES) | bin | 21 | $(SIMPLE): $(SIMPLE_OBJ_FILES) | bin |
| 19 | cc $(LDFLAGS) $^ -o $@ | 22 | cc $(LDFLAGS) $^ -o $@ |
| 20 | 23 | ||
| 24 | $(GZIP): $(GZIP_OBJ_FILES) | bin | ||
| 25 | cc $(LDFLAGS) $^ -o $@ | ||
| 26 | |||
| 21 | obj/%.o: src/%.c | obj | 27 | obj/%.o: src/%.c | obj |
| 22 | cc $(CFLAGS) -c $< -o $@ | 28 | cc $(CFLAGS) -c $< -o $@ |
| 23 | 29 | ||
diff --git a/src/gzip.c b/src/gzip.c new file mode 100644 index 0000000..11828db --- /dev/null +++ b/src/gzip.c | |||
| @@ -0,0 +1,125 @@ | |||
| 1 | // Standard C | ||
| 2 | #include <errno.h> | ||
| 3 | #include <stdio.h> | ||
| 4 | #include <stdlib.h> | ||
| 5 | #include <string.h> | ||
| 6 | |||
| 7 | // System | ||
| 8 | #include <unistd.h> | ||
| 9 | |||
| 10 | // zlib | ||
| 11 | #include <zlib.h> | ||
| 12 | |||
| 13 | enum { BUFFER_SIZE = 8192 }; | ||
| 14 | |||
| 15 | static void | ||
| 16 | usage(void) | ||
| 17 | { | ||
| 18 | (void) fprintf(stderr, "stream [-d] infile outfile\n"); | ||
| 19 | } | ||
| 20 | |||
| 21 | static void | ||
| 22 | error(const char *msg) | ||
| 23 | { | ||
| 24 | perror(msg); | ||
| 25 | exit(EXIT_FAILURE); | ||
| 26 | } | ||
| 27 | |||
| 28 | void | ||
| 29 | do_compress(const char *in_name, const char *out_name, int compress_level) | ||
| 30 | { | ||
| 31 | FILE *in_file = fopen(in_name, "rb"); | ||
| 32 | if ( in_file == NULL ) { | ||
| 33 | error("fopen() on in_file failed"); | ||
| 34 | } | ||
| 35 | |||
| 36 | char mode[10]; | ||
| 37 | (void) snprintf(mode, sizeof mode, "wb%d", compress_level); | ||
| 38 | |||
| 39 | gzFile out_file = gzopen(out_name, mode); | ||
| 40 | if ( out_file == NULL ) { | ||
| 41 | (void) fclose(in_file); | ||
| 42 | error("gzopen() on out_file failed"); | ||
| 43 | } | ||
| 44 | |||
| 45 | unsigned char buffer[BUFFER_SIZE]; | ||
| 46 | |||
| 47 | size_t bytes_read = 0; | ||
| 48 | while ( (bytes_read = fread(buffer, 1, BUFFER_SIZE, in_file)) != 0 ) { | ||
| 49 | gzfwrite(buffer, 1, bytes_read, out_file); | ||
| 50 | } | ||
| 51 | |||
| 52 | (void) fclose(in_file); | ||
| 53 | gzclose(out_file); | ||
| 54 | } | ||
| 55 | |||
| 56 | void | ||
| 57 | do_uncompress(const char *in_name, const char *out_name) | ||
| 58 | { | ||
| 59 | gzFile in_file = gzopen(in_name, "rb"); | ||
| 60 | if ( in_file == NULL ) { | ||
| 61 | error("gzopen() on in_file failed"); | ||
| 62 | } | ||
| 63 | |||
| 64 | FILE *out_file = fopen(out_name, "wb"); | ||
| 65 | if ( out_file == NULL ) { | ||
| 66 | (void) gzclose(in_file); | ||
| 67 | error("fopen() on out_file failed"); | ||
| 68 | } | ||
| 69 | |||
| 70 | unsigned char buffer[BUFFER_SIZE]; | ||
| 71 | |||
| 72 | size_t bytes_read = 0; | ||
| 73 | while ( (bytes_read = gzfread(buffer, 1, BUFFER_SIZE, in_file)) != 0 ) { | ||
| 74 | (void) fwrite(buffer, 1, bytes_read, out_file); | ||
| 75 | } | ||
| 76 | |||
| 77 | gzclose(in_file); | ||
| 78 | (void) fclose(out_file); | ||
| 79 | } | ||
| 80 | |||
| 81 | int | ||
| 82 | main(int argc, char *argv[]) | ||
| 83 | { | ||
| 84 | enum { | ||
| 85 | COMPRESSION_STD = 5, | ||
| 86 | COMPRESSION_BEST = 9 | ||
| 87 | }; | ||
| 88 | |||
| 89 | int decompress = 0; | ||
| 90 | int compress_level = COMPRESSION_STD; | ||
| 91 | |||
| 92 | int opt = 0; | ||
| 93 | while ( (opt = getopt(argc, argv, "dl")) != -1 ) { | ||
| 94 | switch ( opt ) { | ||
| 95 | case 'd': | ||
| 96 | decompress = 1; | ||
| 97 | break; | ||
| 98 | |||
| 99 | case 'l': | ||
| 100 | compress_level = COMPRESSION_BEST; | ||
| 101 | break; | ||
| 102 | |||
| 103 | case '?': | ||
| 104 | default: | ||
| 105 | usage(); | ||
| 106 | return EXIT_SUCCESS; | ||
| 107 | } | ||
| 108 | } | ||
| 109 | argc -= optind; | ||
| 110 | argv += optind; | ||
| 111 | |||
| 112 | if ( argc != 2 ) { | ||
| 113 | usage(); | ||
| 114 | return EXIT_FAILURE; | ||
| 115 | } | ||
| 116 | |||
| 117 | if ( !decompress ) { | ||
| 118 | do_compress(argv[0], argv[1], compress_level); | ||
| 119 | } | ||
| 120 | else { | ||
| 121 | do_uncompress(argv[0], argv[1]); | ||
| 122 | } | ||
| 123 | |||
| 124 | return EXIT_SUCCESS; | ||
| 125 | } | ||
