14void*
memcpy(
void* dest,
const void* src,
size_t n) {
15 uint8_t* d = (uint8_t*)dest;
16 const uint8_t* s = (
const uint8_t*)src;
18 if (s < d+n && d < s+n)
21 asm volatile (
"rep movsb" :
"+D"(d),
"+S"(s),
"+c"(n) : :
"memory");
26void*
memmove(
void* dest,
const void* src,
size_t n) {
27 uint8_t* d = (uint8_t*)dest;
28 const uint8_t* s = (
const uint8_t*)src;
32 asm volatile (
"rep movsb" :
"+D"(d),
"+S"(s),
"+c"(n) : :
"memory");
36 for (
size_t i = n; i > 0; i--) {
44void*
memset(
void* dest,
int c,
size_t n) {
45 uint8_t* d = (uint8_t*)dest;
47 asm volatile (
"rep stosb" :
"+D"(d),
"+a"(c),
"+c"(n) : :
"memory");
52int memcmp(
const void* a,
const void* b,
const size_t n) {
53 const uint8_t* int_a = (uint8_t*)a;
54 const uint8_t* int_b = (uint8_t*)b;
56 for (
size_t i = 0; i < n; i++) {
57 if (int_a[i] < int_b[i])
59 else if (int_a[i] > int_b[i])
66int strcmp(
const char *s1,
const char *s2)
68 while (*s1 && (*s1 == *s2))
74 return (
unsigned char)*s1 - (
unsigned char)*s2;
77int strncmp(
const char *s1,
const char *s2,
size_t n)
79 while (n && *s1 && (*s1 == *s2))
89 return (
unsigned char)*s1 - (
unsigned char)*s2;
96 if (str[index-1] ==
'\0') {
107char*
strcat(
char *str1,
const char *str2) {
108 const size_t str1_len =
strlen(str1);
109 const size_t str2_len =
strlen(str2);
111 for (
size_t i = str1_len; i < str1_len+str2_len; i++) {
112 str1[i] = str2[i - str1_len];
115 str1[str1_len + str2_len] =
'\0';
121 uint64_t next = value;
134 const uint64_t MAX_LENGTH = index;
135 char chars[MAX_LENGTH];
141 if (index >= MAX_LENGTH)
145 chars[index] = ((next % 10) +
'0');
156 for (
int i = index - 1; i >= 0; i--) {
157 buff[MAX_LENGTH - i - 1] = chars[i];
160 buff[MAX_LENGTH] =
'\0';
165void int_to_hex_str(
const unsigned long long value,
char *buff,
const uint8_t char_limit) {
166 unsigned long long next = value;
179 const uint64_t length = char_limit;
186 const char *digits =
"0123456789abcdef";
190 chars[index] = digits[next % 16];
201 for (
int i = index - 1; i >= 0; i--) {
202 buff[index - (i + 1)] = chars[i];
size_t strlen(const char *str)
Calculates the length of a string.
int strncmp(const char *s1, const char *s2, size_t n)
Compares 2 strings up to n characters.
char * strcat(char *str1, const char *str2)
Concatenates (joins) 2 strings.
int strcmp(const char *s1, const char *s2)
Compares 2 strings.
void * memset(void *dest, int c, size_t n)
Fills n bytes of the destination with c.
int memcmp(const void *a, const void *b, const size_t n)
Compares 2 the value of 2 memory addresses.
void * memmove(void *dest, const void *src, size_t n)
Copies memory from one address to the other with possible overlap.
char * int_to_str(const uint64_t value, char *buff)
Converts an integer to a string.
void * memcpy(void *dest, const void *src, size_t n)
Copies memory from one address to the other without overlapping.
void int_to_hex_str(const unsigned long long value, char *buff, const uint8_t char_limit)
Converts an integer value into a hex string.
Utilities for memory and string operations.