add WITH_SENDFILE profiling data (from Pierre Belanger)
[Samba.git] / source / utils / make_smbcodepage.c
blob256dde7c0d15773ff7fabd232759cdc7f6bbdac1
1 /*
2 Unix SMB/Netbios implementation.
3 Version 1.9.
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.
23 #include "includes.h"
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",
34 progname);
35 exit(1);
39 * Read a line from a buffer into a line buffer. Ensure null
40 * terminated.
43 static void read_line( char **buf, char *line_buf, int size)
45 char *p = *buf;
46 int num = 0;
48 for(; *p && (*p != '\n'); p++)
50 if(num < (size - 1))
51 line_buf[num++] = *p;
53 if(*p)
54 p++; /* Go past the '\n' */
55 line_buf[num] = '\0';
56 *buf = p;
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)
67 pstring linebuf;
68 char *p = *buf;
69 int num_lines = 0;
70 char *newbuf = (char *)malloc( *size + 1);
71 char *newbuf_p = NULL;
73 if(newbuf == NULL)
75 fprintf(stderr, "%s: malloc fail for size %d.\n", prog_name, *size + 1);
76 exit(1);
79 newbuf_p = newbuf;
80 *newbuf_p = '\0';
82 while( *p )
84 char *cp;
86 read_line( &p, linebuf, sizeof(linebuf));
87 /* Null terminate after comment. */
88 if((cp = strchr( linebuf, '#'))!= NULL)
89 *cp = '\0';
91 for(cp = linebuf;*cp && isspace(*cp); cp++)
94 if(*cp == '\0')
95 continue;
97 safe_strcpy(newbuf_p, cp, *size - (newbuf_p - newbuf));
98 num_lines++;
99 newbuf_p += (strlen(newbuf_p) + 1);
102 SAFE_FREE(*buf);
103 *buf = newbuf;
104 return num_lines;
108 * Parse a byte from a codepage file.
111 static BOOL parse_byte(char *buf, unsigned char *bp)
113 unsigned int b;
114 char *endptr = NULL;
116 b = (unsigned int)strtol(buf, &endptr, 0);
117 if(endptr == buf || b > 255)
118 return False;
120 *bp = (unsigned char)b;
121 return True;
125 * Parse a bool from a codepage file.
128 static BOOL parse_bool(char *buf, unsigned char *bp)
130 if(isdigit((int)*buf))
132 char *endptr = NULL;
134 *bp = (unsigned char)strtol(buf, &endptr, 0);
135 if(endptr == buf )
136 return False;
137 if(*bp != 0)
138 *bp = 1;
139 } else {
140 if(strcasecmp(buf, "True") && strcasecmp(buf, "False"))
141 return False;
142 if(strcasecmp(buf, "True")==0)
143 *bp = 1;
144 else
145 *bp = 0;
147 return True;
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,
157 msg, buf);
158 exit(1);
162 * Create a compiled codepage file from a codepage definition file.
165 static int do_compile(int codepage, char *input_file, char *output_file)
167 FILE *fp = NULL;
168 size_t size = 0;
169 char *buf = NULL;
170 char *orig_buf = NULL;
171 char output_buf[CODEPAGE_HEADER_SIZE + 4 * MAXCODEPAGELINES];
172 int num_lines = 0;
173 int i = 0;
174 SMB_STRUCT_STAT st;
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));
181 exit(1);
184 size = (uint32)st.st_size;
186 /* I don't believe these things should be bigger than 100k :-) */
187 if(size > 100*1024)
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);
191 exit(1);
194 if((fp = sys_fopen(input_file, "r")) == NULL)
196 fprintf(stderr, "%s: cannot open file %s for input.\n", prog_name, input_file);
197 exit(1);
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);
204 fclose(fp);
205 exit(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));
212 SAFE_FREE(buf);
213 fclose(fp);
214 exit(1);
217 /* Null terminate the text read. */
218 buf[size] = '\0';
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
222 the codepage data.
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);
234 exit(1);
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++)
245 char token_buf[512];
246 char *p = buf;
247 unsigned char b = 0;
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));
293 exit(1);
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));
301 exit(1);
304 fclose(fp);
306 SAFE_FREE(orig_buf);
307 return 0;
311 * Placeholder for now.
314 static int do_decompile( int codepage, char *input_file, char *output_file)
316 size_t size = 0;
317 SMB_STRUCT_STAT st;
318 char header_buf[CODEPAGE_HEADER_SIZE];
319 char *buf = NULL;
320 FILE *fp = NULL;
321 int num_lines = 0;
322 int i = 0;
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));
329 exit(1);
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);
338 exit(1);
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));
350 exit(1);
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));
357 exit(1);
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));
367 exit(1);
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));
377 exit(1);
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));
387 exit(1);
390 size -= CODEPAGE_HEADER_SIZE; /* Remove header */
392 /* Make sure the size is a multiple of 4. */
393 if((size % 4 ) != 0)
395 fprintf(stderr, "%s: filename %s has a codepage size not a \
396 multiple of 4.\n", prog_name, input_file);
397 exit(1);
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",
404 prog_name, size );
405 exit(1);
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));
412 exit(1);
415 fclose(fp);
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));
422 exit(1);
425 fprintf(fp, "#\n# Codepage definition file for IBM Code Page %d.\n#\n",
426 codepage);
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");
438 fclose(fp);
439 return 0;
442 int main(int argc, char **argv)
444 int codepage = 0;
445 char *input_file = NULL;
446 char *output_file = NULL;
447 BOOL compile = False;
449 prog_name = argv[0];
451 if(argc != 5)
452 codepage_usage(prog_name);
454 if(argv[1][0] != 'c' && argv[1][0] != 'C' && argv[1][0] != 'd' &&
455 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')
463 compile = True;
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]);
469 exit(1);
472 if(compile)
473 return do_compile( codepage, input_file, output_file);
474 else
475 return do_decompile( codepage, input_file, output_file);