1 /*
2 * libwebsockets - small server side websockets and web server implementation
3 *
4 * Copyright (C) 2010 - 2019 Andy Green <[email protected]>
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 == NULL)
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 #if defined(__MINGW32__)
62 /* we use filesystem_priv */
63 fop_fd->fd = (int)(intptr_t)ret;
64 #else
65 fop_fd->fd = ret;
66 #endif
67 fop_fd->filesystem_priv = ret;
68 fop_fd->flags = *flags;
69 fop_fd->len = GetFileSize(ret, NULL);
70 if(GetFileSizeEx(ret, &llFileSize))
71 fop_fd->len = llFileSize.QuadPart;
72
73 fop_fd->pos = 0;
74
75 return fop_fd;
76
77 bail:
78 return NULL;
79 }
80
81 int
_lws_plat_file_close(lws_fop_fd_t * fop_fd)82 _lws_plat_file_close(lws_fop_fd_t *fop_fd)
83 {
84 HANDLE fd = (*fop_fd)->filesystem_priv;
85
86 free(*fop_fd);
87 *fop_fd = NULL;
88
89 CloseHandle((HANDLE)fd);
90
91 return 0;
92 }
93
94 lws_fileofs_t
_lws_plat_file_seek_cur(lws_fop_fd_t fop_fd,lws_fileofs_t offset)95 _lws_plat_file_seek_cur(lws_fop_fd_t fop_fd, lws_fileofs_t offset)
96 {
97 LARGE_INTEGER l;
98
99 l.QuadPart = offset;
100 if (!SetFilePointerEx((HANDLE)fop_fd->filesystem_priv, l, NULL, FILE_CURRENT))
101 {
102 lwsl_err("error seeking from cur %ld, offset %ld\n", (long)fop_fd->pos, (long)offset);
103 return -1;
104 }
105
106 LARGE_INTEGER zero;
107 zero.QuadPart = 0;
108 LARGE_INTEGER newPos;
109 if (!SetFilePointerEx((HANDLE)fop_fd->filesystem_priv, zero, &newPos, FILE_CURRENT))
110 {
111 lwsl_err("error seeking from cur %ld, offset %ld\n", (long)fop_fd->pos, (long)offset);
112 return -1;
113 }
114 fop_fd->pos = newPos.QuadPart;
115
116 return newPos.QuadPart;
117 }
118
119 int
_lws_plat_file_read(lws_fop_fd_t fop_fd,lws_filepos_t * amount,uint8_t * buf,lws_filepos_t len)120 _lws_plat_file_read(lws_fop_fd_t fop_fd, lws_filepos_t *amount,
121 uint8_t *buf, lws_filepos_t len)
122 {
123 DWORD _amount;
124
125 if (!ReadFile((HANDLE)fop_fd->filesystem_priv, buf, (DWORD)len, &_amount, NULL)) {
126 *amount = 0;
127
128 return 1;
129 }
130
131 fop_fd->pos += _amount;
132 *amount = (unsigned long)_amount;
133
134 return 0;
135 }
136
137 int
_lws_plat_file_write(lws_fop_fd_t fop_fd,lws_filepos_t * amount,uint8_t * buf,lws_filepos_t len)138 _lws_plat_file_write(lws_fop_fd_t fop_fd, lws_filepos_t *amount,
139 uint8_t* buf, lws_filepos_t len)
140 {
141 DWORD _amount;
142
143 if (!WriteFile((HANDLE)fop_fd->filesystem_priv, buf, (DWORD)len, &_amount, NULL)) {
144 *amount = 0;
145
146 return 1;
147 }
148
149 fop_fd->pos += _amount;
150 *amount = (unsigned long)_amount;
151
152 return 0;
153 }
154
155
156 int
lws_plat_write_cert(struct lws_vhost * vhost,int is_key,int fd,void * buf,size_t len)157 lws_plat_write_cert(struct lws_vhost *vhost, int is_key, int fd, void *buf,
158 size_t len)
159 {
160 int n;
161
162 n = (int)write(fd, buf, (unsigned int)len);
163
164 lseek(fd, 0, SEEK_SET);
165
166 return (size_t)n != len;
167 }
168
169 int
lws_plat_write_file(const char * filename,void * buf,size_t len)170 lws_plat_write_file(const char *filename, void *buf, size_t len)
171 {
172 int m, fd;
173
174 fd = lws_open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0600);
175
176 if (fd == -1)
177 return -1;
178
179 m = (int)write(fd, buf, (unsigned int)len);
180 close(fd);
181
182 return (size_t)m != len;
183 }
184
185 int
lws_plat_read_file(const char * filename,void * buf,size_t len)186 lws_plat_read_file(const char *filename, void *buf, size_t len)
187 {
188 int n, fd = lws_open(filename, O_RDONLY);
189 if (fd == -1)
190 return -1;
191
192 n = (int)read(fd, buf, (unsigned int)len);
193 close(fd);
194
195 return n;
196 }
197
198