lib/util: Make some functions in util.c inline
[sliver-openvswitch.git] / lib / util.c
1 /*
2  * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013 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-thread.h"
34 #include "vlog.h"
35 #ifdef HAVE_PTHREAD_SET_NAME_NP
36 #include <pthread_np.h>
37 #endif
38
39 VLOG_DEFINE_THIS_MODULE(util);
40
41 COVERAGE_DEFINE(util_xalloc);
42
43 /* argv[0] without directory names. */
44 const char *program_name;
45
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);
49
50 /* --version option output. */
51 static char *program_version;
52
53 /* Buffer used by ovs_strerror(). */
54 DEFINE_STATIC_PER_THREAD_DATA(struct { char s[128]; },
55                               strerror_buffer,
56                               { "" });
57
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
82 void
83 out_of_memory(void)
84 {
85     ovs_abort(0, "virtual memory exhausted");
86 }
87
88 void *
89 xcalloc(size_t count, size_t size)
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
99 void *
100 xzalloc(size_t size)
101 {
102     return xcalloc(1, size);
103 }
104
105 void *
106 xmalloc(size_t size)
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 *
117 xrealloc(void *p, size_t size)
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 *
145 xstrdup(const char *s)
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
188 /* Similar to strlcpy() from OpenBSD, but it never reads more than 'size - 1'
189  * bytes from 'src' and doesn't return anything. */
190 void
191 ovs_strlcpy(char *dst, const char *src, size_t size)
192 {
193     if (size > 0) {
194         size_t len = strnlen(src, size - 1);
195         memcpy(dst, src, len);
196         dst[len] = '\0';
197     }
198 }
199
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
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);
237     ovs_abort_valist(err_no, format, args);
238 }
239
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);
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. */
254 void
255 ovs_fatal(int err_no, const char *format, ...)
256 {
257     va_list args;
258
259     va_start(args, format);
260     ovs_fatal_valist(err_no, format, args);
261 }
262
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);
268     exit(EXIT_FAILURE);
269 }
270
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. */
277 void
278 ovs_error(int err_no, const char *format, ...)
279 {
280     va_list args;
281
282     va_start(args, format);
283     ovs_error_valist(err_no, format, args);
284     va_end(args);
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 {
291     const char *subprogram_name = get_subprogram_name();
292     int save_errno = errno;
293
294     if (subprogram_name[0]) {
295         fprintf(stderr, "%s(%s): ", program_name, subprogram_name);
296     } else {
297         fprintf(stderr, "%s: ", program_name);
298     }
299
300     vfprintf(stderr, format, args);
301     if (err_no != 0) {
302         fprintf(stderr, " (%s)", ovs_retval_to_string(err_no));
303     }
304     putc('\n', stderr);
305
306     errno = save_errno;
307 }
308
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 {
321     return (!retval ? ""
322             : retval == EOF ? "End of file"
323             : ovs_strerror(retval));
324 }
325
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. */
349         snprintf(buffer, BUFSIZE, "Unknown error %d", error);
350     }
351 #endif
352
353     errno = save_errno;
354
355     return s;
356 }
357
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  *
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  *
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
373 set_program_name__(const char *argv0, const char *version, const char *date,
374                    const char *time)
375 {
376     const char *slash = strrchr(argv0, '/');
377
378     assert_single_threaded();
379
380     program_name = slash ? slash + 1 : argv0;
381
382     free(program_version);
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     }
394 }
395
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
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.) */
407 void
408 set_subprogram_name(const char *format, ...)
409 {
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
424 #if HAVE_GLIBC_PTHREAD_SETNAME_NP
425     pthread_setname_np(pthread_self(), pname);
426 #elif HAVE_NETBSD_PTHREAD_SETNAME_NP
427     pthread_setname_np(pthread_self(), "%s", pname);
428 #elif HAVE_PTHREAD_SET_NAME_NP
429     pthread_set_name_np(pthread_self(), pname);
430 #endif
431 }
432
433 /* Returns a pointer to a string describing the program version.  The
434  * caller must not modify or free the returned string.
435  */
436 const char *
437 get_program_version(void)
438 {
439     return program_version;
440 }
441
442 /* Print the version information for the program.  */
443 void
444 ovs_print_version(uint8_t min_ofp, uint8_t max_ofp)
445 {
446     printf("%s", program_version);
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. */
476       fprintf(stream, "%08"PRIxMAX"  ", (uintmax_t) ROUND_DOWN(ofs, per_line));
477       for (i = 0; i < start; i++)
478         fprintf(stream, "   ");
479       for (; i < end; i++)
480         fprintf(stream, "%02x%c",
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 /* Converts floating-point string 's' into a double.  If successful, stores
541  * the double in '*d' and returns true; on failure, stores 0 in '*d' and
542  * returns false.
543  *
544  * Underflow (e.g. "1e-9999") is not considered an error, but overflow
545  * (e.g. "1e9999)" is. */
546 bool
547 str_to_double(const char *s, double *d)
548 {
549     int save_errno = errno;
550     char *tail;
551     errno = 0;
552     *d = strtod(s, &tail);
553     if (errno == EINVAL || (errno == ERANGE && *d != 0)
554         || tail == s || *tail != '\0') {
555         errno = save_errno;
556         *d = 0;
557         return false;
558     } else {
559         errno = save_errno;
560         return true;
561     }
562 }
563
564 /* Returns the value of 'c' as a hexadecimal digit. */
565 int
566 hexit_value(int c)
567 {
568     switch (c) {
569     case '0': case '1': case '2': case '3': case '4':
570     case '5': case '6': case '7': case '8': case '9':
571         return c - '0';
572
573     case 'a': case 'A':
574         return 0xa;
575
576     case 'b': case 'B':
577         return 0xb;
578
579     case 'c': case 'C':
580         return 0xc;
581
582     case 'd': case 'D':
583         return 0xd;
584
585     case 'e': case 'E':
586         return 0xe;
587
588     case 'f': case 'F':
589         return 0xf;
590
591     default:
592         return -1;
593     }
594 }
595
596 /* Returns the integer value of the 'n' hexadecimal digits starting at 's', or
597  * UINT_MAX if one of those "digits" is not really a hex digit.  If 'ok' is
598  * nonnull, '*ok' is set to true if the conversion succeeds or to false if a
599  * non-hex digit is detected. */
600 unsigned int
601 hexits_value(const char *s, size_t n, bool *ok)
602 {
603     unsigned int value;
604     size_t i;
605
606     value = 0;
607     for (i = 0; i < n; i++) {
608         int hexit = hexit_value(s[i]);
609         if (hexit < 0) {
610             if (ok) {
611                 *ok = false;
612             }
613             return UINT_MAX;
614         }
615         value = (value << 4) + hexit;
616     }
617     if (ok) {
618         *ok = true;
619     }
620     return value;
621 }
622
623 /* Returns the current working directory as a malloc()'d string, or a null
624  * pointer if the current working directory cannot be determined. */
625 char *
626 get_cwd(void)
627 {
628     long int path_max;
629     size_t size;
630
631     /* Get maximum path length or at least a reasonable estimate. */
632     path_max = pathconf(".", _PC_PATH_MAX);
633     size = (path_max < 0 ? 1024
634             : path_max > 10240 ? 10240
635             : path_max);
636
637     /* Get current working directory. */
638     for (;;) {
639         char *buf = xmalloc(size);
640         if (getcwd(buf, size)) {
641             return xrealloc(buf, strlen(buf) + 1);
642         } else {
643             int error = errno;
644             free(buf);
645             if (error != ERANGE) {
646                 VLOG_WARN("getcwd failed (%s)", ovs_strerror(error));
647                 return NULL;
648             }
649             size *= 2;
650         }
651     }
652 }
653
654 static char *
655 all_slashes_name(const char *s)
656 {
657     return xstrdup(s[0] == '/' && s[1] == '/' && s[2] != '/' ? "//"
658                    : s[0] == '/' ? "/"
659                    : ".");
660 }
661
662 /* Returns the directory name portion of 'file_name' as a malloc()'d string,
663  * similar to the POSIX dirname() function but thread-safe. */
664 char *
665 dir_name(const char *file_name)
666 {
667     size_t len = strlen(file_name);
668     while (len > 0 && file_name[len - 1] == '/') {
669         len--;
670     }
671     while (len > 0 && file_name[len - 1] != '/') {
672         len--;
673     }
674     while (len > 0 && file_name[len - 1] == '/') {
675         len--;
676     }
677     return len ? xmemdup0(file_name, len) : all_slashes_name(file_name);
678 }
679
680 /* Returns the file name portion of 'file_name' as a malloc()'d string,
681  * similar to the POSIX basename() function but thread-safe. */
682 char *
683 base_name(const char *file_name)
684 {
685     size_t end, start;
686
687     end = strlen(file_name);
688     while (end > 0 && file_name[end - 1] == '/') {
689         end--;
690     }
691
692     if (!end) {
693         return all_slashes_name(file_name);
694     }
695
696     start = end;
697     while (start > 0 && file_name[start - 1] != '/') {
698         start--;
699     }
700
701     return xmemdup0(file_name + start, end - start);
702 }
703
704 /* If 'file_name' starts with '/', returns a copy of 'file_name'.  Otherwise,
705  * returns an absolute path to 'file_name' considering it relative to 'dir',
706  * which itself must be absolute.  'dir' may be null or the empty string, in
707  * which case the current working directory is used.
708  *
709  * Returns a null pointer if 'dir' is null and getcwd() fails. */
710 char *
711 abs_file_name(const char *dir, const char *file_name)
712 {
713     if (file_name[0] == '/') {
714         return xstrdup(file_name);
715     } else if (dir && dir[0]) {
716         char *separator = dir[strlen(dir) - 1] == '/' ? "" : "/";
717         return xasprintf("%s%s%s", dir, separator, file_name);
718     } else {
719         char *cwd = get_cwd();
720         if (cwd) {
721             char *abs_name = xasprintf("%s/%s", cwd, file_name);
722             free(cwd);
723             return abs_name;
724         } else {
725             return NULL;
726         }
727     }
728 }
729
730 /* Like readlink(), but returns the link name as a null-terminated string in
731  * allocated memory that the caller must eventually free (with free()).
732  * Returns NULL on error, in which case errno is set appropriately. */
733 char *
734 xreadlink(const char *filename)
735 {
736     size_t size;
737
738     for (size = 64; ; size *= 2) {
739         char *buf = xmalloc(size);
740         ssize_t retval = readlink(filename, buf, size);
741         int error = errno;
742
743         if (retval >= 0 && retval < size) {
744             buf[retval] = '\0';
745             return buf;
746         }
747
748         free(buf);
749         if (retval < 0) {
750             errno = error;
751             return NULL;
752         }
753     }
754 }
755
756 /* Returns a version of 'filename' with symlinks in the final component
757  * dereferenced.  This differs from realpath() in that:
758  *
759  *     - 'filename' need not exist.
760  *
761  *     - If 'filename' does exist as a symlink, its referent need not exist.
762  *
763  *     - Only symlinks in the final component of 'filename' are dereferenced.
764  *
765  * The caller must eventually free the returned string (with free()). */
766 char *
767 follow_symlinks(const char *filename)
768 {
769     struct stat s;
770     char *fn;
771     int i;
772
773     fn = xstrdup(filename);
774     for (i = 0; i < 10; i++) {
775         char *linkname;
776         char *next_fn;
777
778         if (lstat(fn, &s) != 0 || !S_ISLNK(s.st_mode)) {
779             return fn;
780         }
781
782         linkname = xreadlink(fn);
783         if (!linkname) {
784             VLOG_WARN("%s: readlink failed (%s)",
785                       filename, ovs_strerror(errno));
786             return fn;
787         }
788
789         if (linkname[0] == '/') {
790             /* Target of symlink is absolute so use it raw. */
791             next_fn = linkname;
792         } else {
793             /* Target of symlink is relative so add to 'fn''s directory. */
794             char *dir = dir_name(fn);
795
796             if (!strcmp(dir, ".")) {
797                 next_fn = linkname;
798             } else {
799                 char *separator = dir[strlen(dir) - 1] == '/' ? "" : "/";
800                 next_fn = xasprintf("%s%s%s", dir, separator, linkname);
801                 free(linkname);
802             }
803
804             free(dir);
805         }
806
807         free(fn);
808         fn = next_fn;
809     }
810
811     VLOG_WARN("%s: too many levels of symlinks", filename);
812     free(fn);
813     return xstrdup(filename);
814 }
815
816 /* Pass a value to this function if it is marked with
817  * __attribute__((warn_unused_result)) and you genuinely want to ignore
818  * its return value.  (Note that every scalar type can be implicitly
819  * converted to bool.) */
820 void ignore(bool x OVS_UNUSED) { }
821
822 /* Returns an appropriate delimiter for inserting just before the 0-based item
823  * 'index' in a list that has 'total' items in it. */
824 const char *
825 english_list_delimiter(size_t index, size_t total)
826 {
827     return (index == 0 ? ""
828             : index < total - 1 ? ", "
829             : total > 2 ? ", and "
830             : " and ");
831 }
832
833 /* Returns the number of trailing 0-bits in 'n'.  Undefined if 'n' == 0. */
834 #if __GNUC__ >= 4
835 /* Defined inline in util.h. */
836 #else
837 /* Returns the number of trailing 0-bits in 'n'.  Undefined if 'n' == 0. */
838 int
839 raw_ctz(uint64_t n)
840 {
841     uint64_t k;
842     int count = 63;
843
844 #define CTZ_STEP(X)                             \
845     k = n << (X);                               \
846     if (k) {                                    \
847         count -= X;                             \
848         n = k;                                  \
849     }
850     CTZ_STEP(32);
851     CTZ_STEP(16);
852     CTZ_STEP(8);
853     CTZ_STEP(4);
854     CTZ_STEP(2);
855     CTZ_STEP(1);
856 #undef CTZ_STEP
857
858     return count;
859 }
860
861 /* Returns the number of leading 0-bits in 'n'.  Undefined if 'n' == 0. */
862 int
863 raw_clz64(uint64_t n)
864 {
865     uint64_t k;
866     int count = 63;
867
868 #define CLZ_STEP(X)                             \
869     k = n >> (X);                               \
870     if (k) {                                    \
871         count -= X;                             \
872         n = k;                                  \
873     }
874     CLZ_STEP(32);
875     CLZ_STEP(16);
876     CLZ_STEP(8);
877     CLZ_STEP(4);
878     CLZ_STEP(2);
879     CLZ_STEP(1);
880 #undef CLZ_STEP
881
882     return count;
883 }
884 #endif
885
886 #if NEED_COUNT_1BITS_8
887 #define INIT1(X)                                \
888     ((((X) & (1 << 0)) != 0) +                  \
889      (((X) & (1 << 1)) != 0) +                  \
890      (((X) & (1 << 2)) != 0) +                  \
891      (((X) & (1 << 3)) != 0) +                  \
892      (((X) & (1 << 4)) != 0) +                  \
893      (((X) & (1 << 5)) != 0) +                  \
894      (((X) & (1 << 6)) != 0) +                  \
895      (((X) & (1 << 7)) != 0))
896 #define INIT2(X)   INIT1(X),  INIT1((X) +  1)
897 #define INIT4(X)   INIT2(X),  INIT2((X) +  2)
898 #define INIT8(X)   INIT4(X),  INIT4((X) +  4)
899 #define INIT16(X)  INIT8(X),  INIT8((X) +  8)
900 #define INIT32(X) INIT16(X), INIT16((X) + 16)
901 #define INIT64(X) INIT32(X), INIT32((X) + 32)
902
903 const uint8_t count_1bits_8[256] = {
904     INIT64(0), INIT64(64), INIT64(128), INIT64(192)
905 };
906 #endif
907
908 /* Returns true if the 'n' bytes starting at 'p' are zeros. */
909 bool
910 is_all_zeros(const uint8_t *p, size_t n)
911 {
912     size_t i;
913
914     for (i = 0; i < n; i++) {
915         if (p[i] != 0x00) {
916             return false;
917         }
918     }
919     return true;
920 }
921
922 /* Returns true if the 'n' bytes starting at 'p' are 0xff. */
923 bool
924 is_all_ones(const uint8_t *p, size_t n)
925 {
926     size_t i;
927
928     for (i = 0; i < n; i++) {
929         if (p[i] != 0xff) {
930             return false;
931         }
932     }
933     return true;
934 }
935
936 /* Copies 'n_bits' bits starting from bit 'src_ofs' in 'src' to the 'n_bits'
937  * starting from bit 'dst_ofs' in 'dst'.  'src' is 'src_len' bytes long and
938  * 'dst' is 'dst_len' bytes long.
939  *
940  * If you consider all of 'src' to be a single unsigned integer in network byte
941  * order, then bit N is the bit with value 2**N.  That is, bit 0 is the bit
942  * with value 1 in src[src_len - 1], bit 1 is the bit with value 2, bit 2 is
943  * the bit with value 4, ..., bit 8 is the bit with value 1 in src[src_len -
944  * 2], and so on.  Similarly for 'dst'.
945  *
946  * Required invariants:
947  *   src_ofs + n_bits <= src_len * 8
948  *   dst_ofs + n_bits <= dst_len * 8
949  *   'src' and 'dst' must not overlap.
950  */
951 void
952 bitwise_copy(const void *src_, unsigned int src_len, unsigned int src_ofs,
953              void *dst_, unsigned int dst_len, unsigned int dst_ofs,
954              unsigned int n_bits)
955 {
956     const uint8_t *src = src_;
957     uint8_t *dst = dst_;
958
959     src += src_len - (src_ofs / 8 + 1);
960     src_ofs %= 8;
961
962     dst += dst_len - (dst_ofs / 8 + 1);
963     dst_ofs %= 8;
964
965     if (src_ofs == 0 && dst_ofs == 0) {
966         unsigned int n_bytes = n_bits / 8;
967         if (n_bytes) {
968             dst -= n_bytes - 1;
969             src -= n_bytes - 1;
970             memcpy(dst, src, n_bytes);
971
972             n_bits %= 8;
973             src--;
974             dst--;
975         }
976         if (n_bits) {
977             uint8_t mask = (1 << n_bits) - 1;
978             *dst = (*dst & ~mask) | (*src & mask);
979         }
980     } else {
981         while (n_bits > 0) {
982             unsigned int max_copy = 8 - MAX(src_ofs, dst_ofs);
983             unsigned int chunk = MIN(n_bits, max_copy);
984             uint8_t mask = ((1 << chunk) - 1) << dst_ofs;
985
986             *dst &= ~mask;
987             *dst |= ((*src >> src_ofs) << dst_ofs) & mask;
988
989             src_ofs += chunk;
990             if (src_ofs == 8) {
991                 src--;
992                 src_ofs = 0;
993             }
994             dst_ofs += chunk;
995             if (dst_ofs == 8) {
996                 dst--;
997                 dst_ofs = 0;
998             }
999             n_bits -= chunk;
1000         }
1001     }
1002 }
1003
1004 /* Zeros the 'n_bits' bits starting from bit 'dst_ofs' in 'dst'.  'dst' is
1005  * 'dst_len' bytes long.
1006  *
1007  * If you consider all of 'dst' to be a single unsigned integer in network byte
1008  * order, then bit N is the bit with value 2**N.  That is, bit 0 is the bit
1009  * with value 1 in dst[dst_len - 1], bit 1 is the bit with value 2, bit 2 is
1010  * the bit with value 4, ..., bit 8 is the bit with value 1 in dst[dst_len -
1011  * 2], and so on.
1012  *
1013  * Required invariant:
1014  *   dst_ofs + n_bits <= dst_len * 8
1015  */
1016 void
1017 bitwise_zero(void *dst_, unsigned int dst_len, unsigned dst_ofs,
1018              unsigned int n_bits)
1019 {
1020     uint8_t *dst = dst_;
1021
1022     if (!n_bits) {
1023         return;
1024     }
1025
1026     dst += dst_len - (dst_ofs / 8 + 1);
1027     dst_ofs %= 8;
1028
1029     if (dst_ofs) {
1030         unsigned int chunk = MIN(n_bits, 8 - dst_ofs);
1031
1032         *dst &= ~(((1 << chunk) - 1) << dst_ofs);
1033
1034         n_bits -= chunk;
1035         if (!n_bits) {
1036             return;
1037         }
1038
1039         dst--;
1040     }
1041
1042     while (n_bits >= 8) {
1043         *dst-- = 0;
1044         n_bits -= 8;
1045     }
1046
1047     if (n_bits) {
1048         *dst &= ~((1 << n_bits) - 1);
1049     }
1050 }
1051
1052 /* Sets to 1 all of the 'n_bits' bits starting from bit 'dst_ofs' in 'dst'.
1053  * 'dst' is 'dst_len' bytes long.
1054  *
1055  * If you consider all of 'dst' to be a single unsigned integer in network byte
1056  * order, then bit N is the bit with value 2**N.  That is, bit 0 is the bit
1057  * with value 1 in dst[dst_len - 1], bit 1 is the bit with value 2, bit 2 is
1058  * the bit with value 4, ..., bit 8 is the bit with value 1 in dst[dst_len -
1059  * 2], and so on.
1060  *
1061  * Required invariant:
1062  *   dst_ofs + n_bits <= dst_len * 8
1063  */
1064 void
1065 bitwise_one(void *dst_, unsigned int dst_len, unsigned dst_ofs,
1066             unsigned int n_bits)
1067 {
1068     uint8_t *dst = dst_;
1069
1070     if (!n_bits) {
1071         return;
1072     }
1073
1074     dst += dst_len - (dst_ofs / 8 + 1);
1075     dst_ofs %= 8;
1076
1077     if (dst_ofs) {
1078         unsigned int chunk = MIN(n_bits, 8 - dst_ofs);
1079
1080         *dst |= ((1 << chunk) - 1) << dst_ofs;
1081
1082         n_bits -= chunk;
1083         if (!n_bits) {
1084             return;
1085         }
1086
1087         dst--;
1088     }
1089
1090     while (n_bits >= 8) {
1091         *dst-- = 0xff;
1092         n_bits -= 8;
1093     }
1094
1095     if (n_bits) {
1096         *dst |= (1 << n_bits) - 1;
1097     }
1098 }
1099
1100 /* Scans the 'n_bits' bits starting from bit 'dst_ofs' in 'dst' for 1-bits.
1101  * Returns false if any 1-bits are found, otherwise true.  'dst' is 'dst_len'
1102  * bytes long.
1103  *
1104  * If you consider all of 'dst' to be a single unsigned integer in network byte
1105  * order, then bit N is the bit with value 2**N.  That is, bit 0 is the bit
1106  * with value 1 in dst[dst_len - 1], bit 1 is the bit with value 2, bit 2 is
1107  * the bit with value 4, ..., bit 8 is the bit with value 1 in dst[dst_len -
1108  * 2], and so on.
1109  *
1110  * Required invariant:
1111  *   dst_ofs + n_bits <= dst_len * 8
1112  */
1113 bool
1114 bitwise_is_all_zeros(const void *p_, unsigned int len, unsigned int ofs,
1115                      unsigned int n_bits)
1116 {
1117     const uint8_t *p = p_;
1118
1119     if (!n_bits) {
1120         return true;
1121     }
1122
1123     p += len - (ofs / 8 + 1);
1124     ofs %= 8;
1125
1126     if (ofs) {
1127         unsigned int chunk = MIN(n_bits, 8 - ofs);
1128
1129         if (*p & (((1 << chunk) - 1) << ofs)) {
1130             return false;
1131         }
1132
1133         n_bits -= chunk;
1134         if (!n_bits) {
1135             return true;
1136         }
1137
1138         p--;
1139     }
1140
1141     while (n_bits >= 8) {
1142         if (*p) {
1143             return false;
1144         }
1145         n_bits -= 8;
1146         p--;
1147     }
1148
1149     if (n_bits && *p & ((1 << n_bits) - 1)) {
1150         return false;
1151     }
1152
1153     return true;
1154 }
1155
1156 /* Copies the 'n_bits' low-order bits of 'value' into the 'n_bits' bits
1157  * starting at bit 'dst_ofs' in 'dst', which is 'dst_len' bytes long.
1158  *
1159  * If you consider all of 'dst' to be a single unsigned integer in network byte
1160  * order, then bit N is the bit with value 2**N.  That is, bit 0 is the bit
1161  * with value 1 in dst[dst_len - 1], bit 1 is the bit with value 2, bit 2 is
1162  * the bit with value 4, ..., bit 8 is the bit with value 1 in dst[dst_len -
1163  * 2], and so on.
1164  *
1165  * Required invariants:
1166  *   dst_ofs + n_bits <= dst_len * 8
1167  *   n_bits <= 64
1168  */
1169 void
1170 bitwise_put(uint64_t value,
1171             void *dst, unsigned int dst_len, unsigned int dst_ofs,
1172             unsigned int n_bits)
1173 {
1174     ovs_be64 n_value = htonll(value);
1175     bitwise_copy(&n_value, sizeof n_value, 0,
1176                  dst, dst_len, dst_ofs,
1177                  n_bits);
1178 }
1179
1180 /* Returns the value of the 'n_bits' bits starting at bit 'src_ofs' in 'src',
1181  * which is 'src_len' bytes long.
1182  *
1183  * If you consider all of 'src' to be a single unsigned integer in network byte
1184  * order, then bit N is the bit with value 2**N.  That is, bit 0 is the bit
1185  * with value 1 in src[src_len - 1], bit 1 is the bit with value 2, bit 2 is
1186  * the bit with value 4, ..., bit 8 is the bit with value 1 in src[src_len -
1187  * 2], and so on.
1188  *
1189  * Required invariants:
1190  *   src_ofs + n_bits <= src_len * 8
1191  *   n_bits <= 64
1192  */
1193 uint64_t
1194 bitwise_get(const void *src, unsigned int src_len,
1195             unsigned int src_ofs, unsigned int n_bits)
1196 {
1197     ovs_be64 value = htonll(0);
1198
1199     bitwise_copy(src, src_len, src_ofs,
1200                  &value, sizeof value, 0,
1201                  n_bits);
1202     return ntohll(value);
1203 }
1204 \f
1205 /* ovs_scan */
1206
1207 struct scan_spec {
1208     unsigned int width;
1209     enum {
1210         SCAN_DISCARD,
1211         SCAN_CHAR,
1212         SCAN_SHORT,
1213         SCAN_INT,
1214         SCAN_LONG,
1215         SCAN_LLONG,
1216         SCAN_INTMAX_T,
1217         SCAN_PTRDIFF_T,
1218         SCAN_SIZE_T
1219     } type;
1220 };
1221
1222 static const char *
1223 skip_spaces(const char *s)
1224 {
1225     while (isspace((unsigned char) *s)) {
1226         s++;
1227     }
1228     return s;
1229 }
1230
1231 static const char *
1232 scan_int(const char *s, const struct scan_spec *spec, int base, va_list *args)
1233 {
1234     const char *start = s;
1235     uintmax_t value;
1236     bool negative;
1237     int n_digits;
1238
1239     negative = *s == '-';
1240     s += *s == '-' || *s == '+';
1241
1242     if ((!base || base == 16) && *s == '0' && (s[1] == 'x' || s[1] == 'X')) {
1243         base = 16;
1244         s += 2;
1245     } else if (!base) {
1246         base = *s == '0' ? 8 : 10;
1247     }
1248
1249     if (s - start >= spec->width) {
1250         return NULL;
1251     }
1252
1253     value = 0;
1254     n_digits = 0;
1255     while (s - start < spec->width) {
1256         int digit = hexit_value(*s);
1257
1258         if (digit < 0 || digit >= base) {
1259             break;
1260         }
1261         value = value * base + digit;
1262         n_digits++;
1263         s++;
1264     }
1265     if (!n_digits) {
1266         return NULL;
1267     }
1268
1269     if (negative) {
1270         value = -value;
1271     }
1272
1273     switch (spec->type) {
1274     case SCAN_DISCARD:
1275         break;
1276     case SCAN_CHAR:
1277         *va_arg(*args, char *) = value;
1278         break;
1279     case SCAN_SHORT:
1280         *va_arg(*args, short int *) = value;
1281         break;
1282     case SCAN_INT:
1283         *va_arg(*args, int *) = value;
1284         break;
1285     case SCAN_LONG:
1286         *va_arg(*args, long int *) = value;
1287         break;
1288     case SCAN_LLONG:
1289         *va_arg(*args, long long int *) = value;
1290         break;
1291     case SCAN_INTMAX_T:
1292         *va_arg(*args, intmax_t *) = value;
1293         break;
1294     case SCAN_PTRDIFF_T:
1295         *va_arg(*args, ptrdiff_t *) = value;
1296         break;
1297     case SCAN_SIZE_T:
1298         *va_arg(*args, size_t *) = value;
1299         break;
1300     }
1301     return s;
1302 }
1303
1304 static const char *
1305 skip_digits(const char *s)
1306 {
1307     while (*s >= '0' && *s <= '9') {
1308         s++;
1309     }
1310     return s;
1311 }
1312
1313 static const char *
1314 scan_float(const char *s, const struct scan_spec *spec, va_list *args)
1315 {
1316     const char *start = s;
1317     long double value;
1318     char *tail;
1319     char *copy;
1320     bool ok;
1321
1322     s += *s == '+' || *s == '-';
1323     s = skip_digits(s);
1324     if (*s == '.') {
1325         s = skip_digits(s + 1);
1326     }
1327     if (*s == 'e' || *s == 'E') {
1328         s++;
1329         s += *s == '+' || *s == '-';
1330         s = skip_digits(s);
1331     }
1332
1333     if (s - start > spec->width) {
1334         s = start + spec->width;
1335     }
1336
1337     copy = xmemdup0(start, s - start);
1338     value = strtold(copy, &tail);
1339     ok = *tail == '\0';
1340     free(copy);
1341     if (!ok) {
1342         return NULL;
1343     }
1344
1345     switch (spec->type) {
1346     case SCAN_DISCARD:
1347         break;
1348     case SCAN_INT:
1349         *va_arg(*args, float *) = value;
1350         break;
1351     case SCAN_LONG:
1352         *va_arg(*args, double *) = value;
1353         break;
1354     case SCAN_LLONG:
1355         *va_arg(*args, long double *) = value;
1356         break;
1357
1358     case SCAN_CHAR:
1359     case SCAN_SHORT:
1360     case SCAN_INTMAX_T:
1361     case SCAN_PTRDIFF_T:
1362     case SCAN_SIZE_T:
1363         NOT_REACHED();
1364     }
1365     return s;
1366 }
1367
1368 static void
1369 scan_output_string(const struct scan_spec *spec,
1370                    const char *s, size_t n,
1371                    va_list *args)
1372 {
1373     if (spec->type != SCAN_DISCARD) {
1374         char *out = va_arg(*args, char *);
1375         memcpy(out, s, n);
1376         out[n] = '\0';
1377     }
1378 }
1379
1380 static const char *
1381 scan_string(const char *s, const struct scan_spec *spec, va_list *args)
1382 {
1383     size_t n;
1384
1385     for (n = 0; n < spec->width; n++) {
1386         if (!s[n] || isspace((unsigned char) s[n])) {
1387             break;
1388         }
1389     }
1390     if (!n) {
1391         return NULL;
1392     }
1393
1394     scan_output_string(spec, s, n, args);
1395     return s + n;
1396 }
1397
1398 static const char *
1399 parse_scanset(const char *p_, unsigned long *set, bool *complemented)
1400 {
1401     const uint8_t *p = (const uint8_t *) p_;
1402
1403     *complemented = *p == '^';
1404     p += *complemented;
1405
1406     if (*p == ']') {
1407         bitmap_set1(set, ']');
1408         p++;
1409     }
1410
1411     while (*p && *p != ']') {
1412         if (p[1] == '-' && p[2] != ']' && p[2] > *p) {
1413             bitmap_set_multiple(set, *p, p[2] - *p + 1, true);
1414             p += 3;
1415         } else {
1416             bitmap_set1(set, *p++);
1417         }
1418     }
1419     if (*p == ']') {
1420         p++;
1421     }
1422     return (const char *) p;
1423 }
1424
1425 static const char *
1426 scan_set(const char *s, const struct scan_spec *spec, const char **pp,
1427          va_list *args)
1428 {
1429     unsigned long set[BITMAP_N_LONGS(UCHAR_MAX + 1)];
1430     bool complemented;
1431     unsigned int n;
1432
1433     /* Parse the scan set. */
1434     memset(set, 0, sizeof set);
1435     *pp = parse_scanset(*pp, set, &complemented);
1436
1437     /* Parse the data. */
1438     n = 0;
1439     while (s[n]
1440            && bitmap_is_set(set, (unsigned char) s[n]) == !complemented
1441            && n < spec->width) {
1442         n++;
1443     }
1444     if (!n) {
1445         return NULL;
1446     }
1447     scan_output_string(spec, s, n, args);
1448     return s + n;
1449 }
1450
1451 static const char *
1452 scan_chars(const char *s, const struct scan_spec *spec, va_list *args)
1453 {
1454     unsigned int n = spec->width == UINT_MAX ? 1 : spec->width;
1455
1456     if (strlen(s) < n) {
1457         return NULL;
1458     }
1459     if (spec->type != SCAN_DISCARD) {
1460         memcpy(va_arg(*args, char *), s, n);
1461     }
1462     return s + n;
1463 }
1464
1465 /* This is an implementation of the standard sscanf() function, with the
1466  * following exceptions:
1467  *
1468  *   - It returns true if the entire template was successfully scanned and
1469  *     converted, false if any conversion failed.
1470  *
1471  *   - The standard doesn't define sscanf() behavior when an out-of-range value
1472  *     is scanned, e.g. if a "%"PRIi8 conversion scans "-1" or "0x1ff".  Some
1473  *     implementations consider this an error and stop scanning.  This
1474  *     implementation never considers an out-of-range value an error; instead,
1475  *     it stores the least-significant bits of the converted value in the
1476  *     destination, e.g. the value 255 for both examples earlier.
1477  *
1478  *   - Only single-byte characters are supported, that is, the 'l' modifier
1479  *     on %s, %[, and %c is not supported.  The GNU extension 'a' modifier is
1480  *     also not supported.
1481  *
1482  *   - %p is not supported.
1483  */
1484 bool
1485 ovs_scan(const char *s, const char *template, ...)
1486 {
1487     const char *const start = s;
1488     bool ok = false;
1489     const char *p;
1490     va_list args;
1491
1492     va_start(args, template);
1493     p = template;
1494     while (*p != '\0') {
1495         struct scan_spec spec;
1496         unsigned char c = *p++;
1497         bool discard;
1498
1499         if (isspace(c)) {
1500             s = skip_spaces(s);
1501             continue;
1502         } else if (c != '%') {
1503             if (*s != c) {
1504                 goto exit;
1505             }
1506             s++;
1507             continue;
1508         } else if (*p == '%') {
1509             if (*s++ != '%') {
1510                 goto exit;
1511             }
1512             p++;
1513             continue;
1514         }
1515
1516         /* Parse '*' flag. */
1517         discard = *p == '*';
1518         p += discard;
1519
1520         /* Parse field width. */
1521         spec.width = 0;
1522         while (*p >= '0' && *p <= '9') {
1523             spec.width = spec.width * 10 + (*p++ - '0');
1524         }
1525         if (spec.width == 0) {
1526             spec.width = UINT_MAX;
1527         }
1528
1529         /* Parse type modifier. */
1530         switch (*p) {
1531         case 'h':
1532             if (p[1] == 'h') {
1533                 spec.type = SCAN_CHAR;
1534                 p += 2;
1535             } else {
1536                 spec.type = SCAN_SHORT;
1537                 p++;
1538             }
1539             break;
1540
1541         case 'j':
1542             spec.type = SCAN_INTMAX_T;
1543             p++;
1544             break;
1545
1546         case 'l':
1547             if (p[1] == 'l') {
1548                 spec.type = SCAN_LLONG;
1549                 p += 2;
1550             } else {
1551                 spec.type = SCAN_LONG;
1552                 p++;
1553             }
1554             break;
1555
1556         case 'L':
1557         case 'q':
1558             spec.type = SCAN_LLONG;
1559             p++;
1560             break;
1561
1562         case 't':
1563             spec.type = SCAN_PTRDIFF_T;
1564             p++;
1565             break;
1566
1567         case 'z':
1568             spec.type = SCAN_SIZE_T;
1569             p++;
1570             break;
1571
1572         default:
1573             spec.type = SCAN_INT;
1574             break;
1575         }
1576
1577         if (discard) {
1578             spec.type = SCAN_DISCARD;
1579         }
1580
1581         c = *p++;
1582         if (c != 'c' && c != 'n' && c != '[') {
1583             s = skip_spaces(s);
1584         }
1585         switch (c) {
1586         case 'd':
1587             s = scan_int(s, &spec, 10, &args);
1588             break;
1589
1590         case 'i':
1591             s = scan_int(s, &spec, 0, &args);
1592             break;
1593
1594         case 'o':
1595             s = scan_int(s, &spec, 8, &args);
1596             break;
1597
1598         case 'u':
1599             s = scan_int(s, &spec, 10, &args);
1600             break;
1601
1602         case 'x':
1603         case 'X':
1604             s = scan_int(s, &spec, 16, &args);
1605             break;
1606
1607         case 'e':
1608         case 'f':
1609         case 'g':
1610         case 'E':
1611         case 'G':
1612             s = scan_float(s, &spec, &args);
1613             break;
1614
1615         case 's':
1616             s = scan_string(s, &spec, &args);
1617             break;
1618
1619         case '[':
1620             s = scan_set(s, &spec, &p, &args);
1621             break;
1622
1623         case 'c':
1624             s = scan_chars(s, &spec, &args);
1625             break;
1626
1627         case 'n':
1628             if (spec.type != SCAN_DISCARD) {
1629                 *va_arg(args, int *) = s - start;
1630             }
1631             break;
1632         }
1633
1634         if (!s) {
1635             goto exit;
1636         }
1637     }
1638     ok = true;
1639
1640 exit:
1641     va_end(args);
1642     return ok;
1643 }
1644