string.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. /* SPDX-License-Identifier: LGPL-2.1 OR MIT */
  2. /*
  3. * string function definitions for NOLIBC
  4. * Copyright (C) 2017-2021 Willy Tarreau <[email protected]>
  5. */
  6. #ifndef _NOLIBC_STRING_H
  7. #define _NOLIBC_STRING_H
  8. #include "std.h"
  9. static void *malloc(size_t len);
  10. /*
  11. * As much as possible, please keep functions alphabetically sorted.
  12. */
  13. static __attribute__((unused))
  14. int memcmp(const void *s1, const void *s2, size_t n)
  15. {
  16. size_t ofs = 0;
  17. int c1 = 0;
  18. while (ofs < n && !(c1 = ((unsigned char *)s1)[ofs] - ((unsigned char *)s2)[ofs])) {
  19. ofs++;
  20. }
  21. return c1;
  22. }
  23. static __attribute__((unused))
  24. void *_nolibc_memcpy_up(void *dst, const void *src, size_t len)
  25. {
  26. size_t pos = 0;
  27. while (pos < len) {
  28. ((char *)dst)[pos] = ((const char *)src)[pos];
  29. pos++;
  30. }
  31. return dst;
  32. }
  33. static __attribute__((unused))
  34. void *_nolibc_memcpy_down(void *dst, const void *src, size_t len)
  35. {
  36. while (len) {
  37. len--;
  38. ((char *)dst)[len] = ((const char *)src)[len];
  39. }
  40. return dst;
  41. }
  42. /* might be ignored by the compiler without -ffreestanding, then found as
  43. * missing.
  44. */
  45. __attribute__((weak,unused,section(".text.nolibc_memmove")))
  46. void *memmove(void *dst, const void *src, size_t len)
  47. {
  48. size_t dir, pos;
  49. pos = len;
  50. dir = -1;
  51. if (dst < src) {
  52. pos = -1;
  53. dir = 1;
  54. }
  55. while (len) {
  56. pos += dir;
  57. ((char *)dst)[pos] = ((const char *)src)[pos];
  58. len--;
  59. }
  60. return dst;
  61. }
  62. /* must be exported, as it's used by libgcc on ARM */
  63. __attribute__((weak,unused,section(".text.nolibc_memcpy")))
  64. void *memcpy(void *dst, const void *src, size_t len)
  65. {
  66. return _nolibc_memcpy_up(dst, src, len);
  67. }
  68. /* might be ignored by the compiler without -ffreestanding, then found as
  69. * missing.
  70. */
  71. __attribute__((weak,unused,section(".text.nolibc_memset")))
  72. void *memset(void *dst, int b, size_t len)
  73. {
  74. char *p = dst;
  75. while (len--) {
  76. /* prevent gcc from recognizing memset() here */
  77. asm volatile("");
  78. *(p++) = b;
  79. }
  80. return dst;
  81. }
  82. static __attribute__((unused))
  83. char *strchr(const char *s, int c)
  84. {
  85. while (*s) {
  86. if (*s == (char)c)
  87. return (char *)s;
  88. s++;
  89. }
  90. return NULL;
  91. }
  92. static __attribute__((unused))
  93. int strcmp(const char *a, const char *b)
  94. {
  95. unsigned int c;
  96. int diff;
  97. while (!(diff = (unsigned char)*a++ - (c = (unsigned char)*b++)) && c)
  98. ;
  99. return diff;
  100. }
  101. static __attribute__((unused))
  102. char *strcpy(char *dst, const char *src)
  103. {
  104. char *ret = dst;
  105. while ((*dst++ = *src++));
  106. return ret;
  107. }
  108. /* this function is only used with arguments that are not constants or when
  109. * it's not known because optimizations are disabled. Note that gcc 12
  110. * recognizes an strlen() pattern and replaces it with a jump to strlen(),
  111. * thus itself, hence the asm() statement below that's meant to disable this
  112. * confusing practice.
  113. */
  114. static __attribute__((unused))
  115. size_t strlen(const char *str)
  116. {
  117. size_t len;
  118. for (len = 0; str[len]; len++)
  119. asm("");
  120. return len;
  121. }
  122. /* do not trust __builtin_constant_p() at -O0, as clang will emit a test and
  123. * the two branches, then will rely on an external definition of strlen().
  124. */
  125. #if defined(__OPTIMIZE__)
  126. #define nolibc_strlen(x) strlen(x)
  127. #define strlen(str) ({ \
  128. __builtin_constant_p((str)) ? \
  129. __builtin_strlen((str)) : \
  130. nolibc_strlen((str)); \
  131. })
  132. #endif
  133. static __attribute__((unused))
  134. size_t strnlen(const char *str, size_t maxlen)
  135. {
  136. size_t len;
  137. for (len = 0; (len < maxlen) && str[len]; len++);
  138. return len;
  139. }
  140. static __attribute__((unused))
  141. char *strdup(const char *str)
  142. {
  143. size_t len;
  144. char *ret;
  145. len = strlen(str);
  146. ret = malloc(len + 1);
  147. if (__builtin_expect(ret != NULL, 1))
  148. memcpy(ret, str, len + 1);
  149. return ret;
  150. }
  151. static __attribute__((unused))
  152. char *strndup(const char *str, size_t maxlen)
  153. {
  154. size_t len;
  155. char *ret;
  156. len = strnlen(str, maxlen);
  157. ret = malloc(len + 1);
  158. if (__builtin_expect(ret != NULL, 1)) {
  159. memcpy(ret, str, len);
  160. ret[len] = '\0';
  161. }
  162. return ret;
  163. }
  164. static __attribute__((unused))
  165. size_t strlcat(char *dst, const char *src, size_t size)
  166. {
  167. size_t len;
  168. char c;
  169. for (len = 0; dst[len]; len++)
  170. ;
  171. for (;;) {
  172. c = *src;
  173. if (len < size)
  174. dst[len] = c;
  175. if (!c)
  176. break;
  177. len++;
  178. src++;
  179. }
  180. return len;
  181. }
  182. static __attribute__((unused))
  183. size_t strlcpy(char *dst, const char *src, size_t size)
  184. {
  185. size_t len;
  186. char c;
  187. for (len = 0;;) {
  188. c = src[len];
  189. if (len < size)
  190. dst[len] = c;
  191. if (!c)
  192. break;
  193. len++;
  194. }
  195. return len;
  196. }
  197. static __attribute__((unused))
  198. char *strncat(char *dst, const char *src, size_t size)
  199. {
  200. char *orig = dst;
  201. while (*dst)
  202. dst++;
  203. while (size && (*dst = *src)) {
  204. src++;
  205. dst++;
  206. size--;
  207. }
  208. *dst = 0;
  209. return orig;
  210. }
  211. static __attribute__((unused))
  212. int strncmp(const char *a, const char *b, size_t size)
  213. {
  214. unsigned int c;
  215. int diff = 0;
  216. while (size-- &&
  217. !(diff = (unsigned char)*a++ - (c = (unsigned char)*b++)) && c)
  218. ;
  219. return diff;
  220. }
  221. static __attribute__((unused))
  222. char *strncpy(char *dst, const char *src, size_t size)
  223. {
  224. size_t len;
  225. for (len = 0; len < size; len++)
  226. if ((dst[len] = *src))
  227. src++;
  228. return dst;
  229. }
  230. static __attribute__((unused))
  231. char *strrchr(const char *s, int c)
  232. {
  233. const char *ret = NULL;
  234. while (*s) {
  235. if (*s == (char)c)
  236. ret = s;
  237. s++;
  238. }
  239. return (char *)ret;
  240. }
  241. /* make sure to include all global symbols */
  242. #include "nolibc.h"
  243. #endif /* _NOLIBC_STRING_H */