From patchwork Tue May 24 13:58:28 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Aman Karmani X-Patchwork-Id: 35908 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:6914:b0:82:6b11:2509 with SMTP id q20csp554492pzj; Tue, 24 May 2022 06:58:53 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyxOD36K7pIJSxaFDftli6bMn0POIdGfF9v+Ogk0ldnB3A73TruwP2trb54Dq3ZQYY1ruwL X-Received: by 2002:a50:935c:0:b0:42a:befe:f767 with SMTP id n28-20020a50935c000000b0042abefef767mr29044369eda.428.1653400732918; Tue, 24 May 2022 06:58:52 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1653400732; cv=none; d=google.com; s=arc-20160816; b=Zdle7bbrrSTA9ffjfkXCU+uL3N9/ZrLiX327Mf0tCDMEeZbvZqImqBCd8ZJVVAbcOQ 0pmDC/3rE8xLA1LsShP5+gl6dFoib1HzEepoufhR/jl4yOPb1JqsLIUhaPNvKqE8+Sie 98pg1PPxtJYg/gTsRu+DKeBfvd5dX2wO2Ikdw5hxDlvLfyXXkPB0/sch4MNI75sz5ZrT Y3Kck3syTDF41Oxh3VdwYHtixV+nUWcd6Ru8siFn12diIk6p4ScaH338/ETrE8AZ4BHc 4Gmv7MRRkdf5MxUsVUCv0QdJM+QMF+3eIDJur9Gs3HCJv3gCCk2tzH3YVZvfAhUEDbCh xGyw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:cc:reply-to :list-subscribe:list-help:list-post:list-archive:list-unsubscribe :list-id:precedence:subject:to:mime-version:fcc:date:references :in-reply-to:message-id:from:dkim-signature:delivered-to; bh=ytKRMp3Hw5G1fs3Vgnow0e7tCrt65PWefYbeSG796I4=; b=NTBLpSRPIu1gkgf6XTBEdeXUJaHYQxfjJDTU+CuaIf2QoDepPQ6Ft3GR9EKdINhHWr FXDDyocz1mWSTDymFGgIPUrEkRXhQoJUfylhquCku1V59mHaGmLJgh5bJKiRBlo9pQbL LRxUeloKoXNEPE7k9oRkOF5rsArWtX4JTxmUmZhqAWwJ1QLKtojenvbmB5CgZapLAEwE lrBVWyhjJeE4USHr9zqtDL6aJI8dNrnszfse65suzPbZZA27e7moCvLmlF/6L6qN4hr0 kdCogaS+hmKhbS3N0Z6wdW5AB8LQxN9Ktu9xVvGDAlp2dCbWdSyywAb8cQK/yX/dxy9u e3HQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@gmail.com header.s=20210112 header.b=pjdC5at6; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org; dmarc=fail (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id j14-20020aa7c0ce000000b00425d3a0bd56si16571879edp.155.2022.05.24.06.58.52; Tue, 24 May 2022 06:58:52 -0700 (PDT) Received-SPF: pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) client-ip=79.124.17.100; Authentication-Results: mx.google.com; dkim=neutral (body hash did not verify) header.i=@gmail.com header.s=20210112 header.b=pjdC5at6; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org; dmarc=fail (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id E453F68B528; Tue, 24 May 2022 16:58:42 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-pj1-f42.google.com (mail-pj1-f42.google.com [209.85.216.42]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 9773168B292 for ; Tue, 24 May 2022 16:58:34 +0300 (EEST) Received: by mail-pj1-f42.google.com with SMTP id pq9-20020a17090b3d8900b001df622bf81dso2245581pjb.3 for ; Tue, 24 May 2022 06:58:34 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:message-id:in-reply-to:references:date:subject:fcc :content-transfer-encoding:mime-version:to:cc; bh=m7glWo8l9Vtp0k/JtUjc09ZdO7Jhb0P9L5pdwytooDA=; b=pjdC5at6T/7UNklpPTT62Yed1F6WczFKCy+H3Scc474uLP47gR6hVdpL3abuSstSnp E0aPIQP/ID9qJ2t/j8zdEppO9Dm9RZwn5GrCW4RbChWqzFzgYYCnsXi6s89F/soDhiuR DU5SRDe9Y+CluNGOPUVYOp17zeA+95gfOUvKze5pir1l6sprKTO/fqbu3MZRyHmt6VA1 z+ofh55cQpzY5zl/q08t4Cy/gHe1dJfCsiRgO4w7Z6WDIZe7+F1E3K+SBxqHsYqwseY8 T1vTL+3KK5oaIXA56oSvbfRrFbRPtoq8QvEx2Majil2G0Fqry3wHqVgdkFdZoWkghtQ0 /hgw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:message-id:in-reply-to:references:date :subject:fcc:content-transfer-encoding:mime-version:to:cc; bh=m7glWo8l9Vtp0k/JtUjc09ZdO7Jhb0P9L5pdwytooDA=; b=uHVLOA2I8Oc5thQLqB4O7vA3CZz0UUOA6WQaJTN5a/qCXZVQQlPCw26tf0y79iTsoA eVd9v2eTe40wxIwCDCmVgqEdBdsqnY8mJDehBD6HStiAHk7qrL7q43vc/1iCQPpUO/Wt Hm3Xndot8VFTCL+IaFzEx6Vg96gTiLtNnwZPFam6gZDPmEpYzLdO8E5d4zzTOQ5aJxr1 gi7nEu/lCw91Xv2AmyvGat139YrkBCwqolRjnvt9DlcsZbqAd+LHZ56SCYyp9thP4lv2 53hh4jwaKmTdtmiWUaWIim+SKDwqb5XqucqcyPOZ35+uQB1uPkINpuoBdlOpQvT7ZOxr n+ew== X-Gm-Message-State: AOAM531mDu8ahXZnk/xOWrjsHhEjFsznwqmw1aTw1KUeybyga4nR+GgP sd4j0f6cGOVHb7/nGxwm4sZfXzHM1gmoaw== X-Received: by 2002:a17:902:ecc7:b0:161:c860:38e2 with SMTP id a7-20020a170902ecc700b00161c86038e2mr27547885plh.75.1653400712879; Tue, 24 May 2022 06:58:32 -0700 (PDT) Received: from [127.0.0.1] (master.gitmailbox.com. [34.83.118.50]) by smtp.gmail.com with ESMTPSA id o1-20020a17090a9f8100b001df2538c61dsm1727887pjp.23.2022.05.24.06.58.31 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Tue, 24 May 2022 06:58:32 -0700 (PDT) From: softworkz X-Google-Original-From: softworkz Message-Id: <960aa795ffd4018d970fb1768afe3ad11cdb6936.1653400709.git.ffmpegagent@gmail.com> In-Reply-To: References: Date: Tue, 24 May 2022 13:58:28 +0000 Fcc: Sent MIME-Version: 1.0 To: ffmpeg-devel@ffmpeg.org Subject: [FFmpeg-devel] [PATCH v6 1/2] avutil/wchar_filename, file_open: Support long file names on Windows X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: FFmpeg development discussions and patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: FFmpeg development discussions and patches Cc: Martin =?utf-8?q?Storsj=C3=B6?= , softworkz , Hendrik Leppkes Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: E8Gga+ooVXW7 From: softworkz Signed-off-by: softworkz --- libavutil/file_open.c | 2 +- libavutil/wchar_filename.h | 180 +++++++++++++++++++++++++++++++++++++ 2 files changed, 181 insertions(+), 1 deletion(-) diff --git a/libavutil/file_open.c b/libavutil/file_open.c index fb64c2e4ee..58a6073353 100644 --- a/libavutil/file_open.c +++ b/libavutil/file_open.c @@ -45,7 +45,7 @@ static int win32_open(const char *filename_utf8, int oflag, int pmode) wchar_t *filename_w; /* convert UTF-8 to wide chars */ - if (utf8towchar(filename_utf8, &filename_w)) + if (get_extended_win32_path(filename_utf8, &filename_w)) return -1; if (!filename_w) goto fallback; diff --git a/libavutil/wchar_filename.h b/libavutil/wchar_filename.h index 90f082452c..f36d9dfea3 100644 --- a/libavutil/wchar_filename.h +++ b/libavutil/wchar_filename.h @@ -40,6 +40,186 @@ static inline int utf8towchar(const char *filename_utf8, wchar_t **filename_w) MultiByteToWideChar(CP_UTF8, 0, filename_utf8, -1, *filename_w, num_chars); return 0; } + +/** + * Checks for extended path prefixes for which normalization needs to be skipped. + * see .NET6: PathInternal.IsExtended() + * https://github.com/dotnet/runtime/blob/9260c249140ef90b4299d0fe1aa3037e25228518/src/libraries/Common/src/System/IO/PathInternal.Windows.cs#L165 + */ +static inline int path_is_extended(const wchar_t *path) +{ + if (path[0] == L'\\' && (path[1] == L'\\' || path[1] == L'?') && path[2] == L'?' && path[3] == L'\\') + return 1; + + return 0; +} + +/** + * Checks for a device path prefix. + * see .NET6: PathInternal.IsDevice() + * we don't check forward slashes and extended paths (as already done) + * https://github.com/dotnet/runtime/blob/9260c249140ef90b4299d0fe1aa3037e25228518/src/libraries/Common/src/System/IO/PathInternal.Windows.cs#L132 + */ +static inline int path_is_device_path(const wchar_t *path) +{ + if (path[0] == L'\\' && path[1] == L'\\' && path[2] == L'.' && path[3] == L'\\') + return 1; + + return 0; +} + +/** + * Performs path normalization by calling GetFullPathNameW(). + * see .NET6: PathHelper.GetFullPathName() + * https://github.com/dotnet/runtime/blob/2a99e18eedabcf1add064c099da59d9301ce45e0/src/libraries/System.Private.CoreLib/src/System/IO/PathHelper.Windows.cs#L70 + */ +static inline int get_full_path_name(wchar_t **ppath_w) +{ + int num_chars; + wchar_t *temp_w; + + num_chars = GetFullPathNameW(*ppath_w, 0, NULL, NULL); + if (num_chars <= 0) { + errno = EINVAL; + return -1; + } + + temp_w = (wchar_t *)av_calloc(num_chars, sizeof(wchar_t)); + if (!temp_w) { + errno = ENOMEM; + return -1; + } + + num_chars = GetFullPathNameW(*ppath_w, num_chars, temp_w, NULL); + if (num_chars <= 0) { + av_free(temp_w); + errno = EINVAL; + return -1; + } + + av_freep(ppath_w); + *ppath_w = temp_w; + + return 0; +} + +/** + * Normalizes a Windows file or folder path. + * Expansion of short paths (with 8.3 path components) is currently omitted + * as it is not required for accessing long paths. + * see .NET6: PathHelper.Normalize() + * https://github.com/dotnet/runtime/blob/2a99e18eedabcf1add064c099da59d9301ce45e0/src/libraries/System.Private.CoreLib/src/System/IO/PathHelper.Windows.cs#L25 + */ +static inline int path_normalize(wchar_t **ppath_w) +{ + int ret; + + if ((ret = get_full_path_name(ppath_w)) < 0) + return ret; + + /* What .NET does at this point is to call PathHelper.TryExpandShortFileName() + * in case the path contains a '~' character. + * We don't need to do this as we don't need to normalize the file name + * for presentation, and the extended path prefix works with 8.3 path + * components as well + */ + return 0; +} + +/** + * Adds an extended path or UNC prefix to longs paths or paths ending + * with a space or a dot. (' ' or '.'). + * This function expects that the path has been normalized before by + * calling path_normalize() and it doesn't check whether the path is + * actually long (> MAX_PATH). + * see .NET6: PathInternal.EnsureExtendedPrefix() + * https://github.com/dotnet/runtime/blob/9260c249140ef90b4299d0fe1aa3037e25228518/src/libraries/Common/src/System/IO/PathInternal.Windows.cs#L107 + */ +static inline int add_extended_prefix(wchar_t **ppath_w) +{ + const wchar_t *unc_prefix = L"\\\\?\\UNC\\"; + const wchar_t *extended_path_prefix = L"\\\\?\\"; + const wchar_t *path_w = *ppath_w; + const size_t len = wcslen(path_w); + wchar_t *temp_w; + + /* We're skipping the check IsPartiallyQualified() because + * we expect to have called GetFullPathNameW() already. */ + if (len < 2 || path_is_extended(*ppath_w) || path_is_device_path(*ppath_w)) { + return 0; + } + + if (path_w[0] == L'\\' && path_w[1] == L'\\') { + /* unc_prefix length is 8 plus 1 for terminating zeros, + * we subtract 2 for the leading '\\' of the original path */ + temp_w = (wchar_t *)av_calloc(len - 2 + 8 + 1, sizeof(wchar_t)); + if (!temp_w) { + errno = ENOMEM; + return -1; + } + wcscpy(temp_w, unc_prefix); + wcscat(temp_w, path_w + 2); + } else { + // The length of extended_path_prefix is 4 plus 1 for terminating zeros + temp_w = (wchar_t *)av_calloc(len + 4 + 1, sizeof(wchar_t)); + if (!temp_w) { + errno = ENOMEM; + return -1; + } + wcscpy(temp_w, extended_path_prefix); + wcscat(temp_w, path_w); + } + + av_freep(ppath_w); + *ppath_w = temp_w; + + return 0; +} + +/** + * Converts a file or folder path to wchar_t for use with Windows file + * APIs. Paths with extended path prefix (either '\\?\' or \??\') are + * left unchanged. + * All other paths are normalized and converted to absolute paths. + * Longs paths (>= MAX_PATH) are prefixed with the extended path or extended + * UNC path prefix. + * see .NET6: Path.GetFullPath() and Path.GetFullPathInternal() + * https://github.com/dotnet/runtime/blob/2a99e18eedabcf1add064c099da59d9301ce45e0/src/libraries/System.Private.CoreLib/src/System/IO/Path.Windows.cs#L126 + */ +static inline int get_extended_win32_path(const char *path, wchar_t **ppath_w) +{ + int ret; + size_t len; + + if ((ret = utf8towchar(path, ppath_w)) < 0) + return ret; + + if (path_is_extended(*ppath_w)) { + /* Paths prefixed with '\\?\' or \??\' are considered normalized by definition. + * Windows doesn't normalize those paths and neither should we. + */ + return 0; + } + + if ((ret = path_normalize(ppath_w)) < 0) { + av_freep(ppath_w); + return ret; + } + + /* see .NET6: PathInternal.EnsureExtendedPrefixIfNeeded() + * https://github.com/dotnet/runtime/blob/9260c249140ef90b4299d0fe1aa3037e25228518/src/libraries/Common/src/System/IO/PathInternal.Windows.cs#L92 + */ + len = wcslen(*ppath_w); + if (len >= MAX_PATH) { + if ((ret = add_extended_prefix(ppath_w)) < 0) { + av_freep(ppath_w); + return ret; + } + } + + return 0; +} + #endif #endif /* AVUTIL_WCHAR_FILENAME_H */