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.
8 #include "w32_buffer.h"
12 GIT_INLINE(int) handle_wc_error(void)
14 if (GetLastError() == ERROR_INSUFFICIENT_BUFFER
)
22 int git_str_put_w(git_str
*buf
, const wchar_t *string_w
, size_t len_w
)
24 int utf8_len
, utf8_write_len
;
29 } else if (len_w
> INT_MAX
) {
36 /* Measure the string necessary for conversion */
37 if ((utf8_len
= WideCharToMultiByte(CP_UTF8
, WC_ERR_INVALID_CHARS
, string_w
, (int)len_w
, NULL
, 0, NULL
, NULL
)) == 0)
40 GIT_ASSERT(utf8_len
> 0);
42 GIT_ERROR_CHECK_ALLOC_ADD(&new_size
, buf
->size
, (size_t)utf8_len
);
43 GIT_ERROR_CHECK_ALLOC_ADD(&new_size
, new_size
, 1);
45 if (git_str_grow(buf
, new_size
) < 0)
48 if ((utf8_write_len
= WideCharToMultiByte(
49 CP_UTF8
, WC_ERR_INVALID_CHARS
, string_w
, (int)len_w
, &buf
->ptr
[buf
->size
], utf8_len
, NULL
, NULL
)) == 0)
50 return handle_wc_error();
52 GIT_ASSERT(utf8_write_len
== utf8_len
);
54 buf
->size
+= utf8_write_len
;
55 buf
->ptr
[buf
->size
] = '\0';