2 * Copyright (C) the libgit2 contributors. All rights reserved.
4 * This file is part of libgit2, distributed under the GNU GPL v2 with
5 * a Linking Exception. For full terms see the included COPYING file.
11 GIT_INLINE(void) git__set_errno(void)
13 if (GetLastError() == ERROR_INSUFFICIENT_BUFFER
)
20 * Converts a UTF-8 string to wide characters.
22 * @param dest The buffer to receive the wide string.
23 * @param dest_size The size of the buffer, in characters.
24 * @param src The UTF-8 string to convert.
25 * @return The length of the wide string, in characters (not counting the NULL terminator), or < 0 for failure
27 int git__utf8_to_16(wchar_t *dest
, size_t dest_size
, const char *src
)
31 /* Length of -1 indicates NULL termination of the input string. Subtract 1 from the result to
32 * turn 0 into -1 (an error code) and to not count the NULL terminator as part of the string's
33 * length. MultiByteToWideChar never returns int's minvalue, so underflow is not possible */
34 if ((len
= MultiByteToWideChar(CP_UTF8
, MB_ERR_INVALID_CHARS
, src
, -1, dest
, (int)dest_size
) - 1) < 0)
41 * Converts a wide string to UTF-8.
43 * @param dest The buffer to receive the UTF-8 string.
44 * @param dest_size The size of the buffer, in bytes.
45 * @param src The wide string to convert.
46 * @return The length of the UTF-8 string, in bytes (not counting the NULL terminator), or < 0 for failure
48 int git__utf16_to_8(char *dest
, size_t dest_size
, const wchar_t *src
)
52 /* Length of -1 indicates NULL termination of the input string. Subtract 1 from the result to
53 * turn 0 into -1 (an error code) and to not count the NULL terminator as part of the string's
54 * length. WideCharToMultiByte never returns int's minvalue, so underflow is not possible */
55 if ((len
= WideCharToMultiByte(CP_UTF8
, WC_ERR_INVALID_CHARS
, src
, -1, dest
, (int)dest_size
, NULL
, NULL
) - 1) < 0)
62 * Converts a UTF-8 string to wide characters.
63 * Memory is allocated to hold the converted string.
64 * The caller is responsible for freeing the string with git__free.
66 * @param dest Receives a pointer to the wide string.
67 * @param src The UTF-8 string to convert.
68 * @return The length of the wide string, in characters (not counting the NULL terminator), or < 0 for failure
70 int git__utf8_to_16_alloc(wchar_t **dest
, const char *src
)
76 /* Length of -1 indicates NULL termination of the input string */
77 utf16_size
= MultiByteToWideChar(CP_UTF8
, MB_ERR_INVALID_CHARS
, src
, -1, NULL
, 0);
84 if (!(*dest
= git__mallocarray(utf16_size
, sizeof(wchar_t)))) {
89 utf16_size
= MultiByteToWideChar(CP_UTF8
, MB_ERR_INVALID_CHARS
, src
, -1, *dest
, utf16_size
);
98 /* Subtract 1 from the result to turn 0 into -1 (an error code) and to not count the NULL
99 * terminator as part of the string's length. MultiByteToWideChar never returns int's minvalue,
100 * so underflow is not possible */
101 return utf16_size
- 1;
105 * Converts a wide string to UTF-8.
106 * Memory is allocated to hold the converted string.
107 * The caller is responsible for freeing the string with git__free.
109 * @param dest Receives a pointer to the UTF-8 string.
110 * @param src The wide string to convert.
111 * @return The length of the UTF-8 string, in bytes (not counting the NULL terminator), or < 0 for failure
113 int git__utf16_to_8_alloc(char **dest
, const wchar_t *src
)
119 /* Length of -1 indicates NULL termination of the input string */
120 utf8_size
= WideCharToMultiByte(CP_UTF8
, WC_ERR_INVALID_CHARS
, src
, -1, NULL
, 0, NULL
, NULL
);
127 *dest
= git__malloc(utf8_size
);
134 utf8_size
= WideCharToMultiByte(CP_UTF8
, WC_ERR_INVALID_CHARS
, src
, -1, *dest
, utf8_size
, NULL
, NULL
);
143 /* Subtract 1 from the result to turn 0 into -1 (an error code) and to not count the NULL
144 * terminator as part of the string's length. MultiByteToWideChar never returns int's minvalue,
145 * so underflow is not possible */
146 return utf8_size
- 1;