string.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. #include "string.h"
  2. #include "glib.h"
  3. /**
  4. * @brief 拷贝整个字符串
  5. *
  6. * @param dst 目标地址
  7. * @param src 源地址
  8. * @return char* 目标字符串
  9. */
  10. char *strcpy(char *dst, const char *src)
  11. {
  12. while (*src)
  13. {
  14. *(dst++) = *(src++);
  15. }
  16. *dst = 0;
  17. return dst;
  18. }
  19. long strnlen(const char *src, unsigned long maxlen)
  20. {
  21. if (src == NULL)
  22. return 0;
  23. register int __res = 0;
  24. while (src[__res] != '\0' && __res < maxlen)
  25. {
  26. ++__res;
  27. }
  28. return __res;
  29. }
  30. /*
  31. 比较字符串 FirstPart and SecondPart
  32. FirstPart = SecondPart => 0
  33. FirstPart > SecondPart => 1
  34. FirstPart < SecondPart => -1
  35. */
  36. int strcmp(const char *FirstPart, const char *SecondPart)
  37. {
  38. register int __res;
  39. __asm__ __volatile__("cld \n\t"
  40. "1: \n\t"
  41. "lodsb \n\t"
  42. "scasb \n\t"
  43. "jne 2f \n\t"
  44. "testb %%al, %%al \n\t"
  45. "jne 1b \n\t"
  46. "xorl %%eax, %%eax \n\t"
  47. "jmp 3f \n\t"
  48. "2: \n\t"
  49. "movl $1, %%eax \n\t"
  50. "jl 3f \n\t"
  51. "negl %%eax \n\t"
  52. "3: \n\t"
  53. : "=a"(__res)
  54. : "D"(FirstPart), "S"(SecondPart)
  55. :);
  56. return __res;
  57. }
  58. char *strncpy(char *dst, const char *src, long count)
  59. {
  60. __asm__ __volatile__("cld \n\t"
  61. "1: \n\t"
  62. "decq %2 \n\t"
  63. "js 2f \n\t"
  64. "lodsb \n\t"
  65. "stosb \n\t"
  66. "testb %%al, %%al \n\t"
  67. "jne 1b \n\t"
  68. "rep \n\t"
  69. "stosb \n\t"
  70. "2: \n\t"
  71. :
  72. : "S"(src), "D"(dst), "c"(count)
  73. : "ax", "memory");
  74. return dst;
  75. }
  76. long strncpy_from_user(char *dst, const char *src, unsigned long size)
  77. {
  78. if (!verify_area((uint64_t)src, size))
  79. return 0;
  80. strncpy(dst, src, size);
  81. return size;
  82. }
  83. /**
  84. * @brief 测量来自用户空间的字符串的长度,会检验地址空间是否属于用户空间
  85. * @param src
  86. * @param maxlen
  87. * @return long
  88. */
  89. long strnlen_user(const char *src, unsigned long maxlen)
  90. {
  91. unsigned long size = strlen(src);
  92. // 地址不合法
  93. if (!verify_area((uint64_t)src, size))
  94. return 0;
  95. return size <= maxlen ? size : maxlen;
  96. }