More formatting changes. Mostly converted some DEBUG() calls to DEBUGADD()
[samba.git] / source3 / lib / charset.c
index 217f407b9ebd24860bf1183a9f5fdebec42b0e4d..5e9481f9bbf41dcff66f15acd60dc346b53fd678 100644 (file)
@@ -2,7 +2,7 @@
    Unix SMB/Netbios implementation.
    Version 1.9.
    Character set handling
-   Copyright (C) Andrew Tridgell 1992-1997
+   Copyright (C) Andrew Tridgell 1992-1998
    
    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
@@ -127,9 +127,10 @@ static void add_dos_char(int lower, BOOL map_lower_to_upper,
 {
   lower &= 0xff;
   upper &= 0xff;
-  DEBUG(6,("Adding chars 0x%x 0x%x (l->u = %s) (u->l = %s)\n",lower,upper,
-         map_lower_to_upper ? "True" : "False",
-         map_upper_to_lower ? "True" : "False"));
+  DEBUGADD( 6, ( "Adding chars 0x%x 0x%x (l->u = %s) (u->l = %s)\n",
+                 lower, upper,
+                 map_lower_to_upper ? "True" : "False",
+                 map_upper_to_lower ? "True" : "False" ) );
   if (lower) dos_char_map[lower] = 1;
   if (upper) dos_char_map[upper] = 1;
   lower_char_map[lower] = (char)lower; /* Define tolower(lower) */
@@ -145,7 +146,7 @@ static void add_dos_char(int lower, BOOL map_lower_to_upper,
 /****************************************************************************
 initialise the charset arrays
 ****************************************************************************/
-void charset_initialise()
+void charset_initialise(void)
 {
   int i;
 
@@ -167,8 +168,13 @@ void charset_initialise()
   for (i=0; i<=255; i++) {
     char c = (char)i;
     upper_char_map[i] = lower_char_map[i] = c;
-    if (isupper(c)) lower_char_map[i] = tolower(c);
-    if (islower(c)) upper_char_map[i] = toupper(c);
+
+    /* Some systems have buggy isupper/islower for characters
+       above 127. Best not to rely on them. */
+    if(i < 128) {
+      if (isupper(c)) lower_char_map[i] = tolower(c);
+      if (islower(c)) upper_char_map[i] = toupper(c);
+    }
   }
 }
 
@@ -195,10 +201,12 @@ static codepage_p load_client_codepage( int client_codepage )
     return NULL;
   }
 
-  strcpy(codepage_file_name, CODEPAGEDIR);
-  strcat(codepage_file_name, "/");
-  strcat(codepage_file_name, "codepage.");
-  sprintf( &codepage_file_name[strlen(codepage_file_name)], "%03d",
+  pstrcpy(codepage_file_name, CODEPAGEDIR);
+  pstrcat(codepage_file_name, "/");
+  pstrcat(codepage_file_name, "codepage.");
+  slprintf(&codepage_file_name[strlen(codepage_file_name)], 
+          sizeof(pstring)-(strlen(codepage_file_name)+1),
+          "%03d",
            client_codepage);
 
   if(!file_exist(codepage_file_name,&st))
@@ -211,11 +219,11 @@ static codepage_p load_client_codepage( int client_codepage )
   /* Check if it is at least big enough to hold the required
      data. Should be 2 byte version, 2 byte codepage, 4 byte length, 
      plus zero or more bytes of data. Note that the data cannot be more
-     than 512 bytes - giving a max size of 520.
+     than 4 * MAXCODEPAGELINES bytes.
    */
   size = (unsigned int)st.st_size;
 
-  if( size < CODEPAGE_HEADER_SIZE || size > (CODEPAGE_HEADER_SIZE + 256))
+  if( size < CODEPAGE_HEADER_SIZE || size > (CODEPAGE_HEADER_SIZE + 4 * MAXCODEPAGELINES))
   {
     DEBUG(0,("load_client_codepage: file %s is an incorrect size for a \
 code page file.\n", codepage_file_name));
@@ -318,10 +326,9 @@ initialise the client codepage.
 void codepage_initialise(int client_codepage)
 {
   int i;
-  codepage_p cp = NULL;
-  static BOOL done = False;
+  static codepage_p cp = NULL;
 
-  if(done == True) 
+  if(cp != NULL)
   {
     DEBUG(6,
       ("codepage_initialise: called twice - ignoring second client code page = %d\n",
@@ -343,21 +350,26 @@ void codepage_initialise(int client_codepage)
 for code page %d failed. Using default client codepage 932\n", 
              CODEPAGEDIR, client_codepage, client_codepage));
     cp = cp_932;
+    client_codepage = KANJI_CODEPAGE;
 #else /* KANJI */
     DEBUG(6,("codepage_initialise: loading dynamic codepage file %s/codepage.%d \
 for code page %d failed. Using default client codepage 850\n", 
              CODEPAGEDIR, client_codepage, client_codepage));
     cp = cp_850;
+    client_codepage = MSDOS_LATIN_1_CODEPAGE;
 #endif /* KANJI */
   }
 
+  /*
+   * Setup the function pointers for the loaded codepage.
+   */
+  initialize_multibyte_vectors( client_codepage );
+
   if(cp)
   {
     for(i = 0; !((cp[i][0] == '\0') && (cp[i][1] == '\0')); i++)
       add_dos_char(cp[i][0], (BOOL)cp[i][2], cp[i][1], (BOOL)cp[i][3]);
   }
-
-  done = True;
 }
 
 /*******************************************************************