Adding codepage definition files and codepage file compiler/decompiler.
authorJeremy Allison <jra@samba.org>
Sat, 30 Aug 1997 01:13:39 +0000 (01:13 +0000)
committerJeremy Allison <jra@samba.org>
Sat, 30 Aug 1997 01:13:39 +0000 (01:13 +0000)
Adding install/uninstall scripts for compiled codepage files.
Preparing for dynamic codepage loading.
Jeremy (jallison@whistle.com)
(This used to be commit e3fc9533bd87d0d520e3dac98261e42370907b11)

source3/codepages/codepage_def.437 [new file with mode: 0644]
source3/codepages/codepage_def.850 [new file with mode: 0644]
source3/codepages/codepage_def.852 [new file with mode: 0644]
source3/codepages/codepage_def.932 [new file with mode: 0644]
source3/script/installcp.sh [new file with mode: 0755]
source3/script/uninstallcp.sh [new file with mode: 0755]
source3/utils/make_smbcodepage.c [new file with mode: 0644]

diff --git a/source3/codepages/codepage_def.437 b/source3/codepages/codepage_def.437
new file mode 100644 (file)
index 0000000..d357c07
--- /dev/null
@@ -0,0 +1,70 @@
+#
+#   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.
+#
+
+# Codepage definition file for IBM Code Page 437 - MS-DOS Latin US
+# defines lower->upper mapping.
+# Written by Jeremy Allison <jallison@whistle.com>
+
+# The columns are :
+# lower    upper   map upper to lower    map lower to upper
+#
+   0x87     0x80   True                  True
+   0x81     0x9A   True                  True
+   0x82     0x90   True                  True
+   0x83     0x41   True                  False
+   0x84     0x8E   True                  True
+   0x85     0x41   True                  False
+   0x86     0x8F   True                  True
+   0x88     0x45   True                  False
+   0x89     0x45   True                  False
+   0x8A     0x45   True                  False
+   0x8B     0x49   True                  False
+   0x8C     0x49   True                  False
+   0x8D     0x49   True                  False
+   0x91     0x92   True                  True
+   0x93     0x4F   True                  False
+   0x94     0x99   True                  True
+   0x95     0x4F   True                  False
+   0x96     0x55   True                  False
+   0x97     0x55   True                  False
+   0x9B     0      False                 False
+   0x9C     0      False                 False
+   0x9D     0      False                 False
+   0xA0     0x41   True                  False
+   0xA1     0x49   True                  False
+   0xA2     0x4F   True                  False
+   0xA3     0x55   True                  False
+   0xA4     0xA5   True                  True
+   0xA8     0      False                 False
+   0xAD     0      False                 False
+   0xAE     0      False                 False
+   0xAF     0      False                 False
+   0xE0     0      False                 False
+   0xE1     0      False                 False
+   0xE2     0      False                 False
+   0xE3     0      False                 False
+   0xE4     0      False                 False
+   0xE5     0      False                 False
+   0xE6     0      False                 False
+   0xE7     0      False                 False
+   0xE8     0      False                 False
+   0xE9     0      False                 False
+   0xEA     0      False                 False
+   0xEB     0      False                 False
+   0xEC     0      False                 False
+   0xED     0      False                 False
+   0xEE     0      False                 False
+   0xEF     0      False                 False
diff --git a/source3/codepages/codepage_def.850 b/source3/codepages/codepage_def.850
new file mode 100644 (file)
index 0000000..e2466a7
--- /dev/null
@@ -0,0 +1,54 @@
+#
+#   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.
+#
+
+# Codepage definition file for IBM Code Page 850 - MS-DOS Latin 1
+# defines lower->upper mapping.
+# Written by Jeremy Allison (jallison@whistle.com)
+
+# The columns are :
+# lower    upper   map upper to lower    map lower to upper
+#
+  0x85     0xB7    True                  True
+  0xA0     0xB5    True                  True
+  0x83     0xB6    True                  True
+  0xC6     0xC7    True                  True
+  0x84     0x8E    True                  True
+  0x86     0x8F    True                  True
+  0x91     0x92    True                  True
+  0x87     0x80    True                  True
+  0x8A     0xD4    True                  True
+  0x82     0x90    True                  True
+  0x88     0xD2    True                  True
+  0x89     0xD3    True                  True
+  0x8D     0xDE    True                  True
+  0xA1     0xD6    True                  True
+  0x8C     0xD7    True                  True
+  0x8B     0xD8    True                  True
+  0xD0     0xD1    True                  True
+  0xA4     0xA5    True                  True
+  0x95     0xE3    True                  True
+  0xA2     0xE0    True                  True
+  0x93     0xE2    True                  True
+  0xE4     0xE5    True                  True
+  0x94     0x99    True                  True
+  0x9B     0x9D    True                  True
+  0x97     0xEB    True                  True
+  0xA3     0xE9    True                  True
+  0x96     0xEA    True                  True
+  0x81     0x9A    True                  True
+  0xEC     0xED    True                  True
+  0xE7     0xE8    True                  True
+  0x9C     0       False                 False
diff --git a/source3/codepages/codepage_def.852 b/source3/codepages/codepage_def.852
new file mode 100644 (file)
index 0000000..ed14234
--- /dev/null
@@ -0,0 +1,63 @@
+#
+#   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.
+#
+
+# Codepage definition file for IBM Code Page 852 - MS-DOS Latin 2
+# defines lower->upper mapping.
+# Written by Leos Bitto <bitto@altec.cz>
+
+# The columns are :
+# lower    upper   map upper to lower    map lower to upper
+#
+   0x81    0x9A    True                  True
+   0x82    0x90    True                  True
+   0x83    0xB6    True                  True
+   0x84    0x8E    True                  True
+   0x85    0xDE    True                  True
+   0x86    0x8F    True                  True
+   0x87    0x80    True                  True
+   0x88    0x9D    True                  True
+   0x89    0xD3    True                  True
+   0x8B    0x8A    True                  True
+   0x8C    0xD7    True                  True
+   0x92    0x91    True                  True
+   0x93    0xE2    True                  True
+   0x94    0x99    True                  True
+   0x96    0x95    True                  True
+   0x98    0x97    True                  True
+   0x9C    0x9B    True                  True
+   0x9F    0xAC    True                  True
+   0xA0    0xB5    True                  True
+   0xA1    0xD6    True                  True
+   0xA2    0xE0    True                  True
+   0xA3    0xE9    True                  True
+   0xA5    0xA4    True                  True
+   0xA7    0xA6    True                  True
+   0xA9    0xA8    True                  True
+   0xAB    0x8D    True                  True
+   0xAD    0xB8    True                  True
+   0xBE    0xBD    True                  True
+   0xC7    0xC6    True                  True
+   0xD0    0xD1    True                  True
+   0xD4    0xD2    True                  True
+   0xD8    0xB7    True                  True
+   0xE4    0xE3    True                  True
+   0xE5    0xD5    True                  True
+   0xE7    0xE6    True                  True
+   0xEA    0xE8    True                  True
+   0xEC    0xED    True                  True
+   0xEE    0xDD    True                  True
+   0xFB    0xEB    True                  True
+   0xFD    0xFC    True                  True
diff --git a/source3/codepages/codepage_def.932 b/source3/codepages/codepage_def.932
new file mode 100644 (file)
index 0000000..8d9ff63
--- /dev/null
@@ -0,0 +1,24 @@
+#
+#   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.
+#
+
+# Codepage definition file for IBM Code Page 932 - MS-DOS Japanese SJIS
+# defines lower->upper mapping.
+# Written by Jeremy Allison <jallison@whistle.com>
+
+# The columns are :
+# lower    upper   map upper to lower    map lower to upper
+#
+# This file is intentionaly empty - no mappings are done.
\ No newline at end of file
diff --git a/source3/script/installcp.sh b/source3/script/installcp.sh
new file mode 100755 (executable)
index 0000000..bafd841
--- /dev/null
@@ -0,0 +1,36 @@
+#!/bin/sh
+LIBDIR=$1
+CODEPAGEDIR=$2
+BINDIR=$3
+
+shift
+shift
+shift
+
+echo Installing codepage files in $CODEPAGEDIR
+for d in $LIBDIR $CODEPAGEDIR; do
+if [ ! -d $d ]; then
+mkdir $d
+if [ ! -d $d ]; then
+  echo Failed to make directory $d
+  exit 1
+fi
+fi
+done
+
+for p in $*; do
+ echo Creating codepage file $CODEPAGEDIR/codepage.$p from codepage_def.$p
+ $BINDIR/make_smbcodepage c $p codepage_def.$p $CODEPAGEDIR/codepage.$p
+done
+
+
+cat << EOF
+======================================================================
+The code pages have been installed. You may uninstall them using the command
+the command "make uninstallcp" or make "uninstall" to uninstall binaries,
+man pages, shell scripts and code pages.
+======================================================================
+EOF
+
+exit 0
+
diff --git a/source3/script/uninstallcp.sh b/source3/script/uninstallcp.sh
new file mode 100755 (executable)
index 0000000..bd7013c
--- /dev/null
@@ -0,0 +1,33 @@
+#!/bin/sh
+
+CPDIR=$1
+shift
+
+if [ ! -d $CPDIR ]; then
+  echo Directory $CPDIR does not exist!
+  echo Do a "make installcp" or "make install" first.
+  exit 1
+fi
+
+for p in $*; do
+  if [ ! -f $CPDIR/codepage.$p ]; then
+    echo $CPDIR/codepage.$p does not exist!
+  else
+    echo Removing $CPDIR/codepage.$p
+    rm -f $CPDIR/codepage.$p
+    if [ -f $CPDIR/codepage.$p ]; then
+      echo Cannot remove $CPDIR/codepage.$p... does $USER have privileges?
+    fi
+  fi
+done
+
+cat << EOF
+======================================================================
+The code pages have been uninstalled. You may reinstall them using
+the command "make installcp" or "make install" to install binaries,
+man pages, shell scripts and code pages. You may recover a previous version 
+(if any with "make revert").
+======================================================================
+EOF
+
+exit 0
diff --git a/source3/utils/make_smbcodepage.c b/source3/utils/make_smbcodepage.c
new file mode 100644 (file)
index 0000000..c8b4fea
--- /dev/null
@@ -0,0 +1,472 @@
+/* 
+   Unix SMB/Netbios implementation.
+   Version 1.9.
+   Create codepage files from codepage_def.XXX files.
+
+   Copyright (C) Jeremy Allison 1997
+
+   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.
+ */
+
+void codepage_usage(const char *progname)
+{
+  fprintf(stderr, "Usage is : %s [c|d] <codepage> <inputfile> <outputfile>\n",
+         progname);
+  exit(1);
+}
+
+/*
+ * Read a line from a buffer into a line buffer. Ensure null
+ * terminated.
+ */
+
+void read_line( char **buf, char *line_buf, int size)
+{
+  char *p = *buf;
+  int num = 0;
+
+  for(; *p && (*p != '\n'); 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.
+ */
+
+int clean_data( char **buf, uint32 *size)
+{
+  char linebuf[512];
+  char *p = *buf;
+  int num_lines = 0;
+  char *newbuf = (char *)malloc( *size + 1);
+  char *newbuf_p = NULL;
+
+  if(newbuf == NULL)
+  {
+    fprintf(stderr, "%s: malloc fail for size %d.\n", prog_name, *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;
+
+    strcpy(newbuf_p, cp);
+    num_lines++;
+    newbuf_p += (strlen(newbuf_p) + 1);
+  }
+
+  free(*buf);
+  *buf = newbuf;
+  return num_lines;
+}
+
+/*
+ * Parse a byte from a codepage file.
+ */
+
+BOOL parse_byte(char *buf, unsigned char *bp)
+{
+  unsigned int b;
+  char *endptr = NULL;
+
+  b = (unsigned int)strtol(buf, &endptr, 0);
+  if(endptr == buf || b > 255)
+    return False;
+
+  *bp = (unsigned char)b;
+  return True;
+}
+
+/*
+ * Parse a bool from a codepage file.
+ */
+
+BOOL parse_bool(char *buf, unsigned char *bp)
+{
+  if(isdigit(*buf))
+  {
+    char *endptr = NULL;
+
+    *bp = (unsigned char)strtol(buf, &endptr, 0);
+    if(endptr == buf )
+      return False;
+    if(*bp != 0)
+      *bp = 1;
+  } else {
+    if(strcasecmp(buf, "True") && strcasecmp(buf, "False"))
+      return False;
+    if(strcasecmp(buf, "True")==0)
+      *bp = 1;
+    else
+      *bp = 0;
+  }
+  return True;
+}
+
+/*
+ * Print a parse error and exit.
+ */
+
+void parse_error(char *buf, char *msg)
+{
+  fprintf(stderr, "%s: %s whilst parsing line \n%s\n", prog_name,
+          msg, buf);
+  exit(1);
+}
+    
+/*
+ * Create a compiled codepage file from a codepage definition file.
+ */
+
+int do_compile(int codepage, const char *input_file, const char *output_file)
+{
+  FILE *fp = NULL;
+  uint32 size = 0;
+  char *buf = NULL;
+  char output_buf[CODEPAGE_HEADER_SIZE + 512];
+  int num_lines = 0;
+  int i = 0;
+  struct stat st;
+
+  /* Get the size of the input file. Read the entire thing into memory. */
+  if(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 = (uint32)st.st_size;
+
+  /* I don't believe these things should be bigger than 100k :-) */
+  if(size > 100*1024)
+  {
+    fprintf(stderr, "%s: filesize %d is too large for a codepage definition file. \
+The maximum size I will believe is 100k.\n", prog_name, size);
+    exit(1);
+  }
+
+  if((fp = 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);
+
+  /* There can be a maximum of 128 lines. */
+  if(num_lines > 128)
+  {
+    fprintf(stderr, "%s: There can be a maximum 128 lines of data in a codepage \
+definition file. File %s has %d.\n", prog_name, input_file, num_lines);
+    exit(1);
+  }
+
+  /* Setup the output file header. */
+  SSVAL(output_buf,CODEPAGE_VERSION_OFFSET,CODEPAGE_FILE_VERSION_ID);
+  SSVAL(output_buf,CODEPAGE_CLIENT_CODEPAGE_OFFSET,(uint16)codepage);
+  SIVAL(output_buf,CODEPAGE_LENGTH_OFFSET,(num_lines * 4));
+
+  /* Now convert the lines into the compiled form. */
+  for(i = 0; i < num_lines; i++)
+  {
+    char token_buf[512];
+    char *p = buf;
+    unsigned char b = 0;
+
+    /* Get the 'lower' value. */
+    if(!next_token(&p, token_buf, NULL))
+      parse_error(buf, "cannot parse first value");
+    if(!parse_byte( token_buf, &b))
+      parse_error(buf, "first value doesn't resolve to a byte");
+
+    /* Add this to the output buffer. */
+    SCVAL(output_buf,CODEPAGE_HEADER_SIZE+(i*4),b);
+
+    /* Get the 'upper' value. */
+    if(!next_token(&p, token_buf, NULL))
+      parse_error(buf, "cannot parse second value");
+    if(!parse_byte( token_buf, &b))
+      parse_error(buf, "second value doesn't resolve to a byte");
+
+    /* Add this to the output buffer. */
+    SCVAL(output_buf,CODEPAGE_HEADER_SIZE+(i*4) + 1,b);
+
+    /* Get the 'upper to lower' value. */
+    if(!next_token(&p, token_buf, NULL))
+      parse_error(buf, "cannot parse third value");
+    if(!parse_bool( token_buf, &b))
+      parse_error(buf, "third value doesn't resolve to a boolean");
+
+    /* Add this to the output buffer. */
+    SCVAL(output_buf,CODEPAGE_HEADER_SIZE+(i*4) + 2,b);
+
+    /* Get the 'lower to upper' value. */
+    if(!next_token(&p, token_buf, NULL))
+      parse_error(buf, "cannot parse fourth value");
+    if(!parse_bool( token_buf, &b))
+      parse_error(buf, "fourth value doesn't resolve to a boolean");
+
+    /* Add this to the output buffer. */
+    SCVAL(output_buf,CODEPAGE_HEADER_SIZE+(i*4) + 3,b);
+
+    buf += (strlen(buf) + 1);
+  }
+
+  /* Now write out the output_buf. */
+  if((fp = 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, CODEPAGE_HEADER_SIZE + (num_lines*4), fp) !=
+         CODEPAGE_HEADER_SIZE + (num_lines*4))
+  {
+    fprintf(stderr, "%s: Cannot write output file %s. Error was %s.\n",
+            prog_name, output_file, strerror(errno));
+    exit(1);
+  }  
+
+  fclose(fp);
+
+  return 0;
+}
+
+/*
+ * Placeholder for now.
+ */
+
+int do_decompile( int codepage, const char *input_file, const char *output_file)
+{
+  uint32 size = 0;
+  struct stat st;
+  char header_buf[CODEPAGE_HEADER_SIZE];
+  char *buf = NULL;
+  FILE *fp = NULL;
+  int num_lines = 0;
+  int i = 0;
+
+  /* Get the size of the input file. Read the entire thing into memory. */
+  if(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 = (uint32)st.st_size;
+
+  if( size < CODEPAGE_HEADER_SIZE || size > (CODEPAGE_HEADER_SIZE + 256))
+  { 
+    fprintf(stderr, "%s: file %s is an incorrect size for a \
+code page file.\n", prog_name, input_file);
+    exit(1);
+  } 
+  
+  /* Read the first 8 bytes of the codepage file - check
+     the version number and code page number. All the data
+     is held in little endian format.
+   */
+    
+  if((fp = fopen( input_file, "r")) == NULL)
+  { 
+    fprintf(stderr, "%s: cannot open file %s. Error was %s\n",
+              prog_name, input_file, strerror(errno));
+    exit(1);
+  } 
+  
+  if(fread( header_buf, 1, CODEPAGE_HEADER_SIZE, fp)!=CODEPAGE_HEADER_SIZE)
+  { 
+    fprintf(stderr, "%s: cannot read header from file %s. Error was %s\n",
+              prog_name, input_file, strerror(errno));
+    exit(1);
+  } 
+  
+  /* Check the version value */
+  if(SVAL(header_buf,CODEPAGE_VERSION_OFFSET) != CODEPAGE_FILE_VERSION_ID)
+  { 
+    fprintf(stderr, "%s: filename %s has incorrect version id. \
+Needed %hu, got %hu.\n",
+          prog_name, input_file, (uint16)CODEPAGE_FILE_VERSION_ID, 
+          SVAL(header_buf,CODEPAGE_VERSION_OFFSET));
+    exit(1);
+  } 
+  
+  /* Check the codepage matches */
+  if(SVAL(header_buf,CODEPAGE_CLIENT_CODEPAGE_OFFSET) != (uint16)codepage)
+  { 
+    fprintf(stderr, "%s: filename %s has incorrect codepage. \
+Needed %hu, got %hu.\n",
+           prog_name, input_file, (uint16)codepage, 
+           SVAL(header_buf,CODEPAGE_CLIENT_CODEPAGE_OFFSET));
+    exit(1);
+  } 
+  
+  /* Check the length is correct. */
+  if(IVAL(header_buf,CODEPAGE_LENGTH_OFFSET) !=
+                 (unsigned int)(size - CODEPAGE_HEADER_SIZE))
+  { 
+    fprintf(stderr, "%s: filename %s has incorrect size headers. \
+Needed %u, got %u.\n", prog_name, input_file, size - CODEPAGE_HEADER_SIZE,
+               IVAL(header_buf,CODEPAGE_LENGTH_OFFSET));
+    exit(1);
+  } 
+  
+  size -= CODEPAGE_HEADER_SIZE; /* Remove header */
+    
+  /* Make sure the size is a multiple of 4. */
+  if((size % 4 ) != 0)
+  { 
+    fprintf(stderr, "%s: filename %s has a codepage size not a \
+multiple of 4.\n", prog_name, input_file);
+    exit(1);
+  } 
+  
+  /* Allocate space for the code page file and read it all in. */
+  if((buf = (char *)malloc( size )) == NULL)
+  { 
+    fprintf (stderr, "%s: malloc fail for size %d.\n",
+             prog_name, size );
+    exit(1);
+  } 
+  
+  if(fread( buf, 1, size, fp)!=size)
+  { 
+    fprintf(stderr, "%s: read fail on file %s. Error was %s.\n",
+              prog_name, input_file, strerror(errno));
+    exit(1);
+  } 
+  
+  fclose(fp);
+    
+  /* Now dump the codepage into an ascii file. */
+  if((fp = fopen(output_file, "w")) == NULL)
+  {
+    fprintf(stderr, "%s: cannot open file %s. Error was %s\n",
+              prog_name, output_file, strerror(errno));
+    exit(1);
+  } 
+
+  fprintf(fp, "#\n# Codepage definition file for IBM Code Page %d.\n#\n",
+          codepage);
+  fprintf(fp, "# This file was automatically generated.\n#\n");
+  fprintf(fp, "# defines lower->upper mapping.\n");
+  fprintf(fp, "#\n#The columns are :\n# lower\tupper\tu-t-l\tl-t-u\n#\n");
+
+  num_lines = size / 4;
+  for( i = 0; i < num_lines; i++)
+  {
+    fprintf(fp, "0x%02X\t0x%02X\t%s\t%s\n", CVAL(buf, (i*4)), CVAL(buf, (i*4)+1),
+           CVAL(buf, (i*4)+2) ? "True" : "False",
+           CVAL(buf, (i*4)+3) ? "True" : "False");
+  }
+  fclose(fp);
+  return 0;
+}
+
+int main(int argc, char **argv)
+{
+  int codepage = 0;
+  char *input_file = NULL;
+  char *output_file = NULL;
+  BOOL compile = False;
+
+  prog_name = argv[0];
+
+  if(argc != 5)
+    codepage_usage(prog_name);
+
+  if(argv[1][0] != 'c' && argv[1][0] != 'C' && argv[1][0] != 'd' &&
+     argv[1][0] != 'D')
+    codepage_usage(prog_name);
+
+  input_file = argv[3];
+  output_file = argv[4];
+
+  /* Are we compiling or decompiling. */
+  if(argv[1][0] == 'c' || argv[1][0] == 'C')
+    compile = True;
+  /* Convert the second argument into a client codepage value. */
+  if((codepage = atoi(argv[2])) == 0)
+  {
+    fprintf(stderr, "%s: %s is not a valid codepage.\n", prog_name, argv[2]);
+    exit(1);
+  }
+
+  if(compile)
+    return do_compile( codepage, input_file, output_file);
+  else
+    return do_decompile( codepage, input_file, output_file);
+}