forked from libretro/RetroArch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtask_patch.c
844 lines (708 loc) · 22.9 KB
/
task_patch.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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
/* RetroArch - A frontend for libretro.
* Copyright (C) 2010-2014 - Hans-Kristian Arntzen
* Copyright (C) 2011-2016 - Daniel De Matteis
*
* RetroArch is free software: you can redistribute it and/or modify it under the terms
* of the GNU General Public License as published by the Free Software Found-
* ation, either version 3 of the License, or (at your option) any later version.
*
* RetroArch is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
* PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with RetroArch.
* If not, see <http://www.gnu.org/licenses/>.
*/
/* BPS/UPS/IPS implementation from bSNES (nall::).
* Modified for RetroArch. */
/* TODO/FIXME - turn this into actual task */
#include <stdint.h>
#include <string.h>
#include <boolean.h>
#include <compat/msvc.h>
#include <file/file_path.h>
#include <streams/file_stream.h>
#include <string/stdstring.h>
#include <encodings/crc32.h>
#include "../retroarch.h"
#include "../msg_hash.h"
#include "../verbosity.h"
#include "../configuration.h"
enum bps_mode
{
SOURCE_READ = 0,
TARGET_READ,
SOURCE_COPY,
TARGET_COPY
};
enum patch_error
{
PATCH_UNKNOWN = 0,
PATCH_SUCCESS,
PATCH_PATCH_TOO_SMALL,
PATCH_PATCH_INVALID_HEADER,
PATCH_PATCH_INVALID,
PATCH_SOURCE_TOO_SMALL,
PATCH_TARGET_ALLOC_FAILED,
PATCH_SOURCE_INVALID,
PATCH_TARGET_INVALID,
PATCH_SOURCE_CHECKSUM_INVALID,
PATCH_TARGET_CHECKSUM_INVALID,
PATCH_PATCH_CHECKSUM_INVALID
};
struct bps_data
{
const uint8_t *modify_data;
const uint8_t *source_data;
uint8_t *target_data;
size_t modify_length;
size_t source_length;
size_t target_length;
size_t modify_offset;
size_t source_offset;
size_t target_offset;
size_t source_relative_offset;
size_t target_relative_offset;
size_t output_offset;
uint32_t modify_checksum;
uint32_t source_checksum;
uint32_t target_checksum;
};
struct ups_data
{
const uint8_t *patch_data;
const uint8_t *source_data;
uint8_t *target_data;
unsigned patch_length;
unsigned source_length;
unsigned target_length;
unsigned patch_offset;
unsigned source_offset;
unsigned target_offset;
unsigned patch_checksum;
unsigned source_checksum;
unsigned target_checksum;
};
typedef enum patch_error (*patch_func_t)(const uint8_t*, uint64_t,
const uint8_t*, uint64_t, uint8_t**, uint64_t*);
static uint8_t bps_read(struct bps_data *bps)
{
uint8_t data = bps->modify_data[bps->modify_offset++];
bps->modify_checksum = ~(encoding_crc32(
~bps->modify_checksum, &data, 1));
return data;
}
static uint64_t bps_decode(struct bps_data *bps)
{
uint64_t data = 0, shift = 1;
for (;;)
{
uint8_t x = bps_read(bps);
data += (x & 0x7f) * shift;
if (x & 0x80)
break;
shift <<= 7;
data += shift;
}
return data;
}
static void bps_write(struct bps_data *bps, uint8_t data)
{
bps->target_data[bps->output_offset++] = data;
bps->target_checksum = ~(encoding_crc32(~bps->target_checksum, &data, 1));
}
static enum patch_error bps_apply_patch(
const uint8_t *modify_data, uint64_t modify_length,
const uint8_t *source_data, uint64_t source_length,
uint8_t **target_data, uint64_t *target_length)
{
size_t i;
struct bps_data bps;
uint32_t checksum = 0;
size_t modify_source_size = 0;
size_t modify_target_size = 0;
size_t modify_markup_size = 0;
uint32_t modify_source_checksum = 0;
uint32_t modify_target_checksum = 0;
uint32_t modify_modify_checksum = 0;
if (modify_length < 19)
return PATCH_PATCH_TOO_SMALL;
bps.modify_data = modify_data;
bps.source_data = source_data;
bps.target_data = *target_data;
bps.modify_length = modify_length;
bps.source_length = source_length;
bps.target_length = *target_length;
bps.modify_offset = 0;
bps.source_offset = 0;
bps.target_offset = 0;
bps.modify_checksum = ~0;
bps.source_checksum = 0;
bps.target_checksum = ~0;
bps.source_relative_offset = 0;
bps.target_relative_offset = 0;
bps.output_offset = 0;
if ( (bps_read(&bps) != 'B') ||
(bps_read(&bps) != 'P') ||
(bps_read(&bps) != 'S') ||
(bps_read(&bps) != '1'))
return PATCH_PATCH_INVALID_HEADER;
modify_source_size = bps_decode(&bps);
modify_target_size = bps_decode(&bps);
modify_markup_size = bps_decode(&bps);
for (i = 0; i < modify_markup_size; i++)
bps_read(&bps);
if (modify_source_size > bps.source_length)
return PATCH_SOURCE_TOO_SMALL;
if (modify_target_size > bps.target_length)
{
uint8_t *prov=(uint8_t*)malloc((size_t)modify_target_size);
if (!prov)
return PATCH_TARGET_ALLOC_FAILED;
free(*target_data);
bps.target_data = prov;
*target_data = prov;
bps.target_length = modify_target_size;
}
while (bps.modify_offset < bps.modify_length - 12)
{
size_t length = bps_decode(&bps);
unsigned mode = length & 3;
length = (length >> 2) + 1;
switch (mode)
{
case SOURCE_READ:
while (length--)
bps_write(&bps, bps.source_data[bps.output_offset]);
break;
case TARGET_READ:
while (length--)
bps_write(&bps, bps_read(&bps));
break;
case SOURCE_COPY:
case TARGET_COPY:
{
int offset = (int)bps_decode(&bps);
bool negative = offset & 1;
offset >>= 1;
if (negative)
offset = -offset;
if (mode == SOURCE_COPY)
{
bps.source_offset += offset;
while (length--)
bps_write(&bps, bps.source_data[bps.source_offset++]);
}
else
{
bps.target_offset += offset;
while (length--)
bps_write(&bps, bps.target_data[bps.target_offset++]);
break;
}
break;
}
}
}
for (i = 0; i < 32; i += 8)
modify_source_checksum |= bps_read(&bps) << i;
for (i = 0; i < 32; i += 8)
modify_target_checksum |= bps_read(&bps) << i;
checksum = ~bps.modify_checksum;
for (i = 0; i < 32; i += 8)
modify_modify_checksum |= bps_read(&bps) << i;
bps.source_checksum = encoding_crc32(0,
bps.source_data, bps.source_length);
bps.target_checksum = ~bps.target_checksum;
if (bps.source_checksum != modify_source_checksum)
return PATCH_SOURCE_CHECKSUM_INVALID;
if (bps.target_checksum != modify_target_checksum)
return PATCH_TARGET_CHECKSUM_INVALID;
if (checksum != modify_modify_checksum)
return PATCH_PATCH_CHECKSUM_INVALID;
*target_length = modify_target_size;
return PATCH_SUCCESS;
}
static uint8_t ups_patch_read(struct ups_data *data)
{
if (data && data->patch_offset < data->patch_length)
{
uint8_t n = data->patch_data[data->patch_offset++];
data->patch_checksum =
~(encoding_crc32(~data->patch_checksum, &n, 1));
return n;
}
return 0x00;
}
static uint8_t ups_source_read(struct ups_data *data)
{
if (data && data->source_offset < data->source_length)
{
uint8_t n = data->source_data[data->source_offset++];
data->source_checksum =
~(encoding_crc32(~data->source_checksum, &n, 1));
return n;
}
return 0x00;
}
static void ups_target_write(struct ups_data *data, uint8_t n)
{
if (data && data->target_offset < data->target_length)
{
data->target_data[data->target_offset] = n;
data->target_checksum =
~(encoding_crc32(~data->target_checksum, &n, 1));
}
if (data)
data->target_offset++;
}
static uint64_t ups_decode(struct ups_data *data)
{
uint64_t offset = 0, shift = 1;
for (;;)
{
uint8_t x = ups_patch_read(data);
offset += (x & 0x7f) * shift;
if (x & 0x80)
break;
shift <<= 7;
offset += shift;
}
return offset;
}
static enum patch_error ups_apply_patch(
const uint8_t *patchdata, uint64_t patchlength,
const uint8_t *sourcedata, uint64_t sourcelength,
uint8_t **targetdata, uint64_t *targetlength)
{
size_t i;
struct ups_data data;
unsigned source_read_length;
unsigned target_read_length;
uint32_t patch_result_checksum = 0;
uint32_t patch_read_checksum = 0;
uint32_t source_read_checksum = 0;
uint32_t target_read_checksum = 0;
data.patch_data = patchdata;
data.source_data = sourcedata;
data.target_data = *targetdata;
data.patch_length = (unsigned)patchlength;
data.source_length = (unsigned)sourcelength;
data.target_length = (unsigned)*targetlength;
data.patch_offset = 0;
data.source_offset = 0;
data.target_offset = 0;
data.patch_checksum = ~0;
data.source_checksum = ~0;
data.target_checksum = ~0;
if (data.patch_length < 18)
return PATCH_PATCH_INVALID;
if (
(ups_patch_read(&data) != 'U') ||
(ups_patch_read(&data) != 'P') ||
(ups_patch_read(&data) != 'S') ||
(ups_patch_read(&data) != '1')
)
return PATCH_PATCH_INVALID;
source_read_length = (unsigned)ups_decode(&data);
target_read_length = (unsigned)ups_decode(&data);
if ( (data.source_length != source_read_length)
&& (data.source_length != target_read_length))
return PATCH_SOURCE_INVALID;
*targetlength = (data.source_length == source_read_length ?
target_read_length : source_read_length);
if (data.target_length < *targetlength)
{
uint8_t *prov=(uint8_t*)malloc((size_t)*targetlength);
if (!prov)
return PATCH_TARGET_ALLOC_FAILED;
free(*targetdata);
*targetdata = prov;
data.target_data = prov;
}
data.target_length = (unsigned)*targetlength;
while (data.patch_offset < data.patch_length - 12)
{
unsigned length = (unsigned)ups_decode(&data);
while (length--)
ups_target_write(&data, ups_source_read(&data));
for (;;)
{
uint8_t patch_xor = ups_patch_read(&data);
ups_target_write(&data, patch_xor ^ ups_source_read(&data));
if (patch_xor == 0)
break;
}
}
while (data.source_offset < data.source_length)
ups_target_write(&data, ups_source_read(&data));
while (data.target_offset < data.target_length)
ups_target_write(&data, ups_source_read(&data));
for (i = 0; i < 4; i++)
source_read_checksum |= ups_patch_read(&data) << (i * 8);
for (i = 0; i < 4; i++)
target_read_checksum |= ups_patch_read(&data) << (i * 8);
patch_result_checksum = ~data.patch_checksum;
data.source_checksum = ~data.source_checksum;
data.target_checksum = ~data.target_checksum;
for (i = 0; i < 4; i++)
patch_read_checksum |= ups_patch_read(&data) << (i * 8);
if (patch_result_checksum != patch_read_checksum)
return PATCH_PATCH_INVALID;
if ( data.source_checksum == source_read_checksum
&& data.source_length == source_read_length)
{
if ( data.target_checksum == target_read_checksum
&& data.target_length == target_read_length)
return PATCH_SUCCESS;
return PATCH_TARGET_INVALID;
}
else if (data.source_checksum == target_read_checksum
&& data.source_length == target_read_length)
{
if ( data.target_checksum == source_read_checksum
&& data.target_length == source_read_length)
return PATCH_SUCCESS;
return PATCH_TARGET_INVALID;
}
return PATCH_SOURCE_INVALID;
}
static enum patch_error ips_alloc_targetdata(
const uint8_t *patchdata, uint64_t patchlen,
uint64_t sourcelength,
uint8_t **targetdata, uint64_t *targetlength)
{
uint8_t *prov_alloc;
uint32_t offset = 5;
*targetlength = sourcelength;
for (;;)
{
uint32_t address;
unsigned length;
if (offset > patchlen - 3)
break;
address = patchdata[offset++] << 16;
address |= patchdata[offset++] << 8;
address |= patchdata[offset++] << 0;
if (address == 0x454f46) /* EOF */
{
if (offset == patchlen)
{
prov_alloc = (uint8_t*)malloc((size_t)*targetlength);
if (!prov_alloc)
return PATCH_TARGET_ALLOC_FAILED;
free(*targetdata);
*targetdata = prov_alloc;
return PATCH_SUCCESS;
}
else if (offset == patchlen - 3)
{
uint32_t size = patchdata[offset++] << 16;
size |= patchdata[offset++] << 8;
size |= patchdata[offset++] << 0;
*targetlength = size;
prov_alloc = (uint8_t*)malloc((size_t)*targetlength);
if (!prov_alloc)
return PATCH_TARGET_ALLOC_FAILED;
free(*targetdata);
*targetdata = prov_alloc;
return PATCH_SUCCESS;
}
}
if (offset > patchlen - 2)
break;
length = patchdata[offset++] << 8;
length |= patchdata[offset++] << 0;
if (length) /* Copy */
{
if (offset > patchlen - length)
break;
while (length--)
{
address++;
offset++;
}
}
else /* RLE */
{
if (offset > patchlen - 3)
break;
length = patchdata[offset++] << 8;
length |= patchdata[offset++] << 0;
if (length == 0) /* Illegal */
break;
while (length--)
address++;
offset++;
}
if (address > *targetlength)
*targetlength = address;
}
return PATCH_PATCH_INVALID;
}
static enum patch_error ips_apply_patch(
const uint8_t *patchdata, uint64_t patchlen,
const uint8_t *sourcedata, uint64_t sourcelength,
uint8_t **targetdata, uint64_t *targetlength)
{
uint32_t offset = 5;
enum patch_error error_patch = PATCH_UNKNOWN;
if ( patchlen < 8 ||
patchdata[0] != 'P' ||
patchdata[1] != 'A' ||
patchdata[2] != 'T' ||
patchdata[3] != 'C' ||
patchdata[4] != 'H')
return PATCH_PATCH_INVALID;
if ((error_patch = ips_alloc_targetdata(
patchdata, patchlen, sourcelength,
targetdata, targetlength)) != PATCH_SUCCESS)
return error_patch;
memcpy(*targetdata, sourcedata, (size_t)sourcelength);
for (;;)
{
uint32_t address;
unsigned length;
if (offset > patchlen - 3)
break;
address = patchdata[offset++] << 16;
address |= patchdata[offset++] << 8;
address |= patchdata[offset++] << 0;
if (address == 0x454f46) /* EOF */
{
if (offset == patchlen)
return PATCH_SUCCESS;
if (offset == patchlen - 3)
{
#if 0
uint32_t size = patchdata[offset++] << 16;
size |= patchdata[offset++] << 8;
size |= patchdata[offset++] << 0;
#endif
return PATCH_SUCCESS;
}
}
if (offset > patchlen - 2)
break;
length = patchdata[offset++] << 8;
length |= patchdata[offset++] << 0;
if (length) /* Copy */
{
if (offset > patchlen - length)
break;
while (length--)
(*targetdata)[address++] = patchdata[offset++];
}
else /* RLE */
{
if (offset > patchlen - 3)
break;
length = patchdata[offset++] << 8;
length |= patchdata[offset++] << 0;
if (length == 0) /* Illegal */
break;
while (length--)
(*targetdata)[address++] = patchdata[offset];
offset++;
}
}
return PATCH_PATCH_INVALID;
}
static bool apply_patch_content(uint8_t **buf,
ssize_t *size, const char *patch_desc, const char *patch_path,
patch_func_t func, void *patch_data, int64_t patch_size)
{
settings_t *settings = config_get_ptr();
bool show_notification = settings ?
settings->bools.notification_show_patch_applied : false;
enum patch_error err = PATCH_UNKNOWN;
ssize_t ret_size = *size;
uint8_t *ret_buf = *buf;
uint64_t target_size = 0;
uint8_t *patched_content = NULL;
RARCH_LOG("Found %s file in \"%s\", attempting to patch ...\n",
patch_desc, patch_path);
if ((err = func((const uint8_t*)patch_data, patch_size, ret_buf,
ret_size, &patched_content, &target_size)) == PATCH_SUCCESS)
{
free(ret_buf);
*buf = patched_content;
*size = target_size;
/* Show an OSD message */
if (show_notification)
{
const char *patch_filename = path_basename(patch_path);
char msg[256];
msg[0] = '\0';
snprintf(msg, sizeof(msg), msg_hash_to_str(MSG_APPLYING_PATCH),
patch_filename ? patch_filename :
msg_hash_to_str(MENU_ENUM_LABEL_VALUE_UNKNOWN));
runloop_msg_queue_push(msg, 1, 180, false, NULL,
MESSAGE_QUEUE_ICON_DEFAULT, MESSAGE_QUEUE_CATEGORY_INFO);
}
}
else
RARCH_ERR("%s %s: %s #%u\n",
msg_hash_to_str(MSG_FAILED_TO_PATCH),
patch_desc,
msg_hash_to_str(MSG_ERROR),
(unsigned)err);
return true;
}
static bool try_bps_patch(bool allow_bps, const char *name_bps,
uint8_t **buf, ssize_t *size)
{
if ( allow_bps
&& !string_is_empty(name_bps)
&& path_is_valid(name_bps)
)
{
int64_t patch_size;
bool ret = false;
void *patch_data = NULL;
if (!filestream_read_file(name_bps, &patch_data, &patch_size))
return false;
if (patch_size >= 0)
ret = apply_patch_content(buf, size, "BPS", name_bps,
bps_apply_patch, patch_data, patch_size);
if (patch_data)
free(patch_data);
return ret;
}
return false;
}
static bool try_ups_patch(bool allow_ups, const char *name_ups,
uint8_t **buf, ssize_t *size)
{
if ( allow_ups
&& !string_is_empty(name_ups)
&& path_is_valid(name_ups)
)
{
int64_t patch_size;
bool ret = false;
void *patch_data = NULL;
if (!filestream_read_file(name_ups, &patch_data, &patch_size))
return false;
if (patch_size >= 0)
ret = apply_patch_content(buf, size, "UPS", name_ups,
ups_apply_patch, patch_data, patch_size);
if (patch_data)
free(patch_data);
return ret;
}
return false;
}
static bool try_ips_patch(bool allow_ips,
const char *name_ips, uint8_t **buf, ssize_t *size)
{
if ( allow_ips
&& !string_is_empty(name_ips)
&& path_is_valid(name_ips)
)
{
int64_t patch_size;
bool ret = false;
void *patch_data = NULL;
if (!filestream_read_file(name_ips, &patch_data, &patch_size))
return false;
if (patch_size >= 0)
ret = apply_patch_content(buf, size, "IPS", name_ips,
ips_apply_patch, patch_data, patch_size);
if (patch_data)
free(patch_data);
return ret;
}
return false;
}
/**
* patch_content:
* @buf : buffer of the content file.
* @size : size of the content file.
*
* Apply patch to the content file in-memory.
*
**/
bool patch_content(
bool is_ips_pref,
bool is_bps_pref,
bool is_ups_pref,
const char *name_ips,
const char *name_bps,
const char *name_ups,
uint8_t **buf,
void *data)
{
ssize_t *size = (ssize_t*)data;
bool allow_ups = !is_bps_pref && !is_ips_pref;
bool allow_ips = !is_ups_pref && !is_bps_pref;
bool allow_bps = !is_ups_pref && !is_ips_pref;
bool patch_found = false;
if ( (unsigned)is_ips_pref
+ (unsigned)is_bps_pref
+ (unsigned)is_ups_pref > 1)
{
RARCH_WARN("%s\n",
msg_hash_to_str(MSG_SEVERAL_PATCHES_ARE_EXPLICITLY_DEFINED));
return false;
}
/* Attempt to apply first (non-indexed) patch */
if ( try_ips_patch(allow_ips, name_ips, buf, size)
|| try_bps_patch(allow_bps, name_bps, buf, size)
|| try_ups_patch(allow_ups, name_ups, buf, size))
{
/* A patch has been found. Now attempt to apply
* any additional 'indexed' patch files */
size_t name_ips_len = strlen(name_ips);
size_t name_bps_len = strlen(name_bps);
size_t name_ups_len = strlen(name_ups);
char *name_ips_indexed = (char*)malloc((name_ips_len + 2) * sizeof(char));
char *name_bps_indexed = (char*)malloc((name_bps_len + 2) * sizeof(char));
char *name_ups_indexed = (char*)malloc((name_ups_len + 2) * sizeof(char));
/* First patch already applied -> index
* for subsequent patches starts at 1 */
size_t patch_index = 1;
name_ips_indexed[0] = '\0';
name_bps_indexed[0] = '\0';
name_ups_indexed[0] = '\0';
strlcpy(name_ips_indexed, name_ips, (name_ips_len + 1) * sizeof(char));
strlcpy(name_bps_indexed, name_bps, (name_bps_len + 1) * sizeof(char));
strlcpy(name_ups_indexed, name_ups, (name_ups_len + 1) * sizeof(char));
/* Ensure that we NUL terminate *after* the
* index character */
name_ips_indexed[name_ips_len + 1] = '\0';
name_bps_indexed[name_bps_len + 1] = '\0';
name_ups_indexed[name_ups_len + 1] = '\0';
/* try to patch "*.ipsX" */
while (patch_index < 10)
{
/* Add index character to end of patch
* file path string
* > Note: This technique only works for
* index values up to 9 (i.e. single
* digit numbers)
* > If we want to support more than 10
* patches in total, will have to replace
* this with an snprintf() implementation
* (which will have significantly higher
* performance overheads) */
char index_char = '0' + patch_index;
name_ips_indexed[name_ips_len] = index_char;
name_bps_indexed[name_bps_len] = index_char;
name_ups_indexed[name_ups_len] = index_char;
if ( !try_ips_patch(allow_ips, name_ips_indexed, buf, size)
&& !try_bps_patch(allow_bps, name_bps_indexed, buf, size)
&& !try_ups_patch(allow_ups, name_ups_indexed, buf, size))
break;
patch_index++;
}
free(name_ips_indexed);
free(name_bps_indexed);
free(name_ups_indexed);
patch_found = true;
}
if(!patch_found)
RARCH_LOG("%s\n",
msg_hash_to_str(MSG_DID_NOT_FIND_A_VALID_CONTENT_PATCH));
return patch_found;
}