rbzip2 0.2.0 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/LICENSE +1 -1
- data/README.md +73 -37
- data/Rakefile +2 -0
- data/lib/core_ext/io.rb +12 -0
- data/lib/rbzip2.rb +13 -9
- data/lib/rbzip2/adapter.rb +17 -0
- data/lib/rbzip2/ffi.rb +33 -0
- data/lib/rbzip2/ffi/compressor.rb +85 -0
- data/lib/rbzip2/ffi/constants.rb +30 -0
- data/lib/rbzip2/ffi/decompressor.rb +163 -0
- data/lib/rbzip2/ffi/errors.rb +14 -0
- data/lib/rbzip2/io.rb +19 -5
- data/lib/rbzip2/java.rb +23 -0
- data/lib/rbzip2/java/compressor.rb +38 -0
- data/lib/rbzip2/java/decompressor.rb +65 -0
- data/lib/rbzip2/ruby.rb +18 -0
- data/lib/rbzip2/{compressor.rb → ruby/compressor.rb} +141 -191
- data/lib/rbzip2/{constants.rb → ruby/constants.rb} +2 -2
- data/lib/rbzip2/ruby/crc.rb +70 -0
- data/lib/rbzip2/{decompressor.rb → ruby/decompressor.rb} +107 -127
- data/lib/rbzip2/{input_data.rb → ruby/input_data.rb} +8 -18
- data/lib/rbzip2/{output_data.rb → ruby/output_data.rb} +6 -9
- data/lib/rbzip2/version.rb +2 -2
- data/spec/common/compressor_spec.rb +68 -0
- data/spec/common/decompressor_spec.rb +63 -0
- data/spec/ffi/compressor_spec.rb +12 -0
- data/spec/ffi/decompressor_spec.rb +12 -0
- data/spec/java/compressor_spec.rb +12 -0
- data/spec/java/decompressor_spec.rb +12 -0
- data/spec/ruby/compressor_spec.rb +12 -0
- data/spec/ruby/decompressor_spec.rb +12 -0
- metadata +56 -149
- data/.gemtest +0 -0
- data/.gitignore +0 -3
- data/.travis.yml +0 -9
- data/Gemfile +0 -3
- data/Gemfile.lock +0 -28
- data/lib/rbzip2/crc.rb +0 -105
- data/rbzip2.gemspec +0 -27
- data/spec/compressor_spec.rb +0 -42
- data/spec/decompressor_spec.rb +0 -41
- data/spec/fixtures/big_test.bz2 +0 -0
- data/spec/fixtures/big_test.txt +0 -2018
- data/spec/fixtures/test.bz2 +0 -0
- data/spec/fixtures/test.txt +0 -11
- data/spec/helper.rb +0 -12
@@ -0,0 +1,14 @@
|
|
1
|
+
# This code is free software; you can redistribute it and/or modify it under
|
2
|
+
# the terms of the new BSD License.
|
3
|
+
#
|
4
|
+
# Copyright (c) 2013, Brian Lopez
|
5
|
+
# Copyright (c) 2013, Sebastian Staudt
|
6
|
+
|
7
|
+
module RBzip2::FFI
|
8
|
+
|
9
|
+
class Error < StandardError; end
|
10
|
+
class BufferError < Error; end
|
11
|
+
class ConfigError < Error; end
|
12
|
+
class CorruptError < Error; end
|
13
|
+
|
14
|
+
end
|
data/lib/rbzip2/io.rb
CHANGED
@@ -1,16 +1,14 @@
|
|
1
1
|
# This code is free software; you can redistribute it and/or modify it under
|
2
2
|
# the terms of the new BSD License.
|
3
3
|
#
|
4
|
-
# Copyright (c) 2011, Sebastian Staudt
|
5
|
-
|
6
|
-
require 'rbzip2/decompressor'
|
4
|
+
# Copyright (c) 2011-2017, Sebastian Staudt
|
7
5
|
|
8
6
|
class RBzip2::IO
|
9
7
|
|
10
8
|
def initialize(io)
|
11
9
|
@io = io
|
12
|
-
@compressor = Compressor.new io
|
13
|
-
@decompressor = Decompressor.new io
|
10
|
+
@compressor = RBzip2.default_adapter::Compressor.new io
|
11
|
+
@decompressor = RBzip2.default_adapter::Decompressor.new io
|
14
12
|
end
|
15
13
|
|
16
14
|
def close
|
@@ -18,6 +16,22 @@ class RBzip2::IO
|
|
18
16
|
@decompressor.close
|
19
17
|
end
|
20
18
|
|
19
|
+
def getc
|
20
|
+
@decompressor.getc
|
21
|
+
end
|
22
|
+
|
23
|
+
def gets
|
24
|
+
@decompressor.gets
|
25
|
+
end
|
26
|
+
|
27
|
+
def putc(int)
|
28
|
+
@compressor.putc int
|
29
|
+
end
|
30
|
+
|
31
|
+
def puts(line)
|
32
|
+
@compressor.puts line
|
33
|
+
end
|
34
|
+
|
21
35
|
def read
|
22
36
|
@decompressor.read
|
23
37
|
end
|
data/lib/rbzip2/java.rb
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
# This code is free software; you can redistribute it and/or modify it under
|
2
|
+
# the terms of the new BSD License.
|
3
|
+
#
|
4
|
+
# Copyright (c) 2013, Sebastian Staudt
|
5
|
+
|
6
|
+
module RBzip2::Java
|
7
|
+
|
8
|
+
def self.init
|
9
|
+
begin
|
10
|
+
require 'java'
|
11
|
+
include_package 'org.apache.commons.compress.compressors.bzip2'
|
12
|
+
BZip2CompressorOutputStream
|
13
|
+
rescue LoadError, NameError
|
14
|
+
@@available = false
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
extend RBzip2::Adapter
|
19
|
+
|
20
|
+
autoload :Compressor, 'rbzip2/java/compressor'
|
21
|
+
autoload :Decompressor, 'rbzip2/java/decompressor'
|
22
|
+
|
23
|
+
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
# This code is free software; you can redistribute it and/or modify it under
|
2
|
+
# the terms of the new BSD License.
|
3
|
+
#
|
4
|
+
# Copyright (c) 2013-2017, Sebastian Staudt
|
5
|
+
|
6
|
+
class RBzip2::Java::Compressor
|
7
|
+
|
8
|
+
def initialize(io)
|
9
|
+
@io = RBzip2::Java::BZip2CompressorOutputStream.new io.to_outputstream
|
10
|
+
end
|
11
|
+
|
12
|
+
def flush
|
13
|
+
@io.flush
|
14
|
+
end
|
15
|
+
|
16
|
+
def close
|
17
|
+
@io.close
|
18
|
+
end
|
19
|
+
|
20
|
+
def putc(int)
|
21
|
+
if int.is_a? Numeric
|
22
|
+
write int & 0xff
|
23
|
+
else
|
24
|
+
write int.to_s[0]
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
def puts(line)
|
29
|
+
write line + $/
|
30
|
+
end
|
31
|
+
|
32
|
+
def write(bytes)
|
33
|
+
raise 'stream closed' if @io.nil?
|
34
|
+
|
35
|
+
@io.write bytes.to_java_bytes
|
36
|
+
end
|
37
|
+
|
38
|
+
end
|
@@ -0,0 +1,65 @@
|
|
1
|
+
# This code is free software; you can redistribute it and/or modify it under
|
2
|
+
# the terms of the new BSD License.
|
3
|
+
#
|
4
|
+
# Copyright (c) 2013-2017, Sebastian Staudt
|
5
|
+
|
6
|
+
class RBzip2::Java::Decompressor
|
7
|
+
|
8
|
+
def initialize(io)
|
9
|
+
@io = io
|
10
|
+
@is = RBzip2::Java::BZip2CompressorInputStream.new io.to_inputstream
|
11
|
+
end
|
12
|
+
|
13
|
+
def close
|
14
|
+
@is.close
|
15
|
+
end
|
16
|
+
|
17
|
+
def getc
|
18
|
+
read(1)[0].chr
|
19
|
+
end
|
20
|
+
|
21
|
+
def gets
|
22
|
+
line = ''
|
23
|
+
loop do
|
24
|
+
char = getc
|
25
|
+
line += char
|
26
|
+
break if char == "\n"
|
27
|
+
end
|
28
|
+
line
|
29
|
+
end
|
30
|
+
|
31
|
+
def read(length = nil)
|
32
|
+
if length.nil?
|
33
|
+
bytes = Java::byte[0].new
|
34
|
+
chunk = Java::byte[1024].new
|
35
|
+
begin
|
36
|
+
bytes_read = @is.read chunk
|
37
|
+
chunk = chunk[0..(bytes_read - 1)] if bytes_read < 1024
|
38
|
+
bytes += chunk
|
39
|
+
end while bytes_read == 1024
|
40
|
+
else
|
41
|
+
bytes = Java::byte[length].new
|
42
|
+
@is.read bytes
|
43
|
+
end
|
44
|
+
|
45
|
+
String.from_java_bytes bytes
|
46
|
+
end
|
47
|
+
|
48
|
+
def size
|
49
|
+
if @io.is_a? StringIO
|
50
|
+
@io.size
|
51
|
+
elsif @io.is_a? File
|
52
|
+
@io.stat.size
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
def uncompressed
|
57
|
+
@data = read
|
58
|
+
@data.size
|
59
|
+
end
|
60
|
+
|
61
|
+
def inspect
|
62
|
+
"#<#{self.class}: @io=#{@io.inspect} size=#{size} uncompressed=#{uncompressed}>"
|
63
|
+
end
|
64
|
+
|
65
|
+
end
|
data/lib/rbzip2/ruby.rb
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
# This code is free software; you can redistribute it and/or modify it under
|
2
|
+
# the terms of the new BSD License.
|
3
|
+
#
|
4
|
+
# Copyright (c) 2013, Sebastian Staudt
|
5
|
+
|
6
|
+
module RBzip2::Ruby
|
7
|
+
|
8
|
+
extend RBzip2::Adapter
|
9
|
+
|
10
|
+
autoload :CRC, 'rbzip2/ruby/crc'
|
11
|
+
autoload :Compressor, 'rbzip2/ruby/compressor'
|
12
|
+
autoload :Constants, 'rbzip2/ruby/constants'
|
13
|
+
autoload :Decompressor, 'rbzip2/ruby/decompressor'
|
14
|
+
autoload :IO, 'rbzip2/ruby/io'
|
15
|
+
autoload :InputData, 'rbzip2/ruby/input_data'
|
16
|
+
autoload :OutputData, 'rbzip2/ruby/output_data'
|
17
|
+
|
18
|
+
end
|
@@ -1,11 +1,11 @@
|
|
1
|
-
# This code is free software you can redistribute it and/or modify it under
|
1
|
+
# This code is free software; you can redistribute it and/or modify it under
|
2
2
|
# the terms of the new BSD License.
|
3
3
|
#
|
4
|
-
# Copyright (c) 2011, Sebastian Staudt
|
4
|
+
# Copyright (c) 2011-2017, Sebastian Staudt
|
5
5
|
|
6
|
-
class RBzip2::Compressor
|
6
|
+
class RBzip2::Ruby::Compressor
|
7
7
|
|
8
|
-
include RBzip2::Constants
|
8
|
+
include RBzip2::Ruby::Constants
|
9
9
|
|
10
10
|
def self.assign_codes(code, length, min_len, max_len, alpha_size)
|
11
11
|
vec = 0
|
@@ -63,11 +63,10 @@ class RBzip2::Compressor
|
|
63
63
|
heap[1] = heap[n_heap]
|
64
64
|
n_heap -= 1
|
65
65
|
|
66
|
-
yy = 0
|
67
66
|
zz = 1
|
68
67
|
tmp = heap[1]
|
69
68
|
|
70
|
-
|
69
|
+
while true do
|
71
70
|
yy = zz << 1
|
72
71
|
|
73
72
|
break if yy > n_heap
|
@@ -86,11 +85,10 @@ class RBzip2::Compressor
|
|
86
85
|
heap[1] = heap[n_heap]
|
87
86
|
n_heap -= 1
|
88
87
|
|
89
|
-
yy = 0
|
90
88
|
zz = 1
|
91
89
|
tmp = heap[1]
|
92
90
|
|
93
|
-
|
91
|
+
while true do
|
94
92
|
yy = zz << 1
|
95
93
|
|
96
94
|
break if yy > n_heap
|
@@ -119,7 +117,6 @@ class RBzip2::Compressor
|
|
119
117
|
n_heap += 1
|
120
118
|
heap[n_heap] = n_nodes
|
121
119
|
|
122
|
-
tmp = 0
|
123
120
|
zz = n_heap
|
124
121
|
tmp = heap[zz]
|
125
122
|
weight_tmp = weight[tmp]
|
@@ -160,9 +157,7 @@ class RBzip2::Compressor
|
|
160
157
|
def self.vswap(fmap, p1, p2, n)
|
161
158
|
n += p1
|
162
159
|
while p1 < n
|
163
|
-
|
164
|
-
fmap[p1] = fmap[p2]
|
165
|
-
fmap[p2] = t
|
160
|
+
fmap[p1], fmap[p2] = fmap[p2], fmap[p1]
|
166
161
|
p1 += 1
|
167
162
|
p2 += 1
|
168
163
|
end
|
@@ -175,7 +170,7 @@ class RBzip2::Compressor
|
|
175
170
|
@block_size = block_size
|
176
171
|
@buff = 0
|
177
172
|
@combined_crc = 0
|
178
|
-
@crc = RBzip2::CRC.new
|
173
|
+
@crc = RBzip2::Ruby::CRC.new
|
179
174
|
@current_char = -1
|
180
175
|
@io = io
|
181
176
|
@last = 0
|
@@ -309,15 +304,13 @@ class RBzip2::Compressor
|
|
309
304
|
z_pend = 0
|
310
305
|
|
311
306
|
0.upto(last_shadow) do |i|
|
312
|
-
ll_i = unseq_to_seq[block[fmap[i]]
|
307
|
+
ll_i = unseq_to_seq[block[fmap[i]]]
|
313
308
|
tmp = yy[0]
|
314
309
|
j = 0
|
315
310
|
|
316
311
|
while ll_i != tmp
|
317
312
|
j += 1
|
318
|
-
|
319
|
-
tmp = yy[j]
|
320
|
-
yy[j] = tmp2
|
313
|
+
tmp, yy[j] = yy[j], tmp
|
321
314
|
end
|
322
315
|
yy[0] = tmp
|
323
316
|
|
@@ -326,7 +319,7 @@ class RBzip2::Compressor
|
|
326
319
|
else
|
327
320
|
if z_pend > 0
|
328
321
|
z_pend -= 1
|
329
|
-
|
322
|
+
while true do
|
330
323
|
if (z_pend & 1) == 0
|
331
324
|
sfmap[wr] = RUNA
|
332
325
|
mtf_freq[RUNA] += 1
|
@@ -350,7 +343,7 @@ class RBzip2::Compressor
|
|
350
343
|
|
351
344
|
if z_pend > 0
|
352
345
|
z_pend -= 1
|
353
|
-
|
346
|
+
while true do
|
354
347
|
if (z_pend & 1) == 0
|
355
348
|
sfmap[wr] = RUNA
|
356
349
|
mtf_freq[RUNA] += 1
|
@@ -375,7 +368,7 @@ class RBzip2::Compressor
|
|
375
368
|
put_byte 0x42
|
376
369
|
put_byte 0x5a
|
377
370
|
|
378
|
-
@data = RBzip2::OutputData.new @block_size
|
371
|
+
@data = RBzip2::Ruby::OutputData.new @block_size
|
379
372
|
|
380
373
|
put_byte 0x68
|
381
374
|
put_byte 0x30 + @block_size
|
@@ -403,42 +396,37 @@ class RBzip2::Compressor
|
|
403
396
|
block = data_shadow.block
|
404
397
|
fmap = data_shadow.fmap
|
405
398
|
quadrant = data_shadow.quadrant
|
406
|
-
last_shadow = @last
|
407
|
-
work_limit_shadow = @work_limit
|
408
|
-
first_attempt_shadow = @first_attempt
|
409
399
|
|
410
400
|
65537.times { |i| ftab[i] = 0 }
|
411
401
|
|
412
402
|
NUM_OVERSHOOT_BYTES.times do |i|
|
413
|
-
block[
|
403
|
+
block[@last + i + 2] = block[(i % (@last + 1)) + 1]
|
414
404
|
end
|
415
|
-
(
|
405
|
+
(@last + NUM_OVERSHOOT_BYTES).times do |i|
|
416
406
|
quadrant[i] = 0
|
417
407
|
end
|
418
|
-
block[0] = block[
|
408
|
+
block[0] = block[@last + 1]
|
419
409
|
|
420
|
-
c1 = block[0]
|
421
|
-
(
|
422
|
-
c2 = block[i + 1]
|
410
|
+
c1 = block[0]
|
411
|
+
(@last + 1).times do |i|
|
412
|
+
c2 = block[i + 1]
|
423
413
|
ftab[(c1 << 8) + c2] += 1
|
424
414
|
c1 = c2
|
425
415
|
end
|
426
416
|
|
427
417
|
1.upto(65536) { |i| ftab[i] += ftab[i - 1] }
|
428
418
|
|
429
|
-
c1 = block[1]
|
430
|
-
|
431
|
-
c2 = block[i + 2]
|
419
|
+
c1 = block[1]
|
420
|
+
@last.times do |i|
|
421
|
+
c2 = block[i + 2]
|
432
422
|
fmap[ftab[(c1 << 8) + c2] -= 1] = i
|
433
423
|
c1 = c2
|
434
424
|
end
|
435
425
|
|
436
|
-
fmap[ftab[((block[
|
426
|
+
fmap[ftab[((block[@last + 1]) << 8) + block[1]] -= 1] = @last
|
437
427
|
|
438
|
-
|
439
|
-
|
440
|
-
running_order[i] = i
|
441
|
-
end
|
428
|
+
big_done.replace Array.new(256, false)
|
429
|
+
256.times { |i| running_order[i] = i }
|
442
430
|
|
443
431
|
h = 364
|
444
432
|
while h != 1
|
@@ -472,7 +460,7 @@ class RBzip2::Compressor
|
|
472
460
|
hi = (ftab[sb + 1] & CLEARMASK) - 1
|
473
461
|
if hi > lo
|
474
462
|
main_qsort3 data_shadow, lo, hi, 2
|
475
|
-
return if
|
463
|
+
return if @first_attempt && (@work_done > @work_limit)
|
476
464
|
end
|
477
465
|
ftab[sb] = ftab_sb | SETMASK
|
478
466
|
end
|
@@ -483,9 +471,9 @@ class RBzip2::Compressor
|
|
483
471
|
hj = ftab[(ss + 1) << 8] & CLEARMASK
|
484
472
|
(ftab[ss << 8] & CLEARMASK).upto(hj - 1) do |j|
|
485
473
|
fmap_j = fmap[j]
|
486
|
-
c1 = block[fmap_j]
|
487
|
-
|
488
|
-
fmap[copy[c1]] = (fmap_j == 0) ?
|
474
|
+
c1 = block[fmap_j]
|
475
|
+
unless big_done[c1]
|
476
|
+
fmap[copy[c1]] = (fmap_j == 0) ? @last : (fmap_j - 1)
|
489
477
|
copy[c1] += 1
|
490
478
|
end
|
491
479
|
end
|
@@ -508,7 +496,7 @@ class RBzip2::Compressor
|
|
508
496
|
q_val = j >> shifts
|
509
497
|
quadrant[a2update] = q_val
|
510
498
|
if a2update < NUM_OVERSHOOT_BYTES
|
511
|
-
quadrant[a2update +
|
499
|
+
quadrant[a2update + @last + 1] = q_val
|
512
500
|
end
|
513
501
|
end
|
514
502
|
end
|
@@ -536,7 +524,7 @@ class RBzip2::Compressor
|
|
536
524
|
return if main_simple_sort data_shadow, lo, hi, d
|
537
525
|
else
|
538
526
|
d1 = d + 1
|
539
|
-
med = self.class.med3
|
527
|
+
med = self.class.med3 block[fmap[lo] + d1], block[fmap[hi] + d1], block[fmap[(lo + hi) >> 1] + d1]
|
540
528
|
|
541
529
|
un_lo = lo
|
542
530
|
un_hi = hi
|
@@ -545,11 +533,9 @@ class RBzip2::Compressor
|
|
545
533
|
|
546
534
|
while true
|
547
535
|
while un_lo <= un_hi
|
548
|
-
n =
|
536
|
+
n = block[fmap[un_lo] + d1] - med
|
549
537
|
if n == 0
|
550
|
-
|
551
|
-
fmap[un_lo] = fmap[lt_lo]
|
552
|
-
fmap[lt_lo] = temp
|
538
|
+
fmap[un_lo], fmap[lt_lo] = fmap[lt_lo], fmap[un_lo]
|
553
539
|
un_lo += 1
|
554
540
|
lt_lo += 1
|
555
541
|
elsif n < 0
|
@@ -560,11 +546,9 @@ class RBzip2::Compressor
|
|
560
546
|
end
|
561
547
|
|
562
548
|
while un_lo <= un_hi
|
563
|
-
n =
|
549
|
+
n = block[fmap[un_hi] + d1] - med
|
564
550
|
if n == 0
|
565
|
-
|
566
|
-
fmap[un_hi] = fmap[gt_hi]
|
567
|
-
fmap[gt_hi] = temp
|
551
|
+
fmap[un_hi], fmap[gt_hi] = fmap[gt_hi], fmap[un_hi]
|
568
552
|
un_hi -= 1
|
569
553
|
gt_hi -= 1
|
570
554
|
elsif n > 0
|
@@ -575,9 +559,7 @@ class RBzip2::Compressor
|
|
575
559
|
end
|
576
560
|
|
577
561
|
if un_lo <= un_hi
|
578
|
-
|
579
|
-
fmap[un_lo] = fmap[un_hi]
|
580
|
-
fmap[un_hi] = temp
|
562
|
+
fmap[un_lo], fmap[un_hi] = fmap[un_hi], fmap[un_lo]
|
581
563
|
un_lo += 1
|
582
564
|
un_hi -= 1
|
583
565
|
else
|
@@ -599,20 +581,10 @@ class RBzip2::Compressor
|
|
599
581
|
n = lo + un_lo - lt_lo - 1
|
600
582
|
m = hi - (gt_hi - un_hi) + 1
|
601
583
|
|
602
|
-
stack_ll[sp] = lo
|
603
|
-
stack_hh[sp] = n
|
604
|
-
stack_dd[sp] = d
|
605
|
-
sp +=
|
606
|
-
|
607
|
-
stack_ll[sp] = n + 1
|
608
|
-
stack_hh[sp] = m - 1
|
609
|
-
stack_dd[sp] = d1
|
610
|
-
sp += 1
|
611
|
-
|
612
|
-
stack_ll[sp] = m
|
613
|
-
stack_hh[sp] = hi
|
614
|
-
stack_dd[sp] = d
|
615
|
-
sp += 1
|
584
|
+
stack_ll[sp, 3] = lo, n + 1, m
|
585
|
+
stack_hh[sp, 3] = n, m - 1, hi
|
586
|
+
stack_dd[sp, 3] = d, d1, d
|
587
|
+
sp += 3
|
616
588
|
end
|
617
589
|
end
|
618
590
|
end
|
@@ -630,13 +602,8 @@ class RBzip2::Compressor
|
|
630
602
|
fmap = data_shadow.fmap
|
631
603
|
quadrant = data_shadow.quadrant
|
632
604
|
block = data_shadow.block
|
633
|
-
|
634
|
-
last_plus_1 = last_shadow + 1
|
635
|
-
first_attempt_shadow = @first_attempt
|
636
|
-
work_limit_shadow = @work_limit
|
637
|
-
work_done_shadow = @work_done
|
605
|
+
last_plus_1 = @last + 1
|
638
606
|
|
639
|
-
a = nil
|
640
607
|
h = nil
|
641
608
|
i1 = nil
|
642
609
|
i2 = nil
|
@@ -660,14 +627,14 @@ class RBzip2::Compressor
|
|
660
627
|
if quadrant[i1 + 3] == quadrant[i2 + 3]
|
661
628
|
i1 -= last_plus_1 if (i1 += 4) >= last_plus_1
|
662
629
|
i2 -= last_plus_1 if (i2 += 4) >= last_plus_1
|
663
|
-
|
630
|
+
@work_done += 1
|
664
631
|
next
|
665
632
|
elsif quadrant[i1 + 3] > quadrant[i2 + 3]
|
666
633
|
return true
|
667
634
|
else
|
668
635
|
return false
|
669
636
|
end
|
670
|
-
elsif
|
637
|
+
elsif block[i1 + 4] > block[i2 + 4]
|
671
638
|
return true
|
672
639
|
else
|
673
640
|
return false
|
@@ -677,7 +644,7 @@ class RBzip2::Compressor
|
|
677
644
|
else
|
678
645
|
return false
|
679
646
|
end
|
680
|
-
elsif
|
647
|
+
elsif block[i1 + 3] > block[i2 + 3]
|
681
648
|
return true
|
682
649
|
else
|
683
650
|
return false
|
@@ -687,17 +654,17 @@ class RBzip2::Compressor
|
|
687
654
|
else
|
688
655
|
return false
|
689
656
|
end
|
690
|
-
elsif
|
657
|
+
elsif block[i1 + 2] > block[i2 + 2]
|
691
658
|
return true
|
692
659
|
else
|
693
660
|
return false
|
694
661
|
end
|
695
|
-
elsif
|
662
|
+
elsif quadrant[i1] > quadrant[i2]
|
696
663
|
return true
|
697
664
|
else
|
698
665
|
return false
|
699
666
|
end
|
700
|
-
elsif
|
667
|
+
elsif block[i1 + 1] > block[i2 + 1]
|
701
668
|
return true
|
702
669
|
else
|
703
670
|
return false
|
@@ -728,37 +695,37 @@ class RBzip2::Compressor
|
|
728
695
|
if block[i1 + 4] == block[i2 + 4]
|
729
696
|
if block[i1 + 5] == block[i2 + 5]
|
730
697
|
if block[i1 += 6] == block[i2 += 6]
|
731
|
-
x =
|
698
|
+
x = @last
|
732
699
|
|
733
700
|
break unless x_loop.call
|
734
701
|
else
|
735
|
-
if
|
702
|
+
if block[i1] > block[i2]
|
736
703
|
next
|
737
704
|
else
|
738
705
|
break
|
739
706
|
end
|
740
707
|
end
|
741
|
-
elsif
|
708
|
+
elsif block[i1 + 5] > block[i2 + 5]
|
742
709
|
next
|
743
710
|
else
|
744
711
|
break
|
745
712
|
end
|
746
|
-
elsif
|
713
|
+
elsif block[i1 + 4] > block[i2 + 4]
|
747
714
|
next
|
748
715
|
else
|
749
716
|
break
|
750
717
|
end
|
751
|
-
elsif
|
718
|
+
elsif block[i1 + 3] > block[i2 + 3]
|
752
719
|
next
|
753
720
|
else
|
754
721
|
break
|
755
722
|
end
|
756
|
-
elsif
|
723
|
+
elsif block[i1 + 2] > block[i2 + 2]
|
757
724
|
next
|
758
725
|
else
|
759
726
|
break
|
760
727
|
end
|
761
|
-
elsif
|
728
|
+
elsif block[i1 + 1] > block[i2 + 1]
|
762
729
|
next
|
763
730
|
else
|
764
731
|
break
|
@@ -788,12 +755,10 @@ class RBzip2::Compressor
|
|
788
755
|
end
|
789
756
|
end
|
790
757
|
|
791
|
-
break if
|
758
|
+
break if @first_attempt && i <= hi && @work_done > @work_limit
|
792
759
|
end
|
793
760
|
|
794
|
-
@work_done
|
795
|
-
|
796
|
-
first_attempt_shadow && (work_done_shadow > work_limit_shadow)
|
761
|
+
@first_attempt && @work_done > @work_limit
|
797
762
|
end
|
798
763
|
|
799
764
|
def move_to_front_code_and_send
|
@@ -802,6 +767,18 @@ class RBzip2::Compressor
|
|
802
767
|
send_mtf_values
|
803
768
|
end
|
804
769
|
|
770
|
+
def putc(int)
|
771
|
+
if int.is_a? Numeric
|
772
|
+
put_byte int & 0xff
|
773
|
+
else
|
774
|
+
write int.to_s[0].chr
|
775
|
+
end
|
776
|
+
end
|
777
|
+
|
778
|
+
def puts(line)
|
779
|
+
write line + $/
|
780
|
+
end
|
781
|
+
|
805
782
|
def put_byte(c)
|
806
783
|
c = c[0].to_i if c.is_a? String
|
807
784
|
w 8, c
|
@@ -815,9 +792,8 @@ class RBzip2::Compressor
|
|
815
792
|
end
|
816
793
|
|
817
794
|
def randomize_block
|
818
|
-
in_use
|
819
|
-
block
|
820
|
-
last_shadow = @last
|
795
|
+
in_use = @data.in_use
|
796
|
+
block = @data.block
|
821
797
|
|
822
798
|
256.times { |i| in_use[i] = false }
|
823
799
|
|
@@ -825,7 +801,7 @@ class RBzip2::Compressor
|
|
825
801
|
r_t_pos = 0
|
826
802
|
j = 1
|
827
803
|
i = 0
|
828
|
-
while i <=
|
804
|
+
while i <= @last
|
829
805
|
i = j
|
830
806
|
|
831
807
|
if r_n_to_go == 0
|
@@ -836,7 +812,7 @@ class RBzip2::Compressor
|
|
836
812
|
r_n_to_go -= 1
|
837
813
|
block[j] ^= r_n_to_go == 1 ? 1 : 0
|
838
814
|
|
839
|
-
in_use[block[j]
|
815
|
+
in_use[block[j]] = true
|
840
816
|
|
841
817
|
j += 1
|
842
818
|
end
|
@@ -899,7 +875,7 @@ class RBzip2::Compressor
|
|
899
875
|
end
|
900
876
|
end
|
901
877
|
|
902
|
-
gs = ge +1
|
878
|
+
gs = ge + 1
|
903
879
|
rem_f -= a_freq
|
904
880
|
end
|
905
881
|
end
|
@@ -918,7 +894,6 @@ class RBzip2::Compressor
|
|
918
894
|
len_3 = len[3]
|
919
895
|
len_4 = len[4]
|
920
896
|
len_5 = len[5]
|
921
|
-
n_mtf_shadow = @n_mtf
|
922
897
|
n_selectors = 0
|
923
898
|
|
924
899
|
N_ITERS.times do
|
@@ -932,7 +907,7 @@ class RBzip2::Compressor
|
|
932
907
|
|
933
908
|
gs = 0
|
934
909
|
while gs < @n_mtf
|
935
|
-
ge = [gs + G_SIZE - 1,
|
910
|
+
ge = [gs + G_SIZE - 1, @n_mtf - 1].min
|
936
911
|
|
937
912
|
if n_groups == N_GROUPS
|
938
913
|
cost0 = 0
|
@@ -1008,9 +983,7 @@ class RBzip2::Compressor
|
|
1008
983
|
|
1009
984
|
while ll_i != tmp
|
1010
985
|
j += 1
|
1011
|
-
|
1012
|
-
tmp = pos[j]
|
1013
|
-
pos[j] = tmp2
|
986
|
+
tmp, pos[j] = pos[j], tmp
|
1014
987
|
end
|
1015
988
|
|
1016
989
|
pos[0] = tmp
|
@@ -1048,7 +1021,7 @@ class RBzip2::Compressor
|
|
1048
1021
|
end
|
1049
1022
|
end
|
1050
1023
|
|
1051
|
-
16.times { |i| w 1,
|
1024
|
+
16.times { |i| w 1, in_use_16[i] ? 1 : 0 }
|
1052
1025
|
|
1053
1026
|
io_shadow = @io
|
1054
1027
|
live_shadow = @live
|
@@ -1112,83 +1085,71 @@ class RBzip2::Compressor
|
|
1112
1085
|
|
1113
1086
|
def send_mtf_values6(n_groups, alpha_size)
|
1114
1087
|
len = @data.send_mtf_values_len
|
1115
|
-
io_shadow = @io
|
1116
|
-
|
1117
|
-
live_shadow = @live
|
1118
|
-
buff_shadow = @buff
|
1119
1088
|
|
1120
1089
|
n_groups.times do |t|
|
1121
1090
|
len_t = len[t]
|
1122
1091
|
curr = len_t[0] & 0xff
|
1123
1092
|
|
1124
|
-
while
|
1125
|
-
|
1126
|
-
|
1127
|
-
|
1128
|
-
|
1093
|
+
while @live >= 8
|
1094
|
+
@io.write(((@buff >> 24) & 0xffffffff).chr)
|
1095
|
+
@buff <<= 8
|
1096
|
+
@buff &= 0xffffffff
|
1097
|
+
@live -= 8
|
1129
1098
|
end
|
1130
|
-
|
1131
|
-
|
1099
|
+
@buff |= curr << (32 - @live - 5)
|
1100
|
+
@live += 5
|
1132
1101
|
|
1133
1102
|
alpha_size.times do |i|
|
1134
1103
|
lti = len_t[i] & 0xff
|
1135
1104
|
while curr < lti
|
1136
|
-
while
|
1137
|
-
|
1138
|
-
|
1139
|
-
|
1140
|
-
|
1105
|
+
while @live >= 8
|
1106
|
+
@io.write(((@buff >> 24) & 0xffffffff).chr)
|
1107
|
+
@buff <<= 8
|
1108
|
+
@buff &= 0xffffffff
|
1109
|
+
@live -= 8
|
1141
1110
|
end
|
1142
|
-
|
1143
|
-
|
1111
|
+
@buff |= 2 << (32 - @live - 2)
|
1112
|
+
@live += 2
|
1144
1113
|
|
1145
1114
|
curr += 1
|
1146
1115
|
end
|
1147
1116
|
|
1148
1117
|
while curr > lti
|
1149
|
-
while
|
1150
|
-
|
1151
|
-
|
1152
|
-
|
1153
|
-
|
1118
|
+
while @live >= 8
|
1119
|
+
@io.write(((@buff >> 24) & 0xffffffff).chr)
|
1120
|
+
@buff <<= 8
|
1121
|
+
@buff &= 0xffffffff
|
1122
|
+
@live -= 8
|
1154
1123
|
end
|
1155
|
-
|
1156
|
-
|
1124
|
+
@buff |= 3 << (32 - @live - 2)
|
1125
|
+
@live += 2
|
1157
1126
|
|
1158
1127
|
curr -= 1
|
1159
1128
|
end
|
1160
1129
|
|
1161
|
-
while
|
1162
|
-
|
1163
|
-
|
1164
|
-
|
1165
|
-
|
1130
|
+
while @live >= 8
|
1131
|
+
@io.write(((@buff >> 24) & 0xffffffff).chr)
|
1132
|
+
@buff <<= 8
|
1133
|
+
@buff &= 0xffffffff
|
1134
|
+
@live -= 8
|
1166
1135
|
end
|
1167
|
-
|
1136
|
+
@live += 1
|
1168
1137
|
end
|
1169
1138
|
end
|
1170
|
-
|
1171
|
-
@buff = buff_shadow
|
1172
|
-
@live = live_shadow
|
1173
1139
|
end
|
1174
1140
|
|
1175
1141
|
def send_mtf_values7
|
1176
1142
|
data_shadow = @data
|
1177
1143
|
len = data_shadow.send_mtf_values_len
|
1178
1144
|
code = data_shadow.send_mtf_values_code
|
1179
|
-
io_shadow = @io
|
1180
1145
|
selector = data_shadow.selector
|
1181
1146
|
sfmap = data_shadow.sfmap
|
1182
|
-
n_mtf_shadow = @n_mtf
|
1183
1147
|
|
1184
1148
|
sel_ctr = 0
|
1185
1149
|
|
1186
|
-
live_shadow = @live
|
1187
|
-
buff_shadow = @buff
|
1188
|
-
|
1189
1150
|
gs = 0
|
1190
|
-
while gs <
|
1191
|
-
ge = [gs + G_SIZE - 1,
|
1151
|
+
while gs < @n_mtf
|
1152
|
+
ge = [gs + G_SIZE - 1, @n_mtf - 1].min
|
1192
1153
|
selector_sel_ctr = selector[sel_ctr] & 0xff
|
1193
1154
|
code_sel_ctr = code[selector_sel_ctr]
|
1194
1155
|
len_sel_ctr = len[selector_sel_ctr]
|
@@ -1196,15 +1157,15 @@ class RBzip2::Compressor
|
|
1196
1157
|
while gs <= ge
|
1197
1158
|
sfmap_i = sfmap[gs]
|
1198
1159
|
|
1199
|
-
while
|
1200
|
-
|
1201
|
-
|
1202
|
-
|
1203
|
-
|
1160
|
+
while @live >= 8
|
1161
|
+
@io.write(((@buff >> 24) & 0xffffffff).chr)
|
1162
|
+
@buff <<= 8
|
1163
|
+
@buff &= 0xffffffff
|
1164
|
+
@live -= 8
|
1204
1165
|
end
|
1205
1166
|
n = len_sel_ctr[sfmap_i] & 0xff
|
1206
|
-
|
1207
|
-
|
1167
|
+
@buff |= code_sel_ctr[sfmap_i] << (32 - @live - n)
|
1168
|
+
@live += n
|
1208
1169
|
|
1209
1170
|
gs += 1
|
1210
1171
|
end
|
@@ -1212,25 +1173,18 @@ class RBzip2::Compressor
|
|
1212
1173
|
gs = ge + 1
|
1213
1174
|
sel_ctr += 1
|
1214
1175
|
end
|
1215
|
-
|
1216
|
-
@buff = buff_shadow
|
1217
|
-
@live = live_shadow
|
1218
1176
|
end
|
1219
1177
|
|
1220
1178
|
def w(n, v)
|
1221
|
-
|
1222
|
-
|
1223
|
-
|
1224
|
-
|
1225
|
-
|
1226
|
-
io_shadow.write(((buff_shadow >> 24) & 0xffffffff).chr)
|
1227
|
-
buff_shadow <<= 8
|
1228
|
-
buff_shadow &= 0xffffffff
|
1229
|
-
live_shadow -= 8
|
1179
|
+
while @live >= 8
|
1180
|
+
@io.write(((@buff >> 24) & 0xffffffff).chr)
|
1181
|
+
@buff <<= 8
|
1182
|
+
@buff &= 0xffffffff
|
1183
|
+
@live -= 8
|
1230
1184
|
end
|
1231
1185
|
|
1232
|
-
@buff =
|
1233
|
-
@live
|
1186
|
+
@buff = @buff | (v << (32 - @live - n))
|
1187
|
+
@live += n
|
1234
1188
|
end
|
1235
1189
|
|
1236
1190
|
def write(bytes)
|
@@ -1240,8 +1194,8 @@ class RBzip2::Compressor
|
|
1240
1194
|
end
|
1241
1195
|
|
1242
1196
|
def write0(b)
|
1197
|
+
b &= 0xff
|
1243
1198
|
if @current_char != -1
|
1244
|
-
b &= 0xff
|
1245
1199
|
if @current_char == b
|
1246
1200
|
@run_length += 1
|
1247
1201
|
if @run_length > 254
|
@@ -1255,50 +1209,46 @@ class RBzip2::Compressor
|
|
1255
1209
|
@current_char = b
|
1256
1210
|
end
|
1257
1211
|
else
|
1258
|
-
@current_char = b
|
1212
|
+
@current_char = b
|
1259
1213
|
@run_length += 1
|
1260
1214
|
end
|
1261
1215
|
end
|
1262
1216
|
|
1263
1217
|
def write_run
|
1264
|
-
|
1265
|
-
|
1266
|
-
if last_shadow < @allowable_block_size
|
1267
|
-
current_char_shadow = @current_char
|
1218
|
+
if @last < @allowable_block_size
|
1219
|
+
ch = @current_char
|
1268
1220
|
data_shadow = @data
|
1269
|
-
data_shadow.in_use[
|
1270
|
-
|
1221
|
+
data_shadow.in_use[ch] = true
|
1222
|
+
block = data_shadow.block
|
1271
1223
|
|
1272
1224
|
run_length_shadow = @run_length
|
1273
|
-
@crc.update_crc
|
1225
|
+
run_length_shadow.times { @crc.update_crc ch }
|
1274
1226
|
|
1275
1227
|
case run_length_shadow
|
1276
1228
|
when 1
|
1277
|
-
|
1278
|
-
@last
|
1229
|
+
block[@last + 2] = ch
|
1230
|
+
@last += 1
|
1279
1231
|
|
1280
1232
|
when 2
|
1281
|
-
|
1282
|
-
|
1283
|
-
@last
|
1233
|
+
block[@last + 2] = ch
|
1234
|
+
block[@last + 3] = ch
|
1235
|
+
@last += 2
|
1284
1236
|
|
1285
1237
|
when 3
|
1286
|
-
block =
|
1287
|
-
block[
|
1288
|
-
block[
|
1289
|
-
|
1290
|
-
@last = last_shadow + 3
|
1238
|
+
block[@last + 2] = ch
|
1239
|
+
block[@last + 3] = ch
|
1240
|
+
block[@last + 4] = ch
|
1241
|
+
@last += 3
|
1291
1242
|
|
1292
1243
|
else
|
1293
1244
|
run_length_shadow -= 4
|
1294
1245
|
data_shadow.in_use[run_length_shadow] = true
|
1295
|
-
block =
|
1296
|
-
block[
|
1297
|
-
block[
|
1298
|
-
block[
|
1299
|
-
block[
|
1300
|
-
|
1301
|
-
@last = last_shadow + 5
|
1246
|
+
block[@last + 2] = ch
|
1247
|
+
block[@last + 3] = ch
|
1248
|
+
block[@last + 4] = ch
|
1249
|
+
block[@last + 5] = ch
|
1250
|
+
block[@last + 6] = run_length_shadow
|
1251
|
+
@last += 5
|
1302
1252
|
end
|
1303
1253
|
else
|
1304
1254
|
end_block
|