zlib: mark as modified for samba
[tprouty/samba.git] / source4 / lib / compression / lzxpress.c
1 /*
2  * Copyright (C) Matthieu Suiche 2008
3  *
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  *
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * 3. Neither the name of the author nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  *
33  */
34
35 #include "includes.h"
36 #include "replace.h"
37 #include "lzxpress.h"
38
39
40 #define __BUF_POS_CONST(buf,ofs)(((const uint8_t *)buf)+(ofs))
41 #define __PULL_BYTE(buf,ofs) \
42         ((uint8_t)((*__BUF_POS_CONST(buf,ofs)) & 0xFF))
43
44 #ifndef PULL_LE_UINT16
45 #define PULL_LE_UINT16(buf,ofs) ((uint16_t)( \
46         ((uint16_t)(((uint16_t)(__PULL_BYTE(buf,(ofs)+0))) << 0)) | \
47         ((uint16_t)(((uint16_t)(__PULL_BYTE(buf,(ofs)+1))) << 8)) \
48 ))
49 #endif
50
51 #ifndef PULL_LE_UINT32
52 #define PULL_LE_UINT32(buf,ofs) ((uint32_t)( \
53         ((uint32_t)(((uint32_t)(__PULL_BYTE(buf,(ofs)+0))) <<  0)) | \
54         ((uint32_t)(((uint32_t)(__PULL_BYTE(buf,(ofs)+1))) <<  8)) | \
55         ((uint32_t)(((uint32_t)(__PULL_BYTE(buf,(ofs)+2))) << 16)) | \
56         ((uint32_t)(((uint32_t)(__PULL_BYTE(buf,(ofs)+3))) << 24)) \
57 ))
58 #endif
59
60 static uint32_t xpress_decompress(uint8_t *input,
61                                 uint32_t input_size,
62                                 uint8_t *output,
63                                 uint32_t output_size)
64 {
65         uint32_t output_index, input_index;
66         uint32_t indicator, indicator_bit;
67         uint32_t length;
68         uint32_t offset;
69         uint32_t nibble_index;
70
71         output_index = 0;
72         input_index = 0;
73         indicator = 0;
74         indicator_bit = 0;
75         length = 0;
76         offset = 0;
77         nibble_index = 0;
78
79         do {
80                 if (indicator_bit == 0) {
81                         indicator = PULL_LE_UINT32(input, input_index);
82                         input_index += sizeof(uint32_t);
83                         indicator_bit = 32;
84                 }
85                 indicator_bit--;
86
87                 /*
88                  * check whether the bit specified by indicator_bit is set or not
89                  * set in indicator. For example, if indicator_bit has value 4
90                  * check whether the 4th bit of the value in indicator is set
91                  */
92                 if (((indicator >> indicator_bit) & 1) == 0) {
93                         output[output_index] = input[input_index];
94                         input_index += sizeof(uint8_t);
95                         output_index += sizeof(uint8_t);
96                 } else {
97                         length = PULL_LE_UINT16(input, input_index);
98                         input_index += sizeof(uint16_t);
99                         offset = length / 8;
100                         length = length % 8;
101
102                         if (length == 7) {
103                                 if (nibble_index == 0) {
104                                         nibble_index = input_index;
105                                         length = input[input_index] % 16;
106                                         input_index += sizeof(uint8_t);
107                                 } else {
108                                         length = input[nibble_index] / 16;
109                                         nibble_index = 0;
110                                 }
111
112                                 if (length == 15) {
113                                         length = input[input_index];
114                                         input_index += sizeof(uint8_t);
115                                                 if (length == 255) {
116                                                         length = PULL_LE_UINT16(input, input_index);
117                                                         input_index += sizeof(uint16_t);
118                                                         length -= (15 + 7);
119                                                 }
120                                         length += 15;
121                                 }
122                                 length += 7;
123                         }
124
125                         length += 3;
126
127                         do {
128                                 if (output_index >= output_size) break;
129                                 output[output_index] = output[output_index - offset - 1];
130                                 output_index += sizeof(uint8_t);
131                                 length -= sizeof(uint8_t);
132                         } while (length != 0);
133                 }
134
135         } while ((output_index < output_size) && (input_index < input_size));
136
137         return output_index;
138 }
139
140 uint32_t lzxpress_decompress(DATA_BLOB *inbuf,
141                                 DATA_BLOB *outbuf)
142 {
143         return xpress_decompress(inbuf->data, inbuf->length, outbuf->data, outbuf->length);
144 }