forked from DVSwitch/md380tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
localize.c
154 lines (127 loc) · 3.31 KB
/
localize.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
/* \file localize.c
This is a quick and dirty hack in C for patching the MD380
firmware's strings from English to other Roman-alphabet languages.
The code is rather ugly, and all of this will be rewritten lest the
technical debt collector come calling.
For adding new locales, add lines of text to strings.txt.
*/
//Needed for memmem()
#define _GNU_SOURCE
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<wchar.h>
#include<assert.h>
//! Maximum string length.
#define MAXLEN 1024
//! Buffer for the current line of text.
char buf[MAXLEN];
//! Buffer for the string to be found
short en[MAXLEN];
//! Buffer length
int enlen;
//! Buffer for the target to replace it with.
short target[MAXLEN];
//! Target language.
char targetlang[2];
//! Megabyte firmware buffer, just a bit large.
char firmware[0x100000];
//! Size of the firmware.
size_t firmwaresize=0;
//! Ends the wide string with a newline.
void trimstring(short *target, int len){
for(int i=0; i<len && target[i]; i++){
if(target[i]=='\r' || target[i]=='\n')
target[i]=0;
}
}
//! Copies from a char array to a short array.
void copystring(short *dst, char *src){
while(*src){
*dst=*src;
printf("%c",*dst);
dst++; src++;
}
printf("\n");
}
//! Handles a line of input.
int handle(char *line){
//Simple state machine for parsing.
static enum {IDLE, EN, NEW} handlerstate=IDLE;
int len = strlen(line);
if(len<=2){
handlerstate=IDLE;
}else if(*line=='#'){
//Just a comment.
}else if(line[0]=='e' && line[1]=='n'){
printf("\n");
printf("English: %s",line+3);
handlerstate=EN;
enlen=strlen(line+3);
copystring(en,line+3);
trimstring(en,MAXLEN);
}else if(line[0]==targetlang[0] && line[1]==targetlang[1]){
printf("Target: %s",line+3);
copystring(target,line+3);
trimstring(target,MAXLEN);
handlerstate=NEW;
}
if(handlerstate==NEW){
short *toreplace;
if((toreplace=memmem(firmware,firmwaresize*1024,
en,enlen))){
printf("Found it at %08x.\n",
(int) toreplace - (int) firmware + 0x0800C000);
memcpy(toreplace,target,enlen*2+2);
}
handlerstate=IDLE;
}
return handlerstate;
}
//! Open the firmware
void openfirmware(char *filename){
FILE *input = fopen(filename,"rb");
if(input){
firmwaresize=fread(firmware,1024,1024,input);
printf("Read %d blocks of %s\n",(int) firmwaresize,filename);
fclose(input);
}else{
printf("Error reading: %s\n",filename);
}
}
//! Write out the firmware.
void closefirmware(char *filename){
FILE *output = fopen(filename,"wb");
size_t outsize;
if(output){
outsize=fwrite(firmware,1024,firmwaresize,output);
printf("Wrote %d blocks of %s\n",(int) outsize,filename);
fclose(output);
}else{
printf("Error writing: %s\n",filename);
}
}
//! Prints usage info.
void usage(char *filename){
printf("USAGE: \n\t%s language infile outfile < strings.txt\n",
filename);
}
//! Main method.
int main(int argc, char **argv){
if(argc<4){
usage(argv[0]);
exit(1);
}
//Set the global language
targetlang[0]=argv[1][0];
targetlang[1]=argv[1][1];
//Load the input file.
openfirmware(argv[2]);
//Perform the conversion.
while(!feof(stdin)){
fgets(buf,MAXLEN,stdin);
handle(buf);
}
//Write the output file.
closefirmware(argv[3]);
}