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