Branch data Line data Source code
1 : : /*
2 : : * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016 Nicira, Inc.
3 : : *
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:
7 : : *
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.
15 : : */
16 : :
17 : : #include <config.h>
18 : : #include "util.h"
19 : : #include <ctype.h>
20 : : #include <errno.h>
21 : : #include <limits.h>
22 : : #include <pthread.h>
23 : : #include <stdarg.h>
24 : : #include <stdint.h>
25 : : #include <stdio.h>
26 : : #include <stdlib.h>
27 : : #include <string.h>
28 : : #include <sys/stat.h>
29 : : #include <unistd.h>
30 : : #include "bitmap.h"
31 : : #include "byte-order.h"
32 : : #include "coverage.h"
33 : : #include "ovs-rcu.h"
34 : : #include "ovs-thread.h"
35 : : #include "socket-util.h"
36 : : #include "openvswitch/vlog.h"
37 : : #ifdef HAVE_PTHREAD_SET_NAME_NP
38 : : #include <pthread_np.h>
39 : : #endif
40 : :
41 : 53956 : VLOG_DEFINE_THIS_MODULE(util);
42 : :
43 : 1449033873 : COVERAGE_DEFINE(util_xalloc);
44 : :
45 : : /* argv[0] without directory names. */
46 : : char *program_name;
47 : :
48 : : /* Name for the currently running thread or process, for log messages, process
49 : : * listings, and debuggers. */
50 [ - + ]: 5093448 : DEFINE_PER_THREAD_MALLOCED_DATA(char *, subprogram_name);
51 : :
52 : : /* --version option output. */
53 : : static char *program_version;
54 : :
55 : : /* Buffer used by ovs_strerror() and ovs_format_message(). */
56 : 1324 : DEFINE_STATIC_PER_THREAD_DATA(struct { char s[128]; },
57 : : strerror_buffer,
58 : : { "" });
59 : :
60 : : static char *xreadlink(const char *filename);
61 : :
62 : : void
63 : 0 : ovs_assert_failure(const char *where, const char *function,
64 : : const char *condition)
65 : : {
66 : : /* Prevent an infinite loop (or stack overflow) in case VLOG_ABORT happens
67 : : * to trigger an assertion failure of its own. */
68 : : static int reentry = 0;
69 : :
70 [ # # # ]: 0 : switch (reentry++) {
71 : : case 0:
72 : 0 : VLOG_ABORT("%s: assertion %s failed in %s()",
73 : : where, condition, function);
74 : : OVS_NOT_REACHED();
75 : :
76 : : case 1:
77 : 0 : fprintf(stderr, "%s: assertion %s failed in %s()",
78 : : where, condition, function);
79 : 0 : abort();
80 : :
81 : : default:
82 : 0 : abort();
83 : : }
84 : : }
85 : :
86 : : void
87 : 0 : out_of_memory(void)
88 : : {
89 : 0 : ovs_abort(0, "virtual memory exhausted");
90 : : }
91 : :
92 : : void *
93 : 17606566 : xcalloc(size_t count, size_t size)
94 : : {
95 [ + + ][ + + ]: 17606566 : void *p = count && size ? calloc(count, size) : malloc(1);
96 : 17606566 : COVERAGE_INC(util_xalloc);
97 [ - + ]: 17606562 : if (p == NULL) {
98 : 0 : out_of_memory();
99 : : }
100 : 17606562 : return p;
101 : : }
102 : :
103 : : void *
104 : 17601499 : xzalloc(size_t size)
105 : : {
106 : 17601499 : return xcalloc(1, size);
107 : : }
108 : :
109 : : void *
110 : 210906359 : xmalloc(size_t size)
111 : : {
112 [ + + ]: 210906359 : void *p = malloc(size ? size : 1);
113 : 210906359 : COVERAGE_INC(util_xalloc);
114 [ - + ]: 210906377 : if (p == NULL) {
115 : 0 : out_of_memory();
116 : : }
117 : 210906377 : return p;
118 : : }
119 : :
120 : : void *
121 : 11582822 : xrealloc(void *p, size_t size)
122 : : {
123 [ + + ]: 11582822 : p = realloc(p, size ? size : 1);
124 : 11582822 : COVERAGE_INC(util_xalloc);
125 [ - + ]: 11582822 : if (p == NULL) {
126 : 0 : out_of_memory();
127 : : }
128 : 11582822 : return p;
129 : : }
130 : :
131 : : void *
132 : 24383005 : xmemdup(const void *p_, size_t size)
133 : : {
134 : 24383005 : void *p = xmalloc(size);
135 : 24383005 : memcpy(p, p_, size);
136 : 24383005 : return p;
137 : : }
138 : :
139 : : char *
140 : 56302195 : xmemdup0(const char *p_, size_t length)
141 : : {
142 : 56302195 : char *p = xmalloc(length + 1);
143 : 56302195 : memcpy(p, p_, length);
144 : 56302195 : p[length] = '\0';
145 : 56302195 : return p;
146 : : }
147 : :
148 : : char *
149 : 55852549 : xstrdup(const char *s)
150 : : {
151 : 55852549 : return xmemdup0(s, strlen(s));
152 : : }
153 : :
154 : : char * MALLOC_LIKE
155 : 110341 : nullable_xstrdup(const char *s)
156 : : {
157 [ + + ]: 110341 : return s ? xstrdup(s) : NULL;
158 : : }
159 : :
160 : : bool
161 : 99753 : nullable_string_is_equal(const char *a, const char *b)
162 : : {
163 [ + + ][ + + ]: 99753 : return a ? b && !strcmp(a, b) : !b;
[ + + ]
164 : : }
165 : :
166 : : char *
167 : 1930773 : xvasprintf(const char *format, va_list args)
168 : : {
169 : : va_list args2;
170 : : size_t needed;
171 : : char *s;
172 : :
173 : 1930773 : va_copy(args2, args);
174 : 1930773 : needed = vsnprintf(NULL, 0, format, args);
175 : :
176 : 1930773 : s = xmalloc(needed + 1);
177 : :
178 : 1930773 : vsnprintf(s, needed + 1, format, args2);
179 : 1930773 : va_end(args2);
180 : :
181 : 1930773 : return s;
182 : : }
183 : :
184 : : void *
185 : 8167845 : x2nrealloc(void *p, size_t *n, size_t s)
186 : : {
187 [ + + ]: 8167845 : *n = *n == 0 ? 1 : 2 * *n;
188 : 8167845 : return xrealloc(p, *n * s);
189 : : }
190 : :
191 : : /* The desired minimum alignment for an allocated block of memory. */
192 : : #define MEM_ALIGN MAX(sizeof(void *), 8)
193 : : BUILD_ASSERT_DECL(IS_POW2(MEM_ALIGN));
194 : : BUILD_ASSERT_DECL(CACHE_LINE_SIZE >= MEM_ALIGN);
195 : :
196 : : /* Allocates and returns 'size' bytes of memory in dedicated cache lines. That
197 : : * is, the memory block returned will not share a cache line with other data,
198 : : * avoiding "false sharing". (The memory returned will not be at the start of
199 : : * a cache line, though, so don't assume such alignment.)
200 : : *
201 : : * Use free_cacheline() to free the returned memory block. */
202 : : void *
203 : 1386265 : xmalloc_cacheline(size_t size)
204 : : {
205 : : #ifdef HAVE_POSIX_MEMALIGN
206 : : void *p;
207 : : int error;
208 : :
209 : 1386265 : COVERAGE_INC(util_xalloc);
210 [ + - ]: 1386265 : error = posix_memalign(&p, CACHE_LINE_SIZE, size ? size : 1);
211 [ - + ]: 1386265 : if (error != 0) {
212 : 0 : out_of_memory();
213 : : }
214 : 1386265 : return p;
215 : : #else
216 : : void **payload;
217 : : void *base;
218 : :
219 : : /* Allocate room for:
220 : : *
221 : : * - Up to CACHE_LINE_SIZE - 1 bytes before the payload, so that the
222 : : * start of the payload doesn't potentially share a cache line.
223 : : *
224 : : * - A payload consisting of a void *, followed by padding out to
225 : : * MEM_ALIGN bytes, followed by 'size' bytes of user data.
226 : : *
227 : : * - Space following the payload up to the end of the cache line, so
228 : : * that the end of the payload doesn't potentially share a cache line
229 : : * with some following block. */
230 : : base = xmalloc((CACHE_LINE_SIZE - 1)
231 : : + ROUND_UP(MEM_ALIGN + size, CACHE_LINE_SIZE));
232 : :
233 : : /* Locate the payload and store a pointer to the base at the beginning. */
234 : : payload = (void **) ROUND_UP((uintptr_t) base, CACHE_LINE_SIZE);
235 : : *payload = base;
236 : :
237 : : return (char *) payload + MEM_ALIGN;
238 : : #endif
239 : : }
240 : :
241 : : /* Like xmalloc_cacheline() but clears the allocated memory to all zero
242 : : * bytes. */
243 : : void *
244 : 1384755 : xzalloc_cacheline(size_t size)
245 : : {
246 : 1384755 : void *p = xmalloc_cacheline(size);
247 : 1384755 : memset(p, 0, size);
248 : 1384755 : return p;
249 : : }
250 : :
251 : : /* Frees a memory block allocated with xmalloc_cacheline() or
252 : : * xzalloc_cacheline(). */
253 : : void
254 : 1189052 : free_cacheline(void *p)
255 : : {
256 : : #ifdef HAVE_POSIX_MEMALIGN
257 : 1189052 : free(p);
258 : : #else
259 : : if (p) {
260 : : free(*(void **) ((uintptr_t) p - MEM_ALIGN));
261 : : }
262 : : #endif
263 : 1189052 : }
264 : :
265 : : char *
266 : 1006389 : xasprintf(const char *format, ...)
267 : : {
268 : : va_list args;
269 : : char *s;
270 : :
271 : 1006389 : va_start(args, format);
272 : 1006389 : s = xvasprintf(format, args);
273 : 1006389 : va_end(args);
274 : :
275 : 1006389 : return s;
276 : : }
277 : :
278 : : /* Similar to strlcpy() from OpenBSD, but it never reads more than 'size - 1'
279 : : * bytes from 'src' and doesn't return anything. */
280 : : void
281 : 641984 : ovs_strlcpy(char *dst, const char *src, size_t size)
282 : : {
283 [ + ]: 641984 : if (size > 0) {
284 : 641985 : size_t len = strnlen(src, size - 1);
285 : 641989 : memcpy(dst, src, len);
286 : 641989 : dst[len] = '\0';
287 : : }
288 : 641988 : }
289 : :
290 : : /* Copies 'src' to 'dst'. Reads no more than 'size - 1' bytes from 'src'.
291 : : * Always null-terminates 'dst' (if 'size' is nonzero), and writes a zero byte
292 : : * to every otherwise unused byte in 'dst'.
293 : : *
294 : : * Except for performance, the following call:
295 : : * ovs_strzcpy(dst, src, size);
296 : : * is equivalent to these two calls:
297 : : * memset(dst, '\0', size);
298 : : * ovs_strlcpy(dst, src, size);
299 : : *
300 : : * (Thus, ovs_strzcpy() is similar to strncpy() without some of the pitfalls.)
301 : : */
302 : : void
303 : 61278 : ovs_strzcpy(char *dst, const char *src, size_t size)
304 : : {
305 [ + - ]: 61278 : if (size > 0) {
306 : 61278 : size_t len = strnlen(src, size - 1);
307 : 61278 : memcpy(dst, src, len);
308 : 61278 : memset(dst + len, '\0', size - len);
309 : : }
310 : 61278 : }
311 : :
312 : : /* Prints 'format' on stderr, formatting it like printf() does. If 'err_no' is
313 : : * nonzero, then it is formatted with ovs_retval_to_string() and appended to
314 : : * the message inside parentheses. Then, terminates with abort().
315 : : *
316 : : * This function is preferred to ovs_fatal() in a situation where it would make
317 : : * sense for a monitoring process to restart the daemon.
318 : : *
319 : : * 'format' should not end with a new-line, because this function will add one
320 : : * itself. */
321 : : void
322 : 0 : ovs_abort(int err_no, const char *format, ...)
323 : : {
324 : : va_list args;
325 : :
326 : 0 : va_start(args, format);
327 : 0 : ovs_abort_valist(err_no, format, args);
328 : : }
329 : :
330 : : /* Same as ovs_abort() except that the arguments are supplied as a va_list. */
331 : : void
332 : 0 : ovs_abort_valist(int err_no, const char *format, va_list args)
333 : : {
334 : 0 : ovs_error_valist(err_no, format, args);
335 : 0 : abort();
336 : : }
337 : :
338 : : /* Prints 'format' on stderr, formatting it like printf() does. If 'err_no' is
339 : : * nonzero, then it is formatted with ovs_retval_to_string() and appended to
340 : : * the message inside parentheses. Then, terminates with EXIT_FAILURE.
341 : : *
342 : : * 'format' should not end with a new-line, because this function will add one
343 : : * itself. */
344 : : void
345 : 193 : ovs_fatal(int err_no, const char *format, ...)
346 : : {
347 : : va_list args;
348 : :
349 : 193 : va_start(args, format);
350 : 193 : ovs_fatal_valist(err_no, format, args);
351 : : }
352 : :
353 : : /* Same as ovs_fatal() except that the arguments are supplied as a va_list. */
354 : : void
355 : 218 : ovs_fatal_valist(int err_no, const char *format, va_list args)
356 : : {
357 : 218 : ovs_error_valist(err_no, format, args);
358 : 218 : exit(EXIT_FAILURE);
359 : : }
360 : :
361 : : /* Prints 'format' on stderr, formatting it like printf() does. If 'err_no' is
362 : : * nonzero, then it is formatted with ovs_retval_to_string() and appended to
363 : : * the message inside parentheses.
364 : : *
365 : : * 'format' should not end with a new-line, because this function will add one
366 : : * itself. */
367 : : void
368 : 275 : ovs_error(int err_no, const char *format, ...)
369 : : {
370 : : va_list args;
371 : :
372 : 275 : va_start(args, format);
373 : 275 : ovs_error_valist(err_no, format, args);
374 : 275 : va_end(args);
375 : 275 : }
376 : :
377 : : /* Same as ovs_error() except that the arguments are supplied as a va_list. */
378 : : void
379 : 493 : ovs_error_valist(int err_no, const char *format, va_list args)
380 : : {
381 : 493 : const char *subprogram_name = get_subprogram_name();
382 : 493 : int save_errno = errno;
383 : :
384 [ - + ]: 493 : if (subprogram_name[0]) {
385 : 0 : fprintf(stderr, "%s(%s): ", program_name, subprogram_name);
386 : : } else {
387 : 493 : fprintf(stderr, "%s: ", program_name);
388 : : }
389 : :
390 : 493 : vfprintf(stderr, format, args);
391 [ + + ]: 493 : if (err_no != 0) {
392 : 81 : fprintf(stderr, " (%s)", ovs_retval_to_string(err_no));
393 : : }
394 : 493 : putc('\n', stderr);
395 : :
396 : 493 : errno = save_errno;
397 : 493 : }
398 : :
399 : : /* Many OVS functions return an int which is one of:
400 : : * - 0: no error yet
401 : : * - >0: errno value
402 : : * - EOF: end of file (not necessarily an error; depends on the function called)
403 : : *
404 : : * Returns the appropriate human-readable string. The caller must copy the
405 : : * string if it wants to hold onto it, as the storage may be overwritten on
406 : : * subsequent function calls.
407 : : */
408 : : const char *
409 : 96 : ovs_retval_to_string(int retval)
410 : : {
411 : 96 : return (!retval ? ""
412 [ + - ]: 192 : : retval == EOF ? "End of file"
413 [ + + ]: 96 : : ovs_strerror(retval));
414 : : }
415 : :
416 : : /* This function returns the string describing the error number in 'error'
417 : : * for POSIX platforms. For Windows, this function can be used for C library
418 : : * calls. For socket calls that are also used in Windows, use sock_strerror()
419 : : * instead. For WINAPI calls, look at ovs_lasterror_to_string(). */
420 : : const char *
421 : 24773 : ovs_strerror(int error)
422 : : {
423 : : enum { BUFSIZE = sizeof strerror_buffer_get()->s };
424 : : int save_errno;
425 : : char *buffer;
426 : : char *s;
427 : :
428 [ + + ]: 24773 : if (error == 0) {
429 : : /*
430 : : * strerror(0) varies among platforms:
431 : : *
432 : : * Success
433 : : * No error
434 : : * Undefined error: 0
435 : : *
436 : : * We want to provide a consistent result here because
437 : : * our testsuite has test cases which strictly matches
438 : : * log messages containing this string.
439 : : */
440 : 24442 : return "Success";
441 : : }
442 : :
443 : 331 : save_errno = errno;
444 : 331 : buffer = strerror_buffer_get()->s;
445 : :
446 : : #if STRERROR_R_CHAR_P
447 : : /* GNU style strerror_r() might return an immutable static string, or it
448 : : * might write and return 'buffer', but in either case we can pass the
449 : : * returned string directly to the caller. */
450 : 331 : s = strerror_r(error, buffer, BUFSIZE);
451 : : #else /* strerror_r() returns an int. */
452 : : s = buffer;
453 : : if (strerror_r(error, buffer, BUFSIZE)) {
454 : : /* strerror_r() is only allowed to fail on ERANGE (because the buffer
455 : : * is too short). We don't check the actual failure reason because
456 : : * POSIX requires strerror_r() to return the error but old glibc
457 : : * (before 2.13) returns -1 and sets errno. */
458 : : snprintf(buffer, BUFSIZE, "Unknown error %d", error);
459 : : }
460 : : #endif
461 : :
462 : 331 : errno = save_errno;
463 : :
464 : 331 : return s;
465 : : }
466 : :
467 : : /* Sets global "program_name" and "program_version" variables. Should
468 : : * be called at the beginning of main() with "argv[0]" as the argument
469 : : * to 'argv0'.
470 : : *
471 : : * 'version' should contain the version of the caller's program. If 'version'
472 : : * is the same as the VERSION #define, the caller is assumed to be part of Open
473 : : * vSwitch. Otherwise, it is assumed to be an external program linking against
474 : : * the Open vSwitch libraries.
475 : : *
476 : : */
477 : : void
478 : 27298 : ovs_set_program_name(const char *argv0, const char *version)
479 : : {
480 : : char *basename;
481 : : #ifdef _WIN32
482 : : size_t max_len = strlen(argv0) + 1;
483 : :
484 : : SetErrorMode(GetErrorMode() | SEM_NOGPFAULTERRORBOX);
485 : : _set_output_format(_TWO_DIGIT_EXPONENT);
486 : :
487 : : basename = xmalloc(max_len);
488 : : _splitpath_s(argv0, NULL, 0, NULL, 0, basename, max_len, NULL, 0);
489 : : #else
490 : 27298 : const char *slash = strrchr(argv0, '/');
491 [ + + ]: 27298 : basename = xstrdup(slash ? slash + 1 : argv0);
492 : : #endif
493 : :
494 : 27298 : assert_single_threaded();
495 : 27298 : free(program_name);
496 : : /* Remove libtool prefix, if it is there */
497 [ - + ]: 27298 : if (strncmp(basename, "lt-", 3) == 0) {
498 : 0 : char *tmp_name = basename;
499 : 0 : basename = xstrdup(basename + 3);
500 : 0 : free(tmp_name);
501 : : }
502 : 27298 : program_name = basename;
503 : :
504 : 27298 : free(program_version);
505 [ + + ]: 27298 : if (!strcmp(version, VERSION)) {
506 : 27297 : program_version = xasprintf("%s (Open vSwitch) "VERSION"\n",
507 : : program_name);
508 : : } else {
509 : 1 : program_version = xasprintf("%s %s\n"
510 : : "Open vSwitch Library "VERSION"\n",
511 : : program_name, version);
512 : : }
513 : 27298 : }
514 : :
515 : : /* Returns the name of the currently running thread or process. */
516 : : const char *
517 : 836282 : get_subprogram_name(void)
518 : : {
519 : 836282 : const char *name = subprogram_name_get();
520 [ + + ]: 836270 : return name ? name : "";
521 : : }
522 : :
523 : : /* Sets 'subprogram_name' as the name of the currently running thread or
524 : : * process. (This appears in log messages and may also be visible in system
525 : : * process listings and debuggers.) */
526 : : void
527 : 5792 : set_subprogram_name(const char *subprogram_name)
528 : : {
529 [ + - ]: 5792 : char *pname = xstrdup(subprogram_name ? subprogram_name : program_name);
530 : 5793 : free(subprogram_name_set(pname));
531 : :
532 : : #if HAVE_GLIBC_PTHREAD_SETNAME_NP
533 : 5793 : pthread_setname_np(pthread_self(), pname);
534 : : #elif HAVE_NETBSD_PTHREAD_SETNAME_NP
535 : : pthread_setname_np(pthread_self(), "%s", pname);
536 : : #elif HAVE_PTHREAD_SET_NAME_NP
537 : : pthread_set_name_np(pthread_self(), pname);
538 : : #endif
539 : 5792 : }
540 : :
541 : : /* Returns a pointer to a string describing the program version. The
542 : : * caller must not modify or free the returned string.
543 : : */
544 : : const char *
545 : 5 : ovs_get_program_version(void)
546 : : {
547 : 5 : return program_version;
548 : : }
549 : :
550 : : /* Returns a pointer to a string describing the program name. The
551 : : * caller must not modify or free the returned string.
552 : : */
553 : : const char *
554 : 0 : ovs_get_program_name(void)
555 : : {
556 : 0 : return program_name;
557 : : }
558 : :
559 : : /* Print the version information for the program. */
560 : : void
561 : 12 : ovs_print_version(uint8_t min_ofp, uint8_t max_ofp)
562 : : {
563 : 12 : printf("%s", program_version);
564 [ + - ][ - + ]: 12 : if (min_ofp || max_ofp) {
565 : 0 : printf("OpenFlow versions %#x:%#x\n", min_ofp, max_ofp);
566 : : }
567 : 12 : }
568 : :
569 : : /* Writes the 'size' bytes in 'buf' to 'stream' as hex bytes arranged 16 per
570 : : * line. Numeric offsets are also included, starting at 'ofs' for the first
571 : : * byte in 'buf'. If 'ascii' is true then the corresponding ASCII characters
572 : : * are also rendered alongside. */
573 : : void
574 : 28 : ovs_hex_dump(FILE *stream, const void *buf_, size_t size,
575 : : uintptr_t ofs, bool ascii)
576 : : {
577 : 28 : const uint8_t *buf = buf_;
578 : 28 : const size_t per_line = 16; /* Maximum bytes per line. */
579 : :
580 [ + + ]: 69 : while (size > 0)
581 : : {
582 : : size_t start, end, n;
583 : : size_t i;
584 : :
585 : : /* Number of bytes on this line. */
586 : 41 : start = ofs % per_line;
587 : 41 : end = per_line;
588 [ + + ]: 41 : if (end - start > size)
589 : 18 : end = start + size;
590 : 41 : n = end - start;
591 : :
592 : : /* Print line. */
593 : 41 : fprintf(stream, "%08"PRIxMAX" ", (uintmax_t) ROUND_DOWN(ofs, per_line));
594 [ - + ]: 41 : for (i = 0; i < start; i++)
595 : 0 : fprintf(stream, " ");
596 [ + + ]: 569 : for (; i < end; i++)
597 [ + + ]: 528 : fprintf(stream, "%02x%c",
598 : 1056 : buf[i - start], i == per_line / 2 - 1? '-' : ' ');
599 [ - + ]: 41 : if (ascii)
600 : : {
601 [ # # ]: 0 : for (; i < per_line; i++)
602 : 0 : fprintf(stream, " ");
603 : 0 : fprintf(stream, "|");
604 [ # # ]: 0 : for (i = 0; i < start; i++)
605 : 0 : fprintf(stream, " ");
606 [ # # ]: 0 : for (; i < end; i++) {
607 : 0 : int c = buf[i - start];
608 [ # # ][ # # ]: 0 : putc(c >= 32 && c < 127 ? c : '.', stream);
609 : : }
610 [ # # ]: 0 : for (; i < per_line; i++)
611 : 0 : fprintf(stream, " ");
612 : 0 : fprintf(stream, "|");
613 : : }
614 : 41 : fprintf(stream, "\n");
615 : :
616 : 41 : ofs += n;
617 : 41 : buf += n;
618 : 41 : size -= n;
619 : : }
620 : 28 : }
621 : :
622 : : bool
623 : 3336 : str_to_int(const char *s, int base, int *i)
624 : : {
625 : : long long ll;
626 : 3336 : bool ok = str_to_llong(s, base, &ll);
627 : 3336 : *i = ll;
628 : 3336 : return ok;
629 : : }
630 : :
631 : : bool
632 : 0 : str_to_long(const char *s, int base, long *li)
633 : : {
634 : : long long ll;
635 : 0 : bool ok = str_to_llong(s, base, &ll);
636 : 0 : *li = ll;
637 : 0 : return ok;
638 : : }
639 : :
640 : : bool
641 : 13799 : str_to_llong(const char *s, int base, long long *x)
642 : : {
643 : 13799 : int save_errno = errno;
644 : : char *tail;
645 : 13799 : errno = 0;
646 : 13799 : *x = strtoll(s, &tail, base);
647 [ + - ][ + - ]: 13799 : if (errno == EINVAL || errno == ERANGE || tail == s || *tail != '\0') {
[ + + ][ + + ]
648 : 608 : errno = save_errno;
649 : 608 : *x = 0;
650 : 608 : return false;
651 : : } else {
652 : 13191 : errno = save_errno;
653 : 13799 : return true;
654 : : }
655 : : }
656 : :
657 : : bool
658 : 8452 : str_to_uint(const char *s, int base, unsigned int *u)
659 : : {
660 : : long long ll;
661 : 8452 : bool ok = str_to_llong(s, base, &ll);
662 [ + + ][ + - ]: 8452 : if (!ok || ll < 0 || ll > UINT_MAX) {
[ - + ]
663 : 570 : *u = 0;
664 : 570 : return false;
665 : : } else {
666 : 7882 : *u = ll;
667 : 8452 : return true;
668 : : }
669 : : }
670 : :
671 : : /* Converts floating-point string 's' into a double. If successful, stores
672 : : * the double in '*d' and returns true; on failure, stores 0 in '*d' and
673 : : * returns false.
674 : : *
675 : : * Underflow (e.g. "1e-9999") is not considered an error, but overflow
676 : : * (e.g. "1e9999)" is. */
677 : : bool
678 : 262 : str_to_double(const char *s, double *d)
679 : : {
680 : 262 : int save_errno = errno;
681 : : char *tail;
682 : 262 : errno = 0;
683 : 262 : *d = strtod(s, &tail);
684 [ + - ][ + + ]: 262 : if (errno == EINVAL || (errno == ERANGE && *d != 0)
[ + + ]
685 [ + - ][ - + ]: 260 : || tail == s || *tail != '\0') {
686 : 2 : errno = save_errno;
687 : 2 : *d = 0;
688 : 2 : return false;
689 : : } else {
690 : 260 : errno = save_errno;
691 : 262 : return true;
692 : : }
693 : : }
694 : :
695 : : /* Returns the value of 'c' as a hexadecimal digit. */
696 : : int
697 : 43557681 : hexit_value(int c)
698 : : {
699 [ + + + + : 43557681 : switch (c) {
+ + + + ]
700 : : case '0': case '1': case '2': case '3': case '4':
701 : : case '5': case '6': case '7': case '8': case '9':
702 : 28065665 : return c - '0';
703 : :
704 : : case 'a': case 'A':
705 : 1466903 : return 0xa;
706 : :
707 : : case 'b': case 'B':
708 : 1630850 : return 0xb;
709 : :
710 : : case 'c': case 'C':
711 : 1418852 : return 0xc;
712 : :
713 : : case 'd': case 'D':
714 : 3943881 : return 0xd;
715 : :
716 : : case 'e': case 'E':
717 : 1436607 : return 0xe;
718 : :
719 : : case 'f': case 'F':
720 : 2102064 : return 0xf;
721 : :
722 : : default:
723 : 3492859 : return -1;
724 : : }
725 : : }
726 : :
727 : : /* Returns the integer value of the 'n' hexadecimal digits starting at 's', or
728 : : * UINTMAX_MAX if one of those "digits" is not really a hex digit. Sets '*ok'
729 : : * to true if the conversion succeeds or to false if a non-hex digit is
730 : : * detected. */
731 : : uintmax_t
732 : 4981883 : hexits_value(const char *s, size_t n, bool *ok)
733 : : {
734 : : uintmax_t value;
735 : : size_t i;
736 : :
737 : 4981883 : value = 0;
738 [ + + ]: 29944864 : for (i = 0; i < n; i++) {
739 : 25090859 : int hexit = hexit_value(s[i]);
740 [ + + ]: 25090859 : if (hexit < 0) {
741 : 127878 : *ok = false;
742 : 127878 : return UINTMAX_MAX;
743 : : }
744 : 24962981 : value = (value << 4) + hexit;
745 : : }
746 : 4854005 : *ok = true;
747 : 4854005 : return value;
748 : : }
749 : :
750 : : /* Parses the string in 's' as an integer in either hex or decimal format and
751 : : * puts the result right justified in the array 'valuep' that is 'field_width'
752 : : * big. If the string is in hex format, the value may be arbitrarily large;
753 : : * integers are limited to 64-bit values. (The rationale is that decimal is
754 : : * likely to represent a number and 64 bits is a reasonable maximum whereas
755 : : * hex could either be a number or a byte string.)
756 : : *
757 : : * On return 'tail' points to the first character in the string that was
758 : : * not parsed as part of the value. ERANGE is returned if the value is too
759 : : * large to fit in the given field. */
760 : : int
761 : 22113 : parse_int_string(const char *s, uint8_t *valuep, int field_width, char **tail)
762 : : {
763 : : unsigned long long int integer;
764 : : int i;
765 : :
766 [ + + ][ - + ]: 22113 : if (!strncmp(s, "0x", 2) || !strncmp(s, "0X", 2)) {
767 : : uint8_t *hexit_str;
768 : 1079 : int len = 0;
769 : : int val_idx;
770 : 1079 : int err = 0;
771 : :
772 : 1079 : s += 2;
773 : 1079 : hexit_str = xmalloc(field_width * 2);
774 : :
775 : : for (;;) {
776 : : uint8_t hexit;
777 : : bool ok;
778 : :
779 : 7761 : s += strspn(s, " \t\r\n");
780 : 7761 : hexit = hexits_value(s, 1, &ok);
781 [ + + ]: 7761 : if (!ok) {
782 : 1079 : *tail = CONST_CAST(char *, s);
783 : 1079 : break;
784 : : }
785 : :
786 [ + + ][ + + ]: 6682 : if (hexit != 0 || len) {
787 [ - + ]: 6071 : if (DIV_ROUND_UP(len + 1, 2) > field_width) {
788 : 0 : err = ERANGE;
789 : 0 : goto free;
790 : : }
791 : :
792 : 6071 : hexit_str[len] = hexit;
793 : 6071 : len++;
794 : : }
795 : 6682 : s++;
796 : 6682 : }
797 : :
798 : 1079 : val_idx = field_width;
799 [ + + ]: 4517 : for (i = len - 1; i >= 0; i -= 2) {
800 : 3438 : val_idx--;
801 : 3438 : valuep[val_idx] = hexit_str[i];
802 [ + + ]: 3438 : if (i > 0) {
803 : 2633 : valuep[val_idx] += hexit_str[i - 1] << 4;
804 : : }
805 : : }
806 : :
807 : 1079 : memset(valuep, 0, val_idx);
808 : :
809 : : free:
810 : 1079 : free(hexit_str);
811 : 1079 : return err;
812 : : }
813 : :
814 : 21034 : errno = 0;
815 : 21034 : integer = strtoull(s, tail, 0);
816 [ - + ]: 21034 : if (errno) {
817 : 0 : return errno;
818 : : }
819 : :
820 [ + + ]: 94379 : for (i = field_width - 1; i >= 0; i--) {
821 : 73345 : valuep[i] = integer;
822 : 73345 : integer >>= 8;
823 : : }
824 [ - + ]: 21034 : if (integer) {
825 : 0 : return ERANGE;
826 : : }
827 : :
828 : 21034 : return 0;
829 : : }
830 : :
831 : : /* Returns the current working directory as a malloc()'d string, or a null
832 : : * pointer if the current working directory cannot be determined. */
833 : : char *
834 : 827 : get_cwd(void)
835 : : {
836 : : long int path_max;
837 : : size_t size;
838 : :
839 : : /* Get maximum path length or at least a reasonable estimate. */
840 : : #ifndef _WIN32
841 : 827 : path_max = pathconf(".", _PC_PATH_MAX);
842 : : #else
843 : : path_max = MAX_PATH;
844 : : #endif
845 [ + - ]: 827 : size = (path_max < 0 ? 1024
846 : : : path_max > 10240 ? 10240
847 : 827 : : path_max);
848 : :
849 : : /* Get current working directory. */
850 : : for (;;) {
851 : 827 : char *buf = xmalloc(size);
852 [ + - ]: 827 : if (getcwd(buf, size)) {
853 : 827 : return xrealloc(buf, strlen(buf) + 1);
854 : : } else {
855 : 0 : int error = errno;
856 : 0 : free(buf);
857 [ # # ]: 0 : if (error != ERANGE) {
858 [ # # ]: 0 : VLOG_WARN("getcwd failed (%s)", ovs_strerror(error));
859 : 0 : return NULL;
860 : : }
861 : 0 : size *= 2;
862 : : }
863 : 0 : }
864 : : }
865 : :
866 : : static char *
867 : 1201 : all_slashes_name(const char *s)
868 : : {
869 [ + + ][ + + ]: 1201 : return xstrdup(s[0] == '/' && s[1] == '/' && s[2] != '/' ? "//"
[ + + ][ + + ]
870 : 1198 : : s[0] == '/' ? "/"
871 : : : ".");
872 : : }
873 : :
874 : : #ifndef _WIN32
875 : : /* Returns the directory name portion of 'file_name' as a malloc()'d string,
876 : : * similar to the POSIX dirname() function but thread-safe. */
877 : : char *
878 : 5329 : dir_name(const char *file_name)
879 : : {
880 : 5329 : size_t len = strlen(file_name);
881 [ + + ][ + + ]: 5336 : while (len > 0 && file_name[len - 1] == '/') {
882 : 7 : len--;
883 : : }
884 [ + + ][ + + ]: 97740 : while (len > 0 && file_name[len - 1] != '/') {
885 : 92411 : len--;
886 : : }
887 [ + + ][ + + ]: 9466 : while (len > 0 && file_name[len - 1] == '/') {
888 : 4137 : len--;
889 : : }
890 [ + + ]: 5329 : return len ? xmemdup0(file_name, len) : all_slashes_name(file_name);
891 : : }
892 : :
893 : : /* Returns the file name portion of 'file_name' as a malloc()'d string,
894 : : * similar to the POSIX basename() function but thread-safe. */
895 : : char *
896 : 3988 : base_name(const char *file_name)
897 : : {
898 : : size_t end, start;
899 : :
900 : 3988 : end = strlen(file_name);
901 [ + + ][ + + ]: 3995 : while (end > 0 && file_name[end - 1] == '/') {
902 : 7 : end--;
903 : : }
904 : :
905 [ + + ]: 3988 : if (!end) {
906 : 3 : return all_slashes_name(file_name);
907 : : }
908 : :
909 : 3985 : start = end;
910 [ + + ][ + + ]: 89657 : while (start > 0 && file_name[start - 1] != '/') {
911 : 85672 : start--;
912 : : }
913 : :
914 : 3985 : return xmemdup0(file_name + start, end - start);
915 : : }
916 : : #endif /* _WIN32 */
917 : :
918 : : /* If 'file_name' starts with '/', returns a copy of 'file_name'. Otherwise,
919 : : * returns an absolute path to 'file_name' considering it relative to 'dir',
920 : : * which itself must be absolute. 'dir' may be null or the empty string, in
921 : : * which case the current working directory is used.
922 : : *
923 : : * Additionally on Windows, if 'file_name' has a ':', returns a copy of
924 : : * 'file_name'
925 : : *
926 : : * Returns a null pointer if 'dir' is null and getcwd() fails. */
927 : : char *
928 : 44203 : abs_file_name(const char *dir, const char *file_name)
929 : : {
930 [ + + ]: 44203 : if (file_name[0] == '/') {
931 : 37895 : return xstrdup(file_name);
932 : : #ifdef _WIN32
933 : : } else if (strchr(file_name, ':')) {
934 : : return xstrdup(file_name);
935 : : #endif
936 [ + + ][ + - ]: 6308 : } else if (dir && dir[0]) {
937 [ - + ]: 5481 : char *separator = dir[strlen(dir) - 1] == '/' ? "" : "/";
938 : 5481 : return xasprintf("%s%s%s", dir, separator, file_name);
939 : : } else {
940 : 827 : char *cwd = get_cwd();
941 [ + - ]: 827 : if (cwd) {
942 : 827 : char *abs_name = xasprintf("%s/%s", cwd, file_name);
943 : 827 : free(cwd);
944 : 827 : return abs_name;
945 : : } else {
946 : 0 : return NULL;
947 : : }
948 : : }
949 : : }
950 : :
951 : : /* Like readlink(), but returns the link name as a null-terminated string in
952 : : * allocated memory that the caller must eventually free (with free()).
953 : : * Returns NULL on error, in which case errno is set appropriately. */
954 : : static char *
955 : 71 : xreadlink(const char *filename)
956 : : {
957 : : size_t size;
958 : :
959 : 71 : for (size = 64; ; size *= 2) {
960 : 76 : char *buf = xmalloc(size);
961 : 76 : ssize_t retval = readlink(filename, buf, size);
962 : 76 : int error = errno;
963 : :
964 [ + - ][ + + ]: 76 : if (retval >= 0 && retval < size) {
965 : 71 : buf[retval] = '\0';
966 : 71 : return buf;
967 : : }
968 : :
969 : 5 : free(buf);
970 [ - + ]: 5 : if (retval < 0) {
971 : 0 : errno = error;
972 : 0 : return NULL;
973 : : }
974 : 5 : }
975 : : }
976 : :
977 : : /* Returns a version of 'filename' with symlinks in the final component
978 : : * dereferenced. This differs from realpath() in that:
979 : : *
980 : : * - 'filename' need not exist.
981 : : *
982 : : * - If 'filename' does exist as a symlink, its referent need not exist.
983 : : *
984 : : * - Only symlinks in the final component of 'filename' are dereferenced.
985 : : *
986 : : * For Windows platform, this function returns a string that has the same
987 : : * value as the passed string.
988 : : *
989 : : * The caller must eventually free the returned string (with free()). */
990 : : char *
991 : 4363 : follow_symlinks(const char *filename)
992 : : {
993 : : #ifndef _WIN32
994 : : struct stat s;
995 : : char *fn;
996 : : int i;
997 : :
998 : 4363 : fn = xstrdup(filename);
999 [ + + ]: 4434 : for (i = 0; i < 10; i++) {
1000 : : char *linkname;
1001 : : char *next_fn;
1002 : :
1003 [ + + ][ + + ]: 4433 : if (lstat(fn, &s) != 0 || !S_ISLNK(s.st_mode)) {
1004 : 4362 : return fn;
1005 : : }
1006 : :
1007 : 71 : linkname = xreadlink(fn);
1008 [ - + ]: 71 : if (!linkname) {
1009 [ # # ]: 0 : VLOG_WARN("%s: readlink failed (%s)",
1010 : : filename, ovs_strerror(errno));
1011 : 0 : return fn;
1012 : : }
1013 : :
1014 [ + + ]: 71 : if (linkname[0] == '/') {
1015 : : /* Target of symlink is absolute so use it raw. */
1016 : 7 : next_fn = linkname;
1017 : : } else {
1018 : : /* Target of symlink is relative so add to 'fn''s directory. */
1019 : 64 : char *dir = dir_name(fn);
1020 : :
1021 [ + + ]: 64 : if (!strcmp(dir, ".")) {
1022 : 61 : next_fn = linkname;
1023 : : } else {
1024 [ - + ]: 3 : char *separator = dir[strlen(dir) - 1] == '/' ? "" : "/";
1025 : 3 : next_fn = xasprintf("%s%s%s", dir, separator, linkname);
1026 : 3 : free(linkname);
1027 : : }
1028 : :
1029 : 64 : free(dir);
1030 : : }
1031 : :
1032 : 71 : free(fn);
1033 : 71 : fn = next_fn;
1034 : : }
1035 : :
1036 [ + - ]: 1 : VLOG_WARN("%s: too many levels of symlinks", filename);
1037 : 1 : free(fn);
1038 : : #endif
1039 : 4363 : return xstrdup(filename);
1040 : : }
1041 : :
1042 : : /* Pass a value to this function if it is marked with
1043 : : * __attribute__((warn_unused_result)) and you genuinely want to ignore
1044 : : * its return value. (Note that every scalar type can be implicitly
1045 : : * converted to bool.) */
1046 : 212235 : void ignore(bool x OVS_UNUSED) { }
1047 : :
1048 : : /* Returns an appropriate delimiter for inserting just before the 0-based item
1049 : : * 'index' in a list that has 'total' items in it. */
1050 : : const char *
1051 : 56 : english_list_delimiter(size_t index, size_t total)
1052 : : {
1053 : 56 : return (index == 0 ? ""
1054 [ - + ][ # # ]: 56 : : index < total - 1 ? ", "
[ # # ]
1055 : : : total > 2 ? ", and "
1056 : : : " and ");
1057 : : }
1058 : :
1059 : : /* Returns the number of trailing 0-bits in 'n'. Undefined if 'n' == 0. */
1060 : : #if __GNUC__ >= 4 || _MSC_VER
1061 : : /* Defined inline in util.h. */
1062 : : #else
1063 : : /* Returns the number of trailing 0-bits in 'n'. Undefined if 'n' == 0. */
1064 : : int
1065 : : raw_ctz(uint64_t n)
1066 : : {
1067 : : uint64_t k;
1068 : : int count = 63;
1069 : :
1070 : : #define CTZ_STEP(X) \
1071 : : k = n << (X); \
1072 : : if (k) { \
1073 : : count -= X; \
1074 : : n = k; \
1075 : : }
1076 : : CTZ_STEP(32);
1077 : : CTZ_STEP(16);
1078 : : CTZ_STEP(8);
1079 : : CTZ_STEP(4);
1080 : : CTZ_STEP(2);
1081 : : CTZ_STEP(1);
1082 : : #undef CTZ_STEP
1083 : :
1084 : : return count;
1085 : : }
1086 : :
1087 : : /* Returns the number of leading 0-bits in 'n'. Undefined if 'n' == 0. */
1088 : : int
1089 : : raw_clz64(uint64_t n)
1090 : : {
1091 : : uint64_t k;
1092 : : int count = 63;
1093 : :
1094 : : #define CLZ_STEP(X) \
1095 : : k = n >> (X); \
1096 : : if (k) { \
1097 : : count -= X; \
1098 : : n = k; \
1099 : : }
1100 : : CLZ_STEP(32);
1101 : : CLZ_STEP(16);
1102 : : CLZ_STEP(8);
1103 : : CLZ_STEP(4);
1104 : : CLZ_STEP(2);
1105 : : CLZ_STEP(1);
1106 : : #undef CLZ_STEP
1107 : :
1108 : : return count;
1109 : : }
1110 : : #endif
1111 : :
1112 : : #if NEED_COUNT_1BITS_8
1113 : : #define INIT1(X) \
1114 : : ((((X) & (1 << 0)) != 0) + \
1115 : : (((X) & (1 << 1)) != 0) + \
1116 : : (((X) & (1 << 2)) != 0) + \
1117 : : (((X) & (1 << 3)) != 0) + \
1118 : : (((X) & (1 << 4)) != 0) + \
1119 : : (((X) & (1 << 5)) != 0) + \
1120 : : (((X) & (1 << 6)) != 0) + \
1121 : : (((X) & (1 << 7)) != 0))
1122 : : #define INIT2(X) INIT1(X), INIT1((X) + 1)
1123 : : #define INIT4(X) INIT2(X), INIT2((X) + 2)
1124 : : #define INIT8(X) INIT4(X), INIT4((X) + 4)
1125 : : #define INIT16(X) INIT8(X), INIT8((X) + 8)
1126 : : #define INIT32(X) INIT16(X), INIT16((X) + 16)
1127 : : #define INIT64(X) INIT32(X), INIT32((X) + 32)
1128 : :
1129 : : const uint8_t count_1bits_8[256] = {
1130 : : INIT64(0), INIT64(64), INIT64(128), INIT64(192)
1131 : : };
1132 : : #endif
1133 : :
1134 : : /* Returns true if the 'n' bytes starting at 'p' are zeros. */
1135 : : bool
1136 : 3591792 : is_all_zeros(const void *p_, size_t n)
1137 : : {
1138 : 3591792 : const uint8_t *p = p_;
1139 : : size_t i;
1140 : :
1141 [ + + ]: 114119717 : for (i = 0; i < n; i++) {
1142 [ + + ]: 112215333 : if (p[i] != 0x00) {
1143 : 1687408 : return false;
1144 : : }
1145 : : }
1146 : 1904384 : return true;
1147 : : }
1148 : :
1149 : : /* Returns true if the 'n' bytes starting at 'p' are 0xff. */
1150 : : bool
1151 : 2584352 : is_all_ones(const void *p_, size_t n)
1152 : : {
1153 : 2584352 : const uint8_t *p = p_;
1154 : : size_t i;
1155 : :
1156 [ + + ]: 11547695 : for (i = 0; i < n; i++) {
1157 [ + + ]: 9672158 : if (p[i] != 0xff) {
1158 : 708815 : return false;
1159 : : }
1160 : : }
1161 : 1875537 : return true;
1162 : : }
1163 : :
1164 : : /* Copies 'n_bits' bits starting from bit 'src_ofs' in 'src' to the 'n_bits'
1165 : : * starting from bit 'dst_ofs' in 'dst'. 'src' is 'src_len' bytes long and
1166 : : * 'dst' is 'dst_len' bytes long.
1167 : : *
1168 : : * If you consider all of 'src' to be a single unsigned integer in network byte
1169 : : * order, then bit N is the bit with value 2**N. That is, bit 0 is the bit
1170 : : * with value 1 in src[src_len - 1], bit 1 is the bit with value 2, bit 2 is
1171 : : * the bit with value 4, ..., bit 8 is the bit with value 1 in src[src_len -
1172 : : * 2], and so on. Similarly for 'dst'.
1173 : : *
1174 : : * Required invariants:
1175 : : * src_ofs + n_bits <= src_len * 8
1176 : : * dst_ofs + n_bits <= dst_len * 8
1177 : : * 'src' and 'dst' must not overlap.
1178 : : */
1179 : : void
1180 : 127369390 : bitwise_copy(const void *src_, unsigned int src_len, unsigned int src_ofs,
1181 : : void *dst_, unsigned int dst_len, unsigned int dst_ofs,
1182 : : unsigned int n_bits)
1183 : : {
1184 : 127369390 : const uint8_t *src = src_;
1185 : 127369390 : uint8_t *dst = dst_;
1186 : :
1187 : 127369390 : src += src_len - (src_ofs / 8 + 1);
1188 : 127369390 : src_ofs %= 8;
1189 : :
1190 : 127369390 : dst += dst_len - (dst_ofs / 8 + 1);
1191 : 127369390 : dst_ofs %= 8;
1192 : :
1193 [ + + ][ + + ]: 254564284 : if (src_ofs == 0 && dst_ofs == 0) {
1194 : 127194894 : unsigned int n_bytes = n_bits / 8;
1195 [ + + ]: 127194894 : if (n_bytes) {
1196 : 123208046 : dst -= n_bytes - 1;
1197 : 123208046 : src -= n_bytes - 1;
1198 : 123208046 : memcpy(dst, src, n_bytes);
1199 : :
1200 : 123208046 : n_bits %= 8;
1201 : 123208046 : src--;
1202 : 123208046 : dst--;
1203 : : }
1204 [ + + ]: 127194894 : if (n_bits) {
1205 : 3998832 : uint8_t mask = (1 << n_bits) - 1;
1206 : 3998832 : *dst = (*dst & ~mask) | (*src & mask);
1207 : : }
1208 : : } else {
1209 [ + + ]: 1157664 : while (n_bits > 0) {
1210 : 983168 : unsigned int max_copy = 8 - MAX(src_ofs, dst_ofs);
1211 : 983168 : unsigned int chunk = MIN(n_bits, max_copy);
1212 : 983168 : uint8_t mask = ((1 << chunk) - 1) << dst_ofs;
1213 : :
1214 : 983168 : *dst &= ~mask;
1215 : 983168 : *dst |= ((*src >> src_ofs) << dst_ofs) & mask;
1216 : :
1217 : 983168 : src_ofs += chunk;
1218 [ + + ]: 983168 : if (src_ofs == 8) {
1219 : 424884 : src--;
1220 : 424884 : src_ofs = 0;
1221 : : }
1222 : 983168 : dst_ofs += chunk;
1223 [ + + ]: 983168 : if (dst_ofs == 8) {
1224 : 429352 : dst--;
1225 : 429352 : dst_ofs = 0;
1226 : : }
1227 : 983168 : n_bits -= chunk;
1228 : : }
1229 : : }
1230 : 127369390 : }
1231 : :
1232 : : /* Zeros the 'n_bits' bits starting from bit 'dst_ofs' in 'dst'. 'dst' is
1233 : : * 'dst_len' bytes long.
1234 : : *
1235 : : * If you consider all of 'dst' to be a single unsigned integer in network byte
1236 : : * order, then bit N is the bit with value 2**N. That is, bit 0 is the bit
1237 : : * with value 1 in dst[dst_len - 1], bit 1 is the bit with value 2, bit 2 is
1238 : : * the bit with value 4, ..., bit 8 is the bit with value 1 in dst[dst_len -
1239 : : * 2], and so on.
1240 : : *
1241 : : * Required invariant:
1242 : : * dst_ofs + n_bits <= dst_len * 8
1243 : : */
1244 : : void
1245 : 4023712 : bitwise_zero(void *dst_, unsigned int dst_len, unsigned dst_ofs,
1246 : : unsigned int n_bits)
1247 : : {
1248 : 4023712 : uint8_t *dst = dst_;
1249 : :
1250 [ + + ]: 4023712 : if (!n_bits) {
1251 : 3206080 : return;
1252 : : }
1253 : :
1254 : 817632 : dst += dst_len - (dst_ofs / 8 + 1);
1255 : 817632 : dst_ofs %= 8;
1256 : :
1257 [ + + ]: 817632 : if (dst_ofs) {
1258 : 137672 : unsigned int chunk = MIN(n_bits, 8 - dst_ofs);
1259 : :
1260 : 137672 : *dst &= ~(((1 << chunk) - 1) << dst_ofs);
1261 : :
1262 : 137672 : n_bits -= chunk;
1263 [ + + ]: 137672 : if (!n_bits) {
1264 : 136153 : return;
1265 : : }
1266 : :
1267 : 1519 : dst--;
1268 : : }
1269 : :
1270 [ + + ]: 685287 : while (n_bits >= 8) {
1271 : 3808 : *dst-- = 0;
1272 : 3808 : n_bits -= 8;
1273 : : }
1274 : :
1275 [ + + ]: 681479 : if (n_bits) {
1276 : 681304 : *dst &= ~((1 << n_bits) - 1);
1277 : : }
1278 : : }
1279 : :
1280 : : /* Sets to 1 all of the 'n_bits' bits starting from bit 'dst_ofs' in 'dst'.
1281 : : * 'dst' is 'dst_len' bytes long.
1282 : : *
1283 : : * If you consider all of 'dst' to be a single unsigned integer in network byte
1284 : : * order, then bit N is the bit with value 2**N. That is, bit 0 is the bit
1285 : : * with value 1 in dst[dst_len - 1], bit 1 is the bit with value 2, bit 2 is
1286 : : * the bit with value 4, ..., bit 8 is the bit with value 1 in dst[dst_len -
1287 : : * 2], and so on.
1288 : : *
1289 : : * Required invariant:
1290 : : * dst_ofs + n_bits <= dst_len * 8
1291 : : */
1292 : : void
1293 : 8838688 : bitwise_one(void *dst_, unsigned int dst_len, unsigned dst_ofs,
1294 : : unsigned int n_bits)
1295 : : {
1296 : 8838688 : uint8_t *dst = dst_;
1297 : :
1298 [ + + ]: 8838688 : if (!n_bits) {
1299 : 64 : return;
1300 : : }
1301 : :
1302 : 8838624 : dst += dst_len - (dst_ofs / 8 + 1);
1303 : 8838624 : dst_ofs %= 8;
1304 : :
1305 [ + + ]: 8838624 : if (dst_ofs) {
1306 : 66303 : unsigned int chunk = MIN(n_bits, 8 - dst_ofs);
1307 : :
1308 : 66303 : *dst |= ((1 << chunk) - 1) << dst_ofs;
1309 : :
1310 : 66303 : n_bits -= chunk;
1311 [ + + ]: 66303 : if (!n_bits) {
1312 : 64781 : return;
1313 : : }
1314 : :
1315 : 1522 : dst--;
1316 : : }
1317 : :
1318 [ + + ]: 23212277 : while (n_bits >= 8) {
1319 : 14438434 : *dst-- = 0xff;
1320 : 14438434 : n_bits -= 8;
1321 : : }
1322 : :
1323 [ + + ]: 8773843 : if (n_bits) {
1324 : 3901856 : *dst |= (1 << n_bits) - 1;
1325 : : }
1326 : : }
1327 : :
1328 : : /* Scans the 'n_bits' bits starting from bit 'dst_ofs' in 'dst' for 1-bits.
1329 : : * Returns false if any 1-bits are found, otherwise true. 'dst' is 'dst_len'
1330 : : * bytes long.
1331 : : *
1332 : : * If you consider all of 'dst' to be a single unsigned integer in network byte
1333 : : * order, then bit N is the bit with value 2**N. That is, bit 0 is the bit
1334 : : * with value 1 in dst[dst_len - 1], bit 1 is the bit with value 2, bit 2 is
1335 : : * the bit with value 4, ..., bit 8 is the bit with value 1 in dst[dst_len -
1336 : : * 2], and so on.
1337 : : *
1338 : : * Required invariant:
1339 : : * dst_ofs + n_bits <= dst_len * 8
1340 : : */
1341 : : bool
1342 : 13721800 : bitwise_is_all_zeros(const void *p_, unsigned int len, unsigned int ofs,
1343 : : unsigned int n_bits)
1344 : : {
1345 : 13721800 : const uint8_t *p = p_;
1346 : :
1347 [ + + ]: 13721800 : if (!n_bits) {
1348 : 409727 : return true;
1349 : : }
1350 : :
1351 : 13312073 : p += len - (ofs / 8 + 1);
1352 : 13312073 : ofs %= 8;
1353 : :
1354 [ + + ]: 13312073 : if (ofs) {
1355 : 11468845 : unsigned int chunk = MIN(n_bits, 8 - ofs);
1356 : :
1357 [ + + ]: 11468845 : if (*p & (((1 << chunk) - 1) << ofs)) {
1358 : 8706847 : return false;
1359 : : }
1360 : :
1361 : 2761998 : n_bits -= chunk;
1362 [ + + ]: 2761998 : if (!n_bits) {
1363 : 420361 : return true;
1364 : : }
1365 : :
1366 : 2341637 : p--;
1367 : : }
1368 : :
1369 [ + + ]: 5181689 : while (n_bits >= 8) {
1370 [ + + ]: 4003645 : if (*p) {
1371 : 3006821 : return false;
1372 : : }
1373 : 996824 : n_bits -= 8;
1374 : 996824 : p--;
1375 : : }
1376 : :
1377 [ + + ][ + + ]: 1178044 : if (n_bits && *p & ((1 << n_bits) - 1)) {
1378 : 601681 : return false;
1379 : : }
1380 : :
1381 : 576363 : return true;
1382 : : }
1383 : :
1384 : : /* Scans the bits in 'p' that have bit offsets 'start' (inclusive) through
1385 : : * 'end' (exclusive) for the first bit with value 'target'. If one is found,
1386 : : * returns its offset, otherwise 'end'. 'p' is 'len' bytes long.
1387 : : *
1388 : : * If you consider all of 'p' to be a single unsigned integer in network byte
1389 : : * order, then bit N is the bit with value 2**N. That is, bit 0 is the bit
1390 : : * with value 1 in p[len - 1], bit 1 is the bit with value 2, bit 2 is the bit
1391 : : * with value 4, ..., bit 8 is the bit with value 1 in p[len - 2], and so on.
1392 : : *
1393 : : * Required invariant:
1394 : : * start <= end
1395 : : */
1396 : : unsigned int
1397 : 17450764 : bitwise_scan(const void *p, unsigned int len, bool target, unsigned int start,
1398 : : unsigned int end)
1399 : : {
1400 : : unsigned int ofs;
1401 : :
1402 [ + + ]: 156482221 : for (ofs = start; ofs < end; ofs++) {
1403 [ + + ]: 148981984 : if (bitwise_get_bit(p, len, ofs) == target) {
1404 : 9950527 : break;
1405 : : }
1406 : : }
1407 : 17450764 : return ofs;
1408 : : }
1409 : :
1410 : : /* Scans the bits in 'p' that have bit offsets 'start' (inclusive) through
1411 : : * 'end' (exclusive) for the first bit with value 'target', in reverse order.
1412 : : * If one is found, returns its offset, otherwise 'end'. 'p' is 'len' bytes
1413 : : * long.
1414 : : *
1415 : : * If you consider all of 'p' to be a single unsigned integer in network byte
1416 : : * order, then bit N is the bit with value 2**N. That is, bit 0 is the bit
1417 : : * with value 1 in p[len - 1], bit 1 is the bit with value 2, bit 2 is the bit
1418 : : * with value 4, ..., bit 8 is the bit with value 1 in p[len - 2], and so on.
1419 : : *
1420 : : * To scan an entire bit array in reverse order, specify start == len * 8 - 1
1421 : : * and end == -1, in which case the return value is nonnegative if successful
1422 : : * and -1 if no 'target' match is found.
1423 : : *
1424 : : * Required invariant:
1425 : : * start >= end
1426 : : */
1427 : : int
1428 : 34788135 : bitwise_rscan(const void *p, unsigned int len, bool target, int start, int end)
1429 : : {
1430 : 34788135 : const uint8_t *s = p;
1431 : 34788135 : int start_byte = len - (start / 8 + 1);
1432 : 34788135 : int end_byte = len - (end / 8 + 1);
1433 : : int ofs_byte;
1434 : : int ofs;
1435 : : uint8_t the_byte;
1436 : :
1437 : : /* Find the target in the start_byte from starting offset */
1438 : 34788135 : ofs_byte = start_byte;
1439 : 34788135 : the_byte = s[ofs_byte];
1440 [ + + ]: 129211813 : for (ofs = start % 8; ofs >= 0; ofs--) {
1441 [ + + ]: 115834689 : if (((the_byte & (1u << ofs)) != 0) == target) {
1442 : 21411011 : break;
1443 : : }
1444 : : }
1445 [ + + ]: 34788135 : if (ofs < 0) {
1446 : : /* Target not found in start byte, continue searching byte by byte */
1447 [ + + ]: 941429496 : for (ofs_byte = start_byte + 1; ofs_byte <= end_byte; ofs_byte++) {
1448 [ + + ][ + + ]: 936047323 : if ((target && s[ofs_byte])
1449 [ + + ][ + + ]: 930336836 : || (!target && (s[ofs_byte] != 0xff))) {
1450 : : break;
1451 : : }
1452 : : }
1453 [ + + ]: 13377124 : if (ofs_byte > end_byte) {
1454 : 5382173 : return end;
1455 : : }
1456 : 7994951 : the_byte = s[ofs_byte];
1457 : : /* Target is in the_byte, find it bit by bit */
1458 [ + - ]: 25890493 : for (ofs = 7; ofs >= 0; ofs--) {
1459 [ + + ]: 25890493 : if (((the_byte & (1u << ofs)) != 0) == target) {
1460 : 7994951 : break;
1461 : : }
1462 : : }
1463 : : }
1464 : 29405962 : int ret = (len - ofs_byte) * 8 - (8 - ofs);
1465 [ + + ]: 29405962 : if (ret < end) {
1466 : 743511 : return end;
1467 : : }
1468 : 28662451 : return ret;
1469 : : }
1470 : :
1471 : : /* Copies the 'n_bits' low-order bits of 'value' into the 'n_bits' bits
1472 : : * starting at bit 'dst_ofs' in 'dst', which is 'dst_len' bytes long.
1473 : : *
1474 : : * If you consider all of 'dst' to be a single unsigned integer in network byte
1475 : : * order, then bit N is the bit with value 2**N. That is, bit 0 is the bit
1476 : : * with value 1 in dst[dst_len - 1], bit 1 is the bit with value 2, bit 2 is
1477 : : * the bit with value 4, ..., bit 8 is the bit with value 1 in dst[dst_len -
1478 : : * 2], and so on.
1479 : : *
1480 : : * Required invariants:
1481 : : * dst_ofs + n_bits <= dst_len * 8
1482 : : * n_bits <= 64
1483 : : */
1484 : : void
1485 : 120773 : bitwise_put(uint64_t value,
1486 : : void *dst, unsigned int dst_len, unsigned int dst_ofs,
1487 : : unsigned int n_bits)
1488 : : {
1489 : 120773 : ovs_be64 n_value = htonll(value);
1490 : 120773 : bitwise_copy(&n_value, sizeof n_value, 0,
1491 : : dst, dst_len, dst_ofs,
1492 : : n_bits);
1493 : 120773 : }
1494 : :
1495 : : /* Returns the value of the 'n_bits' bits starting at bit 'src_ofs' in 'src',
1496 : : * which is 'src_len' bytes long.
1497 : : *
1498 : : * If you consider all of 'src' to be a single unsigned integer in network byte
1499 : : * order, then bit N is the bit with value 2**N. That is, bit 0 is the bit
1500 : : * with value 1 in src[src_len - 1], bit 1 is the bit with value 2, bit 2 is
1501 : : * the bit with value 4, ..., bit 8 is the bit with value 1 in src[src_len -
1502 : : * 2], and so on.
1503 : : *
1504 : : * Required invariants:
1505 : : * src_ofs + n_bits <= src_len * 8
1506 : : * n_bits <= 64
1507 : : */
1508 : : uint64_t
1509 : 15276874 : bitwise_get(const void *src, unsigned int src_len,
1510 : : unsigned int src_ofs, unsigned int n_bits)
1511 : : {
1512 : 15276874 : ovs_be64 value = htonll(0);
1513 : :
1514 : 15276874 : bitwise_copy(src, src_len, src_ofs,
1515 : : &value, sizeof value, 0,
1516 : : n_bits);
1517 : 15276874 : return ntohll(value);
1518 : : }
1519 : :
1520 : : /* Returns the value of the bit with offset 'ofs' in 'src', which is 'len'
1521 : : * bytes long.
1522 : : *
1523 : : * If you consider all of 'src' to be a single unsigned integer in network byte
1524 : : * order, then bit N is the bit with value 2**N. That is, bit 0 is the bit
1525 : : * with value 1 in src[len - 1], bit 1 is the bit with value 2, bit 2 is the
1526 : : * bit with value 4, ..., bit 8 is the bit with value 1 in src[len - 2], and so
1527 : : * on.
1528 : : *
1529 : : * Required invariants:
1530 : : * ofs < len * 8
1531 : : */
1532 : : bool
1533 : 237027061 : bitwise_get_bit(const void *src_, unsigned int len, unsigned int ofs)
1534 : : {
1535 : 237027061 : const uint8_t *src = src_;
1536 : :
1537 : 237027061 : return (src[len - (ofs / 8 + 1)] & (1u << (ofs % 8))) != 0;
1538 : : }
1539 : :
1540 : : /* Sets the bit with offset 'ofs' in 'dst', which is 'len' bytes long, to 0.
1541 : : *
1542 : : * If you consider all of 'dst' to be a single unsigned integer in network byte
1543 : : * order, then bit N is the bit with value 2**N. That is, bit 0 is the bit
1544 : : * with value 1 in dst[len - 1], bit 1 is the bit with value 2, bit 2 is the
1545 : : * bit with value 4, ..., bit 8 is the bit with value 1 in dst[len - 2], and so
1546 : : * on.
1547 : : *
1548 : : * Required invariants:
1549 : : * ofs < len * 8
1550 : : */
1551 : : void
1552 : 536688 : bitwise_put0(void *dst_, unsigned int len, unsigned int ofs)
1553 : : {
1554 : 536688 : uint8_t *dst = dst_;
1555 : :
1556 : 536688 : dst[len - (ofs / 8 + 1)] &= ~(1u << (ofs % 8));
1557 : 536688 : }
1558 : :
1559 : : /* Sets the bit with offset 'ofs' in 'dst', which is 'len' bytes long, to 1.
1560 : : *
1561 : : * If you consider all of 'dst' to be a single unsigned integer in network byte
1562 : : * order, then bit N is the bit with value 2**N. That is, bit 0 is the bit
1563 : : * with value 1 in dst[len - 1], bit 1 is the bit with value 2, bit 2 is the
1564 : : * bit with value 4, ..., bit 8 is the bit with value 1 in dst[len - 2], and so
1565 : : * on.
1566 : : *
1567 : : * Required invariants:
1568 : : * ofs < len * 8
1569 : : */
1570 : : void
1571 : 1694212 : bitwise_put1(void *dst_, unsigned int len, unsigned int ofs)
1572 : : {
1573 : 1694212 : uint8_t *dst = dst_;
1574 : :
1575 : 1694212 : dst[len - (ofs / 8 + 1)] |= 1u << (ofs % 8);
1576 : 1694212 : }
1577 : :
1578 : : /* Sets the bit with offset 'ofs' in 'dst', which is 'len' bytes long, to 'b'.
1579 : : *
1580 : : * If you consider all of 'dst' to be a single unsigned integer in network byte
1581 : : * order, then bit N is the bit with value 2**N. That is, bit 0 is the bit
1582 : : * with value 1 in dst[len - 1], bit 1 is the bit with value 2, bit 2 is the
1583 : : * bit with value 4, ..., bit 8 is the bit with value 1 in dst[len - 2], and so
1584 : : * on.
1585 : : *
1586 : : * Required invariants:
1587 : : * ofs < len * 8
1588 : : */
1589 : : void
1590 : 1115450 : bitwise_put_bit(void *dst, unsigned int len, unsigned int ofs, bool b)
1591 : : {
1592 [ + + ]: 1115450 : if (b) {
1593 : 578762 : bitwise_put1(dst, len, ofs);
1594 : : } else {
1595 : 536688 : bitwise_put0(dst, len, ofs);
1596 : : }
1597 : 1115450 : }
1598 : :
1599 : : /* Flips the bit with offset 'ofs' in 'dst', which is 'len' bytes long.
1600 : : *
1601 : : * If you consider all of 'dst' to be a single unsigned integer in network byte
1602 : : * order, then bit N is the bit with value 2**N. That is, bit 0 is the bit
1603 : : * with value 1 in dst[len - 1], bit 1 is the bit with value 2, bit 2 is the
1604 : : * bit with value 4, ..., bit 8 is the bit with value 1 in dst[len - 2], and so
1605 : : * on.
1606 : : *
1607 : : * Required invariants:
1608 : : * ofs < len * 8
1609 : : */
1610 : : void
1611 : 2010816 : bitwise_toggle_bit(void *dst_, unsigned int len, unsigned int ofs)
1612 : : {
1613 : 2010816 : uint8_t *dst = dst_;
1614 : :
1615 : 2010816 : dst[len - (ofs / 8 + 1)] ^= 1u << (ofs % 8);
1616 : 2010816 : }
1617 : :
1618 : : /* ovs_scan */
1619 : :
1620 : : struct scan_spec {
1621 : : unsigned int width;
1622 : : enum {
1623 : : SCAN_DISCARD,
1624 : : SCAN_CHAR,
1625 : : SCAN_SHORT,
1626 : : SCAN_INT,
1627 : : SCAN_LONG,
1628 : : SCAN_LLONG,
1629 : : SCAN_INTMAX_T,
1630 : : SCAN_PTRDIFF_T,
1631 : : SCAN_SIZE_T
1632 : : } type;
1633 : : };
1634 : :
1635 : : static const char *
1636 : 3371130 : skip_spaces(const char *s)
1637 : : {
1638 [ + + ]: 4604982 : while (isspace((unsigned char) *s)) {
1639 : 1233852 : s++;
1640 : : }
1641 : 3371130 : return s;
1642 : : }
1643 : :
1644 : : static const char *
1645 : 3369355 : scan_int(const char *s, const struct scan_spec *spec, int base, va_list *args)
1646 : : {
1647 : 3369355 : const char *start = s;
1648 : : uintmax_t value;
1649 : : bool negative;
1650 : : int n_digits;
1651 : :
1652 : 3369355 : negative = *s == '-';
1653 [ + + ][ + + ]: 3369355 : s += *s == '-' || *s == '+';
1654 : :
1655 [ + + ][ + + ]: 3369355 : if ((!base || base == 16) && *s == '0' && (s[1] == 'x' || s[1] == 'X')) {
[ + + ][ + + ]
[ - + ]
1656 : 3713 : base = 16;
1657 : 3713 : s += 2;
1658 [ + + ]: 3365642 : } else if (!base) {
1659 [ + + ]: 14671 : base = *s == '0' ? 8 : 10;
1660 : : }
1661 : :
1662 [ - + ]: 3369355 : if (s - start >= spec->width) {
1663 : 0 : return NULL;
1664 : : }
1665 : :
1666 : 3369355 : value = 0;
1667 : 3369355 : n_digits = 0;
1668 [ + + ]: 10024108 : while (s - start < spec->width) {
1669 : 10024045 : int digit = hexit_value(*s);
1670 : :
1671 [ + + ][ + + ]: 10024045 : if (digit < 0 || digit >= base) {
1672 : : break;
1673 : : }
1674 : 6654753 : value = value * base + digit;
1675 : 6654753 : n_digits++;
1676 : 6654753 : s++;
1677 : : }
1678 [ + + ]: 3369355 : if (!n_digits) {
1679 : 81814 : return NULL;
1680 : : }
1681 : :
1682 [ + + ]: 3287541 : if (negative) {
1683 : 29 : value = -value;
1684 : : }
1685 : :
1686 [ + + + + : 3287541 : switch (spec->type) {
+ + + + +
- ]
1687 : : case SCAN_DISCARD:
1688 : 20 : break;
1689 : : case SCAN_CHAR:
1690 [ + + ]: 3264487 : *va_arg(*args, char *) = value;
1691 : 3264487 : break;
1692 : : case SCAN_SHORT:
1693 [ + + ]: 5479 : *va_arg(*args, short int *) = value;
1694 : 5479 : break;
1695 : : case SCAN_INT:
1696 [ + + ]: 17161 : *va_arg(*args, int *) = value;
1697 : 17161 : break;
1698 : : case SCAN_LONG:
1699 [ + - ]: 263 : *va_arg(*args, long int *) = value;
1700 : 263 : break;
1701 : : case SCAN_LLONG:
1702 [ + - ]: 83 : *va_arg(*args, long long int *) = value;
1703 : 83 : break;
1704 : : case SCAN_INTMAX_T:
1705 [ + - ]: 16 : *va_arg(*args, intmax_t *) = value;
1706 : 16 : break;
1707 : : case SCAN_PTRDIFF_T:
1708 [ + - ]: 16 : *va_arg(*args, ptrdiff_t *) = value;
1709 : 16 : break;
1710 : : case SCAN_SIZE_T:
1711 [ + - ]: 16 : *va_arg(*args, size_t *) = value;
1712 : 16 : break;
1713 : : }
1714 : 3287541 : return s;
1715 : : }
1716 : :
1717 : : static const char *
1718 : 107 : skip_digits(const char *s)
1719 : : {
1720 [ + + ][ + + ]: 229 : while (*s >= '0' && *s <= '9') {
1721 : 122 : s++;
1722 : : }
1723 : 107 : return s;
1724 : : }
1725 : :
1726 : : static const char *
1727 : 58 : scan_float(const char *s, const struct scan_spec *spec, va_list *args)
1728 : : {
1729 : 58 : const char *start = s;
1730 : : long double value;
1731 : : char *tail;
1732 : : char *copy;
1733 : : bool ok;
1734 : :
1735 [ + + ][ + + ]: 58 : s += *s == '+' || *s == '-';
1736 : 58 : s = skip_digits(s);
1737 [ + + ]: 58 : if (*s == '.') {
1738 : 28 : s = skip_digits(s + 1);
1739 : : }
1740 [ + + ][ - + ]: 58 : if (*s == 'e' || *s == 'E') {
1741 : 21 : s++;
1742 [ + - ][ + + ]: 21 : s += *s == '+' || *s == '-';
1743 : 21 : s = skip_digits(s);
1744 : : }
1745 : :
1746 [ + + ]: 58 : if (s - start > spec->width) {
1747 : 24 : s = start + spec->width;
1748 : : }
1749 : :
1750 : 58 : copy = xmemdup0(start, s - start);
1751 : 58 : value = strtold(copy, &tail);
1752 : 58 : ok = *tail == '\0';
1753 : 58 : free(copy);
1754 [ + + ]: 58 : if (!ok) {
1755 : 18 : return NULL;
1756 : : }
1757 : :
1758 [ - + + + : 40 : switch (spec->type) {
- - ]
1759 : : case SCAN_DISCARD:
1760 : 0 : break;
1761 : : case SCAN_INT:
1762 [ + - ]: 13 : *va_arg(*args, float *) = value;
1763 : 13 : break;
1764 : : case SCAN_LONG:
1765 [ + - ]: 14 : *va_arg(*args, double *) = value;
1766 : 14 : break;
1767 : : case SCAN_LLONG:
1768 [ + - ]: 13 : *va_arg(*args, long double *) = value;
1769 : 13 : break;
1770 : :
1771 : : case SCAN_CHAR:
1772 : : case SCAN_SHORT:
1773 : : case SCAN_INTMAX_T:
1774 : : case SCAN_PTRDIFF_T:
1775 : : case SCAN_SIZE_T:
1776 : 0 : OVS_NOT_REACHED();
1777 : : }
1778 : 58 : return s;
1779 : : }
1780 : :
1781 : : static void
1782 : 11641 : scan_output_string(const struct scan_spec *spec,
1783 : : const char *s, size_t n,
1784 : : va_list *args)
1785 : : {
1786 [ + + ]: 11641 : if (spec->type != SCAN_DISCARD) {
1787 [ + + ]: 3555 : char *out = va_arg(*args, char *);
1788 : 3555 : memcpy(out, s, n);
1789 : 3555 : out[n] = '\0';
1790 : : }
1791 : 11641 : }
1792 : :
1793 : : static const char *
1794 : 11 : scan_string(const char *s, const struct scan_spec *spec, va_list *args)
1795 : : {
1796 : : size_t n;
1797 : :
1798 [ + + ]: 55 : for (n = 0; n < spec->width; n++) {
1799 [ + + ][ + + ]: 51 : if (!s[n] || isspace((unsigned char) s[n])) {
1800 : : break;
1801 : : }
1802 : : }
1803 [ + + ]: 11 : if (!n) {
1804 : 2 : return NULL;
1805 : : }
1806 : :
1807 : 9 : scan_output_string(spec, s, n, args);
1808 : 9 : return s + n;
1809 : : }
1810 : :
1811 : : static const char *
1812 : 11672 : parse_scanset(const char *p_, unsigned long *set, bool *complemented)
1813 : : {
1814 : 11672 : const uint8_t *p = (const uint8_t *) p_;
1815 : :
1816 : 11672 : *complemented = *p == '^';
1817 : 11672 : p += *complemented;
1818 : :
1819 [ + + ]: 11672 : if (*p == ']') {
1820 : 2 : bitmap_set1(set, ']');
1821 : 2 : p++;
1822 : : }
1823 : :
1824 [ + - ][ + + ]: 69245 : while (*p && *p != ']') {
1825 [ + + ][ + - ]: 57573 : if (p[1] == '-' && p[2] != ']' && p[2] > *p) {
[ + - ]
1826 : 9678 : bitmap_set_multiple(set, *p, p[2] - *p + 1, true);
1827 : 9678 : p += 3;
1828 : : } else {
1829 : 47895 : bitmap_set1(set, *p++);
1830 : : }
1831 : : }
1832 [ + - ]: 11672 : if (*p == ']') {
1833 : 11672 : p++;
1834 : : }
1835 : 11672 : return (const char *) p;
1836 : : }
1837 : :
1838 : : static const char *
1839 : 11672 : scan_set(const char *s, const struct scan_spec *spec, const char **pp,
1840 : : va_list *args)
1841 : : {
1842 : : unsigned long set[BITMAP_N_LONGS(UCHAR_MAX + 1)];
1843 : : bool complemented;
1844 : : unsigned int n;
1845 : :
1846 : : /* Parse the scan set. */
1847 : 11672 : memset(set, 0, sizeof set);
1848 : 11672 : *pp = parse_scanset(*pp, set, &complemented);
1849 : :
1850 : : /* Parse the data. */
1851 : 11672 : n = 0;
1852 [ + + ]: 51851 : while (s[n]
1853 [ + + ]: 48175 : && bitmap_is_set(set, (unsigned char) s[n]) == !complemented
1854 [ + - ]: 40179 : && n < spec->width) {
1855 : 40179 : n++;
1856 : : }
1857 [ + + ]: 11672 : if (!n) {
1858 : 40 : return NULL;
1859 : : }
1860 : 11632 : scan_output_string(spec, s, n, args);
1861 : 11672 : return s + n;
1862 : : }
1863 : :
1864 : : static const char *
1865 : 6 : scan_chars(const char *s, const struct scan_spec *spec, va_list *args)
1866 : : {
1867 [ + + ]: 6 : unsigned int n = spec->width == UINT_MAX ? 1 : spec->width;
1868 : :
1869 [ + + ]: 6 : if (strlen(s) < n) {
1870 : 1 : return NULL;
1871 : : }
1872 [ + + ]: 5 : if (spec->type != SCAN_DISCARD) {
1873 [ + - ]: 2 : memcpy(va_arg(*args, char *), s, n);
1874 : : }
1875 : 5 : return s + n;
1876 : : }
1877 : :
1878 : : static bool
1879 : 715441 : ovs_scan__(const char *s, int *n, const char *format, va_list *args)
1880 : : {
1881 : 715441 : const char *const start = s;
1882 : 715441 : bool ok = false;
1883 : : const char *p;
1884 : :
1885 : 715441 : p = format;
1886 [ + + ]: 7067238 : while (*p != '\0') {
1887 : : struct scan_spec spec;
1888 : 6539205 : unsigned char c = *p++;
1889 : : bool discard;
1890 : :
1891 [ + + ]: 6539205 : if (isspace(c)) {
1892 : 1706 : s = skip_spaces(s);
1893 : 2704788 : continue;
1894 [ + + ]: 6537499 : } else if (c != '%') {
1895 [ + + ]: 2808612 : if (*s != c) {
1896 : 187408 : goto exit;
1897 : : }
1898 : 2703079 : s++;
1899 : 2703079 : continue;
1900 [ + + ]: 3728887 : } else if (*p == '%') {
1901 [ - + ]: 3 : if (*s++ != '%') {
1902 : 0 : goto exit;
1903 : : }
1904 : 3 : p++;
1905 : 3 : continue;
1906 : : }
1907 : :
1908 : : /* Parse '*' flag. */
1909 : 3728884 : discard = *p == '*';
1910 : 3728884 : p += discard;
1911 : :
1912 : : /* Parse field width. */
1913 : 3728884 : spec.width = 0;
1914 [ + - ][ + + ]: 3734558 : while (*p >= '0' && *p <= '9') {
1915 : 5674 : spec.width = spec.width * 10 + (*p++ - '0');
1916 : : }
1917 [ + + ]: 3728884 : if (spec.width == 0) {
1918 : 3725206 : spec.width = UINT_MAX;
1919 : : }
1920 : :
1921 : : /* Parse type modifier. */
1922 [ + + + + : 3728884 : switch (*p) {
+ + + ]
1923 : : case 'h':
1924 [ + + ]: 3349179 : if (p[1] == 'h') {
1925 : 3343699 : spec.type = SCAN_CHAR;
1926 : 3343699 : p += 2;
1927 : : } else {
1928 : 5480 : spec.type = SCAN_SHORT;
1929 : 5480 : p++;
1930 : : }
1931 : 3349179 : break;
1932 : :
1933 : : case 'j':
1934 : 17 : spec.type = SCAN_INTMAX_T;
1935 : 17 : p++;
1936 : 17 : break;
1937 : :
1938 : : case 'l':
1939 [ + + ]: 859 : if (p[1] == 'l') {
1940 : 576 : spec.type = SCAN_LLONG;
1941 : 576 : p += 2;
1942 : : } else {
1943 : 283 : spec.type = SCAN_LONG;
1944 : 283 : p++;
1945 : : }
1946 : 859 : break;
1947 : :
1948 : : case 'L':
1949 : : case 'q':
1950 : 18 : spec.type = SCAN_LLONG;
1951 : 18 : p++;
1952 : 18 : break;
1953 : :
1954 : : case 't':
1955 : 17 : spec.type = SCAN_PTRDIFF_T;
1956 : 17 : p++;
1957 : 17 : break;
1958 : :
1959 : : case 'z':
1960 : 17 : spec.type = SCAN_SIZE_T;
1961 : 17 : p++;
1962 : 17 : break;
1963 : :
1964 : : default:
1965 : 378777 : spec.type = SCAN_INT;
1966 : 378777 : break;
1967 : : }
1968 : :
1969 [ + + ]: 3728884 : if (discard) {
1970 : 8117 : spec.type = SCAN_DISCARD;
1971 : : }
1972 : :
1973 : 3728884 : c = *p++;
1974 [ + + ][ + + ]: 3728884 : if (c != 'c' && c != 'n' && c != '[') {
[ + + ]
1975 : 3369424 : s = skip_spaces(s);
1976 : : }
1977 [ + + + + : 3728884 : switch (c) {
+ + + + +
+ - ]
1978 : : case 'd':
1979 : 13476 : s = scan_int(s, &spec, 10, args);
1980 : 13476 : break;
1981 : :
1982 : : case 'i':
1983 : 18381 : s = scan_int(s, &spec, 0, args);
1984 : 18381 : break;
1985 : :
1986 : : case 'o':
1987 : 23 : s = scan_int(s, &spec, 8, args);
1988 : 23 : break;
1989 : :
1990 : : case 'u':
1991 : 507146 : s = scan_int(s, &spec, 10, args);
1992 : 507146 : break;
1993 : :
1994 : : case 'x':
1995 : : case 'X':
1996 : 2830329 : s = scan_int(s, &spec, 16, args);
1997 : 2830329 : break;
1998 : :
1999 : : case 'e':
2000 : : case 'f':
2001 : : case 'g':
2002 : : case 'E':
2003 : : case 'G':
2004 : 58 : s = scan_float(s, &spec, args);
2005 : 58 : break;
2006 : :
2007 : : case 's':
2008 : 11 : s = scan_string(s, &spec, args);
2009 : 11 : break;
2010 : :
2011 : : case '[':
2012 : 11672 : s = scan_set(s, &spec, &p, args);
2013 : 11672 : break;
2014 : :
2015 : : case 'c':
2016 : 6 : s = scan_chars(s, &spec, args);
2017 : 6 : break;
2018 : :
2019 : : case 'n':
2020 [ + - ]: 347782 : if (spec.type != SCAN_DISCARD) {
2021 [ + + ]: 347782 : *va_arg(*args, int *) = s - start;
2022 : : }
2023 : 347782 : break;
2024 : : }
2025 : :
2026 [ + + ]: 3728884 : if (!s) {
2027 : 3728884 : goto exit;
2028 : : }
2029 : : }
2030 [ + + ]: 528033 : if (n) {
2031 : 87309 : *n = s - start;
2032 : : }
2033 : :
2034 : 528033 : ok = true;
2035 : : exit:
2036 : 715441 : return ok;
2037 : : }
2038 : :
2039 : : /* This is an implementation of the standard sscanf() function, with the
2040 : : * following exceptions:
2041 : : *
2042 : : * - It returns true if the entire format was successfully scanned and
2043 : : * converted, false if any conversion failed.
2044 : : *
2045 : : * - The standard doesn't define sscanf() behavior when an out-of-range value
2046 : : * is scanned, e.g. if a "%"PRIi8 conversion scans "-1" or "0x1ff". Some
2047 : : * implementations consider this an error and stop scanning. This
2048 : : * implementation never considers an out-of-range value an error; instead,
2049 : : * it stores the least-significant bits of the converted value in the
2050 : : * destination, e.g. the value 255 for both examples earlier.
2051 : : *
2052 : : * - Only single-byte characters are supported, that is, the 'l' modifier
2053 : : * on %s, %[, and %c is not supported. The GNU extension 'a' modifier is
2054 : : * also not supported.
2055 : : *
2056 : : * - %p is not supported.
2057 : : */
2058 : : bool
2059 : 546427 : ovs_scan(const char *s, const char *format, ...)
2060 : : {
2061 : : va_list args;
2062 : : bool res;
2063 : :
2064 : 546427 : va_start(args, format);
2065 : 546427 : res = ovs_scan__(s, NULL, format, &args);
2066 : 546427 : va_end(args);
2067 : 546427 : return res;
2068 : : }
2069 : :
2070 : : /*
2071 : : * This function is similar to ovs_scan(), with an extra parameter `n` added to
2072 : : * return the number of scanned characters.
2073 : : */
2074 : : bool
2075 : 169014 : ovs_scan_len(const char *s, int *n, const char *format, ...)
2076 : : {
2077 : : va_list args;
2078 : : bool success;
2079 : : int n1;
2080 : :
2081 : 169014 : va_start(args, format);
2082 : 169014 : success = ovs_scan__(s + *n, &n1, format, &args);
2083 : 169014 : va_end(args);
2084 [ + + ]: 169014 : if (success) {
2085 : 87309 : *n = *n + n1;
2086 : : }
2087 : 169014 : return success;
2088 : : }
2089 : :
2090 : : void
2091 : 0 : xsleep(unsigned int seconds)
2092 : : {
2093 : 0 : ovsrcu_quiesce_start();
2094 : : #ifdef _WIN32
2095 : : Sleep(seconds * 1000);
2096 : : #else
2097 : 0 : sleep(seconds);
2098 : : #endif
2099 : 0 : ovsrcu_quiesce_end();
2100 : 0 : }
2101 : :
2102 : : /* Determine whether standard output is a tty or not. This is useful to decide
2103 : : * whether to use color output or not when --color option for utilities is set
2104 : : * to `auto`.
2105 : : */
2106 : : bool
2107 : 0 : is_stdout_a_tty(void)
2108 : : {
2109 : 0 : char const *t = getenv("TERM");
2110 [ # # ][ # # ]: 0 : return (isatty(STDOUT_FILENO) && t && strcmp(t, "dumb") != 0);
[ # # ]
2111 : : }
2112 : :
2113 : : #ifdef _WIN32
2114 : :
2115 : : char *
2116 : : ovs_format_message(int error)
2117 : : {
2118 : : enum { BUFSIZE = sizeof strerror_buffer_get()->s };
2119 : : char *buffer = strerror_buffer_get()->s;
2120 : :
2121 : : if (error == 0) {
2122 : : /* See ovs_strerror */
2123 : : return "Success";
2124 : : }
2125 : :
2126 : : FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
2127 : : NULL, error, 0, buffer, BUFSIZE, NULL);
2128 : : return buffer;
2129 : : }
2130 : :
2131 : : /* Returns a null-terminated string that explains the last error.
2132 : : * Use this function to get the error string for WINAPI calls. */
2133 : : char *
2134 : : ovs_lasterror_to_string(void)
2135 : : {
2136 : : return ovs_format_message(GetLastError());
2137 : : }
2138 : :
2139 : : int
2140 : : ftruncate(int fd, off_t length)
2141 : : {
2142 : : int error;
2143 : :
2144 : : error = _chsize_s(fd, length);
2145 : : if (error) {
2146 : : return -1;
2147 : : }
2148 : : return 0;
2149 : : }
2150 : :
2151 : : OVS_CONSTRUCTOR(winsock_start) {
2152 : : WSADATA wsaData;
2153 : : int error;
2154 : :
2155 : : error = WSAStartup(MAKEWORD(2, 2), &wsaData);
2156 : : if (error != 0) {
2157 : : VLOG_FATAL("WSAStartup failed: %s", sock_strerror(sock_errno()));
2158 : : }
2159 : : }
2160 : : #endif
|