summaryrefslogtreecommitdiff
path: root/usr.bin/compress/gzopen.c
blob: eebc943543ead28a24362355b3ab102c52a4b3e7 (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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
/*	$OpenBSD: gzopen.c,v 1.35 2022/06/18 03:23:19 gkoehler Exp $	*/

/*
 * Copyright (c) 1997 Michael Shalayeff
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 */
/* this is partially derived from the zlib's gzio.c file, so the notice: */
/*
  zlib.h -- interface of the 'zlib' general purpose compression library
  version 1.0.4, Jul 24th, 1996.

  Copyright (C) 1995-1996 Jean-loup Gailly and Mark Adler

  This software is provided 'as-is', without any express or implied
  warranty.  In no event will the authors be held liable for any damages
  arising from the use of this software.

  Permission is granted to anyone to use this software for any purpose,
  including commercial applications, and to alter it and redistribute it
  freely, subject to the following restrictions:

  1. The origin of this software must not be misrepresented; you must not
     claim that you wrote the original software. If you use this software
     in a product, an acknowledgment in the product documentation would be
     appreciated but is not required.
  2. Altered source versions must be plainly marked as such, and must not be
     misrepresented as being the original software.
  3. This notice may not be removed or altered from any source distribution.

  Jean-loup Gailly        Mark Adler
  gzip@prep.ai.mit.edu    madler@alumni.caltech.edu


  The data format used by the zlib library is described by RFCs (Request for
  Comments) 1950 to 1952 in the files ftp://ds.internic.net/rfc/rfc1950.txt
  (zlib format), rfc1951.txt (deflate format) and rfc1952.txt (gzip format).
*/

#include <sys/stat.h>
#include <sys/uio.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <limits.h>
#include <zlib.h>
#include "compress.h"

/* gzip flag byte */
#define ASCII_FLAG   0x01 /* bit 0 set: file probably ascii text */
#define HEAD_CRC     0x02 /* bit 1 set: header CRC present */
#define EXTRA_FIELD  0x04 /* bit 2 set: extra field present */
#define ORIG_NAME    0x08 /* bit 3 set: original file name present */
#define COMMENT      0x10 /* bit 4 set: file comment present */
#define RESERVED     0xE0 /* bits 5..7: reserved */

#define DEF_MEM_LEVEL 8
#define OS_CODE 0x03 /* unix */

typedef
struct gz_stream {
	int	z_fd;		/* .gz file */
	int	z_eof;		/* set if end of input file */
	z_stream z_stream;	/* libz stream */
	u_char	z_buf[Z_BUFSIZE]; /* i/o buffer */
	char	z_mode;		/* 'w' or 'r' */
	u_int32_t z_time;	/* timestamp (mtime) */
	u_int32_t z_crc;	/* crc32 of uncompressed data */
	u_int32_t z_hlen;	/* length of the gz header */
	u_int64_t z_total_in;	/* # bytes in */
	u_int64_t z_total_out;	/* # bytes out */
} gz_stream;

static const u_char gz_magic[2] = {0x1f, 0x8b}; /* gzip magic header */

static u_int32_t get_int32(gz_stream *);
static int get_header(gz_stream *, char *, int);
static int get_byte(gz_stream *);

void *
gz_ropen(int fd, char *name, int gotmagic)
{
	gz_stream *s;

	if (fd < 0)
		return NULL;

	if ((s = calloc(1, sizeof(gz_stream))) == NULL)
		return NULL;

	s->z_stream.zalloc = (alloc_func)0;
	s->z_stream.zfree = (free_func)0;
	s->z_stream.opaque = (voidpf)0;
	s->z_stream.next_in = Z_NULL;
	s->z_stream.next_out = Z_NULL;
	s->z_stream.avail_in = s->z_stream.avail_out = 0;
	s->z_fd = 0;
	s->z_eof = 0;
	s->z_time = 0;
	s->z_hlen = 0;
	s->z_total_in = 0;
	s->z_total_out = 0;
	s->z_crc = crc32(0L, Z_NULL, 0);
	s->z_mode = 'r';

	if (inflateInit2(&(s->z_stream), -MAX_WBITS) != Z_OK) {
		free (s);
		return NULL;
	}
	s->z_stream.next_in = s->z_buf;
	s->z_stream.avail_out = Z_BUFSIZE;

	errno = 0;
	s->z_fd = fd;

	/* read the .gz header */
	if (get_header(s, name, gotmagic) != 0) {
		gz_close(s, NULL, NULL, NULL);
		s = NULL;
	}

	return s;
}

static int
get_byte(gz_stream *s)
{
	if (s->z_eof)
		return EOF;

	if (s->z_stream.avail_in == 0) {
		errno = 0;
		s->z_stream.avail_in = read(s->z_fd, s->z_buf, Z_BUFSIZE);
		if ((int)s->z_stream.avail_in <= 0) {
			s->z_eof = 1;
			return EOF;
		}
		s->z_stream.next_in = s->z_buf;
	}
	s->z_stream.avail_in--;
	return *s->z_stream.next_in++;
}

static u_int32_t
get_int32(gz_stream *s)
{
	u_int32_t x;

	x  = ((u_int32_t)(get_byte(s) & 0xff));
	x |= ((u_int32_t)(get_byte(s) & 0xff))<<8;
	x |= ((u_int32_t)(get_byte(s) & 0xff))<<16;
	x |= ((u_int32_t)(get_byte(s) & 0xff))<<24;
	return x;
}

static int
get_header(gz_stream *s, char *name, int gotmagic)
{
	int method; /* method byte */
	int flags;  /* flags byte */
	char *ep;
	uInt len;
	int c;

	/* Check the gzip magic header */
	if (!gotmagic) {
		for (len = 0; len < 2; len++) {
			c = get_byte(s);
			if (c != gz_magic[len]) {
				errno = EFTYPE;
				return -1;
			}
		}
	}

	method = get_byte(s);
	flags = get_byte(s);
	if (method != Z_DEFLATED || (flags & RESERVED) != 0) {
		errno = EFTYPE;
		return -1;
	}

	/* Stash timestamp (mtime) */
	s->z_time = get_int32(s);

	/* Discard xflags and OS code */
	(void)get_byte(s);
	(void)get_byte(s);

	s->z_hlen += 10; /* magic, method, flags, time, xflags, OS code */
	if ((flags & EXTRA_FIELD) != 0) { /* skip the extra field */
		len  =  (uInt)get_byte(s);
		len += ((uInt)get_byte(s))<<8;
		s->z_hlen += 2;
		/* len is garbage if EOF but the loop below will quit anyway */
		while (len-- != 0 && get_byte(s) != EOF)
			s->z_hlen++;
	}

	if ((flags & ORIG_NAME) != 0) { /* read/save the original file name */
		if ((ep = name) != NULL)
			ep += PATH_MAX - 1;
		while ((c = get_byte(s)) != EOF) {
			s->z_hlen++;
			if (c == '\0')
				break;
			if (name < ep)
				*name++ = c;
		}
		if (name != NULL)
			*name = '\0';
	}

	if ((flags & COMMENT) != 0) {   /* skip the .gz file comment */
		while ((c = get_byte(s)) != EOF) {
			s->z_hlen++;
			if (c == '\0')
				break;
		}
	}

	if ((flags & HEAD_CRC) != 0) {  /* skip the header crc */
		(void)get_byte(s);
		(void)get_byte(s);
		s->z_hlen += 2;
	}

	if (s->z_eof) {
		errno = EFTYPE;
		return -1;
	}

	return 0;
}

int
gz_read(void *cookie, char *buf, int len)
{
	gz_stream *s = (gz_stream*)cookie;
	uLong old_total_in;
	u_char *start = buf; /* starting point for crc computation */
	int error = Z_OK;

	/* z_stream.total_in might overflow uLong. */
	old_total_in = s->z_stream.total_in;

	s->z_stream.next_out = buf;
	s->z_stream.avail_out = len;

	while (error == Z_OK && !s->z_eof && s->z_stream.avail_out != 0) {

		if (s->z_stream.avail_in == 0) {

			errno = 0;
			s->z_stream.avail_in = read(s->z_fd, s->z_buf,
			    Z_BUFSIZE);
			if ((int)s->z_stream.avail_in <= 0)
				s->z_eof = 1;
			s->z_stream.next_in = s->z_buf;
		}

		error = inflate(&(s->z_stream), Z_NO_FLUSH);

		if (error == Z_DATA_ERROR) {
			errno = EINVAL;
			goto bad;
		}
		if (error == Z_BUF_ERROR) {
			errno = EIO;
			goto bad;
		}
		if (error == Z_STREAM_END) {
			/* Check CRC and original size */
			s->z_crc = crc32(s->z_crc, start,
			    (uInt)(s->z_stream.next_out - start));
			start = s->z_stream.next_out;

			if (get_int32(s) != s->z_crc) {
				errno = EINVAL;
				goto bad;
			}
			if (get_int32(s) != (u_int32_t)s->z_stream.total_out) {
				errno = EIO;
				return -1;
			}
			s->z_hlen += 2 * sizeof(int32_t);

			/* Check for the existence of an appended file. */
			if (get_header(s, NULL, 0) != 0) {
				s->z_eof = 1;
				break;
			}
			s->z_total_in += (uLong)(s->z_stream.total_in -
			    old_total_in);
			inflateReset(&(s->z_stream));
			s->z_crc = crc32(0L, Z_NULL, 0);
			old_total_in = 0;
			error = Z_OK;
		}
	}
	s->z_crc = crc32(s->z_crc, start,
	    (uInt)(s->z_stream.next_out - start));
	len -= s->z_stream.avail_out;
	s->z_total_in += (uLong)(s->z_stream.total_in - old_total_in);
	s->z_total_out += len;
	return (len);
bad:
	s->z_total_in += (uLong)(s->z_stream.total_in - old_total_in);
	s->z_total_out += (len - s->z_stream.avail_out);
	return (-1);
}

#ifndef SMALL
static int
put_int32(gz_stream *s, u_int32_t x)
{
	u_int32_t y = htole32(x);

	if (write(s->z_fd, &y, sizeof(y)) != sizeof(y))
		return Z_ERRNO;
	return 0;
}

static int
put_header(gz_stream *s, char *name, u_int32_t mtime, int bits)
{
	struct iovec iov[2];
	u_char buf[10];

	buf[0] = gz_magic[0];
	buf[1] = gz_magic[1];
	buf[2] = Z_DEFLATED;
	buf[3] = name ? ORIG_NAME : 0;
	buf[4] = mtime & 0xff;
	buf[5] = (mtime >> 8) & 0xff;
	buf[6] = (mtime >> 16) & 0xff;
	buf[7] = (mtime >> 24) & 0xff;
	buf[8] = bits == 1 ? 4 : bits == 9 ? 2 : 0;	/* xflags */
	buf[9] = OS_CODE;
	iov[0].iov_base = buf;
	iov[0].iov_len = sizeof(buf);
	s->z_hlen = sizeof(buf);

	if (name != NULL) {
		iov[1].iov_base = name;
		iov[1].iov_len = strlen(name) + 1;
		s->z_hlen += iov[1].iov_len;
	}
	if (writev(s->z_fd, iov, name ? 2 : 1) == -1)
		return (-1);
	return (0);
}

void *
gz_wopen(int fd, char *name, int bits, u_int32_t mtime)
{
	gz_stream *s;

	if (fd < 0)
		return NULL;

	if (bits < 0 || bits > Z_BEST_COMPRESSION) {
		errno = EINVAL;
		return NULL;
	}
	if ((s = calloc(1, sizeof(gz_stream))) == NULL)
		return NULL;

	s->z_stream.zalloc = (alloc_func)0;
	s->z_stream.zfree = (free_func)0;
	s->z_stream.opaque = (voidpf)0;
	s->z_stream.next_in = Z_NULL;
	s->z_stream.next_out = Z_NULL;
	s->z_stream.avail_in = s->z_stream.avail_out = 0;
	s->z_fd = 0;
	s->z_eof = 0;
	s->z_time = 0;
	s->z_hlen = 0;
	s->z_total_in = 0;
	s->z_total_out = 0;
	s->z_crc = crc32(0L, Z_NULL, 0);
	s->z_mode = 'w';

	/* windowBits is passed < 0 to suppress zlib header */
	if (deflateInit2(&(s->z_stream), bits, Z_DEFLATED,
			 -MAX_WBITS, DEF_MEM_LEVEL, 0) != Z_OK) {
		free (s);
		return NULL;
	}
	s->z_stream.next_out = s->z_buf;
	s->z_stream.avail_out = Z_BUFSIZE;

	errno = 0;
	s->z_fd = fd;

	/* write the .gz header */
	if (put_header(s, name, mtime, bits) != 0) {
		gz_close(s, NULL, NULL, NULL);
		s = NULL;
	}

	return s;
}
int
gz_write(void *cookie, const char *buf, int len)
{
	gz_stream *s = (gz_stream*)cookie;

	s->z_stream.next_in = (char *)buf;
	s->z_stream.avail_in = len;

	while (s->z_stream.avail_in != 0) {
		if (s->z_stream.avail_out == 0) {
			s->z_total_out += Z_BUFSIZE;

			if (write(s->z_fd, s->z_buf, Z_BUFSIZE) != Z_BUFSIZE)
				break;
			s->z_stream.next_out = s->z_buf;
			s->z_stream.avail_out = Z_BUFSIZE;
		}
		if (deflate(&(s->z_stream), Z_NO_FLUSH) != Z_OK)
			break;
	}
	s->z_crc = crc32(s->z_crc, buf, len);

	len -= s->z_stream.avail_in;
	s->z_total_in += len;
	return len;
}

int
gz_flush(void *cookie, int flush)
{
	gz_stream *s = (gz_stream*)cookie;
	size_t len;
	int done = 0;
	int err;

	if (s == NULL || s->z_mode != 'w') {
		errno = EBADF;
		return Z_ERRNO;
	}

	s->z_stream.avail_in = 0; /* should be zero already anyway */

	for (;;) {
		len = Z_BUFSIZE - s->z_stream.avail_out;

		if (len != 0) {
			s->z_total_out += len;

			if (write(s->z_fd, s->z_buf, len) != len)
				return Z_ERRNO;
			s->z_stream.next_out = s->z_buf;
			s->z_stream.avail_out = Z_BUFSIZE;
		}
		if (done)
			break;
		if ((err = deflate(&(s->z_stream), flush)) != Z_OK &&
		    err != Z_STREAM_END)
			return err;

		/* deflate has finished flushing only when it hasn't
		 * used up all the available space in the output buffer
		 */
		done = (s->z_stream.avail_out != 0 || err == Z_STREAM_END);
	}
	return 0;
}
#endif

int
gz_close(void *cookie, struct z_info *info, const char *name, struct stat *sb)
{
	gz_stream *s = (gz_stream*)cookie;
	int err = 0;

	if (s == NULL)
		return -1;

#ifndef SMALL
	if (s->z_mode == 'w' && (err = gz_flush (s, Z_FINISH)) == Z_OK) {
		if ((err = put_int32 (s, s->z_crc)) == Z_OK) {
			s->z_hlen += sizeof(int32_t);
			if ((err = put_int32 (s, s->z_stream.total_in)) == Z_OK)
				s->z_hlen += sizeof(int32_t);
		}
	}
#endif
	if (!err && s->z_stream.state != NULL) {
		if (s->z_mode == 'w')
#ifndef SMALL
			err = deflateEnd(&s->z_stream);
#else
			err = -1;
#endif
		else if (s->z_mode == 'r')
			err = inflateEnd(&s->z_stream);
	}

	if (info != NULL) {
		info->mtime = s->z_time;
		info->crc = s->z_crc;
		info->hlen = s->z_hlen;
		info->total_in = s->z_total_in;
		info->total_out = s->z_total_out;
	}

	setfile(name, s->z_fd, sb);
	if (!err)
		err = close(s->z_fd);
	else
		(void)close(s->z_fd);

	free(s);

	return err;
}