]> icculus.org git repositories - icculus/xz.git/blob - src/liblzma/delta/delta_common.c
Renamed constants:
[icculus/xz.git] / src / liblzma / delta / delta_common.c
1 ///////////////////////////////////////////////////////////////////////////////
2 //
3 /// \file       delta_common.c
4 /// \brief      Common stuff for Delta encoder and decoder
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 "delta_common.h"
21
22
23 static void
24 delta_coder_end(lzma_coder *coder, lzma_allocator *allocator)
25 {
26         lzma_next_end(&coder->next, allocator);
27         lzma_free(coder, allocator);
28         return;
29 }
30
31
32 extern lzma_ret
33 lzma_delta_coder_init(lzma_next_coder *next, lzma_allocator *allocator,
34                 const lzma_filter_info *filters, lzma_code_function code)
35 {
36         // Allocate memory for the decoder if needed.
37         if (next->coder == NULL) {
38                 next->coder = lzma_alloc(sizeof(lzma_coder), allocator);
39                 if (next->coder == NULL)
40                         return LZMA_MEM_ERROR;
41
42                 // End function is the same for encoder and decoder.
43                 next->end = &delta_coder_end;
44                 next->coder->next = LZMA_NEXT_CODER_INIT;
45         }
46
47         // Coding function is different for encoder and decoder.
48         next->code = code;
49
50         // Set the delta distance.
51         if (filters[0].options == NULL)
52                 return LZMA_PROG_ERROR;
53         next->coder->distance = ((lzma_options_delta *)(filters[0].options))
54                         ->distance;
55         if (next->coder->distance < LZMA_DELTA_DISTANCE_MIN
56                         || next->coder->distance > LZMA_DELTA_DISTANCE_MAX)
57                 return LZMA_OPTIONS_ERROR;
58
59         // Initialize the rest of the variables.
60         next->coder->pos = 0;
61         memzero(next->coder->history, LZMA_DELTA_DISTANCE_MAX);
62
63         // Initialize the next decoder in the chain, if any.
64         return lzma_next_filter_init(&next->coder->next,
65                                 allocator, filters + 1);
66 }