File : varints.adb


   1 package body VarInts is
   2    
   3    procedure VarInt_Write(Stream : not null access Root_Stream_Type'Class;
   4                           I : in VarInt) is
   5    begin
   6       case I is
   7          when 0 .. 16#FC# =>
   8             Unsigned_8'Write(Stream, Unsigned_8(I));
   9          when 16#FD# .. (2**16)-1 =>
  10             Unsigned_8'Write(Stream, 16#FD#);
  11             Unsigned_16'Write(Stream, Unsigned_16(I));
  12          when (2**16) .. (2**32)-1 =>
  13             Unsigned_8'Write(Stream, 16#FE#);
  14             Unsigned_32'Write(Stream, Unsigned_32(I));
  15          when (2**32) .. (2**64)-1 =>
  16             Unsigned_8'Write(Stream, 16#FF#);
  17             Unsigned_64'Write(Stream, Unsigned_64(I));
  18       end case;
  19    end VarInt_Write;
  20    
  21    
  22    procedure VarInt_Read(Stream : not null access Root_Stream_Type'Class;
  23                          I : out VarInt) is
  24       Mark : Unsigned_8;
  25    begin
  26       Unsigned_8'Read(Stream, Mark);
  27       case Mark is
  28          when 0 .. 16#FC# =>
  29             I := VarInt(Mark);
  30          when 16#FD# =>
  31             Unsigned_16'Read(Stream, Unsigned_16(I));
  32          when 16#FE# =>
  33             Unsigned_32'Read(Stream, Unsigned_32(I));
  34          when 16#FF# =>
  35             Unsigned_64'Read(Stream, Unsigned_64(I));
  36       end case;
  37    end VarInt_Read;
  38    
  39 end VarInts;