-
-
Notifications
You must be signed in to change notification settings - Fork 4.6k
/
Copy pathlzf.cpp
373 lines (324 loc) · 10.1 KB
/
lzf.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
/*
* Software License Agreement (BSD License)
*
* Point Cloud Library (PCL) - www.pointclouds.org
* Copyright (c) 2000-2010 Marc Alexander Lehmann <schmorp@schmorp.de>
* Copyright (c) 2010-2011, Willow Garage, Inc.
*
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided
* with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
* $Id$
*
*/
#include <pcl/console/print.h>
#include <pcl/io/lzf.h>
#include <cerrno>
#include <climits>
#include <cstddef>
#include <algorithm>
/*
* Size of hashtable is (1 << HLOG) * sizeof (char *)
* decompression is independent of the hash table size
* the difference between 15 and 14 is very small
* for small blocks (and 14 is usually a bit faster).
* For a low-memory/faster configuration, use HLOG == 13;
* For best compression, use 15 or 16 (or more, up to 22).
*/
#define HLOG 13
using LZF_HSLOT = unsigned int;
using LZF_STATE = unsigned int[1 << (HLOG)];
#if !(defined(__i386) || defined (__amd64))
# define STRICT_ALIGN 1
#else
# define STRICT_ALIGN 0
#endif
#if !STRICT_ALIGN
/* for unaligned accesses we need a 16 bit datatype. */
# if USHRT_MAX == 65535
using u16 = unsigned short;
# elif UINT_MAX == 65535
using u16 = unsigned int;
# else
# undef STRICT_ALIGN
# define STRICT_ALIGN 1
# endif
#endif
// IDX works because it is very similar to a multiplicative hash, e.g.
// ((h * 57321 >> (3*8 - HLOG)) & ((1 << (HLOG)) - 1))
#define IDX(h) ((( (h) >> (3*8 - HLOG)) - (h) ) & ((1 << (HLOG)) - 1))
///////////////////////////////////////////////////////////////////////////////////////////
//
// compressed format
//
// 000LLLLL <L+1> ; literal, L+1=1..33 octets
// LLLooooo oooooooo ; backref L+1=1..7 octets, o+1=1..4096 offset
// 111ooooo LLLLLLLL oooooooo ; backref L+8 octets, o+1=1..4096 offset
//
//
unsigned int
pcl::lzfCompress (const void *const in_data, unsigned int in_len,
void *out_data, unsigned int out_len)
{
LZF_STATE htab{};
const auto *ip = static_cast<const unsigned char *> (in_data);
auto *op = static_cast<unsigned char *> (out_data);
const unsigned char *in_end = ip + in_len;
unsigned char *out_end = op + out_len;
if (!in_len || !out_len)
{
PCL_WARN ("[pcl::lzf_compress] Input or output has 0 size!\n");
return (0);
}
// Start run
int lit = 0;
op++;
unsigned int hval = (ip[0] << 8) | ip[1];
while (ip < in_end - 2)
{
unsigned int *hslot;
hval = (hval << 8) | ip[2];
hslot = htab + IDX (hval);
const unsigned char *ref = *hslot + (static_cast<const unsigned char*> (in_data));
*hslot = static_cast<unsigned int> (ip - (static_cast<const unsigned char*> (in_data)));
// off requires a type wide enough to hold a general pointer difference.
// ISO C doesn't have that (std::size_t might not be enough and ptrdiff_t only
// works for differences within a single object). We also assume that no
// no bit pattern traps. Since the only platform that is both non-POSIX
// and fails to support both assumptions is windows 64 bit, we make a
// special workaround for it.
#if defined (_WIN32) && defined (_M_X64) && defined (_MSC_VER)
// workaround for missing POSIX compliance
unsigned _int64 off;
#else
unsigned long off;
#endif
if (
// The next test will actually take care of this, but this is faster if htab is initialized
ref < ip
&& (off = ip - ref - 1) < (1 << 13)
&& ref > static_cast<const unsigned char *> (in_data)
&& ref[2] == ip[2]
#if STRICT_ALIGN
&& ((ref[1] << 8) | ref[0]) == ((ip[1] << 8) | ip[0])
#else
&& *reinterpret_cast<const u16 *> (ref) == *reinterpret_cast<const u16 *> (ip)
#endif
)
{
// Match found at *ref++
unsigned int len = 2;
std::ptrdiff_t maxlen = in_end - ip - len;
maxlen = maxlen > ((1 << 8) + (1 << 3)) ? ((1 << 8) + (1 << 3)) : maxlen;
// First a faster conservative test
if (op + 3 + 1 >= out_end)
{
// Second the exact but rare test
if (op - !lit + 3 + 1 >= out_end)
{
PCL_WARN ("[pcl::lzf_compress] Attempting to write data outside the output buffer!\n");
return (0);
}
}
// Stop run
op [- lit - 1] = static_cast<unsigned char>(lit - 1);
// Undo run if length is zero
op -= !lit;
while (true)
{
if (maxlen > 16)
{
len++; if (ref [len] != ip [len]) break;
len++; if (ref [len] != ip [len]) break;
len++; if (ref [len] != ip [len]) break;
len++; if (ref [len] != ip [len]) break;
len++; if (ref [len] != ip [len]) break;
len++; if (ref [len] != ip [len]) break;
len++; if (ref [len] != ip [len]) break;
len++; if (ref [len] != ip [len]) break;
len++; if (ref [len] != ip [len]) break;
len++; if (ref [len] != ip [len]) break;
len++; if (ref [len] != ip [len]) break;
len++; if (ref [len] != ip [len]) break;
len++; if (ref [len] != ip [len]) break;
len++; if (ref [len] != ip [len]) break;
len++; if (ref [len] != ip [len]) break;
len++; if (ref [len] != ip [len]) break;
}
do
len++;
while (len < static_cast<unsigned int> (maxlen) && ref[len] == ip[len]);
break;
}
// Len is now #octets - 1
len -= 2;
ip++;
if (len < 7)
{
*op++ = static_cast<unsigned char> ((off >> 8) + (len << 5));
}
else
{
*op++ = static_cast<unsigned char> ((off >> 8) + ( 7 << 5));
*op++ = static_cast<unsigned char> (len - 7);
}
*op++ = static_cast<unsigned char> (off);
// Start run
lit = 0; op++;
ip += len + 1;
if (ip >= in_end - 2)
break;
--ip;
hval = (ip[0] << 8) | ip[1];
hval = (hval << 8) | ip[2];
htab[IDX (hval)] = static_cast<unsigned int> (ip - (static_cast<const unsigned char *> (in_data)));
ip++;
}
else
{
// One more literal byte we must copy
if (op >= out_end)
{
PCL_WARN ("[pcl::lzf_compress] Attempting to copy data outside the output buffer!\n");
return (0);
}
lit++; *op++ = *ip++;
if (lit == (1 << 5))
{
// Stop run
op [- lit - 1] = static_cast<unsigned char> (lit - 1);
// Start run
lit = 0; op++;
}
}
}
// At most 3 bytes can be missing here
if (op + 3 > out_end)
return (0);
while (ip < in_end)
{
lit++; *op++ = *ip++;
if (lit == (1 << 5))
{
// Stop run
op [- lit - 1] = static_cast<unsigned char> (lit - 1);
// Start run
lit = 0; op++;
}
}
// End run
op [- lit - 1] = static_cast<unsigned char> (lit - 1);
// Undo run if length is zero
op -= !lit;
return (static_cast<unsigned int> (op - static_cast<unsigned char *> (out_data)));
}
///////////////////////////////////////////////////////////////////////////////////////////
unsigned int
pcl::lzfDecompress (const void *const in_data, unsigned int in_len,
void *out_data, unsigned int out_len)
{
auto const *ip = static_cast<const unsigned char *> (in_data);
auto *op = static_cast<unsigned char *> (out_data);
unsigned char const *const in_end = ip + in_len;
unsigned char *const out_end = op + out_len;
do
{
unsigned int ctrl = *ip++;
// Literal run
if (ctrl < (1 << 5))
{
ctrl++;
if (op + ctrl > out_end)
{
errno = E2BIG;
return (0);
}
// Check for overflow
if (ip + ctrl > in_end)
{
errno = EINVAL;
return (0);
}
for (unsigned ctrl_c = ctrl; ctrl_c; --ctrl_c)
*op++ = *ip++;
}
// Back reference
else
{
unsigned int len = ctrl >> 5;
unsigned char *ref = op - ((ctrl & 0x1f) << 8) - 1;
// Check for overflow
if (ip >= in_end)
{
errno = EINVAL;
return (0);
}
if (len == 7)
{
len += *ip++;
// Check for overflow
if (ip >= in_end)
{
errno = EINVAL;
return (0);
}
}
ref -= *ip++;
if (op + len + 2 > out_end)
{
errno = E2BIG;
return (0);
}
if (ref < static_cast<unsigned char *> (out_data))
{
errno = EINVAL;
return (0);
}
if (len > 9)
{
len += 2;
if (op >= ref + len)
{
// Disjunct
std::copy(ref, ref + len, op);
op += len;
}
else
{
// Overlapping, use byte by byte copying
do
*op++ = *ref++;
while (--len);
}
}
else
for (unsigned len_c = len + 2 /* case 0 iterates twice */; len_c; --len_c)
*op++ = *ref++;
}
}
while (ip < in_end);
return (static_cast<unsigned int> (op - static_cast<unsigned char*> (out_data)));
}