Debian Codegraph @

components/kascrypt/dcpbase64.pas

raw
1 {******************************************************************************}
2 {* DCPcrypt v2.0 written by David Barton ([email protected]) **********}
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
27 interface
28 uses
29 Sysutils;
30
31 function Base64EncodeStr(const Value: string): string;
32 { Encode a string into Base64 format }
33 function Base64DecodeStr(const Value: string): string;
34 { Decode a Base64 format string }
35 function Base64Encode(pInput: pointer; pOutput: pointer; Size: longint): longint;
36 { Encode a lump of raw data (output is (4/3) times bigger than input) }
37 function Base64Decode(pInput: pointer; pOutput: pointer; Size: longint): longint;
38 { Decode a lump of raw data }
39
40
41 {******************************************************************************}
42 {******************************************************************************}
43 implementation
44 {$Q-}{$R-}
45
46 const
47 B64: array[0..63] of byte= (65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,
48 81,82,83,84,85,86,87,88,89,90,97,98,99,100,101,102,103,104,105,106,107,108,
49 109,110,111,112,113,114,115,116,117,118,119,120,121,122,48,49,50,51,52,53,
50 54,55,56,57,43,47);
51
52 function Base64Encode(pInput: pointer; pOutput: pointer; Size: longint): longint;
53 var
54 i, iptr, optr: integer;
55 Input, Output: PByteArray;
56 begin
57 Input:= PByteArray(pInput); Output:= PByteArray(pOutput);
58 iptr:= 0; optr:= 0;
59 for i:= 1 to (Size div 3) do
60 begin
61 Output^[optr+0]:= B64[Input^[iptr] shr 2];
62 Output^[optr+1]:= B64[((Input^[iptr] and 3) shl 4) + (Input^[iptr+1] shr 4)];
63 Output^[optr+2]:= B64[((Input^[iptr+1] and 15) shl 2) + (Input^[iptr+2] shr 6)];
64 Output^[optr+3]:= B64[Input^[iptr+2] and 63];
65 Inc(optr,4); Inc(iptr,3);
66 end;
67 case (Size mod 3) of
68 1: begin
69 Output^[optr+0]:= B64[Input^[iptr] shr 2];
70 Output^[optr+1]:= B64[(Input^[iptr] and 3) shl 4];
71 Output^[optr+2]:= byte('=');
72 Output^[optr+3]:= byte('=');
73 end;
74 2: begin
75 Output^[optr+0]:= B64[Input^[iptr] shr 2];
76 Output^[optr+1]:= B64[((Input^[iptr] and 3) shl 4) + (Input^[iptr+1] shr 4)];
77 Output^[optr+2]:= B64[(Input^[iptr+1] and 15) shl 2];
78 Output^[optr+3]:= byte('=');
79 end;
80 end;
81 Result:= ((Size+2) div 3) * 4;
82 end;
83
84 function Base64EncodeStr(const Value: string): string;
85 begin
86 SetLength(Result,((Length(Value)+2) div 3) * 4);
87 Base64Encode(@Value[1],@Result[1],Length(Value));
88 end;
89
90 function Base64Decode(pInput: pointer; pOutput: pointer; Size: longint): longint;
91 var
92 i, j, iptr, optr: integer;
93 Temp: array[0..3] of byte;
94 Input, Output: PByteArray;
95 begin
96 Input:= PByteArray(pInput); Output:= PByteArray(pOutput);
97 iptr:= 0; optr:= 0;
98 Result:= 0;
99 for i:= 1 to (Size div 4) do
100 begin
101 for j:= 0 to 3 do
102 begin
103 case Input^[iptr] of
104 65..90 : Temp[j]:= Input^[iptr] - Ord('A');
105 97..122: Temp[j]:= Input^[iptr] - Ord('a') + 26;
106 48..57 : Temp[j]:= Input^[iptr] - Ord('0') + 52;
107 43 : Temp[j]:= 62;
108 47 : Temp[j]:= 63;
109 61 : Temp[j]:= $FF;
110 end;
111 Inc(iptr);
112 end;
113 Output^[optr]:= (Temp[0] shl 2) or (Temp[1] shr 4);
114 Result:= optr+1;
115 if (Temp[2]<> $FF) and (Temp[3]= $FF) then
116 begin
117 Output^[optr+1]:= (Temp[1] shl 4) or (Temp[2] shr 2);
118 Result:= optr+2;
119 Inc(optr)
120 end
121 else if (Temp[2]<> $FF) then
122 begin
123 Output^[optr+1]:= (Temp[1] shl 4) or (Temp[2] shr 2);
124 Output^[optr+2]:= (Temp[2] shl 6) or Temp[3];
125 Result:= optr+3;
126 Inc(optr,2);
127 end;
128 Inc(optr);
129 end;
130 end;
131
132 function Base64DecodeStr(const Value: string): string;
133 begin
134 SetLength(Result,(Length(Value) div 4) * 3);
135 SetLength(Result,Base64Decode(@Value[1],@Result[1],Length(Value)));
136 end;
137
138
139 end.