102 Dalmatians: Puppies to the Rescue
Decompilation Source Reference | GitLab repository
Loading...
Searching...
No Matches
strings.c
Go to the documentation of this file.
1/* Recovered game implementation; original addresses identify provenance. */
7#include <intrin.h>
8#include <math.h>
9#include <stdarg.h>
10#include <string.h>
11
12// Original pcdogs.exe: 0x445f4f
13// Parses leading whitespace, an optional sign, and decimal digits into an integer using the CRT's original arithmetic.
14int32_t String_ParseInt(char const *string)
15{
16 char const *cursor = string;
17
18 while (true)
19 {
20 int32_t is_space;
21
22 if (g_crtLcCodepage <= 1)
23 {
24 uint32_t space_class_bits = (uint32_t)*(uint8_t *)cursor;
25
26 is_space = ((struct CRT_CharacterClass *)g_pCharTypeTable)[space_class_bits].classification_flags & CRT_CTYPE_SPACE;
27 }
28 else
29 is_space = CRT_isctype((uint32_t)*(uint8_t *)cursor, CRT_CTYPE_SPACE);
30
31 if (!is_space)
32 break;
33
34 cursor = &cursor[1];
35 }
36
37 uint32_t ch = (uint32_t)*(uint8_t *)cursor;
38 const char *digit_cursor = cursor + 1;
39 uint32_t sign_character = ch;
40
41 if (ch == '-' || ch == '+')
42 {
43 ch = (uint32_t)*(uint8_t *)digit_cursor;
44 digit_cursor = &digit_cursor[1];
45 }
46
47 int32_t result = 0;
48
49 while (true)
50 {
51 int32_t is_digit;
52
53 if (g_crtLcCodepage <= 1)
54 {
55 uint32_t digit_class_bits = g_pCharTypeTable;
56
57 is_digit = ((struct CRT_CharacterClass *)digit_class_bits)[ch].classification_flags & CRT_CTYPE_DIGIT;
58 }
59 else
60 is_digit = CRT_isctype(ch, CRT_CTYPE_DIGIT);
61
62 if (!is_digit)
63 break;
64
65 result = ch + result * 0xa - '0';
66 ch = (uint32_t)*(uint8_t *)digit_cursor;
67 digit_cursor = &digit_cursor[1];
68 }
69
70 if (sign_character != '-')
71 return result;
72
73 return -(result);
74}
75
76// Original pcdogs.exe: 0x445fda
77// Delegates decimal integer parsing to the CRT integer parser.
78int32_t String_ParseAtoi(char const *string)
79{
80 return String_ParseInt(string);
81}
82
83// Original pcdogs.exe: 0x446650
84// Finds the first occurrence of a terminated substring and returns NULL when no match exists. An empty needle matches the beginning of the input.
85char *CRT_strstr(char const *haystack, char const *needle)
86{
87 if (!*needle)
88 return (char *)haystack;
89
90 for (; *haystack; ++haystack)
91 {
92 const char *haystack_cursor = haystack, *needle_cursor = needle;
93
94 while (*needle_cursor && *haystack_cursor == *needle_cursor)
95 {
96 ++haystack_cursor;
97 ++needle_cursor;
98 }
99
100 if (!*needle_cursor)
101 return (char *)haystack;
102 }
103
104 return NULL;
105}
106
107// Original pcdogs.exe: 0x449080
108// Finds the first occurrence of a byte in a terminated string, including a match for the terminating zero, and returns NULL when absent.
109char *CRT_StrchrCdecl(char const *string, int32_t character)
110{
111 int32_t byte_character = (uint8_t)character;
112
113 /* tailcall */
114 return String_StrchrCRT(byte_character, string);
115}
116
117// Original pcdogs.exe: 0x449086
118// Finds the first occurrence of a byte in a terminated string, including its terminating zero, and returns NULL when absent. The native entry receives the search byte in EAX and the string through the stack.
119char *String_StrchrCRT(int32_t character, char const *string)
120{
121 unsigned char search_byte = (unsigned char)character;
122
123 do
124 {
125 if ((unsigned char)*string == search_byte)
126 return (char *)string;
127 } while (*string++);
128
129 return NULL;
130}
131
132// Original pcdogs.exe: 0x449650
133// Moves bytes safely between overlapping ranges by selecting the appropriate copy direction and returns the destination.
134char *CRT_MoveBytesOverlapSafe(char *dest, char const *src, uint32_t count)
135{
136 unsigned char *destination_bytes = (unsigned char *)dest;
137 const unsigned char *source_bytes = (const unsigned char *)src;
138
139 if ((uintptr_t)destination_bytes > (uintptr_t)source_bytes && (uintptr_t)destination_bytes < (uintptr_t)source_bytes + count)
140 {
141 while (count)
142 {
143 --count;
144 destination_bytes[count] = source_bytes[count];
145 }
146 }
147 else
148 {
149 for (uint32_t i = 0; i < count; ++i)
150 destination_bytes[i] = source_bytes[i];
151 }
152
153 return dest;
154}
155
156// Original pcdogs.exe: 0x44a900
157// Copies a string including its terminator and returns the destination. The caller must provide enough destination storage.
158char *CRT_strcpy(char *dest, char const *src)
159{
160 char *destination_start = dest;
161
162 do
163 {
164 *dest++ = *src;
165 } while (*src++);
166
167 return destination_start;
168}
169
170// Original pcdogs.exe: 0x44a910
171// Appends a string including its terminator to the destination and returns the destination. The caller must provide enough storage for the combined string.
172char *CRT_strcat(char *dest, char const *src)
173{
174 char *destination_start = dest;
175
176 while (*dest)
177 ++dest;
178
179 do
180 {
181 *dest++ = *src;
182 } while (*src++);
183
184 return destination_start;
185}
186
187// Original pcdogs.exe: 0x44ab90
188// Fills the requested byte range with the low byte of the supplied value and returns the destination.
189void *_memset(void *dest, int32_t value, uint32_t count)
190{
191 unsigned char *destination_cursor = dest;
192
193 while (count--)
194 *destination_cursor++ = (unsigned char)value;
195
196 return dest;
197}
198
199// Original pcdogs.exe: 0x44abf0
200// Moves bytes safely between overlapping ranges by selecting the appropriate copy direction and returns the destination.
201void *CRT_MoveBytesOverlapSafeDuplicate(void *dest, void const *src, uint32_t count)
202{
203 unsigned char *destination_cursor = dest;
204 const unsigned char *source_cursor = src;
205
206 if ((uintptr_t)destination_cursor > (uintptr_t)source_cursor && (uintptr_t)destination_cursor - (uintptr_t)source_cursor < count)
207 {
208 destination_cursor += count;
209 source_cursor += count;
210
211 while (count--)
212 *--destination_cursor = *--source_cursor;
213 }
214 else
215 {
216 while (count--)
217 *destination_cursor++ = *source_cursor++;
218 }
219
220 return dest;
221}
222
223// Original pcdogs.exe: 0x44af30
224// Counts the bytes preceding a string's terminating zero.
225uint32_t _strlen(char const *str)
226{
227 const char *string_end = str;
228
229 while (*string_end)
230 ++string_end;
231
232 return (uint32_t)(string_end - str);
233}
234
235// Original pcdogs.exe: 0x44bc50
236// Copies at most the requested number of string bytes and pads the remaining destination with zeroes when the source ends early. A source that fills the entire count does not force a terminator.
237char *_strncpy(char *dest, char const *src, uint32_t count)
238{
239 char *destination_cursor = dest;
240
241 while (count && *src)
242 {
243 *destination_cursor++ = *src++;
244 --count;
245 }
246
247 while (count--)
248 *destination_cursor++ = 0;
249
250 return dest;
251}
252
253// Original pcdogs.exe: 0x44bf72
254// Returns the string length limited to the requested count. The original loop still reads the byte at the limit when no earlier terminator exists, including the first byte when the limit is zero.
255uint32_t CRT_strnlen(char const *str, uint32_t max_count)
256{
257 char const *cursor = str;
258 int32_t remaining_limit = max_count - 1;
259
260 if (max_count)
261 {
262 while (*(uint8_t *)cursor)
263 {
264 cursor = &cursor[1];
265
266 if (!remaining_limit--)
267 break;
268 }
269 }
270
271 if (*(uint8_t *)cursor)
272 return max_count;
273
274 return cursor - str;
275}
276
277// Original pcdogs.exe: 0x44cd10
278// Compares two strings case-insensitively through the CRT lowercase conversion until a mismatch or terminator is reached.
279int32_t __strcmpi(char const *s1, char const *s2)
280{
281 while (1)
282 {
283 unsigned left_character = (unsigned char)*s1++, right_character = (unsigned char)*s2++;
284
285 if (left_character != right_character)
286 {
288 {
289 left_character = (unsigned char)CRT_tolower(left_character);
290 right_character = (unsigned char)CRT_tolower(right_character);
291 }
292 else
293 {
294 if (left_character >= 'A' && left_character <= 'Z')
295 left_character += 32;
296
297 if (right_character >= 'A' && right_character <= 'Z')
298 right_character += 32;
299 }
300
301 if (left_character != right_character)
302 return left_character < right_character ? -1 : 1;
303 }
304
305 if (!right_character)
306 return 0;
307 }
308}
#define CRT_CTYPE_SPACE
Definition constants.h:59
#define CRT_CTYPE_DIGIT
Definition constants.h:58
int32_t CRT_tolower(int32_t ch)
Definition locale.c:241
int32_t CRT_isctype(int32_t ch, int32_t char_type_mask)
Definition locale.c:210
#define g_crtLocaleFlags
Definition globals.h:244
#define g_crtLcCodepage
Definition globals.h:112
int32_t String_ParseInt(char const *string)
Definition strings.c:14
char * CRT_strcat(char *dest, char const *src)
Definition strings.c:172
char * CRT_StrchrCdecl(char const *string, int32_t character)
Definition strings.c:109
int32_t String_ParseAtoi(char const *string)
Definition strings.c:78
char * String_StrchrCRT(int32_t character, char const *string)
Definition strings.c:119
char * _strncpy(char *dest, char const *src, uint32_t count)
Definition strings.c:237
void * CRT_MoveBytesOverlapSafeDuplicate(void *dest, void const *src, uint32_t count)
Definition strings.c:201
int32_t __strcmpi(char const *s1, char const *s2)
Definition strings.c:279
char * CRT_MoveBytesOverlapSafe(char *dest, char const *src, uint32_t count)
Definition strings.c:134
void * _memset(void *dest, int32_t value, uint32_t count)
Definition strings.c:189
uint32_t _strlen(char const *str)
Definition strings.c:225
char * CRT_strstr(char const *haystack, char const *needle)
Definition strings.c:85
char * CRT_strcpy(char *dest, char const *src)
Definition strings.c:158
uint32_t CRT_strnlen(char const *str, uint32_t max_count)
Definition strings.c:255
uint8_t classification_flags
Definition types.h:294
#define g_pCharTypeTable
Definition values.h:135