Repository navigation
Expand file tree
/
Copy pathmakebips.c
More file actions
176 lines (155 loc) · 5.78 KB
/
Copy pathmakebips.c
File metadata and controls
176 lines (155 loc) · 5.78 KB
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
/*
* makeBips.c
*
* (c)2025 bferguson3
* @retrodevdiscord
*
* IPS file with support for up to 4GB files.
* Format is identical to IPS, but offset is 4b instead of 3b
* and size is 4b instead of 2b.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define u8 unsigned char
typedef struct _patch {
u8 offset[4];
u8 size[4];
u8* bytes;
} patch;
int main(int argc, char *argv[]) {
if (argc < 3) {
printf("Usage:\n makebips <base-file> <compared-file>\n");
return 1; // Not enough arguments
}
FILE *f;
f = fopen(argv[1], "rb");
if (!f) return 1; // Error opening file
fseek(f, 0, SEEK_END);
long base_size = ftell(f);
fseek(f, 0, SEEK_SET);
unsigned char *base = (unsigned char *)malloc(base_size);
fread(base, 1, base_size, f);
fclose(f);
f = fopen(argv[2], "rb");
if (!f) {
free(base);
return 1; // Error opening file
}
fseek(f, 0, SEEK_END);
long cmp_size = ftell(f);
fseek(f, 0, SEEK_SET);
unsigned char *cmp = (unsigned char *)malloc(cmp_size);
fread(cmp, 1, cmp_size, f);
fclose(f);
long changes_capacity = 100;
long changes_size = 0;
patch* changes = (patch *)malloc(changes_capacity * sizeof(patch));
int i = 0;
if (base_size <= cmp_size) { // file 2 is bigger
while (i < base_size) {
if (base[i] != cmp[i]) {
long ofs = i;
long o_size = 0;
while ((base[i] != cmp[i]) && (i < cmp_size) && (o_size < 0xffffffff)) {
o_size++;
i++;
}
if (changes_size >= changes_capacity) {
changes_capacity += 100;
changes = (patch*)realloc(changes, changes_capacity * sizeof(patch));
}
changes[changes_size].offset[0] = (ofs & 0xff000000) >> 24;
changes[changes_size].offset[1] = (ofs & 0xff0000) >> 16;
changes[changes_size].offset[2] = (ofs & 0xff00) >> 8;
changes[changes_size].offset[3] = (ofs & 0xff);
changes[changes_size].size[0] = (o_size & 0xff000000) >> 24;
changes[changes_size].size[1] = (o_size & 0xff0000) >> 16;
changes[changes_size].size[2] = (o_size & 0xff00) >> 8;
changes[changes_size].size[3] = (o_size & 0xff);
changes[changes_size].bytes = (u8*)malloc(o_size * sizeof(u8));
for(long b = 0; b < o_size; b++){
changes[changes_size].bytes[b] = cmp[i - o_size + b]; // hmm
}
changes_size++;
}
i++;
}
if(base_size < cmp_size){
int add_size = 0;
int ro = i;
while (i < cmp_size) {
add_size++;
i++;
}
if (changes_size >= changes_capacity) {
changes_capacity *= 2;
changes = (patch*)realloc(changes, changes_capacity * sizeof(patch));
}
changes[changes_size].offset[0] = (ro & 0xff000000) >> 24;
changes[changes_size].offset[1] = (ro & 0xff0000) >> 16;
changes[changes_size].offset[2] = (ro & 0xff00) >> 8;
changes[changes_size].offset[3] = (ro & 0xff);
changes[changes_size].size[0] = (add_size & 0xff000000) >> 24;
changes[changes_size].size[1] = (add_size & 0xff0000) >> 16;
changes[changes_size].size[2] = (add_size & 0xff00) >> 8;
changes[changes_size].size[3] = (add_size & 0xff);
changes[changes_size].bytes = (u8*)malloc(add_size * sizeof(u8));
for(int b = 0; b < add_size; b++){
changes[changes_size].bytes[b] = cmp[i - add_size + b]; // hmm
}
changes_size++;
}
}
else { // file 1 is bigger
printf("File size mismatch: Ensure file #2 is the larger.");
return 2;
}
// length will be 8 + (len of changes * 5) + len of each byte arr
changes = (patch *)realloc(changes, changes_size * sizeof(patch));
long tot = 8 + (5 * changes_size);
//printf("%d, %d\n", changes_size, tot);
for(long p = 0; p < changes_size; p++) {
long len = (changes[p].size[0] << 24) |(changes[p].size[1] << 16) |(changes[p].size[2] << 8) | (changes[p].size[3]);
tot += len;
}
unsigned char * ips = (u8*)malloc(sizeof(u8)*tot);
long ips_size = 0;
printf("Creating %lu patches...\n", changes_size);
ips[ips_size++] = 'P';
ips[ips_size++] = 'A';
ips[ips_size++] = 'T';
ips[ips_size++] = 'C';
ips[ips_size++] = 'H';
for (i = 0; i < changes_size; i++) {
ips[ips_size++] = (u8)(changes[i].offset[0]);
ips[ips_size++] = (u8)(changes[i].offset[1]);
ips[ips_size++] = (u8)(changes[i].offset[2]);
ips[ips_size++] = (u8)(changes[i].offset[3]);
long len = (changes[i].size[0] << 24) |(changes[i].size[1] << 16) |(changes[i].size[2] << 8) | (changes[i].size[3]);
ips[ips_size++] = (u8)(changes[i].size[0]);
ips[ips_size++] = (u8)(changes[i].size[1]);
ips[ips_size++] = (u8)(changes[i].size[2]);
ips[ips_size++] = (u8)(changes[i].size[3]);
for(int j = 0; j < len; j++) {
ips[ips_size++] = (u8)changes[i].bytes[j];
}
}
ips[ips_size++] = 'E';
ips[ips_size++] = 'O';
ips[ips_size++] = 'F';
f = fopen("out.bips", "wb");
if(fwrite(ips, 1, ips_size, f) < ips_size){
printf("out.bips writing failed!\nMust be a bug!!\n");
}
else
printf("out.bips BIPS file written OK.\n");
fclose(f);
free(base);
free(cmp);
for(int i = 0; i < changes_size; i++){
free(changes[i].bytes);
}
free(changes);
return 0;
}