Back to index

im-sdk  12.3.91
Defines | Typedefs | Functions | Variables
xaux_ext_conv.c File Reference
#include "iiimpAux.h"
#include <locale.h>
#include <langinfo.h>
#include <strings.h>
#include <dlfcn.h>
#include "csconv.h"

Go to the source code of this file.

Defines

#define USE_CSCONV
#define UTF16_STRLEN   4096
#define CSC_OPEN_LOCALE   "csconv_open_locale"
#define CSC_OPEN   "csconv_open"
#define CSC_CONV   "csconv"
#define CSC_CLOSE   "csconv_close"

Typedefs

typedef csconv_t(* csc_open_locale_t )(const char *, const char *, const char *)
typedef csconv_t(* csc_open_t )(const char *, const char *)
typedef size_t(* csc_conv_t )(csconv_t, const char **, size_t *, char **, size_t *)
typedef int(* csc_close_t )(csconv_t)

Functions

static void dlopen_csconv ()
static int IIimpConvertToUTF16 (char *from_buf, size_t from_left, char **to_buf, size_t *to_left)
static int IIimpConvertFromUTF16 (char *from_buf, size_t from_left, char **to_buf, size_t *to_left)
size_t utf16_mb (const char **inbuf, size_t *inbytesleft, char **outbuf, size_t *outbytesleft)
size_t mb_utf16 (const char **inbuf, size_t *inbytesleft, char **outbuf, size_t *outbytesleft)

Variables

static void * csc_handle
static csc_open_locale_t csc_open_locale
static csc_open_t csc_open
static csc_conv_t csc_conv
static csc_close_t csc_close
static char * current_locale = 0

Define Documentation

#define CSC_CLOSE   "csconv_close"

Definition at line 85 of file xaux_ext_conv.c.

#define CSC_CONV   "csconv"

Definition at line 84 of file xaux_ext_conv.c.

#define CSC_OPEN   "csconv_open"

Definition at line 83 of file xaux_ext_conv.c.

#define CSC_OPEN_LOCALE   "csconv_open_locale"

Definition at line 82 of file xaux_ext_conv.c.

#define USE_CSCONV

Definition at line 57 of file xaux_ext_conv.c.

#define UTF16_STRLEN   4096

Definition at line 77 of file xaux_ext_conv.c.


Typedef Documentation

typedef int(* csc_close_t)(csconv_t)

Definition at line 92 of file xaux_ext_conv.c.

typedef size_t(* csc_conv_t)(csconv_t, const char **, size_t *, char **, size_t *)

Definition at line 90 of file xaux_ext_conv.c.

typedef csconv_t(* csc_open_locale_t)(const char *, const char *, const char *)

Definition at line 87 of file xaux_ext_conv.c.

typedef csconv_t(* csc_open_t)(const char *, const char *)

Definition at line 89 of file xaux_ext_conv.c.


Function Documentation

static void dlopen_csconv ( ) [static]

Definition at line 111 of file xaux_ext_conv.c.

{
      csc_handle = dlopen(CSC_PATH, RTLD_LAZY);
      if (NULL == csc_handle) {
        csc_handle = (void *)(-1);
       return;
      }

      csc_open_locale = (csc_open_locale_t)dlsym(csc_handle, CSC_OPEN_LOCALE);
      csc_open = (csc_open_t)dlsym(csc_handle, CSC_OPEN);
      csc_conv = (csc_conv_t)dlsym(csc_handle, CSC_CONV);
      csc_close = (csc_close_t)dlsym(csc_handle, CSC_CLOSE);

      if ((NULL == csc_open_locale) || (NULL == csc_open) ||
         (NULL == csc_conv) || (NULL == csc_close)) {
        dlclose(csc_handle);
       csc_handle = (void *)(-1);
       return;
      }
}

Here is the caller graph for this function:

static int IIimpConvertFromUTF16 ( char *  from_buf,
size_t  from_left,
char **  to_buf,
size_t *  to_left 
) [static]

Definition at line 262 of file xaux_ext_conv.c.

                                                     {
  size_t src_len, dst_len;
  char *src, *dst;
#ifndef USE_CSCONV
  const char *ip;
  size_t ileft;
  char *op;
  size_t oleft;
  char buffer[UTF16_STRLEN];       /* Fix me! */
#endif /* !USE_CSCONV */
  const size_t buf_len = UTF16_STRLEN;
  size_t ret = 0;

#ifdef USE_CSCONV
  static csconv_t csconv_cd = NULL;
#endif

#ifdef USE_CSCONV
  do {
    if (((void *)(-1) == csc_handle) ||
       ((csconv_t)(-1) == csconv_cd)) {
      continue;
    }
    if (NULL == csc_handle) {
      dlopen_csconv();
      if ((void *)(-1) == csc_handle) {
       continue;
      }
    }
    if (NULL == csconv_cd) {

      csconv_cd = (csc_open_locale)(current_locale, "MultiByte", "UTF-16");
      
      if ((csconv_t)(-1) == csconv_cd) {
       continue;
      }
    }

    dst = *((char **)to_buf);

    ret = csc_conv(csconv_cd,
                 (const char **)&from_buf, (size_t*)&from_left,
                 &dst, to_left);
    return ret;
  } while (0);
#endif

#ifdef USE_ICONV
  if (i_conv1_FromUTF16 == (iconv_t)-1 ||
      i_conv2_FromUTF16 == (iconv_t)-1) {
    goto done;
  }

  if (i_conv2_FromUTF16 == NULL && !skip_native_to_utf8_conv) {
    char *encoding = nl_langinfo(CODESET);
    if (!strcmp(encoding, "UTF-8")) {
      skip_native_to_utf8_conv = True;
    } else {
      if ((i_conv2_FromUTF16 = iconv_open(encoding, "UTF-8"))
         == (iconv_t) - 1) {
       goto done;
      }
    }
  }
  if (i_conv1_FromUTF16 == NULL) {
    if ((i_conv1_FromUTF16 = iconv_open("UTF-8", "UCS-2"))
       == (iconv_t) - 1) {
      goto done;
    }
  }

  if (skip_native_to_utf8_conv) {
    src = from_buf;
    src_len = from_left;    /* don't stop at '\0' in case of ascii */
    dst = *((char **) to_buf);
    ret = iconv(i_conv1_FromUTF16, (const char**)&src, (size_t*)&src_len,
              (char**)&dst, (size_t*)to_left);
  } else {
    ip = (const char *)from_buf;
    ileft = from_left;

    op = *((char **)to_buf);
    oleft = (size_t)(*to_left);

    while ((0 < ileft) && (0 < oleft)) {
      dst = buffer;
      dst_len = buf_len;
      ret = iconv(i_conv1_FromUTF16, &ip, &ileft, (char**)&dst, &dst_len);
      if ((ret != 0) && (E2BIG != errno)) {
       goto done;
      }

      src = buffer;
      src_len = buf_len - dst_len;

      ret = iconv(i_conv2_FromUTF16, (const char**)&src, (size_t*)&src_len,
                &op, &oleft);
      if (ret != 0) goto done;
    }

    *to_left = oleft;
  }
done:
#endif /* USE_ICONV */
  return(ret);
}

Here is the call graph for this function:

static int IIimpConvertToUTF16 ( char *  from_buf,
size_t  from_left,
char **  to_buf,
size_t *  to_left 
) [static]

Definition at line 134 of file xaux_ext_conv.c.

                                                  {
  size_t src_len, dst_len;
  char *src, *dst;
#ifndef USE_CSCONV
  const char *ip;
  size_t ileft;
  char *op;
  size_t oleft;
  char buffer[UTF16_STRLEN];              /* Fix me! */
#endif /* !USE_CSCONV */
  const size_t buf_len = UTF16_STRLEN;
  size_t ret = 0;
#ifdef USE_CSCONV
  static csconv_t csconv_cd = NULL;

#endif

#ifdef USE_CSCONV
  if (current_locale == NULL){
    current_locale = (char*)strdup(setlocale(LC_CTYPE, NULL));
  }

  do {
    if (((void *)(-1) == csc_handle) ||
       ((csconv_t)(-1) == csconv_cd)) {
      continue;
    }
    if (NULL == csc_handle) {
      dlopen_csconv();
      if ((void *)(-1) == csc_handle) {
       continue;
      }
    }
    if (NULL == csconv_cd) {

      csconv_cd = (csc_open_locale)(current_locale, "UTF-16", "MultiByte");
      
      if ((csconv_t)(-1) == csconv_cd) {
       continue;
      }
    }

    dst = *((char **)to_buf);

    ret = csc_conv(csconv_cd,
                 (const char **)&from_buf, (size_t*)&from_left,
                 &dst, to_left);
    return ret;
  } while (0);
#endif

#ifdef USE_ICONV
  if (i_conv1 == (iconv_t)-1 || i_conv2 == (iconv_t)-1){
    goto done;
  }

  if (i_conv1 == NULL && !skip_native_to_utf8_conv) {
    char *encoding = nl_langinfo(CODESET);
    if (!strcmp(encoding, "UTF-8")) {
      skip_native_to_utf8_conv = True;
    } else {
      if ((i_conv1 = iconv_open("UTF-8", encoding))
         == (iconv_t)-1) {
       goto done;
      }
    }
  }

  if (i_conv2 == NULL){
    if ((i_conv2 = iconv_open("UCS-2",
                           "UTF-8")) == (iconv_t)-1)
      goto done;
  }

  if (!skip_native_to_utf8_conv) {
    ip = (const char *)from_buf;
    ileft = from_left;

    op = *((char **)to_buf);
    oleft = (size_t)(*to_left);

    while ((0 < ileft) && (0 < oleft)) {
      dst = buffer;
      dst_len = buf_len;
      ret = iconv(i_conv1, &ip, &ileft, (char**)&dst, &dst_len);
      if ((ret != 0) && (E2BIG != errno)) {
       goto done;
      }

      src = buffer;
      src_len = buf_len - dst_len;

      ret = iconv(i_conv2, (const char**)&src, (size_t*)&src_len,
                &op, &oleft);
      if (ret != 0) goto done;
    }

    dst_len = oleft;

  } else {
    src = from_buf;
    src_len = from_left;
    dst = *to_buf;
    dst_len = *to_left;
    ret = iconv(i_conv2, (const char**)&src, (size_t*)&src_len,
              (char**)&dst, &dst_len);
  }

  if (0xFEFF == **((CARD16 **)to_buf)) {
    memmove(*to_buf, *to_buf + 2, *to_left - dst_len - 2);
    *to_left = (dst_len + 2);
  } else {
    *to_left = dst_len;
  }
done:
#endif /* USE_ICONV */
  return(ret);
}

Here is the call graph for this function:

size_t mb_utf16 ( const char **  inbuf,
size_t *  inbytesleft,
char **  outbuf,
size_t *  outbytesleft 
)

Definition at line 386 of file xaux_ext_conv.c.

{
        int     r;

        r = IIimpConvertToUTF16((char *)(*inbuf), *inbytesleft,
                                  outbuf, outbytesleft);
        return (size_t)r;
}

Here is the call graph for this function:

size_t utf16_mb ( const char **  inbuf,
size_t *  inbytesleft,
char **  outbuf,
size_t *  outbytesleft 
)

Definition at line 372 of file xaux_ext_conv.c.

{
        int     r;

        r = IIimpConvertFromUTF16((char *)(*inbuf), *inbytesleft,
                                  outbuf, outbytesleft);
        return (size_t)r;
}

Here is the call graph for this function:


Variable Documentation

Definition at line 98 of file xaux_ext_conv.c.

csc_conv_t csc_conv [static]

Definition at line 97 of file xaux_ext_conv.c.

void* csc_handle [static]

Definition at line 94 of file xaux_ext_conv.c.

csc_open_t csc_open [static]

Definition at line 96 of file xaux_ext_conv.c.

Definition at line 95 of file xaux_ext_conv.c.

char* current_locale = 0 [static]

Definition at line 100 of file xaux_ext_conv.c.