OSDN Git Service

datファイルのD&Dで、ファイル名チェックで、ファイルパスまで送っていたので、
[gikonavigoeson/gikonavi.git] / Encrypt / UBase64.pas
1 unit UBase64;
2 (* 2002 Twiddle *)
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¢ *)
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
16 implementation
17
18 const
19   Enc64Tbl: array[0..63] of Char =
20              ('A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P',
21               'Q','R','S','T','U','V','W','X','Y','Z','a','b','c','d','e','f',
22               'g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v',
23               'w','x','y','z','0','1','2','3','4','5','6','7','8','9','+','/');
24
25   (* \83f\83R\81[\83h\83e\81[\83u\83\8b \83\81\83\93\83h\83C *)
26
27 procedure HogeBase64Encode(inputStream, outputStream: TStream);
28 var
29   inBuf: array [0..2] of byte;
30   outBuf: array[0..3] of Char;
31   size: integer;
32 begin
33   repeat
34     size := inputStream.Read(inBuf, sizeof(inBuf));
35     case size of
36     0: break;
37     1:
38       begin
39         outBuf[0] := Enc64Tbl[inBuf[0] shr 2];
40         outBuf[1] := Enc64Tbl[(inBuf[0] shl 4) and $3F];
41         outBuf[2] := '=';
42         outBuf[3] := '=';
43       end;
44     2:
45       begin
46         outBuf[0] := Enc64Tbl[inBuf[0] shr 2];
47         outBuf[1] := Enc64Tbl[((inBuf[0] shl 4) or (inBuf[1] shr 4)) and $3F];
48         outBuf[2] := Enc64Tbl[((inBuf[1] shl 2)                     ) and $3F];
49         outBuf[3] := '=';
50       end;
51     3:
52       begin
53         outBuf[0] := Enc64Tbl[inBuf[0] shr 2];
54         outBuf[1] := Enc64Tbl[((inBuf[0] shl 4) or (inBuf[1] shr 4)) and $3F];
55         outBuf[2] := Enc64Tbl[((inBuf[1] shl 2) or (inBuf[2] shr 6)) and $3F];
56         outBuf[3] := Enc64Tbl[inBuf[2] and $3F];
57       end;
58     end;
59     outputStream.WriteBuffer(outBuf, sizeof(outBuf));
60   until size < sizeof(inBuf);
61 end;
62
63 procedure HogeBase64Decode(inputStream, outputStream: TStream);
64 var
65   inBuf: array [0..3] of Char;
66   outBuf: array[0..2] of byte;
67   code: byte;
68   size: integer;
69   i, endPos: integer;
70 begin
71   code := 0;
72   repeat
73     size := inputStream.Read(inBuf, sizeof(inBuf));
74     if size = 0 then
75       break;
76     if size <> 4 then
77       raise EConvertError.Create('unexpected size');
78     for i := 0 to sizeof(outBuf) -1 do
79       outBuf[i] := 0;
80     endPos := -1;
81     for i := 0 to sizeof(inBuf) - 1 do
82     begin
83       case inBuf[i] of
84       'A'..'Z': code := Ord(inBuf[i]) - Ord('A');
85       'a'..'z': code := Ord(inBuf[i]) - Ord('a') + 26;
86       '0'..'9': code := Ord(inBuf[i]) - Ord('0') + 26 + 26;
87       '+':      code := 62;
88       '/':      code := 63;
89       '=':
90         begin
91           if i = 0 then
92             raise EConvertError.Create('unexpected "="');
93           endPos := i -1;
94           break;
95         end;
96       else
97         raise EConvertError.Create(Format('unexpected code($%2.2X)', [Ord(inBuf[i])]));
98       end;
99       case i of
100       0:
101         begin
102           outBuf[0] := code shl 2;
103         end;
104       1:
105         begin
106           outBuf[0] := outBuf[0] or (code shr 4);
107           outBuf[1] := byte( code shl 4 );
108         end;
109       2:
110         begin
111           outBuf[1] := outBuf[1] or (code shr 2);
112           outBuf[2] := byte( code shl 6 );
113         end;
114       3:
115         begin
116           outBuf[2] := outBuf[2] or code;
117         end;
118       end;
119     end;
120     if endPos < 0 then
121       endPos := sizeof(outBuf);
122     outputStream.WriteBuffer(outBuf, endPos);
123   until size < sizeof(inBuf);
124 end;
125
126
127 function HogeBase64Encode(const inputStr: string): string; overload;
128 var
129   inputStream, outputStream: TStringStream;
130 begin
131   inputStream := TStringStream.Create(inputStr);
132   outputStream:= TStringStream.Create('');
133   HogeBase64Encode(inputStream, outputStream);
134   result := outputStream.DataString;
135   outputStream.Free;
136   inputStream.Free;
137 end;
138
139 function HogeBase64Decode(const inputStr: string): string; overload;
140 var
141   inputStream, outputStream: TStringStream;
142 begin
143   inputStream := TStringStream.Create(inputStr);
144   outputStream:= TStringStream.Create('');
145   HogeBase64Decode(inputStream, outputStream);
146   result := outputStream.DataString;
147   outputStream.Free;
148   inputStream.Free;
149 end;
150
151 end.