IdCoderHeader.pas 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594
  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. $Log$
  13. Rev 1.13 9/8/2004 8:55:46 PM JPMugaas
  14. Fix for compile problem where a char is being compared with an incompatible
  15. type in some compilers.
  16. Rev 1.12 02/07/2004 21:59:28 CCostelloe
  17. Bug fix
  18. Rev 1.11 17/06/2004 14:19:00 CCostelloe
  19. Bug fix for long subject lines that have characters needing CharSet encoding
  20. Rev 1.10 23/04/2004 20:33:04 CCostelloe
  21. Minor change to support From headers holding multiple addresses
  22. Rev 1.9 2004.02.03 5:44:58 PM czhower
  23. Name changes
  24. Rev 1.8 24/01/2004 19:08:14 CCostelloe
  25. Cleaned up warnings
  26. Rev 1.7 1/22/2004 3:56:38 PM SPerry
  27. fixed set problems
  28. Rev 1.6 2004.01.22 2:34:58 PM czhower
  29. TextIsSame + D8 bug workaround
  30. Rev 1.5 10/16/2003 11:11:02 PM DSiders
  31. Added localization comments.
  32. Rev 1.4 10/8/2003 9:49:36 PM GGrieve
  33. Use IdDelete
  34. Rev 1.3 6/10/2003 5:48:46 PM SGrobety
  35. DotNet updates
  36. Rev 1.2 04/09/2003 20:35:28 CCostelloe
  37. Parameter AUseAddressForNameIfNameMissing (defaulting to False to preserve
  38. existing code) added to EncodeAddressItem
  39. Rev 1.1 2003.06.23 9:46:52 AM czhower
  40. Russian, Ukranian support for headers.
  41. Rev 1.0 11/14/2002 02:14:46 PM JPMugaas
  42. }
  43. unit IdCoderHeader;
  44. //refer http://www.faqs.org/rfcs/rfc2047.html
  45. //TODO: Optimize and restructure code
  46. //TODO: Redo this unit to fit with the new coders and use the exisiting MIME stuff
  47. {
  48. 2002-08-21 JM Berg
  49. - brought in line with the RFC regarding
  50. whitespace between encoded words
  51. - added logic so that lines that already seem encoded are really encoded again
  52. (so that if a user types =?iso8859-1?Q?======?= its really encoded again
  53. and displayed like that on the other side)
  54. 2001-Nov-18 Peter Mee
  55. - Fixed multiple QP decoding in single header.
  56. 11-10-2001 - J. Peter Mugaas
  57. - tiny fix for 8bit header encoding suggested by Andrew P.Rybin
  58. }
  59. interface
  60. {$i IdCompilerDefines.inc}
  61. uses
  62. Classes,
  63. IdComponent,
  64. IdEMailAddress;
  65. // Procs
  66. function EncodeAddressItem(EmailAddr: TIdEmailAddressItem; const HeaderEncoding: Char;
  67. const MimeCharSet: string; AUseAddressForNameIfNameMissing: Boolean = False): string;
  68. function EncodeHeader(const Header: string; Specials: String; const HeaderEncoding: Char;
  69. const MimeCharSet: string): string;
  70. function EncodeAddress(EmailAddr: TIdEMailAddressList; const HeaderEncoding: Char;
  71. const MimeCharSet: string; AUseAddressForNameIfNameMissing: Boolean = False): string;
  72. function DecodeHeader(const Header: string): string;
  73. procedure DecodeAddress(EMailAddr: TIdEmailAddressItem);
  74. procedure DecodeAddresses(AEMails: String; EMailAddr: TIdEmailAddressList);
  75. implementation
  76. uses
  77. IdGlobal,
  78. IdGlobalProtocols,
  79. SysUtils;
  80. const
  81. csAddressSpecials: String = '()[]<>:;.,@\"'; {Do not Localize}
  82. base64_tbl: array [0..63] of Char = (
  83. 'A','B','C','D','E','F','G','H', {Do not Localize}
  84. 'I','J','K','L','M','N','O','P', {Do not Localize}
  85. 'Q','R','S','T','U','V','W','X', {Do not Localize}
  86. 'Y','Z','a','b','c','d','e','f', {Do not Localize}
  87. 'g','h','i','j','k','l','m','n', {Do not Localize}
  88. 'o','p','q','r','s','t','u','v', {Do not Localize}
  89. 'w','x','y','z','0','1','2','3', {Do not Localize}
  90. '4','5','6','7','8','9','+','/'); {Do not Localize}
  91. function EncodeAddressItem(EmailAddr: TIdEmailAddressItem; const HeaderEncoding: Char;
  92. const MimeCharSet: string; AUseAddressForNameIfNameMissing: Boolean = False): string;
  93. var
  94. S : string;
  95. I : Integer;
  96. NeedEncode : Boolean;
  97. begin
  98. if AUseAddressForNameIfNameMissing and (EmailAddr.Name = '') then begin
  99. {CC: Use Address as Name...}
  100. EmailAddr.Name := EmailAddr.Address;
  101. end;
  102. if EmailAddr.Name <> '' then {Do not Localize}
  103. begin
  104. NeedEncode := False;
  105. for I := 1 to Length(EmailAddr.Name) do begin
  106. if (EmailAddr.Name[I] < #32) or (EmailAddr.Name[I] >= #127) then
  107. begin
  108. NeedEncode := True;
  109. Break;
  110. end;
  111. end;
  112. if NeedEncode then begin
  113. S := EncodeHeader(EmailAddr.Name, csAddressSpecials, HeaderEncoding, MimeCharSet);
  114. end else begin
  115. { quoted string }
  116. S := '"'; {Do not Localize}
  117. for I := 1 to Length(EmailAddr.Name) do
  118. begin { quote special characters }
  119. if (EmailAddr.Name[I] = '\') or (EmailAddr.Name[I] = '"') then begin
  120. S := S + '\'; {Do not Localize}
  121. end;
  122. S := S + EmailAddr.Name[I];
  123. end;
  124. S := S + '"'; {Do not Localize}
  125. end;
  126. Result := IndyFormat('%s <%s>', [S, EmailAddr.Address]) {Do not Localize}
  127. end
  128. else begin
  129. Result := IndyFormat('%s', [EmailAddr.Address]); {Do not Localize}
  130. end;
  131. end;
  132. function B64(AChar: Char): Byte;
  133. //TODO: Make this use the more efficient MIME Coder
  134. begin
  135. for Result := Low(base64_tbl) to High(base64_tbl) do begin
  136. if AChar = base64_tbl[Result] then begin
  137. Exit;
  138. end;
  139. end;
  140. Result := 0;
  141. end;
  142. function DecodeHeader(const Header: string): string;
  143. var
  144. HeaderCharSet, HeaderEncoding, HeaderData, S: string;
  145. LStartPos, LLength, LEncodingStartPos, LEncodingEndPos, LLastStartPos: Integer;
  146. LLastWordWasEncoded: Boolean;
  147. Buf: TIdBytes;
  148. function ExtractEncoding(const AHeader: string; const AStartPos: Integer;
  149. var VStartPos, VEndPos: Integer; var VCharSet, VEncoding, VData: String): Boolean;
  150. var
  151. LCharSet, LCharSetEnd, LEncoding, LEncodingEnd, LData, LDataEnd: Integer;
  152. begin
  153. Result := False;
  154. //we need a '=? followed by 2 question marks followed by a '?='. {Do not Localize}
  155. //to find the end of the substring, we can't just search for '?=', {Do not Localize}
  156. //example: '=?ISO-8859-1?Q?=E4?=' {Do not Localize}
  157. LCharSet := PosIdx('=?', AHeader, AStartPos); {Do not Localize}
  158. if (LCharSet = 0) or (LCharSet > VEndPos) then begin
  159. Exit;
  160. end;
  161. Inc(LCharSet, 2);
  162. // ignore language, if present
  163. LCharSetEnd := FindFirstOf('*?', AHeader, -1, LCharSet); {Do not Localize}
  164. if (LCharSetEnd = 0) or (LCharSetEnd > VEndPos) then begin
  165. Exit;
  166. end;
  167. if AHeader[LCharSetEnd] = '*' then begin
  168. LEncoding := PosIdx('?', AHeader, LCharSetEnd); {Do not Localize}
  169. if (LEncoding = 0) or (LEncoding > VEndPos) then begin
  170. Exit;
  171. end;
  172. end else begin
  173. LEncoding := LCharSetEnd;
  174. end;
  175. Inc(LEncoding);
  176. LEncodingEnd := PosIdx('?', AHeader, LEncoding); {Do not Localize}
  177. if (LEncodingEnd = 0) or (LEncodingEnd > VEndPos) then begin
  178. Exit;
  179. end;
  180. LData := LEncodingEnd+1;
  181. LDataEnd := PosIdx('?=', AHeader, LData); {Do not Localize}
  182. if (LDataEnd = 0) or (LDataEnd > VEndPos) then begin
  183. Exit;
  184. end;
  185. VStartPos := LCharSet-2;
  186. VEndPos := LDataEnd+1;
  187. VCharSet := Copy(AHeader, LCharSet, LCharSetEnd-LCharSet);
  188. VEncoding := Copy(AHeader, LEncoding, LEncodingEnd-LEncoding);
  189. VData := Copy(AHeader, LData, LDataEnd-LData);
  190. Result := True;
  191. end;
  192. // TODO: use TIdCoderQuotedPrintable and TIdCoderMIME instead
  193. function ExtractEncodedData(const AEncoding, AData: String; var VDecoded: TIdBytes): Boolean;
  194. var
  195. I, J: Integer;
  196. a3: TIdBytes;
  197. a4: array [0..3] of Byte;
  198. begin
  199. Result := False;
  200. SetLength(VDecoded, 0);
  201. case PosInStrArray(AEncoding, ['Q', 'B', '8'], False) of {Do not Localize}
  202. 0: begin // quoted-printable
  203. I := 1;
  204. while I <= Length(AData) do begin
  205. if AData[i] = '_' then begin {Do not Localize}
  206. AppendByte(VDecoded, Ord(' ')); {Do not Localize}
  207. end
  208. else if (AData[i] = '=') and (Length(AData) >= (i+2)) then begin //make sure we can access i+2
  209. AppendByte(VDecoded, IndyStrToInt('$' + Copy(AData, i+1, 2), 32)); {Do not Localize}
  210. Inc(I, 2);
  211. end else
  212. begin
  213. AppendByte(VDecoded, Ord(AData[i]));
  214. end;
  215. Inc(I);
  216. end;
  217. Result := True;
  218. end;
  219. 1: begin // base64
  220. J := Length(AData) div 4;
  221. if J > 0 then
  222. begin
  223. SetLength(a3, 3);
  224. for I := 0 to J-1 do
  225. begin
  226. a4[0] := B64(AData[(I*4)+1]);
  227. a4[1] := B64(AData[(I*4)+2]);
  228. a4[2] := B64(AData[(I*4)+3]);
  229. a4[3] := B64(AData[(I*4)+4]);
  230. a3[0] := Byte((a4[0] shl 2) or (a4[1] shr 4));
  231. a3[1] := Byte((a4[1] shl 4) or (a4[2] shr 2));
  232. a3[2] := Byte((a4[2] shl 6) or (a4[3] shr 0));
  233. if AData[(I*4)+4] = '=' then begin
  234. if AData[(I*4)+3] = '=' then begin
  235. AppendByte(VDecoded, a3[0]);
  236. end else begin
  237. AppendBytes(VDecoded, a3, 0, 2);
  238. end;
  239. Break;
  240. end else begin
  241. AppendBytes(VDecoded, a3, 0, 3);
  242. end;
  243. end;
  244. end;
  245. Result := True;
  246. end;
  247. 2: begin // 8-bit
  248. VDecoded := IndyTextEncoding_8Bit.GetBytes(AData);
  249. Result := True;
  250. end;
  251. end;
  252. end;
  253. begin
  254. Result := Header;
  255. LStartPos := 1;
  256. LLength := Length(Result);
  257. LLastWordWasEncoded := False;
  258. LLastStartPos := LStartPos;
  259. while LStartPos <= LLength do
  260. begin
  261. // valid encoded words can not contain spaces
  262. // if the user types something *almost* like an encoded word,
  263. // and its sent as-is, we need to find this!!
  264. LStartPos := FindFirstNotOf(LWS+CR+LF, Result, LLength, LStartPos);
  265. if LStartPos = 0 then begin
  266. Break;
  267. end;
  268. LEncodingEndPos := FindFirstOf(LWS+CR+LF, Result, LLength, LStartPos);
  269. if LEncodingEndPos <> 0 then begin
  270. Dec(LEncodingEndPos);
  271. end else begin
  272. LEncodingEndPos := LLength;
  273. end;
  274. if ExtractEncoding(Result, LStartPos, LEncodingStartPos, LEncodingEndPos, HeaderCharSet, HeaderEncoding, HeaderData) then
  275. begin
  276. if ExtractEncodedData(HeaderEncoding, HeaderData, Buf) then begin
  277. S := CharsetToEncoding(HeaderCharSet).GetString(Buf);
  278. //replace old substring in header with decoded string,
  279. // ignoring whitespace that separates encoded words:
  280. if LLastWordWasEncoded then begin
  281. Result := Copy(Result, 1, LLastStartPos - 1) + S + Copy(Result, LEncodingEndPos + 1, MaxInt);
  282. LStartPos := LLastStartPos + Length(S);
  283. end else begin
  284. Result := Copy(Result, 1, LEncodingStartPos - 1) + S + Copy(Result, LEncodingEndPos + 1, MaxInt);
  285. LStartPos := LEncodingStartPos + Length(S);
  286. end;
  287. end else
  288. begin
  289. // could not decode the data, so preserve it in case the user
  290. // wants to do it manually. Though, they really should use the
  291. // IdHeaderCoderBase.GHeaderDecodingNeeded hook for that instead...
  292. LStartPos := LEncodingEndPos + 1;
  293. end;
  294. LLength := Length(Result);
  295. LLastWordWasEncoded := True;
  296. LLastStartPos := LStartPos;
  297. end else
  298. begin
  299. LStartPos := FindFirstOf(LWS+CR+LF, Result, LLength, LStartPos);
  300. if LStartPos = 0 then begin
  301. Break;
  302. end;
  303. LLastWordWasEncoded := False;
  304. end;
  305. end;
  306. end;
  307. procedure DecodeAddress(EMailAddr : TIdEmailAddressItem);
  308. begin
  309. EMailAddr.Name := UnquotedStr(DecodeHeader(EMailAddr.Name));
  310. end;
  311. procedure DecodeAddresses(AEMails : String; EMailAddr: TIdEmailAddressList);
  312. var
  313. idx : Integer;
  314. begin
  315. EMailAddr.EMailAddresses := AEMails;
  316. for idx := 0 to EMailAddr.Count-1 do begin
  317. DecodeAddress(EMailAddr[idx]);
  318. end;
  319. end;
  320. function EncodeAddress(EmailAddr: TIdEMailAddressList; const HeaderEncoding: Char;
  321. const MimeCharSet: string; AUseAddressForNameIfNameMissing: Boolean = False): string;
  322. var
  323. idx : Integer;
  324. begin
  325. if EmailAddr.Count > 0 then begin
  326. Result := EncodeAddressItem(EMailAddr[0], HeaderEncoding, MimeCharSet, AUseAddressForNameIfNameMissing);
  327. for idx := 1 to EmailAddr.Count-1 do begin
  328. Result := Result + ', ' + {Do not Localize}
  329. EncodeAddressItem(EMailAddr[idx], HeaderEncoding, MimeCharSet, AUseAddressForNameIfNameMissing);
  330. end;
  331. end else begin
  332. Result := ''; {Do not Localize}
  333. end;
  334. end;
  335. { encode a header field if non-ASCII characters are used }
  336. function EncodeHeader(const Header: string; Specials: String; const HeaderEncoding: Char;
  337. const MimeCharSet: string): string;
  338. const
  339. SPACES = [Ord(' '), 9, 13, 10]; {Do not Localize}
  340. var
  341. T: string;
  342. Buf: TIdBytes;
  343. L, P, Q, R: Integer;
  344. B0, B1, B2: Integer;
  345. InEncode: Integer;
  346. NeedEncode: Boolean;
  347. csNoEncode, csNoReqQuote, csSpecials: TIdBytes;
  348. BeginEncode, EndEncode: string;
  349. procedure EncodeWord(AP: Integer);
  350. const
  351. MaxEncLen = 75;
  352. var
  353. LQ: Integer;
  354. EncLen: Integer;
  355. Enc1: string;
  356. begin
  357. T := T + BeginEncode;
  358. if L < AP then AP := L + 1;
  359. LQ := InEncode;
  360. InEncode := -1;
  361. EncLen := Length(BeginEncode) + 2;
  362. case PosInStrArray(HeaderEncoding, ['Q', 'B'], False) of {Do not Localize}
  363. 0: begin { quoted-printable }
  364. while LQ < AP do
  365. begin
  366. if Buf[LQ] = Ord(' ') then begin {Do not Localize}
  367. Enc1 := '_'; {Do not Localize}
  368. end
  369. else if (not ByteIsInSet(Buf, LQ, csNoReqQuote)) or ByteIsInSet(Buf, LQ, csSpecials) then begin
  370. Enc1 := '=' + IntToHex(Buf[LQ], 2); {Do not Localize}
  371. end
  372. else begin
  373. Enc1 := Char(Buf[LQ]);
  374. end;
  375. if (EncLen + Length(Enc1)) > MaxEncLen then begin
  376. //T := T + EndEncode + #13#10#9 + BeginEncode;
  377. //CC: The #13#10#9 above caused the subsequent call to FoldWrapText to
  378. //insert an extra #13#10 which, being a blank line in the headers,
  379. //was interpreted by email clients, etc., as the end of the headers
  380. //and the start of the message body. FoldWrapText seems to look for
  381. //and treat correctly the sequence #13#10 + ' ' however...
  382. T := T + EndEncode + EOL + ' ' + BeginEncode;
  383. EncLen := Length(BeginEncode) + 2;
  384. end;
  385. T := T + Enc1;
  386. Inc(EncLen, Length(Enc1));
  387. Inc(LQ);
  388. end;
  389. end;
  390. 1: begin { base64 }
  391. while LQ < AP do begin
  392. if (EncLen + 4) > MaxEncLen then begin
  393. //T := T + EndEncode + #13#10#9 + BeginEncode;
  394. //CC: The #13#10#9 above caused the subsequent call to FoldWrapText to
  395. //insert an extra #13#10 which, being a blank line in the headers,
  396. //was interpreted by email clients, etc., as the end of the headers
  397. //and the start of the message body. FoldWrapText seems to look for
  398. //and treat correctly the sequence #13#10 + ' ' however...
  399. T := T + EndEncode + EOL + ' ' + BeginEncode;
  400. EncLen := Length(BeginEncode) + 2;
  401. end;
  402. B0 := Buf[LQ];
  403. case AP - LQ of
  404. 1:
  405. begin
  406. T := T + base64_tbl[B0 shr 2] + base64_tbl[B0 and $03 shl 4] + '=='; {Do not Localize}
  407. end;
  408. 2:
  409. begin
  410. B1 := Buf[LQ + 1];
  411. T := T + base64_tbl[B0 shr 2] +
  412. base64_tbl[B0 and $03 shl 4 + B1 shr 4] +
  413. base64_tbl[B1 and $0F shl 2] + '='; {Do not Localize}
  414. end;
  415. else
  416. begin
  417. B1 := Buf[LQ + 1];
  418. B2 := Buf[LQ + 2];
  419. T := T + base64_tbl[B0 shr 2] +
  420. base64_tbl[B0 and $03 shl 4 + B1 shr 4] +
  421. base64_tbl[B1 and $0F shl 2 + B2 shr 6] +
  422. base64_tbl[B2 and $3F];
  423. end;
  424. end;
  425. Inc(EncLen, 4);
  426. Inc(LQ, 3);
  427. end;
  428. end;
  429. end;
  430. T := T + EndEncode;
  431. end;
  432. function CreateEncodeRange(AStart, AEnd: Byte): TIdBytes;
  433. var
  434. I: Integer;
  435. begin
  436. SetLength(Result, AEnd-AStart+1);
  437. for I := 0 to Length(Result)-1 do begin
  438. Result[I] := AStart+I;
  439. end;
  440. end;
  441. begin
  442. if Header = '' then begin
  443. Result := '';
  444. Exit;
  445. end;
  446. // TODO: this function needs to take encoded codeunits into account when
  447. // deciding where to split the encoded data between adjacent encoded-words,
  448. // so that a single encoded character does not get split between encoded-words
  449. // thus corrupting that character...
  450. Buf := CharsetToEncoding(MimeCharSet).GetBytes(Header);
  451. {Suggested by Andrew P.Rybin for easy 8bit support}
  452. if HeaderEncoding = '8' then begin {Do not Localize}
  453. Result := BytesToStringRaw(Buf);
  454. Exit;
  455. end;//if
  456. // RLebeau 1/7/09: using Char() for #128-#255 because in D2009, the compiler
  457. // may change characters >= #128 from their Ansi codepage value to their true
  458. // Unicode codepoint value, depending on the codepage used for the source code.
  459. // For instance, #128 may become #$20AC...
  460. // RLebeau 2/12/09: changed the logic to use "no-encode" sets instead, so
  461. // that words containing codeunits outside the ASCII range are always
  462. // encoded. This is easier to manage when Unicode data is involved.
  463. csNoEncode := CreateEncodeRange(32, 126);
  464. csNoReqQuote := CreateEncodeRange(33, 60);
  465. AppendByte(csNoReqQuote, 62);
  466. AppendBytes(csNoReqQuote, CreateEncodeRange(64, 94));
  467. AppendBytes(csNoReqQuote, CreateEncodeRange(96, 126));
  468. csSpecials := ToBytes(Specials, IndyTextEncoding_8Bit);
  469. BeginEncode := '=?' + MimeCharSet + '?' + HeaderEncoding + '?'; {Do not Localize}
  470. EndEncode := '?='; {Do not Localize}
  471. // JMBERG: We want to encode stuff that the user typed
  472. // as if it already is encoded!!
  473. if DecodeHeader(Header) <> Header then begin
  474. RemoveBytes(csNoEncode, 1, ByteIndex(Ord('='), csNoEncode));
  475. end;
  476. L := Length(Buf);
  477. P := 0;
  478. T := ''; {Do not Localize}
  479. InEncode := -1;
  480. while P < L do
  481. begin
  482. Q := P;
  483. while (P < L) and (Buf[P] in SPACES) do begin
  484. Inc(P);
  485. end;
  486. R := P;
  487. NeedEncode := False;
  488. while (P < L) and (not (Buf[P] in SPACES)) do begin
  489. if (not ByteIsInSet(Buf, P, csNoEncode)) or ByteIsInSet(Buf, P, csSpecials) then begin
  490. NeedEncode := True;
  491. end;
  492. Inc(P);
  493. end;
  494. if NeedEncode then begin
  495. if InEncode = -1 then begin
  496. T := T + BytesToString(Buf, Q, R - Q);
  497. InEncode := R;
  498. end;
  499. end else
  500. begin
  501. if InEncode <> -1 then begin
  502. EncodeWord(Q);
  503. end;
  504. T := T + BytesToString(Buf, Q, P - Q);
  505. end;
  506. end;
  507. if InEncode <> -1 then begin
  508. EncodeWord(P);
  509. end;
  510. Result := T;
  511. end;
  512. end.