LumaOS  v0.1.116
An experimental x86-64 OS
Loading...
Searching...
No Matches
kstring.c
Go to the documentation of this file.
1
9#include <stddef.h>
10#include <stdbool.h>
11#include <stdint.h>
12#include <lib/kstring.h>
13
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;
17
18 if (s < d+n && d < s+n)
19 return dest;
20
21 asm volatile ("rep movsb" : "+D"(d), "+S"(s), "+c"(n) : : "memory");
22
23 return dest;
24}
25
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;
29
30 if (d < s) {
31 // Copy start -> end
32 asm volatile ("rep movsb" : "+D"(d), "+S"(s), "+c"(n) : : "memory");
33
34 } else {
35 // Copy end -> start
36 for (size_t i = n; i > 0; i--) {
37 d[i-1] = s[i-1];
38 }
39 }
40
41 return dest;
42}
43
44void* memset(void* dest, int c, size_t n) {
45 uint8_t* d = (uint8_t*)dest;
46
47 asm volatile ("rep stosb" : "+D"(d), "+a"(c), "+c"(n) : : "memory");
48
49 return dest;
50}
51
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;
55
56 for (size_t i = 0; i < n; i++) {
57 if (int_a[i] < int_b[i])
58 return -1;
59 else if (int_a[i] > int_b[i])
60 return 1;
61 }
62
63 return 0;
64}
65
66int strcmp(const char *s1, const char *s2)
67{
68 while (*s1 && (*s1 == *s2))
69 {
70 s1++;
71 s2++;
72 }
73
74 return (unsigned char)*s1 - (unsigned char)*s2;
75}
76
77int strncmp(const char *s1, const char *s2, size_t n)
78{
79 while (n && *s1 && (*s1 == *s2))
80 {
81 s1++;
82 s2++;
83 n--;
84 }
85
86 if (n == 0)
87 return 0;
88
89 return (unsigned char)*s1 - (unsigned char)*s2;
90}
91
92size_t strlen(const char *str) {
93 size_t index = 1;
94
95 while (true) {
96 if (str[index-1] == '\0') {
97 index--;
98 break;
99 }
100
101 index++;
102 }
103
104 return index;
105}
106
107char* strcat(char *str1, const char *str2) {
108 const size_t str1_len = strlen(str1);
109 const size_t str2_len = strlen(str2);
110
111 for (size_t i = str1_len; i < str1_len+str2_len; i++) {
112 str1[i] = str2[i - str1_len];
113 }
114
115 str1[str1_len + str2_len] = '\0';
116
117 return str1;
118}
119
120char* int_to_str(const uint64_t value, char *buff) {
121 uint64_t next = value; // Holds the next value to calculate
122 uint64_t index = 0; // Holds how many characters have been calculated
123
124 // Get number length
125 while (next != 0) { // Loop until the next value is 0, so the number is done
126 next /= 10; // move the decimal point over by one to the left
127 index++; // continue, increase length counter
128 }
129
130 // If the value started as 0, set the length to 1
131 if (value == 0)
132 index = 1;
133
134 const uint64_t MAX_LENGTH = index; // Hold the length of the number
135 char chars[MAX_LENGTH]; // Hold the number (backwards) as chars
136
137 next = value; // Reset next value
138 index = 0; // Reset the index
139
140 while (next != 0) {
141 if (index >= MAX_LENGTH) // Check if the index passed the length
142 {
143 break; // Stop the loop if so
144 }
145 chars[index] = ((next % 10) + '0'); // add the remainder of next/10, + '0' to turn it into the corresponding char to the chars array
146
147 next /= 10; // move the decimal, using lossy division
148 index++;
149 }
150
151 if (value == 0) { // If the number started as one
152 chars[0] = '0'; // set the character in chars to 0
153 index = 1; // set index to 1
154 }
155
156 for (int i = index - 1; i >= 0; i--) { // For each char in chars
157 buff[MAX_LENGTH - i - 1] = chars[i]; // Add the char to the end of the string, but leaving space for the null terminator
158 }
159
160 buff[MAX_LENGTH] = '\0'; // Add the null terminator
161
162 return buff;
163}
164
165void int_to_hex_str(const unsigned long long value, char *buff, const uint8_t char_limit) {
166 unsigned long long next = value; // Holds the next value to calculate
167 uint64_t index = 0; // Holds how many characters have been calculated
168
169 // CALCULATE LENGTH
170 while (next != 0) { // Loop until the next value is 0, so the number is done
171 next /= 10; // move the decimal point over by one to the left
172 index++; // continue, increase length counter
173 }
174
175 // If the value started as 0, set the length to 1
176 if (value == 0)
177 index = 1;
178
179 const uint64_t length = char_limit;
180 char chars[length];
181
182 next = value; // Reset next value
183 index = 0; // Reset the index
184
185 while (next != 0) {
186 const char *digits = "0123456789abcdef";
187 if (index >= length) // Check if the index passed the length
188 break; // Stop the loop if so
189
190 chars[index] = digits[next % 16]; // Set the digit to the correct digit based on the remainder
191
192 next /= 16; // move the decimal, using lossy division
193 index++;
194 }
195
196 if (value == 0) { // If the number started as one
197 chars[0] = '0'; // set the character in chars to 0
198 index = 1; // set index to 1
199 }
200
201 for (int i = index - 1; i >= 0; i--) { // For each char in chars backwards, add it to the buffer
202 buff[index - (i + 1)] = chars[i];
203 }
204
205 buff[index] = '0x0';
206}
size_t strlen(const char *str)
Calculates the length of a string.
Definition kstring.c:92
int strncmp(const char *s1, const char *s2, size_t n)
Compares 2 strings up to n characters.
Definition kstring.c:77
char * strcat(char *str1, const char *str2)
Concatenates (joins) 2 strings.
Definition kstring.c:107
int strcmp(const char *s1, const char *s2)
Compares 2 strings.
Definition kstring.c:66
void * memset(void *dest, int c, size_t n)
Fills n bytes of the destination with c.
Definition kstring.c:44
int memcmp(const void *a, const void *b, const size_t n)
Compares 2 the value of 2 memory addresses.
Definition kstring.c:52
void * memmove(void *dest, const void *src, size_t n)
Copies memory from one address to the other with possible overlap.
Definition kstring.c:26
char * int_to_str(const uint64_t value, char *buff)
Converts an integer to a string.
Definition kstring.c:120
void * memcpy(void *dest, const void *src, size_t n)
Copies memory from one address to the other without overlapping.
Definition kstring.c:14
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.
Definition kstring.c:165
Utilities for memory and string operations.