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_han.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) /* Korean EUC doesn't have CS2 or CS3. */ 78 { 79 hcode_type euc_code, utf_code; 80 81 if ((ibtail - ib) < 2) 82 { 83 errno = EINVAL; 84 ret_val = (size_t)-1; 85 break; 86 } 87 88 if (*ib < 0xA1 || *ib > 0xFE || *(ib + 1) < 0xA1 || 89 *(ib + 1) == 0xFF) 90 { 91 errno = EILSEQ; 92 ret_val = (size_t)-1; 93 break; 94 } 95 96 euc_code.code = 0; 97 euc_code.byte.byte3 = *ib; 98 euc_code.byte.byte4 = *(ib + 1); 99 utf_code = _wansung_to_utf8(euc_code); 100 101 if (utf_code.code != 0) 102 { 103 if ((obtail - ob) < 3) 104 { 105 errno = E2BIG; 106 ret_val = (size_t)-1; 107 break; 108 } 109 /*********************************************** 110 * UTF8 code from 2 bytes is 2 or 3 bytes 111 * as of Unicode 3.1 for security reason. 112 * Thus, we need to check the value of first byte 113 ************************************************/ 114 if((char)utf_code.byte.byte2 != '\0') 115 /************************************************ 116 * if utf-8 is 3byte sequence... 117 *************************************************/ 118 *ob++ = (char)utf_code.byte.byte2; 119 if((char)utf_code.byte.byte3 != '\0') 120 /************************************************ 121 * if utf-8 is 2byte sequence... 122 * The reason why I check the second byte is 123 * becuase there's one byte value returned by 124 * _wansung_to_utf8, which is 'space'. 125 *************************************************/ 126 *ob++ = (char)utf_code.byte.byte3; 127 *ob++ = (char)utf_code.byte.byte4; 128 } 129 else /* FAILED - this means input char isn't belong to 130 * input codeset. */ 131 { 132 errno = EILSEQ; 133 ret_val = (size_t)-1; 134 break; 135 } 136 ib += 2; 137 138 } 139 else /* CS0 */ 140 { 141 if (ob >= obtail) 142 { 143 errno = E2BIG; 144 ret_val = (size_t)-1; 145 break; 146 } 147 *ob++ = *ib++; 148 } 149 } 150 151 *inbuf = (char*)ib; 152 *inbufleft = ibtail - ib; 153 *outbuf = (char*)ob; 154 *outbufleft = obtail - ob; 155 156 return(ret_val); 157 } /* end of size_t _icv_iconv(int*, char**, size_t*, char**, size_t*). */ 158