Back to index

im-sdk  12.3.91
Enumerations | Functions
encode.h File Reference
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Enumerations

enum  encode_id_list {
  ENCODE_ERROR = -1, ENCODE_GB2312 = 0, ENCODE_GBK, ENCODE_GB18030,
  ENCODE_BIG5, ENCODE_EUC_TW, ENCODE_BIG5HK, ENCODE_EUC_TH,
  ENCODE_EUC_KO, ENCODE_UTF8, ENCODES_NUM, ENCODE_ERROR = -1,
  ENCODE_UTF8, ENCODES_NUM
}
enum  lang_group_list {
  LANG_ERROR = -1, LANG_ZH_CN = 0, LANG_ZH_TW, LANG_ZH_HK,
  LANG_TH_TH, LANG_KO_KR, LANGS_NUM, LANG_ERROR = -1,
  LANGS_NUM
}

Functions

int Convert_UTF8_To_Native (int encode_id, char *from_buf, size_t from_left, char **to_buf, size_t *to_left)
int Convert_Native_To_UTF16 (int encode_id, char *from_buf, size_t from_left, char **to_buf, size_t *to_left)
int is_valid_encode_string (int encode_id, unsigned char *hzstr, int hzlen)
int get_char_len_by_encodeid (int encode_id, unsigned char *ch_ptr)
int get_encodeid_from_name (char *name)
char * get_name_from_encodeid (int encode_id)
int get_encodeid_from_locale (char *locale)

Enumeration Type Documentation

Enumerator:
ENCODE_ERROR 
ENCODE_GB2312 
ENCODE_GBK 
ENCODE_GB18030 
ENCODE_BIG5 
ENCODE_EUC_TW 
ENCODE_BIG5HK 
ENCODE_EUC_TH 
ENCODE_EUC_KO 
ENCODE_UTF8 
ENCODES_NUM 
ENCODE_ERROR 
ENCODE_UTF8 
ENCODES_NUM 

Definition at line 45 of file encode.h.

Enumerator:
LANG_ERROR 
LANG_ZH_CN 
LANG_ZH_TW 
LANG_ZH_HK 
LANG_TH_TH 
LANG_KO_KR 
LANGS_NUM 
LANG_ERROR 
LANGS_NUM 

Definition at line 51 of file encode.h.

                     {
  LANG_ERROR = -1,
  LANGS_NUM,
};

Function Documentation

int Convert_Native_To_UTF16 ( int  encode_id,
char *  from_buf,
size_t  from_left,
char **  to_buf,
size_t *  to_left 
)

Definition at line 859 of file encode.c.

{
       char   *ip;
       char          *op;
       size_t        ileft, oleft;

       char          *codeset;
       iconv_t              fd_iconv_native_to_utf8;

       size_t        ret = 0;
       int           skip_native_to_utf8_iconv = 0;

       if (encode_id < 0 || encode_id >= ENCODES_NUM)
              return(-1);

       if ( (from_left < 0) || (*to_left < 0) )
              return(-1);

       /* Initialize the iconv of utf8_to_ucs2 */
       if (fd_iconv_UTF8_to_UTF16 == (iconv_t)-1 )
               return(-1);

       if (fd_iconv_UTF8_to_UTF16 == NULL) {
              fd_iconv_UTF8_to_UTF16 = iconv_open("UTF-16", "UTF-8");
              if (fd_iconv_UTF8_to_UTF16 == (iconv_t)-1 )
                     return(-1);
       }

       if (encode_id == ENCODE_UTF8)
              skip_native_to_utf8_iconv = 1;

       ip = (char *) from_buf;
       ileft = from_left;

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

       if (!skip_native_to_utf8_iconv) {
              char          buffer[UTF16_STRLEN];   /* Fix me! */
              const size_t  buf_len = UTF16_STRLEN;
              char          *src, *dst;
              size_t        src_len, dst_len;

              /* Initialize the iconv of native_to_utf8 */
              fd_iconv_native_to_utf8 = encode_info[encode_id].fd_iconv_to_utf8;
              if (fd_iconv_native_to_utf8 == (iconv_t)-1) return(-1);

              if (fd_iconv_native_to_utf8 == NULL) {
                     codeset = encode_info[encode_id].iconv_codeset_name;
                     fd_iconv_native_to_utf8 = iconv_open("UTF-8", codeset);
                     encode_info[encode_id].fd_iconv_to_utf8 = fd_iconv_native_to_utf8;
                     if ( fd_iconv_native_to_utf8 == (iconv_t) -1 )
                            return(-1);
              }

              while ((ileft > 0) && (oleft > 0)) {
                     dst = buffer;
                     dst_len = buf_len;
                     ret = iconv(fd_iconv_native_to_utf8, &ip, &ileft, (char **) &dst, &dst_len);
                     if (ret == -1) {
                            return(-1);
                     }
                     src = buffer;
                     src_len = buf_len - dst_len;
                     ret = iconv(fd_iconv_UTF8_to_UTF16, (char **) &src, &src_len, &op, &oleft);
                     if (ret == -1) {
                            return(-1);
                     }
              }

       } else {
              ret = iconv(fd_iconv_UTF8_to_UTF16, &ip, &ileft, &op, &oleft);
              if (ret == -1) {
                      return(-1);
              }
       }

       if (0xFEFF == **((CARD16 **) to_buf)) {
              memmove(*to_buf, *to_buf + 2, *to_left - oleft - 2);
              *to_left = (oleft + 2);
       } else {
              *to_left = oleft;
       }

       return(0);
}

Here is the caller graph for this function:

int Convert_UTF8_To_Native ( int  encode_id,
char *  from_buf,
size_t  from_left,
char **  to_buf,
size_t *  to_left 
)

Definition at line 808 of file encode.c.

{
       char   *ip;
       char          *op;
       size_t        ileft, oleft;
       iconv_t              fd_iconv;
       char          *codeset;
       size_t        ret = 0;
       
       if (encode_id < 0 || encode_id >= ENCODES_NUM)
              return(-1);

       if ( (from_left < 0) || (*to_left < 0) )
              return(-1);

       ip = (char *) from_buf;
       ileft = from_left;

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

       if (encode_id == ENCODE_UTF8) {
              if (ileft > oleft)
                     return(-1);
              memcpy(op, ip, ileft);
              *to_left = oleft - ileft;
              return(0);
       }

       fd_iconv = encode_info[encode_id].fd_iconv_from_utf8;
       if (fd_iconv == (iconv_t)-1) return(-1);

       if (fd_iconv == NULL) {
              codeset = encode_info[encode_id].iconv_codeset_name;
              fd_iconv = iconv_open(codeset, "UTF-8");
              encode_info[encode_id].fd_iconv_from_utf8 = fd_iconv;
              if ( fd_iconv == (iconv_t) -1 )
                     return(-1);
       }

       ret = iconv(fd_iconv, &ip, &ileft, &op, &oleft);
       if (ret == -1) {
              return(-1);
       }
       *to_left = oleft;
       return(0);
}

Here is the caller graph for this function:

int get_char_len_by_encodeid ( int  encode_id,
unsigned char *  ch_ptr 
)

Definition at line 581 of file encode.c.

{
       int ret = 2;  /* default character length */
       unsigned char code0, code1;

       code0 = ch_ptr[0];
       if (code0 < 0x80) return(1);

       if (encode_id == ENCODE_UTF8) {
              if (code0 > 0xe0)           /* 3 bytes */
                     ret = 3;
       } else if (encode_id == ENCODE_GB18030) {
              code1 = ch_ptr[1];
              if (code0 >=0x81 && code0 <= 0xFE) {
                     if (code1 >= 0x30 && code1 <= 0x39)
                            ret = 4;
              } 
       } else if (encode_id == ENCODE_EUC_TW) {
              if (code0 == 0x8e)          /* 4 bytes */
                     ret = 4;
       } else if (encode_id == ENCODE_EUC_TH) {
              ret = 1;
       }

       return(ret);
}

Here is the caller graph for this function:

int get_encodeid_from_locale ( char *  locale)

Definition at line 523 of file encode.c.

{
       int encode_id, i, ret;
       char *s;
       
       ret = -1;
       for (encode_id = 0; encode_id < ENCODES_NUM; encode_id++) {
              i = 0;
              while (1) {
                     s = encode_info[encode_id].support_locales[i];
                     if (!s || !*s) break;
                     if (!strcasecmp(s, locale)) {
                            ret = encode_id;
                            break;
                     }
                     i++;
              }
              if (ret != -1) break;
       }

       if (ret == -1) ret = ENCODE_ERROR; /* return default encode */

       return(ret);  
}

Here is the caller graph for this function:

int get_encodeid_from_name ( char *  name)

Definition at line 500 of file encode.c.

{
       int encode_id, i, ret;
       char *s;
       
       ret = -1;
       for (encode_id = 0; encode_id < ENCODES_NUM; encode_id++) {
              i = 0;
              while (1) {
                     s = encode_info[encode_id].called_names[i];
                     if (!s || !*s) break;
                     if (!strcasecmp(s, name)) {
                            ret = encode_id;
                            break;
                     }
                     i++;
              }
              if (ret != -1) break;
       }
       if (ret == -1) ret = ENCODE_GB2312; /* return default encode */
       return(ret);  
}

Here is the caller graph for this function:

char* get_name_from_encodeid ( int  encode_id)

Definition at line 548 of file encode.c.

{
       if (encode_id >= 0 && encode_id <= ENCODES_NUM)
              return(encode_info[encode_id].called_names[0]);
       else
              return(NULL);
}

Here is the caller graph for this function:

int is_valid_encode_string ( int  encode_id,
unsigned char *  hzstr,
int  hzlen 
)

Definition at line 730 of file encode.c.

{
       int i, char_len, ret;
       unsigned char *ptr;

       i = 0;
       while (i < hzlen) {
              ptr = hzstr + i;
              if (*ptr < 0x80) {
                     if (*ptr == 0x3f && i < hzlen-1) {
                            if (*(ptr+1) == 0x3f)
                                   return(-1);
                     }

                     i++;
              } else {
                     char_len = get_char_len_by_encodeid(encode_id, ptr);
                     ret = is_valid_code(encode_id, ptr, char_len);
                     if (ret == -1)
                            return(-1);
                     i += char_len;
              }
       }

       return (0);
}

Here is the call graph for this function:

Here is the caller graph for this function: