]> icculus.org git repositories - icculus/xz.git/blob - src/liblzma/lzma/lzma_literal.c
Imported to git.
[icculus/xz.git] / src / liblzma / lzma / lzma_literal.c
1 ///////////////////////////////////////////////////////////////////////////////
2 //
3 /// \file       lzma_literal.c
4 /// \brief      Literal Coder
5 //
6 //  Copyright (C) 1999-2006 Igor Pavlov
7 //  Copyright (C) 2007 Lasse Collin
8 //
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.
13 //
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.
18 //
19 ///////////////////////////////////////////////////////////////////////////////
20
21 #include "lzma_literal.h"
22
23
24 extern lzma_ret
25 lzma_literal_init(lzma_literal_coder **coder, lzma_allocator *allocator,
26                 uint32_t literal_context_bits, uint32_t literal_pos_bits)
27 {
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;
32
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);
36
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);
43
44                 // Allocate a new one.
45                 *coder = lzma_alloc(sizeof(lzma_literal_coder)
46                                 + states * LIT_SIZE * sizeof(probability),
47                                 allocator);
48                 if (*coder == NULL)
49                         return LZMA_MEM_ERROR;
50
51                 // Store the new settings.
52                 (**coder).literal_context_bits = literal_context_bits;
53                 (**coder).literal_pos_bits = literal_pos_bits;
54
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;
58         }
59
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]);
64
65         return LZMA_OK;
66 }
67
68
69 extern void
70 lzma_literal_end(lzma_literal_coder **coder, lzma_allocator *allocator)
71 {
72         lzma_free(*coder, allocator);
73         *coder = NULL;
74 }