diff options
Diffstat (limited to 'source3/lib/util_unistr.c')
-rw-r--r-- | source3/lib/util_unistr.c | 45 |
1 files changed, 42 insertions, 3 deletions
diff --git a/source3/lib/util_unistr.c b/source3/lib/util_unistr.c index 08281fec4f..58ecc19723 100644 --- a/source3/lib/util_unistr.c +++ b/source3/lib/util_unistr.c @@ -231,6 +231,18 @@ size_t strlen_w(const smb_ucs2_t *src) } /******************************************************************* + Count up to max number of characters in a smb_ucs2_t string. +********************************************************************/ +size_t strnlen_w(const smb_ucs2_t *src, size_t max) +{ + size_t len; + + for(len = 0; *src++ && (len < max); len++) ; + + return len; +} + +/******************************************************************* wide strchr() ********************************************************************/ smb_ucs2_t *strchr_w(const smb_ucs2_t *s, smb_ucs2_t c) @@ -440,8 +452,7 @@ smb_ucs2_t *strncat_w(smb_ucs2_t *dest, const smb_ucs2_t *src, const size_t max) if (!dest || !src) return NULL; start = strlen_w(dest); - len = strlen_w(src); - if (len > max) len = max; + len = strnlen_w(src, max); memcpy(&dest[start], src, len*sizeof(smb_ucs2_t)); dest[start+len] = 0; @@ -465,7 +476,6 @@ smb_ucs2_t *strcat_w(smb_ucs2_t *dest, const smb_ucs2_t *src) return dest; } - /******************************************************************* replace any occurence of oldc with newc in unicode string ********************************************************************/ @@ -595,6 +605,35 @@ smb_ucs2_t *strncpy_wa(smb_ucs2_t *dest, const char *src, const size_t max) } /******************************************************************* +convert and duplicate an ascii string +********************************************************************/ +smb_ucs2_t *strdup_wa(const char *src) +{ + return strndup_wa(src, 0); +} + +/* if len == 0 then duplicate the whole string */ +smb_ucs2_t *strndup_wa(const char *src, size_t len) +{ + smb_ucs2_t *dest, *s; + + s = acnv_dosu2(src); + if (!len) len = strlen_w(s); + dest = (smb_ucs2_t *)malloc((len + 1) * sizeof(smb_ucs2_t)); + if (!dest) { + DEBUG(0,("strdup_w: out of memory!\n")); + SAFE_FREE(s); + return NULL; + } + + memcpy(dest, src, len * sizeof(smb_ucs2_t)); + dest[len] = 0; + + SAFE_FREE(s); + return dest; +} + +/******************************************************************* append a string of len bytes and add a terminator ********************************************************************/ |