1 ///////////////////////////////////////////////////////////////////////////////
3 /// \file range_decoder.h
4 /// \brief Range Decoder
6 // Authors: Igor Pavlov
9 // This file has been put into the public domain.
10 // You can do whatever you want with this file.
12 ///////////////////////////////////////////////////////////////////////////////
14 #ifndef LZMA_RANGE_DECODER_H
15 #define LZMA_RANGE_DECODER_H
17 #include "range_common.h"
23 uint32_t init_bytes_left;
27 /// Reads the first five bytes to initialize the range decoder.
28 static inline lzma_ret
29 rc_read_init(lzma_range_decoder *rc, const uint8_t *restrict in,
30 size_t *restrict in_pos, size_t in_size)
32 while (rc->init_bytes_left > 0) {
33 if (*in_pos == in_size)
36 // The first byte is always 0x00. It could have been omitted
37 // in LZMA2 but it wasn't, so one byte is wasted in every
39 if (rc->init_bytes_left == 5 && in[*in_pos] != 0x00)
40 return LZMA_DATA_ERROR;
42 rc->code = (rc->code << 8) | in[*in_pos];
44 --rc->init_bytes_left;
47 return LZMA_STREAM_END;
51 /// Makes local copies of range decoder and *in_pos variables. Doing this
52 /// improves speed significantly. The range decoder macros expect also
53 /// variables `in' and `in_size' to be defined.
54 #define rc_to_local(range_decoder, in_pos) \
55 lzma_range_decoder rc = range_decoder; \
56 size_t rc_in_pos = (in_pos); \
60 /// Stores the local copes back to the range decoder structure.
61 #define rc_from_local(range_decoder, in_pos) \
68 /// Resets the range decoder structure.
69 #define rc_reset(range_decoder) \
71 (range_decoder).range = UINT32_MAX; \
72 (range_decoder).code = 0; \
73 (range_decoder).init_bytes_left = 5; \
77 /// When decoding has been properly finished, rc.code is always zero unless
78 /// the input stream is corrupt. So checking this can catch some corrupt
79 /// files especially if they don't have any other integrity check.
80 #define rc_is_finished(range_decoder) \
81 ((range_decoder).code == 0)
84 /// Read the next input byte if needed. If more input is needed but there is
85 /// no more input available, "goto out" is used to jump out of the main
87 #define rc_normalize(seq) \
89 if (rc.range < RC_TOP_VALUE) { \
90 if (unlikely(rc_in_pos == in_size)) { \
91 coder->sequence = seq; \
94 rc.range <<= RC_SHIFT_BITS; \
95 rc.code = (rc.code << RC_SHIFT_BITS) | in[rc_in_pos++]; \
100 /// Start decoding a bit. This must be used together with rc_update_0()
101 /// and rc_update_1():
103 /// rc_if_0(prob, seq) {
104 /// rc_update_0(prob);
107 /// rc_update_1(prob);
108 /// // Do something else
111 #define rc_if_0(prob, seq) \
113 rc_bound = (rc.range >> RC_BIT_MODEL_TOTAL_BITS) * (prob); \
114 if (rc.code < rc_bound)
117 /// Update the range decoder state and the used probability variable to
118 /// match a decoded bit of 0.
119 #define rc_update_0(prob) \
121 rc.range = rc_bound; \
122 prob += (RC_BIT_MODEL_TOTAL - (prob)) >> RC_MOVE_BITS; \
126 /// Update the range decoder state and the used probability variable to
127 /// match a decoded bit of 1.
128 #define rc_update_1(prob) \
130 rc.range -= rc_bound; \
131 rc.code -= rc_bound; \
132 prob -= (prob) >> RC_MOVE_BITS; \
136 /// Decodes one bit and runs action0 or action1 depending on the decoded bit.
137 /// This macro is used as the last step in bittree reverse decoders since
138 /// those don't use "symbol" for anything else than indexing the probability
140 #define rc_bit_last(prob, action0, action1, seq) \
142 rc_if_0(prob, seq) { \
152 /// Decodes one bit, updates "symbol", and runs action0 or action1 depending
153 /// on the decoded bit.
154 #define rc_bit(prob, action0, action1, seq) \
156 symbol <<= 1; action0, \
157 symbol = (symbol << 1) + 1; action1, \
161 /// Like rc_bit() but add "case seq:" as a prefix. This makes the unrolled
162 /// loops more readable because the code isn't littered with "case"
163 /// statements. On the other hand this also makes it less readable, since
164 /// spotting the places where the decoder loop may be restarted is less
166 #define rc_bit_case(prob, action0, action1, seq) \
167 case seq: rc_bit(prob, action0, action1, seq)
170 /// Decode a bit without using a probability.
171 #define rc_direct(dest, seq) \
175 rc.code -= rc.range; \
176 rc_bound = UINT32_C(0) - (rc.code >> 31); \
177 rc.code += rc.range & rc_bound; \
178 dest = (dest << 1) + (rc_bound + 1); \
182 // NOTE: No macros are provided for bittree decoding. It seems to be simpler
183 // to just write them open in the code.