Ignore:
Timestamp:
Nov 27, 2012, 4:43:17 PM (13 years ago)
Author:
Silvan Scherrer
Message:

Samba Server: updated trunk to 3.6.0

Location:
trunk/server
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/server

  • trunk/server/source3/lib/util_unistr.c

    r414 r745  
    2222#include "includes.h"
    2323
    24 #ifndef MAXUNI
    25 #define MAXUNI 1024
    26 #endif
    27 
    2824/* these 3 tables define the unicode case handling.  They are loaded
    2925   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;
    3226static uint8 *valid_table;
    33 static bool upcase_table_use_unmap;
    34 static bool lowcase_table_use_unmap;
    35 static bool valid_table_use_unmap;
    3627static bool initialized;
    3728
     
    4132void gfree_case_tables(void)
    4233{
    43         if ( upcase_table ) {
    44                 if ( upcase_table_use_unmap )
    45                         unmap_file(upcase_table, 0x20000);
    46                 else
    47                         SAFE_FREE(upcase_table);
    48         }
    49 
    50         if ( lowcase_table ) {
    51                 if ( lowcase_table_use_unmap )
    52                         unmap_file(lowcase_table, 0x20000);
    53                 else
    54                         SAFE_FREE(lowcase_table);
    55         }
    56 
    5734        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;
    6237        }
    6338        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 any
    70  * 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_SETLOCALE
    95         /* 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 #endif
    106 
    107         /* we would like Samba to limp along even if these tables are
    108            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_SETLOCALE
    140         /* Restore the old locale. */
    141         if (saved_locale) {
    142                 setlocale (LC_ALL, saved_locale);
    143                 SAFE_FREE(saved_locale);
    144         }
    145 #endif
    146         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);
    16439}
    16540
     
    17348 **/
    17449
    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 */
     50static void init_valid_table(void)
     51{
     52        if (valid_table) {
    18453                return;
    18554        }
    18655
    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");
    19259                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 can
    198          * 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);
    21760        }
    21861}
     
    23679}
    23780
    238 
    239 /*******************************************************************
    240  Skip past a unicode string, but not more than len. Always move
    241  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 }
    25881
    25982/* Converts a string from internal samba format to unicode
     
    279102
    280103/*******************************************************************
    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 /*******************************************************************
    317104 Determine if a character is valid in a 8.3 name.
    318105********************************************************************/
     
    320107bool isvalid83_w(smb_ucs2_t c)
    321108{
     109        init_valid_table();
    322110        return valid_table[SVAL(&c,0)] != 0;
    323111}
     
    457245 Convert a string to lower case.
    458246 return True if any char is converted
     247
     248 This is unsafe for any string involving a UTF16 character
    459249********************************************************************/
    460250
     
    478268 Convert a string to upper case.
    479269 return True if any char is converted
     270
     271 This is unsafe for any string involving a UTF16 character
    480272********************************************************************/
    481273
     
    816608}
    817609
    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 }
     610smb_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
     621bool isupper_w(smb_ucs2_t v)
     622{
     623        codepoint_t cp = SVAL(&v,0);
     624        return isupper_m(cp);
     625}
     626
     627smb_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
     638bool 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.