3 (*
\82©
\82È
\82è
\83C
\83\93\83`
\83L
\81\95\8cø
\97¦
\88«
\82¢
\81\95\90^
\96Ê
\96Ú
\82É
\8c\9f\8fØ
\82µ
\82Ä
\82È
\82¢ *)
10 procedure HogeBase64Encode(inputStream, outputStream: TStream); overload;
11 procedure HogeBase64Decode(inputStream, outputStream: TStream); overload;
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;
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','+','/');
26 (*
\83f
\83R
\81[
\83h
\83e
\81[
\83u
\83\8b \83\81\83\93\83h
\83C *)
28 procedure HogeBase64Encode(inputStream, outputStream: TStream);
30 inBuf: array [0..2] of byte;
31 outBuf: array[0..3] of Char;
35 size := inputStream.Read(inBuf, sizeof(inBuf));
40 outBuf[0] := Enc64Tbl[inBuf[0] shr 2];
41 outBuf[1] := Enc64Tbl[(inBuf[0] shl 4) and $3F];
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];
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];
60 outputStream.WriteBuffer(outBuf, sizeof(outBuf));
61 until size < sizeof(inBuf);
64 procedure HogeBase64Decode(inputStream, outputStream: TStream);
66 inBuf: array [0..3] of Char;
67 outBuf: array[0..2] of byte;
74 size := inputStream.Read(inBuf, sizeof(inBuf));
78 raise EConvertError.Create('unexpected size');
79 for i := 0 to sizeof(outBuf) -1 do
82 for i := 0 to sizeof(inBuf) - 1 do
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;
93 raise EConvertError.Create('unexpected "="');
98 raise EConvertError.Create(Format('unexpected code($%2.2X)', [Ord(inBuf[i])]));
103 outBuf[0] := code shl 2;
107 outBuf[0] := outBuf[0] or (code shr 4);
108 outBuf[1] := byte( code shl 4 );
112 outBuf[1] := outBuf[1] or (code shr 2);
113 outBuf[2] := byte( code shl 6 );
117 outBuf[2] := outBuf[2] or code;
122 endPos := sizeof(outBuf);
123 outputStream.WriteBuffer(outBuf, endPos);
124 until size < sizeof(inBuf);
128 function HogeBase64Encode(const inputStr: string): string; overload;
130 inputStream, outputStream: TStringStream;
132 inputStream := TStringStream.Create(inputStr);
133 outputStream:= TStringStream.Create('');
134 HogeBase64Encode(inputStream, outputStream);
135 result := outputStream.DataString;
140 function HogeBase64Decode(const inputStr: string): string; overload;
142 inputStream, outputStream: TStringStream;
144 inputStream := TStringStream.Create(inputStr);
145 outputStream:= TStringStream.Create('');
146 HogeBase64Decode(inputStream, outputStream);
147 result := outputStream.DataString;
152 function HogeBase64Encode(inputArray: array of Byte): string; overload;
154 inputStream: TMemoryStream;
155 outputStream: TStringStream;
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;