| 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 | //----------------------------------------------------------------------------- |
| 10 | |
| 11 | #include "string.h" |
| 12 | #include <stdint.h> |
| 13 | |
| 14 | void *memcpy(void *dest, const void *src, int len) |
| 15 | { |
| 16 | uint8_t *d = dest; |
| 17 | const uint8_t *s = src; |
| 18 | while((len--) > 0) { |
| 19 | *d = *s; |
| 20 | d++; |
| 21 | s++; |
| 22 | } |
| 23 | return dest; |
| 24 | } |
| 25 | |
| 26 | void *memset(void *dest, int c, int len) |
| 27 | { |
| 28 | uint8_t *d = dest; |
| 29 | while((len--) > 0) { |
| 30 | *d = c; |
| 31 | d++; |
| 32 | } |
| 33 | return dest; |
| 34 | } |
| 35 | |
| 36 | int memcmp(const void *av, const void *bv, int len) |
| 37 | { |
| 38 | const uint8_t *a = av; |
| 39 | const uint8_t *b = bv; |
| 40 | |
| 41 | while((len--) > 0) { |
| 42 | if(*a != *b) { |
| 43 | return *a - *b; |
| 44 | } |
| 45 | a++; |
| 46 | b++; |
| 47 | } |
| 48 | return 0; |
| 49 | } |
| 50 | |
| 51 | void memxor(uint8_t * dest, uint8_t * src, size_t len) { |
| 52 | for( ; len > 0; len--,dest++,src++) |
| 53 | *dest ^= *src; |
| 54 | } |
| 55 | |
| 56 | int strlen(const char *str) |
| 57 | { |
| 58 | int l = 0; |
| 59 | while(*str) { |
| 60 | l++; |
| 61 | str++; |
| 62 | } |
| 63 | return l; |
| 64 | } |
| 65 | |
| 66 | char* strncat(char *dest, const char *src, unsigned int n) |
| 67 | { |
| 68 | unsigned int dest_len = strlen(dest); |
| 69 | unsigned int i; |
| 70 | |
| 71 | for (i = 0 ; i < n && src[i] != '\0' ; i++) |
| 72 | dest[dest_len + i] = src[i]; |
| 73 | dest[dest_len + i] = '\0'; |
| 74 | |
| 75 | return dest; |
| 76 | } |
| 77 | |
| 78 | char* strcat(char *dest, const char *src) |
| 79 | { |
| 80 | unsigned int dest_len = strlen(dest); |
| 81 | unsigned int i; |
| 82 | |
| 83 | for (i = 0 ; src[i] != '\0' ; i++) |
| 84 | dest[dest_len + i] = src[i]; |
| 85 | dest[dest_len + i] = '\0'; |
| 86 | |
| 87 | return dest; |
| 88 | } |
| 89 | ////////////////////////////////////////// code to do 'itoa' |
| 90 | |
| 91 | /* reverse: reverse string s in place */ |
| 92 | void strreverse(char s[]) |
| 93 | { |
| 94 | int c, i, j; |
| 95 | |
| 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; |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | /* itoa: convert n to characters in s */ |
| 104 | void itoa(int n, char s[]) |
| 105 | { |
| 106 | int i, sign; |
| 107 | |
| 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); |
| 118 | } |
| 119 | |
| 120 | //////////////////////////////////////// END 'itoa' CODE |