From 92faeaeea841dd0bce9460d25429846ae2fdc0af Mon Sep 17 00:00:00 2001 From: Andrew Bartlett Date: Wed, 16 Feb 2011 16:30:56 +1100 Subject: charset Remove use of {isupper,islower,toupper,tolower}_w functions These now call the common _m functions that consider UTF16 code points. This removes the code which will make up a 'lame' table in memory, as this can just as correctly be handled by running the algorithm at runtime (which is to call toupper() and tolower() on characters < 128). When used, a top level waf build will always locate the correct table - in the build tree or outside - due to relinking the installed binary. Andrew Bartlett --- source3/lib/util_unistr.c | 171 ++++------------------------------------------ 1 file changed, 14 insertions(+), 157 deletions(-) (limited to 'source3/lib/util_unistr.c') diff --git a/source3/lib/util_unistr.c b/source3/lib/util_unistr.c index 4cda38dc19..6199a35fc8 100644 --- a/source3/lib/util_unistr.c +++ b/source3/lib/util_unistr.c @@ -27,11 +27,7 @@ /* these 3 tables define the unicode case handling. They are loaded at startup either via mmap() or read() from the lib directory */ -static smb_ucs2_t *upcase_table; -static smb_ucs2_t *lowcase_table; static uint8 *valid_table; -static bool upcase_table_use_unmap; -static bool lowcase_table_use_unmap; static bool valid_table_use_unmap; static bool initialized; @@ -40,20 +36,6 @@ static bool initialized; **/ void gfree_case_tables(void) { - if ( upcase_table ) { - if ( upcase_table_use_unmap ) - unmap_file(upcase_table, 0x20000); - else - SAFE_FREE(upcase_table); - } - - if ( lowcase_table ) { - if ( lowcase_table_use_unmap ) - unmap_file(lowcase_table, 0x20000); - else - SAFE_FREE(lowcase_table); - } - if ( valid_table ) { if ( valid_table_use_unmap ) unmap_file(valid_table, 0x10000); @@ -63,99 +45,6 @@ void gfree_case_tables(void) initialized = false; } -/** - * Load or generate the case handling tables. - * - * The case tables are defined in UCS2 and don't depend on any - * configured parameters, so they never need to be reloaded. - **/ - -void load_case_tables(void) -{ - char *old_locale = NULL, *saved_locale = NULL; - int i; - TALLOC_CTX *frame = NULL; - - if (initialized) { - return; - } - initialized = true; - - frame = talloc_stackframe(); - - upcase_table = (smb_ucs2_t *)map_file(data_path("upcase.dat"), - 0x20000); - upcase_table_use_unmap = ( upcase_table != NULL ); - - lowcase_table = (smb_ucs2_t *)map_file(data_path("lowcase.dat"), - 0x20000); - lowcase_table_use_unmap = ( lowcase_table != NULL ); - -#ifdef HAVE_SETLOCALE - /* Get the name of the current locale. */ - old_locale = setlocale(LC_ALL, NULL); - - if (old_locale) { - /* Save it as it is in static storage. */ - saved_locale = SMB_STRDUP(old_locale); - } - - /* We set back the locale to C to get ASCII-compatible toupper/lower functions. */ - setlocale(LC_ALL, "C"); -#endif - - /* we would like Samba to limp along even if these tables are - not available */ - if (!upcase_table) { - DEBUG(1,("creating lame upcase table\n")); - upcase_table = (smb_ucs2_t *)SMB_MALLOC(0x20000); - if (!upcase_table) { - smb_panic("lame upcase table malloc fail"); - /* notreached. */ - return; - } - for (i=0;i<0x10000;i++) { - smb_ucs2_t v; - SSVAL(&v, 0, i); - upcase_table[v] = i; - } - for (i=0;i<256;i++) { - smb_ucs2_t v; - SSVAL(&v, 0, UCS2_CHAR(i)); - upcase_table[v] = UCS2_CHAR(islower(i)?toupper(i):i); - } - } - - if (!lowcase_table) { - DEBUG(1,("creating lame lowcase table\n")); - lowcase_table = (smb_ucs2_t *)SMB_MALLOC(0x20000); - if (!lowcase_table) { - smb_panic("lame lowcase table malloc fail"); - /* notreached. */ - return; - } - for (i=0;i<0x10000;i++) { - smb_ucs2_t v; - SSVAL(&v, 0, i); - lowcase_table[v] = i; - } - for (i=0;i<256;i++) { - smb_ucs2_t v; - SSVAL(&v, 0, UCS2_CHAR(i)); - lowcase_table[v] = UCS2_CHAR(isupper(i)?tolower(i):i); - } - } - -#ifdef HAVE_SETLOCALE - /* Restore the old locale. */ - if (saved_locale) { - setlocale (LC_ALL, saved_locale); - SAFE_FREE(saved_locale); - } -#endif - TALLOC_FREE(frame); -} - static int check_dos_char_slowly(smb_ucs2_t c) { char buf[10]; @@ -287,42 +176,6 @@ int rpcstr_push_talloc(TALLOC_CTX *ctx, smb_ucs2_t **dest, const char *src) return -1; } -/******************************************************************* - Convert a wchar to upper case. -********************************************************************/ - -smb_ucs2_t toupper_w(smb_ucs2_t val) -{ - return upcase_table[SVAL(&val,0)]; -} - -/******************************************************************* - Convert a wchar to lower case. -********************************************************************/ - -smb_ucs2_t tolower_w( smb_ucs2_t val ) -{ - return lowcase_table[SVAL(&val,0)]; -} - -/******************************************************************* - Determine if a character is lowercase. -********************************************************************/ - -bool islower_w(smb_ucs2_t c) -{ - return upcase_table[SVAL(&c,0)] != c; -} - -/******************************************************************* - Determine if a character is uppercase. -********************************************************************/ - -bool isupper_w(smb_ucs2_t c) -{ - return lowcase_table[SVAL(&c,0)] != c; -} - /******************************************************************* Determine if a character is valid in a 8.3 name. ********************************************************************/ @@ -466,6 +319,8 @@ smb_ucs2_t *strstr_w(const smb_ucs2_t *s, const smb_ucs2_t *ins) /******************************************************************* Convert a string to lower case. return True if any char is converted + + This is unsafe for any string involving a UTF16 character ********************************************************************/ bool strlower_w(smb_ucs2_t *s) @@ -474,7 +329,7 @@ bool strlower_w(smb_ucs2_t *s) bool ret = False; while (*(COPY_UCS2_CHAR(&cp,s))) { - smb_ucs2_t v = tolower_w(cp); + smb_ucs2_t v = tolower_m(cp); if (v != cp) { COPY_UCS2_CHAR(s,&v); ret = True; @@ -487,6 +342,8 @@ bool strlower_w(smb_ucs2_t *s) /******************************************************************* Convert a string to upper case. return True if any char is converted + + This is unsafe for any string involving a UTF16 character ********************************************************************/ bool strupper_w(smb_ucs2_t *s) @@ -494,7 +351,7 @@ bool strupper_w(smb_ucs2_t *s) smb_ucs2_t cp; bool ret = False; while (*(COPY_UCS2_CHAR(&cp,s))) { - smb_ucs2_t v = toupper_w(cp); + smb_ucs2_t v = toupper_m(cp); if (v != cp) { COPY_UCS2_CHAR(s,&v); ret = True; @@ -552,11 +409,11 @@ int strcasecmp_w(const smb_ucs2_t *a, const smb_ucs2_t *b) { smb_ucs2_t cpa, cpb; - while ((*COPY_UCS2_CHAR(&cpb,b)) && toupper_w(*(COPY_UCS2_CHAR(&cpa,a))) == toupper_w(cpb)) { + while ((*COPY_UCS2_CHAR(&cpb,b)) && toupper_m(*(COPY_UCS2_CHAR(&cpa,a))) == toupper_m(cpb)) { a++; b++; } - return (tolower_w(*(COPY_UCS2_CHAR(&cpa,a))) - tolower_w(*(COPY_UCS2_CHAR(&cpb,b)))); + return (tolower_m(*(COPY_UCS2_CHAR(&cpa,a))) - tolower_m(*(COPY_UCS2_CHAR(&cpb,b)))); } /******************************************************************* @@ -568,12 +425,12 @@ int strncasecmp_w(const smb_ucs2_t *a, const smb_ucs2_t *b, size_t len) smb_ucs2_t cpa, cpb; size_t n = 0; - while ((n < len) && *COPY_UCS2_CHAR(&cpb,b) && (toupper_w(*(COPY_UCS2_CHAR(&cpa,a))) == toupper_w(cpb))) { + while ((n < len) && *COPY_UCS2_CHAR(&cpb,b) && (toupper_m(*(COPY_UCS2_CHAR(&cpa,a))) == toupper_m(cpb))) { a++; b++; n++; } - return (len - n)?(tolower_w(*(COPY_UCS2_CHAR(&cpa,a))) - tolower_w(*(COPY_UCS2_CHAR(&cpb,b)))):0; + return (len - n)?(tolower_m(*(COPY_UCS2_CHAR(&cpa,a))) - tolower_m(*(COPY_UCS2_CHAR(&cpb,b)))):0; } /******************************************************************* @@ -831,7 +688,7 @@ smb_ucs2_t *strstr_wa(const smb_ucs2_t *s, const char *ins) int toupper_ascii(int c) { - smb_ucs2_t uc = toupper_w(UCS2_CHAR(c)); + smb_ucs2_t uc = toupper_m(UCS2_CHAR(c)); return UCS2_TO_CHAR(uc); } @@ -841,7 +698,7 @@ int toupper_ascii(int c) int tolower_ascii(int c) { - smb_ucs2_t uc = tolower_w(UCS2_CHAR(c)); + smb_ucs2_t uc = tolower_m(UCS2_CHAR(c)); return UCS2_TO_CHAR(uc); } @@ -851,7 +708,7 @@ int tolower_ascii(int c) int isupper_ascii(int c) { - return isupper_w(UCS2_CHAR(c)); + return isupper_m(UCS2_CHAR(c)); } /************************************************************* @@ -860,5 +717,5 @@ int isupper_ascii(int c) int islower_ascii(int c) { - return islower_w(UCS2_CHAR(c)); + return islower_m(UCS2_CHAR(c)); } -- cgit