1 ///////////////////////////////////////////////////////////////////////////////
3 /// \file lzma_literal.c
4 /// \brief Literal Coder
6 // Copyright (C) 1999-2006 Igor Pavlov
7 // Copyright (C) 2007 Lasse Collin
9 // This library is free software; you can redistribute it and/or
10 // modify it under the terms of the GNU Lesser General Public
11 // License as published by the Free Software Foundation; either
12 // version 2.1 of the License, or (at your option) any later version.
14 // This library is distributed in the hope that it will be useful,
15 // but WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 // Lesser General Public License for more details.
19 ///////////////////////////////////////////////////////////////////////////////
21 #include "lzma_literal.h"
25 lzma_literal_init(lzma_literal_coder *coder,
26 uint32_t literal_context_bits, uint32_t literal_pos_bits)
28 // Verify that arguments are sane.
29 if (literal_context_bits > LZMA_LITERAL_CONTEXT_BITS_MAX
30 || literal_pos_bits > LZMA_LITERAL_POS_BITS_MAX)
31 return LZMA_HEADER_ERROR;
33 // Calculate the number of states the literal coder must store.
34 const uint32_t states = literal_states(
35 literal_pos_bits, literal_context_bits);
37 // Store the new settings.
38 coder->literal_context_bits = literal_context_bits;
39 coder->literal_pos_bits = literal_pos_bits;
41 // Calculate also the literal_pos_mask. It's not changed
42 // anywhere else than here.
43 coder->literal_pos_mask = (1 << literal_pos_bits) - 1;
45 // Reset the literal coder.
46 for (uint32_t i = 0; i < states; ++i)
47 for (uint32_t j = 0; j < LIT_SIZE; ++j)
48 bit_reset(coder->coders[i][j]);