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) 17207753Smmlzma_raw_buffer_decode(const lzma_filter *filters, lzma_allocator *allocator, 18207753Smm const uint8_t *in, size_t *in_pos, size_t in_size, 19207753Smm uint8_t *out, size_t *out_pos, size_t out_size) 20207753Smm{ 21207753Smm // Validate what isn't validated later in filter_common.c. 22207753Smm if (in == NULL || in_pos == NULL || *in_pos > in_size || out == NULL 23207753Smm || out_pos == NULL || *out_pos > out_size) 24207753Smm return LZMA_PROG_ERROR; 25207753Smm 26207753Smm // Initialize the decoer. 27207753Smm lzma_next_coder next = LZMA_NEXT_CODER_INIT; 28207753Smm return_if_error(lzma_raw_decoder_init(&next, allocator, filters)); 29207753Smm 30207753Smm // Store the positions so that we can restore them if something 31207753Smm // goes wrong. 32207753Smm const size_t in_start = *in_pos; 33207753Smm const size_t out_start = *out_pos; 34207753Smm 35207753Smm // Do the actual decoding and free decoder's memory. 36207753Smm lzma_ret ret = next.code(next.coder, allocator, in, in_pos, in_size, 37207753Smm out, out_pos, out_size, LZMA_FINISH); 38207753Smm 39207753Smm if (ret == LZMA_STREAM_END) { 40207753Smm ret = LZMA_OK; 41207753Smm } else { 42207753Smm if (ret == LZMA_OK) { 43207753Smm // Either the input was truncated or the 44207753Smm // output buffer was too small. 45207753Smm assert(*in_pos == in_size || *out_pos == out_size); 46207753Smm 47207753Smm if (*in_pos != in_size) { 48207753Smm // Since input wasn't consumed completely, 49207753Smm // the output buffer became full and is 50207753Smm // too small. 51207753Smm ret = LZMA_BUF_ERROR; 52207753Smm 53207753Smm } else if (*out_pos != out_size) { 54207753Smm // Since output didn't became full, the input 55207753Smm // has to be truncated. 56207753Smm ret = LZMA_DATA_ERROR; 57207753Smm 58207753Smm } else { 59207753Smm // All the input was consumed and output 60207753Smm // buffer is full. Now we don't immediately 61207753Smm // know the reason for the error. Try 62207753Smm // decoding one more byte. If it succeeds, 63207753Smm // then the output buffer was too small. If 64207753Smm // we cannot get a new output byte, the input 65207753Smm // is truncated. 66207753Smm uint8_t tmp[1]; 67207753Smm size_t tmp_pos = 0; 68207753Smm (void)next.code(next.coder, allocator, 69207753Smm in, in_pos, in_size, 70207753Smm tmp, &tmp_pos, 1, LZMA_FINISH); 71207753Smm 72207753Smm if (tmp_pos == 1) 73207753Smm ret = LZMA_BUF_ERROR; 74207753Smm else 75207753Smm ret = LZMA_DATA_ERROR; 76207753Smm } 77207753Smm } 78207753Smm 79207753Smm // Restore the positions. 80207753Smm *in_pos = in_start; 81207753Smm *out_pos = out_start; 82207753Smm } 83207753Smm 84207753Smm lzma_next_end(&next, allocator); 85207753Smm 86207753Smm return ret; 87207753Smm} 88