|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2025 Apple Inc. and the Swift project authors |
| 6 | +// Licensed under Apache License v2.0 with Runtime Library Exception |
| 7 | +// |
| 8 | +// See http://swift.org/LICENSE.txt for license information |
| 9 | +// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors |
| 10 | +// |
| 11 | +//===----------------------------------------------------------------------===// |
| 12 | + |
| 13 | +#if os(Windows) |
| 14 | +public import WinSDK |
| 15 | + |
| 16 | +/// Calls a Win32 API function that fills a (potentially long path) null-terminated string buffer by continually attempting to allocate more memory up until the true max path is reached. |
| 17 | +/// This is especially useful for protecting against race conditions like with GetCurrentDirectoryW where the measured length may no longer be valid on subsequent calls. |
| 18 | +/// - parameter initialSize: Initial size of the buffer (including the null terminator) to allocate to hold the returned string. |
| 19 | +/// - parameter maxSize: Maximum size of the buffer (including the null terminator) to allocate to hold the returned string. |
| 20 | +/// - parameter body: Closure to call the Win32 API function to populate the provided buffer. |
| 21 | +/// Should return the number of UTF-16 code units (not including the null terminator) copied, 0 to indicate an error. |
| 22 | +/// If the buffer is not of sufficient size, should return a value greater than or equal to the size of the buffer. |
| 23 | +private func FillNullTerminatedWideStringBuffer(initialSize: DWORD, maxSize: DWORD, _ body: (UnsafeMutableBufferPointer<WCHAR>) throws -> DWORD) throws -> String { |
| 24 | + var bufferCount = max(1, min(initialSize, maxSize)) |
| 25 | + while bufferCount <= maxSize { |
| 26 | + if let result = try withUnsafeTemporaryAllocation(of: WCHAR.self, capacity: Int(bufferCount), { buffer in |
| 27 | + let count = try body(buffer) |
| 28 | + switch count { |
| 29 | + case 0: |
| 30 | + throw Win32Error(GetLastError()) |
| 31 | + case 1..<DWORD(buffer.count): |
| 32 | + let result = String(decodingCString: buffer.baseAddress!, as: UTF16.self) |
| 33 | + assert(result.utf16.count == count, "Parsed UTF-16 count \(result.utf16.count) != reported UTF-16 count \(count)") |
| 34 | + return result |
| 35 | + default: |
| 36 | + bufferCount *= 2 |
| 37 | + return nil |
| 38 | + } |
| 39 | + }) { |
| 40 | + return result |
| 41 | + } |
| 42 | + } |
| 43 | + throw Win32Error(DWORD(ERROR_INSUFFICIENT_BUFFER)) |
| 44 | +} |
| 45 | + |
| 46 | +private let maxPathLength = DWORD(Int16.max) // https://learn.microsoft.com/en-us/windows/win32/fileio/maximum-file-path-limitation |
| 47 | +private let maxEnvVarLength = DWORD(Int16.max) // https://devblogs.microsoft.com/oldnewthing/20100203-00/ |
| 48 | + |
| 49 | +@_spi(Testing) public func SWB_GetModuleFileNameW(_ hModule: HMODULE?) throws -> String { |
| 50 | + try FillNullTerminatedWideStringBuffer(initialSize: DWORD(MAX_PATH), maxSize: maxPathLength) { |
| 51 | + GetModuleFileNameW(hModule, $0.baseAddress!, DWORD($0.count)) |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +public func SWB_GetEnvironmentVariableW(_ wName: LPCWSTR) throws -> String { |
| 56 | + try FillNullTerminatedWideStringBuffer(initialSize: 1024, maxSize: maxEnvVarLength) { |
| 57 | + GetEnvironmentVariableW(wName, $0.baseAddress!, DWORD($0.count)) |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +public func SWB_GetWindowsDirectoryW() throws -> String { |
| 62 | + try FillNullTerminatedWideStringBuffer(initialSize: DWORD(MAX_PATH), maxSize: maxPathLength) { |
| 63 | + GetWindowsDirectoryW($0.baseAddress!, DWORD($0.count)) |
| 64 | + } |
| 65 | +} |
| 66 | +#endif |
0 commit comments