2 * Copyright (C) Matthieu Suiche 2008
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
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.
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.
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
39 #define __BUF_POS_CONST(buf,ofs)(((const uint8_t *)buf)+(ofs))
40 #define __PULL_BYTE(buf,ofs) \
41 ((uint8_t)((*__BUF_POS_CONST(buf,ofs)) & 0xFF))
43 #ifndef PULL_LE_UINT16
44 #define PULL_LE_UINT16(buf,ofs) ((uint16_t)( \
45 ((uint16_t)(((uint16_t)(__PULL_BYTE(buf,(ofs)+0))) << 0)) | \
46 ((uint16_t)(((uint16_t)(__PULL_BYTE(buf,(ofs)+1))) << 8)) \
50 #ifndef PULL_LE_UINT32
51 #define PULL_LE_UINT32(buf,ofs) ((uint32_t)( \
52 ((uint32_t)(((uint32_t)(__PULL_BYTE(buf,(ofs)+0))) << 0)) | \
53 ((uint32_t)(((uint32_t)(__PULL_BYTE(buf,(ofs)+1))) << 8)) | \
54 ((uint32_t)(((uint32_t)(__PULL_BYTE(buf,(ofs)+2))) << 16)) | \
55 ((uint32_t)(((uint32_t)(__PULL_BYTE(buf,(ofs)+3))) << 24)) \
59 ssize_t lzxpress_compress(const uint8_t *uncompressed,
60 uint32_t uncompressed_size,
62 uint32_t max_compressed_size)
64 uint32_t uncompressed_pos, compressed_pos, byte_left;
65 uint32_t max_offset, best_offset;
67 uint32_t max_len, len, best_len;
68 const uint8_t *str1, *str2;
71 uint32_t indic_bit, nibble_index;
73 uint32_t metadata_size;
77 if (!uncompressed_size) {
83 *(uint32_t *)compressed = 0;
84 compressed_pos = sizeof(uint32_t);
85 indic_pos = &compressed[0];
87 byte_left = uncompressed_size;
91 if (uncompressed_pos > XPRESS_BLOCK_SIZE)
97 max_offset = uncompressed_pos;
99 str1 = &uncompressed[uncompressed_pos];
104 max_offset = MIN(0x1FFF, max_offset);
106 /* search for the longest match in the window for the lookahead buffer */
107 for (offset = 1; (uint32_t)offset <= max_offset; offset++) {
108 str2 = &str1[-offset];
110 /* maximum len we can encode into metadata */
111 max_len = MIN((255 + 15 + 7 + 3), byte_left);
113 for (len = 0; (len < max_len) && (str1[len] == str2[len]); len++);
116 * We check if len is better than the value found before, including the
117 * sequence of identical bytes
119 if (len > best_len) {
122 best_offset = offset;
128 dest = (uint16_t *)&compressed[compressed_pos];
131 /* Classical meta-data */
132 metadata = (uint16_t)(((best_offset - 1) << 3) | (best_len - 3));
133 dest[metadata_size / sizeof(uint16_t)] = metadata;
134 metadata_size += sizeof(uint16_t);
136 metadata = (uint16_t)(((best_offset - 1) << 3) | 7);
137 dest[metadata_size / sizeof(uint16_t)] = metadata;
138 metadata_size = sizeof(uint16_t);
140 if (best_len < (15 + 7 + 3)) {
143 compressed[compressed_pos + metadata_size] = (best_len - (3 + 7)) & 0xF;
144 metadata_size += sizeof(uint8_t);
146 compressed[nibble_index] &= 0xF;
147 compressed[nibble_index] |= (best_len - (3 + 7)) * 16;
149 } else if (best_len < (3 + 7 + 15 + 255)) {
152 compressed[compressed_pos + metadata_size] = 15;
153 metadata_size += sizeof(uint8_t);
155 compressed[nibble_index] &= 0xF;
156 compressed[nibble_index] |= (15 * 16);
159 /* Additional best_len */
160 compressed[compressed_pos + metadata_size] = (best_len - (3 + 7 + 15)) & 0xFF;
161 metadata_size += sizeof(uint8_t);
165 compressed[compressed_pos + metadata_size] |= 15;
166 metadata_size += sizeof(uint8_t);
168 compressed[nibble_index] |= 15 << 4;
171 /* Additional best_len */
172 compressed[compressed_pos + metadata_size] = 255;
174 metadata_size += sizeof(uint8_t);
176 compressed[compressed_pos + metadata_size] = (best_len - 3) & 0xFF;
177 compressed[compressed_pos + metadata_size + 1] = ((best_len - 3) >> 8) & 0xFF;
178 metadata_size += sizeof(uint16_t);
182 indic |= 1 << (32 - ((indic_bit % 32) + 1));
185 if (nibble_index == 0) {
186 nibble_index = compressed_pos + sizeof(uint16_t);
192 compressed_pos += metadata_size;
193 uncompressed_pos += best_len;
194 byte_left -= best_len;
196 compressed[compressed_pos++] = uncompressed[uncompressed_pos++];
201 if ((indic_bit - 1) % 32 > (indic_bit % 32)) {
202 *(uint32_t *)indic_pos = indic;
204 indic_pos = &compressed[compressed_pos];
205 compressed_pos += sizeof(uint32_t);
207 } while (byte_left > 3);
210 compressed[compressed_pos] = uncompressed[uncompressed_pos];
215 if (((indic_bit - 1) % 32) > (indic_bit % 32)){
216 *(uint32_t *)indic_pos = indic;
218 indic_pos = &compressed[compressed_pos];
219 compressed_pos += sizeof(uint32_t);
221 } while (uncompressed_pos < uncompressed_size);
223 if ((indic_bit % 32) > 0) {
224 for (; (indic_bit % 32) != 0; indic_bit++)
225 indic |= 0 << (32 - ((indic_bit % 32) + 1));
227 *(uint32_t *)&compressed[compressed_pos] = 0;
228 *(uint32_t *)indic_pos = indic;
229 compressed_pos += sizeof(uint32_t);
232 return compressed_pos;
235 ssize_t lzxpress_decompress(const uint8_t *input,
238 uint32_t max_output_size)
240 uint32_t output_index, input_index;
241 uint32_t indicator, indicator_bit;
244 uint32_t nibble_index;
255 if (indicator_bit == 0) {
256 indicator = PULL_LE_UINT32(input, input_index);
257 input_index += sizeof(uint32_t);
263 * check whether the bit specified by indicator_bit is set or not
264 * set in indicator. For example, if indicator_bit has value 4
265 * check whether the 4th bit of the value in indicator is set
267 if (((indicator >> indicator_bit) & 1) == 0) {
268 output[output_index] = input[input_index];
269 input_index += sizeof(uint8_t);
270 output_index += sizeof(uint8_t);
272 length = PULL_LE_UINT16(input, input_index);
273 input_index += sizeof(uint16_t);
278 if (nibble_index == 0) {
279 nibble_index = input_index;
280 length = input[input_index] % 16;
281 input_index += sizeof(uint8_t);
283 length = input[nibble_index] / 16;
288 length = input[input_index];
289 input_index += sizeof(uint8_t);
291 length = PULL_LE_UINT16(input, input_index);
292 input_index += sizeof(uint16_t);
303 if ((output_index >= max_output_size) || ((offset + 1) > output_index)) break;
305 output[output_index] = output[output_index - offset - 1];
307 output_index += sizeof(uint8_t);
308 length -= sizeof(uint8_t);
309 } while (length != 0);
311 } while ((output_index < max_output_size) && (input_index < (input_size)));