1207753Smm/////////////////////////////////////////////////////////////////////////////// 2207753Smm// 3207753Smm/// \file filter_buffer_decoder.c 4207753Smm/// \brief Single-call raw decoding 5207753Smm// 6207753Smm// Author: Lasse Collin 7207753Smm// 8207753Smm// This file has been put into the public domain. 9207753Smm// You can do whatever you want with this file. 10207753Smm// 11207753Smm/////////////////////////////////////////////////////////////////////////////// 12207753Smm 13207753Smm#include "filter_decoder.h" 14207753Smm 15207753Smm 16207753Smmextern LZMA_API(lzma_ret) 17278433Srpaulolzma_raw_buffer_decode( 18278433Srpaulo const lzma_filter *filters, const lzma_allocator *allocator, 19207753Smm const uint8_t *in, size_t *in_pos, size_t in_size, 20207753Smm uint8_t *out, size_t *out_pos, size_t out_size) 21207753Smm{ 22207753Smm // Validate what isn't validated later in filter_common.c. 23207753Smm if (in == NULL || in_pos == NULL || *in_pos > in_size || out == NULL 24207753Smm || out_pos == NULL || *out_pos > out_size) 25207753Smm return LZMA_PROG_ERROR; 26207753Smm 27207753Smm // Initialize the decoer. 28207753Smm lzma_next_coder next = LZMA_NEXT_CODER_INIT; 29207753Smm return_if_error(lzma_raw_decoder_init(&next, allocator, filters)); 30207753Smm 31207753Smm // Store the positions so that we can restore them if something 32207753Smm // goes wrong. 33207753Smm const size_t in_start = *in_pos; 34207753Smm const size_t out_start = *out_pos; 35207753Smm 36207753Smm // Do the actual decoding and free decoder's memory. 37207753Smm lzma_ret ret = next.code(next.coder, allocator, in, in_pos, in_size, 38207753Smm out, out_pos, out_size, LZMA_FINISH); 39207753Smm 40207753Smm if (ret == LZMA_STREAM_END) { 41207753Smm ret = LZMA_OK; 42207753Smm } else { 43207753Smm if (ret == LZMA_OK) { 44207753Smm // Either the input was truncated or the 45207753Smm // output buffer was too small. 46207753Smm assert(*in_pos == in_size || *out_pos == out_size); 47207753Smm 48207753Smm if (*in_pos != in_size) { 49207753Smm // Since input wasn't consumed completely, 50207753Smm // the output buffer became full and is 51207753Smm // too small. 52207753Smm ret = LZMA_BUF_ERROR; 53207753Smm 54207753Smm } else if (*out_pos != out_size) { 55207753Smm // Since output didn't became full, the input 56207753Smm // has to be truncated. 57207753Smm ret = LZMA_DATA_ERROR; 58207753Smm 59207753Smm } else { 60207753Smm // All the input was consumed and output 61207753Smm // buffer is full. Now we don't immediately 62207753Smm // know the reason for the error. Try 63207753Smm // decoding one more byte. If it succeeds, 64207753Smm // then the output buffer was too small. If 65207753Smm // we cannot get a new output byte, the input 66207753Smm // is truncated. 67207753Smm uint8_t tmp[1]; 68207753Smm size_t tmp_pos = 0; 69207753Smm (void)next.code(next.coder, allocator, 70207753Smm in, in_pos, in_size, 71207753Smm tmp, &tmp_pos, 1, LZMA_FINISH); 72207753Smm 73207753Smm if (tmp_pos == 1) 74207753Smm ret = LZMA_BUF_ERROR; 75207753Smm else 76207753Smm ret = LZMA_DATA_ERROR; 77207753Smm } 78207753Smm } 79207753Smm 80207753Smm // Restore the positions. 81207753Smm *in_pos = in_start; 82207753Smm *out_pos = out_start; 83207753Smm } 84207753Smm 85207753Smm lzma_next_end(&next, allocator); 86207753Smm 87207753Smm return ret; 88207753Smm} 89