1 /*
2 * libwebsockets - small server side websockets and web server implementation
3 *
4 * Copyright (C) 2010 - 2019 Andy Green <andy@warmcat.com>
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to
8 * deal in the Software without restriction, including without limitation the
9 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10 * sell copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
22 * IN THE SOFTWARE.
23 */
24
25 #ifndef _WINSOCK_DEPRECATED_NO_WARNINGS
26 #define _WINSOCK_DEPRECATED_NO_WARNINGS
27 #endif
28 #include "private-lib-core.h"
29
lws_plat_apply_FD_CLOEXEC(int n)30 int lws_plat_apply_FD_CLOEXEC(int n)
31 {
32 return 0;
33 }
34
35 lws_fop_fd_t
_lws_plat_file_open(const struct lws_plat_file_ops * fops,const char * filename,const char * vpath,lws_fop_flags_t * flags)36 _lws_plat_file_open(const struct lws_plat_file_ops *fops, const char *filename,
37 const char *vpath, lws_fop_flags_t *flags)
38 {
39 HANDLE ret;
40 WCHAR buf[MAX_PATH];
41 lws_fop_fd_t fop_fd;
42 LARGE_INTEGER llFileSize = {0};
43
44 MultiByteToWideChar(CP_UTF8, 0, filename, -1, buf, LWS_ARRAY_SIZE(buf));
45 if (((*flags) & 7) == _O_RDONLY) {
46 ret = CreateFileW(buf, GENERIC_READ, FILE_SHARE_READ,
47 NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
48 } else {
49 ret = CreateFileW(buf, GENERIC_WRITE, 0, NULL,
50 CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
51 }
52
53 if (ret == LWS_INVALID_FILE)
54 goto bail;
55
56 fop_fd = malloc(sizeof(*fop_fd));
57 if (!fop_fd)
58 goto bail;
59
60 fop_fd->fops = fops;
61 fop_fd->fd = ret;
62 fop_fd->filesystem_priv = NULL; /* we don't use it */
63 fop_fd->flags = *flags;
64 fop_fd->len = GetFileSize(ret, NULL);
65 if(GetFileSizeEx(ret, &llFileSize))
66 fop_fd->len = llFileSize.QuadPart;
67
68 fop_fd->pos = 0;
69
70 return fop_fd;
71
72 bail:
73 return NULL;
74 }
75
76 int
_lws_plat_file_close(lws_fop_fd_t * fop_fd)77 _lws_plat_file_close(lws_fop_fd_t *fop_fd)
78 {
79 HANDLE fd = (*fop_fd)->fd;
80
81 free(*fop_fd);
82 *fop_fd = NULL;
83
84 CloseHandle((HANDLE)fd);
85
86 return 0;
87 }
88
89 lws_fileofs_t
_lws_plat_file_seek_cur(lws_fop_fd_t fop_fd,lws_fileofs_t offset)90 _lws_plat_file_seek_cur(lws_fop_fd_t fop_fd, lws_fileofs_t offset)
91 {
92 LARGE_INTEGER l;
93
94 l.QuadPart = offset;
95 return SetFilePointerEx((HANDLE)fop_fd->fd, l, NULL, FILE_CURRENT);
96 }
97
98 int
_lws_plat_file_read(lws_fop_fd_t fop_fd,lws_filepos_t * amount,uint8_t * buf,lws_filepos_t len)99 _lws_plat_file_read(lws_fop_fd_t fop_fd, lws_filepos_t *amount,
100 uint8_t *buf, lws_filepos_t len)
101 {
102 DWORD _amount;
103
104 if (!ReadFile((HANDLE)fop_fd->fd, buf, (DWORD)len, &_amount, NULL)) {
105 *amount = 0;
106
107 return 1;
108 }
109
110 fop_fd->pos += _amount;
111 *amount = (unsigned long)_amount;
112
113 return 0;
114 }
115
116 int
_lws_plat_file_write(lws_fop_fd_t fop_fd,lws_filepos_t * amount,uint8_t * buf,lws_filepos_t len)117 _lws_plat_file_write(lws_fop_fd_t fop_fd, lws_filepos_t *amount,
118 uint8_t* buf, lws_filepos_t len)
119 {
120 DWORD _amount;
121
122 if (!WriteFile((HANDLE)fop_fd->fd, buf, (DWORD)len, &_amount, NULL)) {
123 *amount = 0;
124
125 return 1;
126 }
127
128 fop_fd->pos += _amount;
129 *amount = (unsigned long)_amount;
130
131 return 0;
132 }
133
134
135 int
lws_plat_write_cert(struct lws_vhost * vhost,int is_key,int fd,void * buf,int len)136 lws_plat_write_cert(struct lws_vhost *vhost, int is_key, int fd, void *buf,
137 int len)
138 {
139 int n;
140
141 n = write(fd, buf, len);
142
143 lseek(fd, 0, SEEK_SET);
144
145 return n != len;
146 }
147
148 int
lws_plat_write_file(const char * filename,void * buf,int len)149 lws_plat_write_file(const char *filename, void *buf, int len)
150 {
151 int m, fd;
152
153 fd = lws_open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0600);
154
155 if (fd == -1)
156 return -1;
157
158 m = write(fd, buf, len);
159 close(fd);
160
161 return m != len;
162 }
163
164 int
lws_plat_read_file(const char * filename,void * buf,int len)165 lws_plat_read_file(const char *filename, void *buf, int len)
166 {
167 int n, fd = lws_open(filename, O_RDONLY);
168 if (fd == -1)
169 return -1;
170
171 n = read(fd, buf, len);
172 close(fd);
173
174 return n;
175 }
176
177