Blame src/win32/w32_buffer.c

Packit Service 20376f
/*
Packit Service 20376f
 * Copyright (C) the libgit2 contributors. All rights reserved.
Packit Service 20376f
 *
Packit Service 20376f
 * This file is part of libgit2, distributed under the GNU GPL v2 with
Packit Service 20376f
 * a Linking Exception. For full terms see the included COPYING file.
Packit Service 20376f
 */
Packit Service 20376f
Packit Service 20376f
#include "common.h"
Packit Service 20376f
#include "w32_buffer.h"
Packit Service 20376f
#include "../buffer.h"
Packit Service 20376f
#include "utf-conv.h"
Packit Service 20376f
Packit Service 20376f
GIT_INLINE(int) handle_wc_error(void)
Packit Service 20376f
{
Packit Service 20376f
	if (GetLastError() == ERROR_INSUFFICIENT_BUFFER)
Packit Service 20376f
		errno = ENAMETOOLONG;
Packit Service 20376f
	else
Packit Service 20376f
		errno = EINVAL;
Packit Service 20376f
Packit Service 20376f
	return -1;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
int git_buf_put_w(git_buf *buf, const wchar_t *string_w, size_t len_w)
Packit Service 20376f
{
Packit Service 20376f
	int utf8_len, utf8_write_len;
Packit Service 20376f
	size_t new_size;
Packit Service 20376f
Packit Service 20376f
	if (!len_w)
Packit Service 20376f
		return 0;
Packit Service 20376f
Packit Service 20376f
	assert(string_w);
Packit Service 20376f
Packit Service 20376f
	/* Measure the string necessary for conversion */
Packit Service 20376f
	if ((utf8_len = WideCharToMultiByte(CP_UTF8, WC_ERR_INVALID_CHARS, string_w, len_w, NULL, 0, NULL, NULL)) == 0)
Packit Service 20376f
		return 0;
Packit Service 20376f
Packit Service 20376f
	assert(utf8_len > 0);
Packit Service 20376f
Packit Service 20376f
	GITERR_CHECK_ALLOC_ADD(&new_size, buf->size, (size_t)utf8_len);
Packit Service 20376f
	GITERR_CHECK_ALLOC_ADD(&new_size, new_size, 1);
Packit Service 20376f
Packit Service 20376f
	if (git_buf_grow(buf, new_size) < 0)
Packit Service 20376f
		return -1;
Packit Service 20376f
Packit Service 20376f
	if ((utf8_write_len = WideCharToMultiByte(
Packit Service 20376f
			CP_UTF8, WC_ERR_INVALID_CHARS, string_w, len_w, &buf->ptr[buf->size], utf8_len, NULL, NULL)) == 0)
Packit Service 20376f
		return handle_wc_error();
Packit Service 20376f
Packit Service 20376f
	assert(utf8_write_len == utf8_len);
Packit Service 20376f
Packit Service 20376f
	buf->size += utf8_write_len;
Packit Service 20376f
	buf->ptr[buf->size] = '\0';
Packit Service 20376f
	return 0;
Packit Service 20376f
}