diff options
Diffstat (limited to 'lib/util/charset')
-rw-r--r-- | lib/util/charset/charcnv.c | 42 | ||||
-rw-r--r-- | lib/util/charset/charset.h | 14 | ||||
-rw-r--r-- | lib/util/charset/tests/iconv.c | 11 | ||||
-rw-r--r-- | lib/util/charset/util_unistr.c | 56 |
4 files changed, 77 insertions, 46 deletions
diff --git a/lib/util/charset/charcnv.c b/lib/util/charset/charcnv.c index 18be2bcac2..258730ec82 100644 --- a/lib/util/charset/charcnv.c +++ b/lib/util/charset/charcnv.c @@ -186,7 +186,7 @@ convert: destlen = 2 + (destlen*3); ob = talloc_realloc(ctx, outbuf, char, destlen); if (!ob) { - DEBUG(0, ("convert_string_talloc: realloc failed!\n")); + DEBUG(0, ("iconv_talloc: realloc failed!\n")); talloc_free(outbuf); return (size_t)-1; } else { @@ -237,10 +237,11 @@ convert: * @param destlen maximal length allowed for string * @returns the number of bytes occupied in the destination **/ -_PUBLIC_ ssize_t convert_string_convenience(struct smb_iconv_convenience *ic, +_PUBLIC_ bool convert_string_convenience(struct smb_iconv_convenience *ic, charset_t from, charset_t to, void const *src, size_t srclen, - void *dest, size_t destlen, bool allow_badcharcnv) + void *dest, size_t destlen, size_t *converted_size, + bool allow_badcharcnv) { size_t i_len, o_len; size_t retval; @@ -249,7 +250,8 @@ _PUBLIC_ ssize_t convert_string_convenience(struct smb_iconv_convenience *ic, smb_iconv_t descriptor; if (allow_badcharcnv) { - return -1; + /* Not implemented yet */ + return false; } if (srclen == (size_t)-1) @@ -261,7 +263,8 @@ _PUBLIC_ ssize_t convert_string_convenience(struct smb_iconv_convenience *ic, /* conversion not supported, use as is */ size_t len = MIN(srclen,destlen); memcpy(dest,src,len); - return len; + *converted_size = len; + return true; } i_len=srclen; @@ -272,7 +275,7 @@ _PUBLIC_ ssize_t convert_string_convenience(struct smb_iconv_convenience *ic, switch(errno) { case EINVAL: reason="Incomplete multibyte sequence"; - return -1; + return false; case E2BIG: reason="No more room"; if (from == CH_UNIX) { @@ -285,14 +288,16 @@ _PUBLIC_ ssize_t convert_string_convenience(struct smb_iconv_convenience *ic, charset_name(ic, from), charset_name(ic, to), (int)srclen, (int)destlen)); } - return -1; + return false; case EILSEQ: reason="Illegal multibyte sequence"; - return -1; + return false; } /* smb_panic(reason); */ } - return destlen-o_len; + if (converted_size != NULL) + *converted_size = destlen-o_len; + return true; } /** @@ -305,21 +310,23 @@ _PUBLIC_ ssize_t convert_string_convenience(struct smb_iconv_convenience *ic, * @returns Size in bytes of the converted string; or -1 in case of error. **/ -_PUBLIC_ ssize_t convert_string_talloc_convenience(TALLOC_CTX *ctx, +_PUBLIC_ bool convert_string_talloc_convenience(TALLOC_CTX *ctx, struct smb_iconv_convenience *ic, charset_t from, charset_t to, void const *src, size_t srclen, - void **dest, bool allow_badcharcnv) + void **dest, size_t *converted_size, + bool allow_badcharcnv) { smb_iconv_t descriptor; + ssize_t ret; if (allow_badcharcnv) - return (size_t)-1; + return false; /* Not implemented yet */ *dest = NULL; if (src == NULL || srclen == (size_t)-1 || srclen == 0) - return (size_t)-1; + return false; descriptor = get_conv_handle(ic, from, to); @@ -328,10 +335,15 @@ _PUBLIC_ ssize_t convert_string_talloc_convenience(TALLOC_CTX *ctx, DEBUG(3, ("convert_string_talloc: conversion from %s to %s not supported!\n", charset_name(ic, from), charset_name(ic, to))); - return -1; + return false; } - return iconv_talloc(ctx, descriptor, src, srclen, dest); + ret = iconv_talloc(ctx, descriptor, src, srclen, dest); + if (ret == -1) + return false; + if (converted_size != NULL) + *converted_size = ret; + return true; } /* diff --git a/lib/util/charset/charset.h b/lib/util/charset/charset.h index 3acdde30ad..1f24f8985f 100644 --- a/lib/util/charset/charset.h +++ b/lib/util/charset/charset.h @@ -115,10 +115,11 @@ ssize_t pull_utf8_talloc(TALLOC_CTX *ctx, char **dest, const char *src); ssize_t push_string(void *dest, const char *src, size_t dest_len, int flags); ssize_t pull_string(char *dest, const void *src, size_t dest_len, size_t src_len, int flags); -ssize_t convert_string_talloc(TALLOC_CTX *ctx, +bool convert_string_talloc(TALLOC_CTX *ctx, charset_t from, charset_t to, void const *src, size_t srclen, - void **dest, bool allow_badcharcnv); + void **dest, size_t *converted_size, + bool allow_badcharcnv); size_t convert_string(charset_t from, charset_t to, void const *src, size_t srclen, @@ -148,15 +149,16 @@ struct smb_iconv_convenience *smb_iconv_convenience_init(TALLOC_CTX *mem_ctx, const char *unix_charset, bool native_iconv); -ssize_t convert_string_convenience(struct smb_iconv_convenience *ic, +bool convert_string_convenience(struct smb_iconv_convenience *ic, charset_t from, charset_t to, void const *src, size_t srclen, - void *dest, size_t destlen, bool allow_badcharcnv); -ssize_t convert_string_talloc_convenience(TALLOC_CTX *ctx, + void *dest, size_t destlen, size_t *converted_size, + bool allow_badcharcnv); +bool convert_string_talloc_convenience(TALLOC_CTX *ctx, struct smb_iconv_convenience *ic, charset_t from, charset_t to, void const *src, size_t srclen, - void **dest, bool allow_badcharcnv); + void **dest, size_t *converted_size, bool allow_badcharcnv); /* iconv */ smb_iconv_t smb_iconv_open(const char *tocode, const char *fromcode); int smb_iconv_close(smb_iconv_t cd); diff --git a/lib/util/charset/tests/iconv.c b/lib/util/charset/tests/iconv.c index fbe7b103ab..091876f63b 100644 --- a/lib/util/charset/tests/iconv.c +++ b/lib/util/charset/tests/iconv.c @@ -404,12 +404,12 @@ static bool test_string2key(struct torture_context *tctx) uint16_t *buf; char *dest = NULL; TALLOC_CTX *mem_ctx = talloc_new(tctx); - ssize_t ret; size_t len = (random()%1000)+1; const uint16_t in1[10] = { 'a', 0xd805, 'b', 0xdcf0, 'c', 0, 'd', 'e', 'f', 'g' }; uint8_t le1[20]; uint8_t *munged1; uint8_t *out1; + size_t ret; int i; const char *correct = "a\357\277\275b\357\277\275c\001defg"; @@ -418,8 +418,7 @@ static bool test_string2key(struct torture_context *tctx) torture_comment(tctx, "converting random buffer\n"); - ret = convert_string_talloc(mem_ctx, CH_UTF16MUNGED, CH_UTF8, (void *)buf, len*2, (void**)&dest, false); - if (ret == -1) { + if (!convert_string_talloc(mem_ctx, CH_UTF16MUNGED, CH_UTF8, (void *)buf, len*2, (void**)&dest, &ret, false)) { torture_fail(tctx, "Failed to convert random buffer\n"); } @@ -429,8 +428,7 @@ static bool test_string2key(struct torture_context *tctx) torture_comment(tctx, "converting fixed buffer to UTF16\n"); - ret = convert_string_talloc(mem_ctx, CH_UTF16MUNGED, CH_UTF16, (void *)le1, 20, (void**)&munged1, false); - if (ret == -1) { + if (!convert_string_talloc(mem_ctx, CH_UTF16MUNGED, CH_UTF16, (void *)le1, 20, (void**)&munged1, &ret, false)) { torture_fail(tctx, "Failed to convert fixed buffer to UTF16_MUNGED\n"); } @@ -438,8 +436,7 @@ static bool test_string2key(struct torture_context *tctx) torture_comment(tctx, "converting fixed buffer to UTF8\n"); - ret = convert_string_talloc(mem_ctx, CH_UTF16MUNGED, CH_UTF8, (void *)le1, 20, (void**)&out1, false); - if (ret == -1) { + if (!convert_string_talloc(mem_ctx, CH_UTF16MUNGED, CH_UTF8, (void *)le1, 20, (void**)&out1, &ret, false)) { torture_fail(tctx, "Failed to convert fixed buffer to UTF8\n"); } diff --git a/lib/util/charset/util_unistr.c b/lib/util/charset/util_unistr.c index 96c987393f..13178ea5cc 100644 --- a/lib/util/charset/util_unistr.c +++ b/lib/util/charset/util_unistr.c @@ -670,9 +670,11 @@ static ssize_t push_ascii(void *dest, const char *src, size_t dest_len, int flag **/ _PUBLIC_ ssize_t push_ascii_talloc(TALLOC_CTX *ctx, char **dest, const char *src) { - size_t src_len = strlen(src)+1; + size_t src_len = strlen(src)+1, ret; *dest = NULL; - return convert_string_talloc(ctx, CH_UNIX, CH_DOS, src, src_len, (void **)dest, false); + if (!convert_string_talloc(ctx, CH_UNIX, CH_DOS, src, src_len, (void **)dest, &ret, false)) + return -1; + return (ssize_t)ret; } @@ -781,9 +783,11 @@ static ssize_t push_ucs2(void *dest, const char *src, size_t dest_len, int flags **/ _PUBLIC_ ssize_t push_ucs2_talloc(TALLOC_CTX *ctx, void **dest, const char *src) { - size_t src_len = strlen(src)+1; + size_t src_len = strlen(src)+1, ret; *dest = NULL; - return convert_string_talloc(ctx, CH_UNIX, CH_UTF16, src, src_len, dest, false); + if (!convert_string_talloc(ctx, CH_UNIX, CH_UTF16, src, src_len, dest, &ret, false)) + return -1; + return ret; } @@ -797,9 +801,11 @@ _PUBLIC_ ssize_t push_ucs2_talloc(TALLOC_CTX *ctx, void **dest, const char *src) _PUBLIC_ ssize_t push_utf8_talloc(TALLOC_CTX *ctx, char **dest, const char *src) { - size_t src_len = strlen(src)+1; + size_t src_len = strlen(src)+1, ret; *dest = NULL; - return convert_string_talloc(ctx, CH_UNIX, CH_UTF8, src, src_len, (void **)dest, false); + if (!convert_string_talloc(ctx, CH_UNIX, CH_UTF8, src, src_len, (void **)dest, &ret, false)) + return -1; + return ret; } /** @@ -852,9 +858,11 @@ static size_t pull_ucs2(char *dest, const void *src, size_t dest_len, size_t src _PUBLIC_ ssize_t pull_ascii_talloc(TALLOC_CTX *ctx, char **dest, const char *src) { - size_t src_len = strlen(src)+1; + size_t src_len = strlen(src)+1, ret; *dest = NULL; - return convert_string_talloc(ctx, CH_DOS, CH_UNIX, src, src_len, (void **)dest, false); + if (!convert_string_talloc(ctx, CH_DOS, CH_UNIX, src, src_len, (void **)dest, &ret, false)) + return -1; + return ret; } /** @@ -867,9 +875,11 @@ _PUBLIC_ ssize_t pull_ascii_talloc(TALLOC_CTX *ctx, char **dest, const char *src _PUBLIC_ ssize_t pull_ucs2_talloc(TALLOC_CTX *ctx, char **dest, const void *src) { - size_t src_len = utf16_len(src); + size_t src_len = utf16_len(src), ret; *dest = NULL; - return convert_string_talloc(ctx, CH_UTF16, CH_UNIX, src, src_len, (void **)dest, false); + if (!convert_string_talloc(ctx, CH_UTF16, CH_UNIX, src, src_len, (void **)dest, &ret, false)) + return -1; + return ret; } /** @@ -882,9 +892,11 @@ _PUBLIC_ ssize_t pull_ucs2_talloc(TALLOC_CTX *ctx, char **dest, const void *src) _PUBLIC_ ssize_t pull_utf8_talloc(TALLOC_CTX *ctx, char **dest, const char *src) { - size_t src_len = strlen(src)+1; + size_t src_len = strlen(src)+1, ret; *dest = NULL; - return convert_string_talloc(ctx, CH_UTF8, CH_UNIX, src, src_len, (void **)dest, false); + if (!convert_string_talloc(ctx, CH_UTF8, CH_UNIX, src, src_len, (void **)dest, &ret, false)) + return -1; + return ret; } /** @@ -952,11 +964,16 @@ _PUBLIC_ ssize_t pull_string(char *dest, const void *src, size_t dest_len, size_ **/ _PUBLIC_ size_t convert_string(charset_t from, charset_t to, void const *src, size_t srclen, - void *dest, size_t destlen, bool allow_badcharcnv) + void *dest, size_t destlen, + bool allow_badcharcnv) { - return convert_string_convenience(get_iconv_convenience(), from, to, + size_t ret; + if (!convert_string_convenience(get_iconv_convenience(), from, to, src, srclen, - dest, destlen, allow_badcharcnv); + dest, destlen, &ret, + allow_badcharcnv)) + return -1; + return ret; } /** @@ -964,18 +981,21 @@ _PUBLIC_ size_t convert_string(charset_t from, charset_t to, * * @param srclen length of source buffer. * @param dest always set at least to NULL + * @param converted_size Size in bytes of the converted string * @note -1 is not accepted for srclen. * - * @returns Size in bytes of the converted string; or -1 in case of error. + * @returns boolean indication whether the conversion succeeded **/ -_PUBLIC_ ssize_t convert_string_talloc(TALLOC_CTX *ctx, +_PUBLIC_ bool convert_string_talloc(TALLOC_CTX *ctx, charset_t from, charset_t to, void const *src, size_t srclen, - void **dest, bool allow_badcharcnv) + void **dest, size_t *converted_size, + bool allow_badcharcnv) { return convert_string_talloc_convenience(ctx, get_iconv_convenience(), from, to, src, srclen, dest, + converted_size, allow_badcharcnv); } |