From 80f1d49b61560f326fb55f2df09cc4f30e7a85ea Mon Sep 17 00:00:00 2001 From: Andrew Bartlett Date: Tue, 3 May 2011 12:29:12 +1000 Subject: lib/util/charset Use push_string and talloc_strupper/strlower from common code The only caller of push_string() (not to be confused with push_string_check()) in the common code was encode_pw_buffer(), and it didn't use the alignment or STR_UPPER flags. The talloc_strupper() and talloc_strlower() functions are tested in smbtorture, and are next_codepoint() based. Andrew Bartlett --- lib/util/charset/util_unistr.c | 10 ++-- lib/util/charset/wscript_build | 17 ++---- source3/Makefile.in | 1 + source3/include/proto.h | 4 -- source3/lib/charcnv.c | 122 ----------------------------------------- source3/wscript_build | 3 +- 6 files changed, 12 insertions(+), 145 deletions(-) diff --git a/lib/util/charset/util_unistr.c b/lib/util/charset/util_unistr.c index 1915c73ad5..e4ae65053c 100644 --- a/lib/util/charset/util_unistr.c +++ b/lib/util/charset/util_unistr.c @@ -194,7 +194,7 @@ _PUBLIC_ size_t count_chars_m(const char *s, char c) * @param dest_len the maximum length in bytes allowed in the * destination. If @p dest_len is -1 then no maximum is used. **/ -static bool push_ascii(void *dest, const char *src, size_t dest_len, int flags, size_t *converted_size) +static bool push_ascii_string(void *dest, const char *src, size_t dest_len, int flags, size_t *converted_size) { size_t src_len; bool ret; @@ -204,7 +204,7 @@ static bool push_ascii(void *dest, const char *src, size_t dest_len, int flags, if (tmpbuf == NULL) { return false; } - ret = push_ascii(dest, tmpbuf, dest_len, flags & ~STR_UPPER, converted_size); + ret = push_ascii_string(dest, tmpbuf, dest_len, flags & ~STR_UPPER, converted_size); talloc_free(tmpbuf); return ret; } @@ -232,7 +232,7 @@ static bool push_ascii(void *dest, const char *src, size_t dest_len, int flags, * @param src_len is the length of the source area in bytes. * @returns the number of bytes occupied by the string in @p src. **/ -static ssize_t pull_ascii(char *dest, const void *src, size_t dest_len, size_t src_len, int flags) +static ssize_t pull_ascii_string(char *dest, const void *src, size_t dest_len, size_t src_len, int flags) { size_t size = 0; @@ -373,7 +373,7 @@ _PUBLIC_ ssize_t push_string(void *dest, const char *src, size_t dest_len, int f { if (flags & STR_ASCII) { size_t size = 0; - if (push_ascii(dest, src, dest_len, flags, &size)) { + if (push_ascii_string(dest, src, dest_len, flags, &size)) { return (ssize_t)size; } else { return (ssize_t)-1; @@ -404,7 +404,7 @@ _PUBLIC_ ssize_t push_string(void *dest, const char *src, size_t dest_len, int f _PUBLIC_ ssize_t pull_string(char *dest, const void *src, size_t dest_len, size_t src_len, int flags) { if (flags & STR_ASCII) { - return pull_ascii(dest, src, dest_len, src_len, flags); + return pull_ascii_string(dest, src, dest_len, src_len, flags); } else if (flags & STR_UNICODE) { return pull_ucs2(dest, src, dest_len, src_len, flags); } else { diff --git a/lib/util/charset/wscript_build b/lib/util/charset/wscript_build index 771ff5dc24..7623131146 100644 --- a/lib/util/charset/wscript_build +++ b/lib/util/charset/wscript_build @@ -1,18 +1,11 @@ #!/usr/bin/env python - -if bld.env._SAMBA_BUILD_ == 4: - bld.SAMBA_SUBSYSTEM('CHARSET', - source='util_unistr.c', - public_deps='CODEPOINTS', - public_headers='charset.h', - ) - bld.SAMBA_SUBSYSTEM('ICONV_WRAPPER', source='iconv.c', public_deps='iconv replace talloc') -bld.SAMBA_SUBSYSTEM('CODEPOINTS', - source='codepoints.c convert_string.c util_str.c util_unistr_w.c charcnv.c pull_push.c', - deps='DYNCONFIG ICONV_WRAPPER' - ) +bld.SAMBA_SUBSYSTEM('CHARSET', + public_headers='charset.h', + source='codepoints.c convert_string.c util_str.c util_unistr_w.c charcnv.c pull_push.c util_unistr.c', + deps='DYNCONFIG ICONV_WRAPPER' + ) diff --git a/source3/Makefile.in b/source3/Makefile.in index 6893e01f2c..5e7b63dae9 100644 --- a/source3/Makefile.in +++ b/source3/Makefile.in @@ -458,6 +458,7 @@ LIB_OBJ = $(LIBSAMBAUTIL_OBJ) $(UTIL_OBJ) $(CRYPTO_OBJ) \ lib/wins_srv.o \ lib/util_str.o ../lib/util/util_str_common.o \ ../lib/util/base64.o lib/util_sid.o \ + ../lib/util/charset/util_unistr.o \ ../lib/util/charset/util_unistr_w.o ../lib/util/charset/codepoints.o ../lib/util/charset/util_str.o lib/util_file.o \ lib/util.o lib/util_cmdline.o lib/util_names.o \ lib/util_sock.o lib/sock_exec.o lib/util_sec.o \ diff --git a/source3/include/proto.h b/source3/include/proto.h index 086ba6c557..e8ce418ab2 100644 --- a/source3/include/proto.h +++ b/source3/include/proto.h @@ -82,10 +82,6 @@ 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); -char *talloc_strdup_upper(TALLOC_CTX *ctx, const char *s); -char *strupper_talloc(TALLOC_CTX *ctx, const char *s); -char *talloc_strdup_lower(TALLOC_CTX *ctx, const char *s); -char *strlower_talloc(TALLOC_CTX *ctx, const char *s); size_t ucs2_align(const void *base_ptr, const void *p, int flags); size_t push_ascii(void *dest, const char *src, size_t dest_len, int flags); size_t push_ascii_fstring(void *dest, const char *src); diff --git a/source3/lib/charcnv.c b/source3/lib/charcnv.c index 6b667502da..765e3943ef 100644 --- a/source3/lib/charcnv.c +++ b/source3/lib/charcnv.c @@ -44,98 +44,6 @@ void init_iconv(void) true, global_iconv_handle); } -/** - talloc_strdup() a unix string to upper case. -**/ - -char *talloc_strdup_upper(TALLOC_CTX *ctx, const char *s) -{ - char *out_buffer = talloc_strdup(ctx,s); - const unsigned char *p = (const unsigned char *)s; - unsigned char *q = (unsigned char *)out_buffer; - - if (!q) { - return NULL; - } - - /* this is quite a common operation, so we want it to be - fast. We optimise for the ascii case, knowing that all our - supported multi-byte character sets are ascii-compatible - (ie. they match for the first 128 chars) */ - - while (*p) { - if (*p & 0x80) - break; - *q++ = toupper_ascii_fast(*p); - p++; - } - - if (*p) { - /* MB case. */ - size_t converted_size, converted_size2; - smb_ucs2_t *ubuf = NULL; - - /* We're not using the ascii buffer above. */ - TALLOC_FREE(out_buffer); - - if (!convert_string_talloc(ctx, CH_UNIX, CH_UTF16LE, s, - strlen(s)+1, (void *)&ubuf, - &converted_size)) - { - return NULL; - } - - strupper_w(ubuf); - - if (!convert_string_talloc(ctx, CH_UTF16LE, CH_UNIX, ubuf, - converted_size, (void *)&out_buffer, - &converted_size2)) - { - TALLOC_FREE(ubuf); - return NULL; - } - - /* Don't need the intermediate buffer - * anymore. - */ - TALLOC_FREE(ubuf); - } - - return out_buffer; -} - -char *strupper_talloc(TALLOC_CTX *ctx, const char *s) { - return talloc_strdup_upper(ctx, s); -} - - -char *talloc_strdup_lower(TALLOC_CTX *ctx, const char *s) -{ - size_t converted_size; - smb_ucs2_t *buffer = NULL; - char *out_buffer; - - if (!push_ucs2_talloc(ctx, &buffer, s, &converted_size)) { - return NULL; - } - - strlower_w(buffer); - - if (!pull_ucs2_talloc(ctx, &out_buffer, buffer, &converted_size)) { - TALLOC_FREE(buffer); - return NULL; - } - - TALLOC_FREE(buffer); - - return out_buffer; -} - -char *strlower_talloc(TALLOC_CTX *ctx, const char *s) { - return talloc_strdup_lower(ctx, s); -} - - /** * Copy a string from a char* unix src to a dos codepage string destination. * @@ -626,36 +534,6 @@ size_t push_string_base(const char *base, uint16 flags2, return push_ascii(dest, src, dest_len, flags); } -/** - Copy a string from a char* src to a unicode or ascii - dos codepage destination choosing unicode or ascii based on the - flags supplied - Return the number of bytes occupied by the string in the destination. - flags can have: - STR_TERMINATE means include the null termination. - STR_UPPER means uppercase in the destination. - STR_ASCII use ascii even with unicode packet. - STR_NOALIGN means don't do alignment. - dest_len is the maximum length allowed in the destination. If dest_len - is -1 then no maxiumum is used. -**/ - -ssize_t push_string(void *dest, const char *src, size_t dest_len, int flags) -{ - size_t ret; - - if (!(flags & STR_ASCII) && \ - (flags & STR_UNICODE)) { - ret = push_ucs2(NULL, dest, src, dest_len, flags); - } else { - ret = push_ascii(dest, src, dest_len, flags); - } - if (ret == (size_t)-1) { - return -1; - } - return ret; -} - /** Copy a string from a unicode or ascii source (depending on the packet flags) to a char* destination. diff --git a/source3/wscript_build b/source3/wscript_build index 83cc39ebe7..fbc6693346 100755 --- a/source3/wscript_build +++ b/source3/wscript_build @@ -964,7 +964,7 @@ bld.SAMBA3_SUBSYSTEM('tdb-wrap3', bld.SAMBA3_SUBSYSTEM('CHARSET3', source='''lib/util_str.c lib/charcnv.c lib/fstring.c''', - public_deps='ICONV_WRAPPER CODEPOINTS', + public_deps='ICONV_WRAPPER CHARSET', deps='samba-util') bld.SAMBA3_SUBSYSTEM('ldb3', @@ -1317,7 +1317,6 @@ if not bld.env.toplevel_build: bld.SAMBA3_SUBSYSTEM('tdb-wrap', source='', deps='tdb-wrap3') bld.SAMBA3_SUBSYSTEM('errors', source='', deps='errors3') bld.SAMBA3_SUBSYSTEM('samba-util', source='', deps='DYNCONFIG') - bld.SAMBA3_SUBSYSTEM('CHARSET', source='', deps='CHARSET3') bld.SAMBA3_SUBSYSTEM('ldb', source='', deps='ldb3') bld.SAMBA3_SUBSYSTEM('dcerpc', '', deps='UTIL_TEVENT') bld.SAMBA3_SUBSYSTEM('cli-ldap', '', deps='UTIL_TEVENT') -- cgit