comparison src/os/win32/ngx_files.c @ 2628:64a10d6b97bd

win32 ngx_open_file() supports utf8 names and NGX_FILE_APPEND
author Igor Sysoev <igor@sysoev.ru>
date Mon, 30 Mar 2009 14:51:51 +0000
parents ceef364208c8
children 5190c5dc3486
comparison
equal deleted inserted replaced
2627:c9da3e4dc706 2628:64a10d6b97bd
4 */ 4 */
5 5
6 6
7 #include <ngx_config.h> 7 #include <ngx_config.h>
8 #include <ngx_core.h> 8 #include <ngx_core.h>
9
10
11 #define NGX_UTF16_BUFLEN 256
12
13 static u_short *ngx_utf8_to_utf16(u_short *utf16, u_char *utf8, size_t len);
14
15
16 /* FILE_FLAG_BACKUP_SEMANTICS allows to obtain a handle to a directory */
17
18 ngx_fd_t
19 ngx_open_file(u_char *name, u_long mode, u_long create, u_long access)
20 {
21 u_short *u;
22 ngx_fd_t fd;
23 u_short utf16[NGX_UTF16_BUFLEN];
24
25 u = ngx_utf8_to_utf16(utf16, name, NGX_UTF16_BUFLEN);
26
27 if (u == NULL) {
28 return INVALID_HANDLE_VALUE;
29 }
30
31 fd = CreateFileW(u, mode,
32 FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE,
33 NULL, create, FILE_FLAG_BACKUP_SEMANTICS, NULL);
34
35 if (u != utf16) {
36 ngx_free(u);
37 }
38
39 return fd;
40 }
9 41
10 42
11 ssize_t 43 ssize_t
12 ngx_read_file(ngx_file_t *file, u_char *buf, size_t size, off_t offset) 44 ngx_read_file(ngx_file_t *file, u_char *buf, size_t size, off_t offset)
13 { 45 {
527 return 512; 559 return 512;
528 } 560 }
529 561
530 return sc * bs; 562 return sc * bs;
531 } 563 }
564
565
566 static u_short *
567 ngx_utf8_to_utf16(u_short *utf16, u_char *utf8, size_t len)
568 {
569 u_char *p;
570 u_short *u, *last;
571 uint32_t n;
572
573 p = utf8;
574 u = utf16;
575 last = utf16 + len;
576
577 while (u < last) {
578
579 if (*p < 0x80) {
580 *u = (u_short) *p;
581
582 if (*p == 0) {
583 return utf16;
584 }
585
586 u++;
587 p++;
588
589 continue;
590 }
591
592 n = ngx_utf8_decode(&p, 4);
593
594 if (n > 0xffff) {
595 free(utf16);
596 ngx_set_errno(NGX_EILSEQ);
597 return NULL;
598 }
599
600 *u++ = (u_short) n;
601 }
602
603 /* the given buffer is not enough, allocate a new one */
604
605 u = malloc(((p - utf8) + ngx_strlen(p) + 1) * sizeof(u_short));
606 if (u == NULL) {
607 return NULL;
608 }
609
610 ngx_memcpy(u, utf16, len * 2);
611
612 utf16 = u;
613 u += len;
614
615 for ( ;; ) {
616
617 if (*p < 0x80) {
618 *u = (u_short) *p;
619
620 if (*p == 0) {
621 return utf16;
622 }
623
624 u++;
625 p++;
626
627 continue;
628 }
629
630 n = ngx_utf8_decode(&p, 4);
631
632 if (n > 0xffff) {
633 free(utf16);
634 ngx_set_errno(NGX_EILSEQ);
635 return NULL;
636 }
637
638 *u++ = (u_short) n;
639 }
640
641 /* unreachable */
642 }