summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorThomas Schmucker <ts@its1.de>2026-07-12 22:55:49 +0200
committerThomas Schmucker <ts@its1.de>2026-07-12 22:55:49 +0200
commitf010c2032f4054f2eae544166814d95c8c5ff3d9 (patch)
treedb6466d005d8e2fbf84a639cabc8d3b2b157a4ec /src
parentc17b523d4710134218bca4f2fdbe8fd0cee39ecd (diff)
downloaduse-zlib-f010c2032f4054f2eae544166814d95c8c5ff3d9.tar.gz
use-zlib-f010c2032f4054f2eae544166814d95c8c5ff3d9.tar.bz2
use-zlib-f010c2032f4054f2eae544166814d95c8c5ff3d9.zip
compress und uncompress Funktionen
Diffstat (limited to 'src')
-rw-r--r--src/simple.c93
1 files changed, 71 insertions, 22 deletions
diff --git a/src/simple.c b/src/simple.c
index 5209841..36f7e48 100644
--- a/src/simple.c
+++ b/src/simple.c
@@ -15,21 +15,85 @@ usage(void)
15 exit(EXIT_FAILURE); 15 exit(EXIT_FAILURE);
16} 16}
17 17
18unsigned long
19get_filesize(FILE *file)
20{
21 (void) fseek(file, 0, SEEK_END);
22 unsigned long size = ftell(file);
23 (void) fseek(file, 0, SEEK_SET);
24 return size;
25}
26
27void
28do_compress(FILE *in_file, FILE *out_file, int compress_level)
29{
30 const unsigned long in_size = get_filesize(in_file);
31
32 (void) fwrite(&in_size, sizeof in_size, 1, out_file);
33
34 unsigned char *in_data = malloc(in_size);
35 (void) fread(in_data, 1, in_size, in_file);
36
37 unsigned long out_size = compressBound(in_size);
38 unsigned char *out_data = malloc(out_size);
39
40 const int error = compress2(out_data, &out_size, in_data, in_size, compress_level);
41
42 if ( !error ) {
43 (void) fwrite(out_data, 1, out_size, out_file);
44 }
45 else {
46 printf("Error: %d\n", error);
47 }
48
49 free(out_data);
50 free(in_data);
51}
52
53void
54do_uncompress(FILE *in_file, FILE *out_file)
55{
56 const unsigned long in_size = get_filesize(in_file);
57
58 unsigned char *in_data = malloc(in_size);
59 (void) fread(in_data, 1, in_size, in_file);
60
61 unsigned long out_size = *(unsigned long *) in_data;
62 unsigned char *out_data = malloc(out_size);
63
64 unsigned long tmp = in_size - sizeof(unsigned long);
65
66 const int error = uncompress2(out_data,
67 &out_size,
68 in_data + sizeof(unsigned long),
69 &tmp);
70
71 if ( !error ) {
72 (void) fwrite(out_data, 1, out_size, out_file);
73 }
74 else {
75 printf("Error: %d\n", error);
76 }
77
78 free(out_data);
79 free(in_data);
80}
81
18int 82int
19main(int argc, char *argv[]) 83main(int argc, char *argv[])
20{ 84{
21 // int compress = 1; 85 int decompress = 0;
22 int level = 5; 86 int compress_level = 5;
23 87
24 int opt = 0; 88 int opt = 0;
25 while ( (opt = getopt(argc, argv, "dl")) != -1 ) { 89 while ( (opt = getopt(argc, argv, "dl")) != -1 ) {
26 switch ( opt ) { 90 switch ( opt ) {
27 case 'd': 91 case 'd':
28 // compress = 0; // decompress 92 decompress = 1;
29 break; 93 break;
30 94
31 case 'l': 95 case 'l':
32 level = 9; 96 compress_level = 9;
33 break; 97 break;
34 98
35 case '?': 99 case '?':
@@ -48,28 +112,13 @@ main(int argc, char *argv[])
48 FILE *in_file = fopen(argv[0], "rb"); 112 FILE *in_file = fopen(argv[0], "rb");
49 FILE *out_file = fopen(argv[1], "wb"); 113 FILE *out_file = fopen(argv[1], "wb");
50 114
51 (void) fseek(in_file, 0, SEEK_END); 115 if ( !decompress ) {
52 unsigned long insize = ftell(in_file); // max 2GB... 116 do_compress(in_file, out_file, compress_level);
53 (void) fseek(in_file, 0, SEEK_SET);
54
55 unsigned char *indata = malloc(insize);
56 (void) fread(indata, 1, insize, in_file);
57
58 unsigned long outsize = compressBound(insize);
59 unsigned char *outdata = malloc(outsize);
60
61 int res = compress2(outdata, &outsize, indata, insize, level);
62
63 if ( res == Z_OK ) {
64 (void) fwrite(outdata, 1, outsize, out_file);
65 } 117 }
66 else { 118 else {
67 printf("Error: %d\n", res); 119 do_uncompress(in_file, out_file);
68 } 120 }
69 121
70 free(outdata);
71 free(indata);
72
73 (void) fclose(out_file); 122 (void) fclose(out_file);
74 (void) fclose(in_file); 123 (void) fclose(in_file);
75 124