dl-string.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. #ifndef _LINUX_STRING_H_
  2. #define _LINUX_STRING_H_
  3. #include <dl-sysdep.h> // for do_rem
  4. static size_t _dl_strlen(const char * str);
  5. static char *_dl_strcat(char *dst, const char *src);
  6. static char * _dl_strcpy(char * dst,const char *src);
  7. static int _dl_strcmp(const char * s1,const char * s2);
  8. static int _dl_strncmp(const char * s1,const char * s2,size_t len);
  9. static char * _dl_strchr(const char * str,int c);
  10. static char *_dl_strrchr(const char *str, int c);
  11. static char *_dl_strstr(const char *s1, const char *s2);
  12. static void * _dl_memcpy(void * dst, const void * src, size_t len);
  13. static int _dl_memcmp(const void * s1,const void * s2,size_t len);
  14. static void *_dl_memset(void * str,int c,size_t len);
  15. static char *_dl_get_last_path_component(char *path);
  16. static char *_dl_simple_ltoa(char * local, unsigned long i);
  17. static char *_dl_simple_ltoahex(char * local, unsigned long i);
  18. #ifndef NULL
  19. #define NULL ((void *) 0)
  20. #endif
  21. static inline size_t _dl_strlen(const char * str)
  22. {
  23. register const char *ptr = (char *) str-1;
  24. while (*++ptr);
  25. return (ptr - str);
  26. }
  27. static inline char *_dl_strcat(char *dst, const char *src)
  28. {
  29. register char *ptr = dst-1;
  30. src--;
  31. while (*++ptr)
  32. ;/* empty */
  33. ptr--;
  34. while ((*++ptr = *++src) != 0)
  35. ;/* empty */
  36. return dst;
  37. }
  38. static inline char * _dl_strcpy(char * dst,const char *src)
  39. {
  40. register char *ptr = dst;
  41. dst--;src--;
  42. while ((*++dst = *++src) != 0);
  43. return ptr;
  44. }
  45. static inline int _dl_strcmp(const char * s1,const char * s2)
  46. {
  47. register unsigned char c1, c2;
  48. s1--;s2--;
  49. do {
  50. c1 = (unsigned char) *++s1;
  51. c2 = (unsigned char) *++s2;
  52. if (c1 == '\0')
  53. return c1 - c2;
  54. }
  55. while (c1 == c2);
  56. return c1 - c2;
  57. }
  58. static inline int _dl_strncmp(const char * s1,const char * s2,size_t len)
  59. {
  60. register unsigned char c1 = '\0';
  61. register unsigned char c2 = '\0';
  62. s1--;s2--;
  63. while (len > 0) {
  64. c1 = (unsigned char) *++s1;
  65. c2 = (unsigned char) *++s2;
  66. if (c1 == '\0' || c1 != c2)
  67. return c1 - c2;
  68. len--;
  69. }
  70. return c1 - c2;
  71. }
  72. static inline char * _dl_strchr(const char * str,int c)
  73. {
  74. register char ch;
  75. str--;
  76. do {
  77. if ((ch = *++str) == c)
  78. return (char *) str;
  79. }
  80. while (ch);
  81. return 0;
  82. }
  83. static inline char *_dl_strrchr(const char *str, int c)
  84. {
  85. register char *prev = 0;
  86. register char *ptr = (char *) str-1;
  87. while (*++ptr != '\0') {
  88. if (*ptr == c)
  89. prev = ptr;
  90. }
  91. if (c == '\0')
  92. return(ptr);
  93. return(prev);
  94. }
  95. static inline char *_dl_strstr(const char *s1, const char *s2)
  96. {
  97. register const char *s = s1;
  98. register const char *p = s2;
  99. do {
  100. if (!*p) {
  101. return (char *) s1;;
  102. }
  103. if (*p == *s) {
  104. ++p;
  105. ++s;
  106. } else {
  107. p = s2;
  108. if (!*s) {
  109. return NULL;
  110. }
  111. s = ++s1;
  112. }
  113. } while (1);
  114. }
  115. static inline void * _dl_memcpy(void * dst, const void * src, size_t len)
  116. {
  117. register char *a = dst-1;
  118. register const char *b = src-1;
  119. while (len) {
  120. *++a = *++b;
  121. --len;
  122. }
  123. return dst;
  124. }
  125. static inline int _dl_memcmp(const void * s1,const void * s2,size_t len)
  126. {
  127. unsigned char *c1 = (unsigned char *)s1-1;
  128. unsigned char *c2 = (unsigned char *)s2-1;
  129. while (len) {
  130. if (*++c1 != *++c2)
  131. return *c1 - *c2;
  132. len--;
  133. }
  134. return 0;
  135. }
  136. #if defined(powerpc)
  137. /* Will generate smaller and faster code due to loop unrolling.*/
  138. static inline void *_dl_memset(void *to, int c, size_t n)
  139. {
  140. unsigned long chunks;
  141. unsigned long *tmp_to;
  142. unsigned char *tmp_char;
  143. chunks = n / 4;
  144. tmp_to = to + n;
  145. c = c << 8 | c;
  146. c = c << 16 | c;
  147. if (!chunks)
  148. goto lessthan4;
  149. do {
  150. *--tmp_to = c;
  151. } while (--chunks);
  152. lessthan4:
  153. n = n % 4;
  154. if (!n ) return to;
  155. tmp_char = (unsigned char *)tmp_to;
  156. do {
  157. *--tmp_char = c;
  158. } while (--n);
  159. return to;
  160. }
  161. #else
  162. static inline void * _dl_memset(void * str,int c,size_t len)
  163. {
  164. register char *a = str;
  165. while (len--)
  166. *a++ = c;
  167. return str;
  168. }
  169. #endif
  170. static inline char *_dl_get_last_path_component(char *path)
  171. {
  172. register char *ptr = path-1;
  173. while (*++ptr)
  174. ;/* empty */
  175. /* strip trailing slashes */
  176. while (ptr != path && *--ptr == '/') {
  177. *ptr = '\0';
  178. }
  179. /* find last component */
  180. while (ptr != path && *--ptr != '/')
  181. ;/* empty */
  182. return ptr == path ? ptr : ptr+1;
  183. }
  184. /* Early on, we can't call printf, so use this to print out
  185. * numbers using the SEND_STDERR() macro. Avoid using mod
  186. * or using long division */
  187. static inline char *_dl_simple_ltoa(char * local, unsigned long i)
  188. {
  189. /* 20 digits plus a null terminator should be good for
  190. * 64-bit or smaller ints (2^64 - 1)*/
  191. char *p = &local[22];
  192. *--p = '\0';
  193. do {
  194. char temp;
  195. do_rem(temp, i, 10);
  196. *--p = '0' + temp;
  197. i /= 10;
  198. } while (i > 0);
  199. return p;
  200. }
  201. static inline char *_dl_simple_ltoahex(char * local, unsigned long i)
  202. {
  203. /* 16 digits plus a leading "0x" plus a null terminator,
  204. * should be good for 64-bit or smaller ints */
  205. char *p = &local[22];
  206. *--p = '\0';
  207. do {
  208. char temp = i & 0xf;
  209. if (temp <= 0x09)
  210. *--p = '0' + temp;
  211. else
  212. *--p = 'a' - 0x0a + temp;
  213. i >>= 4;
  214. } while (i > 0);
  215. *--p = 'x';
  216. *--p = '0';
  217. return p;
  218. }
  219. /* The following macros may be used in dl-startup.c to debug
  220. * ldso before ldso has fixed itself up to make function calls */
  221. /* On some (wierd) arches, none of this stuff works at all, so
  222. * disable the whole lot... */
  223. #if defined(__mips__)
  224. #define SEND_STDERR(X)
  225. #define SEND_ADDRESS_STDERR(X, add_a_newline)
  226. #define SEND_NUMBER_STDERR(X, add_a_newline)
  227. #else
  228. /* On some arches constant strings are referenced through the GOT.
  229. * This requires that load_addr must already be defined... */
  230. #if defined(mc68000) || defined(__arm__) || defined(__mips__) \
  231. || defined(__sh__) || defined(__powerpc__)
  232. # define CONSTANT_STRING_GOT_FIXUP(X) \
  233. if ((X) < (const char *) load_addr) (X) += load_addr
  234. #else
  235. # define CONSTANT_STRING_GOT_FIXUP(X)
  236. #endif
  237. #define SEND_STDERR(X) { \
  238. const char *tmp1 = (X); \
  239. CONSTANT_STRING_GOT_FIXUP(tmp1); \
  240. _dl_write (2, tmp1, _dl_strlen(tmp1)); \
  241. };
  242. #define SEND_ADDRESS_STDERR(ADR, add_a_newline) { \
  243. char tmp[26], v, *tmp2, *tmp1 = tmp; \
  244. unsigned long X = (unsigned long)(ADR); \
  245. CONSTANT_STRING_GOT_FIXUP(tmp1); \
  246. tmp2 = tmp1 + sizeof(tmp); \
  247. *--tmp2 = '\0'; \
  248. if (add_a_newline) *--tmp2 = '\n'; \
  249. do { \
  250. v = (X) & 0xf; \
  251. if (v <= 0x09) \
  252. *--tmp2 = '0' + v; \
  253. else \
  254. *--tmp2 = 'a' - 0x0a + v; \
  255. (X) >>= 4; \
  256. } while ((X) > 0); \
  257. *--tmp2 = 'x'; \
  258. *--tmp2 = '0'; \
  259. _dl_write (2, tmp2, tmp1 - tmp2 + sizeof(tmp) - 1); \
  260. };
  261. #define SEND_NUMBER_STDERR(X, add_a_newline) { \
  262. char tmp[26], v, *tmp2, *tmp1 = tmp; \
  263. CONSTANT_STRING_GOT_FIXUP(tmp1); \
  264. tmp2 = tmp1 + sizeof(tmp); \
  265. *--tmp2 = '\0'; \
  266. if (add_a_newline) *--tmp2 = '\n'; \
  267. do { \
  268. do_rem(v, (X), 10); \
  269. *--tmp2 = '0' + v; \
  270. (X) /= 10; \
  271. } while ((X) > 0); \
  272. _dl_write (2, tmp2, tmp1 - tmp2 + sizeof(tmp) - 1); \
  273. };
  274. #endif
  275. #endif