]>
Commit | Line | Data |
---|---|---|
064af421 | 1 | /* |
4749f73d | 2 | * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013 Nicira, Inc. |
064af421 | 3 | * |
a14bc59f BP |
4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
5 | * you may not use this file except in compliance with the License. | |
6 | * You may obtain a copy of the License at: | |
064af421 | 7 | * |
a14bc59f BP |
8 | * http://www.apache.org/licenses/LICENSE-2.0 |
9 | * | |
10 | * Unless required by applicable law or agreed to in writing, software | |
11 | * distributed under the License is distributed on an "AS IS" BASIS, | |
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
13 | * See the License for the specific language governing permissions and | |
14 | * limitations under the License. | |
064af421 BP |
15 | */ |
16 | ||
17 | #include <config.h> | |
18 | #include "util.h" | |
ed2232fc | 19 | #include <ctype.h> |
064af421 | 20 | #include <errno.h> |
711e0157 | 21 | #include <limits.h> |
5fcbed74 | 22 | #include <pthread.h> |
064af421 | 23 | #include <stdarg.h> |
711e0157 | 24 | #include <stdint.h> |
064af421 BP |
25 | #include <stdio.h> |
26 | #include <stdlib.h> | |
27 | #include <string.h> | |
fee0c963 | 28 | #include <sys/stat.h> |
daf03c53 | 29 | #include <unistd.h> |
ed2232fc | 30 | #include "bitmap.h" |
ddc4f8e2 | 31 | #include "byte-order.h" |
064af421 | 32 | #include "coverage.h" |
728a8b14 | 33 | #include "ovs-thread.h" |
daf03c53 | 34 | #include "vlog.h" |
1805ee4b EM |
35 | #ifdef HAVE_PTHREAD_SET_NAME_NP |
36 | #include <pthread_np.h> | |
37 | #endif | |
daf03c53 | 38 | |
d98e6007 | 39 | VLOG_DEFINE_THIS_MODULE(util); |
5136ce49 | 40 | |
d76f09ea BP |
41 | COVERAGE_DEFINE(util_xalloc); |
42 | ||
781dee08 | 43 | /* argv[0] without directory names. */ |
064af421 | 44 | const char *program_name; |
781dee08 | 45 | |
bc9fb3a9 BP |
46 | /* Name for the currently running thread or process, for log messages, process |
47 | * listings, and debuggers. */ | |
48 | DEFINE_PER_THREAD_MALLOCED_DATA(char *, subprogram_name); | |
781dee08 BP |
49 | |
50 | /* --version option output. */ | |
55d5bb44 | 51 | static char *program_version; |
064af421 | 52 | |
5fcbed74 | 53 | /* Buffer used by ovs_strerror(). */ |
2ba4f163 BP |
54 | DEFINE_STATIC_PER_THREAD_DATA(struct { char s[128]; }, |
55 | strerror_buffer, | |
56 | { "" }); | |
5fcbed74 | 57 | |
4749f73d BP |
58 | void |
59 | ovs_assert_failure(const char *where, const char *function, | |
60 | const char *condition) | |
61 | { | |
62 | /* Prevent an infinite loop (or stack overflow) in case VLOG_ABORT happens | |
63 | * to trigger an assertion failure of its own. */ | |
64 | static int reentry = 0; | |
65 | ||
66 | switch (reentry++) { | |
67 | case 0: | |
68 | VLOG_ABORT("%s: assertion %s failed in %s()", | |
69 | where, condition, function); | |
428b2edd | 70 | OVS_NOT_REACHED(); |
4749f73d BP |
71 | |
72 | case 1: | |
73 | fprintf(stderr, "%s: assertion %s failed in %s()", | |
74 | where, condition, function); | |
75 | abort(); | |
76 | ||
77 | default: | |
78 | abort(); | |
79 | } | |
80 | } | |
81 | ||
064af421 | 82 | void |
d295e8e9 | 83 | out_of_memory(void) |
064af421 | 84 | { |
c1c8308a | 85 | ovs_abort(0, "virtual memory exhausted"); |
064af421 BP |
86 | } |
87 | ||
88 | void * | |
d295e8e9 | 89 | xcalloc(size_t count, size_t size) |
064af421 BP |
90 | { |
91 | void *p = count && size ? calloc(count, size) : malloc(1); | |
92 | COVERAGE_INC(util_xalloc); | |
93 | if (p == NULL) { | |
94 | out_of_memory(); | |
95 | } | |
96 | return p; | |
97 | } | |
98 | ||
ec6fde61 BP |
99 | void * |
100 | xzalloc(size_t size) | |
101 | { | |
102 | return xcalloc(1, size); | |
103 | } | |
104 | ||
064af421 | 105 | void * |
d295e8e9 | 106 | xmalloc(size_t size) |
064af421 BP |
107 | { |
108 | void *p = malloc(size ? size : 1); | |
109 | COVERAGE_INC(util_xalloc); | |
110 | if (p == NULL) { | |
111 | out_of_memory(); | |
112 | } | |
113 | return p; | |
114 | } | |
115 | ||
116 | void * | |
d295e8e9 | 117 | xrealloc(void *p, size_t size) |
064af421 BP |
118 | { |
119 | p = realloc(p, size ? size : 1); | |
120 | COVERAGE_INC(util_xalloc); | |
121 | if (p == NULL) { | |
122 | out_of_memory(); | |
123 | } | |
124 | return p; | |
125 | } | |
126 | ||
127 | void * | |
128 | xmemdup(const void *p_, size_t size) | |
129 | { | |
130 | void *p = xmalloc(size); | |
131 | memcpy(p, p_, size); | |
132 | return p; | |
133 | } | |
134 | ||
135 | char * | |
136 | xmemdup0(const char *p_, size_t length) | |
137 | { | |
138 | char *p = xmalloc(length + 1); | |
139 | memcpy(p, p_, length); | |
140 | p[length] = '\0'; | |
141 | return p; | |
142 | } | |
143 | ||
144 | char * | |
d295e8e9 | 145 | xstrdup(const char *s) |
064af421 BP |
146 | { |
147 | return xmemdup0(s, strlen(s)); | |
148 | } | |
149 | ||
150 | char * | |
151 | xvasprintf(const char *format, va_list args) | |
152 | { | |
153 | va_list args2; | |
154 | size_t needed; | |
155 | char *s; | |
156 | ||
157 | va_copy(args2, args); | |
158 | needed = vsnprintf(NULL, 0, format, args); | |
159 | ||
160 | s = xmalloc(needed + 1); | |
161 | ||
162 | vsnprintf(s, needed + 1, format, args2); | |
163 | va_end(args2); | |
164 | ||
165 | return s; | |
166 | } | |
167 | ||
168 | void * | |
169 | x2nrealloc(void *p, size_t *n, size_t s) | |
170 | { | |
171 | *n = *n == 0 ? 1 : 2 * *n; | |
172 | return xrealloc(p, *n * s); | |
173 | } | |
174 | ||
175 | char * | |
176 | xasprintf(const char *format, ...) | |
177 | { | |
178 | va_list args; | |
179 | char *s; | |
180 | ||
181 | va_start(args, format); | |
182 | s = xvasprintf(format, args); | |
183 | va_end(args); | |
184 | ||
185 | return s; | |
186 | } | |
187 | ||
e868fb3d BP |
188 | /* Similar to strlcpy() from OpenBSD, but it never reads more than 'size - 1' |
189 | * bytes from 'src' and doesn't return anything. */ | |
064af421 BP |
190 | void |
191 | ovs_strlcpy(char *dst, const char *src, size_t size) | |
192 | { | |
193 | if (size > 0) { | |
e868fb3d BP |
194 | size_t len = strnlen(src, size - 1); |
195 | memcpy(dst, src, len); | |
196 | dst[len] = '\0'; | |
064af421 BP |
197 | } |
198 | } | |
199 | ||
71d7c22f BP |
200 | /* Copies 'src' to 'dst'. Reads no more than 'size - 1' bytes from 'src'. |
201 | * Always null-terminates 'dst' (if 'size' is nonzero), and writes a zero byte | |
202 | * to every otherwise unused byte in 'dst'. | |
203 | * | |
204 | * Except for performance, the following call: | |
205 | * ovs_strzcpy(dst, src, size); | |
206 | * is equivalent to these two calls: | |
207 | * memset(dst, '\0', size); | |
208 | * ovs_strlcpy(dst, src, size); | |
209 | * | |
210 | * (Thus, ovs_strzcpy() is similar to strncpy() without some of the pitfalls.) | |
211 | */ | |
212 | void | |
213 | ovs_strzcpy(char *dst, const char *src, size_t size) | |
214 | { | |
215 | if (size > 0) { | |
216 | size_t len = strnlen(src, size - 1); | |
217 | memcpy(dst, src, len); | |
218 | memset(dst + len, '\0', size - len); | |
219 | } | |
220 | } | |
221 | ||
c1c8308a BP |
222 | /* Prints 'format' on stderr, formatting it like printf() does. If 'err_no' is |
223 | * nonzero, then it is formatted with ovs_retval_to_string() and appended to | |
224 | * the message inside parentheses. Then, terminates with abort(). | |
225 | * | |
226 | * This function is preferred to ovs_fatal() in a situation where it would make | |
227 | * sense for a monitoring process to restart the daemon. | |
228 | * | |
229 | * 'format' should not end with a new-line, because this function will add one | |
230 | * itself. */ | |
231 | void | |
232 | ovs_abort(int err_no, const char *format, ...) | |
233 | { | |
234 | va_list args; | |
235 | ||
236 | va_start(args, format); | |
d41d4b71 BP |
237 | ovs_abort_valist(err_no, format, args); |
238 | } | |
c1c8308a | 239 | |
d41d4b71 BP |
240 | /* Same as ovs_abort() except that the arguments are supplied as a va_list. */ |
241 | void | |
242 | ovs_abort_valist(int err_no, const char *format, va_list args) | |
243 | { | |
244 | ovs_error_valist(err_no, format, args); | |
c1c8308a BP |
245 | abort(); |
246 | } | |
247 | ||
248 | /* Prints 'format' on stderr, formatting it like printf() does. If 'err_no' is | |
249 | * nonzero, then it is formatted with ovs_retval_to_string() and appended to | |
250 | * the message inside parentheses. Then, terminates with EXIT_FAILURE. | |
251 | * | |
252 | * 'format' should not end with a new-line, because this function will add one | |
253 | * itself. */ | |
064af421 BP |
254 | void |
255 | ovs_fatal(int err_no, const char *format, ...) | |
256 | { | |
257 | va_list args; | |
258 | ||
064af421 | 259 | va_start(args, format); |
fcaddd4d BP |
260 | ovs_fatal_valist(err_no, format, args); |
261 | } | |
064af421 | 262 | |
fcaddd4d BP |
263 | /* Same as ovs_fatal() except that the arguments are supplied as a va_list. */ |
264 | void | |
265 | ovs_fatal_valist(int err_no, const char *format, va_list args) | |
266 | { | |
267 | ovs_error_valist(err_no, format, args); | |
064af421 BP |
268 | exit(EXIT_FAILURE); |
269 | } | |
270 | ||
c1c8308a BP |
271 | /* Prints 'format' on stderr, formatting it like printf() does. If 'err_no' is |
272 | * nonzero, then it is formatted with ovs_retval_to_string() and appended to | |
273 | * the message inside parentheses. | |
274 | * | |
275 | * 'format' should not end with a new-line, because this function will add one | |
276 | * itself. */ | |
064af421 BP |
277 | void |
278 | ovs_error(int err_no, const char *format, ...) | |
279 | { | |
064af421 BP |
280 | va_list args; |
281 | ||
064af421 | 282 | va_start(args, format); |
c1c8308a | 283 | ovs_error_valist(err_no, format, args); |
064af421 | 284 | va_end(args); |
c1c8308a BP |
285 | } |
286 | ||
287 | /* Same as ovs_error() except that the arguments are supplied as a va_list. */ | |
288 | void | |
289 | ovs_error_valist(int err_no, const char *format, va_list args) | |
290 | { | |
bc9fb3a9 | 291 | const char *subprogram_name = get_subprogram_name(); |
c1c8308a BP |
292 | int save_errno = errno; |
293 | ||
781dee08 BP |
294 | if (subprogram_name[0]) { |
295 | fprintf(stderr, "%s(%s): ", program_name, subprogram_name); | |
296 | } else { | |
297 | fprintf(stderr, "%s: ", program_name); | |
298 | } | |
299 | ||
c1c8308a | 300 | vfprintf(stderr, format, args); |
0fec26b0 | 301 | if (err_no != 0) { |
c18ea70d | 302 | fprintf(stderr, " (%s)", ovs_retval_to_string(err_no)); |
0fec26b0 | 303 | } |
064af421 BP |
304 | putc('\n', stderr); |
305 | ||
306 | errno = save_errno; | |
307 | } | |
308 | ||
c18ea70d AE |
309 | /* Many OVS functions return an int which is one of: |
310 | * - 0: no error yet | |
311 | * - >0: errno value | |
312 | * - EOF: end of file (not necessarily an error; depends on the function called) | |
313 | * | |
314 | * Returns the appropriate human-readable string. The caller must copy the | |
315 | * string if it wants to hold onto it, as the storage may be overwritten on | |
316 | * subsequent function calls. | |
317 | */ | |
318 | const char * | |
319 | ovs_retval_to_string(int retval) | |
320 | { | |
5fcbed74 BP |
321 | return (!retval ? "" |
322 | : retval == EOF ? "End of file" | |
323 | : ovs_strerror(retval)); | |
324 | } | |
c18ea70d | 325 | |
5fcbed74 BP |
326 | const char * |
327 | ovs_strerror(int error) | |
328 | { | |
329 | enum { BUFSIZE = sizeof strerror_buffer_get()->s }; | |
330 | int save_errno; | |
331 | char *buffer; | |
332 | char *s; | |
333 | ||
334 | save_errno = errno; | |
335 | buffer = strerror_buffer_get()->s; | |
336 | ||
337 | #if STRERROR_R_CHAR_P | |
338 | /* GNU style strerror_r() might return an immutable static string, or it | |
339 | * might write and return 'buffer', but in either case we can pass the | |
340 | * returned string directly to the caller. */ | |
341 | s = strerror_r(error, buffer, BUFSIZE); | |
342 | #else /* strerror_r() returns an int. */ | |
343 | s = buffer; | |
344 | if (strerror_r(error, buffer, BUFSIZE)) { | |
345 | /* strerror_r() is only allowed to fail on ERANGE (because the buffer | |
346 | * is too short). We don't check the actual failure reason because | |
347 | * POSIX requires strerror_r() to return the error but old glibc | |
348 | * (before 2.13) returns -1 and sets errno. */ | |
5865a8af | 349 | snprintf(buffer, BUFSIZE, "Unknown error %d", error); |
c18ea70d | 350 | } |
5fcbed74 BP |
351 | #endif |
352 | ||
353 | errno = save_errno; | |
354 | ||
355 | return s; | |
c18ea70d AE |
356 | } |
357 | ||
55d5bb44 JP |
358 | /* Sets global "program_name" and "program_version" variables. Should |
359 | * be called at the beginning of main() with "argv[0]" as the argument | |
360 | * to 'argv0'. | |
361 | * | |
e385ef55 EJ |
362 | * 'version' should contain the version of the caller's program. If 'version' |
363 | * is the same as the VERSION #define, the caller is assumed to be part of Open | |
364 | * vSwitch. Otherwise, it is assumed to be an external program linking against | |
365 | * the Open vSwitch libraries. | |
366 | * | |
55d5bb44 JP |
367 | * The 'date' and 'time' arguments should likely be called with |
368 | * "__DATE__" and "__TIME__" to use the time the binary was built. | |
369 | * Alternatively, the "set_program_name" macro may be called to do this | |
370 | * automatically. | |
371 | */ | |
372 | void | |
e385ef55 EJ |
373 | set_program_name__(const char *argv0, const char *version, const char *date, |
374 | const char *time) | |
064af421 | 375 | { |
ed596d3a GS |
376 | #ifdef _WIN32 |
377 | char *basename; | |
378 | size_t max_len = strlen(argv0) + 1; | |
379 | basename = xmalloc(max_len); | |
380 | _splitpath_s(argv0, NULL, 0, NULL, 0, basename, max_len, NULL, 0); | |
381 | assert_single_threaded(); | |
382 | program_name = basename; | |
383 | #else | |
064af421 | 384 | const char *slash = strrchr(argv0, '/'); |
728a8b14 | 385 | assert_single_threaded(); |
064af421 | 386 | program_name = slash ? slash + 1 : argv0; |
ed596d3a | 387 | #endif |
55d5bb44 JP |
388 | |
389 | free(program_version); | |
e385ef55 EJ |
390 | |
391 | if (!strcmp(version, VERSION)) { | |
392 | program_version = xasprintf("%s (Open vSwitch) "VERSION"\n" | |
393 | "Compiled %s %s\n", | |
394 | program_name, date, time); | |
395 | } else { | |
396 | program_version = xasprintf("%s %s\n" | |
397 | "Open vSwitch Library "VERSION"\n" | |
398 | "Compiled %s %s\n", | |
399 | program_name, version, date, time); | |
400 | } | |
55d5bb44 JP |
401 | } |
402 | ||
bc9fb3a9 BP |
403 | /* Returns the name of the currently running thread or process. */ |
404 | const char * | |
405 | get_subprogram_name(void) | |
406 | { | |
407 | const char *name = subprogram_name_get(); | |
408 | return name ? name : ""; | |
409 | } | |
410 | ||
d710edc4 BP |
411 | /* Sets the formatted value of 'format' as the name of the currently running |
412 | * thread or process. (This appears in log messages and may also be visible in | |
413 | * system process listings and debuggers.) */ | |
bc9fb3a9 | 414 | void |
d710edc4 | 415 | set_subprogram_name(const char *format, ...) |
bc9fb3a9 | 416 | { |
d710edc4 BP |
417 | char *pname; |
418 | ||
419 | if (format) { | |
420 | va_list args; | |
421 | ||
422 | va_start(args, format); | |
423 | pname = xvasprintf(format, args); | |
424 | va_end(args); | |
425 | } else { | |
426 | pname = xstrdup(program_name); | |
427 | } | |
428 | ||
429 | free(subprogram_name_set(pname)); | |
430 | ||
8a8cd0ac | 431 | #if HAVE_GLIBC_PTHREAD_SETNAME_NP |
e584f6a8 | 432 | pthread_setname_np(pthread_self(), pname); |
8a8cd0ac | 433 | #elif HAVE_NETBSD_PTHREAD_SETNAME_NP |
e584f6a8 | 434 | pthread_setname_np(pthread_self(), "%s", pname); |
0f13e650 | 435 | #elif HAVE_PTHREAD_SET_NAME_NP |
e584f6a8 | 436 | pthread_set_name_np(pthread_self(), pname); |
0f13e650 | 437 | #endif |
bc9fb3a9 BP |
438 | } |
439 | ||
55d5bb44 JP |
440 | /* Returns a pointer to a string describing the program version. The |
441 | * caller must not modify or free the returned string. | |
b53055f4 | 442 | */ |
55d5bb44 | 443 | const char * |
c71c6043 | 444 | get_program_version(void) |
55d5bb44 JP |
445 | { |
446 | return program_version; | |
064af421 BP |
447 | } |
448 | ||
449 | /* Print the version information for the program. */ | |
450 | void | |
55d5bb44 | 451 | ovs_print_version(uint8_t min_ofp, uint8_t max_ofp) |
064af421 | 452 | { |
55d5bb44 | 453 | printf("%s", program_version); |
064af421 BP |
454 | if (min_ofp || max_ofp) { |
455 | printf("OpenFlow versions %#x:%#x\n", min_ofp, max_ofp); | |
456 | } | |
457 | } | |
458 | ||
459 | /* Writes the 'size' bytes in 'buf' to 'stream' as hex bytes arranged 16 per | |
460 | * line. Numeric offsets are also included, starting at 'ofs' for the first | |
461 | * byte in 'buf'. If 'ascii' is true then the corresponding ASCII characters | |
462 | * are also rendered alongside. */ | |
463 | void | |
464 | ovs_hex_dump(FILE *stream, const void *buf_, size_t size, | |
465 | uintptr_t ofs, bool ascii) | |
466 | { | |
467 | const uint8_t *buf = buf_; | |
468 | const size_t per_line = 16; /* Maximum bytes per line. */ | |
469 | ||
470 | while (size > 0) | |
471 | { | |
472 | size_t start, end, n; | |
473 | size_t i; | |
474 | ||
475 | /* Number of bytes on this line. */ | |
476 | start = ofs % per_line; | |
477 | end = per_line; | |
478 | if (end - start > size) | |
479 | end = start + size; | |
480 | n = end - start; | |
481 | ||
482 | /* Print line. */ | |
34582733 | 483 | fprintf(stream, "%08"PRIxMAX" ", (uintmax_t) ROUND_DOWN(ofs, per_line)); |
064af421 BP |
484 | for (i = 0; i < start; i++) |
485 | fprintf(stream, " "); | |
486 | for (; i < end; i++) | |
34582733 | 487 | fprintf(stream, "%02x%c", |
064af421 BP |
488 | buf[i - start], i == per_line / 2 - 1? '-' : ' '); |
489 | if (ascii) | |
490 | { | |
491 | for (; i < per_line; i++) | |
492 | fprintf(stream, " "); | |
493 | fprintf(stream, "|"); | |
494 | for (i = 0; i < start; i++) | |
495 | fprintf(stream, " "); | |
496 | for (; i < end; i++) { | |
497 | int c = buf[i - start]; | |
498 | putc(c >= 32 && c < 127 ? c : '.', stream); | |
499 | } | |
500 | for (; i < per_line; i++) | |
501 | fprintf(stream, " "); | |
502 | fprintf(stream, "|"); | |
503 | } | |
504 | fprintf(stream, "\n"); | |
505 | ||
506 | ofs += n; | |
507 | buf += n; | |
508 | size -= n; | |
509 | } | |
510 | } | |
511 | ||
512 | bool | |
513 | str_to_int(const char *s, int base, int *i) | |
514 | { | |
515 | long long ll; | |
516 | bool ok = str_to_llong(s, base, &ll); | |
517 | *i = ll; | |
518 | return ok; | |
519 | } | |
520 | ||
521 | bool | |
522 | str_to_long(const char *s, int base, long *li) | |
523 | { | |
524 | long long ll; | |
525 | bool ok = str_to_llong(s, base, &ll); | |
526 | *li = ll; | |
527 | return ok; | |
528 | } | |
529 | ||
530 | bool | |
531 | str_to_llong(const char *s, int base, long long *x) | |
532 | { | |
533 | int save_errno = errno; | |
534 | char *tail; | |
535 | errno = 0; | |
536 | *x = strtoll(s, &tail, base); | |
537 | if (errno == EINVAL || errno == ERANGE || tail == s || *tail != '\0') { | |
538 | errno = save_errno; | |
539 | *x = 0; | |
540 | return false; | |
541 | } else { | |
542 | errno = save_errno; | |
543 | return true; | |
544 | } | |
545 | } | |
546 | ||
f38b84ea BP |
547 | /* Converts floating-point string 's' into a double. If successful, stores |
548 | * the double in '*d' and returns true; on failure, stores 0 in '*d' and | |
549 | * returns false. | |
550 | * | |
551 | * Underflow (e.g. "1e-9999") is not considered an error, but overflow | |
552 | * (e.g. "1e9999)" is. */ | |
553 | bool | |
554 | str_to_double(const char *s, double *d) | |
555 | { | |
556 | int save_errno = errno; | |
557 | char *tail; | |
558 | errno = 0; | |
559 | *d = strtod(s, &tail); | |
560 | if (errno == EINVAL || (errno == ERANGE && *d != 0) | |
561 | || tail == s || *tail != '\0') { | |
562 | errno = save_errno; | |
563 | *d = 0; | |
564 | return false; | |
565 | } else { | |
566 | errno = save_errno; | |
567 | return true; | |
568 | } | |
569 | } | |
570 | ||
571 | /* Returns the value of 'c' as a hexadecimal digit. */ | |
572 | int | |
573 | hexit_value(int c) | |
574 | { | |
575 | switch (c) { | |
576 | case '0': case '1': case '2': case '3': case '4': | |
577 | case '5': case '6': case '7': case '8': case '9': | |
578 | return c - '0'; | |
579 | ||
580 | case 'a': case 'A': | |
581 | return 0xa; | |
582 | ||
583 | case 'b': case 'B': | |
584 | return 0xb; | |
585 | ||
586 | case 'c': case 'C': | |
587 | return 0xc; | |
588 | ||
589 | case 'd': case 'D': | |
590 | return 0xd; | |
591 | ||
592 | case 'e': case 'E': | |
593 | return 0xe; | |
594 | ||
595 | case 'f': case 'F': | |
596 | return 0xf; | |
f38b84ea | 597 | |
09246b99 BP |
598 | default: |
599 | return -1; | |
600 | } | |
f38b84ea | 601 | } |
29d4af60 | 602 | |
bf971267 BP |
603 | /* Returns the integer value of the 'n' hexadecimal digits starting at 's', or |
604 | * UINT_MAX if one of those "digits" is not really a hex digit. If 'ok' is | |
605 | * nonnull, '*ok' is set to true if the conversion succeeds or to false if a | |
606 | * non-hex digit is detected. */ | |
607 | unsigned int | |
608 | hexits_value(const char *s, size_t n, bool *ok) | |
609 | { | |
610 | unsigned int value; | |
611 | size_t i; | |
612 | ||
613 | value = 0; | |
614 | for (i = 0; i < n; i++) { | |
615 | int hexit = hexit_value(s[i]); | |
616 | if (hexit < 0) { | |
617 | if (ok) { | |
618 | *ok = false; | |
619 | } | |
620 | return UINT_MAX; | |
621 | } | |
622 | value = (value << 4) + hexit; | |
623 | } | |
624 | if (ok) { | |
625 | *ok = true; | |
626 | } | |
627 | return value; | |
628 | } | |
629 | ||
daf03c53 BP |
630 | /* Returns the current working directory as a malloc()'d string, or a null |
631 | * pointer if the current working directory cannot be determined. */ | |
632 | char * | |
633 | get_cwd(void) | |
634 | { | |
635 | long int path_max; | |
636 | size_t size; | |
637 | ||
638 | /* Get maximum path length or at least a reasonable estimate. */ | |
639 | path_max = pathconf(".", _PC_PATH_MAX); | |
640 | size = (path_max < 0 ? 1024 | |
641 | : path_max > 10240 ? 10240 | |
642 | : path_max); | |
643 | ||
644 | /* Get current working directory. */ | |
645 | for (;;) { | |
646 | char *buf = xmalloc(size); | |
647 | if (getcwd(buf, size)) { | |
648 | return xrealloc(buf, strlen(buf) + 1); | |
649 | } else { | |
650 | int error = errno; | |
651 | free(buf); | |
652 | if (error != ERANGE) { | |
10a89ef0 | 653 | VLOG_WARN("getcwd failed (%s)", ovs_strerror(error)); |
daf03c53 BP |
654 | return NULL; |
655 | } | |
656 | size *= 2; | |
657 | } | |
658 | } | |
659 | } | |
660 | ||
e1aff6f9 BP |
661 | static char * |
662 | all_slashes_name(const char *s) | |
663 | { | |
664 | return xstrdup(s[0] == '/' && s[1] == '/' && s[2] != '/' ? "//" | |
665 | : s[0] == '/' ? "/" | |
666 | : "."); | |
667 | } | |
668 | ||
29d4af60 BP |
669 | /* Returns the directory name portion of 'file_name' as a malloc()'d string, |
670 | * similar to the POSIX dirname() function but thread-safe. */ | |
671 | char * | |
672 | dir_name(const char *file_name) | |
673 | { | |
674 | size_t len = strlen(file_name); | |
675 | while (len > 0 && file_name[len - 1] == '/') { | |
676 | len--; | |
677 | } | |
678 | while (len > 0 && file_name[len - 1] != '/') { | |
679 | len--; | |
680 | } | |
681 | while (len > 0 && file_name[len - 1] == '/') { | |
682 | len--; | |
683 | } | |
e1aff6f9 BP |
684 | return len ? xmemdup0(file_name, len) : all_slashes_name(file_name); |
685 | } | |
686 | ||
687 | /* Returns the file name portion of 'file_name' as a malloc()'d string, | |
688 | * similar to the POSIX basename() function but thread-safe. */ | |
689 | char * | |
690 | base_name(const char *file_name) | |
691 | { | |
692 | size_t end, start; | |
693 | ||
694 | end = strlen(file_name); | |
695 | while (end > 0 && file_name[end - 1] == '/') { | |
696 | end--; | |
697 | } | |
698 | ||
699 | if (!end) { | |
700 | return all_slashes_name(file_name); | |
29d4af60 | 701 | } |
e1aff6f9 BP |
702 | |
703 | start = end; | |
704 | while (start > 0 && file_name[start - 1] != '/') { | |
705 | start--; | |
706 | } | |
707 | ||
708 | return xmemdup0(file_name + start, end - start); | |
29d4af60 | 709 | } |
18b9283b | 710 | |
daf03c53 BP |
711 | /* If 'file_name' starts with '/', returns a copy of 'file_name'. Otherwise, |
712 | * returns an absolute path to 'file_name' considering it relative to 'dir', | |
713 | * which itself must be absolute. 'dir' may be null or the empty string, in | |
714 | * which case the current working directory is used. | |
715 | * | |
716 | * Returns a null pointer if 'dir' is null and getcwd() fails. */ | |
717 | char * | |
718 | abs_file_name(const char *dir, const char *file_name) | |
719 | { | |
720 | if (file_name[0] == '/') { | |
721 | return xstrdup(file_name); | |
722 | } else if (dir && dir[0]) { | |
723 | char *separator = dir[strlen(dir) - 1] == '/' ? "" : "/"; | |
724 | return xasprintf("%s%s%s", dir, separator, file_name); | |
725 | } else { | |
726 | char *cwd = get_cwd(); | |
727 | if (cwd) { | |
728 | char *abs_name = xasprintf("%s/%s", cwd, file_name); | |
729 | free(cwd); | |
730 | return abs_name; | |
731 | } else { | |
732 | return NULL; | |
733 | } | |
734 | } | |
735 | } | |
736 | ||
fee0c963 BP |
737 | /* Like readlink(), but returns the link name as a null-terminated string in |
738 | * allocated memory that the caller must eventually free (with free()). | |
739 | * Returns NULL on error, in which case errno is set appropriately. */ | |
740 | char * | |
741 | xreadlink(const char *filename) | |
742 | { | |
743 | size_t size; | |
744 | ||
745 | for (size = 64; ; size *= 2) { | |
746 | char *buf = xmalloc(size); | |
747 | ssize_t retval = readlink(filename, buf, size); | |
748 | int error = errno; | |
749 | ||
750 | if (retval >= 0 && retval < size) { | |
751 | buf[retval] = '\0'; | |
752 | return buf; | |
753 | } | |
754 | ||
755 | free(buf); | |
756 | if (retval < 0) { | |
757 | errno = error; | |
758 | return NULL; | |
759 | } | |
760 | } | |
761 | } | |
762 | ||
763 | /* Returns a version of 'filename' with symlinks in the final component | |
764 | * dereferenced. This differs from realpath() in that: | |
765 | * | |
766 | * - 'filename' need not exist. | |
767 | * | |
768 | * - If 'filename' does exist as a symlink, its referent need not exist. | |
769 | * | |
770 | * - Only symlinks in the final component of 'filename' are dereferenced. | |
771 | * | |
772 | * The caller must eventually free the returned string (with free()). */ | |
773 | char * | |
774 | follow_symlinks(const char *filename) | |
775 | { | |
776 | struct stat s; | |
777 | char *fn; | |
778 | int i; | |
779 | ||
780 | fn = xstrdup(filename); | |
781 | for (i = 0; i < 10; i++) { | |
782 | char *linkname; | |
783 | char *next_fn; | |
784 | ||
785 | if (lstat(fn, &s) != 0 || !S_ISLNK(s.st_mode)) { | |
786 | return fn; | |
787 | } | |
788 | ||
789 | linkname = xreadlink(fn); | |
790 | if (!linkname) { | |
10a89ef0 BP |
791 | VLOG_WARN("%s: readlink failed (%s)", |
792 | filename, ovs_strerror(errno)); | |
fee0c963 BP |
793 | return fn; |
794 | } | |
795 | ||
796 | if (linkname[0] == '/') { | |
797 | /* Target of symlink is absolute so use it raw. */ | |
798 | next_fn = linkname; | |
799 | } else { | |
800 | /* Target of symlink is relative so add to 'fn''s directory. */ | |
801 | char *dir = dir_name(fn); | |
802 | ||
803 | if (!strcmp(dir, ".")) { | |
804 | next_fn = linkname; | |
805 | } else { | |
806 | char *separator = dir[strlen(dir) - 1] == '/' ? "" : "/"; | |
807 | next_fn = xasprintf("%s%s%s", dir, separator, linkname); | |
808 | free(linkname); | |
809 | } | |
810 | ||
811 | free(dir); | |
812 | } | |
813 | ||
814 | free(fn); | |
815 | fn = next_fn; | |
816 | } | |
817 | ||
818 | VLOG_WARN("%s: too many levels of symlinks", filename); | |
819 | free(fn); | |
820 | return xstrdup(filename); | |
821 | } | |
daf03c53 | 822 | |
18b9283b | 823 | /* Pass a value to this function if it is marked with |
d295e8e9 JP |
824 | * __attribute__((warn_unused_result)) and you genuinely want to ignore |
825 | * its return value. (Note that every scalar type can be implicitly | |
18b9283b | 826 | * converted to bool.) */ |
c69ee87c | 827 | void ignore(bool x OVS_UNUSED) { } |
44b4d050 BP |
828 | |
829 | /* Returns an appropriate delimiter for inserting just before the 0-based item | |
830 | * 'index' in a list that has 'total' items in it. */ | |
831 | const char * | |
832 | english_list_delimiter(size_t index, size_t total) | |
833 | { | |
834 | return (index == 0 ? "" | |
835 | : index < total - 1 ? ", " | |
836 | : total > 2 ? ", and " | |
837 | : " and "); | |
838 | } | |
711e0157 | 839 | |
0ee140fb | 840 | /* Returns the number of trailing 0-bits in 'n'. Undefined if 'n' == 0. */ |
d43d314e | 841 | #if __GNUC__ >= 4 |
0ee140fb | 842 | /* Defined inline in util.h. */ |
aad29cd1 | 843 | #else |
8c947903 | 844 | /* Returns the number of trailing 0-bits in 'n'. Undefined if 'n' == 0. */ |
481da12c | 845 | int |
d43d314e | 846 | raw_ctz(uint64_t n) |
0ee140fb | 847 | { |
d43d314e BP |
848 | uint64_t k; |
849 | int count = 63; | |
aad29cd1 BP |
850 | |
851 | #define CTZ_STEP(X) \ | |
0ee140fb BP |
852 | k = n << (X); \ |
853 | if (k) { \ | |
854 | count -= X; \ | |
855 | n = k; \ | |
856 | } | |
d43d314e | 857 | CTZ_STEP(32); |
0ee140fb BP |
858 | CTZ_STEP(16); |
859 | CTZ_STEP(8); | |
860 | CTZ_STEP(4); | |
861 | CTZ_STEP(2); | |
862 | CTZ_STEP(1); | |
aad29cd1 BP |
863 | #undef CTZ_STEP |
864 | ||
0ee140fb | 865 | return count; |
aad29cd1 | 866 | } |
8c947903 JR |
867 | |
868 | /* Returns the number of leading 0-bits in 'n'. Undefined if 'n' == 0. */ | |
869 | int | |
870 | raw_clz64(uint64_t n) | |
871 | { | |
872 | uint64_t k; | |
873 | int count = 63; | |
874 | ||
875 | #define CLZ_STEP(X) \ | |
876 | k = n >> (X); \ | |
877 | if (k) { \ | |
878 | count -= X; \ | |
879 | n = k; \ | |
880 | } | |
881 | CLZ_STEP(32); | |
882 | CLZ_STEP(16); | |
883 | CLZ_STEP(8); | |
884 | CLZ_STEP(4); | |
885 | CLZ_STEP(2); | |
886 | CLZ_STEP(1); | |
887 | #undef CLZ_STEP | |
888 | ||
889 | return count; | |
890 | } | |
0ee140fb | 891 | #endif |
75a75043 | 892 | |
381657b3 | 893 | #if NEED_COUNT_1BITS_8 |
a656cb77 BP |
894 | #define INIT1(X) \ |
895 | ((((X) & (1 << 0)) != 0) + \ | |
896 | (((X) & (1 << 1)) != 0) + \ | |
897 | (((X) & (1 << 2)) != 0) + \ | |
898 | (((X) & (1 << 3)) != 0) + \ | |
899 | (((X) & (1 << 4)) != 0) + \ | |
900 | (((X) & (1 << 5)) != 0) + \ | |
901 | (((X) & (1 << 6)) != 0) + \ | |
902 | (((X) & (1 << 7)) != 0)) | |
903 | #define INIT2(X) INIT1(X), INIT1((X) + 1) | |
904 | #define INIT4(X) INIT2(X), INIT2((X) + 2) | |
905 | #define INIT8(X) INIT4(X), INIT4((X) + 4) | |
906 | #define INIT16(X) INIT8(X), INIT8((X) + 8) | |
907 | #define INIT32(X) INIT16(X), INIT16((X) + 16) | |
908 | #define INIT64(X) INIT32(X), INIT32((X) + 32) | |
909 | ||
c3cc4d2d JR |
910 | const uint8_t count_1bits_8[256] = { |
911 | INIT64(0), INIT64(64), INIT64(128), INIT64(192) | |
912 | }; | |
913 | #endif | |
2a4ca27c | 914 | |
75a75043 BP |
915 | /* Returns true if the 'n' bytes starting at 'p' are zeros. */ |
916 | bool | |
917 | is_all_zeros(const uint8_t *p, size_t n) | |
918 | { | |
919 | size_t i; | |
920 | ||
921 | for (i = 0; i < n; i++) { | |
922 | if (p[i] != 0x00) { | |
923 | return false; | |
924 | } | |
925 | } | |
926 | return true; | |
927 | } | |
928 | ||
929 | /* Returns true if the 'n' bytes starting at 'p' are 0xff. */ | |
930 | bool | |
931 | is_all_ones(const uint8_t *p, size_t n) | |
932 | { | |
933 | size_t i; | |
934 | ||
935 | for (i = 0; i < n; i++) { | |
936 | if (p[i] != 0xff) { | |
937 | return false; | |
938 | } | |
939 | } | |
940 | return true; | |
941 | } | |
942 | ||
ddc4f8e2 BP |
943 | /* Copies 'n_bits' bits starting from bit 'src_ofs' in 'src' to the 'n_bits' |
944 | * starting from bit 'dst_ofs' in 'dst'. 'src' is 'src_len' bytes long and | |
945 | * 'dst' is 'dst_len' bytes long. | |
946 | * | |
947 | * If you consider all of 'src' to be a single unsigned integer in network byte | |
948 | * order, then bit N is the bit with value 2**N. That is, bit 0 is the bit | |
949 | * with value 1 in src[src_len - 1], bit 1 is the bit with value 2, bit 2 is | |
950 | * the bit with value 4, ..., bit 8 is the bit with value 1 in src[src_len - | |
951 | * 2], and so on. Similarly for 'dst'. | |
952 | * | |
953 | * Required invariants: | |
954 | * src_ofs + n_bits <= src_len * 8 | |
955 | * dst_ofs + n_bits <= dst_len * 8 | |
956 | * 'src' and 'dst' must not overlap. | |
957 | */ | |
958 | void | |
959 | bitwise_copy(const void *src_, unsigned int src_len, unsigned int src_ofs, | |
960 | void *dst_, unsigned int dst_len, unsigned int dst_ofs, | |
961 | unsigned int n_bits) | |
962 | { | |
963 | const uint8_t *src = src_; | |
964 | uint8_t *dst = dst_; | |
965 | ||
966 | src += src_len - (src_ofs / 8 + 1); | |
967 | src_ofs %= 8; | |
968 | ||
969 | dst += dst_len - (dst_ofs / 8 + 1); | |
970 | dst_ofs %= 8; | |
971 | ||
972 | if (src_ofs == 0 && dst_ofs == 0) { | |
973 | unsigned int n_bytes = n_bits / 8; | |
974 | if (n_bytes) { | |
975 | dst -= n_bytes - 1; | |
976 | src -= n_bytes - 1; | |
977 | memcpy(dst, src, n_bytes); | |
978 | ||
979 | n_bits %= 8; | |
980 | src--; | |
981 | dst--; | |
982 | } | |
983 | if (n_bits) { | |
984 | uint8_t mask = (1 << n_bits) - 1; | |
985 | *dst = (*dst & ~mask) | (*src & mask); | |
986 | } | |
987 | } else { | |
988 | while (n_bits > 0) { | |
989 | unsigned int max_copy = 8 - MAX(src_ofs, dst_ofs); | |
990 | unsigned int chunk = MIN(n_bits, max_copy); | |
991 | uint8_t mask = ((1 << chunk) - 1) << dst_ofs; | |
992 | ||
993 | *dst &= ~mask; | |
994 | *dst |= ((*src >> src_ofs) << dst_ofs) & mask; | |
995 | ||
996 | src_ofs += chunk; | |
997 | if (src_ofs == 8) { | |
998 | src--; | |
999 | src_ofs = 0; | |
1000 | } | |
1001 | dst_ofs += chunk; | |
1002 | if (dst_ofs == 8) { | |
1003 | dst--; | |
1004 | dst_ofs = 0; | |
1005 | } | |
1006 | n_bits -= chunk; | |
1007 | } | |
1008 | } | |
1009 | } | |
1010 | ||
6cc7ea5e BP |
1011 | /* Zeros the 'n_bits' bits starting from bit 'dst_ofs' in 'dst'. 'dst' is |
1012 | * 'dst_len' bytes long. | |
1013 | * | |
1014 | * If you consider all of 'dst' to be a single unsigned integer in network byte | |
1015 | * order, then bit N is the bit with value 2**N. That is, bit 0 is the bit | |
1016 | * with value 1 in dst[dst_len - 1], bit 1 is the bit with value 2, bit 2 is | |
1017 | * the bit with value 4, ..., bit 8 is the bit with value 1 in dst[dst_len - | |
1018 | * 2], and so on. | |
1019 | * | |
1020 | * Required invariant: | |
1021 | * dst_ofs + n_bits <= dst_len * 8 | |
1022 | */ | |
1023 | void | |
1024 | bitwise_zero(void *dst_, unsigned int dst_len, unsigned dst_ofs, | |
1025 | unsigned int n_bits) | |
1026 | { | |
1027 | uint8_t *dst = dst_; | |
1028 | ||
1029 | if (!n_bits) { | |
1030 | return; | |
1031 | } | |
1032 | ||
1033 | dst += dst_len - (dst_ofs / 8 + 1); | |
1034 | dst_ofs %= 8; | |
1035 | ||
1036 | if (dst_ofs) { | |
1037 | unsigned int chunk = MIN(n_bits, 8 - dst_ofs); | |
1038 | ||
1039 | *dst &= ~(((1 << chunk) - 1) << dst_ofs); | |
1040 | ||
1041 | n_bits -= chunk; | |
1042 | if (!n_bits) { | |
1043 | return; | |
1044 | } | |
1045 | ||
1046 | dst--; | |
1047 | } | |
1048 | ||
1049 | while (n_bits >= 8) { | |
1050 | *dst-- = 0; | |
1051 | n_bits -= 8; | |
1052 | } | |
1053 | ||
1054 | if (n_bits) { | |
1055 | *dst &= ~((1 << n_bits) - 1); | |
1056 | } | |
1057 | } | |
1058 | ||
c2dd4932 BP |
1059 | /* Sets to 1 all of the 'n_bits' bits starting from bit 'dst_ofs' in 'dst'. |
1060 | * 'dst' is 'dst_len' bytes long. | |
1061 | * | |
1062 | * If you consider all of 'dst' to be a single unsigned integer in network byte | |
1063 | * order, then bit N is the bit with value 2**N. That is, bit 0 is the bit | |
1064 | * with value 1 in dst[dst_len - 1], bit 1 is the bit with value 2, bit 2 is | |
1065 | * the bit with value 4, ..., bit 8 is the bit with value 1 in dst[dst_len - | |
1066 | * 2], and so on. | |
1067 | * | |
1068 | * Required invariant: | |
1069 | * dst_ofs + n_bits <= dst_len * 8 | |
1070 | */ | |
1071 | void | |
1072 | bitwise_one(void *dst_, unsigned int dst_len, unsigned dst_ofs, | |
1073 | unsigned int n_bits) | |
1074 | { | |
1075 | uint8_t *dst = dst_; | |
1076 | ||
1077 | if (!n_bits) { | |
1078 | return; | |
1079 | } | |
1080 | ||
1081 | dst += dst_len - (dst_ofs / 8 + 1); | |
1082 | dst_ofs %= 8; | |
1083 | ||
1084 | if (dst_ofs) { | |
1085 | unsigned int chunk = MIN(n_bits, 8 - dst_ofs); | |
1086 | ||
1087 | *dst |= ((1 << chunk) - 1) << dst_ofs; | |
1088 | ||
1089 | n_bits -= chunk; | |
1090 | if (!n_bits) { | |
1091 | return; | |
1092 | } | |
1093 | ||
1094 | dst--; | |
1095 | } | |
1096 | ||
1097 | while (n_bits >= 8) { | |
1098 | *dst-- = 0xff; | |
1099 | n_bits -= 8; | |
1100 | } | |
1101 | ||
1102 | if (n_bits) { | |
1103 | *dst |= (1 << n_bits) - 1; | |
1104 | } | |
1105 | } | |
1106 | ||
79a010aa BP |
1107 | /* Scans the 'n_bits' bits starting from bit 'dst_ofs' in 'dst' for 1-bits. |
1108 | * Returns false if any 1-bits are found, otherwise true. 'dst' is 'dst_len' | |
1109 | * bytes long. | |
1110 | * | |
1111 | * If you consider all of 'dst' to be a single unsigned integer in network byte | |
1112 | * order, then bit N is the bit with value 2**N. That is, bit 0 is the bit | |
1113 | * with value 1 in dst[dst_len - 1], bit 1 is the bit with value 2, bit 2 is | |
1114 | * the bit with value 4, ..., bit 8 is the bit with value 1 in dst[dst_len - | |
1115 | * 2], and so on. | |
1116 | * | |
1117 | * Required invariant: | |
1118 | * dst_ofs + n_bits <= dst_len * 8 | |
1119 | */ | |
1120 | bool | |
1121 | bitwise_is_all_zeros(const void *p_, unsigned int len, unsigned int ofs, | |
1122 | unsigned int n_bits) | |
1123 | { | |
1124 | const uint8_t *p = p_; | |
1125 | ||
1126 | if (!n_bits) { | |
1127 | return true; | |
1128 | } | |
1129 | ||
1130 | p += len - (ofs / 8 + 1); | |
1131 | ofs %= 8; | |
1132 | ||
1133 | if (ofs) { | |
1134 | unsigned int chunk = MIN(n_bits, 8 - ofs); | |
1135 | ||
1136 | if (*p & (((1 << chunk) - 1) << ofs)) { | |
1137 | return false; | |
1138 | } | |
1139 | ||
1140 | n_bits -= chunk; | |
1141 | if (!n_bits) { | |
1142 | return true; | |
1143 | } | |
1144 | ||
1145 | p--; | |
1146 | } | |
1147 | ||
1148 | while (n_bits >= 8) { | |
1149 | if (*p) { | |
1150 | return false; | |
1151 | } | |
1152 | n_bits -= 8; | |
1153 | p--; | |
1154 | } | |
1155 | ||
1156 | if (n_bits && *p & ((1 << n_bits) - 1)) { | |
1157 | return false; | |
1158 | } | |
1159 | ||
1160 | return true; | |
1161 | } | |
1162 | ||
ddc4f8e2 BP |
1163 | /* Copies the 'n_bits' low-order bits of 'value' into the 'n_bits' bits |
1164 | * starting at bit 'dst_ofs' in 'dst', which is 'dst_len' bytes long. | |
1165 | * | |
1166 | * If you consider all of 'dst' to be a single unsigned integer in network byte | |
1167 | * order, then bit N is the bit with value 2**N. That is, bit 0 is the bit | |
1168 | * with value 1 in dst[dst_len - 1], bit 1 is the bit with value 2, bit 2 is | |
1169 | * the bit with value 4, ..., bit 8 is the bit with value 1 in dst[dst_len - | |
1170 | * 2], and so on. | |
1171 | * | |
1172 | * Required invariants: | |
1173 | * dst_ofs + n_bits <= dst_len * 8 | |
1174 | * n_bits <= 64 | |
1175 | */ | |
1176 | void | |
1177 | bitwise_put(uint64_t value, | |
1178 | void *dst, unsigned int dst_len, unsigned int dst_ofs, | |
1179 | unsigned int n_bits) | |
1180 | { | |
1181 | ovs_be64 n_value = htonll(value); | |
1182 | bitwise_copy(&n_value, sizeof n_value, 0, | |
1183 | dst, dst_len, dst_ofs, | |
1184 | n_bits); | |
1185 | } | |
1186 | ||
1187 | /* Returns the value of the 'n_bits' bits starting at bit 'src_ofs' in 'src', | |
1188 | * which is 'src_len' bytes long. | |
1189 | * | |
1190 | * If you consider all of 'src' to be a single unsigned integer in network byte | |
1191 | * order, then bit N is the bit with value 2**N. That is, bit 0 is the bit | |
1192 | * with value 1 in src[src_len - 1], bit 1 is the bit with value 2, bit 2 is | |
1193 | * the bit with value 4, ..., bit 8 is the bit with value 1 in src[src_len - | |
1194 | * 2], and so on. | |
1195 | * | |
1196 | * Required invariants: | |
1197 | * src_ofs + n_bits <= src_len * 8 | |
1198 | * n_bits <= 64 | |
1199 | */ | |
1200 | uint64_t | |
1201 | bitwise_get(const void *src, unsigned int src_len, | |
1202 | unsigned int src_ofs, unsigned int n_bits) | |
1203 | { | |
1204 | ovs_be64 value = htonll(0); | |
1205 | ||
1206 | bitwise_copy(src, src_len, src_ofs, | |
1207 | &value, sizeof value, 0, | |
1208 | n_bits); | |
1209 | return ntohll(value); | |
1210 | } | |
ed2232fc BP |
1211 | \f |
1212 | /* ovs_scan */ | |
1213 | ||
1214 | struct scan_spec { | |
1215 | unsigned int width; | |
1216 | enum { | |
1217 | SCAN_DISCARD, | |
1218 | SCAN_CHAR, | |
1219 | SCAN_SHORT, | |
1220 | SCAN_INT, | |
1221 | SCAN_LONG, | |
1222 | SCAN_LLONG, | |
1223 | SCAN_INTMAX_T, | |
1224 | SCAN_PTRDIFF_T, | |
1225 | SCAN_SIZE_T | |
1226 | } type; | |
1227 | }; | |
1228 | ||
1229 | static const char * | |
1230 | skip_spaces(const char *s) | |
1231 | { | |
1232 | while (isspace((unsigned char) *s)) { | |
1233 | s++; | |
1234 | } | |
1235 | return s; | |
1236 | } | |
1237 | ||
1238 | static const char * | |
1239 | scan_int(const char *s, const struct scan_spec *spec, int base, va_list *args) | |
1240 | { | |
1241 | const char *start = s; | |
1242 | uintmax_t value; | |
1243 | bool negative; | |
1244 | int n_digits; | |
1245 | ||
1246 | negative = *s == '-'; | |
1247 | s += *s == '-' || *s == '+'; | |
1248 | ||
1249 | if ((!base || base == 16) && *s == '0' && (s[1] == 'x' || s[1] == 'X')) { | |
1250 | base = 16; | |
1251 | s += 2; | |
1252 | } else if (!base) { | |
1253 | base = *s == '0' ? 8 : 10; | |
1254 | } | |
1255 | ||
1256 | if (s - start >= spec->width) { | |
1257 | return NULL; | |
1258 | } | |
1259 | ||
1260 | value = 0; | |
1261 | n_digits = 0; | |
1262 | while (s - start < spec->width) { | |
1263 | int digit = hexit_value(*s); | |
1264 | ||
1265 | if (digit < 0 || digit >= base) { | |
1266 | break; | |
1267 | } | |
1268 | value = value * base + digit; | |
1269 | n_digits++; | |
1270 | s++; | |
1271 | } | |
1272 | if (!n_digits) { | |
1273 | return NULL; | |
1274 | } | |
1275 | ||
1276 | if (negative) { | |
1277 | value = -value; | |
1278 | } | |
1279 | ||
1280 | switch (spec->type) { | |
1281 | case SCAN_DISCARD: | |
1282 | break; | |
1283 | case SCAN_CHAR: | |
1284 | *va_arg(*args, char *) = value; | |
1285 | break; | |
1286 | case SCAN_SHORT: | |
1287 | *va_arg(*args, short int *) = value; | |
1288 | break; | |
1289 | case SCAN_INT: | |
1290 | *va_arg(*args, int *) = value; | |
1291 | break; | |
1292 | case SCAN_LONG: | |
1293 | *va_arg(*args, long int *) = value; | |
1294 | break; | |
1295 | case SCAN_LLONG: | |
1296 | *va_arg(*args, long long int *) = value; | |
1297 | break; | |
1298 | case SCAN_INTMAX_T: | |
1299 | *va_arg(*args, intmax_t *) = value; | |
1300 | break; | |
1301 | case SCAN_PTRDIFF_T: | |
1302 | *va_arg(*args, ptrdiff_t *) = value; | |
1303 | break; | |
1304 | case SCAN_SIZE_T: | |
1305 | *va_arg(*args, size_t *) = value; | |
1306 | break; | |
1307 | } | |
1308 | return s; | |
1309 | } | |
1310 | ||
1311 | static const char * | |
1312 | skip_digits(const char *s) | |
1313 | { | |
1314 | while (*s >= '0' && *s <= '9') { | |
1315 | s++; | |
1316 | } | |
1317 | return s; | |
1318 | } | |
1319 | ||
1320 | static const char * | |
1321 | scan_float(const char *s, const struct scan_spec *spec, va_list *args) | |
1322 | { | |
1323 | const char *start = s; | |
1324 | long double value; | |
1325 | char *tail; | |
1326 | char *copy; | |
1327 | bool ok; | |
1328 | ||
1329 | s += *s == '+' || *s == '-'; | |
1330 | s = skip_digits(s); | |
1331 | if (*s == '.') { | |
1332 | s = skip_digits(s + 1); | |
1333 | } | |
1334 | if (*s == 'e' || *s == 'E') { | |
1335 | s++; | |
1336 | s += *s == '+' || *s == '-'; | |
1337 | s = skip_digits(s); | |
1338 | } | |
1339 | ||
1340 | if (s - start > spec->width) { | |
1341 | s = start + spec->width; | |
1342 | } | |
1343 | ||
1344 | copy = xmemdup0(start, s - start); | |
1345 | value = strtold(copy, &tail); | |
1346 | ok = *tail == '\0'; | |
1347 | free(copy); | |
1348 | if (!ok) { | |
1349 | return NULL; | |
1350 | } | |
1351 | ||
1352 | switch (spec->type) { | |
1353 | case SCAN_DISCARD: | |
1354 | break; | |
1355 | case SCAN_INT: | |
1356 | *va_arg(*args, float *) = value; | |
1357 | break; | |
1358 | case SCAN_LONG: | |
1359 | *va_arg(*args, double *) = value; | |
1360 | break; | |
1361 | case SCAN_LLONG: | |
1362 | *va_arg(*args, long double *) = value; | |
1363 | break; | |
1364 | ||
1365 | case SCAN_CHAR: | |
1366 | case SCAN_SHORT: | |
1367 | case SCAN_INTMAX_T: | |
1368 | case SCAN_PTRDIFF_T: | |
1369 | case SCAN_SIZE_T: | |
428b2edd | 1370 | OVS_NOT_REACHED(); |
ed2232fc BP |
1371 | } |
1372 | return s; | |
1373 | } | |
1374 | ||
1375 | static void | |
1376 | scan_output_string(const struct scan_spec *spec, | |
1377 | const char *s, size_t n, | |
1378 | va_list *args) | |
1379 | { | |
1380 | if (spec->type != SCAN_DISCARD) { | |
1381 | char *out = va_arg(*args, char *); | |
1382 | memcpy(out, s, n); | |
1383 | out[n] = '\0'; | |
1384 | } | |
1385 | } | |
1386 | ||
1387 | static const char * | |
1388 | scan_string(const char *s, const struct scan_spec *spec, va_list *args) | |
1389 | { | |
1390 | size_t n; | |
1391 | ||
1392 | for (n = 0; n < spec->width; n++) { | |
1393 | if (!s[n] || isspace((unsigned char) s[n])) { | |
1394 | break; | |
1395 | } | |
1396 | } | |
1397 | if (!n) { | |
1398 | return NULL; | |
1399 | } | |
1400 | ||
1401 | scan_output_string(spec, s, n, args); | |
1402 | return s + n; | |
1403 | } | |
1404 | ||
1405 | static const char * | |
1406 | parse_scanset(const char *p_, unsigned long *set, bool *complemented) | |
1407 | { | |
1408 | const uint8_t *p = (const uint8_t *) p_; | |
1409 | ||
1410 | *complemented = *p == '^'; | |
1411 | p += *complemented; | |
1412 | ||
1413 | if (*p == ']') { | |
1414 | bitmap_set1(set, ']'); | |
1415 | p++; | |
1416 | } | |
1417 | ||
1418 | while (*p && *p != ']') { | |
1419 | if (p[1] == '-' && p[2] != ']' && p[2] > *p) { | |
1420 | bitmap_set_multiple(set, *p, p[2] - *p + 1, true); | |
1421 | p += 3; | |
1422 | } else { | |
1423 | bitmap_set1(set, *p++); | |
1424 | } | |
1425 | } | |
1426 | if (*p == ']') { | |
1427 | p++; | |
1428 | } | |
1429 | return (const char *) p; | |
1430 | } | |
1431 | ||
1432 | static const char * | |
1433 | scan_set(const char *s, const struct scan_spec *spec, const char **pp, | |
1434 | va_list *args) | |
1435 | { | |
1436 | unsigned long set[BITMAP_N_LONGS(UCHAR_MAX + 1)]; | |
1437 | bool complemented; | |
1438 | unsigned int n; | |
1439 | ||
1440 | /* Parse the scan set. */ | |
1441 | memset(set, 0, sizeof set); | |
1442 | *pp = parse_scanset(*pp, set, &complemented); | |
1443 | ||
1444 | /* Parse the data. */ | |
1445 | n = 0; | |
1446 | while (s[n] | |
1447 | && bitmap_is_set(set, (unsigned char) s[n]) == !complemented | |
1448 | && n < spec->width) { | |
1449 | n++; | |
1450 | } | |
1451 | if (!n) { | |
1452 | return NULL; | |
1453 | } | |
1454 | scan_output_string(spec, s, n, args); | |
1455 | return s + n; | |
1456 | } | |
1457 | ||
1458 | static const char * | |
1459 | scan_chars(const char *s, const struct scan_spec *spec, va_list *args) | |
1460 | { | |
31499678 | 1461 | unsigned int n = spec->width == UINT_MAX ? 1 : spec->width; |
ed2232fc BP |
1462 | |
1463 | if (strlen(s) < n) { | |
1464 | return NULL; | |
1465 | } | |
1466 | if (spec->type != SCAN_DISCARD) { | |
1467 | memcpy(va_arg(*args, char *), s, n); | |
1468 | } | |
1469 | return s + n; | |
1470 | } | |
1471 | ||
1472 | /* This is an implementation of the standard sscanf() function, with the | |
1473 | * following exceptions: | |
1474 | * | |
037821cf | 1475 | * - It returns true if the entire format was successfully scanned and |
ed2232fc BP |
1476 | * converted, false if any conversion failed. |
1477 | * | |
1478 | * - The standard doesn't define sscanf() behavior when an out-of-range value | |
1479 | * is scanned, e.g. if a "%"PRIi8 conversion scans "-1" or "0x1ff". Some | |
1480 | * implementations consider this an error and stop scanning. This | |
1481 | * implementation never considers an out-of-range value an error; instead, | |
1482 | * it stores the least-significant bits of the converted value in the | |
1483 | * destination, e.g. the value 255 for both examples earlier. | |
1484 | * | |
1485 | * - Only single-byte characters are supported, that is, the 'l' modifier | |
1486 | * on %s, %[, and %c is not supported. The GNU extension 'a' modifier is | |
1487 | * also not supported. | |
1488 | * | |
1489 | * - %p is not supported. | |
1490 | */ | |
1491 | bool | |
037821cf | 1492 | ovs_scan(const char *s, const char *format, ...) |
ed2232fc BP |
1493 | { |
1494 | const char *const start = s; | |
1495 | bool ok = false; | |
1496 | const char *p; | |
1497 | va_list args; | |
1498 | ||
037821cf HL |
1499 | va_start(args, format); |
1500 | p = format; | |
ed2232fc BP |
1501 | while (*p != '\0') { |
1502 | struct scan_spec spec; | |
1503 | unsigned char c = *p++; | |
1504 | bool discard; | |
1505 | ||
1506 | if (isspace(c)) { | |
1507 | s = skip_spaces(s); | |
1508 | continue; | |
1509 | } else if (c != '%') { | |
1510 | if (*s != c) { | |
1511 | goto exit; | |
1512 | } | |
1513 | s++; | |
1514 | continue; | |
1515 | } else if (*p == '%') { | |
1516 | if (*s++ != '%') { | |
1517 | goto exit; | |
1518 | } | |
1519 | p++; | |
1520 | continue; | |
1521 | } | |
1522 | ||
1523 | /* Parse '*' flag. */ | |
1524 | discard = *p == '*'; | |
1525 | p += discard; | |
1526 | ||
1527 | /* Parse field width. */ | |
1528 | spec.width = 0; | |
1529 | while (*p >= '0' && *p <= '9') { | |
1530 | spec.width = spec.width * 10 + (*p++ - '0'); | |
1531 | } | |
1532 | if (spec.width == 0) { | |
1533 | spec.width = UINT_MAX; | |
1534 | } | |
1535 | ||
1536 | /* Parse type modifier. */ | |
1537 | switch (*p) { | |
1538 | case 'h': | |
1539 | if (p[1] == 'h') { | |
1540 | spec.type = SCAN_CHAR; | |
1541 | p += 2; | |
1542 | } else { | |
1543 | spec.type = SCAN_SHORT; | |
1544 | p++; | |
1545 | } | |
1546 | break; | |
1547 | ||
1548 | case 'j': | |
1549 | spec.type = SCAN_INTMAX_T; | |
1550 | p++; | |
1551 | break; | |
1552 | ||
1553 | case 'l': | |
1554 | if (p[1] == 'l') { | |
1555 | spec.type = SCAN_LLONG; | |
1556 | p += 2; | |
1557 | } else { | |
1558 | spec.type = SCAN_LONG; | |
1559 | p++; | |
1560 | } | |
1561 | break; | |
1562 | ||
1563 | case 'L': | |
1564 | case 'q': | |
1565 | spec.type = SCAN_LLONG; | |
1566 | p++; | |
1567 | break; | |
1568 | ||
1569 | case 't': | |
1570 | spec.type = SCAN_PTRDIFF_T; | |
1571 | p++; | |
1572 | break; | |
1573 | ||
1574 | case 'z': | |
1575 | spec.type = SCAN_SIZE_T; | |
1576 | p++; | |
1577 | break; | |
1578 | ||
1579 | default: | |
1580 | spec.type = SCAN_INT; | |
1581 | break; | |
1582 | } | |
1583 | ||
1584 | if (discard) { | |
1585 | spec.type = SCAN_DISCARD; | |
1586 | } | |
1587 | ||
1588 | c = *p++; | |
1589 | if (c != 'c' && c != 'n' && c != '[') { | |
1590 | s = skip_spaces(s); | |
1591 | } | |
1592 | switch (c) { | |
1593 | case 'd': | |
1594 | s = scan_int(s, &spec, 10, &args); | |
1595 | break; | |
1596 | ||
1597 | case 'i': | |
1598 | s = scan_int(s, &spec, 0, &args); | |
1599 | break; | |
1600 | ||
1601 | case 'o': | |
1602 | s = scan_int(s, &spec, 8, &args); | |
1603 | break; | |
1604 | ||
1605 | case 'u': | |
1606 | s = scan_int(s, &spec, 10, &args); | |
1607 | break; | |
1608 | ||
1609 | case 'x': | |
1610 | case 'X': | |
1611 | s = scan_int(s, &spec, 16, &args); | |
1612 | break; | |
1613 | ||
1614 | case 'e': | |
1615 | case 'f': | |
1616 | case 'g': | |
1617 | case 'E': | |
1618 | case 'G': | |
1619 | s = scan_float(s, &spec, &args); | |
1620 | break; | |
1621 | ||
1622 | case 's': | |
1623 | s = scan_string(s, &spec, &args); | |
1624 | break; | |
1625 | ||
1626 | case '[': | |
1627 | s = scan_set(s, &spec, &p, &args); | |
1628 | break; | |
1629 | ||
1630 | case 'c': | |
1631 | s = scan_chars(s, &spec, &args); | |
1632 | break; | |
1633 | ||
1634 | case 'n': | |
1635 | if (spec.type != SCAN_DISCARD) { | |
1636 | *va_arg(args, int *) = s - start; | |
1637 | } | |
1638 | break; | |
1639 | } | |
1640 | ||
1641 | if (!s) { | |
1642 | goto exit; | |
1643 | } | |
1644 | } | |
1645 | ok = true; | |
1646 | ||
1647 | exit: | |
1648 | va_end(args); | |
1649 | return ok; | |
1650 | } | |
1651 | ||
06f14c92 GS |
1652 | #ifdef _WIN32 |
1653 | \f | |
1654 | /* Calls FormatMessage() with GetLastError() as an argument. Returns | |
1655 | * pointer to a buffer that receives the null-terminated string that specifies | |
1656 | * the formatted message and that has to be freed by the caller with | |
1657 | * LocalFree(). */ | |
1658 | char * | |
1659 | ovs_lasterror_to_string(void) | |
1660 | { | |
1661 | char *buffer; | |
1662 | FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | |
1663 | | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, GetLastError(), 0, | |
1664 | (char *)&buffer, 0, NULL); | |
1665 | return buffer; | |
1666 | } | |
1667 | #endif |