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