xref: /titanic_51/usr/src/lib/iconv_modules/euro/common/euro.c (revision 91e1e26ac6a73ce959289cf7d3d96c4baedbe0b8)
1*91e1e26aSAlexander Pyhalov /*
2*91e1e26aSAlexander Pyhalov  * CDDL HEADER START
3*91e1e26aSAlexander Pyhalov  *
4*91e1e26aSAlexander Pyhalov  * The contents of this file are subject to the terms of the
5*91e1e26aSAlexander Pyhalov  * Common Development and Distribution License (the "License").
6*91e1e26aSAlexander Pyhalov  * You may not use this file except in compliance with the License.
7*91e1e26aSAlexander Pyhalov  *
8*91e1e26aSAlexander Pyhalov  * You can obtain a copy of the license at src/OPENSOLARIS.LICENSE
9*91e1e26aSAlexander Pyhalov  * or http://www.opensolaris.org/os/licensing.
10*91e1e26aSAlexander Pyhalov  * See the License for the specific language governing permissions
11*91e1e26aSAlexander Pyhalov  * and limitations under the License.
12*91e1e26aSAlexander Pyhalov  *
13*91e1e26aSAlexander Pyhalov  * When distributing Covered Code, include this CDDL HEADER in each
14*91e1e26aSAlexander Pyhalov  * file and include the License file at src/OPENSOLARIS.LICENSE.
15*91e1e26aSAlexander Pyhalov  * If applicable, add the following below this CDDL HEADER, with the
16*91e1e26aSAlexander Pyhalov  * fields enclosed by brackets "[]" replaced with your own identifying
17*91e1e26aSAlexander Pyhalov  * information: Portions Copyright [yyyy] [name of copyright owner]
18*91e1e26aSAlexander Pyhalov  *
19*91e1e26aSAlexander Pyhalov  * CDDL HEADER END
20*91e1e26aSAlexander Pyhalov  */
21*91e1e26aSAlexander Pyhalov /*
22*91e1e26aSAlexander Pyhalov  * Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
23*91e1e26aSAlexander Pyhalov  * Use is subject to license terms.
24*91e1e26aSAlexander Pyhalov  */
25*91e1e26aSAlexander Pyhalov 
26*91e1e26aSAlexander Pyhalov #include <stdlib.h>
27*91e1e26aSAlexander Pyhalov #include <errno.h>
28*91e1e26aSAlexander Pyhalov #include "euro.h"
29*91e1e26aSAlexander Pyhalov 
30*91e1e26aSAlexander Pyhalov 
31*91e1e26aSAlexander Pyhalov void *
32*91e1e26aSAlexander Pyhalov _icv_open()
33*91e1e26aSAlexander Pyhalov {
34*91e1e26aSAlexander Pyhalov 	return ((void *) MAGIC_NUMBER);
35*91e1e26aSAlexander Pyhalov }
36*91e1e26aSAlexander Pyhalov 
37*91e1e26aSAlexander Pyhalov 
38*91e1e26aSAlexander Pyhalov void
39*91e1e26aSAlexander Pyhalov _icv_close(int *cd)
40*91e1e26aSAlexander Pyhalov {
41*91e1e26aSAlexander Pyhalov 	if (! cd || cd != (int *)MAGIC_NUMBER)
42*91e1e26aSAlexander Pyhalov 		errno = EBADF;
43*91e1e26aSAlexander Pyhalov }
44*91e1e26aSAlexander Pyhalov 
45*91e1e26aSAlexander Pyhalov 
46*91e1e26aSAlexander Pyhalov size_t
47*91e1e26aSAlexander Pyhalov _icv_iconv(int *cd, char **inbuf, size_t *inbytesleft,
48*91e1e26aSAlexander Pyhalov 		char **outbuf, size_t *outbytesleft)
49*91e1e26aSAlexander Pyhalov {
50*91e1e26aSAlexander Pyhalov 	size_t ret_val;
51*91e1e26aSAlexander Pyhalov 	unsigned char c;
52*91e1e26aSAlexander Pyhalov 	unsigned char *ib;
53*91e1e26aSAlexander Pyhalov 	unsigned char *ob;
54*91e1e26aSAlexander Pyhalov 	unsigned char *ibtail;
55*91e1e26aSAlexander Pyhalov 	unsigned char *obtail;
56*91e1e26aSAlexander Pyhalov 
57*91e1e26aSAlexander Pyhalov 	if (cd != (int *)MAGIC_NUMBER) {
58*91e1e26aSAlexander Pyhalov 		errno = EBADF;
59*91e1e26aSAlexander Pyhalov 		return ((size_t)-1);
60*91e1e26aSAlexander Pyhalov 	}
61*91e1e26aSAlexander Pyhalov 
62*91e1e26aSAlexander Pyhalov 	if (!inbuf || !(*inbuf))
63*91e1e26aSAlexander Pyhalov 		return ((size_t)0);
64*91e1e26aSAlexander Pyhalov 
65*91e1e26aSAlexander Pyhalov 	ret_val = 0;
66*91e1e26aSAlexander Pyhalov 	ib = (unsigned char *)*inbuf;
67*91e1e26aSAlexander Pyhalov 	ob = (unsigned char *)*outbuf;
68*91e1e26aSAlexander Pyhalov 	ibtail = ib + *inbytesleft;
69*91e1e26aSAlexander Pyhalov 	obtail = ob + *outbytesleft;
70*91e1e26aSAlexander Pyhalov 
71*91e1e26aSAlexander Pyhalov 	while (ib < ibtail) {
72*91e1e26aSAlexander Pyhalov 		c = *ib;
73*91e1e26aSAlexander Pyhalov 
74*91e1e26aSAlexander Pyhalov 		if (tbl[c].sz == ICV_TYPE_ILLEGAL_CHAR) {
75*91e1e26aSAlexander Pyhalov 			errno = EILSEQ;
76*91e1e26aSAlexander Pyhalov 			ret_val = (size_t)-1;
77*91e1e26aSAlexander Pyhalov 			break;
78*91e1e26aSAlexander Pyhalov 		}
79*91e1e26aSAlexander Pyhalov 
80*91e1e26aSAlexander Pyhalov 		if (obtail <= ob) {
81*91e1e26aSAlexander Pyhalov 			errno = E2BIG;
82*91e1e26aSAlexander Pyhalov 			ret_val = (size_t)-1;
83*91e1e26aSAlexander Pyhalov 			break;
84*91e1e26aSAlexander Pyhalov 		}
85*91e1e26aSAlexander Pyhalov 
86*91e1e26aSAlexander Pyhalov 		*ob++ = tbl[c].ch;
87*91e1e26aSAlexander Pyhalov 		ib++;
88*91e1e26aSAlexander Pyhalov 	}
89*91e1e26aSAlexander Pyhalov 
90*91e1e26aSAlexander Pyhalov 	*inbuf = (char *)ib;
91*91e1e26aSAlexander Pyhalov 	*inbytesleft = ibtail - ib;
92*91e1e26aSAlexander Pyhalov 	*outbuf = (char *)ob;
93*91e1e26aSAlexander Pyhalov 	*outbytesleft = obtail - ob;
94*91e1e26aSAlexander Pyhalov 
95*91e1e26aSAlexander Pyhalov 	return (ret_val);
96*91e1e26aSAlexander Pyhalov }
97