Class: RBzip2::Decompressor

Inherits:
Object
  • Object
show all
Includes:
Constants
Defined in:
lib/rbzip2/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

#initialize(io) ⇒ Decompressor

Returns a new instance of Decompressor.



12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/rbzip2/decompressor.rb', line 12

def initialize(io)
  @buff = 0
  @bytes_read = 0
  @computed_combined_crc = 0
  @crc = RBzip2::CRC.new
  @current_char = -1
  @io = io
  @live = 0
  @stored_combined_crc = 0
  @su_t_pos = 0
  init
end

Instance Method Details

#bitObject



189
190
191
# File 'lib/rbzip2/decompressor.rb', line 189

def bit
  r(1) != 0
end

#check_magicObject



112
113
114
# File 'lib/rbzip2/decompressor.rb', line 112

def check_magic
  raise 'Magic number does not match "BZh".' unless @io.read(3) == 'BZh'
end

#closeObject



160
161
162
163
164
165
# File 'lib/rbzip2/decompressor.rb', line 160

def close
  if @io != $stdin
    @io = nil
    @data = nil
  end
end

#completeObject



152
153
154
155
156
157
158
# File 'lib/rbzip2/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/rbzip2/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
238
239
240
# File 'lib/rbzip2/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

  MAX_CODE_LEN.downto 1 do |i|
    base[i] = 0
    limit[i] = 0
  end

  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
    b = nb
    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



314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
# File 'lib/rbzip2/decompressor.rb', line 314

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_blockObject



138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/rbzip2/decompressor.rb', line 138

def end_block
  @computed_block_crc = @crc.final_crc

  if @stored_block_crc != @computed_block_crc
    @computed_combined_crc = (@stored_combined_crc << 1) | (@stored_combined_crc >> 31)
    @computed_combined_crc ^= @stored_block_crc

    raise 'BZip2 CRC error'
  end

  @computed_combined_crc = (@computed_combined_crc << 1) | (@computed_combined_crc >> 31)
  @computed_combined_crc ^= @computed_block_crc
end

#get_and_move_to_front_decodeObject



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
485
486
487
488
489
490
491
492
493
494
495
496
497
498
# File 'lib/rbzip2/decompressor.rb', line 337

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

  256.downto(0) do |i|
    yy[i] = i
    unzftab[i] = 0
  end

  group_no = 0
  group_pos = G_SIZE - 1
  eob = @n_in_use + 1
  next_sym = get_and_move_to_front_decode0 0
  buff_shadow = @buff
  live_shadow = @live
  last_shadow = -1
  zt = selector[group_no] & 0xff
  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
      loop 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] & 0xff
          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_shadow < zn
          thech = @io.read(1)[0].ord

          raise 'unexpected end of stream' if thech < 0

          buff_shadow = (buff_shadow << 8) | thech
          live_shadow += 8
        end

        zvec = (buff_shadow >> (live_shadow - zn)) & ((1 << zn) - 1)
        live_shadow -= zn

        while zvec > limit_zt[zn]
          zn += 1

          while live_shadow < 1
            thech = @io.read(1)[0].ord

            raise 'unexpected end of stream' if thech < 0

            buff_shadow = (buff_shadow << 8) | thech
            live_shadow += 8
          end

          live_shadow -= 1
          zvec = (zvec << 1) | ((buff_shadow >> live_shadow) & 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_shadow += 1
        ll8[last_shadow] = ch
        s -= 1
      end

      raise 'block overrun' if last_shadow >= limit_last
    else
      last_shadow += 1
      raise 'block overrun' if last_shadow >= limit_last

      tmp = yy[next_sym - 1]
      unzftab[seq_to_unseq[tmp] & 0xff] += 1
      ll8[last_shadow] = 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] & 0xff
        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_shadow < zn
        thech = @io.read(1)[0].ord

        raise 'unexpected end of stream' if thech < 0

        buff_shadow = (buff_shadow << 8) | thech
        live_shadow += 8
      end
      zvec = (buff_shadow >> (live_shadow - zn)) & ((1 << zn) - 1)
      live_shadow -= zn

      while zvec > limit_zt[zn]
        zn += 1
        while live_shadow < 1
          thech = @io.read(1)[0].ord

          raise 'unexpected end of stream' if thech < 0

          buff_shadow = (buff_shadow << 8) | thech
          live_shadow += 8
        end
        live_shadow -= 1
        zvec = (zvec << 1) | ((buff_shadow >> live_shadow) & 1)
      end

      next_sym = perm_zt[zvec - base_zt[zn]]
    end
  end

  @last = last_shadow
  @live = live_shadow
  @buff = buff_shadow
end

#get_and_move_to_front_decode0(group_no) ⇒ Object



500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
# File 'lib/rbzip2/decompressor.rb', line 500

def get_and_move_to_front_decode0(group_no)
  zt = @data.selector[group_no] & 0xff
  limit_zt = @data.limit[zt]
  zn = @data.min_lens[zt]
  zvec = r zn
  live_shadow = @live
  buff_shadow = @buff

  while zvec > limit_zt[zn]
    zn += 1

    while live_shadow < 1
      thech = @io.read(1)[0].ord

      raise 'unexpected end of stream' if thech < 0

      buff_shadow = (buff_shadow << 8) | thech
      live_shadow += 8
    end

    live_shadow -=1
    zvec = (zvec << 1) | ((buff_shadow >> live_shadow) & 1)
  end

  @live = live_shadow
  @buff = buff_shadow

  @data.perm[zt][zvec - @data.base[zt][zn]]
end

#initObject



101
102
103
104
105
106
107
108
109
110
# File 'lib/rbzip2/decompressor.rb', line 101

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_blockObject



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/rbzip2/decompressor.rb', line 116

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::InputData.new @block_size if @data.nil?

    get_and_move_to_front_decode

    @crc.initialize_crc
    @current_state = START_BLOCK_STATE
  end
end

#inspectObject



699
700
701
# File 'lib/rbzip2/decompressor.rb', line 699

def inspect
  "#<#{self.class}: @io=#{@io.inspect} size=#{size} uncompressed=#{uncompressed}>"
end

#intObject



197
198
199
# File 'lib/rbzip2/decompressor.rb', line 197

def int
  (((((r(8) << 8) | r(8)) << 8) | r(8)) << 8) | r(8)
end

#make_mapsObject



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/rbzip2/decompressor.rb', line 85

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/rbzip2/decompressor.rb', line 167

def r(n)
  live_shadow = @live
  buff_shadow = @buff

  if live_shadow < n
    begin
      thech = @io.read(1)[0].ord

      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



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/rbzip2/decompressor.rb', line 29

def read(length = nil)
  raise 'stream closed' if @io.nil?

  if length == 1
    r = read0
    count (r < 0 ? -1 : 1)
    r
  else
    r = StringIO.new
    if length == nil
      loop do
        b = read0
        break if b < 0
        r.write b.chr
      end
    elsif length > 0
      length.times do
        b = read0
        break if b < 0
        r.write b.chr
      end
      count r.size
    end
    r.string
  end
end

#read0Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/rbzip2/decompressor.rb', line 56

def read0
  ret_char = @current_char

  case @current_state
    when EOF
      return -1

    when RAND_PART_B_STATE
      setup_rand_part_b

    when RAND_PART_C_STATE
      setup_rand_part_c

    when NO_RAND_PART_B_STATE
      setup_no_rand_part_b

    when NO_RAND_PART_C_STATE
      setup_no_rand_part_c

    when START_BLOCK_STATE
    when RAND_PART_A_STATE
    when NO_RAND_PART_A_STATE
    else
      raise 'illegal state'
  end

  ret_char
end

#receive_decoding_tablesObject



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
307
308
309
310
311
312
# File 'lib/rbzip2/decompressor.rb', line 242

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

  255.downto(0) do |i|
    in_use[i] = false
  end

  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
  end

  groups.downto(0) do |v|
    pos[v] = v
  end

  selectors.times do |i|
    v = selector_mtf[i] & 0xff
    tmp = pos[v]

    while v > 0 do
      pos[v] = pos[v -= 1]
    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_blockObject



530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
# File 'lib/rbzip2/decompressor.rb', line 530

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 = cftab[0]
  1.upto(256) do |i|
    c += cftab[i]
    cftab[i] = c
  end

  last_shadow = @last
  (last_shadow + 1).times do |i|
    cftab_i = ll8[i] & 0xff
    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_aObject



595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
# File 'lib/rbzip2/decompressor.rb', line 595

def setup_no_rand_part_a
  if @su_i2 <= @last
    @su_ch_prev = @su_ch2
    su_ch2_shadow = @data.ll8[@su_t_pos] & 0xff
    @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_bObject



656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
# File 'lib/rbzip2/decompressor.rb', line 656

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] & 0xff
      @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_cObject



673
674
675
676
677
678
679
680
681
682
683
684
685
# File 'lib/rbzip2/decompressor.rb', line 673

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_aObject



569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
# File 'lib/rbzip2/decompressor.rb', line 569

def setup_rand_part_a
  if @su_i2 <= @last
    @su_ch_prev = @su_ch2
    su_ch2_shadow = @data.ll8[@su_t_pos] & 0xff
    @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_bObject



613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
# File 'lib/rbzip2/decompressor.rb', line 613

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] & 0xff
      @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_cObject



643
644
645
646
647
648
649
650
651
652
653
654
# File 'lib/rbzip2/decompressor.rb', line 643

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

#sizeObject



687
688
689
690
691
692
693
# File 'lib/rbzip2/decompressor.rb', line 687

def size
  if @io.is_a? StringIO
    @io.size
  elsif @io.is_a? File
    @io.stat.size
  end
end

#ubyteObject



193
194
195
# File 'lib/rbzip2/decompressor.rb', line 193

def ubyte
  r 8
end

#uncompressedObject



695
696
697
# File 'lib/rbzip2/decompressor.rb', line 695

def uncompressed
  @last + 1
end