xref: /illumos-gate/usr/src/lib/iconv_modules/euro/common/euro.c (revision 66582b606a8194f7f3ba5b3a3a6dca5b0d346361)
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 2010 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #include <stdlib.h>
27 #include <errno.h>
28 #include "euro.h"
29 
30 
31 void *
32 _icv_open()
33 {
34 	return ((void *) MAGIC_NUMBER);
35 }
36 
37 
38 void
39 _icv_close(int *cd)
40 {
41 	if (! cd || cd != (int *)MAGIC_NUMBER)
42 		errno = EBADF;
43 }
44 
45 
46 size_t
47 _icv_iconv(int *cd, char **inbuf, size_t *inbytesleft,
48 		char **outbuf, size_t *outbytesleft)
49 {
50 	size_t ret_val;
51 	unsigned char c;
52 	unsigned char *ib;
53 	unsigned char *ob;
54 	unsigned char *ibtail;
55 	unsigned char *obtail;
56 
57 	if (cd != (int *)MAGIC_NUMBER) {
58 		errno = EBADF;
59 		return ((size_t)-1);
60 	}
61 
62 	if (!inbuf || !(*inbuf))
63 		return ((size_t)0);
64 
65 	ret_val = 0;
66 	ib = (unsigned char *)*inbuf;
67 	ob = (unsigned char *)*outbuf;
68 	ibtail = ib + *inbytesleft;
69 	obtail = ob + *outbytesleft;
70 
71 	while (ib < ibtail) {
72 		c = *ib;
73 
74 		if (tbl[c].sz == ICV_TYPE_ILLEGAL_CHAR) {
75 			errno = EILSEQ;
76 			ret_val = (size_t)-1;
77 			break;
78 		}
79 
80 		if (obtail <= ob) {
81 			errno = E2BIG;
82 			ret_val = (size_t)-1;
83 			break;
84 		}
85 
86 		*ob++ = tbl[c].ch;
87 		ib++;
88 	}
89 
90 	*inbuf = (char *)ib;
91 	*inbytesleft = ibtail - ib;
92 	*outbuf = (char *)ob;
93 	*outbytesleft = obtail - ob;
94 
95 	return (ret_val);
96 }
97