aboutsummaryrefslogtreecommitdiff
path: root/src/liblzma/lzma/lzma_literal.c
blob: 8f650fbfaedb4ae95f1ec5499ab35558fe4be4ba (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
///////////////////////////////////////////////////////////////////////////////
//
/// \file       lzma_literal.c
/// \brief      Literal Coder
//
//  Copyright (C) 1999-2006 Igor Pavlov
//  Copyright (C) 2007 Lasse Collin
//
//  This library is free software; you can redistribute it and/or
//  modify it under the terms of the GNU Lesser General Public
//  License as published by the Free Software Foundation; either
//  version 2.1 of the License, or (at your option) any later version.
//
//  This library is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
//  Lesser General Public License for more details.
//
///////////////////////////////////////////////////////////////////////////////

#include "lzma_literal.h"


extern lzma_ret
lzma_literal_init(lzma_literal_coder **coder, lzma_allocator *allocator,
		uint32_t literal_context_bits, uint32_t literal_pos_bits)
{
	// Verify that arguments are sane.
	if (literal_context_bits > LZMA_LITERAL_CONTEXT_BITS_MAX
			|| literal_pos_bits > LZMA_LITERAL_POS_BITS_MAX)
		return LZMA_HEADER_ERROR;

	// Calculate the number of states the literal coder must store.
	const uint32_t states = literal_states(
			literal_pos_bits, literal_context_bits);

	// Allocate a new literal coder, if needed.
	if (*coder == NULL || (**coder).literal_context_bits
				!= literal_context_bits
			|| (**coder).literal_pos_bits != literal_pos_bits) {
		// Free the old coder, if any.
		lzma_free(*coder, allocator);

		// Allocate a new one.
		*coder = lzma_alloc(sizeof(lzma_literal_coder)
				+ states * LIT_SIZE * sizeof(probability),
				allocator);
		if (*coder == NULL)
			return LZMA_MEM_ERROR;

		// Store the new settings.
		(**coder).literal_context_bits = literal_context_bits;
		(**coder).literal_pos_bits = literal_pos_bits;

		// Calculate also the literal_pos_mask. It's not changed
		// anywhere else than here.
		(**coder).literal_pos_mask = (1 << literal_pos_bits) - 1;
	}

	// Reset the literal coder.
	for (uint32_t i = 0; i < states; ++i)
		for (uint32_t j = 0; j < LIT_SIZE; ++j)
			bit_reset((**coder).coders[i][j]);

	return LZMA_OK;
}


extern void
lzma_literal_end(lzma_literal_coder **coder, lzma_allocator *allocator)
{
	lzma_free(*coder, allocator);
	*coder = NULL;
}