Commit 11d51ca63184b760e2537bbe08c5ca4c63bd4854

Vicent Marti 2011-10-26T16:43:55

windows: Add support for non-UTF codepages Our previous assumption that all paths in Windows are encoded in UTF-8 is rather weak, specially when considering that Git is encoding-agnostic. These set of functions allow the user to change the library's active codepage globally, so it is possible to access paths and files on all international versions of Windows. Note that the default encoding here is UTF-8 because we assume that 99% of all Git repositories will be in UTF-8. Also, if you use non-ascii characters in paths, anywhere, please burn on a fire.

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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
diff --git a/include/git2/windows.h b/include/git2/windows.h
new file mode 100644
index 0000000..6a2e9e2
--- /dev/null
+++ b/include/git2/windows.h
@@ -0,0 +1,59 @@
+/*
+ * 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.
+ */
+#ifndef INCLUDE_git_windows_h__
+#define INCLUDE_git_windows_h__
+
+#include "common.h"
+
+/**
+ * @file git2/windows.h
+ * @brief Windows-specific functions
+ * @ingroup Git
+ * @{
+ */
+GIT_BEGIN_DECL
+
+/**
+ * Set the active codepage for Windows syscalls
+ *
+ * All syscalls performed by the library will assume
+ * this codepage when converting paths and strings
+ * to use by the Windows kernel.
+ *
+ * The default value of UTF-8 will work automatically
+ * with most Git repositories created on Unix systems.
+ *
+ * This settings needs only be changed when working
+ * with repositories that contain paths in specific,
+ * non-UTF codepages.
+ *
+ * A full list of all available codepage identifiers may
+ * be found at:
+ *
+ * http://msdn.microsoft.com/en-us/library/windows/desktop/dd317756(v=vs.85).aspx
+ *
+ * @param codepage numeric codepage identifier
+ */
+GIT_EXTERN(void) gitwin_set_codepage(unsigned int codepage);
+
+/**
+ * Return the active codepage for Windows syscalls
+ *
+ * @return numeric codepage identifier
+ */
+GIT_EXTERN(unsigned int) gitwin_get_codepage(void);
+
+/**
+ * Set the active Windows codepage to UTF-8 (this is
+ * the default value)
+ */
+GIT_EXTERN(void) gitwin_set_utf8(void);
+
+/** @} */
+GIT_END_DECL
+#endif
+
diff --git a/src/config.c b/src/config.c
index f53afa1..ad6f885 100644
--- a/src/config.c
+++ b/src/config.c
@@ -370,7 +370,7 @@ static int win32_find_system(char *system_config_path)
 		return GIT_ENOTFOUND;
 	}
 
-	apphome_utf8 = conv_utf16_to_utf8(apphome_utf16);
+	apphome_utf8 = gitwin_from_utf16(apphome_utf16);
 	free(apphome_utf16);
 
 	if (strlen(apphome_utf8) >= GIT_PATH_MAX) {
diff --git a/src/win32/dir.c b/src/win32/dir.c
index ab50318..fea74b4 100644
--- a/src/win32/dir.c
+++ b/src/win32/dir.c
@@ -6,7 +6,8 @@
  */
 #define GIT__WIN32_NO_WRAP_DIR
 #include "dir.h"
-#include "utf8-conv.h"
+#include "utf-conv.h"
+#include "git2/windows.h"
 
 static int init_filter(char *filter, size_t n, const char *dir)
 {
@@ -43,7 +44,7 @@ git__DIR *git__opendir(const char *dir)
 	}
 	strcpy(new->dir, dir);
 
-	filter_w = conv_utf8_to_utf16(filter);
+	filter_w = gitwin_to_utf16(filter);
 	new->h = FindFirstFileW(filter_w, &new->f);
 	free(filter_w);
 
@@ -73,7 +74,7 @@ struct git__dirent *git__readdir(git__DIR *d)
 		return NULL;
 
 	d->entry.d_ino = 0;
-	WideCharToMultiByte(CP_UTF8, 0, d->f.cFileName, -1, d->entry.d_name, GIT_PATH_MAX, NULL, NULL);
+	WideCharToMultiByte(gitwin_get_codepage(), 0, d->f.cFileName, -1, d->entry.d_name, GIT_PATH_MAX, NULL, NULL);
 
 	return &d->entry;
 }
@@ -90,7 +91,7 @@ void git__rewinddir(git__DIR *d)
 		d->first = 0;
 
 		if (init_filter(filter, sizeof(filter), d->dir)) {
-			filter_w = conv_utf8_to_utf16(filter);
+			filter_w = gitwin_to_utf16(filter);
 			d->h = FindFirstFileW(filter_w, &d->f);
 			free(filter_w);
 
diff --git a/src/win32/posix.h b/src/win32/posix.h
index 442717e..3774e78 100644
--- a/src/win32/posix.h
+++ b/src/win32/posix.h
@@ -9,7 +9,7 @@
 
 #include "common.h"
 #include "fnmatch.h"
-#include "utf8-conv.h"
+#include "utf-conv.h"
 
 GIT_INLINE(int) p_link(const char *GIT_UNUSED(old), const char *GIT_UNUSED(new))
 {
@@ -21,7 +21,7 @@ GIT_INLINE(int) p_link(const char *GIT_UNUSED(old), const char *GIT_UNUSED(new))
 
 GIT_INLINE(int) p_mkdir(const char *path, int GIT_UNUSED(mode))
 {
-	wchar_t* buf = conv_utf8_to_utf16(path);
+	wchar_t* buf = gitwin_to_utf16(path);
 	int ret = _wmkdir(buf);
 
 	GIT_UNUSED_ARG(mode)
diff --git a/src/win32/posix_w32.c b/src/win32/posix_w32.c
index cc17cc7..a05aafc 100644
--- a/src/win32/posix_w32.c
+++ b/src/win32/posix_w32.c
@@ -6,7 +6,7 @@
  */
 #include "posix.h"
 #include "path.h"
-#include "utf8-conv.h"
+#include "utf-conv.h"
 #include <errno.h>
 #include <io.h>
 #include <fcntl.h>
@@ -17,7 +17,7 @@ int p_unlink(const char *path)
 	int ret = 0;
 	wchar_t* buf;
 
-	buf = conv_utf8_to_utf16(path);
+	buf = gitwin_to_utf16(path);
 	_wchmod(buf, 0666);
 	ret = _wunlink(buf);
 	free(buf);
@@ -59,7 +59,7 @@ GIT_INLINE(time_t) filetime_to_time_t(const FILETIME *ft)
 static int do_lstat(const char *file_name, struct stat *buf)
 {
 	WIN32_FILE_ATTRIBUTE_DATA fdata;
-	wchar_t* fbuf = conv_utf8_to_utf16(file_name);
+	wchar_t* fbuf = gitwin_to_utf16(file_name);
 
 	if (GetFileAttributesExW(fbuf, GetFileExInfoStandard, &fdata)) {
 		int fMode = S_IREAD;
@@ -161,7 +161,7 @@ int p_readlink(const char *link, char *target, size_t target_len)
 				"'GetFinalPathNameByHandleW' is not available in this platform");
 	}
 
-	link_w = conv_utf8_to_utf16(link);
+	link_w = gitwin_to_utf16(link);
 
 	hFile = CreateFileW(link_w,			// file to open
 			GENERIC_READ,			// open for reading
@@ -223,7 +223,7 @@ int p_readlink(const char *link, char *target, size_t target_len)
 int p_open(const char *path, int flags)
 {
 	int fd;
-	wchar_t* buf = conv_utf8_to_utf16(path);
+	wchar_t* buf = gitwin_to_utf16(path);
 	fd = _wopen(buf, flags | _O_BINARY);
 
 	free(buf);
@@ -233,7 +233,7 @@ int p_open(const char *path, int flags)
 int p_creat(const char *path, int mode)
 {
 	int fd;
-	wchar_t* buf = conv_utf8_to_utf16(path);
+	wchar_t* buf = gitwin_to_utf16(path);
 	fd = _wopen(buf, _O_WRONLY | _O_CREAT | _O_TRUNC | _O_BINARY, mode);
 
 	free(buf);
@@ -261,7 +261,7 @@ int p_stat(const char* path, struct stat* buf)
 
 int p_chdir(const char* path)
 {
-	wchar_t* buf = conv_utf8_to_utf16(path);
+	wchar_t* buf = gitwin_to_utf16(path);
 	int ret = _wchdir(buf);
 
 	free(buf);
@@ -270,7 +270,7 @@ int p_chdir(const char* path)
 
 int p_chmod(const char* path, int mode)
 {
-	wchar_t* buf = conv_utf8_to_utf16(path);
+	wchar_t* buf = gitwin_to_utf16(path);
 	int ret = _wchmod(buf, mode);
 
 	free(buf);
@@ -279,7 +279,7 @@ int p_chmod(const char* path, int mode)
 
 int p_rmdir(const char* path)
 {
-	wchar_t* buf = conv_utf8_to_utf16(path);
+	wchar_t* buf = gitwin_to_utf16(path);
 	int ret = _wrmdir(buf);
 
 	free(buf);
@@ -289,7 +289,7 @@ int p_rmdir(const char* path)
 int p_hide_directory__w32(const char *path)
 {
 	int error;
-	wchar_t* buf = conv_utf8_to_utf16(path);
+	wchar_t* buf = gitwin_to_utf16(path);
 
 	error = SetFileAttributesW(buf, FILE_ATTRIBUTE_HIDDEN) != 0 ?
 		GIT_SUCCESS : GIT_ERROR; /* MSDN states a "non zero" value indicates a success */
@@ -305,7 +305,7 @@ int p_hide_directory__w32(const char *path)
 char *p_realpath(const char *orig_path, char *buffer)
 {
 	int ret, alloc = 0;
-	wchar_t* orig_path_w = conv_utf8_to_utf16(orig_path);
+	wchar_t* orig_path_w = gitwin_to_utf16(orig_path);
 	wchar_t* buffer_w = (wchar_t*)git__malloc(GIT_PATH_MAX * sizeof(wchar_t));
 
 	if (buffer == NULL) {
@@ -380,7 +380,7 @@ int p_setenv(const char* name, const char* value, int overwrite)
 
 int p_access(const char* path, int mode)
 {
-	wchar_t *buf = conv_utf8_to_utf16(path);
+	wchar_t *buf = gitwin_to_utf16(path);
 	int ret;
 
 	ret = _waccess(buf, mode);
diff --git a/src/win32/utf-conv.c b/src/win32/utf-conv.c
new file mode 100644
index 0000000..cb60783
--- /dev/null
+++ b/src/win32/utf-conv.c
@@ -0,0 +1,88 @@
+/*
+ * 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 "utf-conv.h"
+
+/*
+ * Default codepage value
+ */
+static int _active_codepage = CP_UTF8;
+
+void gitwin_set_codepage(unsigned int codepage)
+{
+	_active_codepage = codepage;
+}
+
+unsigned int gitwin_get_codepage(void)
+{
+	return _active_codepage;
+}
+
+void gitwin_set_utf8(void)
+{
+	_active_codepage = CP_UTF8;
+}
+
+wchar_t* gitwin_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(_active_codepage, 0, str, -1, ret, cb) == 0) {
+		free(ret);
+		ret = NULL;
+	}
+
+	return ret;
+}
+
+char* gitwin_from_utf16(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(_active_codepage, 0, str, -1, ret, cb, NULL, NULL) == 0) {
+		free(ret);
+		ret = NULL;
+	}
+
+	return ret;
+
+}
diff --git a/src/win32/utf-conv.h b/src/win32/utf-conv.h
new file mode 100644
index 0000000..da03e33
--- /dev/null
+++ b/src/win32/utf-conv.h
@@ -0,0 +1,17 @@
+/*
+ * 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 <wchar.h>
+
+#ifndef INCLUDE_git_utfconv_h__
+#define INCLUDE_git_utfconv_h__
+
+wchar_t* gitwin_to_utf16(const char* str);
+char* gitwin_from_utf16(const wchar_t* str);
+
+#endif
+
diff --git a/src/win32/utf8-conv.c b/src/win32/utf8-conv.c
deleted file mode 100644
index dec6f8e..0000000
--- a/src/win32/utf8-conv.c
+++ /dev/null
@@ -1,68 +0,0 @@
-/*
- * 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;
-
-}
diff --git a/src/win32/utf8-conv.h b/src/win32/utf8-conv.h
deleted file mode 100644
index 1967ac3..0000000
--- a/src/win32/utf8-conv.h
+++ /dev/null
@@ -1,17 +0,0 @@
-/*
- * 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 <wchar.h>
-
-#ifndef INCLUDE_git_utf8conv_h__
-#define INCLUDE_git_utf8conv_h__
-
-wchar_t* conv_utf8_to_utf16(const char* str);
-char* conv_utf16_to_utf8(const wchar_t* str);
-
-#endif
-