lib/util_unistr.c

説明を見る。
00001 /* 
00002    Unix SMB/CIFS implementation.
00003    Samba utility functions
00004    Copyright (C) Andrew Tridgell 1992-2001
00005    Copyright (C) Simo Sorce 2001
00006    Copyright (C) Jeremy Allison 2005
00007    
00008    This program is free software; you can redistribute it and/or modify
00009    it under the terms of the GNU General Public License as published by
00010    the Free Software Foundation; either version 2 of the License, or
00011    (at your option) any later version.
00012    
00013    This program is distributed in the hope that it will be useful,
00014    but WITHOUT ANY WARRANTY; without even the implied warranty of
00015    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016    GNU General Public License for more details.
00017    
00018    You should have received a copy of the GNU General Public License
00019    along with this program; if not, write to the Free Software
00020    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00021 */
00022 
00023 #include "includes.h"
00024 
00025 #ifndef MAXUNI
00026 #define MAXUNI 1024
00027 #endif
00028 
00029 /* these 3 tables define the unicode case handling.  They are loaded
00030    at startup either via mmap() or read() from the lib directory */
00031 static smb_ucs2_t *upcase_table;
00032 static smb_ucs2_t *lowcase_table;
00033 static uint8 *valid_table;
00034 static BOOL upcase_table_use_unmap;
00035 static BOOL lowcase_table_use_unmap;
00036 static BOOL valid_table_use_unmap;
00037 
00038 /**
00039  * This table says which Unicode characters are valid dos
00040  * characters.
00041  *
00042  * Each value is just a single bit.
00043  **/
00044 static uint8 doschar_table[8192]; /* 65536 characters / 8 bits/byte */
00045 
00046 /**
00047  * Destroy global objects allocated by load_case_tables()
00048  **/
00049 void gfree_case_tables(void)
00050 {
00051         if ( upcase_table ) {
00052                 if ( upcase_table_use_unmap )
00053                         unmap_file(upcase_table, 0x20000);
00054                 else
00055                         SAFE_FREE(upcase_table);
00056         }
00057 
00058         if ( lowcase_table ) {
00059                 if ( lowcase_table_use_unmap )
00060                         unmap_file(lowcase_table, 0x20000);
00061                 else
00062                         SAFE_FREE(lowcase_table);
00063         }
00064 
00065         if ( valid_table ) {
00066                 if ( valid_table_use_unmap )
00067                         unmap_file(valid_table, 0x10000);
00068                 else
00069                         SAFE_FREE(valid_table);
00070         }
00071 }
00072 
00073 /**
00074  * Load or generate the case handling tables.
00075  *
00076  * The case tables are defined in UCS2 and don't depend on any
00077  * configured parameters, so they never need to be reloaded.
00078  **/
00079 
00080 void load_case_tables(void)
00081 {
00082         static int initialised;
00083         char *old_locale = NULL, *saved_locale = NULL;
00084         int i;
00085 
00086         if (initialised) {
00087                 return;
00088         }
00089         initialised = 1;
00090 
00091         upcase_table = (smb_ucs2_t *)map_file(lib_path("upcase.dat"),
00092                                               0x20000);
00093         upcase_table_use_unmap = ( upcase_table != NULL );
00094 
00095         lowcase_table = (smb_ucs2_t *)map_file(lib_path("lowcase.dat"),
00096                                                0x20000);
00097         lowcase_table_use_unmap = ( lowcase_table != NULL );
00098 
00099 #ifdef HAVE_SETLOCALE
00100         /* Get the name of the current locale.  */
00101         old_locale = setlocale(LC_ALL, NULL);
00102 
00103         if (old_locale) {
00104                 /* Save it as it is in static storage. */
00105                 saved_locale = SMB_STRDUP(old_locale);
00106         }
00107 
00108         /* We set back the locale to C to get ASCII-compatible toupper/lower functions. */
00109         setlocale(LC_ALL, "C");
00110 #endif
00111 
00112         /* we would like Samba to limp along even if these tables are
00113            not available */
00114         if (!upcase_table) {
00115                 DEBUG(1,("creating lame upcase table\n"));
00116                 upcase_table = (smb_ucs2_t *)SMB_MALLOC(0x20000);
00117                 for (i=0;i<0x10000;i++) {
00118                         smb_ucs2_t v;
00119                         SSVAL(&v, 0, i);
00120                         upcase_table[v] = i;
00121                 }
00122                 for (i=0;i<256;i++) {
00123                         smb_ucs2_t v;
00124                         SSVAL(&v, 0, UCS2_CHAR(i));
00125                         upcase_table[v] = UCS2_CHAR(islower(i)?toupper(i):i);
00126                 }
00127         }
00128 
00129         if (!lowcase_table) {
00130                 DEBUG(1,("creating lame lowcase table\n"));
00131                 lowcase_table = (smb_ucs2_t *)SMB_MALLOC(0x20000);
00132                 for (i=0;i<0x10000;i++) {
00133                         smb_ucs2_t v;
00134                         SSVAL(&v, 0, i);
00135                         lowcase_table[v] = i;
00136                 }
00137                 for (i=0;i<256;i++) {
00138                         smb_ucs2_t v;
00139                         SSVAL(&v, 0, UCS2_CHAR(i));
00140                         lowcase_table[v] = UCS2_CHAR(isupper(i)?tolower(i):i);
00141                 }
00142         }
00143 
00144 #ifdef HAVE_SETLOCALE
00145         /* Restore the old locale. */
00146         if (saved_locale) {
00147                 setlocale (LC_ALL, saved_locale);
00148                 SAFE_FREE(saved_locale);
00149         }
00150 #endif
00151 }
00152 
00153 /*
00154   see if a ucs2 character can be mapped correctly to a dos character
00155   and mapped back to the same character in ucs2
00156 */
00157 
00158 int check_dos_char(smb_ucs2_t c)
00159 {
00160         lazy_initialize_conv();
00161         
00162         /* Find the right byte, and right bit within the byte; return
00163          * 1 or 0 */
00164         return (doschar_table[(c & 0xffff) / 8] & (1 << (c & 7))) != 0;
00165 }
00166 
00167 
00168 static int check_dos_char_slowly(smb_ucs2_t c)
00169 {
00170         char buf[10];
00171         smb_ucs2_t c2 = 0;
00172         int len1, len2;
00173 
00174         len1 = convert_string(CH_UTF16LE, CH_DOS, &c, 2, buf, sizeof(buf),False);
00175         if (len1 == 0) {
00176                 return 0;
00177         }
00178         len2 = convert_string(CH_DOS, CH_UTF16LE, buf, len1, &c2, 2,False);
00179         if (len2 != 2) {
00180                 return 0;
00181         }
00182         return (c == c2);
00183 }
00184 
00185 
00186 /**
00187  * Fill out doschar table the hard way, by examining each character
00188  **/
00189 
00190 void init_doschar_table(void)
00191 {
00192         int i, j, byteval;
00193 
00194         /* For each byte of packed table */
00195         
00196         for (i = 0; i <= 0xffff; i += 8) {
00197                 byteval = 0;
00198                 for (j = 0; j <= 7; j++) {
00199                         smb_ucs2_t c;
00200 
00201                         c = i + j;
00202                         
00203                         if (check_dos_char_slowly(c)) {
00204                                 byteval |= 1 << j;
00205                         }
00206                 }
00207                 doschar_table[i/8] = byteval;
00208         }
00209 }
00210 
00211 
00212 /**
00213  * Load the valid character map table from <tt>valid.dat</tt> or
00214  * create from the configured codepage.
00215  *
00216  * This function is called whenever the configuration is reloaded.
00217  * However, the valid character table is not changed if it's loaded
00218  * from a file, because we can't unmap files.
00219  **/
00220 
00221 void init_valid_table(void)
00222 {
00223         static int mapped_file;
00224         int i;
00225         const char *allowed = ".!#$%&'()_-@^`~";
00226         uint8 *valid_file;
00227 
00228         if (mapped_file) {
00229                 /* Can't unmap files, so stick with what we have */
00230                 return;
00231         }
00232 
00233         valid_file = (uint8 *)map_file(lib_path("valid.dat"), 0x10000);
00234         if (valid_file) {
00235                 valid_table = valid_file;
00236                 mapped_file = 1;
00237                 valid_table_use_unmap = True;
00238                 return;
00239         }
00240 
00241         /* Otherwise, we're using a dynamically created valid_table.
00242          * It might need to be regenerated if the code page changed.
00243          * We know that we're not using a mapped file, so we can
00244          * free() the old one. */
00245         if (valid_table) 
00246                 SAFE_FREE(valid_table);
00247 
00248         /* use free rather than unmap */
00249         valid_table_use_unmap = False;
00250 
00251         DEBUG(2,("creating default valid table\n"));
00252         valid_table = (uint8 *)SMB_MALLOC(0x10000);
00253         for (i=0;i<128;i++) {
00254                 valid_table[i] = isalnum(i) || strchr(allowed,i);
00255         }
00256         
00257         for (;i<0x10000;i++) {
00258                 smb_ucs2_t c;
00259                 SSVAL(&c, 0, i);
00260                 valid_table[i] = check_dos_char(c);
00261         }
00262 }
00263 
00264 /*******************************************************************
00265  Write a string in (little-endian) unicode format. src is in
00266  the current DOS codepage. len is the length in bytes of the
00267  string pointed to by dst.
00268 
00269  if null_terminate is True then null terminate the packet (adds 2 bytes)
00270 
00271  the return value is the length in bytes consumed by the string, including the
00272  null termination if applied
00273 ********************************************************************/
00274 
00275 size_t dos_PutUniCode(char *dst,const char *src, size_t len, BOOL null_terminate)
00276 {
00277         int flags = null_terminate ? STR_UNICODE|STR_NOALIGN|STR_TERMINATE
00278                                    : STR_UNICODE|STR_NOALIGN;
00279         return push_ucs2(NULL, dst, src, len, flags);
00280 }
00281 
00282 
00283 /*******************************************************************
00284  Skip past a unicode string, but not more than len. Always move
00285  past a terminating zero if found.
00286 ********************************************************************/
00287 
00288 char *skip_unibuf(char *src, size_t len)
00289 {
00290         char *srcend = src + len;
00291 
00292         while (src < srcend && SVAL(src,0)) {
00293                 src += 2;
00294         }
00295 
00296         if(!SVAL(src,0)) {
00297                 src += 2;
00298         }
00299 
00300         return src;
00301 }
00302 
00303 /* Copy a string from little-endian or big-endian unicode source (depending
00304  * on flags) to internal samba format destination
00305  */ 
00306 
00307 int rpcstr_pull(char* dest, void *src, int dest_len, int src_len, int flags)
00308 {
00309         if (!src) {
00310                 dest[0] = 0;
00311                 return 0;
00312         }
00313         if(dest_len==-1) {
00314                 dest_len=MAXUNI-3;
00315         }
00316         return pull_ucs2(NULL, dest, src, dest_len, src_len, flags|STR_UNICODE|STR_NOALIGN);
00317 }
00318 
00319 /* Copy a string from a unistr2 source to internal samba format
00320    destination.  Use this instead of direct calls to rpcstr_pull() to avoid
00321    having to determine whether the source string is null terminated. */
00322 
00323 int rpcstr_pull_unistr2_fstring(char *dest, UNISTR2 *src)
00324 {
00325         return pull_ucs2(NULL, dest, src->buffer, sizeof(fstring),
00326                          src->uni_str_len * 2, 0);
00327 }
00328 
00329 /* Helper function to return a talloc'ed string. I have implemented it with a
00330  * copy because I don't really know how pull_ucs2 and friends calculate the
00331  * target size. If this turns out to be a major bottleneck someone with deeper
00332  * multi-byte knowledge needs to revisit this.
00333  * My (VL) use is dsr_getdcname, which returns 6 strings, the alternative would
00334  * have been to manually talloc_strdup them in rpc_client/cli_netlogon.c.
00335  */
00336 
00337 char *rpcstr_pull_unistr2_talloc(TALLOC_CTX *mem_ctx, const UNISTR2 *src)
00338 {
00339         pstring tmp;
00340         size_t result;
00341 
00342         result = pull_ucs2(NULL, tmp, src->buffer, sizeof(tmp),
00343                            src->uni_str_len * 2, 0);
00344         if (result == (size_t)-1) {
00345                 return NULL;
00346         }
00347 
00348         return talloc_strdup(mem_ctx, tmp);
00349 }
00350 
00351 /* Converts a string from internal samba format to unicode
00352  */ 
00353 
00354 int rpcstr_push(void* dest, const char *src, size_t dest_len, int flags)
00355 {
00356         return push_ucs2(NULL, dest, src, dest_len, flags|STR_UNICODE|STR_NOALIGN);
00357 }
00358 
00359 /*******************************************************************
00360  Convert a (little-endian) UNISTR2 structure to an ASCII string.
00361 ********************************************************************/
00362 
00363 void unistr2_to_ascii(char *dest, const UNISTR2 *str, size_t maxlen)
00364 {
00365         if (str == NULL) {
00366                 *dest='\0';
00367                 return;
00368         }
00369         pull_ucs2(NULL, dest, str->buffer, maxlen, str->uni_str_len*2, STR_NOALIGN);
00370 }
00371 
00372 /*******************************************************************
00373  Convert a (little-endian) UNISTR3 structure to an ASCII string.
00374 ********************************************************************/
00375 
00376 void unistr3_to_ascii(char *dest, const UNISTR3 *str, size_t maxlen)
00377 {
00378         if (str == NULL) {
00379                 *dest='\0';
00380                 return;
00381         }
00382         pull_ucs2(NULL, dest, str->str.buffer, maxlen, str->uni_str_len*2,
00383                   STR_NOALIGN);
00384 }
00385         
00386 /*******************************************************************
00387  Give a static string for displaying a UNISTR2.
00388 ********************************************************************/
00389 
00390 const char *unistr2_static(const UNISTR2 *str)
00391 {
00392         static pstring ret;
00393         unistr2_to_ascii(ret, str, sizeof(ret));
00394         return ret;
00395 }
00396 
00397 /*******************************************************************
00398  Duplicate a UNISTR2 string into a null terminated char*
00399  using a talloc context.
00400 ********************************************************************/
00401 
00402 char *unistr2_tdup(TALLOC_CTX *ctx, const UNISTR2 *str)
00403 {
00404         char *s;
00405         int maxlen = (str->uni_str_len+1)*4;
00406         if (!str->buffer) {
00407                 return NULL;
00408         }
00409         s = (char *)TALLOC(ctx, maxlen); /* convervative */
00410         if (!s) {
00411                 return NULL;
00412         }
00413         pull_ucs2(NULL, s, str->buffer, maxlen, str->uni_str_len*2, STR_NOALIGN);
00414         return s;
00415 }
00416 
00417 /*******************************************************************
00418  Convert a wchar to upper case.
00419 ********************************************************************/
00420 
00421 smb_ucs2_t toupper_w(smb_ucs2_t val)
00422 {
00423         return upcase_table[SVAL(&val,0)];
00424 }
00425 
00426 /*******************************************************************
00427  Convert a wchar to lower case.
00428 ********************************************************************/
00429 
00430 smb_ucs2_t tolower_w( smb_ucs2_t val )
00431 {
00432         return lowcase_table[SVAL(&val,0)];
00433 }
00434 
00435 /*******************************************************************
00436  Determine if a character is lowercase.
00437 ********************************************************************/
00438 
00439 BOOL islower_w(smb_ucs2_t c)
00440 {
00441         return upcase_table[SVAL(&c,0)] != c;
00442 }
00443 
00444 /*******************************************************************
00445  Determine if a character is uppercase.
00446 ********************************************************************/
00447 
00448 BOOL isupper_w(smb_ucs2_t c)
00449 {
00450         return lowcase_table[SVAL(&c,0)] != c;
00451 }
00452 
00453 /*******************************************************************
00454  Determine if a character is valid in a 8.3 name.
00455 ********************************************************************/
00456 
00457 BOOL isvalid83_w(smb_ucs2_t c)
00458 {
00459         return valid_table[SVAL(&c,0)] != 0;
00460 }
00461 
00462 /*******************************************************************
00463  Count the number of characters in a smb_ucs2_t string.
00464 ********************************************************************/
00465 
00466 size_t strlen_w(const smb_ucs2_t *src)
00467 {
00468         size_t len;
00469         smb_ucs2_t c;
00470 
00471         for(len = 0; *(COPY_UCS2_CHAR(&c,src)); src++, len++) {
00472                 ;
00473         }
00474 
00475         return len;
00476 }
00477 
00478 /*******************************************************************
00479  Count up to max number of characters in a smb_ucs2_t string.
00480 ********************************************************************/
00481 
00482 size_t strnlen_w(const smb_ucs2_t *src, size_t max)
00483 {
00484         size_t len;
00485         smb_ucs2_t c;
00486 
00487         for(len = 0; (len < max) && *(COPY_UCS2_CHAR(&c,src)); src++, len++) {
00488                 ;
00489         }
00490 
00491         return len;
00492 }
00493 
00494 /*******************************************************************
00495  Wide strchr().
00496 ********************************************************************/
00497 
00498 smb_ucs2_t *strchr_w(const smb_ucs2_t *s, smb_ucs2_t c)
00499 {
00500         smb_ucs2_t cp;
00501         while (*(COPY_UCS2_CHAR(&cp,s))) {
00502                 if (c == cp) {
00503                         return (smb_ucs2_t *)s;
00504                 }
00505                 s++;
00506         }
00507         if (c == cp) {
00508                 return (smb_ucs2_t *)s;
00509         }
00510 
00511         return NULL;
00512 }
00513 
00514 smb_ucs2_t *strchr_wa(const smb_ucs2_t *s, char c)
00515 {
00516         return strchr_w(s, UCS2_CHAR(c));
00517 }
00518 
00519 /*******************************************************************
00520  Wide strrchr().
00521 ********************************************************************/
00522 
00523 smb_ucs2_t *strrchr_w(const smb_ucs2_t *s, smb_ucs2_t c)
00524 {
00525         smb_ucs2_t cp;
00526         const smb_ucs2_t *p = s;
00527         int len = strlen_w(s);
00528 
00529         if (len == 0) {
00530                 return NULL;
00531         }
00532         p += (len - 1);
00533         do {
00534                 if (c == *(COPY_UCS2_CHAR(&cp,p))) {
00535                         return (smb_ucs2_t *)p;
00536                 }
00537         } while (p-- != s);
00538         return NULL;
00539 }
00540 
00541 /*******************************************************************
00542  Wide version of strrchr that returns after doing strrchr 'n' times.
00543 ********************************************************************/
00544 
00545 smb_ucs2_t *strnrchr_w(const smb_ucs2_t *s, smb_ucs2_t c, unsigned int n)
00546 {
00547         smb_ucs2_t cp;
00548         const smb_ucs2_t *p = s;
00549         int len = strlen_w(s);
00550 
00551         if (len == 0 || !n) {
00552                 return NULL;
00553         }
00554         p += (len - 1);
00555         do {
00556                 if (c == *(COPY_UCS2_CHAR(&cp,p))) {
00557                         n--;
00558                 }
00559 
00560                 if (!n) {
00561                         return (smb_ucs2_t *)p;
00562                 }
00563         } while (p-- != s);
00564         return NULL;
00565 }
00566 
00567 /*******************************************************************
00568  Wide strstr().
00569 ********************************************************************/
00570 
00571 smb_ucs2_t *strstr_w(const smb_ucs2_t *s, const smb_ucs2_t *ins)
00572 {
00573         smb_ucs2_t *r;
00574         size_t inslen;
00575 
00576         if (!s || !*s || !ins || !*ins) {
00577                 return NULL;
00578         }
00579 
00580         inslen = strlen_w(ins);
00581         r = (smb_ucs2_t *)s;
00582 
00583         while ((r = strchr_w(r, *ins))) {
00584                 if (strncmp_w(r, ins, inslen) == 0) {
00585                         return r;
00586                 }
00587                 r++;
00588         }
00589 
00590         return NULL;
00591 }
00592 
00593 /*******************************************************************
00594  Convert a string to lower case.
00595  return True if any char is converted
00596 ********************************************************************/
00597 
00598 BOOL strlower_w(smb_ucs2_t *s)
00599 {
00600         smb_ucs2_t cp;
00601         BOOL ret = False;
00602 
00603         while (*(COPY_UCS2_CHAR(&cp,s))) {
00604                 smb_ucs2_t v = tolower_w(cp);
00605                 if (v != cp) {
00606                         COPY_UCS2_CHAR(s,&v);
00607                         ret = True;
00608                 }
00609                 s++;
00610         }
00611         return ret;
00612 }
00613 
00614 /*******************************************************************
00615  Convert a string to upper case.
00616  return True if any char is converted
00617 ********************************************************************/
00618 
00619 BOOL strupper_w(smb_ucs2_t *s)
00620 {
00621         smb_ucs2_t cp;
00622         BOOL ret = False;
00623         while (*(COPY_UCS2_CHAR(&cp,s))) {
00624                 smb_ucs2_t v = toupper_w(cp);
00625                 if (v != cp) {
00626                         COPY_UCS2_CHAR(s,&v);
00627                         ret = True;
00628                 }
00629                 s++;
00630         }
00631         return ret;
00632 }
00633 
00634 /*******************************************************************
00635  Convert a string to "normal" form.
00636 ********************************************************************/
00637 
00638 void strnorm_w(smb_ucs2_t *s, int case_default)
00639 {
00640         if (case_default == CASE_UPPER) {
00641                 strupper_w(s);
00642         } else {
00643                 strlower_w(s);
00644         }
00645 }
00646 
00647 int strcmp_w(const smb_ucs2_t *a, const smb_ucs2_t *b)
00648 {
00649         smb_ucs2_t cpa, cpb;
00650 
00651         while ((*(COPY_UCS2_CHAR(&cpb,b))) && (*(COPY_UCS2_CHAR(&cpa,a)) == cpb)) {
00652                 a++;
00653                 b++;
00654         }
00655         return (*(COPY_UCS2_CHAR(&cpa,a)) - *(COPY_UCS2_CHAR(&cpb,b)));
00656         /* warning: if *a != *b and both are not 0 we return a random
00657                 greater or lesser than 0 number not realted to which
00658                 string is longer */
00659 }
00660 
00661 int strncmp_w(const smb_ucs2_t *a, const smb_ucs2_t *b, size_t len)
00662 {
00663         smb_ucs2_t cpa, cpb;
00664         size_t n = 0;
00665 
00666         while ((n < len) && (*(COPY_UCS2_CHAR(&cpb,b))) && (*(COPY_UCS2_CHAR(&cpa,a)) == cpb)) {
00667                 a++;
00668                 b++;
00669                 n++;
00670         }
00671         return (len - n)?(*(COPY_UCS2_CHAR(&cpa,a)) - *(COPY_UCS2_CHAR(&cpb,b))):0;
00672 }
00673 
00674 /*******************************************************************
00675  Case insensitive string comparison.
00676 ********************************************************************/
00677 
00678 int strcasecmp_w(const smb_ucs2_t *a, const smb_ucs2_t *b)
00679 {
00680         smb_ucs2_t cpa, cpb;
00681 
00682         while ((*COPY_UCS2_CHAR(&cpb,b)) && toupper_w(*(COPY_UCS2_CHAR(&cpa,a))) == toupper_w(cpb)) {
00683                 a++;
00684                 b++;
00685         }
00686         return (tolower_w(*(COPY_UCS2_CHAR(&cpa,a))) - tolower_w(*(COPY_UCS2_CHAR(&cpb,b))));
00687 }
00688 
00689 /*******************************************************************
00690  Case insensitive string comparison, length limited.
00691 ********************************************************************/
00692 
00693 int strncasecmp_w(const smb_ucs2_t *a, const smb_ucs2_t *b, size_t len)
00694 {
00695         smb_ucs2_t cpa, cpb;
00696         size_t n = 0;
00697 
00698         while ((n < len) && *COPY_UCS2_CHAR(&cpb,b) && (toupper_w(*(COPY_UCS2_CHAR(&cpa,a))) == toupper_w(cpb))) {
00699                 a++;
00700                 b++;
00701                 n++;
00702         }
00703         return (len - n)?(tolower_w(*(COPY_UCS2_CHAR(&cpa,a))) - tolower_w(*(COPY_UCS2_CHAR(&cpb,b)))):0;
00704 }
00705 
00706 /*******************************************************************
00707  Compare 2 strings.
00708 ********************************************************************/
00709 
00710 BOOL strequal_w(const smb_ucs2_t *s1, const smb_ucs2_t *s2)
00711 {
00712         if (s1 == s2) {
00713                 return(True);
00714         }
00715         if (!s1 || !s2) {
00716                 return(False);
00717         }
00718   
00719         return(strcasecmp_w(s1,s2)==0);
00720 }
00721 
00722 /*******************************************************************
00723  Compare 2 strings up to and including the nth char.
00724 ******************************************************************/
00725 
00726 BOOL strnequal_w(const smb_ucs2_t *s1,const smb_ucs2_t *s2,size_t n)
00727 {
00728         if (s1 == s2) {
00729                 return(True);
00730         }
00731         if (!s1 || !s2 || !n) {
00732                 return(False);
00733         }
00734   
00735         return(strncasecmp_w(s1,s2,n)==0);
00736 }
00737 
00738 /*******************************************************************
00739  Duplicate string.
00740 ********************************************************************/
00741 
00742 smb_ucs2_t *strdup_w(const smb_ucs2_t *src)
00743 {
00744         return strndup_w(src, 0);
00745 }
00746 
00747 /* if len == 0 then duplicate the whole string */
00748 
00749 smb_ucs2_t *strndup_w(const smb_ucs2_t *src, size_t len)
00750 {
00751         smb_ucs2_t *dest;
00752         
00753         if (!len) {
00754                 len = strlen_w(src);
00755         }
00756         dest = SMB_MALLOC_ARRAY(smb_ucs2_t, len + 1);
00757         if (!dest) {
00758                 DEBUG(0,("strdup_w: out of memory!\n"));
00759                 return NULL;
00760         }
00761 
00762         memcpy(dest, src, len * sizeof(smb_ucs2_t));
00763         dest[len] = 0;
00764         return dest;
00765 }
00766 
00767 /*******************************************************************
00768  Copy a string with max len.
00769 ********************************************************************/
00770 
00771 smb_ucs2_t *strncpy_w(smb_ucs2_t *dest, const smb_ucs2_t *src, const size_t max)
00772 {
00773         smb_ucs2_t cp;
00774         size_t len;
00775         
00776         if (!dest || !src) {
00777                 return NULL;
00778         }
00779         
00780         for (len = 0; (*COPY_UCS2_CHAR(&cp,(src+len))) && (len < max); len++) {
00781                 cp = *COPY_UCS2_CHAR(dest+len,src+len);
00782         }
00783         cp = 0;
00784         for ( /*nothing*/ ; len < max; len++ ) {
00785                 cp = *COPY_UCS2_CHAR(dest+len,&cp);
00786         }
00787         
00788         return dest;
00789 }
00790 
00791 /*******************************************************************
00792  Append a string of len bytes and add a terminator.
00793 ********************************************************************/
00794 
00795 smb_ucs2_t *strncat_w(smb_ucs2_t *dest, const smb_ucs2_t *src, const size_t max)
00796 {       
00797         size_t start;
00798         size_t len;     
00799         smb_ucs2_t z = 0;
00800 
00801         if (!dest || !src) {
00802                 return NULL;
00803         }
00804         
00805         start = strlen_w(dest);
00806         len = strnlen_w(src, max);
00807 
00808         memcpy(&dest[start], src, len*sizeof(smb_ucs2_t));                      
00809         z = *COPY_UCS2_CHAR(dest+start+len,&z);
00810 
00811         return dest;
00812 }
00813 
00814 smb_ucs2_t *strcat_w(smb_ucs2_t *dest, const smb_ucs2_t *src)
00815 {       
00816         size_t start;
00817         size_t len;     
00818         smb_ucs2_t z = 0;
00819         
00820         if (!dest || !src) {
00821                 return NULL;
00822         }
00823         
00824         start = strlen_w(dest);
00825         len = strlen_w(src);
00826 
00827         memcpy(&dest[start], src, len*sizeof(smb_ucs2_t));                      
00828         z = *COPY_UCS2_CHAR(dest+start+len,&z);
00829         
00830         return dest;
00831 }
00832 
00833 
00834 /*******************************************************************
00835  Replace any occurence of oldc with newc in unicode string.
00836 ********************************************************************/
00837 
00838 void string_replace_w(smb_ucs2_t *s, smb_ucs2_t oldc, smb_ucs2_t newc)
00839 {
00840         smb_ucs2_t cp;
00841 
00842         for(;*(COPY_UCS2_CHAR(&cp,s));s++) {
00843                 if(cp==oldc) {
00844                         COPY_UCS2_CHAR(s,&newc);
00845                 }
00846         }
00847 }
00848 
00849 /*******************************************************************
00850  Trim unicode string.
00851 ********************************************************************/
00852 
00853 BOOL trim_string_w(smb_ucs2_t *s, const smb_ucs2_t *front,
00854                                   const smb_ucs2_t *back)
00855 {
00856         BOOL ret = False;
00857         size_t len, front_len, back_len;
00858 
00859         if (!s) {
00860                 return False;
00861         }
00862 
00863         len = strlen_w(s);
00864 
00865         if (front && *front) {
00866                 front_len = strlen_w(front);
00867                 while (len && strncmp_w(s, front, front_len) == 0) {
00868                         memmove(s, (s + front_len), (len - front_len + 1) * sizeof(smb_ucs2_t));
00869                         len -= front_len;
00870                         ret = True;
00871                 }
00872         }
00873         
00874         if (back && *back) {
00875                 back_len = strlen_w(back);
00876                 while (len && strncmp_w((s + (len - back_len)), back, back_len) == 0) {
00877                         s[len - back_len] = 0;
00878                         len -= back_len;
00879                         ret = True;
00880                 }
00881         }
00882 
00883         return ret;
00884 }
00885 
00886 /*
00887   The *_wa() functions take a combination of 7 bit ascii
00888   and wide characters They are used so that you can use string
00889   functions combining C string constants with ucs2 strings
00890 
00891   The char* arguments must NOT be multibyte - to be completely sure
00892   of this only pass string constants */
00893 
00894 int strcmp_wa(const smb_ucs2_t *a, const char *b)
00895 {
00896         smb_ucs2_t cp = 0;
00897 
00898         while (*b && *(COPY_UCS2_CHAR(&cp,a)) == UCS2_CHAR(*b)) {
00899                 a++;
00900                 b++;
00901         }
00902         return (*(COPY_UCS2_CHAR(&cp,a)) - UCS2_CHAR(*b));
00903 }
00904 
00905 int strncmp_wa(const smb_ucs2_t *a, const char *b, size_t len)
00906 {
00907         smb_ucs2_t cp = 0;
00908         size_t n = 0;
00909 
00910         while ((n < len) && *b && *(COPY_UCS2_CHAR(&cp,a)) == UCS2_CHAR(*b)) {
00911                 a++;
00912                 b++;
00913                 n++;
00914         }
00915         return (len - n)?(*(COPY_UCS2_CHAR(&cp,a)) - UCS2_CHAR(*b)):0;
00916 }
00917 
00918 smb_ucs2_t *strpbrk_wa(const smb_ucs2_t *s, const char *p)
00919 {
00920         smb_ucs2_t cp;
00921 
00922         while (*(COPY_UCS2_CHAR(&cp,s))) {
00923                 int i;
00924                 for (i=0; p[i] && cp != UCS2_CHAR(p[i]); i++) 
00925                         ;
00926                 if (p[i]) {
00927                         return (smb_ucs2_t *)s;
00928                 }
00929                 s++;
00930         }
00931         return NULL;
00932 }
00933 
00934 smb_ucs2_t *strstr_wa(const smb_ucs2_t *s, const char *ins)
00935 {
00936         smb_ucs2_t *r;
00937         size_t inslen;
00938 
00939         if (!s || !ins) { 
00940                 return NULL;
00941         }
00942 
00943         inslen = strlen(ins);
00944         r = (smb_ucs2_t *)s;
00945 
00946         while ((r = strchr_w(r, UCS2_CHAR(*ins)))) {
00947                 if (strncmp_wa(r, ins, inslen) == 0) 
00948                         return r;
00949                 r++;
00950         }
00951 
00952         return NULL;
00953 }
00954 
00955 BOOL trim_string_wa(smb_ucs2_t *s, const char *front,
00956                                   const char *back)
00957 {
00958         wpstring f, b;
00959 
00960         if (front) {
00961                 push_ucs2(NULL, f, front, sizeof(wpstring) - 1, STR_TERMINATE);
00962         } else {
00963                 *f = 0;
00964         }
00965         if (back) {
00966                 push_ucs2(NULL, b, back, sizeof(wpstring) - 1, STR_TERMINATE);
00967         } else {
00968                 *b = 0;
00969         }
00970         return trim_string_w(s, f, b);
00971 }
00972 
00973 /*******************************************************************
00974  Returns the length in number of wide characters.
00975 ******************************************************************/
00976 
00977 int unistrlen(uint16 *s)
00978 {
00979         int len;
00980 
00981         if (!s) {
00982                 return -1;
00983         }
00984 
00985         for (len=0; SVAL(s,0); s++,len++) {
00986                 ;
00987         }
00988 
00989         return len;
00990 }
00991 
00992 /*******************************************************************
00993  Strcpy for unicode strings. Returns length (in num of wide chars).
00994  Not odd align safe.
00995 ********************************************************************/
00996 
00997 int unistrcpy(uint16 *dst, uint16 *src)
00998 {
00999         int num_wchars = 0;
01000 
01001         while (SVAL(src,0)) {
01002                 *dst++ = *src++;
01003                 num_wchars++;
01004         }
01005         *dst = 0;
01006 
01007         return num_wchars;
01008 }
01009 
01010 /**
01011  * Samba ucs2 type to UNISTR2 conversion
01012  *
01013  * @param ctx Talloc context to create the dst strcture (if null) and the 
01014  *            contents of the unicode string.
01015  * @param dst UNISTR2 destination. If equals null, then it's allocated.
01016  * @param src smb_ucs2_t source.
01017  * @param max_len maximum number of unicode characters to copy. If equals
01018  *        null, then null-termination of src is taken
01019  *
01020  * @return copied UNISTR2 destination
01021  **/
01022 
01023 UNISTR2* ucs2_to_unistr2(TALLOC_CTX *ctx, UNISTR2* dst, smb_ucs2_t* src)
01024 {
01025         size_t len;
01026 
01027         if (!src) {
01028                 return NULL;
01029         }
01030 
01031         len = strlen_w(src);
01032         
01033         /* allocate UNISTR2 destination if not given */
01034         if (!dst) {
01035                 dst = TALLOC_P(ctx, UNISTR2);
01036                 if (!dst)
01037                         return NULL;
01038         }
01039         if (!dst->buffer) {
01040                 dst->buffer = TALLOC_ARRAY(ctx, uint16, len + 1);
01041                 if (!dst->buffer)
01042                         return NULL;
01043         }
01044         
01045         /* set UNISTR2 parameters */
01046         dst->uni_max_len = len + 1;
01047         dst->offset = 0;
01048         dst->uni_str_len = len;
01049         
01050         /* copy the actual unicode string */
01051         strncpy_w(dst->buffer, src, dst->uni_max_len);
01052         
01053         return dst;
01054 }
01055 
01056 /*************************************************************
01057  ascii only toupper - saves the need for smbd to be in C locale.
01058 *************************************************************/
01059 
01060 int toupper_ascii(int c)
01061 {
01062         smb_ucs2_t uc = toupper_w(UCS2_CHAR(c));
01063         return UCS2_TO_CHAR(uc);
01064 }
01065 
01066 /*************************************************************
01067  ascii only tolower - saves the need for smbd to be in C locale.
01068 *************************************************************/
01069 
01070 int tolower_ascii(int c)
01071 {
01072         smb_ucs2_t uc = tolower_w(UCS2_CHAR(c));
01073         return UCS2_TO_CHAR(uc);
01074 }
01075 
01076 /*************************************************************
01077  ascii only isupper - saves the need for smbd to be in C locale.
01078 *************************************************************/
01079 
01080 int isupper_ascii(int c)
01081 {
01082         return isupper_w(UCS2_CHAR(c));
01083 }
01084 
01085 /*************************************************************
01086  ascii only islower - saves the need for smbd to be in C locale.
01087 *************************************************************/
01088 
01089 int islower_ascii(int c)
01090 {
01091         return islower_w(UCS2_CHAR(c));
01092 }

Sambaに対してSat Aug 29 21:23:00 2009に生成されました。  doxygen 1.4.7