| 1 |
unit UBase64; |
| 2 |
(* 2002 Twiddle *) |
| 3 |
(* ???????ゃ?潟??????合?????鐚????∝?????荐若???????? *) |
| 4 |
|
| 5 |
interface |
| 6 |
uses |
| 7 |
Classes, |
| 8 |
SysUtils; |
| 9 |
|
| 10 |
procedure HogeBase64Encode(inputStream, outputStream: TStream); overload; |
| 11 |
procedure HogeBase64Decode(inputStream, outputStream: TStream); overload; |
| 12 |
|
| 13 |
function HogeBase64Encode(const inputStr: string): string; overload; |
| 14 |
function HogeBase64Decode(const inputStr: string): string; overload; |
| 15 |
function HogeBase64Encode(inputArray: array of Byte): string; overload; |
| 16 |
|
| 17 |
implementation |
| 18 |
|
| 19 |
const |
| 20 |
Enc64Tbl: array[0..63] of Char = |
| 21 |
('A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P', |
| 22 |
'Q','R','S','T','U','V','W','X','Y','Z','a','b','c','d','e','f', |
| 23 |
'g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v', |
| 24 |
'w','x','y','z','0','1','2','3','4','5','6','7','8','9','+','/'); |
| 25 |
|
| 26 |
(* ???潟?若?????若??? ?<?潟??? *) |
| 27 |
|
| 28 |
procedure HogeBase64Encode(inputStream, outputStream: TStream); |
| 29 |
var |
| 30 |
inBuf: array [0..2] of byte; |
| 31 |
outBuf: array[0..3] of Char; |
| 32 |
size: integer; |
| 33 |
begin |
| 34 |
repeat |
| 35 |
size := inputStream.Read(inBuf, sizeof(inBuf)); |
| 36 |
case size of |
| 37 |
0: break; |
| 38 |
1: |
| 39 |
begin |
| 40 |
outBuf[0] := Enc64Tbl[inBuf[0] shr 2]; |
| 41 |
outBuf[1] := Enc64Tbl[(inBuf[0] shl 4) and $3F]; |
| 42 |
outBuf[2] := '='; |
| 43 |
outBuf[3] := '='; |
| 44 |
end; |
| 45 |
2: |
| 46 |
begin |
| 47 |
outBuf[0] := Enc64Tbl[inBuf[0] shr 2]; |
| 48 |
outBuf[1] := Enc64Tbl[((inBuf[0] shl 4) or (inBuf[1] shr 4)) and $3F]; |
| 49 |
outBuf[2] := Enc64Tbl[((inBuf[1] shl 2) ) and $3F]; |
| 50 |
outBuf[3] := '='; |
| 51 |
end; |
| 52 |
3: |
| 53 |
begin |
| 54 |
outBuf[0] := Enc64Tbl[inBuf[0] shr 2]; |
| 55 |
outBuf[1] := Enc64Tbl[((inBuf[0] shl 4) or (inBuf[1] shr 4)) and $3F]; |
| 56 |
outBuf[2] := Enc64Tbl[((inBuf[1] shl 2) or (inBuf[2] shr 6)) and $3F]; |
| 57 |
outBuf[3] := Enc64Tbl[inBuf[2] and $3F]; |
| 58 |
end; |
| 59 |
end; |
| 60 |
outputStream.WriteBuffer(outBuf, sizeof(outBuf)); |
| 61 |
until size < sizeof(inBuf); |
| 62 |
end; |
| 63 |
|
| 64 |
procedure HogeBase64Decode(inputStream, outputStream: TStream); |
| 65 |
var |
| 66 |
inBuf: array [0..3] of Char; |
| 67 |
outBuf: array[0..2] of byte; |
| 68 |
code: byte; |
| 69 |
size: integer; |
| 70 |
i, endPos: integer; |
| 71 |
begin |
| 72 |
code := 0; |
| 73 |
repeat |
| 74 |
size := inputStream.Read(inBuf, sizeof(inBuf)); |
| 75 |
if size = 0 then |
| 76 |
break; |
| 77 |
if size <> 4 then |
| 78 |
raise EConvertError.Create('unexpected size'); |
| 79 |
for i := 0 to sizeof(outBuf) -1 do |
| 80 |
outBuf[i] := 0; |
| 81 |
endPos := -1; |
| 82 |
for i := 0 to sizeof(inBuf) - 1 do |
| 83 |
begin |
| 84 |
case inBuf[i] of |
| 85 |
'A'..'Z': code := Ord(inBuf[i]) - Ord('A'); |
| 86 |
'a'..'z': code := Ord(inBuf[i]) - Ord('a') + 26; |
| 87 |
'0'..'9': code := Ord(inBuf[i]) - Ord('0') + 26 + 26; |
| 88 |
'+': code := 62; |
| 89 |
'/': code := 63; |
| 90 |
'=': |
| 91 |
begin |
| 92 |
if i = 0 then |
| 93 |
raise EConvertError.Create('unexpected "="'); |
| 94 |
endPos := i -1; |
| 95 |
break; |
| 96 |
end; |
| 97 |
else |
| 98 |
raise EConvertError.Create(Format('unexpected code($%2.2X)', [Ord(inBuf[i])])); |
| 99 |
end; |
| 100 |
case i of |
| 101 |
0: |
| 102 |
begin |
| 103 |
outBuf[0] := code shl 2; |
| 104 |
end; |
| 105 |
1: |
| 106 |
begin |
| 107 |
outBuf[0] := outBuf[0] or (code shr 4); |
| 108 |
outBuf[1] := byte( code shl 4 ); |
| 109 |
end; |
| 110 |
2: |
| 111 |
begin |
| 112 |
outBuf[1] := outBuf[1] or (code shr 2); |
| 113 |
outBuf[2] := byte( code shl 6 ); |
| 114 |
end; |
| 115 |
3: |
| 116 |
begin |
| 117 |
outBuf[2] := outBuf[2] or code; |
| 118 |
end; |
| 119 |
end; |
| 120 |
end; |
| 121 |
if endPos < 0 then |
| 122 |
endPos := sizeof(outBuf); |
| 123 |
outputStream.WriteBuffer(outBuf, endPos); |
| 124 |
until size < sizeof(inBuf); |
| 125 |
end; |
| 126 |
|
| 127 |
|
| 128 |
function HogeBase64Encode(const inputStr: string): string; overload; |
| 129 |
var |
| 130 |
inputStream, outputStream: TStringStream; |
| 131 |
begin |
| 132 |
inputStream := TStringStream.Create(inputStr); |
| 133 |
outputStream:= TStringStream.Create(''); |
| 134 |
HogeBase64Encode(inputStream, outputStream); |
| 135 |
result := outputStream.DataString; |
| 136 |
outputStream.Free; |
| 137 |
inputStream.Free; |
| 138 |
end; |
| 139 |
|
| 140 |
function HogeBase64Decode(const inputStr: string): string; overload; |
| 141 |
var |
| 142 |
inputStream, outputStream: TStringStream; |
| 143 |
begin |
| 144 |
inputStream := TStringStream.Create(inputStr); |
| 145 |
outputStream:= TStringStream.Create(''); |
| 146 |
HogeBase64Decode(inputStream, outputStream); |
| 147 |
result := outputStream.DataString; |
| 148 |
outputStream.Free; |
| 149 |
inputStream.Free; |
| 150 |
end; |
| 151 |
|
| 152 |
function HogeBase64Encode(inputArray: array of Byte): string; overload; |
| 153 |
var |
| 154 |
inputStream: TMemoryStream; |
| 155 |
outputStream: TStringStream; |
| 156 |
begin |
| 157 |
inputStream := TMemoryStream.Create; |
| 158 |
inputStream.WriteBuffer(inputArray, Length(inputArray)); |
| 159 |
inputStream.Position := 0; |
| 160 |
outputStream:= TStringStream.Create(''); |
| 161 |
HogeBase64Encode(inputStream, outputStream); |
| 162 |
result := outputStream.DataString; |
| 163 |
outputStream.Free; |
| 164 |
inputStream.Free; |
| 165 |
end; |
| 166 |
|
| 167 |
end. |