1
0
mirror of https://github.com/samba-team/samba.git synced 2025-01-11 05:18:09 +03:00

lib/util/charset use convert_string.c in common

This brings another layer of the charcnv library in common.

Andrew Bartlett

Signed-off-by: Andrew Tridgell <tridge@samba.org>
This commit is contained in:
Andrew Bartlett 2011-04-12 14:49:41 +10:00
parent 75d5ba4109
commit 4081ea5b49
5 changed files with 6 additions and 205 deletions

View File

@ -113,138 +113,3 @@ convert:
return destlen;
}
/**
* 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
* @returns the number of bytes occupied in the destination
* on error, returns -1, and sets errno
**/
_PUBLIC_ 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)
{
size_t i_len, o_len;
ssize_t retval;
const char* inbuf = (const char*)src;
char* outbuf = (char*)dest;
smb_iconv_t descriptor;
if (srclen == (size_t)-1)
srclen = strlen(inbuf)+1;
descriptor = get_conv_handle(ic, from, to);
if (descriptor == (smb_iconv_t)-1 || descriptor == (smb_iconv_t)0) {
if (converted_size) {
*converted_size = 0;
}
errno = EINVAL;
return -1;
}
i_len=srclen;
o_len=destlen;
retval = smb_iconv(descriptor, &inbuf, &i_len, &outbuf, &o_len);
if (converted_size != NULL)
*converted_size = destlen-o_len;
return (retval != (ssize_t)-1);
}
/**
* 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
* @returns the number of bytes occupied in the destination
**/
_PUBLIC_ 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 retval;
retval = convert_string_error_handle(ic, from, to, src, srclen, dest, destlen, converted_size);
if(retval==false) {
const char *reason;
switch(errno) {
case EINVAL:
reason="Incomplete multibyte sequence";
return false;
case E2BIG:
reason="No more room";
if (from == CH_UNIX) {
DEBUG(0,("E2BIG: convert_string_handle(%s,%s): srclen=%d destlen=%d - '%s'\n",
charset_name(ic, from), charset_name(ic, to),
(int)srclen, (int)destlen,
(const char *)src));
} else {
DEBUG(0,("E2BIG: convert_string_handle(%s,%s): srclen=%d destlen=%d\n",
charset_name(ic, from), charset_name(ic, to),
(int)srclen, (int)destlen));
}
return false;
case EILSEQ:
reason="Illegal multibyte sequence";
return false;
default:
return false;
}
}
return true;
}
/**
* 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
* @note -1 is not accepted for srclen.
*
* @returns Size in bytes of the converted string; or -1 in case of error.
**/
_PUBLIC_ 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)
{
void **dest = (void **)dst;
smb_iconv_t descriptor;
ssize_t ret;
*dest = NULL;
if (src == NULL || srclen == (size_t)-1 || srclen == 0)
return false;
descriptor = get_conv_handle(ic, from, to);
if (descriptor == (smb_iconv_t)-1 || descriptor == (smb_iconv_t)0) {
/* conversion not supported, return -1*/
DEBUG(3, ("convert_string_talloc_handle: conversion from %s to %s not supported!\n",
charset_name(ic, from),
charset_name(ic, to)));
return false;
}
ret = iconv_talloc(ctx, descriptor, src, srclen, dest);
if (ret == -1)
return false;
if (converted_size != NULL)
*converted_size = ret;
return true;
}

View File

@ -21,6 +21,7 @@
*/
#include "includes.h"
#include "system/iconv.h"
/**
* @file
@ -387,7 +388,7 @@ bool convert_string_talloc_handle(TALLOC_CTX *ctx, struct smb_iconv_handle *ic,
}
/* +2 is for ucs2 null termination. */
ob = (char *)TALLOC_REALLOC(ctx, ob, destlen + 2);
ob = talloc_realloc(ctx, ob, char, destlen + 2);
if (!ob) {
DEBUG(0, ("convert_string_talloc: realloc failed!\n"));
@ -428,7 +429,7 @@ bool convert_string_talloc_handle(TALLOC_CTX *ctx, struct smb_iconv_handle *ic,
*/
if (o_len > 1024) {
/* We're shrinking here so we know the +2 is safe from wrap. */
ob = (char *)TALLOC_REALLOC(ctx,ob,destlen + 2);
ob = talloc_realloc(ctx,ob, char, destlen + 2);
}
if (destlen && !ob) {

View File

@ -585,68 +585,3 @@ _PUBLIC_ ssize_t pull_string(char *dest, const void *src, size_t dest_len, size_
return -1;
}
}
/**
* 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);
}

View File

@ -3,7 +3,7 @@
if bld.env._SAMBA_BUILD_ == 4:
bld.SAMBA_SUBSYSTEM('CHARSET',
source='charcnv.c util_unistr.c',
source='util_unistr.c',
public_deps='CODEPOINTS',
public_headers='charset.h',
)
@ -13,6 +13,6 @@ bld.SAMBA_SUBSYSTEM('ICONV_WRAPPER',
public_deps='iconv replace talloc')
bld.SAMBA_SUBSYSTEM('CODEPOINTS',
source='codepoints.c util_str.c util_unistr_w.c',
source='codepoints.c convert_string.c util_str.c util_unistr_w.c charcnv.c',
deps='DYNCONFIG ICONV_WRAPPER'
)

View File

@ -963,7 +963,7 @@ bld.SAMBA3_SUBSYSTEM('tdb-wrap3',
vars=locals())
bld.SAMBA3_SUBSYSTEM('CHARSET3',
source='''lib/util_str.c lib/charcnv.c ../lib/util/charset/convert_string.c lib/fstring.c''',
source='''lib/util_str.c lib/charcnv.c lib/fstring.c''',
public_deps='ICONV_WRAPPER CODEPOINTS',
deps='samba-util')