-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathdwsEncoding.pas
More file actions
456 lines (411 loc) · 13.1 KB
/
Copy pathdwsEncoding.pas
File metadata and controls
456 lines (411 loc) · 13.1 KB
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
{**********************************************************************}
{ }
{ "The contents of this file are subject to the Mozilla Public }
{ License Version 1.1 (the "License"); you may not use this }
{ file except in compliance with the License. You may obtain }
{ a copy of the License at http://www.mozilla.org/MPL/ }
{ }
{ Software distributed under the License is distributed on an }
{ "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express }
{ or implied. See the License for the specific language }
{ governing rights and limitations under the License. }
{ }
{ Copyright Creative IT. }
{ Current maintainer: Eric Grange }
{ }
{**********************************************************************}
unit dwsEncoding;
{$I dws.inc}
{$R-}
interface
uses SysUtils;
type
TBase64Alphabet = array [0..63] of Char;
function Base58Encode(const data : RawByteString) : String; overload; inline;
function Base58Encode(data : Pointer; len : Integer) : String; overload;
function Base58Decode(const data : String) : RawByteString;
// RFC 4648 without padding
function Base32Encode(const data : RawByteString) : String; overload; inline;
function Base32Encode(data : Pointer; len : Integer) : String; overload;
function Base32Decode(const data : String) : RawByteString;
function Base64EncodeURI(const data : RawByteString) : String; overload; inline;
function Base64EncodeURI(data : Pointer; len : Integer) : String; overload; inline;
function Base64Encode(const data : RawByteString) : String; overload; inline;
function Base64Encode(data : Pointer; len : Integer) : String; overload; inline;
function Base64Encode(data : Pointer; len : Integer; const alphabet : TBase64Alphabet) : String; overload;
function Base64Decode(const data : String) : RawByteString;
const
cBase58 : String = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz';
cBase64 : TBase64Alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
cBase64URI : TBase64Alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_';
// ------------------------------------------------------------------
// ------------------------------------------------------------------
// ------------------------------------------------------------------
implementation
// ------------------------------------------------------------------
// ------------------------------------------------------------------
// ------------------------------------------------------------------
function DivBy58(var v : Integer) : Integer;
const
cMagic : Cardinal = 2369637129;
{$ifdef WIN32_ASM}
asm
mov ecx, eax
mov eax, dword ptr [eax]
mul eax, cMagic
mov eax, edx
shr eax, 5
mov edx, eax
imul edx, 58
sub dword ptr [ecx], edx
{$else}
begin
Result := UInt64(cMagic)*v shr 37;
v := v - Result*58;
{$endif}
end;
// Base58Encode
//
function Base58Encode(const data : RawByteString) : String;
begin
Result := Base58Encode(Pointer(data), Length(data));
end;
// Base58Encode
//
function Base58Encode(data : Pointer; len : Integer) : String;
var
i, j, carry, nextCarry, n : Integer;
digits : array of Integer;
begin
if len <= 0 then exit;
Result := '';
n := -1;
for j := 1 to len do begin
if PByte(data)[j-1] = 0 then
Result := Result + cBase58[1]
else begin
SetLength(digits, 1);
digits[0] := 0;
n := 0;
break;
end;
end;
for i := Length(Result) to len-1 do begin
digits[0] := (digits[0] shl 8) + PByte(data)[i];
carry := DivBy58(digits[0]);
for j := 1 to n do begin
digits[j] := (digits[j] shl 8) + carry;
carry := DivBy58(digits[j]);
end;
while carry > 0 do begin
Inc(n);
SetLength(digits, n+1);
nextCarry := carry div 58;
digits[n] := carry - nextCarry*58;
carry := nextCarry;
end;
end;
for j := n downto 0 do
Result := Result + cBase58[digits[j]+1];
end;
// Base58Decode
//
function Base58Decode(const data : String) : RawByteString;
var
i, j, carry, n, d : Integer;
bytes : array of Integer;
begin
if data = '' then exit;
Result := '';
n := -1;
for j := 1 to Length(data) do begin
if data[j] = '1' then
Result := Result + #0
else begin
SetLength(bytes, 1);
bytes[0] := 0;
n := 0;
break;
end;
end;
for i := Length(Result)+1 to Length(data) do begin
d := Pos(data[i], cBase58)-1;
if d<0 then
raise Exception.Create('Non-base58 character');
for j := 0 to n do
bytes[j] := bytes[j]*58;
bytes[0] := bytes[0]+d;
carry := 0;
for j := 0 to n do begin
bytes[j] := bytes[j] + carry;
carry := bytes[j] shr 8;
bytes[j] := bytes[j] and $FF;
end;
while carry > 0 do begin
Inc(n);
SetLength(bytes, n+1);
bytes[n] := carry and $FF;
carry := carry shr 8;
end;
end;
for j := n downto 0 do
Result := Result + AnsiChar(bytes[j]);
end;
const
cBase32 : array [0..31] of Char = (
'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P',
'Q','R','S','T','U','V','W','X','Y','Z','2','3','4','5','6','7'
);
function Base32Encode(data : Pointer; len : Integer) : String;
var
i, n, c, b : Integer;
pIn : PByteArray;
pOut : PChar;
begin
if (len = 0) or (data = nil) then Exit('');
n := len;
SetLength(Result, ((n div 5)+1)*8);
c := 0;
b := 0;
pIn := data;
pOut := Pointer(Result);
for i := 0 to n-1 do begin
c := (c shl 8) or pIn[i];
Inc(b, 8);
while b >= 5 do begin
Dec(b, 5);
pOut^ := cBase32[(c shr b) and $1F];
Inc(pOut);
end;
end;
if b > 0 then begin
pOut^ := cBase32[(c shl (5-b)) and $1F];
Inc(pOut);
end;
n := (NativeUInt(pOut)-NativeUInt(Pointer(Result))) div SizeOf(Char);
SetLength(Result, n);
end;
// Base32Encode
//
function Base32Encode(const data : RawByteString) : String;
begin
Result:=Base32Encode(Pointer(data), Length(data));
end;
// Base32Decode
//
var
vBase32DecodeTable : array [#0..'z'] of Byte;
function Base32Decode(const data : String) : RawByteString;
procedure PrepareTable;
var
c : Char;
begin
for c := #0 to High(vBase32DecodeTable) do begin
case c of
'A'..'Z' : vBase32DecodeTable[c] := Ord(c)-Ord('A');
'a'..'z' : vBase32DecodeTable[c] := Ord(c)-Ord('a');
'2'..'7' : vBase32DecodeTable[c] := Ord(c)+(26-Ord('2'));
'0' : vBase32DecodeTable[c] := Ord('O')-Ord('A');
else
vBase32DecodeTable[c] := 255;
end;
end;
end;
var
c, b, i, n, d : Integer;
pIn : PChar;
pOut : PByte;
begin
if data = '' then Exit('');
if vBase32DecodeTable['z']=0 then PrepareTable;
n := Length(data);
SetLength(Result, ((n div 8)+1)*5);
pIn := Pointer(data);
pOut := Pointer(Result);
c := 0;
b := 0;
for i := 0 to n-1 do begin
d := vBase32DecodeTable[pIn[i]];
if d = 255 then begin
if pIn[i] = '=' then break;
raise Exception.CreateFmt('Invalid character (#%d) in Base32', [Ord(pIn[i])]);
end;
c := (c shl 5) or d;
Inc(b, 5);
if b >= 8 then begin
Dec(b, 8);
pOut^ := c shr b;
Inc(pOut);
end;
end;
n := NativeUInt(pOut)-NativeUInt(Pointer(Result));
SetLength(Result, n);
end;
type
TBase64Block = record
v0, skip0 : Byte;
v1, skip1 : Byte;
v2, skip2 : Byte;
v3, skip3 : Byte;
end;
PBase64Block = ^TBase64Block;
var
vBase64Decode : array [0..255] of Integer;
// PrepareBase64DecodeTable
//
procedure PrepareBase64DecodeTable;
var
i : Integer;
begin
// < 0 is for non-base64 characters
// -1 is for invalid characters
// -2 is for characters that are allowed (and ignored) between blocks
// table should be filled sequentially so preparation does not need a threading lock
for i := 0 to High(vBase64Decode) do begin
case Char(i) of
#1..#32 : vBase64Decode[i] := -2;
'A'..'Z' : vBase64Decode[i] := i - Ord('A');
'a'..'z' : vBase64Decode[i] := i + (26 - Ord('a'));
'0'..'9' : vBase64Decode[i] := i + (52 - Ord('0'));
'+', '-' : vBase64Decode[Ord('+')] := 62;
'/', '_' : vBase64Decode[Ord('/')] := 63;
else
vBase64Decode[i] := -1;
end;
end;
end;
// Base64EncodeURI
//
function Base64EncodeURI(const data : RawByteString) : String;
begin
Result := Base64Encode(Pointer(data), Length(data), cBase64URI);
end;
// Base64EncodeURI
//
function Base64EncodeURI(data : Pointer; len : Integer) : String;
begin
Result := Base64Encode(data, len, cBase64URI);
end;
// Base64Encode
//
function Base64Encode(const data : RawByteString) : String;
begin
Result := Base64Encode(Pointer(data), Length(data), cBase64);
end;
// Base64Encode
//
function Base64Encode(data : Pointer; len : Integer) : String;
begin
Result := Base64Encode(data, len, cBase64);
end;
// Base64Encode
//
function Base64Encode(data : Pointer; len : Integer; const alphabet : TBase64Alphabet) : String; overload;
var
outLen, blocks, tail, i : Integer;
dest : PChar;
src : PByte;
c : Cardinal;
begin
outLen := ((len+2) div 3)*4;
SetLength(Result, outLen);
if outLen = 0 then Exit;
blocks := len div 3;
tail := len - blocks*3;
dest := Pointer(Result);
src := PByte(data);
for i := 1 to blocks do begin
c := (src[0] shl 16) + (src[1] shl 8) + src[2];
dest[0] := alphabet[(c shr 18) and $3f];
dest[1] := alphabet[(c shr 12) and $3f];
dest[2] := alphabet[(c shr 6) and $3f];
dest[3] := alphabet[c and $3f];
Inc(dest, 4);
Inc(src, 3);
end;
case tail of
1 : begin
c := src[0] shl 4;
dest[0] := alphabet[(c shr 6) and $3f];
dest[1] := alphabet[c and $3f];
dest[2] := '=';
dest[3] := '=';
end;
2 : begin
c := (src[0] shl 10) + (src[1] shl 2);
dest[0] := alphabet[(c shr 12) and $3f];
dest[1] := alphabet[(c shr 6) and $3f];
dest[2] := alphabet[c and $3f];
dest[3] := '=';
end;
end;
end;
// Base64Decode
//
function Base64Decode(const data : String) : RawByteString;
function Base64Decode(src, srcLast : PBase64Block; dest : PByte) : PByte;
var
c, ch : Integer;
begin
c := 0;
while NativeUInt(src) <= NativeUInt(srcLast) do begin
ch := vBase64Decode[src.v0];
if ch >= 0 then begin
c := ch shl 6;
ch := vBase64Decode[src.v1];
if ch >= 0 then begin
c := (c or ch) shl 6;
ch := vBase64Decode[src.v2];
if ch >= 0 then begin
c := (c or ch) shl 6;
ch := vBase64Decode[src.v3];
if ch >= 0 then begin
c := c or ch;
dest[2] := c;
c := c shr 8;
dest[1] := c;
c := c shr 8;
dest[0] := c;
Inc(dest,3);
Inc(src);
Continue;
end else begin
c := c shr 8;
dest[1] := c;
dest[0] := c shr 8;
Inc(dest, 2);
Break;
end;
end;
end;
end else if ch = -2 then begin
src := @PChar(src)[1];
Continue;
end;
dest[0] := c shr 10;
Inc(dest);
Break;
end;
Result := dest;
end;
var
len, outLen : Integer;
decodedPtr : PByte;
begin
if data = '' then Exit;
if vBase64Decode[High(vBase64Decode)] = 0 then
PrepareBase64DecodeTable;
len := Length(data);
while (data[len] <= ' ') and (len > 1) do Dec(len);
outLen := (len shr 2)*3;
if data[len] = '=' then begin
if (len > 1) and (data[len-1] = '=') then
Dec(outLen, 2)
else Dec(outLen);
end;
SetLength(Result, outLen);
decodedPtr := Base64Decode(Pointer(data), Pointer(@data[len-3]), Pointer(Result));
if decodedPtr <> @PByte(Result)[outLen] then
SetLength(Result, NativeUInt(decodedPtr) - NativeUInt(Pointer(Result)));
end;
end.