mirror of
https://gitlab.com/freepascal.org/fpc/source.git
synced 2025-08-11 16:26:13 +02:00
[PATCH 006/188] update leb encoding utils
From d55f93d5b74595e35ed82bb3fe5048c39259db52 Mon Sep 17 00:00:00 2001 From: Dmitry Boyarintsev <skalogryz.lists@gmail.com> Date: Wed, 25 Sep 2019 16:35:43 -0400 git-svn-id: branches/wasm@46002 -
This commit is contained in:
parent
8f456bbeb5
commit
a9755e1c28
@ -8,14 +8,8 @@ uses
|
|||||||
function ReadU(src: TStream): UInt64;
|
function ReadU(src: TStream): UInt64;
|
||||||
function ReadS(src: TStream; bits: Integer): Int64;
|
function ReadS(src: TStream; bits: Integer): Int64;
|
||||||
|
|
||||||
procedure WriteU8 (src: TStream; vl: UInt8);
|
procedure WriteU(src: TStream; vl: UInt64; bits: integer; fixedSize: Boolean = false);
|
||||||
procedure WriteU16(src: TStream; vl: UInt16);
|
procedure WriteS(src: TStream; vl: Int64; bits: integer);
|
||||||
procedure WriteU32(src: TStream; vl: UInt32);
|
|
||||||
procedure WriteU64(src: TStream; vl: UInt64);
|
|
||||||
procedure WriteS8 (src: TStream; vl: Int8);
|
|
||||||
procedure WriteS16(src: TStream; vl: Int16);
|
|
||||||
procedure WriteS32(src: TStream; vl: Int32);
|
|
||||||
procedure WriteS64(src: TStream; vl: Int64);
|
|
||||||
|
|
||||||
implementation
|
implementation
|
||||||
|
|
||||||
@ -30,7 +24,9 @@ begin
|
|||||||
b := src.ReadByte;
|
b := src.ReadByte;
|
||||||
Result := Result or ((b and $7f) shl sh);
|
Result := Result or ((b and $7f) shl sh);
|
||||||
if (b and $80)>0 then inc(sh, 7)
|
if (b and $80)>0 then inc(sh, 7)
|
||||||
else break;
|
else begin
|
||||||
|
break;
|
||||||
|
end;
|
||||||
end;
|
end;
|
||||||
end;
|
end;
|
||||||
|
|
||||||
@ -39,53 +35,73 @@ var
|
|||||||
b : byte;
|
b : byte;
|
||||||
sh : Integer;
|
sh : Integer;
|
||||||
begin
|
begin
|
||||||
result := 0;
|
Result := 0;
|
||||||
sh := 0;
|
sh := 0;
|
||||||
repeat
|
repeat
|
||||||
b := src.ReadByte;
|
b := src.ReadByte;
|
||||||
result := Result or ((b and $77) shl sh);
|
Result := Result or ((b and $7F) shl sh);
|
||||||
inc(sh, 7);
|
inc(sh, 7);
|
||||||
until ((b and $80) = 0);
|
until ((b and $80) = 0);
|
||||||
|
|
||||||
// sign bit of byte is second high order bit (0x40)
|
// sign bit of byte is second high order bit (0x40)
|
||||||
if (sh < bits) and ((b and $40) > 0) then
|
if (sh < bits) and ((b and $40) > 0) then
|
||||||
// sign extend
|
// sign extend
|
||||||
result := result or ( (not 0) shl sh);
|
result := result or ( (not 0) shl sh);
|
||||||
end;
|
end;
|
||||||
|
|
||||||
procedure WriteU8(src: TStream; vl: UInt8);
|
procedure WriteU(src: TStream; vl: UInt64; bits: integer; fixedSize: Boolean = false);
|
||||||
|
var
|
||||||
|
b: byte;
|
||||||
begin
|
begin
|
||||||
|
if (bits < 0) then bits := sizeof(vl)*8;
|
||||||
|
|
||||||
|
repeat
|
||||||
|
b := (vl and $7f);
|
||||||
|
vl := vl shr 7;
|
||||||
|
|
||||||
|
if bits >0 then begin
|
||||||
|
dec(bits,7);
|
||||||
|
if bits<0 then bits := 0;
|
||||||
|
end;
|
||||||
|
|
||||||
|
if (vl <> 0) or (fixedSize and (bits > 0)) then
|
||||||
|
b := b or $80;
|
||||||
|
|
||||||
|
src.WriteByte(b);
|
||||||
|
until ((vl=0) and not fixedSize) or (bits = 0)
|
||||||
end;
|
end;
|
||||||
|
|
||||||
procedure WriteU16(src: TStream; vl: UInt16);
|
procedure WriteS(src: TStream; vl: Int64; bits: integer);
|
||||||
|
var
|
||||||
|
more : Boolean;
|
||||||
|
negative : Boolean;
|
||||||
|
b : byte;
|
||||||
begin
|
begin
|
||||||
|
negative := vl < 0;
|
||||||
|
|
||||||
end;
|
more := true;
|
||||||
|
negative := (vl < 0);
|
||||||
|
|
||||||
procedure WriteU32(src: TStream; vl: UInt32);
|
if (bits < 0) then bits := sizeof(vl);
|
||||||
begin
|
|
||||||
|
|
||||||
end;
|
while more do begin
|
||||||
|
b := (vl and $7f);
|
||||||
|
vl := vl shr 7;
|
||||||
|
|
||||||
procedure WriteU64(src: TStream; vl: UInt64);
|
{ the following is only necessary if the implementation of >>= uses a
|
||||||
begin
|
logical shift rather than an arithmetic shift for a signed left operand }
|
||||||
end;
|
if (negative) then
|
||||||
|
vl := vl or ((not 0) shl (bits - 7)); // sign extend
|
||||||
|
|
||||||
procedure WriteS8 (src: TStream; vl: Int8);
|
{ sign bit of byte is second high order bit (0x40) }
|
||||||
begin
|
if ((vl = 0) and (b and $40 = 0))
|
||||||
end;
|
or ((vl = -1) and (b and $40 <> 0))
|
||||||
|
then
|
||||||
procedure WriteS16(src: TStream; vl: Int16);
|
more := false
|
||||||
begin
|
else
|
||||||
end;
|
b := b or $80;
|
||||||
|
src.WriteByte(b);
|
||||||
procedure WriteS32(src: TStream; vl: Int32);
|
end;
|
||||||
begin
|
|
||||||
end;
|
|
||||||
|
|
||||||
procedure WriteS64(src: TStream; vl: Int64);
|
|
||||||
begin
|
|
||||||
end;
|
end;
|
||||||
|
|
||||||
end.
|
end.
|
||||||
|
Loading…
Reference in New Issue
Block a user