summaryrefslogtreecommitdiff
path: root/Build/source/utils/lzma/src/liblzmadec/io.c
blob: d82cfbf75665b53ceb4250b48ab6855afb9a4eed (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
/******************************************************************************

    LZMA decoder library with a zlib like API - lzma_FILE I/O functions

    Copyright (C) 1999-2005 Igor Pavlov (http://7-zip.org/)
    Copyright (C) 2005 Lasse Collin <lasse.collin@tukaani.org>
    Based on zlib.h and bzlib.h. FIXME

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Lesser General Public
    License as published by the Free Software Foundation; either
    version 2.1 of the License, or (at your option) any later version.

    This library 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
    Lesser General Public License for more details.

******************************************************************************/

#ifndef LZMADEC_NO_STDIO

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>

#ifdef HAVE_ERRNO_H
#include <errno.h>
#else
extern int errno
#endif

/* Needed for pre-C99 systems that have SIZE_MAX in limits.h. */
#ifdef HAVE_LIMITS_H
#include <limits.h>
#endif

#define LZMADEC_NO_STDIO
#include "lzmadec.h"
#undef LZMADEC_NO_STDIO

#include "private.h"

#ifndef SIZE_MAX
#define SIZE_MAX (~(size_t)0)
#endif

#define LZMADEC_BUFSIZE (LZMA_IN_BUFFER_SIZE - LZMA_REQUIRED_IN_BUFFER_SIZE)

#define LZMADEC_IO_STATUS_OK	0
#define LZMADEC_IO_STATUS_EOF	1
#define LZMADEC_IO_STATUS_ERROR	2

typedef struct {
	lzmadec_stream strm;
	FILE *file;
	uint8_t buffer[LZMADEC_BUFSIZE];
	int_fast8_t status;
} lzmadec_FILE;


/****************************
  Opening and closing a file
 ****************************/

/* This is used by lzmadec_open() and lzmadec_dopen(). */
static lzmadec_FILE *
lzmadec_open_init (lzmadec_FILE *lfile)
{
	/* Check if the file was opened successfully */
	if (lfile->file == NULL) {
		int saved_errno = errno;
		free (lfile);
		errno = saved_errno;
		return NULL; /* Caller can read errno */
	}
	/* Initialize the decoder */
	lfile->strm.lzma_alloc = NULL;
	lfile->strm.lzma_free = NULL;
	lfile->strm.opaque = NULL;
	lfile->strm.avail_in = 0;
	lfile->strm.avail_out = 0;
	if (lzmadec_init (&lfile->strm) != LZMADEC_OK) {
		fclose (lfile->file);
		free (lfile);
		/* Set errno like fopen(2) (and malloc(3)) would set it: */
		errno = ENOMEM;
		return NULL; /* Caller can see faked malloc()'s errno */
	}
	/* Not yet at the end of the stream. */
	lfile->status = LZMADEC_IO_STATUS_OK;
	return lfile;
}

extern lzmadec_FILE *
lzmadec_open (const char *path)
{
	/* Allocate memory for the lzmadec_FILE */
	lzmadec_FILE *lfile = malloc (sizeof (lzmadec_FILE));
	if (lfile == NULL)
		return NULL;
	/* Open the file */
	lfile->file = fopen (path, "rb");
	/* The rest is shared with lzmadec_open() */
	return lzmadec_open_init (lfile);
}

extern lzmadec_FILE *
lzmadec_dopen (int fd)
{
	/* Allocate memory for the lzmadec_FILE */
	lzmadec_FILE *lfile = malloc (sizeof (lzmadec_FILE));
	if (lfile == NULL)
		return NULL;
	/* Open the file */
	lfile->file = fdopen (fd, "rb");
	/* The rest is shared with lzmadec_open() */
	return lzmadec_open_init (lfile);
}

extern int_fast8_t
lzmadec_close (lzmadec_FILE *lfile)
{
	/* Simple check that lfile looks like a valid lzmadec_FILE. */
	if (lfile == NULL || lfile->strm.state == NULL)
		return -1;
	lzmadec_end (&lfile->strm);
	fclose (lfile->file);
	lfile->file = NULL;
	free (lfile);
	return 0;
}


/****************
  Reading a file
 ****************/

extern ssize_t
lzmadec_read (lzmadec_FILE *lfile, uint8_t *buf, const size_t len)
{
	int_fast8_t ret;
	/* Simple check that lfile looks like a valid lzmadec_FILE. */
	if (lfile == NULL || lfile->strm.state == NULL)
		return -1;
	/* Check status */
	if (lfile->status == LZMADEC_IO_STATUS_ERROR)
		return -1;
	if (lfile->status == LZMADEC_IO_STATUS_EOF)
		return 0;
	/* The return value is ssize_t so we limit the maximum read size. */
	lfile->strm.avail_out = MIN (len, SIZE_MAX / 2 - 1);
	lfile->strm.next_out = buf;
	do {
		if (lfile->strm.avail_in == 0) {
			lfile->strm.next_in = lfile->buffer;
			lfile->strm.avail_in = fread (lfile->buffer,
					sizeof (uint8_t), LZMADEC_BUFSIZE,
					lfile->file);
		}
		ret = lzmadec_decode (&lfile->strm, lfile->strm.avail_in == 0);
	} while (lfile->strm.avail_out != 0 && ret == LZMADEC_OK);
	if (ret == LZMADEC_STREAM_END)
		lfile->status = LZMADEC_IO_STATUS_EOF;
	if (ret < 0)
		return -1; /* FIXME: errno? */
	return (len - lfile->strm.avail_out);
}

/* Read until '\n' or '\0' or at maximum of len bytes.
   Slow implementation, similar to what is in zlib. */
extern uint8_t *
lzmadec_gets (lzmadec_FILE *lfile, uint8_t *buf, size_t len)
{
	int_fast8_t ret;
	uint8_t *buf_start = buf;
	/* Sanity checks */
	if (buf == NULL || len < 1)
		return NULL;
	if (lfile == NULL || lfile->strm.state == NULL)
		return NULL;
	/* Read byte by byte (sloooow) and stop when 1) buf is full
	   2) end of file 3) '\n' or '\0' is found. */
	while (--len > 0) {
		ret = lzmadec_read (lfile, buf, 1);
		if (ret != 1) {
			/* Error checking: 1) decoding error or 2) end of file
			   and no characters were read. */
			if (ret < 0 || buf == buf_start)
				return NULL;
			break;
		}
		if (*buf == '\0')
			return buf_start;
		if (*buf++ == '\n')
			break;
	}
	*buf = '\0';
	return buf_start;
}

extern int
lzmadec_getc (lzmadec_FILE *lfile)
{
	uint8_t c;
	if (lzmadec_read (lfile, &c, 1) == 0)
		return -1;
	return (int)(c);
}


/*******
  Other
 *******/

extern off_t
lzmadec_tell (lzmadec_FILE *lfile)
{
	/* Simple check that lfile looks like a valid lzmadec_FILE. */
	if (lfile == NULL || lfile->strm.state == NULL)
		return -1;
	return (off_t)(lfile->strm.total_out);
}

extern int_fast8_t
lzmadec_eof (lzmadec_FILE *lfile)
{
	/* Simple check that lfile looks like a valid lzmadec_FILE. */
	if (lfile == NULL || lfile->strm.state == NULL)
		return -1;
	return lfile->status == LZMADEC_IO_STATUS_EOF;
}

extern int_fast8_t
lzmadec_rewind (lzmadec_FILE *lfile)
{
	/* Simple check that lfile looks like a valid lzmadec_FILE. */
	if (lfile == NULL || lfile->strm.state == NULL)
		return -1;
	/* Rewinding is done by closing the old lzmadec_stream
	   and reinitializing it. */
	if (lzmadec_end (&lfile->strm) != LZMADEC_OK) {
		lfile->status = LZMADEC_IO_STATUS_ERROR;
		return -1;
	}
	rewind (lfile->file);
	if (lzmadec_init (&lfile->strm) != LZMADEC_OK) {
		lfile->status = LZMADEC_IO_STATUS_ERROR;
		return -1;
	}
	lfile->status = LZMADEC_IO_STATUS_OK;
	return 0;
}

extern off_t
lzmadec_seek (lzmadec_FILE *lfile, off_t offset, int whence)
{
	off_t oldpos = (off_t)(lfile->strm.total_out);
	off_t newpos;
	/* Simple check that lfile looks like a valid lzmadec_FILE. */
	if (lfile == NULL || lfile->strm.state == NULL)
		return -1;
	/* Get the new absolute position. */
	switch (whence) {
		case SEEK_SET:
			/* Absolute position must be >= 0. */
			if (offset < 0)
				return -1;
			newpos = offset;
			break;
		case SEEK_CUR:
			/* Need to be careful to avoid integer overflows. */
			if ((offset < 0 && (off_t)(-1 * offset) > oldpos)
					||
					(offset > 0 && (off_t)(offset) + oldpos
					< oldpos))
				return (off_t)(-1);
			newpos = (off_t)(lfile->strm.total_out) + offset;
			break;
		case SEEK_END:
			/* zlib doesn't support SEEK_END. However, liblzmadec
			   provides this as a way to find out uncompressed
			   size of a streamed file (streamed files don't have
			   uncompressed size in their header). */
			newpos = -1;
			break;
		default:
			/* Invalid whence */
			errno = EINVAL;
			return -1;
	}
	/* Seeking with a valid whence value always clears
	   the end of file indicator. */
	lfile->status = LZMADEC_IO_STATUS_OK;
	/* If the new absolute position is backward from current position,
	   we need to rewind and uncompress from the beginning of the file.
	   This is usually slow and thus not recommended. */
	if (whence != SEEK_END && newpos < oldpos) {
		if (lzmadec_rewind (lfile))
			return -1;
		oldpos = 0;
		assert (lfile->strm.total_out == 0);
	}
	/* Maybe we are lucky and don't need to seek at all. ;-) */
	if (newpos == oldpos)
		return oldpos;
	assert (newpos > oldpos || newpos == -1);
	/* Read as many bytes as needed to reach the requested position. */
	{
		/* strm.next_out cannot be NULL so use a temporary buffer. */
		uint8_t buf[LZMADEC_BUFSIZE];
		size_t req_size;
		ssize_t got_size;
		while (newpos > oldpos || newpos == -1) {
			req_size = MIN (LZMADEC_BUFSIZE, newpos - oldpos);
			got_size = lzmadec_read (lfile, buf, req_size);
			if (got_size != (ssize_t)(req_size)) {
				if (got_size < 0) {
					return -1; /* Stream error */
				} else {
					/* End of stream */
					newpos = oldpos + got_size;
					break;
				}
			}
			oldpos += got_size;
		};
	}
	assert (newpos == oldpos);
	assert ((off_t)(lfile->strm.total_out) == newpos);
	return newpos;
}

#endif /* ifndef LZMADEC_NO_STDIO */