IdBuffer.pas 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045
  1. {
  2. $Project$
  3. $Workfile$
  4. $Revision$
  5. $DateUTC$
  6. $Id$
  7. This file is part of the Indy (Internet Direct) project, and is offered
  8. under the dual-licensing agreement described on the Indy website.
  9. (http://www.indyproject.org/)
  10. Copyright:
  11. (c) 1993-2005, Chad Z. Hower and the Indy Pit Crew. All rights reserved.
  12. }
  13. {
  14. $Log$
  15. }
  16. {
  17. Rev 1.47 1/24/2005 7:35:54 PM JPMugaas
  18. Fixed CopyTIdIPV6Address.
  19. Rev 1.46 1/17/2005 7:28:44 PM JPMugaas
  20. Added Index parameter to several functions so you can use TIdBuffer in a
  21. random access manner instead of in a sequential manner. This is good for
  22. some fixed-packet or data types.
  23. Added functions for reading and writing various types to TIdBuffer which use
  24. Byte Order functions. This should facilitate a lot of development as this
  25. gets used more.
  26. Rev 1.45 27.08.2004 21:58:18 Andreas Hausladen
  27. Speed optimization ("const" for string parameters)
  28. Rev 1.44 2004.07.03 19:41:34 czhower
  29. UTF8, SaveToStream
  30. Rev 1.43 6/11/2004 8:48:12 AM DSiders
  31. Added "Do not Localize" comments.
  32. Rev 1.42 6/9/04 7:46:26 PM RLebeau
  33. Updated ExtractToBytes() to allocate the output buffer only if the buffer
  34. length is smaller than the requested number of bytes.
  35. Rev 1.41 5/29/04 10:44:58 PM RLebeau
  36. Updated ExtractToBytes() to allocate the output buffer regardless of the
  37. AAppend parameter
  38. Added empty string return value to Extract() when AByteCount <= 0
  39. Rev 1.40 2004.05.20 11:39:06 AM czhower
  40. IdStreamVCL
  41. Rev 1.39 2004.05.10 1:19:18 PM czhower
  42. Removed unneeded code.
  43. Rev 1.38 5/3/2004 12:57:00 PM BGooijen
  44. Fixes for 0-based
  45. Rev 1.37 2004.05.03 11:15:42 AM czhower
  46. Changed Find to IndexOf and made 0 based to be consistent.
  47. Rev 1.36 2004.05.01 4:26:52 PM czhower
  48. Added PeekByte
  49. Rev 1.35 2004.04.16 11:30:26 PM czhower
  50. Size fix to IdBuffer, optimizations, and memory leaks
  51. Rev 1.34 2004.04.08 7:06:44 PM czhower
  52. Peek support.
  53. Rev 1.33 2004.04.08 3:56:24 PM czhower
  54. Fixed bug with Intercept byte count. Also removed Bytes from Buffer.
  55. Rev 1.32 2004.04.08 2:03:34 AM czhower
  56. Fixes to Bytes.
  57. Rev 1.31 2004.04.07 3:59:44 PM czhower
  58. Bug fix for WriteDirect.
  59. Rev 1.30 2004.04.07 3:46:30 PM czhower
  60. Compile fix.
  61. Rev 1.29 4/7/2004 1:02:14 PM BGooijen
  62. when extract* is called with -1 or no parameters all data it extracted
  63. Rev 1.28 2004.03.29 9:58:38 PM czhower
  64. Is now buffered. Now takes 2/3rds the time as before.
  65. Rev 1.27 23/03/2004 18:33:44 CCostelloe
  66. Bug fix: ReadLn returns a previously-read line if FBytes also accessed
  67. in-between (causes MoveHeadToStartIfNecessary to be called)
  68. Rev 1.26 18/03/2004 20:24:26 CCostelloe
  69. Speed improvement by adding FHeadIndex: 10 MB base64 decode reduced from 10
  70. hours to 62 seconds.
  71. Rev 1.25 2004.03.03 11:55:02 AM czhower
  72. IdStream change
  73. Rev 1.24 3/1/04 7:33:12 PM RLebeau
  74. Updated Remove() to call the OnBytesRemoved event handler.
  75. Rev 1.23 2004.02.03 4:17:14 PM czhower
  76. For unit name changes.
  77. Rev 1.22 1/11/2004 5:48:48 PM BGooijen
  78. Added AApend parameter to ExtractToBytes
  79. Rev 1.21 1/7/2004 8:36:32 PM BGooijen
  80. Arguments were in wrong order
  81. Rev 1.20 22/11/2003 10:35:04 PM GGrieve
  82. Reverse copy direction in TIdBuffer.ExtractToStream
  83. Rev 1.19 2003.10.24 10:44:54 AM czhower
  84. IdStream implementation, bug fixes.
  85. Rev 1.18 10/15/2003 1:03:40 PM DSiders
  86. Created resource strings for TIdBuffer.Find exceptions.
  87. Rev 1.17 2003.10.14 1:27:06 PM czhower
  88. Uupdates + Intercept support
  89. Rev 1.16 2003.10.11 5:47:00 PM czhower
  90. -VCL fixes for servers
  91. -Chain suport for servers (Super core)
  92. -Scheduler upgrades
  93. -Full yarn support
  94. Rev 1.15 10/5/2003 10:24:20 PM BGooijen
  95. Changed WriteBytes(var ...) to WriteBytes(const ...)
  96. Rev 1.14 10/3/2003 10:46:38 PM BGooijen
  97. Fixed Range Check Exception, and fixed ExtractToStream
  98. Rev 1.13 2003.10.02 8:29:12 PM czhower
  99. Changed names of byte conversion routines to be more readily understood and
  100. not to conflict with already in use ones.
  101. Rev 1.12 2003.10.02 12:44:58 PM czhower
  102. Comment added
  103. Rev 1.11 10/2/2003 5:23:14 PM GGrieve
  104. make Bytes a public property
  105. Rev 1.10 10/2/2003 5:00:38 PM GGrieve
  106. Fix bug in find - can't find last char
  107. Rev 1.9 2003.10.02 10:37:00 AM czhower
  108. Comments
  109. Rev 1.8 10/2/2003 3:54:06 PM GGrieve
  110. Finish cleaning up - no $IFDEFs but still optimal on both win32 and DontNet
  111. Rev 1.7 10/1/2003 10:58:38 PM BGooijen
  112. Removed unused var
  113. Rev 1.6 10/1/2003 8:15:58 PM BGooijen
  114. Fixed Range Check Error on D7
  115. Rev 1.5 10/1/2003 8:02:22 PM BGooijen
  116. Removed some ifdefs and improved code
  117. Rev 1.4 10/1/2003 10:49:02 PM GGrieve
  118. Rework buffer for Octane Compability
  119. Rev 1.3 2003.10.01 2:30:44 PM czhower
  120. .Net
  121. Rev 1.2 2003.10.01 1:37:32 AM czhower
  122. .Net
  123. Rev 1.1 2003.10.01 1:12:32 AM czhower
  124. .Net
  125. Rev 1.0 2003.09.30 10:33:56 PM czhower
  126. Readd after accidental delete.
  127. Rev 1.14 2003.09.30 10:33:16 PM czhower
  128. Updates
  129. Rev 1.13 2003.07.16 5:05:06 PM czhower
  130. Phase 1 of IdBuffer changes for compat.
  131. Rev 1.12 6/29/2003 10:56:22 PM BGooijen
  132. Removed .Memory from the buffer, and added some extra methods
  133. Rev 1.11 2003.06.25 4:29:06 PM czhower
  134. Free --> FreeAndNil
  135. Rev 1.10 2003.01.17 2:18:36 PM czhower
  136. Rev 1.9 12-14-2002 22:08:24 BGooijen
  137. Changed FMemory to FMemory.Memory in some places
  138. Rev 1.8 12-14-2002 22:02:34 BGooijen
  139. changed Memory to FMemory in some places, to remove some issues
  140. Rev 1.7 12/11/2002 04:27:02 AM JPMugaas
  141. Fixed compiler warning.
  142. Rev 1.6 12/11/2002 03:53:44 AM JPMugaas
  143. Merged the buffer classes.
  144. Rev 1.5 2002.12.07 12:26:18 AM czhower
  145. Rev 1.4 12-6-2002 20:34:06 BGooijen
  146. Now compiles on Delphi 5
  147. Rev 1.3 6/12/2002 11:00:14 AM SGrobety
  148. Rev 1.2 12/5/2002 02:55:44 PM JPMugaas
  149. Added AddStream method for reading a stream into the buffer class.
  150. Rev 1.1 23.11.2002 12:59:48 JBerg
  151. fixed packbuffer
  152. Rev 1.0 11/13/2002 08:38:32 AM JPMugaas
  153. }
  154. unit IdBuffer;
  155. {$I IdCompilerDefines.inc}
  156. {
  157. .Net forces us to perform copies from strings to Bytes so that it can do the
  158. proper unicode and other conversions.
  159. IdBuffer is for storing data we cannot deal with right now and we do not know
  160. the size. It must be optimized for adding to the end, and extracting from the
  161. beginning. First pass we are just making it work, later using bubbling we will
  162. optimize it for such tasks.
  163. The copy is a separate issue and we considered several options. For .net we will
  164. always have to copy data to send or to receive to translate it to binary. For
  165. example if we have a string it must be converted to bytes. This conversion
  166. requires a copy. All strings are Unicode and must be converted to single
  167. bytes by a convertor. This is not limited to strings.
  168. In VCL previously all strings were AnsiString so we used a pointer and just
  169. accessed the memory directly from the string. This avoided the overhead of a
  170. copy.
  171. We have come up with several ideas on how to allow the copy on .net, while
  172. avoiding the copy on VCL to keep the performance benefit. However we must do
  173. it in a single source manner and in a manner that does not impact the code
  174. negatively.
  175. For now for VCL we also do a copy. This has the advantage that Byte arrays are
  176. reference counted and automaticaly handled by Delphi. For example:
  177. WriteBytes(StringToBytes(s));
  178. The array returned by this function will automatically be freed by Delphi.
  179. There are other options that are nearly as transparent but have the additional
  180. overhead of requiring class creation. These classes can be used to copy for .net
  181. and proxy on VCL. It all works very nice and has low memory overhead. The
  182. objects can then be freed by default in methods that accept them.
  183. However after analysis, copy on VCL may not be that bad after all. The copy
  184. only really impacts strings. The overhead to copy strings is minimal and only
  185. used in commands etc. The big transfers come from files, streams, or other.
  186. Such transfers have to be mapped into memory in VCL anyways, and if we map
  187. directly into the byte array instead of the previous classes peformance should
  188. be fine.
  189. In short - copy under VCL should be acceptable if we watch for bottlenecks and
  190. fix them appropriately without having to creat proxy classes. The only problem
  191. remains for transmitting large memory blocks. But if this is done against a
  192. fixed copy buffer the performance hit will be neglible and it is not a common
  193. task to transmit large memory blocks.
  194. For such transfers from streams, etc the user can declare a persistent array
  195. of bytes that is not freed between each call to WriteBytes.
  196. -Kudzu
  197. }
  198. interface
  199. uses
  200. Classes,
  201. IdException,
  202. IdGlobal,
  203. SysUtils;
  204. type
  205. EIdNotEnoughDataInBuffer = class(EIdException);
  206. EIdTooMuchDataInBuffer = class(EIdException); // only 2GB is allowed -
  207. TIdBufferBytesRemoved = procedure(ASender: TObject; ABytes: Integer) of object;
  208. // TIdBuffer is used as an internal buffer to isolate Indy from pointers and
  209. // memory allocations. It also allows optimizations to be kept in a single place.
  210. //
  211. // TIdBuffer is primarily used as a read/write buffer for the communication layer.
  212. TIdBuffer = class(TObject)
  213. private
  214. function GetAsString: string;
  215. protected
  216. FBytes: TIdBytes;
  217. FByteEncoding: IIdTextEncoding;
  218. {$IFDEF STRING_IS_ANSI}
  219. FAnsiEncoding: IIdTextEncoding;
  220. {$ENDIF}
  221. FGrowthFactor: Integer;
  222. FHeadIndex: Integer;
  223. FOnBytesRemoved: TIdBufferBytesRemoved;
  224. FSize: Integer;
  225. //
  226. procedure CheckAdd(AByteCount : Integer; const AIndex : Integer);
  227. procedure CheckByteCount(var VByteCount : Integer; const AIndex : Integer);
  228. function GetCapacity: Integer;
  229. procedure SetCapacity(AValue: Integer);
  230. public
  231. procedure Clear;
  232. constructor Create; overload;
  233. constructor Create(AOnBytesRemoved: TIdBufferBytesRemoved); overload;
  234. constructor Create(AGrowthFactor: Integer); overload;
  235. constructor Create(const ABytes : TIdBytes; const ALength : Integer = -1); overload;
  236. procedure CompactHead(ACanShrink: Boolean = True);
  237. destructor Destroy; override;
  238. {
  239. Most of these now have an AIndex parameter. If that is less than 0,
  240. we are accessing data sequentially. That means, read the data from the HeadIndex
  241. and "remove" the data you read.
  242. If AIndex is 0 or greater, the HeadIndex is disregarded and no deletion is done.
  243. You are just reading from a particular location in a random access manner.
  244. }
  245. // will extract number of bytes and decode as specified
  246. function Extract(AByteCount: Integer = -1; AByteEncoding: IIdTextEncoding = nil
  247. {$IFDEF STRING_IS_ANSI}; ADestEncoding: IIdTextEncoding = nil{$ENDIF}
  248. ): string; {$IFDEF HAS_DEPRECATED}deprecated{$IFDEF HAS_DEPRECATED_MSG} 'Use ExtractToString()'{$ENDIF};{$ENDIF}
  249. function ExtractToString(AByteCount: Integer = -1; AByteEncoding: IIdTextEncoding = nil
  250. {$IFDEF STRING_IS_ANSI}; ADestEncoding: IIdTextEncoding = nil{$ENDIF}
  251. ): string;
  252. // all 3 extract routines append to existing data, if any
  253. procedure ExtractToStream(const AStream: TStream; AByteCount: Integer = -1; const AIndex: Integer = -1);
  254. procedure ExtractToIdBuffer(ABuffer: TIdBuffer; AByteCount: Integer = -1; const AIndex : Integer = -1);
  255. procedure ExtractToBytes(var VBytes: TIdBytes; AByteCount: Integer = -1;
  256. AAppend: Boolean = True; AIndex : Integer = -1);
  257. function ExtractToUInt8(const AIndex : Integer): UInt8;
  258. function ExtractToByte(const AIndex : Integer): UInt8; {$IFDEF HAS_DEPRECATED}deprecated{$IFDEF HAS_DEPRECATED_MSG} 'Use ExtractToUInt8()'{$ENDIF};{$ENDIF}
  259. function ExtractToUInt16(const AIndex : Integer; AConvert: Boolean = True): UInt16;
  260. function ExtractToWord(const AIndex : Integer): UInt16; {$IFDEF HAS_DEPRECATED}deprecated{$IFDEF HAS_DEPRECATED_MSG} 'Use ExtractToUInt16()'{$ENDIF};{$ENDIF}
  261. function ExtractToUInt32(const AIndex : Integer; AConvert: Boolean = True): UInt32;
  262. function ExtractToLongWord(const AIndex : Integer): UInt32; {$IFDEF HAS_DEPRECATED}deprecated{$IFDEF HAS_DEPRECATED_MSG} 'Use ExtractToUInt32()'{$ENDIF};{$ENDIF}
  263. function ExtractToUInt64(const AIndex : Integer; AConvert: Boolean = True): TIdUInt64;
  264. procedure ExtractToIPv6(const AIndex : Integer; var VAddress: TIdIPv6Address);
  265. function IndexOf(const AByte: Byte; AStartPos: Integer = 0): Integer; overload;
  266. function IndexOf(const ABytes: TIdBytes; AStartPos: Integer = 0): Integer; overload;
  267. function IndexOf(const AString: string; AStartPos: Integer = 0;
  268. AByteEncoding: IIdTextEncoding = nil
  269. {$IFDEF STRING_IS_ANSI}; ASrcEncoding: IIdTextEncoding = nil{$ENDIF}
  270. ): Integer; overload;
  271. function PeekByte(AIndex: Integer): Byte;
  272. procedure Remove(AByteCount: Integer);
  273. procedure SaveToStream(const AStream: TStream);
  274. { Most of these now have an ADestIndex parameter. If that is less than 0,
  275. we are writing data sequentially.
  276. If ADestIndex is 0 or greater, you are setting bytes in a particular
  277. location in a random access manner.
  278. }
  279. // Write
  280. procedure Write(const AString: string; AByteEncoding: IIdTextEncoding = nil;
  281. const ADestIndex: Integer = -1
  282. {$IFDEF STRING_IS_ANSI}; ASrcEncoding: IIdTextEncoding = nil{$ENDIF}
  283. ); overload;
  284. procedure Write(const ABytes: TIdBytes; const ADestIndex: Integer = -1); overload;
  285. procedure Write(const ABytes: TIdBytes; const ALength, AOffset : Integer; const ADestIndex: Integer = -1); overload;
  286. procedure Write(AStream: TStream; AByteCount: Integer = 0); overload;
  287. procedure Write(const AValue: TIdUInt64; const ADestIndex: Integer = -1); overload;
  288. procedure Write(const AValue: UInt32; const ADestIndex: Integer = -1); overload;
  289. procedure Write(const AValue: UInt16; const ADestIndex: Integer = -1); overload;
  290. procedure Write(const AValue: UInt8; const ADestIndex: Integer = -1); overload;
  291. procedure Write(const AValue: TIdIPv6Address; const ADestIndex: Integer = -1); overload;
  292. //
  293. //Kudzu: I have removed the Bytes property. Do not add it back - it allowed "internal" access
  294. // which caused compacting or internal knowledge. Access via Extract or other such methods
  295. // instead. Bytes could also be easily confused with FBytes internally and cause issues.
  296. //
  297. // Bytes also allowed direct acces without removing which could cause concurrency issues if
  298. // the reference was kept.
  299. //
  300. property Capacity: Integer read GetCapacity write SetCapacity;
  301. property Encoding: IIdTextEncoding read FByteEncoding write FByteEncoding;
  302. {$IFDEF STRING_IS_ANSI}
  303. property AnsiEncoding: IIdTextEncoding read FAnsiEncoding write FAnsiEncoding;
  304. {$ENDIF}
  305. property GrowthFactor: Integer read FGrowthFactor write FGrowthFactor;
  306. property Size: Integer read FSize;
  307. //useful for testing. returns buffer as string without extraction.
  308. property AsString: string read GetAsString;
  309. end;
  310. implementation
  311. uses
  312. IdResourceStringsCore,
  313. IdStream,
  314. IdStack; //needed for byte order functions
  315. procedure TIdBuffer.CheckAdd(AByteCount : Integer; const AIndex : Integer);
  316. begin
  317. if (MaxInt - AByteCount) < (Size + AIndex) then begin
  318. raise EIdTooMuchDataInBuffer.Create(RSTooMuchDataInBuffer);
  319. end;
  320. end;
  321. procedure TIdBuffer.CheckByteCount(var VByteCount : Integer; const AIndex : Integer);
  322. begin
  323. if VByteCount = -1 then begin
  324. VByteCount := Size+AIndex;
  325. end
  326. else if VByteCount > (Size+AIndex) then begin
  327. raise EIdNotEnoughDataInBuffer.CreateFmt('%s (%d/%d)', [RSNotEnoughDataInBuffer, VByteCount, Size]); {do not localize}
  328. end;
  329. end;
  330. procedure TIdBuffer.Clear;
  331. begin
  332. SetLength(FBytes, 0);
  333. FHeadIndex := 0;
  334. FSize := Length(FBytes);
  335. end;
  336. constructor TIdBuffer.Create(AGrowthFactor: Integer);
  337. begin
  338. Create;
  339. FGrowthFactor := AGrowthFactor;
  340. end;
  341. constructor TIdBuffer.Create(AOnBytesRemoved: TIdBufferBytesRemoved);
  342. begin
  343. Create;
  344. FOnBytesRemoved := AOnBytesRemoved;
  345. end;
  346. constructor TIdBuffer.Create(const ABytes: TIdBytes; const ALength: Integer);
  347. begin
  348. Create;
  349. if ALength < 0 then
  350. begin
  351. FBytes := ABytes;
  352. FSize := Length(ABytes);
  353. end else
  354. begin
  355. SetLength(FBytes, ALength);
  356. if ALength > 0 then
  357. begin
  358. CopyTIdBytes(ABytes, 0, FBytes, 0, ALength);
  359. FSize := ALength;
  360. end;
  361. end;
  362. end;
  363. destructor TIdBuffer.Destroy;
  364. begin
  365. Clear;
  366. inherited Destroy;
  367. //do only at the last moment
  368. TIdStack.DecUsage;
  369. end;
  370. {$I IdDeprecatedImplBugOff.inc}
  371. function TIdBuffer.Extract(AByteCount: Integer = -1; AByteEncoding: IIdTextEncoding = nil
  372. {$IFDEF STRING_IS_ANSI}; ADestEncoding: IIdTextEncoding = nil{$ENDIF}
  373. ): string;
  374. {$I IdDeprecatedImplBugOn.inc}
  375. {$IFDEF USE_CLASSINLINE}inline;{$ENDIF}
  376. begin
  377. Result := ExtractToString(AByteCount, AByteEncoding{$IFDEF STRING_IS_ANSI}, ADestEncoding{$ENDIF});
  378. end;
  379. function TIdBuffer.ExtractToString(AByteCount: Integer = -1; AByteEncoding: IIdTextEncoding = nil
  380. {$IFDEF STRING_IS_ANSI}; ADestEncoding: IIdTextEncoding = nil{$ENDIF}
  381. ): string;
  382. var
  383. LBytes: TIdBytes;
  384. begin
  385. if AByteCount < 0 then begin
  386. AByteCount := Size;
  387. end;
  388. if AByteCount > 0 then
  389. begin
  390. if AByteEncoding = nil then begin
  391. AByteEncoding := FByteEncoding;
  392. EnsureEncoding(AByteEncoding);
  393. end;
  394. {$IFDEF STRING_IS_ANSI}
  395. if ADestEncoding = nil then begin
  396. ADestEncoding := FAnsiEncoding;
  397. EnsureEncoding(ADestEncoding, encOSDefault);
  398. end;
  399. {$ENDIF}
  400. // TODO: convert directly from FBytes without allocating a local TIdBytes anymore...
  401. {
  402. CheckByteCount(AByteCount, 0);
  403. try
  404. Result := BytesToString(FBytes, FHeadIndex, AByteCount, AByteEncoding($IFDEF STRING_IS_ANSI), ADestEncoding($ENDIF));
  405. finally
  406. Remove(AByteCount);
  407. end;
  408. }
  409. ExtractToBytes(LBytes, AByteCount);
  410. Result := BytesToString(LBytes, AByteEncoding
  411. {$IFDEF STRING_IS_ANSI}, ADestEncoding{$ENDIF}
  412. );
  413. end else begin
  414. Result := '';
  415. end;
  416. end;
  417. procedure TIdBuffer.ExtractToBytes(var VBytes: TIdBytes; AByteCount: Integer = -1;
  418. AAppend: Boolean = True; AIndex : Integer = -1);
  419. var
  420. LOldSize: Integer;
  421. LIndex : Integer;
  422. begin
  423. if AByteCount < 0 then begin
  424. AByteCount := Size;
  425. end;
  426. LIndex := IndyMax(AIndex, 0);
  427. if AByteCount > 0 then begin
  428. CheckByteCount(AByteCount, LIndex);
  429. if AAppend then begin
  430. LOldSize := Length(VBytes);
  431. SetLength(VBytes, LOldSize + AByteCount);
  432. end else begin
  433. LOldSize := 0;
  434. if Length(VBytes) < AByteCount then begin
  435. SetLength(VBytes, AByteCount);
  436. end;
  437. end;
  438. if AIndex < 0 then
  439. begin
  440. CopyTIdBytes(FBytes, FHeadIndex, VBytes, LOldSize, AByteCount);
  441. Remove(AByteCount);
  442. end else
  443. begin
  444. CopyTIdBytes(FBytes, AIndex, VBytes, LOldSize, AByteCount);
  445. end;
  446. end;
  447. end;
  448. procedure TIdBuffer.ExtractToIdBuffer(ABuffer: TIdBuffer; AByteCount: Integer = -1;
  449. const AIndex: Integer = -1);
  450. var
  451. LBytes: TIdBytes;
  452. begin
  453. if AByteCount < 0 then begin
  454. AByteCount := Size;
  455. end;
  456. //TODO: Optimize this routine to directly copy from one to the other
  457. ExtractToBytes(LBytes, AByteCount, True, AIndex);
  458. ABuffer.Write(LBytes);
  459. end;
  460. procedure TIdBuffer.ExtractToStream(const AStream: TStream; AByteCount: Integer = -1;
  461. const AIndex: Integer = -1);
  462. var
  463. LIndex : Integer;
  464. LBytes : TIdBytes;
  465. begin
  466. if AByteCount < 0 then begin
  467. AByteCount := Size;
  468. end;
  469. LIndex := IndyMax(AIndex, 0);
  470. if AIndex < 0 then
  471. begin
  472. // TODO: remove CompactHead() here and pass FHeadIndex to TIdStreamHelper.Write():
  473. {
  474. CheckByteCount(AByteCount, FHeadIndex);
  475. TIdStreamHelper.Write(AStream, FBytes, AByteCount, FHeadIndex);
  476. Remove(AByteCount);
  477. }
  478. CompactHead;
  479. CheckByteCount(AByteCount, LIndex);
  480. TIdStreamHelper.Write(AStream, FBytes, AByteCount);
  481. Remove(AByteCount);
  482. end else
  483. begin
  484. // TODO: remove CopyTIdBytes() here and pass FBytes and AIndex to TIdStreamHelper.Write():
  485. {
  486. CheckByteCount(AByteCount, LIndex);
  487. TIdStreamHelper.Write(AStream, FBytes, AByteCount, AIndex);
  488. }
  489. CheckByteCount(AByteCount, LIndex);
  490. SetLength(LBytes, AByteCount);
  491. CopyTIdBytes(FBytes, AIndex, LBytes, 0, AByteCount);
  492. TIdStreamHelper.Write(AStream, LBytes, AByteCount);
  493. end;
  494. end;
  495. procedure TIdBuffer.Remove(AByteCount: Integer);
  496. begin
  497. if AByteCount >= Size then begin
  498. Clear;
  499. end else begin
  500. Inc(FHeadIndex, AByteCount);
  501. Dec(FSize, AByteCount);
  502. if FHeadIndex > GrowthFactor then begin
  503. CompactHead;
  504. end;
  505. end;
  506. if Assigned(FOnBytesRemoved) then begin
  507. FOnBytesRemoved(Self, AByteCount);
  508. end;
  509. end;
  510. procedure TIdBuffer.CompactHead(ACanShrink: Boolean = True);
  511. begin
  512. // Only try to compact if needed.
  513. if FHeadIndex > 0 then begin
  514. CopyTIdBytes(FBytes, FHeadIndex, FBytes, 0, Size);
  515. FHeadIndex := 0;
  516. if ACanShrink and ((Capacity - Size - FHeadIndex) > GrowthFactor) then begin
  517. SetLength(FBytes, FHeadIndex + Size + GrowthFactor);
  518. end;
  519. end;
  520. end;
  521. procedure TIdBuffer.Write(const ABytes: TIdBytes; const ADestIndex: Integer = -1);
  522. {$IFDEF USE_CLASSINLINE}inline;{$ENDIF}
  523. begin
  524. Write(ABytes, Length(ABytes), 0, ADestIndex);
  525. end;
  526. procedure TIdBuffer.Write(AStream: TStream; AByteCount: Integer);
  527. var
  528. LAdded: Integer;
  529. LLength: Integer;
  530. begin
  531. if AByteCount < 0 then begin
  532. // Copy remaining
  533. LAdded := AStream.Size - AStream.Position;
  534. end else if AByteCount = 0 then begin
  535. // Copy all
  536. AStream.Position := 0;
  537. LAdded := AStream.Size;
  538. end else begin
  539. LAdded := IndyMin(AByteCount, AStream.Size - AStream.Position);
  540. end;
  541. if LAdded > 0 then begin
  542. LLength := Size;
  543. CheckAdd(LAdded, 0);
  544. CompactHead;
  545. SetLength(FBytes, LLength + LAdded);
  546. TIdStreamHelper.ReadBytes(AStream, FBytes, LAdded, LLength);
  547. Inc(FSize, LAdded);
  548. end;
  549. end;
  550. function TIdBuffer.IndexOf(const AString: string; AStartPos: Integer = 0;
  551. AByteEncoding: IIdTextEncoding = nil
  552. {$IFDEF STRING_IS_ANSI}; ASrcEncoding: IIdTextEncoding = nil{$ENDIF}
  553. ): Integer;
  554. begin
  555. if AByteEncoding = nil then begin
  556. AByteEncoding := FByteEncoding;
  557. end;
  558. {$IFDEF STRING_IS_ANSI}
  559. if ASrcEncoding = nil then begin
  560. ASrcEncoding := FAnsiEncoding;
  561. end;
  562. {$ENDIF}
  563. Result := IndexOf(
  564. ToBytes(AString, AByteEncoding{$IFDEF STRING_IS_ANSI}, ASrcEncoding{$ENDIF}),
  565. AStartPos);
  566. end;
  567. function TIdBuffer.IndexOf(const ABytes: TIdBytes; AStartPos: Integer = 0): Integer;
  568. var
  569. i, j, LEnd, BytesLen: Integer;
  570. LFound: Boolean;
  571. begin
  572. Result := -1;
  573. // Dont search if it empty
  574. if Size > 0 then begin
  575. if Length(ABytes) = 0 then begin
  576. raise EIdException.Create(RSBufferMissingTerminator); // TODO: create a new Exception class for this
  577. end;
  578. if (AStartPos < 0) or (AStartPos >= Size) then begin
  579. raise EIdException.Create(RSBufferInvalidStartPos); // TODO: create a new Exception class for this
  580. end;
  581. BytesLen := Length(ABytes);
  582. LEnd := FHeadIndex + Size;
  583. for i := FHeadIndex + AStartPos to LEnd - BytesLen do begin
  584. LFound := True;
  585. for j := 0 to BytesLen - 1 do begin
  586. if (i + j) >= LEnd then begin
  587. Break;
  588. end;
  589. if FBytes[i + j] <> ABytes[j] then begin
  590. LFound := False;
  591. Break;
  592. end;
  593. end;
  594. if LFound then begin
  595. Result := i - FHeadIndex;
  596. Break;
  597. end;
  598. end;
  599. end;
  600. end;
  601. function TIdBuffer.IndexOf(const AByte: Byte; AStartPos: Integer = 0): Integer;
  602. var
  603. i: Integer;
  604. begin
  605. Result := -1;
  606. // Dont search if it empty
  607. if Size > 0 then begin
  608. if (AStartPos < 0) or (AStartPos >= Size) then begin
  609. raise EIdException.Create(RSBufferInvalidStartPos); // TODO: create a new Exception class for this
  610. end;
  611. for i := (FHeadIndex + AStartPos) to (FHeadIndex + Size - 1) do begin
  612. if FBytes[i] = AByte then begin
  613. Result := i - FHeadIndex;
  614. Break;
  615. end;
  616. end;
  617. end;
  618. end;
  619. procedure TIdBuffer.Write(const AString: string; AByteEncoding: IIdTextEncoding = nil;
  620. const ADestIndex : Integer = -1
  621. {$IFDEF STRING_IS_ANSI}; ASrcEncoding: IIdTextEncoding = nil{$ENDIF}
  622. );
  623. begin
  624. if AByteEncoding = nil then begin
  625. AByteEncoding := FByteEncoding;
  626. end;
  627. {$IFDEF STRING_IS_ANSI}
  628. if ASrcEncoding = nil then begin
  629. ASrcEncoding := FAnsiEncoding;
  630. end;
  631. {$ENDIF}
  632. Write(
  633. ToBytes(AString, AByteEncoding{$IFDEF STRING_IS_ANSI}, ASrcEncoding{$ENDIF}),
  634. ADestIndex);
  635. end;
  636. function TIdBuffer.GetCapacity: Integer;
  637. begin
  638. Result := Length(FBytes);
  639. end;
  640. procedure TIdBuffer.SetCapacity(AValue: Integer);
  641. begin
  642. if AValue < Size then begin
  643. raise EIdException.Create('Capacity cannot be smaller than Size'); {do not localize} // TODO: add a resource string, and create a new Exception class for this
  644. end;
  645. CompactHead;
  646. SetLength(FBytes, AValue);
  647. end;
  648. constructor TIdBuffer.Create;
  649. begin
  650. inherited Create;
  651. FGrowthFactor := 2048;
  652. Clear;
  653. TIdStack.IncUsage;
  654. end;
  655. function TIdBuffer.PeekByte(AIndex: Integer): Byte;
  656. begin
  657. if Size = 0 then begin
  658. raise EIdException.Create('No bytes in buffer.'); {do not localize} // TODO: add a resource string, and create a new Exception class for this
  659. end;
  660. if (AIndex < 0) or (AIndex >= Size) then begin
  661. raise EIdException.Create('Index out of bounds.'); {do not localize} // TODO: add a resource string, and create a new Exception class for this
  662. end;
  663. Result := FBytes[FHeadIndex + AIndex];
  664. end;
  665. procedure TIdBuffer.SaveToStream(const AStream: TStream);
  666. begin
  667. CompactHead(False);
  668. TIdStreamHelper.Write(AStream, FBytes, Size);
  669. end;
  670. procedure TIdBuffer.ExtractToIPv6(const AIndex: Integer; var VAddress: TIdIPv6Address);
  671. var
  672. LIndex : Integer;
  673. begin
  674. if AIndex < 0 then begin
  675. LIndex := FHeadIndex;
  676. end else begin
  677. LIndex := AIndex;
  678. end;
  679. BytesToIPv6(FBytes, VAddress, LIndex);
  680. VAddress := GStack.NetworkToHost(VAddress);
  681. if AIndex < 0 then begin
  682. Remove(16);
  683. end;
  684. end;
  685. function TIdBuffer.ExtractToUInt64(const AIndex: Integer; AConvert: Boolean = True): TIdUInt64;
  686. var
  687. LIndex : Integer;
  688. begin
  689. if AIndex < 0 then begin
  690. LIndex := FHeadIndex;
  691. end else begin
  692. LIndex := AIndex;
  693. end;
  694. Result := BytesToUInt64(FBytes, LIndex);
  695. if AIndex < 0 then begin
  696. Remove(8);
  697. end;
  698. if AConvert then begin
  699. Result := GStack.NetworkToHost(Result);
  700. end;
  701. end;
  702. function TIdBuffer.ExtractToUInt32(const AIndex: Integer; AConvert: Boolean = True): UInt32;
  703. var
  704. LIndex : Integer;
  705. begin
  706. if AIndex < 0 then begin
  707. LIndex := FHeadIndex;
  708. end else begin
  709. LIndex := AIndex;
  710. end;
  711. Result := BytesToUInt32(FBytes, LIndex);
  712. if AIndex < 0 then begin
  713. Remove(4);
  714. end;
  715. if AConvert then begin
  716. Result := GStack.NetworkToHost(Result);
  717. end;
  718. end;
  719. {$I IdDeprecatedImplBugOff.inc}
  720. function TIdBuffer.ExtractToLongWord(const AIndex: Integer): UInt32;
  721. {$I IdDeprecatedImplBugOn.inc}
  722. {$IFDEF USE_CLASSINLINE}inline;{$ENDIF}
  723. begin
  724. Result := ExtractToUInt32(AIndex);
  725. end;
  726. function TIdBuffer.ExtractToUInt16(const AIndex: Integer; AConvert: Boolean = True): UInt16;
  727. var
  728. LIndex : Integer;
  729. begin
  730. if AIndex < 0 then begin
  731. LIndex := FHeadIndex;
  732. end else begin
  733. LIndex := AIndex;
  734. end;
  735. Result := BytesToUInt16(FBytes, LIndex);
  736. if AIndex < 0 then begin
  737. Remove(2);
  738. end;
  739. if AConvert then begin
  740. Result := GStack.NetworkToHost(Result);
  741. end;
  742. end;
  743. {$I IdDeprecatedImplBugOff.inc}
  744. function TIdBuffer.ExtractToWord(const AIndex: Integer): UInt16;
  745. {$I IdDeprecatedImplBugOn.inc}
  746. {$IFDEF USE_CLASSINLINE}inline;{$ENDIF}
  747. begin
  748. Result := ExtractToUInt16(AIndex);
  749. end;
  750. function TIdBuffer.ExtractToUInt8(const AIndex: Integer): UInt8;
  751. var
  752. LIndex : Integer;
  753. begin
  754. if AIndex < 0 then begin
  755. LIndex := FHeadIndex;
  756. end else begin
  757. LIndex := AIndex;
  758. end;
  759. Result := FBytes[LIndex];
  760. if AIndex < 0 then begin
  761. Remove(1);
  762. end;
  763. end;
  764. {$I IdDeprecatedImplBugOff.inc}
  765. function TIdBuffer.ExtractToByte(const AIndex: Integer): UInt8;
  766. {$I IdDeprecatedImplBugOn.inc}
  767. {$IFDEF USE_CLASSINLINE}inline;{$ENDIF}
  768. begin
  769. Result := ExtractToUInt8(AIndex);
  770. end;
  771. procedure TIdBuffer.Write(const AValue: UInt16; const ADestIndex: Integer);
  772. var
  773. LVal : UInt16;
  774. LIndex : Integer;
  775. begin
  776. if ADestIndex < 0 then
  777. begin
  778. LIndex := FHeadIndex + Size;
  779. SetLength(FBytes, LIndex+2);
  780. end else
  781. begin
  782. LIndex := ADestIndex;
  783. end;
  784. LVal := GStack.HostToNetwork(AValue);
  785. CopyTIdUInt16(LVal, FBytes, LIndex);
  786. if LIndex >= FSize then begin
  787. FSize := LIndex+2;
  788. end;
  789. end;
  790. procedure TIdBuffer.Write(const AValue: UInt8; const ADestIndex: Integer);
  791. var
  792. LIndex : Integer;
  793. begin
  794. if ADestIndex < 0 then
  795. begin
  796. LIndex := FHeadIndex + Size;
  797. SetLength(FBytes, LIndex+1);
  798. end else
  799. begin
  800. LIndex := ADestIndex;
  801. end;
  802. FBytes[LIndex] := AValue;
  803. if LIndex >= FSize then begin
  804. FSize := LIndex+1;
  805. end;
  806. end;
  807. procedure TIdBuffer.Write(const AValue: TIdIPv6Address; const ADestIndex: Integer);
  808. var
  809. LVal : TIdIPv6Address;
  810. LIndex : Integer;
  811. begin
  812. if ADestIndex < 0 then
  813. begin
  814. LIndex := FHeadIndex + Size;
  815. SetLength(FBytes, LIndex + 16);
  816. end else
  817. begin
  818. LIndex := ADestIndex;
  819. end;
  820. LVal := GStack.HostToNetwork(AValue);
  821. CopyTIdIPV6Address(LVal, FBytes, LIndex);
  822. if LIndex >= FSize then begin
  823. FSize := LIndex+16;
  824. end;
  825. end;
  826. procedure TIdBuffer.Write(const AValue: TIdUInt64; const ADestIndex: Integer);
  827. var
  828. LVal: TIdUInt64;
  829. LIndex: Integer;
  830. begin
  831. if ADestIndex < 0 then
  832. begin
  833. LIndex := FHeadIndex + Size;
  834. SetLength(FBytes, LIndex + 8);
  835. end else
  836. begin
  837. LIndex := ADestIndex;
  838. end;
  839. LVal := GStack.HostToNetwork(AValue);
  840. CopyTIdUInt64(LVal, FBytes, LIndex);
  841. if LIndex >= FSize then begin
  842. FSize := LIndex + 8;
  843. end;
  844. end;
  845. procedure TIdBuffer.Write(const AValue: UInt32; const ADestIndex: Integer);
  846. var
  847. LVal : UInt32;
  848. LIndex : Integer;
  849. begin
  850. if ADestIndex < 0 then
  851. begin
  852. LIndex := FHeadIndex + Size;
  853. SetLength(FBytes, LIndex + 4);
  854. end else
  855. begin
  856. LIndex := ADestIndex;
  857. end;
  858. LVal := GStack.HostToNetwork(AValue);
  859. CopyTIdUInt32(LVal, FBytes, LIndex);
  860. if LIndex >= FSize then begin
  861. FSize := LIndex+4;
  862. end;
  863. end;
  864. procedure TIdBuffer.Write(const ABytes: TIdBytes; const ALength, AOffset : Integer;
  865. const ADestIndex: Integer = -1);
  866. var
  867. LByteLength: Integer;
  868. LIndex : Integer;
  869. begin
  870. LByteLength := IndyLength(ABytes, ALength, AOffset);
  871. if LByteLength = 0 then begin
  872. Exit;
  873. end;
  874. LIndex := IndyMax(ADestIndex, 0);
  875. CheckAdd(LByteLength, LIndex);
  876. if Size = 0 then begin
  877. FHeadIndex := 0;
  878. if ADestIndex < 0 then
  879. begin
  880. FBytes := ToBytes(ABytes, LByteLength, AOffset);
  881. FSize := LByteLength;
  882. end else
  883. begin
  884. FSize := ADestIndex + LByteLength;
  885. SetLength(FBytes, FSize);
  886. CopyTIdBytes(ABytes, AOffset, FBytes, ADestIndex, LByteLength);
  887. end;
  888. end
  889. else if ADestIndex < 0 then
  890. begin
  891. CompactHead(False);
  892. if (Capacity - Size - FHeadIndex) < LByteLength then begin
  893. SetLength(FBytes, Size + LByteLength + GrowthFactor);
  894. end;
  895. CopyTIdBytes(ABytes, AOffset, FBytes, FHeadIndex + Size, LByteLength);
  896. Inc(FSize, LByteLength);
  897. end else
  898. begin
  899. CopyTIdBytes(ABytes, AOffset, FBytes, LIndex, LByteLength);
  900. if LIndex >= FSize then begin
  901. FSize := LIndex + LByteLength;
  902. end;
  903. end;
  904. end;
  905. function TIdBuffer.GetAsString: string;
  906. begin
  907. Result := BytesToString(FBytes, FByteEncoding
  908. {$IFDEF STRING_IS_ANSI}, FAnsiEncoding{$ENDIF}
  909. );
  910. end;
  911. end.