string.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Taken from:
  4. * linux/lib/string.c
  5. *
  6. * Copyright (C) 1991, 1992 Linus Torvalds
  7. */
  8. #include <linux/ctype.h>
  9. #include <linux/kernel.h>
  10. #include <linux/types.h>
  11. #include <linux/string.h>
  12. #ifndef EFI_HAVE_STRLEN
  13. /**
  14. * strlen - Find the length of a string
  15. * @s: The string to be sized
  16. */
  17. size_t strlen(const char *s)
  18. {
  19. const char *sc;
  20. for (sc = s; *sc != '\0'; ++sc)
  21. /* nothing */;
  22. return sc - s;
  23. }
  24. #endif
  25. #ifndef EFI_HAVE_STRNLEN
  26. /**
  27. * strnlen - Find the length of a length-limited string
  28. * @s: The string to be sized
  29. * @count: The maximum number of bytes to search
  30. */
  31. size_t strnlen(const char *s, size_t count)
  32. {
  33. const char *sc;
  34. for (sc = s; count-- && *sc != '\0'; ++sc)
  35. /* nothing */;
  36. return sc - s;
  37. }
  38. #endif
  39. /**
  40. * strstr - Find the first substring in a %NUL terminated string
  41. * @s1: The string to be searched
  42. * @s2: The string to search for
  43. */
  44. char *strstr(const char *s1, const char *s2)
  45. {
  46. size_t l1, l2;
  47. l2 = strlen(s2);
  48. if (!l2)
  49. return (char *)s1;
  50. l1 = strlen(s1);
  51. while (l1 >= l2) {
  52. l1--;
  53. if (!memcmp(s1, s2, l2))
  54. return (char *)s1;
  55. s1++;
  56. }
  57. return NULL;
  58. }
  59. #ifndef EFI_HAVE_STRCMP
  60. /**
  61. * strcmp - Compare two strings
  62. * @cs: One string
  63. * @ct: Another string
  64. */
  65. int strcmp(const char *cs, const char *ct)
  66. {
  67. unsigned char c1, c2;
  68. while (1) {
  69. c1 = *cs++;
  70. c2 = *ct++;
  71. if (c1 != c2)
  72. return c1 < c2 ? -1 : 1;
  73. if (!c1)
  74. break;
  75. }
  76. return 0;
  77. }
  78. #endif
  79. /**
  80. * strncmp - Compare two length-limited strings
  81. * @cs: One string
  82. * @ct: Another string
  83. * @count: The maximum number of bytes to compare
  84. */
  85. int strncmp(const char *cs, const char *ct, size_t count)
  86. {
  87. unsigned char c1, c2;
  88. while (count) {
  89. c1 = *cs++;
  90. c2 = *ct++;
  91. if (c1 != c2)
  92. return c1 < c2 ? -1 : 1;
  93. if (!c1)
  94. break;
  95. count--;
  96. }
  97. return 0;
  98. }
  99. /* Works only for digits and letters, but small and fast */
  100. #define TOLOWER(x) ((x) | 0x20)
  101. static unsigned int simple_guess_base(const char *cp)
  102. {
  103. if (cp[0] == '0') {
  104. if (TOLOWER(cp[1]) == 'x' && isxdigit(cp[2]))
  105. return 16;
  106. else
  107. return 8;
  108. } else {
  109. return 10;
  110. }
  111. }
  112. /**
  113. * simple_strtoull - convert a string to an unsigned long long
  114. * @cp: The start of the string
  115. * @endp: A pointer to the end of the parsed string will be placed here
  116. * @base: The number base to use
  117. */
  118. unsigned long long simple_strtoull(const char *cp, char **endp, unsigned int base)
  119. {
  120. unsigned long long result = 0;
  121. if (!base)
  122. base = simple_guess_base(cp);
  123. if (base == 16 && cp[0] == '0' && TOLOWER(cp[1]) == 'x')
  124. cp += 2;
  125. while (isxdigit(*cp)) {
  126. unsigned int value;
  127. value = isdigit(*cp) ? *cp - '0' : TOLOWER(*cp) - 'a' + 10;
  128. if (value >= base)
  129. break;
  130. result = result * base + value;
  131. cp++;
  132. }
  133. if (endp)
  134. *endp = (char *)cp;
  135. return result;
  136. }
  137. long simple_strtol(const char *cp, char **endp, unsigned int base)
  138. {
  139. if (*cp == '-')
  140. return -simple_strtoull(cp + 1, endp, base);
  141. return simple_strtoull(cp, endp, base);
  142. }
  143. #ifdef CONFIG_EFI_PARAMS_FROM_FDT
  144. #ifndef EFI_HAVE_STRRCHR
  145. /**
  146. * strrchr - Find the last occurrence of a character in a string
  147. * @s: The string to be searched
  148. * @c: The character to search for
  149. */
  150. char *strrchr(const char *s, int c)
  151. {
  152. const char *last = NULL;
  153. do {
  154. if (*s == (char)c)
  155. last = s;
  156. } while (*s++);
  157. return (char *)last;
  158. }
  159. #endif
  160. #ifndef EFI_HAVE_MEMCHR
  161. /**
  162. * memchr - Find a character in an area of memory.
  163. * @s: The memory area
  164. * @c: The byte to search for
  165. * @n: The size of the area.
  166. *
  167. * returns the address of the first occurrence of @c, or %NULL
  168. * if @c is not found
  169. */
  170. void *memchr(const void *s, int c, size_t n)
  171. {
  172. const unsigned char *p = s;
  173. while (n-- != 0) {
  174. if ((unsigned char)c == *p++) {
  175. return (void *)(p - 1);
  176. }
  177. }
  178. return NULL;
  179. }
  180. #endif
  181. #endif