diff options
author | Tim Prouty <tim.prouty@isilon.com> | 2008-04-29 14:36:24 -0700 |
---|---|---|
committer | Volker Lendecke <vl@samba.org> | 2008-05-20 22:40:13 +0200 |
commit | fb37f156009611af0dd454a0fb0829a09cd638ac (patch) | |
tree | e97403a13dd39b7ef485d36c6c7856045e6e4bf3 /source3/lib/charcnv.c | |
parent | 6ed27edbcd3ba1893636a8072c8d7a621437daf7 (diff) | |
download | samba-fb37f156009611af0dd454a0fb0829a09cd638ac.tar.gz samba-fb37f156009611af0dd454a0fb0829a09cd638ac.tar.bz2 samba-fb37f156009611af0dd454a0fb0829a09cd638ac.zip |
Cleanup size_t return values in callers of convert_string_allocate
This patch is the second iteration of an inside-out conversion to cleanup
functions in charcnv.c returning size_t == -1 to indicate failure.
(This used to be commit 6b189dabc562d86dcaa685419d0cb6ea276f100d)
Diffstat (limited to 'source3/lib/charcnv.c')
-rw-r--r-- | source3/lib/charcnv.c | 267 |
1 files changed, 143 insertions, 124 deletions
diff --git a/source3/lib/charcnv.c b/source3/lib/charcnv.c index 81b7238763..b1a5393461 100644 --- a/source3/lib/charcnv.c +++ b/source3/lib/charcnv.c @@ -515,7 +515,7 @@ size_t convert_string(charset_t from, charset_t to, * true * @note -1 is not accepted for srclen. * - * @return True if new buffer was correctly allocated, and string was + * @return true if new buffer was correctly allocated, and string was * converted. * * Ensure the srclen contains the terminating zero. @@ -749,24 +749,22 @@ bool convert_string_allocate(TALLOC_CTX *ctx, charset_t from, charset_t to, * * @param srclen length of source buffer. * @param dest always set at least to NULL + * @parm converted_size set to the number of bytes occupied by the string in + * the destination on success. * @note -1 is not accepted for srclen. * - * @returns Size in bytes of the converted string; or -1 in case of error. - **/ -size_t convert_string_talloc(TALLOC_CTX *ctx, charset_t from, charset_t to, - void const *src, size_t srclen, void *dst, - bool allow_bad_conv) + * @return true if new buffer was correctly allocated, and string was + * converted. + */ +bool convert_string_talloc(TALLOC_CTX *ctx, charset_t from, charset_t to, + void const *src, size_t srclen, void *dst, + size_t *converted_size, bool allow_bad_conv) { void **dest = (void **)dst; - size_t dest_len; *dest = NULL; - if (!convert_string_allocate(ctx, from, to, src, srclen, dest, - &dest_len, allow_bad_conv)) - return (size_t)-1; - if (*dest == NULL) - return (size_t)-1; - return dest_len; + return convert_string_allocate(ctx, from, to, src, srclen, dest, + converted_size, allow_bad_conv); } size_t unix_strupper(const char *src, size_t srclen, char *dest, size_t destlen) @@ -774,10 +772,10 @@ size_t unix_strupper(const char *src, size_t srclen, char *dest, size_t destlen) size_t size; smb_ucs2_t *buffer; - size = push_ucs2_allocate(&buffer, src); - if (size == (size_t)-1) { + if (!push_ucs2_allocate(&buffer, src, &size)) { return (size_t)-1; } + if (!strupper_w(buffer) && (dest == src)) { free(buffer); return srclen; @@ -816,20 +814,25 @@ char *strdup_upper(const char *s) if (*p) { /* MB case. */ - size_t size, size2; + size_t converted_size, converted_size2; smb_ucs2_t *buffer = NULL; SAFE_FREE(out_buffer); if (!convert_string_allocate(NULL, CH_UNIX, CH_UTF16LE, s, - strlen(s) + 1, (void **)(void *)&buffer, &size, - True)) { + strlen(s) + 1, + (void **)(void *)&buffer, + &converted_size, True)) + { return NULL; } strupper_w(buffer); if (!convert_string_allocate(NULL, CH_UTF16LE, CH_UNIX, buffer, - size, (void **)(void *)&out_buffer, &size2, True)) { + converted_size, + (void **)(void *)&out_buffer, + &converted_size2, True)) + { TALLOC_FREE(buffer); return NULL; } @@ -871,36 +874,33 @@ char *talloc_strdup_upper(TALLOC_CTX *ctx, const char *s) if (*p) { /* MB case. */ - size_t size; + size_t converted_size, converted_size2; smb_ucs2_t *ubuf = NULL; /* We're not using the ascii buffer above. */ TALLOC_FREE(out_buffer); - size = convert_string_talloc(ctx, CH_UNIX, CH_UTF16LE, - s, strlen(s)+1, - (void *)&ubuf, - True); - if (size == (size_t)-1) { + if (!convert_string_talloc(ctx, CH_UNIX, CH_UTF16LE, s, + strlen(s)+1, (void *)&ubuf, + &converted_size, True)) + { return NULL; } strupper_w(ubuf); - size = convert_string_talloc(ctx, CH_UTF16LE, CH_UNIX, - ubuf, size, - (void *)&out_buffer, - True); + if (!convert_string_talloc(ctx, CH_UTF16LE, CH_UNIX, ubuf, + converted_size, (void *)&out_buffer, + &converted_size2, True)) + { + TALLOC_FREE(ubuf); + return NULL; + } /* Don't need the intermediate buffer * anymore. */ - TALLOC_FREE(ubuf); - - if (size == (size_t)-1) { - return NULL; - } } return out_buffer; @@ -912,7 +912,9 @@ size_t unix_strlower(const char *src, size_t srclen, char *dest, size_t destlen) smb_ucs2_t *buffer = NULL; if (!convert_string_allocate(NULL, CH_UNIX, CH_UTF16LE, src, srclen, - (void **)(void *)&buffer, &size, True)) { + (void **)(void *)&buffer, &size, + True)) + { smb_panic("failed to create UCS2 buffer"); } if (!strlower_w(buffer) && (dest == src)) { @@ -930,49 +932,45 @@ size_t unix_strlower(const char *src, size_t srclen, char *dest, size_t destlen) char *strdup_lower(const char *s) { - size_t size; + size_t converted_size; smb_ucs2_t *buffer = NULL; char *out_buffer; - size = push_ucs2_allocate(&buffer, s); - if (size == -1 || !buffer) { + if (!push_ucs2_allocate(&buffer, s, &converted_size)) { return NULL; } strlower_w(buffer); - size = pull_ucs2_allocate(&out_buffer, buffer); - SAFE_FREE(buffer); - - if (size == (size_t)-1) { + if (!pull_ucs2_allocate(&out_buffer, buffer, &converted_size)) { + SAFE_FREE(buffer); return NULL; } + SAFE_FREE(buffer); + return out_buffer; } char *talloc_strdup_lower(TALLOC_CTX *ctx, const char *s) { - size_t size; + size_t converted_size; smb_ucs2_t *buffer = NULL; char *out_buffer; - size = push_ucs2_talloc(ctx, &buffer, s); - if (size == -1 || !buffer) { - TALLOC_FREE(buffer); + if (!push_ucs2_talloc(ctx, &buffer, s, &converted_size)) { return NULL; } strlower_w(buffer); - size = pull_ucs2_talloc(ctx, &out_buffer, buffer); - TALLOC_FREE(buffer); - - if (size == (size_t)-1) { - TALLOC_FREE(out_buffer); + if (!pull_ucs2_talloc(ctx, &out_buffer, buffer, &converted_size)) { + TALLOC_FREE(buffer); return NULL; } + TALLOC_FREE(buffer); + return out_buffer; } @@ -1049,8 +1047,7 @@ size_t push_ascii_nstring(void *dest, const char *src) smb_ucs2_t *buffer; conv_silent = True; - buffer_len = push_ucs2_allocate(&buffer, src); - if (buffer_len == (size_t)-1) { + if (!push_ucs2_allocate(&buffer, src, &buffer_len)) { smb_panic("failed to create UCS2 buffer"); } @@ -1081,16 +1078,13 @@ size_t push_ascii_nstring(void *dest, const char *src) Push and malloc an ascii string. src and dest null terminated. ********************************************************************/ -size_t push_ascii_allocate(char **dest, const char *src) +bool push_ascii_allocate(char **dest, const char *src, size_t *converted_size) { - size_t dest_len, src_len = strlen(src)+1; + size_t src_len = strlen(src)+1; *dest = NULL; - if (!convert_string_allocate(NULL, CH_UNIX, CH_DOS, src, src_len, - (void **)dest, &dest_len, True)) - return (size_t)-1; - else - return dest_len; + return convert_string_allocate(NULL, CH_UNIX, CH_DOS, src, src_len, + (void **)dest, converted_size, True); } /** @@ -1172,7 +1166,7 @@ static size_t pull_ascii_base_talloc(TALLOC_CTX *ctx, int flags) { char *dest = NULL; - size_t dest_len = 0; + size_t converted_size; #ifdef DEVELOPER /* Ensure we never use the braindead "malloc" varient. */ @@ -1203,13 +1197,15 @@ static size_t pull_ascii_base_talloc(TALLOC_CTX *ctx, } if (!convert_string_allocate(ctx, CH_DOS, CH_UNIX, src, src_len, &dest, - &dest_len, True)) - dest_len = 0; + &converted_size, True)) + { + converted_size = 0; + } - if (dest_len && dest) { + if (converted_size && dest) { /* Did we already process the terminating zero ? */ - if (dest[dest_len-1] != 0) { - dest[dest_len-1] = 0; + if (dest[converted_size - 1] != 0) { + dest[converted_size - 1] = 0; } } else if (dest) { dest[0] = 0; @@ -1311,16 +1307,20 @@ size_t push_ucs2(const void *base_ptr, void *dest, const char *src, size_t dest_ * allocating a buffer using talloc(). * * @param dest always set at least to NULL + * @parm converted_size set to the number of bytes occupied by the string in + * the destination on success. * - * @returns The number of bytes occupied by the string in the destination - * or -1 in case of error. + * @return true if new buffer was correctly allocated, and string was + * converted. **/ -size_t push_ucs2_talloc(TALLOC_CTX *ctx, smb_ucs2_t **dest, const char *src) +bool push_ucs2_talloc(TALLOC_CTX *ctx, smb_ucs2_t **dest, const char *src, + size_t *converted_size) { size_t src_len = strlen(src)+1; *dest = NULL; - return convert_string_talloc(ctx, CH_UNIX, CH_UTF16LE, src, src_len, (void **)dest, True); + return convert_string_talloc(ctx, CH_UNIX, CH_UTF16LE, src, src_len, + (void **)dest, converted_size, True); } @@ -1328,21 +1328,21 @@ size_t push_ucs2_talloc(TALLOC_CTX *ctx, smb_ucs2_t **dest, const char *src) * Copy a string from a unix char* src to a UCS2 destination, allocating a buffer * * @param dest always set at least to NULL + * @parm converted_size set to the number of bytes occupied by the string in + * the destination on success. * - * @returns The number of bytes occupied by the string in the destination - * or -1 in case of error. + * @return true if new buffer was correctly allocated, and string was + * converted. **/ -size_t push_ucs2_allocate(smb_ucs2_t **dest, const char *src) +bool push_ucs2_allocate(smb_ucs2_t **dest, const char *src, + size_t *converted_size) { - size_t dest_len, src_len = strlen(src)+1; + size_t src_len = strlen(src)+1; *dest = NULL; - if (!convert_string_allocate(NULL, CH_UNIX, CH_UTF16LE, src, src_len, - (void **)dest, &dest_len, True)) - return (size_t)-1; - else - return dest_len; + return convert_string_allocate(NULL, CH_UNIX, CH_UTF16LE, src, src_len, + (void **)dest, converted_size, True); } /** @@ -1394,36 +1394,41 @@ size_t push_utf8_fstring(void *dest, const char *src) * Copy a string from a unix char* src to a UTF-8 destination, allocating a buffer using talloc * * @param dest always set at least to NULL + * @parm converted_size set to the number of bytes occupied by the string in + * the destination on success. * - * @returns The number of bytes occupied by the string in the destination + * @return true if new buffer was correctly allocated, and string was + * converted. **/ -size_t push_utf8_talloc(TALLOC_CTX *ctx, char **dest, const char *src) +bool push_utf8_talloc(TALLOC_CTX *ctx, char **dest, const char *src, + size_t *converted_size) { size_t src_len = strlen(src)+1; *dest = NULL; - return convert_string_talloc(ctx, CH_UNIX, CH_UTF8, src, src_len, (void**)dest, True); + return convert_string_talloc(ctx, CH_UNIX, CH_UTF8, src, src_len, + (void**)dest, converted_size, True); } /** * Copy a string from a unix char* src to a UTF-8 destination, allocating a buffer * * @param dest always set at least to NULL + * @parm converted_size set to the number of bytes occupied by the string in + * the destination on success. * - * @returns The number of bytes occupied by the string in the destination + * @return true if new buffer was correctly allocated, and string was + * converted. **/ -size_t push_utf8_allocate(char **dest, const char *src) +bool push_utf8_allocate(char **dest, const char *src, size_t *converted_size) { - size_t dest_len, src_len = strlen(src)+1; + size_t src_len = strlen(src)+1; *dest = NULL; - if (!convert_string_allocate(NULL, CH_UNIX, CH_UTF8, src, src_len, - (void **)dest, &dest_len, True)) - return (size_t)-1; - else - return dest_len; + return convert_string_allocate(NULL, CH_UNIX, CH_UTF8, src, src_len, + (void **)dest, converted_size, True); } /** @@ -1564,14 +1569,8 @@ size_t pull_ucs2_base_talloc(TALLOC_CTX *ctx, src_len &= ~1; } - dest_len = convert_string_talloc(ctx, - CH_UTF16LE, - CH_UNIX, - src, - src_len, - (void *)&dest, - True); - if (dest_len == (size_t)-1) { + if (!convert_string_talloc(ctx, CH_UTF16LE, CH_UNIX, src, src_len, + (void *)&dest, &dest_len, True)) { dest_len = 0; } @@ -1614,83 +1613,103 @@ size_t pull_ucs2_fstring(char *dest, const void *src) * Copy a string from a UCS2 src to a unix char * destination, allocating a buffer using talloc * * @param dest always set at least to NULL + * @parm converted_size set to the number of bytes occupied by the string in + * the destination on success. * - * @returns The number of bytes occupied by the string in the destination + * @return true if new buffer was correctly allocated, and string was + * converted. **/ -size_t pull_ucs2_talloc(TALLOC_CTX *ctx, char **dest, const smb_ucs2_t *src) +bool pull_ucs2_talloc(TALLOC_CTX *ctx, char **dest, const smb_ucs2_t *src, + size_t *converted_size) { size_t src_len = (strlen_w(src)+1) * sizeof(smb_ucs2_t); + *dest = NULL; - return convert_string_talloc(ctx, CH_UTF16LE, CH_UNIX, src, src_len, (void **)dest, True); + return convert_string_talloc(ctx, CH_UTF16LE, CH_UNIX, src, src_len, + (void **)dest, converted_size, True); } /** * Copy a string from a UCS2 src to a unix char * destination, allocating a buffer * * @param dest always set at least to NULL - * - * @returns The number of bytes occupied by the string in the destination + * @parm converted_size set to the number of bytes occupied by the string in + * the destination on success. + * @return true if new buffer was correctly allocated, and string was + * converted. **/ -size_t pull_ucs2_allocate(char **dest, const smb_ucs2_t *src) +bool pull_ucs2_allocate(char **dest, const smb_ucs2_t *src, + size_t *converted_size) { - size_t dest_len, src_len = (strlen_w(src)+1) * sizeof(smb_ucs2_t); + size_t src_len = (strlen_w(src)+1) * sizeof(smb_ucs2_t); + *dest = NULL; - if (!convert_string_allocate(NULL, CH_UTF16LE, CH_UNIX, src, src_len, - (void **)dest, &dest_len, True)) - return (size_t)-1; - else - return dest_len; + return convert_string_allocate(NULL, CH_UTF16LE, CH_UNIX, src, src_len, + (void **)dest, converted_size, True); } /** * Copy a string from a UTF-8 src to a unix char * destination, allocating a buffer using talloc * * @param dest always set at least to NULL + * @parm converted_size set to the number of bytes occupied by the string in + * the destination on success. * - * @returns The number of bytes occupied by the string in the destination + * @return true if new buffer was correctly allocated, and string was + * converted. **/ -size_t pull_utf8_talloc(TALLOC_CTX *ctx, char **dest, const char *src) +bool pull_utf8_talloc(TALLOC_CTX *ctx, char **dest, const char *src, + size_t *converted_size) { size_t src_len = strlen(src)+1; + *dest = NULL; - return convert_string_talloc(ctx, CH_UTF8, CH_UNIX, src, src_len, (void **)dest, True); + return convert_string_talloc(ctx, CH_UTF8, CH_UNIX, src, src_len, + (void **)dest, converted_size, True); } /** * Copy a string from a UTF-8 src to a unix char * destination, allocating a buffer * * @param dest always set at least to NULL + * @parm converted_size set to the number of bytes occupied by the string in + * the destination on success. * - * @returns The number of bytes occupied by the string in the destination + * @return true if new buffer was correctly allocated, and string was + * converted. **/ -size_t pull_utf8_allocate(char **dest, const char *src) +bool pull_utf8_allocate(char **dest, const char *src, size_t *converted_size) { - size_t dest_len, src_len = strlen(src)+1; + size_t src_len = strlen(src)+1; + *dest = NULL; - if (!convert_string_allocate(NULL, CH_UTF8, CH_UNIX, src, src_len, - (void **)dest, &dest_len, True)) - return (size_t)-1; - else - return dest_len; + return convert_string_allocate(NULL, CH_UTF8, CH_UNIX, src, src_len, + (void **)dest, converted_size, True); } /** * Copy a string from a DOS src to a unix char * destination, allocating a buffer using talloc * * @param dest always set at least to NULL + * @parm converted_size set to the number of bytes occupied by the string in + * the destination on success. * - * @returns The number of bytes occupied by the string in the destination + * @return true if new buffer was correctly allocated, and string was + * converted. **/ -size_t pull_ascii_talloc(TALLOC_CTX *ctx, char **dest, const char *src) +bool pull_ascii_talloc(TALLOC_CTX *ctx, char **dest, const char *src, + size_t *converted_size) { size_t src_len = strlen(src)+1; + *dest = NULL; - return convert_string_talloc(ctx, CH_DOS, CH_UNIX, src, src_len, (void **)dest, True); + return convert_string_talloc(ctx, CH_DOS, CH_UNIX, src, src_len, + (void **)dest, converted_size, True); } /** |