1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * You can obtain a copy of the license at src/OPENSOLARIS.LICENSE 9 * or http://www.opensolaris.org/os/licensing. 10 * See the License for the specific language governing permissions 11 * and limitations under the License. 12 * 13 * When distributing Covered Code, include this CDDL HEADER in each 14 * file and include the License file at src/OPENSOLARIS.LICENSE. 15 * If applicable, add the following below this CDDL HEADER, with the 16 * fields enclosed by brackets "[]" replaced with your own identifying 17 * information: Portions Copyright [yyyy] [name of copyright owner] 18 * 19 * CDDL HEADER END 20 */ 21 /* 22 * Copyright (c) 1996 by Sun Microsystems, Inc. 23 */ 24 25 26 #include <errno.h> 27 #include <widec.h> 28 #include "common_def.h" 29 #include "common_thai.h" 30 #include "euc_utf_api.h" 31 32 33 /**** _ I C V _ O P E N ****/ 34 35 void* _icv_open() 36 { 37 return((void*)MAGIC_NUMBER); 38 } /* end of int _icv_open(). */ 39 40 41 /**** _ I C V _ C L O S E ****/ 42 43 void _icv_close(int* cd) 44 { 45 if (!cd || cd != (int*)MAGIC_NUMBER) 46 errno = EBADF; 47 } /* end of void _icv_close(int*). */ 48 49 50 /**** _ I C V _ I C O N V ****/ 51 52 size_t _icv_iconv(int* cd, char** inbuf, size_t* inbufleft, 53 char** outbuf, size_t* outbufleft) 54 { 55 size_t ret_val = 0; 56 unsigned char* ib; 57 unsigned char* ob; 58 unsigned char* ibtail; 59 unsigned char* obtail; 60 61 if (!cd || cd != (int*)MAGIC_NUMBER) 62 { 63 errno = EBADF; 64 return((size_t)-1); 65 } 66 67 if (!inbuf || !(*inbuf)) 68 return((size_t)0); 69 70 ib = (unsigned char*)*inbuf; 71 ob = (unsigned char*)*outbuf; 72 ibtail = ib + *inbufleft; 73 obtail = ob + *outbufleft; 74 75 while (ib < ibtail) 76 { 77 if (*ib & 0x80) /* Thai EUC doesn't have CS2 or CS3. */ 78 { 79 hcode_type euc_code, utf_code; 80 81 if ((ibtail - ib) < 1) 82 { 83 errno = EINVAL; 84 ret_val = (size_t)-1; 85 break; 86 } 87 88 if ((*ib < 0xA1) || 89 (*ib > 0xFB) || 90 ((*ib > 0xDC) && (*ib < 0xDE))) 91 { 92 errno = EILSEQ; 93 ret_val = (size_t)-1; 94 break; 95 } 96 97 euc_code.code = 0; 98 euc_code.byte.byte4 = *ib; 99 100 utf_code = _eucTH_to_utf8(euc_code); 101 102 if (utf_code.code != 0) 103 { 104 if ((obtail - ob) < 3) 105 { 106 errno = E2BIG; 107 ret_val = (size_t)-1; 108 break; 109 } 110 /* UTF8 code from 2 bytes is always 3 bytes */ 111 *ob++ = (char)utf_code.byte.byte2; 112 *ob++ = (char)utf_code.byte.byte3; 113 *ob++ = (char)utf_code.byte.byte4; 114 } 115 else /* FAILED - this means input char isn't belong to 116 * input codeset. */ 117 { 118 errno = EILSEQ; 119 ret_val = (size_t)-1; 120 break; 121 } 122 ib += 1; 123 124 } 125 else /* CS0 */ 126 { 127 if (ob >= obtail) 128 { 129 errno = E2BIG; 130 ret_val = (size_t)-1; 131 break; 132 } 133 *ob++ = *ib++; 134 } 135 } 136 137 *inbuf = (char*)ib; 138 *inbufleft = ibtail - ib; 139 *outbuf = (char*)ob; 140 *outbufleft = obtail - ob; 141 142 return(ret_val); 143 } /* end of size_t _icv_iconv(int*, char**, size_t*, char**, size_t*). */ 144