From 79e6fea77443099a6ebe4a2e3bbf606cd444341d Mon Sep 17 00:00:00 2001 From: Andrew Bartlett Date: Tue, 12 Apr 2011 14:36:17 +1000 Subject: lib/util/charset create _handle functions for convert_string() et al This is now API compatible with the existing code in lib/util/charset lazy_initialize_conv() is no longer called as init_iconv() is called when the smb.conf is processed, and get_conv_handle() will auto-init with defaults if required. load_case_tables_library() is no longer requried as all binaries and libraries already load these in their entry points, as otherwise all the other string functions would fail. Andrew Bartlett --- lib/util/charset/convert_string.c | 115 ++++++++++++++++++++++++++++---------- source3/include/proto.h | 1 - source3/lib/charcnv.c | 12 ---- 3 files changed, 86 insertions(+), 42 deletions(-) diff --git a/lib/util/charset/convert_string.c b/lib/util/charset/convert_string.c index 86bb625fe1..14dad61aeb 100644 --- a/lib/util/charset/convert_string.c +++ b/lib/util/charset/convert_string.c @@ -54,19 +54,17 @@ * **/ -static bool convert_string_internal(charset_t from, charset_t to, - void const *src, size_t srclen, - void *dest, size_t destlen, size_t *converted_size) +static bool convert_string_internal(struct smb_iconv_handle *ic, + charset_t from, charset_t to, + void const *src, size_t srclen, + void *dest, size_t destlen, size_t *converted_size) { size_t i_len, o_len; size_t retval; const char* inbuf = (const char*)src; char* outbuf = (char*)dest; smb_iconv_t descriptor; - struct smb_iconv_handle *ic; - lazy_initialize_conv(); - ic = get_iconv_handle(); descriptor = get_conv_handle(ic, from, to); if (srclen == (size_t)-1) { @@ -112,10 +110,11 @@ static bool convert_string_internal(charset_t from, charset_t to, * Don't change unless you really know what you are doing. JRA. **/ -bool convert_string_error(charset_t from, charset_t to, - void const *src, size_t srclen, - void *dest, size_t destlen, - size_t *converted_size) +bool convert_string_error_handle(struct smb_iconv_handle *ic, + charset_t from, charset_t to, + void const *src, size_t srclen, + void *dest, size_t destlen, + size_t *converted_size) { /* * NB. We deliberately don't do a strlen here if srclen == -1. @@ -155,7 +154,7 @@ bool convert_string_error(charset_t from, charset_t to, #ifdef BROKEN_UNICODE_COMPOSE_CHARACTERS goto general_case; #else - bool ret = convert_string_internal(from, to, p, slen, q, dlen, converted_size); + bool ret = convert_string_internal(ic, from, to, p, slen, q, dlen, converted_size); *converted_size += retval; return ret; #endif @@ -197,7 +196,7 @@ bool convert_string_error(charset_t from, charset_t to, #ifdef BROKEN_UNICODE_COMPOSE_CHARACTERS goto general_case; #else - bool ret = convert_string_internal(from, to, p, slen, q, dlen, converted_size); + bool ret = convert_string_internal(ic, from, to, p, slen, q, dlen, converted_size); *converted_size += retval; return ret; #endif @@ -239,7 +238,7 @@ bool convert_string_error(charset_t from, charset_t to, #ifdef BROKEN_UNICODE_COMPOSE_CHARACTERS goto general_case; #else - bool ret = convert_string_internal(from, to, p, slen, q, dlen, converted_size); + bool ret = convert_string_internal(ic, from, to, p, slen, q, dlen, converted_size); *converted_size += retval; return ret; #endif @@ -262,15 +261,16 @@ bool convert_string_error(charset_t from, charset_t to, #ifdef BROKEN_UNICODE_COMPOSE_CHARACTERS general_case: #endif - return convert_string_internal(from, to, src, srclen, dest, destlen, converted_size); + return convert_string_internal(ic, from, to, src, srclen, dest, destlen, converted_size); } -bool convert_string(charset_t from, charset_t to, - void const *src, size_t srclen, - void *dest, size_t destlen, - size_t *converted_size) +bool convert_string_handle(struct smb_iconv_handle *ic, + charset_t from, charset_t to, + void const *src, size_t srclen, + void *dest, size_t destlen, + size_t *converted_size) { - bool ret = convert_string_error(from, to, src, srclen, dest, destlen, converted_size); + bool ret = convert_string_error_handle(ic, from, to, src, srclen, dest, destlen, converted_size); if(ret==false) { const char *reason="unknown error"; @@ -282,10 +282,6 @@ bool convert_string(charset_t from, charset_t to, break; case E2BIG: { - struct smb_iconv_handle *ic; - lazy_initialize_conv(); - ic = get_iconv_handle(); - reason="No more room"; if (from == CH_UNIX) { DEBUG(3,("E2BIG: convert_string(%s,%s): srclen=%u destlen=%u - '%s'\n", @@ -331,9 +327,10 @@ bool convert_string(charset_t from, charset_t to, * I hate the goto's in this function. It's embarressing..... * There has to be a cleaner way to do this. JRA. */ -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 convert_string_talloc_handle(TALLOC_CTX *ctx, struct smb_iconv_handle *ic, + charset_t from, charset_t to, + void const *src, size_t srclen, void *dst, + size_t *converted_size) { size_t i_len, o_len, destlen = (srclen * 3) / 2; @@ -342,7 +339,6 @@ bool convert_string_talloc(TALLOC_CTX *ctx, charset_t from, charset_t to, char *outbuf = NULL, *ob = NULL; smb_iconv_t descriptor; void **dest = (void **)dst; - struct smb_iconv_handle *ic; *dest = NULL; @@ -371,8 +367,6 @@ bool convert_string_talloc(TALLOC_CTX *ctx, charset_t from, charset_t to, return true; } - lazy_initialize_conv(); - ic = get_iconv_handle(); descriptor = get_conv_handle(ic, from, to); if (descriptor == (smb_iconv_t)-1 || descriptor == (smb_iconv_t)0) { @@ -465,3 +459,66 @@ bool convert_string_talloc(TALLOC_CTX *ctx, charset_t from, charset_t to, *converted_size = destlen; return true; } + +/** + * Convert string from one encoding to another, making error checking etc + * + * @param src pointer to source string (multibyte or singlebyte) + * @param srclen length of the source string in bytes + * @param dest pointer to destination string (multibyte or singlebyte) + * @param destlen maximal length allowed for string + * @param converted_size the number of bytes occupied in the destination + * + * @returns true on success, false on fail. + **/ +_PUBLIC_ bool convert_string(charset_t from, charset_t to, + void const *src, size_t srclen, + void *dest, size_t destlen, + size_t *converted_size) +{ + return convert_string_handle(get_iconv_handle(), from, to, + src, srclen, + dest, destlen, converted_size); +} + +/** + * Convert string from one encoding to another, making error checking etc + * + * @param src pointer to source string (multibyte or singlebyte) + * @param srclen length of the source string in bytes + * @param dest pointer to destination string (multibyte or singlebyte) + * @param destlen maximal length allowed for string + * @param converted_size the number of bytes occupied in the destination + * + * @returns true on success, false on fail. + **/ +_PUBLIC_ bool convert_string_error(charset_t from, charset_t to, + void const *src, size_t srclen, + void *dest, size_t destlen, + size_t *converted_size) +{ + return convert_string_error_handle(get_iconv_handle(), from, to, + src, srclen, + dest, destlen, converted_size); +} + +/** + * Convert between character sets, allocating a new buffer using talloc for the result. + * + * @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 boolean indication whether the conversion succeeded + **/ + +_PUBLIC_ bool convert_string_talloc(TALLOC_CTX *ctx, + charset_t from, charset_t to, + void const *src, size_t srclen, + void *dest, size_t *converted_size) +{ + return convert_string_talloc_handle(ctx, get_iconv_handle(), + from, to, src, srclen, dest, + converted_size); +} diff --git a/source3/include/proto.h b/source3/include/proto.h index 2b6e168248..d4ab13d497 100644 --- a/source3/include/proto.h +++ b/source3/include/proto.h @@ -72,7 +72,6 @@ int bitmap_find(struct bitmap *bm, unsigned ofs); /* The following definitions come from lib/charcnv.c */ -void lazy_initialize_conv(void); void gfree_charcnv(void); void init_iconv(void); bool convert_string(charset_t from, charset_t to, diff --git a/source3/lib/charcnv.c b/source3/lib/charcnv.c index bde6510314..f6fed8d1ec 100644 --- a/source3/lib/charcnv.c +++ b/source3/lib/charcnv.c @@ -22,24 +22,12 @@ */ #include "includes.h" -static bool initialized; - -void lazy_initialize_conv(void) -{ - if (!initialized) { - load_case_tables_library(); - init_iconv(); - initialized = true; - } -} - /** * Destroy global objects allocated by init_iconv() **/ void gfree_charcnv(void) { TALLOC_FREE(global_iconv_handle); - initialized = false; } /** -- cgit