This commit is contained in:
Aaron Patterson 2016-04-14 15:02:07 -07:00
Родитель aa084e74f0
Коммит 31ab659338
2 изменённых файлов: 7 добавлений и 34 удалений

Просмотреть файл

@ -27,14 +27,9 @@ static VALUE rb_mBERT;
static VALUE rb_cDecode;
static VALUE rb_cTuple;
typedef struct bert_buf bert_buf;
typedef VALUE (*bert_ptr)(struct bert_buf *buf);
struct bert_buf {
const uint8_t *data;
const uint8_t *end;
bert_ptr *callbacks;
};
static VALUE bert_read_invalid(struct bert_buf *buf);
@ -51,10 +46,10 @@ static VALUE bert_read_list(struct bert_buf *buf);
static VALUE bert_read_bin(struct bert_buf *buf);
static VALUE bert_read_enc_string(struct bert_buf *buf);
static VALUE bert_read_unicode_string(struct bert_buf *buf);
static VALUE bert_read_unicode_string(struct bert_buf *buf);
static VALUE bert_read_sbignum(struct bert_buf *buf);
static VALUE bert_read_lbignum(struct bert_buf *buf);
typedef VALUE (*bert_ptr)(struct bert_buf *buf);
static bert_ptr bert_callbacks[] = {
&bert_read_sint,
&bert_read_int,
@ -118,7 +113,7 @@ static VALUE bert_read(struct bert_buf *buf)
if (!BERT_VALID_TYPE(type))
rb_raise(rb_eRuntimeError, "Invalid tag '%d' for term", type);
return buf->callbacks[type - BERT_TYPE_OFFSET](buf);
return bert_callbacks[type - BERT_TYPE_OFFSET](buf);
}
static VALUE bert_read_dict(struct bert_buf *buf)
@ -517,17 +512,11 @@ static VALUE rb_bert_decode(VALUE klass, VALUE rb_string)
bert_buf_ensure(&buf, 1);
proto_version = bert_buf_read8(&buf);
switch(proto_version) {
case ERL_VERSION:
buf.callbacks = bert_callbacks;
break;
case ERL_VERSION2:
buf.callbacks = bert_callbacks;
break;
default:
rb_raise(rb_eTypeError, "Invalid magic value for BERT string");
if (proto_version == ERL_VERSION || proto_version == ERL_VERSION2) {
return bert_read(&buf);
} else {
rb_raise(rb_eTypeError, "Invalid magic value for BERT string");
}
return bert_read(&buf);
}
static VALUE rb_bert_impl(VALUE klass)

Просмотреть файл

@ -3,20 +3,6 @@ module BERT
attr_accessor :in
include Types
class V1 < Decode
def read_bin
fail("Invalid Type, not an erlang binary") unless read_1 == BIN
length = read_4
read_string(length)
end
def read_erl_string
fail("Invalid Type, not an erlang string") unless read_1 == STRING
length = read_2
read_string(length).unpack('C' * length)
end
end
def self.impl
'Ruby'
end
@ -26,9 +12,7 @@ module BERT
io.set_encoding('binary') if io.respond_to?(:set_encoding)
header = io.getbyte
case header
when MAGIC
Decode::V1.new(io).read_any
when VERSION_2
when MAGIC, VERSION_2
new(io).read_any
else
fail("Bad Magic")