Class: Krypt::ASN1::Instream

Inherits:
Object
  • Object
show all
Defined in:
ext/krypt/core/krypt_asn1_in_adapter.c,
ext/krypt/core/krypt_asn1_in_adapter.c

Overview

Acts as a drop-in replacement for an IO. It cannot be instantiated on its own, instances may be obtained by calling Header#value_io. Instream supports a reduced subset of the interface defined by IO.

Example usage

Reading the contents of an Instream

der_io = # some IO representing a DER-encoded ASN.1 value
parser = Krypt::ASN1::Parser.new
token = parser.next(der_io)
instream = token.value_io
value = instream.read # contains the raw bytes of the token's value

Instance Method Summary collapse

Instance Method Details

#read([len = nil], [buf = nil]) ⇒ String?

Please see IO#read for details.

Returns:

  • (String, nil)


88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'ext/krypt/core/krypt_asn1_in_adapter.c', line 88

static VALUE
krypt_instream_adapter_read(int argc, VALUE *argv, VALUE self)
{
    krypt_instream_adapter *adapter;
    VALUE ret;
    VALUE vlen = Qnil;
    VALUE vbuf = Qnil;

    rb_scan_args(argc, argv, "02", &vlen, &vbuf);

    int_krypt_instream_adapter_get(self, adapter);

    if (binyo_instream_rb_read(adapter->in, vlen, vbuf, &ret) == BINYO_ERR)
	rb_raise(eKryptError, "Error reading stream");
    return ret;
}

#seek(n, [whence = :SEEK_SET]) ⇒ 0

Please see IO#seek for details.

Returns:

  • (0)


132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'ext/krypt/core/krypt_asn1_in_adapter.c', line 132

static VALUE
krypt_instream_adapter_seek(int argc, VALUE *argv, VALUE self)
{
    VALUE n, vwhence = sBinyo_ID_SEEK_SET;
    int whence;
    krypt_instream_adapter *adapter;

    rb_scan_args(argc, argv, "11", &n, &whence);

    int_krypt_instream_adapter_get(self, adapter);
    whence = int_whence_for(vwhence);
    if (binyo_instream_seek(adapter->in, NUM2INT(n), whence) == BINYO_ERR)
        rb_raise(eKryptASN1ParseError, "Seek failed");

    return INT2FIX(0); /* same as rb_io_seek */
}