-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathTxtEncodings.pas
198 lines (155 loc) · 4.68 KB
/
TxtEncodings.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
unit TxtEncodings;
{$mode objfpc}{$H+}
//PV Unpack
//https://github.com/PascalVault
//Licence: MIT
//Last update: 2023-10-25
interface
uses
Classes, SysUtils, DateUtils, Math, Dialogs;
procedure DecodeB64(InStr, OutStr: TStream; ALength: Integer);
procedure DecodeYENC(InStr, OutStr: TStream; ALength: Integer);
procedure DecodeXXE(InStr, OutStr: TStream; ALength: Integer);
procedure DecodeUUE(InStr, OutStr: TStream; ALength: Integer);
implementation
procedure DecodeB64(InStr, OutStr: TStream; ALength: Integer);
const CharsTab: String = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
var Chars: array[0..255] of Byte;
List: TStringList;
Buf: String;
DecLine, Line: String;
LineB: array of Byte absolute Line;
Val: Cardinal;
ValC: array[0..3] of Char absolute Val;
Len: Integer;
k,j: Integer;
begin
//convert char tab- for speed-up
for k:=0 to 255 do Chars[k] := 0;
for k:=1 to Length(CharsTab) do
Chars[ord(CharsTab[k])] := k-1;
SetLength(Buf, ALength);
InStr.Read(Buf[1], ALength);
List := TStringList.Create;
List.Text := Buf;
Buf := '';
DecLine := '';
for k:=0 to List.Count-1 do begin
if List[k] = '' then continue;
Line := List[k];
Len := Length(Line);
j := 0;
while j < Len do begin
Val := (Chars[ LineB[j+0] ] shl 18) +
(Chars[ LineB[j+1] ] shl 12) +
(Chars[ LineB[j+2] ] shl 6) +
Chars[ LineB[j+3] ];
DecLine := DecLine + ValC[2] + ValC[1] + ValC[0];
Inc(j,4);
end;
end;
List.Free;
OutStr.Write(DecLine[1], Length(DecLine));
end;
procedure DecodeYENC(InStr, OutStr: TStream; ALength: Integer);
var Ch: Byte;
Buf: String;
DecLine: String;
i: Integer;
begin
SetLength(Buf, ALength);
InStr.Read(Buf[1], ALength);
DecLine := '';
i := 1;
while i <= ALength do begin
Ch := ord(Buf[i]);
if Ch = ord('=') then begin
DecLine := DecLine + chr(ord(Buf[i+1])-106);
Inc(i);
end
else if (Ch <> 13) and (Ch <> 10) then DecLine := DecLine + chr(Ch - 42);
Inc(i);
end;
OutStr.Write(DecLine[1], Length(DecLine));
end;
procedure DecodeXXE(InStr, OutStr: TStream; ALength: Integer);
const CharsTab: String = '+-0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
var Chars: array[0..255] of Byte;
List: TStringList;
Buf: String;
DecLine, Line: String;
LineB: array of Byte absolute Line;
Val: Cardinal;
ValC: array[0..3] of Char absolute Val;
Len: Integer;
k,j: Integer;
begin
//convert char tab- for speed-up
for k:=0 to 255 do Chars[k] := 0;
for k:=1 to Length(CharsTab) do
Chars[ord(CharsTab[k])] := k-1;
SetLength(Buf, ALength);
InStr.Read(Buf[1], ALength);
List := TStringList.Create;
List.Text := Buf;
Buf := '';
DecLine := '';
for k:=0 to List.Count-1 do begin
if List[k] = '' then continue;
if List[k] = '+' then break;
Line := List[k];
Len := Chars[LineB[0]];
j := 1;
while j<Ceil(4*Len/3) do begin
Val := (Chars[ LineB[j+0] ] shl 18) +
(Chars[ LineB[j+1] ] shl 12) +
(Chars[ LineB[j+2] ] shl 6) +
Chars[ LineB[j+3] ];
DecLine := DecLine + ValC[2] + ValC[1] + ValC[0];
Inc(j,4);
end;
end;
List.Free;
OutStr.Write(DecLine[1], Length(DecLine));
end;
procedure DecodeUUE(InStr, OutStr: TStream; ALength: Integer);
const CharsTab: String = ' !"#$%&''()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_';
var Chars: array[0..255] of Byte;
List: TStringList;
Buf: String;
DecLine, Line: String;
LineB: array of Byte absolute Line;
Val: Cardinal;
ValC: array[0..3] of Char absolute Val;
Len: Integer;
k,j: Integer;
begin
//convert char tab- for speed-up
for k:=0 to 255 do Chars[k] := 0;
for k:=1 to Length(CharsTab) do
Chars[ord(CharsTab[k])] := k-1;
SetLength(Buf, ALength);
InStr.Read(Buf[1], ALength);
List := TStringList.Create;
List.Text := Buf;
Buf := '';
DecLine := '';
for k:=0 to List.Count-1 do begin
if List[k] = '' then continue;
if List[k] = '`' then break;
Line := List[k];
Len := Chars[LineB[0]];
j := 1;
while j<Ceil(4*Len/3) do begin
Val := (Chars[ LineB[j+0] ] shl 18) +
(Chars[ LineB[j+1] ] shl 12) +
(Chars[ LineB[j+2] ] shl 6) +
Chars[ LineB[j+3] ];
DecLine := DecLine + ValC[2] + ValC[1] + ValC[0];
Inc(j,4);
end;
end;
List.Free;
OutStr.Write(DecLine[1], Length(DecLine));
end;
end.