DCPbase64.pas 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. {******************************************************************************}
  2. {* DCPcrypt v2.0 written by David Barton (crypto@cityinthesky.co.uk) **********}
  3. {******************************************************************************}
  4. {* A Base64 encoding/decoding unit ********************************************}
  5. {******************************************************************************}
  6. {* Copyright (c) 1999-2002 David Barton *}
  7. {* Permission is hereby granted, free of charge, to any person obtaining a *}
  8. {* copy of this software and associated documentation files (the "Software"), *}
  9. {* to deal in the Software without restriction, including without limitation *}
  10. {* the rights to use, copy, modify, merge, publish, distribute, sublicense, *}
  11. {* and/or sell copies of the Software, and to permit persons to whom the *}
  12. {* Software is furnished to do so, subject to the following conditions: *}
  13. {* *}
  14. {* The above copyright notice and this permission notice shall be included in *}
  15. {* all copies or substantial portions of the Software. *}
  16. {* *}
  17. {* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *}
  18. {* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *}
  19. {* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL *}
  20. {* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *}
  21. {* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING *}
  22. {* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER *}
  23. {* DEALINGS IN THE SOFTWARE. *}
  24. {******************************************************************************}
  25. unit DCPbase64;
  26. interface
  27. uses
  28. Sysutils;
  29. function Base64EncodeStr(const Value: AnsiString): AnsiString;
  30. { Encode a string into Base64 format }
  31. function Base64DecodeStr(const Value: AnsiString): AnsiString;
  32. { Decode a Base64 format string }
  33. function Base64Encode(pInput: pointer; pOutput: pointer; Size: longint): longint;
  34. { Encode a lump of raw data (output is (4/3) times bigger than input) }
  35. function Base64Decode(pInput: pointer; pOutput: pointer; Size: longint): longint;
  36. { Decode a lump of raw data }
  37. {******************************************************************************}
  38. {******************************************************************************}
  39. implementation
  40. {$Q-}{$R-}
  41. const
  42. B64: array[0..63] of byte= (65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,
  43. 81,82,83,84,85,86,87,88,89,90,97,98,99,100,101,102,103,104,105,106,107,108,
  44. 109,110,111,112,113,114,115,116,117,118,119,120,121,122,48,49,50,51,52,53,
  45. 54,55,56,57,43,47);
  46. function Base64Encode(pInput: pointer; pOutput: pointer; Size: longint): longint;
  47. var
  48. i, iptr, optr: integer;
  49. Input, Output: PByteArray;
  50. begin
  51. Input:= PByteArray(pInput); Output:= PByteArray(pOutput);
  52. iptr:= 0; optr:= 0;
  53. for i:= 1 to (Size div 3) do
  54. begin
  55. Output^[optr+0]:= B64[Input^[iptr] shr 2];
  56. Output^[optr+1]:= B64[((Input^[iptr] and 3) shl 4) + (Input^[iptr+1] shr 4)];
  57. Output^[optr+2]:= B64[((Input^[iptr+1] and 15) shl 2) + (Input^[iptr+2] shr 6)];
  58. Output^[optr+3]:= B64[Input^[iptr+2] and 63];
  59. Inc(optr,4); Inc(iptr,3);
  60. end;
  61. case (Size mod 3) of
  62. 1: begin
  63. Output^[optr+0]:= B64[Input^[iptr] shr 2];
  64. Output^[optr+1]:= B64[(Input^[iptr] and 3) shl 4];
  65. Output^[optr+2]:= byte('=');
  66. Output^[optr+3]:= byte('=');
  67. end;
  68. 2: begin
  69. Output^[optr+0]:= B64[Input^[iptr] shr 2];
  70. Output^[optr+1]:= B64[((Input^[iptr] and 3) shl 4) + (Input^[iptr+1] shr 4)];
  71. Output^[optr+2]:= B64[(Input^[iptr+1] and 15) shl 2];
  72. Output^[optr+3]:= byte('=');
  73. end;
  74. end;
  75. Result:= ((Size+2) div 3) * 4;
  76. end;
  77. function Base64EncodeStr(const Value: AnsiString): AnsiString;
  78. begin
  79. SetLength(Result,((Length(Value)+2) div 3) * 4);
  80. Base64Encode(@Value[1],@Result[1],Length(Value));
  81. end;
  82. function Base64Decode(pInput: pointer; pOutput: pointer; Size: longint): longint;
  83. var
  84. i, j, iptr, optr: integer;
  85. Temp: array[0..3] of byte;
  86. Input, Output: PByteArray;
  87. begin
  88. Input:= PByteArray(pInput); Output:= PByteArray(pOutput);
  89. iptr:= 0; optr:= 0;
  90. Result:= 0;
  91. for i:= 1 to (Size div 4) do
  92. begin
  93. for j:= 0 to 3 do
  94. begin
  95. case Input^[iptr] of
  96. 65..90 : Temp[j]:= Input^[iptr] - Ord('A');
  97. 97..122: Temp[j]:= Input^[iptr] - Ord('a') + 26;
  98. 48..57 : Temp[j]:= Input^[iptr] - Ord('0') + 52;
  99. 43 : Temp[j]:= 62;
  100. 47 : Temp[j]:= 63;
  101. 61 : Temp[j]:= $FF;
  102. end;
  103. Inc(iptr);
  104. end;
  105. Output^[optr]:= (Temp[0] shl 2) or (Temp[1] shr 4);
  106. Result:= optr+1;
  107. if (Temp[2]<> $FF) and (Temp[3]= $FF) then
  108. begin
  109. Output^[optr+1]:= (Temp[1] shl 4) or (Temp[2] shr 2);
  110. Result:= optr+2;
  111. Inc(optr)
  112. end
  113. else if (Temp[2]<> $FF) then
  114. begin
  115. Output^[optr+1]:= (Temp[1] shl 4) or (Temp[2] shr 2);
  116. Output^[optr+2]:= (Temp[2] shl 6) or Temp[3];
  117. Result:= optr+3;
  118. Inc(optr,2);
  119. end;
  120. Inc(optr);
  121. end;
  122. end;
  123. function Base64DecodeStr(const Value: AnsiString): AnsiString;
  124. begin
  125. SetLength(Result,(Length(Value) div 4) * 3);
  126. SetLength(Result,Base64Decode(@Value[1],@Result[1],Length(Value)));
  127. end;
  128. end.