]> icculus.org git repositories - icculus/xz.git/blob - src/liblzma/common/next_coder.c
Imported to git.
[icculus/xz.git] / src / liblzma / common / next_coder.c
1 ///////////////////////////////////////////////////////////////////////////////
2 //
3 /// \file       next_coder.c
4 /// \brief      Initializing and freeing the next coder in the chain
5 //
6 //  Copyright (C) 2007 Lasse Collin
7 //
8 //  This library is free software; you can redistribute it and/or
9 //  modify it under the terms of the GNU Lesser General Public
10 //  License as published by the Free Software Foundation; either
11 //  version 2.1 of the License, or (at your option) any later version.
12 //
13 //  This library is distributed in the hope that it will be useful,
14 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
15 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16 //  Lesser General Public License for more details.
17 //
18 ///////////////////////////////////////////////////////////////////////////////
19
20 #include "common.h"
21
22 extern lzma_ret
23 lzma_next_filter_init(lzma_next_coder *next, lzma_allocator *allocator,
24                 const lzma_filter_info *filters)
25 {
26         lzma_ret ret = LZMA_OK;
27
28         // Free the existing coder if it is different than the current one.
29         if ((uintptr_t)(filters[0].init) != next->init)
30                 lzma_next_coder_end(next, allocator);
31
32         if (filters[0].init != NULL) {
33                 // Initialize the new coder.
34                 ret = filters[0].init(next, allocator, filters);
35
36                 // Set the init function pointer if initialization was
37                 // successful. next->code and next->end are set by the
38                 // initialization function itself.
39                 if (ret == LZMA_OK) {
40                         next->init = (uintptr_t)(filters[0].init);
41                         assert(next->code != NULL);
42                         assert(next->end != NULL);
43                 } else {
44                         lzma_next_coder_end(next, allocator);
45                 }
46         }
47
48         return ret;
49 }
50
51
52 extern void
53 lzma_next_coder_end(lzma_next_coder *next, lzma_allocator *allocator)
54 {
55         if (next != NULL) {
56                 if (next->end != NULL)
57                         next->end(next->coder, allocator);
58
59                 // Reset the variables so the we don't accidentally think
60                 // that it is an already initialized coder.
61                 *next = LZMA_NEXT_CODER_INIT;
62         }
63
64         return;
65 }