]> icculus.org git repositories - icculus/xz.git/blob - src/liblzma/common/delta_common.c
Removed src/liblzma/common/sysdefs.h symlink, which was
[icculus/xz.git] / src / liblzma / common / 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_coder_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         // Copy Uncompressed Size which is used to limit the output size
51         // in the Delta decoder.
52         next->coder->uncompressed_size = filters[0].uncompressed_size;
53
54         // Set the delta distance.
55         if (filters[0].options == NULL)
56                 return LZMA_PROG_ERROR;
57         next->coder->distance = ((lzma_options_delta *)(filters[0].options))
58                         ->distance;
59         if (next->coder->distance < LZMA_DELTA_DISTANCE_MIN
60                         || next->coder->distance > LZMA_DELTA_DISTANCE_MAX)
61                 return LZMA_HEADER_ERROR;
62
63         // Initialize the rest of the variables.
64         next->coder->pos = 0;
65         memzero(next->coder->history, LZMA_DELTA_DISTANCE_MAX);
66
67         // Initialize the next decoder in the chain, if any.
68         return lzma_next_filter_init(&next->coder->next,
69                                 allocator, filters + 1);
70 }