aboutsummaryrefslogtreecommitdiff
path: root/src/liblzma/common/next_coder.c
blob: c10fe24dba8f78396ea957c00f4a73ff86fa58ab (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
///////////////////////////////////////////////////////////////////////////////
//
/// \file       next_coder.c
/// \brief      Initializing and freeing the next coder in the chain
//
//  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 "common.h"

extern lzma_ret
lzma_next_filter_init(lzma_next_coder *next, lzma_allocator *allocator,
		const lzma_filter_info *filters)
{
	lzma_ret ret = LZMA_OK;

	// Free the existing coder if it is different than the current one.
	if ((uintptr_t)(filters[0].init) != next->init)
		lzma_next_coder_end(next, allocator);

	if (filters[0].init != NULL) {
		// Initialize the new coder.
		ret = filters[0].init(next, allocator, filters);

		// Set the init function pointer if initialization was
		// successful. next->code and next->end are set by the
		// initialization function itself.
		if (ret == LZMA_OK) {
			next->init = (uintptr_t)(filters[0].init);
			assert(next->code != NULL);
			assert(next->end != NULL);
		} else {
			lzma_next_coder_end(next, allocator);
		}
	}

	return ret;
}


extern void
lzma_next_coder_end(lzma_next_coder *next, lzma_allocator *allocator)
{
	if (next != NULL) {
		if (next->end != NULL)
			next->end(next->coder, allocator);

		// Reset the variables so the we don't accidentally think
		// that it is an already initialized coder.
		*next = LZMA_NEXT_CODER_INIT;
	}

	return;
}