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_decompress(const uint8_t *input,
62 uint32_t max_output_size)
64 uint32_t output_index, input_index;
65 uint32_t indicator, indicator_bit;
68 uint32_t nibble_index;
79 if (indicator_bit == 0) {
80 indicator = PULL_LE_UINT32(input, input_index);
81 input_index += sizeof(uint32_t);
87 * check whether the bit specified by indicator_bit is set or not
88 * set in indicator. For example, if indicator_bit has value 4
89 * check whether the 4th bit of the value in indicator is set
91 if (((indicator >> indicator_bit) & 1) == 0) {
92 output[output_index] = input[input_index];
93 input_index += sizeof(uint8_t);
94 output_index += sizeof(uint8_t);
96 length = PULL_LE_UINT16(input, input_index);
97 input_index += sizeof(uint16_t);
102 if (nibble_index == 0) {
103 nibble_index = input_index;
104 length = input[input_index] % 16;
105 input_index += sizeof(uint8_t);
107 length = input[nibble_index] / 16;
112 length = input[input_index];
113 input_index += sizeof(uint8_t);
115 length = PULL_LE_UINT16(input, input_index);
116 input_index += sizeof(uint16_t);
127 if ((output_index >= max_output_size) || ((offset + 1) > output_index)) break;
129 output[output_index] = output[output_index - offset - 1];
131 output_index += sizeof(uint8_t);
132 length -= sizeof(uint8_t);
133 } while (length != 0);
135 } while ((output_index < max_output_size) && (input_index < (input_size)));