1207753Smm/////////////////////////////////////////////////////////////////////////////// 2207753Smm// 3207753Smm/// \file alone_decoder.c 4207753Smm/// \brief Decoder for LZMA_Alone files 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 "common.h" 14207753Smm#include "lzma_encoder.h" 15207753Smm 16207753Smm 17207753Smm#define ALONE_HEADER_SIZE (1 + 4 + 8) 18207753Smm 19207753Smm 20207753Smmstruct lzma_coder_s { 21207753Smm lzma_next_coder next; 22207753Smm 23207753Smm enum { 24207753Smm SEQ_HEADER, 25207753Smm SEQ_CODE, 26207753Smm } sequence; 27207753Smm 28207753Smm size_t header_pos; 29207753Smm uint8_t header[ALONE_HEADER_SIZE]; 30207753Smm}; 31207753Smm 32207753Smm 33207753Smmstatic lzma_ret 34207753Smmalone_encode(lzma_coder *coder, 35223935Smm lzma_allocator *allocator lzma_attribute((__unused__)), 36207753Smm const uint8_t *restrict in, size_t *restrict in_pos, 37207753Smm size_t in_size, uint8_t *restrict out, 38207753Smm size_t *restrict out_pos, size_t out_size, 39207753Smm lzma_action action) 40207753Smm{ 41207753Smm while (*out_pos < out_size) 42207753Smm switch (coder->sequence) { 43207753Smm case SEQ_HEADER: 44207753Smm lzma_bufcpy(coder->header, &coder->header_pos, 45207753Smm ALONE_HEADER_SIZE, 46207753Smm out, out_pos, out_size); 47207753Smm if (coder->header_pos < ALONE_HEADER_SIZE) 48207753Smm return LZMA_OK; 49207753Smm 50207753Smm coder->sequence = SEQ_CODE; 51207753Smm break; 52207753Smm 53207753Smm case SEQ_CODE: 54207753Smm return coder->next.code(coder->next.coder, 55207753Smm allocator, in, in_pos, in_size, 56207753Smm out, out_pos, out_size, action); 57207753Smm 58207753Smm default: 59207753Smm assert(0); 60207753Smm return LZMA_PROG_ERROR; 61207753Smm } 62207753Smm 63207753Smm return LZMA_OK; 64207753Smm} 65207753Smm 66207753Smm 67207753Smmstatic void 68207753Smmalone_encoder_end(lzma_coder *coder, lzma_allocator *allocator) 69207753Smm{ 70207753Smm lzma_next_end(&coder->next, allocator); 71207753Smm lzma_free(coder, allocator); 72207753Smm return; 73207753Smm} 74207753Smm 75207753Smm 76207753Smm// At least for now, this is not used by any internal function. 77207753Smmstatic lzma_ret 78207753Smmalone_encoder_init(lzma_next_coder *next, lzma_allocator *allocator, 79207753Smm const lzma_options_lzma *options) 80207753Smm{ 81207753Smm lzma_next_coder_init(&alone_encoder_init, next, allocator); 82207753Smm 83207753Smm if (next->coder == NULL) { 84207753Smm next->coder = lzma_alloc(sizeof(lzma_coder), allocator); 85207753Smm if (next->coder == NULL) 86207753Smm return LZMA_MEM_ERROR; 87207753Smm 88207753Smm next->code = &alone_encode; 89207753Smm next->end = &alone_encoder_end; 90207753Smm next->coder->next = LZMA_NEXT_CODER_INIT; 91207753Smm } 92207753Smm 93207753Smm // Basic initializations 94207753Smm next->coder->sequence = SEQ_HEADER; 95207753Smm next->coder->header_pos = 0; 96207753Smm 97207753Smm // Encode the header: 98207753Smm // - Properties (1 byte) 99207753Smm if (lzma_lzma_lclppb_encode(options, next->coder->header)) 100207753Smm return LZMA_OPTIONS_ERROR; 101207753Smm 102207753Smm // - Dictionary size (4 bytes) 103207753Smm if (options->dict_size < LZMA_DICT_SIZE_MIN) 104207753Smm return LZMA_OPTIONS_ERROR; 105207753Smm 106223935Smm // Round up to the next 2^n or 2^n + 2^(n - 1) depending on which 107207753Smm // one is the next unless it is UINT32_MAX. While the header would 108207753Smm // allow any 32-bit integer, we do this to keep the decoder of liblzma 109207753Smm // accepting the resulting files. 110207753Smm uint32_t d = options->dict_size - 1; 111207753Smm d |= d >> 2; 112207753Smm d |= d >> 3; 113207753Smm d |= d >> 4; 114207753Smm d |= d >> 8; 115207753Smm d |= d >> 16; 116207753Smm if (d != UINT32_MAX) 117207753Smm ++d; 118207753Smm 119207753Smm unaligned_write32le(next->coder->header + 1, d); 120207753Smm 121207753Smm // - Uncompressed size (always unknown and using EOPM) 122207753Smm memset(next->coder->header + 1 + 4, 0xFF, 8); 123207753Smm 124207753Smm // Initialize the LZMA encoder. 125207753Smm const lzma_filter_info filters[2] = { 126207753Smm { 127207753Smm .init = &lzma_lzma_encoder_init, 128207753Smm .options = (void *)(options), 129207753Smm }, { 130207753Smm .init = NULL, 131207753Smm } 132207753Smm }; 133207753Smm 134207753Smm return lzma_next_filter_init(&next->coder->next, allocator, filters); 135207753Smm} 136207753Smm 137207753Smm 138207753Smm/* 139207753Smmextern lzma_ret 140207753Smmlzma_alone_encoder_init(lzma_next_coder *next, lzma_allocator *allocator, 141207753Smm const lzma_options_alone *options) 142207753Smm{ 143207753Smm lzma_next_coder_init(&alone_encoder_init, next, allocator, options); 144207753Smm} 145207753Smm*/ 146207753Smm 147207753Smm 148207753Smmextern LZMA_API(lzma_ret) 149207753Smmlzma_alone_encoder(lzma_stream *strm, const lzma_options_lzma *options) 150207753Smm{ 151207753Smm lzma_next_strm_init(alone_encoder_init, strm, options); 152207753Smm 153207753Smm strm->internal->supported_actions[LZMA_RUN] = true; 154207753Smm strm->internal->supported_actions[LZMA_FINISH] = true; 155207753Smm 156207753Smm return LZMA_OK; 157207753Smm} 158