summaryrefslogtreecommitdiff
path: root/src/win32/utf8-conv.c
blob: dec6f8e79dc978899cc0e5660ecbd84cda0984f4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
/*
 * Copyright (C) 2009-2011 the libgit2 contributors
 *
 * This file is part of libgit2, distributed under the GNU GPL v2 with
 * a Linking Exception. For full terms see the included COPYING file.
 */

#include "common.h"
#include "utf8-conv.h"

wchar_t* conv_utf8_to_utf16(const char* str)
{
	wchar_t* ret;
	int cb;

	if (!str) {
		return NULL;
	}

	cb = strlen(str) * sizeof(wchar_t);
	if (cb == 0) {
		ret = (wchar_t*)git__malloc(sizeof(wchar_t));
		ret[0] = 0;
		return ret;
	}

	/* Add space for null terminator */
	cb += sizeof(wchar_t);

	ret = (wchar_t*)git__malloc(cb);

	if (MultiByteToWideChar(CP_UTF8, 0, str, -1, ret, cb) == 0) {
		free(ret);
		ret = NULL;
	}

	return ret;
}

char* conv_utf16_to_utf8(const wchar_t* str)
{
	char* ret;
	int cb;

	if (!str) {
		return NULL;
	}

	cb = wcslen(str) * sizeof(char);
	if (cb == 0) {
		ret = (char*)git__malloc(sizeof(char));
		ret[0] = 0;
		return ret;
	}

	/* Add space for null terminator */
	cb += sizeof(char);

	ret = (char*)git__malloc(cb);

	if (WideCharToMultiByte(CP_UTF8, 0, str, -1, ret, cb, NULL, NULL) == 0) {
		free(ret);
		ret = NULL;
	}

	return ret;

}