ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/Urlader/urlib.c
(Generate patch)

Comparing Urlader/urlib.c (file contents):
Revision 1.2 by root, Mon Jan 2 09:23:04 2012 UTC vs.
Revision 1.6 by root, Tue Jan 17 18:38:37 2012 UTC

1/*
2 * Copyright (c) 2012 Marc Alexander Lehmann <schmorp@schmorp.de>
3 *
4 * Redistribution and use in source and binary forms, with or without modifica-
5 * tion, are permitted provided that the following conditions are met:
6 *
7 * 1. Redistributions of source code must retain the above copyright notice,
8 * this list of conditions and the following disclaimer.
9 *
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
15 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MER-
16 * CHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
17 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPE-
18 * CIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
20 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
21 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTH-
22 * ERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
23 * OF THE POSSIBILITY OF SUCH DAMAGE.
24 *
25 * Alternatively, the contents of this file may be used under the terms of
26 * the GNU General Public License ("GPL") version 2 or any later version,
27 * in which case the provisions of the GPL are applicable instead of
28 * the above. If you wish to allow the use of your version of this file
29 * only under the terms of the GPL and not to allow others to use your
30 * version of this file under the BSD license, indicate your decision
31 * by deleting the provisions above and replace them with the notice
32 * and other provisions required by the GPL. If you do not delete the
33 * provisions above, a recipient may use your version of this file under
34 * either the BSD or the GPL.
35 */
36
1#include "urlib.h" 37#include "urlib.h"
2 38
3#include <fcntl.h> 39#include <fcntl.h>
4#include <stdio.h> 40#include <stdio.h>
5#include <string.h> 41#include <string.h>
21 57
22 #define u_setenv(name,value) SetEnvironmentVariable (name, value) 58 #define u_setenv(name,value) SetEnvironmentVariable (name, value)
23 #define u_mkdir(path) !CreateDirectory (path, NULL) 59 #define u_mkdir(path) !CreateDirectory (path, NULL)
24 #define u_chdir(path) !SetCurrentDirectory (path) 60 #define u_chdir(path) !SetCurrentDirectory (path)
25 #define u_rename(fr,to) !MoveFile (fr, to) 61 #define u_rename(fr,to) !MoveFile (fr, to)
26 #define u_open(path) CreateFile (path, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_SEQUENTIAL_SCAN, NULL) 62 #define u_open(path) CreateFile (path, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_DELETE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_SEQUENTIAL_SCAN, NULL)
27 #define u_creat(path,exec) CreateFile (path, GENERIC_WRITE, FILE_SHARE_WRITE, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_SEQUENTIAL_SCAN, NULL) 63 #define u_creat(path,exec) CreateFile (path, GENERIC_WRITE, FILE_SHARE_WRITE, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_SEQUENTIAL_SCAN, NULL)
28 #define u_creat(path,exec) CreateFile (path, GENERIC_WRITE, FILE_SHARE_WRITE, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_SEQUENTIAL_SCAN, NULL) 64 #define u_creat(path,exec) CreateFile (path, GENERIC_WRITE, FILE_SHARE_WRITE, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_SEQUENTIAL_SCAN, NULL)
29 #define u_close(handle) CloseHandle (handle) 65 #define u_close(handle) CloseHandle (handle)
30 #define u_append(path,add) PathAppend (path, add) 66 #define u_append(path,add) PathAppend (path, add)
31 #define u_write(handle,data,len) (WriteFile (handle, data, len, &dword, 0) ? dword : -1) 67 #define u_write(handle,data,len) (WriteFile (handle, data, len, &dword, 0) ? dword : -1)
32 68
33 #define u_fsync(handle) FlushFileBuffers (handle) 69 #define u_fsync(handle) FlushFileBuffers (handle)
34 #define u_sync() 70 #define u_sync()
35 71
36 #define u_lockfile(path) CreateFile (path, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL) 72 #define u_lockfile(path) CreateFile (path, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL)
37 #define u_cloexec(handle) 73 #define u_cloexec(handle)
38 74
39#else 75#else
40 76
41 #define _GNU_SOURCE 1 77 #define _GNU_SOURCE 1
110static void * 146static void *
111u_malloc (unsigned int size) 147u_malloc (unsigned int size)
112{ 148{
113 void *addr; 149 void *addr;
114 150
115#ifdef _WIN32 151 if (!size)
116 HANDLE handle = CreateFileMapping (0, 0, PAGE_READWRITE, 0, size, NULL);
117
118 if (!handle)
119 return 0; 152 return 0;
120 153
154#ifdef _WIN32
155 {
156 HANDLE handle = CreateFileMapping (0, 0, PAGE_READWRITE, 0, size, NULL);
157
158 addr = 0;
159 if (handle)
160 {
121 addr = MapViewOfFile (handle, FILE_MAP_WRITE, 0, 0, size); 161 addr = MapViewOfFile (handle, FILE_MAP_WRITE, 0, 0, size);
122
123 CloseHandle (handle); 162 CloseHandle (handle);
163 }
164 }
124#elif defined (MAP_ANONYMOUS) 165#elif defined (MAP_ANONYMOUS)
125 addr = mmap (0, size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANON, -1, 0); 166 addr = mmap (0, size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANON, -1, 0);
126 167
127 if (addr == (void *)-1) 168 if (addr == (void *)-1)
128 addr = 0; 169 addr = 0;
129#else 170#else
130 addr = malloc (size); 171 addr = malloc (size);
131#endif 172#endif
132 173
174 if (!addr)
175 u_fatal ("memory allocation failure, aborting.");
176
133 return addr; 177 return addr;
134} 178}
135 179
136static void 180static void
137u_free (void *addr, unsigned int size) 181u_free (void *addr, unsigned int size)
138{ 182{
183 if (!addr)
184 return;
185
139#ifdef _WIN32 186#ifdef _WIN32
140 UnmapViewOfFile (addr); 187 UnmapViewOfFile (addr);
141#elif defined (MAP_ANONYMOUS) 188#elif defined (MAP_ANONYMOUS)
142 munmap (addr, size); 189 munmap (addr, size);
143#else 190#else
144 free (addr); 191 free (addr);
145#endif 192#endif
146} 193}
147 194
148static void * 195static void *
196u_realloc (void *addr, unsigned int old_size, unsigned int new_size)
197{
198 void *addr2 = u_malloc (new_size);
199 memcpy (addr2, addr, (new_size < old_size ? new_size : old_size));
200 u_free (addr, old_size);
201
202 return addr2;
203}
204
205static void *
149u_mmap (u_handle h, unsigned int size) 206u_mmap (u_handle h, unsigned int size)
150{ 207{
151 void *addr; 208 void *addr;
152 209
153#ifdef _WIN32 210#ifdef _WIN32
175#ifdef _WIN32 232#ifdef _WIN32
176 UnmapViewOfFile (addr); 233 UnmapViewOfFile (addr);
177#else 234#else
178 munmap (addr, len); 235 munmap (addr, len);
179#endif 236#endif
237}
238
239/////////////////////////////////////////////////////////////////////////////
240
241typedef struct
242{
243 char *addr;
244 unsigned int used;
245 unsigned int size;
246} u_dynbuf;
247
248static void *
249u_dynbuf_append (u_dynbuf *dynbuf, void *data, unsigned int len)
250{
251 char *dest;
252
253 if ((dynbuf->used += len) > dynbuf->size)
254 {
255 unsigned int new_size = dynbuf->size ? dynbuf->size * 2 : 4096;
256 dynbuf->addr = u_realloc (dynbuf->addr, dynbuf->size, new_size);
257 dynbuf->size = new_size;
258 }
259
260 dest = dynbuf->addr + dynbuf->used - len;
261
262 if (data)
263 memcpy (dest, data, len);
264
265 return dest;
180} 266}
181 267
182///////////////////////////////////////////////////////////////////////////// 268/////////////////////////////////////////////////////////////////////////////
183 269
184static void 270static void

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines