Path: blob/master/Utilities/cmliblzma/liblzma/common/common.c
3153 views
// SPDX-License-Identifier: 0BSD12///////////////////////////////////////////////////////////////////////////////3//4/// \file common.c5/// \brief Common functions needed in many places in liblzma6//7// Author: Lasse Collin8//9///////////////////////////////////////////////////////////////////////////////1011#include "common.h"121314/////////////15// Version //16/////////////1718extern LZMA_API(uint32_t)19lzma_version_number(void)20{21return LZMA_VERSION;22}232425extern LZMA_API(const char *)26lzma_version_string(void)27{28return LZMA_VERSION_STRING;29}303132///////////////////////33// Memory allocation //34///////////////////////3536lzma_attr_alloc_size(1)37extern void *38lzma_alloc(size_t size, const lzma_allocator *allocator)39{40// Some malloc() variants return NULL if called with size == 0.41if (size == 0)42size = 1;4344void *ptr;4546if (allocator != NULL && allocator->alloc != NULL)47ptr = allocator->alloc(allocator->opaque, 1, size);48else49ptr = malloc(size);5051return ptr;52}535455lzma_attr_alloc_size(1)56extern void *57lzma_alloc_zero(size_t size, const lzma_allocator *allocator)58{59// Some calloc() variants return NULL if called with size == 0.60if (size == 0)61size = 1;6263void *ptr;6465if (allocator != NULL && allocator->alloc != NULL) {66ptr = allocator->alloc(allocator->opaque, 1, size);67if (ptr != NULL)68memzero(ptr, size);69} else {70ptr = calloc(1, size);71}7273return ptr;74}757677extern void78lzma_free(void *ptr, const lzma_allocator *allocator)79{80if (allocator != NULL && allocator->free != NULL)81allocator->free(allocator->opaque, ptr);82else83free(ptr);8485return;86}878889//////////90// Misc //91//////////9293extern size_t94lzma_bufcpy(const uint8_t *restrict in, size_t *restrict in_pos,95size_t in_size, uint8_t *restrict out,96size_t *restrict out_pos, size_t out_size)97{98const size_t in_avail = in_size - *in_pos;99const size_t out_avail = out_size - *out_pos;100const size_t copy_size = my_min(in_avail, out_avail);101102// Call memcpy() only if there is something to copy. If there is103// nothing to copy, in or out might be NULL and then the memcpy()104// call would trigger undefined behavior.105if (copy_size > 0)106memcpy(out + *out_pos, in + *in_pos, copy_size);107108*in_pos += copy_size;109*out_pos += copy_size;110111return copy_size;112}113114115extern lzma_ret116lzma_next_filter_init(lzma_next_coder *next, const lzma_allocator *allocator,117const lzma_filter_info *filters)118{119lzma_next_coder_init(filters[0].init, next, allocator);120next->id = filters[0].id;121return filters[0].init == NULL122? LZMA_OK : filters[0].init(next, allocator, filters);123}124125126extern lzma_ret127lzma_next_filter_update(lzma_next_coder *next, const lzma_allocator *allocator,128const lzma_filter *reversed_filters)129{130// Check that the application isn't trying to change the Filter ID.131// End of filters is indicated with LZMA_VLI_UNKNOWN in both132// reversed_filters[0].id and next->id.133if (reversed_filters[0].id != next->id)134return LZMA_PROG_ERROR;135136if (reversed_filters[0].id == LZMA_VLI_UNKNOWN)137return LZMA_OK;138139assert(next->update != NULL);140return next->update(next->coder, allocator, NULL, reversed_filters);141}142143144extern void145lzma_next_end(lzma_next_coder *next, const lzma_allocator *allocator)146{147if (next->init != (uintptr_t)(NULL)) {148// To avoid tiny end functions that simply call149// lzma_free(coder, allocator), we allow leaving next->end150// NULL and call lzma_free() here.151if (next->end != NULL)152next->end(next->coder, allocator);153else154lzma_free(next->coder, allocator);155156// Reset the variables so the we don't accidentally think157// that it is an already initialized coder.158*next = LZMA_NEXT_CODER_INIT;159}160161return;162}163164165//////////////////////////////////////166// External to internal API wrapper //167//////////////////////////////////////168169extern lzma_ret170lzma_strm_init(lzma_stream *strm)171{172if (strm == NULL)173return LZMA_PROG_ERROR;174175if (strm->internal == NULL) {176strm->internal = lzma_alloc(sizeof(lzma_internal),177strm->allocator);178if (strm->internal == NULL)179return LZMA_MEM_ERROR;180181strm->internal->next = LZMA_NEXT_CODER_INIT;182}183184memzero(strm->internal->supported_actions,185sizeof(strm->internal->supported_actions));186strm->internal->sequence = ISEQ_RUN;187strm->internal->allow_buf_error = false;188189strm->total_in = 0;190strm->total_out = 0;191192return LZMA_OK;193}194195196extern LZMA_API(lzma_ret)197lzma_code(lzma_stream *strm, lzma_action action)198{199// Sanity checks200if ((strm->next_in == NULL && strm->avail_in != 0)201|| (strm->next_out == NULL && strm->avail_out != 0)202|| strm->internal == NULL203|| strm->internal->next.code == NULL204|| (unsigned int)(action) > LZMA_ACTION_MAX205|| !strm->internal->supported_actions[action])206return LZMA_PROG_ERROR;207208// Check if unsupported members have been set to non-zero or non-NULL,209// which would indicate that some new feature is wanted.210if (strm->reserved_ptr1 != NULL211|| strm->reserved_ptr2 != NULL212|| strm->reserved_ptr3 != NULL213|| strm->reserved_ptr4 != NULL214|| strm->reserved_int2 != 0215|| strm->reserved_int3 != 0216|| strm->reserved_int4 != 0217|| strm->reserved_enum1 != LZMA_RESERVED_ENUM218|| strm->reserved_enum2 != LZMA_RESERVED_ENUM)219return LZMA_OPTIONS_ERROR;220221switch (strm->internal->sequence) {222case ISEQ_RUN:223switch (action) {224case LZMA_RUN:225break;226227case LZMA_SYNC_FLUSH:228strm->internal->sequence = ISEQ_SYNC_FLUSH;229break;230231case LZMA_FULL_FLUSH:232strm->internal->sequence = ISEQ_FULL_FLUSH;233break;234235case LZMA_FINISH:236strm->internal->sequence = ISEQ_FINISH;237break;238239case LZMA_FULL_BARRIER:240strm->internal->sequence = ISEQ_FULL_BARRIER;241break;242}243244break;245246case ISEQ_SYNC_FLUSH:247// The same action must be used until we return248// LZMA_STREAM_END, and the amount of input must not change.249if (action != LZMA_SYNC_FLUSH250|| strm->internal->avail_in != strm->avail_in)251return LZMA_PROG_ERROR;252253break;254255case ISEQ_FULL_FLUSH:256if (action != LZMA_FULL_FLUSH257|| strm->internal->avail_in != strm->avail_in)258return LZMA_PROG_ERROR;259260break;261262case ISEQ_FINISH:263if (action != LZMA_FINISH264|| strm->internal->avail_in != strm->avail_in)265return LZMA_PROG_ERROR;266267break;268269case ISEQ_FULL_BARRIER:270if (action != LZMA_FULL_BARRIER271|| strm->internal->avail_in != strm->avail_in)272return LZMA_PROG_ERROR;273274break;275276case ISEQ_END:277return LZMA_STREAM_END;278279case ISEQ_ERROR:280default:281return LZMA_PROG_ERROR;282}283284size_t in_pos = 0;285size_t out_pos = 0;286lzma_ret ret = strm->internal->next.code(287strm->internal->next.coder, strm->allocator,288strm->next_in, &in_pos, strm->avail_in,289strm->next_out, &out_pos, strm->avail_out, action);290291// Updating next_in and next_out has to be skipped when they are NULL292// to avoid null pointer + 0 (undefined behavior). Do this by checking293// in_pos > 0 and out_pos > 0 because this way NULL + non-zero (a bug)294// will get caught one way or other.295if (in_pos > 0) {296strm->next_in += in_pos;297strm->avail_in -= in_pos;298strm->total_in += in_pos;299}300301if (out_pos > 0) {302strm->next_out += out_pos;303strm->avail_out -= out_pos;304strm->total_out += out_pos;305}306307strm->internal->avail_in = strm->avail_in;308309switch (ret) {310case LZMA_OK:311// Don't return LZMA_BUF_ERROR when it happens the first time.312// This is to avoid returning LZMA_BUF_ERROR when avail_out313// was zero but still there was no more data left to written314// to next_out.315if (out_pos == 0 && in_pos == 0) {316if (strm->internal->allow_buf_error)317ret = LZMA_BUF_ERROR;318else319strm->internal->allow_buf_error = true;320} else {321strm->internal->allow_buf_error = false;322}323break;324325case LZMA_TIMED_OUT:326strm->internal->allow_buf_error = false;327ret = LZMA_OK;328break;329330case LZMA_SEEK_NEEDED:331strm->internal->allow_buf_error = false;332333// If LZMA_FINISH was used, reset it back to the334// LZMA_RUN-based state so that new input can be supplied335// by the application.336if (strm->internal->sequence == ISEQ_FINISH)337strm->internal->sequence = ISEQ_RUN;338339break;340341case LZMA_STREAM_END:342if (strm->internal->sequence == ISEQ_SYNC_FLUSH343|| strm->internal->sequence == ISEQ_FULL_FLUSH344|| strm->internal->sequence345== ISEQ_FULL_BARRIER)346strm->internal->sequence = ISEQ_RUN;347else348strm->internal->sequence = ISEQ_END;349350// Fall through351352case LZMA_NO_CHECK:353case LZMA_UNSUPPORTED_CHECK:354case LZMA_GET_CHECK:355case LZMA_MEMLIMIT_ERROR:356// Something else than LZMA_OK, but not a fatal error,357// that is, coding may be continued (except if ISEQ_END).358strm->internal->allow_buf_error = false;359break;360361default:362// All the other errors are fatal; coding cannot be continued.363assert(ret != LZMA_BUF_ERROR);364strm->internal->sequence = ISEQ_ERROR;365break;366}367368return ret;369}370371372extern LZMA_API(void)373lzma_end(lzma_stream *strm)374{375if (strm != NULL && strm->internal != NULL) {376lzma_next_end(&strm->internal->next, strm->allocator);377lzma_free(strm->internal, strm->allocator);378strm->internal = NULL;379}380381return;382}383384385#ifdef HAVE_SYMBOL_VERSIONS_LINUX386// This is for compatibility with binaries linked against liblzma that387// has been patched with xz-5.2.2-compat-libs.patch from RHEL/CentOS 7.388LZMA_SYMVER_API("lzma_get_progress@XZ_5.2.2",389void, lzma_get_progress_522)(lzma_stream *strm,390uint64_t *progress_in, uint64_t *progress_out) lzma_nothrow391__attribute__((__alias__("lzma_get_progress_52")));392393LZMA_SYMVER_API("lzma_get_progress@@XZ_5.2",394void, lzma_get_progress_52)(lzma_stream *strm,395uint64_t *progress_in, uint64_t *progress_out) lzma_nothrow;396397#define lzma_get_progress lzma_get_progress_52398#endif399extern LZMA_API(void)400lzma_get_progress(lzma_stream *strm,401uint64_t *progress_in, uint64_t *progress_out)402{403if (strm->internal->next.get_progress != NULL) {404strm->internal->next.get_progress(strm->internal->next.coder,405progress_in, progress_out);406} else {407*progress_in = strm->total_in;408*progress_out = strm->total_out;409}410411return;412}413414415extern LZMA_API(lzma_check)416lzma_get_check(const lzma_stream *strm)417{418// Return LZMA_CHECK_NONE if we cannot know the check type.419// It's a bug in the application if this happens.420if (strm->internal->next.get_check == NULL)421return LZMA_CHECK_NONE;422423return strm->internal->next.get_check(strm->internal->next.coder);424}425426427extern LZMA_API(uint64_t)428lzma_memusage(const lzma_stream *strm)429{430uint64_t memusage;431uint64_t old_memlimit;432433if (strm == NULL || strm->internal == NULL434|| strm->internal->next.memconfig == NULL435|| strm->internal->next.memconfig(436strm->internal->next.coder,437&memusage, &old_memlimit, 0) != LZMA_OK)438return 0;439440return memusage;441}442443444extern LZMA_API(uint64_t)445lzma_memlimit_get(const lzma_stream *strm)446{447uint64_t old_memlimit;448uint64_t memusage;449450if (strm == NULL || strm->internal == NULL451|| strm->internal->next.memconfig == NULL452|| strm->internal->next.memconfig(453strm->internal->next.coder,454&memusage, &old_memlimit, 0) != LZMA_OK)455return 0;456457return old_memlimit;458}459460461extern LZMA_API(lzma_ret)462lzma_memlimit_set(lzma_stream *strm, uint64_t new_memlimit)463{464// Dummy variables to simplify memconfig functions465uint64_t old_memlimit;466uint64_t memusage;467468if (strm == NULL || strm->internal == NULL469|| strm->internal->next.memconfig == NULL)470return LZMA_PROG_ERROR;471472// Zero is a special value that cannot be used as an actual limit.473// If 0 was specified, use 1 instead.474if (new_memlimit == 0)475new_memlimit = 1;476477return strm->internal->next.memconfig(strm->internal->next.coder,478&memusage, &old_memlimit, new_memlimit);479}480481482