/* * CDDL HEADER START * * The contents of this file are subject to the terms of the * Common Development and Distribution License (the "License"). * You may not use this file except in compliance with the License. * * You can obtain a copy of the license at src/OPENSOLARIS.LICENSE * or http://www.opensolaris.org/os/licensing. * See the License for the specific language governing permissions * and limitations under the License. * * When distributing Covered Code, include this CDDL HEADER in each * file and include the License file at src/OPENSOLARIS.LICENSE. * If applicable, add the following below this CDDL HEADER, with the * fields enclosed by brackets "[]" replaced with your own identifying * information: Portions Copyright [yyyy] [name of copyright owner] * * CDDL HEADER END */ /* * Copyright (c) 1994, 1995 by Sun Microsystems, Inc. * Copyright (c) 1994, Nihon Sun Microsystems K.K. * All Rights Reserved. */ #include #include #include #include #define MAGIC_NUMBER (0x216513) #define ERR_RETURN (-1) /* result code on error */ #define GET(c) ((c) = *ip, ip++, ileft--) #define PUT(c) (*op = (c), op++, oleft--) #define UNGET() (ip--, ileft++) /* * Open; called from iconv_open() */ void * _icv_open() { return ((void*)MAGIC_NUMBER); } /* * Close; called from iconv_close */ void _icv_close(int* cd) { if (!cd || cd != (int*)MAGIC_NUMBER) errno = EBADF; } /* * Actual conversion; called from iconv() */ size_t _icv_iconv(int* cd, char **inbuf, size_t *inbytesleft, char **outbuf, size_t *outbytesleft) { unsigned char *ip, ic, *op; size_t ileft, oleft; size_t retval = 0; if (!cd || cd != (int*)MAGIC_NUMBER) { errno = EBADF; return((size_t)ERR_RETURN); } if ((inbuf == 0) || (*inbuf == 0)) return((size_t)0); ip = (unsigned char*)*inbuf; op = (unsigned char *)*outbuf; ileft = *inbytesleft; oleft = *outbytesleft; /* * Main loop; basically 1 loop per 1 input byte */ while (ileft > 0) { GET(ic); if (oleft < 1) { UNGET(); errno = E2BIG; retval = ERR_RETURN; goto ret; } if (isascii(ic)) PUT(ic); else { PUT('_'); retval++; } } ret: *inbuf = (char *)ip; *inbytesleft = ileft; *outbuf = (char *)op; *outbytesleft = oleft; return (retval); } # # CDDL HEADER START # # The contents of this file are subject to the terms of the # Common Development and Distribution License (the "License"). # You may not use this file except in compliance with the License. # # You can obtain a copy of the license at src/OPENSOLARIS.LICENSE # or http://www.opensolaris.org/os/licensing. # See the License for the specific language governing permissions # and limitations under the License. # # When distributing Covered Code, include this CDDL HEADER in each # file and include the License file at src/OPENSOLARIS.LICENSE. # If applicable, add the following below this CDDL HEADER, with the # fields enclosed by brackets "[]" replaced with your own identifying # information: Portions Copyright [yyyy] [name of copyright owner] # # CDDL HEADER END # # # Copyright 2006 Sun Microsystems, Inc. All rights reserved. # Use is subject to license terms. # include $(SRC)/Makefile.master # Hammerhead: amd64-only SUBDIRS = $(MACH64) all : TARGET = all clean : TARGET = clean clobber : TARGET = clobber install : TARGET = install echo : TARGET = echo .KEEP_STATE: all clean clobber install echo: $(SUBDIRS) .PARALLEL: $(SUBDIRS) $(SUBDIRS): FRC @cd $@; pwd; $(MAKE) $(TARGET) FRC: # # CDDL HEADER START # # The contents of this file are subject to the terms of the # Common Development and Distribution License (the "License"). # You may not use this file except in compliance with the License. # # You can obtain a copy of the license at src/OPENSOLARIS.LICENSE # or http://www.opensolaris.org/os/licensing. # See the License for the specific language governing permissions # and limitations under the License. # # When distributing Covered Code, include this CDDL HEADER in each # file and include the License file at src/OPENSOLARIS.LICENSE. # If applicable, add the following below this CDDL HEADER, with the # fields enclosed by brackets "[]" replaced with your own identifying # information: Portions Copyright [yyyy] [name of copyright owner] # # CDDL HEADER END # # # Copyright 2006 Sun Microsystems, Inc. All rights reserved. # Use is subject to license terms. # SRCS = 646%CODESET.c ALL_SOS = 646%CODESET.so LINK_TARGETS = 646%CODESET.so dummy: all include $(SRC)/lib/iconv_modules/Makefile.iconv $(CREATE_LINKS): $(ICONV_LINK_TARGETS) $(SYMLINK) -f 646%CODESET.so $(ICONV_DIR)/646%5601.so $(SYMLINK) -f 646%CODESET.so $(ICONV_DIR)/646%BIG5.so $(SYMLINK) -f 646%CODESET.so $(ICONV_DIR)/646%cns11643.so $(SYMLINK) -f 646%CODESET.so $(ICONV_DIR)/646%eucJP.so $(SYMLINK) -f 646%CODESET.so $(ICONV_DIR)/646%gb2312.so $(SYMLINK) -f 646%CODESET.so $(ICONV_DIR)/646%GBK.so $(SYMLINK) -f 646%CODESET.so $(ICONV_DIR)/646%PCK.so $(SYMLINK) -f 646%CODESET.so $(ICONV_DIR)/646%SJIS.so $(TOUCH) $@ all: $(ALL_SOS) # # CDDL HEADER START # # The contents of this file are subject to the terms of the # Common Development and Distribution License (the "License"). # You may not use this file except in compliance with the License. # # You can obtain a copy of the license at src/OPENSOLARIS.LICENSE # or http://www.opensolaris.org/os/licensing. # See the License for the specific language governing permissions # and limitations under the License. # # When distributing Covered Code, include this CDDL HEADER in each # file and include the License file at src/OPENSOLARIS.LICENSE. # If applicable, add the following below this CDDL HEADER, with the # fields enclosed by brackets "[]" replaced with your own identifying # information: Portions Copyright [yyyy] [name of copyright owner] # # CDDL HEADER END # # ---------------------------------------------------------------------- # start of Imakefile # Copyright 2006 Sun Microsystems, Inc. All rights reserved. # Use is subject to license terms. # SRCS = tab_lookup.c utf8%ibm.c ibm%utf8.c dummy: all include ../Makefile.iconv all: $(MACH64) $(OBJS) # # This file and its contents are supplied under the terms of the # Common Development and Distribution License ("CDDL"), version 1.0. # You may only use this file in accordance with the terms of version # 1.0 of the CDDL. # # A full copy of the text of the CDDL should have accompanied this # source. A copy of the CDDL is also available via the Internet at # http://www.illumos.org/license/CDDL. # # # Copyright 2017 Alexander Pyhalov # include ../Makefile.com include ../../Makefile.iconv.64 ICONV_LINK_TARGETS = $(LINK_TARGETS:%=$(ICONV_DIR_64)/%) $(ICONV_LINK_TARGETS) : FILEMODE= 755 ICONV_LIBS = $(ALL_SOS:%=$(ICONV_DIR_64)/%) $(ICONV_LIBS) : FILEMODE= 755 ICONV_DIR = $(ICONV_DIR_64) install: all $(ICONV_DIR_64) .WAIT $(ICONV_LIBS) $(CREATE_LINKS) /* * CDDL HEADER START * * The contents of this file are subject to the terms of the * Common Development and Distribution License (the "License"). * You may not use this file except in compliance with the License. * * You can obtain a copy of the license at src/OPENSOLARIS.LICENSE * or http://www.opensolaris.org/os/licensing. * See the License for the specific language governing permissions * and limitations under the License. * * When distributing Covered Code, include this CDDL HEADER in each * file and include the License file at src/OPENSOLARIS.LICENSE. * If applicable, add the following below this CDDL HEADER, with the * fields enclosed by brackets "[]" replaced with your own identifying * information: Portions Copyright [yyyy] [name of copyright owner] * * CDDL HEADER END */ /* * Copyright (c) 1997, by Sun Microsystems, Inc. * All rights reserved. */ #include #include #include #include #include "tab_lookup.h" /* table lookup data types */ #define MSB 0x80 /* most significant bit */ #define MBYTE 0x8e /* multi-byte (4 byte character) */ #define PMASK 0xa0 /* plane number mask */ #define ONEBYTE 0xff /* right most byte */ /* non-identified character */ #define UTF8_NON_ID_CHAR1 0xEF #define UTF8_NON_ID_CHAR2 0xBF #define UTF8_NON_ID_CHAR3 0xBD enum _USTATE { C0, C1 }; /* * Actual conversion; called from iconv() * Input is UTF-8 data. * first convert to UCS2 */ size_t _icv_iconv(_icv_state *st, char **inbuf, size_t *inbytesleft, char **outbuf, size_t *outbytesleft) { /* * Actual conversion; called from iconv() */ char c1, c2; int n, unidx; unsigned long ibm_code; #ifdef DEBUG fprintf(stderr, "========== iconv(): IBM --> UTF8 ==========\n"); #endif if (st == NULL) { errno = EBADF; return ((size_t) -1); } if (inbuf == NULL || *inbuf == NULL) { /* Reset request. */ st->ustate = C0; st->_errno = 0; return ((size_t) 0); } st->_errno = 0; /* reset internal errno */ errno = 0; /* reset external errno */ /* a state machine for interpreting UTF8 code */ while (*inbytesleft > 0 && *outbytesleft > 0) { switch (st->ustate) { case C0 : st->keepc[0] = (**inbuf); st->ustate = C1; break; case C1 : st->keepc[1] = (**inbuf); n = ibm_to_utf8(st, *outbuf, *outbytesleft); if (n > 0) { (*outbuf) += n; (*outbytesleft) -= n; } else { st->_errno = errno; return((size_t)-1); } st->ustate = C0; st->_errno = 0; break; default: /* should never come here */ st->_errno = errno = EILSEQ; st->ustate = C0; /* reset state */ break; } (*inbuf)++; (*inbytesleft)--; if (st->_errno) { #ifdef DEBUG fprintf(stderr, "!!!!!\tst->_errno = %d\tst->ustate = %d\n", st->_errno, st->ustate); #endif break; } if (errno) return((size_t)-1); } if (*outbytesleft == 0) { errno = E2BIG; return((size_t)-1); } return (*inbytesleft); } /* * IBM code --> (Unicode) * Unicode --> UTF8 (FSS-UTF) * (File System Safe Universal Character Set Transformation Format) * Return: > 0 - converted with enough space in output buffer * = 0 - no space in outbuf */ int ibm_to_utf8(st, buf, buflen) _icv_state *st; char *buf; size_t buflen; { unsigned long ibm_val; /* Big-5 value */ int unidx; /* Unicode index */ unsigned long uni_val; /* Unicode */ ibm_val = ((st->keepc[0]&ONEBYTE) << 8) + (st->keepc[1]&ONEBYTE); #ifdef DEBUG fprintf(stderr, "%x\t", ibm_val); #endif unidx = bisearch(ibm_val, st, st->table_size); if (unidx >= 0) { if ( st->left_to_right ) uni_val = st->table[unidx].right_code; else uni_val = st->table[unidx].left_code; } #ifdef DEBUG fprintf(stderr, "unidx = %d, unicode = %x\t", unidx, uni_val); #endif if (unidx >= 0) { /* do Unicode to UTF8 conversion */ if (uni_val > 0x0000 && uni_val <= 0x07ff) { if (buflen < 2) { #ifdef DEBUG fprintf(stderr, "outbuf overflow in ibm_to_utf8()!!\n"); #endif errno = E2BIG; return(0); } *buf = (char)((uni_val >> 6) & 0x1f) | 0xc0; *(buf+1) = (char)(uni_val & 0x3f) | 0x80; #ifdef DEBUG fprintf(stderr, "%x %x\n", *buf&ONEBYTE, *(buf+1)&ONEBYTE); #endif return(2); } if (uni_val > 0x0800 && uni_val <= 0xffff) { if (buflen < 3) { #ifdef DEBUG fprintf(stderr, "outbuf overflow in ibm_to_utf8()!!\n"); #endif errno = E2BIG; return(0); } *buf = (char)((uni_val >> 12) & 0xf) | 0xe0; *(buf+1) = (char)((uni_val >>6) & 0x3f) | 0x80; *(buf+2) = (char)(uni_val & 0x3f) | 0x80; #ifdef DEBUG fprintf(stderr, "%x %x %x\n", *buf&ONEBYTE, *(buf+1)&ONEBYTE, *(buf+2)&ONEBYTE); #endif return(3); } } /* can't find a match in IBM --> UTF8 table or illegal UTF8 code */ if (buflen < 3) { #ifdef DEBUG fprintf(stderr, "outbuf overflow in ibm_to_utf8()!!\n"); #endif errno = E2BIG; return(0); } *buf = (char)UTF8_NON_ID_CHAR1; *(buf+1) = (char)UTF8_NON_ID_CHAR2; *(buf+2) = (char)UTF8_NON_ID_CHAR3; #ifdef DEBUG fprintf(stderr, "%c %c %c\n", *buf, *(buf+1), *(buf+2)); #endif return(3); } /* * CDDL HEADER START * * The contents of this file are subject to the terms of the * Common Development and Distribution License (the "License"). * You may not use this file except in compliance with the License. * * You can obtain a copy of the license at src/OPENSOLARIS.LICENSE * or http://www.opensolaris.org/os/licensing. * See the License for the specific language governing permissions * and limitations under the License. * * When distributing Covered Code, include this CDDL HEADER in each * file and include the License file at src/OPENSOLARIS.LICENSE. * If applicable, add the following below this CDDL HEADER, with the * fields enclosed by brackets "[]" replaced with your own identifying * information: Portions Copyright [yyyy] [name of copyright owner] * * CDDL HEADER END */ /* * Copyright (c) 1997, by Sun Microsystems, Inc. * All rights reserved. */ #include #include #include #include #include "tab_lookup.h" /* table lookup data types */ #define MSB 0x80 /* most significant bit */ #define ONEBYTE 0xff /* right most byte */ enum _USTATE { U0, U1, U11, U2, U3, U4 }; /* * Actual conversion; called from iconv() * Input is UTF-8 data. * first convert to UCS2 */ size_t _icv_iconv(_icv_state *st, char **inbuf, size_t *inbytesleft, char **outbuf, size_t *outbytesleft) { /* * Actual conversion; called from iconv() */ /*========================================================= * * State Machine for interpreting UTF8 code * *========================================================= * * 3 byte unicode * +----->------->-------+ * | | * ^ v * | 2 byte U2 ---> U3 * | unicode v * +------> U0 -------> U1 +-------->U4---+ * ^ ascii | | ^ | * | | +-------->--------->--------+ | * | v v * +----<---+-----<------------<------------<------------+ * * +----<---+-----<------------<------------<------------+ * *=========================================================*/ char c1, c2; int n, unidx; unsigned long ibm_code; #ifdef DEBUG fprintf(stderr, "========== iconv(): UTF8 --> IBM ==========\n"); #endif if (st == NULL) { errno = EBADF; return ((size_t) -1); } if (inbuf == NULL || *inbuf == NULL) { /* Reset request. */ st->ustate = U0; st->_errno = 0; return ((size_t) 0); } st->_errno = 0; /* reset internal errno */ errno = 0; /* reset external errno */ /* a state machine for interpreting UTF8 code */ while (*inbytesleft > 0 && *outbytesleft > 0) { switch (st->ustate) { case U0: /* assuming ASCII in the beginning */ if ((**inbuf & MSB) == 0) { /* ASCII */ **outbuf = **inbuf; (*outbuf)++; (*outbytesleft)--; } else { /* Chinese character */ if ((**inbuf & 0xe0) == 0xc0) { /* 2 byte unicode */ st->ustate = U1; st->keepc[0] = **inbuf; } else if ((**inbuf & 0xf0) == 0xe0) { /* 3 byte */ st->ustate = U2; st->keepc[0] = **inbuf; } else { /* illegal unicode */ /* st->_errno = errno = EINVAL; */ /* possible UNICODE ko_KR-UTF8 */ c1 =st->keepc[0] = **inbuf; st->ustate = U11; break; } } break; case U1: /* 2 byte unicode */ if ((**inbuf & 0xc0) == MSB) { st->ustate = U4; st->keepc[1] = **inbuf; c1 = (st->keepc[0]&0x1c)>>2; c2 = ((st->keepc[0]&0x03)<<6) | ((**inbuf)&0x3f); #ifdef DEBUG fprintf(stderr, "UTF8: %02x%02x --> ", st->keepc[0]&ONEBYTE, st->keepc[1]&ONEBYTE); #endif continue; /* should not advance *inbuf */ } else { st->_errno = errno = EINVAL; } break; case U11: /* 3 byte unicode - 2nd byte */ c2 =st->keepc[1] = **inbuf; st->ustate = U4; continue; break; case U2: /* 3 byte unicode - 2nd byte */ if ((**inbuf & 0xc0) == MSB) { st->ustate = U3; st->keepc[1] = **inbuf; } else { st->_errno = errno = EINVAL; } break; case U3: /* 3 byte unicode - 3rd byte */ if ((**inbuf & 0xc0) == MSB) { st->ustate = U4; st->keepc[2] = **inbuf; c1 = ((st->keepc[0]&0x0f)<<4) | ((st->keepc[1]&0x3c)>>2); c2 = ((st->keepc[1]&0x03)<<6) | ((**inbuf)&0x3f); #ifdef DEBUG fprintf(stderr, "UTF8: %02x%02x%02x --> ", st->keepc[0]&ONEBYTE, st->keepc[1]&ONEBYTE, **inbuf&ONEBYTE); #endif continue; /* should not advance *inbuf */ } else { st->_errno = errno = EINVAL; } break; case U4: n = get_ibm_by_utf(st, c1, c2, &unidx, &ibm_code); if (n != 0) { /* legal unicode;illegal Big5 */ st->_errno = errno = EILSEQ; break; } n = utf8_to_ibm(unidx, ibm_code, *outbuf, *outbytesleft); if (n > 0) { (*outbuf) += n; (*outbytesleft) -= n; } else { st->_errno = errno; return((size_t)-1); } st->ustate = U0; st->_errno = 0; break; default: /* should never come here */ st->_errno = errno = EILSEQ; st->ustate = U0; /* reset state */ break; } (*inbuf)++; (*inbytesleft)--; if (st->_errno) { #ifdef DEBUG fprintf(stderr, "!!!!!\tst->_errno = %d\tst->ustate = %d\n", st->_errno, st->ustate); #endif break; } if (errno) return((size_t)-1); } if (*outbytesleft == 0) { errno = E2BIG; return((size_t)-1); } return (*inbytesleft); } /* * Match IBM code by UTF8 code; * Return: = 0 - match from Unicode to IBM found * = 1 - match from Unicode to IBM NOT found * * Since binary search of the UTF8 to IBM table is necessary, might as well * return index and IBM code matching to the unicode. */ int get_ibm_by_utf(st, c1, c2, unidx, ibm_code) _icv_state *st; char c1, c2; int *unidx; unsigned long *ibm_code; { unsigned long unicode; unicode = (unsigned long) ((c1 & ONEBYTE) << 8) + (c2 & ONEBYTE); *unidx = bisearch(unicode, st, st->table_size); if ((*unidx) >= 0) { if ( st->left_to_right ) *ibm_code = st->table[*unidx].right_code; else *ibm_code = st->table[*unidx].left_code; } else ; /* match from UTF8 to IBM not found */ #ifdef DEBUG fprintf(stderr, "Unicode=%04x, idx=%5d, IBM=%x ", unicode, *unidx, *ibm_code); #endif return(0); } /* * ISO/IEC 10646 (Unicode) --> IBM * Unicode --> UTF8 (FSS-UTF) * (File System Safe Universal Character Set Transformation Format) * Return: > 0 - converted with enough space in output buffer * = 0 - no space in outbuf */ int utf8_to_ibm(unidx, ibm_code, buf, buflen) int unidx; unsigned long ibm_code; char *buf; size_t buflen; { unsigned long val; /* IBM value */ char c1, c2, ibm_str[3]; if (unidx < 0) /* no match from UTF8 to IBM */ ibm_code = (unsigned long)NON_ID_CHAR; { val = ibm_code & 0xffff; c1 = (char) ((val & 0xff00) >> 8); c2 = (char) (val & 0xff); } *buf = ibm_str[0] = c1; *(buf+1) = ibm_str[1] = c2; ibm_str[2] = NULL; #ifdef DEBUG fprintf(stderr, "\t->%x %x<-\n", *buf, *(buf+1)); #endif if (buflen < 2) { errno = E2BIG; return(0); } return(2); } /* * CDDL HEADER START * * The contents of this file are subject to the terms of the * Common Development and Distribution License (the "License"). * You may not use this file except in compliance with the License. * * You can obtain a copy of the license at src/OPENSOLARIS.LICENSE * or http://www.opensolaris.org/os/licensing. * See the License for the specific language governing permissions * and limitations under the License. * * When distributing Covered Code, include this CDDL HEADER in each * file and include the License file at src/OPENSOLARIS.LICENSE. * If applicable, add the following below this CDDL HEADER, with the * fields enclosed by brackets "[]" replaced with your own identifying * information: Portions Copyright [yyyy] [name of copyright owner] * * CDDL HEADER END */ /* * Copyright 2002 Sun Microsystems, Inc. All rights reserved. * Use is subject to license terms. */ #include #include "../inc/common_defs.h" /* * convert utf8 string to unicode * return value: 0 - fail * 1 - success */ int convert_utf8_to_ucs4(uchar_t *ib, int utf8_len, uint_t *unicode) { uchar_t first_byte = *ib; uint_t u4; if ( number_of_bytes_in_utf8_char[first_byte] != utf8_len) return 0; u4 = (uint_t)(*ib++ & masks_tbl[utf8_len]); for (; utf8_len > 1; utf8_len--) { u4 = (u4 << ICV_UTF8_BIT_SHIFT) | (((uint_t) *ib) & ICV_UTF8_BIT_MASK); ++ib; } *unicode = u4; return 1; } /* * check whether the input 'str' is valid UTF-8 byte sequence or not, * which lenght is specified by 'utf8_len' * * return: 0 - invalid byte sequence * 1 - valid byte sequence */ int is_valid_utf8_string(uchar_t *str, int utf8_len) { uint_t unicode = 0; uchar_t *ib = str; uchar_t first_byte; int is_second_byte = 0, len=utf8_len; if (number_of_bytes_in_utf8_char[*ib] == ICV_TYPE_ILLEGAL_CHAR || number_of_bytes_in_utf8_char[*ib] != utf8_len ) return 0; first_byte = *ib; --utf8_len; ++ib; is_second_byte = 1; while (utf8_len != 0) { if (is_second_byte) { if ( *ib < valid_min_2nd_byte[first_byte] || *ib > valid_max_2nd_byte[first_byte] ) return 0; is_second_byte = 0; } else if ((*ib & 0xc0) != 0x80) /* 0x80 -- 0xbf */ return 0; --utf8_len; ++ib; } convert_utf8_to_ucs4(str, len, &unicode); if (unicode == 0xFFFE || unicode == 0xFFFF) return 0; return 1; } /* * CDDL HEADER START * * The contents of this file are subject to the terms of the * Common Development and Distribution License (the "License"). * You may not use this file except in compliance with the License. * * You can obtain a copy of the license at src/OPENSOLARIS.LICENSE * or http://www.opensolaris.org/os/licensing. * See the License for the specific language governing permissions * and limitations under the License. * * When distributing Covered Code, include this CDDL HEADER in each * file and include the License file at src/OPENSOLARIS.LICENSE. * If applicable, add the following below this CDDL HEADER, with the * fields enclosed by brackets "[]" replaced with your own identifying * information: Portions Copyright [yyyy] [name of copyright owner] * * CDDL HEADER END */ /* * Copyright 1998 Sun Microsystems, Inc. All rights reserved. * Use is subject to license terms. */ #ifndef GENTYPES_H #define GENTYPES_H #include typedef uchar_t i8859_t; typedef unsigned int ucs4_t; typedef unsigned short int ucs2_t; #endif /* * CDDL HEADER START * * The contents of this file are subject to the terms of the * Common Development and Distribution License (the "License"). * You may not use this file except in compliance with the License. * * You can obtain a copy of the license at src/OPENSOLARIS.LICENSE * or http://www.opensolaris.org/os/licensing. * See the License for the specific language governing permissions * and limitations under the License. * * When distributing Covered Code, include this CDDL HEADER in each * file and include the License file at src/OPENSOLARIS.LICENSE. * If applicable, add the following below this CDDL HEADER, with the * fields enclosed by brackets "[]" replaced with your own identifying * information: Portions Copyright [yyyy] [name of copyright owner] * * CDDL HEADER END */ /* * Copyright (c) 1997, by Sun Microsystems, Inc. * All rights reserved. */ #include #include #include #include #include "tab_lookup.h" /* table lookup data types */ int bisearch(unsigned long val, _icv_state *st, int n); #define MSB 0x80 /* most significant bit */ #define MBYTE 0x8e /* multi-byte (4 byte character) */ #define PMASK 0xa0 /* plane number mask */ #define ONEBYTE 0xff /* right most byte */ /* non-identified character */ #define UTF8_NON_ID_CHAR1 0xEF #define UTF8_NON_ID_CHAR2 0xBF #define UTF8_NON_ID_CHAR3 0xBD enum _USTATE { C0, C1, C2 }; int ibm_to_utf8(_icv_state *st, char *buf, size_t buflen); /* * Actual conversion; called from iconv() * Input is UTF-8 data. * first convert to UCS2 */ size_t _icv_iconv(_icv_state *st, char **inbuf, size_t *inbytesleft, char **outbuf, size_t *outbytesleft) { /* * Actual conversion; called from iconv() */ int n; #ifdef DEBUG fprintf(stderr, "========== iconv(): IBM --> UTF8 ==========\n"); #endif if (st == NULL) { errno = EBADF; return ((size_t) -1); } if (inbuf == NULL || *inbuf == NULL) { /* Reset request. */ st->ustate = C0; st->_errno = 0; st->shift = SHIFT_IN; return ((size_t) 0); } st->_errno = 0; /* reset internal errno */ errno = 0; /* reset external errno */ /* a state machine for interpreting UTF8 code */ while (*inbytesleft > 0 && *outbytesleft > 0) { switch (**inbuf) { case SHIFT_OUT : if (st->shift == SHIFT_IN) { st->shift = SHIFT_OUT; (*inbuf)++; (*inbytesleft)--; continue; } break; case SHIFT_IN : if (st->shift == SHIFT_OUT) { st->shift = SHIFT_IN; (*inbuf)++; (*inbytesleft)--; continue; } break; } switch (st->ustate) { case C0 : /* the input is ascii, single byte, convert it */ if (st->shift == SHIFT_IN) { st->keepc[0] = 0x0; st->keepc[1] = **inbuf; st->ustate = C2; continue; } /* two bytes character */ st->keepc[0] = (**inbuf); st->ustate = C1; break; case C1 : st->keepc[1] = (**inbuf); st->ustate = C2; continue; case C2 : n = ibm_to_utf8(st, *outbuf, *outbytesleft); if (n > 0) { (*outbuf) += n; (*outbytesleft) -= n; } else { st->_errno = errno; return((size_t)-1); } st->ustate = C0; st->_errno = 0; break; default: /* should never come here */ st->_errno = errno = EILSEQ; st->ustate = C0; /* reset state */ break; } (*inbuf)++; (*inbytesleft)--; if (st->_errno) { #ifdef DEBUG fprintf(stderr, "!!!!!\tst->_errno = %d\tst->ustate = %d\n", st->_errno, st->ustate); #endif break; } if (errno) return((size_t)-1); } if (*outbytesleft == 0) { errno = E2BIG; return((size_t)-1); } return (*inbytesleft); } /* * IBM code --> (Unicode) * Unicode --> UTF8 (FSS-UTF) * (File System Safe Universal Character Set Transformation Format) * Return: > 0 - converted with enough space in output buffer * = 0 - no space in outbuf */ int ibm_to_utf8(st, buf, buflen) _icv_state *st; char *buf; size_t buflen; { unsigned long ibm_val; /* Big-5 value */ int unidx; /* Unicode index */ unsigned long uni_val; /* Unicode */ ibm_val = ((st->keepc[0]&ONEBYTE) << 8) + (st->keepc[1]&ONEBYTE); #ifdef DEBUG fprintf(stderr, "%x\t", ibm_val); #endif unidx = bisearch(ibm_val, st, st->table_size); if (unidx >= 0) { if ( st->left_to_right ) uni_val = st->table[unidx].right_code; else uni_val = st->table[unidx].left_code; } #ifdef DEBUG fprintf(stderr, "unidx = %d, unicode = %x\t", unidx, uni_val); #endif if (unidx >= 0) { /* do Unicode to UTF8 conversion */ if (uni_val <= 0x07f) { if (buflen < 1) { errno = E2BIG; return 0; } *buf = uni_val; return 1; } if (uni_val >= 0x0080 && uni_val <= 0x07ff) { if (buflen < 2) { #ifdef DEBUG fprintf(stderr, "outbuf overflow in ibm_to_utf8()!!\n"); #endif errno = E2BIG; return(0); } *buf = (char)((uni_val >> 6) & 0x1f) | 0xc0; *(buf+1) = (char)(uni_val & 0x3f) | 0x80; #ifdef DEBUG fprintf(stderr, "%x %x\n", *buf&ONEBYTE, *(buf+1)&ONEBYTE); #endif return(2); } if (uni_val >= 0x0800 && uni_val <= 0xffff) { if (buflen < 3) { #ifdef DEBUG fprintf(stderr, "outbuf overflow in ibm_to_utf8()!!\n"); #endif errno = E2BIG; return(0); } *buf = (char)((uni_val >> 12) & 0xf) | 0xe0; *(buf+1) = (char)((uni_val >>6) & 0x3f) | 0x80; *(buf+2) = (char)(uni_val & 0x3f) | 0x80; #ifdef DEBUG fprintf(stderr, "%x %x %x\n", *buf&ONEBYTE, *(buf+1)&ONEBYTE, *(buf+2)&ONEBYTE); #endif return(3); } } /* can't find a match in IBM --> UTF8 table or illegal UTF8 code */ if (buflen < 3) { #ifdef DEBUG fprintf(stderr, "outbuf overflow in ibm_to_utf8()!!\n"); #endif errno = E2BIG; return(0); } *buf = (char)UTF8_NON_ID_CHAR1; *(buf+1) = (char)UTF8_NON_ID_CHAR2; *(buf+2) = (char)UTF8_NON_ID_CHAR3; #ifdef DEBUG fprintf(stderr, "%c %c %c\n", *buf, *(buf+1), *(buf+2)); #endif return(3); } /* * CDDL HEADER START * * The contents of this file are subject to the terms of the * Common Development and Distribution License (the "License"). * You may not use this file except in compliance with the License. * * You can obtain a copy of the license at src/OPENSOLARIS.LICENSE * or http://www.opensolaris.org/os/licensing. * See the License for the specific language governing permissions * and limitations under the License. * * When distributing Covered Code, include this CDDL HEADER in each * file and include the License file at src/OPENSOLARIS.LICENSE. * If applicable, add the following below this CDDL HEADER, with the * fields enclosed by brackets "[]" replaced with your own identifying * information: Portions Copyright [yyyy] [name of copyright owner] * * CDDL HEADER END */ /* * Copyright (c) 1997, by Sun Microsystems, Inc. * All rights reserved. */ #include #include #include #include #include "tab_lookup.h" /* table lookup data types */ int bisearch(unsigned long val, _icv_state *st, int n); /* * Actual conversion; called from iconv(). * Peforms conversion as per the parameters specified in * structure st. */ size_t _icv_iconv_lu(_icv_state *st, unsigned char **ibuf, size_t *inbytesleft, unsigned char **obuf, size_t *outbytesleft) { int idx, data_size; unsigned long search_val = 0, match_val; unsigned char **inbuf, **outbuf; inbuf = (unsigned char **)ibuf; outbuf = (unsigned char **)obuf; if (st == NULL) { errno = EBADF; return ((size_t)-1); } if (inbuf == NULL || *inbuf == NULL) { /* Reset request */ return 0; } errno = 0; while (*inbytesleft > 0 && *outbytesleft > 0) { fprintf(stderr, "INBL: %d , OUBL: %d \n", *inbytesleft, *outbytesleft); search_val = 0; /* * form a search member * lookup character by character */ if ( st->left_to_right ) { /* * create search val from the left code */ data_size = st->left_code_size; while ( data_size > 0 ) { search_val = ( search_val << 8 ) | ( **inbuf ); data_size--; (*inbuf)++; (*inbytesleft)--; } idx = bisearch(search_val, st, st->table_size); #ifdef TEST fprintf(stderr, "Match idx: %d \n", idx); #endif if ( idx >= 0 ) { /* * create matched code from the right column */ match_val = st->table[idx].right_code; } else { match_val = NON_ID_CHAR; } /* * Check sufficient space in the outbuf */ if ( *outbytesleft >= st->right_code_size ) { data_size = st->right_code_size; while ( data_size > 0 ) { *(*outbuf + data_size-- - 1 ) = (unsigned char) (match_val & 0xff); #ifdef TEST fprintf(stderr, "outbyte: %x \n", (unsigned char) (match_val & 0xff)); #endif match_val >>= 8; } (*outbuf) += st->right_code_size; (*outbytesleft) -= st->right_code_size; } else { /* no space for outbytes */ errno = E2BIG; return ((size_t)-1); } } else { /* search from right to left */ /* * create search val from the left code */ data_size = st->right_code_size; while ( data_size > 0 ) { search_val = ( search_val << 8 ) | ( **inbuf ); data_size--; (*inbuf)++; (*inbytesleft)--; } idx = bisearch(search_val, st, st->table_size); #ifdef TEST fprintf(stderr, "Match idx: %d \n", idx); #endif if ( idx >= 0 ) { /* * create matched code from the right column */ match_val = st->table[idx].left_code; } else { match_val = UCS2_NON_ID_CHAR; } /* * Check sufficient space in the outbuf */ if ( *outbytesleft >= st->left_code_size ) { data_size = st->left_code_size; while ( data_size > 0 ) { *(*outbuf + data_size-- - 1 ) = (unsigned char) (match_val & 0xff); #ifdef TEST fprintf(stderr, "outbyte: %x \n", (unsigned char) (match_val & 0xff)); #endif match_val >>= 8; } (*outbuf) += st->left_code_size; (*outbytesleft) -= st->left_code_size; } else { /* no space for outbytes */ errno = E2BIG; return ((size_t)-1); } } #ifdef TEST fprintf(stderr, "Search: %x match: %x \n", search_val, match_val); #endif }/* (*inbytesleft) && (*outbytesleft) */ if ( *inbytesleft && (!(*outbytesleft)) ) { errno = E2BIG; return ((size_t)-1); } return (*inbytesleft); } /* * Performs the binary search in the lookup table of structure * st. Memebers (left_to_right, right_to_left) control * the lookup direction. */ int bisearch(unsigned long val, _icv_state *st, int n) { int low, high, mid; #ifdef TEST fprintf(stderr, "Search: %x limit: %d \n", val, n); #endif low = 0; high = n - 1; while ( low <= high ) { mid = (low + high) / 2; if ( st->left_to_right ) { if ( val < st->table[mid].left_code ) high = mid - 1; else if ( val > st->table[mid].left_code ) low = mid + 1; else /* found match */ return mid; } else { if ( val < st->table[mid].right_code ) high = mid - 1; else if ( val > st->table[mid].right_code ) low = mid + 1; else /* found match */ return mid; } } return (-1); } /* * CDDL HEADER START * * The contents of this file are subject to the terms of the * Common Development and Distribution License (the "License"). * You may not use this file except in compliance with the License. * * You can obtain a copy of the license at src/OPENSOLARIS.LICENSE * or http://www.opensolaris.org/os/licensing. * See the License for the specific language governing permissions * and limitations under the License. * * When distributing Covered Code, include this CDDL HEADER in each * file and include the License file at src/OPENSOLARIS.LICENSE. * If applicable, add the following below this CDDL HEADER, with the * fields enclosed by brackets "[]" replaced with your own identifying * information: Portions Copyright [yyyy] [name of copyright owner] * * CDDL HEADER END */ /* * Copyright (c) 1997, by Sun Microsystems, Inc. * All rights reserved. */ typedef struct _lookup_table { unsigned long left_code; unsigned long right_code; } lookup_table; typedef enum { SHIFT_OUT=0x0e, SHIFT_IN } SHIFT; typedef struct _icv_state { boolean_t left_to_right; /* if true the search input characters * in the left column */ boolean_t right_to_left; /* * if true then search input characters * in the right column */ lookup_table *table; /* mapping table */ int table_size; /* no of lookup records */ int left_code_size; /* data size of the left code */ int right_code_size; /* data size of the right code */ char keepc[6]; /* maximum # byte of UTF8 code */ short ustate; SHIFT shift; int _errno; /* internal errno */ } _icv_state; extern int errno; /* external errno */ #define UCS2_NON_ID_CHAR 0xFFFD #define NON_ID_CHAR 0x3F3F /* '??' */ extern size_t _icv_iconv_lu(_icv_state *st, unsigned char **ibuf, size_t *inbytesleft, unsigned char **obuf, size_t *outbytesleft); /* * CDDL HEADER START * * The contents of this file are subject to the terms of the * Common Development and Distribution License (the "License"). * You may not use this file except in compliance with the License. * * You can obtain a copy of the license at src/OPENSOLARIS.LICENSE * or http://www.opensolaris.org/os/licensing. * See the License for the specific language governing permissions * and limitations under the License. * * When distributing Covered Code, include this CDDL HEADER in each * file and include the License file at src/OPENSOLARIS.LICENSE. * If applicable, add the following below this CDDL HEADER, with the * fields enclosed by brackets "[]" replaced with your own identifying * information: Portions Copyright [yyyy] [name of copyright owner] * * CDDL HEADER END */ /* * Copyright 1998 Sun Microsystems, Inc. All rights reserved. * Use is subject to license terms. */ #ifndef UCS2_H #define UCS2_H #include #include #include "gentypes.h" #define UCS2_NBYTE 2 #define UCS2_MAXVAL 0xfffd #define valid_ucs2_value(n) (_valid_ucs2_value(n)) #define room_for_ucs2_cnv(p1,p2) (((p1)+sizeof(ucs2_t))<=(p2)) #define no_room_for_ucs2_cnv(p1,p2) (((p1)+sizeof(ucs2_t))>(p2)) #define incomplete_ucs2_seq(p1,p2) (((p1)+sizeof(ucs2_t))>(p2)) #define next_ucs2_ptr(p) (((uchar_t*)(p))+sizeof(ucs2_t)) #define ext_ucs2_lsb(n) ((uchar_t)(((ucs2_t)(n))&((ucs2_t)0x00ff))) #define ext_ucs2_msb(n) ((uchar_t)(((((ucs2_t)(n))&((ucs2_t)0xff00)))>>8)) #define get_ucs2_word(p) (*((ucs2_t*)(p))) #define set_ucs2_word(p,n) ((*((ucs2_t*)(p)))=(n)) #if defined(_BIG_ENDIAN) #define get_ucs2_word_BB(p) \ (((ucs2_t)((*(p))<<8))|((ucs2_t)(*((p)+1)))) #define set_ucs2_word_BB(p,n) \ (((*(p))=ext_ucs2_msb(n)),((*((p)+1))=ext_ucs2_lsb(n))) #else #define get_ucs2_word_BB(p) \ (((ucs2_t)(((*((p)+1)))<<8))|((ucs2_t)(*(p)))) #define set_ucs2_word_BB(p,n) \ ((*((p)+1))=ext_ucs2_msb(n),(*(p))=ext_ucs2_lsb(n)) #endif int _valid_ucs2_value(ucs4_t); #endif /* * CDDL HEADER START * * The contents of this file are subject to the terms of the * Common Development and Distribution License (the "License"). * You may not use this file except in compliance with the License. * * You can obtain a copy of the license at src/OPENSOLARIS.LICENSE * or http://www.opensolaris.org/os/licensing. * See the License for the specific language governing permissions * and limitations under the License. * * When distributing Covered Code, include this CDDL HEADER in each * file and include the License file at src/OPENSOLARIS.LICENSE. * If applicable, add the following below this CDDL HEADER, with the * fields enclosed by brackets "[]" replaced with your own identifying * information: Portions Copyright [yyyy] [name of copyright owner] * * CDDL HEADER END */ /* * Copyright 1998 Sun Microsystems, Inc. All rights reserved. * Use is subject to license terms. */ #ifndef UCS4_H #define UCS4_H #include #include #include "ucs2.h" #include "gentypes.h" #define UCS4_NBYTE 4 #define UCS4_MAXVAL 0x7fffffff #define UCS4_PPRC_MAXVAL 0xfffd /* PPRC: Per Plane Row Column */ #define room_for_ucs4_cnv(p1,p2) (((p1)+sizeof(ucs4_t))<=(p2)) #define no_room_for_ucs4_cnv(p1,p2) (((p1)+sizeof(ucs4_t))>(p2)) #define incomplete_ucs4_seq(p1,p2) (((p1)+sizeof(ucs4_t))>(p2)) #define valid_ucs4_value(n) (_valid_ucs4_value(n)) #define ext_ucs4_lsw(n) ((ucs2_t)((((ucs4_t)(n))&((ucs4_t)0x0000ffff)))) #define ext_ucs4_msw(n) ((ucs2_t)((((ucs4_t)(n))&((ucs4_t)0xffff0000))>>16)) #define get_ucs4_word(p) (*((ucs4_t*)(p))) #define set_ucs4_word(p,n) ((*((ucs4_t*)(p)))=n) #if defined(_BIG_ENDIAN) #define get_ucs4_word_BB(p) (((ucs4_t)(get_ucs2_word_BB((p))<<16))|\ ((ucs4_t)(get_ucs2_word_BB((p)+2)))) #define set_ucs4_word_BB(p,n) (set_ucs2_word_BB((p),ext_ucs4_msw(n)),\ set_ucs2_word_BB((p)+2,ext_ucs4_lsw(n))) #else #define get_ucs4_word_BB(p) (((ucs4_t)(get_ucs2_word_BB((p)+2)<<16))|\ ((ucs4_t)(get_ucs2_word_BB(p)))) #define set_ucs4_word_BB(p,n) (set_ucs2_word_BB((p)+2,ext_ucs4_msw(n)),\ set_ucs2_word_BB((p),ext_ucs4_lsw(n))) #endif int _valid_ucs4_value(ucs4_t); #endif /* * CDDL HEADER START * * The contents of this file are subject to the terms of the * Common Development and Distribution License (the "License"). * You may not use this file except in compliance with the License. * * You can obtain a copy of the license at src/OPENSOLARIS.LICENSE * or http://www.opensolaris.org/os/licensing. * See the License for the specific language governing permissions * and limitations under the License. * * When distributing Covered Code, include this CDDL HEADER in each * file and include the License file at src/OPENSOLARIS.LICENSE. * If applicable, add the following below this CDDL HEADER, with the * fields enclosed by brackets "[]" replaced with your own identifying * information: Portions Copyright [yyyy] [name of copyright owner] * * CDDL HEADER END */ /* * Copyright (c) 1997, by Sun Microsystems, Inc. * All rights reserved. */ #include #include #include #include #include "tab_lookup.h" /* table lookup data types */ #define MSB 0x80 /* most significant bit */ #define ONEBYTE 0xff /* right most byte */ enum _USTATE { U0, U1, U11, U2, U3, U4 }; int get_ibm_by_utf(_icv_state *st, char c1, char c2, int *unidx, unsigned long *ibm_code); int bisearch(unsigned long val, _icv_state *st, int n); int utf8_to_ibm(int unidx, unsigned long ibm_code, char *buf, size_t buflen, _icv_state *st); /* * Actual conversion; called from iconv() * Input is UTF-8 data. * first convert to UCS2 */ size_t _icv_iconv(_icv_state *st, char **inbuf, size_t *inbytesleft, char **outbuf, size_t *outbytesleft) { /* * Actual conversion; called from iconv() */ /*========================================================= * * State Machine for interpreting UTF8 code * *========================================================= * * 3 byte unicode * +----->------->-------+ * | | * ^ v * | 2 byte U2 ---> U3 * | unicode v * +------> U0 -------> U1 +-------->U4---+ * ^ ascii | | ^ | * | | +-------->--------->--------+ | * | v v * +----<---+-----<------------<------------<------------+ * * +----<---+-----<------------<------------<------------+ * *=========================================================*/ char c1 = '\0', c2 = '\0'; int n, unidx; unsigned long ibm_code; #ifdef DEBUG fprintf(stderr, "========== iconv(): UTF8 --> IBM ==========\n"); #endif if (st == NULL) { errno = EBADF; return ((size_t) -1); } if (inbuf == NULL || *inbuf == NULL) { /* Reset request. */ st->ustate = U0; st->_errno = 0; st->shift = SHIFT_IN; return ((size_t) 0); } st->_errno = 0; /* reset internal errno */ errno = 0; /* reset external errno */ /* a state machine for interpreting UTF8 code */ while (*inbytesleft > 0 && *outbytesleft > 0) { switch (st->ustate) { case U0: /* it is ascii, convert it immediately */ if ((**inbuf & MSB) == 0) { /* ASCII */ st->ustate = U4; st->keepc[0] = **inbuf; c1 = 0x0; c2 = **inbuf; continue; } else { /* Chinese character */ if ((**inbuf & 0xe0) == 0xc0) { /* 2 byte unicode */ st->ustate = U1; st->keepc[0] = **inbuf; } else if ((**inbuf & 0xf0) == 0xe0) { /* 3 byte */ st->ustate = U2; st->keepc[0] = **inbuf; } else { /* illegal unicode */ /* st->_errno = errno = EINVAL; */ /* possible UNICODE ko_KR-UTF8 */ c1 =st->keepc[0] = **inbuf; st->ustate = U11; break; } } break; case U1: /* 2 byte unicode */ if ((**inbuf & 0xc0) == MSB) { st->ustate = U4; st->keepc[1] = **inbuf; c1 = (st->keepc[0]&0x1c)>>2; c2 = ((st->keepc[0]&0x03)<<6) | ((**inbuf)&0x3f); #ifdef DEBUG fprintf(stderr, "UTF8: %02x%02x --> ", st->keepc[0]&ONEBYTE, st->keepc[1]&ONEBYTE); #endif continue; /* should not advance *inbuf */ } else { st->_errno = errno = EINVAL; } break; case U11: /* 3 byte unicode - 2nd byte */ c2 =st->keepc[1] = **inbuf; st->ustate = U4; continue; break; case U2: /* 3 byte unicode - 2nd byte */ if ((**inbuf & 0xc0) == MSB) { st->ustate = U3; st->keepc[1] = **inbuf; } else { st->_errno = errno = EINVAL; } break; case U3: /* 3 byte unicode - 3rd byte */ if ((**inbuf & 0xc0) == MSB) { st->ustate = U4; st->keepc[2] = **inbuf; c1 = ((st->keepc[0]&0x0f)<<4) | ((st->keepc[1]&0x3c)>>2); c2 = ((st->keepc[1]&0x03)<<6) | ((**inbuf)&0x3f); #ifdef DEBUG fprintf(stderr, "UTF8: %02x%02x%02x --> ", st->keepc[0]&ONEBYTE, st->keepc[1]&ONEBYTE, **inbuf&ONEBYTE); #endif continue; /* should not advance *inbuf */ } else { st->_errno = errno = EINVAL; } break; case U4: n = get_ibm_by_utf(st, c1, c2, &unidx, &ibm_code); if (n != 0) { /* legal unicode;illegal Big5 */ st->_errno = errno = EILSEQ; break; } n = utf8_to_ibm(unidx, ibm_code, *outbuf, *outbytesleft, st); if (n > 0) { (*outbuf) += n; (*outbytesleft) -= n; } else { st->_errno = errno; return((size_t)-1); } st->ustate = U0; st->_errno = 0; break; default: /* should never come here */ st->_errno = errno = EILSEQ; st->ustate = U0; /* reset state */ break; } (*inbuf)++; (*inbytesleft)--; if (st->_errno) { #ifdef DEBUG fprintf(stderr, "!!!!!\tst->_errno = %d\tst->ustate = %d\n", st->_errno, st->ustate); #endif break; } if (errno) return((size_t)-1); } if (*outbytesleft == 0) { errno = E2BIG; return((size_t)-1); } return (*inbytesleft); } /* * Match IBM code by UTF8 code; * Return: = 0 - match from Unicode to IBM found * = 1 - match from Unicode to IBM NOT found * * Since binary search of the UTF8 to IBM table is necessary, might as well * return index and IBM code matching to the unicode. */ int get_ibm_by_utf(st, c1, c2, unidx, ibm_code) _icv_state *st; char c1, c2; int *unidx; unsigned long *ibm_code; { unsigned long unicode; unicode = (unsigned long) ((c1 & ONEBYTE) << 8) + (c2 & ONEBYTE); *unidx = bisearch(unicode, st, st->table_size); if ((*unidx) >= 0) { if ( st->left_to_right ) *ibm_code = st->table[*unidx].right_code; else *ibm_code = st->table[*unidx].left_code; } #ifdef DEBUG fprintf(stderr, "Unicode=%04x, idx=%5d, IBM=%x ", unicode, *unidx, *ibm_code); #endif return(0); } /* * ISO/IEC 10646 (Unicode) --> IBM * Unicode --> UTF8 (FSS-UTF) * (File System Safe Universal Character Set Transformation Format) * Return: > 0 - converted with enough space in output buffer * = 0 - no space in outbuf */ int utf8_to_ibm(int unidx, unsigned long ibm_code, char *buf, size_t buflen, _icv_state *st) { unsigned long val; /* IBM value */ char c1, c2, ibm_str[3]; if (unidx < 0) /* no match from UTF8 to IBM */ ibm_code = (unsigned long)NON_ID_CHAR; { val = ibm_code & 0xffff; c1 = (char) ((val & 0xff00) >> 8); c2 = (char) (val & 0xff); } /* it is single byte ascii */ if ( c1 == 0x0 ) { if ( st->shift == SHIFT_OUT ) { if (buflen < 2) { errno = E2BIG; return 0; } *buf = SHIFT_IN; *(buf+1) = c2; st->shift = SHIFT_IN; return 2; } if (buflen < 1) { errno = E2BIG; return 0; } *buf = c2; return 1; } /* it is the first two bytes character */ if ( st->shift == SHIFT_IN ) { if (buflen < 3) { errno = E2BIG; return 0; } *buf = SHIFT_OUT; st->shift = SHIFT_OUT; *(buf+1) = c1; *(buf+2) = c2; return 3; } *buf = ibm_str[0] = c1; *(buf+1) = ibm_str[1] = c2; ibm_str[2] = '\0'; #ifdef DEBUG fprintf(stderr, "\t->%x %x<-\n", *buf, *(buf+1)); #endif if (buflen < 2) { errno = E2BIG; return(0); } return(2); }