xref: /dflybsd-src/contrib/xz/src/liblzma/common/common.c (revision b5feb3da7c498482b19d14ac6f2b1901005f7d94)
12940b44dSPeter Avalos ///////////////////////////////////////////////////////////////////////////////
22940b44dSPeter Avalos //
3*e151908bSDaniel Fojt /// \file       common.c
42940b44dSPeter Avalos /// \brief      Common functions needed in many places in liblzma
52940b44dSPeter Avalos //
62940b44dSPeter Avalos //  Author:     Lasse Collin
72940b44dSPeter Avalos //
82940b44dSPeter Avalos //  This file has been put into the public domain.
92940b44dSPeter Avalos //  You can do whatever you want with this file.
102940b44dSPeter Avalos //
112940b44dSPeter Avalos ///////////////////////////////////////////////////////////////////////////////
122940b44dSPeter Avalos 
132940b44dSPeter Avalos #include "common.h"
142940b44dSPeter Avalos 
152940b44dSPeter Avalos 
162940b44dSPeter Avalos /////////////
172940b44dSPeter Avalos // Version //
182940b44dSPeter Avalos /////////////
192940b44dSPeter Avalos 
202940b44dSPeter Avalos extern LZMA_API(uint32_t)
lzma_version_number(void)212940b44dSPeter Avalos lzma_version_number(void)
222940b44dSPeter Avalos {
232940b44dSPeter Avalos 	return LZMA_VERSION;
242940b44dSPeter Avalos }
252940b44dSPeter Avalos 
262940b44dSPeter Avalos 
272940b44dSPeter Avalos extern LZMA_API(const char *)
lzma_version_string(void)282940b44dSPeter Avalos lzma_version_string(void)
292940b44dSPeter Avalos {
302940b44dSPeter Avalos 	return LZMA_VERSION_STRING;
312940b44dSPeter Avalos }
322940b44dSPeter Avalos 
332940b44dSPeter Avalos 
342940b44dSPeter Avalos ///////////////////////
352940b44dSPeter Avalos // Memory allocation //
362940b44dSPeter Avalos ///////////////////////
372940b44dSPeter Avalos 
38114db65bSPeter Avalos extern void * lzma_attribute((__malloc__)) lzma_attr_alloc_size(1)
lzma_alloc(size_t size,const lzma_allocator * allocator)3915ab8c86SJohn Marino lzma_alloc(size_t size, const lzma_allocator *allocator)
402940b44dSPeter Avalos {
412940b44dSPeter Avalos 	// Some malloc() variants return NULL if called with size == 0.
422940b44dSPeter Avalos 	if (size == 0)
432940b44dSPeter Avalos 		size = 1;
442940b44dSPeter Avalos 
452940b44dSPeter Avalos 	void *ptr;
462940b44dSPeter Avalos 
472940b44dSPeter Avalos 	if (allocator != NULL && allocator->alloc != NULL)
482940b44dSPeter Avalos 		ptr = allocator->alloc(allocator->opaque, 1, size);
492940b44dSPeter Avalos 	else
502940b44dSPeter Avalos 		ptr = malloc(size);
512940b44dSPeter Avalos 
522940b44dSPeter Avalos 	return ptr;
532940b44dSPeter Avalos }
542940b44dSPeter Avalos 
552940b44dSPeter Avalos 
5615ab8c86SJohn Marino extern void * lzma_attribute((__malloc__)) lzma_attr_alloc_size(1)
lzma_alloc_zero(size_t size,const lzma_allocator * allocator)5715ab8c86SJohn Marino lzma_alloc_zero(size_t size, const lzma_allocator *allocator)
5815ab8c86SJohn Marino {
5915ab8c86SJohn Marino 	// Some calloc() variants return NULL if called with size == 0.
6015ab8c86SJohn Marino 	if (size == 0)
6115ab8c86SJohn Marino 		size = 1;
6215ab8c86SJohn Marino 
6315ab8c86SJohn Marino 	void *ptr;
6415ab8c86SJohn Marino 
6515ab8c86SJohn Marino 	if (allocator != NULL && allocator->alloc != NULL) {
6615ab8c86SJohn Marino 		ptr = allocator->alloc(allocator->opaque, 1, size);
6715ab8c86SJohn Marino 		if (ptr != NULL)
6815ab8c86SJohn Marino 			memzero(ptr, size);
6915ab8c86SJohn Marino 	} else {
7015ab8c86SJohn Marino 		ptr = calloc(1, size);
7115ab8c86SJohn Marino 	}
7215ab8c86SJohn Marino 
7315ab8c86SJohn Marino 	return ptr;
7415ab8c86SJohn Marino }
7515ab8c86SJohn Marino 
7615ab8c86SJohn Marino 
772940b44dSPeter Avalos extern void
lzma_free(void * ptr,const lzma_allocator * allocator)7815ab8c86SJohn Marino lzma_free(void *ptr, const lzma_allocator *allocator)
792940b44dSPeter Avalos {
802940b44dSPeter Avalos 	if (allocator != NULL && allocator->free != NULL)
812940b44dSPeter Avalos 		allocator->free(allocator->opaque, ptr);
822940b44dSPeter Avalos 	else
832940b44dSPeter Avalos 		free(ptr);
842940b44dSPeter Avalos 
852940b44dSPeter Avalos 	return;
862940b44dSPeter Avalos }
872940b44dSPeter Avalos 
882940b44dSPeter Avalos 
892940b44dSPeter Avalos //////////
902940b44dSPeter Avalos // Misc //
912940b44dSPeter Avalos //////////
922940b44dSPeter Avalos 
932940b44dSPeter Avalos extern size_t
lzma_bufcpy(const uint8_t * restrict in,size_t * restrict in_pos,size_t in_size,uint8_t * restrict out,size_t * restrict out_pos,size_t out_size)942940b44dSPeter Avalos lzma_bufcpy(const uint8_t *restrict in, size_t *restrict in_pos,
952940b44dSPeter Avalos 		size_t in_size, uint8_t *restrict out,
962940b44dSPeter Avalos 		size_t *restrict out_pos, size_t out_size)
972940b44dSPeter Avalos {
982940b44dSPeter Avalos 	const size_t in_avail = in_size - *in_pos;
992940b44dSPeter Avalos 	const size_t out_avail = out_size - *out_pos;
1002940b44dSPeter Avalos 	const size_t copy_size = my_min(in_avail, out_avail);
1012940b44dSPeter Avalos 
102*e151908bSDaniel Fojt 	// Call memcpy() only if there is something to copy. If there is
103*e151908bSDaniel Fojt 	// nothing to copy, in or out might be NULL and then the memcpy()
104*e151908bSDaniel Fojt 	// call would trigger undefined behavior.
105*e151908bSDaniel Fojt 	if (copy_size > 0)
1062940b44dSPeter Avalos 		memcpy(out + *out_pos, in + *in_pos, copy_size);
1072940b44dSPeter Avalos 
1082940b44dSPeter Avalos 	*in_pos += copy_size;
1092940b44dSPeter Avalos 	*out_pos += copy_size;
1102940b44dSPeter Avalos 
1112940b44dSPeter Avalos 	return copy_size;
1122940b44dSPeter Avalos }
1132940b44dSPeter Avalos 
1142940b44dSPeter Avalos 
1152940b44dSPeter Avalos extern lzma_ret
lzma_next_filter_init(lzma_next_coder * next,const lzma_allocator * allocator,const lzma_filter_info * filters)11615ab8c86SJohn Marino lzma_next_filter_init(lzma_next_coder *next, const lzma_allocator *allocator,
1172940b44dSPeter Avalos 		const lzma_filter_info *filters)
1182940b44dSPeter Avalos {
1192940b44dSPeter Avalos 	lzma_next_coder_init(filters[0].init, next, allocator);
1202940b44dSPeter Avalos 	next->id = filters[0].id;
1212940b44dSPeter Avalos 	return filters[0].init == NULL
1222940b44dSPeter Avalos 			? LZMA_OK : filters[0].init(next, allocator, filters);
1232940b44dSPeter Avalos }
1242940b44dSPeter Avalos 
1252940b44dSPeter Avalos 
1262940b44dSPeter Avalos extern lzma_ret
lzma_next_filter_update(lzma_next_coder * next,const lzma_allocator * allocator,const lzma_filter * reversed_filters)12715ab8c86SJohn Marino lzma_next_filter_update(lzma_next_coder *next, const lzma_allocator *allocator,
1282940b44dSPeter Avalos 		const lzma_filter *reversed_filters)
1292940b44dSPeter Avalos {
1302940b44dSPeter Avalos 	// Check that the application isn't trying to change the Filter ID.
1312940b44dSPeter Avalos 	// End of filters is indicated with LZMA_VLI_UNKNOWN in both
1322940b44dSPeter Avalos 	// reversed_filters[0].id and next->id.
1332940b44dSPeter Avalos 	if (reversed_filters[0].id != next->id)
1342940b44dSPeter Avalos 		return LZMA_PROG_ERROR;
1352940b44dSPeter Avalos 
1362940b44dSPeter Avalos 	if (reversed_filters[0].id == LZMA_VLI_UNKNOWN)
1372940b44dSPeter Avalos 		return LZMA_OK;
1382940b44dSPeter Avalos 
1392940b44dSPeter Avalos 	assert(next->update != NULL);
1402940b44dSPeter Avalos 	return next->update(next->coder, allocator, NULL, reversed_filters);
1412940b44dSPeter Avalos }
1422940b44dSPeter Avalos 
1432940b44dSPeter Avalos 
1442940b44dSPeter Avalos extern void
lzma_next_end(lzma_next_coder * next,const lzma_allocator * allocator)14515ab8c86SJohn Marino lzma_next_end(lzma_next_coder *next, const lzma_allocator *allocator)
1462940b44dSPeter Avalos {
1472940b44dSPeter Avalos 	if (next->init != (uintptr_t)(NULL)) {
1482940b44dSPeter Avalos 		// To avoid tiny end functions that simply call
1492940b44dSPeter Avalos 		// lzma_free(coder, allocator), we allow leaving next->end
1502940b44dSPeter Avalos 		// NULL and call lzma_free() here.
1512940b44dSPeter Avalos 		if (next->end != NULL)
1522940b44dSPeter Avalos 			next->end(next->coder, allocator);
1532940b44dSPeter Avalos 		else
1542940b44dSPeter Avalos 			lzma_free(next->coder, allocator);
1552940b44dSPeter Avalos 
1562940b44dSPeter Avalos 		// Reset the variables so the we don't accidentally think
1572940b44dSPeter Avalos 		// that it is an already initialized coder.
1582940b44dSPeter Avalos 		*next = LZMA_NEXT_CODER_INIT;
1592940b44dSPeter Avalos 	}
1602940b44dSPeter Avalos 
1612940b44dSPeter Avalos 	return;
1622940b44dSPeter Avalos }
1632940b44dSPeter Avalos 
1642940b44dSPeter Avalos 
1652940b44dSPeter Avalos //////////////////////////////////////
1662940b44dSPeter Avalos // External to internal API wrapper //
1672940b44dSPeter Avalos //////////////////////////////////////
1682940b44dSPeter Avalos 
1692940b44dSPeter Avalos extern lzma_ret
lzma_strm_init(lzma_stream * strm)1702940b44dSPeter Avalos lzma_strm_init(lzma_stream *strm)
1712940b44dSPeter Avalos {
1722940b44dSPeter Avalos 	if (strm == NULL)
1732940b44dSPeter Avalos 		return LZMA_PROG_ERROR;
1742940b44dSPeter Avalos 
1752940b44dSPeter Avalos 	if (strm->internal == NULL) {
1762940b44dSPeter Avalos 		strm->internal = lzma_alloc(sizeof(lzma_internal),
1772940b44dSPeter Avalos 				strm->allocator);
1782940b44dSPeter Avalos 		if (strm->internal == NULL)
1792940b44dSPeter Avalos 			return LZMA_MEM_ERROR;
1802940b44dSPeter Avalos 
1812940b44dSPeter Avalos 		strm->internal->next = LZMA_NEXT_CODER_INIT;
1822940b44dSPeter Avalos 	}
1832940b44dSPeter Avalos 
18415ab8c86SJohn Marino 	memzero(strm->internal->supported_actions,
18515ab8c86SJohn Marino 			sizeof(strm->internal->supported_actions));
1862940b44dSPeter Avalos 	strm->internal->sequence = ISEQ_RUN;
1872940b44dSPeter Avalos 	strm->internal->allow_buf_error = false;
1882940b44dSPeter Avalos 
1892940b44dSPeter Avalos 	strm->total_in = 0;
1902940b44dSPeter Avalos 	strm->total_out = 0;
1912940b44dSPeter Avalos 
1922940b44dSPeter Avalos 	return LZMA_OK;
1932940b44dSPeter Avalos }
1942940b44dSPeter Avalos 
1952940b44dSPeter Avalos 
1962940b44dSPeter Avalos extern LZMA_API(lzma_ret)
lzma_code(lzma_stream * strm,lzma_action action)1972940b44dSPeter Avalos lzma_code(lzma_stream *strm, lzma_action action)
1982940b44dSPeter Avalos {
1992940b44dSPeter Avalos 	// Sanity checks
2002940b44dSPeter Avalos 	if ((strm->next_in == NULL && strm->avail_in != 0)
2012940b44dSPeter Avalos 			|| (strm->next_out == NULL && strm->avail_out != 0)
2022940b44dSPeter Avalos 			|| strm->internal == NULL
2032940b44dSPeter Avalos 			|| strm->internal->next.code == NULL
20415ab8c86SJohn Marino 			|| (unsigned int)(action) > LZMA_ACTION_MAX
2052940b44dSPeter Avalos 			|| !strm->internal->supported_actions[action])
2062940b44dSPeter Avalos 		return LZMA_PROG_ERROR;
2072940b44dSPeter Avalos 
2082940b44dSPeter Avalos 	// Check if unsupported members have been set to non-zero or non-NULL,
2092940b44dSPeter Avalos 	// which would indicate that some new feature is wanted.
2102940b44dSPeter Avalos 	if (strm->reserved_ptr1 != NULL
2112940b44dSPeter Avalos 			|| strm->reserved_ptr2 != NULL
2122940b44dSPeter Avalos 			|| strm->reserved_ptr3 != NULL
2132940b44dSPeter Avalos 			|| strm->reserved_ptr4 != NULL
2142940b44dSPeter Avalos 			|| strm->reserved_int1 != 0
2152940b44dSPeter Avalos 			|| strm->reserved_int2 != 0
2162940b44dSPeter Avalos 			|| strm->reserved_int3 != 0
2172940b44dSPeter Avalos 			|| strm->reserved_int4 != 0
2182940b44dSPeter Avalos 			|| strm->reserved_enum1 != LZMA_RESERVED_ENUM
2192940b44dSPeter Avalos 			|| strm->reserved_enum2 != LZMA_RESERVED_ENUM)
2202940b44dSPeter Avalos 		return LZMA_OPTIONS_ERROR;
2212940b44dSPeter Avalos 
2222940b44dSPeter Avalos 	switch (strm->internal->sequence) {
2232940b44dSPeter Avalos 	case ISEQ_RUN:
2242940b44dSPeter Avalos 		switch (action) {
2252940b44dSPeter Avalos 		case LZMA_RUN:
2262940b44dSPeter Avalos 			break;
2272940b44dSPeter Avalos 
2282940b44dSPeter Avalos 		case LZMA_SYNC_FLUSH:
2292940b44dSPeter Avalos 			strm->internal->sequence = ISEQ_SYNC_FLUSH;
2302940b44dSPeter Avalos 			break;
2312940b44dSPeter Avalos 
2322940b44dSPeter Avalos 		case LZMA_FULL_FLUSH:
2332940b44dSPeter Avalos 			strm->internal->sequence = ISEQ_FULL_FLUSH;
2342940b44dSPeter Avalos 			break;
2352940b44dSPeter Avalos 
2362940b44dSPeter Avalos 		case LZMA_FINISH:
2372940b44dSPeter Avalos 			strm->internal->sequence = ISEQ_FINISH;
2382940b44dSPeter Avalos 			break;
23915ab8c86SJohn Marino 
24015ab8c86SJohn Marino 		case LZMA_FULL_BARRIER:
24115ab8c86SJohn Marino 			strm->internal->sequence = ISEQ_FULL_BARRIER;
24215ab8c86SJohn Marino 			break;
2432940b44dSPeter Avalos 		}
2442940b44dSPeter Avalos 
2452940b44dSPeter Avalos 		break;
2462940b44dSPeter Avalos 
2472940b44dSPeter Avalos 	case ISEQ_SYNC_FLUSH:
2482940b44dSPeter Avalos 		// The same action must be used until we return
2492940b44dSPeter Avalos 		// LZMA_STREAM_END, and the amount of input must not change.
2502940b44dSPeter Avalos 		if (action != LZMA_SYNC_FLUSH
2512940b44dSPeter Avalos 				|| strm->internal->avail_in != strm->avail_in)
2522940b44dSPeter Avalos 			return LZMA_PROG_ERROR;
2532940b44dSPeter Avalos 
2542940b44dSPeter Avalos 		break;
2552940b44dSPeter Avalos 
2562940b44dSPeter Avalos 	case ISEQ_FULL_FLUSH:
2572940b44dSPeter Avalos 		if (action != LZMA_FULL_FLUSH
2582940b44dSPeter Avalos 				|| strm->internal->avail_in != strm->avail_in)
2592940b44dSPeter Avalos 			return LZMA_PROG_ERROR;
2602940b44dSPeter Avalos 
2612940b44dSPeter Avalos 		break;
2622940b44dSPeter Avalos 
2632940b44dSPeter Avalos 	case ISEQ_FINISH:
2642940b44dSPeter Avalos 		if (action != LZMA_FINISH
2652940b44dSPeter Avalos 				|| strm->internal->avail_in != strm->avail_in)
2662940b44dSPeter Avalos 			return LZMA_PROG_ERROR;
2672940b44dSPeter Avalos 
2682940b44dSPeter Avalos 		break;
2692940b44dSPeter Avalos 
27015ab8c86SJohn Marino 	case ISEQ_FULL_BARRIER:
27115ab8c86SJohn Marino 		if (action != LZMA_FULL_BARRIER
27215ab8c86SJohn Marino 				|| strm->internal->avail_in != strm->avail_in)
27315ab8c86SJohn Marino 			return LZMA_PROG_ERROR;
27415ab8c86SJohn Marino 
27515ab8c86SJohn Marino 		break;
27615ab8c86SJohn Marino 
2772940b44dSPeter Avalos 	case ISEQ_END:
2782940b44dSPeter Avalos 		return LZMA_STREAM_END;
2792940b44dSPeter Avalos 
2802940b44dSPeter Avalos 	case ISEQ_ERROR:
2812940b44dSPeter Avalos 	default:
2822940b44dSPeter Avalos 		return LZMA_PROG_ERROR;
2832940b44dSPeter Avalos 	}
2842940b44dSPeter Avalos 
2852940b44dSPeter Avalos 	size_t in_pos = 0;
2862940b44dSPeter Avalos 	size_t out_pos = 0;
2872940b44dSPeter Avalos 	lzma_ret ret = strm->internal->next.code(
2882940b44dSPeter Avalos 			strm->internal->next.coder, strm->allocator,
2892940b44dSPeter Avalos 			strm->next_in, &in_pos, strm->avail_in,
2902940b44dSPeter Avalos 			strm->next_out, &out_pos, strm->avail_out, action);
2912940b44dSPeter Avalos 
2922940b44dSPeter Avalos 	strm->next_in += in_pos;
2932940b44dSPeter Avalos 	strm->avail_in -= in_pos;
2942940b44dSPeter Avalos 	strm->total_in += in_pos;
2952940b44dSPeter Avalos 
2962940b44dSPeter Avalos 	strm->next_out += out_pos;
2972940b44dSPeter Avalos 	strm->avail_out -= out_pos;
2982940b44dSPeter Avalos 	strm->total_out += out_pos;
2992940b44dSPeter Avalos 
3002940b44dSPeter Avalos 	strm->internal->avail_in = strm->avail_in;
3012940b44dSPeter Avalos 
30215ab8c86SJohn Marino 	// Cast is needed to silence a warning about LZMA_TIMED_OUT, which
30315ab8c86SJohn Marino 	// isn't part of lzma_ret enumeration.
30415ab8c86SJohn Marino 	switch ((unsigned int)(ret)) {
3052940b44dSPeter Avalos 	case LZMA_OK:
3062940b44dSPeter Avalos 		// Don't return LZMA_BUF_ERROR when it happens the first time.
3072940b44dSPeter Avalos 		// This is to avoid returning LZMA_BUF_ERROR when avail_out
3082940b44dSPeter Avalos 		// was zero but still there was no more data left to written
3092940b44dSPeter Avalos 		// to next_out.
3102940b44dSPeter Avalos 		if (out_pos == 0 && in_pos == 0) {
3112940b44dSPeter Avalos 			if (strm->internal->allow_buf_error)
3122940b44dSPeter Avalos 				ret = LZMA_BUF_ERROR;
3132940b44dSPeter Avalos 			else
3142940b44dSPeter Avalos 				strm->internal->allow_buf_error = true;
3152940b44dSPeter Avalos 		} else {
3162940b44dSPeter Avalos 			strm->internal->allow_buf_error = false;
3172940b44dSPeter Avalos 		}
3182940b44dSPeter Avalos 		break;
3192940b44dSPeter Avalos 
32015ab8c86SJohn Marino 	case LZMA_TIMED_OUT:
32115ab8c86SJohn Marino 		strm->internal->allow_buf_error = false;
32215ab8c86SJohn Marino 		ret = LZMA_OK;
32315ab8c86SJohn Marino 		break;
32415ab8c86SJohn Marino 
3252940b44dSPeter Avalos 	case LZMA_STREAM_END:
3262940b44dSPeter Avalos 		if (strm->internal->sequence == ISEQ_SYNC_FLUSH
32715ab8c86SJohn Marino 				|| strm->internal->sequence == ISEQ_FULL_FLUSH
32815ab8c86SJohn Marino 				|| strm->internal->sequence
32915ab8c86SJohn Marino 					== ISEQ_FULL_BARRIER)
3302940b44dSPeter Avalos 			strm->internal->sequence = ISEQ_RUN;
3312940b44dSPeter Avalos 		else
3322940b44dSPeter Avalos 			strm->internal->sequence = ISEQ_END;
3332940b44dSPeter Avalos 
3342940b44dSPeter Avalos 	// Fall through
3352940b44dSPeter Avalos 
3362940b44dSPeter Avalos 	case LZMA_NO_CHECK:
3372940b44dSPeter Avalos 	case LZMA_UNSUPPORTED_CHECK:
3382940b44dSPeter Avalos 	case LZMA_GET_CHECK:
3392940b44dSPeter Avalos 	case LZMA_MEMLIMIT_ERROR:
3402940b44dSPeter Avalos 		// Something else than LZMA_OK, but not a fatal error,
3412940b44dSPeter Avalos 		// that is, coding may be continued (except if ISEQ_END).
3422940b44dSPeter Avalos 		strm->internal->allow_buf_error = false;
3432940b44dSPeter Avalos 		break;
3442940b44dSPeter Avalos 
3452940b44dSPeter Avalos 	default:
3462940b44dSPeter Avalos 		// All the other errors are fatal; coding cannot be continued.
3472940b44dSPeter Avalos 		assert(ret != LZMA_BUF_ERROR);
3482940b44dSPeter Avalos 		strm->internal->sequence = ISEQ_ERROR;
3492940b44dSPeter Avalos 		break;
3502940b44dSPeter Avalos 	}
3512940b44dSPeter Avalos 
3522940b44dSPeter Avalos 	return ret;
3532940b44dSPeter Avalos }
3542940b44dSPeter Avalos 
3552940b44dSPeter Avalos 
3562940b44dSPeter Avalos extern LZMA_API(void)
lzma_end(lzma_stream * strm)3572940b44dSPeter Avalos lzma_end(lzma_stream *strm)
3582940b44dSPeter Avalos {
3592940b44dSPeter Avalos 	if (strm != NULL && strm->internal != NULL) {
3602940b44dSPeter Avalos 		lzma_next_end(&strm->internal->next, strm->allocator);
3612940b44dSPeter Avalos 		lzma_free(strm->internal, strm->allocator);
3622940b44dSPeter Avalos 		strm->internal = NULL;
3632940b44dSPeter Avalos 	}
3642940b44dSPeter Avalos 
3652940b44dSPeter Avalos 	return;
3662940b44dSPeter Avalos }
3672940b44dSPeter Avalos 
3682940b44dSPeter Avalos 
36915ab8c86SJohn Marino extern LZMA_API(void)
lzma_get_progress(lzma_stream * strm,uint64_t * progress_in,uint64_t * progress_out)37015ab8c86SJohn Marino lzma_get_progress(lzma_stream *strm,
37115ab8c86SJohn Marino 		uint64_t *progress_in, uint64_t *progress_out)
37215ab8c86SJohn Marino {
37315ab8c86SJohn Marino 	if (strm->internal->next.get_progress != NULL) {
37415ab8c86SJohn Marino 		strm->internal->next.get_progress(strm->internal->next.coder,
37515ab8c86SJohn Marino 				progress_in, progress_out);
37615ab8c86SJohn Marino 	} else {
37715ab8c86SJohn Marino 		*progress_in = strm->total_in;
37815ab8c86SJohn Marino 		*progress_out = strm->total_out;
37915ab8c86SJohn Marino 	}
38015ab8c86SJohn Marino 
38115ab8c86SJohn Marino 	return;
38215ab8c86SJohn Marino }
38315ab8c86SJohn Marino 
38415ab8c86SJohn Marino 
3852940b44dSPeter Avalos extern LZMA_API(lzma_check)
lzma_get_check(const lzma_stream * strm)3862940b44dSPeter Avalos lzma_get_check(const lzma_stream *strm)
3872940b44dSPeter Avalos {
3882940b44dSPeter Avalos 	// Return LZMA_CHECK_NONE if we cannot know the check type.
3892940b44dSPeter Avalos 	// It's a bug in the application if this happens.
3902940b44dSPeter Avalos 	if (strm->internal->next.get_check == NULL)
3912940b44dSPeter Avalos 		return LZMA_CHECK_NONE;
3922940b44dSPeter Avalos 
3932940b44dSPeter Avalos 	return strm->internal->next.get_check(strm->internal->next.coder);
3942940b44dSPeter Avalos }
3952940b44dSPeter Avalos 
3962940b44dSPeter Avalos 
3972940b44dSPeter Avalos extern LZMA_API(uint64_t)
lzma_memusage(const lzma_stream * strm)3982940b44dSPeter Avalos lzma_memusage(const lzma_stream *strm)
3992940b44dSPeter Avalos {
4002940b44dSPeter Avalos 	uint64_t memusage;
4012940b44dSPeter Avalos 	uint64_t old_memlimit;
4022940b44dSPeter Avalos 
4032940b44dSPeter Avalos 	if (strm == NULL || strm->internal == NULL
4042940b44dSPeter Avalos 			|| strm->internal->next.memconfig == NULL
4052940b44dSPeter Avalos 			|| strm->internal->next.memconfig(
4062940b44dSPeter Avalos 				strm->internal->next.coder,
4072940b44dSPeter Avalos 				&memusage, &old_memlimit, 0) != LZMA_OK)
4082940b44dSPeter Avalos 		return 0;
4092940b44dSPeter Avalos 
4102940b44dSPeter Avalos 	return memusage;
4112940b44dSPeter Avalos }
4122940b44dSPeter Avalos 
4132940b44dSPeter Avalos 
4142940b44dSPeter Avalos extern LZMA_API(uint64_t)
lzma_memlimit_get(const lzma_stream * strm)4152940b44dSPeter Avalos lzma_memlimit_get(const lzma_stream *strm)
4162940b44dSPeter Avalos {
4172940b44dSPeter Avalos 	uint64_t old_memlimit;
4182940b44dSPeter Avalos 	uint64_t memusage;
4192940b44dSPeter Avalos 
4202940b44dSPeter Avalos 	if (strm == NULL || strm->internal == NULL
4212940b44dSPeter Avalos 			|| strm->internal->next.memconfig == NULL
4222940b44dSPeter Avalos 			|| strm->internal->next.memconfig(
4232940b44dSPeter Avalos 				strm->internal->next.coder,
4242940b44dSPeter Avalos 				&memusage, &old_memlimit, 0) != LZMA_OK)
4252940b44dSPeter Avalos 		return 0;
4262940b44dSPeter Avalos 
4272940b44dSPeter Avalos 	return old_memlimit;
4282940b44dSPeter Avalos }
4292940b44dSPeter Avalos 
4302940b44dSPeter Avalos 
4312940b44dSPeter Avalos extern LZMA_API(lzma_ret)
lzma_memlimit_set(lzma_stream * strm,uint64_t new_memlimit)4322940b44dSPeter Avalos lzma_memlimit_set(lzma_stream *strm, uint64_t new_memlimit)
4332940b44dSPeter Avalos {
4342940b44dSPeter Avalos 	// Dummy variables to simplify memconfig functions
4352940b44dSPeter Avalos 	uint64_t old_memlimit;
4362940b44dSPeter Avalos 	uint64_t memusage;
4372940b44dSPeter Avalos 
4382940b44dSPeter Avalos 	if (strm == NULL || strm->internal == NULL
4392940b44dSPeter Avalos 			|| strm->internal->next.memconfig == NULL)
4402940b44dSPeter Avalos 		return LZMA_PROG_ERROR;
4412940b44dSPeter Avalos 
44246a2189dSzrj 	// Zero is a special value that cannot be used as an actual limit.
44346a2189dSzrj 	// If 0 was specified, use 1 instead.
44446a2189dSzrj 	if (new_memlimit == 0)
44546a2189dSzrj 		new_memlimit = 1;
4462940b44dSPeter Avalos 
4472940b44dSPeter Avalos 	return strm->internal->next.memconfig(strm->internal->next.coder,
4482940b44dSPeter Avalos 			&memusage, &old_memlimit, new_memlimit);
4492940b44dSPeter Avalos }
450