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, lzma_allocator *allocator,
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 // Allocate a new literal coder, if needed.
38 if (*coder == NULL || (**coder).literal_context_bits
39 != literal_context_bits
40 || (**coder).literal_pos_bits != literal_pos_bits) {
41 // Free the old coder, if any.
42 lzma_free(*coder, allocator);
44 // Allocate a new one.
45 *coder = lzma_alloc(sizeof(lzma_literal_coder)
46 + states * LIT_SIZE * sizeof(probability),
49 return LZMA_MEM_ERROR;
51 // Store the new settings.
52 (**coder).literal_context_bits = literal_context_bits;
53 (**coder).literal_pos_bits = literal_pos_bits;
55 // Calculate also the literal_pos_mask. It's not changed
56 // anywhere else than here.
57 (**coder).literal_pos_mask = (1 << literal_pos_bits) - 1;
60 // Reset the literal coder.
61 for (uint32_t i = 0; i < states; ++i)
62 for (uint32_t j = 0; j < LIT_SIZE; ++j)
63 bit_reset((**coder).coders[i][j]);
70 lzma_literal_end(lzma_literal_coder **coder, lzma_allocator *allocator)
72 lzma_free(*coder, allocator);