xref: /titanic_51/usr/src/lib/iconv_modules/ko/common/utf_to_njh_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 "utf_njh_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))		/* 7 bits */
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)	/* 16 bits */
88 		{
89 			hcode_type utf8_code, njh_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 			njh_code = _utf8_to_johap92(utf8_code);
111 
112 			if (njh_code.code != 0) {
113 				/* If find something -> Johap92 code */
114 				*ob++ = njh_code.byte.byte3;
115 				*ob++ = njh_code.byte.byte4;
116 			}
117 			else
118 			{
119 				/* Let's assume the code is non-identical. */
120 				if ((obtail - ob) < 2)
121 				{
122 					errno = E2BIG;
123 					ret_val = (size_t)-1;
124 					break;
125 				}
126 				*ob++ = NON_IDENTICAL;
127 				*ob++ = NON_IDENTICAL;
128 				ret_val += 2;
129 			}
130 			ib += 3;
131 		}
132 		else  /* 11, 21, 26 & 31 bits codes won't be able to convert. */
133 		{
134 			short int offset;
135 
136 			if ((*ib & 0xE0) == 0xC0)  /* 11 */
137 				offset = 2;
138 			else if ((*ib & 0xF0) == 0xE0)  /* 16 */
139 				offset = 3;
140 			else if ((*ib & 0xF8) == 0xF0)  /* 21 */
141 				offset = 4;
142 			else if ((*ib & 0xFC) == 0xF8)  /* 26 */
143 				offset = 5;
144 			else if ((*ib & 0xFE) == 0xFC)  /* 31 */
145 				offset = 6;
146 			else  /* Illegal sequence. */
147 				offset = 1;
148 
149 			if ((ibtail - ib) < offset)
150 			{
151 				errno = EINVAL;
152 				ret_val = (size_t)-1;
153 				break;
154 			}
155 
156 			if (!is_valid_utf8_string(ib, offset))
157 		        {
158 				errno = EILSEQ;
159 				ret_val = (size_t)-1;
160 				break;
161 		        }
162 
163 			ib += offset;
164 
165 			/* Let's assume the code is non-identical. */
166 			offset = (offset > 2) ? 2 : 1;
167 			if ((obtail - ob) < offset)
168 			{
169 				errno = E2BIG;
170 				ret_val = (size_t)-1;
171 				break;
172 			}
173 			*ob++ = NON_IDENTICAL;
174 			if (offset > 1)
175 				*ob++ = NON_IDENTICAL;
176 			ret_val += offset;
177 		}
178 	}
179 
180 	*inbuf = (char*)ib;
181 	*inbufleft = ibtail - ib;
182 	*outbuf = (char*)ob;
183 	*outbufleft = obtail - ob;
184 
185 	return(ret_val);
186 }  /* end of size_t _icv_iconv(int*, char**, size_t*, char**, size_t*).*/
187