* (this is a bad interface and needs fixing. JRA).
* @param srclen length of source buffer.
* @param dest always set at least to NULL
+ * @param converted_size set to the size of the allocated buffer on return
+ * true
* @note -1 is not accepted for srclen.
*
- * @returns Size in bytes of the converted string; or -1 in case of error.
+ * @return True if new buffer was correctly allocated, and string was
+ * converted.
*
* Ensure the srclen contains the terminating zero.
*
* There has to be a cleaner way to do this. JRA.
**/
-size_t convert_string_allocate(TALLOC_CTX *ctx, charset_t from, charset_t to,
- void const *src, size_t srclen, void *dst, bool allow_bad_conv)
+bool convert_string_allocate(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)
{
size_t i_len, o_len, destlen = (srclen * 3) / 2;
size_t retval;
*dest = NULL;
- if (src == NULL || srclen == (size_t)-1)
- return (size_t)-1;
- if (srclen == 0)
- return 0;
+ if (!converted_size) {
+ errno = EINVAL;
+ return false;
+ }
+
+ if (src == NULL || srclen == (size_t)-1) {
+ errno = EINVAL;
+ return false;
+ }
+ if (srclen == 0) {
+ *converted_size = 0;
+ return true;
+ }
lazy_initialize_conv();
if (descriptor == (smb_iconv_t)-1 || descriptor == (smb_iconv_t)0) {
if (!conv_silent)
DEBUG(0,("convert_string_allocate: Conversion not supported.\n"));
- return (size_t)-1;
+ errno = EOPNOTSUPP;
+ return false;
}
convert:
DEBUG(0, ("convert_string_allocate: destlen wrapped !\n"));
if (!ctx)
SAFE_FREE(outbuf);
- return (size_t)-1;
+ errno = EOPNOTSUPP;
+ return false;
} else {
destlen = destlen * 2;
}
if (!ob) {
DEBUG(0, ("convert_string_allocate: realloc failed!\n"));
- return (size_t)-1;
+ errno = ENOMEM;
+ return false;
}
outbuf = ob;
i_len = srclen;
} else {
SAFE_FREE(ob);
}
- return (size_t)-1;
+ return false;
}
out:
if (destlen && !ob) {
DEBUG(0, ("convert_string_allocate: out of memory!\n"));
- return (size_t)-1;
+ errno = ENOMEM;
+ return false;
}
*dest = ob;
ob[destlen] = '\0';
ob[destlen+1] = '\0';
- return destlen;
+ *converted_size = destlen;
+ return true;
use_as_is:
size_t dest_len;
*dest = NULL;
- dest_len=convert_string_allocate(ctx, from, to, src, srclen, dest, allow_bad_conv);
- if (dest_len == (size_t)-1)
+ 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;
if (*p) {
/* MB case. */
- size_t size;
+ size_t size, size2;
smb_ucs2_t *buffer = NULL;
SAFE_FREE(out_buffer);
- size = convert_string_allocate(NULL,
- CH_UNIX,
- CH_UTF16LE,
- s,
- strlen(s) + 1,
- (void **)(void *)&buffer,
- True);
- if (size == (size_t)-1) {
+ if (!convert_string_allocate(NULL, CH_UNIX, CH_UTF16LE, s,
+ strlen(s) + 1, (void **)(void *)&buffer, &size,
+ True)) {
return NULL;
}
strupper_w(buffer);
- size = convert_string_allocate(NULL,
- CH_UTF16LE,
- CH_UNIX,
- buffer,
- size,
- (void **)(void *)&out_buffer,
- True);
+ if (!convert_string_allocate(NULL, CH_UTF16LE, CH_UNIX, buffer,
+ size, (void **)(void *)&out_buffer, &size2, True)) {
+ TALLOC_FREE(buffer);
+ return NULL;
+ }
/* Don't need the intermediate buffer
* anymore.
*/
-
TALLOC_FREE(buffer);
- if (size == (size_t)-1) {
- return NULL;
- }
}
return out_buffer;
size_t size;
smb_ucs2_t *buffer = NULL;
- size = convert_string_allocate(NULL, CH_UNIX, CH_UTF16LE, src, srclen,
- (void **)(void *)&buffer, True);
- if (size == (size_t)-1 || !buffer) {
+ if (!convert_string_allocate(NULL, CH_UNIX, CH_UTF16LE, src, srclen,
+ (void **)(void *)&buffer, &size, True)) {
smb_panic("failed to create UCS2 buffer");
}
if (!strlower_w(buffer) && (dest == src)) {
size_t push_ascii_allocate(char **dest, const char *src)
{
- size_t src_len = strlen(src)+1;
+ size_t dest_len, src_len = strlen(src)+1;
*dest = NULL;
- return convert_string_allocate(NULL, CH_UNIX, CH_DOS, src, src_len, (void **)dest, True);
+ 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;
}
/**
}
}
- dest_len = convert_string_allocate(ctx,
- CH_DOS,
- CH_UNIX,
- src,
- src_len,
- &dest,
- True);
-
- if (dest_len == (size_t)-1) {
+ if (!convert_string_allocate(ctx, CH_DOS, CH_UNIX, src, src_len, &dest,
+ &dest_len, True))
dest_len = 0;
- }
if (dest_len && dest) {
/* Did we already process the terminating zero ? */
size_t push_ucs2_allocate(smb_ucs2_t **dest, const char *src)
{
- size_t src_len = strlen(src)+1;
+ size_t dest_len, src_len = strlen(src)+1;
*dest = NULL;
- return convert_string_allocate(NULL, CH_UNIX, CH_UTF16LE, src, src_len, (void **)dest, True);
+ 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;
}
/**
size_t push_utf8_allocate(char **dest, const char *src)
{
- size_t src_len = strlen(src)+1;
+ size_t dest_len, src_len = strlen(src)+1;
*dest = NULL;
- return convert_string_allocate(NULL, CH_UNIX, CH_UTF8, src, src_len, (void **)dest, True);
+ 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;
}
/**
size_t pull_ucs2_allocate(char **dest, const smb_ucs2_t *src)
{
- size_t src_len = (strlen_w(src)+1) * sizeof(smb_ucs2_t);
+ size_t dest_len, src_len = (strlen_w(src)+1) * sizeof(smb_ucs2_t);
*dest = NULL;
- return convert_string_allocate(NULL, CH_UTF16LE, CH_UNIX, src, src_len, (void **)dest, True);
+ 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;
}
/**
size_t pull_utf8_allocate(char **dest, const char *src)
{
- size_t src_len = strlen(src)+1;
+ size_t dest_len, src_len = strlen(src)+1;
*dest = NULL;
- return convert_string_allocate(NULL, CH_UTF8, CH_UNIX, src, src_len, (void **)dest, True);
+ 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;
}
/**