]> icculus.org git repositories - icculus/xz.git/blob - src/liblzma/lzma/lzma_literal.h
Add limit of lc + lp <= 4. Now we can allocate the
[icculus/xz.git] / src / liblzma / lzma / lzma_literal.h
1 ///////////////////////////////////////////////////////////////////////////////
2 //
3 /// \file       lzma_literal.h
4 /// \brief      Literal Coder
5 ///
6 /// This is used as is by both LZMA encoder and decoder.
7 //
8 //  Copyright (C) 1999-2006 Igor Pavlov
9 //  Copyright (C) 2007 Lasse Collin
10 //
11 //  This library is free software; you can redistribute it and/or
12 //  modify it under the terms of the GNU Lesser General Public
13 //  License as published by the Free Software Foundation; either
14 //  version 2.1 of the License, or (at your option) any later version.
15 //
16 //  This library is distributed in the hope that it will be useful,
17 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
18 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19 //  Lesser General Public License for more details.
20 //
21 ///////////////////////////////////////////////////////////////////////////////
22
23 #ifndef LZMA_LITERAL_H
24 #define LZMA_LITERAL_H
25
26 #include "common.h"
27
28 // We need typedef of `probability'.
29 #include "range_common.h"
30
31
32 /// Each literal coder is divided in three sections:
33 ///   - 0x001-0x0FF: Without match byte
34 ///   - 0x101-0x1FF: With match byte; match bit is 0
35 ///   - 0x201-0x2FF: With match byte; match bit is 1
36 #define LIT_SIZE 0x300
37
38 /// Calculate how many states are needed. Each state has
39 /// LIT_SIZE `probability' variables.
40 #define literal_states(literal_context_bits, literal_pos_bits) \
41         (1U << ((literal_context_bits) + (literal_pos_bits)))
42
43 /// Locate the literal coder for the next literal byte. The choice depends on
44 ///   - the lowest literal_pos_bits bits of the position of the current
45 ///     byte; and
46 ///   - the highest literal_context_bits bits of the previous byte.
47 #define literal_get_subcoder(literal_coder, pos, prev_byte) \
48         (literal_coder).coders[(((pos) & (literal_coder).literal_pos_mask) \
49                         << (literal_coder).literal_context_bits) \
50                 + ((prev_byte) >> (8 - (literal_coder).literal_context_bits))]
51
52
53 typedef struct {
54         uint32_t literal_context_bits;
55         uint32_t literal_pos_bits;
56
57         /// literal_pos_mask is always (1 << literal_pos_bits) - 1.
58         uint32_t literal_pos_mask;
59
60         /// There are (1 << (literal_pos_bits + literal_context_bits))
61         /// literal coders.
62         probability coders[1 << LZMA_LITERAL_BITS_MAX][LIT_SIZE];
63
64 } lzma_literal_coder;
65
66
67 extern lzma_ret lzma_literal_init(
68                 lzma_literal_coder *coder,
69                 uint32_t literal_context_bits, uint32_t literal_pos_bits);
70
71 #endif