Path: blob/master/Utilities/cmliblzma/liblzma/simple/simple_coder.c
3156 views
// SPDX-License-Identifier: 0BSD12///////////////////////////////////////////////////////////////////////////////3//4/// \file simple_coder.c5/// \brief Wrapper for simple filters6///7/// Simple filters don't change the size of the data i.e. number of bytes8/// in equals the number of bytes out.9//10// Author: Lasse Collin11//12///////////////////////////////////////////////////////////////////////////////1314#include "simple_private.h"151617/// Copied or encodes/decodes more data to out[].18static lzma_ret19copy_or_code(lzma_simple_coder *coder, const lzma_allocator *allocator,20const uint8_t *restrict in, size_t *restrict in_pos,21size_t in_size, uint8_t *restrict out,22size_t *restrict out_pos, size_t out_size, lzma_action action)23{24assert(!coder->end_was_reached);2526if (coder->next.code == NULL) {27lzma_bufcpy(in, in_pos, in_size, out, out_pos, out_size);2829// Check if end of stream was reached.30if (coder->is_encoder && action == LZMA_FINISH31&& *in_pos == in_size)32coder->end_was_reached = true;3334} else {35// Call the next coder in the chain to provide us some data.36const lzma_ret ret = coder->next.code(37coder->next.coder, allocator,38in, in_pos, in_size,39out, out_pos, out_size, action);4041if (ret == LZMA_STREAM_END) {42assert(!coder->is_encoder43|| action == LZMA_FINISH);44coder->end_was_reached = true;4546} else if (ret != LZMA_OK) {47return ret;48}49}5051return LZMA_OK;52}535455static size_t56call_filter(lzma_simple_coder *coder, uint8_t *buffer, size_t size)57{58const size_t filtered = coder->filter(coder->simple,59coder->now_pos, coder->is_encoder,60buffer, size);61coder->now_pos += filtered;62return filtered;63}646566static lzma_ret67simple_code(void *coder_ptr, const lzma_allocator *allocator,68const uint8_t *restrict in, size_t *restrict in_pos,69size_t in_size, uint8_t *restrict out,70size_t *restrict out_pos, size_t out_size, lzma_action action)71{72lzma_simple_coder *coder = coder_ptr;7374// TODO: Add partial support for LZMA_SYNC_FLUSH. We can support it75// in cases when the filter is able to filter everything. With most76// simple filters it can be done at offset that is a multiple of 2,77// 4, or 16. With x86 filter, it needs good luck, and thus cannot78// be made to work predictably.79if (action == LZMA_SYNC_FLUSH)80return LZMA_OPTIONS_ERROR;8182// Flush already filtered data from coder->buffer[] to out[].83if (coder->pos < coder->filtered) {84lzma_bufcpy(coder->buffer, &coder->pos, coder->filtered,85out, out_pos, out_size);8687// If we couldn't flush all the filtered data, return to88// application immediately.89if (coder->pos < coder->filtered)90return LZMA_OK;9192if (coder->end_was_reached) {93assert(coder->filtered == coder->size);94return LZMA_STREAM_END;95}96}9798// If we get here, there is no filtered data left in the buffer.99coder->filtered = 0;100101assert(!coder->end_was_reached);102103// If there is more output space left than there is unfiltered data104// in coder->buffer[], flush coder->buffer[] to out[], and copy/code105// more data to out[] hopefully filling it completely. Then filter106// the data in out[]. This step is where most of the data gets107// filtered if the buffer sizes used by the application are reasonable.108const size_t out_avail = out_size - *out_pos;109const size_t buf_avail = coder->size - coder->pos;110if (out_avail > buf_avail || buf_avail == 0) {111// Store the old position so that we know from which byte112// to start filtering.113const size_t out_start = *out_pos;114115// Flush data from coder->buffer[] to out[], but don't reset116// coder->pos and coder->size yet. This way the coder can be117// restarted if the next filter in the chain returns e.g.118// LZMA_MEM_ERROR.119//120// Do the memcpy() conditionally because out can be NULL121// (in which case buf_avail is always 0). Calling memcpy()122// with a null-pointer is undefined even if the third123// argument is 0.124if (buf_avail > 0)125memcpy(out + *out_pos, coder->buffer + coder->pos,126buf_avail);127128*out_pos += buf_avail;129130// Copy/Encode/Decode more data to out[].131{132const lzma_ret ret = copy_or_code(coder, allocator,133in, in_pos, in_size,134out, out_pos, out_size, action);135assert(ret != LZMA_STREAM_END);136if (ret != LZMA_OK)137return ret;138}139140// Filter out[] unless there is nothing to filter.141// This way we avoid null pointer + 0 (undefined behavior)142// when out == NULL.143const size_t size = *out_pos - out_start;144const size_t filtered = size == 0 ? 0 : call_filter(145coder, out + out_start, size);146147const size_t unfiltered = size - filtered;148assert(unfiltered <= coder->allocated / 2);149150// Now we can update coder->pos and coder->size, because151// the next coder in the chain (if any) was successful.152coder->pos = 0;153coder->size = unfiltered;154155if (coder->end_was_reached) {156// The last byte has been copied to out[] already.157// They are left as is.158coder->size = 0;159160} else if (unfiltered > 0) {161// There is unfiltered data left in out[]. Copy it to162// coder->buffer[] and rewind *out_pos appropriately.163*out_pos -= unfiltered;164memcpy(coder->buffer, out + *out_pos, unfiltered);165}166} else if (coder->pos > 0) {167memmove(coder->buffer, coder->buffer + coder->pos, buf_avail);168coder->size -= coder->pos;169coder->pos = 0;170}171172assert(coder->pos == 0);173174// If coder->buffer[] isn't empty, try to fill it by copying/decoding175// more data. Then filter coder->buffer[] and copy the successfully176// filtered data to out[]. It is probable, that some filtered and177// unfiltered data will be left to coder->buffer[].178if (coder->size > 0) {179{180const lzma_ret ret = copy_or_code(coder, allocator,181in, in_pos, in_size,182coder->buffer, &coder->size,183coder->allocated, action);184assert(ret != LZMA_STREAM_END);185if (ret != LZMA_OK)186return ret;187}188189coder->filtered = call_filter(190coder, coder->buffer, coder->size);191192// Everything is considered to be filtered if coder->buffer[]193// contains the last bytes of the data.194if (coder->end_was_reached)195coder->filtered = coder->size;196197// Flush as much as possible.198lzma_bufcpy(coder->buffer, &coder->pos, coder->filtered,199out, out_pos, out_size);200}201202// Check if we got everything done.203if (coder->end_was_reached && coder->pos == coder->size)204return LZMA_STREAM_END;205206return LZMA_OK;207}208209210static void211simple_coder_end(void *coder_ptr, const lzma_allocator *allocator)212{213lzma_simple_coder *coder = coder_ptr;214lzma_next_end(&coder->next, allocator);215lzma_free(coder->simple, allocator);216lzma_free(coder, allocator);217return;218}219220221static lzma_ret222simple_coder_update(void *coder_ptr, const lzma_allocator *allocator,223const lzma_filter *filters_null lzma_attribute((__unused__)),224const lzma_filter *reversed_filters)225{226lzma_simple_coder *coder = coder_ptr;227228// No update support, just call the next filter in the chain.229return lzma_next_filter_update(230&coder->next, allocator, reversed_filters + 1);231}232233234extern lzma_ret235lzma_simple_coder_init(lzma_next_coder *next, const lzma_allocator *allocator,236const lzma_filter_info *filters,237size_t (*filter)(void *simple, uint32_t now_pos,238bool is_encoder, uint8_t *buffer, size_t size),239size_t simple_size, size_t unfiltered_max,240uint32_t alignment, bool is_encoder)241{242// Allocate memory for the lzma_simple_coder structure if needed.243lzma_simple_coder *coder = next->coder;244if (coder == NULL) {245// Here we allocate space also for the temporary buffer. We246// need twice the size of unfiltered_max, because then it247// is always possible to filter at least unfiltered_max bytes248// more data in coder->buffer[] if it can be filled completely.249coder = lzma_alloc(sizeof(lzma_simple_coder)250+ 2 * unfiltered_max, allocator);251if (coder == NULL)252return LZMA_MEM_ERROR;253254next->coder = coder;255next->code = &simple_code;256next->end = &simple_coder_end;257next->update = &simple_coder_update;258259coder->next = LZMA_NEXT_CODER_INIT;260coder->filter = filter;261coder->allocated = 2 * unfiltered_max;262263// Allocate memory for filter-specific data structure.264if (simple_size > 0) {265coder->simple = lzma_alloc(simple_size, allocator);266if (coder->simple == NULL)267return LZMA_MEM_ERROR;268} else {269coder->simple = NULL;270}271}272273if (filters[0].options != NULL) {274const lzma_options_bcj *simple = filters[0].options;275coder->now_pos = simple->start_offset;276if (coder->now_pos & (alignment - 1))277return LZMA_OPTIONS_ERROR;278} else {279coder->now_pos = 0;280}281282// Reset variables.283coder->is_encoder = is_encoder;284coder->end_was_reached = false;285coder->pos = 0;286coder->filtered = 0;287coder->size = 0;288289return lzma_next_filter_init(&coder->next, allocator, filters + 1);290}291292293