File: URLParser%2BICU.swift

package info (click to toggle)
swiftlang 6.0.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,519,992 kB
  • sloc: cpp: 9,107,863; ansic: 2,040,022; asm: 1,135,751; python: 296,500; objc: 82,456; f90: 60,502; lisp: 34,951; pascal: 19,946; sh: 18,133; perl: 7,482; ml: 4,937; javascript: 4,117; makefile: 3,840; awk: 3,535; xml: 914; fortran: 619; cs: 573; ruby: 573
file content (218 lines) | stat: -rw-r--r-- 8,068 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
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2024 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
//
//===----------------------------------------------------------------------===//

#if canImport(FoundationEssentials)
import FoundationEssentials
#endif

internal import _FoundationICU

#if !FOUNDATION_FRAMEWORK
@_dynamicReplacement(for: _uidnaHook())
private func _uidnaHook_localized() -> UIDNAHook.Type? {
    return UIDNAHookICU.self
}
#endif

struct UIDNAHookICU: UIDNAHook {
    // `Sendable` notes: `UIDNA` from ICU is thread safe.
    struct UIDNAPointer : @unchecked Sendable {
        init(_ ptr: OpaquePointer?) { self.idnaTranscoder = ptr }
        var idnaTranscoder: OpaquePointer?
    }

    private static func U_SUCCESS(_ x: Int32) -> Bool {
        return x <= U_ZERO_ERROR.rawValue
    }

    private static let idnaTranscoder: UIDNAPointer? = {
        var status = U_ZERO_ERROR
        let options = UInt32(
            UIDNA_CHECK_BIDI                    |
            UIDNA_CHECK_CONTEXTJ                |
            UIDNA_NONTRANSITIONAL_TO_UNICODE    |
            UIDNA_NONTRANSITIONAL_TO_ASCII
        )
        let encoder = uidna_openUTS46(options, &status)
        guard U_SUCCESS(status.rawValue) else {
            return nil
        }
        return UIDNAPointer(encoder)
    }()

    private static func shouldAllow(_ errors: UInt32, encodeToASCII: Bool) -> Bool {
        let allowedErrors: UInt32
        if encodeToASCII {
            allowedErrors = 0
        } else {
            allowedErrors = UInt32(
                UIDNA_ERROR_EMPTY_LABEL             |
                UIDNA_ERROR_LABEL_TOO_LONG          |
                UIDNA_ERROR_DOMAIN_NAME_TOO_LONG    |
                UIDNA_ERROR_LEADING_HYPHEN          |
                UIDNA_ERROR_TRAILING_HYPHEN         |
                UIDNA_ERROR_HYPHEN_3_4
            )
        }
        return errors & ~allowedErrors == 0
    }

    /// Type of `uidna_nameToASCII` and `uidna_nameToUnicode` functions
    private typealias TranscodingFunction<T> = (OpaquePointer?, UnsafePointer<T>?, Int32, UnsafeMutablePointer<T>?, Int32, UnsafeMutablePointer<UIDNAInfo>?, UnsafeMutablePointer<UErrorCode>?) -> Int32

    private static func IDNACodedHost<T: FixedWidthInteger>(
        hostBuffer: UnsafeBufferPointer<T>,
        transcode: TranscodingFunction<T>,
        allowErrors: (UInt32) -> Bool,
        createString: (UnsafeMutablePointer<T>, Int) -> String?
    ) -> String? {
        let maxHostBufferLength = 2048
        if hostBuffer.count > maxHostBufferLength {
            return nil
        }

        guard let transcoder = idnaTranscoder else {
            return nil
        }

        let result: String? = withUnsafeTemporaryAllocation(of: T.self, capacity: maxHostBufferLength) { outBuffer in
            var processingDetails = UIDNAInfo(
                size: Int16(MemoryLayout<UIDNAInfo>.size),
                isTransitionalDifferent: 0,
                reservedB3: 0,
                errors: 0,
                reservedI2: 0,
                reservedI3: 0
            )
            var error = U_ZERO_ERROR

            let hostBufferPtr = hostBuffer.baseAddress!
            let outBufferPtr = outBuffer.baseAddress!

            let charsConverted = transcode(
                transcoder.idnaTranscoder,
                hostBufferPtr,
                Int32(hostBuffer.count),
                outBufferPtr,
                Int32(outBuffer.count),
                &processingDetails,
                &error
            )

            if U_SUCCESS(error.rawValue), allowErrors(processingDetails.errors), charsConverted > 0 {
                return createString(outBufferPtr, Int(charsConverted))
            }
            return nil
        }
        return result
    }

    private static func IDNACodedHostUTF8(_ utf8Buffer: UnsafeBufferPointer<UInt8>, encodeToASCII: Bool) -> String? {
        var transcode = uidna_nameToUnicodeUTF8
        if encodeToASCII {
            transcode = uidna_nameToASCII_UTF8
        }
        return utf8Buffer.withMemoryRebound(to: CChar.self) { charBuffer in
            return IDNACodedHost(
                hostBuffer: charBuffer,
                transcode: transcode,
                allowErrors: { errors in
                    shouldAllow(errors, encodeToASCII: encodeToASCII)
                },
                createString: { ptr, count in
                    let outBuffer = UnsafeBufferPointer(start: ptr, count: count).withMemoryRebound(to: UInt8.self) { $0 }
                    var hostsAreEqual = false
                    if outBuffer.count == utf8Buffer.count {
                        hostsAreEqual = true
                        for i in 0..<outBuffer.count {
                            if utf8Buffer[i] == outBuffer[i] {
                                continue
                            }
                            guard utf8Buffer[i]._lowercased == outBuffer[i] else {
                                hostsAreEqual = false
                                break
                            }
                        }
                    }
                    if hostsAreEqual {
                        return String._tryFromUTF8(utf8Buffer)
                    } else {
                        return String._tryFromUTF8(outBuffer)
                    }
                }
            )
        }
    }

    private static func IDNACodedHostUTF16(_ utf16Buffer: UnsafeBufferPointer<UInt16>, encodeToASCII: Bool) -> String? {
        var transcode = uidna_nameToUnicode
        if encodeToASCII {
            transcode = uidna_nameToASCII
        }
        return IDNACodedHost(
            hostBuffer: utf16Buffer,
            transcode: transcode,
            allowErrors: { errors in
                shouldAllow(errors, encodeToASCII: encodeToASCII)
            },
            createString: { ptr, count in
                let outBuffer = UnsafeBufferPointer(start: ptr, count: count)
                var hostsAreEqual = false
                if outBuffer.count == utf16Buffer.count {
                    hostsAreEqual = true
                    for i in 0..<outBuffer.count {
                        if utf16Buffer[i] == outBuffer[i] {
                            continue
                        }
                        guard utf16Buffer[i] < 128,
                              UInt8(utf16Buffer[i])._lowercased == outBuffer[i] else {
                            hostsAreEqual = false
                            break
                        }
                    }
                }
                if hostsAreEqual {
                    return String(_utf16: utf16Buffer)
                } else {
                    return String(_utf16: outBuffer)
                }
            }
        )
    }

    private static func IDNACodedHost(_ host: some StringProtocol, encodeToASCII: Bool) -> String? {
        let fastResult = host.utf8.withContiguousStorageIfAvailable {
            IDNACodedHostUTF8($0, encodeToASCII: encodeToASCII)
        }
        if let fastResult {
            return fastResult
        }
        #if FOUNDATION_FRAMEWORK
        if let fastCharacters = host._ns._fastCharacterContents() {
            let charsBuffer = UnsafeBufferPointer(start: fastCharacters, count: host._ns.length)
            return IDNACodedHostUTF16(charsBuffer, encodeToASCII: encodeToASCII)
        }
        #endif
        var hostString = String(host)
        return hostString.withUTF8 {
            IDNACodedHostUTF8($0, encodeToASCII: encodeToASCII)
        }
    }

    static func encode(_ host: some StringProtocol) -> String? {
        return IDNACodedHost(host, encodeToASCII: true)
    }

    static func decode(_ host: some StringProtocol) -> String? {
        return IDNACodedHost(host, encodeToASCII: false)
    }

}