-
Notifications
You must be signed in to change notification settings - Fork 27
/
ministdlib.c
73 lines (63 loc) · 1.64 KB
/
ministdlib.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include "APIResolve.h"
// Really simple re-implementation of some stdlib functions using basic C
// Had to disable optimization here since GCC kept being smart and replacing this with
// a call to memset :)
#pragma GCC push_options
#pragma GCC optimize ("O0")
void* _memset(void * dst, int s, size_t count) {
size_t i = 0;
char *dst2 = dst;
for (i=0; i<count; i++) {
dst2[i] = s;
}
return dst;
}
#pragma GCC pop_options
size_t _strlen(const char *s) {
size_t i = 0;
while (1) {
if (s[i] == 0) {
return i;
}
i++;
}
}
void * _memcpy (void *dst, const void *src, size_t n) {
size_t i = 0;
char *dst2 = dst;
const char *src2 = src;
for (i=0; i<n; i++) {
dst2[i] = src2[i];
}
return dst;
}
// Really simple puts for debugging
void _puts(char *buf) {
tGetStdHandle _GetStdHandle = (tGetStdHandle)getFunctionPtr(HASH_KERNEL32, HASH_GetStdHandle);
tWriteFile _WriteFile = (tWriteFile)getFunctionPtr(HASH_KERNEL32, HASH_WriteFile);
DWORD done;
_WriteFile(_GetStdHandle(STD_OUTPUT_HANDLE), buf, _strlen(buf), &done, NULL);
}
// These strcmp function only return 0 when equal and -1 when different
// they can't be used for sorting
int _strcmp (const char *s1, const char *s2) {
int i;
if (_strlen(s1) != _strlen(s2)) {
return -1;
}
for(i=0; i<_strlen(s1); i++) {
if (s1[i] != s2[i]) {
return -1;
}
}
return 0;
}
int _strncmp (const char *s1, const char *s2, size_t sz) {
int i;
for(i=0; i<sz; i++) {
if (s1[i] != s2[i]) {
return -1;
}
}
return 0;
}