diff options
Diffstat (limited to 'source3/lib')
-rw-r--r-- | source3/lib/charcnv.c | 46 | ||||
-rw-r--r-- | source3/lib/util_unistr.c | 205 |
2 files changed, 153 insertions, 98 deletions
diff --git a/source3/lib/charcnv.c b/source3/lib/charcnv.c index 2929233388..59a2af72a4 100644 --- a/source3/lib/charcnv.c +++ b/source3/lib/charcnv.c @@ -171,7 +171,7 @@ size_t convert_string_allocate(charset_t from, charset_t to, return -1; } - destlen = MAX(srclen, 1024); + destlen = MAX(srclen, 512); outbuf = NULL; convert: destlen = destlen * 2; @@ -208,15 +208,13 @@ convert: } destlen = destlen - o_len; - *dest = (char *)malloc(destlen); + *dest = (char *)realloc(outbuf,destlen); if (!*dest) { DEBUG(0, ("convert_string_allocate: out of memory!\n")); free(outbuf); return -1; } - memcpy(*dest, outbuf, destlen); - free(outbuf); - + return destlen; } @@ -478,3 +476,41 @@ int align_string(const void *base_ptr, const char *p, int flags) } return 0; } + + + +/**************************************************************************** +convert from ucs2 to unix charset and return the +allocated and converted string or NULL if an error occurred. +you must provide a zero terminated string. +the returning string will be zero terminated. +****************************************************************************/ +char *acnv_u2ux(const smb_ucs2_t *src) +{ + size_t slen; + size_t dlen; + void *dest; + + slen = strlen_w(src) + 1; + dlen = convert_string_allocate(CH_UCS2, CH_UNIX, src, slen, &dest); + if (dlen == -1) return NULL; + else return dest; +} + +/**************************************************************************** +convert from ucs2 to unix charset and return the +allocated and converted string or NULL if an error occurred. +you must provide a zero terminated string. +the returning string will be zero terminated. +****************************************************************************/ +smb_ucs2_t *acnv_uxu2(const char *src) +{ + size_t slen; + size_t dlen; + void *dest; + + slen = strlen(src) + 1; + dlen = convert_string_allocate(CH_UNIX, CH_UCS2, src, slen, &dest); + if (dlen == -1) return NULL; + else return dest; +} diff --git a/source3/lib/util_unistr.c b/source3/lib/util_unistr.c index 8a7253a718..08281fec4f 100644 --- a/source3/lib/util_unistr.c +++ b/source3/lib/util_unistr.c @@ -271,29 +271,13 @@ smb_ucs2_t *strstr_w(const smb_ucs2_t *s, const smb_ucs2_t *ins) slen = strlen_w(s); inslen = strlen_w(ins); r = (smb_ucs2_t *)s; - while (r = strchr_w(r, *ins)) { + while ((r = strchr_w(r, *ins))) { if (strncmp_w(r, ins, inslen) == 0) return r; r++; } return NULL; } -smb_ucs2_t *strstr_wa(const smb_ucs2_t *s, const char *ins) -{ - smb_ucs2_t *r; - size_t slen, inslen; - - if (!s || !*s || !ins || !*ins) return NULL; - slen = strlen_w(s); - inslen = strlen(ins); - r = (smb_ucs2_t *)s; - while (r = strchr_w(r, UCS2_CHAR(*ins))) { - if (strncmp_wa(r, ins, inslen) == 0) return r; - r++; - } - return NULL; -} - /******************************************************************* Convert a string to lower case. return True if any char is converted @@ -342,6 +326,22 @@ void strnorm_w(smb_ucs2_t *s) strlower_w(s); } +int strcmp_w(const smb_ucs2_t *a, const smb_ucs2_t *b) +{ + while (*b && *a == *b) { a++; b++; } + return (*a - *b); + /* warning: if *a != *b and both are not 0 we retrun a random + greater or lesser than 0 number not realted to which + string is longer */ +} + +int strncmp_w(const smb_ucs2_t *a, const smb_ucs2_t *b, size_t len) +{ + size_t n = 0; + while ((n < len) && *b && *a == *b) { a++; b++; n++;} + return (len - n)?(*a - *b):0; +} + /******************************************************************* case insensitive string comparison ********************************************************************/ @@ -449,6 +449,74 @@ smb_ucs2_t *strncat_w(smb_ucs2_t *dest, const smb_ucs2_t *src, const size_t max) return dest; } +smb_ucs2_t *strcat_w(smb_ucs2_t *dest, const smb_ucs2_t *src) +{ + size_t start; + size_t len; + + if (!dest || !src) return NULL; + + start = strlen_w(dest); + len = strlen_w(src); + + memcpy(&dest[start], src, len*sizeof(smb_ucs2_t)); + dest[start+len] = 0; + + return dest; +} + + +/******************************************************************* +replace any occurence of oldc with newc in unicode string +********************************************************************/ + +void string_replace_w(smb_ucs2_t *s, smb_ucs2_t oldc, smb_ucs2_t newc) +{ + for(;*s;s++) { + if(*s==oldc) *s=newc; + } +} + +/******************************************************************* +trim unicode string +********************************************************************/ + +BOOL trim_string_w(smb_ucs2_t *s, const smb_ucs2_t *front, + const smb_ucs2_t *back) +{ + BOOL ret = False; + size_t len, lw, front_len, flw, back_len, blw; + + if (!s || !*s) return False; + + len = strlen_w(s); + + if (front && *front) { + front_len = strlen_w(front); + flw = front_len * sizeof(smb_ucs2_t); + lw = (len + 1) * sizeof(smb_ucs2_t); + while (len && strncmp_w(s, front, front_len) == 0) { + memcpy(s, s + flw, lw - flw); + len -= front_len; + lw -= flw; + ret = True; + } + } + + if (back && *back) { + back_len = strlen_w(back); + blw = back_len * sizeof(smb_ucs2_t); + lw = len * sizeof(smb_ucs2_t); + while (len && strncmp_w(s + lw - blw, back, back_len) == 0) { + s[len - back_len] = 0; + len -= back_len; + lw -= blw; + ret = True; + } + } + + return ret; +} /* The *_wa() functions take a combination of 7 bit ascii @@ -468,22 +536,6 @@ void pstrcpy_wa(smb_ucs2_t *dest, const char *src) } } -int strcmp_w(const smb_ucs2_t *a, const smb_ucs2_t *b) -{ - while (*b && *a == *b) { a++; b++; } - return (*a - *b); - /* warning: if *a != *b and both are not 0 we retrun a random - greater or lesser than 0 number not realted to which - string is longer */ -} - -int strncmp_w(const smb_ucs2_t *a, const smb_ucs2_t *b, size_t len) -{ - size_t n = 0; - while ((n < len) && *b && *a == *b) { a++; b++; n++;} - return (len - n)?(*a - *b):0; -} - int strcmp_wa(const smb_ucs2_t *a, const char *b) { while (*b && *a == UCS2_CHAR(*b)) { a++; b++; } @@ -509,6 +561,21 @@ smb_ucs2_t *strpbrk_wa(const smb_ucs2_t *s, const char *p) return NULL; } +smb_ucs2_t *strstr_wa(const smb_ucs2_t *s, const char *ins) +{ + smb_ucs2_t *r; + size_t slen, inslen; + + if (!s || !*s || !ins || !*ins) return NULL; + slen = strlen_w(s); + inslen = strlen(ins); + r = (smb_ucs2_t *)s; + while ((r = strchr_w(r, UCS2_CHAR(*ins)))) { + if (strncmp_wa(r, ins, inslen) == 0) return r; + r++; + } + return NULL; +} /******************************************************************* copy a string with max len @@ -519,19 +586,14 @@ smb_ucs2_t *strncpy_wa(smb_ucs2_t *dest, const char *src, const size_t max) smb_ucs2_t *ucs2_src; if (!dest || !src) return NULL; - ucs2_src = (smb_ucs2_t *)malloc((strlen(src)+1)*sizeof(smb_ucs2_t)); - if (!ucs2_src) { - DEBUG(0,("strncpy_wa: out of memory!\n")); + if (!(ucs2_src = acnv_uxu2(src))) return NULL; - } - push_ucs2(NULL, ucs2_src, src, -1, STR_TERMINATE|STR_NOALIGN); strncpy_w(dest, ucs2_src, max); SAFE_FREE(ucs2_src); return dest; } - /******************************************************************* append a string of len bytes and add a terminator ********************************************************************/ @@ -541,68 +603,25 @@ smb_ucs2_t *strncat_wa(smb_ucs2_t *dest, const char *src, const size_t max) smb_ucs2_t *ucs2_src; if (!dest || !src) return NULL; - ucs2_src = (smb_ucs2_t *)malloc((strlen(src)+1)*sizeof(smb_ucs2_t)); - if (!ucs2_src) { - DEBUG(0,("strncat_wa: out of memory!\n")); + if (!(ucs2_src = acnv_uxu2(src))) return NULL; - } - push_ucs2(NULL, ucs2_src, src, -1, STR_TERMINATE|STR_NOALIGN); strncat_w(dest, ucs2_src, max); SAFE_FREE(ucs2_src); return dest; } -/******************************************************************* -replace any occurence of oldc with newc in unicode string -********************************************************************/ - -void string_replace_w(smb_ucs2_t *s, smb_ucs2_t oldc, smb_ucs2_t newc) -{ - for(;*s;s++) { - if(*s==oldc) *s=newc; - } -} - -/******************************************************************* -trim unicode string -********************************************************************/ - -BOOL trim_string_w(smb_ucs2_t *s, const smb_ucs2_t *front, - const smb_ucs2_t *back) -{ - BOOL ret = False; - size_t len, lw, front_len, flw, back_len, blw; - - if (!s || !*s) return False; - - len = strlen_w(s); - - if (front && *front) { - front_len = strlen_w(front); - flw = front_len * sizeof(smb_ucs2_t); - lw = (len + 1) * sizeof(smb_ucs2_t); - while (len && strncmp_w(s, front, front_len) == 0) { - memcpy(s, s + flw, lw - flw); - len -= front_len; - lw -= flw; - ret = True; - } - } +smb_ucs2_t *strcat_wa(smb_ucs2_t *dest, const char *src) +{ + smb_ucs2_t *ucs2_src; - if (back && *back) { - back_len = strlen_w(back); - blw = back_len * sizeof(smb_ucs2_t); - lw = len * sizeof(smb_ucs2_t); - while (len && strncmp_w(s + lw - blw, back, back_len) == 0) { - s[len - back_len] = 0; - len -= back_len; - lw -= blw; - ret = True; - } - } - - return ret; + if (!dest || !src) return NULL; + if (!(ucs2_src = acnv_uxu2(src))) + return NULL; + + strcat_w(dest, ucs2_src); + SAFE_FREE(ucs2_src); + return dest; } BOOL trim_string_wa(smb_ucs2_t *s, const char *front, |