Class: Bzip3::Decoder

Inherits:
Object
  • Object
show all
Defined in:
lib/extbzip3/fordoc.rb,
lib/extbzip3.rb,
lib/extbzip3/decoder.rb,
ext/extbzip3_decoder.c

Overview

Decoder class.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(inport, blocksize = 16 << 20) ⇒ Decoder

Returns a new instance of Decoder.



5
6
7
8
9
10
11
12
13
14
# File 'lib/extbzip3/decoder.rb', line 5

def initialize(inport, blocksize = 16 << 20)
  @bzip3 = BlockProcessor.new(blocksize)
  @inport = inport
  @readbuf = nil
  @destbuf = nil
  @closed = false
  @eof = false

  self
end

Class Method Details

.decode(src, maxdest = nil, dest = "", **opts) ⇒ String .decode(src, dest, **opts) ⇒ String

decode bzip3 sequence.

Parameters:

  • src (String)

    describe bzip3 sequence

  • maxdest (Integer)

    describe maximum dest size

  • dest (String)

    describe destination

  • opts (Hash)

Returns:

  • (String)

    dest for decoded bzip3



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
# File 'ext/extbzip3_decoder.c', line 416

static VALUE
decoder_s_decode(int argc, VALUE argv[], VALUE mod)
{
    size_t insize, outsize;
    struct { VALUE src, maxdest, dest, opts; } args;
    argc = rb_scan_args(argc, argv, "12:", &args.src, &args.maxdest, &args.dest, &args.opts);

    enum { numkw = 4 };
    ID idtab[numkw] = { rb_intern("concat"), rb_intern("partial"), rb_intern("blocksize"), rb_intern("format") };
    union { struct { VALUE concat, partial, blocksize, format; }; VALUE vect[numkw]; } opts;
    rb_get_kwargs(args.opts, idtab, 0, numkw, opts.vect);

    switch (argc) {
    case 1:
        insize = RSTRING_LEN(args.src);
        outsize = aux_scan_size(aux_conv_to_format(opts.format), RSTRING_PTR(args.src), RSTRING_END(args.src), RB_UNDEF_P(opts.concat) || RTEST(opts.concat));
        args.dest = rb_str_buf_new(outsize);
        break;
    case 2:
        insize = RSTRING_LEN(args.src);

        if (rb_type_p(args.maxdest, RUBY_T_FIXNUM) || rb_type_p(args.maxdest, RUBY_T_BIGNUM)) {
            outsize = NUM2SIZET(args.maxdest);
            args.dest = rb_str_buf_new(outsize);
        } else {
            args.dest = args.maxdest;
            outsize = aux_scan_size(aux_conv_to_format(opts.format), RSTRING_PTR(args.src), RSTRING_END(args.src), RB_UNDEF_P(opts.concat) || RTEST(opts.concat));
            rb_str_modify(args.dest);
            rb_str_set_len(args.dest, 0);
            rb_str_modify_expand(args.dest, outsize);
        }

        break;
    case 3:
        insize = RSTRING_LEN(args.src);
        outsize = NUM2SIZET(args.maxdest);
        rb_str_modify(args.dest);
        rb_str_set_len(args.dest, 0);
        rb_str_modify_expand(args.dest, outsize);

        break;
    }

    // TODO: maxdest, partial

    int status = aux_oneshot_decode(RSTRING_PTR(args.src), RSTRING_PTR(args.dest), insize, &outsize,
                                    aux_conv_to_format(opts.format),
                                    (RB_NIL_OR_UNDEF_P(opts.blocksize) ? (16 << 20) : NUM2INT(opts.blocksize)),
                                    RB_UNDEF_P(opts.concat) || RTEST(opts.concat));
    extbzip3_check_error(status);

    rb_str_set_len(args.dest, outsize);

    return args.dest;
}

.open(*args, **opts, &block) ⇒ Object



39
40
41
42
43
44
45
46
47
48
# File 'lib/extbzip3.rb', line 39

def open(*args, **opts, &block)
  bz3 = new(*args, **opts)
  return bz3 unless block

  begin
    yield bz3
  ensure
    bz3.close unless bz3.closed?
  end
end

Instance Method Details

#closeObject



372
373
374
375
376
377
378
379
# File 'ext/extbzip3_decoder.c', line 372

def close
  @closed = true
  @bzip3 = nil
  @readbuf = nil
  @destbuf = nil

  self
end

#closed?Boolean

Returns:

  • (Boolean)


386
387
388
# File 'ext/extbzip3_decoder.c', line 386

def closed?
  @closed
end

#eof?Boolean Also known as: eof

Returns:

  • (Boolean)


392
393
394
# File 'ext/extbzip3_decoder.c', line 392

def eof?
  @eof
end

#read(size = nil, dest = "") ⇒ Object



308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
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
# File 'ext/extbzip3_decoder.c', line 308

def read(size = nil, buf = "".b)
  buf ||= "".b
  buf[0..-1] = "".b

  return buf if size == 0

  size &&= 0 | Integer(0 + size)

  @readbuf ||= "".b

  while true
    if @destbuf
      if size && @destbuf.bytesize >= size
        buf << @destbuf.byteslice(0, size)
        @destbuf[0...size] = "".b
        break
      else
        buf << @destbuf
        size -= @destbuf.bytesize if size
        @destbuf[0..-1] = "".b
      end
    else
      @destbuf = "".b

      (sig, bs) = @inport.read(9)&.unpack("a5V")
      raise "意図しない EOF" unless bs
      raise "シグネチャが違う" unless sig == "BZ3v1"
      raise "おかしなブロックサイズ" unless ((65 << 10) .. (511 << 20)).include? bs
      raise "ブロックサイズがでかすぎ" unless bs <= @bzip3.blocksize
    end

    unless b1 = @inport.read(8)
      @eof = true
      break
    end
    raise "意図しない EOF" if b1.bytesize < 8
    if b1.byteslice(0, 5) == "BZ3v1"
      b2 = @inport.read(1)
      if b2 && b2.bytesize > 0
        b1 << b2
        bs = b1.unpack1("@5V")
        raise "ブロックサイズがでかすぎ" unless bs <= @bzip3.blocksize
        next
      end
    end
    (packedsize, originsize) = b1.unpack("VV")
    raise "元のデータがデカすぎ" unless originsize <= @bzip3.blocksize
    raise "意図しない EOF" unless @inport.read(packedsize, @readbuf)
    raise "大きさがあってない" unless @readbuf.bytesize == packedsize
    @bzip3.decode(@readbuf, @destbuf, originsize)
  end

  buf.empty? ? nil : buf
end