]>
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); | |
70 | NOT_REACHED(); | |
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 BP |
375 | { |
376 | const char *slash = strrchr(argv0, '/'); | |
728a8b14 BP |
377 | |
378 | assert_single_threaded(); | |
379 | ||
064af421 | 380 | program_name = slash ? slash + 1 : argv0; |
55d5bb44 JP |
381 | |
382 | free(program_version); | |
e385ef55 EJ |
383 | |
384 | if (!strcmp(version, VERSION)) { | |
385 | program_version = xasprintf("%s (Open vSwitch) "VERSION"\n" | |
386 | "Compiled %s %s\n", | |
387 | program_name, date, time); | |
388 | } else { | |
389 | program_version = xasprintf("%s %s\n" | |
390 | "Open vSwitch Library "VERSION"\n" | |
391 | "Compiled %s %s\n", | |
392 | program_name, version, date, time); | |
393 | } | |
55d5bb44 JP |
394 | } |
395 | ||
bc9fb3a9 BP |
396 | /* Returns the name of the currently running thread or process. */ |
397 | const char * | |
398 | get_subprogram_name(void) | |
399 | { | |
400 | const char *name = subprogram_name_get(); | |
401 | return name ? name : ""; | |
402 | } | |
403 | ||
d710edc4 BP |
404 | /* Sets the formatted value of 'format' as the name of the currently running |
405 | * thread or process. (This appears in log messages and may also be visible in | |
406 | * system process listings and debuggers.) */ | |
bc9fb3a9 | 407 | void |
d710edc4 | 408 | set_subprogram_name(const char *format, ...) |
bc9fb3a9 | 409 | { |
d710edc4 BP |
410 | char *pname; |
411 | ||
412 | if (format) { | |
413 | va_list args; | |
414 | ||
415 | va_start(args, format); | |
416 | pname = xvasprintf(format, args); | |
417 | va_end(args); | |
418 | } else { | |
419 | pname = xstrdup(program_name); | |
420 | } | |
421 | ||
422 | free(subprogram_name_set(pname)); | |
423 | ||
8a8cd0ac | 424 | #if HAVE_GLIBC_PTHREAD_SETNAME_NP |
e584f6a8 | 425 | pthread_setname_np(pthread_self(), pname); |
8a8cd0ac | 426 | #elif HAVE_NETBSD_PTHREAD_SETNAME_NP |
e584f6a8 | 427 | pthread_setname_np(pthread_self(), "%s", pname); |
0f13e650 | 428 | #elif HAVE_PTHREAD_SET_NAME_NP |
e584f6a8 | 429 | pthread_set_name_np(pthread_self(), pname); |
0f13e650 | 430 | #endif |
bc9fb3a9 BP |
431 | } |
432 | ||
55d5bb44 JP |
433 | /* Returns a pointer to a string describing the program version. The |
434 | * caller must not modify or free the returned string. | |
b53055f4 | 435 | */ |
55d5bb44 | 436 | const char * |
c71c6043 | 437 | get_program_version(void) |
55d5bb44 JP |
438 | { |
439 | return program_version; | |
064af421 BP |
440 | } |
441 | ||
442 | /* Print the version information for the program. */ | |
443 | void | |
55d5bb44 | 444 | ovs_print_version(uint8_t min_ofp, uint8_t max_ofp) |
064af421 | 445 | { |
55d5bb44 | 446 | printf("%s", program_version); |
064af421 BP |
447 | if (min_ofp || max_ofp) { |
448 | printf("OpenFlow versions %#x:%#x\n", min_ofp, max_ofp); | |
449 | } | |
450 | } | |
451 | ||
452 | /* Writes the 'size' bytes in 'buf' to 'stream' as hex bytes arranged 16 per | |
453 | * line. Numeric offsets are also included, starting at 'ofs' for the first | |
454 | * byte in 'buf'. If 'ascii' is true then the corresponding ASCII characters | |
455 | * are also rendered alongside. */ | |
456 | void | |
457 | ovs_hex_dump(FILE *stream, const void *buf_, size_t size, | |
458 | uintptr_t ofs, bool ascii) | |
459 | { | |
460 | const uint8_t *buf = buf_; | |
461 | const size_t per_line = 16; /* Maximum bytes per line. */ | |
462 | ||
463 | while (size > 0) | |
464 | { | |
465 | size_t start, end, n; | |
466 | size_t i; | |
467 | ||
468 | /* Number of bytes on this line. */ | |
469 | start = ofs % per_line; | |
470 | end = per_line; | |
471 | if (end - start > size) | |
472 | end = start + size; | |
473 | n = end - start; | |
474 | ||
475 | /* Print line. */ | |
34582733 | 476 | fprintf(stream, "%08"PRIxMAX" ", (uintmax_t) ROUND_DOWN(ofs, per_line)); |
064af421 BP |
477 | for (i = 0; i < start; i++) |
478 | fprintf(stream, " "); | |
479 | for (; i < end; i++) | |
34582733 | 480 | fprintf(stream, "%02x%c", |
064af421 BP |
481 | buf[i - start], i == per_line / 2 - 1? '-' : ' '); |
482 | if (ascii) | |
483 | { | |
484 | for (; i < per_line; i++) | |
485 | fprintf(stream, " "); | |
486 | fprintf(stream, "|"); | |
487 | for (i = 0; i < start; i++) | |
488 | fprintf(stream, " "); | |
489 | for (; i < end; i++) { | |
490 | int c = buf[i - start]; | |
491 | putc(c >= 32 && c < 127 ? c : '.', stream); | |
492 | } | |
493 | for (; i < per_line; i++) | |
494 | fprintf(stream, " "); | |
495 | fprintf(stream, "|"); | |
496 | } | |
497 | fprintf(stream, "\n"); | |
498 | ||
499 | ofs += n; | |
500 | buf += n; | |
501 | size -= n; | |
502 | } | |
503 | } | |
504 | ||
505 | bool | |
506 | str_to_int(const char *s, int base, int *i) | |
507 | { | |
508 | long long ll; | |
509 | bool ok = str_to_llong(s, base, &ll); | |
510 | *i = ll; | |
511 | return ok; | |
512 | } | |
513 | ||
514 | bool | |
515 | str_to_long(const char *s, int base, long *li) | |
516 | { | |
517 | long long ll; | |
518 | bool ok = str_to_llong(s, base, &ll); | |
519 | *li = ll; | |
520 | return ok; | |
521 | } | |
522 | ||
523 | bool | |
524 | str_to_llong(const char *s, int base, long long *x) | |
525 | { | |
526 | int save_errno = errno; | |
527 | char *tail; | |
528 | errno = 0; | |
529 | *x = strtoll(s, &tail, base); | |
530 | if (errno == EINVAL || errno == ERANGE || tail == s || *tail != '\0') { | |
531 | errno = save_errno; | |
532 | *x = 0; | |
533 | return false; | |
534 | } else { | |
535 | errno = save_errno; | |
536 | return true; | |
537 | } | |
538 | } | |
539 | ||
540 | bool | |
541 | str_to_uint(const char *s, int base, unsigned int *u) | |
542 | { | |
543 | return str_to_int(s, base, (int *) u); | |
544 | } | |
545 | ||
546 | bool | |
547 | str_to_ulong(const char *s, int base, unsigned long *ul) | |
548 | { | |
549 | return str_to_long(s, base, (long *) ul); | |
550 | } | |
551 | ||
552 | bool | |
553 | str_to_ullong(const char *s, int base, unsigned long long *ull) | |
554 | { | |
555 | return str_to_llong(s, base, (long long *) ull); | |
556 | } | |
f38b84ea BP |
557 | |
558 | /* Converts floating-point string 's' into a double. If successful, stores | |
559 | * the double in '*d' and returns true; on failure, stores 0 in '*d' and | |
560 | * returns false. | |
561 | * | |
562 | * Underflow (e.g. "1e-9999") is not considered an error, but overflow | |
563 | * (e.g. "1e9999)" is. */ | |
564 | bool | |
565 | str_to_double(const char *s, double *d) | |
566 | { | |
567 | int save_errno = errno; | |
568 | char *tail; | |
569 | errno = 0; | |
570 | *d = strtod(s, &tail); | |
571 | if (errno == EINVAL || (errno == ERANGE && *d != 0) | |
572 | || tail == s || *tail != '\0') { | |
573 | errno = save_errno; | |
574 | *d = 0; | |
575 | return false; | |
576 | } else { | |
577 | errno = save_errno; | |
578 | return true; | |
579 | } | |
580 | } | |
581 | ||
582 | /* Returns the value of 'c' as a hexadecimal digit. */ | |
583 | int | |
584 | hexit_value(int c) | |
585 | { | |
586 | switch (c) { | |
587 | case '0': case '1': case '2': case '3': case '4': | |
588 | case '5': case '6': case '7': case '8': case '9': | |
589 | return c - '0'; | |
590 | ||
591 | case 'a': case 'A': | |
592 | return 0xa; | |
593 | ||
594 | case 'b': case 'B': | |
595 | return 0xb; | |
596 | ||
597 | case 'c': case 'C': | |
598 | return 0xc; | |
599 | ||
600 | case 'd': case 'D': | |
601 | return 0xd; | |
602 | ||
603 | case 'e': case 'E': | |
604 | return 0xe; | |
605 | ||
606 | case 'f': case 'F': | |
607 | return 0xf; | |
f38b84ea | 608 | |
09246b99 BP |
609 | default: |
610 | return -1; | |
611 | } | |
f38b84ea | 612 | } |
29d4af60 | 613 | |
bf971267 BP |
614 | /* Returns the integer value of the 'n' hexadecimal digits starting at 's', or |
615 | * UINT_MAX if one of those "digits" is not really a hex digit. If 'ok' is | |
616 | * nonnull, '*ok' is set to true if the conversion succeeds or to false if a | |
617 | * non-hex digit is detected. */ | |
618 | unsigned int | |
619 | hexits_value(const char *s, size_t n, bool *ok) | |
620 | { | |
621 | unsigned int value; | |
622 | size_t i; | |
623 | ||
624 | value = 0; | |
625 | for (i = 0; i < n; i++) { | |
626 | int hexit = hexit_value(s[i]); | |
627 | if (hexit < 0) { | |
628 | if (ok) { | |
629 | *ok = false; | |
630 | } | |
631 | return UINT_MAX; | |
632 | } | |
633 | value = (value << 4) + hexit; | |
634 | } | |
635 | if (ok) { | |
636 | *ok = true; | |
637 | } | |
638 | return value; | |
639 | } | |
640 | ||
daf03c53 BP |
641 | /* Returns the current working directory as a malloc()'d string, or a null |
642 | * pointer if the current working directory cannot be determined. */ | |
643 | char * | |
644 | get_cwd(void) | |
645 | { | |
646 | long int path_max; | |
647 | size_t size; | |
648 | ||
649 | /* Get maximum path length or at least a reasonable estimate. */ | |
650 | path_max = pathconf(".", _PC_PATH_MAX); | |
651 | size = (path_max < 0 ? 1024 | |
652 | : path_max > 10240 ? 10240 | |
653 | : path_max); | |
654 | ||
655 | /* Get current working directory. */ | |
656 | for (;;) { | |
657 | char *buf = xmalloc(size); | |
658 | if (getcwd(buf, size)) { | |
659 | return xrealloc(buf, strlen(buf) + 1); | |
660 | } else { | |
661 | int error = errno; | |
662 | free(buf); | |
663 | if (error != ERANGE) { | |
10a89ef0 | 664 | VLOG_WARN("getcwd failed (%s)", ovs_strerror(error)); |
daf03c53 BP |
665 | return NULL; |
666 | } | |
667 | size *= 2; | |
668 | } | |
669 | } | |
670 | } | |
671 | ||
e1aff6f9 BP |
672 | static char * |
673 | all_slashes_name(const char *s) | |
674 | { | |
675 | return xstrdup(s[0] == '/' && s[1] == '/' && s[2] != '/' ? "//" | |
676 | : s[0] == '/' ? "/" | |
677 | : "."); | |
678 | } | |
679 | ||
29d4af60 BP |
680 | /* Returns the directory name portion of 'file_name' as a malloc()'d string, |
681 | * similar to the POSIX dirname() function but thread-safe. */ | |
682 | char * | |
683 | dir_name(const char *file_name) | |
684 | { | |
685 | size_t len = strlen(file_name); | |
686 | while (len > 0 && file_name[len - 1] == '/') { | |
687 | len--; | |
688 | } | |
689 | while (len > 0 && file_name[len - 1] != '/') { | |
690 | len--; | |
691 | } | |
692 | while (len > 0 && file_name[len - 1] == '/') { | |
693 | len--; | |
694 | } | |
e1aff6f9 BP |
695 | return len ? xmemdup0(file_name, len) : all_slashes_name(file_name); |
696 | } | |
697 | ||
698 | /* Returns the file name portion of 'file_name' as a malloc()'d string, | |
699 | * similar to the POSIX basename() function but thread-safe. */ | |
700 | char * | |
701 | base_name(const char *file_name) | |
702 | { | |
703 | size_t end, start; | |
704 | ||
705 | end = strlen(file_name); | |
706 | while (end > 0 && file_name[end - 1] == '/') { | |
707 | end--; | |
708 | } | |
709 | ||
710 | if (!end) { | |
711 | return all_slashes_name(file_name); | |
29d4af60 | 712 | } |
e1aff6f9 BP |
713 | |
714 | start = end; | |
715 | while (start > 0 && file_name[start - 1] != '/') { | |
716 | start--; | |
717 | } | |
718 | ||
719 | return xmemdup0(file_name + start, end - start); | |
29d4af60 | 720 | } |
18b9283b | 721 | |
daf03c53 BP |
722 | /* If 'file_name' starts with '/', returns a copy of 'file_name'. Otherwise, |
723 | * returns an absolute path to 'file_name' considering it relative to 'dir', | |
724 | * which itself must be absolute. 'dir' may be null or the empty string, in | |
725 | * which case the current working directory is used. | |
726 | * | |
727 | * Returns a null pointer if 'dir' is null and getcwd() fails. */ | |
728 | char * | |
729 | abs_file_name(const char *dir, const char *file_name) | |
730 | { | |
731 | if (file_name[0] == '/') { | |
732 | return xstrdup(file_name); | |
733 | } else if (dir && dir[0]) { | |
734 | char *separator = dir[strlen(dir) - 1] == '/' ? "" : "/"; | |
735 | return xasprintf("%s%s%s", dir, separator, file_name); | |
736 | } else { | |
737 | char *cwd = get_cwd(); | |
738 | if (cwd) { | |
739 | char *abs_name = xasprintf("%s/%s", cwd, file_name); | |
740 | free(cwd); | |
741 | return abs_name; | |
742 | } else { | |
743 | return NULL; | |
744 | } | |
745 | } | |
746 | } | |
747 | ||
fee0c963 BP |
748 | /* Like readlink(), but returns the link name as a null-terminated string in |
749 | * allocated memory that the caller must eventually free (with free()). | |
750 | * Returns NULL on error, in which case errno is set appropriately. */ | |
751 | char * | |
752 | xreadlink(const char *filename) | |
753 | { | |
754 | size_t size; | |
755 | ||
756 | for (size = 64; ; size *= 2) { | |
757 | char *buf = xmalloc(size); | |
758 | ssize_t retval = readlink(filename, buf, size); | |
759 | int error = errno; | |
760 | ||
761 | if (retval >= 0 && retval < size) { | |
762 | buf[retval] = '\0'; | |
763 | return buf; | |
764 | } | |
765 | ||
766 | free(buf); | |
767 | if (retval < 0) { | |
768 | errno = error; | |
769 | return NULL; | |
770 | } | |
771 | } | |
772 | } | |
773 | ||
774 | /* Returns a version of 'filename' with symlinks in the final component | |
775 | * dereferenced. This differs from realpath() in that: | |
776 | * | |
777 | * - 'filename' need not exist. | |
778 | * | |
779 | * - If 'filename' does exist as a symlink, its referent need not exist. | |
780 | * | |
781 | * - Only symlinks in the final component of 'filename' are dereferenced. | |
782 | * | |
783 | * The caller must eventually free the returned string (with free()). */ | |
784 | char * | |
785 | follow_symlinks(const char *filename) | |
786 | { | |
787 | struct stat s; | |
788 | char *fn; | |
789 | int i; | |
790 | ||
791 | fn = xstrdup(filename); | |
792 | for (i = 0; i < 10; i++) { | |
793 | char *linkname; | |
794 | char *next_fn; | |
795 | ||
796 | if (lstat(fn, &s) != 0 || !S_ISLNK(s.st_mode)) { | |
797 | return fn; | |
798 | } | |
799 | ||
800 | linkname = xreadlink(fn); | |
801 | if (!linkname) { | |
10a89ef0 BP |
802 | VLOG_WARN("%s: readlink failed (%s)", |
803 | filename, ovs_strerror(errno)); | |
fee0c963 BP |
804 | return fn; |
805 | } | |
806 | ||
807 | if (linkname[0] == '/') { | |
808 | /* Target of symlink is absolute so use it raw. */ | |
809 | next_fn = linkname; | |
810 | } else { | |
811 | /* Target of symlink is relative so add to 'fn''s directory. */ | |
812 | char *dir = dir_name(fn); | |
813 | ||
814 | if (!strcmp(dir, ".")) { | |
815 | next_fn = linkname; | |
816 | } else { | |
817 | char *separator = dir[strlen(dir) - 1] == '/' ? "" : "/"; | |
818 | next_fn = xasprintf("%s%s%s", dir, separator, linkname); | |
819 | free(linkname); | |
820 | } | |
821 | ||
822 | free(dir); | |
823 | } | |
824 | ||
825 | free(fn); | |
826 | fn = next_fn; | |
827 | } | |
828 | ||
829 | VLOG_WARN("%s: too many levels of symlinks", filename); | |
830 | free(fn); | |
831 | return xstrdup(filename); | |
832 | } | |
daf03c53 | 833 | |
18b9283b | 834 | /* Pass a value to this function if it is marked with |
d295e8e9 JP |
835 | * __attribute__((warn_unused_result)) and you genuinely want to ignore |
836 | * its return value. (Note that every scalar type can be implicitly | |
18b9283b | 837 | * converted to bool.) */ |
c69ee87c | 838 | void ignore(bool x OVS_UNUSED) { } |
44b4d050 BP |
839 | |
840 | /* Returns an appropriate delimiter for inserting just before the 0-based item | |
841 | * 'index' in a list that has 'total' items in it. */ | |
842 | const char * | |
843 | english_list_delimiter(size_t index, size_t total) | |
844 | { | |
845 | return (index == 0 ? "" | |
846 | : index < total - 1 ? ", " | |
847 | : total > 2 ? ", and " | |
848 | : " and "); | |
849 | } | |
711e0157 | 850 | |
0ee140fb | 851 | /* Returns the number of trailing 0-bits in 'n'. Undefined if 'n' == 0. */ |
d43d314e | 852 | #if __GNUC__ >= 4 |
0ee140fb | 853 | /* Defined inline in util.h. */ |
aad29cd1 | 854 | #else |
8c947903 | 855 | /* Returns the number of trailing 0-bits in 'n'. Undefined if 'n' == 0. */ |
481da12c | 856 | int |
d43d314e | 857 | raw_ctz(uint64_t n) |
0ee140fb | 858 | { |
d43d314e BP |
859 | uint64_t k; |
860 | int count = 63; | |
aad29cd1 BP |
861 | |
862 | #define CTZ_STEP(X) \ | |
0ee140fb BP |
863 | k = n << (X); \ |
864 | if (k) { \ | |
865 | count -= X; \ | |
866 | n = k; \ | |
867 | } | |
d43d314e | 868 | CTZ_STEP(32); |
0ee140fb BP |
869 | CTZ_STEP(16); |
870 | CTZ_STEP(8); | |
871 | CTZ_STEP(4); | |
872 | CTZ_STEP(2); | |
873 | CTZ_STEP(1); | |
aad29cd1 BP |
874 | #undef CTZ_STEP |
875 | ||
0ee140fb | 876 | return count; |
aad29cd1 | 877 | } |
8c947903 JR |
878 | |
879 | /* Returns the number of leading 0-bits in 'n'. Undefined if 'n' == 0. */ | |
880 | int | |
881 | raw_clz64(uint64_t n) | |
882 | { | |
883 | uint64_t k; | |
884 | int count = 63; | |
885 | ||
886 | #define CLZ_STEP(X) \ | |
887 | k = n >> (X); \ | |
888 | if (k) { \ | |
889 | count -= X; \ | |
890 | n = k; \ | |
891 | } | |
892 | CLZ_STEP(32); | |
893 | CLZ_STEP(16); | |
894 | CLZ_STEP(8); | |
895 | CLZ_STEP(4); | |
896 | CLZ_STEP(2); | |
897 | CLZ_STEP(1); | |
898 | #undef CLZ_STEP | |
899 | ||
900 | return count; | |
901 | } | |
0ee140fb | 902 | #endif |
75a75043 | 903 | |
a656cb77 | 904 | /* Returns the number of 1-bits in 'x', between 0 and 32 inclusive. */ |
2a4ca27c | 905 | static unsigned int |
fb9aefa3 | 906 | count_1bits_32(uint32_t x) |
a656cb77 BP |
907 | { |
908 | /* In my testing, this implementation is over twice as fast as any other | |
909 | * portable implementation that I tried, including GCC 4.4 | |
910 | * __builtin_popcount(), although nonportable asm("popcnt") was over 50% | |
911 | * faster. */ | |
912 | #define INIT1(X) \ | |
913 | ((((X) & (1 << 0)) != 0) + \ | |
914 | (((X) & (1 << 1)) != 0) + \ | |
915 | (((X) & (1 << 2)) != 0) + \ | |
916 | (((X) & (1 << 3)) != 0) + \ | |
917 | (((X) & (1 << 4)) != 0) + \ | |
918 | (((X) & (1 << 5)) != 0) + \ | |
919 | (((X) & (1 << 6)) != 0) + \ | |
920 | (((X) & (1 << 7)) != 0)) | |
921 | #define INIT2(X) INIT1(X), INIT1((X) + 1) | |
922 | #define INIT4(X) INIT2(X), INIT2((X) + 2) | |
923 | #define INIT8(X) INIT4(X), INIT4((X) + 4) | |
924 | #define INIT16(X) INIT8(X), INIT8((X) + 8) | |
925 | #define INIT32(X) INIT16(X), INIT16((X) + 16) | |
926 | #define INIT64(X) INIT32(X), INIT32((X) + 32) | |
927 | ||
fb9aefa3 | 928 | static const uint8_t count_1bits_8[256] = { |
a656cb77 BP |
929 | INIT64(0), INIT64(64), INIT64(128), INIT64(192) |
930 | }; | |
931 | ||
fb9aefa3 BP |
932 | return (count_1bits_8[x & 0xff] + |
933 | count_1bits_8[(x >> 8) & 0xff] + | |
934 | count_1bits_8[(x >> 16) & 0xff] + | |
935 | count_1bits_8[x >> 24]); | |
a656cb77 BP |
936 | } |
937 | ||
2a4ca27c BP |
938 | /* Returns the number of 1-bits in 'x', between 0 and 64 inclusive. */ |
939 | unsigned int | |
fb9aefa3 | 940 | count_1bits(uint64_t x) |
2a4ca27c | 941 | { |
fb9aefa3 | 942 | return count_1bits_32(x) + count_1bits_32(x >> 32); |
2a4ca27c BP |
943 | } |
944 | ||
75a75043 BP |
945 | /* Returns true if the 'n' bytes starting at 'p' are zeros. */ |
946 | bool | |
947 | is_all_zeros(const uint8_t *p, size_t n) | |
948 | { | |
949 | size_t i; | |
950 | ||
951 | for (i = 0; i < n; i++) { | |
952 | if (p[i] != 0x00) { | |
953 | return false; | |
954 | } | |
955 | } | |
956 | return true; | |
957 | } | |
958 | ||
959 | /* Returns true if the 'n' bytes starting at 'p' are 0xff. */ | |
960 | bool | |
961 | is_all_ones(const uint8_t *p, size_t n) | |
962 | { | |
963 | size_t i; | |
964 | ||
965 | for (i = 0; i < n; i++) { | |
966 | if (p[i] != 0xff) { | |
967 | return false; | |
968 | } | |
969 | } | |
970 | return true; | |
971 | } | |
972 | ||
ddc4f8e2 BP |
973 | /* Copies 'n_bits' bits starting from bit 'src_ofs' in 'src' to the 'n_bits' |
974 | * starting from bit 'dst_ofs' in 'dst'. 'src' is 'src_len' bytes long and | |
975 | * 'dst' is 'dst_len' bytes long. | |
976 | * | |
977 | * If you consider all of 'src' to be a single unsigned integer in network byte | |
978 | * order, then bit N is the bit with value 2**N. That is, bit 0 is the bit | |
979 | * with value 1 in src[src_len - 1], bit 1 is the bit with value 2, bit 2 is | |
980 | * the bit with value 4, ..., bit 8 is the bit with value 1 in src[src_len - | |
981 | * 2], and so on. Similarly for 'dst'. | |
982 | * | |
983 | * Required invariants: | |
984 | * src_ofs + n_bits <= src_len * 8 | |
985 | * dst_ofs + n_bits <= dst_len * 8 | |
986 | * 'src' and 'dst' must not overlap. | |
987 | */ | |
988 | void | |
989 | bitwise_copy(const void *src_, unsigned int src_len, unsigned int src_ofs, | |
990 | void *dst_, unsigned int dst_len, unsigned int dst_ofs, | |
991 | unsigned int n_bits) | |
992 | { | |
993 | const uint8_t *src = src_; | |
994 | uint8_t *dst = dst_; | |
995 | ||
996 | src += src_len - (src_ofs / 8 + 1); | |
997 | src_ofs %= 8; | |
998 | ||
999 | dst += dst_len - (dst_ofs / 8 + 1); | |
1000 | dst_ofs %= 8; | |
1001 | ||
1002 | if (src_ofs == 0 && dst_ofs == 0) { | |
1003 | unsigned int n_bytes = n_bits / 8; | |
1004 | if (n_bytes) { | |
1005 | dst -= n_bytes - 1; | |
1006 | src -= n_bytes - 1; | |
1007 | memcpy(dst, src, n_bytes); | |
1008 | ||
1009 | n_bits %= 8; | |
1010 | src--; | |
1011 | dst--; | |
1012 | } | |
1013 | if (n_bits) { | |
1014 | uint8_t mask = (1 << n_bits) - 1; | |
1015 | *dst = (*dst & ~mask) | (*src & mask); | |
1016 | } | |
1017 | } else { | |
1018 | while (n_bits > 0) { | |
1019 | unsigned int max_copy = 8 - MAX(src_ofs, dst_ofs); | |
1020 | unsigned int chunk = MIN(n_bits, max_copy); | |
1021 | uint8_t mask = ((1 << chunk) - 1) << dst_ofs; | |
1022 | ||
1023 | *dst &= ~mask; | |
1024 | *dst |= ((*src >> src_ofs) << dst_ofs) & mask; | |
1025 | ||
1026 | src_ofs += chunk; | |
1027 | if (src_ofs == 8) { | |
1028 | src--; | |
1029 | src_ofs = 0; | |
1030 | } | |
1031 | dst_ofs += chunk; | |
1032 | if (dst_ofs == 8) { | |
1033 | dst--; | |
1034 | dst_ofs = 0; | |
1035 | } | |
1036 | n_bits -= chunk; | |
1037 | } | |
1038 | } | |
1039 | } | |
1040 | ||
6cc7ea5e BP |
1041 | /* Zeros the 'n_bits' bits starting from bit 'dst_ofs' in 'dst'. 'dst' is |
1042 | * 'dst_len' bytes long. | |
1043 | * | |
1044 | * If you consider all of 'dst' to be a single unsigned integer in network byte | |
1045 | * order, then bit N is the bit with value 2**N. That is, bit 0 is the bit | |
1046 | * with value 1 in dst[dst_len - 1], bit 1 is the bit with value 2, bit 2 is | |
1047 | * the bit with value 4, ..., bit 8 is the bit with value 1 in dst[dst_len - | |
1048 | * 2], and so on. | |
1049 | * | |
1050 | * Required invariant: | |
1051 | * dst_ofs + n_bits <= dst_len * 8 | |
1052 | */ | |
1053 | void | |
1054 | bitwise_zero(void *dst_, unsigned int dst_len, unsigned dst_ofs, | |
1055 | unsigned int n_bits) | |
1056 | { | |
1057 | uint8_t *dst = dst_; | |
1058 | ||
1059 | if (!n_bits) { | |
1060 | return; | |
1061 | } | |
1062 | ||
1063 | dst += dst_len - (dst_ofs / 8 + 1); | |
1064 | dst_ofs %= 8; | |
1065 | ||
1066 | if (dst_ofs) { | |
1067 | unsigned int chunk = MIN(n_bits, 8 - dst_ofs); | |
1068 | ||
1069 | *dst &= ~(((1 << chunk) - 1) << dst_ofs); | |
1070 | ||
1071 | n_bits -= chunk; | |
1072 | if (!n_bits) { | |
1073 | return; | |
1074 | } | |
1075 | ||
1076 | dst--; | |
1077 | } | |
1078 | ||
1079 | while (n_bits >= 8) { | |
1080 | *dst-- = 0; | |
1081 | n_bits -= 8; | |
1082 | } | |
1083 | ||
1084 | if (n_bits) { | |
1085 | *dst &= ~((1 << n_bits) - 1); | |
1086 | } | |
1087 | } | |
1088 | ||
c2dd4932 BP |
1089 | /* Sets to 1 all of the 'n_bits' bits starting from bit 'dst_ofs' in 'dst'. |
1090 | * 'dst' is 'dst_len' bytes long. | |
1091 | * | |
1092 | * If you consider all of 'dst' to be a single unsigned integer in network byte | |
1093 | * order, then bit N is the bit with value 2**N. That is, bit 0 is the bit | |
1094 | * with value 1 in dst[dst_len - 1], bit 1 is the bit with value 2, bit 2 is | |
1095 | * the bit with value 4, ..., bit 8 is the bit with value 1 in dst[dst_len - | |
1096 | * 2], and so on. | |
1097 | * | |
1098 | * Required invariant: | |
1099 | * dst_ofs + n_bits <= dst_len * 8 | |
1100 | */ | |
1101 | void | |
1102 | bitwise_one(void *dst_, unsigned int dst_len, unsigned dst_ofs, | |
1103 | unsigned int n_bits) | |
1104 | { | |
1105 | uint8_t *dst = dst_; | |
1106 | ||
1107 | if (!n_bits) { | |
1108 | return; | |
1109 | } | |
1110 | ||
1111 | dst += dst_len - (dst_ofs / 8 + 1); | |
1112 | dst_ofs %= 8; | |
1113 | ||
1114 | if (dst_ofs) { | |
1115 | unsigned int chunk = MIN(n_bits, 8 - dst_ofs); | |
1116 | ||
1117 | *dst |= ((1 << chunk) - 1) << dst_ofs; | |
1118 | ||
1119 | n_bits -= chunk; | |
1120 | if (!n_bits) { | |
1121 | return; | |
1122 | } | |
1123 | ||
1124 | dst--; | |
1125 | } | |
1126 | ||
1127 | while (n_bits >= 8) { | |
1128 | *dst-- = 0xff; | |
1129 | n_bits -= 8; | |
1130 | } | |
1131 | ||
1132 | if (n_bits) { | |
1133 | *dst |= (1 << n_bits) - 1; | |
1134 | } | |
1135 | } | |
1136 | ||
79a010aa BP |
1137 | /* Scans the 'n_bits' bits starting from bit 'dst_ofs' in 'dst' for 1-bits. |
1138 | * Returns false if any 1-bits are found, otherwise true. 'dst' is 'dst_len' | |
1139 | * bytes long. | |
1140 | * | |
1141 | * If you consider all of 'dst' to be a single unsigned integer in network byte | |
1142 | * order, then bit N is the bit with value 2**N. That is, bit 0 is the bit | |
1143 | * with value 1 in dst[dst_len - 1], bit 1 is the bit with value 2, bit 2 is | |
1144 | * the bit with value 4, ..., bit 8 is the bit with value 1 in dst[dst_len - | |
1145 | * 2], and so on. | |
1146 | * | |
1147 | * Required invariant: | |
1148 | * dst_ofs + n_bits <= dst_len * 8 | |
1149 | */ | |
1150 | bool | |
1151 | bitwise_is_all_zeros(const void *p_, unsigned int len, unsigned int ofs, | |
1152 | unsigned int n_bits) | |
1153 | { | |
1154 | const uint8_t *p = p_; | |
1155 | ||
1156 | if (!n_bits) { | |
1157 | return true; | |
1158 | } | |
1159 | ||
1160 | p += len - (ofs / 8 + 1); | |
1161 | ofs %= 8; | |
1162 | ||
1163 | if (ofs) { | |
1164 | unsigned int chunk = MIN(n_bits, 8 - ofs); | |
1165 | ||
1166 | if (*p & (((1 << chunk) - 1) << ofs)) { | |
1167 | return false; | |
1168 | } | |
1169 | ||
1170 | n_bits -= chunk; | |
1171 | if (!n_bits) { | |
1172 | return true; | |
1173 | } | |
1174 | ||
1175 | p--; | |
1176 | } | |
1177 | ||
1178 | while (n_bits >= 8) { | |
1179 | if (*p) { | |
1180 | return false; | |
1181 | } | |
1182 | n_bits -= 8; | |
1183 | p--; | |
1184 | } | |
1185 | ||
1186 | if (n_bits && *p & ((1 << n_bits) - 1)) { | |
1187 | return false; | |
1188 | } | |
1189 | ||
1190 | return true; | |
1191 | } | |
1192 | ||
ddc4f8e2 BP |
1193 | /* Copies the 'n_bits' low-order bits of 'value' into the 'n_bits' bits |
1194 | * starting at bit 'dst_ofs' in 'dst', which is 'dst_len' bytes long. | |
1195 | * | |
1196 | * If you consider all of 'dst' to be a single unsigned integer in network byte | |
1197 | * order, then bit N is the bit with value 2**N. That is, bit 0 is the bit | |
1198 | * with value 1 in dst[dst_len - 1], bit 1 is the bit with value 2, bit 2 is | |
1199 | * the bit with value 4, ..., bit 8 is the bit with value 1 in dst[dst_len - | |
1200 | * 2], and so on. | |
1201 | * | |
1202 | * Required invariants: | |
1203 | * dst_ofs + n_bits <= dst_len * 8 | |
1204 | * n_bits <= 64 | |
1205 | */ | |
1206 | void | |
1207 | bitwise_put(uint64_t value, | |
1208 | void *dst, unsigned int dst_len, unsigned int dst_ofs, | |
1209 | unsigned int n_bits) | |
1210 | { | |
1211 | ovs_be64 n_value = htonll(value); | |
1212 | bitwise_copy(&n_value, sizeof n_value, 0, | |
1213 | dst, dst_len, dst_ofs, | |
1214 | n_bits); | |
1215 | } | |
1216 | ||
1217 | /* Returns the value of the 'n_bits' bits starting at bit 'src_ofs' in 'src', | |
1218 | * which is 'src_len' bytes long. | |
1219 | * | |
1220 | * If you consider all of 'src' to be a single unsigned integer in network byte | |
1221 | * order, then bit N is the bit with value 2**N. That is, bit 0 is the bit | |
1222 | * with value 1 in src[src_len - 1], bit 1 is the bit with value 2, bit 2 is | |
1223 | * the bit with value 4, ..., bit 8 is the bit with value 1 in src[src_len - | |
1224 | * 2], and so on. | |
1225 | * | |
1226 | * Required invariants: | |
1227 | * src_ofs + n_bits <= src_len * 8 | |
1228 | * n_bits <= 64 | |
1229 | */ | |
1230 | uint64_t | |
1231 | bitwise_get(const void *src, unsigned int src_len, | |
1232 | unsigned int src_ofs, unsigned int n_bits) | |
1233 | { | |
1234 | ovs_be64 value = htonll(0); | |
1235 | ||
1236 | bitwise_copy(src, src_len, src_ofs, | |
1237 | &value, sizeof value, 0, | |
1238 | n_bits); | |
1239 | return ntohll(value); | |
1240 | } | |
ed2232fc BP |
1241 | \f |
1242 | /* ovs_scan */ | |
1243 | ||
1244 | struct scan_spec { | |
1245 | unsigned int width; | |
1246 | enum { | |
1247 | SCAN_DISCARD, | |
1248 | SCAN_CHAR, | |
1249 | SCAN_SHORT, | |
1250 | SCAN_INT, | |
1251 | SCAN_LONG, | |
1252 | SCAN_LLONG, | |
1253 | SCAN_INTMAX_T, | |
1254 | SCAN_PTRDIFF_T, | |
1255 | SCAN_SIZE_T | |
1256 | } type; | |
1257 | }; | |
1258 | ||
1259 | static const char * | |
1260 | skip_spaces(const char *s) | |
1261 | { | |
1262 | while (isspace((unsigned char) *s)) { | |
1263 | s++; | |
1264 | } | |
1265 | return s; | |
1266 | } | |
1267 | ||
1268 | static const char * | |
1269 | scan_int(const char *s, const struct scan_spec *spec, int base, va_list *args) | |
1270 | { | |
1271 | const char *start = s; | |
1272 | uintmax_t value; | |
1273 | bool negative; | |
1274 | int n_digits; | |
1275 | ||
1276 | negative = *s == '-'; | |
1277 | s += *s == '-' || *s == '+'; | |
1278 | ||
1279 | if ((!base || base == 16) && *s == '0' && (s[1] == 'x' || s[1] == 'X')) { | |
1280 | base = 16; | |
1281 | s += 2; | |
1282 | } else if (!base) { | |
1283 | base = *s == '0' ? 8 : 10; | |
1284 | } | |
1285 | ||
1286 | if (s - start >= spec->width) { | |
1287 | return NULL; | |
1288 | } | |
1289 | ||
1290 | value = 0; | |
1291 | n_digits = 0; | |
1292 | while (s - start < spec->width) { | |
1293 | int digit = hexit_value(*s); | |
1294 | ||
1295 | if (digit < 0 || digit >= base) { | |
1296 | break; | |
1297 | } | |
1298 | value = value * base + digit; | |
1299 | n_digits++; | |
1300 | s++; | |
1301 | } | |
1302 | if (!n_digits) { | |
1303 | return NULL; | |
1304 | } | |
1305 | ||
1306 | if (negative) { | |
1307 | value = -value; | |
1308 | } | |
1309 | ||
1310 | switch (spec->type) { | |
1311 | case SCAN_DISCARD: | |
1312 | break; | |
1313 | case SCAN_CHAR: | |
1314 | *va_arg(*args, char *) = value; | |
1315 | break; | |
1316 | case SCAN_SHORT: | |
1317 | *va_arg(*args, short int *) = value; | |
1318 | break; | |
1319 | case SCAN_INT: | |
1320 | *va_arg(*args, int *) = value; | |
1321 | break; | |
1322 | case SCAN_LONG: | |
1323 | *va_arg(*args, long int *) = value; | |
1324 | break; | |
1325 | case SCAN_LLONG: | |
1326 | *va_arg(*args, long long int *) = value; | |
1327 | break; | |
1328 | case SCAN_INTMAX_T: | |
1329 | *va_arg(*args, intmax_t *) = value; | |
1330 | break; | |
1331 | case SCAN_PTRDIFF_T: | |
1332 | *va_arg(*args, ptrdiff_t *) = value; | |
1333 | break; | |
1334 | case SCAN_SIZE_T: | |
1335 | *va_arg(*args, size_t *) = value; | |
1336 | break; | |
1337 | } | |
1338 | return s; | |
1339 | } | |
1340 | ||
1341 | static const char * | |
1342 | skip_digits(const char *s) | |
1343 | { | |
1344 | while (*s >= '0' && *s <= '9') { | |
1345 | s++; | |
1346 | } | |
1347 | return s; | |
1348 | } | |
1349 | ||
1350 | static const char * | |
1351 | scan_float(const char *s, const struct scan_spec *spec, va_list *args) | |
1352 | { | |
1353 | const char *start = s; | |
1354 | long double value; | |
1355 | char *tail; | |
1356 | char *copy; | |
1357 | bool ok; | |
1358 | ||
1359 | s += *s == '+' || *s == '-'; | |
1360 | s = skip_digits(s); | |
1361 | if (*s == '.') { | |
1362 | s = skip_digits(s + 1); | |
1363 | } | |
1364 | if (*s == 'e' || *s == 'E') { | |
1365 | s++; | |
1366 | s += *s == '+' || *s == '-'; | |
1367 | s = skip_digits(s); | |
1368 | } | |
1369 | ||
1370 | if (s - start > spec->width) { | |
1371 | s = start + spec->width; | |
1372 | } | |
1373 | ||
1374 | copy = xmemdup0(start, s - start); | |
1375 | value = strtold(copy, &tail); | |
1376 | ok = *tail == '\0'; | |
1377 | free(copy); | |
1378 | if (!ok) { | |
1379 | return NULL; | |
1380 | } | |
1381 | ||
1382 | switch (spec->type) { | |
1383 | case SCAN_DISCARD: | |
1384 | break; | |
1385 | case SCAN_INT: | |
1386 | *va_arg(*args, float *) = value; | |
1387 | break; | |
1388 | case SCAN_LONG: | |
1389 | *va_arg(*args, double *) = value; | |
1390 | break; | |
1391 | case SCAN_LLONG: | |
1392 | *va_arg(*args, long double *) = value; | |
1393 | break; | |
1394 | ||
1395 | case SCAN_CHAR: | |
1396 | case SCAN_SHORT: | |
1397 | case SCAN_INTMAX_T: | |
1398 | case SCAN_PTRDIFF_T: | |
1399 | case SCAN_SIZE_T: | |
1400 | NOT_REACHED(); | |
1401 | } | |
1402 | return s; | |
1403 | } | |
1404 | ||
1405 | static void | |
1406 | scan_output_string(const struct scan_spec *spec, | |
1407 | const char *s, size_t n, | |
1408 | va_list *args) | |
1409 | { | |
1410 | if (spec->type != SCAN_DISCARD) { | |
1411 | char *out = va_arg(*args, char *); | |
1412 | memcpy(out, s, n); | |
1413 | out[n] = '\0'; | |
1414 | } | |
1415 | } | |
1416 | ||
1417 | static const char * | |
1418 | scan_string(const char *s, const struct scan_spec *spec, va_list *args) | |
1419 | { | |
1420 | size_t n; | |
1421 | ||
1422 | for (n = 0; n < spec->width; n++) { | |
1423 | if (!s[n] || isspace((unsigned char) s[n])) { | |
1424 | break; | |
1425 | } | |
1426 | } | |
1427 | if (!n) { | |
1428 | return NULL; | |
1429 | } | |
1430 | ||
1431 | scan_output_string(spec, s, n, args); | |
1432 | return s + n; | |
1433 | } | |
1434 | ||
1435 | static const char * | |
1436 | parse_scanset(const char *p_, unsigned long *set, bool *complemented) | |
1437 | { | |
1438 | const uint8_t *p = (const uint8_t *) p_; | |
1439 | ||
1440 | *complemented = *p == '^'; | |
1441 | p += *complemented; | |
1442 | ||
1443 | if (*p == ']') { | |
1444 | bitmap_set1(set, ']'); | |
1445 | p++; | |
1446 | } | |
1447 | ||
1448 | while (*p && *p != ']') { | |
1449 | if (p[1] == '-' && p[2] != ']' && p[2] > *p) { | |
1450 | bitmap_set_multiple(set, *p, p[2] - *p + 1, true); | |
1451 | p += 3; | |
1452 | } else { | |
1453 | bitmap_set1(set, *p++); | |
1454 | } | |
1455 | } | |
1456 | if (*p == ']') { | |
1457 | p++; | |
1458 | } | |
1459 | return (const char *) p; | |
1460 | } | |
1461 | ||
1462 | static const char * | |
1463 | scan_set(const char *s, const struct scan_spec *spec, const char **pp, | |
1464 | va_list *args) | |
1465 | { | |
1466 | unsigned long set[BITMAP_N_LONGS(UCHAR_MAX + 1)]; | |
1467 | bool complemented; | |
1468 | unsigned int n; | |
1469 | ||
1470 | /* Parse the scan set. */ | |
1471 | memset(set, 0, sizeof set); | |
1472 | *pp = parse_scanset(*pp, set, &complemented); | |
1473 | ||
1474 | /* Parse the data. */ | |
1475 | n = 0; | |
1476 | while (s[n] | |
1477 | && bitmap_is_set(set, (unsigned char) s[n]) == !complemented | |
1478 | && n < spec->width) { | |
1479 | n++; | |
1480 | } | |
1481 | if (!n) { | |
1482 | return NULL; | |
1483 | } | |
1484 | scan_output_string(spec, s, n, args); | |
1485 | return s + n; | |
1486 | } | |
1487 | ||
1488 | static const char * | |
1489 | scan_chars(const char *s, const struct scan_spec *spec, va_list *args) | |
1490 | { | |
31499678 | 1491 | unsigned int n = spec->width == UINT_MAX ? 1 : spec->width; |
ed2232fc BP |
1492 | |
1493 | if (strlen(s) < n) { | |
1494 | return NULL; | |
1495 | } | |
1496 | if (spec->type != SCAN_DISCARD) { | |
1497 | memcpy(va_arg(*args, char *), s, n); | |
1498 | } | |
1499 | return s + n; | |
1500 | } | |
1501 | ||
1502 | /* This is an implementation of the standard sscanf() function, with the | |
1503 | * following exceptions: | |
1504 | * | |
1505 | * - It returns true if the entire template was successfully scanned and | |
1506 | * converted, false if any conversion failed. | |
1507 | * | |
1508 | * - The standard doesn't define sscanf() behavior when an out-of-range value | |
1509 | * is scanned, e.g. if a "%"PRIi8 conversion scans "-1" or "0x1ff". Some | |
1510 | * implementations consider this an error and stop scanning. This | |
1511 | * implementation never considers an out-of-range value an error; instead, | |
1512 | * it stores the least-significant bits of the converted value in the | |
1513 | * destination, e.g. the value 255 for both examples earlier. | |
1514 | * | |
1515 | * - Only single-byte characters are supported, that is, the 'l' modifier | |
1516 | * on %s, %[, and %c is not supported. The GNU extension 'a' modifier is | |
1517 | * also not supported. | |
1518 | * | |
1519 | * - %p is not supported. | |
1520 | */ | |
1521 | bool | |
1522 | ovs_scan(const char *s, const char *template, ...) | |
1523 | { | |
1524 | const char *const start = s; | |
1525 | bool ok = false; | |
1526 | const char *p; | |
1527 | va_list args; | |
1528 | ||
1529 | va_start(args, template); | |
1530 | p = template; | |
1531 | while (*p != '\0') { | |
1532 | struct scan_spec spec; | |
1533 | unsigned char c = *p++; | |
1534 | bool discard; | |
1535 | ||
1536 | if (isspace(c)) { | |
1537 | s = skip_spaces(s); | |
1538 | continue; | |
1539 | } else if (c != '%') { | |
1540 | if (*s != c) { | |
1541 | goto exit; | |
1542 | } | |
1543 | s++; | |
1544 | continue; | |
1545 | } else if (*p == '%') { | |
1546 | if (*s++ != '%') { | |
1547 | goto exit; | |
1548 | } | |
1549 | p++; | |
1550 | continue; | |
1551 | } | |
1552 | ||
1553 | /* Parse '*' flag. */ | |
1554 | discard = *p == '*'; | |
1555 | p += discard; | |
1556 | ||
1557 | /* Parse field width. */ | |
1558 | spec.width = 0; | |
1559 | while (*p >= '0' && *p <= '9') { | |
1560 | spec.width = spec.width * 10 + (*p++ - '0'); | |
1561 | } | |
1562 | if (spec.width == 0) { | |
1563 | spec.width = UINT_MAX; | |
1564 | } | |
1565 | ||
1566 | /* Parse type modifier. */ | |
1567 | switch (*p) { | |
1568 | case 'h': | |
1569 | if (p[1] == 'h') { | |
1570 | spec.type = SCAN_CHAR; | |
1571 | p += 2; | |
1572 | } else { | |
1573 | spec.type = SCAN_SHORT; | |
1574 | p++; | |
1575 | } | |
1576 | break; | |
1577 | ||
1578 | case 'j': | |
1579 | spec.type = SCAN_INTMAX_T; | |
1580 | p++; | |
1581 | break; | |
1582 | ||
1583 | case 'l': | |
1584 | if (p[1] == 'l') { | |
1585 | spec.type = SCAN_LLONG; | |
1586 | p += 2; | |
1587 | } else { | |
1588 | spec.type = SCAN_LONG; | |
1589 | p++; | |
1590 | } | |
1591 | break; | |
1592 | ||
1593 | case 'L': | |
1594 | case 'q': | |
1595 | spec.type = SCAN_LLONG; | |
1596 | p++; | |
1597 | break; | |
1598 | ||
1599 | case 't': | |
1600 | spec.type = SCAN_PTRDIFF_T; | |
1601 | p++; | |
1602 | break; | |
1603 | ||
1604 | case 'z': | |
1605 | spec.type = SCAN_SIZE_T; | |
1606 | p++; | |
1607 | break; | |
1608 | ||
1609 | default: | |
1610 | spec.type = SCAN_INT; | |
1611 | break; | |
1612 | } | |
1613 | ||
1614 | if (discard) { | |
1615 | spec.type = SCAN_DISCARD; | |
1616 | } | |
1617 | ||
1618 | c = *p++; | |
1619 | if (c != 'c' && c != 'n' && c != '[') { | |
1620 | s = skip_spaces(s); | |
1621 | } | |
1622 | switch (c) { | |
1623 | case 'd': | |
1624 | s = scan_int(s, &spec, 10, &args); | |
1625 | break; | |
1626 | ||
1627 | case 'i': | |
1628 | s = scan_int(s, &spec, 0, &args); | |
1629 | break; | |
1630 | ||
1631 | case 'o': | |
1632 | s = scan_int(s, &spec, 8, &args); | |
1633 | break; | |
1634 | ||
1635 | case 'u': | |
1636 | s = scan_int(s, &spec, 10, &args); | |
1637 | break; | |
1638 | ||
1639 | case 'x': | |
1640 | case 'X': | |
1641 | s = scan_int(s, &spec, 16, &args); | |
1642 | break; | |
1643 | ||
1644 | case 'e': | |
1645 | case 'f': | |
1646 | case 'g': | |
1647 | case 'E': | |
1648 | case 'G': | |
1649 | s = scan_float(s, &spec, &args); | |
1650 | break; | |
1651 | ||
1652 | case 's': | |
1653 | s = scan_string(s, &spec, &args); | |
1654 | break; | |
1655 | ||
1656 | case '[': | |
1657 | s = scan_set(s, &spec, &p, &args); | |
1658 | break; | |
1659 | ||
1660 | case 'c': | |
1661 | s = scan_chars(s, &spec, &args); | |
1662 | break; | |
1663 | ||
1664 | case 'n': | |
1665 | if (spec.type != SCAN_DISCARD) { | |
1666 | *va_arg(args, int *) = s - start; | |
1667 | } | |
1668 | break; | |
1669 | } | |
1670 | ||
1671 | if (!s) { | |
1672 | goto exit; | |
1673 | } | |
1674 | } | |
1675 | ok = true; | |
1676 | ||
1677 | exit: | |
1678 | va_end(args); | |
1679 | return ok; | |
1680 | } | |
1681 |