ClpBaseInputStream.pas 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. { *********************************************************************************** }
  2. { * CryptoLib Library * }
  3. { * Copyright (c) 2018 - 20XX Ugochukwu Mmaduekwe * }
  4. { * Github Repository <https://github.com/Xor-el> * }
  5. { * Distributed under the MIT software license, see the accompanying file LICENSE * }
  6. { * or visit http://www.opensource.org/licenses/mit-license.php. * }
  7. { * Acknowledgements: * }
  8. { * * }
  9. { * Thanks to Sphere 10 Software (http://www.sphere10.com/) for sponsoring * }
  10. { * development of this library * }
  11. { * ******************************************************************************* * }
  12. (* &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& *)
  13. unit ClpBaseInputStream;
  14. {$I ..\..\Include\CryptoLib.inc}
  15. interface
  16. uses
  17. Classes,
  18. ClpCryptoLibTypes;
  19. type
  20. TBaseInputStream = class abstract(TStream)
  21. {$IFDEF DELPHI}
  22. private
  23. function GetPosition: Int64; inline;
  24. procedure SetPosition(const Pos: Int64); inline;
  25. procedure SetSize64(const NewSize: Int64); inline;
  26. {$ENDIF DELPHI}
  27. protected
  28. {$IFDEF FPC}
  29. function GetPosition: Int64; override;
  30. procedure SetPosition(const Pos: Int64); override;
  31. procedure SetSize64(const NewSize: Int64); override;
  32. {$ENDIF FPC}
  33. function GetSize: Int64; override;
  34. procedure SetSize(NewSize: LongInt); overload; override;
  35. procedure SetSize(const NewSize: Int64); overload; override;
  36. function QueryInterface({$IFDEF FPC}constref {$ELSE}const
  37. {$ENDIF FPC} IID: TGUID; out Obj): HResult; {$IFDEF MSWINDOWS} stdcall
  38. {$ELSE} cdecl {$ENDIF MSWINDOWS};
  39. function _AddRef: Integer; {$IFDEF MSWINDOWS} stdcall {$ELSE} cdecl
  40. {$ENDIF MSWINDOWS};
  41. function _Release: Integer; {$IFDEF MSWINDOWS} stdcall {$ELSE} cdecl
  42. {$ENDIF MSWINDOWS};
  43. public
  44. function ReadByte: Int32; virtual;
  45. function Read(var Buffer; Count: LongInt): LongInt; overload; override;
  46. function Write(const Buffer; Count: LongInt): LongInt; overload; override;
  47. function Read(Buffer: TCryptoLibByteArray; Offset, Count: LongInt)
  48. : Int32; overload;
  49. {$IFDEF SUPPORT_TSTREAM_READ_BYTEARRAY_OVERLOAD} override {$ELSE} virtual
  50. {$ENDIF SUPPORT_TSTREAM_READ_BYTEARRAY_OVERLOAD};
  51. function Write(const Buffer: TCryptoLibByteArray; Offset, Count: LongInt)
  52. : Int32; overload;
  53. {$IFDEF SUPPORT_TSTREAM_WRITE_BYTEARRAY_OVERLOAD} override {$ELSE} virtual
  54. {$ENDIF SUPPORT_TSTREAM_WRITE_BYTEARRAY_OVERLOAD};
  55. function Seek(Offset: LongInt; Origin: Word): LongInt; overload; override;
  56. function Seek(const Offset: Int64; Origin: TSeekOrigin): Int64;
  57. overload; override;
  58. {$IFNDEF _FIXINSIGHT_}
  59. property Size: Int64 read GetSize write SetSize64;
  60. {$ENDIF}
  61. property Position: Int64 read GetPosition write SetPosition;
  62. end;
  63. implementation
  64. uses
  65. // included here to avoid circular dependency :)
  66. ClpStreamSorter;
  67. { TBaseInputStream }
  68. function TBaseInputStream.GetPosition: Int64;
  69. begin
  70. raise ENotSupportedCryptoLibException.Create('');
  71. end;
  72. function TBaseInputStream.GetSize: Int64;
  73. begin
  74. raise ENotSupportedCryptoLibException.Create('');
  75. end;
  76. function TBaseInputStream.QueryInterface({$IFDEF FPC}constref {$ELSE}const
  77. {$ENDIF FPC} IID: TGUID; out Obj): HResult;
  78. begin
  79. if GetInterface(IID, Obj) then
  80. result := 0
  81. else
  82. result := E_NOINTERFACE;
  83. end;
  84. {$IFNDEF _FIXINSIGHT_}
  85. function TBaseInputStream.Read(var Buffer; Count: LongInt): LongInt;
  86. begin
  87. raise ENotSupportedCryptoLibException.Create('');
  88. end;
  89. function TBaseInputStream.Write(const Buffer; Count: LongInt): LongInt;
  90. begin
  91. raise ENotSupportedCryptoLibException.Create('');
  92. end;
  93. {$ENDIF}
  94. function TBaseInputStream.ReadByte: Int32;
  95. var
  96. Buffer: TCryptoLibByteArray;
  97. begin
  98. System.SetLength(Buffer, 1);
  99. // if (Read(Buffer, 0, 1) = 0) then
  100. if (TStreamSorter.Read(Self, Buffer, 0, 1) = 0) then
  101. begin
  102. result := -1;
  103. end
  104. else
  105. begin
  106. result := Int32(Buffer[0]);
  107. end;
  108. end;
  109. function TBaseInputStream.Seek(Offset: LongInt; Origin: Word): LongInt;
  110. begin
  111. result := Seek(Int64(Offset), TSeekOrigin(Origin));
  112. end;
  113. {$IFNDEF _FIXINSIGHT_}
  114. function TBaseInputStream.Seek(const Offset: Int64; Origin: TSeekOrigin): Int64;
  115. begin
  116. raise ENotSupportedCryptoLibException.Create('');
  117. end;
  118. procedure TBaseInputStream.SetPosition(const Pos: Int64);
  119. begin
  120. raise ENotSupportedCryptoLibException.Create('');
  121. end;
  122. {$ENDIF}
  123. procedure TBaseInputStream.SetSize(const NewSize: Int64);
  124. begin
  125. SetSize(LongInt(NewSize));
  126. end;
  127. procedure TBaseInputStream.SetSize(NewSize: LongInt);
  128. begin
  129. raise ENotSupportedCryptoLibException.Create('');
  130. end;
  131. procedure TBaseInputStream.SetSize64(const NewSize: Int64);
  132. begin
  133. SetSize(NewSize);
  134. end;
  135. function TBaseInputStream.Read(Buffer: TCryptoLibByteArray;
  136. Offset, Count: LongInt): Int32;
  137. var
  138. &pos, endPoint, b: Int32;
  139. begin
  140. Pos := Offset;
  141. try
  142. endPoint := Offset + Count;
  143. while (Pos < endPoint) do
  144. begin
  145. b := ReadByte();
  146. if (b = -1) then
  147. begin
  148. break;
  149. end;
  150. Buffer[Pos] := Byte(b);
  151. System.Inc(Pos);
  152. end;
  153. except
  154. on e: EIOCryptoLibException do
  155. begin
  156. if (Pos = Offset) then
  157. raise;
  158. end;
  159. end;
  160. result := Pos - Offset;
  161. end;
  162. {$IFNDEF _FIXINSIGHT_}
  163. function TBaseInputStream.Write(const Buffer: TCryptoLibByteArray;
  164. Offset, Count: LongInt): Int32;
  165. begin
  166. raise ENotSupportedCryptoLibException.Create('');
  167. end;
  168. {$ENDIF}
  169. function TBaseInputStream._AddRef: Integer;
  170. begin
  171. result := -1;
  172. end;
  173. function TBaseInputStream._Release: Integer;
  174. begin
  175. result := -1;
  176. end;
  177. end.