2 Unix SMB/Netbios implementation.
4 Create codepage files from codepage_def.XXX files.
6 Copyright (C) Jeremy Allison 1997-1999.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 static char *prog_name = NULL;
28 * Print program usage and die.
31 static void codepage_usage(char *progname)
33 fprintf(stderr, "Usage is : %s [c|d] <codepage> <inputfile> <outputfile>\n",
39 * Read a line from a buffer into a line buffer. Ensure null
43 static void read_line( char **buf, char *line_buf, int size)
48 for(; *p && (*p != '\n'); p++)
54 p++; /* Go past the '\n' */
60 * Strip comment lines and blank lines from the data.
61 * Copies into a new buffer and frees the old.
62 * Returns the number of lines copied.
65 static int clean_data( char **buf, size_t *size)
70 char *newbuf = (char *)malloc( *size + 1);
71 char *newbuf_p = NULL;
75 fprintf(stderr, "%s: malloc fail for size %d.\n", prog_name, *size + 1);
86 read_line( &p, linebuf, sizeof(linebuf));
87 /* Null terminate after comment. */
88 if((cp = strchr( linebuf, '#'))!= NULL)
91 for(cp = linebuf;*cp && isspace(*cp); cp++)
97 safe_strcpy(newbuf_p, cp, *size - (newbuf_p - newbuf));
99 newbuf_p += (strlen(newbuf_p) + 1);
108 * Parse a byte from a codepage file.
111 static BOOL parse_byte(char *buf, unsigned char *bp)
116 b = (unsigned int)strtol(buf, &endptr, 0);
117 if(endptr == buf || b > 255)
120 *bp = (unsigned char)b;
125 * Parse a bool from a codepage file.
128 static BOOL parse_bool(char *buf, unsigned char *bp)
130 if(isdigit((int)*buf))
134 *bp = (unsigned char)strtol(buf, &endptr, 0);
140 if(strcasecmp(buf, "True") && strcasecmp(buf, "False"))
142 if(strcasecmp(buf, "True")==0)
151 * Print a parse error and exit.
154 static void parse_error(char *buf, char *msg)
156 fprintf(stderr, "%s: %s whilst parsing line \n%s\n", prog_name,
162 * Create a compiled codepage file from a codepage definition file.
165 static int do_compile(int codepage, char *input_file, char *output_file)
170 char output_buf[CODEPAGE_HEADER_SIZE + 4 * MAXCODEPAGELINES];
175 /* Get the size of the input file. Read the entire thing into memory. */
176 if(sys_stat((char *)input_file, &st)!= 0)
178 fprintf(stderr, "%s: failed to get the file size for file %s. Error was %s\n",
179 prog_name, input_file, strerror(errno));
183 size = (uint32)st.st_size;
185 /* I don't believe these things should be bigger than 100k :-) */
188 fprintf(stderr, "%s: filesize %d is too large for a codepage definition file. \
189 The maximum size I will believe is 100k.\n", prog_name, size);
193 if((fp = sys_fopen(input_file, "r")) == NULL)
195 fprintf(stderr, "%s: cannot open file %s for input.\n", prog_name, input_file);
199 /* As we will be reading text, allocate one more byte for a '\0' */
200 if((buf = (char *)malloc( size + 1 )) == NULL)
202 fprintf(stderr, "%s: malloc fail for size %d.\n", prog_name, size + 1);
207 if(fread( buf, 1, size, fp) != size)
209 fprintf(stderr, "%s: read failed for file %s. Error was %s.\n", prog_name,
210 input_file, strerror(errno));
216 /* Null terminate the text read. */
219 /* Go through the data line by line, strip out comments (anything
220 after a '#' to end-of-line) and blank lines. The rest should be
224 num_lines = clean_data( &buf, &size);
226 /* There can be a maximum of MAXCODEPAGELINES lines. */
227 if(num_lines > MAXCODEPAGELINES)
229 fprintf(stderr, "%s: There can be a maximum %d lines of data in a codepage \
230 definition file. File %s has %d.\n", prog_name, MAXCODEPAGELINES, input_file, num_lines);
234 /* Setup the output file header. */
235 SSVAL(output_buf,CODEPAGE_VERSION_OFFSET,CODEPAGE_FILE_VERSION_ID);
236 SSVAL(output_buf,CODEPAGE_CLIENT_CODEPAGE_OFFSET,(uint16)codepage);
237 SIVAL(output_buf,CODEPAGE_LENGTH_OFFSET,(num_lines * 4));
239 /* Now convert the lines into the compiled form. */
240 for(i = 0; i < num_lines; i++)
246 /* Get the 'lower' value. */
247 if(!next_token(&p, token_buf, NULL, sizeof(token_buf)))
248 parse_error(buf, "cannot parse first value");
249 if(!parse_byte( token_buf, &b))
250 parse_error(buf, "first value doesn't resolve to a byte");
252 /* Add this to the output buffer. */
253 SCVAL(output_buf,CODEPAGE_HEADER_SIZE+(i*4),b);
255 /* Get the 'upper' value. */
256 if(!next_token(&p, token_buf, NULL, sizeof(token_buf)))
257 parse_error(buf, "cannot parse second value");
258 if(!parse_byte( token_buf, &b))
259 parse_error(buf, "second value doesn't resolve to a byte");
261 /* Add this to the output buffer. */
262 SCVAL(output_buf,CODEPAGE_HEADER_SIZE+(i*4) + 1,b);
264 /* Get the 'upper to lower' value. */
265 if(!next_token(&p, token_buf, NULL, sizeof(token_buf)))
266 parse_error(buf, "cannot parse third value");
267 if(!parse_bool( token_buf, &b))
268 parse_error(buf, "third value doesn't resolve to a boolean");
270 /* Add this to the output buffer. */
271 SCVAL(output_buf,CODEPAGE_HEADER_SIZE+(i*4) + 2,b);
273 /* Get the 'lower to upper' value. */
274 if(!next_token(&p, token_buf, NULL, sizeof(token_buf)))
275 parse_error(buf, "cannot parse fourth value");
276 if(!parse_bool( token_buf, &b))
277 parse_error(buf, "fourth value doesn't resolve to a boolean");
279 /* Add this to the output buffer. */
280 SCVAL(output_buf,CODEPAGE_HEADER_SIZE+(i*4) + 3,b);
282 buf += (strlen(buf) + 1);
285 /* Now write out the output_buf. */
286 if((fp = sys_fopen(output_file, "w"))==NULL)
288 fprintf(stderr, "%s: Cannot open output file %s. Error was %s.\n",
289 prog_name, output_file, strerror(errno));
293 if(fwrite(output_buf, 1, CODEPAGE_HEADER_SIZE + (num_lines*4), fp) !=
294 CODEPAGE_HEADER_SIZE + (num_lines*4))
296 fprintf(stderr, "%s: Cannot write output file %s. Error was %s.\n",
297 prog_name, output_file, strerror(errno));
307 * Placeholder for now.
310 static int do_decompile( int codepage, char *input_file, char *output_file)
314 char header_buf[CODEPAGE_HEADER_SIZE];
320 /* Get the size of the input file. Read the entire thing into memory. */
321 if(sys_stat((char *)input_file, &st)!= 0)
323 fprintf(stderr, "%s: failed to get the file size for file %s. Error was %s\n",
324 prog_name, input_file, strerror(errno));
328 size = (size_t)st.st_size;
330 if( size < CODEPAGE_HEADER_SIZE || size > (CODEPAGE_HEADER_SIZE + 256))
332 fprintf(stderr, "%s: file %s is an incorrect size for a \
333 code page file.\n", prog_name, input_file);
337 /* Read the first 8 bytes of the codepage file - check
338 the version number and code page number. All the data
339 is held in little endian format.
342 if((fp = sys_fopen( input_file, "r")) == NULL)
344 fprintf(stderr, "%s: cannot open file %s. Error was %s\n",
345 prog_name, input_file, strerror(errno));
349 if(fread( header_buf, 1, CODEPAGE_HEADER_SIZE, fp)!=CODEPAGE_HEADER_SIZE)
351 fprintf(stderr, "%s: cannot read header from file %s. Error was %s\n",
352 prog_name, input_file, strerror(errno));
356 /* Check the version value */
357 if(SVAL(header_buf,CODEPAGE_VERSION_OFFSET) != CODEPAGE_FILE_VERSION_ID)
359 fprintf(stderr, "%s: filename %s has incorrect version id. \
360 Needed %hu, got %hu.\n",
361 prog_name, input_file, (uint16)CODEPAGE_FILE_VERSION_ID,
362 SVAL(header_buf,CODEPAGE_VERSION_OFFSET));
366 /* Check the codepage matches */
367 if(SVAL(header_buf,CODEPAGE_CLIENT_CODEPAGE_OFFSET) != (uint16)codepage)
369 fprintf(stderr, "%s: filename %s has incorrect codepage. \
370 Needed %hu, got %hu.\n",
371 prog_name, input_file, (uint16)codepage,
372 SVAL(header_buf,CODEPAGE_CLIENT_CODEPAGE_OFFSET));
376 /* Check the length is correct. */
377 if(IVAL(header_buf,CODEPAGE_LENGTH_OFFSET) !=
378 (unsigned int)(size - CODEPAGE_HEADER_SIZE))
380 fprintf(stderr, "%s: filename %s has incorrect size headers. \
381 Needed %u, got %u.\n", prog_name, input_file, size - CODEPAGE_HEADER_SIZE,
382 IVAL(header_buf,CODEPAGE_LENGTH_OFFSET));
386 size -= CODEPAGE_HEADER_SIZE; /* Remove header */
388 /* Make sure the size is a multiple of 4. */
391 fprintf(stderr, "%s: filename %s has a codepage size not a \
392 multiple of 4.\n", prog_name, input_file);
396 /* Allocate space for the code page file and read it all in. */
397 if((buf = (char *)malloc( size )) == NULL)
399 fprintf (stderr, "%s: malloc fail for size %d.\n",
404 if(fread( buf, 1, size, fp)!=size)
406 fprintf(stderr, "%s: read fail on file %s. Error was %s.\n",
407 prog_name, input_file, strerror(errno));
413 /* Now dump the codepage into an ascii file. */
414 if((fp = sys_fopen(output_file, "w")) == NULL)
416 fprintf(stderr, "%s: cannot open file %s. Error was %s\n",
417 prog_name, output_file, strerror(errno));
421 fprintf(fp, "#\n# Codepage definition file for IBM Code Page %d.\n#\n",
423 fprintf(fp, "# This file was automatically generated.\n#\n");
424 fprintf(fp, "# defines lower->upper mapping.\n");
425 fprintf(fp, "#\n#The columns are :\n# lower\tupper\tu-t-l\tl-t-u\n#\n");
427 num_lines = size / 4;
428 for( i = 0; i < num_lines; i++)
430 fprintf(fp, "0x%02X\t0x%02X\t%s\t%s\n", CVAL(buf, (i*4)), CVAL(buf, (i*4)+1),
431 CVAL(buf, (i*4)+2) ? "True" : "False",
432 CVAL(buf, (i*4)+3) ? "True" : "False");
438 int main(int argc, char **argv)
441 char *input_file = NULL;
442 char *output_file = NULL;
443 BOOL compile = False;
448 codepage_usage(prog_name);
450 if(argv[1][0] != 'c' && argv[1][0] != 'C' && argv[1][0] != 'd' &&
452 codepage_usage(prog_name);
454 input_file = argv[3];
455 output_file = argv[4];
457 /* Are we compiling or decompiling. */
458 if(argv[1][0] == 'c' || argv[1][0] == 'C')
461 /* Convert the second argument into a client codepage value. */
462 if((codepage = atoi(argv[2])) == 0)
464 fprintf(stderr, "%s: %s is not a valid codepage.\n", prog_name, argv[2]);
469 return do_compile( codepage, input_file, output_file);
471 return do_decompile( codepage, input_file, output_file);