cutils.c 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  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-common.h"
  25. #include "host-utils.h"
  26. #include <math.h>
  27. #include "qemu_socket.h"
  28. #include "iov.h"
  29. void strpadcpy(char *buf, int buf_size, const char *str, char pad)
  30. {
  31. int len = qemu_strnlen(str, buf_size);
  32. memcpy(buf, str, len);
  33. memset(buf + len, pad, buf_size - len);
  34. }
  35. void pstrcpy(char *buf, int buf_size, const char *str)
  36. {
  37. int c;
  38. char *q = buf;
  39. if (buf_size <= 0)
  40. return;
  41. for(;;) {
  42. c = *str++;
  43. if (c == 0 || q >= buf + buf_size - 1)
  44. break;
  45. *q++ = c;
  46. }
  47. *q = '\0';
  48. }
  49. /* strcat and truncate. */
  50. char *pstrcat(char *buf, int buf_size, const char *s)
  51. {
  52. int len;
  53. len = strlen(buf);
  54. if (len < buf_size)
  55. pstrcpy(buf + len, buf_size - len, s);
  56. return buf;
  57. }
  58. int strstart(const char *str, const char *val, const char **ptr)
  59. {
  60. const char *p, *q;
  61. p = str;
  62. q = val;
  63. while (*q != '\0') {
  64. if (*p != *q)
  65. return 0;
  66. p++;
  67. q++;
  68. }
  69. if (ptr)
  70. *ptr = p;
  71. return 1;
  72. }
  73. int stristart(const char *str, const char *val, const char **ptr)
  74. {
  75. const char *p, *q;
  76. p = str;
  77. q = val;
  78. while (*q != '\0') {
  79. if (qemu_toupper(*p) != qemu_toupper(*q))
  80. return 0;
  81. p++;
  82. q++;
  83. }
  84. if (ptr)
  85. *ptr = p;
  86. return 1;
  87. }
  88. /* XXX: use host strnlen if available ? */
  89. int qemu_strnlen(const char *s, int max_len)
  90. {
  91. int i;
  92. for(i = 0; i < max_len; i++) {
  93. if (s[i] == '\0') {
  94. break;
  95. }
  96. }
  97. return i;
  98. }
  99. time_t mktimegm(struct tm *tm)
  100. {
  101. time_t t;
  102. int y = tm->tm_year + 1900, m = tm->tm_mon + 1, d = tm->tm_mday;
  103. if (m < 3) {
  104. m += 12;
  105. y--;
  106. }
  107. t = 86400ULL * (d + (153 * m - 457) / 5 + 365 * y + y / 4 - y / 100 +
  108. y / 400 - 719469);
  109. t += 3600 * tm->tm_hour + 60 * tm->tm_min + tm->tm_sec;
  110. return t;
  111. }
  112. int qemu_fls(int i)
  113. {
  114. return 32 - clz32(i);
  115. }
  116. /*
  117. * Make sure data goes on disk, but if possible do not bother to
  118. * write out the inode just for timestamp updates.
  119. *
  120. * Unfortunately even in 2009 many operating systems do not support
  121. * fdatasync and have to fall back to fsync.
  122. */
  123. int qemu_fdatasync(int fd)
  124. {
  125. #ifdef CONFIG_FDATASYNC
  126. return fdatasync(fd);
  127. #else
  128. return fsync(fd);
  129. #endif
  130. }
  131. /* io vectors */
  132. void qemu_iovec_init(QEMUIOVector *qiov, int alloc_hint)
  133. {
  134. qiov->iov = g_malloc(alloc_hint * sizeof(struct iovec));
  135. qiov->niov = 0;
  136. qiov->nalloc = alloc_hint;
  137. qiov->size = 0;
  138. }
  139. void qemu_iovec_init_external(QEMUIOVector *qiov, struct iovec *iov, int niov)
  140. {
  141. int i;
  142. qiov->iov = iov;
  143. qiov->niov = niov;
  144. qiov->nalloc = -1;
  145. qiov->size = 0;
  146. for (i = 0; i < niov; i++)
  147. qiov->size += iov[i].iov_len;
  148. }
  149. void qemu_iovec_add(QEMUIOVector *qiov, void *base, size_t len)
  150. {
  151. assert(qiov->nalloc != -1);
  152. if (qiov->niov == qiov->nalloc) {
  153. qiov->nalloc = 2 * qiov->nalloc + 1;
  154. qiov->iov = g_realloc(qiov->iov, qiov->nalloc * sizeof(struct iovec));
  155. }
  156. qiov->iov[qiov->niov].iov_base = base;
  157. qiov->iov[qiov->niov].iov_len = len;
  158. qiov->size += len;
  159. ++qiov->niov;
  160. }
  161. /*
  162. * Concatenates (partial) iovecs from src to the end of dst.
  163. * It starts copying after skipping `soffset' bytes at the
  164. * beginning of src and adds individual vectors from src to
  165. * dst copies up to `sbytes' bytes total, or up to the end
  166. * of src if it comes first. This way, it is okay to specify
  167. * very large value for `sbytes' to indicate "up to the end
  168. * of src".
  169. * Only vector pointers are processed, not the actual data buffers.
  170. */
  171. void qemu_iovec_concat(QEMUIOVector *dst,
  172. QEMUIOVector *src, size_t soffset, size_t sbytes)
  173. {
  174. int i;
  175. size_t done;
  176. struct iovec *siov = src->iov;
  177. assert(dst->nalloc != -1);
  178. assert(src->size >= soffset);
  179. for (i = 0, done = 0; done < sbytes && i < src->niov; i++) {
  180. if (soffset < siov[i].iov_len) {
  181. size_t len = MIN(siov[i].iov_len - soffset, sbytes - done);
  182. qemu_iovec_add(dst, siov[i].iov_base + soffset, len);
  183. done += len;
  184. soffset = 0;
  185. } else {
  186. soffset -= siov[i].iov_len;
  187. }
  188. }
  189. /* return done; */
  190. }
  191. void qemu_iovec_destroy(QEMUIOVector *qiov)
  192. {
  193. assert(qiov->nalloc != -1);
  194. qemu_iovec_reset(qiov);
  195. g_free(qiov->iov);
  196. qiov->nalloc = 0;
  197. qiov->iov = NULL;
  198. }
  199. void qemu_iovec_reset(QEMUIOVector *qiov)
  200. {
  201. assert(qiov->nalloc != -1);
  202. qiov->niov = 0;
  203. qiov->size = 0;
  204. }
  205. size_t qemu_iovec_to_buf(QEMUIOVector *qiov, size_t offset,
  206. void *buf, size_t bytes)
  207. {
  208. return iov_to_buf(qiov->iov, qiov->niov, offset, buf, bytes);
  209. }
  210. size_t qemu_iovec_from_buf(QEMUIOVector *qiov, size_t offset,
  211. const void *buf, size_t bytes)
  212. {
  213. return iov_from_buf(qiov->iov, qiov->niov, offset, buf, bytes);
  214. }
  215. size_t qemu_iovec_memset(QEMUIOVector *qiov, size_t offset,
  216. int fillc, size_t bytes)
  217. {
  218. return iov_memset(qiov->iov, qiov->niov, offset, fillc, bytes);
  219. }
  220. /*
  221. * Checks if a buffer is all zeroes
  222. *
  223. * Attention! The len must be a multiple of 4 * sizeof(long) due to
  224. * restriction of optimizations in this function.
  225. */
  226. bool buffer_is_zero(const void *buf, size_t len)
  227. {
  228. /*
  229. * Use long as the biggest available internal data type that fits into the
  230. * CPU register and unroll the loop to smooth out the effect of memory
  231. * latency.
  232. */
  233. size_t i;
  234. long d0, d1, d2, d3;
  235. const long * const data = buf;
  236. assert(len % (4 * sizeof(long)) == 0);
  237. len /= sizeof(long);
  238. for (i = 0; i < len; i += 4) {
  239. d0 = data[i + 0];
  240. d1 = data[i + 1];
  241. d2 = data[i + 2];
  242. d3 = data[i + 3];
  243. if (d0 || d1 || d2 || d3) {
  244. return false;
  245. }
  246. }
  247. return true;
  248. }
  249. #ifndef _WIN32
  250. /* Sets a specific flag */
  251. int fcntl_setfl(int fd, int flag)
  252. {
  253. int flags;
  254. flags = fcntl(fd, F_GETFL);
  255. if (flags == -1)
  256. return -errno;
  257. if (fcntl(fd, F_SETFL, flags | flag) == -1)
  258. return -errno;
  259. return 0;
  260. }
  261. #endif
  262. static int64_t suffix_mul(char suffix, int64_t unit)
  263. {
  264. switch (qemu_toupper(suffix)) {
  265. case STRTOSZ_DEFSUFFIX_B:
  266. return 1;
  267. case STRTOSZ_DEFSUFFIX_KB:
  268. return unit;
  269. case STRTOSZ_DEFSUFFIX_MB:
  270. return unit * unit;
  271. case STRTOSZ_DEFSUFFIX_GB:
  272. return unit * unit * unit;
  273. case STRTOSZ_DEFSUFFIX_TB:
  274. return unit * unit * unit * unit;
  275. }
  276. return -1;
  277. }
  278. /*
  279. * Convert string to bytes, allowing either B/b for bytes, K/k for KB,
  280. * M/m for MB, G/g for GB or T/t for TB. End pointer will be returned
  281. * in *end, if not NULL. Return -1 on error.
  282. */
  283. int64_t strtosz_suffix_unit(const char *nptr, char **end,
  284. const char default_suffix, int64_t unit)
  285. {
  286. int64_t retval = -1;
  287. char *endptr;
  288. unsigned char c;
  289. int mul_required = 0;
  290. double val, mul, integral, fraction;
  291. errno = 0;
  292. val = strtod(nptr, &endptr);
  293. if (isnan(val) || endptr == nptr || errno != 0) {
  294. goto fail;
  295. }
  296. fraction = modf(val, &integral);
  297. if (fraction != 0) {
  298. mul_required = 1;
  299. }
  300. c = *endptr;
  301. mul = suffix_mul(c, unit);
  302. if (mul >= 0) {
  303. endptr++;
  304. } else {
  305. mul = suffix_mul(default_suffix, unit);
  306. assert(mul >= 0);
  307. }
  308. if (mul == 1 && mul_required) {
  309. goto fail;
  310. }
  311. if ((val * mul >= INT64_MAX) || val < 0) {
  312. goto fail;
  313. }
  314. retval = val * mul;
  315. fail:
  316. if (end) {
  317. *end = endptr;
  318. }
  319. return retval;
  320. }
  321. int64_t strtosz_suffix(const char *nptr, char **end, const char default_suffix)
  322. {
  323. return strtosz_suffix_unit(nptr, end, default_suffix, 1024);
  324. }
  325. int64_t strtosz(const char *nptr, char **end)
  326. {
  327. return strtosz_suffix(nptr, end, STRTOSZ_DEFSUFFIX_MB);
  328. }
  329. int qemu_parse_fd(const char *param)
  330. {
  331. int fd;
  332. char *endptr = NULL;
  333. fd = strtol(param, &endptr, 10);
  334. if (*endptr || (fd == 0 && param == endptr)) {
  335. return -1;
  336. }
  337. return fd;
  338. }
  339. int qemu_parse_fdset(const char *param)
  340. {
  341. return qemu_parse_fd(param);
  342. }
  343. /* round down to the nearest power of 2*/
  344. int64_t pow2floor(int64_t value)
  345. {
  346. if (!is_power_of_2(value)) {
  347. value = 0x8000000000000000ULL >> clz64(value);
  348. }
  349. return value;
  350. }
  351. /*
  352. * Implementation of ULEB128 (http://en.wikipedia.org/wiki/LEB128)
  353. * Input is limited to 14-bit numbers
  354. */
  355. int uleb128_encode_small(uint8_t *out, uint32_t n)
  356. {
  357. g_assert(n <= 0x3fff);
  358. if (n < 0x80) {
  359. *out++ = n;
  360. return 1;
  361. } else {
  362. *out++ = (n & 0x7f) | 0x80;
  363. *out++ = n >> 7;
  364. return 2;
  365. }
  366. }
  367. int uleb128_decode_small(const uint8_t *in, uint32_t *n)
  368. {
  369. if (!(*in & 0x80)) {
  370. *n = *in++;
  371. return 1;
  372. } else {
  373. *n = *in++ & 0x7f;
  374. /* we exceed 14 bit number */
  375. if (*in & 0x80) {
  376. return -1;
  377. }
  378. *n |= *in++ << 7;
  379. return 2;
  380. }
  381. }