xref: /illumos-gate/usr/src/lib/iconv_modules/th_TH/common/838_to_874_main.c (revision 16d8656330ae5622ec32e5007f62145ebafdc50f)
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) 1999 by Sun Microsystems, Inc.
23  */
24 
25 
26 #include <errno.h>
27 #include <widec.h>
28 #include "common_def.h"
29 #include "ibm_thai_api.h"
30 #include "common_thai.h"
31 
32 /****  _ I C V _ O P E N  ****/
33 
_icv_open()34 void* _icv_open()
35 {
36         return((void*)MAGIC_NUMBER);
37 }  /* end of int _icv_open(). */
38 
39 
40 /****  _ I C V _ C L O S E  ****/
41 
_icv_close(int * cd)42 void _icv_close(int* cd)
43 {
44         if (!cd || cd != (int*)MAGIC_NUMBER)
45                 errno = EBADF;
46 }  /* end of void _icv_close(int*). */
47 
48 
49 /****  _ I C V _ I C O N V  ****/
50 
_icv_iconv(int * cd,char ** inbuf,size_t * inbufleft,char ** outbuf,size_t * outbufleft)51 size_t _icv_iconv(int* cd, char** inbuf, size_t* inbufleft,
52 			char** outbuf, size_t* outbufleft)
53 {
54 	size_t		ret_val = 0;
55 	unsigned char*	ib;
56 	unsigned char*	ob;
57 	unsigned char*	ibtail;
58 	unsigned char*	obtail;
59 
60 	if (!cd || cd != (int*)MAGIC_NUMBER)
61 	{
62 		errno = EBADF;
63 		return((size_t)-1);
64 	}
65 
66 	if (!inbuf || !(*inbuf))
67 		return((size_t)0);
68 
69 	ib = (unsigned char*)*inbuf;
70 	ob = (unsigned char*)*outbuf;
71 	ibtail = ib + *inbufleft;
72 	obtail = ob + *outbufleft;
73 
74 	while (ib < ibtail)
75 	{
76 		hcode_type euc_code, utf_code;
77 
78 		if ((ibtail - ib) < 1)
79 		{
80 			errno = EINVAL;
81 			ret_val = (size_t)-1;
82 			break;
83 		}
84 
85 		/* euc_code.byte.byte4 is the variable */
86 		/* that would be sent to a function to */
87 		/* find the coversion and get the retu-*/
88 		/* -rned value to put back.	       */
89 		euc_code.code = 0;
90 		euc_code.byte.byte4 = *ib;
91 
92 		utf_code = _838_to_874(euc_code);
93 
94 		if (utf_code.code != 0)
95 		{
96 			if ((obtail - ob) < 1)
97 			{
98 				errno = E2BIG;
99 				ret_val = (size_t)-1;
100 				break;
101 			}
102 			*ob++ = (char)utf_code.byte.byte4;
103 		}
104 		else  /* FAILED - this means input char isn't belong to
105 		       *	  input codeset. */
106 		{
107 			errno = EILSEQ;
108 			ret_val = (size_t)-1;
109 			break;
110 		}
111 		ib += 1;
112 	}
113 
114 	*inbuf = (char*)ib;
115 	*inbufleft = ibtail - ib;
116 	*outbuf = (char*)ob;
117 	*outbufleft = obtail - ob;
118 
119 	return(ret_val);
120 }  /* end of size_t _icv_iconv(int*, char**, size_t*, char**, size_t*). */
121