libfst 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.yardopts +11 -0
- data/LICENSE +674 -0
- data/README.md +133 -0
- data/ext/extconf.rb +5 -0
- data/ext/fastlz.c +549 -0
- data/ext/fastlz.h +109 -0
- data/ext/fst_config.h +11 -0
- data/ext/fst_win_unistd.h +52 -0
- data/ext/fstapi.c +7204 -0
- data/ext/fstapi.h +466 -0
- data/ext/libfst_rb.c +2525 -0
- data/ext/lz4.c +2789 -0
- data/ext/lz4.h +868 -0
- data/lib/libfst/reader.rb +345 -0
- data/lib/libfst/tfp.rb +112 -0
- data/lib/libfst/vcd.rb +597 -0
- data/lib/libfst/version.rb +4 -0
- data/lib/libfst/writer.rb +50 -0
- data/lib/libfst.rb +6 -0
- data/libfst.gemspec +50 -0
- data/samples/create_file.rb +69 -0
- data/samples/gtkwave.png +0 -0
- data/samples/out.gtkw +46 -0
- data/samples/read2.rb +8 -0
- data/samples/read_file.rb +8 -0
- data/samples/skinny_rand.fst +0 -0
- data/samples/transaction_filter_process/full_boot.fst +0 -0
- data/samples/transaction_filter_process/full_boot.gtkw +39 -0
- data/samples/transaction_filter_process/sdcard.rb +793 -0
- data/samples/transaction_filter_process/uart.rb +141 -0
- data/samples/vcd/skinny_rand.vcd.xz +0 -0
- data/samples/vcd/vcd_read.rb +34 -0
- metadata +72 -0
data/README.md
ADDED
@@ -0,0 +1,133 @@
|
|
1
|
+
|
2
|
+
Fast Signal Trace reader/writer for Ruby
|
3
|
+
========================================
|
4
|
+
|
5
|
+
This gem is a Ruby binding to the _fstapi_ library designed by the
|
6
|
+
[GTKWave](https://github.com/gtkwave/gtkwave) authors.
|
7
|
+
It allows to read and write Fast Signal Trace (FST) format waveforms from Ruby.
|
8
|
+
|
9
|
+
FST is an open format for dumpfiles generated by EDA logic simulation tools
|
10
|
+
such as [GHDL](https://github.com/ghdl/ghdl), [nvc](https://github.com/nickg/nvc)
|
11
|
+
or [Icarus Verilog](https://steveicarus.github.io/iverilog/).
|
12
|
+
It was created by the author of [GTKWave](https://github.com/gtkwave/gtkwave)
|
13
|
+
in 2014 as an alternative to the [VCD](https://en.wikipedia.org/wiki/Value_change_dump)
|
14
|
+
(Value Change Dump) file format.
|
15
|
+
Unlike VCD, FST is a compressed binary format designed for very fast sequential
|
16
|
+
and random access.
|
17
|
+
|
18
|
+
Although the FST format and library are widely used, there is unfortunately no
|
19
|
+
documentation for the libfst library API (more details on this:
|
20
|
+
[FST API documentation · Issue #70 · gtkwave/gtkwave · GitHub](https://github.com/gtkwave/gtkwave/issues/70)).
|
21
|
+
However, an unofficial specification of the format can be found at this address: <https://blog.timhutt.co.uk/fst_spec/>.
|
22
|
+
|
23
|
+
A similar binding for Python can be found [here](https://github.com/mschlaegl/pylibfst).
|
24
|
+
|
25
|
+
|
26
|
+
Examples
|
27
|
+
--------
|
28
|
+
|
29
|
+
### Write an FST file from Ruby
|
30
|
+
|
31
|
+
```ruby
|
32
|
+
require 'libfst'
|
33
|
+
|
34
|
+
fst = LibFST::Writer.new 'out.fst'
|
35
|
+
|
36
|
+
fst.set_date_string 'Wed Apr 17 21:40:39 2024'
|
37
|
+
fst.set_version_string __FILE__
|
38
|
+
fst.set_comment 'This is a comment'
|
39
|
+
fst.set_file_type :vhdl
|
40
|
+
fst.set_time_scale :ns
|
41
|
+
|
42
|
+
fst.set_scope(:vcd_module, name: 'testbench')
|
43
|
+
clk = fst.create_variable('clk', type: :sv_logic)
|
44
|
+
fst.set_scope(:vcd_module, name: 'uart')
|
45
|
+
rx = fst.create_variable('rx', type: :sv_logic, direction: :input)
|
46
|
+
data = fst.create_variable('data', type: :sv_logic, direction: :output, length: 8)
|
47
|
+
cnt = fst.create_variable('cnt', type: :vcd_integer)
|
48
|
+
fst.set_upscope
|
49
|
+
fst.set_scope(:vcd_module, name: 'analog')
|
50
|
+
ch0 = fst.create_variable('ch0', type: :vcd_real)
|
51
|
+
fst.set_upscope
|
52
|
+
fst.set_upscope
|
53
|
+
|
54
|
+
message = 'Hello, world!'.bytes
|
55
|
+
clk_period = 1000
|
56
|
+
time = 0
|
57
|
+
fst.emit_time_change(time)
|
58
|
+
clock = 0
|
59
|
+
clk.value = clock
|
60
|
+
rx.value = 1
|
61
|
+
cnt.value = 0
|
62
|
+
counter = 0
|
63
|
+
byte = nil
|
64
|
+
bit_cnt = -2
|
65
|
+
|
66
|
+
loop do
|
67
|
+
time += clk_period
|
68
|
+
fst.emit_time_change(time)
|
69
|
+
ch0.value = 1.65 + 1.25*(Math.sin(2*Math::PI*4*time/1400000) + 0.5*(rand()-0.5))
|
70
|
+
|
71
|
+
clock ^= 1
|
72
|
+
clk.value = clock
|
73
|
+
counter += 1
|
74
|
+
if counter >= 10 then
|
75
|
+
counter = 0
|
76
|
+
bit_cnt += 1
|
77
|
+
if bit_cnt == -1 then # start bit
|
78
|
+
rx.value = 0
|
79
|
+
byte = message.shift
|
80
|
+
elsif bit_cnt < 8 then
|
81
|
+
break if byte.nil?
|
82
|
+
rx.value = byte[bit_cnt]
|
83
|
+
elsif bit_cnt == 8 then # stop bit 1
|
84
|
+
rx.value = 1
|
85
|
+
data.value = byte
|
86
|
+
else # stop bit 2
|
87
|
+
bit_cnt = -2
|
88
|
+
end
|
89
|
+
end
|
90
|
+
cnt.value = counter
|
91
|
+
end
|
92
|
+
|
93
|
+
fst.close # finalize the output file
|
94
|
+
# The writer instance should not be used after it has been closed
|
95
|
+
```
|
96
|
+
|
97
|
+
Opening the output file `out.fst` with [GTKWave](https://github.com/gtkwave/gtkwave) gives something like this:
|
98
|
+

|
99
|
+
|
100
|
+
### Read an FST file from Ruby
|
101
|
+
|
102
|
+
|
103
|
+
Install
|
104
|
+
-------
|
105
|
+
|
106
|
+
LibFST can be installed from *rubygems.org*:
|
107
|
+
```bash
|
108
|
+
gem install libfst
|
109
|
+
```
|
110
|
+
|
111
|
+
Build from sources
|
112
|
+
------------------
|
113
|
+
|
114
|
+
Package the gem file and install it:
|
115
|
+
```bash
|
116
|
+
git clone https://gitlab.ensta-bretagne.fr/bollenth/libfst.rb.git
|
117
|
+
cd libfst.rb
|
118
|
+
gem build libfst.gemspec
|
119
|
+
gem install --local ./libfst-*.gem
|
120
|
+
```
|
121
|
+
|
122
|
+
Build the documentation:
|
123
|
+
```bash
|
124
|
+
# Install Yard if you don't have it already
|
125
|
+
gem install yard
|
126
|
+
# Generate the documentation
|
127
|
+
cd libfst.rb
|
128
|
+
yard
|
129
|
+
# View the doc
|
130
|
+
firefox doc/index.html
|
131
|
+
```
|
132
|
+
|
133
|
+
|
data/ext/extconf.rb
ADDED
data/ext/fastlz.c
ADDED
@@ -0,0 +1,549 @@
|
|
1
|
+
/*
|
2
|
+
FastLZ - lightning-fast lossless compression library
|
3
|
+
|
4
|
+
Copyright (C) 2007 Ariya Hidayat (ariya@kde.org)
|
5
|
+
Copyright (C) 2006 Ariya Hidayat (ariya@kde.org)
|
6
|
+
Copyright (C) 2005 Ariya Hidayat (ariya@kde.org)
|
7
|
+
|
8
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
9
|
+
of this software and associated documentation files (the "Software"), to deal
|
10
|
+
in the Software without restriction, including without limitation the rights
|
11
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
12
|
+
copies of the Software, and to permit persons to whom the Software is
|
13
|
+
furnished to do so, subject to the following conditions:
|
14
|
+
|
15
|
+
The above copyright notice and this permission notice shall be included in
|
16
|
+
all copies or substantial portions of the Software.
|
17
|
+
|
18
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
19
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
20
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
21
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
22
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
23
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
24
|
+
THE SOFTWARE.
|
25
|
+
|
26
|
+
SPDX-License-Identifier: MIT
|
27
|
+
*/
|
28
|
+
|
29
|
+
#include "fastlz.h"
|
30
|
+
|
31
|
+
#if !defined(FASTLZ__COMPRESSOR) && !defined(FASTLZ_DECOMPRESSOR)
|
32
|
+
|
33
|
+
/*
|
34
|
+
* Always check for bound when decompressing.
|
35
|
+
* Generally it is best to leave it defined.
|
36
|
+
*/
|
37
|
+
#define FASTLZ_SAFE
|
38
|
+
|
39
|
+
|
40
|
+
/*
|
41
|
+
* Give hints to the compiler for branch prediction optimization.
|
42
|
+
*/
|
43
|
+
#if defined(__GNUC__) && (__GNUC__ > 2)
|
44
|
+
#define FASTLZ_EXPECT_CONDITIONAL(c) (__builtin_expect((c), 1))
|
45
|
+
#define FASTLZ_UNEXPECT_CONDITIONAL(c) (__builtin_expect((c), 0))
|
46
|
+
#else
|
47
|
+
#define FASTLZ_EXPECT_CONDITIONAL(c) (c)
|
48
|
+
#define FASTLZ_UNEXPECT_CONDITIONAL(c) (c)
|
49
|
+
#endif
|
50
|
+
|
51
|
+
/*
|
52
|
+
* Use inlined functions for supported systems.
|
53
|
+
*/
|
54
|
+
#if defined(__GNUC__) || defined(__DMC__) || defined(__POCC__) || defined(__WATCOMC__) || defined(__SUNPRO_C)
|
55
|
+
#define FASTLZ_INLINE inline
|
56
|
+
#elif defined(__BORLANDC__) || defined(_MSC_VER) || defined(__LCC__)
|
57
|
+
#define FASTLZ_INLINE __inline
|
58
|
+
#else
|
59
|
+
#define FASTLZ_INLINE
|
60
|
+
#endif
|
61
|
+
|
62
|
+
/*
|
63
|
+
* Prevent accessing more than 8-bit at once, except on x86 architectures.
|
64
|
+
*/
|
65
|
+
#if !defined(FASTLZ_STRICT_ALIGN)
|
66
|
+
#define FASTLZ_STRICT_ALIGN
|
67
|
+
#if defined(__i386__) || defined(__386) /* GNU C, Sun Studio */
|
68
|
+
#undef FASTLZ_STRICT_ALIGN
|
69
|
+
#elif defined(__i486__) || defined(__i586__) || defined(__i686__) || defined(__amd64) /* GNU C */
|
70
|
+
#undef FASTLZ_STRICT_ALIGN
|
71
|
+
#elif defined(_M_IX86) /* Intel, MSVC */
|
72
|
+
#undef FASTLZ_STRICT_ALIGN
|
73
|
+
#elif defined(__386)
|
74
|
+
#undef FASTLZ_STRICT_ALIGN
|
75
|
+
#elif defined(_X86_) /* MinGW */
|
76
|
+
#undef FASTLZ_STRICT_ALIGN
|
77
|
+
#elif defined(__I86__) /* Digital Mars */
|
78
|
+
#undef FASTLZ_STRICT_ALIGN
|
79
|
+
#endif
|
80
|
+
#endif
|
81
|
+
|
82
|
+
/* prototypes */
|
83
|
+
int fastlz_compress(const void* input, int length, void* output);
|
84
|
+
int fastlz_compress_level(int level, const void* input, int length, void* output);
|
85
|
+
int fastlz_decompress(const void* input, int length, void* output, int maxout);
|
86
|
+
|
87
|
+
#define MAX_COPY 32
|
88
|
+
#define MAX_LEN 264 /* 256 + 8 */
|
89
|
+
#define MAX_DISTANCE 8192
|
90
|
+
|
91
|
+
#if !defined(FASTLZ_STRICT_ALIGN)
|
92
|
+
#define FASTLZ_READU16(p) *((const flzuint16*)(p))
|
93
|
+
#else
|
94
|
+
#define FASTLZ_READU16(p) ((p)[0] | (p)[1]<<8)
|
95
|
+
#endif
|
96
|
+
|
97
|
+
#define HASH_LOG 13
|
98
|
+
#define HASH_SIZE (1<< HASH_LOG)
|
99
|
+
#define HASH_MASK (HASH_SIZE-1)
|
100
|
+
#define HASH_FUNCTION(v,p) { v = FASTLZ_READU16(p); v ^= FASTLZ_READU16(p+1)^(v>>(16-HASH_LOG));v &= HASH_MASK; }
|
101
|
+
|
102
|
+
#undef FASTLZ_LEVEL
|
103
|
+
#define FASTLZ_LEVEL 1
|
104
|
+
|
105
|
+
#undef FASTLZ_COMPRESSOR
|
106
|
+
#undef FASTLZ_DECOMPRESSOR
|
107
|
+
#define FASTLZ_COMPRESSOR fastlz1_compress
|
108
|
+
#define FASTLZ_DECOMPRESSOR fastlz1_decompress
|
109
|
+
static FASTLZ_INLINE int FASTLZ_COMPRESSOR(const void* input, int length, void* output);
|
110
|
+
static FASTLZ_INLINE int FASTLZ_DECOMPRESSOR(const void* input, int length, void* output, int maxout);
|
111
|
+
#include "fastlz.c"
|
112
|
+
|
113
|
+
#undef FASTLZ_LEVEL
|
114
|
+
#define FASTLZ_LEVEL 2
|
115
|
+
|
116
|
+
#undef MAX_DISTANCE
|
117
|
+
#define MAX_DISTANCE 8191
|
118
|
+
#define MAX_FARDISTANCE (65535+MAX_DISTANCE-1)
|
119
|
+
|
120
|
+
#undef FASTLZ_COMPRESSOR
|
121
|
+
#undef FASTLZ_DECOMPRESSOR
|
122
|
+
#define FASTLZ_COMPRESSOR fastlz2_compress
|
123
|
+
#define FASTLZ_DECOMPRESSOR fastlz2_decompress
|
124
|
+
static FASTLZ_INLINE int FASTLZ_COMPRESSOR(const void* input, int length, void* output);
|
125
|
+
static FASTLZ_INLINE int FASTLZ_DECOMPRESSOR(const void* input, int length, void* output, int maxout);
|
126
|
+
#include "fastlz.c"
|
127
|
+
|
128
|
+
int fastlz_compress(const void* input, int length, void* output)
|
129
|
+
{
|
130
|
+
/* for short block, choose fastlz1 */
|
131
|
+
if(length < 65536)
|
132
|
+
return fastlz1_compress(input, length, output);
|
133
|
+
|
134
|
+
/* else... */
|
135
|
+
return fastlz2_compress(input, length, output);
|
136
|
+
}
|
137
|
+
|
138
|
+
int fastlz_decompress(const void* input, int length, void* output, int maxout)
|
139
|
+
{
|
140
|
+
/* magic identifier for compression level */
|
141
|
+
int level = ((*(const flzuint8*)input) >> 5) + 1;
|
142
|
+
|
143
|
+
if(level == 1)
|
144
|
+
return fastlz1_decompress(input, length, output, maxout);
|
145
|
+
if(level == 2)
|
146
|
+
return fastlz2_decompress(input, length, output, maxout);
|
147
|
+
|
148
|
+
/* unknown level, trigger error */
|
149
|
+
return 0;
|
150
|
+
}
|
151
|
+
|
152
|
+
int fastlz_compress_level(int level, const void* input, int length, void* output)
|
153
|
+
{
|
154
|
+
if(level == 1)
|
155
|
+
return fastlz1_compress(input, length, output);
|
156
|
+
if(level == 2)
|
157
|
+
return fastlz2_compress(input, length, output);
|
158
|
+
|
159
|
+
return 0;
|
160
|
+
}
|
161
|
+
|
162
|
+
#else /* !defined(FASTLZ_COMPRESSOR) && !defined(FASTLZ_DECOMPRESSOR) */
|
163
|
+
|
164
|
+
static FASTLZ_INLINE int FASTLZ_COMPRESSOR(const void* input, int length, void* output)
|
165
|
+
{
|
166
|
+
const flzuint8* ip = (const flzuint8*) input;
|
167
|
+
const flzuint8* ip_bound = ip + length - 2;
|
168
|
+
const flzuint8* ip_limit = ip + length - 12;
|
169
|
+
flzuint8* op = (flzuint8*) output;
|
170
|
+
|
171
|
+
const flzuint8* htab[HASH_SIZE];
|
172
|
+
const flzuint8** hslot;
|
173
|
+
flzuint32 hval;
|
174
|
+
|
175
|
+
flzuint32 copy;
|
176
|
+
|
177
|
+
/* sanity check */
|
178
|
+
if(FASTLZ_UNEXPECT_CONDITIONAL(length < 4))
|
179
|
+
{
|
180
|
+
if(length)
|
181
|
+
{
|
182
|
+
/* create literal copy only */
|
183
|
+
*op++ = length-1;
|
184
|
+
ip_bound++;
|
185
|
+
while(ip <= ip_bound)
|
186
|
+
*op++ = *ip++;
|
187
|
+
return length+1;
|
188
|
+
}
|
189
|
+
else
|
190
|
+
return 0;
|
191
|
+
}
|
192
|
+
|
193
|
+
/* initializes hash table */
|
194
|
+
for (hslot = htab; hslot < htab + HASH_SIZE; hslot++)
|
195
|
+
*hslot = ip;
|
196
|
+
|
197
|
+
/* we start with literal copy */
|
198
|
+
copy = 2;
|
199
|
+
*op++ = MAX_COPY-1;
|
200
|
+
*op++ = *ip++;
|
201
|
+
*op++ = *ip++;
|
202
|
+
|
203
|
+
/* main loop */
|
204
|
+
while(FASTLZ_EXPECT_CONDITIONAL(ip < ip_limit))
|
205
|
+
{
|
206
|
+
const flzuint8* ref;
|
207
|
+
flzuint32 distance;
|
208
|
+
|
209
|
+
/* minimum match length */
|
210
|
+
flzuint32 len = 3;
|
211
|
+
|
212
|
+
/* comparison starting-point */
|
213
|
+
const flzuint8* anchor = ip;
|
214
|
+
|
215
|
+
/* check for a run */
|
216
|
+
#if FASTLZ_LEVEL==2
|
217
|
+
if(ip[0] == ip[-1] && FASTLZ_READU16(ip-1)==FASTLZ_READU16(ip+1))
|
218
|
+
{
|
219
|
+
distance = 1;
|
220
|
+
/* ip += 3; */ /* scan-build, never used */
|
221
|
+
ref = anchor - 1 + 3;
|
222
|
+
goto match;
|
223
|
+
}
|
224
|
+
#endif
|
225
|
+
|
226
|
+
/* find potential match */
|
227
|
+
HASH_FUNCTION(hval,ip);
|
228
|
+
hslot = htab + hval;
|
229
|
+
ref = htab[hval];
|
230
|
+
|
231
|
+
/* calculate distance to the match */
|
232
|
+
distance = anchor - ref;
|
233
|
+
|
234
|
+
/* update hash table */
|
235
|
+
*hslot = anchor;
|
236
|
+
|
237
|
+
/* is this a match? check the first 3 bytes */
|
238
|
+
if(distance==0 ||
|
239
|
+
#if FASTLZ_LEVEL==1
|
240
|
+
(distance >= MAX_DISTANCE) ||
|
241
|
+
#else
|
242
|
+
(distance >= MAX_FARDISTANCE) ||
|
243
|
+
#endif
|
244
|
+
*ref++ != *ip++ || *ref++!=*ip++ || *ref++!=*ip++)
|
245
|
+
goto literal;
|
246
|
+
|
247
|
+
#if FASTLZ_LEVEL==2
|
248
|
+
/* far, needs at least 5-byte match */
|
249
|
+
if(distance >= MAX_DISTANCE)
|
250
|
+
{
|
251
|
+
if(*ip++ != *ref++ || *ip++!= *ref++)
|
252
|
+
goto literal;
|
253
|
+
len += 2;
|
254
|
+
}
|
255
|
+
|
256
|
+
match:
|
257
|
+
#endif
|
258
|
+
|
259
|
+
/* last matched byte */
|
260
|
+
ip = anchor + len;
|
261
|
+
|
262
|
+
/* distance is biased */
|
263
|
+
distance--;
|
264
|
+
|
265
|
+
if(!distance)
|
266
|
+
{
|
267
|
+
/* zero distance means a run */
|
268
|
+
flzuint8 x = ip[-1];
|
269
|
+
while(ip < ip_bound)
|
270
|
+
if(*ref++ != x) break; else ip++;
|
271
|
+
}
|
272
|
+
else
|
273
|
+
for(;;)
|
274
|
+
{
|
275
|
+
/* safe because the outer check against ip limit */
|
276
|
+
if(*ref++ != *ip++) break;
|
277
|
+
if(*ref++ != *ip++) break;
|
278
|
+
if(*ref++ != *ip++) break;
|
279
|
+
if(*ref++ != *ip++) break;
|
280
|
+
if(*ref++ != *ip++) break;
|
281
|
+
if(*ref++ != *ip++) break;
|
282
|
+
if(*ref++ != *ip++) break;
|
283
|
+
if(*ref++ != *ip++) break;
|
284
|
+
while(ip < ip_bound)
|
285
|
+
if(*ref++ != *ip++) break;
|
286
|
+
break;
|
287
|
+
}
|
288
|
+
|
289
|
+
/* if we have copied something, adjust the copy count */
|
290
|
+
if(copy)
|
291
|
+
/* copy is biased, '0' means 1 byte copy */
|
292
|
+
*(op-copy-1) = copy-1;
|
293
|
+
else
|
294
|
+
/* back, to overwrite the copy count */
|
295
|
+
op--;
|
296
|
+
|
297
|
+
/* reset literal counter */
|
298
|
+
copy = 0;
|
299
|
+
|
300
|
+
/* length is biased, '1' means a match of 3 bytes */
|
301
|
+
ip -= 3;
|
302
|
+
len = ip - anchor;
|
303
|
+
|
304
|
+
/* encode the match */
|
305
|
+
#if FASTLZ_LEVEL==2
|
306
|
+
if(distance < MAX_DISTANCE)
|
307
|
+
{
|
308
|
+
if(len < 7)
|
309
|
+
{
|
310
|
+
*op++ = (len << 5) + (distance >> 8);
|
311
|
+
*op++ = (distance & 255);
|
312
|
+
}
|
313
|
+
else
|
314
|
+
{
|
315
|
+
*op++ = (7 << 5) + (distance >> 8);
|
316
|
+
for(len-=7; len >= 255; len-= 255)
|
317
|
+
*op++ = 255;
|
318
|
+
*op++ = len;
|
319
|
+
*op++ = (distance & 255);
|
320
|
+
}
|
321
|
+
}
|
322
|
+
else
|
323
|
+
{
|
324
|
+
/* far away, but not yet in the another galaxy... */
|
325
|
+
if(len < 7)
|
326
|
+
{
|
327
|
+
distance -= MAX_DISTANCE;
|
328
|
+
*op++ = (len << 5) + 31;
|
329
|
+
*op++ = 255;
|
330
|
+
*op++ = distance >> 8;
|
331
|
+
*op++ = distance & 255;
|
332
|
+
}
|
333
|
+
else
|
334
|
+
{
|
335
|
+
distance -= MAX_DISTANCE;
|
336
|
+
*op++ = (7 << 5) + 31;
|
337
|
+
for(len-=7; len >= 255; len-= 255)
|
338
|
+
*op++ = 255;
|
339
|
+
*op++ = len;
|
340
|
+
*op++ = 255;
|
341
|
+
*op++ = distance >> 8;
|
342
|
+
*op++ = distance & 255;
|
343
|
+
}
|
344
|
+
}
|
345
|
+
#else
|
346
|
+
|
347
|
+
if(FASTLZ_UNEXPECT_CONDITIONAL(len > MAX_LEN-2))
|
348
|
+
while(len > MAX_LEN-2)
|
349
|
+
{
|
350
|
+
*op++ = (7 << 5) + (distance >> 8);
|
351
|
+
*op++ = MAX_LEN - 2 - 7 -2;
|
352
|
+
*op++ = (distance & 255);
|
353
|
+
len -= MAX_LEN-2;
|
354
|
+
}
|
355
|
+
|
356
|
+
if(len < 7)
|
357
|
+
{
|
358
|
+
*op++ = (len << 5) + (distance >> 8);
|
359
|
+
*op++ = (distance & 255);
|
360
|
+
}
|
361
|
+
else
|
362
|
+
{
|
363
|
+
*op++ = (7 << 5) + (distance >> 8);
|
364
|
+
*op++ = len - 7;
|
365
|
+
*op++ = (distance & 255);
|
366
|
+
}
|
367
|
+
#endif
|
368
|
+
|
369
|
+
/* update the hash at match boundary */
|
370
|
+
HASH_FUNCTION(hval,ip);
|
371
|
+
htab[hval] = ip++;
|
372
|
+
HASH_FUNCTION(hval,ip);
|
373
|
+
htab[hval] = ip++;
|
374
|
+
|
375
|
+
/* assuming literal copy */
|
376
|
+
*op++ = MAX_COPY-1;
|
377
|
+
|
378
|
+
continue;
|
379
|
+
|
380
|
+
literal:
|
381
|
+
*op++ = *anchor++;
|
382
|
+
ip = anchor;
|
383
|
+
copy++;
|
384
|
+
if(FASTLZ_UNEXPECT_CONDITIONAL(copy == MAX_COPY))
|
385
|
+
{
|
386
|
+
copy = 0;
|
387
|
+
*op++ = MAX_COPY-1;
|
388
|
+
}
|
389
|
+
}
|
390
|
+
|
391
|
+
/* left-over as literal copy */
|
392
|
+
ip_bound++;
|
393
|
+
while(ip <= ip_bound)
|
394
|
+
{
|
395
|
+
*op++ = *ip++;
|
396
|
+
copy++;
|
397
|
+
if(copy == MAX_COPY)
|
398
|
+
{
|
399
|
+
copy = 0;
|
400
|
+
*op++ = MAX_COPY-1;
|
401
|
+
}
|
402
|
+
}
|
403
|
+
|
404
|
+
/* if we have copied something, adjust the copy length */
|
405
|
+
if(copy)
|
406
|
+
*(op-copy-1) = copy-1;
|
407
|
+
else
|
408
|
+
op--;
|
409
|
+
|
410
|
+
#if FASTLZ_LEVEL==2
|
411
|
+
/* marker for fastlz2 */
|
412
|
+
*(flzuint8*)output |= (1 << 5);
|
413
|
+
#endif
|
414
|
+
|
415
|
+
return op - (flzuint8*)output;
|
416
|
+
}
|
417
|
+
|
418
|
+
static FASTLZ_INLINE int FASTLZ_DECOMPRESSOR(const void* input, int length, void* output, int maxout)
|
419
|
+
{
|
420
|
+
const flzuint8* ip = (const flzuint8*) input;
|
421
|
+
const flzuint8* ip_limit = ip + length;
|
422
|
+
flzuint8* op = (flzuint8*) output;
|
423
|
+
flzuint8* op_limit = op + maxout;
|
424
|
+
flzuint32 ctrl = (*ip++) & 31;
|
425
|
+
int loop = 1;
|
426
|
+
|
427
|
+
do
|
428
|
+
{
|
429
|
+
const flzuint8* ref = op;
|
430
|
+
flzuint32 len = ctrl >> 5;
|
431
|
+
flzuint32 ofs = (ctrl & 31) << 8;
|
432
|
+
|
433
|
+
if(ctrl >= 32)
|
434
|
+
{
|
435
|
+
#if FASTLZ_LEVEL==2
|
436
|
+
flzuint8 code;
|
437
|
+
#endif
|
438
|
+
len--;
|
439
|
+
ref -= ofs;
|
440
|
+
if (len == 7-1)
|
441
|
+
#if FASTLZ_LEVEL==1
|
442
|
+
len += *ip++;
|
443
|
+
ref -= *ip++;
|
444
|
+
#else
|
445
|
+
do
|
446
|
+
{
|
447
|
+
code = *ip++;
|
448
|
+
len += code;
|
449
|
+
} while (code==255);
|
450
|
+
code = *ip++;
|
451
|
+
ref -= code;
|
452
|
+
|
453
|
+
/* match from 16-bit distance */
|
454
|
+
if(FASTLZ_UNEXPECT_CONDITIONAL(code==255))
|
455
|
+
if(FASTLZ_EXPECT_CONDITIONAL(ofs==(31 << 8)))
|
456
|
+
{
|
457
|
+
ofs = (*ip++) << 8;
|
458
|
+
ofs += *ip++;
|
459
|
+
ref = op - ofs - MAX_DISTANCE;
|
460
|
+
}
|
461
|
+
#endif
|
462
|
+
|
463
|
+
#ifdef FASTLZ_SAFE
|
464
|
+
if (FASTLZ_UNEXPECT_CONDITIONAL(op + len + 3 > op_limit))
|
465
|
+
return 0;
|
466
|
+
|
467
|
+
if (FASTLZ_UNEXPECT_CONDITIONAL(ref-1 < (flzuint8 *)output))
|
468
|
+
return 0;
|
469
|
+
#endif
|
470
|
+
|
471
|
+
if(FASTLZ_EXPECT_CONDITIONAL(ip < ip_limit))
|
472
|
+
ctrl = *ip++;
|
473
|
+
else
|
474
|
+
loop = 0;
|
475
|
+
|
476
|
+
if(ref == op)
|
477
|
+
{
|
478
|
+
/* optimize copy for a run */
|
479
|
+
flzuint8 b = ref[-1];
|
480
|
+
*op++ = b;
|
481
|
+
*op++ = b;
|
482
|
+
*op++ = b;
|
483
|
+
for(; len; --len)
|
484
|
+
*op++ = b;
|
485
|
+
}
|
486
|
+
else
|
487
|
+
{
|
488
|
+
#if !defined(FASTLZ_STRICT_ALIGN)
|
489
|
+
const flzuint16* p;
|
490
|
+
flzuint16* q;
|
491
|
+
#endif
|
492
|
+
/* copy from reference */
|
493
|
+
ref--;
|
494
|
+
*op++ = *ref++;
|
495
|
+
*op++ = *ref++;
|
496
|
+
*op++ = *ref++;
|
497
|
+
|
498
|
+
#if !defined(FASTLZ_STRICT_ALIGN)
|
499
|
+
/* copy a byte, so that now it's word aligned */
|
500
|
+
if(len & 1)
|
501
|
+
{
|
502
|
+
*op++ = *ref++;
|
503
|
+
len--;
|
504
|
+
}
|
505
|
+
|
506
|
+
/* copy 16-bit at once */
|
507
|
+
q = (flzuint16*) op;
|
508
|
+
op += len;
|
509
|
+
p = (const flzuint16*) ref;
|
510
|
+
for(len>>=1; len > 4; len-=4)
|
511
|
+
{
|
512
|
+
*q++ = *p++;
|
513
|
+
*q++ = *p++;
|
514
|
+
*q++ = *p++;
|
515
|
+
*q++ = *p++;
|
516
|
+
}
|
517
|
+
for(; len; --len)
|
518
|
+
*q++ = *p++;
|
519
|
+
#else
|
520
|
+
for(; len; --len)
|
521
|
+
*op++ = *ref++;
|
522
|
+
#endif
|
523
|
+
}
|
524
|
+
}
|
525
|
+
else
|
526
|
+
{
|
527
|
+
ctrl++;
|
528
|
+
#ifdef FASTLZ_SAFE
|
529
|
+
if (FASTLZ_UNEXPECT_CONDITIONAL(op + ctrl > op_limit))
|
530
|
+
return 0;
|
531
|
+
if (FASTLZ_UNEXPECT_CONDITIONAL(ip + ctrl > ip_limit))
|
532
|
+
return 0;
|
533
|
+
#endif
|
534
|
+
|
535
|
+
*op++ = *ip++;
|
536
|
+
for(--ctrl; ctrl; ctrl--)
|
537
|
+
*op++ = *ip++;
|
538
|
+
|
539
|
+
loop = FASTLZ_EXPECT_CONDITIONAL(ip < ip_limit);
|
540
|
+
if(loop)
|
541
|
+
ctrl = *ip++;
|
542
|
+
}
|
543
|
+
}
|
544
|
+
while(FASTLZ_EXPECT_CONDITIONAL(loop));
|
545
|
+
|
546
|
+
return op - (flzuint8*)output;
|
547
|
+
}
|
548
|
+
|
549
|
+
#endif /* !defined(FASTLZ_COMPRESSOR) && !defined(FASTLZ_DECOMPRESSOR) */
|