Repository navigation
Expand file tree
/
Copy pathPythonEncoding.cs
More file actions
860 lines (693 loc) · 36.3 KB
/
Copy pathPythonEncoding.cs
File metadata and controls
860 lines (693 loc) · 36.3 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
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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the Apache 2.0 License.
// See the LICENSE file in the project root for more information.
using IronPython.Runtime.Operations;
using System;
using System.Collections.Generic;
using System.Text;
namespace IronPython.Runtime {
#if FEATURE_ENCODING
/// <summary>
/// Wrapper class for any well-behaved <see cref="System.Text.Encoding"/> (like any encodings provided by .NET)
/// that allows for encoding/decoding fallbacks operating on byte level.
/// </summary>
/// <remarks>
/// Python encoding/decoding fallbacks (called "error handlers" in Python documentation) can deliver
/// fallback values as strings as well as bytes. .NET fallback mechanism only allows for characters as fallbacks,
/// and only those characters that are deemed valid by a given encoding. So, for instance, lone surrogate escapes,
/// produced by Python's 'surrogateescape' error handler, are not allowed in the standard .NET fallback protocol.
///
/// This class extends the standard .NET fallback protocol to allow fallbacks to provide values normally
/// not allowed by .NET but allowed by Python. It also allows the fallbacks to provide fallback values
/// as a sequence of bytes.
///
/// Note: Currently, it is not possible to set the fallbacks through assignment to
/// <see cref="EncoderFallback"/> or <see cref="DecoderFallback"/>; the fallbacks have to be provided
/// to the constructor. Also, the fallbacks have to be of type <see cref="PythonEncoderFallback"/>
/// and <see cref="PythonDecoderFallback"/>, which implement the extended fallback protocol.
/// </remarks>
internal class PythonEncoding : Encoding {
// The following two must be different from each other and be pass-through characters for UTF-7
private const char Pass1Marker = '?';
private const char Pass2Marker = '-';
public int CharacterWidth { get; }
public bool IsBigEndian { get; } // meaningful only for wide-char encodings
private Encoding Pass1Encoding { get; }
private Encoding Pass2Encoding { get; }
private Decoder _residentDecoder;
private Encoder _residentEncoder;
public PythonEncoding(Encoding encoding, PythonEncoderFallback encoderFallback, PythonDecoderFallback decoderFallback)
//: base(0, encoderFallback, decoderFallback) // unfortunately, this constructor is internal until .NET Framework 4.6
{
if (encoding == null) throw new ArgumentNullException(nameof(encoding));
if (encoderFallback == null) throw new ArgumentNullException(nameof(encoderFallback));
if (decoderFallback == null) throw new ArgumentNullException(nameof(decoderFallback));
byte[] markerBytes = encoding.GetBytes(new[] { Pass1Marker });
CharacterWidth = markerBytes.Length;
IsBigEndian = markerBytes[0] == 0;
// set up pass 1 Encoding, using provided fallback instances
encoderFallback.Encoding = decoderFallback.Encoding = this;
encoderFallback.IsPass1 = decoderFallback.IsPass1 = true;
Pass1Encoding = (Encoding)encoding.Clone();
Pass1Encoding.EncoderFallback = encoderFallback;
Pass1Encoding.DecoderFallback = decoderFallback;
// set up pass 2 Encoding, using clones of provided fallback instances
encoderFallback = (PythonEncoderFallback)encoderFallback.Clone();
decoderFallback = (PythonDecoderFallback)decoderFallback.Clone();
encoderFallback.IsPass1 = decoderFallback.IsPass1 = false;
Pass2Encoding = (Encoding)encoding.Clone();
Pass2Encoding.EncoderFallback = encoderFallback;
Pass2Encoding.DecoderFallback = decoderFallback;
}
public override int GetByteCount(char[] chars, int index, int count) {
if (_residentEncoder == null) {
_residentEncoder = GetEncoder();
} else {
_residentEncoder.Reset();
}
return _residentEncoder.GetByteCount(chars, index, count, flush: true);
}
public override int GetBytes(char[] chars, int charIndex, int charCount, byte[] bytes, int byteIndex) {
if (_residentEncoder == null) {
_residentEncoder = GetEncoder();
} else {
_residentEncoder.Reset();
}
return _residentEncoder.GetBytes(chars, charIndex, charCount, bytes, byteIndex, flush: true);
}
public override int GetCharCount(byte[] bytes, int index, int count) {
if (_residentDecoder == null) {
_residentDecoder = GetDecoder();
} else {
_residentDecoder.Reset();
}
return _residentDecoder.GetCharCount(bytes, index, count, flush: true);
}
public override int GetChars(byte[] bytes, int byteIndex, int byteCount, char[] chars, int charIndex) {
if (_residentDecoder == null) {
_residentDecoder = GetDecoder();
} else {
_residentDecoder.Reset();
}
return _residentDecoder.GetChars(bytes, byteIndex, byteCount, chars, charIndex, flush: true);
}
public override int GetMaxByteCount(int charCount)
=> Pass1Encoding.GetMaxByteCount(charCount);
public override int GetMaxCharCount(int byteCount)
=> Pass1Encoding.GetMaxCharCount(byteCount);
public override Encoder GetEncoder()
=> new PythonEncoder(this);
public override Decoder GetDecoder()
=> new PythonDecoder(this);
public override int CodePage => Pass1Encoding.CodePage;
public override int WindowsCodePage => Pass1Encoding.WindowsCodePage;
public override string EncodingName => Pass1Encoding.EncodingName;
public override string HeaderName => Pass1Encoding.HeaderName;
public override string BodyName => Pass1Encoding.BodyName;
public override string WebName => Pass1Encoding.WebName;
public override bool IsBrowserDisplay => false;
public override bool IsBrowserSave => false;
public override bool IsMailNewsDisplay => false;
public override bool IsMailNewsSave => false;
public override bool IsSingleByte => Pass1Encoding.IsSingleByte;
public override int GetHashCode() => Pass1Encoding.GetHashCode();
public override byte[] GetPreamble() => Pass1Encoding.GetPreamble();
public override bool IsAlwaysNormalized(NormalizationForm form) => false;
public static bool HasBugCorefx29898 {
get {
if (_hasBugCorefx29898 == null) {
try {
var codec = (Encoding)new UTF8Encoding(false, throwOnInvalidBytes: true);
codec.GetCharCount(new byte[] { 255 });
_hasBugCorefx29898 = false;
} catch (DecoderFallbackException ex) {
_hasBugCorefx29898 = (ex.Index < 0);
}
}
return (bool)_hasBugCorefx29898;
}
}
private static bool? _hasBugCorefx29898;
public class PythonEncoder : Encoder {
private readonly PythonEncoding _parentEncoding;
private readonly Encoder _pass1encoder;
private Encoder _pass2encoder;
private readonly int _characterWidth;
public PythonEncoder(PythonEncoding parentEncoding) {
_parentEncoding = parentEncoding;
_characterWidth = _parentEncoding.CharacterWidth;
_pass1encoder = _parentEncoding.Pass1Encoding.GetEncoder();
}
public override int GetByteCount(char[] chars, int index, int count, bool flush) {
int numBytes;
var fbuf1 = _pass1encoder.FallbackBuffer as PythonEncoderFallbackBuffer;
if (fbuf1 != null) fbuf1.ByteCountingMode = true;
try {
numBytes = _pass1encoder.GetByteCount(chars, index, count, flush);
fbuf1?.ThrowIfNotEmpty(count, flush);
} finally {
if (fbuf1 != null) fbuf1.ByteCountingMode = false;
}
return numBytes;
}
public override int GetBytes(char[] chars, int charIndex, int charCount, byte[] bytes, int byteIndex, bool flush) {
var fbuf1 = _pass1encoder.FallbackBuffer as PythonEncoderFallbackBuffer;
var fbuf2 = _pass2encoder?.FallbackBuffer as PythonEncoderFallbackBuffer;
int? fbkIdxStart = fbuf1?.FallbackByteCount;
int written = _pass1encoder.GetBytes(chars, charIndex, charCount, bytes, byteIndex, flush);
// If the final increment and there were no fallback bytes, the job is done
if (fbuf1 == null || flush && fbuf1.FallbackByteCount == fbkIdxStart && fbuf1.IsEmpty && (fbuf2?.IsEmpty ?? true)) {
return written;
}
// Lazy creation of _pass2encoder
if (_pass2encoder == null) {
_pass2encoder = _parentEncoding.Pass2Encoding.GetEncoder();
fbuf2 = (PythonEncoderFallbackBuffer)_pass2encoder.FallbackBuffer;
}
// Restore original fallback bytes
var bytes2 = new byte[written];
_pass2encoder.GetBytes(chars, charIndex, charCount, bytes2, 0, flush);
for (int i = 0, j = byteIndex; i < written; i++, j++) {
if (bytes[j] != bytes2[i]) {
int ofs = (j / _characterWidth) * _characterWidth;
for (int p = 0; p < _characterWidth; p++) {
bytes[ofs++] = fbuf2.GetFallbackByte();
}
int skip = ofs - j - 1;
i += skip;
j += skip;
}
}
// Check if all fallback bytes are restored properly
fbuf1.ThrowIfNotEmpty(charCount, flush);
fbuf2.ThrowIfNotEmpty(charCount, flush);
return written;
}
public override void Reset() {
_pass1encoder.Reset();
_pass2encoder?.Reset();
}
}
public abstract class PythonEncoderFallback : EncoderFallback, ICloneable {
public PythonEncoding Encoding { get; set; }
public bool IsPass1 { get; set; }
public virtual object Clone() => MemberwiseClone();
}
protected abstract class PythonEncoderFallbackBuffer : EncoderFallbackBuffer {
private readonly char _marker;
private readonly Queue<byte> _fallbackBytes;
private struct ByteCounter {
private int _bcmCounter; // used when only counting bytes
private int _encCounter; // used during actual encoding
public bool ByteCountingMode { get; set; }
public int Value => ByteCountingMode ? _bcmCounter : _encCounter;
public void AddNumBytes(int numBytes) {
if (ByteCountingMode) {
_bcmCounter += numBytes;
} else {
_encCounter += numBytes;
}
}
public void Reset() => _bcmCounter = _encCounter = 0;
}
private ByteCounter _byteCnt;
private int _fbkCnt;
// for error reporting
private char _lastCharUnknown;
private int _lastIndexUnknown = -1;
public PythonEncoderFallbackBuffer(bool isPass1, PythonEncoding encoding) {
_marker = isPass1 ? Pass1Marker : Pass2Marker;
_fallbackBytes = isPass1 ? null : new Queue<byte>();
this.EncodingCharWidth = encoding.CharacterWidth;
this.CodePage = encoding.CodePage;
}
protected int EncodingCharWidth { get; }
protected int CodePage { get; }
public abstract byte[] GetFallbackBytes(char charUnknown, int index);
public override bool Fallback(char charUnknown, int index) {
// The design limitation for wide-char encodings is that
// fallback bytes must be char-aligned.
if (_byteCnt.Value % EncodingCharWidth != 0) {
// bytes are not char-aligned, the fallback chars must be consecutive
if (index != _lastIndexUnknown + 1) {
throw PythonOps.UnicodeEncodeError($"incomplete input sequence", _lastCharUnknown, _lastIndexUnknown);
}
}
byte[] newFallbackBytes = GetFallbackBytes(charUnknown, index);
if (!ByteCountingMode) {
if (_fallbackBytes != null) {
foreach (byte b in newFallbackBytes) {
_fallbackBytes.Enqueue(b);
}
}
_fbkCnt += newFallbackBytes.Length;
}
_byteCnt.AddNumBytes(newFallbackBytes.Length);
_lastCharUnknown = charUnknown;
_lastIndexUnknown = index;
return _byteCnt.Value >= EncodingCharWidth;
}
public override bool Fallback(char charUnknownHigh, char charUnknownLow, int index)
=> Fallback(charUnknownHigh, index) | Fallback(charUnknownLow, index + 1);
public override int Remaining => _byteCnt.Value / EncodingCharWidth;
public override char GetNextChar() {
if (_byteCnt.Value < EncodingCharWidth) return char.MinValue;
_byteCnt.AddNumBytes(-EncodingCharWidth);
return _marker;
}
public override bool MovePrevious() {
if (_byteCnt.Value > 0) return false;
_byteCnt.AddNumBytes(EncodingCharWidth);
return true;
}
public byte GetFallbackByte() {
_fbkCnt--;
return _fallbackBytes.Dequeue();
}
public virtual bool IsEmpty => (_fallbackBytes?.Count ?? 0) == 0;
public int FallbackByteCount => _fbkCnt;
public virtual bool ByteCountingMode {
get { return _byteCnt.ByteCountingMode; }
set { _byteCnt.ByteCountingMode = value; }
}
public virtual void ThrowIfNotEmpty(int endIndex, bool flush) {
if (flush && !IsEmpty || _byteCnt.Value % EncodingCharWidth != 0 && endIndex != _lastIndexUnknown + 1) {
throw PythonOps.UnicodeEncodeError($"incomplete input sequence", _lastCharUnknown, _lastIndexUnknown);
}
if (ByteCountingMode) {
// This increment has successfully been counted.
// Therefore there will be no errors during translation.
_lastIndexUnknown = -1;
} else {
_lastIndexUnknown -= endIndex; // prep. for next incremental encoding step
}
}
public override void Reset() {
_fallbackBytes?.Clear();
_fbkCnt = 0;
_byteCnt.Reset();
_lastCharUnknown = '\0';
_lastIndexUnknown = -1;
}
}
private class PythonDecoder : Decoder {
private readonly PythonEncoding _parentEncoding;
private readonly Decoder _pass1decoder;
private Decoder _pass2decoder;
public PythonDecoder(PythonEncoding parentEncoding) {
_parentEncoding = parentEncoding;
_pass1decoder = _parentEncoding.Pass1Encoding.GetDecoder();
}
public override int GetCharCount(byte[] bytes, int index, int count)
=> _pass1decoder.GetCharCount(bytes, index, count, flush: true);
public override int GetCharCount(byte[] bytes, int index, int count, bool flush) {
int numChars;
var fbuf1 = _pass1decoder.FallbackBuffer as PythonDecoderFallbackBuffer;
if (fbuf1 != null) fbuf1.CharCountingMode = true;
try {
numChars = _pass1decoder.GetCharCount(bytes, index, count, flush);
fbuf1?.ThrowIfNotEmpty(count, flush);
} finally {
if (fbuf1 != null) fbuf1.CharCountingMode = false;
}
return numChars;
}
public override int GetChars(byte[] bytes, int byteIndex, int byteCount, char[] chars, int charIndex)
=> GetChars(bytes, byteIndex, byteCount, chars, charIndex, flush: true);
public override int GetChars(byte[] bytes, int byteIndex, int byteCount, char[] chars, int charIndex, bool flush) {
var fbuf1 = _pass1decoder.FallbackBuffer as PythonDecoderFallbackBuffer;
var fbuf2 = _pass2decoder?.FallbackBuffer as PythonDecoderFallbackBuffer;
int? surIdxStart = fbuf1?.FallbackCharCount;
int written = _pass1decoder.GetChars(bytes, byteIndex, byteCount, chars, charIndex, flush);
// If the final increment and there were no fallback characters, the job is done
if (fbuf1 == null || flush && fbuf1.FallbackCharCount == surIdxStart && fbuf1.IsEmpty && (fbuf2?.IsEmpty ?? true)) {
return written;
}
// Lazy creation of _pass2decoder
if (_pass2decoder == null) {
_pass2decoder = _parentEncoding.Pass2Encoding.GetDecoder();
fbuf2 = (PythonDecoderFallbackBuffer)_pass2decoder.FallbackBuffer;
}
// replace surrogate markers with actual surrogates
var chars2 = new char[written];
_pass2decoder.GetChars(bytes, byteIndex, byteCount, chars2, 0, flush);
for (int i = 0, j = charIndex; i < written; i++, j++) {
if (chars[j] != chars2[i]) {
chars[j] = fbuf2.GetFallbackChar();
}
}
// Check if all fallback chars are restored properly
fbuf1.ThrowIfNotEmpty(byteCount, flush);
fbuf2.ThrowIfNotEmpty(byteCount, flush);
return written;
}
public override void Reset() {
_pass1decoder.Reset();
_pass2decoder?.Reset();
}
}
public abstract class PythonDecoderFallback : DecoderFallback, ICloneable {
public PythonEncoding Encoding { get; set; }
public bool IsPass1 { get; set; }
public virtual object Clone() => MemberwiseClone();
}
protected abstract class PythonDecoderFallbackBuffer : DecoderFallbackBuffer {
private readonly char _marker;
private readonly Queue<char> _fallbackChars;
private int _fbkCnt;
private int _charNum;
private int _charCnt;
public PythonDecoderFallbackBuffer(bool isPass1, PythonEncoding encoding) {
_marker = isPass1 ? Pass1Marker : Pass2Marker;
_fallbackChars = isPass1 ? null : new Queue<char>();
this.EncodingCharWidth = encoding.CharacterWidth;
this.CodePage = encoding.CodePage;
}
protected int EncodingCharWidth { get; }
protected int CodePage { get; }
public abstract char[] GetFallbackChars(byte[] bytesUnknown, int index);
public override bool Fallback(byte[] bytesUnknown, int index) {
if (this.CharCountingMode && this.CodePage == 65001 && PythonEncoding.HasBugCorefx29898) { // only for UTF-8
index += bytesUnknown.Length;
}
char[] newFallbackChars = GetFallbackChars(bytesUnknown, index);
_charNum = newFallbackChars.Length;
if (_fallbackChars != null) {
for (int i = 0; i < _charNum; i++) {
_fallbackChars.Enqueue(newFallbackChars[i]);
}
}
_fbkCnt += _charNum;
_charCnt = _charNum;
return true;
}
public override int Remaining => _charCnt;
public override char GetNextChar() {
if (_charCnt <= 0) return char.MinValue;
_charCnt--;
return _marker; // unfortunately, returning the actual fallback char here might result in an exception
}
public override bool MovePrevious() {
if (_charCnt >= _charNum) return false;
_charCnt++;
return true;
}
// not called for pass1 decoding
public char GetFallbackChar() {
_fbkCnt--;
return _fallbackChars.Dequeue();
}
public virtual bool IsEmpty => (_fallbackChars?.Count ?? 0) == 0;
public int FallbackCharCount => _fbkCnt;
public virtual bool CharCountingMode { get; set; }
public virtual void ThrowIfNotEmpty(int endIndex, bool flush) {
if (flush && !IsEmpty) {
// If this exception is being thrown, the problem is with the code, not the input sequence.
// Therefore, the exception does not carry any input data.
throw new DecoderFallbackException("internal error");
}
}
public override void Reset() {
_fallbackChars?.Clear();
_fbkCnt = 0;
_charNum = _charCnt = 0;
}
}
}
internal class PythonSurrogateEscapeEncoding : PythonEncoding {
// Defined in PEP 383
private const ushort LoneSurrogateBase = 0xdc00;
public PythonSurrogateEscapeEncoding(Encoding encoding)
: base(encoding, new SurrogateEscapeEncoderFallback(), new SurrogateEscapeDecoderFallback()) { }
public class SurrogateEscapeEncoderFallback : PythonEncoderFallback {
public override int MaxCharCount => 1;
public override EncoderFallbackBuffer CreateFallbackBuffer()
=> new SurrogateEscapeEncoderFallbackBuffer(this.IsPass1, this.Encoding);
}
private class SurrogateEscapeEncoderFallbackBuffer : PythonEncoderFallbackBuffer {
public SurrogateEscapeEncoderFallbackBuffer(bool isPass1, PythonEncoding encoding)
: base(isPass1, encoding) { }
public override byte[] GetFallbackBytes(char charUnknown, int index) {
if ((charUnknown & ~0xff) != LoneSurrogateBase) {
// EncoderFallbackException(string, char, int) is not accessible here
throw PythonOps.UnicodeEncodeError(
$"'surrogateescape' error handler: value not in range(0x{LoneSurrogateBase:x4}, 0x{LoneSurrogateBase+0x100:x4})",
charUnknown,
index
);
}
return new[] { (byte)(charUnknown & 0xff) };
}
}
public class SurrogateEscapeDecoderFallback : PythonDecoderFallback {
public override int MaxCharCount => this.Encoding.CharacterWidth;
public override DecoderFallbackBuffer CreateFallbackBuffer()
=> new SurrogateEscapeDecoderFallbackBuffer(this.IsPass1, this.Encoding);
}
private class SurrogateEscapeDecoderFallbackBuffer : PythonDecoderFallbackBuffer {
public SurrogateEscapeDecoderFallbackBuffer(bool isPass1, PythonEncoding encoding)
: base(isPass1, encoding) { }
public override char[] GetFallbackChars(byte[] bytesUnknown, int index) {
int charNum = bytesUnknown.Length;
char[] fallbackChars = new char[charNum];
for (int i = 0; i < charNum; i++) {
if (this.EncodingCharWidth == 1) {
// test for value below 128
if (bytesUnknown[i] < 128u) {
throw new DecoderFallbackException(
$"values below 128 cannot be smuggled (PEP 383)",
bytesUnknown,
index
);
}
}
// no test for "else" case because all supported wide char encodings (UTF-16LE, UTF-16BE, UTF-32LE, UTF-32BE)
// will never fall back for values under 128
fallbackChars[i] = (char)(bytesUnknown[i] | LoneSurrogateBase);
}
return fallbackChars;
}
}
}
internal class PythonSurrogatePassEncoding : PythonEncoding {
private const ushort SurrogateRangeStart = 0xd800;
private const ushort SurrogateRangeEnd = 0xdfff;
private const byte Utf8LeadByte = 0b_1110_0000;
private const byte Utf8LeadBytePayload = 0b_1111;
private const byte Utf8ContByte = 0b_10_000000;
private const byte Utf8ContBytePayload = 0b_111111;
public PythonSurrogatePassEncoding(Encoding encoding)
: base(encoding, new SurrogatePassEncoderFallback(), new SurrogatePassDecoderFallback()) { }
public class SurrogatePassEncoderFallback : PythonEncoderFallback {
public override int MaxCharCount => 1;
public override EncoderFallbackBuffer CreateFallbackBuffer()
=> new SurrogatePassEncoderFallbackBuffer(this.IsPass1, this.Encoding);
}
private class SurrogatePassEncoderFallbackBuffer : PythonEncoderFallbackBuffer {
private readonly int _codePage;
private readonly bool _isBigEndianEncoding;
public SurrogatePassEncoderFallbackBuffer(bool isPass1, PythonEncoding encoding)
: base(isPass1, encoding) {
_codePage = encoding.CodePage;
_isBigEndianEncoding = encoding.IsBigEndian;
}
public override byte[] GetFallbackBytes(char charUnknown, int index) {
if (charUnknown < SurrogateRangeStart || SurrogateRangeEnd < charUnknown) {
// EncoderFallbackException(string, char, int) is not accessible here
throw PythonOps.UnicodeEncodeError(
$"'surrogatepass' error handler: value not in range(0x{SurrogateRangeStart:x4}, 0x{SurrogateRangeEnd + 1:x4})",
charUnknown,
index
);
}
byte[] fallbackBytes;
if (this.EncodingCharWidth > 1) {
fallbackBytes = BitConverter.GetBytes(charUnknown);
if (fallbackBytes.Length == this.EncodingCharWidth) {
// UTF-16LE or UTF-16BE
if (BitConverter.IsLittleEndian == _isBigEndianEncoding) {
// swap bytes for non-native endianness encoding
var temp = fallbackBytes[0];
fallbackBytes[0] = fallbackBytes[1];
fallbackBytes[1] = temp;
}
} else {
// UTF-32LE or UTF-32BE
byte[] paddedBytes = new byte[this.EncodingCharWidth];
if (!BitConverter.IsLittleEndian) {
Array.Reverse(fallbackBytes); // to little endian
}
Array.Copy(fallbackBytes, 0, paddedBytes, 0, fallbackBytes.Length);
fallbackBytes = paddedBytes;
if (_isBigEndianEncoding) {
Array.Reverse(fallbackBytes);
}
}
} else if (_codePage == 65001) {
// UTF-8
fallbackBytes = new byte[3]; // UTF-8 for range U+0800 to U+FFFF
ushort codepoint = charUnknown;
fallbackBytes[0] = (byte)(Utf8LeadByte | codepoint >> 12);
codepoint &= 0b_111111_111111;
fallbackBytes[1] = (byte)(Utf8ContByte | codepoint >> 6);
codepoint &= 0b_111111;
fallbackBytes[2] = (byte)(Utf8ContByte | codepoint);
} else {
throw PythonOps.UnicodeEncodeError($"'surrogatepass' error handler does not support this encoding (cp{_codePage})", charUnknown, index);
}
return fallbackBytes;
}
}
public class SurrogatePassDecoderFallback : PythonDecoderFallback {
public override int MaxCharCount => this.Encoding.CharacterWidth;
public override DecoderFallbackBuffer CreateFallbackBuffer()
=> new SurrogatePassDecoderFallbackBuffer(this.IsPass1, this.Encoding);
}
private class SurrogatePassDecoderFallbackBuffer : PythonDecoderFallbackBuffer {
private class ByteBuffer {
private byte[] _buffer;
private int _buflen;
private int _bufidx;
private byte[] _savebuf;
private int _savelen;
private int _saveidx;
public ByteBuffer(int size) {
_buffer = new byte[size];
}
public byte[] Bytes => _buffer;
public int Length => _buflen;
public int Index { get => _bufidx; set { _bufidx = value; } }
public int EndIndex => _bufidx + _buflen;
public void AddByte(byte b) {
_buffer[_buflen++] = b;
}
public void Flush() {
_bufidx += _buflen;
_buflen = 0;
}
public byte[] TrimmedBytes() {
var copy = new byte[_buflen];
Array.Copy(_buffer, 0, copy, 0, _buflen);
return copy;
}
public void Save() {
if (_savebuf != null) {
Array.Copy(_buffer, _savebuf, _buflen);
} else {
_savebuf = (byte[])_buffer.Clone();
}
_savelen = _buflen;
_saveidx = _bufidx;
}
public void Restore() {
if (_savebuf != null) {
Array.Copy(_savebuf, _buffer, _savelen);
}
_buflen = _savelen;
_bufidx = _saveidx;
}
}
private ByteBuffer _buffer;
public SurrogatePassDecoderFallbackBuffer(bool isPass1, PythonEncoding encoding)
: base(isPass1, encoding) {
}
public override char[] GetFallbackChars(byte[] bytesUnknown, int index) {
const int bytesPerChar = 3; // UTF-8 uses 3 bytes to encode a surrogate
int numBytes = bytesUnknown.Length;
char fallbackChar = char.MinValue;
char[] fallbackChars = null;
int numFallbackChars = 0;
switch (this.CodePage) {
case 65001: // UTF-8
if (_buffer == null) _buffer = new ByteBuffer(bytesPerChar);
if (index != _buffer.EndIndex) {
// new fallback sequence
if (_buffer.Length != 0) {
// leftover bytes not consumed
Throw(_buffer.TrimmedBytes(), _buffer.Index);
}
_buffer.Index = index;
}
fallbackChars = new char[(_buffer.Length + numBytes) / bytesPerChar];
for (int i = 0; i < numBytes; i++) {
_buffer.AddByte(bytesUnknown[i]);
if (_buffer.Length == bytesPerChar) {
byte[] bytes = _buffer.Bytes;
if ((bytes[0] & ~Utf8LeadBytePayload) != Utf8LeadByte
|| (bytes[1] & ~Utf8ContBytePayload) != Utf8ContByte
|| (bytes[2] & ~Utf8ContBytePayload) != Utf8ContByte) {
Throw(bytes, _buffer.Index);
}
int fallbackValue = bytes[0] & Utf8LeadBytePayload;
fallbackValue = (bytes[1] & Utf8ContBytePayload) | (fallbackValue << 6);
fallbackValue = (bytes[2] & Utf8ContBytePayload) | (fallbackValue << 6);
if (fallbackValue < SurrogateRangeStart || SurrogateRangeEnd < fallbackValue) {
Throw(bytes, _buffer.Index);
}
fallbackChars[numFallbackChars++] = (char)fallbackValue;
_buffer.Flush();
}
}
break;
case 1200: // UTF-16LE
if (numBytes != 2) break;
fallbackChar = (char)(bytesUnknown[0] | (bytesUnknown[1] << 8));
break;
case 1201: // UTF-16BE
if (numBytes != 2) break;
fallbackChar = (char)(bytesUnknown[1] | (bytesUnknown[0] << 8));
break;
case 12000: // UTF-32LE
if (numBytes != 4) break;
if (bytesUnknown[2] != 0 || bytesUnknown[3] != 0) break;
fallbackChar = (char)(bytesUnknown[0] | (bytesUnknown[1] << 8));
break;
case 12001: // UTF-32BE
if (numBytes != 4) break;
if (bytesUnknown[1] != 0 || bytesUnknown[0] != 0) break;
fallbackChar = (char)(bytesUnknown[3] | (bytesUnknown[2] << 8));
break;
default:
throw new DecoderFallbackException($"'surrogatepass' error handler does not support this encoding (cp{this.CodePage})", bytesUnknown, index);
}
if (fallbackChars == null) {
if (fallbackChar < SurrogateRangeStart || SurrogateRangeEnd < fallbackChar) {
Throw(bytesUnknown, index);
}
fallbackChars = new[] { fallbackChar };
}
return fallbackChars;
}
public override bool IsEmpty => base.IsEmpty && (_buffer?.Length ?? 0) == 0;
public override bool CharCountingMode {
get => base.CharCountingMode;
set {
base.CharCountingMode = value;
if (value) {
_buffer?.Save();
} else {
_buffer?.Restore();
}
}
}
public override void ThrowIfNotEmpty(int endIndex, bool flush) {
if (_buffer != null) {
if (_buffer.Length != 0 && (_buffer.EndIndex != endIndex || flush)) {
// leftover bytes not consumed
Throw(_buffer.TrimmedBytes(), _buffer.Index);
}
// Prepare for next incremental decode step (if any)
_buffer.Index = -_buffer.Length;
}
base.ThrowIfNotEmpty(endIndex, flush);
}
public override void Reset() {
base.Reset();
_buffer = null;
}
protected void Throw(byte[] bytesUnknown, int index)
=> throw new DecoderFallbackException($"'surrogatepass' error handler: not a surrogate character", bytesUnknown, index);
}
}
#endif
}