]> git.proxmox.com Git - libgit2.git/blob - src/win32/utf-conv.c
Merge pull request #1075 from carlosmn/alternates-recurse
[libgit2.git] / src / win32 / utf-conv.c
1 /*
2 * Copyright (C) 2009-2012 the libgit2 contributors
3 *
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.
6 */
7
8 #include "common.h"
9 #include "utf-conv.h"
10
11 #define U16_LEAD(c) (wchar_t)(((c)>>10)+0xd7c0)
12 #define U16_TRAIL(c) (wchar_t)(((c)&0x3ff)|0xdc00)
13
14 #if 0
15 void git__utf8_to_16(wchar_t *dest, size_t length, const char *src)
16 {
17 wchar_t *pDest = dest;
18 uint32_t ch;
19 const uint8_t* pSrc = (uint8_t*) src;
20
21 assert(dest && src && length);
22
23 length--;
24
25 while(*pSrc && length > 0) {
26 ch = *pSrc++;
27 length--;
28
29 if(ch < 0xc0) {
30 /*
31 * ASCII, or a trail byte in lead position which is treated like
32 * a single-byte sequence for better character boundary
33 * resynchronization after illegal sequences.
34 */
35 *pDest++ = (wchar_t)ch;
36 continue;
37 } else if(ch < 0xe0) { /* U+0080..U+07FF */
38 if (pSrc[0]) {
39 /* 0x3080 = (0xc0 << 6) + 0x80 */
40 *pDest++ = (wchar_t)((ch << 6) + *pSrc++ - 0x3080);
41 continue;
42 }
43 } else if(ch < 0xf0) { /* U+0800..U+FFFF */
44 if (pSrc[0] && pSrc[1]) {
45 /* no need for (ch & 0xf) because the upper bits are truncated after <<12 in the cast to (UChar) */
46 /* 0x2080 = (0x80 << 6) + 0x80 */
47 ch = (ch << 12) + (*pSrc++ << 6);
48 *pDest++ = (wchar_t)(ch + *pSrc++ - 0x2080);
49 continue;
50 }
51 } else /* f0..f4 */ { /* U+10000..U+10FFFF */
52 if (length >= 1 && pSrc[0] && pSrc[1] && pSrc[2]) {
53 /* 0x3c82080 = (0xf0 << 18) + (0x80 << 12) + (0x80 << 6) + 0x80 */
54 ch = (ch << 18) + (*pSrc++ << 12);
55 ch += *pSrc++ << 6;
56 ch += *pSrc++ - 0x3c82080;
57 *(pDest++) = U16_LEAD(ch);
58 *(pDest++) = U16_TRAIL(ch);
59 length--; /* two bytes for this character */
60 continue;
61 }
62 }
63
64 /* truncated character at the end */
65 *pDest++ = 0xfffd;
66 break;
67 }
68
69 *pDest++ = 0x0;
70 }
71 #endif
72
73 int git__utf8_to_16(wchar_t *dest, size_t length, const char *src)
74 {
75 return MultiByteToWideChar(CP_UTF8, 0, src, -1, dest, (int)length);
76 }
77
78 int git__utf16_to_8(char *out, const wchar_t *input)
79 {
80 return WideCharToMultiByte(CP_UTF8, 0, input, -1, out, GIT_WIN_PATH, NULL, NULL);
81 }