-
Notifications
You must be signed in to change notification settings - Fork 15
/
translate.c
186 lines (170 loc) · 4.12 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
#include <stdio.h>
#include <string.h>
#include <unistr.h>
#include <stdlib.h>
#define MAX_STRING_LENGTH 2048
#define MAX_STRINGS 8192
void readstring(FILE *f, char *p) {
char c;
int i;
char *op=p;
c=fgetc(f);
if(feof(f)) return;
if(c!='\"') {
puts("ERROR: mismatched quotes");
exit(1);
}
for(i=0;i<MAX_STRING_LENGTH;i++) {
p[i]=fgetc(f);
if(i>0 && p[i]<15 && p[i-1]==34)
break;
}
if(i>0)
p[i-1]=0;
c=p[i];
if(c==13)
c=fgetc(f);
if(c!=10 && !feof(f)) {
printf("ERROR: mismatched eol quotes %d %s\n",c, op);
}
// printf("read(%s)\n",op);
}
unsigned short crc16(const unsigned char *data, int len) {
unsigned short crc = 0xFFFF;
int i;
if (len) do {
crc ^= *data++;
for (i=0; i<8; i++) {
if (crc & 1) crc = (crc >> 1) ^ 0x8408;
else crc >>= 1;
}
} while (--len);
return(~crc);
}
int nstrings=0;
char **original=0;
char **translation=0;
void load_dictionary(char *untran,char *tran) {
FILE *f,*g;
if(original==0) {
original=malloc(MAX_STRINGS*sizeof(char *));
translation=malloc(MAX_STRINGS*sizeof(char *));
}
g=fopen(untran,"rb");
f=fopen(tran,"rb");
char st[MAX_STRING_LENGTH],st1[MAX_STRING_LENGTH];
int j;
while(!feof(g)) {
readstring(g,st);
readstring(f,st1);
printf("[%s -> %s]\n",st,st1);
for(j=0;j<nstrings;j++)
if(!strcmp(st,original[j]))
break;
if(j==nstrings) {
original[j]=malloc(strlen(st)+1);
translation[j]=malloc(strlen(st1)+1);
strcpy(original[j],st);
strcpy(translation[j],st1);
nstrings++;
}// else printf("Done Already %d\n",nstrings);
}
}
char *get_translation(char *s) {
int j;
for(j=0;j<nstrings;j++)
if(!strcmp(s,original[j]))
return translation[j];
printf("%s could not be translated\n",s);
return s;
}
int main(int argc,char *argv[]) {
char data[200000];
char st[MAX_STRING_LENGTH];
short st_u16[MAX_STRING_LENGTH];
char *infile="untranslated.txt",*outfile="PF090JPJPN.LNG";
FILE *f,*g;
int i;
unsigned *id=(unsigned *) data;
id[0]=0xa55a5aa5;
id[1]=0x01000001;
id[2]=0x39304650;
id[3]=0x45504a30;
id[4]=0;
id[5]=0;
id[6]=0;// len
id[7]=0x40;
id[8]=0;//last
id[9]=0xaaaaaa02;
id[10]=0xaaaaaaaa;
id[11]=0xaaaaaaaa;
id[12]=0xaaaaaaaa;
id[13]=0xaaaaaaaa;
id[14]=0xaaaaaaaa;
id[15]=0xaaaaaaaa;
if(argc>1)
infile=argv[1];
if(argc>2)
outfile=argv[2];
g=fopen(infile,"rb");
int start=0x40;
unsigned short *idx=(unsigned short *)(data+0x40);
short nstrings=0;
int offset=0;
int next=0;
short ch;
load_dictionary("untranslated_090.txt","translated_090.txt");
load_dictionary("untranslated_110.txt","translated_110.txt");
while(!feof(g)) {
readstring(g,st);
// printf("%d %s\n",nstrings,st);
nstrings++;
}
nstrings+=2;
rewind(g);
nstrings=nstrings-3;
printf("Found %d strings\n",nstrings);
unsigned short *strptr=idx+nstrings;
int off=0x40+(nstrings+3)*2;
int ntrans=0;
char *tr;
char **strings=malloc(sizeof(char *)*nstrings);
for(i=0;i<nstrings;i++) {
readstring(g,st);
tr=get_translation(st);
printf("%s -> %s\n",st,tr);
strings[i]=malloc(strlen(tr)+1);
strcpy(strings[i],tr);
int j;
for(j=0;j<i;j++)
if(!strcmp(tr,strings[j]))
break;
if(j<i) {
idx[i]=idx[j];
} else {
// puts(st);
idx[i]=(off-0x40)/2;
if((off-0x40)/2>65535) {
printf("ERROR: index too large %x\n",(off-0x40)/2);
exit(1);
}
size_t sz=512;
u8_to_u16(tr,strlen(tr),(short *)(data+off),&sz);
off+=sz*2;
data[off++]=0;
data[off++]=0;
}
}
idx[i]=(off-0x40)/2;
id[8]=off;
strcpy(data+off,"RG_VOICE_DATA");
off+=strlen("RG_VOICE_DATA");
data[off++]=0xf1; // this looks like a checksum at the end of the file but
data[off++]=0x8d; // it doesn't seem to matter if it's wrong
id[6]=off;
printf("%d strings\n",nstrings);
f=fopen(outfile,"wb");
fwrite(data,off,1,f);
fclose(f);
return(0);
}