Blame src/win32/utf-conv.c

Packit ae9e2a
/*
Packit ae9e2a
 * Copyright (C) the libgit2 contributors. All rights reserved.
Packit ae9e2a
 *
Packit ae9e2a
 * This file is part of libgit2, distributed under the GNU GPL v2 with
Packit ae9e2a
 * a Linking Exception. For full terms see the included COPYING file.
Packit ae9e2a
 */
Packit ae9e2a
Packit ae9e2a
#include "common.h"
Packit ae9e2a
#include "utf-conv.h"
Packit ae9e2a
Packit ae9e2a
GIT_INLINE(void) git__set_errno(void)
Packit ae9e2a
{
Packit ae9e2a
	if (GetLastError() == ERROR_INSUFFICIENT_BUFFER)
Packit ae9e2a
		errno = ENAMETOOLONG;
Packit ae9e2a
	else
Packit ae9e2a
		errno = EINVAL;
Packit ae9e2a
}
Packit ae9e2a
Packit ae9e2a
/**
Packit ae9e2a
 * Converts a UTF-8 string to wide characters.
Packit ae9e2a
 *
Packit ae9e2a
 * @param dest The buffer to receive the wide string.
Packit ae9e2a
 * @param dest_size The size of the buffer, in characters.
Packit ae9e2a
 * @param src The UTF-8 string to convert.
Packit ae9e2a
 * @return The length of the wide string, in characters (not counting the NULL terminator), or < 0 for failure
Packit ae9e2a
 */
Packit ae9e2a
int git__utf8_to_16(wchar_t *dest, size_t dest_size, const char *src)
Packit ae9e2a
{
Packit ae9e2a
	int len;
Packit ae9e2a
Packit ae9e2a
	/* Length of -1 indicates NULL termination of the input string. Subtract 1 from the result to
Packit ae9e2a
	* turn 0 into -1 (an error code) and to not count the NULL terminator as part of the string's
Packit ae9e2a
	* length. MultiByteToWideChar never returns int's minvalue, so underflow is not possible */
Packit ae9e2a
	if ((len = MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, src, -1, dest, (int)dest_size) - 1) < 0)
Packit ae9e2a
		git__set_errno();
Packit ae9e2a
Packit ae9e2a
	return len;
Packit ae9e2a
}
Packit ae9e2a
Packit ae9e2a
/**
Packit ae9e2a
 * Converts a wide string to UTF-8.
Packit ae9e2a
 *
Packit ae9e2a
 * @param dest The buffer to receive the UTF-8 string.
Packit ae9e2a
 * @param dest_size The size of the buffer, in bytes.
Packit ae9e2a
 * @param src The wide string to convert.
Packit ae9e2a
 * @return The length of the UTF-8 string, in bytes (not counting the NULL terminator), or < 0 for failure
Packit ae9e2a
 */
Packit ae9e2a
int git__utf16_to_8(char *dest, size_t dest_size, const wchar_t *src)
Packit ae9e2a
{
Packit ae9e2a
	int len;
Packit ae9e2a
Packit ae9e2a
	/* Length of -1 indicates NULL termination of the input string. Subtract 1 from the result to
Packit ae9e2a
	 * turn 0 into -1 (an error code) and to not count the NULL terminator as part of the string's
Packit ae9e2a
	 * length. WideCharToMultiByte never returns int's minvalue, so underflow is not possible */
Packit ae9e2a
	if ((len = WideCharToMultiByte(CP_UTF8, WC_ERR_INVALID_CHARS, src, -1, dest, (int)dest_size, NULL, NULL) - 1) < 0)
Packit ae9e2a
		git__set_errno();
Packit ae9e2a
Packit ae9e2a
	return len;
Packit ae9e2a
}
Packit ae9e2a
Packit ae9e2a
/**
Packit ae9e2a
 * Converts a UTF-8 string to wide characters.
Packit ae9e2a
 * Memory is allocated to hold the converted string.
Packit ae9e2a
 * The caller is responsible for freeing the string with git__free.
Packit ae9e2a
 *
Packit ae9e2a
 * @param dest Receives a pointer to the wide string.
Packit ae9e2a
 * @param src The UTF-8 string to convert.
Packit ae9e2a
 * @return The length of the wide string, in characters (not counting the NULL terminator), or < 0 for failure
Packit ae9e2a
 */
Packit ae9e2a
int git__utf8_to_16_alloc(wchar_t **dest, const char *src)
Packit ae9e2a
{
Packit ae9e2a
	int utf16_size;
Packit ae9e2a
Packit ae9e2a
	*dest = NULL;
Packit ae9e2a
Packit ae9e2a
	/* Length of -1 indicates NULL termination of the input string */
Packit ae9e2a
	utf16_size = MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, src, -1, NULL, 0);
Packit ae9e2a
Packit ae9e2a
	if (!utf16_size) {
Packit ae9e2a
		git__set_errno();
Packit ae9e2a
		return -1;
Packit ae9e2a
	}
Packit ae9e2a
Packit ae9e2a
	if (!(*dest = git__mallocarray(utf16_size, sizeof(wchar_t)))) {
Packit ae9e2a
		errno = ENOMEM;
Packit ae9e2a
		return -1;
Packit ae9e2a
	}
Packit ae9e2a
Packit ae9e2a
	utf16_size = MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, src, -1, *dest, utf16_size);
Packit ae9e2a
Packit ae9e2a
	if (!utf16_size) {
Packit ae9e2a
		git__set_errno();
Packit ae9e2a
Packit ae9e2a
		git__free(*dest);
Packit ae9e2a
		*dest = NULL;
Packit ae9e2a
	}
Packit ae9e2a
Packit ae9e2a
	/* Subtract 1 from the result to turn 0 into -1 (an error code) and to not count the NULL
Packit ae9e2a
	 * terminator as part of the string's length. MultiByteToWideChar never returns int's minvalue,
Packit ae9e2a
	 * so underflow is not possible */
Packit ae9e2a
	return utf16_size - 1;
Packit ae9e2a
}
Packit ae9e2a
Packit ae9e2a
/**
Packit ae9e2a
 * Converts a wide string to UTF-8.
Packit ae9e2a
 * Memory is allocated to hold the converted string.
Packit ae9e2a
 * The caller is responsible for freeing the string with git__free.
Packit ae9e2a
 *
Packit ae9e2a
 * @param dest Receives a pointer to the UTF-8 string.
Packit ae9e2a
 * @param src The wide string to convert.
Packit ae9e2a
 * @return The length of the UTF-8 string, in bytes (not counting the NULL terminator), or < 0 for failure
Packit ae9e2a
 */
Packit ae9e2a
int git__utf16_to_8_alloc(char **dest, const wchar_t *src)
Packit ae9e2a
{
Packit ae9e2a
	int utf8_size;
Packit ae9e2a
Packit ae9e2a
	*dest = NULL;
Packit ae9e2a
Packit ae9e2a
	/* Length of -1 indicates NULL termination of the input string */
Packit ae9e2a
	utf8_size = WideCharToMultiByte(CP_UTF8, WC_ERR_INVALID_CHARS, src, -1, NULL, 0, NULL, NULL);
Packit ae9e2a
Packit ae9e2a
	if (!utf8_size) {
Packit ae9e2a
		git__set_errno();
Packit ae9e2a
		return -1;
Packit ae9e2a
	}
Packit ae9e2a
Packit ae9e2a
	*dest = git__malloc(utf8_size);
Packit ae9e2a
Packit ae9e2a
	if (!*dest) {
Packit ae9e2a
		errno = ENOMEM;
Packit ae9e2a
		return -1;
Packit ae9e2a
	}
Packit ae9e2a
Packit ae9e2a
	utf8_size = WideCharToMultiByte(CP_UTF8, WC_ERR_INVALID_CHARS, src, -1, *dest, utf8_size, NULL, NULL);
Packit ae9e2a
Packit ae9e2a
	if (!utf8_size) {
Packit ae9e2a
		git__set_errno();
Packit ae9e2a
Packit ae9e2a
		git__free(*dest);
Packit ae9e2a
		*dest = NULL;
Packit ae9e2a
	}
Packit ae9e2a
Packit ae9e2a
	/* Subtract 1 from the result to turn 0 into -1 (an error code) and to not count the NULL
Packit ae9e2a
	 * terminator as part of the string's length. MultiByteToWideChar never returns int's minvalue,
Packit ae9e2a
	 * so underflow is not possible */
Packit ae9e2a
	return utf8_size - 1;
Packit ae9e2a
}