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) 1995 by Sun Microsystems, Inc. 23 * All Rights Reserved. 24 */ 25 26 27 #include <errno.h> 28 #include "ktable.h" 29 #include "hangulcode.h" 30 31 static unsigned short _wansung_to_johap92(unsigned short code); 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 { 69 return((size_t)0); 70 } 71 72 ib = (unsigned char*)*inbuf; 73 ob = (unsigned char*)*outbuf; 74 ibtail = ib + *inbufleft; 75 obtail = ob + *outbufleft; 76 77 while (ib < ibtail) 78 { 79 if (!(*ib & 0x80)) /* 7 bits */ 80 { 81 if (ob >= obtail) 82 { 83 errno = E2BIG; 84 ret_val = (size_t)-1; 85 break; 86 } 87 *ob++ = *ib++; 88 } 89 else 90 { 91 unsigned short code; 92 93 if ((ibtail - ib) < 2) 94 { 95 errno = EINVAL; 96 ret_val = (size_t)-1; 97 break; 98 } 99 100 if ((obtail - ob) < 2) 101 { 102 errno = E2BIG; 103 ret_val = (size_t)-1; 104 break; 105 } 106 107 code = _wansung_to_johap92((unsigned short)(*ib)<<8 | 108 (unsigned short)(*(ib + 1))); 109 if (code != FAILED && code != ILLEGAL_SEQ) 110 { 111 *ob++ = (unsigned char)(code >> 8); 112 *ob++ = (unsigned char)(code & 0xFF); 113 } 114 else 115 { 116 *ob++ = NON_IDENTICAL; 117 *ob++ = NON_IDENTICAL; 118 } 119 ib += 2; 120 } 121 } 122 123 *inbuf = (char*)ib; 124 *inbufleft = ibtail - ib; 125 *outbuf = (char*)ob; 126 *outbufleft = obtail - ob; 127 128 return(ret_val); 129 } /* end of size_t _icv_iconv(int*, char**, size_t*, char**, size_t*).*/ 130 131 132 /**** _ W A N S U N G _ T O _ J O H A P 9 2 ****/ 133 134 static unsigned short _wansung_to_johap92(unsigned short code) 135 { 136 register unsigned short jc, jc2; 137 register short h, i, l; 138 short ci, v, cf; 139 short disp; 140 long cfbit; 141 142 if ((unsigned short)(code & 0xFF) < 0xA1) 143 return(ILLEGAL_SEQ); 144 145 if (code >= 0xB0A1 && code <= 0xC8FE) /* Hangul */ 146 { 147 for (h = CI_CNT, l = 0; ; ) 148 { 149 ci = (l + h) / 2; 150 if (l >= h) 151 break; 152 if (code < cmp_srchtbl[ci][0]) 153 h = ci - 1; 154 else if (code < cmp_srchtbl[ci + 1][0]) 155 break; 156 else 157 l = ci + 1; 158 } 159 160 for (v = 1; ; ) 161 { 162 if (code < cmp_srchtbl[ci][v]) 163 { 164 while (!cmp_srchtbl[ci][--v]) 165 ; 166 break; 167 } 168 else if (v == V_CNT) 169 break; 170 v++; 171 } 172 173 disp = code - cmp_srchtbl[ci][v]; 174 if (((short)(cmp_srchtbl[ci][v] & BYTE_MASK) + disp) > 0xfe) 175 disp -= SKIP; 176 177 for (cfbit = cmp_bitmap[ci][v], i = -1, cf = -1; i < disp; cf++) 178 { 179 if (cfbit & BIT_MASK) 180 i++; 181 cfbit >>= 1; 182 } 183 184 if (cf == -1) 185 return(FAILED); 186 187 code = ci + 2; 188 code = (code << 5) | (v + (v + 1) / 6 * 2 + 3); 189 return((code << 5) | (cf + cf / 18) | 0x8000); 190 } 191 else if (code >= 0xA1A1 && code <= 0xACFE) /* Special symbols */ 192 { 193 jc = (((unsigned short)(code - 0xA100) >> 1) + 0xD900) & 0xFF00; 194 jc2 = code & 0xFF; 195 if ((unsigned short)(code >> 8) % 2) 196 return(jc | (jc2 - ((jc2 > 0xEE) ? 0x5E : 0x70))); 197 return(jc | jc2); 198 } 199 else if (code >= 0xCAA1 && code <= 0xFDFE) /* Hanja */ 200 { 201 jc = (((unsigned short)(code - 0xCA00) >> 1) + 0xE000) & 0xFF00; 202 jc2 = code & 0xFF; 203 if ((unsigned short)(code >> 8) % 2) 204 return(jc | jc2); 205 return(jc | (jc2 - ((jc2 > 0xEE) ? 0x5E : 0x70))); 206 } 207 else if ((code >= 0xC9A1 && code <= 0xC9FE) || 208 (code >= 0xFEA1 && code <= 0xFEFE)) /* User-definable area */ 209 { 210 if ((code & 0xFF00) == 0xFE00) 211 return(0xD800 | (code & 0xFF)); 212 code = code & 0xFF; 213 return(0xD800 | (code - ((code > 0xEE) ? 0x5E : 0x70))); 214 } 215 216 return(FAILED); 217 } /* end of static unsigned short _wansung_to_johap92(unsigned short). */ 218