#include <assert.h>
#include <stdint.h>
#include <util.h>
typedef unsigned long __attribute__((__may_alias__)) ulong_alias;
void
memzero(void *s, unsigned long n)
{
uint8_t *p = s;
assert((unsigned long)s % sizeof(unsigned long) == 0);
assert(n % sizeof(unsigned long) == 0);
while (n != 0) {
*(ulong_alias *)p = 0;
p += sizeof(ulong_alias);
n -= sizeof(ulong_alias);
}
}
void*
memset(void *s, unsigned long c, unsigned long n)
{
uint8_t *p;
if (likely(c == 0 && ((unsigned long)s % sizeof(unsigned long)) == 0 && (n % sizeof(unsigned long)) == 0)) {
memzero(s, n);
} else {
for (p = (uint8_t *)s; n > 0; n--, p++) {
*p = (uint8_t)c;
}
}
return s;
}
void* USED
memcpy(void* ptr_dst, const void* ptr_src, unsigned long n)
{
uint8_t *p;
const uint8_t *q;
for (p = (uint8_t *)ptr_dst, q = (const uint8_t *)ptr_src; n; n--, p++, q++) {
*p = *q;
}
return ptr_dst;
}
int PURE
strncmp(const char* s1, const char* s2, int n)
{
word_t i;
int diff;
for (i = 0; i < n; i++) {
diff = ((unsigned char*)s1)[i] - ((unsigned char*)s2)[i];
if (diff != 0 || s1[i] == '\0') {
return diff;
}
}
return 0;
}
long CONST
char_to_long(char c)
{
if (c >= '0' && c <= '9') {
return c - '0';
} else if (c >= 'A' && c <= 'F') {
return c - 'A' + 10;
} else if (c >= 'a' && c <= 'f') {
return c - 'a' + 10;
}
return -1;
}
long PURE
str_to_long(const char* str)
{
unsigned int base;
long res;
long val = 0;
char c;
if (*str == '0' && (*(str + 1) == 'x' || *(str + 1) == 'X')) {
base = 16;
str += 2;
} else {
base = 10;
}
if (!*str) {
return -1;
}
c = *str;
while (c != '\0') {
res = char_to_long(c);
if (res == -1 || res >= base) {
return -1;
}
val = val * base + res;
str++;
c = *str;
}
return val;
}