xref: /titanic_51/usr/src/lib/iconv_modules/ko/common/ojh_to_utf_main.c (revision 91e1e26ac6a73ce959289cf7d3d96c4baedbe0b8)
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 "ojh_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 Johap code */
78 		{
79 			hcode_type ojh_code, utf_code;
80 
81 			if ((ibtail - ib) < 2)
82 			{
83 				errno = EINVAL;
84 				ret_val = (size_t)-1;
85 				break;
86 			}
87 
88 			ojh_code.code = 0;
89 			ojh_code.byte.byte3 = *ib;
90 			ojh_code.byte.byte4 = *(ib + 1);
91 
92 			utf_code = _johap82_to_utf8(ojh_code);
93 
94 			if (utf_code.code != 0)
95 			{
96 				if ((obtail - ob) < 3)
97 				{
98 					errno = E2BIG;
99 					ret_val = (size_t)-1;
100 					break;
101 				}
102 				/* UTF8 code from 2 bytes is always 3 bytes */
103 				*ob++ = (char)utf_code.byte.byte2;
104 				*ob++ = (char)utf_code.byte.byte3;
105 				*ob++ = (char)utf_code.byte.byte4;
106 			}
107 			else  /* FAILED - this means input char isn't belong to
108 			       *	  input codeset. */
109 			{
110 				errno = EILSEQ;
111 				ret_val = (size_t)-1;
112 				break;
113 			}
114 			ib += 2;
115 
116 		}
117 		else  /* CS0 */
118 		{
119 			if (ob >= obtail)
120 			{
121 				errno = E2BIG;
122 				ret_val = (size_t)-1;
123 				break;
124 			}
125 			*ob++ = *ib++;
126 		}
127 	}
128 
129 	*inbuf = (char*)ib;
130 	*inbufleft = ibtail - ib;
131 	*outbuf = (char*)ob;
132 	*outbufleft = obtail - ob;
133 
134 	return(ret_val);
135 }  /* end of size_t _icv_iconv(int*, char**, size_t*, char**, size_t*). */
136