]>
git.proxmox.com Git - libgit2.git/blob - src/win32/utf-conv.c
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 #ifndef WC_ERR_INVALID_CHARS
12 # define WC_ERR_INVALID_CHARS 0x80
15 GIT_INLINE(DWORD
) get_wc_flags(void)
17 static char inited
= 0;
20 /* Invalid code point check supported on Vista+ only */
22 flags
= git_has_win32_version(6, 0, 0) ? WC_ERR_INVALID_CHARS
: 0;
30 * Converts a UTF-8 string to wide characters.
32 * @param dest The buffer to receive the wide string.
33 * @param dest_size The size of the buffer, in characters.
34 * @param src The UTF-8 string to convert.
35 * @return The length of the wide string, in characters (not counting the NULL terminator), or < 0 for failure
37 int git__utf8_to_16(wchar_t *dest
, size_t dest_size
, const char *src
)
39 /* Length of -1 indicates NULL termination of the input string. Subtract 1 from the result to
40 * turn 0 into -1 (an error code) and to not count the NULL terminator as part of the string's
41 * length. MultiByteToWideChar never returns int's minvalue, so underflow is not possible */
42 return MultiByteToWideChar(CP_UTF8
, MB_ERR_INVALID_CHARS
, src
, -1, dest
, (int)dest_size
) - 1;
46 * Converts a wide string to UTF-8.
48 * @param dest The buffer to receive the UTF-8 string.
49 * @param dest_size The size of the buffer, in bytes.
50 * @param src The wide string to convert.
51 * @return The length of the UTF-8 string, in bytes (not counting the NULL terminator), or < 0 for failure
53 int git__utf16_to_8(char *dest
, size_t dest_size
, const wchar_t *src
)
55 /* Length of -1 indicates NULL termination of the input string. Subtract 1 from the result to
56 * turn 0 into -1 (an error code) and to not count the NULL terminator as part of the string's
57 * length. WideCharToMultiByte never returns int's minvalue, so underflow is not possible */
58 return WideCharToMultiByte(CP_UTF8
, get_wc_flags(), src
, -1, dest
, (int)dest_size
, NULL
, NULL
) - 1;
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);
82 *dest
= git__malloc(utf16_size
* sizeof(wchar_t));
87 utf16_size
= MultiByteToWideChar(CP_UTF8
, MB_ERR_INVALID_CHARS
, src
, -1, *dest
, utf16_size
);
94 /* Subtract 1 from the result to turn 0 into -1 (an error code) and to not count the NULL
95 * terminator as part of the string's length. MultiByteToWideChar never returns int's minvalue,
96 * so underflow is not possible */
97 return utf16_size
- 1;
101 * Converts a wide string to UTF-8.
102 * Memory is allocated to hold the converted string.
103 * The caller is responsible for freeing the string with git__free.
105 * @param dest Receives a pointer to the UTF-8 string.
106 * @param src The wide string to convert.
107 * @return The length of the UTF-8 string, in bytes (not counting the NULL terminator), or < 0 for failure
109 int git__utf16_to_8_alloc(char **dest
, const wchar_t *src
)
112 DWORD dwFlags
= get_wc_flags();
116 /* Length of -1 indicates NULL termination of the input string */
117 utf8_size
= WideCharToMultiByte(CP_UTF8
, dwFlags
, src
, -1, NULL
, 0, NULL
, NULL
);
122 *dest
= git__malloc(utf8_size
);
127 utf8_size
= WideCharToMultiByte(CP_UTF8
, dwFlags
, src
, -1, *dest
, utf8_size
, NULL
, NULL
);
134 /* Subtract 1 from the result to turn 0 into -1 (an error code) and to not count the NULL
135 * terminator as part of the string's length. MultiByteToWideChar never returns int's minvalue,
136 * so underflow is not possible */
137 return utf8_size
- 1;