lebutils.pas 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. { This file is part of wasmbin - a collection of WebAssembly binary utils.
  2. Copyright (C) 2019, 2020 Dmitry Boyarintsev <[email protected]>
  3. Copyright (C) 2020 by the Free Pascal development team
  4. This source is free software; you can redistribute it and/or modify it under
  5. the terms of the GNU General Public License as published by the Free
  6. Software Foundation; either version 2 of the License, or (at your option)
  7. any later version.
  8. This code is distributed in the hope that it will be useful, but WITHOUT ANY
  9. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  10. FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
  11. details.
  12. A copy of the GNU General Public License is available on the World Wide Web
  13. at <http://www.gnu.org/copyleft/gpl.html>. You can also obtain it by writing
  14. to the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
  15. Boston, MA 02110-1335, USA.
  16. }
  17. unit lebutils;
  18. {$mode objfpc}{$H+}
  19. interface
  20. uses
  21. SysUtils, Classes;
  22. function ReadU(src: TStream): UInt64;
  23. function ReadS(src: TStream; bits: Integer): Int64;
  24. procedure WriteU(src: TStream; vl: UInt64; bits: integer; fixedSize: Boolean = false);
  25. procedure WriteS(src: TStream; vl: Int64; bits: integer);
  26. procedure WriteU64(src: TStream; vl: UInt64);
  27. procedure WriteU32(src: TStream; vl: UInt32);
  28. procedure WriteU16(src: TStream; vl: UInt16);
  29. procedure WriteU8(src: TStream; vl: UInt8);
  30. procedure WriteS64(src: TStream; vl: Int64);
  31. implementation
  32. function ReadU(src: TStream): UInt64;
  33. var
  34. b : byte;
  35. sh : integer;
  36. begin
  37. Result := 0;
  38. sh := 0;
  39. while true do begin
  40. b := src.ReadByte;
  41. Result := Result or ((b and $7f) shl sh);
  42. if (b and $80)>0 then inc(sh, 7)
  43. else begin
  44. break;
  45. end;
  46. end;
  47. end;
  48. function ReadS(src: TStream; bits: Integer): Int64;
  49. var
  50. b : byte;
  51. sh : Integer;
  52. begin
  53. Result := 0;
  54. sh := 0;
  55. repeat
  56. b := src.ReadByte;
  57. Result := Result or ((b and $7F) shl sh);
  58. inc(sh, 7);
  59. until ((b and $80) = 0);
  60. // sign bit of byte is second high order bit (0x40)
  61. if (sh < bits) and ((b and $40) > 0) then
  62. // sign extend
  63. result := result or ( (not 0) shl sh);
  64. end;
  65. procedure WriteU(src: TStream; vl: UInt64; bits: integer; fixedSize: Boolean = false);
  66. var
  67. b: byte;
  68. begin
  69. if (bits < 0) then bits := sizeof(vl)*8;
  70. repeat
  71. b := (vl and $7f);
  72. vl := vl shr 7;
  73. if bits >0 then begin
  74. dec(bits,7);
  75. if bits<0 then bits := 0;
  76. end;
  77. if (vl <> 0) or (fixedSize and (bits > 0)) then
  78. b := b or $80;
  79. src.WriteByte(b);
  80. until ((vl=0) and not fixedSize) or (bits = 0)
  81. end;
  82. procedure WriteS(src: TStream; vl: Int64; bits: integer);
  83. var
  84. more : Boolean;
  85. b : byte;
  86. begin
  87. more := true;
  88. if (bits < 0) then bits := sizeof(vl);
  89. while more do begin
  90. b := (vl and $7f);
  91. vl := SarInt64(vl, 7);
  92. { sign bit of byte is second high order bit (0x40) }
  93. if ((vl = 0) and (b and $40 = 0))
  94. or ((vl = -1) and (b and $40 <> 0))
  95. then
  96. more := false
  97. else
  98. b := b or $80;
  99. src.WriteByte(b);
  100. end;
  101. end;
  102. procedure WriteU32(src: TStream; vl: UInt32);
  103. begin
  104. WriteU(src, vl, sizeof(vl)*8);
  105. end;
  106. procedure WriteU64(src: TStream; vl: UInt64);
  107. begin
  108. WriteU(src, vl, sizeof(vl)*8);
  109. end;
  110. procedure WriteU16(src: TStream; vl: UInt16);
  111. begin
  112. WriteU(src, vl, sizeof(vl)*8);
  113. end;
  114. procedure WriteU8(src: TStream; vl: UInt8);
  115. begin
  116. WriteU(src, vl, sizeof(vl)*8);
  117. end;
  118. procedure WriteS64(src: TStream; vl: Int64);
  119. begin
  120. WriteS(src, vl, sizeof(vl)*8);
  121. end;
  122. end.