-
Notifications
You must be signed in to change notification settings - Fork 10
/
translate.c
396 lines (335 loc) · 21.1 KB
/
translate.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
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct trans_struct {
long len;
char orig[1024];
char tr1[1024];
long tr_len;
} trans_string_struct;
char tr1[1024] = { 0 };
void hex2str(long input, char output[16]) {
char tmpstr[9] = { 0 };
sprintf(tmpstr, "%08X", input);
for (int i = 0; i < 8; i++) {
output[2 * i] = tmpstr[i];
};
};
long str2hex(char input[16]) {
char tmpstr[9] = { 0 };
long output;
for (int i = 0; i < 8; i++) {
tmpstr[i] = input[2 * i];
};
output = strtol(tmpstr, NULL, 16);
return output;
};
void transfile_to_array(const unsigned char *data, long len, int line_cnt, trans_string_struct *in_arr) {
unsigned char p_tmp[2];
char c_str_len[16] = { 0 };
long l_str_len = 0;
const unsigned char *start_addr = data;
char first_line = 1, new_line = 1, tab = 0, trans_idx = 1;
int good_strings = 0, bad_strings = 0;
if (*data == 0xFF) data++; // Skip BOM header
if (*data == 0xFE) data++;
p_tmp[0] = *data;
data++;
p_tmp[1] = *data;
data++;
int i = 0;
while (i < line_cnt) {
switch ((p_tmp[0] << 8) + p_tmp[1]) {
case 0x0D00:
p_tmp[0] = *data;
data++;
p_tmp[1] = *data;
data++;
if (p_tmp[0] == 0x0A && p_tmp[1] == 0x00) { // new line
if (new_line == 0) i++;
new_line = 1;
if (i >= line_cnt) continue; //detect last line w/o data - exit from cycle
p_tmp[0] = *data;
data++;
p_tmp[1] = *data;
data++;
}
break;
case 0x0900: //divider TAB detected
p_tmp[0] = *data;
data++;
p_tmp[1] = *data;
data++;
tab = 1;
break;
default: // just get new 2-bytes
if (first_line != 1) { //Not first string
p_tmp[0] = *data;
data++;
p_tmp[1] = *data;
data++;
}
};
if (new_line == 1) {
if (p_tmp[0] == 0x0D && p_tmp[1] == 0x00) { //New line again
continue;
}
else
{
new_line = 0;
if (first_line == 1) {
first_line = 0;
i = 0;
}
data--;
data--;
for (int j = 0; j < 16; j++) { // Get lenght of the original string
c_str_len[j] = *data;
data++;
}
l_str_len = str2hex(c_str_len);
if ((l_str_len == 0) || (l_str_len > 1024)) { //if empty, not correct or too long string - mark it 0, no processing
in_arr[i].len = 0;
trans_idx = 0;
bad_strings++;
}
else
{
in_arr[i].len = l_str_len;
trans_idx = 1;
good_strings++;
};
p_tmp[0] = *data;
data++;
p_tmp[1] = *data;
data++;
}
}
if (tab == 1) {
int pos = 0;
long trans_len = 0;
do {
if (p_tmp[0] == 0x09 && p_tmp[1] == 0x00) { // Next TAB detected, go out
tab = 0;
trans_idx = 2;
continue;
}
if (p_tmp[0] == 0x0D && p_tmp[1] == 0x00) { // New line detected, go out
tab = 0;
continue;
}
if (trans_idx == 1) {
in_arr[i].orig[pos] = p_tmp[0];
pos++;
in_arr[i].orig[pos] = p_tmp[1];
pos++;
}
if (trans_idx == 2) {
in_arr[i].tr1[pos] = p_tmp[0];
pos++;
in_arr[i].tr1[pos] = p_tmp[1];
pos++;
trans_len = trans_len + 2;
}
p_tmp[0] = *data;
data++;
p_tmp[1] = *data;
data++;
} while (tab);
in_arr[i].tr_len = trans_len;
}
if ((data - start_addr) > len) break;
}
printf("Processed input strings = %d\n", line_cnt);
printf("Good strings = %d\n", good_strings);
printf("Bad strings (len > 1024) = %d\n", bad_strings);
};
int get_translation(long len, char *str, trans_string_struct *in_arr, int trans_line_idx_count, trans_string_struct *tr_arr) {
int is_equal = 0;
for (int i = 0; i < trans_line_idx_count; i++) {
if ((in_arr[i].len > 0) && (len == in_arr[i].len)) {
is_equal = 1;
for (int j = 0; j < len; j++) {
if (str[j] != in_arr[i].orig[j]) {
is_equal = 0;
break;
}
}
if (is_equal) {
*tr_arr = in_arr[i];
//printf("%d ", i); // uncomment to debug translated strings
//for (int k = 0; k < tr_arr->tr_len;k++) {
// if(tr_arr->tr1[k] != 0x00 && tr_arr->tr1[k] != 0x0A) printf("%c", tr_arr->tr1[k]);
//}
//printf("\n");
return 1;
}
}
}
return 0;
}
int main(int argc, char *argv[])
{
const trans_string_struct empty_struct = {0,0,0};
const long OFFSET = 0x18C8D0L; // RZ09 1.04 0x18C8D0L; RZ77 2.07 = 0x1EA87CL; ZH0009 = 0x147140
const long BLOCK_LEN = 0xAF00BL; // RZ09 1.04 0xAF00BL; RZ77 2.07 = 0xB5CDFL; ZH0009 = 0xACD7F
FILE *fin, *ftr, *fout;
long trans_lenght, in_pos = 0, last_in_pos = 0;
int trans_line_idx_count = 0;
int processed_strings_cnt = 0, found_strings_cnt = 0;
char *datfile = "initDB.dat";
char *transfile = "translation.txt";
char *datfileout = "initDB_out.dat";
char fffe[2] = { 0xFF,0xFE };
char div[2] = { 0x09, 0x00 }; //TAB 0x09 " 0x22
char divcrlf[6] = { 0x09, 0x00, 0x0D, 0x00, 0x0A, 0x00 };
char cr = 0x0D;
char lf = 0x0A;
char zero = 0x00;
char str[1024] = { 0 };
char tmpstr[16] = { 0 };
ftr = fopen(transfile, "rb");
fseek(ftr, 0L, SEEK_END);
trans_lenght = ftell(ftr);
unsigned char *p_trans = malloc(trans_lenght);
rewind(ftr);
unsigned char *p_tmp = malloc(0xF);
int str_cnt = 0;
fread(p_tmp, 1, 2, ftr); // Skip BOM header
while (!feof(ftr)) // Counts strings in the translation file exclude empty strings
{
fread(p_tmp, 1, 2, ftr);
if (p_tmp[0] == 0x0D && p_tmp[1] == 0x00)
{
fread(p_tmp, 1, 2, ftr);
if (p_tmp[0] == 0x0A && p_tmp[1] == 0x00 && str_cnt > 0) {
trans_line_idx_count++;
str_cnt = 0;
}
}
else str_cnt++;
}
trans_string_struct *in_arr = malloc(trans_line_idx_count * sizeof(*in_arr));
for (int i = 0; i < trans_line_idx_count; i++) { // Initialize trans strings struct
in_arr[i] = empty_struct;
};
trans_string_struct *tr_arr = malloc(sizeof(*tr_arr));
tr_arr[0] = empty_struct;
int *trans_strings_size = malloc(trans_line_idx_count * sizeof(*trans_strings_size));
char *trans_strings_orig = malloc(trans_line_idx_count * sizeof(*trans_strings_orig));
rewind(ftr);
fread(p_trans, 1, trans_lenght, ftr);
transfile_to_array(p_trans, trans_lenght, trans_line_idx_count, in_arr);
fin = fopen(datfile, "rb");
unsigned char *p_out = malloc(OFFSET);
fread(p_out, 1, OFFSET, fin);
rewind(fin);
fout = fopen(datfileout, "wb");
fwrite(p_out, 1, OFFSET, fout);
in_pos = OFFSET;
last_in_pos = OFFSET;
free(p_out);
unsigned char *p = malloc(0xFF);
fseek(fin, OFFSET, SEEK_SET); //0x18C0D4L
unsigned int index = 0;
for (int i = 0; i < (BLOCK_LEN + 2) / 2; i++) {
fread(p, 1, 2, fin);
if (p[0] == 0xFF && p[1] == 0xFF) {
index = 0;
continue;
};
if (p[0] == 0x00 && p[1] == 0x00) {
if (index > 0) {
if (index > 8 && str[index - 8] == 0x2E && str[index - 6] == 0x67 && str[index - 4] == 0x69 && str[index - 2] == 0x66) { // Skip links to images, .gif
index = 0;
continue;
}
if (index > 8 && str[index - 8] == 0x2E && str[index - 6] == 0x62 && str[index - 4] == 0x6D && str[index - 2] == 0x70) { // Skip links to images, .bmp
index = 0;
continue;
}
if (index > 8 && str[index - 8] == 0x2E && str[index - 6] == 0x70 && str[index - 4] == 0x6E && str[index - 2] == 0x67) { // Skip links to images, .png
index = 0;
continue;
}
if (index > 8 && str[index - 8] == 0x2E && str[index - 6] == 0x6A && str[index - 4] == 0x70 && str[index - 2] == 0x67) { // Skip links to images, .jpg
index = 0;
continue;
}
if (index > 8 && str[0] == 0x53 && str[2] == 0x54 && str[4] == 0x52 && str[6] == 0x5F) { // Skip "STR_" started strings
index = 0;
continue;
}
if (index > 8 && str[0] == 0x4D && str[2] == 0x53 && str[4] == 0x47 && str[6] == 0x5F) { // Skip "MSG_" started strings
index = 0;
continue;
}
if (index > 8 && str[0] == 0x4D && str[2] == 0x4D && str[4] == 0x5F) { // Skip "MM_" started strings
index = 0;
continue;
}
if (index > 2 && str[0] == 0x1B && str[1] == 0x00) { // Skip ESC sequences
index = 0;
continue;
}
if (index > 2 && str[0] == 0x23 && str[1] == 0x00) { // Skip # starts string IDs
index = 0;
continue;
}
if (get_translation(index, str, in_arr, trans_line_idx_count, tr_arr)) {
//printf("Str lenght = %2X\n", index);
//printf("Str = %s\n", str);
//printf("Tr1 = %s\n", tr_arr->tr1);
found_strings_cnt++;
if (tr_arr->tr_len > 0) { // If translation string is empty (len == 0), skip changing the data-file
in_pos = OFFSET + (i + 1) * 2 - (index + 2); //Input data file string start position
//printf("Position for start writing = %2X\n", last_in_pos);
//printf("Position for out string = %2X\n", in_pos);
//printf("Bytes to write = %2X\n", in_pos - last_in_pos);
long in_curr_pos = ftell(fin);
long in_bytes_as_is = in_pos - last_in_pos;
rewind(fin);
fseek(fin, last_in_pos, SEEK_SET);
unsigned char *p_out = malloc(in_bytes_as_is);
fread(p_out, 1, in_bytes_as_is, fin);
fseek(fout, 0L, SEEK_END);
fwrite(p_out, 1, in_bytes_as_is, fout);
if (tr_arr[0].len != tr_arr[0].tr_len) {
printf("WARNING!!! Translation string is not equal to original string:\norig_len = %2X orig = %s \ntrsn_len = %2X tran = ", tr_arr[0].len, &tr_arr[0].orig, tr_arr[0].tr_len);
for (int k = 0; k < tr_arr[0].len;k++) {
if(tr_arr[0].tr1[k] != 0x00) printf("%c", tr_arr[0].tr1[k]);
}
printf("\n");
}
//fwrite(&tr_arr[0].tr1, 1, tr_arr[0].tr_len + 2, fout); // Get len as translation file string len !!! be careful!!
fwrite(&tr_arr[0].tr1, 1, tr_arr[0].len + 2, fout); // Get len from sorce DAT file str
rewind(fin);
fseek(fin, in_curr_pos, SEEK_SET);
last_in_pos = in_curr_pos;
}
}
processed_strings_cnt++;
index = 0;
memset(str, 0, sizeof(str));
};
continue;
};
str[index] = p[0];
index++;
str[index] = p[1];
index++;
};
long in_bytes_as_is = OFFSET + BLOCK_LEN + 1 - last_in_pos;
rewind(fin);
fseek(fin, last_in_pos, SEEK_SET);
unsigned char *p_out1 = malloc(in_bytes_as_is);
fread(p_out1, 1, in_bytes_as_is, fin);
fseek(fout, 0L, SEEK_END);
fwrite(p_out1, 1, in_bytes_as_is, fout);
printf("Processed strings in data file = %d\n", processed_strings_cnt);
printf("Found strings in data file = %d\n", found_strings_cnt);
fclose(ftr);
fclose(fin);
fclose(fout);
}