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
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))
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)) \
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)) \
60 static uint32_t xpress_decompress(uint8_t *input,
65 uint32_t output_index, input_index;
66 uint32_t indicator, indicator_bit;
69 uint32_t nibble_index;
80 if (indicator_bit == 0) {
81 indicator = PULL_LE_UINT32(input, input_index);
82 input_index += sizeof(uint32_t);
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
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);
97 length = PULL_LE_UINT16(input, input_index);
98 input_index += sizeof(uint16_t);
103 if (nibble_index == 0) {
104 nibble_index = input_index;
105 length = input[input_index] % 16;
106 input_index += sizeof(uint8_t);
108 length = input[nibble_index] / 16;
113 length = input[input_index];
114 input_index += sizeof(uint8_t);
116 length = PULL_LE_UINT16(input, input_index);
117 input_index += sizeof(uint16_t);
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);
135 } while ((output_index < output_size) && (input_index < input_size));
140 uint32_t lzxpress_decompress(DATA_BLOB *inbuf,
143 return xpress_decompress(inbuf->data, inbuf->length, outbuf->data, outbuf->length);