summaryrefslogtreecommitdiffstats
path: root/src/gzip.c
blob: cd56d3af79894ded665807efce0de4869371042c (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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
/*
 * Copyright (C) 2023  Patrick McDermott
 *
 * This file is part of opkg-opkg.
 *
 * opkg-opkg is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * opkg-opkg is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with opkg-opkg.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <stdio.h>
#include <stdlib.h>
#include <zlib.h>
#include "defs.h"
#include "gzip.h"
#include "ustar.h"

#define OPKG_OPK_GZIP_WINDOW_BITS_ (15 + 16)

enum _opkg_opk_gzip_dir {
	_OPKG_OPK_GZIP_DIR_READ,
	_OPKG_OPK_GZIP_DIR_WRITE,
};

struct opkg_opk_gzip {
	enum _opkg_opk_gzip_dir  dir;
	opkg_opk_gzip_read_func *read_func;
	void                    *user_data;
	FILE                    *write_fp;
	char                     write_buffer[512];
	z_stream                 stream;
	gz_header                gz_header;
};

struct opkg_opk_gzip *
opkg_opk_gzip_init_read(opkg_opk_gzip_read_func *read_func, void *user_data)
{
	struct opkg_opk_gzip *gzip;

	gzip = malloc(sizeof(*gzip));
	if (gzip == NULL) {
		return NULL;
	}

	gzip->dir        = _OPKG_OPK_GZIP_DIR_READ;
	gzip->read_func  = read_func;
	gzip->user_data  = user_data;

	gzip->stream.zalloc   = Z_NULL;
	gzip->stream.zfree    = Z_NULL;
	gzip->stream.opaque   = Z_NULL;

	gzip->stream.next_in  = Z_NULL;
	gzip->stream.avail_in = 0;
	if (inflateInit2(&gzip->stream, OPKG_OPK_GZIP_WINDOW_BITS_) != Z_OK) {
		free(gzip);
		return NULL;
	}

	return gzip;
}

struct opkg_opk_gzip *
opkg_opk_gzip_init_write(FILE *write_fp)
{
	struct opkg_opk_gzip *gzip;

	gzip = malloc(sizeof(*gzip));
	if (gzip == NULL) {
		return NULL;
	}

	gzip->dir      = _OPKG_OPK_GZIP_DIR_WRITE;
	gzip->write_fp = write_fp;

	gzip->stream.zalloc   = Z_NULL;
	gzip->stream.zfree    = Z_NULL;
	gzip->stream.opaque   = Z_NULL;

	gzip->stream.next_out  = Z_NULL;
	gzip->stream.avail_out = 0;
	if (deflateInit2(&gzip->stream, 9, Z_DEFLATED,
				OPKG_OPK_GZIP_WINDOW_BITS_, 9,
				Z_DEFAULT_STRATEGY) != Z_OK) {
		free(gzip);
		return NULL;
	}
	gzip->gz_header.text    = 0;
	gzip->gz_header.time    = 0;       /* Stored as 32-bit uint */
	gzip->gz_header.os      = 3;       /* Unix, per RFC 1952 */
	gzip->gz_header.extra   = Z_NULL;
	gzip->gz_header.name    = Z_NULL;
	gzip->gz_header.comment = Z_NULL;
	gzip->gz_header.hcrc    = 0;
	if (deflateSetHeader(&gzip->stream, &gzip->gz_header) != Z_OK) {
		deflateEnd(&gzip->stream);
		free(gzip);
		return NULL;
	}

	return gzip;
}

int
opkg_opk_gzip_read(struct opkg_opk_gzip *gzip, void *record)
{
	int end;

	/* Sanity check */
	if (gzip->dir != _OPKG_OPK_GZIP_DIR_READ) {
		return OPKG_OPK_ERROR;
	}

	gzip->stream.next_out  = record;
	gzip->stream.avail_out = OPKG_OPK_USTAR_RECORD_SIZE;

	for (;;) {
		end = 0;
		if (gzip->stream.avail_in == 0) {
			/* Input buffer is empty and needs refilled. */
			switch (gzip->read_func(gzip->user_data,
						(char **) &gzip->stream.next_in,
						(size_t *) &gzip->stream.
							avail_in)) {
				case OPKG_OPK_OK:
					break;
				case OPKG_OPK_END:
					end = 1;
					break;
				case OPKG_OPK_ERROR:
				default:
					return OPKG_OPK_ERROR;
			}
		}
		switch (inflate(&gzip->stream, Z_SYNC_FLUSH)) {
			case Z_OK:
				break;
			case Z_BUF_ERROR:
				if (end == 1) {
					return OPKG_OPK_ERROR;
				}
				break;
			case Z_STREAM_END:
				if (gzip->stream.avail_out != 0) {
					/* Premature end */
					return OPKG_OPK_ERROR;
				}
				return OPKG_OPK_END;
			default:
				return OPKG_OPK_ERROR;
		}
		if (gzip->stream.avail_out == 0) {
			/* Output buffer is filled and ready for use. */
			return OPKG_OPK_OK;
		}
	}
}

static int
_opkg_opk_gzip_write(struct opkg_opk_gzip *gzip, void *record, size_t size,
		int last)
{
	size_t len;

//	memcpy(gzip->buffer, record, size);
//	gzip->write_func(gzip->user_data, size);
//	if (last == 0) {
//		return OPKG_OPK_OK;
//	} else {
//		return OPKG_OPK_END;
//	}

	/* Sanity check */
	if (gzip->dir != _OPKG_OPK_GZIP_DIR_WRITE) {
		return OPKG_OPK_ERROR;
	}

	gzip->stream.next_in  = record;
	gzip->stream.avail_in = size;

	do {
		gzip->stream.next_out  = gzip->write_buffer;
		gzip->stream.avail_out = sizeof(gzip->write_buffer);
		switch (deflate(&gzip->stream,
					(last > 0 ? Z_FINISH : Z_NO_FLUSH))) {
			case Z_OK:
			case Z_BUF_ERROR:
			case Z_STREAM_END:
				break;
			default:
				return OPKG_OPK_ERROR;
		}
		/* Process output buffer. */
		len = sizeof(gzip->write_buffer) - gzip->stream.avail_out;
		if (fwrite(gzip->write_buffer, 1, len, gzip->write_fp) != len) {
			return OPKG_OPK_ERROR;
		}
	} while (gzip->stream.avail_out == 0);

	if (gzip->stream.avail_in != 0) {
		return OPKG_OPK_ERROR;
	}
	if (last == 0) {
		/* Input buffer is empty and needs refilled. */
		return OPKG_OPK_OK;
	} else {
		return OPKG_OPK_END;
	}
}

int
opkg_opk_gzip_write(struct opkg_opk_gzip *gzip, void *record, size_t size)
{
	if (_opkg_opk_gzip_write(gzip, record, size, 0) == OPKG_OPK_OK) {
		return OPKG_OPK_OK;
	} else {
		return OPKG_OPK_ERROR;
	}
}

int
opkg_opk_gzip_free(struct opkg_opk_gzip *gzip)
{
	int ret;

	ret = OPKG_OPK_OK;
	if (gzip->dir == _OPKG_OPK_GZIP_DIR_READ) {
		if (inflateEnd(&gzip->stream) != Z_OK) {
			ret = OPKG_OPK_ERROR;
		}
	} else {
		if (_opkg_opk_gzip_write(gzip, NULL, 0, 1) != OPKG_OPK_END) {
			ret = OPKG_OPK_ERROR;
		}
		if (deflateEnd(&gzip->stream) != Z_OK) {
			ret = OPKG_OPK_ERROR;
		}
	}
	free(gzip);
	return ret;
}

#if 1  /* Test code */

/*
 * echo 2.0 >debian-binary
 * tar -cf test.tar debian-binary          # if using GNU tar...
 * truncate -s 2048 test.tar               # ...remove extra trailer records
 * busybox tar -cf test.tar debian-binary  # or just use BB tar
 * flags=$(pkg-config --cflags --libs zlib)
 * gcc -Wall -Wextra -pedantic -g -O0 -o gzip src/gzip.c ${flags}
 * ./gzip test.tar test.tar.gz
 * gunzip -c test.tar.gz 1>test.tar.ungz
 * ls -l test.tar*             # test.tar and test.tar.ungz should be 2048 bytes
 *                             # test.tar.gz should be 108 bytes or so
 * cmp test.tar test.tar.ungz  # should be quiet
 * bvi test.tar.gz             # compare to RFC 1952
 * gzip -9cn test.tar 1>test.tar.gzip
 * ls -l test.tar.gz*          # both should be 108 bytes or so
 * cmp test.tar.gz*            # should be quiet
 */

#include <stdio.h>

int
main(int argc __attribute__((__unused__)), char *argv[])
{
	int                   ret;
	FILE                 *in_fp;
	FILE                 *out_fp;
	struct opkg_opk_gzip *gzip;
	char                  in_buffer[512];
	size_t                in_size;

	ret = EXIT_SUCCESS;
	in_fp = fopen(argv[1], "rb");
	if (in_fp == NULL) {
		fputs("Error opening input", stderr);
		ret = EXIT_FAILURE;
		goto out0;
	}
	out_fp = fopen(argv[2], "wb");
	if (out_fp == NULL) {
		fputs("Error opening output", stderr);
		ret = EXIT_FAILURE;
		goto out1;
	}
	gzip = opkg_opk_gzip_init_write(out_fp);
	if (gzip == NULL) {
		fputs("Error initializing compressor", stderr);
		ret = EXIT_FAILURE;
		goto out2;
	}
	while ((in_size = fread(in_buffer, 1, sizeof(in_buffer), in_fp)) > 0) {
		if (opkg_opk_gzip_write(gzip, in_buffer, in_size) !=
				OPKG_OPK_OK) {
			fputs("Error compressing", stderr);
			ret = EXIT_FAILURE;
			goto out3;
		}
	}
 out3:
	if (opkg_opk_gzip_free(gzip) != OPKG_OPK_OK) {
		fputs("Error freeing compressor", stderr);
		ret = EXIT_FAILURE;
	}
 out2:
	fclose(out_fp);
 out1:
	fclose(in_fp);
 out0:
	return ret;
}

#endif