summaryrefslogtreecommitdiff
path: root/src/util
diff options
context:
space:
mode:
authorEdward Thomson <ethomson@edwardthomson.com>2023-04-22 23:12:00 +0100
committerEdward Thomson <ethomson@edwardthomson.com>2023-04-22 23:17:32 +0100
commit190a4c55df72b32adf4d60f77cbc47276b74f84b (patch)
tree896d6d41c2ea6305992bd646bbbe79a3fdb5f6fe /src/util
parente288f874a3a73ef31f88bb524f6d25d5ff3c5a3a (diff)
parent8a62616f43fe5ea37d41296f40293ff97aa88cfa (diff)
downloadlibgit2-190a4c55df72b32adf4d60f77cbc47276b74f84b.tar.gz
Merge remote-tracking branch 'origin/main' into shallow-clone-network
Diffstat (limited to 'src/util')
-rw-r--r--src/util/CMakeLists.txt1
-rw-r--r--src/util/filebuf.c7
-rw-r--r--src/util/filebuf.h25
-rw-r--r--src/util/fs_path.c2
-rw-r--r--src/util/git2_features.h.in1
-rw-r--r--src/util/util.c2
-rw-r--r--src/util/win32/error.c2
-rw-r--r--src/util/win32/path_w32.c16
-rw-r--r--src/util/win32/posix_w32.c2
-rw-r--r--src/util/win32/utf-conv.c148
-rw-r--r--src/util/win32/utf-conv.h95
-rw-r--r--src/util/win32/w32_util.c2
12 files changed, 193 insertions, 110 deletions
diff --git a/src/util/CMakeLists.txt b/src/util/CMakeLists.txt
index 2207041ef..ee35eb961 100644
--- a/src/util/CMakeLists.txt
+++ b/src/util/CMakeLists.txt
@@ -9,7 +9,6 @@ configure_file(git2_features.h.in git2_features.h)
set(UTIL_INCLUDES
"${PROJECT_BINARY_DIR}/src/util"
"${PROJECT_BINARY_DIR}/include"
- "${PROJECT_BINARY_DIR}/include/git2"
"${PROJECT_SOURCE_DIR}/src/util"
"${PROJECT_SOURCE_DIR}/include")
diff --git a/src/util/filebuf.c b/src/util/filebuf.c
index e014d43b2..7afb76b88 100644
--- a/src/util/filebuf.c
+++ b/src/util/filebuf.c
@@ -302,11 +302,16 @@ int git_filebuf_open_withsize(git_filebuf *file, const char *path, int flags, mo
}
/* If we are hashing on-write, allocate a new hash context */
- if (flags & GIT_FILEBUF_HASH_CONTENTS) {
+ if (flags & GIT_FILEBUF_HASH_SHA1) {
file->compute_digest = 1;
if (git_hash_ctx_init(&file->digest, GIT_HASH_ALGORITHM_SHA1) < 0)
goto cleanup;
+ } else if (flags & GIT_FILEBUF_HASH_SHA256) {
+ file->compute_digest = 1;
+
+ if (git_hash_ctx_init(&file->digest, GIT_HASH_ALGORITHM_SHA256) < 0)
+ goto cleanup;
}
compression = flags >> GIT_FILEBUF_DEFLATE_SHIFT;
diff --git a/src/util/filebuf.h b/src/util/filebuf.h
index 4a61ae4e3..e23b9ed2a 100644
--- a/src/util/filebuf.h
+++ b/src/util/filebuf.h
@@ -17,13 +17,14 @@
# define GIT_FILEBUF_THREADS
#endif
-#define GIT_FILEBUF_HASH_CONTENTS (1 << 0)
-#define GIT_FILEBUF_APPEND (1 << 2)
+#define GIT_FILEBUF_HASH_SHA1 (1 << 0)
+#define GIT_FILEBUF_HASH_SHA256 (1 << 1)
+#define GIT_FILEBUF_APPEND (1 << 2)
#define GIT_FILEBUF_CREATE_LEADING_DIRS (1 << 3)
-#define GIT_FILEBUF_TEMPORARY (1 << 4)
-#define GIT_FILEBUF_DO_NOT_BUFFER (1 << 5)
-#define GIT_FILEBUF_FSYNC (1 << 6)
-#define GIT_FILEBUF_DEFLATE_SHIFT (7)
+#define GIT_FILEBUF_TEMPORARY (1 << 4)
+#define GIT_FILEBUF_DO_NOT_BUFFER (1 << 5)
+#define GIT_FILEBUF_FSYNC (1 << 6)
+#define GIT_FILEBUF_DEFLATE_SHIFT (7)
#define GIT_FILELOCK_EXTENSION ".lock\0"
#define GIT_FILELOCK_EXTLENGTH 6
@@ -91,4 +92,16 @@ int git_filebuf_hash(unsigned char *out, git_filebuf *file);
int git_filebuf_flush(git_filebuf *file);
int git_filebuf_stats(time_t *mtime, size_t *size, git_filebuf *file);
+GIT_INLINE(int) git_filebuf_hash_flags(git_hash_algorithm_t algorithm)
+{
+ switch (algorithm) {
+ case GIT_HASH_ALGORITHM_SHA1:
+ return GIT_FILEBUF_HASH_SHA1;
+ case GIT_HASH_ALGORITHM_SHA256:
+ return GIT_FILEBUF_HASH_SHA256;
+ default:
+ return 0;
+ }
+}
+
#endif
diff --git a/src/util/fs_path.c b/src/util/fs_path.c
index b52867e77..e03fcf7c7 100644
--- a/src/util/fs_path.c
+++ b/src/util/fs_path.c
@@ -2015,7 +2015,7 @@ int git_fs_path_find_executable(git_str *fullpath, const char *executable)
git_win32_path fullpath_w, executable_w;
int error;
- if (git__utf8_to_16(executable_w, GIT_WIN_PATH_MAX, executable) < 0)
+ if (git_utf8_to_16(executable_w, GIT_WIN_PATH_MAX, executable) < 0)
return -1;
error = git_win32_path_find_executable(fullpath_w, executable_w);
diff --git a/src/util/git2_features.h.in b/src/util/git2_features.h.in
index fbf0cab60..1575be641 100644
--- a/src/util/git2_features.h.in
+++ b/src/util/git2_features.h.in
@@ -41,6 +41,7 @@
#cmakedefine GIT_OPENSSL_DYNAMIC 1
#cmakedefine GIT_SECURE_TRANSPORT 1
#cmakedefine GIT_MBEDTLS 1
+#cmakedefine GIT_SCHANNEL 1
#cmakedefine GIT_SHA1_COLLISIONDETECT 1
#cmakedefine GIT_SHA1_WIN32 1
diff --git a/src/util/util.c b/src/util/util.c
index aee95fddf..9c9f2c040 100644
--- a/src/util/util.c
+++ b/src/util/util.c
@@ -743,7 +743,7 @@ int git__getenv(git_str *out, const char *name)
git_str_clear(out);
- if (git__utf8_to_16_alloc(&wide_name, name) < 0)
+ if (git_utf8_to_16_alloc(&wide_name, name) < 0)
return -1;
if ((value_len = GetEnvironmentVariableW(wide_name, NULL, 0)) > 0) {
diff --git a/src/util/win32/error.c b/src/util/win32/error.c
index 3a52fb5a9..dfd6fa1e8 100644
--- a/src/util/win32/error.c
+++ b/src/util/win32/error.c
@@ -43,7 +43,7 @@ char *git_win32_get_error_message(DWORD error_code)
(LPWSTR)&lpMsgBuf, 0, NULL)) {
/* Convert the message to UTF-8. If this fails, we will
* return NULL, which is a condition expected by the caller */
- if (git__utf16_to_8_alloc(&utf8_msg, lpMsgBuf) < 0)
+ if (git_utf8_from_16_alloc(&utf8_msg, lpMsgBuf) < 0)
utf8_msg = NULL;
LocalFree(lpMsgBuf);
diff --git a/src/util/win32/path_w32.c b/src/util/win32/path_w32.c
index d9fc8292b..7a559e45c 100644
--- a/src/util/win32/path_w32.c
+++ b/src/util/win32/path_w32.c
@@ -336,13 +336,13 @@ int git_win32_path_from_utf8(git_win32_path out, const char *src)
/* See if this is an absolute path (beginning with a drive letter) */
if (git_fs_path_is_absolute(src)) {
- if (git__utf8_to_16(dest, GIT_WIN_PATH_MAX, src) < 0)
+ if (git_utf8_to_16(dest, GIT_WIN_PATH_MAX, src) < 0)
goto on_error;
}
/* File-prefixed NT-style paths beginning with \\?\ */
else if (path__is_nt_namespace(src)) {
/* Skip the NT prefix, the destination already contains it */
- if (git__utf8_to_16(dest, GIT_WIN_PATH_MAX, src + PATH__NT_NAMESPACE_LEN) < 0)
+ if (git_utf8_to_16(dest, GIT_WIN_PATH_MAX, src + PATH__NT_NAMESPACE_LEN) < 0)
goto on_error;
}
/* UNC paths */
@@ -351,7 +351,7 @@ int git_win32_path_from_utf8(git_win32_path out, const char *src)
dest += 4;
/* Skip the leading "\\" */
- if (git__utf8_to_16(dest, GIT_WIN_PATH_MAX - 2, src + 2) < 0)
+ if (git_utf8_to_16(dest, GIT_WIN_PATH_MAX - 2, src + 2) < 0)
goto on_error;
}
/* Absolute paths omitting the drive letter */
@@ -365,7 +365,7 @@ int git_win32_path_from_utf8(git_win32_path out, const char *src)
}
/* Skip the drive letter specification ("C:") */
- if (git__utf8_to_16(dest + 2, GIT_WIN_PATH_MAX - 2, src) < 0)
+ if (git_utf8_to_16(dest + 2, GIT_WIN_PATH_MAX - 2, src) < 0)
goto on_error;
}
/* Relative paths */
@@ -377,7 +377,7 @@ int git_win32_path_from_utf8(git_win32_path out, const char *src)
dest[cwd_len++] = L'\\';
- if (git__utf8_to_16(dest + cwd_len, GIT_WIN_PATH_MAX - cwd_len, src) < 0)
+ if (git_utf8_to_16(dest + cwd_len, GIT_WIN_PATH_MAX - cwd_len, src) < 0)
goto on_error;
}
@@ -404,7 +404,7 @@ int git_win32_path_relative_from_utf8(git_win32_path out, const char *src)
return git_win32_path_from_utf8(out, src);
}
- if ((len = git__utf8_to_16(dest, GIT_WIN_PATH_MAX, src)) < 0)
+ if ((len = git_utf8_to_16(dest, GIT_WIN_PATH_MAX, src)) < 0)
return -1;
for (p = dest; p < (dest + len); p++) {
@@ -433,7 +433,7 @@ int git_win32_path_to_utf8(git_win32_utf8_path dest, const wchar_t *src)
}
}
- if ((len = git__utf16_to_8(out, GIT_WIN_PATH_UTF8, src)) < 0)
+ if ((len = git_utf8_from_16(out, GIT_WIN_PATH_UTF8, src)) < 0)
return len;
git_fs_path_mkposix(dest);
@@ -471,7 +471,7 @@ char *git_win32_path_8dot3_name(const char *path)
if (namelen > 12 || (shortname = git__malloc(namelen + 1)) == NULL)
return NULL;
- if ((len = git__utf16_to_8(shortname, namelen + 1, start)) < 0)
+ if ((len = git_utf8_from_16(shortname, namelen + 1, start)) < 0)
return NULL;
return shortname;
diff --git a/src/util/win32/posix_w32.c b/src/util/win32/posix_w32.c
index 5862e5c9a..3fec469a6 100644
--- a/src/util/win32/posix_w32.c
+++ b/src/util/win32/posix_w32.c
@@ -649,7 +649,7 @@ int p_getcwd(char *buffer_out, size_t size)
git_win32_path_remove_namespace(cwd, wcslen(cwd));
/* Convert the working directory back to UTF-8 */
- if (git__utf16_to_8(buffer_out, size, cwd) < 0) {
+ if (git_utf8_from_16(buffer_out, size, cwd) < 0) {
DWORD code = GetLastError();
if (code == ERROR_INSUFFICIENT_BUFFER)
diff --git a/src/util/win32/utf-conv.c b/src/util/win32/utf-conv.c
index 4bde3023a..ad35c0c35 100644
--- a/src/util/win32/utf-conv.c
+++ b/src/util/win32/utf-conv.c
@@ -15,108 +15,114 @@ GIT_INLINE(void) git__set_errno(void)
errno = EINVAL;
}
-/**
- * Converts a UTF-8 string to wide characters.
- *
- * @param dest The buffer to receive the wide string.
- * @param dest_size The size of the buffer, in characters.
- * @param src The UTF-8 string to convert.
- * @return The length of the wide string, in characters (not counting the NULL terminator), or < 0 for failure
- */
-int git__utf8_to_16(wchar_t *dest, size_t dest_size, const char *src)
+int git_utf8_to_16(wchar_t *dest, size_t dest_size, const char *src)
+{
+ /* Length of -1 indicates NULL termination of the input string. */
+ return git_utf8_to_16_with_len(dest, dest_size, src, -1);
+}
+
+int git_utf8_to_16_with_len(
+ wchar_t *dest,
+ size_t _dest_size,
+ const char *src,
+ int src_len)
{
+ int dest_size = (int)min(_dest_size, INT_MAX);
int len;
- /* Length of -1 indicates NULL termination of the input string. Subtract 1 from the result to
- * turn 0 into -1 (an error code) and to not count the NULL terminator as part of the string's
- * length. MultiByteToWideChar never returns int's minvalue, so underflow is not possible */
- if ((len = MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, src, -1, dest, (int)dest_size) - 1) < 0)
+ /*
+ * Subtract 1 from the result to turn 0 into -1 (an error code) and
+ * to not count the NULL terminator as part of the string's length.
+ * MultiByteToWideChar never returns int's minvalue, so underflow
+ * is not possible.
+ */
+ len = MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS,
+ src, src_len, dest, dest_size) - 1;
+
+ if (len < 0)
git__set_errno();
return len;
}
-/**
- * Converts a wide string to UTF-8.
- *
- * @param dest The buffer to receive the UTF-8 string.
- * @param dest_size The size of the buffer, in bytes.
- * @param src The wide string to convert.
- * @return The length of the UTF-8 string, in bytes (not counting the NULL terminator), or < 0 for failure
- */
-int git__utf16_to_8(char *dest, size_t dest_size, const wchar_t *src)
+int git_utf8_from_16(char *dest, size_t dest_size, const wchar_t *src)
{
+ /* Length of -1 indicates NULL termination of the input string. */
+ return git_utf8_from_16_with_len(dest, dest_size, src, -1);
+}
+
+int git_utf8_from_16_with_len(
+ char *dest,
+ size_t _dest_size,
+ const wchar_t *src,
+ int src_len)
+{
+ int dest_size = (int)min(_dest_size, INT_MAX);
int len;
- /* Length of -1 indicates NULL termination of the input string. Subtract 1 from the result to
- * turn 0 into -1 (an error code) and to not count the NULL terminator as part of the string's
- * length. WideCharToMultiByte never returns int's minvalue, so underflow is not possible */
- if ((len = WideCharToMultiByte(CP_UTF8, WC_ERR_INVALID_CHARS, src, -1, dest, (int)dest_size, NULL, NULL) - 1) < 0)
+ /*
+ * Subtract 1 from the result to turn 0 into -1 (an error code) and
+ * to not count the NULL terminator as part of the string's length.
+ * WideCharToMultiByte never returns int's minvalue, so underflow
+ * is not possible.
+ */
+ len = WideCharToMultiByte(CP_UTF8, WC_ERR_INVALID_CHARS,
+ src, src_len, dest, dest_size, NULL, NULL) - 1;
+
+ if (len < 0)
git__set_errno();
return len;
}
-/**
- * Converts a UTF-8 string to wide characters.
- * Memory is allocated to hold the converted string.
- * The caller is responsible for freeing the string with git__free.
- *
- * @param dest Receives a pointer to the wide string.
- * @param src The UTF-8 string to convert.
- * @return The length of the wide string, in characters (not counting the NULL terminator), or < 0 for failure
- */
-int git__utf8_to_16_alloc(wchar_t **dest, const char *src)
+int git_utf8_to_16_alloc(wchar_t **dest, const char *src)
+{
+ /* Length of -1 indicates NULL termination of the input string. */
+ return git_utf8_to_16_alloc_with_len(dest, src, -1);
+}
+
+int git_utf8_to_16_alloc_with_len(wchar_t **dest, const char *src, int src_len)
{
int utf16_size;
*dest = NULL;
- /* Length of -1 indicates NULL termination of the input string */
- utf16_size = MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, src, -1, NULL, 0);
+ utf16_size = MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS,
+ src, src_len, NULL, 0);
if (!utf16_size) {
git__set_errno();
return -1;
}
- if (!(*dest = git__mallocarray(utf16_size, sizeof(wchar_t)))) {
- errno = ENOMEM;
- return -1;
- }
+ *dest = git__mallocarray(utf16_size, sizeof(wchar_t));
+ GIT_ERROR_CHECK_ALLOC(*dest);
- utf16_size = MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, src, -1, *dest, utf16_size);
-
- if (!utf16_size) {
- git__set_errno();
+ utf16_size = git_utf8_to_16_with_len(*dest, (size_t)utf16_size,
+ src, src_len);
+ if (utf16_size < 0) {
git__free(*dest);
*dest = NULL;
}
- /* Subtract 1 from the result to turn 0 into -1 (an error code) and to not count the NULL
- * terminator as part of the string's length. MultiByteToWideChar never returns int's minvalue,
- * so underflow is not possible */
- return utf16_size - 1;
+ return utf16_size;
}
-/**
- * Converts a wide string to UTF-8.
- * Memory is allocated to hold the converted string.
- * The caller is responsible for freeing the string with git__free.
- *
- * @param dest Receives a pointer to the UTF-8 string.
- * @param src The wide string to convert.
- * @return The length of the UTF-8 string, in bytes (not counting the NULL terminator), or < 0 for failure
- */
-int git__utf16_to_8_alloc(char **dest, const wchar_t *src)
+int git_utf8_from_16_alloc(char **dest, const wchar_t *src)
+{
+ /* Length of -1 indicates NULL termination of the input string. */
+ return git_utf8_from_16_alloc_with_len(dest, src, -1);
+}
+
+int git_utf8_from_16_alloc_with_len(char **dest, const wchar_t *src, int src_len)
{
int utf8_size;
*dest = NULL;
- /* Length of -1 indicates NULL termination of the input string */
- utf8_size = WideCharToMultiByte(CP_UTF8, WC_ERR_INVALID_CHARS, src, -1, NULL, 0, NULL, NULL);
+ utf8_size = WideCharToMultiByte(CP_UTF8, WC_ERR_INVALID_CHARS,
+ src, src_len, NULL, 0, NULL, NULL);
if (!utf8_size) {
git__set_errno();
@@ -124,23 +130,15 @@ int git__utf16_to_8_alloc(char **dest, const wchar_t *src)
}
*dest = git__malloc(utf8_size);
+ GIT_ERROR_CHECK_ALLOC(*dest);
- if (!*dest) {
- errno = ENOMEM;
- return -1;
- }
-
- utf8_size = WideCharToMultiByte(CP_UTF8, WC_ERR_INVALID_CHARS, src, -1, *dest, utf8_size, NULL, NULL);
-
- if (!utf8_size) {
- git__set_errno();
+ utf8_size = WideCharToMultiByte(CP_UTF8, WC_ERR_INVALID_CHARS,
+ src, src_len, *dest, utf8_size, NULL, NULL);
+ if (utf8_size < 0) {
git__free(*dest);
*dest = NULL;
}
- /* Subtract 1 from the result to turn 0 into -1 (an error code) and to not count the NULL
- * terminator as part of the string's length. MultiByteToWideChar never returns int's minvalue,
- * so underflow is not possible */
- return utf8_size - 1;
+ return utf8_size;
}
diff --git a/src/util/win32/utf-conv.h b/src/util/win32/utf-conv.h
index 120d647ef..301f5a6d3 100644
--- a/src/util/win32/utf-conv.h
+++ b/src/util/win32/utf-conv.h
@@ -16,14 +16,45 @@
#endif
/**
+ * Converts a NUL-terminated UTF-8 string to wide characters. This is a
+ * convenience function for `git_utf8_to_16_with_len`.
+ *
+ * @param dest The buffer to receive the wide string.
+ * @param dest_size The size of the buffer, in characters.
+ * @param src The UTF-8 string to convert.
+ * @return The length of the wide string, in characters
+ * (not counting the NULL terminator), or < 0 for failure
+ */
+int git_utf8_to_16(wchar_t *dest, size_t dest_size, const char *src);
+
+/**
* Converts a UTF-8 string to wide characters.
*
* @param dest The buffer to receive the wide string.
* @param dest_size The size of the buffer, in characters.
* @param src The UTF-8 string to convert.
- * @return The length of the wide string, in characters (not counting the NULL terminator), or < 0 for failure
+ * @param src_len The length of the string to convert.
+ * @return The length of the wide string, in characters
+ * (not counting the NULL terminator), or < 0 for failure
+ */
+int git_utf8_to_16_with_len(
+ wchar_t *dest,
+ size_t dest_size,
+ const char *src,
+ int src_len);
+
+/**
+ * Converts a NUL-terminated wide string to UTF-8. This is a convenience
+ * function for `git_utf8_from_16_with_len`.
+ *
+ * @param dest The buffer to receive the UTF-8 string.
+ * @param dest_size The size of the buffer, in bytes.
+ * @param src The wide string to convert.
+ * @param src_len The length of the string to convert.
+ * @return The length of the UTF-8 string, in bytes
+ * (not counting the NULL terminator), or < 0 for failure
*/
-int git__utf8_to_16(wchar_t *dest, size_t dest_size, const char *src);
+int git_utf8_from_16(char *dest, size_t dest_size, const wchar_t *src);
/**
* Converts a wide string to UTF-8.
@@ -31,30 +62,66 @@ int git__utf8_to_16(wchar_t *dest, size_t dest_size, const char *src);
* @param dest The buffer to receive the UTF-8 string.
* @param dest_size The size of the buffer, in bytes.
* @param src The wide string to convert.
- * @return The length of the UTF-8 string, in bytes (not counting the NULL terminator), or < 0 for failure
+ * @param src_len The length of the string to convert.
+ * @return The length of the UTF-8 string, in bytes
+ * (not counting the NULL terminator), or < 0 for failure
*/
-int git__utf16_to_8(char *dest, size_t dest_size, const wchar_t *src);
+int git_utf8_from_16_with_len(char *dest, size_t dest_size, const wchar_t *src, int src_len);
/**
- * Converts a UTF-8 string to wide characters.
- * Memory is allocated to hold the converted string.
- * The caller is responsible for freeing the string with git__free.
+ * Converts a UTF-8 string to wide characters. Memory is allocated to hold
+ * the converted string. The caller is responsible for freeing the string
+ * with git__free.
*
* @param dest Receives a pointer to the wide string.
* @param src The UTF-8 string to convert.
- * @return The length of the wide string, in characters (not counting the NULL terminator), or < 0 for failure
+ * @return The length of the wide string, in characters
+ * (not counting the NULL terminator), or < 0 for failure
*/
-int git__utf8_to_16_alloc(wchar_t **dest, const char *src);
+int git_utf8_to_16_alloc(wchar_t **dest, const char *src);
/**
- * Converts a wide string to UTF-8.
- * Memory is allocated to hold the converted string.
- * The caller is responsible for freeing the string with git__free.
+ * Converts a UTF-8 string to wide characters. Memory is allocated to hold
+ * the converted string. The caller is responsible for freeing the string
+ * with git__free.
+ *
+ * @param dest Receives a pointer to the wide string.
+ * @param src The UTF-8 string to convert.
+ * @param src_len The length of the string.
+ * @return The length of the wide string, in characters
+ * (not counting the NULL terminator), or < 0 for failure
+ */
+int git_utf8_to_16_alloc_with_len(
+ wchar_t **dest,
+ const char *src,
+ int src_len);
+
+/**
+ * Converts a wide string to UTF-8. Memory is allocated to hold the
+ * converted string. The caller is responsible for freeing the string
+ * with git__free.
+ *
+ * @param dest Receives a pointer to the UTF-8 string.
+ * @param src The wide string to convert.
+ * @return The length of the UTF-8 string, in bytes
+ * (not counting the NULL terminator), or < 0 for failure
+ */
+int git_utf8_from_16_alloc(char **dest, const wchar_t *src);
+
+/**
+ * Converts a wide string to UTF-8. Memory is allocated to hold the
+ * converted string. The caller is responsible for freeing the string
+ * with git__free.
*
* @param dest Receives a pointer to the UTF-8 string.
* @param src The wide string to convert.
- * @return The length of the UTF-8 string, in bytes (not counting the NULL terminator), or < 0 for failure
+ * @param src_len The length of the wide string.
+ * @return The length of the UTF-8 string, in bytes
+ * (not counting the NULL terminator), or < 0 for failure
*/
-int git__utf16_to_8_alloc(char **dest, const wchar_t *src);
+int git_utf8_from_16_alloc_with_len(
+ char **dest,
+ const wchar_t *src,
+ int src_len);
#endif
diff --git a/src/util/win32/w32_util.c b/src/util/win32/w32_util.c
index fe4b75bae..f5b006a19 100644
--- a/src/util/win32/w32_util.c
+++ b/src/util/win32/w32_util.c
@@ -115,7 +115,7 @@ int git_win32__file_attribute_to_stat(
/* st_size gets the UTF-8 length of the target name, in bytes,
* not counting the NULL terminator */
- if ((st->st_size = git__utf16_to_8(NULL, 0, target)) < 0) {
+ if ((st->st_size = git_utf8_from_16(NULL, 0, target)) < 0) {
git_error_set(GIT_ERROR_OS, "could not convert reparse point name for '%ls'", path);
return -1;
}