OLD | NEW |
(Empty) | |
| 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 struct lzma_coder_s { |
| 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 }; |
| 31 |
| 32 |
| 33 static lzma_ret |
| 34 alone_encode(lzma_coder *coder, |
| 35 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 while (*out_pos < out_size) |
| 42 switch (coder->sequence) { |
| 43 case SEQ_HEADER: |
| 44 lzma_bufcpy(coder->header, &coder->header_pos, |
| 45 ALONE_HEADER_SIZE, |
| 46 out, out_pos, out_size); |
| 47 if (coder->header_pos < ALONE_HEADER_SIZE) |
| 48 return LZMA_OK; |
| 49 |
| 50 coder->sequence = SEQ_CODE; |
| 51 break; |
| 52 |
| 53 case SEQ_CODE: |
| 54 return coder->next.code(coder->next.coder, |
| 55 allocator, in, in_pos, in_size, |
| 56 out, out_pos, out_size, action); |
| 57 |
| 58 default: |
| 59 assert(0); |
| 60 return LZMA_PROG_ERROR; |
| 61 } |
| 62 |
| 63 return LZMA_OK; |
| 64 } |
| 65 |
| 66 |
| 67 static void |
| 68 alone_encoder_end(lzma_coder *coder, lzma_allocator *allocator) |
| 69 { |
| 70 lzma_next_end(&coder->next, allocator); |
| 71 lzma_free(coder, allocator); |
| 72 return; |
| 73 } |
| 74 |
| 75 |
| 76 // At least for now, this is not used by any internal function. |
| 77 static lzma_ret |
| 78 alone_encoder_init(lzma_next_coder *next, lzma_allocator *allocator, |
| 79 const lzma_options_lzma *options) |
| 80 { |
| 81 lzma_next_coder_init(&alone_encoder_init, next, allocator); |
| 82 |
| 83 if (next->coder == NULL) { |
| 84 next->coder = lzma_alloc(sizeof(lzma_coder), allocator); |
| 85 if (next->coder == NULL) |
| 86 return LZMA_MEM_ERROR; |
| 87 |
| 88 next->code = &alone_encode; |
| 89 next->end = &alone_encoder_end; |
| 90 next->coder->next = LZMA_NEXT_CODER_INIT; |
| 91 } |
| 92 |
| 93 // Basic initializations |
| 94 next->coder->sequence = SEQ_HEADER; |
| 95 next->coder->header_pos = 0; |
| 96 |
| 97 // Encode the header: |
| 98 // - Properties (1 byte) |
| 99 if (lzma_lzma_lclppb_encode(options, next->coder->header)) |
| 100 return LZMA_OPTIONS_ERROR; |
| 101 |
| 102 // - Dictionary size (4 bytes) |
| 103 if (options->dict_size < LZMA_DICT_SIZE_MIN) |
| 104 return LZMA_OPTIONS_ERROR; |
| 105 |
| 106 // Round up to to the next 2^n or 2^n + 2^(n - 1) depending on which |
| 107 // one is the next unless it is UINT32_MAX. While the header would |
| 108 // allow any 32-bit integer, we do this to keep the decoder of liblzma |
| 109 // accepting the resulting files. |
| 110 uint32_t d = options->dict_size - 1; |
| 111 d |= d >> 2; |
| 112 d |= d >> 3; |
| 113 d |= d >> 4; |
| 114 d |= d >> 8; |
| 115 d |= d >> 16; |
| 116 if (d != UINT32_MAX) |
| 117 ++d; |
| 118 |
| 119 unaligned_write32le(next->coder->header + 1, d); |
| 120 |
| 121 // - Uncompressed size (always unknown and using EOPM) |
| 122 memset(next->coder->header + 1 + 4, 0xFF, 8); |
| 123 |
| 124 // Initialize the LZMA encoder. |
| 125 const lzma_filter_info filters[2] = { |
| 126 { |
| 127 .init = &lzma_lzma_encoder_init, |
| 128 .options = (void *)(options), |
| 129 }, { |
| 130 .init = NULL, |
| 131 } |
| 132 }; |
| 133 |
| 134 return lzma_next_filter_init(&next->coder->next, allocator, filters); |
| 135 } |
| 136 |
| 137 |
| 138 /* |
| 139 extern lzma_ret |
| 140 lzma_alone_encoder_init(lzma_next_coder *next, lzma_allocator *allocator, |
| 141 const lzma_options_alone *options) |
| 142 { |
| 143 lzma_next_coder_init(&alone_encoder_init, next, allocator, options); |
| 144 } |
| 145 */ |
| 146 |
| 147 |
| 148 extern LZMA_API(lzma_ret) |
| 149 lzma_alone_encoder(lzma_stream *strm, const lzma_options_lzma *options) |
| 150 { |
| 151 lzma_next_strm_init(alone_encoder_init, strm, options); |
| 152 |
| 153 strm->internal->supported_actions[LZMA_RUN] = true; |
| 154 strm->internal->supported_actions[LZMA_FINISH] = true; |
| 155 |
| 156 return LZMA_OK; |
| 157 } |
OLD | NEW |