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 "utf_euc_api.h" 31 #include "common_defs.h" 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)) /* 1-byte sequence */ 78 { 79 if (ob >= obtail) 80 { 81 errno = E2BIG; 82 ret_val = (size_t)-1; 83 break; 84 } 85 *ob++ = *ib++; 86 } 87 else if((*ib & 0xF0) == 0xE0) /* 3-byte sequence */ 88 { 89 hcode_type utf8_code, euc_code; 90 91 if ((ibtail - ib) < 3) 92 { 93 errno = EINVAL; 94 ret_val = (size_t)-1; 95 break; 96 } 97 98 if (!is_valid_utf8_string(ib, 3)) 99 { 100 errno = EILSEQ; 101 ret_val = (size_t)-1; 102 break; 103 } 104 105 utf8_code.byte.byte1 = 0; 106 utf8_code.byte.byte2 = *ib; 107 utf8_code.byte.byte3 = *(ib + 1); 108 utf8_code.byte.byte4 = *(ib + 2); 109 110 euc_code = _utf8_to_wansung(utf8_code); 111 112 if ((obtail - ob) < 2) 113 { 114 errno = E2BIG; 115 ret_val = (size_t)-1; 116 break; 117 } 118 119 if (euc_code.code != 0) { 120 /* If find something -> EUC code */ 121 *ob++ = euc_code.byte.byte3; 122 *ob++ = euc_code.byte.byte4; 123 } 124 else 125 { 126 /* Let's assume the code is non-identical. */ 127 *ob++ = NON_IDENTICAL; 128 ret_val += 1; 129 } 130 ib += 3; 131 } 132 else if((*ib & 0xE0) == 0xC0) /* 2-byte sequence */ 133 { 134 hcode_type utf8_code, euc_code; 135 136 if ((ibtail - ib) < 2) 137 { 138 errno = EINVAL; 139 ret_val = (size_t)-1; 140 break; 141 } 142 143 if (!is_valid_utf8_string(ib, 2)) 144 { 145 errno = EILSEQ; 146 ret_val = (size_t)-1; 147 break; 148 } 149 150 utf8_code.byte.byte1 = 0; 151 utf8_code.byte.byte2 = 0; 152 utf8_code.byte.byte3 = *ib; 153 utf8_code.byte.byte4 = *(ib + 1); 154 155 euc_code = _utf8_to_wansung(utf8_code); 156 157 if ((obtail - ob) < 2) 158 { 159 errno = E2BIG; 160 ret_val = (size_t)-1; 161 break; 162 } 163 164 if (euc_code.code != 0) { 165 *ob++ = euc_code.byte.byte3; 166 *ob++ = euc_code.byte.byte4; 167 } 168 else 169 { 170 *ob++ = NON_IDENTICAL; 171 ret_val += 1; 172 } 173 ib += 2; 174 } 175 else /* 11, 21, 26 & 31 bits codes won't be able to convert. */ 176 { 177 short int offset, offset2; 178 179 if ((*ib & 0xE0) == 0xC0) /* 11 */ 180 offset = 2; 181 else if ((*ib & 0xF0) == 0xE0) /* 16 */ 182 offset = 3; 183 else if ((*ib & 0xF8) == 0xF0) /* 21 */ 184 offset = 4; 185 else if ((*ib & 0xFC) == 0xF8) /* 26 */ 186 offset = 5; 187 else if ((*ib & 0xFE) == 0xFC) /* 31 */ 188 offset = 6; 189 else /* Illegal sequence. */ 190 offset = 1; 191 192 if ((ibtail - ib) < offset) 193 { 194 errno = EINVAL; 195 ret_val = (size_t)-1; 196 break; 197 } 198 199 if (!is_valid_utf8_string(ib, offset)) 200 { 201 errno = EILSEQ; 202 ret_val = (size_t)-1; 203 break; 204 } 205 206 /* Let's assume the code is non-identical. */ 207 offset2 = (offset > 2) ? 2 : 1; 208 if ((obtail - ob) < offset2) 209 { 210 errno = E2BIG; 211 ret_val = (size_t)-1; 212 break; 213 } 214 215 *ob++ = NON_IDENTICAL; 216 if (offset2 > 1) 217 *ob++ = NON_IDENTICAL; 218 219 ib += offset; 220 221 ret_val += offset2; 222 } 223 } 224 225 *inbuf = (char*)ib; 226 *inbufleft = ibtail - ib; 227 *outbuf = (char*)ob; 228 *outbufleft = obtail - ob; 229 230 return(ret_val); 231 } /* end of size_t _icv_iconv(int*, char**, size_t*, char**, size_t*).*/ 232