Branch data Line data Source code
1 : : #include "asar.h"
2 : : #include "virtualfile.h"
3 : : #include "unicode.h"
4 : :
5 : : #include "platform/file-helpers.h"
6 : :
7 : : #define typed_malloc(type, count) (type*)malloc(sizeof(type)*(count))
8 : : #define typed_realloc(type, ptr, count) (type*)realloc(ptr, sizeof(type)*(count))
9 : :
10 : :
11 : : // Detects if str starts with a UTF-8 byte order mark.
12 : : // If so, throws a warning, then returns the number of bytes we should skip ahead in the string.
13 : 363 : size_t check_bom(const char* str)
14 : : {
15 : : // RPG Hacker: We could also check for BoMs of incompatible encodings here (like UTF-16)
16 : : // and throw errors, but not sure if that's worth adding. Asar never supported any wide
17 : : // encodings to begin with, so it's unreasonable to assume that any UTF-16 patches currently
18 : : // exist for it. As for future patches, those should be caught by the "must be UTF-8" checks
19 : : // I have already implemented further below.
20 : : // I think UTF-8 + BoM is the only case that could lead to confusion if we didn't handle it,
21 : : // so that's why I have added this.
22 [ + + + - : 363 : if (str[0u] == '\xEF' && str[1u] == '\xBB' && str[2u] == '\xBF')
+ - ]
23 : : {
24 : 1 : asar_throw_warning(0, warning_id_byte_order_mark_utf8);
25 : 1 : return 3u;
26 : : }
27 : :
28 : : return 0u;
29 : : }
30 : :
31 : :
32 : 137 : char * readfile(const char * fname, const char * basepath)
33 : : {
34 : 137 : virtual_file_handle myfile = filesystem->open_file(fname, basepath);
35 [ + + ]: 137 : if (myfile == INVALID_VIRTUAL_FILE_HANDLE) return nullptr;
36 : 134 : size_t datalen = filesystem->get_file_size(myfile);
37 : 134 : char * data= typed_malloc(char, datalen+1);
38 : 134 : data[filesystem->read_file(myfile, data, 0u, datalen)] = 0;
39 : 134 : filesystem->close_file(myfile);
40 : :
41 [ + + ]: 134 : if (!is_valid_utf8(data))
42 : : {
43 : 1 : free(data);
44 : 1 : asar_throw_error(0, error_type_block, error_id_invalid_utf8);
45 : : }
46 [ + + ]: 133 : if(check_bom(data)){
47 : 1 : data[0] = ' ';
48 : 1 : data[1] = ' ';
49 : 1 : data[2] = ' ';
50 : : }
51 : : return data;
52 : : }
53 : :
54 : : // RPG Hacker: like readfile(), but doesn't use virtual file system
55 : : // and instead read our file directly.
56 : 230 : char * readfilenative(const char * fname)
57 : : {
58 : 230 : FileHandleType myfile = open_file(fname, FileOpenMode_Read);
59 [ + - ]: 230 : if (myfile == InvalidFileHandle) return nullptr;
60 : 230 : size_t datalen = (size_t)get_file_size(myfile);
61 : 230 : char * data = typed_malloc(char, datalen + 1);
62 : 230 : data[read_file(myfile, data, datalen)] = 0;
63 : 230 : close_file(myfile);
64 : :
65 [ - + ]: 230 : if (!is_valid_utf8(data)) asar_throw_error(0, error_type_block, error_id_invalid_utf8);
66 [ - + ]: 230 : if(check_bom(data)){
67 : 0 : data[0] = ' ';
68 : 0 : data[1] = ' ';
69 : 0 : data[2] = ' ';
70 : : }
71 : : return data;
72 : : }
73 : :
74 : 120 : bool readfile(const char * fname, const char * basepath, char ** data, int * len)
75 : : {
76 : 120 : virtual_file_handle myfile = filesystem->open_file(fname, basepath);
77 [ + + ]: 120 : if (!myfile) return false;
78 : 114 : size_t datalen = filesystem->get_file_size(myfile);
79 : 114 : *data= typed_malloc(char, datalen);
80 : 114 : *len = (int)filesystem->read_file(myfile, *data, 0, datalen);
81 : 114 : filesystem->close_file(myfile);
82 : 114 : return true;
83 : : }
84 : :
85 : : #define isq(n) (((0x2227 ^ (0x0101 * (n))) - 0x0101UL) & ~(0x2227 ^ (0x0101 * (n))) & 0x8080UL)
86 : : #define isqp(n) (((0x22272829 ^ (0x01010101 * (n))) - 0x01010101UL) & ~(0x22272829 ^ (0x01010101 * (n))) & 0x80808080UL)
87 : :
88 : : // RPG Hacker: Only index this with ASCII characters.
89 : : // Anything else doesn't make sense, anyways.
90 : : const bool qparlut[128] = {
91 : : 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
92 : : 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
93 : : 0, 0, 1, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0,
94 : : 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
95 : : 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
96 : : 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
97 : : 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
98 : : 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
99 : : };
100 : :
101 : : //this will leave the last char found as the one pointed at
102 : 24446 : inline bool skip_quote(char *&str)
103 : : {
104 : :
105 [ + + ]: 24446 : if(*str == '"') str = strchr(str + 1, '"');
106 [ + + ]: 22668 : else if(*str == '\'')
107 : : {
108 : : int codepoint;
109 : 417 : str += utf8_val(&codepoint, str + 1) + 1;
110 [ - + ]: 417 : if(*str != '\'') return false;
111 : : }
112 : 24446 : return str;
113 : : }
114 : :
115 : : //eat 1 char or quote/par
116 : 5640 : inline bool skip_par(char *&str)
117 : : {
118 : : int par = 0;
119 [ + + + + : 5640 : if(*str != '\'' && *str != '"' && *str != '(' && *str != ')')
+ - ]
120 : : {
121 : 4527 : str++;
122 : 4527 : return true;
123 : : }
124 : : while(true)
125 : : {
126 : 6741 : char *t = str;
127 [ + + ]: 6741 : if(*str == '"') t = strchr(t + 1, '"');
128 [ - + ]: 6570 : else if(*str == '\'')
129 : : {
130 : : int codepoint;
131 : 0 : t += utf8_val(&codepoint, t + 1) + 1;
132 [ # # ]: 0 : if(*t != '\'') return false;
133 : : }
134 [ + + ]: 6570 : else if(*t == '(')
135 : : {
136 : 1020 : par++;
137 : : }
138 [ + + ]: 5550 : else if(*t == ')')
139 : : {
140 : 1020 : par--;
141 [ - + ]: 1020 : if(par < 0) return false;
142 : : }
143 : :
144 : 6741 : str = t + 1;
145 [ + + + + ]: 6741 : if(!*str || !par) return par == 0 ? true : false;
146 : : }
147 : : }
148 : :
149 : : //instr should not be duplicate chars. Instr should also not be 1 char
150 : 21560 : string& string::qreplace(const char * instr, const char * outstr)
151 : : {
152 : : string& thisstring =*this;
153 [ + + ]: 21560 : if (!strstr(thisstring, instr)) return thisstring;
154 : 15 : int inlen = strlen(instr);
155 : 15 : string out;
156 [ + + ]: 246 : for (int i=0;thisstring[i];)
157 : : {
158 [ + + ]: 231 : if (!strncmp((const char*)thisstring +i, instr, inlen))
159 : : {
160 : 15 : out+=outstr;
161 : 15 : i+=inlen;
162 : : }
163 : : // randomdude999: prevent appending the null terminator to the output
164 [ + - ]: 216 : else if(!isq(thisstring[i])) out+= thisstring[i++];
165 : : else
166 : : {
167 : : char *start = raw() + i;
168 : 0 : char *end = start;
169 [ # # # # ]: 0 : if(!skip_quote(end)) return thisstring;
170 : 0 : out.append(raw(), i, end - start + i + 1);
171 : : i += end - start + 1;
172 : :
173 : : }
174 : : }
175 : : thisstring =out;
176 : 15 : return thisstring;
177 : 15 : }
178 : :
179 : 23386 : string& string::qnormalize()
180 : : {
181 : : string& thisstring =*this;
182 : 23386 : string out;
183 : : char *startstr = thisstring.raw();
184 : : char *str = startstr;
185 [ + + ]: 48280 : while(str = strpbrk(str, "'\" \t,\r"))
186 : : {
187 [ + + ]: 24912 : if(is_space(*str))
188 : : {
189 [ + + + + ]: 19786 : if(str[0] == ' ' && !is_space(str[1]))
190 : : {
191 : 19756 : str++;
192 : 19756 : continue;
193 : : }
194 : 30 : out.append(startstr, 0, str - startstr);
195 : 30 : out += ' ';
196 [ + + ]: 126 : while(is_space(*str)) str++;
197 : : startstr = str;
198 [ + + ]: 5126 : }else if(*str == ',')
199 : : {
200 : 3002 : str++;
201 [ + + ]: 3002 : if(is_space(*str))
202 : : {
203 : 858 : out.append(startstr, 0, str - startstr);
204 [ + + ]: 1731 : while(is_space(*str)) str++;
205 : : startstr = str;
206 : : }
207 : : }
208 : : else
209 : : {
210 : 2124 : str = strchr(str + 1, *str); //confirm quotes has already been run, so this should be okay
211 [ + + ]: 2124 : if(!str) return thisstring;
212 : 2106 : str++;
213 : : }
214 : : }
215 [ + + ]: 23368 : if(startstr != thisstring.raw())
216 : : {
217 : 699 : out.append(startstr, 0, strlen(startstr)); //the remaining
218 : :
219 : : thisstring = out;
220 : : }
221 : : return thisstring;
222 : 23386 : }
223 : :
224 : 10537 : bool confirmquotes(const char * str)
225 : : {
226 [ + + ]: 11043 : while(*str)
227 : : {
228 : : char *dquote = strchr((char *)str, '"');
229 : : char *squote = strchr((char *)str, '\'');
230 [ + + ]: 7528 : if(dquote || squote)
231 : : {
232 [ + + + + ]: 511 : if(dquote && (dquote < squote || !squote))
233 : : {
234 : 410 : dquote = strchr(dquote+1, '"');
235 : 406 : if(dquote) str = dquote+1;
236 : : else return false;
237 : : }
238 : : else
239 : : {
240 : : int codepoint;
241 : 101 : squote += utf8_val(&codepoint, squote + 1) + 1;
242 [ + + ]: 101 : if(*squote == '\'') str = squote+1;
243 : 1 : else return false;
244 : : }
245 : : }
246 : : else
247 : : {
248 : : return true;
249 : : }
250 : : }
251 : : return true;
252 : : }
253 : :
254 : 367 : bool confirmqpar(const char * str)
255 : : {
256 : : //todo fully optimize
257 : : int par = 0;
258 [ - + + + ]: 4903 : while((unsigned char)*str >= 128 || !qparlut[*str]) str++;
259 [ + + ]: 1266 : while(*str)
260 : : {
261 [ + + ]: 899 : if(*str == '"')
262 : : {
263 : 183 : str = strchr(str + 1, '"');
264 [ - + ]: 183 : if(!str++) return false;
265 : : }
266 [ + + ]: 716 : else if(*str == '\'')
267 : : {
268 : : int codepoint;
269 : 12 : str += utf8_val(&codepoint, str + 1) + 1;
270 [ + - ]: 12 : if(*str == '\'') str++;
271 : 0 : else return false;
272 : : }
273 : : else
274 : : {
275 : 704 : par += 1 - ((*str++ - '(') << 1);
276 [ - + ]: 704 : if(par < 0) return false;
277 : : }
278 [ - + + + ]: 2275 : while((unsigned char)*str >= 128 || !qparlut[*str]) str++;
279 : : }
280 : 367 : return !par;
281 : : }
282 : :
283 : 26127 : char ** split(char * str, char key, int * len)
284 : : {
285 : 26127 : char *thisentry=strchr(str, key);
286 [ + + ]: 26127 : if (!thisentry)
287 : : {
288 : 14062 : char ** out= typed_malloc(char*, 2);
289 : 14062 : out[0]=str;
290 : 14062 : out[1]=nullptr;
291 [ + + ]: 14062 : if (len) *len=1;
292 : 14062 : return out;
293 : : }
294 : : int count=15; //makes the default alloc 8 elements, sounds fair.
295 : 12065 : char ** outdata= typed_malloc(char*, (size_t)count+1);
296 : :
297 : : int newcount=0;
298 : 12065 : outdata[newcount++]=str;
299 : : do{
300 : 20610 : *thisentry = 0;
301 : 20610 : thisentry++;
302 : 20610 : outdata[newcount++]=thisentry;
303 [ + + ]: 20610 : if(newcount >= count)
304 : : {
305 : 130 : count *= 2;
306 : 130 : outdata = typed_realloc(char *, outdata, count);
307 : : }
308 [ + + ]: 20610 : }while((thisentry = strchr(thisentry, key)));
309 : :
310 : 12065 : outdata[newcount]= nullptr;
311 [ + + ]: 12065 : if (len) *len=newcount;
312 : : return outdata;
313 : : }
314 : :
315 : 27035 : char ** qsplit(char * str, char key, int * len)
316 : : {
317 [ + + + + ]: 27035 : if (!strchr(str, '"') && !strchr(str, '\'')) return split(str, key, len);
318 : :
319 : : int count=15;
320 : 1808 : char ** outdata= typed_malloc(char*, (size_t)count+1);
321 : : int newcount=0;
322 : 1808 : char * thisentry=str;
323 : 1808 : outdata[newcount++]=thisentry;
324 [ + + ]: 18597 : while (*thisentry) /*todo fix*/
325 : : {
326 [ + + ]: 16789 : if (*thisentry == key)
327 : : {
328 : 1940 : *thisentry=0;
329 : 1940 : thisentry++;
330 : 1940 : outdata[newcount++]=thisentry;
331 [ - + ]: 1940 : if(newcount >= count)
332 : : {
333 : 0 : count *= 2;
334 : 0 : outdata = typed_realloc(char *, outdata, count);
335 : : }
336 : : }
337 [ + - ]: 14849 : else if(skip_quote(thisentry)) thisentry++;
338 : : else return nullptr;
339 : : }
340 : 1808 : outdata[newcount]= nullptr;
341 [ + + ]: 1808 : if (len) *len=newcount;
342 : : return outdata;
343 : : }
344 : :
345 : 21560 : char ** qsplitstr(char * str, const char * key, int * len)
346 : : {
347 : : //check if the str is found first
348 [ + + ]: 21560 : if (!strstr(str, key))
349 : : {
350 : 21368 : char ** out= typed_malloc(char*, 2);
351 : 21368 : out[0]=str;
352 : 21368 : out[1]=nullptr;
353 [ - + ]: 21368 : if (len) *len=1;
354 : 21368 : return out;
355 : : }
356 : :
357 : 192 : int keylen=(int)strlen(key);
358 : : int count=15;
359 : 192 : char ** outdata= typed_malloc(char*, (size_t)count+1);
360 : : int newcount=0;
361 : 192 : char * thisentry=str;
362 : 192 : outdata[newcount++]=thisentry;
363 [ + + ]: 10647 : while (*thisentry) /*todo fix*/
364 : : {
365 [ + + ]: 10455 : if (!strncmp(thisentry, key, (size_t)keylen))
366 : : {
367 : 858 : *thisentry=0;
368 : 858 : thisentry+=keylen;
369 : 858 : outdata[newcount++]=thisentry;
370 [ - + ]: 858 : if(newcount >= count)
371 : : {
372 : 0 : count *= 2;
373 : 0 : outdata = typed_realloc(char *, outdata, count);
374 : : }
375 : : }
376 [ + - ]: 9597 : else if(skip_quote(thisentry)) thisentry++;
377 : : else return nullptr;
378 : : }
379 : 192 : outdata[newcount]= nullptr;
380 [ - + ]: 192 : if (len) *len=newcount;
381 : : return outdata;
382 : : }
383 : :
384 : : //this function is most commonly called in cases where additional chars are very likely
385 : 5580 : char ** qpsplit(char * str, char key, int * len)
386 : : {
387 [ + + + - ]: 5580 : if (!strchr(str, '(') && !strchr(str, ')')) return qsplit(str, key, len);
388 : : int count=7;
389 : 963 : char ** outdata= typed_malloc(char*, (size_t)count+1);
390 : :
391 : : int newcount=0;
392 : 963 : char * thisentry=str;
393 : 963 : outdata[newcount++]=thisentry;
394 [ + + ]: 6810 : while (*thisentry)
395 : : {
396 : : //skippar(*thisentry, thisentry++, return nullptr;)
397 [ + + ]: 5847 : if (*thisentry == key)
398 : : {
399 : 447 : *thisentry=0;
400 : 447 : thisentry++;
401 : 447 : outdata[newcount++]=thisentry;
402 [ + + ]: 447 : if(newcount >= count)
403 : : {
404 : 6 : count *= 2;
405 : 6 : outdata = typed_realloc(char *, outdata, count);
406 : : }
407 : : }
408 [ + - ]: 5400 : else if(!skip_par(thisentry)) return nullptr;
409 : : }
410 : 963 : outdata[newcount]= nullptr;
411 [ + + ]: 963 : if (len) *len=newcount;
412 : : return outdata;
413 : : }
414 : :
415 : 420 : string &itrim(string &input, const char * left, const char * right)
416 : : {
417 : : bool nukeright=true;
418 : : int totallen=input.length();
419 : 420 : int rightlen=(int)strlen(right);
420 [ + - ]: 420 : if (rightlen && rightlen<=totallen)
421 : : {
422 : 420 : const char * rightend=right+rightlen;
423 : 420 : const char * strend=input.data()+totallen;
424 [ + + ]: 1410 : while (right!=rightend)
425 : : {
426 : 990 : rightend--;
427 : 990 : strend--;
428 [ - + ]: 990 : if (to_lower(*strend)!=to_lower(*rightend)) nukeright=false;
429 : : }
430 [ + - ]: 420 : if (nukeright)
431 : : {
432 : 420 : totallen-=rightlen;
433 : : input.truncate(totallen);
434 : : }
435 : : }
436 : : bool nukeleft=true;
437 : 420 : int leftlen = strlen(left);
438 [ + + - + ]: 420 : if(leftlen == 1 && input.data()[0] == left[0])
439 : : {
440 : 102 : return input = string(input.data()+1, (input.length()-1));
441 : : }
442 : : else
443 : : {
444 [ - + ]: 318 : for (int i = 0; i < leftlen; i++)
445 : : {
446 [ # # ]: 0 : if (to_lower(input.data()[i])!=to_lower(left[i])) nukeleft=false;
447 : : }
448 [ + - ]: 636 : if (nukeleft) input = string(input.data()+leftlen, (input.length()-leftlen));
449 : : }
450 : : return input;
451 : : }
452 : :
453 : 0 : char* strqpchr(char* str, char key)
454 : : {
455 [ # # ]: 0 : while (*str)
456 : : {
457 [ # # ]: 0 : if (*str == key) return str;
458 [ # # ]: 0 : else if(!skip_par(str)) return nullptr;
459 : : }
460 : : return nullptr;
461 : : }
462 : :
463 : 63 : char* strqpstr(char* str, const char* key)
464 : : {
465 : 63 : size_t keylen = strlen(key);
466 [ + + ]: 303 : while (*str)
467 : : {
468 [ + + ]: 300 : if (!strncmp(str, key, keylen)) return str;
469 [ - + ]: 240 : else if(!skip_par(str)) return nullptr;
470 : : }
471 : : return nullptr;
472 : : }
473 : :
474 : : extern const uint8_t char_props[256] = {
475 : : //x0 x1 x2 x3 x4 x5 x6 x7 x8 x9 xA xB xC xD xE xF
476 : : 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x80,0x80,0x00,0x00,0x80,0x00,0x00, // 0x
477 : : 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, // 1x
478 : : 0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, // 2x !"#$%&'()*+,-./
479 : : 0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x00,0x00,0x00,0x00,0x00,0x00, // 3x 0123456789:;<=>?
480 : : 0x00,0x23,0x23,0x23,0x23,0x23,0x23,0x22,0x22,0x22,0x22,0x22,0x22,0x22,0x22,0x22, // 4x @ABCDEFGHIJKLMNO
481 : : 0x22,0x22,0x22,0x22,0x22,0x22,0x22,0x22,0x22,0x22,0x22,0x00,0x00,0x00,0x00,0x08, // 5x PQRSTUVWXYZ[\]^_
482 : : 0x00,0x25,0x25,0x25,0x25,0x25,0x25,0x24,0x24,0x24,0x24,0x24,0x24,0x24,0x24,0x24, // 6x `abcdefghijklmno
483 : : 0x24,0x24,0x24,0x24,0x24,0x24,0x24,0x24,0x24,0x24,0x24,0x00,0x00,0x00,0x00,0x00, // 7x pqrstuvwxyz{|}~
484 : : 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, // 8x
485 : : 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, // 9x
486 : : 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, // Ax
487 : : 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, // Bx
488 : : 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, // Cx
489 : : 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, // Dx
490 : : 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, // Ex
491 : : 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, // Fx
492 : : };
|