yaz0 0.3.0 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,49 @@
1
+ #ifndef LIBYAZ0_H
2
+ #define LIBYAZ0_H
3
+
4
+ #include <stdint.h>
5
+ #include <yaz0.h>
6
+
7
+ #define MODE_NONE 0
8
+ #define MODE_DECOMPRESS 1
9
+ #define MODE_COMPRESS 2
10
+
11
+ #define WINDOW_SIZE 0x4000
12
+ #define HASH_MAX_ENTRIES 0x8000
13
+ #define HASH_REBUILD 0x3000
14
+
15
+ #define likely(x) (__builtin_expect((x),1))
16
+ #define unlikely(x) (__builtin_expect((x),0))
17
+ #define unreachable() __builtin_unreachable()
18
+
19
+ struct Yaz0Stream
20
+ {
21
+ int mode;
22
+ int headersDone;
23
+ int level;
24
+ uint32_t decompSize;
25
+ uint32_t totalOut;
26
+ const uint8_t* in;
27
+ uint8_t* out;
28
+ uint32_t sizeIn;
29
+ uint32_t sizeOut;
30
+ uint32_t cursorIn;
31
+ uint32_t cursorOut;
32
+ uint8_t auxBuf[16];
33
+ uint8_t auxSize;
34
+ uint8_t groupHeader;
35
+ uint8_t groupCount;
36
+ uint32_t window_start;
37
+ uint32_t window_end;
38
+ uint8_t window[WINDOW_SIZE];
39
+ uint32_t htSize;
40
+ uint32_t htHashes[HASH_MAX_ENTRIES];
41
+ uint32_t htEntries[HASH_MAX_ENTRIES];
42
+ };
43
+
44
+ int yaz0_RunDecompress(Yaz0Stream* stream);
45
+ int yaz0_RunCompress(Yaz0Stream* stream);
46
+
47
+ uint32_t swap32(uint32_t v);
48
+
49
+ #endif /* LIBYAZ0_H */
@@ -0,0 +1,6 @@
1
+ #include "libyaz0.h"
2
+
3
+ uint32_t swap32(uint32_t in)
4
+ {
5
+ return ((in & 0xFF) << 24) | ((in & 0xFF00) << 8) | ((in & 0xFF0000) >> 8) | ((in & 0xFF000000) >> 24);
6
+ }
@@ -0,0 +1,14 @@
1
+ require "bundler/setup"
2
+ require "yaz0"
3
+
4
+ RSpec.configure do |config|
5
+ # Enable flags like --only-failures and --next-failure
6
+ config.example_status_persistence_file_path = ".rspec_status"
7
+
8
+ # Disable RSpec exposing methods globally on `Module` and `main`
9
+ config.disable_monkey_patching!
10
+
11
+ config.expect_with :rspec do |c|
12
+ c.syntax = :expect
13
+ end
14
+ end
data/spec/yaz0_spec.rb ADDED
@@ -0,0 +1,39 @@
1
+ RSpec.describe Yaz0 do
2
+ it "has a version number" do
3
+ expect(Yaz0::VERSION).not_to be nil
4
+ end
5
+
6
+ it "can compress" do
7
+ expect(Yaz0.compress("Hello, world")).to be_a(String)
8
+ end
9
+
10
+ it "can decompress" do
11
+ expect(Yaz0.decompress(Yaz0.compress("Hello, world!"))).to eq("Hello, world!")
12
+ end
13
+
14
+ it "works on very redundent string" do
15
+ a = "A" * 8192
16
+ a_compressed = Yaz0.compress(a)
17
+ expect(a_compressed.size).to be < a.size
18
+ expect(Yaz0.decompress(a_compressed)).to eq(a)
19
+ end
20
+
21
+ it "compresses identically" do
22
+ data0 = Yaz0.compress("A" * 8192)
23
+ data1 = Yaz0.compress("A" * 8192)
24
+ expect(data0).to eq(data1)
25
+ end
26
+
27
+ it "decompresses identically" do
28
+ cdata = Yaz0.compress("A" * 8192)
29
+ data0 = Yaz0.decompress(cdata)
30
+ data1 = Yaz0.decompress(cdata)
31
+ expect(data0).to eq(data1)
32
+
33
+ cdata0 = Yaz0.compress("A" * 8192)
34
+ cdata1 = Yaz0.compress("A" * 8192)
35
+ data0 = Yaz0.decompress(cdata0)
36
+ data1 = Yaz0.decompress(cdata1)
37
+ expect(data0).to eq(data1)
38
+ end
39
+ end
data/yaz0.gemspec CHANGED
@@ -14,9 +14,19 @@ Gem::Specification.new do |spec|
14
14
 
15
15
  # Specify which files should be added to the gem when it is released.
16
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
17
+ spec.files = Dir[
18
+ "ext/**/*",
19
+ "lib/**/*",
20
+ "spec/**/*",
21
+ "libyaz0/include/**/*",
22
+ "libyaz0/src/libyaz0/**/*",
23
+ "LICENSE.txt",
24
+ "README.md",
25
+ "Gemfile",
26
+ "Rakefile",
27
+ "yaz0.gemspec",
28
+ ".rspec"
29
+ ]
20
30
  spec.bindir = "exe"
21
31
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
22
32
  spec.require_paths = ["lib"]
metadata CHANGED
@@ -1,16 +1,16 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: yaz0
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nax
8
- autorequire:
8
+ autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2020-05-19 00:00:00.000000000 Z
11
+ date: 2022-08-09 00:00:00.000000000 Z
12
12
  dependencies: []
13
- description:
13
+ description:
14
14
  email:
15
15
  - max@bacoux.com
16
16
  executables: []
@@ -18,30 +18,33 @@ extensions:
18
18
  - ext/yaz0/extconf.rb
19
19
  extra_rdoc_files: []
20
20
  files:
21
- - ".gitignore"
22
21
  - ".rspec"
23
- - ".travis.yml"
24
22
  - Gemfile
25
23
  - LICENSE.txt
26
24
  - README.md
27
25
  - Rakefile
28
- - bin/console
29
- - bin/setup
30
- - ext/yaz0/buffer.c
31
- - ext/yaz0/compress.c
32
- - ext/yaz0/decompress.c
26
+ - ext/yaz0/ext_yaz0.c
27
+ - ext/yaz0/ext_yaz0.h
33
28
  - ext/yaz0/extconf.rb
34
- - ext/yaz0/yaz0.c
35
- - ext/yaz0/yaz0.h
36
29
  - lib/yaz0.rb
37
30
  - lib/yaz0/version.rb
31
+ - lib/yaz0/yaz0.bundle
32
+ - libyaz0/include/yaz0.h
33
+ - libyaz0/src/libyaz0/CMakeLists.txt
34
+ - libyaz0/src/libyaz0/compress.c
35
+ - libyaz0/src/libyaz0/decompress.c
36
+ - libyaz0/src/libyaz0/libyaz0.c
37
+ - libyaz0/src/libyaz0/libyaz0.h
38
+ - libyaz0/src/libyaz0/util.c
39
+ - spec/spec_helper.rb
40
+ - spec/yaz0_spec.rb
38
41
  - yaz0.gemspec
39
42
  homepage: https://github.com/Nax/ruby-yaz0
40
43
  licenses:
41
44
  - MIT
42
45
  metadata:
43
46
  source_code_uri: https://github.com/Nax/ruby-yaz0
44
- post_install_message:
47
+ post_install_message:
45
48
  rdoc_options: []
46
49
  require_paths:
47
50
  - lib
@@ -56,8 +59,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
56
59
  - !ruby/object:Gem::Version
57
60
  version: '0'
58
61
  requirements: []
59
- rubygems_version: 3.1.2
60
- signing_key:
62
+ rubygems_version: 3.3.4
63
+ signing_key:
61
64
  specification_version: 4
62
65
  summary: Compress and decompress Yaz0 data.
63
66
  test_files: []
data/.gitignore DELETED
@@ -1,19 +0,0 @@
1
- /.bundle/
2
- /.yardoc
3
- /_yardoc/
4
- /coverage/
5
- /doc/
6
- /pkg/
7
- /spec/reports/
8
- /tmp/
9
- *.bundle
10
- *.so
11
- *.o
12
- *.a
13
- mkmf.log
14
-
15
- # rspec failure tracking
16
- .rspec_status
17
-
18
- /vendor
19
- /Gemfile.lock
data/.travis.yml DELETED
@@ -1,6 +0,0 @@
1
- ---
2
- language: ruby
3
- cache: bundler
4
- rvm:
5
- - 2.6.3
6
- before_install: gem install bundler -v 2.1.4
data/bin/console DELETED
@@ -1,14 +0,0 @@
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 DELETED
@@ -1,8 +0,0 @@
1
- #!/usr/bin/env bash
2
- set -euo pipefail
3
- IFS=$'\n\t'
4
- set -vx
5
-
6
- bundle install
7
-
8
- # Do any other automated setup that you need to do here
data/ext/yaz0/buffer.c DELETED
@@ -1,29 +0,0 @@
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 DELETED
@@ -1,319 +0,0 @@
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
- int32_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, dst, &tmp);
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
- }
@@ -1,65 +0,0 @@
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
- dst->capacity = dstSize;
27
-
28
- while (inCursor < dataLen && dst->size < dstSize)
29
- {
30
- group = data[inCursor++];
31
- for (int b = 0; b < 8; ++b)
32
- {
33
- if (inCursor >= dataLen || dst->size >= dstSize)
34
- break;
35
-
36
- if (group & (1 << (7 - b)))
37
- {
38
- /* Direct data */
39
- dst->data[dst->size++] = data[inCursor++];
40
- }
41
- else
42
- {
43
- if (data[inCursor] & 0xf0)
44
- {
45
- len = (data[inCursor] >> 4) + 2;
46
- rrr = (((data[inCursor] & 0xf) << 8) | (data[inCursor + 1])) + 1;
47
- inCursor += 2;
48
- }
49
- else
50
- {
51
- rrr = (((data[inCursor] & 0xf) << 8) | (data[inCursor + 1])) + 1;
52
- len = (data[inCursor + 2]) + 0x12;
53
- inCursor += 3;
54
- }
55
- for (int i = 0; i < len; ++i)
56
- {
57
- dst->data[dst->size] = dst->data[dst->size - rrr];
58
- dst->size++;
59
- }
60
- }
61
- }
62
- }
63
-
64
- return 1;
65
- }