yaz0 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +18 -0
- data/.rspec +3 -0
- data/.travis.yml +6 -0
- data/Gemfile +8 -0
- data/Gemfile.lock +37 -0
- data/LICENSE.txt +21 -0
- data/README.md +18 -0
- data/Rakefile +14 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/ext/yaz0/buffer.c +29 -0
- data/ext/yaz0/compress.c +319 -0
- data/ext/yaz0/decompress.c +64 -0
- data/ext/yaz0/extconf.rb +3 -0
- data/ext/yaz0/yaz0.c +48 -0
- data/ext/yaz0/yaz0.h +38 -0
- data/lib/yaz0.rb +7 -0
- data/lib/yaz0/version.rb +3 -0
- data/yaz0.gemspec +24 -0
- metadata +64 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: ebd330069f0f8a3ec7830485d1b91d3be87f32c72374bfdd68ed3af43730ea7b
|
4
|
+
data.tar.gz: bfb1a68354eb81b6649817f8560e1692236ed384cdcd4780c8434b83348e8c9d
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: eae386ef7038fa29f9f46473255c86c5b7c97078cbd5515f93d7d01434d38ba06b6d42779b3f230911aa80d660452e1673c3cd7c5d9e1e944b2d0572f2f6f2fb
|
7
|
+
data.tar.gz: 3e31817e2ab96530e91fda4dfa7881d4571b5d946d64f50dd3ab54e872f68243adf3750b07b88d1e1ffbc0b9ed931712c7d31e04c9e73cb12e09452ec17b4592
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,37 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
yaz0 (0.1.0)
|
5
|
+
|
6
|
+
GEM
|
7
|
+
remote: https://rubygems.org/
|
8
|
+
specs:
|
9
|
+
diff-lcs (1.3)
|
10
|
+
rake (12.3.3)
|
11
|
+
rake-compiler (1.1.0)
|
12
|
+
rake
|
13
|
+
rspec (3.9.0)
|
14
|
+
rspec-core (~> 3.9.0)
|
15
|
+
rspec-expectations (~> 3.9.0)
|
16
|
+
rspec-mocks (~> 3.9.0)
|
17
|
+
rspec-core (3.9.2)
|
18
|
+
rspec-support (~> 3.9.3)
|
19
|
+
rspec-expectations (3.9.2)
|
20
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
21
|
+
rspec-support (~> 3.9.0)
|
22
|
+
rspec-mocks (3.9.1)
|
23
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
24
|
+
rspec-support (~> 3.9.0)
|
25
|
+
rspec-support (3.9.3)
|
26
|
+
|
27
|
+
PLATFORMS
|
28
|
+
x64-mingw32
|
29
|
+
|
30
|
+
DEPENDENCIES
|
31
|
+
rake (~> 12.0)
|
32
|
+
rake-compiler
|
33
|
+
rspec (~> 3.0)
|
34
|
+
yaz0!
|
35
|
+
|
36
|
+
BUNDLED WITH
|
37
|
+
2.1.4
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2020 Maxime Bacoux
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
# Yaz0
|
2
|
+
|
3
|
+
A simple gem to compress/decompress data using the Yaz0 compression algorithm.
|
4
|
+
|
5
|
+
## Usage
|
6
|
+
|
7
|
+
require 'yaz0'
|
8
|
+
|
9
|
+
Yaz0.decompress(Yaz0.compress("Hello, world!")) == "Hello, world!"
|
10
|
+
|
11
|
+
## Contributing
|
12
|
+
|
13
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/Nax/ruby-yaz0.
|
14
|
+
|
15
|
+
|
16
|
+
## License
|
17
|
+
|
18
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
2
|
+
require "rspec/core/rake_task"
|
3
|
+
|
4
|
+
RSpec::Core::RakeTask.new(:spec)
|
5
|
+
|
6
|
+
require "rake/extensiontask"
|
7
|
+
|
8
|
+
task :build => :compile
|
9
|
+
|
10
|
+
Rake::ExtensionTask.new("yaz0") do |ext|
|
11
|
+
ext.lib_dir = "lib/yaz0"
|
12
|
+
end
|
13
|
+
|
14
|
+
task :default => [:clobber, :compile, :spec]
|
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "yaz0"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
data/ext/yaz0/buffer.c
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
#include <stdlib.h>
|
2
|
+
#include <string.h>
|
3
|
+
#include <stdio.h>
|
4
|
+
#include "yaz0.h"
|
5
|
+
|
6
|
+
void yaz0BufferAlloc(Yaz0Buffer *buf, size_t cap)
|
7
|
+
{
|
8
|
+
buf->size = 0;
|
9
|
+
buf->capacity = cap;
|
10
|
+
buf->data = malloc(cap);
|
11
|
+
}
|
12
|
+
|
13
|
+
void yaz0BufferFree(Yaz0Buffer *buf)
|
14
|
+
{
|
15
|
+
free(buf->data);
|
16
|
+
buf->data = NULL;
|
17
|
+
}
|
18
|
+
|
19
|
+
void yaz0BufferWrite(Yaz0Buffer *buf, const void *data, size_t len)
|
20
|
+
{
|
21
|
+
while (buf->size + len > buf->capacity)
|
22
|
+
{
|
23
|
+
buf->capacity = buf->capacity + buf->capacity / 2;
|
24
|
+
buf->data = realloc(buf->data, buf->capacity);
|
25
|
+
}
|
26
|
+
|
27
|
+
memcpy(buf->data + buf->size, data, len);
|
28
|
+
buf->size += len;
|
29
|
+
}
|
data/ext/yaz0/compress.c
ADDED
@@ -0,0 +1,319 @@
|
|
1
|
+
/*
|
2
|
+
* Yaz0
|
3
|
+
*
|
4
|
+
* Compress yaz0 files
|
5
|
+
*/
|
6
|
+
|
7
|
+
#include <stdio.h>
|
8
|
+
#include <stdlib.h>
|
9
|
+
#include <stdint.h>
|
10
|
+
#include <string.h>
|
11
|
+
|
12
|
+
#include "yaz0.h"
|
13
|
+
|
14
|
+
#define RUN_NONE 0
|
15
|
+
#define RUN_DATA 1
|
16
|
+
#define RUN_REF 2
|
17
|
+
|
18
|
+
typedef struct
|
19
|
+
{
|
20
|
+
uint8_t type;
|
21
|
+
union {
|
22
|
+
uint8_t data;
|
23
|
+
struct
|
24
|
+
{
|
25
|
+
uint16_t offset;
|
26
|
+
uint16_t len;
|
27
|
+
};
|
28
|
+
};
|
29
|
+
} Yaz0Run;
|
30
|
+
|
31
|
+
static void makeRunNone(Yaz0Run *r)
|
32
|
+
{
|
33
|
+
r->type = RUN_NONE;
|
34
|
+
}
|
35
|
+
|
36
|
+
static void makeRunData(Yaz0Run *r, uint8_t data)
|
37
|
+
{
|
38
|
+
r->type = RUN_DATA;
|
39
|
+
r->data = data;
|
40
|
+
}
|
41
|
+
|
42
|
+
static void makeRunRef(Yaz0Run *r, uint16_t offset, uint16_t len)
|
43
|
+
{
|
44
|
+
r->type = RUN_REF;
|
45
|
+
r->offset = offset;
|
46
|
+
r->len = len;
|
47
|
+
}
|
48
|
+
|
49
|
+
typedef struct
|
50
|
+
{
|
51
|
+
Yaz0Run r[8];
|
52
|
+
uint32_t len;
|
53
|
+
uint8_t group;
|
54
|
+
} Yaz0Chunk;
|
55
|
+
|
56
|
+
static void makeChunkEmpty(Yaz0Chunk *c)
|
57
|
+
{
|
58
|
+
for (int i = 0; i < 8; ++i)
|
59
|
+
{
|
60
|
+
makeRunNone(&(c->r[i]));
|
61
|
+
}
|
62
|
+
c->len = 0;
|
63
|
+
c->group = 0;
|
64
|
+
}
|
65
|
+
|
66
|
+
typedef struct
|
67
|
+
{
|
68
|
+
Yaz0Buffer *dst;
|
69
|
+
const char *data;
|
70
|
+
uint32_t dataSize;
|
71
|
+
uint32_t inCursor;
|
72
|
+
} Yaz0Compressor;
|
73
|
+
|
74
|
+
static int runCost(const Yaz0Run *run)
|
75
|
+
{
|
76
|
+
switch (run->type)
|
77
|
+
{
|
78
|
+
case RUN_NONE:
|
79
|
+
return 1000;
|
80
|
+
case RUN_DATA:
|
81
|
+
return 0;
|
82
|
+
case RUN_REF:
|
83
|
+
return ((run->len >= 0x12) ? 3 : 2) - run->len;
|
84
|
+
}
|
85
|
+
return 0;
|
86
|
+
}
|
87
|
+
|
88
|
+
static int chunkCost(const Yaz0Chunk *ch)
|
89
|
+
{
|
90
|
+
int acc;
|
91
|
+
|
92
|
+
acc = 0;
|
93
|
+
for (int i = 0; i < 8; ++i)
|
94
|
+
{
|
95
|
+
acc += runCost(ch->r + i);
|
96
|
+
}
|
97
|
+
|
98
|
+
return acc;
|
99
|
+
}
|
100
|
+
|
101
|
+
static void bestChunk(Yaz0Chunk *dst, const Yaz0Chunk *a, const Yaz0Chunk *b)
|
102
|
+
{
|
103
|
+
const Yaz0Chunk *src = chunkCost(a) <= chunkCost(b) ? a : b;
|
104
|
+
if (src != dst)
|
105
|
+
{
|
106
|
+
memcpy(dst, src, sizeof(*dst));
|
107
|
+
}
|
108
|
+
}
|
109
|
+
|
110
|
+
static void bestRun(Yaz0Run *dst, const Yaz0Run *a, const Yaz0Run *b)
|
111
|
+
{
|
112
|
+
const Yaz0Run *src = (runCost(a) <= runCost(b)) ? a : b;
|
113
|
+
if (src != dst)
|
114
|
+
{
|
115
|
+
memcpy(dst, src, sizeof(*dst));
|
116
|
+
}
|
117
|
+
}
|
118
|
+
|
119
|
+
static uint32_t runLength(const Yaz0Run *r)
|
120
|
+
{
|
121
|
+
switch (r->type)
|
122
|
+
{
|
123
|
+
case RUN_NONE:
|
124
|
+
return 0;
|
125
|
+
case RUN_DATA:
|
126
|
+
return 1;
|
127
|
+
case RUN_REF:
|
128
|
+
return r->len;
|
129
|
+
}
|
130
|
+
return 0;
|
131
|
+
}
|
132
|
+
|
133
|
+
static int makeMatchPattern(Yaz0Run *run, Yaz0Compressor *compressor, int len)
|
134
|
+
{
|
135
|
+
const char *pattern = compressor->data + compressor->inCursor;
|
136
|
+
int cursorBase = (int)compressor->inCursor - 0x1000;
|
137
|
+
int cursorMax = (int)compressor->inCursor - len;
|
138
|
+
|
139
|
+
if (compressor->inCursor + len > compressor->dataSize)
|
140
|
+
return 0;
|
141
|
+
|
142
|
+
if (cursorBase < 0)
|
143
|
+
cursorBase = 0;
|
144
|
+
|
145
|
+
if (cursorMax < 0)
|
146
|
+
return 0;
|
147
|
+
|
148
|
+
for (int i = cursorMax; i >= cursorBase; --i)
|
149
|
+
{
|
150
|
+
if (memcmp(compressor->data + i, pattern, len) == 0)
|
151
|
+
{
|
152
|
+
makeRunRef(run, compressor->inCursor - i, len);
|
153
|
+
return 1;
|
154
|
+
}
|
155
|
+
}
|
156
|
+
return 0;
|
157
|
+
}
|
158
|
+
|
159
|
+
static void refineMatch(Yaz0Compressor *compressor, Yaz0Run *run)
|
160
|
+
{
|
161
|
+
if (run->type != RUN_REF)
|
162
|
+
return;
|
163
|
+
|
164
|
+
if (run->offset != run->len)
|
165
|
+
return;
|
166
|
+
|
167
|
+
int initialLen = run->len;
|
168
|
+
|
169
|
+
for (;;)
|
170
|
+
{
|
171
|
+
if (run->len == 0x111)
|
172
|
+
return;
|
173
|
+
if (compressor->inCursor + run->len == compressor->dataSize)
|
174
|
+
return;
|
175
|
+
if (compressor->data[compressor->inCursor + run->len] != compressor->data[compressor->inCursor + (run->len % initialLen)])
|
176
|
+
return;
|
177
|
+
run->len++;
|
178
|
+
}
|
179
|
+
}
|
180
|
+
|
181
|
+
static void makeMatch(Yaz0Run *dst, Yaz0Compressor *compressor)
|
182
|
+
{
|
183
|
+
Yaz0Run run;
|
184
|
+
|
185
|
+
for (int i = 1; i <= 0x111; ++i)
|
186
|
+
{
|
187
|
+
if (!makeMatchPattern(&run, compressor, i))
|
188
|
+
break;
|
189
|
+
refineMatch(compressor, &run);
|
190
|
+
bestRun(dst, &run, dst);
|
191
|
+
}
|
192
|
+
}
|
193
|
+
|
194
|
+
static void makeRun(Yaz0Run *dst, Yaz0Compressor *compressor, int dataOnly)
|
195
|
+
{
|
196
|
+
Yaz0Run tmp;
|
197
|
+
|
198
|
+
makeRunNone(dst);
|
199
|
+
if (compressor->inCursor == compressor->dataSize)
|
200
|
+
return;
|
201
|
+
/* Get the basic data run */
|
202
|
+
makeRunData(dst, compressor->data[compressor->inCursor]);
|
203
|
+
if (!dataOnly)
|
204
|
+
{
|
205
|
+
makeRunNone(&tmp);
|
206
|
+
makeMatch(&tmp, compressor);
|
207
|
+
bestRun(dst, &tmp, dst);
|
208
|
+
}
|
209
|
+
}
|
210
|
+
|
211
|
+
static void writeRun(Yaz0Compressor *c, const Yaz0Run *r)
|
212
|
+
{
|
213
|
+
uint8_t tmp8;
|
214
|
+
uint16_t tmp16;
|
215
|
+
|
216
|
+
switch (r->type)
|
217
|
+
{
|
218
|
+
case RUN_NONE:
|
219
|
+
break;
|
220
|
+
case RUN_DATA:
|
221
|
+
yaz0BufferWrite(c->dst, &r->data, 1);
|
222
|
+
break;
|
223
|
+
case RUN_REF:
|
224
|
+
tmp16 = (r->offset - 1);
|
225
|
+
if (r->len <= 0x11)
|
226
|
+
{
|
227
|
+
tmp16 |= ((((uint16_t)r->len) - 2) << 12);
|
228
|
+
tmp16 = swap16(tmp16);
|
229
|
+
yaz0BufferWrite(c->dst, &tmp16, 2);
|
230
|
+
}
|
231
|
+
else
|
232
|
+
{
|
233
|
+
tmp16 = swap16(tmp16);
|
234
|
+
yaz0BufferWrite(c->dst, &tmp16, 2);
|
235
|
+
tmp8 = (r->len - 0x12);
|
236
|
+
yaz0BufferWrite(c->dst, &tmp8, 1);
|
237
|
+
}
|
238
|
+
break;
|
239
|
+
}
|
240
|
+
}
|
241
|
+
|
242
|
+
static void makeChunkCandidate(Yaz0Chunk *dst, Yaz0Compressor *c, uint8_t mask)
|
243
|
+
{
|
244
|
+
uint32_t cursor;
|
245
|
+
|
246
|
+
cursor = c->inCursor;
|
247
|
+
makeChunkEmpty(dst);
|
248
|
+
for (int i = 0; i < 8; ++i)
|
249
|
+
{
|
250
|
+
makeRun(dst->r + i, c, mask & (1 << (7 - i)));
|
251
|
+
if (dst->r[i].type == RUN_DATA)
|
252
|
+
{
|
253
|
+
dst->group |= (1 << (7 - i));
|
254
|
+
}
|
255
|
+
c->inCursor += runLength(dst->r + i);
|
256
|
+
dst->len += runLength(dst->r + i);
|
257
|
+
}
|
258
|
+
|
259
|
+
c->inCursor = cursor;
|
260
|
+
}
|
261
|
+
|
262
|
+
static void makeChunk(Yaz0Compressor *c)
|
263
|
+
{
|
264
|
+
Yaz0Chunk ch;
|
265
|
+
Yaz0Chunk ch2;
|
266
|
+
|
267
|
+
makeChunkCandidate(&ch, c, 0x00);
|
268
|
+
makeChunkCandidate(&ch2, c, 0x01);
|
269
|
+
bestChunk(&ch, &ch, &ch2);
|
270
|
+
makeChunkCandidate(&ch2, c, 0x02);
|
271
|
+
bestChunk(&ch, &ch, &ch2);
|
272
|
+
makeChunkCandidate(&ch2, c, 0x04);
|
273
|
+
bestChunk(&ch, &ch, &ch2);
|
274
|
+
makeChunkCandidate(&ch2, c, 0x08);
|
275
|
+
bestChunk(&ch, &ch, &ch2);
|
276
|
+
makeChunkCandidate(&ch2, c, 0x10);
|
277
|
+
bestChunk(&ch, &ch, &ch2);
|
278
|
+
makeChunkCandidate(&ch2, c, 0x20);
|
279
|
+
bestChunk(&ch, &ch, &ch2);
|
280
|
+
makeChunkCandidate(&ch2, c, 0x40);
|
281
|
+
bestChunk(&ch, &ch, &ch2);
|
282
|
+
makeChunkCandidate(&ch2, c, 0x80);
|
283
|
+
bestChunk(&ch, &ch, &ch2);
|
284
|
+
|
285
|
+
yaz0BufferWrite(c->dst, &ch.group, 1);
|
286
|
+
for (int i = 0; i < 8; ++i)
|
287
|
+
{
|
288
|
+
writeRun(c, ch.r + i);
|
289
|
+
}
|
290
|
+
c->inCursor += ch.len;
|
291
|
+
}
|
292
|
+
|
293
|
+
static void writeHeader(Yaz0Compressor *c)
|
294
|
+
{
|
295
|
+
uint32_t tmp;
|
296
|
+
|
297
|
+
yaz0BufferWrite(c->dst, "Yaz0", 4);
|
298
|
+
tmp = swap32(c->dataSize);
|
299
|
+
yaz0BufferWrite(c->dst, &tmp, 4);
|
300
|
+
tmp = 0;
|
301
|
+
yaz0BufferWrite(c->dst, &tmp, 4);
|
302
|
+
yaz0BufferWrite(c->dst, &tmp, 4);
|
303
|
+
}
|
304
|
+
|
305
|
+
int yaz0Compress(Yaz0Buffer *dst, const char *data, size_t len)
|
306
|
+
{
|
307
|
+
Yaz0Compressor compressor;
|
308
|
+
|
309
|
+
compressor.dst = dst;
|
310
|
+
compressor.data = data;
|
311
|
+
compressor.dataSize = len;
|
312
|
+
compressor.inCursor = 0;
|
313
|
+
|
314
|
+
writeHeader(&compressor);
|
315
|
+
|
316
|
+
while (compressor.inCursor < compressor.dataSize)
|
317
|
+
makeChunk(&compressor);
|
318
|
+
return 1;
|
319
|
+
}
|
@@ -0,0 +1,64 @@
|
|
1
|
+
#include <stdint.h>
|
2
|
+
#include <stdlib.h>
|
3
|
+
#include <string.h>
|
4
|
+
#include "yaz0.h"
|
5
|
+
|
6
|
+
int yaz0Decompress(Yaz0Buffer *dst, const char *d, size_t dataLen)
|
7
|
+
{
|
8
|
+
const uint8_t *data;
|
9
|
+
uint32_t dstSize;
|
10
|
+
unsigned inCursor;
|
11
|
+
uint8_t group;
|
12
|
+
int len;
|
13
|
+
int rrr;
|
14
|
+
|
15
|
+
data = d;
|
16
|
+
if (dataLen < 0x10)
|
17
|
+
return 0;
|
18
|
+
if (memcmp(data, "Yaz0", 4) != 0)
|
19
|
+
return 0;
|
20
|
+
|
21
|
+
dstSize = *(uint32_t *)(data + 4);
|
22
|
+
dstSize = swap32(dstSize);
|
23
|
+
|
24
|
+
inCursor = 0x10;
|
25
|
+
dst->data = realloc(dst->data, dstSize);
|
26
|
+
|
27
|
+
while (inCursor < dataLen)
|
28
|
+
{
|
29
|
+
group = data[inCursor++];
|
30
|
+
for (int b = 0; b < 8; ++b)
|
31
|
+
{
|
32
|
+
if (inCursor >= dataLen)
|
33
|
+
break;
|
34
|
+
|
35
|
+
if (group & (1 << (7 - b)))
|
36
|
+
{
|
37
|
+
/* Direct data */
|
38
|
+
dst->data[dst->size++] = data[inCursor++];
|
39
|
+
}
|
40
|
+
else
|
41
|
+
{
|
42
|
+
if (data[inCursor] & 0xf0)
|
43
|
+
{
|
44
|
+
len = (data[inCursor] >> 4) + 2;
|
45
|
+
rrr = (((data[inCursor] & 0xf) << 8) | (data[inCursor + 1])) + 1;
|
46
|
+
inCursor += 2;
|
47
|
+
}
|
48
|
+
else
|
49
|
+
{
|
50
|
+
rrr = (((data[inCursor] & 0xf) << 8) | (data[inCursor + 1])) + 1;
|
51
|
+
len = (data[inCursor + 2]) + 0x12;
|
52
|
+
inCursor += 3;
|
53
|
+
}
|
54
|
+
for (int i = 0; i < len; ++i)
|
55
|
+
{
|
56
|
+
dst->data[dst->size] = dst->data[dst->size - rrr];
|
57
|
+
dst->size++;
|
58
|
+
}
|
59
|
+
}
|
60
|
+
}
|
61
|
+
}
|
62
|
+
|
63
|
+
return 1;
|
64
|
+
}
|
data/ext/yaz0/extconf.rb
ADDED
data/ext/yaz0/yaz0.c
ADDED
@@ -0,0 +1,48 @@
|
|
1
|
+
#include "yaz0.h"
|
2
|
+
#include <stdio.h>
|
3
|
+
|
4
|
+
static VALUE compress(VALUE self, VALUE str)
|
5
|
+
{
|
6
|
+
Yaz0Buffer buffer;
|
7
|
+
VALUE ret;
|
8
|
+
|
9
|
+
if (TYPE(str) != T_STRING)
|
10
|
+
{
|
11
|
+
rb_raise(rb_eTypeError, "Expected a string");
|
12
|
+
return Qnil;
|
13
|
+
}
|
14
|
+
|
15
|
+
yaz0BufferAlloc(&buffer, 16);
|
16
|
+
yaz0Compress(&buffer, StringValuePtr(str), RSTRING_LEN(str));
|
17
|
+
ret = rb_str_new(buffer.data, buffer.size);
|
18
|
+
yaz0BufferFree(&buffer);
|
19
|
+
|
20
|
+
return ret;
|
21
|
+
}
|
22
|
+
|
23
|
+
static VALUE decompress(VALUE self, VALUE str)
|
24
|
+
{
|
25
|
+
Yaz0Buffer buffer;
|
26
|
+
VALUE ret;
|
27
|
+
|
28
|
+
if (TYPE(str) != T_STRING)
|
29
|
+
{
|
30
|
+
rb_raise(rb_eTypeError, "Expected a string");
|
31
|
+
return Qnil;
|
32
|
+
}
|
33
|
+
|
34
|
+
yaz0BufferAlloc(&buffer, 16);
|
35
|
+
yaz0Decompress(&buffer, StringValuePtr(str), RSTRING_LEN(str));
|
36
|
+
ret = rb_str_new(buffer.data, buffer.size);
|
37
|
+
yaz0BufferFree(&buffer);
|
38
|
+
|
39
|
+
return ret;
|
40
|
+
}
|
41
|
+
|
42
|
+
void Init_yaz0(void)
|
43
|
+
{
|
44
|
+
VALUE mod;
|
45
|
+
mod = rb_define_module("Yaz0");
|
46
|
+
rb_define_module_function(mod, "compress", &compress, 1);
|
47
|
+
rb_define_module_function(mod, "decompress", &decompress, 1);
|
48
|
+
}
|
data/ext/yaz0/yaz0.h
ADDED
@@ -0,0 +1,38 @@
|
|
1
|
+
#ifndef YAZ0_H
|
2
|
+
#define YAZ0_H 1
|
3
|
+
|
4
|
+
#include <stddef.h>
|
5
|
+
#include "ruby.h"
|
6
|
+
|
7
|
+
/*
|
8
|
+
* Swap
|
9
|
+
*/
|
10
|
+
inline static uint16_t swap16(uint16_t v)
|
11
|
+
{
|
12
|
+
return (v << 8) | (v >> 8);
|
13
|
+
}
|
14
|
+
|
15
|
+
inline static uint32_t swap32(uint32_t v)
|
16
|
+
{
|
17
|
+
return (v << 24) | ((v << 8) & 0x00ff0000) | ((v >> 8) & 0x0000ff00) | (v >> 24);
|
18
|
+
}
|
19
|
+
|
20
|
+
/*
|
21
|
+
* Buffer
|
22
|
+
*/
|
23
|
+
|
24
|
+
typedef struct
|
25
|
+
{
|
26
|
+
size_t size;
|
27
|
+
size_t capacity;
|
28
|
+
char *data;
|
29
|
+
} Yaz0Buffer;
|
30
|
+
|
31
|
+
void yaz0BufferAlloc(Yaz0Buffer *buf, size_t cap);
|
32
|
+
void yaz0BufferFree(Yaz0Buffer *buf);
|
33
|
+
void yaz0BufferWrite(Yaz0Buffer *buf, const void *data, size_t len);
|
34
|
+
|
35
|
+
int yaz0Compress(Yaz0Buffer *dst, const char *data, size_t len);
|
36
|
+
int yaz0Decompress(Yaz0Buffer *dst, const char *data, size_t len);
|
37
|
+
|
38
|
+
#endif /* YAZ0_H */
|
data/lib/yaz0.rb
ADDED
data/lib/yaz0/version.rb
ADDED
data/yaz0.gemspec
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
require_relative 'lib/yaz0/version'
|
2
|
+
|
3
|
+
Gem::Specification.new do |spec|
|
4
|
+
spec.name = "yaz0"
|
5
|
+
spec.version = Yaz0::VERSION
|
6
|
+
spec.authors = ["Nax"]
|
7
|
+
spec.email = ["max@bacoux.com"]
|
8
|
+
|
9
|
+
spec.summary = "Compress and decompress Yaz0 data."
|
10
|
+
spec.homepage = "https://github.com/Nax/ruby-yaz0"
|
11
|
+
spec.license = "MIT"
|
12
|
+
spec.required_ruby_version = Gem::Requirement.new(">= 2.3.0")
|
13
|
+
spec.metadata["source_code_uri"] = "https://github.com/Nax/ruby-yaz0"
|
14
|
+
|
15
|
+
# Specify which files should be added to the gem when it is released.
|
16
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
17
|
+
spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
|
18
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
19
|
+
end
|
20
|
+
spec.bindir = "exe"
|
21
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
22
|
+
spec.require_paths = ["lib"]
|
23
|
+
spec.extensions = ["ext/yaz0/extconf.rb"]
|
24
|
+
end
|
metadata
ADDED
@@ -0,0 +1,64 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: yaz0
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Nax
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2020-05-18 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description:
|
14
|
+
email:
|
15
|
+
- max@bacoux.com
|
16
|
+
executables: []
|
17
|
+
extensions:
|
18
|
+
- ext/yaz0/extconf.rb
|
19
|
+
extra_rdoc_files: []
|
20
|
+
files:
|
21
|
+
- ".gitignore"
|
22
|
+
- ".rspec"
|
23
|
+
- ".travis.yml"
|
24
|
+
- Gemfile
|
25
|
+
- Gemfile.lock
|
26
|
+
- LICENSE.txt
|
27
|
+
- README.md
|
28
|
+
- Rakefile
|
29
|
+
- bin/console
|
30
|
+
- bin/setup
|
31
|
+
- ext/yaz0/buffer.c
|
32
|
+
- ext/yaz0/compress.c
|
33
|
+
- ext/yaz0/decompress.c
|
34
|
+
- ext/yaz0/extconf.rb
|
35
|
+
- ext/yaz0/yaz0.c
|
36
|
+
- ext/yaz0/yaz0.h
|
37
|
+
- lib/yaz0.rb
|
38
|
+
- lib/yaz0/version.rb
|
39
|
+
- yaz0.gemspec
|
40
|
+
homepage: https://github.com/Nax/ruby-yaz0
|
41
|
+
licenses:
|
42
|
+
- MIT
|
43
|
+
metadata:
|
44
|
+
source_code_uri: https://github.com/Nax/ruby-yaz0
|
45
|
+
post_install_message:
|
46
|
+
rdoc_options: []
|
47
|
+
require_paths:
|
48
|
+
- lib
|
49
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
50
|
+
requirements:
|
51
|
+
- - ">="
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: 2.3.0
|
54
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
55
|
+
requirements:
|
56
|
+
- - ">="
|
57
|
+
- !ruby/object:Gem::Version
|
58
|
+
version: '0'
|
59
|
+
requirements: []
|
60
|
+
rubygems_version: 3.1.2
|
61
|
+
signing_key:
|
62
|
+
specification_version: 4
|
63
|
+
summary: Compress and decompress Yaz0 data.
|
64
|
+
test_files: []
|