aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChristian Grothoff <christian@grothoff.org>2019-10-06 22:18:01 +0200
committerChristian Grothoff <christian@grothoff.org>2019-10-06 22:18:01 +0200
commite31795501c38a9380291fed9d62c903989cfede1 (patch)
tree504fd625f58f1c62f1e605c510699df252c6de81
parent1544473910fb629895bfa7a757f6d8cf31c973b7 (diff)
downloadlibmicrohttpd-e31795501c38a9380291fed9d62c903989cfede1.tar.gz
libmicrohttpd-e31795501c38a9380291fed9d62c903989cfede1.zip
indentation fixes
-rw-r--r--src/examples/fileserver_example.c84
-rw-r--r--src/microhttpd/basicauth.c87
-rw-r--r--src/microhttpd/memorypool.c239
-rw-r--r--src/microhttpd/mhd_threads.c165
4 files changed, 295 insertions, 280 deletions
diff --git a/src/examples/fileserver_example.c b/src/examples/fileserver_example.c
index b8935fa1..46e6aad6 100644
--- a/src/examples/fileserver_example.c
+++ b/src/examples/fileserver_example.c
@@ -34,7 +34,8 @@
34#include <fcntl.h> 34#include <fcntl.h>
35#endif /* HAVE_FCNTL_H */ 35#endif /* HAVE_FCNTL_H */
36 36
37#define PAGE "<html><head><title>File not found</title></head><body>File not found</body></html>" 37#define PAGE \
38 "<html><head><title>File not found</title></head><body>File not found</body></html>"
38 39
39#ifndef S_ISREG 40#ifndef S_ISREG
40#define S_ISREG(x) (S_IFREG == (x & S_IFREG)) 41#define S_ISREG(x) (S_IFREG == (x & S_IFREG))
@@ -47,67 +48,67 @@ ahc_echo (void *cls,
47 const char *method, 48 const char *method,
48 const char *version, 49 const char *version,
49 const char *upload_data, 50 const char *upload_data,
50 size_t *upload_data_size, void **ptr) 51 size_t *upload_data_size, void **ptr)
51{ 52{
52 static int aptr; 53 static int aptr;
53 struct MHD_Response *response; 54 struct MHD_Response *response;
54 int ret; 55 int ret;
55 int fd; 56 int fd;
56 struct stat buf; 57 struct stat buf;
57 (void)cls; /* Unused. Silent compiler warning. */ 58 (void) cls; /* Unused. Silent compiler warning. */
58 (void)version; /* Unused. Silent compiler warning. */ 59 (void) version; /* Unused. Silent compiler warning. */
59 (void)upload_data; /* Unused. Silent compiler warning. */ 60 (void) upload_data; /* Unused. Silent compiler warning. */
60 (void)upload_data_size; /* Unused. Silent compiler warning. */ 61 (void) upload_data_size; /* Unused. Silent compiler warning. */
61 62
62 if ( (0 != strcmp (method, MHD_HTTP_METHOD_GET)) && 63 if ( (0 != strcmp (method, MHD_HTTP_METHOD_GET)) &&
63 (0 != strcmp (method, MHD_HTTP_METHOD_HEAD)) ) 64 (0 != strcmp (method, MHD_HTTP_METHOD_HEAD)) )
64 return MHD_NO; /* unexpected method */ 65 return MHD_NO; /* unexpected method */
65 if (&aptr != *ptr) 66 if (&aptr != *ptr)
66 { 67 {
67 /* do never respond on first call */ 68 /* do never respond on first call */
68 *ptr = &aptr; 69 *ptr = &aptr;
69 return MHD_YES; 70 return MHD_YES;
70 } 71 }
71 *ptr = NULL; /* reset when done */ 72 *ptr = NULL; /* reset when done */
72 /* WARNING: direct usage of url as filename is for example only! 73 /* WARNING: direct usage of url as filename is for example only!
73 * NEVER pass received data directly as parameter to file manipulation 74 * NEVER pass received data directly as parameter to file manipulation
74 * functions. Always check validity of data before using. 75 * functions. Always check validity of data before using.
75 */ 76 */
76 if (NULL != strstr(url, "../")) /* Very simplified check! */ 77 if (NULL != strstr (url, "../")) /* Very simplified check! */
77 fd = -1; /* Do not allow usage of parent directories. */ 78 fd = -1; /* Do not allow usage of parent directories. */
78 else 79 else
79 fd = open (url + 1, O_RDONLY); 80 fd = open (url + 1, O_RDONLY);
80 if (-1 != fd) 81 if (-1 != fd)
82 {
83 if ( (0 != fstat (fd, &buf)) ||
84 (! S_ISREG (buf.st_mode)) )
81 { 85 {
82 if ( (0 != fstat (fd, &buf)) || 86 /* not a regular file, refuse to serve */
83 (! S_ISREG (buf.st_mode)) ) 87 if (0 != close (fd))
84 { 88 abort ();
85 /* not a regular file, refuse to serve */ 89 fd = -1;
86 if (0 != close (fd))
87 abort ();
88 fd = -1;
89 }
90 } 90 }
91 }
91 if (-1 == fd) 92 if (-1 == fd)
92 { 93 {
93 response = MHD_create_response_from_buffer (strlen (PAGE), 94 response = MHD_create_response_from_buffer (strlen (PAGE),
94 (void *) PAGE, 95 (void *) PAGE,
95 MHD_RESPMEM_PERSISTENT); 96 MHD_RESPMEM_PERSISTENT);
96 ret = MHD_queue_response (connection, MHD_HTTP_NOT_FOUND, response); 97 ret = MHD_queue_response (connection, MHD_HTTP_NOT_FOUND, response);
97 MHD_destroy_response (response); 98 MHD_destroy_response (response);
98 } 99 }
99 else 100 else
101 {
102 response = MHD_create_response_from_fd64 (buf.st_size, fd);
103 if (NULL == response)
100 { 104 {
101 response = MHD_create_response_from_fd64 (buf.st_size, fd); 105 if (0 != close (fd))
102 if (NULL == response) 106 abort ();
103 { 107 return MHD_NO;
104 if (0 != close (fd))
105 abort ();
106 return MHD_NO;
107 }
108 ret = MHD_queue_response (connection, MHD_HTTP_OK, response);
109 MHD_destroy_response (response);
110 } 108 }
109 ret = MHD_queue_response (connection, MHD_HTTP_OK, response);
110 MHD_destroy_response (response);
111 }
111 return ret; 112 return ret;
112} 113}
113 114
@@ -118,11 +119,12 @@ main (int argc, char *const *argv)
118 struct MHD_Daemon *d; 119 struct MHD_Daemon *d;
119 120
120 if (argc != 2) 121 if (argc != 2)
121 { 122 {
122 printf ("%s PORT\n", argv[0]); 123 printf ("%s PORT\n", argv[0]);
123 return 1; 124 return 1;
124 } 125 }
125 d = MHD_start_daemon (MHD_USE_THREAD_PER_CONNECTION | MHD_USE_INTERNAL_POLLING_THREAD | MHD_USE_ERROR_LOG, 126 d = MHD_start_daemon (MHD_USE_THREAD_PER_CONNECTION
127 | MHD_USE_INTERNAL_POLLING_THREAD | MHD_USE_ERROR_LOG,
126 atoi (argv[1]), 128 atoi (argv[1]),
127 NULL, NULL, &ahc_echo, PAGE, MHD_OPTION_END); 129 NULL, NULL, &ahc_echo, PAGE, MHD_OPTION_END);
128 if (d == NULL) 130 if (d == NULL)
diff --git a/src/microhttpd/basicauth.c b/src/microhttpd/basicauth.c
index 1d0c53ad..f1e38af4 100644
--- a/src/microhttpd/basicauth.c
+++ b/src/microhttpd/basicauth.c
@@ -31,7 +31,7 @@
31/** 31/**
32 * Beginning string for any valid Basic authentication header. 32 * Beginning string for any valid Basic authentication header.
33 */ 33 */
34#define _BASIC_BASE "Basic " 34#define _BASIC_BASE "Basic "
35 35
36 36
37/** 37/**
@@ -40,12 +40,12 @@
40 * @param connection The MHD connection structure 40 * @param connection The MHD connection structure
41 * @param password a pointer for the password 41 * @param password a pointer for the password
42 * @return NULL if no username could be found, a pointer 42 * @return NULL if no username could be found, a pointer
43 * to the username if found 43 * to the username if found
44 * @ingroup authentication 44 * @ingroup authentication
45 */ 45 */
46char * 46char *
47MHD_basic_auth_get_username_password (struct MHD_Connection *connection, 47MHD_basic_auth_get_username_password (struct MHD_Connection *connection,
48 char** password) 48 char**password)
49{ 49{
50 const char *header; 50 const char *header;
51 char *decode; 51 char *decode;
@@ -55,7 +55,8 @@ MHD_basic_auth_get_username_password (struct MHD_Connection *connection,
55 if ( (MHD_NO == MHD_lookup_connection_value_n (connection, 55 if ( (MHD_NO == MHD_lookup_connection_value_n (connection,
56 MHD_HEADER_KIND, 56 MHD_HEADER_KIND,
57 MHD_HTTP_HEADER_AUTHORIZATION, 57 MHD_HTTP_HEADER_AUTHORIZATION,
58 MHD_STATICSTR_LEN_ (MHD_HTTP_HEADER_AUTHORIZATION), 58 MHD_STATICSTR_LEN_ (
59 MHD_HTTP_HEADER_AUTHORIZATION),
59 &header, 60 &header,
60 NULL)) || 61 NULL)) ||
61 (0 != strncmp (header, 62 (0 != strncmp (header,
@@ -64,44 +65,44 @@ MHD_basic_auth_get_username_password (struct MHD_Connection *connection,
64 return NULL; 65 return NULL;
65 header += MHD_STATICSTR_LEN_ (_BASIC_BASE); 66 header += MHD_STATICSTR_LEN_ (_BASIC_BASE);
66 if (NULL == (decode = BASE64Decode (header))) 67 if (NULL == (decode = BASE64Decode (header)))
67 { 68 {
68#ifdef HAVE_MESSAGES 69#ifdef HAVE_MESSAGES
69 MHD_DLOG (connection->daemon, 70 MHD_DLOG (connection->daemon,
70 _("Error decoding basic authentication\n")); 71 _ ("Error decoding basic authentication\n"));
71#endif 72#endif
72 return NULL; 73 return NULL;
73 } 74 }
74 /* Find user:password pattern */ 75 /* Find user:password pattern */
75 if (NULL == (separator = strchr (decode, 76 if (NULL == (separator = strchr (decode,
76 ':'))) 77 ':')))
77 { 78 {
78#ifdef HAVE_MESSAGES 79#ifdef HAVE_MESSAGES
79 MHD_DLOG(connection->daemon, 80 MHD_DLOG (connection->daemon,
80 _("Basic authentication doesn't contain ':' separator\n")); 81 _ ("Basic authentication doesn't contain ':' separator\n"));
81#endif 82#endif
82 free (decode); 83 free (decode);
83 return NULL; 84 return NULL;
84 } 85 }
85 if (NULL == (user = strdup (decode))) 86 if (NULL == (user = strdup (decode)))
86 { 87 {
87 free (decode); 88 free (decode);
88 return NULL; 89 return NULL;
89 } 90 }
90 user[separator - decode] = '\0'; /* cut off at ':' */ 91 user[separator - decode] = '\0'; /* cut off at ':' */
91 if (NULL != password) 92 if (NULL != password)
93 {
94 *password = strdup (separator + 1);
95 if (NULL == *password)
92 { 96 {
93 *password = strdup (separator + 1);
94 if (NULL == *password)
95 {
96#ifdef HAVE_MESSAGES 97#ifdef HAVE_MESSAGES
97 MHD_DLOG(connection->daemon, 98 MHD_DLOG (connection->daemon,
98 _("Failed to allocate memory for password\n")); 99 _ ("Failed to allocate memory for password\n"));
99#endif 100#endif
100 free (decode); 101 free (decode);
101 free (user); 102 free (user);
102 return NULL; 103 return NULL;
103 }
104 } 104 }
105 }
105 free (decode); 106 free (decode);
106 return user; 107 return user;
107} 108}
@@ -121,20 +122,20 @@ MHD_basic_auth_get_username_password (struct MHD_Connection *connection,
121 */ 122 */
122int 123int
123MHD_queue_basic_auth_fail_response (struct MHD_Connection *connection, 124MHD_queue_basic_auth_fail_response (struct MHD_Connection *connection,
124 const char *realm, 125 const char *realm,
125 struct MHD_Response *response) 126 struct MHD_Response *response)
126{ 127{
127 int ret; 128 int ret;
128 int res; 129 int res;
129 size_t hlen = strlen(realm) + strlen("Basic realm=\"\"") + 1; 130 size_t hlen = strlen (realm) + strlen ("Basic realm=\"\"") + 1;
130 char *header; 131 char *header;
131 132
132 header = (char *) malloc(hlen); 133 header = (char *) malloc (hlen);
133 if (NULL == header) 134 if (NULL == header)
134 { 135 {
135#ifdef HAVE_MESSAGES 136#ifdef HAVE_MESSAGES
136 MHD_DLOG(connection->daemon, 137 MHD_DLOG (connection->daemon,
137 "Failed to allocate memory for auth header\n"); 138 "Failed to allocate memory for auth header\n");
138#endif /* HAVE_MESSAGES */ 139#endif /* HAVE_MESSAGES */
139 return MHD_NO; 140 return MHD_NO;
140 } 141 }
@@ -142,25 +143,27 @@ MHD_queue_basic_auth_fail_response (struct MHD_Connection *connection,
142 hlen, 143 hlen,
143 "Basic realm=\"%s\"", 144 "Basic realm=\"%s\"",
144 realm); 145 realm);
145 if (res > 0 && (size_t)res < hlen) 146 if ((res > 0)&&((size_t) res < hlen))
146 ret = MHD_add_response_header (response, 147 ret = MHD_add_response_header (response,
147 MHD_HTTP_HEADER_WWW_AUTHENTICATE, 148 MHD_HTTP_HEADER_WWW_AUTHENTICATE,
148 header); 149 header);
149 else 150 else
150 ret = MHD_NO; 151 ret = MHD_NO;
151 152
152 free(header); 153 free (header);
153 if (MHD_YES == ret) 154 if (MHD_YES == ret)
155 {
154 ret = MHD_queue_response (connection, 156 ret = MHD_queue_response (connection,
155 MHD_HTTP_UNAUTHORIZED, 157 MHD_HTTP_UNAUTHORIZED,
156 response); 158 response);
159 }
157 else 160 else
158 { 161 {
159#ifdef HAVE_MESSAGES 162#ifdef HAVE_MESSAGES
160 MHD_DLOG (connection->daemon, 163 MHD_DLOG (connection->daemon,
161 _("Failed to add Basic auth header\n")); 164 _ ("Failed to add Basic auth header\n"));
162#endif /* HAVE_MESSAGES */ 165#endif /* HAVE_MESSAGES */
163 } 166 }
164 return ret; 167 return ret;
165} 168}
166 169
diff --git a/src/microhttpd/memorypool.c b/src/microhttpd/memorypool.c
index 96c20ea6..ddfd08d3 100644
--- a/src/microhttpd/memorypool.c
+++ b/src/microhttpd/memorypool.c
@@ -45,13 +45,13 @@
45#endif /* HAVE_SYSCONF */ 45#endif /* HAVE_SYSCONF */
46 46
47/* define MAP_ANONYMOUS for Mac OS X */ 47/* define MAP_ANONYMOUS for Mac OS X */
48#if defined(MAP_ANON) && !defined(MAP_ANONYMOUS) 48#if defined(MAP_ANON) && ! defined(MAP_ANONYMOUS)
49#define MAP_ANONYMOUS MAP_ANON 49#define MAP_ANONYMOUS MAP_ANON
50#endif 50#endif
51#if defined(_WIN32) 51#if defined(_WIN32)
52#define MAP_FAILED NULL 52#define MAP_FAILED NULL
53#elif !defined(MAP_FAILED) 53#elif ! defined(MAP_FAILED)
54#define MAP_FAILED ((void*)-1) 54#define MAP_FAILED ((void*) -1)
55#endif 55#endif
56 56
57/** 57/**
@@ -62,7 +62,8 @@
62/** 62/**
63 * Round up 'n' to a multiple of ALIGN_SIZE. 63 * Round up 'n' to a multiple of ALIGN_SIZE.
64 */ 64 */
65#define ROUND_TO_ALIGN(n) (((n)+(ALIGN_SIZE-1)) / (ALIGN_SIZE) * (ALIGN_SIZE)) 65#define ROUND_TO_ALIGN(n) (((n) + (ALIGN_SIZE - 1)) \
66 / (ALIGN_SIZE) *(ALIGN_SIZE))
66 67
67#if defined(PAGE_SIZE) 68#if defined(PAGE_SIZE)
68#define MHD_DEF_PAGE_SIZE_ PAGE_SIZE 69#define MHD_DEF_PAGE_SIZE_ PAGE_SIZE
@@ -93,7 +94,7 @@ MHD_init_mem_pools_ (void)
93#elif defined(_WIN32) 94#elif defined(_WIN32)
94 SYSTEM_INFO si; 95 SYSTEM_INFO si;
95 GetSystemInfo (&si); 96 GetSystemInfo (&si);
96 MHD_sys_page_size_ = (size_t)si.dwPageSize; 97 MHD_sys_page_size_ = (size_t) si.dwPageSize;
97#else 98#else
98 MHD_sys_page_size_ = MHD_DEF_PAGE_SIZE_; 99 MHD_sys_page_size_ = MHD_DEF_PAGE_SIZE_;
99#endif /* _WIN32 */ 100#endif /* _WIN32 */
@@ -151,45 +152,47 @@ MHD_pool_create (size_t max)
151#if defined(MAP_ANONYMOUS) || defined(_WIN32) 152#if defined(MAP_ANONYMOUS) || defined(_WIN32)
152 if ( (max <= 32 * 1024) || 153 if ( (max <= 32 * 1024) ||
153 (max < MHD_sys_page_size_ * 4 / 3) ) 154 (max < MHD_sys_page_size_ * 4 / 3) )
155 {
154 pool->memory = MAP_FAILED; 156 pool->memory = MAP_FAILED;
157 }
155 else 158 else
156 { 159 {
157 /* Round up allocation to page granularity. */ 160 /* Round up allocation to page granularity. */
158 alloc_size = max + MHD_sys_page_size_ - 1; 161 alloc_size = max + MHD_sys_page_size_ - 1;
159 alloc_size -= alloc_size % MHD_sys_page_size_; 162 alloc_size -= alloc_size % MHD_sys_page_size_;
160#if defined(MAP_ANONYMOUS) && !defined(_WIN32) 163#if defined(MAP_ANONYMOUS) && ! defined(_WIN32)
161 pool->memory = mmap (NULL, 164 pool->memory = mmap (NULL,
162 alloc_size, 165 alloc_size,
163 PROT_READ | PROT_WRITE, 166 PROT_READ | PROT_WRITE,
164 MAP_PRIVATE | MAP_ANONYMOUS, 167 MAP_PRIVATE | MAP_ANONYMOUS,
165 -1, 168 -1,
166 0); 169 0);
167#elif defined(_WIN32) 170#elif defined(_WIN32)
168 pool->memory = VirtualAlloc (NULL, 171 pool->memory = VirtualAlloc (NULL,
169 alloc_size, 172 alloc_size,
170 MEM_COMMIT | MEM_RESERVE, 173 MEM_COMMIT | MEM_RESERVE,
171 PAGE_READWRITE); 174 PAGE_READWRITE);
172#endif /* _WIN32 */ 175#endif /* _WIN32 */
173 } 176 }
174#else /* ! _WIN32 && ! MAP_ANONYMOUS */ 177#else /* ! _WIN32 && ! MAP_ANONYMOUS */
175 pool->memory = MAP_FAILED; 178 pool->memory = MAP_FAILED;
176#endif /* ! _WIN32 && ! MAP_ANONYMOUS */ 179#endif /* ! _WIN32 && ! MAP_ANONYMOUS */
177 if (MAP_FAILED == pool->memory) 180 if (MAP_FAILED == pool->memory)
181 {
182 alloc_size = ROUND_TO_ALIGN (max);
183 pool->memory = malloc (alloc_size);
184 if (NULL == pool->memory)
178 { 185 {
179 alloc_size = ROUND_TO_ALIGN(max); 186 free (pool);
180 pool->memory = malloc (alloc_size); 187 return NULL;
181 if (NULL == pool->memory)
182 {
183 free (pool);
184 return NULL;
185 }
186 pool->is_mmap = false;
187 } 188 }
189 pool->is_mmap = false;
190 }
188#if defined(MAP_ANONYMOUS) || defined(_WIN32) 191#if defined(MAP_ANONYMOUS) || defined(_WIN32)
189 else 192 else
190 { 193 {
191 pool->is_mmap = true; 194 pool->is_mmap = true;
192 } 195 }
193#endif /* _WIN32 || MAP_ANONYMOUS */ 196#endif /* _WIN32 || MAP_ANONYMOUS */
194 pool->pos = 0; 197 pool->pos = 0;
195 pool->end = alloc_size; 198 pool->end = alloc_size;
@@ -211,10 +214,10 @@ MHD_pool_destroy (struct MemoryPool *pool)
211 214
212 mhd_assert (pool->end >= pool->pos); 215 mhd_assert (pool->end >= pool->pos);
213 mhd_assert (pool->size >= pool->end - pool->pos); 216 mhd_assert (pool->size >= pool->end - pool->pos);
214 if (!pool->is_mmap) 217 if (! pool->is_mmap)
215 free (pool->memory); 218 free (pool->memory);
216 else 219 else
217#if defined(MAP_ANONYMOUS) && !defined(_WIN32) 220#if defined(MAP_ANONYMOUS) && ! defined(_WIN32)
218 munmap (pool->memory, 221 munmap (pool->memory,
219 pool->size); 222 pool->size);
220#elif defined(_WIN32) 223#elif defined(_WIN32)
@@ -256,7 +259,7 @@ MHD_pool_get_free (struct MemoryPool *pool)
256 */ 259 */
257void * 260void *
258MHD_pool_allocate (struct MemoryPool *pool, 261MHD_pool_allocate (struct MemoryPool *pool,
259 size_t size, 262 size_t size,
260 bool from_end) 263 bool from_end)
261{ 264{
262 void *ret; 265 void *ret;
@@ -271,15 +274,15 @@ MHD_pool_allocate (struct MemoryPool *pool,
271 (pool->pos + asize < pool->pos)) 274 (pool->pos + asize < pool->pos))
272 return NULL; 275 return NULL;
273 if (from_end) 276 if (from_end)
274 { 277 {
275 ret = &pool->memory[pool->end - asize]; 278 ret = &pool->memory[pool->end - asize];
276 pool->end -= asize; 279 pool->end -= asize;
277 } 280 }
278 else 281 else
279 { 282 {
280 ret = &pool->memory[pool->pos]; 283 ret = &pool->memory[pool->pos];
281 pool->pos += asize; 284 pool->pos += asize;
282 } 285 }
283 return ret; 286 return ret;
284} 287}
285 288
@@ -304,8 +307,8 @@ MHD_pool_allocate (struct MemoryPool *pool,
304void * 307void *
305MHD_pool_reallocate (struct MemoryPool *pool, 308MHD_pool_reallocate (struct MemoryPool *pool,
306 void *old, 309 void *old,
307 size_t old_size, 310 size_t old_size,
308 size_t new_size) 311 size_t new_size)
309{ 312{
310 size_t asize; 313 size_t asize;
311 uint8_t *new_blc; 314 uint8_t *new_blc;
@@ -313,36 +316,37 @@ MHD_pool_reallocate (struct MemoryPool *pool,
313 mhd_assert (pool->end >= pool->pos); 316 mhd_assert (pool->end >= pool->pos);
314 mhd_assert (pool->size >= pool->end - pool->pos); 317 mhd_assert (pool->size >= pool->end - pool->pos);
315 mhd_assert (old != NULL || old_size == 0); 318 mhd_assert (old != NULL || old_size == 0);
316 mhd_assert (old == NULL || pool->memory <= (uint8_t*)old); 319 mhd_assert (old == NULL || pool->memory <= (uint8_t*) old);
317 mhd_assert (old == NULL || pool->memory + pool->size >= (uint8_t*)old + old_size); 320 mhd_assert (old == NULL || pool->memory + pool->size >= (uint8_t*) old
321 + old_size);
318 /* Blocks "from the end" must not be reallocated */ 322 /* Blocks "from the end" must not be reallocated */
319 mhd_assert (old == NULL || pool->memory + pool->pos > (uint8_t*)old); 323 mhd_assert (old == NULL || pool->memory + pool->pos > (uint8_t*) old);
320 324
321 if (0 != old_size) 325 if (0 != old_size)
322 { /* Need to save some data */ 326 { /* Need to save some data */
323 const size_t old_offset = (uint8_t*)old - pool->memory; 327 const size_t old_offset = (uint8_t*) old - pool->memory;
324 const bool shrinking = (old_size > new_size); 328 const bool shrinking = (old_size > new_size);
325 /* Try resizing in-place */ 329 /* Try resizing in-place */
326 if (shrinking) 330 if (shrinking)
327 { /* Shrinking in-place, zero-out freed part */ 331 { /* Shrinking in-place, zero-out freed part */
328 memset ((uint8_t*)old + new_size, 0, old_size - new_size); 332 memset ((uint8_t*) old + new_size, 0, old_size - new_size);
329 } 333 }
330 if (pool->pos == ROUND_TO_ALIGN (old_offset + old_size)) 334 if (pool->pos == ROUND_TO_ALIGN (old_offset + old_size))
331 { /* "old" block is the last allocated block */ 335 { /* "old" block is the last allocated block */
332 const size_t new_apos = ROUND_TO_ALIGN (old_offset + new_size); 336 const size_t new_apos = ROUND_TO_ALIGN (old_offset + new_size);
333 if (!shrinking) 337 if (! shrinking)
334 { /* Grow in-place, check for enough space. */ 338 { /* Grow in-place, check for enough space. */
335 if ( (new_apos > pool->end) || 339 if ( (new_apos > pool->end) ||
336 (new_apos < pool->pos) ) /* Value wrap */ 340 (new_apos < pool->pos) ) /* Value wrap */
337 return NULL; /* No space */ 341 return NULL; /* No space */
338 } 342 }
339 /* Resized in-place */ 343 /* Resized in-place */
340 pool->pos = new_apos; 344 pool->pos = new_apos;
341 return old; 345 return old;
342 }
343 if (shrinking)
344 return old; /* Resized in-place, freed part remains allocated */
345 } 346 }
347 if (shrinking)
348 return old; /* Resized in-place, freed part remains allocated */
349 }
346 /* Need to allocate new block */ 350 /* Need to allocate new block */
347 asize = ROUND_TO_ALIGN (new_size); 351 asize = ROUND_TO_ALIGN (new_size);
348 if ( ( (0 == asize) && 352 if ( ( (0 == asize) &&
@@ -354,12 +358,12 @@ MHD_pool_reallocate (struct MemoryPool *pool,
354 pool->pos += asize; 358 pool->pos += asize;
355 359
356 if (0 != old_size) 360 if (0 != old_size)
357 { 361 {
358 /* Move data to new block, old block remains allocated */ 362 /* Move data to new block, old block remains allocated */
359 memcpy (new_blc, old, old_size); 363 memcpy (new_blc, old, old_size);
360 /* Zero-out old block */ 364 /* Zero-out old block */
361 memset (old, 0, old_size); 365 memset (old, 0, old_size);
362 } 366 }
363 return new_blc; 367 return new_blc;
364} 368}
365 369
@@ -379,59 +383,60 @@ MHD_pool_reallocate (struct MemoryPool *pool,
379 */ 383 */
380void * 384void *
381MHD_pool_reset (struct MemoryPool *pool, 385MHD_pool_reset (struct MemoryPool *pool,
382 void *keep, 386 void *keep,
383 size_t copy_bytes, 387 size_t copy_bytes,
384 size_t new_size) 388 size_t new_size)
385{ 389{
386 mhd_assert (pool->end >= pool->pos); 390 mhd_assert (pool->end >= pool->pos);
387 mhd_assert (pool->size >= pool->end - pool->pos); 391 mhd_assert (pool->size >= pool->end - pool->pos);
388 mhd_assert (copy_bytes < new_size); 392 mhd_assert (copy_bytes < new_size);
389 mhd_assert (keep != NULL || copy_bytes == 0); 393 mhd_assert (keep != NULL || copy_bytes == 0);
390 mhd_assert (keep == NULL || pool->memory <= (uint8_t*)keep); 394 mhd_assert (keep == NULL || pool->memory <= (uint8_t*) keep);
391 mhd_assert (keep == NULL || pool->memory + pool->size >= (uint8_t*)keep + copy_bytes); 395 mhd_assert (keep == NULL || pool->memory + pool->size >= (uint8_t*) keep
396 + copy_bytes);
392 if ( (NULL != keep) && 397 if ( (NULL != keep) &&
393 (keep != pool->memory) ) 398 (keep != pool->memory) )
394 { 399 {
395 if (0 != copy_bytes) 400 if (0 != copy_bytes)
396 memmove (pool->memory, 401 memmove (pool->memory,
397 keep, 402 keep,
398 copy_bytes); 403 copy_bytes);
399 } 404 }
400 /* technically not needed, but safer to zero out */ 405 /* technically not needed, but safer to zero out */
401 if (pool->size > copy_bytes) 406 if (pool->size > copy_bytes)
402 { 407 {
403 size_t to_zero; /** Size of area to zero-out */ 408 size_t to_zero; /** Size of area to zero-out */
404 409
405 to_zero = pool->size - copy_bytes; 410 to_zero = pool->size - copy_bytes;
406#ifdef _WIN32 411#ifdef _WIN32
407 if (pool->is_mmap) 412 if (pool->is_mmap)
408 { 413 {
409 size_t to_recommit; /** Size of decommitted and re-committed area. */ 414 size_t to_recommit; /** Size of decommitted and re-committed area. */
410 uint8_t *recommit_addr; 415 uint8_t *recommit_addr;
411 /* Round down to page size */ 416 /* Round down to page size */
412 to_recommit = to_zero - to_zero % MHD_sys_page_size_; 417 to_recommit = to_zero - to_zero % MHD_sys_page_size_;
413 recommit_addr = pool->memory + pool->size - to_recommit; 418 recommit_addr = pool->memory + pool->size - to_recommit;
414 419
415 /* De-committing and re-committing again clear memory and make 420 /* De-committing and re-committing again clear memory and make
416 * pages free / available for other needs until accessed. */ 421 * pages free / available for other needs until accessed. */
417 if (VirtualFree (recommit_addr, 422 if (VirtualFree (recommit_addr,
418 to_recommit, 423 to_recommit,
419 MEM_DECOMMIT)) 424 MEM_DECOMMIT))
420 { 425 {
421 to_zero -= to_recommit; 426 to_zero -= to_recommit;
422 427
423 if (recommit_addr != VirtualAlloc (recommit_addr, 428 if (recommit_addr != VirtualAlloc (recommit_addr,
424 to_recommit, 429 to_recommit,
425 MEM_COMMIT, 430 MEM_COMMIT,
426 PAGE_READWRITE)) 431 PAGE_READWRITE))
427 abort(); /* Serious error, must never happen */ 432 abort (); /* Serious error, must never happen */
428 } 433 }
429 }
430#endif /* _WIN32 */
431 memset (&pool->memory[copy_bytes],
432 0,
433 to_zero);
434 } 434 }
435#endif /* _WIN32 */
436 memset (&pool->memory[copy_bytes],
437 0,
438 to_zero);
439 }
435 pool->pos = ROUND_TO_ALIGN (new_size); 440 pool->pos = ROUND_TO_ALIGN (new_size);
436 pool->end = pool->size; 441 pool->end = pool->size;
437 return pool->memory; 442 return pool->memory;
diff --git a/src/microhttpd/mhd_threads.c b/src/microhttpd/mhd_threads.c
index 6578e4b1..6be4cb44 100644
--- a/src/microhttpd/mhd_threads.c
+++ b/src/microhttpd/mhd_threads.c
@@ -46,12 +46,14 @@
46#else /* MHD_USE_THREAD_NAME_ */ 46#else /* MHD_USE_THREAD_NAME_ */
47 47
48#if defined(MHD_USE_POSIX_THREADS) 48#if defined(MHD_USE_POSIX_THREADS)
49#if defined(HAVE_PTHREAD_ATTR_SETNAME_NP_NETBSD) || defined(HAVE_PTHREAD_ATTR_SETNAME_NP_IBMI) 49#if defined(HAVE_PTHREAD_ATTR_SETNAME_NP_NETBSD) || \
50 defined(HAVE_PTHREAD_ATTR_SETNAME_NP_IBMI)
50# define MHD_USE_THREAD_ATTR_SETNAME 1 51# define MHD_USE_THREAD_ATTR_SETNAME 1
51#endif /* HAVE_PTHREAD_ATTR_SETNAME_NP_NETBSD || HAVE_PTHREAD_ATTR_SETNAME_NP_IBMI */ 52#endif /* HAVE_PTHREAD_ATTR_SETNAME_NP_NETBSD || HAVE_PTHREAD_ATTR_SETNAME_NP_IBMI */
52 53
53#if defined(HAVE_PTHREAD_SETNAME_NP_GNU) || defined(HAVE_PTHREAD_SET_NAME_NP_FREEBSD) \ 54#if defined(HAVE_PTHREAD_SETNAME_NP_GNU) || \
54 || defined(HAVE_PTHREAD_SETNAME_NP_NETBSD) 55 defined(HAVE_PTHREAD_SET_NAME_NP_FREEBSD) \
56 || defined(HAVE_PTHREAD_SETNAME_NP_NETBSD)
55 57
56/** 58/**
57 * Set thread name 59 * Set thread name
@@ -61,25 +63,25 @@
61 * @return non-zero on success, zero otherwise 63 * @return non-zero on success, zero otherwise
62 */ 64 */
63static int 65static int
64MHD_set_thread_name_(const MHD_thread_ID_ thread_id, 66MHD_set_thread_name_ (const MHD_thread_ID_ thread_id,
65 const char *thread_name) 67 const char *thread_name)
66{ 68{
67 if (NULL == thread_name) 69 if (NULL == thread_name)
68 return 0; 70 return 0;
69 71
70#if defined(HAVE_PTHREAD_SETNAME_NP_GNU) 72#if defined(HAVE_PTHREAD_SETNAME_NP_GNU)
71 return !pthread_setname_np (thread_id, thread_name); 73 return ! pthread_setname_np (thread_id, thread_name);
72#elif defined(HAVE_PTHREAD_SET_NAME_NP_FREEBSD) 74#elif defined(HAVE_PTHREAD_SET_NAME_NP_FREEBSD)
73 /* FreeBSD and OpenBSD use different name and void return type */ 75 /* FreeBSD and OpenBSD use different name and void return type */
74 pthread_set_name_np (thread_id, thread_name); 76 pthread_set_name_np (thread_id, thread_name);
75 return !0; 77 return ! 0;
76#elif defined(HAVE_PTHREAD_SETNAME_NP_NETBSD) 78#elif defined(HAVE_PTHREAD_SETNAME_NP_NETBSD)
77 /* NetBSD use 3 arguments: second argument is string in printf-like format, 79 /* NetBSD use 3 arguments: second argument is string in printf-like format,
78 * third argument is single argument for printf; 80 * third argument is single argument for printf;
79 * OSF1 use 3 arguments too, but last one always must be zero (NULL). 81 * OSF1 use 3 arguments too, but last one always must be zero (NULL).
80 * MHD doesn't use '%' in thread names, so both form are used in same way. 82 * MHD doesn't use '%' in thread names, so both form are used in same way.
81 */ 83 */
82 return !pthread_setname_np (thread_id, thread_name, 0); 84 return ! pthread_setname_np (thread_id, thread_name, 0);
83#endif /* HAVE_PTHREAD_SETNAME_NP_NETBSD */ 85#endif /* HAVE_PTHREAD_SETNAME_NP_NETBSD */
84} 86}
85 87
@@ -90,10 +92,10 @@ MHD_set_thread_name_(const MHD_thread_ID_ thread_id,
90 * @param n name to set 92 * @param n name to set
91 * @return non-zero on success, zero otherwise 93 * @return non-zero on success, zero otherwise
92 */ 94 */
93#define MHD_set_cur_thread_name_(n) MHD_set_thread_name_(pthread_self(),(n)) 95#define MHD_set_cur_thread_name_(n) MHD_set_thread_name_ (pthread_self (),(n))
94#else /* __QNXNTO__ */ 96#else /* __QNXNTO__ */
95/* Special case for QNX Neutrino - using zero for thread ID sets name faster. */ 97/* Special case for QNX Neutrino - using zero for thread ID sets name faster. */
96#define MHD_set_cur_thread_name_(n) MHD_set_thread_name_(0,(n)) 98#define MHD_set_cur_thread_name_(n) MHD_set_thread_name_ (0,(n))
97#endif /* __QNXNTO__ */ 99#endif /* __QNXNTO__ */
98#elif defined(HAVE_PTHREAD_SETNAME_NP_DARWIN) 100#elif defined(HAVE_PTHREAD_SETNAME_NP_DARWIN)
99 101
@@ -102,7 +104,7 @@ MHD_set_thread_name_(const MHD_thread_ID_ thread_id,
102 * @param n name to set 104 * @param n name to set
103 * @return non-zero on success, zero otherwise 105 * @return non-zero on success, zero otherwise
104 */ 106 */
105#define MHD_set_cur_thread_name_(n) (!(pthread_setname_np((n)))) 107#define MHD_set_cur_thread_name_(n) (! (pthread_setname_np ((n))))
106#endif /* HAVE_PTHREAD_SETNAME_NP_DARWIN */ 108#endif /* HAVE_PTHREAD_SETNAME_NP_DARWIN */
107 109
108#elif defined(MHD_USE_W32_THREADS) 110#elif defined(MHD_USE_W32_THREADS)
@@ -117,8 +119,8 @@ MHD_set_thread_name_(const MHD_thread_ID_ thread_id,
117 * @return non-zero on success, zero otherwise 119 * @return non-zero on success, zero otherwise
118 */ 120 */
119static int 121static int
120MHD_set_thread_name_(const MHD_thread_ID_ thread_id, 122MHD_set_thread_name_ (const MHD_thread_ID_ thread_id,
121 const char *thread_name) 123 const char *thread_name)
122{ 124{
123 static const DWORD VC_SETNAME_EXC = 0x406D1388; 125 static const DWORD VC_SETNAME_EXC = 0x406D1388;
124#pragma pack(push,8) 126#pragma pack(push,8)
@@ -149,7 +151,7 @@ MHD_set_thread_name_(const MHD_thread_ID_ thread_id,
149 __except (EXCEPTION_EXECUTE_HANDLER) 151 __except (EXCEPTION_EXECUTE_HANDLER)
150 {} 152 {}
151 153
152 return !0; 154 return ! 0;
153} 155}
154 156
155 157
@@ -158,7 +160,7 @@ MHD_set_thread_name_(const MHD_thread_ID_ thread_id,
158 * @param n name to set 160 * @param n name to set
159 * @return non-zero on success, zero otherwise 161 * @return non-zero on success, zero otherwise
160 */ 162 */
161#define MHD_set_cur_thread_name_(n) MHD_set_thread_name_(-1,(n)) 163#define MHD_set_cur_thread_name_(n) MHD_set_thread_name_ (-1,(n))
162#endif /* _MSC_FULL_VER */ 164#endif /* _MSC_FULL_VER */
163#endif /* MHD_USE_W32_THREADS */ 165#endif /* MHD_USE_W32_THREADS */
164 166
@@ -184,21 +186,21 @@ MHD_create_thread_ (MHD_thread_handle_ID_ *thread,
184 int res; 186 int res;
185 187
186 if (0 != stack_size) 188 if (0 != stack_size)
189 {
190 pthread_attr_t attr;
191 res = pthread_attr_init (&attr);
192 if (0 == res)
187 { 193 {
188 pthread_attr_t attr; 194 res = pthread_attr_setstacksize (&attr,
189 res = pthread_attr_init (&attr); 195 stack_size);
190 if (0 == res) 196 if (0 == res)
191 { 197 res = pthread_create (&(thread->handle),
192 res = pthread_attr_setstacksize (&attr, 198 &attr,
193 stack_size); 199 start_routine,
194 if (0 == res) 200 arg);
195 res = pthread_create (&(thread->handle), 201 pthread_attr_destroy (&attr);
196 &attr,
197 start_routine,
198 arg);
199 pthread_attr_destroy (&attr);
200 }
201 } 202 }
203 }
202 else 204 else
203 res = pthread_create (&(thread->handle), 205 res = pthread_create (&(thread->handle),
204 NULL, 206 NULL,
@@ -208,28 +210,28 @@ MHD_create_thread_ (MHD_thread_handle_ID_ *thread,
208 if (0 != res) 210 if (0 != res)
209 errno = res; 211 errno = res;
210 212
211 return !res; 213 return ! res;
212#elif defined(MHD_USE_W32_THREADS) 214#elif defined(MHD_USE_W32_THREADS)
213#if SIZE_MAX != UINT_MAX 215#if SIZE_MAX != UINT_MAX
214 if (stack_size > UINT_MAX) 216 if (stack_size > UINT_MAX)
215 { 217 {
216 errno = EINVAL; 218 errno = EINVAL;
217 return 0; 219 return 0;
218 } 220 }
219#endif /* SIZE_MAX != UINT_MAX */ 221#endif /* SIZE_MAX != UINT_MAX */
220 222
221 thread->handle = (MHD_thread_handle_) 223 thread->handle = (MHD_thread_handle_)
222 _beginthreadex (NULL, 224 _beginthreadex (NULL,
223 (unsigned int) stack_size, 225 (unsigned int) stack_size,
224 start_routine, 226 start_routine,
225 arg, 227 arg,
226 0, 228 0,
227 NULL); 229 NULL);
228 230
229 if ((MHD_thread_handle_)-1 == thread->handle) 231 if ((MHD_thread_handle_) - 1 == thread->handle)
230 return 0; 232 return 0;
231 233
232 return !0; 234 return ! 0;
233#endif 235#endif
234} 236}
235 237
@@ -258,21 +260,21 @@ struct MHD_named_helper_param_
258static MHD_THRD_RTRN_TYPE_ MHD_THRD_CALL_SPEC_ 260static MHD_THRD_RTRN_TYPE_ MHD_THRD_CALL_SPEC_
259named_thread_starter (void *data) 261named_thread_starter (void *data)
260{ 262{
261 struct MHD_named_helper_param_ * const param = 263 struct MHD_named_helper_param_ *const param =
262 (struct MHD_named_helper_param_ *) data; 264 (struct MHD_named_helper_param_ *) data;
263 void * arg; 265 void *arg;
264 MHD_THREAD_START_ROUTINE_ thr_func; 266 MHD_THREAD_START_ROUTINE_ thr_func;
265 267
266 if (NULL == data) 268 if (NULL == data)
267 return (MHD_THRD_RTRN_TYPE_)0; 269 return (MHD_THRD_RTRN_TYPE_) 0;
268 270
269 MHD_set_cur_thread_name_ (param->name); 271 MHD_set_cur_thread_name_ (param->name);
270 272
271 arg = param->arg; 273 arg = param->arg;
272 thr_func = param->start_routine; 274 thr_func = param->start_routine;
273 free(data); 275 free (data);
274 276
275 return thr_func(arg); 277 return thr_func (arg);
276} 278}
277#endif /* ! MHD_USE_THREAD_ATTR_SETNAME */ 279#endif /* ! MHD_USE_THREAD_ATTR_SETNAME */
278 280
@@ -289,7 +291,7 @@ named_thread_starter (void *data)
289 */ 291 */
290int 292int
291MHD_create_named_thread_ (MHD_thread_handle_ID_ *thread, 293MHD_create_named_thread_ (MHD_thread_handle_ID_ *thread,
292 const char* thread_name, 294 const char*thread_name,
293 size_t stack_size, 295 size_t stack_size,
294 MHD_THREAD_START_ROUTINE_ start_routine, 296 MHD_THREAD_START_ROUTINE_ start_routine,
295 void *arg) 297 void *arg)
@@ -300,41 +302,44 @@ MHD_create_named_thread_ (MHD_thread_handle_ID_ *thread,
300 302
301 res = pthread_attr_init (&attr); 303 res = pthread_attr_init (&attr);
302 if (0 == res) 304 if (0 == res)
303 { 305 {
304#if defined(HAVE_PTHREAD_ATTR_SETNAME_NP_NETBSD) 306#if defined(HAVE_PTHREAD_ATTR_SETNAME_NP_NETBSD)
305 /* NetBSD use 3 arguments: second argument is string in printf-like format, 307 /* NetBSD use 3 arguments: second argument is string in printf-like format,
306 * third argument is single argument for printf; 308 * third argument is single argument for printf;
307 * OSF1 use 3 arguments too, but last one always must be zero (NULL). 309 * OSF1 use 3 arguments too, but last one always must be zero (NULL).
308 * MHD doesn't use '%' in thread names, so both form are used in same way. 310 * MHD doesn't use '%' in thread names, so both form are used in same way.
309 */ 311 */
310 res = pthread_attr_setname_np (&attr, thread_name, 0); 312 res = pthread_attr_setname_np (&attr,
313 thread_name,
314 0);
311#elif defined(HAVE_PTHREAD_ATTR_SETNAME_NP_IBMI) 315#elif defined(HAVE_PTHREAD_ATTR_SETNAME_NP_IBMI)
312 res = pthread_attr_setname_np (&attr, thread_name); 316 res = pthread_attr_setname_np (&attr,
317 thread_name);
313#else 318#else
314#error No pthread_attr_setname_np() function. 319#error No pthread_attr_setname_np() function.
315#endif 320#endif
316 if (res == 0 && 0 != stack_size) 321 if ((res == 0) &&(0 != stack_size) )
317 res = pthread_attr_setstacksize (&attr, 322 res = pthread_attr_setstacksize (&attr,
318 stack_size); 323 stack_size);
319 if (0 == res) 324 if (0 == res)
320 res = pthread_create (&(thread->handle), 325 res = pthread_create (&(thread->handle),
321 &attr, 326 &attr,
322 start_routine, 327 start_routine,
323 arg); 328 arg);
324 pthread_attr_destroy (&attr); 329 pthread_attr_destroy (&attr);
325 } 330 }
326 if (0 != res) 331 if (0 != res)
327 errno = res; 332 errno = res;
328 333
329 return !res; 334 return ! res;
330#else /* ! MHD_USE_THREAD_ATTR_SETNAME */ 335#else /* ! MHD_USE_THREAD_ATTR_SETNAME */
331 struct MHD_named_helper_param_ *param; 336 struct MHD_named_helper_param_ *param;
332 337
333 if (NULL == thread_name) 338 if (NULL == thread_name)
334 { 339 {
335 errno = EINVAL; 340 errno = EINVAL;
336 return 0; 341 return 0;
337 } 342 }
338 343
339 param = malloc (sizeof (struct MHD_named_helper_param_)); 344 param = malloc (sizeof (struct MHD_named_helper_param_));
340 if (NULL == param) 345 if (NULL == param)
@@ -347,16 +352,16 @@ MHD_create_named_thread_ (MHD_thread_handle_ID_ *thread,
347 /* Set thread name in thread itself to avoid problems with 352 /* Set thread name in thread itself to avoid problems with
348 * threads which terminated before name is set in other thread. 353 * threads which terminated before name is set in other thread.
349 */ 354 */
350 if (! MHD_create_thread_(thread, 355 if (! MHD_create_thread_ (thread,
351 stack_size, 356 stack_size,
352 &named_thread_starter, 357 &named_thread_starter,
353 (void*)param)) 358 (void*) param))
354 { 359 {
355 free (param); 360 free (param);
356 return 0; 361 return 0;
357 } 362 }
358 363
359 return !0; 364 return ! 0;
360#endif /* ! MHD_USE_THREAD_ATTR_SETNAME */ 365#endif /* ! MHD_USE_THREAD_ATTR_SETNAME */
361} 366}
362 367