File: StringUTF8Validation.swift

package info (click to toggle)
swiftlang 6.2.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,856,264 kB
  • sloc: cpp: 9,995,718; ansic: 2,234,019; asm: 1,092,167; python: 313,940; objc: 82,726; f90: 80,126; lisp: 38,373; pascal: 25,580; sh: 20,378; ml: 5,058; perl: 4,751; makefile: 4,725; awk: 3,535; javascript: 3,018; xml: 918; fortran: 664; cs: 573; ruby: 396
file content (319 lines) | stat: -rw-r--r-- 12,487 bytes parent folder | download
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
//===--- StringUTF8Validation.swift ---------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2019 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//

private func _isUTF8MultiByteLeading(_ x: UInt8) -> Bool {
  return (0xC2...0xF4).contains(x)
}

private func _isNotOverlong_F0(_ x: UInt8) -> Bool {
  return (0x90...0xBF).contains(x)
}

private func _isNotInvalid_F4(_ x: UInt8) -> Bool {
  return UTF8.isContinuation(x) && x <= 0x8F
}

private func _isNotOverlong_E0(_ x: UInt8) -> Bool {
  return (0xA0...0xBF).contains(x)
}

private func _isNotInvalid_ED(_ x: UInt8) -> Bool {
  return UTF8.isContinuation(x) && x <= 0x9F
}

internal struct UTF8ExtraInfo: Equatable {
  public var isASCII: Bool
}

@inline(never) // slow-path
private func _diagnoseInvalidUTF8MultiByteLeading(
  _ x: UInt8
) -> _UTF8EncodingErrorKind {
  _internalInvariant(x >= 0x80)
  _internalInvariant(!_isUTF8MultiByteLeading(x))
  switch x {
  case 0x80...0xBF:
    return .unexpectedContinuationByte
  case 0xC0..<0xC2:
    return .overlongEncodingByte
  default:
    _internalInvariant(x > 0xF4)
    return .invalidNonSurrogateCodePointByte
  }
}

internal enum UTF8ValidationResult {
  case success(UTF8ExtraInfo)
  case error(
    kind: _UTF8EncodingErrorKind, toBeReplaced: Range<Int>
  )
}

// FIXME: refactor other parts of stdlib to avoid this dumb mirror enum
//
// Mirror of UTF8.ValidationError.Kind, available on 6.1
internal struct _UTF8EncodingErrorKind: Error, Sendable, Hashable
// TODO: embedded?, Codable
  , RawRepresentable {
  internal var rawValue: UInt8

  @available(SwiftStdlib 6.2, *)
  internal var _publicKind: UTF8.ValidationError.Kind {
    .init(rawValue: self.rawValue)!
  }

  @inlinable
  internal init(rawValue: UInt8) {
    self.rawValue = rawValue
  }

  /// A continuation byte (`10xxxxxx`) outside of a multi-byte sequence
  @_alwaysEmitIntoClient
  internal static var unexpectedContinuationByte: Self {
    .init(rawValue: 0)
  }

  /// A byte in a surrogate code point (`U+D800..U+DFFF`) sequence
  @_alwaysEmitIntoClient
  internal static var surrogateCodePointByte: Self {
    .init(rawValue: 1)
  }

  /// A byte in an invalid, non-surrogate code point (`>U+10FFFF`) sequence
  @_alwaysEmitIntoClient
  internal static var invalidNonSurrogateCodePointByte: Self {
    .init(rawValue: 2)
  }

  /// A byte in an overlong encoding sequence
  @_alwaysEmitIntoClient
  internal static var overlongEncodingByte: Self {
    .init(rawValue: 3)
  }

  /// A multi-byte sequence that is the start of a valid multi-byte scalar
  /// but is cut off before ending correctly
  @_alwaysEmitIntoClient
  internal static var truncatedScalar: Self {
    .init(rawValue: 4)
  }
}

extension UTF8ValidationResult: Equatable {}

internal func validateUTF8(_ buf: UnsafeBufferPointer<UInt8>) -> UTF8ValidationResult {
  if unsafe _allASCII(buf) {
    return .success(UTF8ExtraInfo(isASCII: true))
  }

  var iter = unsafe buf.makeIterator()
  var lastValidIndex = buf.startIndex

  @inline(__always) func guarantee(
    _ f: (UInt8) -> Bool,
    _ err: _UTF8EncodingErrorKind
  ) throws(_UTF8EncodingErrorKind) {
    guard let cu = unsafe iter.next() else {
      throw .truncatedScalar
    }
    guard f(cu) else {
      throw err
    }
  }
  @inline(__always) func guaranteeContinuation(
  ) throws(_UTF8EncodingErrorKind) {
    try guarantee(UTF8.isContinuation, .truncatedScalar)
  }

  func _legacyInvalidLengthCalculation(_ _buffer: (_storage: UInt32, ())) -> Int {
    // function body copied from UTF8.ForwardParser._invalidLength
    if _buffer._storage               & 0b0__1100_0000__1111_0000
                                     == 0b0__1000_0000__1110_0000 {
      // 2-byte prefix of 3-byte sequence. The top 5 bits of the decoded result
      // must be nonzero and not a surrogate
      let top5Bits = _buffer._storage & 0b0__0010_0000__0000_1111
      if top5Bits != 0 && top5Bits   != 0b0__0010_0000__0000_1101 { return 2 }
    }
    else if _buffer._storage                & 0b0__1100_0000__1111_1000
                                           == 0b0__1000_0000__1111_0000
    {
      // Prefix of 4-byte sequence. The top 5 bits of the decoded result
      // must be nonzero and no greater than 0b0__0100_0000
      let top5bits = UInt16(_buffer._storage & 0b0__0011_0000__0000_0111)
      if top5bits != 0 && top5bits.byteSwapped <= 0b0__0000_0100__0000_0000 {
        return _buffer._storage   & 0b0__1100_0000__0000_0000__0000_0000
                                 == 0b0__1000_0000__0000_0000__0000_0000 ? 3 : 2
      }
    }
    return 1
  }

  func _legacyNarrowIllegalRange(buf: Slice<UnsafeBufferPointer<UInt8>>) -> Range<Int> {
    var reversePacked: UInt32 = 0
    if let third = unsafe buf.dropFirst(2).first {
      reversePacked |= UInt32(third)
      reversePacked <<= 8
    }
    if let second = unsafe buf.dropFirst().first {
      reversePacked |= UInt32(second)
      reversePacked <<= 8
    }
    unsafe reversePacked |= UInt32(buf.first!)
    let _buffer: (_storage: UInt32, x: ()) = (reversePacked, ())
    let invalids = _legacyInvalidLengthCalculation(_buffer)
    return unsafe buf.startIndex ..< buf.startIndex + invalids
  }

  func findInvalidRange(_ buf: Slice<UnsafeBufferPointer<UInt8>>) -> Range<Int> {
    var endIndex = unsafe buf.startIndex
    var iter = unsafe buf.makeIterator()
    _ = unsafe iter.next()
    while let cu = unsafe iter.next(), UTF8.isContinuation(cu) {
      endIndex += 1
      // Unicode's Maximal subpart of an ill-formed subsequence will yield
      // at most 3 bytes of error.
      if unsafe buf.distance(from: buf.startIndex, to: endIndex) >= 3 {
        break
      }
    }
    let illegalRange = unsafe Range(buf.startIndex...endIndex)
    unsafe _internalInvariant(illegalRange.clamped(to: (buf.startIndex..<buf.endIndex)) == illegalRange,
                 "illegal range out of full range")
    // FIXME: Remove the call to `_legacyNarrowIllegalRange` and return `illegalRange` directly
    return unsafe _legacyNarrowIllegalRange(buf: buf[illegalRange])
  }

  do throws(_UTF8EncodingErrorKind) {

    /*
    The table of valid UTF-8 is:

     ╔════════════════════╦════════╦════════╦════════╦════════╗
     ║    Scalar value    ║ Byte 0 ║ Byte 1 ║ Byte 2 ║ Byte 3 ║
     ╠════════════════════╬════════╬════════╬════════╬════════╣
     ║ U+0000..U+007F     ║ 00..7F ║        ║        ║        ║
     ║ U+0080..U+07FF     ║ C2..DF ║ Contin ║        ║        ║
     ║ U+0800..U+0FFF     ║ E0     ║ A0..BF ║ Contin ║        ║
     ║ U+1000..U+CFFF     ║ E1..EC ║ Contin ║ Contin ║        ║
     ║ U+D000..U+D7FF     ║ ED     ║ 80..9F ║ Contin ║        ║
     ║ U+E000..U+FFFF     ║ EE..EF ║ Contin ║ Contin ║        ║
     ║ U+10000..U+3FFFF   ║ F0     ║ 90..BF ║ Contin ║ Contin ║
     ║ U+40000..U+FFFFF   ║ F1..F3 ║ Contin ║ Contin ║ Contin ║
     ║ U+100000..U+10FFFF ║ F4     ║ 80..8F ║ Contin ║ Contin ║
     ╚════════════════════╩════════╩════════╩════════╩════════╝

     "Contin" is any continuation byte, i.e. 80..BF or 10xxxxxx
     */
    var isASCII = true
    while let cu = unsafe iter.next() {
      if UTF8.isASCII(cu) { lastValidIndex &+= 1; continue }
      isASCII = false
      if _slowPath(!_isUTF8MultiByteLeading(cu)) {
        throw _diagnoseInvalidUTF8MultiByteLeading(cu)
      }
      switch cu {
      case 0xC2...0xDF:
        try guaranteeContinuation()
        lastValidIndex &+= 2
      case 0xE0:
        try guarantee(_isNotOverlong_E0, .overlongEncodingByte)
        try guaranteeContinuation()
        lastValidIndex &+= 3
      case 0xE1...0xEC:
        try guaranteeContinuation()
        try guaranteeContinuation()
        lastValidIndex &+= 3
      case 0xED:
        try guarantee(_isNotInvalid_ED, .surrogateCodePointByte)
        try guaranteeContinuation()
        lastValidIndex &+= 3
      case 0xEE...0xEF:
        try guaranteeContinuation()
        try guaranteeContinuation()
        lastValidIndex &+= 3
      case 0xF0:
        try guarantee(_isNotOverlong_F0, .overlongEncodingByte)
        try guaranteeContinuation()
        try guaranteeContinuation()
        lastValidIndex &+= 4
      case 0xF1...0xF3:
        try guaranteeContinuation()
        try guaranteeContinuation()
        try guaranteeContinuation()
        lastValidIndex &+= 4
      case 0xF4:
        try guarantee(
          _isNotInvalid_F4, .invalidNonSurrogateCodePointByte)
        try guaranteeContinuation()
        try guaranteeContinuation()
        lastValidIndex &+= 4
      default:
        Builtin.unreachable()
      }
    }
    return .success(UTF8ExtraInfo(isASCII: isASCII))
  } catch {
    return unsafe .error(
      kind: error,
      toBeReplaced: findInvalidRange(buf[lastValidIndex...]))
  }
}

internal func repairUTF8(_ input: UnsafeBufferPointer<UInt8>, firstKnownBrokenRange: Range<Int>) -> String {
  _internalInvariant(!input.isEmpty, "empty input doesn't need to be repaired")
  _internalInvariant(firstKnownBrokenRange.clamped(to: input.indices) == firstKnownBrokenRange)
  // During this process, `remainingInput` contains the remaining bytes to process. It's split into three
  // non-overlapping sub-regions:
  //
  //  1. `goodChunk` (may be empty) containing bytes that are known good UTF-8 and can be copied into the output String
  //  2. `brokenRange` (never empty) the next range of broken bytes,
  //  3. the remainder (implicit, will become the next `remainingInput`)
  //
  // At the beginning of the process, the `goodChunk` starts at the beginning and extends to just before the first
  // known broken byte. The known broken bytes are covered in the `brokenRange` and everything following that is
  // the remainder.
  // We then copy the `goodChunk` into the target buffer and append a UTF8 replacement character. `brokenRange` is
  // skipped (replaced by the replacement character) and we restart the same process. This time, `goodChunk` extends
  // from the byte after the previous `brokenRange` to the next `brokenRange`.
  var result = _StringGuts()
  let replacementCharacterCount = Unicode.Scalar._replacementCharacter.withUTF8CodeUnits { $0.count }
  result.reserveCapacity(input.count + 5 * replacementCharacterCount) // extra space for some replacement characters

  var brokenRange: Range<Int> = firstKnownBrokenRange
  var remainingInput = unsafe input
  repeat {
    _internalInvariant(!brokenRange.isEmpty, "broken range empty")
    _internalInvariant(!remainingInput.isEmpty, "empty remaining input doesn't need to be repaired")
    let goodChunk = unsafe remainingInput[..<brokenRange.startIndex]

    // very likely this capacity reservation does not actually do anything because we reserved space for the entire
    // input plus up to five replacement characters up front
    result.reserveCapacity(result.count + remainingInput.count + replacementCharacterCount)

    // we can now safely append the next known good bytes and a replacement character
    unsafe result.appendInPlace(unsafe UnsafeBufferPointer(rebasing: goodChunk),
                         isASCII: false /* appending replacement character anyway, so let's not bother */)
    Unicode.Scalar._replacementCharacter.withUTF8CodeUnits {
      unsafe result.appendInPlace($0, isASCII: false)
    }

    unsafe remainingInput = unsafe UnsafeBufferPointer(rebasing: remainingInput[brokenRange.endIndex...])
    switch unsafe validateUTF8(remainingInput) {
    case .success:
      unsafe result.appendInPlace(remainingInput, isASCII: false)
      return String(result)
    case .error(_, let newBrokenRange):
      brokenRange = newBrokenRange
    }
  } while !remainingInput.isEmpty
  return String(result)
}