2
0

cutils.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. void pstrcpy(char *buf, int buf_size, const char *str)
  27. {
  28. int c;
  29. char *q = buf;
  30. if (buf_size <= 0)
  31. return;
  32. for(;;) {
  33. c = *str++;
  34. if (c == 0 || q >= buf + buf_size - 1)
  35. break;
  36. *q++ = c;
  37. }
  38. *q = '\0';
  39. }
  40. /* strcat and truncate. */
  41. char *pstrcat(char *buf, int buf_size, const char *s)
  42. {
  43. int len;
  44. len = strlen(buf);
  45. if (len < buf_size)
  46. pstrcpy(buf + len, buf_size - len, s);
  47. return buf;
  48. }
  49. int strstart(const char *str, const char *val, const char **ptr)
  50. {
  51. const char *p, *q;
  52. p = str;
  53. q = val;
  54. while (*q != '\0') {
  55. if (*p != *q)
  56. return 0;
  57. p++;
  58. q++;
  59. }
  60. if (ptr)
  61. *ptr = p;
  62. return 1;
  63. }
  64. int stristart(const char *str, const char *val, const char **ptr)
  65. {
  66. const char *p, *q;
  67. p = str;
  68. q = val;
  69. while (*q != '\0') {
  70. if (qemu_toupper(*p) != qemu_toupper(*q))
  71. return 0;
  72. p++;
  73. q++;
  74. }
  75. if (ptr)
  76. *ptr = p;
  77. return 1;
  78. }
  79. time_t mktimegm(struct tm *tm)
  80. {
  81. time_t t;
  82. int y = tm->tm_year + 1900, m = tm->tm_mon + 1, d = tm->tm_mday;
  83. if (m < 3) {
  84. m += 12;
  85. y--;
  86. }
  87. t = 86400 * (d + (153 * m - 457) / 5 + 365 * y + y / 4 - y / 100 +
  88. y / 400 - 719469);
  89. t += 3600 * tm->tm_hour + 60 * tm->tm_min + tm->tm_sec;
  90. return t;
  91. }
  92. int qemu_fls(int i)
  93. {
  94. return 32 - clz32(i);
  95. }
  96. /* io vectors */
  97. void qemu_iovec_init(QEMUIOVector *qiov, int alloc_hint)
  98. {
  99. qiov->iov = qemu_malloc(alloc_hint * sizeof(struct iovec));
  100. qiov->niov = 0;
  101. qiov->nalloc = alloc_hint;
  102. qiov->size = 0;
  103. }
  104. void qemu_iovec_add(QEMUIOVector *qiov, void *base, size_t len)
  105. {
  106. if (qiov->niov == qiov->nalloc) {
  107. qiov->nalloc = 2 * qiov->nalloc + 1;
  108. qiov->iov = qemu_realloc(qiov->iov, qiov->nalloc * sizeof(struct iovec));
  109. }
  110. qiov->iov[qiov->niov].iov_base = base;
  111. qiov->iov[qiov->niov].iov_len = len;
  112. qiov->size += len;
  113. ++qiov->niov;
  114. }
  115. void qemu_iovec_destroy(QEMUIOVector *qiov)
  116. {
  117. qemu_free(qiov->iov);
  118. }
  119. void qemu_iovec_reset(QEMUIOVector *qiov)
  120. {
  121. qiov->niov = 0;
  122. qiov->size = 0;
  123. }
  124. void qemu_iovec_to_buffer(QEMUIOVector *qiov, void *buf)
  125. {
  126. uint8_t *p = (uint8_t *)buf;
  127. int i;
  128. for (i = 0; i < qiov->niov; ++i) {
  129. memcpy(p, qiov->iov[i].iov_base, qiov->iov[i].iov_len);
  130. p += qiov->iov[i].iov_len;
  131. }
  132. }
  133. void qemu_iovec_from_buffer(QEMUIOVector *qiov, const void *buf, size_t count)
  134. {
  135. const uint8_t *p = (const uint8_t *)buf;
  136. size_t copy;
  137. int i;
  138. for (i = 0; i < qiov->niov && count; ++i) {
  139. copy = count;
  140. if (copy > qiov->iov[i].iov_len)
  141. copy = qiov->iov[i].iov_len;
  142. memcpy(qiov->iov[i].iov_base, p, copy);
  143. p += copy;
  144. count -= copy;
  145. }
  146. }