-
Notifications
You must be signed in to change notification settings - Fork 2
/
ass-rt.c
243 lines (211 loc) · 3.53 KB
/
ass-rt.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
#include "ass.h"
extern IBios* interface;
//-------
//STRINGS
//-------
int strcpy_s(char* dest, size_t len, const char* src)
{
size_t i;
if (!dest) return 1;
if (len == 0) return 1;
if (!src)
{
dest[0] = '\0';
return 1;
}
for (i = 0; i < len; i++)
if ((dest[i] = src[i]) == '\0') return 0;
dest[0] = '\0';
return 2;
}
int strcat_s(char* dest, size_t len, const char* src)
{
size_t i, j;
if (!dest) return 1;
if (len == 0) return 1;
if (!src)
{
dest[0] = '\0';
return 1;
}
for (i = 0; i < len; i++)
{
if (dest[i] == '\0')
{
for (j = 0; (j + i) < len; j++)
if ((dest[j + i] = src[j]) == '\0') return 0;
}
}
dest[0] = '\0';
return 2;
}
size_t strnlen_s(const char* str, size_t max)
{
size_t i;
if (str == 0) return 0;
for (i = 0; i < max && *str; i++, str++);
return i;
}
int strncmp(const char *l, const char *r, size_t max)
{
const unsigned char *a=(void *)l, *b=(void *)r;
if (!max--) return 0;
for (; *a && *b && max && *a == *b ; a++, b++, max--);
return *a - *b;
}
char *strchr(const char *haystack, int needle)
{
size_t pos = 0;
while (haystack[pos])
{
if (haystack[pos] == needle)
return (char*)(haystack + pos);
pos++;
}
return 0;
}
char *strrchr(const char *haystack, int needle)
{
size_t pos = strnlen_s(haystack, 2048) + 1;
while (pos--)
if (haystack[pos] == needle)
return (char*)(haystack + pos);
return 0;
}
int strkitten_s(char* dest, size_t len, char src)
{
size_t i;
if (!dest) return 1;
if (len == 0) return 1;
i = strnlen_s(dest, len);
if (i == len)
return 2;
dest[i++] = src;
dest[i++] = '\0';
return 0;
}
int isgraph(int c)
{
return (unsigned int)c - 0x21 < 0x5E;
}
int isprint(int c)
{
return (unsigned int)c - 0x20 < 0x5F;
}
int isalpha(int c)
{
return ((unsigned int)c | 32) - 'a' < 26;
}
int isdigit(int c)
{
return (unsigned int)c - '0' < 10;
}
int isalnum(int c)
{
return isalpha(c) || isdigit(c);
}
int isspace(int c)
{
return c == ' ' || (unsigned int) c - '\t' < 5;
}
int ispunct(int c)
{
return isgraph(c) && !isalnum(c);
}
int isblank(int c)
{
return (c == ' ' || c == '\t');
}
int isxdigit(int c)
{
return isdigit(c) || ((unsigned int)c | 32) - 'a' < 6;
}
int iscntrl(int c)
{
return (unsigned int)c < 0x20 || c == 0x7F;
}
int isascii(int c)
{
return !(c & ~0x7F);
}
int isupper(int c)
{
return (unsigned int)c - 'A' < 26;
}
int islower(int c)
{
return (unsigned int)c - 'a' < 26;
}
int toupper(int c)
{
if (islower(c))
return c & 0x5F;
return c;
}
int tolower(int c)
{
if (isupper(c))
return c | 32;
return c;
}
int toascii(int c)
{
return c & 0x7F;
}
char* strcpy(char* s2, const char* s1)
{
int l = strnlen_s((char*)s1, 1024);
memcpy(s2, s1, l + 1);
s2[l + 1] = 0;
return s2;
}
char* strdup(const char* s1)
{
int l = strnlen_s((char*)s1, 1024);
char* s2 = (char*)malloc(l + 1);
memcpy(s2, s1, l + 1);
s2[l + 1] = 0;
return s2;
}
int atoi(char* str)
{
int res = 0;
for (int i = 0; str[i] != '\0'; ++i)
res = res * 10 + str[i] - '0';
return res;
}
//------
//MEMCPY
//------
void* memcpy(void* dst, const void* src, size_t count)
{
void* ret = dst;
while (count--)
{
*(char*)dst = *(char*)src;
dst = (char*)dst + 1;
src = (char*)src + 1;
}
return(ret);
}
void* memset(void* dst, int val, size_t count)
{
void* start = dst;
while (count--)
{
*(char *)dst = (char)val;
dst = (char *)dst + 1;
}
return(start);
}
int memcmp(const void* dst, const void* src, size_t count)
{
int r = 0;
const char* d = (const char*)dst;
const char* s = (const char*)src;
while (count-- && r == 0)
{
r = *d++ - *s++;
}
return r;
}