1 //-----------------------------------------------------------------------------
2 // Copyright (C) Proxmark3 contributors. See AUTHORS.md for details.
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // See LICENSE.txt for the text of the license.
15 //-----------------------------------------------------------------------------
16 // Common string.h functions
17 //-----------------------------------------------------------------------------
20 void *memcpy(void *dest
, const void *src
, int len
) {
22 const uint8_t *s
= src
;
31 void *memmove(void *dest
, const void *src
, size_t len
) {
38 char *lasts
= (char *)s
+ (len
- 1);
39 char *lastd
= d
+ (len
- 1);
46 void *memset(void *dest
, uint8_t c
, int len
) {
55 int memcmp(const void *av
, const void *bv
, int len
) {
56 const uint8_t *a
= av
;
57 const uint8_t *b
= bv
;
69 void memxor(uint8_t *dest
, uint8_t *src
, size_t len
) {
70 for (; len
> 0; len
--, dest
++, src
++)
74 int strlen(const char *str
) {
76 for (p
= str
; *p
!= '\0'; ++p
) {};
80 char *strncat(char *dest
, const char *src
, unsigned int n
) {
81 int dest_len
= strlen(dest
);
84 for (i
= 0 ; i
< n
&& src
[i
] != '\0' ; i
++)
85 dest
[dest_len
+ i
] = src
[i
];
87 dest
[dest_len
+ i
] = '\0';
92 char *strcat(char *dest
, const char *src
) {
93 int dest_len
= strlen(dest
);
96 for (i
= 0 ; src
[i
] != '\0' ; i
++)
97 dest
[dest_len
+ i
] = src
[i
];
98 dest
[dest_len
+ i
] = '\0';
102 ////////////////////////////////////////// code to do 'itoa'
104 /* reverse: reverse string s in place */
105 void strreverse(char s
[]) {
106 int j
= strlen(s
) - 1;
108 for (int i
= 0; i
< j
; i
++, j
--) {
115 /* itoa: convert n to characters in s */
116 void itoa(int n
, char s
[]) {
118 if ((sign
= n
) < 0) /* record sign */
119 n
= -n
; /* make n positive */
122 do { /* generate digits in reverse order */
123 s
[i
++] = n
% 10 + '0'; /* get next digit */
124 } while ((n
/= 10) > 0); /* delete it */
131 //////////////////////////////////////// END 'itoa' CODE
135 char *strcpy(char *dst
, const char *src
) {
138 for (; (*dst
= *src
) != '\0'; ++src
, ++dst
);
142 char *strncpy(char *dst
, const char *src
, size_t n
) {
148 if ((*d
++ = *s
++) == 0) {
149 /* NUL pad the remaining n-1 bytes */
160 int strcmp(const char *s1
, const char *s2
) {
161 while (*s1
== *s2
++) {
166 return (*(unsigned char *) s1
- * (unsigned char *) --s2
);
169 char *__strtok_r(char *, const char *, char **);
171 char *__strtok_r(char *s
, const char *delim
, char **last
) {
175 if (s
== NULL
&& (s
= *last
) == NULL
)
179 * Skip (span) leading delimiters (s += strspn(s, delim), sort of).
183 for (spanp
= (char *)delim
; (sc
= *spanp
++) != 0;) {
189 /* no non-delimiter characters */
196 * Scan token (scan for delimiters: s += strcspn(s, delim), sort of).
197 * Note that delim must have one NUL; we stop if we see that, too.
201 spanp
= (char *)delim
;
203 if ((sc
= *spanp
++) == c
) {
216 char *strtok(char *s
, const char *delim
) {
219 return (__strtok_r(s
, delim
, &last
));
223 char *strchr(const char *s
, int c
) {
224 while (*s
!= (char)c
)
230 size_t strspn(const char *s1
, const char *s2
) {
232 while (*s1
&& strchr(s2
, *s1
++))
237 char *strrchr(const char *s
, int c
) {
246 size_t strcspn(const char *s1
, const char *s2
) {
256 char *strpbrk(const char *s1
, const char *s2
) {
258 if (strchr(s2
, *s1
++))
263 int strncmp(const char *s1
, const char *s2
, size_t n
) {
266 return *(unsigned char *)(s1
- 1) - *(unsigned char *)(s2
- 1);
273 #define isspace(a) __extension__ ({ unsigned char bb__isspace = (a) - 9; bb__isspace == (' ' - 9) || bb__isspace <= (13 - 9); })
275 unsigned long strtoul(const char *p
, char **out_p
, int base
) {
280 if (((base
== 16) || (base
== 0)) &&
281 ((*p
== '0') && ((p
[1] == 'x') || (p
[1] == 'X')))) {
293 if ((c
>= '0') && (c
<= '9') && (c
- '0' < base
))
294 v
= (v
* base
) + (c
- '0');
295 else if ((c
>= 'a') && (c
<= 'z') && (c
- 'a' + 10 < base
))
296 v
= (v
* base
) + (c
- 'a' + 10);
297 else if ((c
>= 'A') && (c
<= 'Z') && (c
- 'A' + 10 < base
))
298 v
= (v
* base
) + (c
- 'A' + 10);
304 if (out_p
) *out_p
= (char *)p
;
308 long strtol(const char *p
, char **out_p
, int base
) {
318 if (((base
== 16) || (base
== 0)) &&
319 ((*p
== '0') && ((p
[1] == 'x') || (p
[1] == 'X')))) {
331 if ((c
>= '0') && (c
<= '9') && (c
- '0' < base
))
332 v
= (v
* base
) + (c
- '0');
333 else if ((c
>= 'a') && (c
<= 'z') && (c
- 'a' + 10 < base
))
334 v
= (v
* base
) + (c
- 'a' + 10);
335 else if ((c
>= 'A') && (c
<= 'Z') && (c
- 'A' + 10 < base
))
336 v
= (v
* base
) + (c
- 'A' + 10);
343 if (out_p
) *out_p
= (char *)p
;
347 char c_tolower(int c
) {
348 // (int)a = 97, (int)A = 65
349 // (a)97 - (A)65 = 32
350 // therefore 32 + 65 = a
351 return c
> 64 && c
< 91 ? c
+ 32 : c
;
354 char c_isprint(unsigned char c
) {
355 if (c
>= 0x20 && c
<= 0x7e)