string.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * arch/arm/boot/compressed/string.c
  4. *
  5. * Small subset of simple string routines
  6. */
  7. #define __NO_FORTIFY
  8. #include <linux/string.h>
  9. /*
  10. * The decompressor is built without KASan but uses the same redirects as the
  11. * rest of the kernel when CONFIG_KASAN is enabled, defining e.g. memcpy()
  12. * to __memcpy() but since we are not linking with the main kernel string
  13. * library in the decompressor, that will lead to link failures.
  14. *
  15. * Undefine KASan's versions, define the wrapped functions and alias them to
  16. * the right names so that when e.g. __memcpy() appear in the code, it will
  17. * still be linked to this local version of memcpy().
  18. */
  19. #ifdef CONFIG_KASAN
  20. #undef memcpy
  21. #undef memmove
  22. #undef memset
  23. void *__memcpy(void *__dest, __const void *__src, size_t __n) __alias(memcpy);
  24. void *__memmove(void *__dest, __const void *__src, size_t count) __alias(memmove);
  25. void *__memset(void *s, int c, size_t count) __alias(memset);
  26. #endif
  27. void *memcpy(void *__dest, __const void *__src, size_t __n)
  28. {
  29. int i = 0;
  30. unsigned char *d = (unsigned char *)__dest, *s = (unsigned char *)__src;
  31. for (i = __n >> 3; i > 0; i--) {
  32. *d++ = *s++;
  33. *d++ = *s++;
  34. *d++ = *s++;
  35. *d++ = *s++;
  36. *d++ = *s++;
  37. *d++ = *s++;
  38. *d++ = *s++;
  39. *d++ = *s++;
  40. }
  41. if (__n & 1 << 2) {
  42. *d++ = *s++;
  43. *d++ = *s++;
  44. *d++ = *s++;
  45. *d++ = *s++;
  46. }
  47. if (__n & 1 << 1) {
  48. *d++ = *s++;
  49. *d++ = *s++;
  50. }
  51. if (__n & 1)
  52. *d++ = *s++;
  53. return __dest;
  54. }
  55. void *memmove(void *__dest, __const void *__src, size_t count)
  56. {
  57. unsigned char *d = __dest;
  58. const unsigned char *s = __src;
  59. if (__dest == __src)
  60. return __dest;
  61. if (__dest < __src)
  62. return memcpy(__dest, __src, count);
  63. while (count--)
  64. d[count] = s[count];
  65. return __dest;
  66. }
  67. size_t strlen(const char *s)
  68. {
  69. const char *sc = s;
  70. while (*sc != '\0')
  71. sc++;
  72. return sc - s;
  73. }
  74. size_t strnlen(const char *s, size_t count)
  75. {
  76. const char *sc;
  77. for (sc = s; count-- && *sc != '\0'; ++sc)
  78. /* nothing */;
  79. return sc - s;
  80. }
  81. int memcmp(const void *cs, const void *ct, size_t count)
  82. {
  83. const unsigned char *su1 = cs, *su2 = ct, *end = su1 + count;
  84. int res = 0;
  85. while (su1 < end) {
  86. res = *su1++ - *su2++;
  87. if (res)
  88. break;
  89. }
  90. return res;
  91. }
  92. int strcmp(const char *cs, const char *ct)
  93. {
  94. unsigned char c1, c2;
  95. int res = 0;
  96. do {
  97. c1 = *cs++;
  98. c2 = *ct++;
  99. res = c1 - c2;
  100. if (res)
  101. break;
  102. } while (c1);
  103. return res;
  104. }
  105. void *memchr(const void *s, int c, size_t count)
  106. {
  107. const unsigned char *p = s;
  108. while (count--)
  109. if ((unsigned char)c == *p++)
  110. return (void *)(p - 1);
  111. return NULL;
  112. }
  113. char *strchr(const char *s, int c)
  114. {
  115. while (*s != (char)c)
  116. if (*s++ == '\0')
  117. return NULL;
  118. return (char *)s;
  119. }
  120. char *strrchr(const char *s, int c)
  121. {
  122. const char *last = NULL;
  123. do {
  124. if (*s == (char)c)
  125. last = s;
  126. } while (*s++);
  127. return (char *)last;
  128. }
  129. #undef memset
  130. void *memset(void *s, int c, size_t count)
  131. {
  132. char *xs = s;
  133. while (count--)
  134. *xs++ = c;
  135. return s;
  136. }