-
Notifications
You must be signed in to change notification settings - Fork 4.5k
/
Utf8.cs
225 lines (183 loc) · 11.6 KB
/
Utf8.cs
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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Buffers;
using System.Diagnostics;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
namespace System.Text.Unicode
{
#if SYSTEM_PRIVATE_CORELIB
public
#else
internal
#endif
static class Utf8
{
/*
* OperationStatus-based APIs for transcoding of chunked data.
* This method is similar to Encoding.UTF8.GetBytes / GetChars but has a
* different calling convention, different error handling mechanisms, and
* different performance characteristics.
*
* If 'replaceInvalidSequences' is true, the method will replace any ill-formed
* subsequence in the source with U+FFFD when transcoding to the destination,
* then it will continue processing the remainder of the buffers. Otherwise
* the method will return OperationStatus.InvalidData.
*
* If the method does return an error code, the out parameters will represent
* how much of the data was successfully transcoded, and the location of the
* ill-formed subsequence can be deduced from these values.
*
* If 'replaceInvalidSequences' is true, the method is guaranteed never to return
* OperationStatus.InvalidData. If 'isFinalBlock' is true, the method is
* guaranteed never to return OperationStatus.NeedMoreData.
*/
/// <summary>
/// Transcodes the UTF-16 <paramref name="source"/> buffer to <paramref name="destination"/> as UTF-8.
/// </summary>
/// <remarks>
/// If <paramref name="replaceInvalidSequences"/> is <see langword="true"/>, invalid UTF-16 sequences
/// in <paramref name="source"/> will be replaced with U+FFFD in <paramref name="destination"/>, and
/// this method will not return <see cref="OperationStatus.InvalidData"/>.
/// </remarks>
public static unsafe OperationStatus FromUtf16(ReadOnlySpan<char> source, Span<byte> destination, out int charsRead, out int bytesWritten, bool replaceInvalidSequences = true, bool isFinalBlock = true)
{
// Throwaway span accesses - workaround for https://github.com/dotnet/runtime/issues/12332
_ = source.Length;
_ = destination.Length;
fixed (char* pOriginalSource = &MemoryMarshal.GetReference(source))
fixed (byte* pOriginalDestination = &MemoryMarshal.GetReference(destination))
{
// We're going to bulk transcode as much as we can in a loop, iterating
// every time we see bad data that requires replacement.
OperationStatus operationStatus = OperationStatus.Done;
char* pInputBufferRemaining = pOriginalSource;
byte* pOutputBufferRemaining = pOriginalDestination;
while (!source.IsEmpty)
{
// We've pinned the spans at the entry point to this method.
// It's safe for us to use Unsafe.AsPointer on them during this loop.
operationStatus = Utf8Utility.TranscodeToUtf8(
pInputBuffer: (char*)Unsafe.AsPointer(ref MemoryMarshal.GetReference(source)),
inputLength: source.Length,
pOutputBuffer: (byte*)Unsafe.AsPointer(ref MemoryMarshal.GetReference(destination)),
outputBytesRemaining: destination.Length,
pInputBufferRemaining: out pInputBufferRemaining,
pOutputBufferRemaining: out pOutputBufferRemaining);
// If we finished the operation entirely or we ran out of space in the destination buffer,
// or if we need more input data and the caller told us that there's possibly more data
// coming, return immediately.
if (operationStatus <= OperationStatus.DestinationTooSmall
|| (operationStatus == OperationStatus.NeedMoreData && !isFinalBlock))
{
break;
}
// We encountered invalid data, or we need more data but the caller told us we're
// at the end of the stream. In either case treat this as truly invalid.
// If the caller didn't tell us to replace invalid sequences, return immediately.
if (!replaceInvalidSequences)
{
operationStatus = OperationStatus.InvalidData; // status code may have been NeedMoreData - force to be error
break;
}
// We're going to attempt to write U+FFFD to the destination buffer.
// Do we even have enough space to do so?
destination = destination.Slice((int)(pOutputBufferRemaining - (byte*)Unsafe.AsPointer(ref MemoryMarshal.GetReference(destination))));
if (destination.Length <= 2)
{
operationStatus = OperationStatus.DestinationTooSmall;
break;
}
destination[0] = 0xEF; // U+FFFD = [ EF BF BD ] in UTF-8
destination[1] = 0xBF;
destination[2] = 0xBD;
destination = destination.Slice(3);
// Invalid UTF-16 sequences are always of length 1. Just skip the next character.
source = source.Slice((int)(pInputBufferRemaining - (char*)Unsafe.AsPointer(ref MemoryMarshal.GetReference(source))) + 1);
operationStatus = OperationStatus.Done; // we patched the error - if we're about to break out of the loop this is a success case
pInputBufferRemaining = (char*)Unsafe.AsPointer(ref MemoryMarshal.GetReference(source));
pOutputBufferRemaining = (byte*)Unsafe.AsPointer(ref MemoryMarshal.GetReference(destination));
}
// Not possible to make any further progress - report to our caller how far we got.
charsRead = (int)(pInputBufferRemaining - pOriginalSource);
bytesWritten = (int)(pOutputBufferRemaining - pOriginalDestination);
return operationStatus;
}
}
/// <summary>
/// Transcodes the UTF-8 <paramref name="source"/> buffer to <paramref name="destination"/> as UTF-16.
/// </summary>
/// <remarks>
/// If <paramref name="replaceInvalidSequences"/> is <see langword="true"/>, invalid UTF-8 sequences
/// in <paramref name="source"/> will be replaced with U+FFFD in <paramref name="destination"/>, and
/// this method will not return <see cref="OperationStatus.InvalidData"/>.
/// </remarks>
public static unsafe OperationStatus ToUtf16(ReadOnlySpan<byte> source, Span<char> destination, out int bytesRead, out int charsWritten, bool replaceInvalidSequences = true, bool isFinalBlock = true)
{
// Throwaway span accesses - workaround for https://github.com/dotnet/runtime/issues/12332
_ = source.Length;
_ = destination.Length;
// We'll be mutating these values throughout our loop.
fixed (byte* pOriginalSource = &MemoryMarshal.GetReference(source))
fixed (char* pOriginalDestination = &MemoryMarshal.GetReference(destination))
{
// We're going to bulk transcode as much as we can in a loop, iterating
// every time we see bad data that requires replacement.
OperationStatus operationStatus = OperationStatus.Done;
byte* pInputBufferRemaining = pOriginalSource;
char* pOutputBufferRemaining = pOriginalDestination;
while (!source.IsEmpty)
{
// We've pinned the spans at the entry point to this method.
// It's safe for us to use Unsafe.AsPointer on them during this loop.
operationStatus = Utf8Utility.TranscodeToUtf16(
pInputBuffer: (byte*)Unsafe.AsPointer(ref MemoryMarshal.GetReference(source)),
inputLength: source.Length,
pOutputBuffer: (char*)Unsafe.AsPointer(ref MemoryMarshal.GetReference(destination)),
outputCharsRemaining: destination.Length,
pInputBufferRemaining: out pInputBufferRemaining,
pOutputBufferRemaining: out pOutputBufferRemaining);
// If we finished the operation entirely or we ran out of space in the destination buffer,
// or if we need more input data and the caller told us that there's possibly more data
// coming, return immediately.
if (operationStatus <= OperationStatus.DestinationTooSmall
|| (operationStatus == OperationStatus.NeedMoreData && !isFinalBlock))
{
break;
}
// We encountered invalid data, or we need more data but the caller told us we're
// at the end of the stream. In either case treat this as truly invalid.
// If the caller didn't tell us to replace invalid sequences, return immediately.
if (!replaceInvalidSequences)
{
operationStatus = OperationStatus.InvalidData; // status code may have been NeedMoreData - force to be error
break;
}
// We're going to attempt to write U+FFFD to the destination buffer.
// Do we even have enough space to do so?
destination = destination.Slice((int)(pOutputBufferRemaining - (char*)Unsafe.AsPointer(ref MemoryMarshal.GetReference(destination))));
if (destination.IsEmpty)
{
operationStatus = OperationStatus.DestinationTooSmall;
break;
}
destination[0] = (char)UnicodeUtility.ReplacementChar;
destination = destination.Slice(1);
// Now figure out how many bytes of the source we must skip over before we should retry
// the operation. This might be more than 1 byte.
source = source.Slice((int)(pInputBufferRemaining - (byte*)Unsafe.AsPointer(ref MemoryMarshal.GetReference(source))));
Debug.Assert(!source.IsEmpty, "Expected 'Done' if source is fully consumed.");
Rune.DecodeFromUtf8(source, out _, out int bytesConsumedJustNow);
source = source.Slice(bytesConsumedJustNow);
operationStatus = OperationStatus.Done; // we patched the error - if we're about to break out of the loop this is a success case
pInputBufferRemaining = (byte*)Unsafe.AsPointer(ref MemoryMarshal.GetReference(source));
pOutputBufferRemaining = (char*)Unsafe.AsPointer(ref MemoryMarshal.GetReference(destination));
}
// Not possible to make any further progress - report to our caller how far we got.
bytesRead = (int)(pInputBufferRemaining - pOriginalSource);
charsWritten = (int)(pOutputBufferRemaining - pOriginalDestination);
return operationStatus;
}
}
}
}