diff options
Diffstat (limited to 'src/common')
-rw-r--r-- | src/common/Makefile | 8 | ||||
-rw-r--r-- | src/common/include/stdlib.h | 14 | ||||
-rw-r--r-- | src/common/include/string.h | 14 | ||||
-rw-r--r-- | src/common/include/types.h | 17 | ||||
-rw-r--r-- | src/common/string.c | 80 |
5 files changed, 133 insertions, 0 deletions
diff --git a/src/common/Makefile b/src/common/Makefile new file mode 100644 index 0000000..ef0f6b1 --- /dev/null +++ b/src/common/Makefile @@ -0,0 +1,8 @@ +Out = _common.o +Obj = string.o +ExtObj = + +include $(SrcPath)/common.make + +LDFLAGS += -r +CFLAGS += -I $(SrcPath)/common/include diff --git a/src/common/include/stdlib.h b/src/common/include/stdlib.h new file mode 100644 index 0000000..2c5e6dd --- /dev/null +++ b/src/common/include/stdlib.h @@ -0,0 +1,14 @@ +#ifndef _DEF_STDLIB_H +#define _DEF_STDLIB_H + +#include <types.h> + +#define MIN(a, b) ((a) < (b) ? (a) : (b)) +#define MAX(a, b) ((a) > (b) ? (a) : (b)) + +void *memcpy(void *dest, const void *src, int count); +uint8_t *memset(uint8_t *dest, uint8_t val, int count); +uint16_t *memsetw(uint16_t *dest, uint16_t val, int count); + +#endif + diff --git a/src/common/include/string.h b/src/common/include/string.h new file mode 100644 index 0000000..a41459e --- /dev/null +++ b/src/common/include/string.h @@ -0,0 +1,14 @@ +#ifndef _DEF_STRING_H +#define _DEF_STRING_H + +// #include <stdlib.h> +#include <types.h> + +int strlen(const char *str); +char *strcpy(char *dest, const char *src); +// char *strdup(const char *src); // uses malloc, that's bad +char *strchr(const char *str, char c); +char *strcat(char *dest, const char *src); +int strcmp(const char *s1, const char *s2); + +#endif diff --git a/src/common/include/types.h b/src/common/include/types.h new file mode 100644 index 0000000..ec24ce5 --- /dev/null +++ b/src/common/include/types.h @@ -0,0 +1,17 @@ +#ifndef DEF_TYPES_H +#define DEF_TYPES_H + +typedef unsigned long long uint64_t; +typedef unsigned int uint32_t; +typedef unsigned short uint16_t; +typedef unsigned char uint8_t; +typedef long long int64_t; +typedef int int32_t; +typedef short int16_t; +typedef char int8_t; + +typedef unsigned int size_t; + +#define NULL 0 + +#endif diff --git a/src/common/string.c b/src/common/string.c new file mode 100644 index 0000000..b2ec309 --- /dev/null +++ b/src/common/string.c @@ -0,0 +1,80 @@ +#include <string.h> +#include <stdlib.h> + +int strlen(const char *str) { + int i = 0; + while (str[i++]); + return i; +} + +char *strchr(const char *str, char c) { + while (*str) { + if (*str == c) return (char*)str; + str++; + } + return NULL; +} + +char *strcpy(char *dest, const char *src) { + memcpy(dest, src, strlen(src) + 1); + return (char*)src; +} + +/*char *strdup(const char *src) { + char* ret = malloc(strlen(src) + 1); + if (ret == NULL) return ret; + strcpy(ret, src); + return ret; +}*/ + +char *strcat(char *dest, const char *src) { + char *dest2 = dest; + dest2 += strlen(dest) - 1; + while (*src) { + *dest2 = *src; + src++; + dest2++; + } + *dest2 = 0; + return dest; +} + +int strcmp(const char *s1, const char *s2) { + while ((*s1) && (*s1 == *s2)) { + s1++; + s2++; + } + return (* (unsigned char*)s1 - *(unsigned char*)s2); +} + +void *memcpy(void *vd, const void *vs, int count) { + uint8_t *dest = (uint8_t*)vd, *src = (uint8_t*)vs; + uint32_t f = count % 4, n = count / 4, i; + const uint32_t* s = (uint32_t*)src; + uint32_t* d = (uint32_t*)dest; + for (i = 0; i < n; i++) { + d[i] = s[i]; + } + if (f != 0) { + for (i = count - f; i < count; i++) { + dest[i] = src[i]; + } + } + return vd; +} + +uint8_t *memset(uint8_t *dest, uint8_t val, int count) { + int i; + for (i = 0; i < count; i++) { + dest[i] = val; + } + return dest; +} + +uint16_t *memsetw(uint16_t *dest, uint16_t val, int count) { + int i; + for (i = 0; i < count; i++) { + dest[i] = val; + } + return dest; +} |