summaryrefslogtreecommitdiff
path: root/src/stream.c
blob: 286f41aff2721434c8b0ffd634e2680353774911 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
// Standard C
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

// System
#include <unistd.h>

// zlib
#include <zlib.h>

enum { BUFFER_SIZE = 8192 };

static void
usage(void)
{
	(void) fprintf(stderr, "compress [-d] infile outfile\n");
}

static void
print_zlib_error(const char *operation, int error)
{
	if ( error == Z_ERRNO ) {
		(void) fprintf(stderr, "%s error: %s\n", operation, strerror(errno));
	}
	else {
		(void) fprintf(stderr, "%s error: %s\n", operation, zError(error));
	}
}

int
do_compress(FILE *in_file, FILE *out_file, int compress_level)
{
	z_stream strm = { 0 };

	int error = deflateInit(&strm, compress_level);
	if ( error ) {
		return error;
	}

	unsigned char in_buffer[BUFFER_SIZE];
	unsigned char out_buffer[BUFFER_SIZE];
	int           flush = Z_NO_FLUSH;

	do {
		strm.avail_in = fread(in_buffer, 1, BUFFER_SIZE, in_file);

		if ( ferror(in_file) ) {
			deflateEnd(&strm);
			return Z_ERRNO;
		}

		flush        = feof(in_file) ? Z_FINISH : Z_NO_FLUSH;
		strm.next_in = in_buffer;

		do {
			strm.avail_out = BUFFER_SIZE;
			strm.next_out  = out_buffer;

			error = deflate(&strm, flush);

			size_t have = BUFFER_SIZE - strm.avail_out;
			if ( fwrite(out_buffer, 1, have, out_file) != have || ferror(out_file) ) {
				deflateEnd(&strm);
				return Z_ERRNO;
			}
		} while ( strm.avail_out == 0 );
	} while ( flush != Z_FINISH );

	deflateEnd(&strm);
	return Z_OK;
}

int
do_uncompress(FILE *in_file, FILE *out_file)
{
	z_stream strm = { 0 };

	int error = inflateInit(&strm);
	if ( error ) {
		return error;
	}

	unsigned char in_buffer[BUFFER_SIZE];
	unsigned char out_buffer[BUFFER_SIZE];

	do {
		strm.avail_in = fread(in_buffer, 1, BUFFER_SIZE, in_file);

		if ( ferror(in_file) ) {
			inflateEnd(&strm);
			return Z_ERRNO;
		}

		if ( strm.avail_in == 0 ) {
			break;
		}

		strm.next_in = in_buffer;

		do {
			strm.avail_out = BUFFER_SIZE;
			strm.next_out  = out_buffer;

			error = inflate(&strm, Z_NO_FLUSH);

			switch ( error ) {
			case Z_NEED_DICT:
				error = Z_DATA_ERROR; /* and fall through */
			case Z_DATA_ERROR:
			case Z_MEM_ERROR:
				(void) inflateEnd(&strm);
				return error;
			default:
				break;
			}

			size_t have = BUFFER_SIZE - strm.avail_out;
			if ( fwrite(out_buffer, 1, have, out_file) != have || ferror(out_file) ) {
				(void) inflateEnd(&strm);
				return Z_ERRNO;
			}
		} while ( strm.avail_out == 0 );
	} while ( error != Z_STREAM_END );

	inflateEnd(&strm);

	return error == Z_STREAM_END ? Z_OK : Z_DATA_ERROR;
}

int
main(int argc, char *argv[])
{
	enum {
		COMPRESSION_STD  = 5,
		COMPRESSION_BEST = 9
	};

	int decompress     = 0;
	int compress_level = COMPRESSION_STD;

	int opt = 0;
	while ( (opt = getopt(argc, argv, "dl")) != -1 ) {
		switch ( opt ) {
		case 'd':
			decompress = 1;
			break;

		case 'l':
			compress_level = COMPRESSION_BEST;
			break;

		case '?':
		default:
			usage();
			return EXIT_SUCCESS;
		}
	}
	argc -= optind;
	argv += optind;

	if ( argc != 2 ) {
		usage();
	}

	FILE *in_file  = fopen(argv[0], "rb");
	FILE *out_file = fopen(argv[1], "wb");

	if ( !decompress ) {
		int error = do_compress(in_file, out_file, compress_level);
		if ( error ) {
			print_zlib_error("compression", error);
		}
	}
	else {
		int error = do_uncompress(in_file, out_file);
		if ( error ) {
			print_zlib_error("extraction", error);
		}
	}

	(void) fclose(out_file);
	(void) fclose(in_file);

	return EXIT_SUCCESS;
}