1 /////////////////////////////////////////////////////////////////////////////// 2 // 3 /// \file alone_decoder.c 4 /// \brief Decoder for LZMA_Alone files 5 // 6 // Author: Lasse Collin 7 // 8 // This file has been put into the public domain. 9 // You can do whatever you want with this file. 10 // 11 /////////////////////////////////////////////////////////////////////////////// 12 13 #include "common.h" 14 #include "lzma_encoder.h" 15 16 17 #define ALONE_HEADER_SIZE (1 + 4 + 8) 18 19 20 typedef struct { 21 lzma_next_coder next; 22 23 enum { 24 SEQ_HEADER, 25 SEQ_CODE, 26 } sequence; 27 28 size_t header_pos; 29 uint8_t header[ALONE_HEADER_SIZE]; 30 } lzma_alone_coder; 31 32 33 static lzma_ret 34 alone_encode(void *coder_ptr, 35 const lzma_allocator *allocator lzma_attribute((__unused__)), 36 const uint8_t *restrict in, size_t *restrict in_pos, 37 size_t in_size, uint8_t *restrict out, 38 size_t *restrict out_pos, size_t out_size, 39 lzma_action action) 40 { 41 lzma_alone_coder *coder = coder_ptr; 42 43 while (*out_pos < out_size) 44 switch (coder->sequence) { 45 case SEQ_HEADER: 46 lzma_bufcpy(coder->header, &coder->header_pos, 47 ALONE_HEADER_SIZE, 48 out, out_pos, out_size); 49 if (coder->header_pos < ALONE_HEADER_SIZE) 50 return LZMA_OK; 51 52 coder->sequence = SEQ_CODE; 53 break; 54 55 case SEQ_CODE: 56 return coder->next.code(coder->next.coder, 57 allocator, in, in_pos, in_size, 58 out, out_pos, out_size, action); 59 60 default: 61 assert(0); 62 return LZMA_PROG_ERROR; 63 } 64 65 return LZMA_OK; 66 } 67 68 69 static void 70 alone_encoder_end(void *coder_ptr, const lzma_allocator *allocator) 71 { 72 lzma_alone_coder *coder = coder_ptr; 73 lzma_next_end(&coder->next, allocator); 74 lzma_free(coder, allocator); 75 return; 76 } 77 78 79 // At least for now, this is not used by any internal function. 80 static lzma_ret 81 alone_encoder_init(lzma_next_coder *next, const lzma_allocator *allocator, 82 const lzma_options_lzma *options) 83 { 84 lzma_next_coder_init(&alone_encoder_init, next, allocator); 85 86 lzma_alone_coder *coder = next->coder; 87 88 if (coder == NULL) { 89 coder = lzma_alloc(sizeof(lzma_alone_coder), allocator); 90 if (coder == NULL) 91 return LZMA_MEM_ERROR; 92 93 next->coder = coder; 94 next->code = &alone_encode; 95 next->end = &alone_encoder_end; 96 coder->next = LZMA_NEXT_CODER_INIT; 97 } 98 99 // Basic initializations 100 coder->sequence = SEQ_HEADER; 101 coder->header_pos = 0; 102 103 // Encode the header: 104 // - Properties (1 byte) 105 if (lzma_lzma_lclppb_encode(options, coder->header)) 106 return LZMA_OPTIONS_ERROR; 107 108 // - Dictionary size (4 bytes) 109 if (options->dict_size < LZMA_DICT_SIZE_MIN) 110 return LZMA_OPTIONS_ERROR; 111 112 // Round up to the next 2^n or 2^n + 2^(n - 1) depending on which 113 // one is the next unless it is UINT32_MAX. While the header would 114 // allow any 32-bit integer, we do this to keep the decoder of liblzma 115 // accepting the resulting files. 116 uint32_t d = options->dict_size - 1; 117 d |= d >> 2; 118 d |= d >> 3; 119 d |= d >> 4; 120 d |= d >> 8; 121 d |= d >> 16; 122 if (d != UINT32_MAX) 123 ++d; 124 125 unaligned_write32le(coder->header + 1, d); 126 127 // - Uncompressed size (always unknown and using EOPM) 128 memset(coder->header + 1 + 4, 0xFF, 8); 129 130 // Initialize the LZMA encoder. 131 const lzma_filter_info filters[2] = { 132 { 133 .init = &lzma_lzma_encoder_init, 134 .options = (void *)(options), 135 }, { 136 .init = NULL, 137 } 138 }; 139 140 return lzma_next_filter_init(&coder->next, allocator, filters); 141 } 142 143 144 /* 145 extern lzma_ret 146 lzma_alone_encoder_init(lzma_next_coder *next, const lzma_allocator *allocator, 147 const lzma_options_alone *options) 148 { 149 lzma_next_coder_init(&alone_encoder_init, next, allocator, options); 150 } 151 */ 152 153 154 extern LZMA_API(lzma_ret) 155 lzma_alone_encoder(lzma_stream *strm, const lzma_options_lzma *options) 156 { 157 lzma_next_strm_init(alone_encoder_init, strm, options); 158 159 strm->internal->supported_actions[LZMA_RUN] = true; 160 strm->internal->supported_actions[LZMA_FINISH] = true; 161 162 return LZMA_OK; 163 } 164