Skip to content

Commit

Permalink
Add remaining varint unchecked conversions
Browse files Browse the repository at this point in the history
  • Loading branch information
masonticehurst committed Feb 13, 2024
1 parent c37fc47 commit 95a77c9
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 13 deletions.
36 changes: 34 additions & 2 deletions eRINA_STM32F7/src/net/protobuf.adb
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ package body Protobuf is
end case;
end Tag_To_Wire_Type;

function To_VARINT (V : in Byte_Vector) return Uint64 is
function VARINT_To_Uint64 (V : in Byte_Vector) return Uint64 is
Working_Bit_Array : Bit_Array (1 .. 64) := (others => 0);
begin
-- MT: TODO: dropping this requirement for now
Expand Down Expand Up @@ -72,5 +72,37 @@ package body Protobuf is

-- MT: TODO: May need some idiot proofing
return Bit_Array_To_Uint64 (Working_Bit_Array);
end To_VARINT;
end VARINT_To_Uint64;

function VARINT_To_Uint32 (V : in Byte_Vector) return Uint32 is
function To_Uint32 is new Ada.Unchecked_Conversion(Uint64, Uint32);
begin
return To_Uint32 (VARINT_To_Uint64(V));
end VARINT_To_Uint32;

function VARINT_To_Int32 (V : in Byte_Vector) return Int32 is
function To_Int32 is new Ada.Unchecked_Conversion(Uint64, Int32);
begin
return To_Int32 (VARINT_To_Uint64(V));
end VARINT_To_Int32;

function VARINT_To_Int64 (V : in Byte_Vector) return Int64 is
function To_Int64 is new Ada.Unchecked_Conversion(Uint64, Int64);
begin
return To_Int64 (VARINT_To_Uint64(V));
end VARINT_To_Int64;

-- Different signed types, sint32 and sint64 vs int32 or int64, encode negative integers differently
-- The implementation of these may be wrong for now...
function VARINT_To_SInt32 (V : in Byte_Vector) return Int32 is
function To_Int32 is new Ada.Unchecked_Conversion(Uint64, Int32);
begin
return To_Int32 (VARINT_To_Uint64(V));
end VARINT_To_SInt32;

function VARINT_To_SInt64 (V : in Byte_Vector) return Int64 is
function To_Int64 is new Ada.Unchecked_Conversion(Uint64, Int64);
begin
return To_Int64 (VARINT_To_Uint64(V));
end VARINT_To_SInt64;
end Protobuf;
19 changes: 8 additions & 11 deletions eRINA_STM32F7/src/net/protobuf.ads
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,14 @@ package Protobuf is
-- Variable-width integers, or varints, are at the core of the wire format.
-- They allow encoding unsigned 64-bit ints using anywhere between 1 and 10 bytes
-- Where smaller values use fewer bytes
function To_VARINT (V : in Byte_Vector) return Uint64;
function VARINT_To_Uint64 (V : in Byte_Vector) return Uint64;
function VARINT_To_Uint32 (Input : Uint64) return Uint32;

-- MT: TODO: Implement these
function VARINT_To_Int32 (Input : Uint64) return Int32;
function VARINT_To_Int64 (Input : Uint64) return Int64;

-- function VARINT_To_Int32 (Input : Uint64) return Int32;
-- function VARINT_To_Int64 (Input : Uint64) return Int64;

-- Different signed types, sint32 and sint64 vs int32 or int64, encode negative integers differently
-- function VARINT_To_SInt32 (Input : Uint64) return Int32;
-- function VARINT_To_SInt64 (Input : Uint64) return Int64;

-- function VARINT_To_Uint32 (Input : Uint64) return Uint32;
-- function VARINT_To_Uint64 (Input : Uint64) return Uint64;
-- Different signed types, sint32 and sint64 vs int32 or int64, encode negative integers differently
-- The implementation of these may be wrong for now...
function VARINT_To_SInt32 (Input : Uint64) return Int32;
function VARINT_To_SInt64 (Input : Uint64) return Int64;
end Protobuf;

0 comments on commit 95a77c9

Please sign in to comment.