diff --git a/ext/bert/c/decode.c b/ext/bert/c/decode.c index 85c08e1..3e492f0 100644 --- a/ext/bert/c/decode.c +++ b/ext/bert/c/decode.c @@ -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)