| ... | @@ -1296,33 +1296,40 @@ pub fn cStrToWin32PrefixedFileW(s: [*:0]const u8) !PathSpace { | ... | @@ -1296,33 +1296,40 @@ pub fn cStrToWin32PrefixedFileW(s: [*:0]const u8) !PathSpace { |
| 1296 | /// it will get NT-style prefix `\??\` prepended automatically. For prepending | 1296 | /// it will get NT-style prefix `\??\` prepended automatically. For prepending |
| 1297 | /// Win32-style prefix, see `sliceToWin32PrefixedFileW` instead. | 1297 | /// Win32-style prefix, see `sliceToWin32PrefixedFileW` instead. |
| 1298 | pub fn sliceToPrefixedFileW(s: []const u8) !PathSpace { | 1298 | pub fn sliceToPrefixedFileW(s: []const u8) !PathSpace { |
| 1299 | // TODO https://github.com/ziglang/zig/issues/2765 | 1299 | return sliceToPrefixedFileWInternal(s, PathPrefix.Nt); |
| 1300 | var path_space: PathSpace = undefined; | | |
| 1301 | const prefix_index: usize = if (mem.startsWith(u8, s, "\\??\\")) 4 else 0; | | |
| 1302 | for (s[prefix_index..]) |byte| { | | |
| 1303 | switch (byte) { | | |
| 1304 | '*', '?', '"', '<', '>', '|' => return error.BadPathName, | | |
| 1305 | else => {}, | | |
| 1306 | } | | |
| 1307 | } | | |
| 1308 | const start_index = if (prefix_index > 0 or !std.fs.path.isAbsolute(s)) 0 else blk: { | | |
| 1309 | const prefix = [_]u16{ '\\', '?', '?', '\\' }; | | |
| 1310 | mem.copy(u16, path_space.data[0..], &prefix); | | |
| 1311 | break :blk prefix.len; | | |
| 1312 | }; | | |
| 1313 | path_space.len = start_index + try std.unicode.utf8ToUtf16Le(path_space.data[start_index..], s); | | |
| 1314 | if (path_space.len > path_space.data.len) return error.NameTooLong; | | |
| 1315 | path_space.ensureNtStyle(); | | |
| 1316 | return path_space; | | |
| 1317 | } | 1300 | } |
| 1318 | | 1301 | |
| 1319 | /// Converts the path `s` to WTF16, null-terminated. If the path is absolute, | 1302 | /// Converts the path `s` to WTF16, null-terminated. If the path is absolute, |
| 1320 | /// it will get Win32-style extended prefix `\\?\` prepended automatically. For prepending | 1303 | /// it will get Win32-style extended prefix `\\?\` prepended automatically. For prepending |
| 1321 | /// NT-style prefix, see `sliceToPrefixedFileW` instead. | 1304 | /// NT-style prefix, see `sliceToPrefixedFileW` instead. |
| 1322 | pub fn sliceToWin32PrefixedFileW(s: []const u8) !PathSpace { | 1305 | pub fn sliceToWin32PrefixedFileW(s: []const u8) !PathSpace { |
| | 1306 | return sliceToPrefixedFileWInternal(s, PathPrefix.Win32); |
| | 1307 | } |
| | 1308 | |
| | 1309 | const PathPrefix = enum { |
| | 1310 | Win32, |
| | 1311 | Nt, |
| | 1312 | |
| | 1313 | fn toUtf8(self: PathPrefix) []const u8 { |
| | 1314 | return switch (self) { |
| | 1315 | .Win32 => "\\\\?\\", |
| | 1316 | .Nt => "\\??\\", |
| | 1317 | }; |
| | 1318 | } |
| | 1319 | |
| | 1320 | fn toUtf16(self: PathPrefix) []const u16 { |
| | 1321 | return switch (self) { |
| | 1322 | .Win32 => &[_]u16{ '\\', '\\', '?', '\\' }, |
| | 1323 | .Nt => &[_]u16{ '\\', '?', '?', '\\' }, |
| | 1324 | }; |
| | 1325 | } |
| | 1326 | }; |
| | 1327 | |
| | 1328 | fn sliceToPrefixedFileWInternal(s: []const u8, prefix: PathPrefix) !PathSpace { |
| 1323 | // TODO https://github.com/ziglang/zig/issues/2765 | 1329 | // TODO https://github.com/ziglang/zig/issues/2765 |
| 1324 | var path_space: PathSpace = undefined; | 1330 | var path_space: PathSpace = undefined; |
| 1325 | const prefix_index: usize = if (mem.startsWith(u8, s, "\\\\?\\")) 4 else 0; | 1331 | const prefix_utf8 = prefix.toUtf8(); |
| | 1332 | const prefix_index: usize = if (mem.startsWith(u8, s, prefix_utf8)) prefix_utf8.len else 0; |
| 1326 | for (s[prefix_index..]) |byte| { | 1333 | for (s[prefix_index..]) |byte| { |
| 1327 | switch (byte) { | 1334 | switch (byte) { |
| 1328 | '*', '?', '"', '<', '>', '|' => return error.BadPathName, | 1335 | '*', '?', '"', '<', '>', '|' => return error.BadPathName, |
| ... | @@ -1330,9 +1337,9 @@ pub fn sliceToWin32PrefixedFileW(s: []const u8) !PathSpace { | ... | @@ -1330,9 +1337,9 @@ pub fn sliceToWin32PrefixedFileW(s: []const u8) !PathSpace { |
| 1330 | } | 1337 | } |
| 1331 | } | 1338 | } |
| 1332 | const start_index = if (prefix_index > 0 or !std.fs.path.isAbsolute(s)) 0 else blk: { | 1339 | const start_index = if (prefix_index > 0 or !std.fs.path.isAbsolute(s)) 0 else blk: { |
| 1333 | const prefix = [_]u16{ '\\', '\\', '?', '\\' }; | 1340 | const prefix_utf16 = prefix.toUtf16(); |
| 1334 | mem.copy(u16, path_space.data[0..], &prefix); | 1341 | mem.copy(u16, path_space.data[0..], prefix_utf16); |
| 1335 | break :blk prefix.len; | 1342 | break :blk prefix_utf16.len; |
| 1336 | }; | 1343 | }; |
| 1337 | path_space.len = start_index + try std.unicode.utf8ToUtf16Le(path_space.data[start_index..], s); | 1344 | path_space.len = start_index + try std.unicode.utf8ToUtf16Le(path_space.data[start_index..], s); |
| 1338 | if (path_space.len > path_space.data.len) return error.NameTooLong; | 1345 | if (path_space.len > path_space.data.len) return error.NameTooLong; |