Merge pull request #108 from p-l-/fix-hf-mf-csave
[legacy-proxmark3.git] / armsrc / string.c
blob945a4cf6d0dae98a8d58c513120673e57f5735f4
1 //-----------------------------------------------------------------------------
2 // Jonathan Westhues, Sept 2005
3 //
4 // This code is licensed to you under the terms of the GNU GPL, version 2 or,
5 // at your option, any later version. See the LICENSE.txt file for the text of
6 // the license.
7 //-----------------------------------------------------------------------------
8 // Common string.h functions
9 //-----------------------------------------------------------------------------
11 #include "string.h"
12 #include <stdint.h>
14 void *memcpy(void *dest, const void *src, int len)
16 uint8_t *d = dest;
17 const uint8_t *s = src;
18 while((len--) > 0) {
19 *d = *s;
20 d++;
21 s++;
23 return dest;
26 void *memset(void *dest, int c, int len)
28 uint8_t *d = dest;
29 while((len--) > 0) {
30 *d = c;
31 d++;
33 return dest;
36 int memcmp(const void *av, const void *bv, int len)
38 const uint8_t *a = av;
39 const uint8_t *b = bv;
41 while((len--) > 0) {
42 if(*a != *b) {
43 return *a - *b;
45 a++;
46 b++;
48 return 0;
51 void memxor(uint8_t * dest, uint8_t * src, size_t len) {
52 for( ; len > 0; len--,dest++,src++)
53 *dest ^= *src;
56 int strlen(const char *str)
58 int l = 0;
59 while(*str) {
60 l++;
61 str++;
63 return l;
66 char* strncat(char *dest, const char *src, unsigned int n)
68 unsigned int dest_len = strlen(dest);
69 unsigned int i;
71 for (i = 0 ; i < n && src[i] != '\0' ; i++)
72 dest[dest_len + i] = src[i];
73 dest[dest_len + i] = '\0';
75 return dest;
78 char* strcat(char *dest, const char *src)
80 unsigned int dest_len = strlen(dest);
81 unsigned int i;
83 for (i = 0 ; src[i] != '\0' ; i++)
84 dest[dest_len + i] = src[i];
85 dest[dest_len + i] = '\0';
87 return dest;
89 ////////////////////////////////////////// code to do 'itoa'
91 /* reverse: reverse string s in place */
92 void strreverse(char s[])
94 int c, i, j;
96 for (i = 0, j = strlen(s)-1; i<j; i++, j--) {
97 c = s[i];
98 s[i] = s[j];
99 s[j] = c;
103 /* itoa: convert n to characters in s */
104 void itoa(int n, char s[])
106 int i, sign;
108 if ((sign = n) < 0) /* record sign */
109 n = -n; /* make n positive */
110 i = 0;
111 do { /* generate digits in reverse order */
112 s[i++] = n % 10 + '0'; /* get next digit */
113 } while ((n /= 10) > 0); /* delete it */
114 if (sign < 0)
115 s[i++] = '-';
116 s[i] = '\0';
117 strreverse(s);
120 //////////////////////////////////////// END 'itoa' CODE