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.
10 GIT_INLINE(void) git__set_errno(void)
12 if (GetLastError() == ERROR_INSUFFICIENT_BUFFER
)
19 * Converts a UTF-8 string to wide characters.
21 * @param dest The buffer to receive the wide string.
22 * @param dest_size The size of the buffer, in characters.
23 * @param src The UTF-8 string to convert.
24 * @return The length of the wide string, in characters (not counting the NULL terminator), or < 0 for failure
26 int git__utf8_to_16(wchar_t *dest
, size_t dest_size
, const char *src
)
30 /* Length of -1 indicates NULL termination of the input string. Subtract 1 from the result to
31 * turn 0 into -1 (an error code) and to not count the NULL terminator as part of the string's
32 * length. MultiByteToWideChar never returns int's minvalue, so underflow is not possible */
33 if ((len
= MultiByteToWideChar(CP_UTF8
, MB_ERR_INVALID_CHARS
, src
, -1, dest
, (int)dest_size
) - 1) < 0)
40 * Converts a wide string to UTF-8.
42 * @param dest The buffer to receive the UTF-8 string.
43 * @param dest_size The size of the buffer, in bytes.
44 * @param src The wide string to convert.
45 * @return The length of the UTF-8 string, in bytes (not counting the NULL terminator), or < 0 for failure
47 int git__utf16_to_8(char *dest
, size_t dest_size
, const wchar_t *src
)
51 /* Length of -1 indicates NULL termination of the input string. Subtract 1 from the result to
52 * turn 0 into -1 (an error code) and to not count the NULL terminator as part of the string's
53 * length. WideCharToMultiByte never returns int's minvalue, so underflow is not possible */
54 if ((len
= WideCharToMultiByte(CP_UTF8
, WC_ERR_INVALID_CHARS
, src
, -1, dest
, (int)dest_size
, NULL
, NULL
) - 1) < 0)
61 * Converts a UTF-8 string to wide characters.
62 * Memory is allocated to hold the converted string.
63 * The caller is responsible for freeing the string with git__free.
65 * @param dest Receives a pointer to the wide string.
66 * @param src The UTF-8 string to convert.
67 * @return The length of the wide string, in characters (not counting the NULL terminator), or < 0 for failure
69 int git__utf8_to_16_alloc(wchar_t **dest
, const char *src
)
75 /* Length of -1 indicates NULL termination of the input string */
76 utf16_size
= MultiByteToWideChar(CP_UTF8
, MB_ERR_INVALID_CHARS
, src
, -1, NULL
, 0);
83 if (!(*dest
= git__mallocarray(utf16_size
, sizeof(wchar_t)))) {
88 utf16_size
= MultiByteToWideChar(CP_UTF8
, MB_ERR_INVALID_CHARS
, src
, -1, *dest
, utf16_size
);
97 /* Subtract 1 from the result to turn 0 into -1 (an error code) and to not count the NULL
98 * terminator as part of the string's length. MultiByteToWideChar never returns int's minvalue,
99 * so underflow is not possible */
100 return utf16_size
- 1;
104 * Converts a wide string to UTF-8.
105 * Memory is allocated to hold the converted string.
106 * The caller is responsible for freeing the string with git__free.
108 * @param dest Receives a pointer to the UTF-8 string.
109 * @param src The wide string to convert.
110 * @return The length of the UTF-8 string, in bytes (not counting the NULL terminator), or < 0 for failure
112 int git__utf16_to_8_alloc(char **dest
, const wchar_t *src
)
118 /* Length of -1 indicates NULL termination of the input string */
119 utf8_size
= WideCharToMultiByte(CP_UTF8
, WC_ERR_INVALID_CHARS
, src
, -1, NULL
, 0, NULL
, NULL
);
126 *dest
= git__malloc(utf8_size
);
133 utf8_size
= WideCharToMultiByte(CP_UTF8
, WC_ERR_INVALID_CHARS
, src
, -1, *dest
, utf8_size
, NULL
, NULL
);
142 /* Subtract 1 from the result to turn 0 into -1 (an error code) and to not count the NULL
143 * terminator as part of the string's length. MultiByteToWideChar never returns int's minvalue,
144 * so underflow is not possible */
145 return utf8_size
- 1;