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
|
/*
Unix SMB/Netbios implementation.
Version 2.0.x.
Create unicode map files from unicode_def.XXX files.
Copyright (C) Jeremy Allison 1997-1999.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include "includes.h"
static char *prog_name = NULL;
/*
* Print program usage and die.
*/
static void unicode_map_usage(char *progname)
{
fprintf(stderr, "Usage is : %s <codepage> <inputfile> <outputfile>\n",
progname);
exit(1);
}
/*
* Read a line from a buffer into a line buffer. Ensure null
* terminated.
*/
static void read_line( char **buf, char *line_buf, size_t size)
{
char *p = *buf;
size_t num = 0;
for(; *p && (*p != '\n') && (*p != '\032'); p++) {
if(num < (size - 1))
line_buf[num++] = *p;
}
if(*p)
p++; /* Go past the '\n' */
line_buf[num] = '\0';
*buf = p;
}
/*
* Strip comment lines and blank lines from the data.
* Copies into a new buffer and frees the old.
* Returns the number of lines copied.
*/
static size_t clean_data( char **buf, size_t *size)
{
pstring linebuf;
char *p = *buf;
size_t num_lines = 0;
char *newbuf = (char *)malloc( *size + 1);
char *newbuf_p = NULL;
if(newbuf == NULL) {
fprintf(stderr, "%s: malloc fail for size %u.\n", prog_name, (unsigned int)(*size + 1));
exit(1);
}
newbuf_p = newbuf;
*newbuf_p = '\0';
while( *p ) {
char *cp;
read_line( &p, linebuf, sizeof(linebuf));
/* Null terminate after comment. */
if((cp = strchr( linebuf, '#'))!= NULL)
*cp = '\0';
for(cp = linebuf;*cp && isspace(*cp); cp++)
;
if(*cp == '\0')
continue;
safe_strcpy(newbuf_p, cp, *size - (newbuf_p - newbuf));
num_lines++;
newbuf_p += (strlen(newbuf_p) + 1);
}
free(*buf);
*buf = newbuf;
return num_lines;
}
/*
* Parse a uint16 from a codepage file.
*/
static BOOL parse_uint16(char *buf, uint16 *uip)
{
unsigned int ui;
char *endptr = NULL;
ui = (unsigned int)strtol(buf, &endptr, 0);
if(endptr == buf || ui > 65535)
return False;
*uip = (uint16)ui;
return True;
}
/*
* Print a parse error and exit.
*/
static void parse_error(const char *buf, const char *input_file, const char *msg)
{
fprintf(stderr, "%s: In file %s : %s whilst parsing line \n%s\n", prog_name,
input_file, msg, buf);
exit(1);
}
/*
* Create a compiled unicode map file from a unicode map definition file.
*/
static int do_compile(const char *codepage, const char *input_file, const char *output_file)
{
FILE *fp = NULL;
size_t size = 0;
size_t offset = 0;
char *buf = NULL;
char *orig_buf = NULL;
char *output_buf = NULL;
uint16 cp_to_ucs2[65536];
uint16 ucs2_to_cp[65536];
BOOL multibyte_code_page = False;
int num_lines = 0;
int i = 0;
SMB_STRUCT_STAT st;
/* Get the size of the input file. Read the entire thing into memory. */
if(sys_stat((char *)input_file, &st)!= 0) {
fprintf(stderr, "%s: failed to get the file size for file %s. Error was %s\n",
prog_name, input_file, strerror(errno));
exit(1);
}
size = (size_t)st.st_size;
if((fp = sys_fopen(input_file, "r")) == NULL) {
fprintf(stderr, "%s: cannot open file %s for input.\n", prog_name, input_file);
exit(1);
}
/* As we will be reading text, allocate one more byte for a '\0' */
if((buf = (char *)malloc( size + 1 )) == NULL) {
fprintf(stderr, "%s: malloc fail for size %d.\n", prog_name, size + 1);
fclose(fp);
exit(1);
}
if(fread( buf, 1, size, fp) != size) {
fprintf(stderr, "%s: read failed for file %s. Error was %s.\n", prog_name,
input_file, strerror(errno));
free((char *)buf);
fclose(fp);
exit(1);
}
/* Null terminate the text read. */
buf[size] = '\0';
/* Go through the data line by line, strip out comments (anything
after a '#' to end-of-line) and blank lines. The rest should be
the codepage data.
*/
num_lines = clean_data( &buf, &size);
orig_buf = buf; /* Store for free(). */
/*
* Initialize the output data.
*/
memset(cp_to_ucs2, '\0', sizeof(cp_to_ucs2));
ucs2_to_cp[0] = 0;
for (i = 1; i < 65536; i++)
ucs2_to_cp[i] = (uint16)'_';
/* Now convert the lines into the compiled form. */
for(i = 0; i < num_lines; i++) {
char token_buf[512];
char *p = buf;
uint16 cp = 0;
uint16 ucs2 = 0;
/* Get the codepage value. */
if(!next_token(&p, token_buf, NULL, sizeof(token_buf)))
parse_error(buf, input_file, "cannot parse first value");
if(!parse_uint16( token_buf, &cp))
parse_error(buf, input_file, "first value doesn't resolve to an unsigned 16 bit integer");
if(cp > 255)
multibyte_code_page = True;
/* Get the ucs2 value. */
if(!next_token(&p, token_buf, NULL, sizeof(token_buf))) {
/*
* Some of the multibyte codepage to unicode map files
* list a single byte as a leading multibyte and have no
* second value.
*/
buf += (strlen(buf) + 1);
continue;
}
if(!parse_uint16( token_buf, &ucs2))
parse_error(buf, input_file, "second value doesn't resolve to an unsigned 16 bit integer");
/*
* Set up the cross reference in little-endian format.
*/
SSVAL(((char *)&cp_to_ucs2[cp]),0,ucs2);
SSVAL(((char *)&ucs2_to_cp[ucs2]),0,cp);
/*
* Next line.
*/
buf += (strlen(buf) + 1);
}
size = UNICODE_MAP_HEADER_SIZE + (multibyte_code_page ? (4*65536) : (2*256 + 2*65536));
if((output_buf = (char *)malloc( size )) == NULL) {
fprintf(stderr, "%s: output buffer malloc fail for size %d.\n", prog_name, size);
fclose(fp);
exit(1);
}
/* Setup the output file header. */
SSVAL(output_buf,UNICODE_MAP_VERSION_OFFSET,UNICODE_MAP_FILE_VERSION_ID);
memset(&output_buf[UNICODE_MAP_CLIENT_CODEPAGE_OFFSET],'\0',UNICODE_MAP_CODEPAGE_ID_SIZE);
safe_strcpy(&output_buf[UNICODE_MAP_CLIENT_CODEPAGE_OFFSET], codepage, UNICODE_MAP_CODEPAGE_ID_SIZE - 1);
output_buf[UNICODE_MAP_CLIENT_CODEPAGE_OFFSET+UNICODE_MAP_CODEPAGE_ID_SIZE-1] = '\0';
offset = UNICODE_MAP_HEADER_SIZE;
if (multibyte_code_page) {
SIVAL(output_buf,UNICODE_MAP_CP_TO_UNICODE_LENGTH_OFFSET,2*65536);
memcpy(output_buf+offset, (char *)cp_to_ucs2, 2*65536);
offset += 2*65536;
} else {
SIVAL(output_buf,UNICODE_MAP_CP_TO_UNICODE_LENGTH_OFFSET,2*256);
memcpy(output_buf+offset, (char *)cp_to_ucs2, 2*256);
offset += 2*256;
}
SIVAL(output_buf,UNICODE_MAP_UNICODE_TO_CP_LENGTH_OFFSET,65536*2);
memcpy(output_buf+offset, (char *)ucs2_to_cp, 2*65536);
/* Now write out the output_buf. */
if((fp = sys_fopen(output_file, "w"))==NULL) {
fprintf(stderr, "%s: Cannot open output file %s. Error was %s.\n",
prog_name, output_file, strerror(errno));
exit(1);
}
if(fwrite(output_buf, 1, size, fp) != size) {
fprintf(stderr, "%s: Cannot write output file %s. Error was %s.\n",
prog_name, output_file, strerror(errno));
exit(1);
}
fclose(fp);
free(orig_buf);
free(output_buf);
return 0;
}
int main(int argc, char **argv)
{
const char *codepage = NULL;
char *input_file = NULL;
char *output_file = NULL;
prog_name = argv[0];
if(argc != 4)
unicode_map_usage(prog_name);
codepage = argv[1];
input_file = argv[2];
output_file = argv[3];
return do_compile( codepage, input_file, output_file);
}
|