15a645f22SBen Gras ///////////////////////////////////////////////////////////////////////////////
25a645f22SBen Gras //
35a645f22SBen Gras /// \file range_decoder.h
45a645f22SBen Gras /// \brief Range Decoder
55a645f22SBen Gras ///
65a645f22SBen Gras // Authors: Igor Pavlov
75a645f22SBen Gras // Lasse Collin
85a645f22SBen Gras //
95a645f22SBen Gras // This file has been put into the public domain.
105a645f22SBen Gras // You can do whatever you want with this file.
115a645f22SBen Gras //
125a645f22SBen Gras ///////////////////////////////////////////////////////////////////////////////
135a645f22SBen Gras
145a645f22SBen Gras #ifndef LZMA_RANGE_DECODER_H
155a645f22SBen Gras #define LZMA_RANGE_DECODER_H
165a645f22SBen Gras
175a645f22SBen Gras #include "range_common.h"
185a645f22SBen Gras
195a645f22SBen Gras
205a645f22SBen Gras typedef struct {
215a645f22SBen Gras uint32_t range;
225a645f22SBen Gras uint32_t code;
235a645f22SBen Gras uint32_t init_bytes_left;
245a645f22SBen Gras } lzma_range_decoder;
255a645f22SBen Gras
265a645f22SBen Gras
275a645f22SBen Gras /// Reads the first five bytes to initialize the range decoder.
28*0a6a1f1dSLionel Sambuc 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)295a645f22SBen Gras rc_read_init(lzma_range_decoder *rc, const uint8_t *restrict in,
305a645f22SBen Gras size_t *restrict in_pos, size_t in_size)
315a645f22SBen Gras {
325a645f22SBen Gras while (rc->init_bytes_left > 0) {
335a645f22SBen Gras if (*in_pos == in_size)
34*0a6a1f1dSLionel Sambuc return LZMA_OK;
35*0a6a1f1dSLionel Sambuc
36*0a6a1f1dSLionel Sambuc // The first byte is always 0x00. It could have been omitted
37*0a6a1f1dSLionel Sambuc // in LZMA2 but it wasn't, so one byte is wasted in every
38*0a6a1f1dSLionel Sambuc // LZMA2 chunk.
39*0a6a1f1dSLionel Sambuc if (rc->init_bytes_left == 5 && in[*in_pos] != 0x00)
40*0a6a1f1dSLionel Sambuc return LZMA_DATA_ERROR;
415a645f22SBen Gras
425a645f22SBen Gras rc->code = (rc->code << 8) | in[*in_pos];
435a645f22SBen Gras ++*in_pos;
445a645f22SBen Gras --rc->init_bytes_left;
455a645f22SBen Gras }
465a645f22SBen Gras
47*0a6a1f1dSLionel Sambuc return LZMA_STREAM_END;
485a645f22SBen Gras }
495a645f22SBen Gras
505a645f22SBen Gras
515a645f22SBen Gras /// Makes local copies of range decoder and *in_pos variables. Doing this
525a645f22SBen Gras /// improves speed significantly. The range decoder macros expect also
535a645f22SBen Gras /// variables `in' and `in_size' to be defined.
545a645f22SBen Gras #define rc_to_local(range_decoder, in_pos) \
555a645f22SBen Gras lzma_range_decoder rc = range_decoder; \
565a645f22SBen Gras size_t rc_in_pos = (in_pos); \
575a645f22SBen Gras uint32_t rc_bound
585a645f22SBen Gras
595a645f22SBen Gras
605a645f22SBen Gras /// Stores the local copes back to the range decoder structure.
615a645f22SBen Gras #define rc_from_local(range_decoder, in_pos) \
625a645f22SBen Gras do { \
635a645f22SBen Gras range_decoder = rc; \
645a645f22SBen Gras in_pos = rc_in_pos; \
655a645f22SBen Gras } while (0)
665a645f22SBen Gras
675a645f22SBen Gras
685a645f22SBen Gras /// Resets the range decoder structure.
695a645f22SBen Gras #define rc_reset(range_decoder) \
705a645f22SBen Gras do { \
715a645f22SBen Gras (range_decoder).range = UINT32_MAX; \
725a645f22SBen Gras (range_decoder).code = 0; \
735a645f22SBen Gras (range_decoder).init_bytes_left = 5; \
745a645f22SBen Gras } while (0)
755a645f22SBen Gras
765a645f22SBen Gras
775a645f22SBen Gras /// When decoding has been properly finished, rc.code is always zero unless
785a645f22SBen Gras /// the input stream is corrupt. So checking this can catch some corrupt
795a645f22SBen Gras /// files especially if they don't have any other integrity check.
805a645f22SBen Gras #define rc_is_finished(range_decoder) \
815a645f22SBen Gras ((range_decoder).code == 0)
825a645f22SBen Gras
835a645f22SBen Gras
845a645f22SBen Gras /// Read the next input byte if needed. If more input is needed but there is
855a645f22SBen Gras /// no more input available, "goto out" is used to jump out of the main
865a645f22SBen Gras /// decoder loop.
875a645f22SBen Gras #define rc_normalize(seq) \
885a645f22SBen Gras do { \
895a645f22SBen Gras if (rc.range < RC_TOP_VALUE) { \
905a645f22SBen Gras if (unlikely(rc_in_pos == in_size)) { \
915a645f22SBen Gras coder->sequence = seq; \
925a645f22SBen Gras goto out; \
935a645f22SBen Gras } \
945a645f22SBen Gras rc.range <<= RC_SHIFT_BITS; \
955a645f22SBen Gras rc.code = (rc.code << RC_SHIFT_BITS) | in[rc_in_pos++]; \
965a645f22SBen Gras } \
975a645f22SBen Gras } while (0)
985a645f22SBen Gras
995a645f22SBen Gras
1005a645f22SBen Gras /// Start decoding a bit. This must be used together with rc_update_0()
1015a645f22SBen Gras /// and rc_update_1():
1025a645f22SBen Gras ///
1035a645f22SBen Gras /// rc_if_0(prob, seq) {
1045a645f22SBen Gras /// rc_update_0(prob);
1055a645f22SBen Gras /// // Do something
1065a645f22SBen Gras /// } else {
1075a645f22SBen Gras /// rc_update_1(prob);
1085a645f22SBen Gras /// // Do something else
1095a645f22SBen Gras /// }
1105a645f22SBen Gras ///
1115a645f22SBen Gras #define rc_if_0(prob, seq) \
1125a645f22SBen Gras rc_normalize(seq); \
1135a645f22SBen Gras rc_bound = (rc.range >> RC_BIT_MODEL_TOTAL_BITS) * (prob); \
1145a645f22SBen Gras if (rc.code < rc_bound)
1155a645f22SBen Gras
1165a645f22SBen Gras
1175a645f22SBen Gras /// Update the range decoder state and the used probability variable to
1185a645f22SBen Gras /// match a decoded bit of 0.
1195a645f22SBen Gras #define rc_update_0(prob) \
1205a645f22SBen Gras do { \
1215a645f22SBen Gras rc.range = rc_bound; \
1225a645f22SBen Gras prob += (RC_BIT_MODEL_TOTAL - (prob)) >> RC_MOVE_BITS; \
1235a645f22SBen Gras } while (0)
1245a645f22SBen Gras
1255a645f22SBen Gras
1265a645f22SBen Gras /// Update the range decoder state and the used probability variable to
1275a645f22SBen Gras /// match a decoded bit of 1.
1285a645f22SBen Gras #define rc_update_1(prob) \
1295a645f22SBen Gras do { \
1305a645f22SBen Gras rc.range -= rc_bound; \
1315a645f22SBen Gras rc.code -= rc_bound; \
1325a645f22SBen Gras prob -= (prob) >> RC_MOVE_BITS; \
1335a645f22SBen Gras } while (0)
1345a645f22SBen Gras
1355a645f22SBen Gras
1365a645f22SBen Gras /// Decodes one bit and runs action0 or action1 depending on the decoded bit.
1375a645f22SBen Gras /// This macro is used as the last step in bittree reverse decoders since
1385a645f22SBen Gras /// those don't use "symbol" for anything else than indexing the probability
1395a645f22SBen Gras /// arrays.
1405a645f22SBen Gras #define rc_bit_last(prob, action0, action1, seq) \
1415a645f22SBen Gras do { \
1425a645f22SBen Gras rc_if_0(prob, seq) { \
1435a645f22SBen Gras rc_update_0(prob); \
1445a645f22SBen Gras action0; \
1455a645f22SBen Gras } else { \
1465a645f22SBen Gras rc_update_1(prob); \
1475a645f22SBen Gras action1; \
1485a645f22SBen Gras } \
1495a645f22SBen Gras } while (0)
1505a645f22SBen Gras
1515a645f22SBen Gras
1525a645f22SBen Gras /// Decodes one bit, updates "symbol", and runs action0 or action1 depending
1535a645f22SBen Gras /// on the decoded bit.
1545a645f22SBen Gras #define rc_bit(prob, action0, action1, seq) \
1555a645f22SBen Gras rc_bit_last(prob, \
1565a645f22SBen Gras symbol <<= 1; action0, \
1575a645f22SBen Gras symbol = (symbol << 1) + 1; action1, \
1585a645f22SBen Gras seq);
1595a645f22SBen Gras
1605a645f22SBen Gras
1615a645f22SBen Gras /// Like rc_bit() but add "case seq:" as a prefix. This makes the unrolled
1625a645f22SBen Gras /// loops more readable because the code isn't littered with "case"
1635a645f22SBen Gras /// statements. On the other hand this also makes it less readable, since
1645a645f22SBen Gras /// spotting the places where the decoder loop may be restarted is less
1655a645f22SBen Gras /// obvious.
1665a645f22SBen Gras #define rc_bit_case(prob, action0, action1, seq) \
1675a645f22SBen Gras case seq: rc_bit(prob, action0, action1, seq)
1685a645f22SBen Gras
1695a645f22SBen Gras
1705a645f22SBen Gras /// Decode a bit without using a probability.
1715a645f22SBen Gras #define rc_direct(dest, seq) \
1725a645f22SBen Gras do { \
1735a645f22SBen Gras rc_normalize(seq); \
1745a645f22SBen Gras rc.range >>= 1; \
1755a645f22SBen Gras rc.code -= rc.range; \
1765a645f22SBen Gras rc_bound = UINT32_C(0) - (rc.code >> 31); \
1775a645f22SBen Gras rc.code += rc.range & rc_bound; \
1785a645f22SBen Gras dest = (dest << 1) + (rc_bound + 1); \
1795a645f22SBen Gras } while (0)
1805a645f22SBen Gras
1815a645f22SBen Gras
1825a645f22SBen Gras // NOTE: No macros are provided for bittree decoding. It seems to be simpler
1835a645f22SBen Gras // to just write them open in the code.
1845a645f22SBen Gras
1855a645f22SBen Gras #endif
186