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 *orig_buf
= NULL
;
171 char output_buf
[CODEPAGE_HEADER_SIZE
+ 4 * MAXCODEPAGELINES
];
176 /* Get the size of the input file. Read the entire thing into memory. */
177 if(sys_stat((char *)input_file
, &st
)!= 0)
179 fprintf(stderr
, "%s: failed to get the file size for file %s. Error was %s\n",
180 prog_name
, input_file
, strerror(errno
));
184 size
= (uint32
)st
.st_size
;
186 /* I don't believe these things should be bigger than 100k :-) */
189 fprintf(stderr
, "%s: filesize %d is too large for a codepage definition file. \
190 The maximum size I will believe is 100k.\n", prog_name
, size
);
194 if((fp
= sys_fopen(input_file
, "r")) == NULL
)
196 fprintf(stderr
, "%s: cannot open file %s for input.\n", prog_name
, input_file
);
200 /* As we will be reading text, allocate one more byte for a '\0' */
201 if((buf
= (char *)malloc( size
+ 1 )) == NULL
)
203 fprintf(stderr
, "%s: malloc fail for size %d.\n", prog_name
, size
+ 1);
208 if(fread( buf
, 1, size
, fp
) != size
)
210 fprintf(stderr
, "%s: read failed for file %s. Error was %s.\n", prog_name
,
211 input_file
, strerror(errno
));
217 /* Null terminate the text read. */
220 /* Go through the data line by line, strip out comments (anything
221 after a '#' to end-of-line) and blank lines. The rest should be
225 num_lines
= clean_data( &buf
, &size
);
227 orig_buf
= buf
; /* Save for free(). */
229 /* There can be a maximum of MAXCODEPAGELINES lines. */
230 if(num_lines
> MAXCODEPAGELINES
)
232 fprintf(stderr
, "%s: There can be a maximum %d lines of data in a codepage \
233 definition file. File %s has %d.\n", prog_name
, MAXCODEPAGELINES
, input_file
, num_lines
);
237 /* Setup the output file header. */
238 SSVAL(output_buf
,CODEPAGE_VERSION_OFFSET
,CODEPAGE_FILE_VERSION_ID
);
239 SSVAL(output_buf
,CODEPAGE_CLIENT_CODEPAGE_OFFSET
,(uint16
)codepage
);
240 SIVAL(output_buf
,CODEPAGE_LENGTH_OFFSET
,(num_lines
* 4));
242 /* Now convert the lines into the compiled form. */
243 for(i
= 0; i
< num_lines
; i
++)
249 /* Get the 'lower' value. */
250 if(!next_token(&p
, token_buf
, NULL
, sizeof(token_buf
)))
251 parse_error(buf
, "cannot parse first value");
252 if(!parse_byte( token_buf
, &b
))
253 parse_error(buf
, "first value doesn't resolve to a byte");
255 /* Add this to the output buffer. */
256 SCVAL(output_buf
,CODEPAGE_HEADER_SIZE
+(i
*4),b
);
258 /* Get the 'upper' value. */
259 if(!next_token(&p
, token_buf
, NULL
, sizeof(token_buf
)))
260 parse_error(buf
, "cannot parse second value");
261 if(!parse_byte( token_buf
, &b
))
262 parse_error(buf
, "second value doesn't resolve to a byte");
264 /* Add this to the output buffer. */
265 SCVAL(output_buf
,CODEPAGE_HEADER_SIZE
+(i
*4) + 1,b
);
267 /* Get the 'upper to lower' value. */
268 if(!next_token(&p
, token_buf
, NULL
, sizeof(token_buf
)))
269 parse_error(buf
, "cannot parse third value");
270 if(!parse_bool( token_buf
, &b
))
271 parse_error(buf
, "third value doesn't resolve to a boolean");
273 /* Add this to the output buffer. */
274 SCVAL(output_buf
,CODEPAGE_HEADER_SIZE
+(i
*4) + 2,b
);
276 /* Get the 'lower to upper' value. */
277 if(!next_token(&p
, token_buf
, NULL
, sizeof(token_buf
)))
278 parse_error(buf
, "cannot parse fourth value");
279 if(!parse_bool( token_buf
, &b
))
280 parse_error(buf
, "fourth value doesn't resolve to a boolean");
282 /* Add this to the output buffer. */
283 SCVAL(output_buf
,CODEPAGE_HEADER_SIZE
+(i
*4) + 3,b
);
285 buf
+= (strlen(buf
) + 1);
288 /* Now write out the output_buf. */
289 if((fp
= sys_fopen(output_file
, "w"))==NULL
)
291 fprintf(stderr
, "%s: Cannot open output file %s. Error was %s.\n",
292 prog_name
, output_file
, strerror(errno
));
296 if(fwrite(output_buf
, 1, CODEPAGE_HEADER_SIZE
+ (num_lines
*4), fp
) !=
297 CODEPAGE_HEADER_SIZE
+ (num_lines
*4))
299 fprintf(stderr
, "%s: Cannot write output file %s. Error was %s.\n",
300 prog_name
, output_file
, strerror(errno
));
311 * Placeholder for now.
314 static int do_decompile( int codepage
, char *input_file
, char *output_file
)
318 char header_buf
[CODEPAGE_HEADER_SIZE
];
324 /* Get the size of the input file. Read the entire thing into memory. */
325 if(sys_stat((char *)input_file
, &st
)!= 0)
327 fprintf(stderr
, "%s: failed to get the file size for file %s. Error was %s\n",
328 prog_name
, input_file
, strerror(errno
));
332 size
= (size_t)st
.st_size
;
334 if( size
< CODEPAGE_HEADER_SIZE
|| size
> (CODEPAGE_HEADER_SIZE
+ 256))
336 fprintf(stderr
, "%s: file %s is an incorrect size for a \
337 code page file.\n", prog_name
, input_file
);
341 /* Read the first 8 bytes of the codepage file - check
342 the version number and code page number. All the data
343 is held in little endian format.
346 if((fp
= sys_fopen( input_file
, "r")) == NULL
)
348 fprintf(stderr
, "%s: cannot open file %s. Error was %s\n",
349 prog_name
, input_file
, strerror(errno
));
353 if(fread( header_buf
, 1, CODEPAGE_HEADER_SIZE
, fp
)!=CODEPAGE_HEADER_SIZE
)
355 fprintf(stderr
, "%s: cannot read header from file %s. Error was %s\n",
356 prog_name
, input_file
, strerror(errno
));
360 /* Check the version value */
361 if(SVAL(header_buf
,CODEPAGE_VERSION_OFFSET
) != CODEPAGE_FILE_VERSION_ID
)
363 fprintf(stderr
, "%s: filename %s has incorrect version id. \
364 Needed %hu, got %hu.\n",
365 prog_name
, input_file
, (uint16
)CODEPAGE_FILE_VERSION_ID
,
366 SVAL(header_buf
,CODEPAGE_VERSION_OFFSET
));
370 /* Check the codepage matches */
371 if(SVAL(header_buf
,CODEPAGE_CLIENT_CODEPAGE_OFFSET
) != (uint16
)codepage
)
373 fprintf(stderr
, "%s: filename %s has incorrect codepage. \
374 Needed %hu, got %hu.\n",
375 prog_name
, input_file
, (uint16
)codepage
,
376 SVAL(header_buf
,CODEPAGE_CLIENT_CODEPAGE_OFFSET
));
380 /* Check the length is correct. */
381 if(IVAL(header_buf
,CODEPAGE_LENGTH_OFFSET
) !=
382 (unsigned int)(size
- CODEPAGE_HEADER_SIZE
))
384 fprintf(stderr
, "%s: filename %s has incorrect size headers. \
385 Needed %u, got %u.\n", prog_name
, input_file
, size
- CODEPAGE_HEADER_SIZE
,
386 IVAL(header_buf
,CODEPAGE_LENGTH_OFFSET
));
390 size
-= CODEPAGE_HEADER_SIZE
; /* Remove header */
392 /* Make sure the size is a multiple of 4. */
395 fprintf(stderr
, "%s: filename %s has a codepage size not a \
396 multiple of 4.\n", prog_name
, input_file
);
400 /* Allocate space for the code page file and read it all in. */
401 if((buf
= (char *)malloc( size
)) == NULL
)
403 fprintf (stderr
, "%s: malloc fail for size %d.\n",
408 if(fread( buf
, 1, size
, fp
)!=size
)
410 fprintf(stderr
, "%s: read fail on file %s. Error was %s.\n",
411 prog_name
, input_file
, strerror(errno
));
417 /* Now dump the codepage into an ascii file. */
418 if((fp
= sys_fopen(output_file
, "w")) == NULL
)
420 fprintf(stderr
, "%s: cannot open file %s. Error was %s\n",
421 prog_name
, output_file
, strerror(errno
));
425 fprintf(fp
, "#\n# Codepage definition file for IBM Code Page %d.\n#\n",
427 fprintf(fp
, "# This file was automatically generated.\n#\n");
428 fprintf(fp
, "# defines lower->upper mapping.\n");
429 fprintf(fp
, "#\n#The columns are :\n# lower\tupper\tu-t-l\tl-t-u\n#\n");
431 num_lines
= size
/ 4;
432 for( i
= 0; i
< num_lines
; i
++)
434 fprintf(fp
, "0x%02X\t0x%02X\t%s\t%s\n", CVAL(buf
, (i
*4)), CVAL(buf
, (i
*4)+1),
435 CVAL(buf
, (i
*4)+2) ? "True" : "False",
436 CVAL(buf
, (i
*4)+3) ? "True" : "False");
442 int main(int argc
, char **argv
)
445 char *input_file
= NULL
;
446 char *output_file
= NULL
;
447 BOOL compile
= False
;
452 codepage_usage(prog_name
);
454 if(argv
[1][0] != 'c' && argv
[1][0] != 'C' && argv
[1][0] != 'd' &&
456 codepage_usage(prog_name
);
458 input_file
= argv
[3];
459 output_file
= argv
[4];
461 /* Are we compiling or decompiling. */
462 if(argv
[1][0] == 'c' || argv
[1][0] == 'C')
465 /* Convert the second argument into a client codepage value. */
466 if((codepage
= atoi(argv
[2])) == 0)
468 fprintf(stderr
, "%s: %s is not a valid codepage.\n", prog_name
, argv
[2]);
473 return do_compile( codepage
, input_file
, output_file
);
475 return do_decompile( codepage
, input_file
, output_file
);