diff options
Diffstat (limited to 'src/util/strings.c')
-rw-r--r-- | src/util/strings.c | 1789 |
1 files changed, 895 insertions, 894 deletions
diff --git a/src/util/strings.c b/src/util/strings.c index 25b09f8e2..3442a6bf7 100644 --- a/src/util/strings.c +++ b/src/util/strings.c | |||
@@ -34,10 +34,10 @@ | |||
34 | #include <unistr.h> | 34 | #include <unistr.h> |
35 | #include <uniconv.h> | 35 | #include <uniconv.h> |
36 | 36 | ||
37 | #define LOG(kind, ...) GNUNET_log_from(kind, "util-strings", __VA_ARGS__) | 37 | #define LOG(kind, ...) GNUNET_log_from (kind, "util-strings", __VA_ARGS__) |
38 | 38 | ||
39 | #define LOG_STRERROR(kind, syscall) \ | 39 | #define LOG_STRERROR(kind, syscall) \ |
40 | GNUNET_log_from_strerror(kind, "util-strings", syscall) | 40 | GNUNET_log_from_strerror (kind, "util-strings", syscall) |
41 | 41 | ||
42 | 42 | ||
43 | /** | 43 | /** |
@@ -62,7 +62,7 @@ | |||
62 | * (or number of bytes that would have been written) | 62 | * (or number of bytes that would have been written) |
63 | */ | 63 | */ |
64 | size_t | 64 | size_t |
65 | GNUNET_STRINGS_buffer_fill(char *buffer, size_t size, unsigned int count, ...) | 65 | GNUNET_STRINGS_buffer_fill (char *buffer, size_t size, unsigned int count, ...) |
66 | { | 66 | { |
67 | size_t needed; | 67 | size_t needed; |
68 | size_t slen; | 68 | size_t slen; |
@@ -70,21 +70,21 @@ GNUNET_STRINGS_buffer_fill(char *buffer, size_t size, unsigned int count, ...) | |||
70 | va_list ap; | 70 | va_list ap; |
71 | 71 | ||
72 | needed = 0; | 72 | needed = 0; |
73 | va_start(ap, count); | 73 | va_start (ap, count); |
74 | while (count > 0) | 74 | while (count > 0) |
75 | { | 75 | { |
76 | s = va_arg(ap, const char *); | 76 | s = va_arg (ap, const char *); |
77 | 77 | ||
78 | slen = strlen(s) + 1; | 78 | slen = strlen (s) + 1; |
79 | if (buffer != NULL) | 79 | if (buffer != NULL) |
80 | { | 80 | { |
81 | GNUNET_assert(needed + slen <= size); | 81 | GNUNET_assert (needed + slen <= size); |
82 | GNUNET_memcpy(&buffer[needed], s, slen); | 82 | GNUNET_memcpy (&buffer[needed], s, slen); |
83 | } | ||
84 | needed += slen; | ||
85 | count--; | ||
86 | } | 83 | } |
87 | va_end(ap); | 84 | needed += slen; |
85 | count--; | ||
86 | } | ||
87 | va_end (ap); | ||
88 | return needed; | 88 | return needed; |
89 | } | 89 | } |
90 | 90 | ||
@@ -97,23 +97,23 @@ GNUNET_STRINGS_buffer_fill(char *buffer, size_t size, unsigned int count, ...) | |||
97 | * @return string representing the array of @a pids | 97 | * @return string representing the array of @a pids |
98 | */ | 98 | */ |
99 | char * | 99 | char * |
100 | GNUNET_STRINGS_pp2s(const struct GNUNET_PeerIdentity *pids, | 100 | GNUNET_STRINGS_pp2s (const struct GNUNET_PeerIdentity *pids, |
101 | unsigned int num_pids) | 101 | unsigned int num_pids) |
102 | { | 102 | { |
103 | char *buf; | 103 | char *buf; |
104 | size_t off; | 104 | size_t off; |
105 | size_t plen = num_pids * 5 + 1; | 105 | size_t plen = num_pids * 5 + 1; |
106 | 106 | ||
107 | off = 0; | 107 | off = 0; |
108 | buf = GNUNET_malloc(plen); | 108 | buf = GNUNET_malloc (plen); |
109 | for (unsigned int i = 0; i < num_pids; i++) | 109 | for (unsigned int i = 0; i < num_pids; i++) |
110 | { | 110 | { |
111 | off += GNUNET_snprintf(&buf[off], | 111 | off += GNUNET_snprintf (&buf[off], |
112 | plen - off, | 112 | plen - off, |
113 | "%s%s", | 113 | "%s%s", |
114 | GNUNET_i2s(&pids[i]), | 114 | GNUNET_i2s (&pids[i]), |
115 | (i == num_pids - 1) ? "" : "-"); | 115 | (i == num_pids - 1) ? "" : "-"); |
116 | } | 116 | } |
117 | return buf; | 117 | return buf; |
118 | } | 118 | } |
119 | 119 | ||
@@ -132,10 +132,10 @@ GNUNET_STRINGS_pp2s(const struct GNUNET_PeerIdentity *pids, | |||
132 | * in the buffer, or 0 on error. | 132 | * in the buffer, or 0 on error. |
133 | */ | 133 | */ |
134 | unsigned int | 134 | unsigned int |
135 | GNUNET_STRINGS_buffer_tokenize(const char *buffer, | 135 | GNUNET_STRINGS_buffer_tokenize (const char *buffer, |
136 | size_t size, | 136 | size_t size, |
137 | unsigned int count, | 137 | unsigned int count, |
138 | ...) | 138 | ...) |
139 | { | 139 | { |
140 | unsigned int start; | 140 | unsigned int start; |
141 | unsigned int needed; | 141 | unsigned int needed; |
@@ -143,24 +143,24 @@ GNUNET_STRINGS_buffer_tokenize(const char *buffer, | |||
143 | va_list ap; | 143 | va_list ap; |
144 | 144 | ||
145 | needed = 0; | 145 | needed = 0; |
146 | va_start(ap, count); | 146 | va_start (ap, count); |
147 | while (count > 0) | 147 | while (count > 0) |
148 | { | 148 | { |
149 | r = va_arg(ap, const char **); | 149 | r = va_arg (ap, const char **); |
150 | 150 | ||
151 | start = needed; | 151 | start = needed; |
152 | while ((needed < size) && (buffer[needed] != '\0')) | 152 | while ((needed < size) && (buffer[needed] != '\0')) |
153 | needed++; | 153 | needed++; |
154 | if (needed == size) | 154 | if (needed == size) |
155 | { | 155 | { |
156 | va_end(ap); | 156 | va_end (ap); |
157 | return 0; /* error */ | 157 | return 0; /* error */ |
158 | } | ||
159 | *r = &buffer[start]; | ||
160 | needed++; /* skip 0-termination */ | ||
161 | count--; | ||
162 | } | 158 | } |
163 | va_end(ap); | 159 | *r = &buffer[start]; |
160 | needed++; /* skip 0-termination */ | ||
161 | count--; | ||
162 | } | ||
163 | va_end (ap); | ||
164 | return needed; | 164 | return needed; |
165 | } | 165 | } |
166 | 166 | ||
@@ -172,33 +172,33 @@ GNUNET_STRINGS_buffer_tokenize(const char *buffer, | |||
172 | * @return fancy representation of the size (possibly rounded) for humans | 172 | * @return fancy representation of the size (possibly rounded) for humans |
173 | */ | 173 | */ |
174 | char * | 174 | char * |
175 | GNUNET_STRINGS_byte_size_fancy(unsigned long long size) | 175 | GNUNET_STRINGS_byte_size_fancy (unsigned long long size) |
176 | { | 176 | { |
177 | const char *unit = _(/* size unit */ "b"); | 177 | const char *unit = _ (/* size unit */ "b"); |
178 | char *ret; | 178 | char *ret; |
179 | 179 | ||
180 | if (size > 5 * 1024) | 180 | if (size > 5 * 1024) |
181 | { | ||
182 | size = size / 1024; | ||
183 | unit = "KiB"; | ||
184 | if (size > 5 * 1024) | ||
181 | { | 185 | { |
182 | size = size / 1024; | 186 | size = size / 1024; |
183 | unit = "KiB"; | 187 | unit = "MiB"; |
184 | if (size > 5 * 1024) | 188 | if (size > 5 * 1024) |
189 | { | ||
190 | size = size / 1024; | ||
191 | unit = "GiB"; | ||
192 | if (size > 5 * 1024) | ||
185 | { | 193 | { |
186 | size = size / 1024; | 194 | size = size / 1024; |
187 | unit = "MiB"; | 195 | unit = "TiB"; |
188 | if (size > 5 * 1024) | ||
189 | { | ||
190 | size = size / 1024; | ||
191 | unit = "GiB"; | ||
192 | if (size > 5 * 1024) | ||
193 | { | ||
194 | size = size / 1024; | ||
195 | unit = "TiB"; | ||
196 | } | ||
197 | } | ||
198 | } | 196 | } |
197 | } | ||
199 | } | 198 | } |
200 | ret = GNUNET_malloc(32); | 199 | } |
201 | GNUNET_snprintf(ret, 32, "%llu %s", size, unit); | 200 | ret = GNUNET_malloc (32); |
201 | GNUNET_snprintf (ret, 32, "%llu %s", size, unit); | ||
202 | return ret; | 202 | return ret; |
203 | } | 203 | } |
204 | 204 | ||
@@ -216,13 +216,13 @@ GNUNET_STRINGS_byte_size_fancy(unsigned long long size) | |||
216 | * null byte | 216 | * null byte |
217 | */ | 217 | */ |
218 | size_t | 218 | size_t |
219 | GNUNET_strlcpy(char *dst, const char *src, size_t n) | 219 | GNUNET_strlcpy (char *dst, const char *src, size_t n) |
220 | { | 220 | { |
221 | size_t slen; | 221 | size_t slen; |
222 | 222 | ||
223 | GNUNET_assert(0 != n); | 223 | GNUNET_assert (0 != n); |
224 | slen = strnlen(src, n - 1); | 224 | slen = strnlen (src, n - 1); |
225 | memcpy(dst, src, slen); | 225 | memcpy (dst, src, slen); |
226 | dst[slen] = '\0'; | 226 | dst[slen] = '\0'; |
227 | return slen; | 227 | return slen; |
228 | } | 228 | } |
@@ -231,7 +231,8 @@ GNUNET_strlcpy(char *dst, const char *src, size_t n) | |||
231 | /** | 231 | /** |
232 | * Unit conversion table entry for 'convert_with_table'. | 232 | * Unit conversion table entry for 'convert_with_table'. |
233 | */ | 233 | */ |
234 | struct ConversionTable { | 234 | struct ConversionTable |
235 | { | ||
235 | /** | 236 | /** |
236 | * Name of the unit (or NULL for end of table). | 237 | * Name of the unit (or NULL for end of table). |
237 | */ | 238 | */ |
@@ -256,9 +257,9 @@ struct ConversionTable { | |||
256 | * @return #GNUNET_OK on success, #GNUNET_SYSERR on error | 257 | * @return #GNUNET_OK on success, #GNUNET_SYSERR on error |
257 | */ | 258 | */ |
258 | static int | 259 | static int |
259 | convert_with_table(const char *input, | 260 | convert_with_table (const char *input, |
260 | const struct ConversionTable *table, | 261 | const struct ConversionTable *table, |
261 | unsigned long long *output) | 262 | unsigned long long *output) |
262 | { | 263 | { |
263 | unsigned long long ret; | 264 | unsigned long long ret; |
264 | char *in; | 265 | char *in; |
@@ -268,41 +269,41 @@ convert_with_table(const char *input, | |||
268 | 269 | ||
269 | ret = 0; | 270 | ret = 0; |
270 | last = 0; | 271 | last = 0; |
271 | in = GNUNET_strdup(input); | 272 | in = GNUNET_strdup (input); |
272 | for (tok = strtok(in, " "); tok != NULL; tok = strtok(NULL, " ")) | 273 | for (tok = strtok (in, " "); tok != NULL; tok = strtok (NULL, " ")) |
274 | { | ||
275 | do | ||
273 | { | 276 | { |
274 | do | 277 | i = 0; |
278 | while ((table[i].name != NULL) && (0 != strcasecmp (table[i].name, tok))) | ||
279 | i++; | ||
280 | if (table[i].name != NULL) | ||
281 | { | ||
282 | last *= table[i].value; | ||
283 | break; /* next tok */ | ||
284 | } | ||
285 | else | ||
286 | { | ||
287 | char *endptr; | ||
288 | ret += last; | ||
289 | errno = 0; | ||
290 | last = strtoull (tok, &endptr, 10); | ||
291 | if ((0 != errno) || (endptr == tok)) | ||
275 | { | 292 | { |
276 | i = 0; | 293 | GNUNET_free (in); |
277 | while ((table[i].name != NULL) && (0 != strcasecmp(table[i].name, tok))) | 294 | return GNUNET_SYSERR; /* expected number */ |
278 | i++; | ||
279 | if (table[i].name != NULL) | ||
280 | { | ||
281 | last *= table[i].value; | ||
282 | break; /* next tok */ | ||
283 | } | ||
284 | else | ||
285 | { | ||
286 | char *endptr; | ||
287 | ret += last; | ||
288 | errno = 0; | ||
289 | last = strtoull(tok, &endptr, 10); | ||
290 | if ((0 != errno) || (endptr == tok)) | ||
291 | { | ||
292 | GNUNET_free(in); | ||
293 | return GNUNET_SYSERR; /* expected number */ | ||
294 | } | ||
295 | if ('\0' == endptr[0]) | ||
296 | break; /* next tok */ | ||
297 | else | ||
298 | tok = endptr; /* and re-check (handles times like "10s") */ | ||
299 | } | ||
300 | } | 295 | } |
301 | while (GNUNET_YES); | 296 | if ('\0' == endptr[0]) |
297 | break; /* next tok */ | ||
298 | else | ||
299 | tok = endptr; /* and re-check (handles times like "10s") */ | ||
300 | } | ||
302 | } | 301 | } |
302 | while (GNUNET_YES); | ||
303 | } | ||
303 | ret += last; | 304 | ret += last; |
304 | *output = ret; | 305 | *output = ret; |
305 | GNUNET_free(in); | 306 | GNUNET_free (in); |
306 | return GNUNET_OK; | 307 | return GNUNET_OK; |
307 | } | 308 | } |
308 | 309 | ||
@@ -315,8 +316,8 @@ convert_with_table(const char *input, | |||
315 | * @return #GNUNET_OK on success, #GNUNET_SYSERR on error | 316 | * @return #GNUNET_OK on success, #GNUNET_SYSERR on error |
316 | */ | 317 | */ |
317 | int | 318 | int |
318 | GNUNET_STRINGS_fancy_size_to_bytes(const char *fancy_size, | 319 | GNUNET_STRINGS_fancy_size_to_bytes (const char *fancy_size, |
319 | unsigned long long *size) | 320 | unsigned long long *size) |
320 | { | 321 | { |
321 | static const struct ConversionTable table[] = | 322 | static const struct ConversionTable table[] = |
322 | { { "B", 1 }, | 323 | { { "B", 1 }, |
@@ -334,7 +335,7 @@ GNUNET_STRINGS_fancy_size_to_bytes(const char *fancy_size, | |||
334 | { "EB", 1000LL * 1000LL * 1000LL * 1024LL * 1000LL * 1000LL }, | 335 | { "EB", 1000LL * 1000LL * 1000LL * 1024LL * 1000LL * 1000LL }, |
335 | { NULL, 0 } }; | 336 | { NULL, 0 } }; |
336 | 337 | ||
337 | return convert_with_table(fancy_size, table, size); | 338 | return convert_with_table (fancy_size, table, size); |
338 | } | 339 | } |
339 | 340 | ||
340 | 341 | ||
@@ -347,8 +348,8 @@ GNUNET_STRINGS_fancy_size_to_bytes(const char *fancy_size, | |||
347 | * @return #GNUNET_OK on success, #GNUNET_SYSERR on error | 348 | * @return #GNUNET_OK on success, #GNUNET_SYSERR on error |
348 | */ | 349 | */ |
349 | int | 350 | int |
350 | GNUNET_STRINGS_fancy_time_to_relative(const char *fancy_time, | 351 | GNUNET_STRINGS_fancy_time_to_relative (const char *fancy_time, |
351 | struct GNUNET_TIME_Relative *rtime) | 352 | struct GNUNET_TIME_Relative *rtime) |
352 | { | 353 | { |
353 | static const struct ConversionTable table[] = | 354 | static const struct ConversionTable table[] = |
354 | { { "us", 1 }, | 355 | { { "us", 1 }, |
@@ -377,13 +378,13 @@ GNUNET_STRINGS_fancy_time_to_relative(const char *fancy_time, | |||
377 | int ret; | 378 | int ret; |
378 | unsigned long long val; | 379 | unsigned long long val; |
379 | 380 | ||
380 | if (0 == strcasecmp("forever", fancy_time)) | 381 | if (0 == strcasecmp ("forever", fancy_time)) |
381 | { | 382 | { |
382 | *rtime = GNUNET_TIME_UNIT_FOREVER_REL; | 383 | *rtime = GNUNET_TIME_UNIT_FOREVER_REL; |
383 | return GNUNET_OK; | 384 | return GNUNET_OK; |
384 | } | 385 | } |
385 | ret = convert_with_table(fancy_time, table, &val); | 386 | ret = convert_with_table (fancy_time, table, &val); |
386 | rtime->rel_value_us = (uint64_t)val; | 387 | rtime->rel_value_us = (uint64_t) val; |
387 | return ret; | 388 | return ret; |
388 | } | 389 | } |
389 | 390 | ||
@@ -398,33 +399,33 @@ GNUNET_STRINGS_fancy_time_to_relative(const char *fancy_time, | |||
398 | * @return #GNUNET_OK on success, #GNUNET_SYSERR on error | 399 | * @return #GNUNET_OK on success, #GNUNET_SYSERR on error |
399 | */ | 400 | */ |
400 | int | 401 | int |
401 | GNUNET_STRINGS_fancy_time_to_absolute(const char *fancy_time, | 402 | GNUNET_STRINGS_fancy_time_to_absolute (const char *fancy_time, |
402 | struct GNUNET_TIME_Absolute *atime) | 403 | struct GNUNET_TIME_Absolute *atime) |
403 | { | 404 | { |
404 | struct tm tv; | 405 | struct tm tv; |
405 | time_t t; | 406 | time_t t; |
406 | const char *eos; | 407 | const char *eos; |
407 | 408 | ||
408 | if (0 == strcasecmp("end of time", fancy_time)) | 409 | if (0 == strcasecmp ("end of time", fancy_time)) |
409 | { | 410 | { |
410 | *atime = GNUNET_TIME_UNIT_FOREVER_ABS; | 411 | *atime = GNUNET_TIME_UNIT_FOREVER_ABS; |
411 | return GNUNET_OK; | 412 | return GNUNET_OK; |
412 | } | 413 | } |
413 | eos = &fancy_time[strlen(fancy_time)]; | 414 | eos = &fancy_time[strlen (fancy_time)]; |
414 | memset(&tv, 0, sizeof(tv)); | 415 | memset (&tv, 0, sizeof(tv)); |
415 | if ((eos != strptime(fancy_time, "%a %b %d %H:%M:%S %Y", &tv)) && | 416 | if ((eos != strptime (fancy_time, "%a %b %d %H:%M:%S %Y", &tv)) && |
416 | (eos != strptime(fancy_time, "%c", &tv)) && | 417 | (eos != strptime (fancy_time, "%c", &tv)) && |
417 | (eos != strptime(fancy_time, "%Ec", &tv)) && | 418 | (eos != strptime (fancy_time, "%Ec", &tv)) && |
418 | (eos != strptime(fancy_time, "%Y-%m-%d %H:%M:%S", &tv)) && | 419 | (eos != strptime (fancy_time, "%Y-%m-%d %H:%M:%S", &tv)) && |
419 | (eos != strptime(fancy_time, "%Y-%m-%d %H:%M", &tv)) && | 420 | (eos != strptime (fancy_time, "%Y-%m-%d %H:%M", &tv)) && |
420 | (eos != strptime(fancy_time, "%x", &tv)) && | 421 | (eos != strptime (fancy_time, "%x", &tv)) && |
421 | (eos != strptime(fancy_time, "%Ex", &tv)) && | 422 | (eos != strptime (fancy_time, "%Ex", &tv)) && |
422 | (eos != strptime(fancy_time, "%Y-%m-%d", &tv)) && | 423 | (eos != strptime (fancy_time, "%Y-%m-%d", &tv)) && |
423 | (eos != strptime(fancy_time, "%Y-%m", &tv)) && | 424 | (eos != strptime (fancy_time, "%Y-%m", &tv)) && |
424 | (eos != strptime(fancy_time, "%Y", &tv))) | 425 | (eos != strptime (fancy_time, "%Y", &tv))) |
425 | return GNUNET_SYSERR; | 426 | return GNUNET_SYSERR; |
426 | t = mktime(&tv); | 427 | t = mktime (&tv); |
427 | atime->abs_value_us = (uint64_t)((uint64_t)t * 1000LL * 1000LL); | 428 | atime->abs_value_us = (uint64_t) ((uint64_t) t * 1000LL * 1000LL); |
428 | return GNUNET_OK; | 429 | return GNUNET_OK; |
429 | } | 430 | } |
430 | 431 | ||
@@ -443,10 +444,10 @@ GNUNET_STRINGS_fancy_time_to_absolute(const char *fancy_time, | |||
443 | * string is returned. | 444 | * string is returned. |
444 | */ | 445 | */ |
445 | char * | 446 | char * |
446 | GNUNET_STRINGS_conv(const char *input, | 447 | GNUNET_STRINGS_conv (const char *input, |
447 | size_t len, | 448 | size_t len, |
448 | const char *input_charset, | 449 | const char *input_charset, |
449 | const char *output_charset) | 450 | const char *output_charset) |
450 | { | 451 | { |
451 | char *ret; | 452 | char *ret; |
452 | uint8_t *u8_string; | 453 | uint8_t *u8_string; |
@@ -454,51 +455,51 @@ GNUNET_STRINGS_conv(const char *input, | |||
454 | size_t u8_string_length; | 455 | size_t u8_string_length; |
455 | size_t encoded_string_length; | 456 | size_t encoded_string_length; |
456 | 457 | ||
457 | u8_string = u8_conv_from_encoding(input_charset, | 458 | u8_string = u8_conv_from_encoding (input_charset, |
458 | iconveh_error, | 459 | iconveh_error, |
459 | input, | 460 | input, |
460 | len, | 461 | len, |
461 | NULL, | 462 | NULL, |
462 | NULL, | 463 | NULL, |
463 | &u8_string_length); | 464 | &u8_string_length); |
464 | if (NULL == u8_string) | 465 | if (NULL == u8_string) |
465 | { | 466 | { |
466 | LOG_STRERROR(GNUNET_ERROR_TYPE_WARNING, "u8_conv_from_encoding"); | 467 | LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "u8_conv_from_encoding"); |
467 | goto fail; | 468 | goto fail; |
468 | } | 469 | } |
469 | if (0 == strcmp(output_charset, "UTF-8")) | 470 | if (0 == strcmp (output_charset, "UTF-8")) |
470 | { | 471 | { |
471 | ret = GNUNET_malloc(u8_string_length + 1); | 472 | ret = GNUNET_malloc (u8_string_length + 1); |
472 | GNUNET_memcpy(ret, u8_string, u8_string_length); | 473 | GNUNET_memcpy (ret, u8_string, u8_string_length); |
473 | ret[u8_string_length] = '\0'; | 474 | ret[u8_string_length] = '\0'; |
474 | free(u8_string); | 475 | free (u8_string); |
475 | return ret; | 476 | return ret; |
476 | } | 477 | } |
477 | encoded_string = u8_conv_to_encoding(output_charset, | 478 | encoded_string = u8_conv_to_encoding (output_charset, |
478 | iconveh_error, | 479 | iconveh_error, |
479 | u8_string, | 480 | u8_string, |
480 | u8_string_length, | 481 | u8_string_length, |
481 | NULL, | 482 | NULL, |
482 | NULL, | 483 | NULL, |
483 | &encoded_string_length); | 484 | &encoded_string_length); |
484 | free(u8_string); | 485 | free (u8_string); |
485 | if (NULL == encoded_string) | 486 | if (NULL == encoded_string) |
486 | { | 487 | { |
487 | LOG_STRERROR(GNUNET_ERROR_TYPE_WARNING, "u8_conv_to_encoding"); | 488 | LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "u8_conv_to_encoding"); |
488 | goto fail; | 489 | goto fail; |
489 | } | 490 | } |
490 | ret = GNUNET_malloc(encoded_string_length + 1); | 491 | ret = GNUNET_malloc (encoded_string_length + 1); |
491 | GNUNET_memcpy(ret, encoded_string, encoded_string_length); | 492 | GNUNET_memcpy (ret, encoded_string, encoded_string_length); |
492 | ret[encoded_string_length] = '\0'; | 493 | ret[encoded_string_length] = '\0'; |
493 | free(encoded_string); | 494 | free (encoded_string); |
494 | return ret; | 495 | return ret; |
495 | fail: | 496 | fail: |
496 | LOG(GNUNET_ERROR_TYPE_WARNING, | 497 | LOG (GNUNET_ERROR_TYPE_WARNING, |
497 | _("Character sets requested were `%s'->`%s'\n"), | 498 | _ ("Character sets requested were `%s'->`%s'\n"), |
498 | "UTF-8", | 499 | "UTF-8", |
499 | output_charset); | 500 | output_charset); |
500 | ret = GNUNET_malloc(len + 1); | 501 | ret = GNUNET_malloc (len + 1); |
501 | GNUNET_memcpy(ret, input, len); | 502 | GNUNET_memcpy (ret, input, len); |
502 | ret[len] = '\0'; | 503 | ret[len] = '\0'; |
503 | return ret; | 504 | return ret; |
504 | } | 505 | } |
@@ -517,9 +518,9 @@ fail: | |||
517 | * string is returned. | 518 | * string is returned. |
518 | */ | 519 | */ |
519 | char * | 520 | char * |
520 | GNUNET_STRINGS_to_utf8(const char *input, size_t len, const char *charset) | 521 | GNUNET_STRINGS_to_utf8 (const char *input, size_t len, const char *charset) |
521 | { | 522 | { |
522 | return GNUNET_STRINGS_conv(input, len, charset, "UTF-8"); | 523 | return GNUNET_STRINGS_conv (input, len, charset, "UTF-8"); |
523 | } | 524 | } |
524 | 525 | ||
525 | 526 | ||
@@ -535,9 +536,9 @@ GNUNET_STRINGS_to_utf8(const char *input, size_t len, const char *charset) | |||
535 | * string is returned. | 536 | * string is returned. |
536 | */ | 537 | */ |
537 | char * | 538 | char * |
538 | GNUNET_STRINGS_from_utf8(const char *input, size_t len, const char *charset) | 539 | GNUNET_STRINGS_from_utf8 (const char *input, size_t len, const char *charset) |
539 | { | 540 | { |
540 | return GNUNET_STRINGS_conv(input, len, "UTF-8", charset); | 541 | return GNUNET_STRINGS_conv (input, len, "UTF-8", charset); |
541 | } | 542 | } |
542 | 543 | ||
543 | 544 | ||
@@ -549,20 +550,20 @@ GNUNET_STRINGS_from_utf8(const char *input, size_t len, const char *charset) | |||
549 | * @param output output buffer | 550 | * @param output output buffer |
550 | */ | 551 | */ |
551 | void | 552 | void |
552 | GNUNET_STRINGS_utf8_tolower(const char *input, char *output) | 553 | GNUNET_STRINGS_utf8_tolower (const char *input, char *output) |
553 | { | 554 | { |
554 | uint8_t *tmp_in; | 555 | uint8_t *tmp_in; |
555 | size_t len; | 556 | size_t len; |
556 | 557 | ||
557 | tmp_in = u8_tolower((uint8_t *)input, | 558 | tmp_in = u8_tolower ((uint8_t *) input, |
558 | strlen((char *)input), | 559 | strlen ((char *) input), |
559 | NULL, | 560 | NULL, |
560 | UNINORM_NFD, | 561 | UNINORM_NFD, |
561 | NULL, | 562 | NULL, |
562 | &len); | 563 | &len); |
563 | GNUNET_memcpy(output, tmp_in, len); | 564 | GNUNET_memcpy (output, tmp_in, len); |
564 | output[len] = '\0'; | 565 | output[len] = '\0'; |
565 | free(tmp_in); | 566 | free (tmp_in); |
566 | } | 567 | } |
567 | 568 | ||
568 | 569 | ||
@@ -574,20 +575,20 @@ GNUNET_STRINGS_utf8_tolower(const char *input, char *output) | |||
574 | * @param output output buffer | 575 | * @param output output buffer |
575 | */ | 576 | */ |
576 | void | 577 | void |
577 | GNUNET_STRINGS_utf8_toupper(const char *input, char *output) | 578 | GNUNET_STRINGS_utf8_toupper (const char *input, char *output) |
578 | { | 579 | { |
579 | uint8_t *tmp_in; | 580 | uint8_t *tmp_in; |
580 | size_t len; | 581 | size_t len; |
581 | 582 | ||
582 | tmp_in = u8_toupper((uint8_t *)input, | 583 | tmp_in = u8_toupper ((uint8_t *) input, |
583 | strlen((char *)input), | 584 | strlen ((char *) input), |
584 | NULL, | 585 | NULL, |
585 | UNINORM_NFD, | 586 | UNINORM_NFD, |
586 | NULL, | 587 | NULL, |
587 | &len); | 588 | &len); |
588 | GNUNET_memcpy(output, tmp_in, len); | 589 | GNUNET_memcpy (output, tmp_in, len); |
589 | output[len] = '\0'; | 590 | output[len] = '\0'; |
590 | free(tmp_in); | 591 | free (tmp_in); |
591 | } | 592 | } |
592 | 593 | ||
593 | 594 | ||
@@ -599,7 +600,7 @@ GNUNET_STRINGS_utf8_toupper(const char *input, char *output) | |||
599 | * NULL is returned on error | 600 | * NULL is returned on error |
600 | */ | 601 | */ |
601 | char * | 602 | char * |
602 | GNUNET_STRINGS_filename_expand(const char *fil) | 603 | GNUNET_STRINGS_filename_expand (const char *fil) |
603 | { | 604 | { |
604 | char *buffer; | 605 | char *buffer; |
605 | size_t len; | 606 | size_t len; |
@@ -611,64 +612,64 @@ GNUNET_STRINGS_filename_expand(const char *fil) | |||
611 | 612 | ||
612 | if (fil[0] == DIR_SEPARATOR) | 613 | if (fil[0] == DIR_SEPARATOR) |
613 | /* absolute path, just copy */ | 614 | /* absolute path, just copy */ |
614 | return GNUNET_strdup(fil); | 615 | return GNUNET_strdup (fil); |
615 | if (fil[0] == '~') | 616 | if (fil[0] == '~') |
617 | { | ||
618 | fm = getenv ("HOME"); | ||
619 | if (fm == NULL) | ||
616 | { | 620 | { |
617 | fm = getenv("HOME"); | 621 | LOG (GNUNET_ERROR_TYPE_WARNING, |
618 | if (fm == NULL) | 622 | _ ("Failed to expand `$HOME': environment variable `HOME' not set")); |
619 | { | 623 | return NULL; |
620 | LOG(GNUNET_ERROR_TYPE_WARNING, | ||
621 | _("Failed to expand `$HOME': environment variable `HOME' not set")); | ||
622 | return NULL; | ||
623 | } | ||
624 | fm = GNUNET_strdup(fm); | ||
625 | /* do not copy '~' */ | ||
626 | fil_ptr = fil + 1; | ||
627 | |||
628 | /* skip over dir seperator to be consistent */ | ||
629 | if (fil_ptr[0] == DIR_SEPARATOR) | ||
630 | fil_ptr++; | ||
631 | } | 624 | } |
625 | fm = GNUNET_strdup (fm); | ||
626 | /* do not copy '~' */ | ||
627 | fil_ptr = fil + 1; | ||
628 | |||
629 | /* skip over dir seperator to be consistent */ | ||
630 | if (fil_ptr[0] == DIR_SEPARATOR) | ||
631 | fil_ptr++; | ||
632 | } | ||
632 | else | 633 | else |
634 | { | ||
635 | /* relative path */ | ||
636 | fil_ptr = fil; | ||
637 | len = 512; | ||
638 | fm = NULL; | ||
639 | while (1) | ||
633 | { | 640 | { |
634 | /* relative path */ | 641 | buffer = GNUNET_malloc (len); |
635 | fil_ptr = fil; | 642 | if (getcwd (buffer, len) != NULL) |
636 | len = 512; | 643 | { |
637 | fm = NULL; | 644 | fm = buffer; |
638 | while (1) | 645 | break; |
639 | { | 646 | } |
640 | buffer = GNUNET_malloc(len); | 647 | if ((errno == ERANGE) && (len < 1024 * 1024 * 4)) |
641 | if (getcwd(buffer, len) != NULL) | 648 | { |
642 | { | 649 | len *= 2; |
643 | fm = buffer; | 650 | GNUNET_free (buffer); |
644 | break; | 651 | continue; |
645 | } | 652 | } |
646 | if ((errno == ERANGE) && (len < 1024 * 1024 * 4)) | 653 | GNUNET_free (buffer); |
647 | { | 654 | break; |
648 | len *= 2; | 655 | } |
649 | GNUNET_free(buffer); | 656 | if (fm == NULL) |
650 | continue; | 657 | { |
651 | } | 658 | LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "getcwd"); |
652 | GNUNET_free(buffer); | 659 | buffer = getenv ("PWD"); /* alternative */ |
653 | break; | 660 | if (buffer != NULL) |
654 | } | 661 | fm = GNUNET_strdup (buffer); |
655 | if (fm == NULL) | ||
656 | { | ||
657 | LOG_STRERROR(GNUNET_ERROR_TYPE_WARNING, "getcwd"); | ||
658 | buffer = getenv("PWD"); /* alternative */ | ||
659 | if (buffer != NULL) | ||
660 | fm = GNUNET_strdup(buffer); | ||
661 | } | ||
662 | if (fm == NULL) | ||
663 | fm = GNUNET_strdup("./"); /* give up */ | ||
664 | } | 662 | } |
665 | GNUNET_asprintf(&buffer, | 663 | if (fm == NULL) |
666 | "%s%s%s", | 664 | fm = GNUNET_strdup ("./"); /* give up */ |
667 | fm, | 665 | } |
668 | (fm[strlen(fm) - 1] == DIR_SEPARATOR) ? "" | 666 | GNUNET_asprintf (&buffer, |
669 | : DIR_SEPARATOR_STR, | 667 | "%s%s%s", |
670 | fil_ptr); | 668 | fm, |
671 | GNUNET_free(fm); | 669 | (fm[strlen (fm) - 1] == DIR_SEPARATOR) ? "" |
670 | : DIR_SEPARATOR_STR, | ||
671 | fil_ptr); | ||
672 | GNUNET_free (fm); | ||
672 | return buffer; | 673 | return buffer; |
673 | } | 674 | } |
674 | 675 | ||
@@ -683,47 +684,47 @@ GNUNET_STRINGS_filename_expand(const char *fil) | |||
683 | * @return time as human-readable string | 684 | * @return time as human-readable string |
684 | */ | 685 | */ |
685 | const char * | 686 | const char * |
686 | GNUNET_STRINGS_relative_time_to_string(struct GNUNET_TIME_Relative delta, | 687 | GNUNET_STRINGS_relative_time_to_string (struct GNUNET_TIME_Relative delta, |
687 | int do_round) | 688 | int do_round) |
688 | { | 689 | { |
689 | static char buf[128]; | 690 | static char buf[128]; |
690 | const char *unit = _(/* time unit */ "µs"); | 691 | const char *unit = _ (/* time unit */ "µs"); |
691 | uint64_t dval = delta.rel_value_us; | 692 | uint64_t dval = delta.rel_value_us; |
692 | 693 | ||
693 | if (GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us == delta.rel_value_us) | 694 | if (GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us == delta.rel_value_us) |
694 | return _("forever"); | 695 | return _ ("forever"); |
695 | if (0 == delta.rel_value_us) | 696 | if (0 == delta.rel_value_us) |
696 | return _("0 ms"); | 697 | return _ ("0 ms"); |
697 | if (((GNUNET_YES == do_round) && (dval > 5 * 1000)) || (0 == (dval % 1000))) | 698 | if (((GNUNET_YES == do_round) && (dval > 5 * 1000)) || (0 == (dval % 1000))) |
699 | { | ||
700 | dval = dval / 1000; | ||
701 | unit = _ (/* time unit */ "ms"); | ||
702 | if (((GNUNET_YES == do_round) && (dval > 5 * 1000)) || (0 == (dval % 1000))) | ||
698 | { | 703 | { |
699 | dval = dval / 1000; | 704 | dval = dval / 1000; |
700 | unit = _(/* time unit */ "ms"); | 705 | unit = _ (/* time unit */ "s"); |
701 | if (((GNUNET_YES == do_round) && (dval > 5 * 1000)) || (0 == (dval % 1000))) | 706 | if (((GNUNET_YES == do_round) && (dval > 5 * 60)) || (0 == (dval % 60))) |
707 | { | ||
708 | dval = dval / 60; | ||
709 | unit = _ (/* time unit */ "m"); | ||
710 | if (((GNUNET_YES == do_round) && (dval > 5 * 60)) || (0 == (dval % 60))) | ||
702 | { | 711 | { |
703 | dval = dval / 1000; | 712 | dval = dval / 60; |
704 | unit = _(/* time unit */ "s"); | 713 | unit = _ (/* time unit */ "h"); |
705 | if (((GNUNET_YES == do_round) && (dval > 5 * 60)) || (0 == (dval % 60))) | 714 | if (((GNUNET_YES == do_round) && (dval > 5 * 24)) || |
706 | { | 715 | (0 == (dval % 24))) |
707 | dval = dval / 60; | 716 | { |
708 | unit = _(/* time unit */ "m"); | 717 | dval = dval / 24; |
709 | if (((GNUNET_YES == do_round) && (dval > 5 * 60)) || (0 == (dval % 60))) | 718 | if (1 == dval) |
710 | { | 719 | unit = _ (/* time unit */ "day"); |
711 | dval = dval / 60; | 720 | else |
712 | unit = _(/* time unit */ "h"); | 721 | unit = _ (/* time unit */ "days"); |
713 | if (((GNUNET_YES == do_round) && (dval > 5 * 24)) || | 722 | } |
714 | (0 == (dval % 24))) | ||
715 | { | ||
716 | dval = dval / 24; | ||
717 | if (1 == dval) | ||
718 | unit = _(/* time unit */ "day"); | ||
719 | else | ||
720 | unit = _(/* time unit */ "days"); | ||
721 | } | ||
722 | } | ||
723 | } | ||
724 | } | 723 | } |
724 | } | ||
725 | } | 725 | } |
726 | GNUNET_snprintf(buf, sizeof(buf), "%llu %s", dval, unit); | 726 | } |
727 | GNUNET_snprintf (buf, sizeof(buf), "%llu %s", dval, unit); | ||
727 | return buf; | 728 | return buf; |
728 | } | 729 | } |
729 | 730 | ||
@@ -738,16 +739,16 @@ GNUNET_STRINGS_relative_time_to_string(struct GNUNET_TIME_Relative delta, | |||
738 | * @return timestamp in human-readable form in local time | 739 | * @return timestamp in human-readable form in local time |
739 | */ | 740 | */ |
740 | const char * | 741 | const char * |
741 | GNUNET_STRINGS_absolute_time_to_string(struct GNUNET_TIME_Absolute t) | 742 | GNUNET_STRINGS_absolute_time_to_string (struct GNUNET_TIME_Absolute t) |
742 | { | 743 | { |
743 | static char buf[255]; | 744 | static char buf[255]; |
744 | time_t tt; | 745 | time_t tt; |
745 | struct tm *tp; | 746 | struct tm *tp; |
746 | 747 | ||
747 | if (t.abs_value_us == GNUNET_TIME_UNIT_FOREVER_ABS.abs_value_us) | 748 | if (t.abs_value_us == GNUNET_TIME_UNIT_FOREVER_ABS.abs_value_us) |
748 | return _("end of time"); | 749 | return _ ("end of time"); |
749 | tt = t.abs_value_us / 1000LL / 1000LL; | 750 | tt = t.abs_value_us / 1000LL / 1000LL; |
750 | tp = localtime(&tt); | 751 | tp = localtime (&tt); |
751 | /* This is hacky, but i don't know a way to detect libc character encoding. | 752 | /* This is hacky, but i don't know a way to detect libc character encoding. |
752 | * Just expect utf8 from glibc these days. | 753 | * Just expect utf8 from glibc these days. |
753 | * As for msvcrt, use the wide variant, which always returns utf16 | 754 | * As for msvcrt, use the wide variant, which always returns utf16 |
@@ -755,7 +756,7 @@ GNUNET_STRINGS_absolute_time_to_string(struct GNUNET_TIME_Absolute t) | |||
755 | * set conversion routines to convert to UTF8). | 756 | * set conversion routines to convert to UTF8). |
756 | */ | 757 | */ |
757 | 758 | ||
758 | strftime(buf, sizeof(buf), "%a %b %d %H:%M:%S %Y", tp); | 759 | strftime (buf, sizeof(buf), "%a %b %d %H:%M:%S %Y", tp); |
759 | 760 | ||
760 | return buf; | 761 | return buf; |
761 | } | 762 | } |
@@ -773,12 +774,12 @@ GNUNET_STRINGS_absolute_time_to_string(struct GNUNET_TIME_Absolute t) | |||
773 | * itself. | 774 | * itself. |
774 | */ | 775 | */ |
775 | const char * | 776 | const char * |
776 | GNUNET_STRINGS_get_short_name(const char *filename) | 777 | GNUNET_STRINGS_get_short_name (const char *filename) |
777 | { | 778 | { |
778 | const char *short_fn = filename; | 779 | const char *short_fn = filename; |
779 | const char *ss; | 780 | const char *ss; |
780 | 781 | ||
781 | while (NULL != (ss = strstr(short_fn, DIR_SEPARATOR_STR)) && (ss[1] != '\0')) | 782 | while (NULL != (ss = strstr (short_fn, DIR_SEPARATOR_STR)) && (ss[1] != '\0')) |
782 | short_fn = 1 + ss; | 783 | short_fn = 1 + ss; |
783 | return short_fn; | 784 | return short_fn; |
784 | } | 785 | } |
@@ -792,51 +793,51 @@ GNUNET_STRINGS_get_short_name(const char *filename) | |||
792 | * @return corresponding numeric value | 793 | * @return corresponding numeric value |
793 | */ | 794 | */ |
794 | static unsigned int | 795 | static unsigned int |
795 | getValue__(unsigned char a) | 796 | getValue__ (unsigned char a) |
796 | { | 797 | { |
797 | unsigned int dec; | 798 | unsigned int dec; |
798 | 799 | ||
799 | switch (a) | 800 | switch (a) |
800 | { | 801 | { |
801 | case 'O': | 802 | case 'O': |
802 | case 'o': | 803 | case 'o': |
803 | a = '0'; | 804 | a = '0'; |
804 | break; | 805 | break; |
805 | 806 | ||
806 | case 'i': | 807 | case 'i': |
807 | case 'I': | 808 | case 'I': |
808 | case 'l': | 809 | case 'l': |
809 | case 'L': | 810 | case 'L': |
810 | a = '1'; | 811 | a = '1'; |
811 | break; | 812 | break; |
812 | 813 | ||
813 | /* also consider U to be V */ | 814 | /* also consider U to be V */ |
814 | case 'u': | 815 | case 'u': |
815 | case 'U': | 816 | case 'U': |
816 | a = 'V'; | 817 | a = 'V'; |
817 | break; | 818 | break; |
818 | 819 | ||
819 | default: | 820 | default: |
820 | break; | 821 | break; |
821 | } | 822 | } |
822 | if ((a >= '0') && (a <= '9')) | 823 | if ((a >= '0') && (a <= '9')) |
823 | return a - '0'; | 824 | return a - '0'; |
824 | if ((a >= 'a') && (a <= 'z')) | 825 | if ((a >= 'a') && (a <= 'z')) |
825 | a = toupper(a); | 826 | a = toupper (a); |
826 | /* return (a - 'a' + 10); */ | 827 | /* return (a - 'a' + 10); */ |
827 | dec = 0; | 828 | dec = 0; |
828 | if ((a >= 'A') && (a <= 'Z')) | 829 | if ((a >= 'A') && (a <= 'Z')) |
829 | { | 830 | { |
830 | if ('I' < a) | 831 | if ('I' < a) |
831 | dec++; | 832 | dec++; |
832 | if ('L' < a) | 833 | if ('L' < a) |
833 | dec++; | 834 | dec++; |
834 | if ('O' < a) | 835 | if ('O' < a) |
835 | dec++; | 836 | dec++; |
836 | if ('U' < a) | 837 | if ('U' < a) |
837 | dec++; | 838 | dec++; |
838 | return(a - 'A' + 10 - dec); | 839 | return(a - 'A' + 10 - dec); |
839 | } | 840 | } |
840 | return -1; | 841 | return -1; |
841 | } | 842 | } |
842 | 843 | ||
@@ -854,10 +855,10 @@ getValue__(unsigned char a) | |||
854 | * @return pointer to the next byte in @a out or NULL on error. | 855 | * @return pointer to the next byte in @a out or NULL on error. |
855 | */ | 856 | */ |
856 | char * | 857 | char * |
857 | GNUNET_STRINGS_data_to_string(const void *data, | 858 | GNUNET_STRINGS_data_to_string (const void *data, |
858 | size_t size, | 859 | size_t size, |
859 | char *out, | 860 | char *out, |
860 | size_t out_size) | 861 | size_t out_size) |
861 | { | 862 | { |
862 | /** | 863 | /** |
863 | * 32 characters for encoding | 864 | * 32 characters for encoding |
@@ -871,36 +872,36 @@ GNUNET_STRINGS_data_to_string(const void *data, | |||
871 | 872 | ||
872 | udata = data; | 873 | udata = data; |
873 | if (out_size < (size * 8 + 4) / 5) | 874 | if (out_size < (size * 8 + 4) / 5) |
874 | { | 875 | { |
875 | GNUNET_break(0); | 876 | GNUNET_break (0); |
876 | return NULL; | 877 | return NULL; |
877 | } | 878 | } |
878 | vbit = 0; | 879 | vbit = 0; |
879 | wpos = 0; | 880 | wpos = 0; |
880 | rpos = 0; | 881 | rpos = 0; |
881 | bits = 0; | 882 | bits = 0; |
882 | while ((rpos < size) || (vbit > 0)) | 883 | while ((rpos < size) || (vbit > 0)) |
884 | { | ||
885 | if ((rpos < size) && (vbit < 5)) | ||
883 | { | 886 | { |
884 | if ((rpos < size) && (vbit < 5)) | 887 | bits = (bits << 8) | udata[rpos++]; /* eat 8 more bits */ |
885 | { | 888 | vbit += 8; |
886 | bits = (bits << 8) | udata[rpos++]; /* eat 8 more bits */ | ||
887 | vbit += 8; | ||
888 | } | ||
889 | if (vbit < 5) | ||
890 | { | ||
891 | bits <<= (5 - vbit); /* zero-padding */ | ||
892 | GNUNET_assert(vbit == ((size * 8) % 5)); | ||
893 | vbit = 5; | ||
894 | } | ||
895 | if (wpos >= out_size) | ||
896 | { | ||
897 | GNUNET_break(0); | ||
898 | return NULL; | ||
899 | } | ||
900 | out[wpos++] = encTable__[(bits >> (vbit - 5)) & 31]; | ||
901 | vbit -= 5; | ||
902 | } | 889 | } |
903 | GNUNET_assert(0 == vbit); | 890 | if (vbit < 5) |
891 | { | ||
892 | bits <<= (5 - vbit); /* zero-padding */ | ||
893 | GNUNET_assert (vbit == ((size * 8) % 5)); | ||
894 | vbit = 5; | ||
895 | } | ||
896 | if (wpos >= out_size) | ||
897 | { | ||
898 | GNUNET_break (0); | ||
899 | return NULL; | ||
900 | } | ||
901 | out[wpos++] = encTable__[(bits >> (vbit - 5)) & 31]; | ||
902 | vbit -= 5; | ||
903 | } | ||
904 | GNUNET_assert (0 == vbit); | ||
904 | if (wpos < out_size) | 905 | if (wpos < out_size) |
905 | out[wpos] = '\0'; | 906 | out[wpos] = '\0'; |
906 | return &out[wpos]; | 907 | return &out[wpos]; |
@@ -918,7 +919,7 @@ GNUNET_STRINGS_data_to_string(const void *data, | |||
918 | * @return freshly allocated, null-terminated string | 919 | * @return freshly allocated, null-terminated string |
919 | */ | 920 | */ |
920 | char * | 921 | char * |
921 | GNUNET_STRINGS_data_to_string_alloc(const void *buf, size_t size) | 922 | GNUNET_STRINGS_data_to_string_alloc (const void *buf, size_t size) |
922 | { | 923 | { |
923 | char *str_buf; | 924 | char *str_buf; |
924 | size_t len = size * 8; | 925 | size_t len = size * 8; |
@@ -927,13 +928,13 @@ GNUNET_STRINGS_data_to_string_alloc(const void *buf, size_t size) | |||
927 | if (len % 5 > 0) | 928 | if (len % 5 > 0) |
928 | len += 5 - len % 5; | 929 | len += 5 - len % 5; |
929 | len /= 5; | 930 | len /= 5; |
930 | str_buf = GNUNET_malloc(len + 1); | 931 | str_buf = GNUNET_malloc (len + 1); |
931 | end = GNUNET_STRINGS_data_to_string(buf, size, str_buf, len); | 932 | end = GNUNET_STRINGS_data_to_string (buf, size, str_buf, len); |
932 | if (NULL == end) | 933 | if (NULL == end) |
933 | { | 934 | { |
934 | GNUNET_free(str_buf); | 935 | GNUNET_free (str_buf); |
935 | return NULL; | 936 | return NULL; |
936 | } | 937 | } |
937 | *end = '\0'; | 938 | *end = '\0'; |
938 | return str_buf; | 939 | return str_buf; |
939 | } | 940 | } |
@@ -950,10 +951,10 @@ GNUNET_STRINGS_data_to_string_alloc(const void *buf, size_t size) | |||
950 | * @return #GNUNET_OK on success, #GNUNET_SYSERR if result has the wrong encoding | 951 | * @return #GNUNET_OK on success, #GNUNET_SYSERR if result has the wrong encoding |
951 | */ | 952 | */ |
952 | int | 953 | int |
953 | GNUNET_STRINGS_string_to_data(const char *enc, | 954 | GNUNET_STRINGS_string_to_data (const char *enc, |
954 | size_t enclen, | 955 | size_t enclen, |
955 | void *out, | 956 | void *out, |
956 | size_t out_size) | 957 | size_t out_size) |
957 | { | 958 | { |
958 | unsigned int rpos; | 959 | unsigned int rpos; |
959 | unsigned int wpos; | 960 | unsigned int wpos; |
@@ -965,48 +966,48 @@ GNUNET_STRINGS_string_to_data(const char *enc, | |||
965 | unsigned int encoded_len = out_size * 8; | 966 | unsigned int encoded_len = out_size * 8; |
966 | 967 | ||
967 | if (0 == enclen) | 968 | if (0 == enclen) |
968 | { | 969 | { |
969 | if (0 == out_size) | 970 | if (0 == out_size) |
970 | return GNUNET_OK; | 971 | return GNUNET_OK; |
971 | return GNUNET_SYSERR; | 972 | return GNUNET_SYSERR; |
972 | } | 973 | } |
973 | uout = out; | 974 | uout = out; |
974 | wpos = out_size; | 975 | wpos = out_size; |
975 | rpos = enclen; | 976 | rpos = enclen; |
976 | if ((encoded_len % 5) > 0) | 977 | if ((encoded_len % 5) > 0) |
977 | { | 978 | { |
978 | vbit = encoded_len % 5; /* padding! */ | 979 | vbit = encoded_len % 5; /* padding! */ |
979 | shift = 5 - vbit; | 980 | shift = 5 - vbit; |
980 | bits = (ret = getValue__(enc[--rpos])) >> shift; | 981 | bits = (ret = getValue__ (enc[--rpos])) >> shift; |
981 | } | 982 | } |
982 | else | 983 | else |
983 | { | 984 | { |
984 | vbit = 5; | 985 | vbit = 5; |
985 | shift = 0; | 986 | shift = 0; |
986 | bits = (ret = getValue__(enc[--rpos])); | 987 | bits = (ret = getValue__ (enc[--rpos])); |
987 | } | 988 | } |
988 | if ((encoded_len + shift) / 5 != enclen) | 989 | if ((encoded_len + shift) / 5 != enclen) |
989 | return GNUNET_SYSERR; | 990 | return GNUNET_SYSERR; |
990 | if (-1 == ret) | 991 | if (-1 == ret) |
991 | return GNUNET_SYSERR; | 992 | return GNUNET_SYSERR; |
992 | while (wpos > 0) | 993 | while (wpos > 0) |
994 | { | ||
995 | if (0 == rpos) | ||
993 | { | 996 | { |
994 | if (0 == rpos) | 997 | GNUNET_break (0); |
995 | { | 998 | return GNUNET_SYSERR; |
996 | GNUNET_break(0); | 999 | } |
997 | return GNUNET_SYSERR; | 1000 | bits = ((ret = getValue__ (enc[--rpos])) << vbit) | bits; |
998 | } | 1001 | if (-1 == ret) |
999 | bits = ((ret = getValue__(enc[--rpos])) << vbit) | bits; | 1002 | return GNUNET_SYSERR; |
1000 | if (-1 == ret) | 1003 | vbit += 5; |
1001 | return GNUNET_SYSERR; | 1004 | if (vbit >= 8) |
1002 | vbit += 5; | 1005 | { |
1003 | if (vbit >= 8) | 1006 | uout[--wpos] = (unsigned char) bits; |
1004 | { | 1007 | bits >>= 8; |
1005 | uout[--wpos] = (unsigned char)bits; | 1008 | vbit -= 8; |
1006 | bits >>= 8; | ||
1007 | vbit -= 8; | ||
1008 | } | ||
1009 | } | 1009 | } |
1010 | } | ||
1010 | if ((0 != rpos) || (0 != vbit)) | 1011 | if ((0 != rpos) || (0 != vbit)) |
1011 | return GNUNET_SYSERR; | 1012 | return GNUNET_SYSERR; |
1012 | return GNUNET_OK; | 1013 | return GNUNET_OK; |
@@ -1030,9 +1031,9 @@ GNUNET_STRINGS_string_to_data(const char *enc, | |||
1030 | * (if they weren't NULL). | 1031 | * (if they weren't NULL). |
1031 | */ | 1032 | */ |
1032 | int | 1033 | int |
1033 | GNUNET_STRINGS_parse_uri(const char *path, | 1034 | GNUNET_STRINGS_parse_uri (const char *path, |
1034 | char **scheme_part, | 1035 | char **scheme_part, |
1035 | const char **path_part) | 1036 | const char **path_part) |
1036 | { | 1037 | { |
1037 | size_t len; | 1038 | size_t len; |
1038 | size_t i; | 1039 | size_t i; |
@@ -1040,51 +1041,51 @@ GNUNET_STRINGS_parse_uri(const char *path, | |||
1040 | int pp_state = 0; | 1041 | int pp_state = 0; |
1041 | const char *post_scheme_part = NULL; | 1042 | const char *post_scheme_part = NULL; |
1042 | 1043 | ||
1043 | len = strlen(path); | 1044 | len = strlen (path); |
1044 | for (end = 0, i = 0; !end && i < len; i++) | 1045 | for (end = 0, i = 0; ! end && i < len; i++) |
1046 | { | ||
1047 | switch (pp_state) | ||
1045 | { | 1048 | { |
1046 | switch (pp_state) | 1049 | case 0: |
1047 | { | 1050 | if ((path[i] == ':') && (i > 0)) |
1048 | case 0: | 1051 | { |
1049 | if ((path[i] == ':') && (i > 0)) | 1052 | pp_state += 1; |
1050 | { | 1053 | continue; |
1051 | pp_state += 1; | 1054 | } |
1052 | continue; | 1055 | if (! (((path[i] >= 'A') &&(path[i] <= 'Z') ) || |
1053 | } | 1056 | ((path[i] >= 'a') &&(path[i] <= 'z') ) || |
1054 | if (!((path[i] >= 'A' && path[i] <= 'Z') || | 1057 | ((path[i] >= '0') &&(path[i] <= '9') ) ||(path[i] == '+') || |
1055 | (path[i] >= 'a' && path[i] <= 'z') || | 1058 | (path[i] == '-') || (path[i] == '.'))) |
1056 | (path[i] >= '0' && path[i] <= '9') || path[i] == '+' || | 1059 | end = 1; |
1057 | path[i] == '-' || (path[i] == '.'))) | 1060 | break; |
1058 | end = 1; | ||
1059 | break; | ||
1060 | 1061 | ||
1061 | case 1: | 1062 | case 1: |
1062 | case 2: | 1063 | case 2: |
1063 | if (path[i] == '/') | 1064 | if (path[i] == '/') |
1064 | { | 1065 | { |
1065 | pp_state += 1; | 1066 | pp_state += 1; |
1066 | continue; | 1067 | continue; |
1067 | } | 1068 | } |
1068 | end = 1; | 1069 | end = 1; |
1069 | break; | 1070 | break; |
1070 | 1071 | ||
1071 | case 3: | 1072 | case 3: |
1072 | post_scheme_part = &path[i]; | 1073 | post_scheme_part = &path[i]; |
1073 | end = 1; | 1074 | end = 1; |
1074 | break; | 1075 | break; |
1075 | 1076 | ||
1076 | default: | 1077 | default: |
1077 | end = 1; | 1078 | end = 1; |
1078 | } | ||
1079 | } | 1079 | } |
1080 | } | ||
1080 | if (post_scheme_part == NULL) | 1081 | if (post_scheme_part == NULL) |
1081 | return GNUNET_NO; | 1082 | return GNUNET_NO; |
1082 | if (scheme_part) | 1083 | if (scheme_part) |
1083 | { | 1084 | { |
1084 | *scheme_part = GNUNET_malloc(post_scheme_part - path + 1); | 1085 | *scheme_part = GNUNET_malloc (post_scheme_part - path + 1); |
1085 | GNUNET_memcpy(*scheme_part, path, post_scheme_part - path); | 1086 | GNUNET_memcpy (*scheme_part, path, post_scheme_part - path); |
1086 | (*scheme_part)[post_scheme_part - path] = '\0'; | 1087 | (*scheme_part)[post_scheme_part - path] = '\0'; |
1087 | } | 1088 | } |
1088 | if (path_part) | 1089 | if (path_part) |
1089 | *path_part = post_scheme_part; | 1090 | *path_part = post_scheme_part; |
1090 | return GNUNET_YES; | 1091 | return GNUNET_YES; |
@@ -1106,10 +1107,10 @@ GNUNET_STRINGS_parse_uri(const char *path, | |||
1106 | * @return #GNUNET_YES if @a filename is absolute, #GNUNET_NO otherwise. | 1107 | * @return #GNUNET_YES if @a filename is absolute, #GNUNET_NO otherwise. |
1107 | */ | 1108 | */ |
1108 | int | 1109 | int |
1109 | GNUNET_STRINGS_path_is_absolute(const char *filename, | 1110 | GNUNET_STRINGS_path_is_absolute (const char *filename, |
1110 | int can_be_uri, | 1111 | int can_be_uri, |
1111 | int *r_is_uri, | 1112 | int *r_is_uri, |
1112 | char **r_uri_scheme) | 1113 | char **r_uri_scheme) |
1113 | { | 1114 | { |
1114 | const char *post_scheme_path; | 1115 | const char *post_scheme_path; |
1115 | int is_uri; | 1116 | int is_uri; |
@@ -1120,28 +1121,28 @@ GNUNET_STRINGS_path_is_absolute(const char *filename, | |||
1120 | if (filename[0] == '/') | 1121 | if (filename[0] == '/') |
1121 | return GNUNET_YES; | 1122 | return GNUNET_YES; |
1122 | if (can_be_uri) | 1123 | if (can_be_uri) |
1124 | { | ||
1125 | is_uri = GNUNET_STRINGS_parse_uri (filename, &uri, &post_scheme_path); | ||
1126 | if (r_is_uri) | ||
1127 | *r_is_uri = is_uri; | ||
1128 | if (is_uri) | ||
1123 | { | 1129 | { |
1124 | is_uri = GNUNET_STRINGS_parse_uri(filename, &uri, &post_scheme_path); | 1130 | if (r_uri_scheme) |
1125 | if (r_is_uri) | 1131 | *r_uri_scheme = uri; |
1126 | *r_is_uri = is_uri; | 1132 | else |
1127 | if (is_uri) | 1133 | GNUNET_free_non_null (uri); |
1128 | { | ||
1129 | if (r_uri_scheme) | ||
1130 | *r_uri_scheme = uri; | ||
1131 | else | ||
1132 | GNUNET_free_non_null(uri); | ||
1133 | 1134 | ||
1134 | return GNUNET_STRINGS_path_is_absolute(post_scheme_path, | 1135 | return GNUNET_STRINGS_path_is_absolute (post_scheme_path, |
1135 | GNUNET_NO, | 1136 | GNUNET_NO, |
1136 | NULL, | 1137 | NULL, |
1137 | NULL); | 1138 | NULL); |
1138 | } | ||
1139 | } | 1139 | } |
1140 | } | ||
1140 | else | 1141 | else |
1141 | { | 1142 | { |
1142 | if (r_is_uri) | 1143 | if (r_is_uri) |
1143 | *r_is_uri = GNUNET_NO; | 1144 | *r_is_uri = GNUNET_NO; |
1144 | } | 1145 | } |
1145 | 1146 | ||
1146 | return GNUNET_NO; | 1147 | return GNUNET_NO; |
1147 | } | 1148 | } |
@@ -1156,33 +1157,33 @@ GNUNET_STRINGS_path_is_absolute(const char *filename, | |||
1156 | * fails, #GNUNET_SYSERR when a check can't be performed | 1157 | * fails, #GNUNET_SYSERR when a check can't be performed |
1157 | */ | 1158 | */ |
1158 | int | 1159 | int |
1159 | GNUNET_STRINGS_check_filename(const char *filename, | 1160 | GNUNET_STRINGS_check_filename (const char *filename, |
1160 | enum GNUNET_STRINGS_FilenameCheck checks) | 1161 | enum GNUNET_STRINGS_FilenameCheck checks) |
1161 | { | 1162 | { |
1162 | struct stat st; | 1163 | struct stat st; |
1163 | 1164 | ||
1164 | if ((NULL == filename) || (filename[0] == '\0')) | 1165 | if ((NULL == filename) || (filename[0] == '\0')) |
1165 | return GNUNET_SYSERR; | 1166 | return GNUNET_SYSERR; |
1166 | if (0 != (checks & GNUNET_STRINGS_CHECK_IS_ABSOLUTE)) | 1167 | if (0 != (checks & GNUNET_STRINGS_CHECK_IS_ABSOLUTE)) |
1167 | if (!GNUNET_STRINGS_path_is_absolute(filename, GNUNET_NO, NULL, NULL)) | 1168 | if (! GNUNET_STRINGS_path_is_absolute (filename, GNUNET_NO, NULL, NULL)) |
1168 | return GNUNET_NO; | 1169 | return GNUNET_NO; |
1169 | if (0 != (checks & | 1170 | if (0 != (checks |
1170 | (GNUNET_STRINGS_CHECK_EXISTS | GNUNET_STRINGS_CHECK_IS_DIRECTORY | | 1171 | & (GNUNET_STRINGS_CHECK_EXISTS | GNUNET_STRINGS_CHECK_IS_DIRECTORY |
1171 | GNUNET_STRINGS_CHECK_IS_LINK))) | 1172 | | GNUNET_STRINGS_CHECK_IS_LINK))) |
1173 | { | ||
1174 | if (0 != stat (filename, &st)) | ||
1172 | { | 1175 | { |
1173 | if (0 != stat(filename, &st)) | 1176 | if (0 != (checks & GNUNET_STRINGS_CHECK_EXISTS)) |
1174 | { | 1177 | return GNUNET_NO; |
1175 | if (0 != (checks & GNUNET_STRINGS_CHECK_EXISTS)) | 1178 | else |
1176 | return GNUNET_NO; | 1179 | return GNUNET_SYSERR; |
1177 | else | ||
1178 | return GNUNET_SYSERR; | ||
1179 | } | ||
1180 | } | 1180 | } |
1181 | } | ||
1181 | if (0 != (checks & GNUNET_STRINGS_CHECK_IS_DIRECTORY)) | 1182 | if (0 != (checks & GNUNET_STRINGS_CHECK_IS_DIRECTORY)) |
1182 | if (!S_ISDIR(st.st_mode)) | 1183 | if (! S_ISDIR (st.st_mode)) |
1183 | return GNUNET_NO; | 1184 | return GNUNET_NO; |
1184 | if (0 != (checks & GNUNET_STRINGS_CHECK_IS_LINK)) | 1185 | if (0 != (checks & GNUNET_STRINGS_CHECK_IS_LINK)) |
1185 | if (!S_ISLNK(st.st_mode)) | 1186 | if (! S_ISLNK (st.st_mode)) |
1186 | return GNUNET_NO; | 1187 | return GNUNET_NO; |
1187 | return GNUNET_YES; | 1188 | return GNUNET_YES; |
1188 | } | 1189 | } |
@@ -1201,9 +1202,9 @@ GNUNET_STRINGS_check_filename(const char *filename, | |||
1201 | * case the contents of @a r_buf are undefined. | 1202 | * case the contents of @a r_buf are undefined. |
1202 | */ | 1203 | */ |
1203 | int | 1204 | int |
1204 | GNUNET_STRINGS_to_address_ipv6(const char *zt_addr, | 1205 | GNUNET_STRINGS_to_address_ipv6 (const char *zt_addr, |
1205 | uint16_t addrlen, | 1206 | uint16_t addrlen, |
1206 | struct sockaddr_in6 *r_buf) | 1207 | struct sockaddr_in6 *r_buf) |
1207 | { | 1208 | { |
1208 | char zbuf[addrlen + 1]; | 1209 | char zbuf[addrlen + 1]; |
1209 | int ret; | 1210 | int ret; |
@@ -1213,51 +1214,51 @@ GNUNET_STRINGS_to_address_ipv6(const char *zt_addr, | |||
1213 | 1214 | ||
1214 | if (addrlen < 6) | 1215 | if (addrlen < 6) |
1215 | return GNUNET_SYSERR; | 1216 | return GNUNET_SYSERR; |
1216 | GNUNET_memcpy(zbuf, zt_addr, addrlen); | 1217 | GNUNET_memcpy (zbuf, zt_addr, addrlen); |
1217 | if ('[' != zbuf[0]) | 1218 | if ('[' != zbuf[0]) |
1218 | { | 1219 | { |
1219 | GNUNET_log(GNUNET_ERROR_TYPE_WARNING, | 1220 | GNUNET_log (GNUNET_ERROR_TYPE_WARNING, |
1220 | _("IPv6 address did not start with `['\n")); | 1221 | _ ("IPv6 address did not start with `['\n")); |
1221 | return GNUNET_SYSERR; | 1222 | return GNUNET_SYSERR; |
1222 | } | 1223 | } |
1223 | zbuf[addrlen] = '\0'; | 1224 | zbuf[addrlen] = '\0'; |
1224 | port_colon = strrchr(zbuf, ':'); | 1225 | port_colon = strrchr (zbuf, ':'); |
1225 | if (NULL == port_colon) | 1226 | if (NULL == port_colon) |
1226 | { | 1227 | { |
1227 | GNUNET_log(GNUNET_ERROR_TYPE_WARNING, | 1228 | GNUNET_log (GNUNET_ERROR_TYPE_WARNING, |
1228 | _("IPv6 address did contain ':' to separate port number\n")); | 1229 | _ ("IPv6 address did contain ':' to separate port number\n")); |
1229 | return GNUNET_SYSERR; | 1230 | return GNUNET_SYSERR; |
1230 | } | 1231 | } |
1231 | if (']' != *(port_colon - 1)) | 1232 | if (']' != *(port_colon - 1)) |
1232 | { | 1233 | { |
1233 | GNUNET_log( | 1234 | GNUNET_log ( |
1234 | GNUNET_ERROR_TYPE_WARNING, | 1235 | GNUNET_ERROR_TYPE_WARNING, |
1235 | _("IPv6 address did contain ']' before ':' to separate port number\n")); | 1236 | _ ("IPv6 address did contain ']' before ':' to separate port number\n")); |
1236 | return GNUNET_SYSERR; | 1237 | return GNUNET_SYSERR; |
1237 | } | 1238 | } |
1238 | ret = sscanf(port_colon, ":%u%1s", &port, dummy); | 1239 | ret = sscanf (port_colon, ":%u%1s", &port, dummy); |
1239 | if ((1 != ret) || (port > 65535)) | 1240 | if ((1 != ret) || (port > 65535)) |
1240 | { | 1241 | { |
1241 | GNUNET_log( | 1242 | GNUNET_log ( |
1242 | GNUNET_ERROR_TYPE_WARNING, | 1243 | GNUNET_ERROR_TYPE_WARNING, |
1243 | _("IPv6 address did contain a valid port number after the last ':'\n")); | 1244 | _ ("IPv6 address did contain a valid port number after the last ':'\n")); |
1244 | return GNUNET_SYSERR; | 1245 | return GNUNET_SYSERR; |
1245 | } | 1246 | } |
1246 | *(port_colon - 1) = '\0'; | 1247 | *(port_colon - 1) = '\0'; |
1247 | memset(r_buf, 0, sizeof(struct sockaddr_in6)); | 1248 | memset (r_buf, 0, sizeof(struct sockaddr_in6)); |
1248 | ret = inet_pton(AF_INET6, &zbuf[1], &r_buf->sin6_addr); | 1249 | ret = inet_pton (AF_INET6, &zbuf[1], &r_buf->sin6_addr); |
1249 | if (ret <= 0) | 1250 | if (ret <= 0) |
1250 | { | 1251 | { |
1251 | GNUNET_log(GNUNET_ERROR_TYPE_WARNING, | 1252 | GNUNET_log (GNUNET_ERROR_TYPE_WARNING, |
1252 | _("Invalid IPv6 address `%s': %s\n"), | 1253 | _ ("Invalid IPv6 address `%s': %s\n"), |
1253 | &zbuf[1], | 1254 | &zbuf[1], |
1254 | strerror(errno)); | 1255 | strerror (errno)); |
1255 | return GNUNET_SYSERR; | 1256 | return GNUNET_SYSERR; |
1256 | } | 1257 | } |
1257 | r_buf->sin6_port = htons(port); | 1258 | r_buf->sin6_port = htons (port); |
1258 | r_buf->sin6_family = AF_INET6; | 1259 | r_buf->sin6_family = AF_INET6; |
1259 | #if HAVE_SOCKADDR_IN_SIN_LEN | 1260 | #if HAVE_SOCKADDR_IN_SIN_LEN |
1260 | r_buf->sin6_len = (u_char)sizeof(struct sockaddr_in6); | 1261 | r_buf->sin6_len = (u_char) sizeof(struct sockaddr_in6); |
1261 | #endif | 1262 | #endif |
1262 | return GNUNET_OK; | 1263 | return GNUNET_OK; |
1263 | } | 1264 | } |
@@ -1275,9 +1276,9 @@ GNUNET_STRINGS_to_address_ipv6(const char *zt_addr, | |||
1275 | * the contents of @a r_buf are undefined. | 1276 | * the contents of @a r_buf are undefined. |
1276 | */ | 1277 | */ |
1277 | int | 1278 | int |
1278 | GNUNET_STRINGS_to_address_ipv4(const char *zt_addr, | 1279 | GNUNET_STRINGS_to_address_ipv4 (const char *zt_addr, |
1279 | uint16_t addrlen, | 1280 | uint16_t addrlen, |
1280 | struct sockaddr_in *r_buf) | 1281 | struct sockaddr_in *r_buf) |
1281 | { | 1282 | { |
1282 | unsigned int temps[4]; | 1283 | unsigned int temps[4]; |
1283 | unsigned int port; | 1284 | unsigned int port; |
@@ -1286,14 +1287,14 @@ GNUNET_STRINGS_to_address_ipv4(const char *zt_addr, | |||
1286 | 1287 | ||
1287 | if (addrlen < 9) | 1288 | if (addrlen < 9) |
1288 | return GNUNET_SYSERR; | 1289 | return GNUNET_SYSERR; |
1289 | cnt = sscanf(zt_addr, | 1290 | cnt = sscanf (zt_addr, |
1290 | "%u.%u.%u.%u:%u%1s", | 1291 | "%u.%u.%u.%u:%u%1s", |
1291 | &temps[0], | 1292 | &temps[0], |
1292 | &temps[1], | 1293 | &temps[1], |
1293 | &temps[2], | 1294 | &temps[2], |
1294 | &temps[3], | 1295 | &temps[3], |
1295 | &port, | 1296 | &port, |
1296 | dummy); | 1297 | dummy); |
1297 | if (5 != cnt) | 1298 | if (5 != cnt) |
1298 | return GNUNET_SYSERR; | 1299 | return GNUNET_SYSERR; |
1299 | for (cnt = 0; cnt < 4; cnt++) | 1300 | for (cnt = 0; cnt < 4; cnt++) |
@@ -1302,11 +1303,11 @@ GNUNET_STRINGS_to_address_ipv4(const char *zt_addr, | |||
1302 | if (port > 65535) | 1303 | if (port > 65535) |
1303 | return GNUNET_SYSERR; | 1304 | return GNUNET_SYSERR; |
1304 | r_buf->sin_family = AF_INET; | 1305 | r_buf->sin_family = AF_INET; |
1305 | r_buf->sin_port = htons(port); | 1306 | r_buf->sin_port = htons (port); |
1306 | r_buf->sin_addr.s_addr = | 1307 | r_buf->sin_addr.s_addr = |
1307 | htonl((temps[0] << 24) + (temps[1] << 16) + (temps[2] << 8) + temps[3]); | 1308 | htonl ((temps[0] << 24) + (temps[1] << 16) + (temps[2] << 8) + temps[3]); |
1308 | #if HAVE_SOCKADDR_IN_SIN_LEN | 1309 | #if HAVE_SOCKADDR_IN_SIN_LEN |
1309 | r_buf->sin_len = (u_char)sizeof(struct sockaddr_in); | 1310 | r_buf->sin_len = (u_char) sizeof(struct sockaddr_in); |
1310 | #endif | 1311 | #endif |
1311 | return GNUNET_OK; | 1312 | return GNUNET_OK; |
1312 | } | 1313 | } |
@@ -1324,17 +1325,17 @@ GNUNET_STRINGS_to_address_ipv4(const char *zt_addr, | |||
1324 | * case the contents of @a r_buf are undefined. | 1325 | * case the contents of @a r_buf are undefined. |
1325 | */ | 1326 | */ |
1326 | int | 1327 | int |
1327 | GNUNET_STRINGS_to_address_ip(const char *addr, | 1328 | GNUNET_STRINGS_to_address_ip (const char *addr, |
1328 | uint16_t addrlen, | 1329 | uint16_t addrlen, |
1329 | struct sockaddr_storage *r_buf) | 1330 | struct sockaddr_storage *r_buf) |
1330 | { | 1331 | { |
1331 | if (addr[0] == '[') | 1332 | if (addr[0] == '[') |
1332 | return GNUNET_STRINGS_to_address_ipv6(addr, | 1333 | return GNUNET_STRINGS_to_address_ipv6 (addr, |
1333 | addrlen, | 1334 | addrlen, |
1334 | (struct sockaddr_in6 *)r_buf); | 1335 | (struct sockaddr_in6 *) r_buf); |
1335 | return GNUNET_STRINGS_to_address_ipv4(addr, | 1336 | return GNUNET_STRINGS_to_address_ipv4 (addr, |
1336 | addrlen, | 1337 | addrlen, |
1337 | (struct sockaddr_in *)r_buf); | 1338 | (struct sockaddr_in *) r_buf); |
1338 | } | 1339 | } |
1339 | 1340 | ||
1340 | 1341 | ||
@@ -1348,49 +1349,49 @@ GNUNET_STRINGS_to_address_ip(const char *addr, | |||
1348 | * @return 0 on error, otherwise number of bytes in @a sa | 1349 | * @return 0 on error, otherwise number of bytes in @a sa |
1349 | */ | 1350 | */ |
1350 | size_t | 1351 | size_t |
1351 | GNUNET_STRINGS_parse_socket_addr(const char *addr, | 1352 | GNUNET_STRINGS_parse_socket_addr (const char *addr, |
1352 | uint8_t *af, | 1353 | uint8_t *af, |
1353 | struct sockaddr **sa) | 1354 | struct sockaddr **sa) |
1354 | { | 1355 | { |
1355 | char *cp = GNUNET_strdup(addr); | 1356 | char *cp = GNUNET_strdup (addr); |
1356 | 1357 | ||
1357 | *af = AF_UNSPEC; | 1358 | *af = AF_UNSPEC; |
1358 | if ('[' == *addr) | 1359 | if ('[' == *addr) |
1360 | { | ||
1361 | /* IPv6 */ | ||
1362 | *sa = GNUNET_malloc (sizeof(struct sockaddr_in6)); | ||
1363 | if (GNUNET_OK != | ||
1364 | GNUNET_STRINGS_to_address_ipv6 (cp, | ||
1365 | strlen (cp), | ||
1366 | (struct sockaddr_in6 *) *sa)) | ||
1359 | { | 1367 | { |
1360 | /* IPv6 */ | 1368 | GNUNET_free (*sa); |
1361 | *sa = GNUNET_malloc(sizeof(struct sockaddr_in6)); | 1369 | *sa = NULL; |
1362 | if (GNUNET_OK != | 1370 | GNUNET_free (cp); |
1363 | GNUNET_STRINGS_to_address_ipv6(cp, | 1371 | return 0; |
1364 | strlen(cp), | ||
1365 | (struct sockaddr_in6 *)*sa)) | ||
1366 | { | ||
1367 | GNUNET_free(*sa); | ||
1368 | *sa = NULL; | ||
1369 | GNUNET_free(cp); | ||
1370 | return 0; | ||
1371 | } | ||
1372 | *af = AF_INET6; | ||
1373 | GNUNET_free(cp); | ||
1374 | return sizeof(struct sockaddr_in6); | ||
1375 | } | 1372 | } |
1373 | *af = AF_INET6; | ||
1374 | GNUNET_free (cp); | ||
1375 | return sizeof(struct sockaddr_in6); | ||
1376 | } | ||
1376 | else | 1377 | else |
1378 | { | ||
1379 | /* IPv4 */ | ||
1380 | *sa = GNUNET_malloc (sizeof(struct sockaddr_in)); | ||
1381 | if (GNUNET_OK != | ||
1382 | GNUNET_STRINGS_to_address_ipv4 (cp, | ||
1383 | strlen (cp), | ||
1384 | (struct sockaddr_in *) *sa)) | ||
1377 | { | 1385 | { |
1378 | /* IPv4 */ | 1386 | GNUNET_free (*sa); |
1379 | *sa = GNUNET_malloc(sizeof(struct sockaddr_in)); | 1387 | *sa = NULL; |
1380 | if (GNUNET_OK != | 1388 | GNUNET_free (cp); |
1381 | GNUNET_STRINGS_to_address_ipv4(cp, | 1389 | return 0; |
1382 | strlen(cp), | ||
1383 | (struct sockaddr_in *)*sa)) | ||
1384 | { | ||
1385 | GNUNET_free(*sa); | ||
1386 | *sa = NULL; | ||
1387 | GNUNET_free(cp); | ||
1388 | return 0; | ||
1389 | } | ||
1390 | *af = AF_INET; | ||
1391 | GNUNET_free(cp); | ||
1392 | return sizeof(struct sockaddr_in); | ||
1393 | } | 1390 | } |
1391 | *af = AF_INET; | ||
1392 | GNUNET_free (cp); | ||
1393 | return sizeof(struct sockaddr_in); | ||
1394 | } | ||
1394 | } | 1395 | } |
1395 | 1396 | ||
1396 | 1397 | ||
@@ -1399,7 +1400,7 @@ GNUNET_STRINGS_parse_socket_addr(const char *addr, | |||
1399 | * freed with a single call to GNUNET_free(); | 1400 | * freed with a single call to GNUNET_free(); |
1400 | */ | 1401 | */ |
1401 | static char *const * | 1402 | static char *const * |
1402 | _make_continuous_arg_copy(int argc, char *const *argv) | 1403 | _make_continuous_arg_copy (int argc, char *const *argv) |
1403 | { | 1404 | { |
1404 | size_t argvsize = 0; | 1405 | size_t argvsize = 0; |
1405 | int i; | 1406 | int i; |
@@ -1407,17 +1408,17 @@ _make_continuous_arg_copy(int argc, char *const *argv) | |||
1407 | char *p; | 1408 | char *p; |
1408 | 1409 | ||
1409 | for (i = 0; i < argc; i++) | 1410 | for (i = 0; i < argc; i++) |
1410 | argvsize += strlen(argv[i]) + 1 + sizeof(char *); | 1411 | argvsize += strlen (argv[i]) + 1 + sizeof(char *); |
1411 | new_argv = GNUNET_malloc(argvsize + sizeof(char *)); | 1412 | new_argv = GNUNET_malloc (argvsize + sizeof(char *)); |
1412 | p = (char *)&new_argv[argc + 1]; | 1413 | p = (char *) &new_argv[argc + 1]; |
1413 | for (i = 0; i < argc; i++) | 1414 | for (i = 0; i < argc; i++) |
1414 | { | 1415 | { |
1415 | new_argv[i] = p; | 1416 | new_argv[i] = p; |
1416 | strcpy(p, argv[i]); | 1417 | strcpy (p, argv[i]); |
1417 | p += strlen(argv[i]) + 1; | 1418 | p += strlen (argv[i]) + 1; |
1418 | } | 1419 | } |
1419 | new_argv[argc] = NULL; | 1420 | new_argv[argc] = NULL; |
1420 | return (char *const *)new_argv; | 1421 | return (char *const *) new_argv; |
1421 | } | 1422 | } |
1422 | 1423 | ||
1423 | 1424 | ||
@@ -1436,13 +1437,13 @@ _make_continuous_arg_copy(int argc, char *const *argv) | |||
1436 | * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure | 1437 | * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure |
1437 | */ | 1438 | */ |
1438 | int | 1439 | int |
1439 | GNUNET_STRINGS_get_utf8_args(int argc, | 1440 | GNUNET_STRINGS_get_utf8_args (int argc, |
1440 | char *const *argv, | 1441 | char *const *argv, |
1441 | int *u8argc, | 1442 | int *u8argc, |
1442 | char *const **u8argv) | 1443 | char *const **u8argv) |
1443 | { | 1444 | { |
1444 | char *const *new_argv = | 1445 | char *const *new_argv = |
1445 | (char *const *)_make_continuous_arg_copy(argc, argv); | 1446 | (char *const *) _make_continuous_arg_copy (argc, argv); |
1446 | *u8argv = new_argv; | 1447 | *u8argv = new_argv; |
1447 | *u8argc = argc; | 1448 | *u8argc = argc; |
1448 | return GNUNET_OK; | 1449 | return GNUNET_OK; |
@@ -1459,8 +1460,8 @@ GNUNET_STRINGS_get_utf8_args(int argc, | |||
1459 | * @a port_policy is malformed | 1460 | * @a port_policy is malformed |
1460 | */ | 1461 | */ |
1461 | static int | 1462 | static int |
1462 | parse_port_policy(const char *port_policy, | 1463 | parse_port_policy (const char *port_policy, |
1463 | struct GNUNET_STRINGS_PortPolicy *pp) | 1464 | struct GNUNET_STRINGS_PortPolicy *pp) |
1464 | { | 1465 | { |
1465 | const char *pos; | 1466 | const char *pos; |
1466 | int s; | 1467 | int s; |
@@ -1469,36 +1470,36 @@ parse_port_policy(const char *port_policy, | |||
1469 | 1470 | ||
1470 | pos = port_policy; | 1471 | pos = port_policy; |
1471 | if ('!' == *pos) | 1472 | if ('!' == *pos) |
1473 | { | ||
1474 | pp->negate_portrange = GNUNET_YES; | ||
1475 | pos++; | ||
1476 | } | ||
1477 | if (2 == sscanf (pos, "%u-%u%1s", &s, &e, eol)) | ||
1478 | { | ||
1479 | if ((0 == s) || (s > 0xFFFF) || (e < s) || (e > 0xFFFF)) | ||
1472 | { | 1480 | { |
1473 | pp->negate_portrange = GNUNET_YES; | 1481 | GNUNET_log (GNUNET_ERROR_TYPE_WARNING, _ ("Port not in range\n")); |
1474 | pos++; | 1482 | return GNUNET_SYSERR; |
1475 | } | 1483 | } |
1476 | if (2 == sscanf(pos, "%u-%u%1s", &s, &e, eol)) | 1484 | pp->start_port = (uint16_t) s; |
1485 | pp->end_port = (uint16_t) e; | ||
1486 | return GNUNET_OK; | ||
1487 | } | ||
1488 | if (1 == sscanf (pos, "%u%1s", &s, eol)) | ||
1489 | { | ||
1490 | if ((0 == s) || (s > 0xFFFF)) | ||
1477 | { | 1491 | { |
1478 | if ((0 == s) || (s > 0xFFFF) || (e < s) || (e > 0xFFFF)) | 1492 | GNUNET_log (GNUNET_ERROR_TYPE_WARNING, _ ("Port not in range\n")); |
1479 | { | 1493 | return GNUNET_SYSERR; |
1480 | GNUNET_log(GNUNET_ERROR_TYPE_WARNING, _("Port not in range\n")); | ||
1481 | return GNUNET_SYSERR; | ||
1482 | } | ||
1483 | pp->start_port = (uint16_t)s; | ||
1484 | pp->end_port = (uint16_t)e; | ||
1485 | return GNUNET_OK; | ||
1486 | } | 1494 | } |
1487 | if (1 == sscanf(pos, "%u%1s", &s, eol)) | ||
1488 | { | ||
1489 | if ((0 == s) || (s > 0xFFFF)) | ||
1490 | { | ||
1491 | GNUNET_log(GNUNET_ERROR_TYPE_WARNING, _("Port not in range\n")); | ||
1492 | return GNUNET_SYSERR; | ||
1493 | } | ||
1494 | 1495 | ||
1495 | pp->start_port = (uint16_t)s; | 1496 | pp->start_port = (uint16_t) s; |
1496 | pp->end_port = (uint16_t)s; | 1497 | pp->end_port = (uint16_t) s; |
1497 | return GNUNET_OK; | 1498 | return GNUNET_OK; |
1498 | } | 1499 | } |
1499 | GNUNET_log(GNUNET_ERROR_TYPE_WARNING, | 1500 | GNUNET_log (GNUNET_ERROR_TYPE_WARNING, |
1500 | _("Malformed port policy `%s'\n"), | 1501 | _ ("Malformed port policy `%s'\n"), |
1501 | port_policy); | 1502 | port_policy); |
1502 | return GNUNET_SYSERR; | 1503 | return GNUNET_SYSERR; |
1503 | } | 1504 | } |
1504 | 1505 | ||
@@ -1516,7 +1517,7 @@ parse_port_policy(const char *port_policy, | |||
1516 | * NULL if the synatx is flawed | 1517 | * NULL if the synatx is flawed |
1517 | */ | 1518 | */ |
1518 | struct GNUNET_STRINGS_IPv4NetworkPolicy * | 1519 | struct GNUNET_STRINGS_IPv4NetworkPolicy * |
1519 | GNUNET_STRINGS_parse_ipv4_policy(const char *routeListX) | 1520 | GNUNET_STRINGS_parse_ipv4_policy (const char *routeListX) |
1520 | { | 1521 | { |
1521 | unsigned int count; | 1522 | unsigned int count; |
1522 | unsigned int i; | 1523 | unsigned int i; |
@@ -1534,164 +1535,164 @@ GNUNET_STRINGS_parse_ipv4_policy(const char *routeListX) | |||
1534 | 1535 | ||
1535 | if (NULL == routeListX) | 1536 | if (NULL == routeListX) |
1536 | return NULL; | 1537 | return NULL; |
1537 | len = strlen(routeListX); | 1538 | len = strlen (routeListX); |
1538 | if (0 == len) | 1539 | if (0 == len) |
1539 | return NULL; | 1540 | return NULL; |
1540 | routeList = GNUNET_strdup(routeListX); | 1541 | routeList = GNUNET_strdup (routeListX); |
1541 | count = 0; | 1542 | count = 0; |
1542 | for (i = 0; i < len; i++) | 1543 | for (i = 0; i < len; i++) |
1543 | if (routeList[i] == ';') | 1544 | if (routeList[i] == ';') |
1544 | count++; | 1545 | count++; |
1545 | result = GNUNET_malloc(sizeof(struct GNUNET_STRINGS_IPv4NetworkPolicy) * | 1546 | result = GNUNET_malloc (sizeof(struct GNUNET_STRINGS_IPv4NetworkPolicy) |
1546 | (count + 1)); | 1547 | * (count + 1)); |
1547 | i = 0; | 1548 | i = 0; |
1548 | pos = 0; | 1549 | pos = 0; |
1549 | while (i < count) | 1550 | while (i < count) |
1550 | { | 1551 | { |
1551 | for (colon = pos; ':' != routeList[colon]; colon++) | 1552 | for (colon = pos; ':' != routeList[colon]; colon++) |
1552 | if ((';' == routeList[colon]) || ('\0' == routeList[colon])) | 1553 | if ((';' == routeList[colon]) || ('\0' == routeList[colon])) |
1553 | break; | 1554 | break; |
1554 | for (end = colon; ';' != routeList[end]; end++) | 1555 | for (end = colon; ';' != routeList[end]; end++) |
1555 | if ('\0' == routeList[end]) | ||
1556 | break; | ||
1557 | if ('\0' == routeList[end]) | 1556 | if ('\0' == routeList[end]) |
1558 | break; | 1557 | break; |
1559 | routeList[end] = '\0'; | 1558 | if ('\0' == routeList[end]) |
1560 | if (':' == routeList[colon]) | 1559 | break; |
1561 | { | 1560 | routeList[end] = '\0'; |
1562 | routeList[colon] = '\0'; | 1561 | if (':' == routeList[colon]) |
1563 | if (GNUNET_OK != parse_port_policy(&routeList[colon + 1], &result[i].pp)) | 1562 | { |
1564 | break; | 1563 | routeList[colon] = '\0'; |
1565 | } | 1564 | if (GNUNET_OK != parse_port_policy (&routeList[colon + 1], &result[i].pp)) |
1566 | cnt = sscanf(&routeList[pos], | 1565 | break; |
1567 | "%u.%u.%u.%u/%u.%u.%u.%u%1s", | 1566 | } |
1568 | &temps[0], | 1567 | cnt = sscanf (&routeList[pos], |
1569 | &temps[1], | 1568 | "%u.%u.%u.%u/%u.%u.%u.%u%1s", |
1570 | &temps[2], | 1569 | &temps[0], |
1571 | &temps[3], | 1570 | &temps[1], |
1572 | &temps[4], | 1571 | &temps[2], |
1573 | &temps[5], | 1572 | &temps[3], |
1574 | &temps[6], | 1573 | &temps[4], |
1575 | &temps[7], | 1574 | &temps[5], |
1576 | dummy); | 1575 | &temps[6], |
1577 | if (8 == cnt) | 1576 | &temps[7], |
1577 | dummy); | ||
1578 | if (8 == cnt) | ||
1579 | { | ||
1580 | for (j = 0; j < 8; j++) | ||
1581 | if (temps[j] > 0xFF) | ||
1578 | { | 1582 | { |
1579 | for (j = 0; j < 8; j++) | 1583 | LOG (GNUNET_ERROR_TYPE_WARNING, |
1580 | if (temps[j] > 0xFF) | 1584 | _ ("Invalid format for IP: `%s'\n"), |
1581 | { | 1585 | &routeList[pos]); |
1582 | LOG(GNUNET_ERROR_TYPE_WARNING, | 1586 | GNUNET_free (result); |
1583 | _("Invalid format for IP: `%s'\n"), | 1587 | GNUNET_free (routeList); |
1584 | &routeList[pos]); | 1588 | return NULL; |
1585 | GNUNET_free(result); | ||
1586 | GNUNET_free(routeList); | ||
1587 | return NULL; | ||
1588 | } | ||
1589 | result[i].network.s_addr = htonl((temps[0] << 24) + (temps[1] << 16) + | ||
1590 | (temps[2] << 8) + temps[3]); | ||
1591 | result[i].netmask.s_addr = htonl((temps[4] << 24) + (temps[5] << 16) + | ||
1592 | (temps[6] << 8) + temps[7]); | ||
1593 | pos = end + 1; | ||
1594 | i++; | ||
1595 | continue; | ||
1596 | } | 1589 | } |
1597 | /* try second notation */ | 1590 | result[i].network.s_addr = htonl ((temps[0] << 24) + (temps[1] << 16) |
1598 | cnt = sscanf(&routeList[pos], | 1591 | + (temps[2] << 8) + temps[3]); |
1599 | "%u.%u.%u.%u/%u%1s", | 1592 | result[i].netmask.s_addr = htonl ((temps[4] << 24) + (temps[5] << 16) |
1600 | &temps[0], | 1593 | + (temps[6] << 8) + temps[7]); |
1601 | &temps[1], | 1594 | pos = end + 1; |
1602 | &temps[2], | 1595 | i++; |
1603 | &temps[3], | 1596 | continue; |
1604 | &slash, | 1597 | } |
1605 | dummy); | 1598 | /* try second notation */ |
1606 | if (5 == cnt) | 1599 | cnt = sscanf (&routeList[pos], |
1600 | "%u.%u.%u.%u/%u%1s", | ||
1601 | &temps[0], | ||
1602 | &temps[1], | ||
1603 | &temps[2], | ||
1604 | &temps[3], | ||
1605 | &slash, | ||
1606 | dummy); | ||
1607 | if (5 == cnt) | ||
1608 | { | ||
1609 | for (j = 0; j < 4; j++) | ||
1610 | if (temps[j] > 0xFF) | ||
1607 | { | 1611 | { |
1608 | for (j = 0; j < 4; j++) | 1612 | LOG (GNUNET_ERROR_TYPE_WARNING, |
1609 | if (temps[j] > 0xFF) | 1613 | _ ("Invalid format for IP: `%s'\n"), |
1610 | { | 1614 | &routeList[pos]); |
1611 | LOG(GNUNET_ERROR_TYPE_WARNING, | 1615 | GNUNET_free (result); |
1612 | _("Invalid format for IP: `%s'\n"), | 1616 | GNUNET_free (routeList); |
1613 | &routeList[pos]); | 1617 | return NULL; |
1614 | GNUNET_free(result); | ||
1615 | GNUNET_free(routeList); | ||
1616 | return NULL; | ||
1617 | } | ||
1618 | result[i].network.s_addr = htonl((temps[0] << 24) + (temps[1] << 16) + | ||
1619 | (temps[2] << 8) + temps[3]); | ||
1620 | if ((slash <= 32) && (slash >= 0)) | ||
1621 | { | ||
1622 | result[i].netmask.s_addr = 0; | ||
1623 | while (slash > 0) | ||
1624 | { | ||
1625 | result[i].netmask.s_addr = | ||
1626 | (result[i].netmask.s_addr >> 1) + 0x80000000; | ||
1627 | slash--; | ||
1628 | } | ||
1629 | result[i].netmask.s_addr = htonl(result[i].netmask.s_addr); | ||
1630 | pos = end + 1; | ||
1631 | i++; | ||
1632 | continue; | ||
1633 | } | ||
1634 | else | ||
1635 | { | ||
1636 | LOG(GNUNET_ERROR_TYPE_WARNING, | ||
1637 | _("Invalid network notation ('/%d' is not legal in IPv4 CIDR)."), | ||
1638 | slash); | ||
1639 | GNUNET_free(result); | ||
1640 | GNUNET_free(routeList); | ||
1641 | return NULL; /* error */ | ||
1642 | } | ||
1643 | } | 1618 | } |
1644 | /* try third notation */ | 1619 | result[i].network.s_addr = htonl ((temps[0] << 24) + (temps[1] << 16) |
1645 | slash = 32; | 1620 | + (temps[2] << 8) + temps[3]); |
1646 | cnt = sscanf(&routeList[pos], | 1621 | if ((slash <= 32) && (slash >= 0)) |
1647 | "%u.%u.%u.%u%1s", | 1622 | { |
1648 | &temps[0], | 1623 | result[i].netmask.s_addr = 0; |
1649 | &temps[1], | 1624 | while (slash > 0) |
1650 | &temps[2], | ||
1651 | &temps[3], | ||
1652 | dummy); | ||
1653 | if (4 == cnt) | ||
1654 | { | 1625 | { |
1655 | for (j = 0; j < 4; j++) | 1626 | result[i].netmask.s_addr = |
1656 | if (temps[j] > 0xFF) | 1627 | (result[i].netmask.s_addr >> 1) + 0x80000000; |
1657 | { | 1628 | slash--; |
1658 | LOG(GNUNET_ERROR_TYPE_WARNING, | ||
1659 | _("Invalid format for IP: `%s'\n"), | ||
1660 | &routeList[pos]); | ||
1661 | GNUNET_free(result); | ||
1662 | GNUNET_free(routeList); | ||
1663 | return NULL; | ||
1664 | } | ||
1665 | result[i].network.s_addr = htonl((temps[0] << 24) + (temps[1] << 16) + | ||
1666 | (temps[2] << 8) + temps[3]); | ||
1667 | result[i].netmask.s_addr = 0; | ||
1668 | while (slash > 0) | ||
1669 | { | ||
1670 | result[i].netmask.s_addr = (result[i].netmask.s_addr >> 1) + 0x80000000; | ||
1671 | slash--; | ||
1672 | } | ||
1673 | result[i].netmask.s_addr = htonl(result[i].netmask.s_addr); | ||
1674 | pos = end + 1; | ||
1675 | i++; | ||
1676 | continue; | ||
1677 | } | 1629 | } |
1678 | LOG(GNUNET_ERROR_TYPE_WARNING, | 1630 | result[i].netmask.s_addr = htonl (result[i].netmask.s_addr); |
1679 | _("Invalid format for IP: `%s'\n"), | 1631 | pos = end + 1; |
1680 | &routeList[pos]); | 1632 | i++; |
1681 | GNUNET_free(result); | 1633 | continue; |
1682 | GNUNET_free(routeList); | 1634 | } |
1683 | return NULL; /* error */ | 1635 | else |
1636 | { | ||
1637 | LOG (GNUNET_ERROR_TYPE_WARNING, | ||
1638 | _ ("Invalid network notation ('/%d' is not legal in IPv4 CIDR)."), | ||
1639 | slash); | ||
1640 | GNUNET_free (result); | ||
1641 | GNUNET_free (routeList); | ||
1642 | return NULL; /* error */ | ||
1643 | } | ||
1684 | } | 1644 | } |
1685 | if (pos < strlen(routeList)) | 1645 | /* try third notation */ |
1646 | slash = 32; | ||
1647 | cnt = sscanf (&routeList[pos], | ||
1648 | "%u.%u.%u.%u%1s", | ||
1649 | &temps[0], | ||
1650 | &temps[1], | ||
1651 | &temps[2], | ||
1652 | &temps[3], | ||
1653 | dummy); | ||
1654 | if (4 == cnt) | ||
1686 | { | 1655 | { |
1687 | LOG(GNUNET_ERROR_TYPE_WARNING, | 1656 | for (j = 0; j < 4; j++) |
1688 | _("Invalid format: `%s'\n"), | 1657 | if (temps[j] > 0xFF) |
1689 | &routeListX[pos]); | 1658 | { |
1690 | GNUNET_free(result); | 1659 | LOG (GNUNET_ERROR_TYPE_WARNING, |
1691 | GNUNET_free(routeList); | 1660 | _ ("Invalid format for IP: `%s'\n"), |
1692 | return NULL; /* oops */ | 1661 | &routeList[pos]); |
1662 | GNUNET_free (result); | ||
1663 | GNUNET_free (routeList); | ||
1664 | return NULL; | ||
1665 | } | ||
1666 | result[i].network.s_addr = htonl ((temps[0] << 24) + (temps[1] << 16) | ||
1667 | + (temps[2] << 8) + temps[3]); | ||
1668 | result[i].netmask.s_addr = 0; | ||
1669 | while (slash > 0) | ||
1670 | { | ||
1671 | result[i].netmask.s_addr = (result[i].netmask.s_addr >> 1) + 0x80000000; | ||
1672 | slash--; | ||
1673 | } | ||
1674 | result[i].netmask.s_addr = htonl (result[i].netmask.s_addr); | ||
1675 | pos = end + 1; | ||
1676 | i++; | ||
1677 | continue; | ||
1693 | } | 1678 | } |
1694 | GNUNET_free(routeList); | 1679 | LOG (GNUNET_ERROR_TYPE_WARNING, |
1680 | _ ("Invalid format for IP: `%s'\n"), | ||
1681 | &routeList[pos]); | ||
1682 | GNUNET_free (result); | ||
1683 | GNUNET_free (routeList); | ||
1684 | return NULL; /* error */ | ||
1685 | } | ||
1686 | if (pos < strlen (routeList)) | ||
1687 | { | ||
1688 | LOG (GNUNET_ERROR_TYPE_WARNING, | ||
1689 | _ ("Invalid format: `%s'\n"), | ||
1690 | &routeListX[pos]); | ||
1691 | GNUNET_free (result); | ||
1692 | GNUNET_free (routeList); | ||
1693 | return NULL; /* oops */ | ||
1694 | } | ||
1695 | GNUNET_free (routeList); | ||
1695 | return result; /* ok */ | 1696 | return result; /* ok */ |
1696 | } | 1697 | } |
1697 | 1698 | ||
@@ -1708,7 +1709,7 @@ GNUNET_STRINGS_parse_ipv4_policy(const char *routeListX) | |||
1708 | * @return the converted list, 0-terminated, NULL if the synatx is flawed | 1709 | * @return the converted list, 0-terminated, NULL if the synatx is flawed |
1709 | */ | 1710 | */ |
1710 | struct GNUNET_STRINGS_IPv6NetworkPolicy * | 1711 | struct GNUNET_STRINGS_IPv6NetworkPolicy * |
1711 | GNUNET_STRINGS_parse_ipv6_policy(const char *routeListX) | 1712 | GNUNET_STRINGS_parse_ipv6_policy (const char *routeListX) |
1712 | { | 1713 | { |
1713 | unsigned int count; | 1714 | unsigned int count; |
1714 | unsigned int i; | 1715 | unsigned int i; |
@@ -1727,110 +1728,110 @@ GNUNET_STRINGS_parse_ipv6_policy(const char *routeListX) | |||
1727 | 1728 | ||
1728 | if (NULL == routeListX) | 1729 | if (NULL == routeListX) |
1729 | return NULL; | 1730 | return NULL; |
1730 | len = strlen(routeListX); | 1731 | len = strlen (routeListX); |
1731 | if (0 == len) | 1732 | if (0 == len) |
1732 | return NULL; | 1733 | return NULL; |
1733 | routeList = GNUNET_strdup(routeListX); | 1734 | routeList = GNUNET_strdup (routeListX); |
1734 | count = 0; | 1735 | count = 0; |
1735 | for (i = 0; i < len; i++) | 1736 | for (i = 0; i < len; i++) |
1736 | if (';' == routeList[i]) | 1737 | if (';' == routeList[i]) |
1737 | count++; | 1738 | count++; |
1738 | if (';' != routeList[len - 1]) | 1739 | if (';' != routeList[len - 1]) |
1739 | { | 1740 | { |
1740 | LOG(GNUNET_ERROR_TYPE_WARNING, | 1741 | LOG (GNUNET_ERROR_TYPE_WARNING, |
1741 | _("Invalid network notation (does not end with ';': `%s')\n"), | 1742 | _ ("Invalid network notation (does not end with ';': `%s')\n"), |
1742 | routeList); | 1743 | routeList); |
1743 | GNUNET_free(routeList); | 1744 | GNUNET_free (routeList); |
1744 | return NULL; | 1745 | return NULL; |
1745 | } | 1746 | } |
1746 | 1747 | ||
1747 | result = GNUNET_malloc(sizeof(struct GNUNET_STRINGS_IPv6NetworkPolicy) * | 1748 | result = GNUNET_malloc (sizeof(struct GNUNET_STRINGS_IPv6NetworkPolicy) |
1748 | (count + 1)); | 1749 | * (count + 1)); |
1749 | i = 0; | 1750 | i = 0; |
1750 | pos = 0; | 1751 | pos = 0; |
1751 | while (i < count) | 1752 | while (i < count) |
1753 | { | ||
1754 | start = pos; | ||
1755 | while (';' != routeList[pos]) | ||
1756 | pos++; | ||
1757 | slash = pos; | ||
1758 | while ((slash >= start) && (routeList[slash] != '/')) | ||
1759 | slash--; | ||
1760 | |||
1761 | if (slash < start) | ||
1752 | { | 1762 | { |
1753 | start = pos; | 1763 | memset (&result[i].netmask, 0xFF, sizeof(struct in6_addr)); |
1754 | while (';' != routeList[pos]) | ||
1755 | pos++; | ||
1756 | slash = pos; | 1764 | slash = pos; |
1757 | while ((slash >= start) && (routeList[slash] != '/')) | 1765 | } |
1758 | slash--; | 1766 | else |
1759 | 1767 | { | |
1760 | if (slash < start) | 1768 | routeList[pos] = '\0'; |
1761 | { | 1769 | for (colon = pos; ':' != routeList[colon]; colon--) |
1762 | memset(&result[i].netmask, 0xFF, sizeof(struct in6_addr)); | 1770 | if ('/' == routeList[colon]) |
1763 | slash = pos; | 1771 | break; |
1764 | } | 1772 | if (':' == routeList[colon]) |
1765 | else | 1773 | { |
1774 | routeList[colon] = '\0'; | ||
1775 | if (GNUNET_OK != | ||
1776 | parse_port_policy (&routeList[colon + 1], &result[i].pp)) | ||
1766 | { | 1777 | { |
1767 | routeList[pos] = '\0'; | 1778 | GNUNET_free (result); |
1768 | for (colon = pos; ':' != routeList[colon]; colon--) | 1779 | GNUNET_free (routeList); |
1769 | if ('/' == routeList[colon]) | 1780 | return NULL; |
1770 | break; | ||
1771 | if (':' == routeList[colon]) | ||
1772 | { | ||
1773 | routeList[colon] = '\0'; | ||
1774 | if (GNUNET_OK != | ||
1775 | parse_port_policy(&routeList[colon + 1], &result[i].pp)) | ||
1776 | { | ||
1777 | GNUNET_free(result); | ||
1778 | GNUNET_free(routeList); | ||
1779 | return NULL; | ||
1780 | } | ||
1781 | } | ||
1782 | ret = inet_pton(AF_INET6, &routeList[slash + 1], &result[i].netmask); | ||
1783 | if (ret <= 0) | ||
1784 | { | ||
1785 | save = errno; | ||
1786 | if ((1 != sscanf(&routeList[slash + 1], "%u%1s", &bits, dummy)) || | ||
1787 | (bits > 128)) | ||
1788 | { | ||
1789 | if (0 == ret) | ||
1790 | LOG(GNUNET_ERROR_TYPE_WARNING, | ||
1791 | _("Wrong format `%s' for netmask\n"), | ||
1792 | &routeList[slash + 1]); | ||
1793 | else | ||
1794 | { | ||
1795 | errno = save; | ||
1796 | LOG_STRERROR(GNUNET_ERROR_TYPE_WARNING, "inet_pton"); | ||
1797 | } | ||
1798 | GNUNET_free(result); | ||
1799 | GNUNET_free(routeList); | ||
1800 | return NULL; | ||
1801 | } | ||
1802 | off = 0; | ||
1803 | while (bits > 8) | ||
1804 | { | ||
1805 | result[i].netmask.s6_addr[off++] = 0xFF; | ||
1806 | bits -= 8; | ||
1807 | } | ||
1808 | while (bits > 0) | ||
1809 | { | ||
1810 | result[i].netmask.s6_addr[off] = | ||
1811 | (result[i].netmask.s6_addr[off] >> 1) + 0x80; | ||
1812 | bits--; | ||
1813 | } | ||
1814 | } | ||
1815 | } | 1781 | } |
1816 | routeList[slash] = '\0'; | 1782 | } |
1817 | ret = inet_pton(AF_INET6, &routeList[start], &result[i].network); | 1783 | ret = inet_pton (AF_INET6, &routeList[slash + 1], &result[i].netmask); |
1818 | if (ret <= 0) | 1784 | if (ret <= 0) |
1785 | { | ||
1786 | save = errno; | ||
1787 | if ((1 != sscanf (&routeList[slash + 1], "%u%1s", &bits, dummy)) || | ||
1788 | (bits > 128)) | ||
1819 | { | 1789 | { |
1820 | if (0 == ret) | 1790 | if (0 == ret) |
1821 | LOG(GNUNET_ERROR_TYPE_WARNING, | 1791 | LOG (GNUNET_ERROR_TYPE_WARNING, |
1822 | _("Wrong format `%s' for network\n"), | 1792 | _ ("Wrong format `%s' for netmask\n"), |
1823 | &routeList[slash + 1]); | 1793 | &routeList[slash + 1]); |
1824 | else | 1794 | else |
1825 | LOG_STRERROR(GNUNET_ERROR_TYPE_ERROR, "inet_pton"); | 1795 | { |
1826 | GNUNET_free(result); | 1796 | errno = save; |
1827 | GNUNET_free(routeList); | 1797 | LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "inet_pton"); |
1798 | } | ||
1799 | GNUNET_free (result); | ||
1800 | GNUNET_free (routeList); | ||
1828 | return NULL; | 1801 | return NULL; |
1829 | } | 1802 | } |
1830 | pos++; | 1803 | off = 0; |
1831 | i++; | 1804 | while (bits > 8) |
1805 | { | ||
1806 | result[i].netmask.s6_addr[off++] = 0xFF; | ||
1807 | bits -= 8; | ||
1808 | } | ||
1809 | while (bits > 0) | ||
1810 | { | ||
1811 | result[i].netmask.s6_addr[off] = | ||
1812 | (result[i].netmask.s6_addr[off] >> 1) + 0x80; | ||
1813 | bits--; | ||
1814 | } | ||
1815 | } | ||
1832 | } | 1816 | } |
1833 | GNUNET_free(routeList); | 1817 | routeList[slash] = '\0'; |
1818 | ret = inet_pton (AF_INET6, &routeList[start], &result[i].network); | ||
1819 | if (ret <= 0) | ||
1820 | { | ||
1821 | if (0 == ret) | ||
1822 | LOG (GNUNET_ERROR_TYPE_WARNING, | ||
1823 | _ ("Wrong format `%s' for network\n"), | ||
1824 | &routeList[slash + 1]); | ||
1825 | else | ||
1826 | LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "inet_pton"); | ||
1827 | GNUNET_free (result); | ||
1828 | GNUNET_free (routeList); | ||
1829 | return NULL; | ||
1830 | } | ||
1831 | pos++; | ||
1832 | i++; | ||
1833 | } | ||
1834 | GNUNET_free (routeList); | ||
1834 | return result; | 1835 | return result; |
1835 | } | 1836 | } |
1836 | 1837 | ||
@@ -1853,46 +1854,46 @@ static char *cvt = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" | |||
1853 | * @return the size of the output | 1854 | * @return the size of the output |
1854 | */ | 1855 | */ |
1855 | size_t | 1856 | size_t |
1856 | GNUNET_STRINGS_base64_encode(const void *in, size_t len, char **output) | 1857 | GNUNET_STRINGS_base64_encode (const void *in, size_t len, char **output) |
1857 | { | 1858 | { |
1858 | const char *data = in; | 1859 | const char *data = in; |
1859 | size_t ret; | 1860 | size_t ret; |
1860 | char *opt; | 1861 | char *opt; |
1861 | 1862 | ||
1862 | ret = 0; | 1863 | ret = 0; |
1863 | opt = GNUNET_malloc(2 + (len * 4 / 3) + 8); | 1864 | opt = GNUNET_malloc (2 + (len * 4 / 3) + 8); |
1864 | for (size_t i = 0; i < len; ++i) | 1865 | for (size_t i = 0; i < len; ++i) |
1866 | { | ||
1867 | char c; | ||
1868 | |||
1869 | c = (data[i] >> 2) & 0x3f; | ||
1870 | opt[ret++] = cvt[(int) c]; | ||
1871 | c = (data[i] << 4) & 0x3f; | ||
1872 | if (++i < len) | ||
1873 | c |= (data[i] >> 4) & 0x0f; | ||
1874 | opt[ret++] = cvt[(int) c]; | ||
1875 | if (i < len) | ||
1865 | { | 1876 | { |
1866 | char c; | 1877 | c = (data[i] << 2) & 0x3f; |
1867 | |||
1868 | c = (data[i] >> 2) & 0x3f; | ||
1869 | opt[ret++] = cvt[(int)c]; | ||
1870 | c = (data[i] << 4) & 0x3f; | ||
1871 | if (++i < len) | 1878 | if (++i < len) |
1872 | c |= (data[i] >> 4) & 0x0f; | 1879 | c |= (data[i] >> 6) & 0x03; |
1873 | opt[ret++] = cvt[(int)c]; | 1880 | opt[ret++] = cvt[(int) c]; |
1874 | if (i < len) | ||
1875 | { | ||
1876 | c = (data[i] << 2) & 0x3f; | ||
1877 | if (++i < len) | ||
1878 | c |= (data[i] >> 6) & 0x03; | ||
1879 | opt[ret++] = cvt[(int)c]; | ||
1880 | } | ||
1881 | else | ||
1882 | { | ||
1883 | ++i; | ||
1884 | opt[ret++] = FILLCHAR; | ||
1885 | } | ||
1886 | if (i < len) | ||
1887 | { | ||
1888 | c = data[i] & 0x3f; | ||
1889 | opt[ret++] = cvt[(int)c]; | ||
1890 | } | ||
1891 | else | ||
1892 | { | ||
1893 | opt[ret++] = FILLCHAR; | ||
1894 | } | ||
1895 | } | 1881 | } |
1882 | else | ||
1883 | { | ||
1884 | ++i; | ||
1885 | opt[ret++] = FILLCHAR; | ||
1886 | } | ||
1887 | if (i < len) | ||
1888 | { | ||
1889 | c = data[i] & 0x3f; | ||
1890 | opt[ret++] = cvt[(int) c]; | ||
1891 | } | ||
1892 | else | ||
1893 | { | ||
1894 | opt[ret++] = FILLCHAR; | ||
1895 | } | ||
1896 | } | ||
1896 | *output = opt; | 1897 | *output = opt; |
1897 | return ret; | 1898 | return ret; |
1898 | } | 1899 | } |
@@ -1917,61 +1918,61 @@ GNUNET_STRINGS_base64_encode(const void *in, size_t len, char **output) | |||
1917 | * @return the size of the output | 1918 | * @return the size of the output |
1918 | */ | 1919 | */ |
1919 | size_t | 1920 | size_t |
1920 | GNUNET_STRINGS_base64_decode(const char *data, size_t len, void **out) | 1921 | GNUNET_STRINGS_base64_decode (const char *data, size_t len, void **out) |
1921 | { | 1922 | { |
1922 | char *output; | 1923 | char *output; |
1923 | size_t ret = 0; | 1924 | size_t ret = 0; |
1924 | 1925 | ||
1925 | #define CHECK_CRLF \ | 1926 | #define CHECK_CRLF \ |
1926 | while (data[i] == '\r' || data[i] == '\n') \ | 1927 | while (data[i] == '\r' || data[i] == '\n') \ |
1927 | { \ | 1928 | { \ |
1928 | GNUNET_log(GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK, \ | 1929 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK, \ |
1929 | "ignoring CR/LF\n"); \ | 1930 | "ignoring CR/LF\n"); \ |
1930 | i++; \ | 1931 | i++; \ |
1931 | if (i >= len) \ | 1932 | if (i >= len) \ |
1932 | goto END; \ | 1933 | goto END; \ |
1933 | } | 1934 | } |
1934 | 1935 | ||
1935 | output = GNUNET_malloc((len * 3 / 4) + 8); | 1936 | output = GNUNET_malloc ((len * 3 / 4) + 8); |
1936 | GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, | 1937 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, |
1937 | "base64_decode decoding len=%d\n", | 1938 | "base64_decode decoding len=%d\n", |
1938 | (int)len); | 1939 | (int) len); |
1939 | for (size_t i = 0; i < len; ++i) | 1940 | for (size_t i = 0; i < len; ++i) |
1940 | { | 1941 | { |
1941 | char c; | 1942 | char c; |
1942 | char c1; | 1943 | char c1; |
1943 | 1944 | ||
1945 | CHECK_CRLF; | ||
1946 | if (FILLCHAR == data[i]) | ||
1947 | break; | ||
1948 | c = (char) cvtfind (data[i]); | ||
1949 | ++i; | ||
1950 | CHECK_CRLF; | ||
1951 | c1 = (char) cvtfind (data[i]); | ||
1952 | c = (c << 2) | ((c1 >> 4) & 0x3); | ||
1953 | output[ret++] = c; | ||
1954 | if (++i < len) | ||
1955 | { | ||
1944 | CHECK_CRLF; | 1956 | CHECK_CRLF; |
1945 | if (FILLCHAR == data[i]) | 1957 | c = data[i]; |
1958 | if (FILLCHAR == c) | ||
1946 | break; | 1959 | break; |
1947 | c = (char)cvtfind(data[i]); | 1960 | c = (char) cvtfind (c); |
1948 | ++i; | 1961 | c1 = ((c1 << 4) & 0xf0) | ((c >> 2) & 0xf); |
1962 | output[ret++] = c1; | ||
1963 | } | ||
1964 | if (++i < len) | ||
1965 | { | ||
1949 | CHECK_CRLF; | 1966 | CHECK_CRLF; |
1950 | c1 = (char)cvtfind(data[i]); | 1967 | c1 = data[i]; |
1951 | c = (c << 2) | ((c1 >> 4) & 0x3); | 1968 | if (FILLCHAR == c1) |
1969 | break; | ||
1970 | |||
1971 | c1 = (char) cvtfind (c1); | ||
1972 | c = ((c << 6) & 0xc0) | c1; | ||
1952 | output[ret++] = c; | 1973 | output[ret++] = c; |
1953 | if (++i < len) | ||
1954 | { | ||
1955 | CHECK_CRLF; | ||
1956 | c = data[i]; | ||
1957 | if (FILLCHAR == c) | ||
1958 | break; | ||
1959 | c = (char)cvtfind(c); | ||
1960 | c1 = ((c1 << 4) & 0xf0) | ((c >> 2) & 0xf); | ||
1961 | output[ret++] = c1; | ||
1962 | } | ||
1963 | if (++i < len) | ||
1964 | { | ||
1965 | CHECK_CRLF; | ||
1966 | c1 = data[i]; | ||
1967 | if (FILLCHAR == c1) | ||
1968 | break; | ||
1969 | |||
1970 | c1 = (char)cvtfind(c1); | ||
1971 | c = ((c << 6) & 0xc0) | c1; | ||
1972 | output[ret++] = c; | ||
1973 | } | ||
1974 | } | 1974 | } |
1975 | } | ||
1975 | END: | 1976 | END: |
1976 | *out = output; | 1977 | *out = output; |
1977 | return ret; | 1978 | return ret; |