1///////////////////////////////////////////////////////////////////////////////
2//
3/// \file       common.h
4/// \brief      Common functions needed in many places in liblzma
5//
6//  Author:     Lasse Collin
7//
8//  This file has been put into the public domain.
9//  You can do whatever you want with this file.
10//
11///////////////////////////////////////////////////////////////////////////////
12
13#include "common.h"
14
15
16/////////////
17// Version //
18/////////////
19
20extern LZMA_API(uint32_t)
21lzma_version_number(void)
22{
23	return LZMA_VERSION;
24}
25
26
27extern LZMA_API(const char *)
28lzma_version_string(void)
29{
30	return LZMA_VERSION_STRING;
31}
32
33
34///////////////////////
35// Memory allocation //
36///////////////////////
37
38extern void * lzma_attribute((__malloc__)) lzma_attr_alloc_size(1)
39lzma_alloc(size_t size, lzma_allocator *allocator)
40{
41	// Some malloc() variants return NULL if called with size == 0.
42	if (size == 0)
43		size = 1;
44
45	void *ptr;
46
47	if (allocator != NULL && allocator->alloc != NULL)
48		ptr = allocator->alloc(allocator->opaque, 1, size);
49	else
50		ptr = malloc(size);
51
52	return ptr;
53}
54
55
56extern void
57lzma_free(void *ptr, lzma_allocator *allocator)
58{
59	if (allocator != NULL && allocator->free != NULL)
60		allocator->free(allocator->opaque, ptr);
61	else
62		free(ptr);
63
64	return;
65}
66
67
68//////////
69// Misc //
70//////////
71
72extern size_t
73lzma_bufcpy(const uint8_t *restrict in, size_t *restrict in_pos,
74		size_t in_size, uint8_t *restrict out,
75		size_t *restrict out_pos, size_t out_size)
76{
77	const size_t in_avail = in_size - *in_pos;
78	const size_t out_avail = out_size - *out_pos;
79	const size_t copy_size = my_min(in_avail, out_avail);
80
81	memcpy(out + *out_pos, in + *in_pos, copy_size);
82
83	*in_pos += copy_size;
84	*out_pos += copy_size;
85
86	return copy_size;
87}
88
89
90extern lzma_ret
91lzma_next_filter_init(lzma_next_coder *next, lzma_allocator *allocator,
92		const lzma_filter_info *filters)
93{
94	lzma_next_coder_init(filters[0].init, next, allocator);
95	next->id = filters[0].id;
96	return filters[0].init == NULL
97			? LZMA_OK : filters[0].init(next, allocator, filters);
98}
99
100
101extern lzma_ret
102lzma_next_filter_update(lzma_next_coder *next, lzma_allocator *allocator,
103		const lzma_filter *reversed_filters)
104{
105	// Check that the application isn't trying to change the Filter ID.
106	// End of filters is indicated with LZMA_VLI_UNKNOWN in both
107	// reversed_filters[0].id and next->id.
108	if (reversed_filters[0].id != next->id)
109		return LZMA_PROG_ERROR;
110
111	if (reversed_filters[0].id == LZMA_VLI_UNKNOWN)
112		return LZMA_OK;
113
114	assert(next->update != NULL);
115	return next->update(next->coder, allocator, NULL, reversed_filters);
116}
117
118
119extern void
120lzma_next_end(lzma_next_coder *next, lzma_allocator *allocator)
121{
122	if (next->init != (uintptr_t)(NULL)) {
123		// To avoid tiny end functions that simply call
124		// lzma_free(coder, allocator), we allow leaving next->end
125		// NULL and call lzma_free() here.
126		if (next->end != NULL)
127			next->end(next->coder, allocator);
128		else
129			lzma_free(next->coder, allocator);
130
131		// Reset the variables so the we don't accidentally think
132		// that it is an already initialized coder.
133		*next = LZMA_NEXT_CODER_INIT;
134	}
135
136	return;
137}
138
139
140//////////////////////////////////////
141// External to internal API wrapper //
142//////////////////////////////////////
143
144extern lzma_ret
145lzma_strm_init(lzma_stream *strm)
146{
147	if (strm == NULL)
148		return LZMA_PROG_ERROR;
149
150	if (strm->internal == NULL) {
151		strm->internal = lzma_alloc(sizeof(lzma_internal),
152				strm->allocator);
153		if (strm->internal == NULL)
154			return LZMA_MEM_ERROR;
155
156		strm->internal->next = LZMA_NEXT_CODER_INIT;
157	}
158
159	strm->internal->supported_actions[LZMA_RUN] = false;
160	strm->internal->supported_actions[LZMA_SYNC_FLUSH] = false;
161	strm->internal->supported_actions[LZMA_FULL_FLUSH] = false;
162	strm->internal->supported_actions[LZMA_FINISH] = false;
163	strm->internal->sequence = ISEQ_RUN;
164	strm->internal->allow_buf_error = false;
165
166	strm->total_in = 0;
167	strm->total_out = 0;
168
169	return LZMA_OK;
170}
171
172
173extern LZMA_API(lzma_ret)
174lzma_code(lzma_stream *strm, lzma_action action)
175{
176	// Sanity checks
177	if ((strm->next_in == NULL && strm->avail_in != 0)
178			|| (strm->next_out == NULL && strm->avail_out != 0)
179			|| strm->internal == NULL
180			|| strm->internal->next.code == NULL
181			|| (unsigned int)(action) > LZMA_FINISH
182			|| !strm->internal->supported_actions[action])
183		return LZMA_PROG_ERROR;
184
185	// Check if unsupported members have been set to non-zero or non-NULL,
186	// which would indicate that some new feature is wanted.
187	if (strm->reserved_ptr1 != NULL
188			|| strm->reserved_ptr2 != NULL
189			|| strm->reserved_ptr3 != NULL
190			|| strm->reserved_ptr4 != NULL
191			|| strm->reserved_int1 != 0
192			|| strm->reserved_int2 != 0
193			|| strm->reserved_int3 != 0
194			|| strm->reserved_int4 != 0
195			|| strm->reserved_enum1 != LZMA_RESERVED_ENUM
196			|| strm->reserved_enum2 != LZMA_RESERVED_ENUM)
197		return LZMA_OPTIONS_ERROR;
198
199	switch (strm->internal->sequence) {
200	case ISEQ_RUN:
201		switch (action) {
202		case LZMA_RUN:
203			break;
204
205		case LZMA_SYNC_FLUSH:
206			strm->internal->sequence = ISEQ_SYNC_FLUSH;
207			break;
208
209		case LZMA_FULL_FLUSH:
210			strm->internal->sequence = ISEQ_FULL_FLUSH;
211			break;
212
213		case LZMA_FINISH:
214			strm->internal->sequence = ISEQ_FINISH;
215			break;
216		}
217
218		break;
219
220	case ISEQ_SYNC_FLUSH:
221		// The same action must be used until we return
222		// LZMA_STREAM_END, and the amount of input must not change.
223		if (action != LZMA_SYNC_FLUSH
224				|| strm->internal->avail_in != strm->avail_in)
225			return LZMA_PROG_ERROR;
226
227		break;
228
229	case ISEQ_FULL_FLUSH:
230		if (action != LZMA_FULL_FLUSH
231				|| strm->internal->avail_in != strm->avail_in)
232			return LZMA_PROG_ERROR;
233
234		break;
235
236	case ISEQ_FINISH:
237		if (action != LZMA_FINISH
238				|| strm->internal->avail_in != strm->avail_in)
239			return LZMA_PROG_ERROR;
240
241		break;
242
243	case ISEQ_END:
244		return LZMA_STREAM_END;
245
246	case ISEQ_ERROR:
247	default:
248		return LZMA_PROG_ERROR;
249	}
250
251	size_t in_pos = 0;
252	size_t out_pos = 0;
253	lzma_ret ret = strm->internal->next.code(
254			strm->internal->next.coder, strm->allocator,
255			strm->next_in, &in_pos, strm->avail_in,
256			strm->next_out, &out_pos, strm->avail_out, action);
257
258	strm->next_in += in_pos;
259	strm->avail_in -= in_pos;
260	strm->total_in += in_pos;
261
262	strm->next_out += out_pos;
263	strm->avail_out -= out_pos;
264	strm->total_out += out_pos;
265
266	strm->internal->avail_in = strm->avail_in;
267
268	switch (ret) {
269	case LZMA_OK:
270		// Don't return LZMA_BUF_ERROR when it happens the first time.
271		// This is to avoid returning LZMA_BUF_ERROR when avail_out
272		// was zero but still there was no more data left to written
273		// to next_out.
274		if (out_pos == 0 && in_pos == 0) {
275			if (strm->internal->allow_buf_error)
276				ret = LZMA_BUF_ERROR;
277			else
278				strm->internal->allow_buf_error = true;
279		} else {
280			strm->internal->allow_buf_error = false;
281		}
282		break;
283
284	case LZMA_STREAM_END:
285		if (strm->internal->sequence == ISEQ_SYNC_FLUSH
286				|| strm->internal->sequence == ISEQ_FULL_FLUSH)
287			strm->internal->sequence = ISEQ_RUN;
288		else
289			strm->internal->sequence = ISEQ_END;
290
291	// Fall through
292
293	case LZMA_NO_CHECK:
294	case LZMA_UNSUPPORTED_CHECK:
295	case LZMA_GET_CHECK:
296	case LZMA_MEMLIMIT_ERROR:
297		// Something else than LZMA_OK, but not a fatal error,
298		// that is, coding may be continued (except if ISEQ_END).
299		strm->internal->allow_buf_error = false;
300		break;
301
302	default:
303		// All the other errors are fatal; coding cannot be continued.
304		assert(ret != LZMA_BUF_ERROR);
305		strm->internal->sequence = ISEQ_ERROR;
306		break;
307	}
308
309	return ret;
310}
311
312
313extern LZMA_API(void)
314lzma_end(lzma_stream *strm)
315{
316	if (strm != NULL && strm->internal != NULL) {
317		lzma_next_end(&strm->internal->next, strm->allocator);
318		lzma_free(strm->internal, strm->allocator);
319		strm->internal = NULL;
320	}
321
322	return;
323}
324
325
326extern LZMA_API(lzma_check)
327lzma_get_check(const lzma_stream *strm)
328{
329	// Return LZMA_CHECK_NONE if we cannot know the check type.
330	// It's a bug in the application if this happens.
331	if (strm->internal->next.get_check == NULL)
332		return LZMA_CHECK_NONE;
333
334	return strm->internal->next.get_check(strm->internal->next.coder);
335}
336
337
338extern LZMA_API(uint64_t)
339lzma_memusage(const lzma_stream *strm)
340{
341	uint64_t memusage;
342	uint64_t old_memlimit;
343
344	if (strm == NULL || strm->internal == NULL
345			|| strm->internal->next.memconfig == NULL
346			|| strm->internal->next.memconfig(
347				strm->internal->next.coder,
348				&memusage, &old_memlimit, 0) != LZMA_OK)
349		return 0;
350
351	return memusage;
352}
353
354
355extern LZMA_API(uint64_t)
356lzma_memlimit_get(const lzma_stream *strm)
357{
358	uint64_t old_memlimit;
359	uint64_t memusage;
360
361	if (strm == NULL || strm->internal == NULL
362			|| strm->internal->next.memconfig == NULL
363			|| strm->internal->next.memconfig(
364				strm->internal->next.coder,
365				&memusage, &old_memlimit, 0) != LZMA_OK)
366		return 0;
367
368	return old_memlimit;
369}
370
371
372extern LZMA_API(lzma_ret)
373lzma_memlimit_set(lzma_stream *strm, uint64_t new_memlimit)
374{
375	// Dummy variables to simplify memconfig functions
376	uint64_t old_memlimit;
377	uint64_t memusage;
378
379	if (strm == NULL || strm->internal == NULL
380			|| strm->internal->next.memconfig == NULL)
381		return LZMA_PROG_ERROR;
382
383	if (new_memlimit != 0 && new_memlimit < LZMA_MEMUSAGE_BASE)
384		return LZMA_MEMLIMIT_ERROR;
385
386	return strm->internal->next.memconfig(strm->internal->next.coder,
387			&memusage, &old_memlimit, new_memlimit);
388}
389