cutils.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851
  1. /*
  2. * Simple C functions to supplement the C library
  3. *
  4. * Copyright (c) 2006 Fabrice Bellard
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy
  7. * of this software and associated documentation files (the "Software"), to deal
  8. * in the Software without restriction, including without limitation the rights
  9. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. * copies of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  19. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. * THE SOFTWARE.
  23. */
  24. #include "qemu/osdep.h"
  25. #include "qemu/host-utils.h"
  26. #include <math.h>
  27. #include "qemu-common.h"
  28. #include "qemu/sockets.h"
  29. #include "qemu/iov.h"
  30. #include "net/net.h"
  31. #include "qemu/ctype.h"
  32. #include "qemu/cutils.h"
  33. #include "qemu/error-report.h"
  34. void strpadcpy(char *buf, int buf_size, const char *str, char pad)
  35. {
  36. int len = qemu_strnlen(str, buf_size);
  37. memcpy(buf, str, len);
  38. memset(buf + len, pad, buf_size - len);
  39. }
  40. void pstrcpy(char *buf, int buf_size, const char *str)
  41. {
  42. int c;
  43. char *q = buf;
  44. if (buf_size <= 0)
  45. return;
  46. for(;;) {
  47. c = *str++;
  48. if (c == 0 || q >= buf + buf_size - 1)
  49. break;
  50. *q++ = c;
  51. }
  52. *q = '\0';
  53. }
  54. /* strcat and truncate. */
  55. char *pstrcat(char *buf, int buf_size, const char *s)
  56. {
  57. int len;
  58. len = strlen(buf);
  59. if (len < buf_size)
  60. pstrcpy(buf + len, buf_size - len, s);
  61. return buf;
  62. }
  63. int strstart(const char *str, const char *val, const char **ptr)
  64. {
  65. const char *p, *q;
  66. p = str;
  67. q = val;
  68. while (*q != '\0') {
  69. if (*p != *q)
  70. return 0;
  71. p++;
  72. q++;
  73. }
  74. if (ptr)
  75. *ptr = p;
  76. return 1;
  77. }
  78. int stristart(const char *str, const char *val, const char **ptr)
  79. {
  80. const char *p, *q;
  81. p = str;
  82. q = val;
  83. while (*q != '\0') {
  84. if (qemu_toupper(*p) != qemu_toupper(*q))
  85. return 0;
  86. p++;
  87. q++;
  88. }
  89. if (ptr)
  90. *ptr = p;
  91. return 1;
  92. }
  93. /* XXX: use host strnlen if available ? */
  94. int qemu_strnlen(const char *s, int max_len)
  95. {
  96. int i;
  97. for(i = 0; i < max_len; i++) {
  98. if (s[i] == '\0') {
  99. break;
  100. }
  101. }
  102. return i;
  103. }
  104. char *qemu_strsep(char **input, const char *delim)
  105. {
  106. char *result = *input;
  107. if (result != NULL) {
  108. char *p;
  109. for (p = result; *p != '\0'; p++) {
  110. if (strchr(delim, *p)) {
  111. break;
  112. }
  113. }
  114. if (*p == '\0') {
  115. *input = NULL;
  116. } else {
  117. *p = '\0';
  118. *input = p + 1;
  119. }
  120. }
  121. return result;
  122. }
  123. time_t mktimegm(struct tm *tm)
  124. {
  125. time_t t;
  126. int y = tm->tm_year + 1900, m = tm->tm_mon + 1, d = tm->tm_mday;
  127. if (m < 3) {
  128. m += 12;
  129. y--;
  130. }
  131. t = 86400ULL * (d + (153 * m - 457) / 5 + 365 * y + y / 4 - y / 100 +
  132. y / 400 - 719469);
  133. t += 3600 * tm->tm_hour + 60 * tm->tm_min + tm->tm_sec;
  134. return t;
  135. }
  136. /*
  137. * Make sure data goes on disk, but if possible do not bother to
  138. * write out the inode just for timestamp updates.
  139. *
  140. * Unfortunately even in 2009 many operating systems do not support
  141. * fdatasync and have to fall back to fsync.
  142. */
  143. int qemu_fdatasync(int fd)
  144. {
  145. #ifdef CONFIG_FDATASYNC
  146. return fdatasync(fd);
  147. #else
  148. return fsync(fd);
  149. #endif
  150. }
  151. #ifndef _WIN32
  152. /* Sets a specific flag */
  153. int fcntl_setfl(int fd, int flag)
  154. {
  155. int flags;
  156. flags = fcntl(fd, F_GETFL);
  157. if (flags == -1)
  158. return -errno;
  159. if (fcntl(fd, F_SETFL, flags | flag) == -1)
  160. return -errno;
  161. return 0;
  162. }
  163. #endif
  164. static int64_t suffix_mul(char suffix, int64_t unit)
  165. {
  166. switch (qemu_toupper(suffix)) {
  167. case 'B':
  168. return 1;
  169. case 'K':
  170. return unit;
  171. case 'M':
  172. return unit * unit;
  173. case 'G':
  174. return unit * unit * unit;
  175. case 'T':
  176. return unit * unit * unit * unit;
  177. case 'P':
  178. return unit * unit * unit * unit * unit;
  179. case 'E':
  180. return unit * unit * unit * unit * unit * unit;
  181. }
  182. return -1;
  183. }
  184. /*
  185. * Convert string to bytes, allowing either B/b for bytes, K/k for KB,
  186. * M/m for MB, G/g for GB or T/t for TB. End pointer will be returned
  187. * in *end, if not NULL. Return -ERANGE on overflow, and -EINVAL on
  188. * other error.
  189. */
  190. static int do_strtosz(const char *nptr, const char **end,
  191. const char default_suffix, int64_t unit,
  192. uint64_t *result)
  193. {
  194. int retval;
  195. const char *endptr;
  196. unsigned char c;
  197. int mul_required = 0;
  198. double val, mul, integral, fraction;
  199. retval = qemu_strtod_finite(nptr, &endptr, &val);
  200. if (retval) {
  201. goto out;
  202. }
  203. fraction = modf(val, &integral);
  204. if (fraction != 0) {
  205. mul_required = 1;
  206. }
  207. c = *endptr;
  208. mul = suffix_mul(c, unit);
  209. if (mul >= 0) {
  210. endptr++;
  211. } else {
  212. mul = suffix_mul(default_suffix, unit);
  213. assert(mul >= 0);
  214. }
  215. if (mul == 1 && mul_required) {
  216. retval = -EINVAL;
  217. goto out;
  218. }
  219. /*
  220. * Values near UINT64_MAX overflow to 2**64 when converting to double
  221. * precision. Compare against the maximum representable double precision
  222. * value below 2**64, computed as "the next value after 2**64 (0x1p64) in
  223. * the direction of 0".
  224. */
  225. if ((val * mul > nextafter(0x1p64, 0)) || val < 0) {
  226. retval = -ERANGE;
  227. goto out;
  228. }
  229. *result = val * mul;
  230. retval = 0;
  231. out:
  232. if (end) {
  233. *end = endptr;
  234. } else if (*endptr) {
  235. retval = -EINVAL;
  236. }
  237. return retval;
  238. }
  239. int qemu_strtosz(const char *nptr, const char **end, uint64_t *result)
  240. {
  241. return do_strtosz(nptr, end, 'B', 1024, result);
  242. }
  243. int qemu_strtosz_MiB(const char *nptr, const char **end, uint64_t *result)
  244. {
  245. return do_strtosz(nptr, end, 'M', 1024, result);
  246. }
  247. int qemu_strtosz_metric(const char *nptr, const char **end, uint64_t *result)
  248. {
  249. return do_strtosz(nptr, end, 'B', 1000, result);
  250. }
  251. /**
  252. * Helper function for error checking after strtol() and the like
  253. */
  254. static int check_strtox_error(const char *nptr, char *ep,
  255. const char **endptr, int libc_errno)
  256. {
  257. assert(ep >= nptr);
  258. if (endptr) {
  259. *endptr = ep;
  260. }
  261. /* Turn "no conversion" into an error */
  262. if (libc_errno == 0 && ep == nptr) {
  263. return -EINVAL;
  264. }
  265. /* Fail when we're expected to consume the string, but didn't */
  266. if (!endptr && *ep) {
  267. return -EINVAL;
  268. }
  269. return -libc_errno;
  270. }
  271. /**
  272. * Convert string @nptr to an integer, and store it in @result.
  273. *
  274. * This is a wrapper around strtol() that is harder to misuse.
  275. * Semantics of @nptr, @endptr, @base match strtol() with differences
  276. * noted below.
  277. *
  278. * @nptr may be null, and no conversion is performed then.
  279. *
  280. * If no conversion is performed, store @nptr in *@endptr and return
  281. * -EINVAL.
  282. *
  283. * If @endptr is null, and the string isn't fully converted, return
  284. * -EINVAL. This is the case when the pointer that would be stored in
  285. * a non-null @endptr points to a character other than '\0'.
  286. *
  287. * If the conversion overflows @result, store INT_MAX in @result,
  288. * and return -ERANGE.
  289. *
  290. * If the conversion underflows @result, store INT_MIN in @result,
  291. * and return -ERANGE.
  292. *
  293. * Else store the converted value in @result, and return zero.
  294. */
  295. int qemu_strtoi(const char *nptr, const char **endptr, int base,
  296. int *result)
  297. {
  298. char *ep;
  299. long long lresult;
  300. assert((unsigned) base <= 36 && base != 1);
  301. if (!nptr) {
  302. if (endptr) {
  303. *endptr = nptr;
  304. }
  305. return -EINVAL;
  306. }
  307. errno = 0;
  308. lresult = strtoll(nptr, &ep, base);
  309. if (lresult < INT_MIN) {
  310. *result = INT_MIN;
  311. errno = ERANGE;
  312. } else if (lresult > INT_MAX) {
  313. *result = INT_MAX;
  314. errno = ERANGE;
  315. } else {
  316. *result = lresult;
  317. }
  318. return check_strtox_error(nptr, ep, endptr, errno);
  319. }
  320. /**
  321. * Convert string @nptr to an unsigned integer, and store it in @result.
  322. *
  323. * This is a wrapper around strtoul() that is harder to misuse.
  324. * Semantics of @nptr, @endptr, @base match strtoul() with differences
  325. * noted below.
  326. *
  327. * @nptr may be null, and no conversion is performed then.
  328. *
  329. * If no conversion is performed, store @nptr in *@endptr and return
  330. * -EINVAL.
  331. *
  332. * If @endptr is null, and the string isn't fully converted, return
  333. * -EINVAL. This is the case when the pointer that would be stored in
  334. * a non-null @endptr points to a character other than '\0'.
  335. *
  336. * If the conversion overflows @result, store UINT_MAX in @result,
  337. * and return -ERANGE.
  338. *
  339. * Else store the converted value in @result, and return zero.
  340. *
  341. * Note that a number with a leading minus sign gets converted without
  342. * the minus sign, checked for overflow (see above), then negated (in
  343. * @result's type). This is exactly how strtoul() works.
  344. */
  345. int qemu_strtoui(const char *nptr, const char **endptr, int base,
  346. unsigned int *result)
  347. {
  348. char *ep;
  349. long long lresult;
  350. assert((unsigned) base <= 36 && base != 1);
  351. if (!nptr) {
  352. if (endptr) {
  353. *endptr = nptr;
  354. }
  355. return -EINVAL;
  356. }
  357. errno = 0;
  358. lresult = strtoull(nptr, &ep, base);
  359. /* Windows returns 1 for negative out-of-range values. */
  360. if (errno == ERANGE) {
  361. *result = -1;
  362. } else {
  363. if (lresult > UINT_MAX) {
  364. *result = UINT_MAX;
  365. errno = ERANGE;
  366. } else if (lresult < INT_MIN) {
  367. *result = UINT_MAX;
  368. errno = ERANGE;
  369. } else {
  370. *result = lresult;
  371. }
  372. }
  373. return check_strtox_error(nptr, ep, endptr, errno);
  374. }
  375. /**
  376. * Convert string @nptr to a long integer, and store it in @result.
  377. *
  378. * This is a wrapper around strtol() that is harder to misuse.
  379. * Semantics of @nptr, @endptr, @base match strtol() with differences
  380. * noted below.
  381. *
  382. * @nptr may be null, and no conversion is performed then.
  383. *
  384. * If no conversion is performed, store @nptr in *@endptr and return
  385. * -EINVAL.
  386. *
  387. * If @endptr is null, and the string isn't fully converted, return
  388. * -EINVAL. This is the case when the pointer that would be stored in
  389. * a non-null @endptr points to a character other than '\0'.
  390. *
  391. * If the conversion overflows @result, store LONG_MAX in @result,
  392. * and return -ERANGE.
  393. *
  394. * If the conversion underflows @result, store LONG_MIN in @result,
  395. * and return -ERANGE.
  396. *
  397. * Else store the converted value in @result, and return zero.
  398. */
  399. int qemu_strtol(const char *nptr, const char **endptr, int base,
  400. long *result)
  401. {
  402. char *ep;
  403. assert((unsigned) base <= 36 && base != 1);
  404. if (!nptr) {
  405. if (endptr) {
  406. *endptr = nptr;
  407. }
  408. return -EINVAL;
  409. }
  410. errno = 0;
  411. *result = strtol(nptr, &ep, base);
  412. return check_strtox_error(nptr, ep, endptr, errno);
  413. }
  414. /**
  415. * Convert string @nptr to an unsigned long, and store it in @result.
  416. *
  417. * This is a wrapper around strtoul() that is harder to misuse.
  418. * Semantics of @nptr, @endptr, @base match strtoul() with differences
  419. * noted below.
  420. *
  421. * @nptr may be null, and no conversion is performed then.
  422. *
  423. * If no conversion is performed, store @nptr in *@endptr and return
  424. * -EINVAL.
  425. *
  426. * If @endptr is null, and the string isn't fully converted, return
  427. * -EINVAL. This is the case when the pointer that would be stored in
  428. * a non-null @endptr points to a character other than '\0'.
  429. *
  430. * If the conversion overflows @result, store ULONG_MAX in @result,
  431. * and return -ERANGE.
  432. *
  433. * Else store the converted value in @result, and return zero.
  434. *
  435. * Note that a number with a leading minus sign gets converted without
  436. * the minus sign, checked for overflow (see above), then negated (in
  437. * @result's type). This is exactly how strtoul() works.
  438. */
  439. int qemu_strtoul(const char *nptr, const char **endptr, int base,
  440. unsigned long *result)
  441. {
  442. char *ep;
  443. assert((unsigned) base <= 36 && base != 1);
  444. if (!nptr) {
  445. if (endptr) {
  446. *endptr = nptr;
  447. }
  448. return -EINVAL;
  449. }
  450. errno = 0;
  451. *result = strtoul(nptr, &ep, base);
  452. /* Windows returns 1 for negative out-of-range values. */
  453. if (errno == ERANGE) {
  454. *result = -1;
  455. }
  456. return check_strtox_error(nptr, ep, endptr, errno);
  457. }
  458. /**
  459. * Convert string @nptr to an int64_t.
  460. *
  461. * Works like qemu_strtol(), except it stores INT64_MAX on overflow,
  462. * and INT_MIN on underflow.
  463. */
  464. int qemu_strtoi64(const char *nptr, const char **endptr, int base,
  465. int64_t *result)
  466. {
  467. char *ep;
  468. assert((unsigned) base <= 36 && base != 1);
  469. if (!nptr) {
  470. if (endptr) {
  471. *endptr = nptr;
  472. }
  473. return -EINVAL;
  474. }
  475. errno = 0;
  476. /* FIXME This assumes int64_t is long long */
  477. *result = strtoll(nptr, &ep, base);
  478. return check_strtox_error(nptr, ep, endptr, errno);
  479. }
  480. /**
  481. * Convert string @nptr to an uint64_t.
  482. *
  483. * Works like qemu_strtoul(), except it stores UINT64_MAX on overflow.
  484. */
  485. int qemu_strtou64(const char *nptr, const char **endptr, int base,
  486. uint64_t *result)
  487. {
  488. char *ep;
  489. assert((unsigned) base <= 36 && base != 1);
  490. if (!nptr) {
  491. if (endptr) {
  492. *endptr = nptr;
  493. }
  494. return -EINVAL;
  495. }
  496. errno = 0;
  497. /* FIXME This assumes uint64_t is unsigned long long */
  498. *result = strtoull(nptr, &ep, base);
  499. /* Windows returns 1 for negative out-of-range values. */
  500. if (errno == ERANGE) {
  501. *result = -1;
  502. }
  503. return check_strtox_error(nptr, ep, endptr, errno);
  504. }
  505. /**
  506. * Convert string @nptr to a double.
  507. *
  508. * This is a wrapper around strtod() that is harder to misuse.
  509. * Semantics of @nptr and @endptr match strtod() with differences
  510. * noted below.
  511. *
  512. * @nptr may be null, and no conversion is performed then.
  513. *
  514. * If no conversion is performed, store @nptr in *@endptr and return
  515. * -EINVAL.
  516. *
  517. * If @endptr is null, and the string isn't fully converted, return
  518. * -EINVAL. This is the case when the pointer that would be stored in
  519. * a non-null @endptr points to a character other than '\0'.
  520. *
  521. * If the conversion overflows, store +/-HUGE_VAL in @result, depending
  522. * on the sign, and return -ERANGE.
  523. *
  524. * If the conversion underflows, store +/-0.0 in @result, depending on the
  525. * sign, and return -ERANGE.
  526. *
  527. * Else store the converted value in @result, and return zero.
  528. */
  529. int qemu_strtod(const char *nptr, const char **endptr, double *result)
  530. {
  531. char *ep;
  532. if (!nptr) {
  533. if (endptr) {
  534. *endptr = nptr;
  535. }
  536. return -EINVAL;
  537. }
  538. errno = 0;
  539. *result = strtod(nptr, &ep);
  540. return check_strtox_error(nptr, ep, endptr, errno);
  541. }
  542. /**
  543. * Convert string @nptr to a finite double.
  544. *
  545. * Works like qemu_strtod(), except that "NaN" and "inf" are rejected
  546. * with -EINVAL and no conversion is performed.
  547. */
  548. int qemu_strtod_finite(const char *nptr, const char **endptr, double *result)
  549. {
  550. double tmp;
  551. int ret;
  552. ret = qemu_strtod(nptr, endptr, &tmp);
  553. if (!ret && !isfinite(tmp)) {
  554. if (endptr) {
  555. *endptr = nptr;
  556. }
  557. ret = -EINVAL;
  558. }
  559. if (ret != -EINVAL) {
  560. *result = tmp;
  561. }
  562. return ret;
  563. }
  564. /**
  565. * Searches for the first occurrence of 'c' in 's', and returns a pointer
  566. * to the trailing null byte if none was found.
  567. */
  568. #ifndef HAVE_STRCHRNUL
  569. const char *qemu_strchrnul(const char *s, int c)
  570. {
  571. const char *e = strchr(s, c);
  572. if (!e) {
  573. e = s + strlen(s);
  574. }
  575. return e;
  576. }
  577. #endif
  578. /**
  579. * parse_uint:
  580. *
  581. * @s: String to parse
  582. * @value: Destination for parsed integer value
  583. * @endptr: Destination for pointer to first character not consumed
  584. * @base: integer base, between 2 and 36 inclusive, or 0
  585. *
  586. * Parse unsigned integer
  587. *
  588. * Parsed syntax is like strtoull()'s: arbitrary whitespace, a single optional
  589. * '+' or '-', an optional "0x" if @base is 0 or 16, one or more digits.
  590. *
  591. * If @s is null, or @base is invalid, or @s doesn't start with an
  592. * integer in the syntax above, set *@value to 0, *@endptr to @s, and
  593. * return -EINVAL.
  594. *
  595. * Set *@endptr to point right beyond the parsed integer (even if the integer
  596. * overflows or is negative, all digits will be parsed and *@endptr will
  597. * point right beyond them).
  598. *
  599. * If the integer is negative, set *@value to 0, and return -ERANGE.
  600. *
  601. * If the integer overflows unsigned long long, set *@value to
  602. * ULLONG_MAX, and return -ERANGE.
  603. *
  604. * Else, set *@value to the parsed integer, and return 0.
  605. */
  606. int parse_uint(const char *s, unsigned long long *value, char **endptr,
  607. int base)
  608. {
  609. int r = 0;
  610. char *endp = (char *)s;
  611. unsigned long long val = 0;
  612. assert((unsigned) base <= 36 && base != 1);
  613. if (!s) {
  614. r = -EINVAL;
  615. goto out;
  616. }
  617. errno = 0;
  618. val = strtoull(s, &endp, base);
  619. if (errno) {
  620. r = -errno;
  621. goto out;
  622. }
  623. if (endp == s) {
  624. r = -EINVAL;
  625. goto out;
  626. }
  627. /* make sure we reject negative numbers: */
  628. while (qemu_isspace(*s)) {
  629. s++;
  630. }
  631. if (*s == '-') {
  632. val = 0;
  633. r = -ERANGE;
  634. goto out;
  635. }
  636. out:
  637. *value = val;
  638. *endptr = endp;
  639. return r;
  640. }
  641. /**
  642. * parse_uint_full:
  643. *
  644. * @s: String to parse
  645. * @value: Destination for parsed integer value
  646. * @base: integer base, between 2 and 36 inclusive, or 0
  647. *
  648. * Parse unsigned integer from entire string
  649. *
  650. * Have the same behavior of parse_uint(), but with an additional check
  651. * for additional data after the parsed number. If extra characters are present
  652. * after the parsed number, the function will return -EINVAL, and *@v will
  653. * be set to 0.
  654. */
  655. int parse_uint_full(const char *s, unsigned long long *value, int base)
  656. {
  657. char *endp;
  658. int r;
  659. r = parse_uint(s, value, &endp, base);
  660. if (r < 0) {
  661. return r;
  662. }
  663. if (*endp) {
  664. *value = 0;
  665. return -EINVAL;
  666. }
  667. return 0;
  668. }
  669. int qemu_parse_fd(const char *param)
  670. {
  671. long fd;
  672. char *endptr;
  673. errno = 0;
  674. fd = strtol(param, &endptr, 10);
  675. if (param == endptr /* no conversion performed */ ||
  676. errno != 0 /* not representable as long; possibly others */ ||
  677. *endptr != '\0' /* final string not empty */ ||
  678. fd < 0 /* invalid as file descriptor */ ||
  679. fd > INT_MAX /* not representable as int */) {
  680. return -1;
  681. }
  682. return fd;
  683. }
  684. /*
  685. * Implementation of ULEB128 (http://en.wikipedia.org/wiki/LEB128)
  686. * Input is limited to 14-bit numbers
  687. */
  688. int uleb128_encode_small(uint8_t *out, uint32_t n)
  689. {
  690. g_assert(n <= 0x3fff);
  691. if (n < 0x80) {
  692. *out = n;
  693. return 1;
  694. } else {
  695. *out++ = (n & 0x7f) | 0x80;
  696. *out = n >> 7;
  697. return 2;
  698. }
  699. }
  700. int uleb128_decode_small(const uint8_t *in, uint32_t *n)
  701. {
  702. if (!(*in & 0x80)) {
  703. *n = *in;
  704. return 1;
  705. } else {
  706. *n = *in++ & 0x7f;
  707. /* we exceed 14 bit number */
  708. if (*in & 0x80) {
  709. return -1;
  710. }
  711. *n |= *in << 7;
  712. return 2;
  713. }
  714. }
  715. /*
  716. * helper to parse debug environment variables
  717. */
  718. int parse_debug_env(const char *name, int max, int initial)
  719. {
  720. char *debug_env = getenv(name);
  721. char *inv = NULL;
  722. long debug;
  723. if (!debug_env) {
  724. return initial;
  725. }
  726. errno = 0;
  727. debug = strtol(debug_env, &inv, 10);
  728. if (inv == debug_env) {
  729. return initial;
  730. }
  731. if (debug < 0 || debug > max || errno != 0) {
  732. warn_report("%s not in [0, %d]", name, max);
  733. return initial;
  734. }
  735. return debug;
  736. }
  737. /*
  738. * Helper to print ethernet mac address
  739. */
  740. const char *qemu_ether_ntoa(const MACAddr *mac)
  741. {
  742. static char ret[18];
  743. snprintf(ret, sizeof(ret), "%02x:%02x:%02x:%02x:%02x:%02x",
  744. mac->a[0], mac->a[1], mac->a[2], mac->a[3], mac->a[4], mac->a[5]);
  745. return ret;
  746. }
  747. /*
  748. * Return human readable string for size @val.
  749. * @val can be anything that uint64_t allows (no more than "16 EiB").
  750. * Use IEC binary units like KiB, MiB, and so forth.
  751. * Caller is responsible for passing it to g_free().
  752. */
  753. char *size_to_str(uint64_t val)
  754. {
  755. static const char *suffixes[] = { "", "Ki", "Mi", "Gi", "Ti", "Pi", "Ei" };
  756. uint64_t div;
  757. int i;
  758. /*
  759. * The exponent (returned in i) minus one gives us
  760. * floor(log2(val * 1024 / 1000). The correction makes us
  761. * switch to the higher power when the integer part is >= 1000.
  762. * (see e41b509d68afb1f for more info)
  763. */
  764. frexp(val / (1000.0 / 1024.0), &i);
  765. i = (i - 1) / 10;
  766. div = 1ULL << (i * 10);
  767. return g_strdup_printf("%0.3g %sB", (double)val / div, suffixes[i]);
  768. }
  769. int qemu_pstrcmp0(const char **str1, const char **str2)
  770. {
  771. return g_strcmp0(*str1, *str2);
  772. }