Repository navigation
Expand file tree
/
Copy pathIPS.c
More file actions
executable file
·166 lines (138 loc) · 4.37 KB
/
Copy pathIPS.c
File metadata and controls
executable file
·166 lines (138 loc) · 4.37 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
/* IPS specific functions */
#include "AIPS.h"
#include "IPS.h"
/**
* Reads a record from the patch file.
*
* This code both detects, and reads the next record in the
* filePointer at the given location and writes the results into the
* patchData struct supplied.
*
* @param struct *patchData A pointer to a patchData struct that the
* information will be written to.
* @param FILE *filePointer a pointer to the patch file being read.
*
* @return int: 1 if successful, 0 otherwise.
*/
int IPSReadRecord(struct patchData *patch, FILE *filePointer) {
unsigned char offset[3], size[2];
if(fread(&offset, BYTE, 3, filePointer) &&
fread(&size, BYTE, 2, filePointer)) {
/* Fix linear reads */
patch->size = BYTE2_TO_UINT(size);
patch->offset = BYTE3_TO_UINT(offset);
if(patch->size == 0) {
return IPSReadRLE(patch, filePointer);
}
patch->data = (char*)malloc(patch->size + 1);
return fread(patch->data, BYTE, patch->size, filePointer);
}
return 0;
}
/**
* Read an RLE-Encoded patch...
*
* RLE patches are simple patches with a single byte repeating a
* specified quantity of times. The first 16 bits are the size of the
* patch, or the number of times to repeat the insert, and the value
* immediately succeeding this number is the patch character itself...
*
* @param struct patchData *patch A patch struct pointer holding the
* offset, the size, and the data...
* @param FILE *filePointer a file pointer to the current location in
* the patch file.
*
* @return Returns 1 on success, 0 on failure.
*/
int IPSReadRLE(struct patchData *patch, FILE *filePointer) {
char size[2];
if(fread(&size, BYTE, 2, filePointer)) {
unsigned int count;
char data = '\0';
patch->size = BYTE2_TO_UINT(size);
if(!fread(&data, BYTE, 1, filePointer)){
return 0;
}
patch->data = (char*)malloc((patch->size * sizeof(char)) + 1);
for(count = 0; count < patch->size; count++) {
patch->data[count] = (char)data;
}
return 1;
}
return 0;
}
/**
* Checks that an IPS file has the correct header
*
* Checks that the next 5 bytes (In an IPS file, the first 5 bytes are
* where the header is present) say "PATCH".
*
* @param FILE *filePointer the pointer to the IPS patch file.
*
* @returns int 1 on a valid PATCH header, 0 otherwise.
*/
int IPSCheckPatch(FILE *filePointer, int verbose) {
char buffer[6];
if(fread(buffer, BYTE, 5, filePointer) == 0) {
return 0;
}
buffer[5] = '\0';
/* Valid patch header? */
if(strcmp(buffer, "PATCH") == 0) {
if(verbose) {
printf("This appears to be a valid IPS patch file...\n");
}
return 1;
}
return 0;
}
/**
* Patches a file using an IPS file
*
* This function will attempt to patch a file specified in it's
* parameters with the patch file also specified in it's parameters
* according to the flags set.
*
* @param struct pStruct *params A pointer to a parameter struct that
* contains the files and parameters in which to patch the file.
*
* @return Returns 1 on success or 0 on failure.
*/
int IPSPatchFile(struct pStruct *params) {
struct patchData patch = {0, 0, NULL};
while(IPSReadRecord(&patch, params->patchFile)) {
if((params->flags & ARG_VERYVERBOSE)) {
printf("Applied patch. Offset: Byte %d size: %d bytes\n",
(unsigned int)patch.offset,
(unsigned int)patch.size);
}
fseek(params->romFile, patch.offset, SEEK_SET);
fwrite(patch.data, patch.size, 1, params->romFile);
free(patch.data);
}
return 0;
}
/**
* Creates a patch file with the passed in pStruct
*
* This function creates an IPS patch file
*/
int IPSCreatePatch(struct pStruct *params) {
rewind(params->patchFile);
fwrite("PATCH", 1, sizeof("PATCH"), params->patchFile);
fwrite("EOF", 1, sizeof("EOF"), params->patchFile);
return 0;
}
int IPSWriteRecord(struct patchData *patch, FILE *filePointer) {
fwrite(&patch->offset, 3, 1, filePointer);
fwrite(&patch->size, 2, 1, filePointer);
fwrite(&patch->data, sizeof(patch->data), 1, filePointer);
return 0;
}
int IPSWriteRLE(struct patchData *patch, FILE *filePointer) {
ASSERT(&patch->offset == 0, "RLE writing occurring without offset of 0.");
fwrite(&patch->offset, 2, 1, filePointer); /* Offset should be 0 */
fwrite(&patch->size, 2, 1, filePointer);
fwrite(&patch->data, 1, 1, filePointer);
return 0;
}