-
Notifications
You must be signed in to change notification settings - Fork 3
/
unicode-c.c
539 lines (464 loc) · 14.1 KB
/
unicode-c.c
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
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
#include <caml/mlvalues.h>
#include <caml/alloc.h>
#include <caml/memory.h>
#include <caml/unixsupport.h>
// DELETEME
#include <caml/custom.h>
//#include "ptr.h"
#include <stdio.h>
// ONLY IF IT'S WINDOWS!
#if defined(__WIN32) || defined(_WIN32) || defined(__WIN32__)
#ifndef WINVER
#define WINVER 0x0501
#define _WIN32_WINNT 0x0501
#endif
#include <Windows.h>
#include <WinIoCtl.h>
#include <KtmW32.h>
#include <shellapi.h>
#include <fcntl.h>
#define win_bad(out,bad) {\
(out) = caml_alloc(1, 1);\
Store_field((out), 0, Val_int(bad));\
}
#define win_good(out,good) {\
(out) = caml_alloc(1, 0);\
Store_field((out), 0, (good));\
}
#define win_error(out,good,bad) {\
if((bad) == 0) win_good(out,good)\
else win_bad(out,bad)\
}
CAMLprim value uni_is_win() {
return(Val_int(1));
}
/***********/
/* UNICODE */
/***********/
CAMLprim void uni_set_utf8_output() {
SetConsoleOutputCP(CP_UTF8);
}
CAMLprim void uni_silly_print(value s) {
CAMLparam1(s);
wprintf(L"%S", String_val(s));
fflush(stdout);
CAMLreturn0;
}
// Convert a UTF8 string to a UTF16 string
// Optionally save the trailing null
CAMLprim value uni_utf16_of_utf8(value utf8_val, value include_null) {
CAMLparam2(utf8_val, include_null);
int err = 0;
// Every OCaml string is null-terminated, so it's safe to add 1 to the length
long utf8_len = Bool_val(include_null) ? string_length(utf8_val) + 1 : string_length(utf8_val);
CAMLlocal2(unicode_val, out_val);
if(utf8_len == 0) {
unicode_val = caml_alloc_string(0);
} else {
int needed = MultiByteToWideChar(CP_UTF8, 0, String_val(utf8_val), utf8_len, NULL, 0);
if(needed == 0) {
err = GetLastError();
} else {
int n2;
unicode_val = caml_alloc_string(sizeof(WCHAR) * needed);
n2 = MultiByteToWideChar(CP_UTF8, 0, String_val(utf8_val), utf8_len, (LPWSTR)String_val(unicode_val), needed);
if(n2 == 0) {
err = GetLastError();
}
}
}
win_error(out_val, unicode_val, err);
CAMLreturn(out_val);
}
CAMLprim value uni_utf8_of_utf16(value byte_len_val, value unicode_val) {
CAMLparam2(byte_len_val, unicode_val);
int err = 0;
int len = Int_val(byte_len_val) / sizeof(WCHAR);
CAMLlocal2(utf8_val, out_val);
if(string_length(unicode_val) < sizeof(WCHAR)) {
utf8_val = caml_alloc_string(0);
} else {
int needed = WideCharToMultiByte(CP_UTF8, 0, (LPCWSTR)String_val(unicode_val), len, NULL, 0, NULL, NULL);
if(needed == 0) {
err = GetLastError();
} else {
int n2;
utf8_val = caml_alloc_string(needed);
n2 = WideCharToMultiByte(CP_UTF8, 0, (LPCWSTR)String_val(unicode_val), len, String_val(utf8_val), needed, NULL, NULL);
if(n2 == 0) {
err = GetLastError();
}
}
}
win_error(out_val, utf8_val, err);
CAMLreturn(out_val);
}
CAMLprim value uni_active_of_utf16(value byte_len_val, value unicode_val) {
CAMLparam2(byte_len_val, unicode_val);
int err = 0;
int len = Int_val(byte_len_val) / sizeof(WCHAR);
CAMLlocal2(utf8_val, out_val);
printf("ACP: %d\n", GetACP());
if(string_length(unicode_val) < sizeof(WCHAR)) {
utf8_val = caml_alloc_string(0);
} else {
int needed = WideCharToMultiByte(GetACP(), 0, (LPCWSTR)String_val(unicode_val), len, NULL, 0, NULL, NULL);
if(needed == 0) {
err = GetLastError();
} else {
int n2;
utf8_val = caml_alloc_string(needed);
n2 = WideCharToMultiByte(GetACP(), 0, (LPCWSTR)String_val(unicode_val), len, String_val(utf8_val), needed, NULL, NULL);
if(n2 == 0) {
err = GetLastError();
}
}
}
win_error(out_val, utf8_val, err);
CAMLreturn(out_val);
}
CAMLprim value uni_utf8_of_utf16_and_length(value unicode_val, value len_val) {
CAMLparam2(unicode_val, len_val);
int err = 0;
int len_in_wchars = Int_val(len_val);
CAMLlocal2(utf8_val, out_val);
if(len_in_wchars < 1) {
utf8_val = caml_alloc_string(0);
} else {
int needed = WideCharToMultiByte(CP_UTF8, 0, (LPCWSTR)String_val(unicode_val), len_in_wchars, NULL, 0, NULL, NULL);
if(needed == 0) {
err = GetLastError();
} else {
int n2;
utf8_val = caml_alloc_string(needed);
n2 = WideCharToMultiByte(CP_UTF8, 0, (LPCWSTR)String_val(unicode_val), len_in_wchars, String_val(utf8_val), needed, NULL, NULL);
if(n2 == 0) {
err = GetLastError();
}
}
}
win_error(out_val, utf8_val, err);
CAMLreturn(out_val);
}
CAMLprim value uni_get_utf16_command_line() {
CAMLparam0();
CAMLlocal2(str_val, out_val);
int err = 0;
LPWSTR com_str = GetCommandLineW();
int needed = sizeof(WCHAR) * wcslen(com_str);
if(needed == 0) {
err = GetLastError();
} else {
str_val = caml_alloc_string(needed);
memcpy(String_val(str_val), com_str, needed);
}
win_error(out_val, str_val, err);
CAMLreturn(out_val);
}
CAMLprim value uni_get_utf8_argv() {
CAMLparam0();
CAMLlocal3(str_val, array_val, out_val);
int num_args;
int needed;
int err = 0;
LPWSTR com_str = GetCommandLineW();
LPWSTR *argv = CommandLineToArgvW(com_str, &num_args);
if(argv == NULL) {
err = GetLastError();
} else {
int i;
int needed;
LPWSTR str;
array_val = caml_alloc_tuple(num_args);
// printf("NUM ARGS: %d\n", num_args);
for(i = 0; i < num_args; i++) {
// printf("i = %d\n", i);
str = argv[i];
needed = WideCharToMultiByte(CP_UTF8, 0, str, -1, NULL, 0, NULL, NULL);
// printf("needed = %d\n", needed);
if(needed == 0) {
err = GetLastError();
break;
} else {
// WideCharToMultiByte with -1 returns the terminating null char as well
// Luckily OCaml allocates a terminating null after any string
str_val = caml_alloc_string(needed - 1);
needed = WideCharToMultiByte(CP_UTF8, 0, str, -1, String_val(str_val), needed, NULL, NULL);
// printf("needed2 = %d\n", needed);
if(needed == 0) {
err = GetLastError();
break;
} else {
Store_field(array_val, i, str_val);
}
}
}
}
win_error(out_val, array_val, err);
CAMLreturn(out_val);
}
// OPENFILE
// Basically copied from otherlibs\win32unix\open.c
static int open_access_flags[12] = {
GENERIC_READ, GENERIC_WRITE, GENERIC_READ|GENERIC_WRITE,
0, 0, 0, 0, 0, 0, 0, 0, 0
};
static int open_create_flags[12] = {
0, 0, 0, 0, 0, O_CREAT, O_TRUNC, O_EXCL, 0, 0, 0, 0
};
CAMLprim value uni_openfile_utf16(value path, value flags, value perm)
{
int fileaccess, createflags, fileattrib, filecreate;
SECURITY_ATTRIBUTES attr;
HANDLE h;
fileaccess = convert_flag_list(flags, open_access_flags);
createflags = convert_flag_list(flags, open_create_flags);
if ((createflags & (O_CREAT | O_EXCL)) == (O_CREAT | O_EXCL))
filecreate = CREATE_NEW;
else if ((createflags & (O_CREAT | O_TRUNC)) == (O_CREAT | O_TRUNC))
filecreate = CREATE_ALWAYS;
else if (createflags & O_TRUNC)
filecreate = TRUNCATE_EXISTING;
else if (createflags & O_CREAT)
filecreate = OPEN_ALWAYS;
else
filecreate = OPEN_EXISTING;
if ((createflags & O_CREAT) && (Int_val(perm) & 0200) == 0)
fileattrib = FILE_ATTRIBUTE_READONLY;
else
fileattrib = FILE_ATTRIBUTE_NORMAL;
attr.nLength = sizeof(attr);
attr.lpSecurityDescriptor = NULL;
attr.bInheritHandle = TRUE;
h = CreateFileW((LPCWSTR)String_val(path), fileaccess,
FILE_SHARE_READ | FILE_SHARE_WRITE, &attr,
filecreate, fileattrib, NULL);
if (h == INVALID_HANDLE_VALUE) {
win32_maperr(GetLastError());
uerror("Unicode.openfile", Nothing); // The path here will be in UTF-16, so OCaml won't be able to print it right
}
return win_alloc_handle(h);
}
CAMLprim value uni_readdir_find_first_file_utf16(value name_val) {
CAMLparam1(name_val);
HANDLE handle;
DWORD err = 0;
WIN32_FIND_DATAW f;
CAMLlocal3(out_val, tuple_val, out_str_val);
handle = FindFirstFileW((LPCWSTR)String_val(name_val), &f);
if(handle == INVALID_HANDLE_VALUE) {
err = GetLastError();
win_bad(out_val, err);
} else {
size_t str_len = wcslen(f.cFileName) + 1; // include the null
tuple_val = caml_alloc_tuple(2);
out_str_val = caml_alloc_string(str_len * sizeof(WCHAR));
memcpy(String_val(out_str_val), f.cFileName, str_len * sizeof(WCHAR));
Store_field(tuple_val, 0, win_alloc_handle(handle));
Store_field(tuple_val, 1, out_str_val);
win_good(out_val, tuple_val);
}
CAMLreturn(out_val);
}
CAMLprim value uni_readdir_find_next_file_utf16(value handle_val) {
CAMLparam1(handle_val);
HANDLE handle = Handle_val(handle_val);
WIN32_FIND_DATAW f;
DWORD err = 0;
CAMLlocal2(out_val, out_str_val);
if(FindNextFileW(handle, &f)) {
size_t str_len = wcslen(f.cFileName) + 1;
out_str_val = caml_alloc_string(str_len * sizeof(WCHAR));
memcpy(String_val(out_str_val), f.cFileName, str_len * sizeof(WCHAR));
win_good(out_val, out_str_val);
} else {
err = GetLastError();
win_bad(out_val, err);
}
CAMLreturn(out_val);
}
CAMLprim value uni_readdir_find_close(value handle_val) {
CAMLparam1(handle_val);
HANDLE handle = Handle_val(handle_val);
CAMLlocal1(out_val);
if(FindClose(handle)) {
win_good(out_val, Val_int(0));
} else {
win_bad(out_val, GetLastError());
}
CAMLreturn(out_val);
}
// Taken from stat.c
#include <sys/types.h>
#include <sys/stat.h>
#ifndef S_IFLNK
#define S_IFLNK 0
#endif
#ifndef S_IFIFO
#define S_IFIFO 0
#endif
#ifndef S_IFSOCK
#define S_IFSOCK 0
#endif
#ifndef S_IFBLK
#define S_IFBLK 0
#endif
static int file_kind_table[] = {
S_IFREG, S_IFDIR, S_IFCHR, S_IFBLK, S_IFLNK, S_IFIFO, S_IFSOCK
};
CAMLprim value uni_stat_utf16(value name_val) {
CAMLparam1(name_val);
struct _stati64 buf;
int ret;
wchar_t *name = (wchar_t *)String_val(name_val);
CAMLlocal2(out_val, tuple_val);
ret = _wstati64(name, &buf);
if(ret == -1) {
win_bad(out_val, GetLastError());
} else if(buf.st_size > Max_long) {
// Too big to fit into an int
win_bad(out_val, ERROR_ARITHMETIC_OVERFLOW);
} else {
tuple_val = caml_alloc_tuple(12);
Store_field(tuple_val, 0, Val_int(buf.st_dev));
Store_field(tuple_val, 1, Val_int(buf.st_ino));
Store_field(tuple_val, 2, cst_to_constr(buf.st_mode & S_IFMT, file_kind_table, sizeof(file_kind_table) / sizeof(int), 0));
Store_field(tuple_val, 3, Val_int(buf.st_mode & 07777));
Store_field(tuple_val, 4, Val_int(buf.st_nlink));
Store_field(tuple_val, 5, Val_int(buf.st_uid));
Store_field(tuple_val, 6, Val_int(buf.st_gid));
Store_field(tuple_val, 7, Val_int(buf.st_rdev));
Store_field(tuple_val, 8, Val_int(buf.st_size)); // no int64 version
Store_field(tuple_val, 9, copy_double((double)buf.st_atime));
Store_field(tuple_val, 10, copy_double((double)buf.st_mtime));
Store_field(tuple_val, 11, copy_double((double)buf.st_ctime));
win_good(out_val, tuple_val);
}
CAMLreturn(out_val);
}
CAMLprim value uni_file_exists_utf16(value name_val) {
CAMLparam1(name_val);
struct _stati64 buf;
int ret;
wchar_t *name = (wchar_t *)String_val(name_val);
CAMLlocal1(out_val);
ret = _wstati64(name, &buf);
CAMLreturn(Val_bool(ret != -1));
}
CAMLprim value uni_rename_utf16(value path1, value path2)
{
CAMLparam2(path1, path2);
static int supports_MoveFileEx = -1; /* don't know yet */
wchar_t *wpath1;
wchar_t *wpath2;
BOOL ok;
CAMLlocal1(out_val);
wpath1 = (wchar_t *)String_val(path1);
wpath2 = (wchar_t *)String_val(path2);
if (supports_MoveFileEx < 0) {
OSVERSIONINFO VersionInfo;
VersionInfo.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
supports_MoveFileEx =
(GetVersionEx(&VersionInfo) != 0)
&& (VersionInfo.dwPlatformId == VER_PLATFORM_WIN32_NT);
}
if (supports_MoveFileEx > 0) {
ok = MoveFileExW(wpath1, wpath2,
MOVEFILE_REPLACE_EXISTING | MOVEFILE_WRITE_THROUGH |
MOVEFILE_COPY_ALLOWED);
} else {
ok = MoveFileW(wpath1, wpath2);
}
if(ok) {
win_good(out_val, Val_int(0));
} else {
win_bad(out_val, GetLastError());
}
CAMLreturn(out_val);
}
CAMLprim value uni_remove_utf16(value name_val) {
CAMLparam1(name_val);
CAMLlocal1(out_val);
int err = 0;
if(!DeleteFileW((wchar_t *)String_val(name_val))) {
win_bad(out_val, GetLastError());
} else {
win_good(out_val, Val_int(0));
}
CAMLreturn(out_val);
}
#else
#define RETURN_INVALID {\
CAMLlocal1(out_val);\
out_val = caml_alloc(1, 1);\
Store_field(out_val, 0, Val_int(1));\
CAMLreturn(out_val);\
}
CAMLprim value uni_is_win() {
return(Val_int(0));
}
CAMLprim void uni_set_utf8_output() {}
CAMLprim void uni_silly_print(value s) {
CAMLparam1(s);
printf("%s", String_val(s));
fflush(stdout);
CAMLreturn0;
}
// None of these functions need to exist in Linux
CAMLprim value uni_utf16_of_utf8(value utf8_val, value include_null) {
CAMLparam2(utf8_val, include_null);
RETURN_INVALID;
}
CAMLprim value uni_utf8_of_utf16(value unicode_val) {
CAMLparam1(unicode_val);
RETURN_INVALID;
}
CAMLprim value uni_active_of_utf16(value byte_len_val, value unicode_val) {
CAMLparam2(byte_len_val, unicode_val);
RETURN_INVALID;
}
CAMLprim value uni_utf8_of_utf16_and_length(value unicode_val, value len_val) {
CAMLparam2(unicode_val, len_val);
RETURN_INVALID;
}
CAMLprim value uni_get_utf16_command_line() {
CAMLparam0();
RETURN_INVALID;
}
CAMLprim value uni_get_utf8_argv() {
CAMLparam0();
RETURN_INVALID;
}
CAMLprim value uni_openfile_utf16(value path, value flags, value perm) {
CAMLparam3(path, flags, perm);
RETURN_INVALID;
}
CAMLprim value uni_readdir_find_first_file_utf16(value name_val) {
CAMLparam1(name_val);
RETURN_INVALID;
}
CAMLprim value uni_readdir_find_next_file_utf16(value handle_val) {
CAMLparam1(handle_val);
RETURN_INVALID;
}
CAMLprim value uni_readdir_find_close(value handle_val) {
CAMLparam1(handle_val);
RETURN_INVALID;
}
CAMLprim value uni_stat_utf16(value name_val) {
CAMLparam1(name_val);
RETURN_INVALID;
}
CAMLprim value uni_file_exists_utf16(value name_val) {
CAMLparam1(name_val);
RETURN_INVALID;
}
CAMLprim value uni_rename_utf16(value path1, value path2) {
CAMLparam2(path1, path2);
RETURN_INVALID;
}
CAMLprim value uni_remove_utf16(value name_val) {
CAMLparam1(name_val);
RETURN_INVALID;
}
#endif