diff options
Diffstat (limited to 'source4/lib/util/util_str.c')
-rw-r--r-- | source4/lib/util/util_str.c | 82 |
1 files changed, 42 insertions, 40 deletions
diff --git a/source4/lib/util/util_str.c b/source4/lib/util/util_str.c index 67e59474fd..9ea6403c52 100644 --- a/source4/lib/util/util_str.c +++ b/source4/lib/util/util_str.c @@ -23,7 +23,6 @@ #include "includes.h" #include "libcli/raw/smb.h" -#include "pstring.h" #include "system/locale.h" /** @@ -237,7 +236,7 @@ _PUBLIC_ void hex_encode(const unsigned char *buff_in, size_t len, char **out_he int i; char *hex_buffer; - *out_hex_buffer = smb_xmalloc((len*2)+1); + *out_hex_buffer = malloc_array_p(char, (len*2)+1); hex_buffer = *out_hex_buffer; for (i = 0; i < len; i++) @@ -261,39 +260,6 @@ _PUBLIC_ char *hex_encode_talloc(TALLOC_CTX *mem_ctx, const unsigned char *buff_ } /** - Set a string value, allocing the space for the string -**/ -static bool string_init(char **dest,const char *src) -{ - if (!src) src = ""; - - (*dest) = strdup(src); - if ((*dest) == NULL) { - DEBUG(0,("Out of memory in string_init\n")); - return false; - } - return true; -} - -/** - Free a string value. -**/ -_PUBLIC_ void string_free(char **s) -{ - if (s) SAFE_FREE(*s); -} - -/** - Set a string value, deallocating any existing space, and allocing the space - for the string -**/ -_PUBLIC_ bool string_set(char **dest, const char *src) -{ - string_free(dest); - return string_init(dest,src); -} - -/** Substitute a string for a pattern in another string. Make sure there is enough room! @@ -305,10 +271,10 @@ _PUBLIC_ bool string_set(char **dest, const char *src) use of len==0 which was for no length checks to be done. **/ -_PUBLIC_ void string_sub(char *s,const char *pattern, const char *insert, size_t len) +_PUBLIC_ void string_sub(char *s, const char *pattern, const char *insert, size_t len) { char *p; - ssize_t ls,lp,li, i; + ssize_t ls, lp, li, i; if (!insert || !pattern || !*pattern || !s) return; @@ -320,7 +286,7 @@ _PUBLIC_ void string_sub(char *s,const char *pattern, const char *insert, size_t if (len == 0) len = ls + 1; /* len is number of *bytes* */ - while (lp <= ls && (p = strstr(s,pattern))) { + while (lp <= ls && (p = strstr(s, pattern))) { if (ls + (li-lp) >= len) { DEBUG(0,("ERROR: string overflow by %d in string_sub(%.50s, %d)\n", (int)(ls + (li-lp) - len), @@ -351,6 +317,42 @@ _PUBLIC_ void string_sub(char *s,const char *pattern, const char *insert, size_t } } +/** + * Talloc'ed version of string_sub + */ +_PUBLIC_ char *string_sub_talloc(TALLOC_CTX *mem_ctx, const char *s, + const char *pattern, const char *insert) +{ + const char *p; + char *ret; + size_t len, alloc_len; + + if (insert == NULL || pattern == NULL || !*pattern || s == NULL) + return NULL; + + /* determine length needed */ + len = strlen(s); + + for (p = strstr(s, pattern); p != NULL; + p = strstr(p+strlen(pattern), pattern)) { + len += strlen(insert) - strlen(pattern); + } + + alloc_len = MAX(len, strlen(s))+1; + ret = talloc_array(mem_ctx, char, alloc_len); + if (ret == NULL) + return NULL; + strncpy(ret, s, alloc_len); + string_sub(ret, pattern, insert, alloc_len); + + ret = talloc_realloc(mem_ctx, ret, char, len+1); + if (ret == NULL) + return NULL; + + SMB_ASSERT(ret[len] == '\0'); + + return ret; +} /** Similar to string_sub() but allows for any character to be substituted. @@ -457,7 +459,7 @@ _PUBLIC_ const char *str_format_nbt_domain(TALLOC_CTX *mem_ctx, const char *s) if (!s || !*s) { return talloc_strdup(mem_ctx, ""); } - ret = talloc_size(mem_ctx, strlen(s)+2); + ret = talloc_array(mem_ctx, char, strlen(s)+2); if (!ret) { return ret; } @@ -566,7 +568,7 @@ _PUBLIC_ char *attrib_string(TALLOC_CTX *mem_ctx, uint32_t attrib) }; char *ret; - ret = talloc_size(mem_ctx, ARRAY_SIZE(attr_strs)+1); + ret = talloc_array(mem_ctx, char, ARRAY_SIZE(attr_strs)+1); if (!ret) { return NULL; } |