/
SeBase64.pas
153 lines (141 loc) · 3.62 KB
/
SeBase64.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
unit SeBase64;
{$IFDEF FPC}
{$MODE delphi}
{$ENDIF}
interface
uses SysUtils;
// Convierte datos binarios a texto (base64)
function BinToStr(Data: PByteArray; Len: Integer): AnsiString;
// Convierte texto a datos binarios
function StrToBin(Str: AnsiString; var Len: Integer; Dirty: Boolean): PByteArray; overload;
function StrToBin(Str: AnsiString; var Len: Integer): PByteArray; overload;
// Limpia una cadena elimnando los caracteres que sobran
function Base64CleanStr(Str: PAnsiChar; Count: Integer): Integer;
implementation
var
Table: array[0..255] of AnsiChar;
invTable: array[#0..#255] of Byte;
// Rellenamos la tablas auxiliares
procedure InitTables;
var
i: Integer;
c: AnsiChar;
begin
FillChar(Table,Sizeof(Table),#0);
FillChar(invTable,Sizeof(Table),#$FF);
i:= 0;
for c:= 'A' to 'Z' do
begin
Table[i]:= c;
invTable[c]:= i;
inc(i);
end;
for c:= 'a' to 'z' do
begin
Table[i]:= c;
invTable[c]:= i;
inc(i);
end;
for c:= '0' to '9' do
begin
Table[i]:= c;
invTable[c]:= i;
inc(i);
end;
Table[62]:= '+';
Table[63]:= '/';
invTable['+']:= 62;
invTable['/']:= 63;
end;
function BinToStr(Data: PByteArray; Len: Integer): AnsiString;
begin
Result:= EmptyStr;
while Len > 2 do
begin
Result:= Result + Table[Data[0] shr 2];
Result:= Result + Table[((Data[0] and $03) shl 4) + (Data[1] shr 4)];
Result:= Result + Table[((Data[1] and $0F) shl 2) + (Data[2] shr 6)];
Result:= Result + Table[Data[2] and $3F];
inc(PByte(Data),3);
dec(Len,3);
end;
case Len of
2: begin
Result:= Result + Table[Data[0] shr 2];
Result:= Result + Table[((Data[0] and $03) shl 4) + (Data[1] shr 4)];
Result:= Result + Table[((Data[1] and $0F) shl 2)];
Result:= Result + '=';
end;
1: begin
Result:= Result + Table[Data[0] shr 2];
Result:= Result + Table[((Data[0] and $03) shl 4)];
Result:= Result + '==';
end;
end;
end;
// Limpia una cadena elimnando los caracteres que sobran
function Base64CleanStr(Str: PAnsiChar; Count: Integer): Integer;
var
P: PAnsiChar;
begin
Result:= 0;
P:= Str;
while Count > 0 do
begin
if invTable[Str^] <> $FF then
begin
P^:= Str^;
inc(P);
inc(Result);
end;
inc(Str);
dec(Count);
end;
end;
function StrToBin(Str: AnsiString; var Len: Integer; Dirty: Boolean): PByteArray;
var
i: Integer;
begin
Result:= nil;
// Si no esta limpia, limpiamos la cadena
if Dirty then
SetLength(Str,Base64CleanStr(PAnsiChar(Str),Length(Str)));
// Reservamos memoria para el resultado
Len:= (Length(Str) div 4) * 3;
case Length(Str) mod 4 of
0: ;
3: inc(Len,2);
2: inc(Len);
else Exit;
end;
GetMem(Result, Len);
for i:= 0 to (Len div 3) - 1 do
begin
Result[3*i]:= (invTable[Str[(4*i)+1]] shl 2)
+ (invTable[Str[(4*i)+2]] shr 4);
Result[(3*i)+1]:= ((invTable[Str[(4*i)+2]] and $0F) shl 4)
+ (invTable[Str[(4*i)+3]] shr 2);
Result[(3*i)+2]:= ((invTable[Str[(4*i)+3]] and $03) shl 6)
+ invTable[Str[(4*i)+4]];
end;
i:= (Len div 3);
case Length(Str) mod 4 of
3: begin
Result[3*i]:= (invTable[Str[(4*i)+1]] shl 2)
+ (invTable[Str[(4*i)+2]] shr 4);
Result[(3*i)+1]:= ((invTable[Str[(4*i)+2]] and $0F) shl 4)
+ (invTable[Str[(4*i)+3]] shr 2);
end;
2: begin
Result[3*i]:= (invTable[Str[(4*i)+1]] shl 2)
+ (invTable[Str[(4*i)+2]] shr 4);
end;
end;
end;
function StrToBin(Str: AnsiString; var Len: Integer): PByteArray;
begin
Result:= StrToBin(Str,Len,TRUE);
end;
initialization
InitTables;
end.