summaryrefslogtreecommitdiffstats
path: root/src/decompress.c
blob: c4832410266844022eb1b4cdeb542d6c79d6c994 (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
/*
 * decompress.c: decompression abstraction layer
 *
 * Copyright (C) 2007, 2008 Colin Watson.
 *
 * This file is part of man-db.
 *
 * man-db 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 2 of the License, or
 * (at your option) any later version.
 *
 * man-db 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 man-db; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

#ifdef HAVE_CONFIG_H
#  include "config.h"
#endif /* HAVE_CONFIG_H */

#include <assert.h>
#include <string.h>
#include <stdbool.h>
#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>

#ifdef HAVE_LIBZ
#  include "zlib.h"
#endif /* HAVE_LIBZ */

#include "pipeline.h"

#include "attribute.h"
#include "minmax.h"
#include "xalloc.h"
#include "xstrndup.h"
#include "xvasprintf.h"

#include "manconfig.h"

#include "compression.h"
#include "sandbox.h"

#include "decompress.h"

enum decompress_tag {
	DECOMPRESS_PIPELINE,
	DECOMPRESS_INPROCESS
};

struct decompress_inprocess {
	char *buf;
	size_t len;
	size_t offset;
	char *line_cache;
};

struct decompress {
	enum decompress_tag tag;
	union {
		pipeline *p;
		struct decompress_inprocess inprocess;
	} u;
};

/* Create a new pipeline-based decompressor.  Takes ownership of p. */
static decompress *decompress_new_pipeline (pipeline *p)
{
	decompress *d = XMALLOC (decompress);

	d->tag = DECOMPRESS_PIPELINE;
	d->u.p = p;

	return d;
}

#ifdef HAVE_LIBZ

/* Create a new in-process decompressor.  Takes ownership of buf. */
static decompress *decompress_new_inprocess (char *buf, size_t len)
{
	decompress *d = XMALLOC (decompress);

	d->tag = DECOMPRESS_INPROCESS;
	d->u.inprocess.buf = buf;
	d->u.inprocess.len = len;
	d->u.inprocess.offset = 0;
	d->u.inprocess.line_cache = NULL;

	return d;
}

static void decompress_zlib (void *data MAYBE_UNUSED)
{
	gzFile zlibfile;
	int fd;

	fd = dup (STDIN_FILENO);
	if (fd < 0)
		return;

	zlibfile = gzdopen (fd, "r");
	if (!zlibfile) {
		close (fd);
		return;
	}

	for (;;) {
		char buffer[4096];
		int r = gzread (zlibfile, buffer, 4096);
		if (r <= 0)
			break;
		if (fwrite (buffer, 1, (size_t) r, stdout) < (size_t) r)
			break;
	}

	gzclose (zlibfile);
	return;
}

/* The largest number of uncompressed bytes we're prepared to read into
 * memory.  (We actually allow at most one fewer byte than this, for easy
 * EOF detection.)
 *
 * At the time of writing, 11 out of 27959 (0.04%) installed manual pages on
 * the author's system were larger than this.
 *
 * We could lift this restriction if we streamed in-process decompression
 * instead, but that's a bit complicated: we'd also need to stream encoding
 * conversion, and there's relatively little point until lexgrog can rely on
 * preprocessor header lines rather than having to scan the whole file for
 * preprocessor indications.  For the time being, one-shot buffering is
 * cheap enough and much simpler.
 */
#define MAX_INPROCESS 1048576

static decompress *decompress_try_zlib (const char *filename)
{
	gzFile zlibfile;
	/* We only ever call this from the parent process (and don't
	 * currently use threads), and this lets us skip per-file memory
	 * allocation.
	 */
	static char buffer[MAX_INPROCESS];
	int len = 0;

	zlibfile = gzopen (filename, "r");
	if (!zlibfile)
		return NULL;

	while (len < MAX_INPROCESS) {
		/* Read one more byte than we're prepared to return, in
		 * order to detect EOF at the right position.  The "len >=
		 * MAX_INPROCESS" check below catches the boundary case.
		 */
		int r = gzread (zlibfile, buffer + len, MAX_INPROCESS - len);
		if (r < 0) {
			gzclose (zlibfile);
			return NULL;
		} else if (r == 0)
			break;
		else
			len += r;
	}

	gzclose (zlibfile);
	if (len >= MAX_INPROCESS)
		return NULL;
	/* Copy input data so that we don't have potential data corruption
	 * if more than one in-process decompressor is active at once.  (An
	 * alternative might be to use a lock to prevent that situation.)
	 */
	return decompress_new_inprocess (xmemdup (buffer, (size_t) len),
					 (size_t) len);
}

#define OPEN_FLAGS_UNUSED
#else /* !HAVE_LIBZ */
#define OPEN_FLAGS_UNUSED MAYBE_UNUSED
#endif /* HAVE_LIBZ */

extern man_sandbox *sandbox;

decompress *decompress_open (const char *filename, int flags OPEN_FLAGS_UNUSED)
{
	pipecmd *cmd;
	pipeline *p;
	struct stat st;
#ifdef HAVE_LIBZ
	size_t filename_len;
#endif /* HAVE_LIBZ */
	char *ext;
	struct compression *comp;

	if (stat (filename, &st) < 0 || S_ISDIR (st.st_mode))
		return NULL;

#ifdef HAVE_LIBZ
	filename_len = strlen (filename);
	if (filename_len > 3 && STREQ (filename + filename_len - 3, ".gz")) {
		if (flags & DECOMPRESS_ALLOW_INPROCESS) {
			decompress *d = decompress_try_zlib (filename);
			if (d)
				return d;
		}

		cmd = pipecmd_new_function ("zcat", &decompress_zlib, NULL,
					    NULL);
		pipecmd_pre_exec (cmd, sandbox_load, sandbox_free, sandbox);
		p = pipeline_new_commands (cmd, (void *) 0);
		goto got_pipeline;
	}
#endif /* HAVE_LIBZ */

	ext = strrchr (filename, '.');
	if (ext) {
		++ext;

		for (comp = comp_list; comp->ext; ++comp) {
			if (!STREQ (comp->ext, ext))
				continue;

			cmd = pipecmd_new_argstr (comp->prog);
			pipecmd_pre_exec (cmd, sandbox_load, sandbox_free,
					  sandbox);
			p = pipeline_new_commands (cmd, (void *) 0);
			goto got_pipeline;
		}
	}

#ifdef HAVE_GZIP
	/* HP-UX */
	ext = strstr (filename, ".Z/");
	if (ext) {
		cmd = pipecmd_new_argstr (PROG_GUNZIP);
		pipecmd_pre_exec (cmd, sandbox_load, sandbox_free, sandbox);
		p = pipeline_new_commands (cmd, (void *) 0);
		goto got_pipeline;
	}
#endif

	p = pipeline_new ();

got_pipeline:
	pipeline_want_infile (p, filename);
	pipeline_want_out (p, -1);
	return decompress_new_pipeline (p);
}

decompress *decompress_fdopen (int fd)
{
	pipeline *p;
#ifdef HAVE_LIBZ
	pipecmd *cmd;
#endif /* HAVE_LIBZ */

#ifdef HAVE_LIBZ
	cmd = pipecmd_new_function ("zcat", &decompress_zlib, NULL, NULL);
	pipecmd_pre_exec (cmd, sandbox_load, sandbox_free, sandbox);
	p = pipeline_new_commands (cmd, (void *) 0);
#else /* HAVE_LIBZ */
	p = pipeline_new ();
#endif /* HAVE_LIBZ */

	pipeline_want_in (p, fd);
	pipeline_want_out (p, -1);
	return decompress_new_pipeline (p);
}

bool ATTRIBUTE_PURE decompress_is_pipeline (decompress *d)
{
	return d->tag == DECOMPRESS_PIPELINE;
}

pipeline * ATTRIBUTE_PURE decompress_get_pipeline (decompress *d)
{
	assert (d->tag == DECOMPRESS_PIPELINE);
	return d->u.p;
}

const char * ATTRIBUTE_PURE decompress_inprocess_buf (decompress *d)
{
	assert (d->tag == DECOMPRESS_INPROCESS);
	return d->u.inprocess.buf;
}

size_t ATTRIBUTE_PURE decompress_inprocess_len (decompress *d)
{
	assert (d->tag == DECOMPRESS_INPROCESS);
	return d->u.inprocess.len;
}

void decompress_inprocess_replace (decompress *d, char *buf, size_t len)
{
	assert (d->tag == DECOMPRESS_INPROCESS);

	free (d->u.inprocess.line_cache);
	free (d->u.inprocess.buf);

	d->u.inprocess.buf = buf;
	d->u.inprocess.len = len;
	d->u.inprocess.offset = 0;
	d->u.inprocess.line_cache = NULL;
}

void decompress_start (decompress *d)
{
	if (d->tag == DECOMPRESS_PIPELINE)
		pipeline_start (d->u.p);
}

const char *decompress_read (decompress *d, size_t *len)
{
	if (d->tag == DECOMPRESS_PIPELINE)
		return pipeline_read (d->u.p, len);
	else {
		const char *ret;
		assert (d->tag == DECOMPRESS_INPROCESS);
		*len = MIN (*len, d->u.inprocess.len - d->u.inprocess.offset);
		ret = d->u.inprocess.buf + d->u.inprocess.offset;
		d->u.inprocess.offset += *len;
		return ret;
	}
}

const char *decompress_peek (decompress *d, size_t *len)
{
	if (d->tag == DECOMPRESS_PIPELINE)
		return pipeline_peek (d->u.p, len);
	else {
		assert (d->tag == DECOMPRESS_INPROCESS);
		*len = MIN (*len, d->u.inprocess.len - d->u.inprocess.offset);
		return d->u.inprocess.buf + d->u.inprocess.offset;
	}
}

void decompress_peek_skip (decompress *d, size_t len)
{
	if (d->tag == DECOMPRESS_PIPELINE)
		pipeline_peek_skip (d->u.p, len);
	else {
		assert (d->tag == DECOMPRESS_INPROCESS);
		assert (len <= d->u.inprocess.len - d->u.inprocess.offset);
		d->u.inprocess.offset += len;
	}
}

const char *decompress_readline (decompress *d)
{
	if (d->tag == DECOMPRESS_PIPELINE)
		return pipeline_readline (d->u.p);
	else {
		const char *cur, *end;
		assert (d->tag == DECOMPRESS_INPROCESS);
		/* This isn't on the hot path (only called for a few lines
		 * at the start of the file), so we can afford to
		 * reallocate.
		 */
		if (d->u.inprocess.line_cache) {
			free (d->u.inprocess.line_cache);
			d->u.inprocess.line_cache = NULL;
		}
		cur = d->u.inprocess.buf + d->u.inprocess.offset;
		end = memchr (cur, '\n',
			      d->u.inprocess.len - d->u.inprocess.offset);
		if (end) {
			d->u.inprocess.line_cache = xstrndup
				(cur, end - cur + 1);
			d->u.inprocess.offset += end - cur + 1;
			return d->u.inprocess.line_cache;
		} else
			return NULL;
	}
}

const char *decompress_peekline (decompress *d)
{
	if (d->tag == DECOMPRESS_PIPELINE)
		return pipeline_peekline (d->u.p);
	else {
		const char *cur, *end;
		assert (d->tag == DECOMPRESS_INPROCESS);
		/* This isn't on the hot path (only called for a few lines
		 * at the start of the file), so we can afford to
		 * reallocate.
		 */
		if (d->u.inprocess.line_cache) {
			free (d->u.inprocess.line_cache);
			d->u.inprocess.line_cache = NULL;
		}
		cur = d->u.inprocess.buf + d->u.inprocess.offset;
		end = memchr (cur, '\n',
			      d->u.inprocess.len - d->u.inprocess.offset);
		if (end) {
			d->u.inprocess.line_cache = xstrndup
				(cur, end - cur + 1);
			return d->u.inprocess.line_cache;
		} else
			return NULL;
	}
}

int decompress_wait (decompress *d)
{
	if (d->tag == DECOMPRESS_PIPELINE)
		return pipeline_wait (d->u.p);
	else {
		assert (d->tag == DECOMPRESS_INPROCESS);
		return 0;
	}
}

void decompress_free (decompress *d)
{
	if (!d)
		return;
	if (d->tag == DECOMPRESS_PIPELINE)
		pipeline_free (d->u.p);
	else {
		assert (d->tag == DECOMPRESS_INPROCESS);
		free (d->u.inprocess.line_cache);
		free (d->u.inprocess.buf);
	}
	free (d);
}