From 41f536bd46b38e1f9e7a0a5f26b54570a656f1ea Mon Sep 17 00:00:00 2001 From: Alberto Restifo Date: Sun, 19 Apr 2020 13:22:50 +0200 Subject: [PATCH] Add support for Binary decoding --- src/cbor/lexer.cr | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/src/cbor/lexer.cr b/src/cbor/lexer.cr index 53261ff..ebd480b 100644 --- a/src/cbor/lexer.cr +++ b/src/cbor/lexer.cr @@ -70,6 +70,17 @@ class CBOR::Lexer consume_int(to_negative_int(read(UInt32))) when 0x3b consume_int(to_negative_int(read(UInt64))) + when 0x40..0x57 + # Reads a single byte which is offset by 0x40 + Token::BytesT.new(@current_byte_number, value: Bytes[current_byte - 0x40]) + when 0x58 + consume_binary(read(UInt8)) + when 0x59 + consume_binary(read(UInt16)) + when 0x5a + consume_binary(read(UInt32)) + when 0x5b + consume_binary(read(UInt64)) else fail end @@ -86,6 +97,16 @@ class CBOR::Lexer Token::IntT.new(@current_byte_number, value) end + private def consume_binary(size) + bytes = Bytes.new(size) + @io.read_fully(bytes) + @byte_number += size + Token::BytesT.new(@current_byte_number, bytes) + end + + # Creates a method overloaded for each UInt sizes to convert the UInt into + # the respective Int capable of containing the value + {% begin %} {% uints = %w(UInt8 UInt16 UInt32 UInt64) %} {% conv = %w(to_i8 to_i16 to_i32 to_i64 to_i128) %}