xref: /dflybsd-src/contrib/xz/src/liblzma/rangecoder/range_decoder.h (revision 4381ed9d7ee193d719c4e4a94a9d267d177981c1)
12940b44dSPeter Avalos ///////////////////////////////////////////////////////////////////////////////
22940b44dSPeter Avalos //
32940b44dSPeter Avalos /// \file       range_decoder.h
42940b44dSPeter Avalos /// \brief      Range Decoder
52940b44dSPeter Avalos ///
62940b44dSPeter Avalos //  Authors:    Igor Pavlov
72940b44dSPeter Avalos //              Lasse Collin
82940b44dSPeter Avalos //
92940b44dSPeter Avalos //  This file has been put into the public domain.
102940b44dSPeter Avalos //  You can do whatever you want with this file.
112940b44dSPeter Avalos //
122940b44dSPeter Avalos ///////////////////////////////////////////////////////////////////////////////
132940b44dSPeter Avalos 
142940b44dSPeter Avalos #ifndef LZMA_RANGE_DECODER_H
152940b44dSPeter Avalos #define LZMA_RANGE_DECODER_H
162940b44dSPeter Avalos 
172940b44dSPeter Avalos #include "range_common.h"
182940b44dSPeter Avalos 
192940b44dSPeter Avalos 
202940b44dSPeter Avalos typedef struct {
212940b44dSPeter Avalos 	uint32_t range;
222940b44dSPeter Avalos 	uint32_t code;
232940b44dSPeter Avalos 	uint32_t init_bytes_left;
242940b44dSPeter Avalos } lzma_range_decoder;
252940b44dSPeter Avalos 
262940b44dSPeter Avalos 
272940b44dSPeter Avalos /// Reads the first five bytes to initialize the range decoder.
28*15ab8c86SJohn Marino static inline lzma_ret
rc_read_init(lzma_range_decoder * rc,const uint8_t * restrict in,size_t * restrict in_pos,size_t in_size)292940b44dSPeter Avalos rc_read_init(lzma_range_decoder *rc, const uint8_t *restrict in,
302940b44dSPeter Avalos 		size_t *restrict in_pos, size_t in_size)
312940b44dSPeter Avalos {
322940b44dSPeter Avalos 	while (rc->init_bytes_left > 0) {
332940b44dSPeter Avalos 		if (*in_pos == in_size)
34*15ab8c86SJohn Marino 			return LZMA_OK;
35*15ab8c86SJohn Marino 
36*15ab8c86SJohn Marino 		// The first byte is always 0x00. It could have been omitted
37*15ab8c86SJohn Marino 		// in LZMA2 but it wasn't, so one byte is wasted in every
38*15ab8c86SJohn Marino 		// LZMA2 chunk.
39*15ab8c86SJohn Marino 		if (rc->init_bytes_left == 5 && in[*in_pos] != 0x00)
40*15ab8c86SJohn Marino 			return LZMA_DATA_ERROR;
412940b44dSPeter Avalos 
422940b44dSPeter Avalos 		rc->code = (rc->code << 8) | in[*in_pos];
432940b44dSPeter Avalos 		++*in_pos;
442940b44dSPeter Avalos 		--rc->init_bytes_left;
452940b44dSPeter Avalos 	}
462940b44dSPeter Avalos 
47*15ab8c86SJohn Marino 	return LZMA_STREAM_END;
482940b44dSPeter Avalos }
492940b44dSPeter Avalos 
502940b44dSPeter Avalos 
512940b44dSPeter Avalos /// Makes local copies of range decoder and *in_pos variables. Doing this
522940b44dSPeter Avalos /// improves speed significantly. The range decoder macros expect also
532940b44dSPeter Avalos /// variables `in' and `in_size' to be defined.
542940b44dSPeter Avalos #define rc_to_local(range_decoder, in_pos) \
552940b44dSPeter Avalos 	lzma_range_decoder rc = range_decoder; \
562940b44dSPeter Avalos 	size_t rc_in_pos = (in_pos); \
572940b44dSPeter Avalos 	uint32_t rc_bound
582940b44dSPeter Avalos 
592940b44dSPeter Avalos 
602940b44dSPeter Avalos /// Stores the local copes back to the range decoder structure.
612940b44dSPeter Avalos #define rc_from_local(range_decoder, in_pos) \
622940b44dSPeter Avalos do { \
632940b44dSPeter Avalos 	range_decoder = rc; \
642940b44dSPeter Avalos 	in_pos = rc_in_pos; \
652940b44dSPeter Avalos } while (0)
662940b44dSPeter Avalos 
672940b44dSPeter Avalos 
682940b44dSPeter Avalos /// Resets the range decoder structure.
692940b44dSPeter Avalos #define rc_reset(range_decoder) \
702940b44dSPeter Avalos do { \
712940b44dSPeter Avalos 	(range_decoder).range = UINT32_MAX; \
722940b44dSPeter Avalos 	(range_decoder).code = 0; \
732940b44dSPeter Avalos 	(range_decoder).init_bytes_left = 5; \
742940b44dSPeter Avalos } while (0)
752940b44dSPeter Avalos 
762940b44dSPeter Avalos 
772940b44dSPeter Avalos /// When decoding has been properly finished, rc.code is always zero unless
782940b44dSPeter Avalos /// the input stream is corrupt. So checking this can catch some corrupt
792940b44dSPeter Avalos /// files especially if they don't have any other integrity check.
802940b44dSPeter Avalos #define rc_is_finished(range_decoder) \
812940b44dSPeter Avalos 	((range_decoder).code == 0)
822940b44dSPeter Avalos 
832940b44dSPeter Avalos 
842940b44dSPeter Avalos /// Read the next input byte if needed. If more input is needed but there is
852940b44dSPeter Avalos /// no more input available, "goto out" is used to jump out of the main
862940b44dSPeter Avalos /// decoder loop.
872940b44dSPeter Avalos #define rc_normalize(seq) \
882940b44dSPeter Avalos do { \
892940b44dSPeter Avalos 	if (rc.range < RC_TOP_VALUE) { \
902940b44dSPeter Avalos 		if (unlikely(rc_in_pos == in_size)) { \
912940b44dSPeter Avalos 			coder->sequence = seq; \
922940b44dSPeter Avalos 			goto out; \
932940b44dSPeter Avalos 		} \
942940b44dSPeter Avalos 		rc.range <<= RC_SHIFT_BITS; \
952940b44dSPeter Avalos 		rc.code = (rc.code << RC_SHIFT_BITS) | in[rc_in_pos++]; \
962940b44dSPeter Avalos 	} \
972940b44dSPeter Avalos } while (0)
982940b44dSPeter Avalos 
992940b44dSPeter Avalos 
1002940b44dSPeter Avalos /// Start decoding a bit. This must be used together with rc_update_0()
1012940b44dSPeter Avalos /// and rc_update_1():
1022940b44dSPeter Avalos ///
1032940b44dSPeter Avalos ///     rc_if_0(prob, seq) {
1042940b44dSPeter Avalos ///         rc_update_0(prob);
1052940b44dSPeter Avalos ///         // Do something
1062940b44dSPeter Avalos ///     } else {
1072940b44dSPeter Avalos ///         rc_update_1(prob);
1082940b44dSPeter Avalos ///         // Do something else
1092940b44dSPeter Avalos ///     }
1102940b44dSPeter Avalos ///
1112940b44dSPeter Avalos #define rc_if_0(prob, seq) \
1122940b44dSPeter Avalos 	rc_normalize(seq); \
1132940b44dSPeter Avalos 	rc_bound = (rc.range >> RC_BIT_MODEL_TOTAL_BITS) * (prob); \
1142940b44dSPeter Avalos 	if (rc.code < rc_bound)
1152940b44dSPeter Avalos 
1162940b44dSPeter Avalos 
1172940b44dSPeter Avalos /// Update the range decoder state and the used probability variable to
1182940b44dSPeter Avalos /// match a decoded bit of 0.
1192940b44dSPeter Avalos #define rc_update_0(prob) \
1202940b44dSPeter Avalos do { \
1212940b44dSPeter Avalos 	rc.range = rc_bound; \
1222940b44dSPeter Avalos 	prob += (RC_BIT_MODEL_TOTAL - (prob)) >> RC_MOVE_BITS; \
1232940b44dSPeter Avalos } while (0)
1242940b44dSPeter Avalos 
1252940b44dSPeter Avalos 
1262940b44dSPeter Avalos /// Update the range decoder state and the used probability variable to
1272940b44dSPeter Avalos /// match a decoded bit of 1.
1282940b44dSPeter Avalos #define rc_update_1(prob) \
1292940b44dSPeter Avalos do { \
1302940b44dSPeter Avalos 	rc.range -= rc_bound; \
1312940b44dSPeter Avalos 	rc.code -= rc_bound; \
1322940b44dSPeter Avalos 	prob -= (prob) >> RC_MOVE_BITS; \
1332940b44dSPeter Avalos } while (0)
1342940b44dSPeter Avalos 
1352940b44dSPeter Avalos 
1362940b44dSPeter Avalos /// Decodes one bit and runs action0 or action1 depending on the decoded bit.
1372940b44dSPeter Avalos /// This macro is used as the last step in bittree reverse decoders since
1382940b44dSPeter Avalos /// those don't use "symbol" for anything else than indexing the probability
1392940b44dSPeter Avalos /// arrays.
1402940b44dSPeter Avalos #define rc_bit_last(prob, action0, action1, seq) \
1412940b44dSPeter Avalos do { \
1422940b44dSPeter Avalos 	rc_if_0(prob, seq) { \
1432940b44dSPeter Avalos 		rc_update_0(prob); \
1442940b44dSPeter Avalos 		action0; \
1452940b44dSPeter Avalos 	} else { \
1462940b44dSPeter Avalos 		rc_update_1(prob); \
1472940b44dSPeter Avalos 		action1; \
1482940b44dSPeter Avalos 	} \
1492940b44dSPeter Avalos } while (0)
1502940b44dSPeter Avalos 
1512940b44dSPeter Avalos 
1522940b44dSPeter Avalos /// Decodes one bit, updates "symbol", and runs action0 or action1 depending
1532940b44dSPeter Avalos /// on the decoded bit.
1542940b44dSPeter Avalos #define rc_bit(prob, action0, action1, seq) \
1552940b44dSPeter Avalos 	rc_bit_last(prob, \
1562940b44dSPeter Avalos 		symbol <<= 1; action0, \
1572940b44dSPeter Avalos 		symbol = (symbol << 1) + 1; action1, \
1582940b44dSPeter Avalos 		seq);
1592940b44dSPeter Avalos 
1602940b44dSPeter Avalos 
1612940b44dSPeter Avalos /// Like rc_bit() but add "case seq:" as a prefix. This makes the unrolled
1622940b44dSPeter Avalos /// loops more readable because the code isn't littered with "case"
1632940b44dSPeter Avalos /// statements. On the other hand this also makes it less readable, since
1642940b44dSPeter Avalos /// spotting the places where the decoder loop may be restarted is less
1652940b44dSPeter Avalos /// obvious.
1662940b44dSPeter Avalos #define rc_bit_case(prob, action0, action1, seq) \
1672940b44dSPeter Avalos 	case seq: rc_bit(prob, action0, action1, seq)
1682940b44dSPeter Avalos 
1692940b44dSPeter Avalos 
1702940b44dSPeter Avalos /// Decode a bit without using a probability.
1712940b44dSPeter Avalos #define rc_direct(dest, seq) \
1722940b44dSPeter Avalos do { \
1732940b44dSPeter Avalos 	rc_normalize(seq); \
1742940b44dSPeter Avalos 	rc.range >>= 1; \
1752940b44dSPeter Avalos 	rc.code -= rc.range; \
1762940b44dSPeter Avalos 	rc_bound = UINT32_C(0) - (rc.code >> 31); \
1772940b44dSPeter Avalos 	rc.code += rc.range & rc_bound; \
1782940b44dSPeter Avalos 	dest = (dest << 1) + (rc_bound + 1); \
1792940b44dSPeter Avalos } while (0)
1802940b44dSPeter Avalos 
1812940b44dSPeter Avalos 
1822940b44dSPeter Avalos // NOTE: No macros are provided for bittree decoding. It seems to be simpler
1832940b44dSPeter Avalos // to just write them open in the code.
1842940b44dSPeter Avalos 
1852940b44dSPeter Avalos #endif
186