1207753Smm///////////////////////////////////////////////////////////////////////////////
2207753Smm//
3207753Smm/// \file       auto_decoder.c
4207753Smm/// \brief      Autodetect between .xz Stream and .lzma (LZMA_Alone) formats
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 "stream_decoder.h"
14207753Smm#include "alone_decoder.h"
15207753Smm
16207753Smm
17207753Smmstruct lzma_coder_s {
18207753Smm	/// Stream decoder or LZMA_Alone decoder
19207753Smm	lzma_next_coder next;
20207753Smm
21207753Smm	uint64_t memlimit;
22207753Smm	uint32_t flags;
23207753Smm
24207753Smm	enum {
25207753Smm		SEQ_INIT,
26207753Smm		SEQ_CODE,
27207753Smm		SEQ_FINISH,
28207753Smm	} sequence;
29207753Smm};
30207753Smm
31207753Smm
32207753Smmstatic lzma_ret
33278433Srpauloauto_decode(lzma_coder *coder, const lzma_allocator *allocator,
34207753Smm		const uint8_t *restrict in, size_t *restrict in_pos,
35207753Smm		size_t in_size, uint8_t *restrict out,
36207753Smm		size_t *restrict out_pos, size_t out_size, lzma_action action)
37207753Smm{
38207753Smm	switch (coder->sequence) {
39207753Smm	case SEQ_INIT:
40207753Smm		if (*in_pos >= in_size)
41207753Smm			return LZMA_OK;
42207753Smm
43207753Smm		// Update the sequence now, because we want to continue from
44207753Smm		// SEQ_CODE even if we return some LZMA_*_CHECK.
45207753Smm		coder->sequence = SEQ_CODE;
46207753Smm
47207753Smm		// Detect the file format. For now this is simple, since if
48207753Smm		// it doesn't start with 0xFD (the first magic byte of the
49207753Smm		// new format), it has to be LZMA_Alone, or something that
50207753Smm		// we don't support at all.
51207753Smm		if (in[*in_pos] == 0xFD) {
52207753Smm			return_if_error(lzma_stream_decoder_init(
53207753Smm					&coder->next, allocator,
54207753Smm					coder->memlimit, coder->flags));
55207753Smm		} else {
56207753Smm			return_if_error(lzma_alone_decoder_init(&coder->next,
57262754Sdelphij					allocator, coder->memlimit, true));
58207753Smm
59207753Smm			// If the application wants to know about missing
60207753Smm			// integrity check or about the check in general, we
61207753Smm			// need to handle it here, because LZMA_Alone decoder
62207753Smm			// doesn't accept any flags.
63207753Smm			if (coder->flags & LZMA_TELL_NO_CHECK)
64207753Smm				return LZMA_NO_CHECK;
65207753Smm
66207753Smm			if (coder->flags & LZMA_TELL_ANY_CHECK)
67207753Smm				return LZMA_GET_CHECK;
68207753Smm		}
69207753Smm
70207753Smm	// Fall through
71207753Smm
72207753Smm	case SEQ_CODE: {
73207753Smm		const lzma_ret ret = coder->next.code(
74207753Smm				coder->next.coder, allocator,
75207753Smm				in, in_pos, in_size,
76207753Smm				out, out_pos, out_size, action);
77207753Smm		if (ret != LZMA_STREAM_END
78207753Smm				|| (coder->flags & LZMA_CONCATENATED) == 0)
79207753Smm			return ret;
80207753Smm
81207753Smm		coder->sequence = SEQ_FINISH;
82207753Smm	}
83207753Smm
84207753Smm	// Fall through
85207753Smm
86207753Smm	case SEQ_FINISH:
87207753Smm		// When LZMA_DECODE_CONCATENATED was used and we were decoding
88207753Smm		// LZMA_Alone file, we need to check check that there is no
89207753Smm		// trailing garbage and wait for LZMA_FINISH.
90207753Smm		if (*in_pos < in_size)
91207753Smm			return LZMA_DATA_ERROR;
92207753Smm
93207753Smm		return action == LZMA_FINISH ? LZMA_STREAM_END : LZMA_OK;
94207753Smm
95207753Smm	default:
96207753Smm		assert(0);
97207753Smm		return LZMA_PROG_ERROR;
98207753Smm	}
99207753Smm}
100207753Smm
101207753Smm
102207753Smmstatic void
103278433Srpauloauto_decoder_end(lzma_coder *coder, const lzma_allocator *allocator)
104207753Smm{
105207753Smm	lzma_next_end(&coder->next, allocator);
106207753Smm	lzma_free(coder, allocator);
107207753Smm	return;
108207753Smm}
109207753Smm
110207753Smm
111207753Smmstatic lzma_check
112207753Smmauto_decoder_get_check(const lzma_coder *coder)
113207753Smm{
114207753Smm	// It is LZMA_Alone if get_check is NULL.
115207753Smm	return coder->next.get_check == NULL ? LZMA_CHECK_NONE
116207753Smm			: coder->next.get_check(coder->next.coder);
117207753Smm}
118207753Smm
119207753Smm
120207753Smmstatic lzma_ret
121207753Smmauto_decoder_memconfig(lzma_coder *coder, uint64_t *memusage,
122207753Smm		uint64_t *old_memlimit, uint64_t new_memlimit)
123207753Smm{
124207753Smm	lzma_ret ret;
125207753Smm
126207753Smm	if (coder->next.memconfig != NULL) {
127207753Smm		ret = coder->next.memconfig(coder->next.coder,
128207753Smm				memusage, old_memlimit, new_memlimit);
129207753Smm		assert(*old_memlimit == coder->memlimit);
130207753Smm	} else {
131207753Smm		// No coder is configured yet. Use the base value as
132207753Smm		// the current memory usage.
133207753Smm		*memusage = LZMA_MEMUSAGE_BASE;
134207753Smm		*old_memlimit = coder->memlimit;
135207753Smm		ret = LZMA_OK;
136207753Smm	}
137207753Smm
138207753Smm	if (ret == LZMA_OK && new_memlimit != 0)
139207753Smm		coder->memlimit = new_memlimit;
140207753Smm
141207753Smm	return ret;
142207753Smm}
143207753Smm
144207753Smm
145207753Smmstatic lzma_ret
146278433Srpauloauto_decoder_init(lzma_next_coder *next, const lzma_allocator *allocator,
147207753Smm		uint64_t memlimit, uint32_t flags)
148207753Smm{
149207753Smm	lzma_next_coder_init(&auto_decoder_init, next, allocator);
150207753Smm
151207753Smm	if (memlimit == 0)
152207753Smm		return LZMA_PROG_ERROR;
153207753Smm
154207753Smm	if (flags & ~LZMA_SUPPORTED_FLAGS)
155207753Smm		return LZMA_OPTIONS_ERROR;
156207753Smm
157207753Smm	if (next->coder == NULL) {
158207753Smm		next->coder = lzma_alloc(sizeof(lzma_coder), allocator);
159207753Smm		if (next->coder == NULL)
160207753Smm			return LZMA_MEM_ERROR;
161207753Smm
162207753Smm		next->code = &auto_decode;
163207753Smm		next->end = &auto_decoder_end;
164207753Smm		next->get_check = &auto_decoder_get_check;
165207753Smm		next->memconfig = &auto_decoder_memconfig;
166207753Smm		next->coder->next = LZMA_NEXT_CODER_INIT;
167207753Smm	}
168207753Smm
169207753Smm	next->coder->memlimit = memlimit;
170207753Smm	next->coder->flags = flags;
171207753Smm	next->coder->sequence = SEQ_INIT;
172207753Smm
173207753Smm	return LZMA_OK;
174207753Smm}
175207753Smm
176207753Smm
177207753Smmextern LZMA_API(lzma_ret)
178207753Smmlzma_auto_decoder(lzma_stream *strm, uint64_t memlimit, uint32_t flags)
179207753Smm{
180207753Smm	lzma_next_strm_init(auto_decoder_init, strm, memlimit, flags);
181207753Smm
182207753Smm	strm->internal->supported_actions[LZMA_RUN] = true;
183207753Smm	strm->internal->supported_actions[LZMA_FINISH] = true;
184207753Smm
185207753Smm	return LZMA_OK;
186207753Smm}
187