string.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. /*
  2. * string.c
  3. *
  4. * Copyright (C) 2021 bzt (bztsrc@gitlab)
  5. *
  6. * Permission is hereby granted, free of charge, to any person
  7. * obtaining a copy of this software and associated documentation
  8. * files (the "Software"), to deal in the Software without
  9. * restriction, including without limitation the rights to use, copy,
  10. * modify, merge, publish, distribute, sublicense, and/or sell copies
  11. * of the Software, and to permit persons to whom the Software is
  12. * furnished to do so, subject to the following conditions:
  13. *
  14. * The above copyright notice and this permission notice shall be
  15. * included in all copies or substantial portions of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  18. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  19. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  20. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  21. * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  22. * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  24. * DEALINGS IN THE SOFTWARE.
  25. *
  26. * This file is part of the POSIX-UEFI package.
  27. * @brief Implementing functions which are defined in string.h
  28. *
  29. */
  30. #include <uefi.h>
  31. void *memcpy(void *dst, const void *src, size_t n)
  32. {
  33. uint8_t *a=(uint8_t*)dst,*b=(uint8_t*)src;
  34. if(src && dst && src != dst && n>0) {
  35. while(n--) *a++ = *b++;
  36. }
  37. return dst;
  38. }
  39. void *memmove(void *dst, const void *src, size_t n)
  40. {
  41. uint8_t *a=(uint8_t*)dst,*b=(uint8_t*)src;
  42. if(src && dst && src != dst && n>0) {
  43. if(a>b && a<b+n) {
  44. a+=n-1; b+=n-1; while(n-->0) *a--=*b--;
  45. } else {
  46. while(n--) *a++ = *b++;
  47. }
  48. }
  49. return dst;
  50. }
  51. void *memset(void *s, int c, size_t n)
  52. {
  53. uint8_t *p=(uint8_t*)s;
  54. if(s && n>0) {
  55. while(n--) *p++ = (uint8_t)c;
  56. }
  57. return s;
  58. }
  59. int memcmp(const void *s1, const void *s2, size_t n)
  60. {
  61. uint8_t *a=(uint8_t*)s1,*b=(uint8_t*)s2;
  62. if(s1 && s2 && s1 != s2 && n>0) {
  63. while(n--) {
  64. if(*a != *b) return *a - *b;
  65. a++; b++;
  66. }
  67. }
  68. return 0;
  69. }
  70. void *memchr(const void *s, int c, size_t n)
  71. {
  72. uint8_t *e, *p=(uint8_t*)s;
  73. if(s && n>0) {
  74. for(e=p+n; p<e; p++) if(*p==(uint8_t)c) return p;
  75. }
  76. return NULL;
  77. }
  78. void *memrchr(const void *s, int c, size_t n)
  79. {
  80. uint8_t *e, *p=(uint8_t*)s;
  81. if(s && n>0) {
  82. for(e=p+n; p<e; --e) if(*e==(uint8_t)c) return e;
  83. }
  84. return NULL;
  85. }
  86. void *memmem(const void *haystack, size_t hl, const void *needle, size_t nl)
  87. {
  88. uint8_t *c = (uint8_t*)haystack;
  89. if(!haystack || !needle || !hl || !nl || nl > hl) return NULL;
  90. hl -= nl - 1;
  91. while(hl) {
  92. if(!memcmp(c, needle, nl)) return c;
  93. c++; hl--;
  94. }
  95. return NULL;
  96. }
  97. void *memrmem(const void *haystack, size_t hl, const void *needle, size_t nl)
  98. {
  99. uint8_t *c = (uint8_t*)haystack;
  100. if(!haystack || !needle || !hl || !nl || nl > hl) return NULL;
  101. hl -= nl;
  102. c += hl;
  103. while(hl) {
  104. if(!memcmp(c, needle, nl)) return c;
  105. c--; hl--;
  106. }
  107. return NULL;
  108. }
  109. char_t *strcpy(char_t *dst, const char_t *src)
  110. {
  111. char_t *s = dst;
  112. if(src && dst && src != dst) {
  113. while(*src) {*dst++=*src++;} *dst=0;
  114. }
  115. return s;
  116. }
  117. char_t *strncpy(char_t *dst, const char_t *src, size_t n)
  118. {
  119. char_t *s = dst;
  120. const char_t *e = src+n;
  121. if(src && dst && src != dst && n>0) {
  122. while(*src && src<e) {*dst++=*src++;} *dst=0;
  123. }
  124. return s;
  125. }
  126. char_t *strcat(char_t *dst, const char_t *src)
  127. {
  128. char_t *s = dst;
  129. if(src && dst) {
  130. dst += strlen(dst);
  131. while(*src) {*dst++=*src++;} *dst=0;
  132. }
  133. return s;
  134. }
  135. int strcmp(const char_t *s1, const char_t *s2)
  136. {
  137. if(s1 && s2 && s1!=s2) {
  138. while(*s1 && *s1==*s2){s1++;s2++;}
  139. return *s1-*s2;
  140. }
  141. return 0;
  142. }
  143. char_t *strncat(char_t *dst, const char_t *src, size_t n)
  144. {
  145. char_t *s = dst;
  146. const char_t *e = src+n;
  147. if(src && dst && n>0) {
  148. dst += strlen(dst);
  149. while(*src && src<e) {*dst++=*src++;} *dst=0;
  150. }
  151. return s;
  152. }
  153. int strncmp(const char_t *s1, const char_t *s2, size_t n)
  154. {
  155. const char_t *e = s1+n-1;
  156. if(s1 && s2 && s1!=s2 && n>0) {
  157. while(s1<e && *s1 && *s1==*s2){s1++;s2++;}
  158. return *s1-*s2;
  159. }
  160. return 0;
  161. }
  162. char_t *strdup(const char_t *s)
  163. {
  164. size_t i = (strlen(s)+1) * sizeof(char_t);
  165. char_t *s2 = (char_t *)malloc(i);
  166. if(s2 != NULL) memcpy(s2, (const void*)s, i);
  167. return s2;
  168. }
  169. char_t *strchr(const char_t *s, int c)
  170. {
  171. if(s) {
  172. while(*s) {
  173. if(*s == (char_t)c) return (char_t*)s;
  174. s++;
  175. }
  176. }
  177. return NULL;
  178. }
  179. char_t *strrchr(const char_t *s, int c)
  180. {
  181. char_t *e;
  182. if(s) {
  183. e = (char_t*)s + strlen(s) - 1;
  184. while(s <= e) {
  185. if(*e == (char_t)c) return e;
  186. e--;
  187. }
  188. }
  189. return NULL;
  190. }
  191. char_t *strstr(const char_t *haystack, const char_t *needle)
  192. {
  193. return memmem(haystack, strlen(haystack) * sizeof(char_t), needle, strlen(needle) * sizeof(char_t));
  194. }
  195. static char_t *_strtok_r(char_t *s, const char_t *d, char_t **p)
  196. {
  197. int c, sc;
  198. char_t *tok, *sp;
  199. if(d == NULL || (s == NULL && (s=*p) == NULL)) return NULL;
  200. again:
  201. c = *s++;
  202. for(sp = (char_t *)d; (sc=*sp++)!=0;) {
  203. if(c == sc) goto again;
  204. }
  205. if (c == 0) { *p=NULL; return NULL; }
  206. tok = s-1;
  207. while(1) {
  208. c = *s++;
  209. sp = (char_t *)d;
  210. do {
  211. if((sc=*sp++) == c) {
  212. if(c == 0) s = NULL;
  213. else *(s-1) = 0;
  214. *p = s;
  215. return tok;
  216. }
  217. } while(sc != 0);
  218. }
  219. return NULL;
  220. }
  221. char_t *strtok(char_t *s, const char_t *delim)
  222. {
  223. static char_t *p;
  224. return _strtok_r (s, delim, &p);
  225. }
  226. char_t *strtok_r(char_t *s, const char_t *delim, char_t **ptr)
  227. {
  228. return _strtok_r (s, delim, ptr);
  229. }
  230. size_t strlen (const char_t *__s)
  231. {
  232. size_t ret;
  233. if(!__s) return 0;
  234. for(ret = 0; __s[ret]; ret++);
  235. return ret;
  236. }