Class: RBzip2::Ruby::Decompressor
- Inherits:
-
Object
- Object
- RBzip2::Ruby::Decompressor
show all
- Includes:
- Constants
- Defined in:
- lib/facter/util/rbzip2-0.3.0/lib/rbzip2/ruby/decompressor.rb
Constant Summary
Constants included
from Constants
Constants::BASEBLOCKSIZE, Constants::CLEARMASK, Constants::DEPTH_THRESH, Constants::EOF, Constants::GREATER_ICOST, Constants::G_SIZE, Constants::INCS, Constants::LESSER_ICOST, Constants::MAX_ALPHA_SIZE, Constants::MAX_BLOCK_SIZE, Constants::MAX_CODE_LEN, Constants::MAX_SELECTORS, Constants::MIN_BLOCK_SIZE, Constants::NO_RAND_PART_A_STATE, Constants::NO_RAND_PART_B_STATE, Constants::NO_RAND_PART_C_STATE, Constants::NUM_OVERSHOOT_BYTES, Constants::N_GROUPS, Constants::N_ITERS, Constants::QSORT_STACK_SIZE, Constants::RAND_PART_A_STATE, Constants::RAND_PART_B_STATE, Constants::RAND_PART_C_STATE, Constants::RNUMS, Constants::RUNA, Constants::RUNB, Constants::SETMASK, Constants::SMALL_THRESH, Constants::START_BLOCK_STATE, Constants::WORK_FACTOR
Instance Method Summary
collapse
Constructor Details
Returns a new instance of Decompressor.
12
13
14
15
16
17
18
19
20
21
22
23
|
# File 'lib/facter/util/rbzip2-0.3.0/lib/rbzip2/ruby/decompressor.rb', line 12
def initialize(io)
@buff = 0
@bytes_read = 0
@computed_combined_crc = 0
@crc = RBzip2::Ruby::CRC.new
@current_char = -1
@io = io
@live = 0
@stored_combined_crc = 0
@su_t_pos = 0
init
end
|
Instance Method Details
#bit ⇒ Object
189
190
191
|
# File 'lib/facter/util/rbzip2-0.3.0/lib/rbzip2/ruby/decompressor.rb', line 189
def bit
r(1) != 0
end
|
#check_magic ⇒ Object
117
118
119
|
# File 'lib/facter/util/rbzip2-0.3.0/lib/rbzip2/ruby/decompressor.rb', line 117
def check_magic
raise 'Magic number does not match "BZh".' unless @io.read(3) == 'BZh'
end
|
#close ⇒ Object
160
161
162
163
164
165
|
# File 'lib/facter/util/rbzip2-0.3.0/lib/rbzip2/ruby/decompressor.rb', line 160
def close
if @io != $stdin
@io = nil
@data = nil
end
end
|
#complete ⇒ Object
152
153
154
155
156
157
158
|
# File 'lib/facter/util/rbzip2-0.3.0/lib/rbzip2/ruby/decompressor.rb', line 152
def complete
@stored_combined_crc = int
@current_state = EOF
@data = nil
raise 'BZip2 CRC error' if @stored_combined_crc != @computed_combined_crc
end
|
#count(read) ⇒ Object
25
26
27
|
# File 'lib/facter/util/rbzip2-0.3.0/lib/rbzip2/ruby/decompressor.rb', line 25
def count(read)
@bytes_read += read if read != -1
end
|
#create_decode_tables(limit, base, perm, length, min_len, max_len, alpha_size) ⇒ Object
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
|
# File 'lib/facter/util/rbzip2-0.3.0/lib/rbzip2/ruby/decompressor.rb', line 201
def create_decode_tables(limit, base, perm, length, min_len, max_len, alpha_size)
pp = 0
(min_len..max_len).each do |i|
alpha_size.times do |j|
if length[j] == i
perm[pp] = j
pp += 1
end
end
end
base[1..MAX_CODE_LEN] = 0
limit[1..MAX_CODE_LEN] = 0
alpha_size.times do |i|
base[length[i] + 1] += 1
end
b = 0
1.upto(MAX_CODE_LEN - 1) do |i|
b += base[i]
base[i] = b
end
vec = 0
min_len.upto(max_len) do |i|
b = base[i]
nb = base[i + 1]
vec += nb - b
limit[i] = vec - 1
vec = vec << 1
end
(min_len + 1).upto(max_len) do |i|
base[i] = ((limit[i - 1] + 1) << 1) - base[i]
end
end
|
#create_huffman_decoding_tables(alpha_size, groups) ⇒ Object
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
|
# File 'lib/facter/util/rbzip2-0.3.0/lib/rbzip2/ruby/decompressor.rb', line 308
def create_huffman_decoding_tables(alpha_size, groups)
len = @data.temp_char_array_2d
min_lens = @data.min_lens
limit = @data.limit
base = @data.base
perm = @data.perm
groups.times do |t|
min_len = 32
max_len = 0
len_t = len[t]
(alpha_size - 1).downto 0 do |i|
lent = len_t[i]
max_len = lent if lent > max_len
min_len = lent if lent < min_len
end
create_decode_tables limit[t], base[t], perm[t], len[t], min_len, max_len, alpha_size
min_lens[t] = min_len
end
end
|
#end_block ⇒ Object
143
144
145
146
147
148
149
150
|
# File 'lib/facter/util/rbzip2-0.3.0/lib/rbzip2/ruby/decompressor.rb', line 143
def end_block
@computed_block_crc = @crc.final_crc
raise 'BZip2 CRC error' if @stored_block_crc != @computed_block_crc
@computed_combined_crc = ((@computed_combined_crc << 1) & 0xffffffff) | (@computed_combined_crc >> 31)
@computed_combined_crc ^= @computed_block_crc
end
|
#get_and_move_to_front_decode ⇒ Object
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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
|
# File 'lib/facter/util/rbzip2-0.3.0/lib/rbzip2/ruby/decompressor.rb', line 331
def get_and_move_to_front_decode
@orig_ptr = r 24
receive_decoding_tables
ll8 = @data.ll8
unzftab = @data.unzftab
selector = @data.selector
seq_to_unseq = @data.seq_to_unseq
yy = @data.get_and_move_to_front_decode_yy
min_lens = @data.min_lens
limit = @data.limit
base = @data.base
perm = @data.perm
limit_last = @block_size * BASEBLOCKSIZE
yy.fill(0..256) { |i| i }
unzftab.fill 0
group_no = 0
group_pos = G_SIZE - 1
eob = @n_in_use + 1
next_sym = get_and_move_to_front_decode0 0
@last = -1
zt = selector[group_no]
base_zt = base[zt]
limit_zt = limit[zt]
perm_zt = perm[zt]
min_lens_zt = min_lens[zt]
while next_sym != eob
if next_sym == RUNA || next_sym == RUNB
s = -1
n = 1
while true do
if next_sym == RUNA
s += n
elsif next_sym == RUNB
s += n << 1
else
break
end
if group_pos == 0
group_pos = G_SIZE - 1
group_no += 1
zt = selector[group_no]
base_zt = base[zt]
limit_zt = limit[zt]
perm_zt = perm[zt]
min_lens_zt = min_lens[zt]
else
group_pos -= 1
end
zn = min_lens_zt
while @live < zn
thech = @io.readbyte
raise 'unexpected end of stream' if thech < 0
@buff = ((@buff << 8) & 0xffffffff) | thech
@live += 8
end
zvec = ((@buff >> (@live - zn)) & 0xffffffff) & ((1 << zn) - 1)
@live -= zn
while zvec > limit_zt[zn]
zn += 1
while @live < 1
thech = @io.readbyte
raise 'unexpected end of stream' if thech < 0
@buff = ((@buff << 8) & 0xffffffff) | thech
@live += 8
end
@live -= 1
zvec = (zvec << 1) | ((@buff >> @live) & 1)
end
next_sym = perm_zt[zvec - base_zt[zn]]
n = n << 1
end
ch = seq_to_unseq[yy[0]]
unzftab[ch & 0xff] += s + 1
while s >= 0
@last += 1
ll8[@last] = ch
s -= 1
end
raise 'block overrun' if @last >= limit_last
else
@last += 1
raise 'block overrun' if @last >= limit_last
tmp = yy[next_sym - 1]
unzftab[seq_to_unseq[tmp]] += 1
ll8[@last] = seq_to_unseq[tmp]
yy[1, next_sym - 1] = yy[0, next_sym - 1]
yy[0] = tmp
if group_pos == 0
group_pos = G_SIZE - 1
group_no += 1
zt = selector[group_no]
base_zt = base[zt]
limit_zt = limit[zt]
perm_zt = perm[zt]
min_lens_zt = min_lens[zt]
else
group_pos -= 1
end
zn = min_lens_zt
while @live < zn
thech = @io.readbyte
raise 'unexpected end of stream' if thech < 0
@buff = ((@buff << 8) & 0xffffffff) | thech
@live += 8
end
zvec = (@buff >> (@live - zn)) & ((1 << zn) - 1)
@live -= zn
while zvec > limit_zt[zn]
zn += 1
while @live < 1
thech = @io.readbyte
raise 'unexpected end of stream' if thech < 0
@buff = ((@buff << 8) & 0xffffffff) | thech
@live += 8
end
@live -= 1
zvec = (zvec << 1) | ((@buff >> @live) & 1)
end
next_sym = perm_zt[zvec - base_zt[zn]]
end
end
end
|
#get_and_move_to_front_decode0(group_no) ⇒ Object
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
|
# File 'lib/facter/util/rbzip2-0.3.0/lib/rbzip2/ruby/decompressor.rb', line 486
def get_and_move_to_front_decode0(group_no)
zt = @data.selector[group_no]
limit_zt = @data.limit[zt]
zn = @data.min_lens[zt]
zvec = r zn
while zvec > limit_zt[zn]
zn += 1
while @live < 1
thech = @io.readbyte
raise 'unexpected end of stream' if thech < 0
@buff = ((@buff << 8) & 0xffffffff) | thech
@live += 8
end
@live -=1
zvec = (zvec << 1) | ((@buff >> @live) & 1)
end
@data.perm[zt][zvec - @data.base[zt][zn]]
end
|
#getc ⇒ Object
29
30
31
|
# File 'lib/facter/util/rbzip2-0.3.0/lib/rbzip2/ruby/decompressor.rb', line 29
def getc
read 1
end
|
#gets ⇒ Object
33
34
35
36
37
38
39
40
41
|
# File 'lib/facter/util/rbzip2-0.3.0/lib/rbzip2/ruby/decompressor.rb', line 33
def gets
line = ''
loop do
char = getc
line += char
break if char == "\n"
end
line
end
|
#init ⇒ Object
106
107
108
109
110
111
112
113
114
115
|
# File 'lib/facter/util/rbzip2-0.3.0/lib/rbzip2/ruby/decompressor.rb', line 106
def init
check_magic
block_size = @io.read(1).to_i
raise 'Illegal block size.' if block_size < 1 || block_size > 9
@block_size = block_size
init_block
setup_block
end
|
#init_block ⇒ Object
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
|
# File 'lib/facter/util/rbzip2-0.3.0/lib/rbzip2/ruby/decompressor.rb', line 121
def init_block
magic = [ubyte, ubyte, ubyte, ubyte, ubyte, ubyte]
if magic == [0x17, 0x72, 0x45, 0x38, 0x50, 0x90]
complete
elsif magic != [0x31, 0x41, 0x59, 0x26, 0x53, 0x59]
@current_state = EOF
raise 'Bad block header.'
else
@stored_block_crc = int
@block_randomised = bit
@data = RBzip2::Ruby::InputData.new @block_size if @data.nil?
get_and_move_to_front_decode
@crc.initialize_crc
@current_state = START_BLOCK_STATE
end
end
|
#inspect ⇒ Object
679
680
681
|
# File 'lib/facter/util/rbzip2-0.3.0/lib/rbzip2/ruby/decompressor.rb', line 679
def inspect
"#<#{self.class}: @io=#{@io.inspect} size=#{size} uncompressed=#{uncompressed}>"
end
|
#int ⇒ Object
197
198
199
|
# File 'lib/facter/util/rbzip2-0.3.0/lib/rbzip2/ruby/decompressor.rb', line 197
def int
r 32
end
|
#make_maps ⇒ Object
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
|
# File 'lib/facter/util/rbzip2-0.3.0/lib/rbzip2/ruby/decompressor.rb', line 90
def make_maps
in_use = @data.in_use
seq_to_unseq = @data.seq_to_unseq
n_in_use_shadow = 0
256.times do |i|
if in_use[i]
seq_to_unseq[n_in_use_shadow] = i
n_in_use_shadow += 1
end
end
@n_in_use = n_in_use_shadow
end
|
#r(n) ⇒ Object
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
|
# File 'lib/facter/util/rbzip2-0.3.0/lib/rbzip2/ruby/decompressor.rb', line 167
def r(n)
live_shadow = @live
buff_shadow = @buff
if live_shadow < n
begin
thech = @io.readbyte
raise 'unexpected end of stream' if thech < 0
buff_shadow = (buff_shadow << 8) | thech
live_shadow += 8
end while live_shadow < n
@buff = buff_shadow
end
@live = live_shadow - n
(buff_shadow >> (live_shadow - n)) & ((1 << n) - 1)
end
|
#read(length = nil) ⇒ Object
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
|
# File 'lib/facter/util/rbzip2-0.3.0/lib/rbzip2/ruby/decompressor.rb', line 43
def read(length = nil)
raise 'stream closed' if @io.nil?
if length == 1
r = read0
count (r < 0 ? -1 : 1)
r.chr
else
r = ''
if length == nil
while true do
b = read0
break if b < 0
r << b.chr
end
elsif length > 0
length.times do
b = read0
break if b < 0
r << b.chr
end
count r.size
end
r
end
end
|
#read0 ⇒ Object
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
|
# File 'lib/facter/util/rbzip2-0.3.0/lib/rbzip2/ruby/decompressor.rb', line 70
def read0
ret_char = @current_char
if @current_state == RAND_PART_B_STATE
setup_rand_part_b
elsif @current_state == NO_RAND_PART_B_STATE
setup_no_rand_part_b
elsif @current_state == RAND_PART_C_STATE
setup_rand_part_c
elsif @current_state == NO_RAND_PART_C_STATE
setup_no_rand_part_c
elsif @current_state == EOF
return -1
else
raise 'illegal state'
end
ret_char
end
|
#receive_decoding_tables ⇒ Object
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
|
# File 'lib/facter/util/rbzip2-0.3.0/lib/rbzip2/ruby/decompressor.rb', line 239
def receive_decoding_tables
in_use = @data.in_use
pos = @data.receive_decoding_tables_pos
selector = @data.selector
selector_mtf = @data.selector_mtf
in_use16 = 0
16.times do |i|
in_use16 |= 1 << i if bit
end
in_use.fill false
16.times do |i|
if (in_use16 & (1 << i)) != 0
i16 = i << 4
16.times do |j|
in_use[i16 + j] = true if bit
end
end
end
make_maps
alpha_size = @n_in_use + 2
groups = r 3
selectors = r 15
selectors.times do |i|
j = 0
while bit
j += 1
end
selector_mtf[i] = j & 0xff
end
pos.fill(0..groups) { |v| v & 0xff }
selectors.times do |i|
v = selector_mtf[i]
tmp = pos[v]
while v > 0 do
v -= 1
pos[v + 1] = pos[v]
end
pos[0] = tmp
selector[i] = tmp
end
len = @data.temp_char_array_2d
groups.times do |t|
curr = r 5
len_t = len[t]
alpha_size.times do |i|
while bit
curr += bit ? -1 : 1
end
len_t[i] = curr
end
@data.temp_char_array_2d[t] = len_t
end
create_huffman_decoding_tables alpha_size, groups
end
|
#setup_block ⇒ Object
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
|
# File 'lib/facter/util/rbzip2-0.3.0/lib/rbzip2/ruby/decompressor.rb', line 511
def setup_block
return if @data.nil?
cftab = @data.cftab
tt = @data.init_tt @last + 1
ll8 = @data.ll8
cftab[0] = 0
cftab[1, 256] = @data.unzftab[0, 256]
c = 0
1.upto(256) do |i|
c += cftab[i]
cftab[i] = c
end
(@last + 1).times do |i|
cftab_i = ll8[i]
tt[cftab[cftab_i]] = i
cftab[cftab_i] += 1
end
raise 'stream corrupted' if @orig_ptr < 0 || @orig_ptr >= tt.size
@su_t_pos = tt[@orig_ptr]
@su_count = 0
@su_i2 = 0
@su_ch2 = 256
if @block_randomised
@su_r_n_to_go = 0
@su_r_t_pos = 0
setup_rand_part_a
else
setup_no_rand_part_a
end
end
|
#setup_no_rand_part_a ⇒ Object
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
|
# File 'lib/facter/util/rbzip2-0.3.0/lib/rbzip2/ruby/decompressor.rb', line 575
def setup_no_rand_part_a
if @su_i2 <= @last
@su_ch_prev = @su_ch2
su_ch2_shadow = @data.ll8[@su_t_pos]
@su_ch2 = su_ch2_shadow
@su_t_pos = @data.tt[@su_t_pos]
@su_i2 += 1
@current_char = su_ch2_shadow
@current_state = NO_RAND_PART_B_STATE
@crc.update_crc su_ch2_shadow
else
@current_state = NO_RAND_PART_A_STATE
end_block
init_block
setup_block
end
end
|
#setup_no_rand_part_b ⇒ Object
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
|
# File 'lib/facter/util/rbzip2-0.3.0/lib/rbzip2/ruby/decompressor.rb', line 636
def setup_no_rand_part_b
if @su_ch2 != @su_ch_prev
@su_count = 1
setup_no_rand_part_a
else
@su_count += 1
if @su_count >= 4
@su_z = @data.ll8[@su_t_pos]
@su_t_pos = @data.tt[@su_t_pos]
@su_j2 = 0
setup_no_rand_part_c
else
setup_no_rand_part_a
end
end
end
|
#setup_no_rand_part_c ⇒ Object
653
654
655
656
657
658
659
660
661
662
663
664
665
|
# File 'lib/facter/util/rbzip2-0.3.0/lib/rbzip2/ruby/decompressor.rb', line 653
def setup_no_rand_part_c
if @su_j2 < @su_z
su_ch2_shadow = @su_ch2
@current_char = su_ch2_shadow
@crc.update_crc su_ch2_shadow
@su_j2 += 1
@current_state = NO_RAND_PART_C_STATE
else
@su_i2 += 1
@su_count = 0
setup_no_rand_part_a
end
end
|
#setup_rand_part_a ⇒ Object
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
|
# File 'lib/facter/util/rbzip2-0.3.0/lib/rbzip2/ruby/decompressor.rb', line 549
def setup_rand_part_a
if @su_i2 <= @last
@su_ch_prev = @su_ch2
su_ch2_shadow = @data.ll8[@su_t_pos]
@su_t_pos = @data.tt[@su_t_pos]
if @su_r_n_to_go == 0
@su_r_n_to_go = RNUMS[@su_r_t_pos] - 1
@su_r_t_pos += 1
@su_r_t_pos = 0 if @su_r_t_pos == 512
else
@su_r_n_to_go -= 1
end
@su_ch2 = su_ch2_shadow ^= (@su_r_n_to_go == 1) ? 1 : 0
@su_i2 += 1
@current_char = su_ch2_shadow
@current_state = RAND_PART_B_STATE
@crc.update_crc su_ch2_shadow
else
end_block
init_block
setup_block
end
end
|
#setup_rand_part_b ⇒ Object
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
|
# File 'lib/facter/util/rbzip2-0.3.0/lib/rbzip2/ruby/decompressor.rb', line 593
def setup_rand_part_b
if @su_ch2 != @su_ch_prev
@current_state = RAND_PART_A_STATE
@su_count = 1
setup_rand_part_a
else
@su_count += 1
if @su_count >= 4
@su_z = @data.ll8[@su_t_pos]
@su_t_pos = @data.tt[@su_t_pos]
if @su_r_n_to_go == 0
@su_r_n_to_go = RNUMS[@su_r_t_pos] - 1
@su_r_t_pos += 1
@su_r_t_pos = 0 if @su_r_t_pos == 512
else
@su_r_n_to_go -= 1
end
@su_j2 = 0
@current_state = RAND_PART_C_STATE
@su_z ^= 1 if @su_r_n_to_go == 1
setup_rand_part_c
else
@current_state = RAND_PART_A_STATE
setup_rand_part_a
end
end
end
|
#setup_rand_part_c ⇒ Object
623
624
625
626
627
628
629
630
631
632
633
634
|
# File 'lib/facter/util/rbzip2-0.3.0/lib/rbzip2/ruby/decompressor.rb', line 623
def setup_rand_part_c
if @su_j2 < @su_z
@current_char = @su_ch2
@crc.update_crc @su_ch2
@su_j2 += 1
else
@current_state = RAND_PART_A_STATE
@su_i2 += 1
@su_count = 0
setup_rand_part_a
end
end
|
#size ⇒ Object
667
668
669
670
671
672
673
|
# File 'lib/facter/util/rbzip2-0.3.0/lib/rbzip2/ruby/decompressor.rb', line 667
def size
if @io.is_a? StringIO
@io.size
elsif @io.is_a? File
@io.stat.size
end
end
|
#ubyte ⇒ Object
193
194
195
|
# File 'lib/facter/util/rbzip2-0.3.0/lib/rbzip2/ruby/decompressor.rb', line 193
def ubyte
r 8
end
|
#uncompressed ⇒ Object
675
676
677
|
# File 'lib/facter/util/rbzip2-0.3.0/lib/rbzip2/ruby/decompressor.rb', line 675
def uncompressed
@last + 1
end
|