-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
BinTextEnc.pas
4931 lines (4248 loc) · 177 KB
/
BinTextEnc.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
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
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
{-------------------------------------------------------------------------------
This Source Code Form is subject to the terms of the Mozilla Public
License, v. 2.0. If a copy of the MPL was not distributed with this
file, You can obtain one at http://mozilla.org/MPL/2.0/.
-------------------------------------------------------------------------------}
{===============================================================================
Binary data to text encoding and decoding
- Base16, Base32(Hex) and Base64(URL) encodings should be compliant with
RFC 4648
- Base85 encoding is using Z85 alphabet with undescore ("_", #95) as an
all-zero compression character
- Base16 corresponds to usual hexadecimal encoding
- Base10 only encodes to three-digit decimal number representing given byte
- when using Base85 and ASCII85, the decoded data migh end or start (in
case of reversed data reading) with zeroes that were not part of the
original data (a result of these encodings being block-based, these
zeroes are block padding)
Version 2.1.3 (2024-04-14)
Last change 2024-04-28
©2015-2024 František Milt
Contacts:
František Milt: frantisek.milt@gmail.com
Support:
If you find this code useful, please consider supporting its author(s) by
making a small donation using the following link(s):
https://www.paypal.me/FMilt
Changelog:
For detailed changelog and history please refer to this git repository:
github.com/TheLazyTomcat/Lib.BinTextEnc
Dependencies:
AuxClasses - github.com/TheLazyTomcat/Lib.AuxClasses
* AuxExceptions - github.com/TheLazyTomcat/Lib.AuxExceptions
AuxTypes - github.com/TheLazyTomcat/Lib.AuxTypes
StrRect - github.com/TheLazyTomcat/Lib.StrRect
Library AuxExceptions is required only when rebasing local exception classes
(see symbol BinTextEnc_UseAuxExceptions for details).
Library AuxExceptions might also be required as an indirect dependency.
Indirect dependencies:
SimpleCPUID - github.com/TheLazyTomcat/Lib.SimpleCPUID
UInt64Utils - github.com/TheLazyTomcat/Lib.UInt64Utils
WinFileInfo - github.com/TheLazyTomcat/Lib.WinFileInfo
===============================================================================}
unit BinTextEnc;
{
BinTextEnc_UseAuxExceptions
If you want library-specific exceptions to be based on more advanced classes
provided by AuxExceptions library instead of basic Exception class, and don't
want to or cannot change code in this unit, you can define global symbol
BinTextEnc_UseAuxExceptions to achieve this.
}
{$IF Defined(BinTextEnc_UseAuxExceptions)}
{$DEFINE UseAuxExceptions}
{$IFEND}
//------------------------------------------------------------------------------
{$IFDEF FPC}
{$MODE ObjFPC}
{$MODESWITCH DuplicateLocals+}
{$DEFINE FPC_DisableWarns}
{$MACRO ON}
{$ENDIF}
{$H+}
{$IFOPT Q+}
{$DEFINE OverflowChecks}
{$ENDIF}
interface
uses
SysUtils, Classes,
AuxTypes, AuxClasses{$IFDEF UseAuxExceptions}, AuxExceptions{$ENDIF};
{===============================================================================
Library-specific exceptions
===============================================================================}
type
EBTEException = class({$IFDEF UseAuxExceptions}EAEGeneralException{$ELSE}Exception{$ENDIF});
EBTEInvalidValue = class(EBTEException);
EBTEInvalidState = class(EBTEException);
EBTETooMuchData = class(EBTEException);
EBTEBufferTooSmall = class(EBTEException);
EBTEProcessingError = class(EBTEException);
{===============================================================================
--------------------------------------------------------------------------------
TBTETranscoder
--------------------------------------------------------------------------------
===============================================================================}
type
TBTEEncoding = (encUnknown,encBase2,encBase4,encBase8,encBase10,encBase16,
encBase32,encBase32Hex,encBase64,encBase64URL,encBase85,
encASCII85);
{
efReversible encoding supports reversed data reading
efPadding encoding supports padding
efCompression encoding supports compression
efTrim encoding supports trimming
efOutputSize it is possible to obtain length of encoded string or
size of decoded data
efSlowOutputSize same as efOutputSize, but the data/string must be
scanned to obtain the size, this can be slow
efInpreciseOutputSize reported encoded length or decoded size can be
inprecise (note that it will never be smaller), it is
usually rounded to some boundary (eg. block)
}
TBTEEncodingFeature = (efReversible,efPadding,efCompression,efTrim,
efOutputSize,efSlowOutputSize,efInpreciseOutputSize);
TBTEEncodingFeatures = set of TBTEEncodingFeature;
TBTEEncodingTable = array of Char;
TBTEDecodingTable = array[0..127] of Byte;
{===============================================================================
TBTETranscoder - class declaration
===============================================================================}
type
TBTETranscoder = class(TCustomObject)
protected
fEncodedString: String;
fIsProcessing: Boolean;
fBreakProcessing: Boolean;
// encoding settings
fHeader: Boolean;
fReversed: Boolean;
fPadding: Boolean;
fPaddingChar: Char;
fCompression: Boolean;
fCompressionChar: Char;
fTrim: Boolean;
// processing variables
fEncodedStringPos: TStrOff;
// events
fProgressCoef: Integer;
fOnProgressEvent: TFloatEvent;
fOnProgressCallback: TFloatCallback;
// getters/setters
Function GetEncodedString: String; virtual;
procedure SetEncodedString(const Value: String); virtual;
procedure SetPadding(Value: Boolean); virtual;
procedure SetReversed(Value: Boolean); virtual;
procedure SetPaddingChar(Value: Char); virtual;
procedure SetCompression(Value: Boolean); virtual;
procedure SetCompressionChar(Value: Char); virtual;
procedure SetTrim(Value: Boolean); virtual;
procedure SetHeader(Value: Boolean); virtual;
// events firing
procedure DoProgress(Progress: Double); virtual;
// initialization, finalization
procedure InitializeTable; virtual; abstract;
procedure Initialize; virtual;
procedure Finalize; virtual;
// header methods
class Function HeaderNumberExtract(const EncodedString: String; out HeaderNumber: UInt16): Boolean; virtual;
// auxiliary
Function ResolveDataPointer(Ptr: Pointer; Size: TMemSize; RevOffset: UInt32 = 1): Pointer; virtual;
procedure AdvanceDataPointer(var Ptr: Pointer; Delta: TMemSize = 1); virtual;
public
{
Use following to build encoding table type from other types.
}
class Function CreateEncodingTable(const Source: array of Char): TBTEEncodingTable; overload; virtual;
class Function CreateEncodingTable(const Source: String): TBTEEncodingTable; overload; virtual;
{
DataLimit returns maximum number of bytes that can be processed by a
particular encoder.
If you try to encode or even get encoded length for more data, an
EBTETooMuchData exception will be raised.
This limit is here to prevent potential out-of-memory errors and severe
rounding errors in some calculations when encoding.
Note that this limit changes for each encoding, and is selected so that
encoded string can reach a maximum of about 512M characters in length.
}
class Function DataLimit: TMemSize; virtual; abstract;
{
CharLimit sets limit on how long a string can be assigned to the property
EncodedString. If you try to assign longer string, an EBTETooMuchData
exception will be raised.
Currently it is always 512M characters.
}
class Function CharLimit: TStrSize; virtual;
{
Encoding returns what encoding the class is providing.
}
class Function Encoding: TBTEEncoding; virtual; abstract;
{
EncodingFeatures returns set of fetures the particular encoder or decoder
is supporting.
To test for support of specific feature, use the "in" set operator.
If the feature is supported, then the appropriate enumeration values will
be included, otherwise the value will not be included in the returned set.
See decription of type TBTEEncoding for detailed description of indvidual
features.
}
class Function EncodingFeatures: TBTEEncodingFeatures; virtual; abstract;
{
EncodingTableLength returns strict length of the encoding table (sometimes
also termed "alphabet").
}
class Function EncodingTableLength: Integer; virtual; abstract;
{
HeaderLength returns length, in characters, of the header that can be
stored at the beginning of encoded strings.
It will always return 6.
}
class Function HeaderLength: TStrSize; virtual;
{
HeaderPresent returns true when EncodedString starts with a substring that
can be an encoded string header. False otherwise.
When CheckEncoding is set to true, and the string contains a pattern
compatible with header, the function will also check whether the header
contains a valid encoding number or not.
}
class Function HeaderPresent(const EncodedString: String; CheckEncoding: Boolean = False): Boolean; virtual;
{
HeaderEncoding reads header from provided encoded string and returns which
encoding is stored in this header.
If the string does not contain a valid header or the stored encoding is not
known/valid, it will return encUnknown.
}
class Function HeaderEncoding(const EncodedString: String): TBTEEncoding; virtual;
{
HeaderReversed reads header from provided encoded string and returns whether
the encoded string was constructed with reversed data read (see description
of Reversed property for details) or not.
}
class Function HeaderReversed(const EncodedString: String): Boolean; virtual;
{
EncodingTableIsValid checs validity of passed encoding table.
It checks length of the table, ordinal values of all characters (must be
below or equal to 127), and that no character repeats. Note that some
encodings might require stricter rules, they are then checked too.
}
class Function EncodingTableIsValid(const EncodingTable: TBTEEncodingTable): Boolean; virtual;
constructor Create;
destructor Destroy; override;
Function BreakProcessing: Boolean; virtual;
// properties
property EncodedString: String read GetEncodedString write SetEncodedString;
property IsProcessing: Boolean read fIsProcessing;
{
Encoder
When set to true, the encoded string will start with a header sequence
that stores used encoding scheme and some other encoding options (namely
reversed flag).
Decoder
Read-only property (can be set to a different value, but that is of no
use).
Value of this property is set when decoding or when obtaining decoded
size. It is set to true when the encoded string contained a valid header,
false otherwise.
}
property Header: Boolean read fHeader write SetHeader;
{
Encoder
When set to true, then the data being encoded are read in reverse (from
the last byte/block to the first). When false, reading is done in normal
order (from first byte/block to last).
Decoder
If the encoded string contains a valid header, then this value is set
according to a reverse flag in this header (true when the flag is set,
false otherwise).
If you use encoded string that does NOT contain a header, then you must
set this property to a value used during encoding, otherwise the decoded
data will be completely wrong.
}
property Reversed: Boolean read fReversed write SetReversed;
{
Encoder
When set to true, and when the encoding supports padding, then the
produced encoded string will be padded (missing characters at the end
replaced by PaddingCharacter) to certain length.
Decoder
Read-only property.
Value is set when decoding or when obtaining decoded size. It is set to
true when the encoded string ends with one or more PaddingCharacter,
false otherwise.
Note that this does not reflect whether padding was active during
encoding, only whether there is padding character(s) or not.
}
property Padding: Boolean read fPadding write SetPadding;
property PaddingCharacter: Char read fPaddingChar write SetPaddingChar;
{
Encoder
When set to true, and when the encoding supports compression, then some
of the encoded data can be compressed when encoding.
Decoder
Read-only property.
Value is set when decoding or when obtaining decoded size. It is set to
true when the encoded string contains one or more CompressionCharacter,
false otherwise.
Note that this does not reflect whether compression was active during
encoding, only whether there is compression character(s) or not.
}
property Compression: Boolean read fCompression write SetCompression;
property CompressionCharacter: Char read fCompressionChar write SetCompressionChar;
{
Encoder
When set to true, and when the encoding supports trimming, then some
the resulting encoded string might be shortened by characters that are
not carrying any information about the encoded data.
Decoder
Read-only property.
Value is set when decoding or when obtaining decoded size. It is set to
true when the encoded string seems to be trimmed (is shorter than is
expected), false otherwise.
Note that this does not reflect whether trimming was active during
encoding, only whether there are some missing characters.
}
property Trim: Boolean read fTrim write SetTrim;
property ProgressCoefficient: Integer read fProgressCoef write fProgressCoef;
property OnProgressEvent: TFloatEvent read fOnProgressEvent write fOnProgressEvent;
property OnProgressCallback: TFloatCallback read fOnProgressCallback write fOnProgressCallback;
property OnProgress: TFloatEvent read fOnProgressEvent write fOnProgressEvent;
end;
{===============================================================================
--------------------------------------------------------------------------------
TBTEEncoder
--------------------------------------------------------------------------------
===============================================================================}
{===============================================================================
TBTEEncoder - class declaration
===============================================================================}
type
TBTEEncoder = class(TBTETranscoder)
protected
fEncodingTable: TBTEEncodingTable;
Function GetEncodingTable: TBTEEncodingTable; virtual;
procedure SetEncodingTable(const Value: TBTEEncodingTable); virtual;
procedure AssignEncodingTable(const EncodingTable: TBTEEncodingTable); virtual;
// processing
procedure WriteHeader; virtual;
procedure WriteChar(C: Char); virtual;
procedure WritePadding; virtual;
procedure Encode(const Buffer; Size: TMemSize); virtual;
procedure EncodeSpecific(const Buffer; Size: TMemSize); virtual; abstract;
public
// resulting string length
Function EncodedLengthFromBuffer(const Buffer; Size: TMemSize): TStrSize; virtual; abstract;
Function EncodedLengthFromMemory(Mem: Pointer; Size: TMemSize): TStrSize; virtual;
Function EncodedLengthFromStream(Stream: TStream; Count: Int64 = -1): TStrSize; virtual;
Function EncodedLengthFromFile(const FileName: String): TStrSize; virtual;
// processing
procedure EncodeFromBuffer(const Buffer; Size: TMemSize); virtual;
procedure EncodeFromMemory(Mem: Pointer; Size: TMemSize); virtual;
procedure EncodeFromStream(Stream: TStream; Count: Int64 = -1); virtual;
procedure EncodeFromFile(const FileName: String); virtual;
property EncodingTable: TBTEEncodingTable read GetEncodingTable write SetEncodingTable;
end;
TBTEEncoderClass = class of TBTEEncoder;
{===============================================================================
--------------------------------------------------------------------------------
TBTEDecoder
--------------------------------------------------------------------------------
===============================================================================}
{===============================================================================
TBTEDecoder - class declaration
===============================================================================}
type
TBTEDecoder = class(TBTETranscoder)
protected
fDecodingTable: TBTEDecodingTable;
// processing
procedure ReadHeader; virtual; // sets properties Header and Reversed (if header is present)
Function ReadChar: Char; virtual;
procedure RollBack; virtual;
Function ResolveChar(C: Char): Byte; virtual;
Function CountPadding: TStrSize; virtual;
procedure Decode(out Buffer; Size: TMemSize); virtual;
procedure DecodeSpecific(out Buffer; Size: TMemSize); virtual; abstract;
public
procedure ConstructDecodingTable(const EncodingTable: TBTEEncodingTable); virtual;
// resulting data size
Function DecodedSize: TMemSize; virtual; abstract; // sets properties Padding, Compression and Trim
//processing
Function DecodeIntoBuffer(out Buffer; Size: TMemSize): TMemSize; virtual;
Function DecodeIntoMemory(Mem: Pointer; Size: TMemSize): TMemSize; overload; virtual;
Function DecodeIntoMemory(out Mem: Pointer): TMemSize; overload; virtual;
Function DecodeIntoStream(Stream: TStream): TMemSize; virtual;
Function DecodeIntoFile(const FileName: String): TMemSize; virtual;
property DecodingTable: TBTEDecodingTable read fDecodingTable write fDecodingTable;
end;
TBTEDecoderClass = class of TBTEDecoder;
{===============================================================================
--------------------------------------------------------------------------------
TBase2Encoder
--------------------------------------------------------------------------------
===============================================================================}
{===============================================================================
TBase2Encoder - class declaration
===============================================================================}
type
TBase2Encoder = class(TBTEEncoder)
protected
procedure InitializeTable; override;
procedure EncodeSpecific(const Buffer; Size: TMemSize); override;
public
class Function DataLimit: TMemSize; override;
class Function Encoding: TBTEEncoding; override;
class Function EncodingFeatures: TBTEEncodingFeatures; override;
class Function EncodingTableLength: Integer; override;
Function EncodedLengthFromBuffer(const Buffer; Size: TMemSize): TStrSize; override;
end;
{===============================================================================
--------------------------------------------------------------------------------
TBase2Decoder
--------------------------------------------------------------------------------
===============================================================================}
{===============================================================================
TBase2Decoder - class declaration
===============================================================================}
type
TBase2Decoder = class(TBTEDecoder)
protected
procedure InitializeTable; override;
procedure DecodeSpecific(out Buffer; Size: TMemSize); override;
public
class Function DataLimit: TMemSize; override;
class Function Encoding: TBTEEncoding; override;
class Function EncodingFeatures: TBTEEncodingFeatures; override;
class Function EncodingTableLength: Integer; override;
Function DecodedSize: TMemSize; override;
end;
{===============================================================================
--------------------------------------------------------------------------------
TBase4Encoder
--------------------------------------------------------------------------------
===============================================================================}
{===============================================================================
TBase4Encoder - class declaration
===============================================================================}
type
TBase4Encoder = class(TBTEEncoder)
protected
procedure InitializeTable; override;
procedure EncodeSpecific(const Buffer; Size: TMemSize); override;
public
class Function DataLimit: TMemSize; override;
class Function Encoding: TBTEEncoding; override;
class Function EncodingFeatures: TBTEEncodingFeatures; override;
class Function EncodingTableLength: Integer; override;
Function EncodedLengthFromBuffer(const Buffer; Size: TMemSize): TStrSize; override;
end;
{===============================================================================
--------------------------------------------------------------------------------
TBase4Decoder
--------------------------------------------------------------------------------
===============================================================================}
{===============================================================================
TBase4Decoder - class declaration
===============================================================================}
type
TBase4Decoder = class(TBTEDecoder)
protected
procedure InitializeTable; override;
procedure DecodeSpecific(out Buffer; Size: TMemSize); override;
public
class Function DataLimit: TMemSize; override;
class Function Encoding: TBTEEncoding; override;
class Function EncodingFeatures: TBTEEncodingFeatures; override;
class Function EncodingTableLength: Integer; override;
Function DecodedSize: TMemSize; override;
end;
{===============================================================================
--------------------------------------------------------------------------------
TBase8Encoder
--------------------------------------------------------------------------------
===============================================================================}
{===============================================================================
TBase8Encoder - class declaration
===============================================================================}
type
TBase8Encoder = class(TBTEEncoder)
protected
procedure InitializeTable; override;
procedure EncodeSpecific(const Buffer; Size: TMemSize); override;
public
class Function DataLimit: TMemSize; override;
class Function Encoding: TBTEEncoding; override;
class Function EncodingFeatures: TBTEEncodingFeatures; override;
class Function EncodingTableLength: Integer; override;
Function EncodedLengthFromBuffer(const Buffer; Size: TMemSize): TStrSize; override;
end;
{===============================================================================
--------------------------------------------------------------------------------
TBase8Decoder
--------------------------------------------------------------------------------
===============================================================================}
{===============================================================================
TBase8Decoder - class declaration
===============================================================================}
type
TBase8Decoder = class(TBTEDecoder)
protected
procedure InitializeTable; override;
procedure DecodeSpecific(out Buffer; Size: TMemSize); override;
public
class Function DataLimit: TMemSize; override;
class Function Encoding: TBTEEncoding; override;
class Function EncodingFeatures: TBTEEncodingFeatures; override;
class Function EncodingTableLength: Integer; override;
Function DecodedSize: TMemSize; override;
end;
{===============================================================================
--------------------------------------------------------------------------------
TBase10Encoder
--------------------------------------------------------------------------------
===============================================================================}
{===============================================================================
TBase10Encoder - class declaration
===============================================================================}
type
TBase10Encoder = class(TBTEEncoder)
protected
procedure InitializeTable; override;
procedure EncodeSpecific(const Buffer; Size: TMemSize); override;
public
class Function DataLimit: TMemSize; override;
class Function Encoding: TBTEEncoding; override;
class Function EncodingFeatures: TBTEEncodingFeatures; override;
class Function EncodingTableLength: Integer; override;
Function EncodedLengthFromBuffer(const Buffer; Size: TMemSize): TStrSize; override;
end;
{===============================================================================
--------------------------------------------------------------------------------
TBase10Decoder
--------------------------------------------------------------------------------
===============================================================================}
{===============================================================================
TBase10Decoder - class declaration
===============================================================================}
type
TBase10Decoder = class(TBTEDecoder)
protected
procedure InitializeTable; override;
procedure DecodeSpecific(out Buffer; Size: TMemSize); override;
public
class Function DataLimit: TMemSize; override;
class Function Encoding: TBTEEncoding; override;
class Function EncodingFeatures: TBTEEncodingFeatures; override;
class Function EncodingTableLength: Integer; override;
Function DecodedSize: TMemSize; override;
end;
{===============================================================================
--------------------------------------------------------------------------------
TBase16Encoder
--------------------------------------------------------------------------------
===============================================================================}
{===============================================================================
TBase16Encoder - class declaration
===============================================================================}
type
TBase16Encoder = class(TBTEEncoder)
protected
procedure InitializeTable; override;
procedure EncodeSpecific(const Buffer; Size: TMemSize); override;
public
class Function DataLimit: TMemSize; override;
class Function Encoding: TBTEEncoding; override;
class Function EncodingFeatures: TBTEEncodingFeatures; override;
class Function EncodingTableLength: Integer; override;
Function EncodedLengthFromBuffer(const Buffer; Size: TMemSize): TStrSize; override;
end;
{===============================================================================
--------------------------------------------------------------------------------
TBase16Decoder
--------------------------------------------------------------------------------
===============================================================================}
{===============================================================================
TBase16Decoder - class declaration
===============================================================================}
type
TBase16Decoder = class(TBTEDecoder)
protected
procedure InitializeTable; override;
procedure DecodeSpecific(out Buffer; Size: TMemSize); override;
public
class Function DataLimit: TMemSize; override;
class Function Encoding: TBTEEncoding; override;
class Function EncodingFeatures: TBTEEncodingFeatures; override;
class Function EncodingTableLength: Integer; override;
Function DecodedSize: TMemSize; override;
end;
{===============================================================================
--------------------------------------------------------------------------------
TBase32Encoder
--------------------------------------------------------------------------------
===============================================================================}
{===============================================================================
TBase32Encoder - class declaration
===============================================================================}
type
TBase32Encoder = class(TBTEEncoder)
protected
procedure InitializeTable; override;
procedure EncodeSpecific(const Buffer; Size: TMemSize); override;
public
class Function DataLimit: TMemSize; override;
class Function Encoding: TBTEEncoding; override;
class Function EncodingFeatures: TBTEEncodingFeatures; override;
class Function EncodingTableLength: Integer; override;
Function EncodedLengthFromBuffer(const Buffer; Size: TMemSize): TStrSize; override;
end;
{===============================================================================
--------------------------------------------------------------------------------
TBase32Decoder
--------------------------------------------------------------------------------
===============================================================================}
{===============================================================================
TBase32Decoder - class declaration
===============================================================================}
type
TBase32Decoder = class(TBTEDecoder)
protected
procedure InitializeTable; override;
procedure DecodeSpecific(out Buffer; Size: TMemSize); override;
public
class Function DataLimit: TMemSize; override;
class Function Encoding: TBTEEncoding; override;
class Function EncodingFeatures: TBTEEncodingFeatures; override;
class Function EncodingTableLength: Integer; override;
Function DecodedSize: TMemSize; override;
end;
{===============================================================================
--------------------------------------------------------------------------------
TBase32HexEncoder
--------------------------------------------------------------------------------
===============================================================================}
{===============================================================================
TBase32HexEncoder - class declaration
===============================================================================}
type
TBase32HexEncoder = class(TBase32Encoder)
protected
procedure InitializeTable; override;
public
class Function Encoding: TBTEEncoding; override;
class Function EncodingTableLength: Integer; override;
end;
{===============================================================================
--------------------------------------------------------------------------------
TBase32HexDecoder
--------------------------------------------------------------------------------
===============================================================================}
{===============================================================================
TBase32HexDecoder - class declaration
===============================================================================}
type
TBase32HexDecoder = class(TBase32Decoder)
protected
procedure InitializeTable; override;
public
class Function Encoding: TBTEEncoding; override;
class Function EncodingTableLength: Integer; override;
end;
{===============================================================================
--------------------------------------------------------------------------------
TBase64Encoder
--------------------------------------------------------------------------------
===============================================================================}
{===============================================================================
TBase64Encoder - class declaration
===============================================================================}
type
TBase64Encoder = class(TBTEEncoder)
protected
procedure InitializeTable; override;
procedure EncodeSpecific(const Buffer; Size: TMemSize); override;
public
class Function DataLimit: TMemSize; override;
class Function Encoding: TBTEEncoding; override;
class Function EncodingFeatures: TBTEEncodingFeatures; override;
class Function EncodingTableLength: Integer; override;
Function EncodedLengthFromBuffer(const Buffer; Size: TMemSize): TStrSize; override;
end;
{===============================================================================
--------------------------------------------------------------------------------
TBase64Decoder
--------------------------------------------------------------------------------
===============================================================================}
{===============================================================================
TBase64Decoder - class declaration
===============================================================================}
type
TBase64Decoder = class(TBTEDecoder)
protected
procedure InitializeTable; override;
procedure DecodeSpecific(out Buffer; Size: TMemSize); override;
public
class Function DataLimit: TMemSize; override;
class Function Encoding: TBTEEncoding; override;
class Function EncodingFeatures: TBTEEncodingFeatures; override;
class Function EncodingTableLength: Integer; override;
Function DecodedSize: TMemSize; override;
end;
{===============================================================================
--------------------------------------------------------------------------------
TBase64URLEncoder
--------------------------------------------------------------------------------
===============================================================================}
{===============================================================================
TBase64URLEncoder - class declaration
===============================================================================}
type
TBase64URLEncoder = class(TBase64Encoder)
protected
procedure InitializeTable; override;
public
class Function Encoding: TBTEEncoding; override;
class Function EncodingTableLength: Integer; override;
end;
{===============================================================================
--------------------------------------------------------------------------------
TBase64URLDecoder
--------------------------------------------------------------------------------
===============================================================================}
{===============================================================================
TBase64URLDecoder - class declaration
===============================================================================}
type
TBase64URLDecoder = class(TBase64Decoder)
protected
procedure InitializeTable; override;
public
class Function Encoding: TBTEEncoding; override;
class Function EncodingTableLength: Integer; override;
end;
{===============================================================================
--------------------------------------------------------------------------------
TBase85Encoder
--------------------------------------------------------------------------------
===============================================================================}
{===============================================================================
TBase85Encoder - class declaration
===============================================================================}
type
TBase85Encoder = class(TBTEEncoder)
protected
procedure InitializeTable; override;
procedure EncodeSpecific(const Buffer; Size: TMemSize); override;
public
class Function DataLimit: TMemSize; override;
class Function Encoding: TBTEEncoding; override;
class Function EncodingFeatures: TBTEEncodingFeatures; override;
class Function EncodingTableLength: Integer; override;
class Function EncodingTableIsValid(const EncodingTable: TBTEEncodingTable): Boolean; override;
Function EncodedLengthFromBuffer(const Buffer; Size: TMemSize): TStrSize; override;
end;
{===============================================================================
--------------------------------------------------------------------------------
TBase85Decoder
--------------------------------------------------------------------------------
===============================================================================}
{===============================================================================
TBase85Decoder - class declaration
===============================================================================}
type
TBase85Decoder = class(TBTEDecoder)
protected
Function ReadChar: Char; override;
procedure InitializeTable; override;
procedure DecodeSpecific(out Buffer; Size: TMemSize); override;
public
class Function DataLimit: TMemSize; override;
class Function Encoding: TBTEEncoding; override;
class Function EncodingFeatures: TBTEEncodingFeatures; override;
class Function EncodingTableLength: Integer; override;
class Function EncodingTableIsValid(const EncodingTable: TBTEEncodingTable): Boolean; override;
Function DecodedSize: TMemSize; override;
end;
{===============================================================================
--------------------------------------------------------------------------------
TASCII85Encoder
--------------------------------------------------------------------------------
===============================================================================}
{===============================================================================
TASCII85Encoder - class declaration
===============================================================================}
type
TASCII85Encoder = class(TBase85Encoder)
protected
procedure InitializeTable; override;
public
class Function Encoding: TBTEEncoding; override;
class Function EncodingTableLength: Integer; override;
end;
{===============================================================================
--------------------------------------------------------------------------------
TASCII85Decoder
--------------------------------------------------------------------------------
===============================================================================}
{===============================================================================
TASCII85Decoder - class declaration
===============================================================================}
type
TASCII85Decoder = class(TBase85Decoder)
protected
procedure InitializeTable; override;
public
class Function Encoding: TBTEEncoding; override;
class Function EncodingTableLength: Integer; override;
end;
{===============================================================================
--------------------------------------------------------------------------------
Procedural interface
--------------------------------------------------------------------------------
===============================================================================}
{===============================================================================
Procedural interface - specialized functions declaration
===============================================================================}
Function EncodedLength_Base2(const Buffer; Size: TMemSize; Header: Boolean = False): TStrSize;
Function Encode_Base2(const Buffer; Size: TMemSize; Header: Boolean = False; Reversed: Boolean = False): String;
Function DecodedSize_Base2(const EncodedString: String): TMemSize;
Function Decode_Base2(const EncodedString: String; out Buffer; Size: TMemSize; Reversed: Boolean = False): TMemSize;
//------------------------------------------------------------------------------
Function EncodedLength_Base4(const Buffer; Size: TMemSize; Header: Boolean = False): TStrSize;
Function Encode_Base4(const Buffer; Size: TMemSize; Header: Boolean = False; Reversed: Boolean = False): String;
Function DecodedSize_Base4(const EncodedString: String): TMemSize;
Function Decode_Base4(const EncodedString: String; out Buffer; Size: TMemSize; Reversed: Boolean = False): TMemSize;
//------------------------------------------------------------------------------
Function EncodedLength_Base8(const Buffer; Size: TMemSize; Header: Boolean = False; Padding: Boolean = False): TStrSize;
Function Encode_Base8(const Buffer; Size: TMemSize; Header: Boolean = False; Reversed: Boolean = False; Padding: Boolean = False): String;
Function DecodedSize_Base8(const EncodedString: String): TMemSize;
Function Decode_Base8(const EncodedString: String; out Buffer; Size: TMemSize; Reversed: Boolean = False): TMemSize;
//------------------------------------------------------------------------------
Function EncodedLength_Base10(const Buffer; Size: TMemSize; Header: Boolean = False): TStrSize;
Function Encode_Base10(const Buffer; Size: TMemSize; Header: Boolean = False; Reversed: Boolean = False): String;
Function DecodedSize_Base10(const EncodedString: String): TMemSize;
Function Decode_Base10(const EncodedString: String; out Buffer; Size: TMemSize; Reversed: Boolean = False): TMemSize;
//------------------------------------------------------------------------------
Function EncodedLength_Base16(const Buffer; Size: TMemSize; Header: Boolean = False): TStrSize;
Function Encode_Base16(const Buffer; Size: TMemSize; Header: Boolean = False; Reversed: Boolean = False): String;
Function DecodedSize_Base16(const EncodedString: String): TMemSize;
Function Decode_Base16(const EncodedString: String; out Buffer; Size: TMemSize; Reversed: Boolean = False): TMemSize;
//------------------------------------------------------------------------------
Function EncodedLength_Base32(const Buffer; Size: TMemSize; Header: Boolean = False; Padding: Boolean = False): TStrSize;
Function Encode_Base32(const Buffer; Size: TMemSize; Header: Boolean = False; Reversed: Boolean = False; Padding: Boolean = False): String;
Function DecodedSize_Base32(const EncodedString: String): TMemSize;
Function Decode_Base32(const EncodedString: String; out Buffer; Size: TMemSize; Reversed: Boolean = False): TMemSize;
//------------------------------------------------------------------------------
Function EncodedLength_Base32Hex(const Buffer; Size: TMemSize; Header: Boolean = False; Padding: Boolean = False): TStrSize;
Function Encode_Base32Hex(const Buffer; Size: TMemSize; Header: Boolean = False; Reversed: Boolean = False; Padding: Boolean = False): String;
Function DecodedSize_Base32Hex(const EncodedString: String): TMemSize;
Function Decode_Base32Hex(const EncodedString: String; out Buffer; Size: TMemSize; Reversed: Boolean = False): TMemSize;
//------------------------------------------------------------------------------
Function EncodedLength_Base64(const Buffer; Size: TMemSize; Header: Boolean = False; Padding: Boolean = False): TStrSize;
Function Encode_Base64(const Buffer; Size: TMemSize; Header: Boolean = False; Reversed: Boolean = False; Padding: Boolean = False): String;
Function DecodedSize_Base64(const EncodedString: String): TMemSize;
Function Decode_Base64(const EncodedString: String; out Buffer; Size: TMemSize; Reversed: Boolean = False): TMemSize;
//------------------------------------------------------------------------------
Function EncodedLength_Base64URL(const Buffer; Size: TMemSize; Header: Boolean = False; Padding: Boolean = False): TStrSize;
Function Encode_Base64URL(const Buffer; Size: TMemSize; Header: Boolean = False; Reversed: Boolean = False; Padding: Boolean = False): String;
Function DecodedSize_Base64URL(const EncodedString: String): TMemSize;
Function Decode_Base64URL(const EncodedString: String; out Buffer; Size: TMemSize; Reversed: Boolean = False): TMemSize;
//------------------------------------------------------------------------------
Function EncodedLength_Base85(const Buffer; Size: TMemSize; Header: Boolean = False; Reversed: Boolean = False; Compression: Boolean = False; Trim: Boolean = False): TStrSize;
Function Encode_Base85(const Buffer; Size: TMemSize; Header: Boolean = False; Reversed: Boolean = False; Compression: Boolean = False; Trim: Boolean = False): String;
Function DecodedSize_Base85(const EncodedString: String): TMemSize;
Function Decode_Base85(const EncodedString: String; out Buffer; Size: TMemSize; Reversed: Boolean = False): TMemSize;
//------------------------------------------------------------------------------
Function EncodedLength_ASCII85(const Buffer; Size: TMemSize; Header: Boolean = False; Reversed: Boolean = False; Compression: Boolean = False; Trim: Boolean = False): TStrSize;
Function Encode_ASCII85(const Buffer; Size: TMemSize; Header: Boolean = False; Reversed: Boolean = False; Compression: Boolean = False; Trim: Boolean = False): String;
Function DecodedSize_ASCII85(const EncodedString: String): TMemSize;
Function Decode_ASCII85(const EncodedString: String; out Buffer; Size: TMemSize; Reversed: Boolean = False): TMemSize;
{===============================================================================
Procedural interface - universal functions declaration
===============================================================================}
Function EncodedLength(Encoding: TBTEEncoding; const Buffer; Size: TMemSize; Header: Boolean = False;
Reversed: Boolean = False; Padding: Boolean = False; Compression: Boolean = False; Trim: Boolean = False): TStrSize;
Function Encode(Encoding: TBTEEncoding; const Buffer; Size: TMemSize; Header: Boolean = False;
Reversed: Boolean = False; Padding: Boolean = False; Compression: Boolean = False; Trim: Boolean = False): String;
Function DecodedSize(Encoding: TBTEEncoding; const EncodedString: String): TMemSize; overload;
{
Buffer must be already allocated to accommodate all data (pass allocated size
in Size parameter). To get the required size, use function DecodedSize.
}
Function Decode(Encoding: TBTEEncoding; const EncodedString: String; out Buffer; Size: TMemSize; Reversed: Boolean = False): TMemSize; overload;
//------------------------------------------------------------------------------
// retuns encUnknown if the string does not contain a valid header
Function HeaderEncoding(const EncodedString: String): TBTEEncoding;
Function DecodedSize(const EncodedString: String): TMemSize; overload;
Function Decode(const EncodedString: String; out Buffer; Size: TMemSize): TMemSize; overload;
//------------------------------------------------------------------------------
Function EncoderFromEncoding(Encoding: TBTEEncoding): TBTEEncoderClass;