Changeset 745 for trunk/server/source3/lib/util_unistr.c
- Timestamp:
- Nov 27, 2012, 4:43:17 PM (13 years ago)
- Location:
- trunk/server
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/server
- Property svn:mergeinfo changed
/vendor/current merged: 581,587,591,594,597,600,615,618,740
- Property svn:mergeinfo changed
-
trunk/server/source3/lib/util_unistr.c
r414 r745 22 22 #include "includes.h" 23 23 24 #ifndef MAXUNI25 #define MAXUNI 102426 #endif27 28 24 /* these 3 tables define the unicode case handling. They are loaded 29 25 at startup either via mmap() or read() from the lib directory */ 30 static smb_ucs2_t *upcase_table;31 static smb_ucs2_t *lowcase_table;32 26 static uint8 *valid_table; 33 static bool upcase_table_use_unmap;34 static bool lowcase_table_use_unmap;35 static bool valid_table_use_unmap;36 27 static bool initialized; 37 28 … … 41 32 void gfree_case_tables(void) 42 33 { 43 if ( upcase_table ) {44 if ( upcase_table_use_unmap )45 unmap_file(upcase_table, 0x20000);46 else47 SAFE_FREE(upcase_table);48 }49 50 if ( lowcase_table ) {51 if ( lowcase_table_use_unmap )52 unmap_file(lowcase_table, 0x20000);53 else54 SAFE_FREE(lowcase_table);55 }56 57 34 if ( valid_table ) { 58 if ( valid_table_use_unmap ) 59 unmap_file(valid_table, 0x10000); 60 else 61 SAFE_FREE(valid_table); 35 unmap_file(valid_table, 0x10000); 36 valid_table = NULL; 62 37 } 63 38 initialized = false; 64 }65 66 /**67 * Load or generate the case handling tables.68 *69 * The case tables are defined in UCS2 and don't depend on any70 * configured parameters, so they never need to be reloaded.71 **/72 73 void load_case_tables(void)74 {75 char *old_locale = NULL, *saved_locale = NULL;76 int i;77 TALLOC_CTX *frame = NULL;78 79 if (initialized) {80 return;81 }82 initialized = true;83 84 frame = talloc_stackframe();85 86 upcase_table = (smb_ucs2_t *)map_file(data_path("upcase.dat"),87 0x20000);88 upcase_table_use_unmap = ( upcase_table != NULL );89 90 lowcase_table = (smb_ucs2_t *)map_file(data_path("lowcase.dat"),91 0x20000);92 lowcase_table_use_unmap = ( lowcase_table != NULL );93 94 #ifdef HAVE_SETLOCALE95 /* Get the name of the current locale. */96 old_locale = setlocale(LC_ALL, NULL);97 98 if (old_locale) {99 /* Save it as it is in static storage. */100 saved_locale = SMB_STRDUP(old_locale);101 }102 103 /* We set back the locale to C to get ASCII-compatible toupper/lower functions. */104 setlocale(LC_ALL, "C");105 #endif106 107 /* we would like Samba to limp along even if these tables are108 not available */109 if (!upcase_table) {110 DEBUG(1,("creating lame upcase table\n"));111 upcase_table = (smb_ucs2_t *)SMB_MALLOC(0x20000);112 for (i=0;i<0x10000;i++) {113 smb_ucs2_t v;114 SSVAL(&v, 0, i);115 upcase_table[v] = i;116 }117 for (i=0;i<256;i++) {118 smb_ucs2_t v;119 SSVAL(&v, 0, UCS2_CHAR(i));120 upcase_table[v] = UCS2_CHAR(islower(i)?toupper(i):i);121 }122 }123 124 if (!lowcase_table) {125 DEBUG(1,("creating lame lowcase table\n"));126 lowcase_table = (smb_ucs2_t *)SMB_MALLOC(0x20000);127 for (i=0;i<0x10000;i++) {128 smb_ucs2_t v;129 SSVAL(&v, 0, i);130 lowcase_table[v] = i;131 }132 for (i=0;i<256;i++) {133 smb_ucs2_t v;134 SSVAL(&v, 0, UCS2_CHAR(i));135 lowcase_table[v] = UCS2_CHAR(isupper(i)?tolower(i):i);136 }137 }138 139 #ifdef HAVE_SETLOCALE140 /* Restore the old locale. */141 if (saved_locale) {142 setlocale (LC_ALL, saved_locale);143 SAFE_FREE(saved_locale);144 }145 #endif146 TALLOC_FREE(frame);147 }148 149 static int check_dos_char_slowly(smb_ucs2_t c)150 {151 char buf[10];152 smb_ucs2_t c2 = 0;153 int len1, len2;154 155 len1 = convert_string(CH_UTF16LE, CH_DOS, &c, 2, buf, sizeof(buf),False);156 if (len1 == 0) {157 return 0;158 }159 len2 = convert_string(CH_DOS, CH_UTF16LE, buf, len1, &c2, 2,False);160 if (len2 != 2) {161 return 0;162 }163 return (c == c2);164 39 } 165 40 … … 173 48 **/ 174 49 175 void init_valid_table(void) 176 { 177 static int mapped_file; 178 int i; 179 const char *allowed = ".!#$%&'()_-@^`~"; 180 uint8 *valid_file; 181 182 if (mapped_file) { 183 /* Can't unmap files, so stick with what we have */ 50 static void init_valid_table(void) 51 { 52 if (valid_table) { 184 53 return; 185 54 } 186 55 187 valid_file = (uint8 *)map_file(data_path("valid.dat"), 0x10000); 188 if (valid_file) { 189 valid_table = valid_file; 190 mapped_file = 1; 191 valid_table_use_unmap = True; 56 valid_table = (uint8 *)map_file(data_path("valid.dat"), 0x10000); 57 if (!valid_table) { 58 smb_panic("Could not load valid.dat file required for mangle method=hash"); 192 59 return; 193 }194 195 /* Otherwise, we're using a dynamically created valid_table.196 * It might need to be regenerated if the code page changed.197 * We know that we're not using a mapped file, so we can198 * free() the old one. */199 SAFE_FREE(valid_table);200 201 /* use free rather than unmap */202 valid_table_use_unmap = False;203 204 DEBUG(2,("creating default valid table\n"));205 valid_table = (uint8 *)SMB_MALLOC(0x10000);206 SMB_ASSERT(valid_table != NULL);207 for (i=0;i<128;i++) {208 valid_table[i] = isalnum(i) || strchr(allowed,i);209 }210 211 lazy_initialize_conv();212 213 for (;i<0x10000;i++) {214 smb_ucs2_t c;215 SSVAL(&c, 0, i);216 valid_table[i] = check_dos_char_slowly(c);217 60 } 218 61 } … … 236 79 } 237 80 238 239 /*******************************************************************240 Skip past a unicode string, but not more than len. Always move241 past a terminating zero if found.242 ********************************************************************/243 244 char *skip_unibuf(char *src, size_t len)245 {246 char *srcend = src + len;247 248 while (src < srcend && SVAL(src,0)) {249 src += 2;250 }251 252 if(!SVAL(src,0)) {253 src += 2;254 }255 256 return src;257 }258 81 259 82 /* Converts a string from internal samba format to unicode … … 279 102 280 103 /******************************************************************* 281 Convert a wchar to upper case.282 ********************************************************************/283 284 smb_ucs2_t toupper_w(smb_ucs2_t val)285 {286 return upcase_table[SVAL(&val,0)];287 }288 289 /*******************************************************************290 Convert a wchar to lower case.291 ********************************************************************/292 293 smb_ucs2_t tolower_w( smb_ucs2_t val )294 {295 return lowcase_table[SVAL(&val,0)];296 }297 298 /*******************************************************************299 Determine if a character is lowercase.300 ********************************************************************/301 302 bool islower_w(smb_ucs2_t c)303 {304 return upcase_table[SVAL(&c,0)] != c;305 }306 307 /*******************************************************************308 Determine if a character is uppercase.309 ********************************************************************/310 311 bool isupper_w(smb_ucs2_t c)312 {313 return lowcase_table[SVAL(&c,0)] != c;314 }315 316 /*******************************************************************317 104 Determine if a character is valid in a 8.3 name. 318 105 ********************************************************************/ … … 320 107 bool isvalid83_w(smb_ucs2_t c) 321 108 { 109 init_valid_table(); 322 110 return valid_table[SVAL(&c,0)] != 0; 323 111 } … … 457 245 Convert a string to lower case. 458 246 return True if any char is converted 247 248 This is unsafe for any string involving a UTF16 character 459 249 ********************************************************************/ 460 250 … … 478 268 Convert a string to upper case. 479 269 return True if any char is converted 270 271 This is unsafe for any string involving a UTF16 character 480 272 ********************************************************************/ 481 273 … … 816 608 } 817 609 818 /************************************************************* 819 ascii only toupper - saves the need for smbd to be in C locale. 820 *************************************************************/ 821 822 int toupper_ascii(int c) 823 { 824 smb_ucs2_t uc = toupper_w(UCS2_CHAR(c)); 825 return UCS2_TO_CHAR(uc); 826 } 827 828 /************************************************************* 829 ascii only tolower - saves the need for smbd to be in C locale. 830 *************************************************************/ 831 832 int tolower_ascii(int c) 833 { 834 smb_ucs2_t uc = tolower_w(UCS2_CHAR(c)); 835 return UCS2_TO_CHAR(uc); 836 } 837 838 /************************************************************* 839 ascii only isupper - saves the need for smbd to be in C locale. 840 *************************************************************/ 841 842 int isupper_ascii(int c) 843 { 844 return isupper_w(UCS2_CHAR(c)); 845 } 846 847 /************************************************************* 848 ascii only islower - saves the need for smbd to be in C locale. 849 *************************************************************/ 850 851 int islower_ascii(int c) 852 { 853 return islower_w(UCS2_CHAR(c)); 854 } 610 smb_ucs2_t toupper_w(smb_ucs2_t v) 611 { 612 smb_ucs2_t ret; 613 /* LE to native. */ 614 codepoint_t cp = SVAL(&v,0); 615 cp = toupper_m(cp); 616 /* native to LE. */ 617 SSVAL(&ret,0,cp); 618 return ret; 619 } 620 621 bool isupper_w(smb_ucs2_t v) 622 { 623 codepoint_t cp = SVAL(&v,0); 624 return isupper_m(cp); 625 } 626 627 smb_ucs2_t tolower_w(smb_ucs2_t v) 628 { 629 smb_ucs2_t ret; 630 /* LE to native. */ 631 codepoint_t cp = SVAL(&v,0); 632 cp = tolower_m(cp); 633 /* native to LE. */ 634 SSVAL(&ret,0,cp); 635 return ret; 636 } 637 638 bool islower_w(smb_ucs2_t v) 639 { 640 codepoint_t cp = SVAL(&v,0); 641 return islower_m(cp); 642 }
Note:
See TracChangeset
for help on using the changeset viewer.