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
|
{
*********************************************************************
Copyright (C) 2012 Paul Ishenin,
member of the Free Pascal Development Team
See the file COPYING.FPC, included in this distribution,
for details about the copyright.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*********************************************************************
}
{ TEncoding }
class function TEncoding.GetANSI: TEncoding;
begin
{$ifdef FPC_HAS_FEATURE_THREADING}
EnterCriticalSection(FLock);
try
{$endif}
if not Assigned(FStandardEncodings[seAnsi]) then
begin
if Assigned(widestringmanager.GetStandardCodePageProc) then
FStandardEncodings[seAnsi] := TMBCSEncoding.Create(widestringmanager.GetStandardCodePageProc(scpAnsi))
else
FStandardEncodings[seAnsi] := TMBCSEncoding.Create(DefaultSystemCodePage);
end;
{$ifdef FPC_HAS_FEATURE_THREADING}
finally
LeaveCriticalSection(FLock);
end;
{$endif}
Result := FStandardEncodings[seAnsi];
end;
function TEncoding.GetAnsiBytes(const S: string): TBytes;
begin
if S='' then
Result := nil
else
Result := GetAnsiBytes(S, 1, Length(S));
end;
function TEncoding.GetAnsiBytes(const S: string; CharIndex, CharCount: Integer
): TBytes;
begin
Result := GetAnsiBytes(Pointer(@S[CharIndex]), CharCount);
end;
function TEncoding.GetAnsiString(const Bytes: TBytes): string;
begin
if Length(Bytes)=0 then
Result := ''
else
Result := GetAnsiString(Bytes, 0, Length(Bytes));
end;
function TEncoding.GetAnsiString(const Bytes: TBytes; ByteIndex,
ByteCount: Integer): string;
begin
Result := GetAnsiString(Pointer(@Bytes[ByteIndex]), ByteCount);
SetCodePage(RawByteString(Result), DefaultSystemCodePage, False);
end;
class function TEncoding.GetASCII: TEncoding;
begin
{$ifdef FPC_HAS_FEATURE_THREADING}
EnterCriticalSection(FLock);
try
{$endif}
if not Assigned(FStandardEncodings[seAscii]) then
FStandardEncodings[seAscii] := TMBCSEncoding.Create(CP_ASCII);
{$ifdef FPC_HAS_FEATURE_THREADING}
finally
LeaveCriticalSection(FLock);
end;
{$endif}
Result := FStandardEncodings[seAscii];
end;
class function TEncoding.GetBigEndianUnicode: TEncoding;
begin
{$ifdef FPC_HAS_FEATURE_THREADING}
EnterCriticalSection(FLock);
try
{$endif}
if not Assigned(FStandardEncodings[seBigEndianUnicode]) then
FStandardEncodings[seBigEndianUnicode] := TBigEndianUnicodeEncoding.Create;
{$ifdef FPC_HAS_FEATURE_THREADING}
finally
LeaveCriticalSection(FLock);
end;
{$endif}
Result := FStandardEncodings[seBigEndianUnicode];
end;
class function TEncoding.GetDefault: TEncoding;
begin
Result := GetSystemEncoding;
end;
class function TEncoding.GetSystemEncoding: TEncoding;
var
I: Integer;
begin
{$ifdef FPC_HAS_FEATURE_THREADING}
EnterCriticalSection(FLock);
try
{$endif}
for I := Low(FSystemEncodings) to High(FSystemEncodings) do
begin
if FSystemEncodings[I].CodePage=DefaultSystemCodePage then
begin
Result := FSystemEncodings[I];
if I<>Low(FSystemEncodings) then // exchange with first position to find it faster the next time
begin
FSystemEncodings[I] := FSystemEncodings[Low(FSystemEncodings)];
FSystemEncodings[Low(FSystemEncodings)] := Result;
end;
Exit;
end;
end;
// not found - create new encoding at first position
Result := TMBCSEncoding.Create(DefaultSystemCodePage);
SetLength(FSystemEncodings, Length(FSystemEncodings)+1);
if High(FSystemEncodings)<>Low(FSystemEncodings) then
FSystemEncodings[High(FSystemEncodings)] := FSystemEncodings[Low(FSystemEncodings)];
FSystemEncodings[Low(FSystemEncodings)] := Result;
{$ifdef FPC_HAS_FEATURE_THREADING}
finally
LeaveCriticalSection(FLock);
end;
{$endif}
end;
class function TEncoding.GetUnicode: TEncoding;
begin
{$ifdef FPC_HAS_FEATURE_THREADING}
EnterCriticalSection(FLock);
try
{$endif}
if not Assigned(FStandardEncodings[seUnicode]) then
FStandardEncodings[seUnicode] := TUnicodeEncoding.Create;
{$ifdef FPC_HAS_FEATURE_THREADING}
finally
LeaveCriticalSection(FLock);
end;
{$endif}
Result := FStandardEncodings[seUnicode];
end;
class function TEncoding.GetUTF7: TEncoding;
begin
{$ifdef FPC_HAS_FEATURE_THREADING}
EnterCriticalSection(FLock);
try
{$endif}
if not Assigned(FStandardEncodings[seUTF7]) then
FStandardEncodings[seUTF7] := TUTF7Encoding.Create;
{$ifdef FPC_HAS_FEATURE_THREADING}
finally
LeaveCriticalSection(FLock);
end;
{$endif}
Result := FStandardEncodings[seUTF7];
end;
class function TEncoding.GetUTF8: TEncoding;
begin
{$ifdef FPC_HAS_FEATURE_THREADING}
EnterCriticalSection(FLock);
try
{$endif}
if not Assigned(FStandardEncodings[seUTF8]) then
FStandardEncodings[seUTF8] := TUTF8Encoding.Create;
{$ifdef FPC_HAS_FEATURE_THREADING}
finally
LeaveCriticalSection(FLock);
end;
{$endif}
Result := FStandardEncodings[seUTF8];
end;
class procedure TEncoding.FreeEncodings;
var
E: TStandardEncoding;
I: Integer;
begin
{$ifdef FPC_HAS_FEATURE_THREADING}
EnterCriticalSection(FLock);
try
{$endif}
for E := Low(FStandardEncodings) to High(FStandardEncodings) do
FreeAndNil(FStandardEncodings[E]);
for I := Low(FSystemEncodings) to High(FSystemEncodings) do
FSystemEncodings[I].Free;
SetLength(FSystemEncodings, 0);
{$ifdef FPC_HAS_FEATURE_THREADING}
finally
LeaveCriticalSection(FLock);
end;
{$endif}
end;
class constructor TEncoding.Create;
var
E: TStandardEncoding;
begin
for E := Low(FStandardEncodings) to High(FStandardEncodings) do
FStandardEncodings[E] := nil;
{$ifdef FPC_HAS_FEATURE_THREADING}
InitCriticalSection(FLock);
{$endif}
end;
class destructor TEncoding.Destroy;
begin
FreeEncodings;
{$ifdef FPC_HAS_FEATURE_THREADING}
DoneCriticalSection(FLock);
{$endif}
end;
function TEncoding.Clone: TEncoding;
begin
Result := nil;
end;
class function TEncoding.Convert(Source, Destination: TEncoding;
const Bytes: TBytes): TBytes;
begin
Result := Destination.GetBytes(Source.GetChars(Bytes));
end;
class function TEncoding.Convert(Source, Destination: TEncoding;
const Bytes: TBytes; StartIndex, Count: Integer): TBytes;
begin
Result := Destination.GetBytes(Source.GetChars(Bytes, StartIndex, Count));
end;
class function TEncoding.IsStandardEncoding(AEncoding: TEncoding): Boolean;
var
Encoding: TEncoding;
begin
if Assigned(AEncoding) then
begin
for Encoding in FStandardEncodings do
if Encoding = AEncoding then
Exit(True);
for Encoding in FSystemEncodings do
if Encoding = AEncoding then
Exit(True);
end;
Result := False;
end;
class function TEncoding.GetBufferEncoding(const Buffer: TBytes; var AEncoding: TEncoding): Integer;
begin
Result := GetBufferEncoding(Buffer, AEncoding, Default);
end;
class function TEncoding.GetBufferEncoding(const Buffer: TBytes;
var AEncoding: TEncoding; ADefaultEncoding: TEncoding): Integer;
function CheckEncoding(AEncoding: TEncoding; out ByteCount: Integer): Boolean;
var
Preamble: TBytes;
begin
Preamble := AEncoding.GetPreamble;
ByteCount := Length(Preamble);
Result := (Length(Buffer) >= ByteCount) and (ByteCount > 0);
if Result then
Result := CompareMem(@Preamble[0], @Buffer[0], ByteCount);
end;
begin
if Assigned(AEncoding) then
begin
if not CheckEncoding(AEncoding, Result) then
Result := 0;
end
else
if CheckEncoding(Unicode, Result) then
AEncoding := Unicode
else
if CheckEncoding(BigEndianUnicode, Result) then
AEncoding := BigEndianUnicode
else
if CheckEncoding(UTF8, Result) then
AEncoding := UTF8
else
begin
AEncoding := ADefaultEncoding;
Result := 0;
end;
end;
function TEncoding.GetByteCount(const Chars: TUnicodeCharArray): Integer;
begin
if Length(Chars)=0 then
Result := 0
else
Result := GetByteCount(Chars, 0, Length(Chars));
end;
function TEncoding.GetByteCount(const Chars: TUnicodeCharArray; CharIndex,
CharCount: Integer): Integer;
begin
if (CharCount < 0) or (Length(Chars) < CharCount + CharIndex) then
raise EEncodingError.CreateFmt(SInvalidCount, [CharCount]);
if (CharIndex < 0) then
raise EEncodingError.CreateFmt(SCharacterIndexOutOfBounds, [CharIndex]);
Result := GetByteCount(@Chars[CharIndex], CharCount);
end;
function TEncoding.GetByteCount(const S: UnicodeString): Integer;
begin
if S='' then
Result := 0
else
Result := GetByteCount(PUnicodeChar(S), Length(S));
end;
function TEncoding.GetByteCount(const S: UnicodeString; CharIndex, CharCount: Integer): Integer;
begin
if (CharCount < 0) or (Length(S) < CharCount + CharIndex) then
raise EEncodingError.CreateFmt(SInvalidCount, [CharCount]);
if (CharIndex < 1) then
raise EEncodingError.CreateFmt(SCharacterIndexOutOfBounds, [CharIndex]);
Result := GetByteCount(@S[CharIndex], CharCount);
end;
function TEncoding.GetBytes(const Chars: TUnicodeCharArray): TBytes;
begin
SetLength(Result, GetByteCount(Chars));
if Length(Result)>0 then
GetBytes(@Chars[0], Length(Chars), @Result[0], Length(Result));
end;
function TEncoding.GetBytes(const Chars: TUnicodeCharArray; CharIndex,
CharCount: Integer): TBytes;
begin
if (CharCount < 0) or (Length(Chars) < CharCount + CharIndex) then
raise EEncodingError.CreateFmt(SInvalidCount, [CharCount]);
if (CharIndex < 0) then
raise EEncodingError.CreateFmt(SCharacterIndexOutOfBounds, [CharIndex]);
SetLength(Result, GetByteCount(Chars, CharIndex, CharCount));
GetBytes(@Chars[CharIndex], CharCount, @Result[0], Length(Result));
end;
function TEncoding.GetBytes(const Chars: TUnicodeCharArray; CharIndex,
CharCount: Integer; const Bytes: TBytes; ByteIndex: Integer): Integer;
var
ByteLen: Integer;
begin
ByteLen := Length(Bytes);
if (ByteLen = 0) and (CharCount > 0) then
raise EEncodingError.Create(SInvalidDestinationArray);
if (ByteIndex < 0) or (ByteLen < ByteIndex) then
raise EEncodingError.CreateFmt(SInvalidDestinationIndex, [ByteIndex]);
if (CharCount < 0) or (Length(Chars) < CharCount + CharIndex) then
raise EEncodingError.CreateFmt(SInvalidCount, [CharCount]);
if (CharIndex < 0) then
raise EEncodingError.CreateFmt(SCharacterIndexOutOfBounds, [CharIndex]);
Result := GetBytes(@Chars[CharIndex], CharCount, @Bytes[ByteIndex], ByteLen - ByteIndex);
end;
function TEncoding.GetBytes(const S: UnicodeString): TBytes;
begin
SetLength(Result, GetByteCount(S));
if Length(Result)>0 then
GetBytes(@S[1], Length(S), @Result[0], Length(Result));
end;
function TEncoding.GetBytes(const S: UnicodeString; CharIndex, CharCount: Integer;
const Bytes: TBytes; ByteIndex: Integer): Integer;
var
ByteLen: Integer;
begin
ByteLen := Length(Bytes);
if (ByteLen = 0) and (CharCount > 0) then
raise EEncodingError.Create(SInvalidDestinationArray);
if (ByteIndex < 0) or (ByteLen < ByteIndex) then
raise EEncodingError.CreateFmt(SInvalidDestinationIndex, [ByteIndex]);
if (CharCount < 0) or (Length(S) < CharCount + CharIndex) then
raise EEncodingError.CreateFmt(SInvalidCount, [CharCount]);
if (CharIndex < 1) then
raise EEncodingError.CreateFmt(SCharacterIndexOutOfBounds, [CharIndex]);
Result := GetBytes(@S[CharIndex], CharCount, @Bytes[ByteIndex], ByteLen - ByteIndex);
end;
function TEncoding.GetCharCount(const Bytes: TBytes): Integer;
begin
if Length(Bytes)=0 then
Result := 0
else
Result := GetCharCount(@Bytes[0], Length(Bytes));
end;
function TEncoding.GetCharCount(const Bytes: TBytes; ByteIndex,
ByteCount: Integer): Integer;
begin
if (ByteIndex < 0) or (Length(Bytes) < ByteIndex) then
raise EEncodingError.CreateFmt(SInvalidDestinationIndex, [ByteIndex]);
Result := GetCharCount(@Bytes[ByteIndex], ByteCount);
end;
function TEncoding.GetChars(const Bytes: TBytes): TUnicodeCharArray;
begin
SetLength(Result, GetCharCount(Bytes));
if Length(Result)>0 then
GetChars(@Bytes[0], Length(Bytes), @Result[0], Length(Result));
end;
function TEncoding.GetChars(const Bytes: TBytes; ByteIndex, ByteCount: Integer): TUnicodeCharArray;
begin
if (ByteIndex < 0) or (Length(Bytes) < ByteIndex) then
raise EEncodingError.CreateFmt(SInvalidDestinationIndex, [ByteIndex]);
SetLength(Result, GetCharCount(Bytes, ByteIndex, ByteCount));
GetChars(@Bytes[ByteIndex], ByteCount, @Result[0], Length(Result));
end;
function TEncoding.GetChars(const Bytes: TBytes; ByteIndex, ByteCount: Integer;
const Chars: TUnicodeCharArray; CharIndex: Integer): Integer;
var
CharLen: Integer;
begin
if (ByteIndex < 0) or (Length(Bytes) <= ByteIndex) then
raise EEncodingError.CreateFmt(SInvalidDestinationIndex, [ByteIndex]);
CharLen := Length(Chars);
if (CharIndex < 0) or (CharLen <= CharIndex) then
raise EEncodingError.CreateFmt(SCharacterIndexOutOfBounds, [CharIndex]);
Result := GetChars(@Bytes[ByteIndex], ByteCount, @Chars[CharIndex], CharLen - CharIndex);
end;
class function TEncoding.GetEncoding(CodePage: Integer): TEncoding;
begin
case CodePage of
CP_UTF16: Result := TUnicodeEncoding.Create;
CP_UTF16BE: Result := TBigEndianUnicodeEncoding.Create;
CP_UTF7: Result := TUTF7Encoding.Create;
CP_UTF8: Result := TUTF8Encoding.Create;
else
Result := TMBCSEncoding.Create(CodePage);
end;
end;
class function TEncoding.GetEncoding(const EncodingName: UnicodeString): TEncoding;
var
ACodePage: TSystemCodePage;
begin
ACodePage := CodePageNameToCodePage(AnsiString(EncodingName));
if ACodePage = $FFFF then
raise EEncodingError.CreateFmt(SNotValidCodePageName, [EncodingName]);
Result := TMBCSEncoding.Create(ACodePage);
end;
function TEncoding.GetString(const Bytes: TBytes): UnicodeString;
var
Chars: TUnicodeCharArray;
begin
if Length(Bytes)=0 then
Result := ''
else
begin
Chars := GetChars(Bytes);
SetString(Result, PUnicodeChar(Chars), Length(Chars));
end;
end;
function TEncoding.GetString(const Bytes: TBytes; ByteIndex, ByteCount: Integer): UnicodeString;
var
Chars: TUnicodeCharArray;
begin
Chars := GetChars(Bytes, ByteIndex, ByteCount);
SetString(Result, PUnicodeChar(Chars), Length(Chars));
end;
{ TMBCSEncoding }
function TMBCSEncoding.GetByteCount(Chars: PUnicodeChar; CharCount: Integer): Integer;
var
S: RawByteString;
begin
widestringmanager.Unicode2AnsiMoveProc(Chars, S, CodePage, CharCount);
Result := Length(S);
end;
function TMBCSEncoding.GetBytes(Chars: PUnicodeChar; CharCount: Integer; Bytes: PByte;
ByteCount: Integer): Integer;
var
S: RawByteString;
begin
widestringmanager.Unicode2AnsiMoveProc(Chars, S, CodePage, CharCount);
Result := Length(S);
if ByteCount < Result then
Result := ByteCount;
if Result > 0 then
Move(S[1], Bytes[0], Result);
end;
function TMBCSEncoding.GetCharCount(Bytes: PByte; ByteCount: Integer): Integer;
var
U: UnicodeString;
begin
widestringmanager.Ansi2UnicodeMoveProc(PChar(Bytes), CodePage, U, ByteCount);
Result := Length(U);
end;
function TMBCSEncoding.GetChars(Bytes: PByte; ByteCount: Integer; Chars: PUnicodeChar;
CharCount: Integer): Integer;
var
U: UnicodeString;
begin
widestringmanager.Ansi2UnicodeMoveProc(PChar(Bytes), CodePage, U, ByteCount);
Result := Length(U);
if CharCount < Result then
Result := CharCount;
if Result > 0 then
Move(U[1], Chars[0], Result * SizeOf(UnicodeChar));
end;
function TMBCSEncoding.GetCodePage: Cardinal;
begin
Result := FCodePage;
end;
function TMBCSEncoding.GetEncodingName: UnicodeString;
begin
Result := UnicodeString(CodePageToCodePageName(CodePage));
end;
constructor TMBCSEncoding.Create;
begin
Create(DefaultSystemCodePage, 0, 0);
end;
constructor TMBCSEncoding.Create(ACodePage: Integer);
begin
Create(ACodePage, 0, 0);
end;
constructor TMBCSEncoding.Create(ACodePage, MBToWCharFlags,
WCharToMBFlags: Integer);
begin
FCodePage := ACodePage;
FMBToWCharFlags := MBToWCharFlags;
FWCharToMBFlags := WCharToMBFlags;
case ACodePage of
CP_UTF7, CP_UTF8, CP_UTF16, CP_UTF16BE: FIsSingleByte := False;
else
FIsSingleByte := True;
end;
end;
function TMBCSEncoding.Clone: TEncoding;
begin
Result := TMBCSEncoding.Create(FCodePage, FMBToWCharFlags, FWCharToMBFlags);
end;
function TMBCSEncoding.GetAnsiBytes(Chars: PChar; CharCount: Integer): TBytes;
var
S: RawByteString;
begin
SetString(S, Chars, CharCount);
SetCodePage(S, DefaultSystemCodePage, False);
SetCodePage(S, GetCodePage, True);
SetLength(Result, Length(S));
if Length(S)>0 then
Move(S[1], Result[0], Length(S));
end;
function TMBCSEncoding.GetAnsiString(Bytes: PByte; ByteCount: Integer): string;
begin
SetString(Result, Pointer(Bytes), ByteCount);
SetCodePage(RawByteString(Result), GetCodePage, False);
SetCodePage(RawByteString(Result), DefaultSystemCodePage, True);
end;
function TMBCSEncoding.GetMaxByteCount(CharCount: Integer): Integer;
begin
Result := CharCount;
end;
function TMBCSEncoding.GetMaxCharCount(ByteCount: Integer): Integer;
begin
Result := ByteCount;
end;
function TMBCSEncoding.GetPreamble: TBytes;
begin
case CodePage of
CP_UTF8:
begin
SetLength(Result, 3);
Result[0] := $EF;
Result[1] := $BB;
Result[2] := $BF;
end;
CP_UTF16:
begin
SetLength(Result, 2);
Result[0] := $FF;
Result[1] := $FE;
end;
CP_UTF16BE:
begin
SetLength(Result, 2);
Result[0] := $FE;
Result[1] := $FF;
end;
else
Result := nil;
end;
end;
{ TUTF7Encoding }
constructor TUTF7Encoding.Create;
begin
inherited Create(CP_UTF7);
FIsSingleByte := False;
end;
function TUTF7Encoding.Clone: TEncoding;
begin
Result := TUTF7Encoding.Create;
end;
function TUTF7Encoding.GetMaxByteCount(CharCount: Integer): Integer;
begin
Result := CharCount * 3 + 2;
end;
function TUTF7Encoding.GetMaxCharCount(ByteCount: Integer): Integer;
begin
Result := ByteCount;
end;
{ TUTF8Encoding }
constructor TUTF8Encoding.Create;
begin
inherited Create(CP_UTF8);
FIsSingleByte := False;
end;
function TUTF8Encoding.Clone: TEncoding;
begin
Result := TUTF8Encoding.Create;
end;
function TUTF8Encoding.GetMaxByteCount(CharCount: Integer): Integer;
begin
Result := CharCount * 3;
end;
function TUTF8Encoding.GetMaxCharCount(ByteCount: Integer): Integer;
begin
Result := ByteCount;
end;
function TUTF8Encoding.GetPreamble: TBytes;
begin
SetLength(Result, 3);
Result[0] := $EF;
Result[1] := $BB;
Result[2] := $BF;
end;
{ TUnicodeEncoding }
function TUnicodeEncoding.GetByteCount(Chars: PUnicodeChar; CharCount: Integer): Integer;
begin
Result := CharCount * SizeOf(UnicodeChar);
end;
function TUnicodeEncoding.GetBytes(Chars: PUnicodeChar; CharCount: Integer;
Bytes: PByte; ByteCount: Integer): Integer;
begin
Result := CharCount * SizeOf(UnicodeChar);
if ByteCount < Result then
Result := ByteCount;
if Result > 0 then
Move(Chars[0], Bytes[0], Result);
end;
function TUnicodeEncoding.GetCharCount(Bytes: PByte; ByteCount: Integer): Integer;
begin
Result := ByteCount div SizeOf(UnicodeChar);
end;
function TUnicodeEncoding.GetChars(Bytes: PByte; ByteCount: Integer;
Chars: PUnicodeChar; CharCount: Integer): Integer;
begin
Result := ByteCount div 2;
if CharCount < Result then
Result := CharCount;
Move(Bytes[0], Chars[0], Result * SizeOf(UnicodeChar));
end;
function TUnicodeEncoding.GetCodePage: Cardinal;
begin
Result := CP_UTF16;
end;
function TUnicodeEncoding.GetEncodingName: UnicodeString;
begin
Result := UnicodeString(CodePageToCodePageName(CodePage));
end;
constructor TUnicodeEncoding.Create;
begin
inherited Create;
FIsSingleByte := False;
FMaxCharSize := SizeOf(UnicodeChar);
end;
function TUnicodeEncoding.Clone: TEncoding;
begin
Result := TUnicodeEncoding.Create;
end;
function TUnicodeEncoding.GetAnsiBytes(Chars: PChar; CharCount: Integer
): TBytes;
var
U: UnicodeString;
begin
widestringmanager.Ansi2UnicodeMoveProc(Chars, DefaultSystemCodePage, U, CharCount);
SetLength(Result, Length(U)*SizeOf(UnicodeChar));
if Length(Result)>0 then
Move(U[1], Result[0], Length(Result));
end;
function TUnicodeEncoding.GetAnsiString(Bytes: PByte; ByteCount: Integer
): string;
begin
widestringmanager.Unicode2AnsiMoveProc(PUnicodeChar(Bytes), RawByteString(Result), DefaultSystemCodePage, ByteCount div SizeOf(UnicodeChar));
end;
function TUnicodeEncoding.GetMaxByteCount(CharCount: Integer): Integer;
begin
Result := CharCount * SizeOf(UnicodeChar);
end;
function TUnicodeEncoding.GetMaxCharCount(ByteCount: Integer): Integer;
begin
Result := ByteCount div SizeOf(UnicodeChar);
end;
function TUnicodeEncoding.GetPreamble: TBytes;
begin
SetLength(Result, 2);
Result[0] := $FF;
Result[1] := $FE;
end;
{ TBigEndianUnicodeEncoding }
function TBigEndianUnicodeEncoding.GetBytes(Chars: PUnicodeChar; CharCount: Integer;
Bytes: PByte; ByteCount: Integer): Integer;
var
LastByte: PByte;
begin
Result := CharCount * SizeOf(UnicodeChar);
if ByteCount < Result then
Result := ByteCount;
LastByte := @Bytes[Result];
while Bytes < LastByte do
begin
Bytes^ := Hi(Word(Chars^));
inc(Bytes);
if Bytes < LastByte then
Bytes^ := Lo(Word(Chars^));
inc(Bytes);
inc(Chars);
end;
end;
function TBigEndianUnicodeEncoding.GetChars(Bytes: PByte; ByteCount: Integer;
Chars: PUnicodeChar; CharCount: Integer): Integer;
var
LastChar: PUnicodeChar;
begin
Result := ByteCount div SizeOf(UnicodeChar);
if CharCount < Result then
Result := CharCount;
LastChar := @Chars[Result];
while Chars < LastChar do
begin
Chars^ := UnicodeChar(Bytes[1] + Bytes[0] shl 8);
inc(Bytes, SizeOf(UnicodeChar));
inc(Chars);
end;
end;
function TBigEndianUnicodeEncoding.GetCodePage: Cardinal;
begin
Result := CP_UTF16BE;
end;
function TBigEndianUnicodeEncoding.GetEncodingName: UnicodeString;
begin
Result := UnicodeString(CodePageToCodePageName(CodePage));
end;
function TBigEndianUnicodeEncoding.Clone: TEncoding;
begin
Result := TBigEndianUnicodeEncoding.Create;
end;
function TBigEndianUnicodeEncoding.GetAnsiBytes(Chars: PChar; CharCount: Integer
): TBytes;
begin
Result := TEncoding.Unicode.GetAnsiBytes(Chars, CharCount);
Swap(Result);
end;
function TBigEndianUnicodeEncoding.GetAnsiString(Bytes: PByte;
ByteCount: Integer): string;
var
B: TBytes;
begin
if ByteCount=0 then
Exit('');
SetLength(B, ByteCount);
Move(Bytes^, B[0], ByteCount);
Swap(B);
Result := TEncoding.Unicode.GetAnsiString(PByte(@B[0]), ByteCount);
end;
function TBigEndianUnicodeEncoding.GetPreamble: TBytes;
begin
SetLength(Result, 2);
Result[0] := $FE;
Result[1] := $FF;
end;
procedure TBigEndianUnicodeEncoding.Swap(var B: TBytes);
var
LastB, I: Integer;
C: Byte;
begin
LastB := Length(B)-1;
I := 0;
while I < LastB do
begin
C := B[I];
B[I] := B[I+1];
B[I+1] := C;
Inc(I, 2);
end;
end;
|