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) 1994, Sun Microsystems, Inc. 23 * Copyright (c) 1994, Nihon Sun Microsystems K.K. 24 */ 25 26 #include <stdio.h> 27 #include <stdlib.h> 28 #include <errno.h> 29 #include <euc.h> 30 #include "japanese.h" 31 32 /* 33 * struct _cv_state; to keep status 34 */ 35 struct _icv_state { 36 int _st_cset; 37 int _st_stat; 38 }; 39 40 extern int errno; 41 42 /* 43 * Open; called from iconv_open(); as taken unchanged from @(#)ISO-2022-JP%SJIS. 44 */ 45 void * 46 _icv_open() 47 { 48 struct _icv_state *st; 49 50 if ((st = (struct _icv_state *)malloc(sizeof(struct _icv_state))) 51 == NULL) 52 return ((void *)ERR_RETURN); 53 54 st->_st_cset = CS_0; 55 st->_st_stat = ST_INIT; 56 57 return (st); 58 } 59 60 61 /* 62 * Close; called from iconv_close(); as taken unchanged from @(#)ISO-2022-JP%SJIS. 63 */ 64 void 65 _icv_close(struct _icv_state *st) 66 { 67 free(st); 68 } 69 70 71 72 /* 73 * Actual conversion; called from iconv() 74 */ 75 size_t 76 _icv_iconv(struct _icv_state *st, char **inbuf, size_t *inbytesleft, 77 char **outbuf, size_t *outbytesleft) 78 { 79 int cset, stat; 80 unsigned char *op, ic; 81 char *ip; 82 size_t ileft, oleft; 83 size_t retval; 84 85 cset = st->_st_cset; 86 stat = st->_st_stat; 87 88 /* 89 * If (inbuf == 0 || *inbuf == 0) then this conversion is 90 * placed into initial state. 91 */ 92 if ((inbuf == 0) || (*inbuf == 0)) { 93 cset = CS_0; 94 stat = ST_INIT; 95 op = (unsigned char *)*outbuf; 96 oleft = *outbytesleft; 97 retval = 0; 98 goto ret2; 99 } 100 101 ip = *inbuf; 102 op = (unsigned char *)*outbuf; 103 ileft = *inbytesleft; 104 oleft = *outbytesleft; 105 /* Everything down to here was taken unchanged from @(#)ISO-2022-JP%SJIS. 106 ======================================================================= 107 108 * 109 * Main loop; basically 1 loop per 1 input byte 110 */ 111 112 while (ileft > 0) 113 { 114 GET(ic); 115 /* 116 If the char is one of the following [ / ] { | } then convert 117 it to its corresponding value. In all other cases if the char 118 is greater than octal \178 ( ie a high bit char) convert it 119 to an underscore (_), as it has no mapping to 7 bit ASCII. 120 Otrherwise the char is the same in both cose sets. 121 */ 122 123 if ( ic == '\100' ) 124 ic = '\311'; 125 else if ( ic == '[' ) 126 ic = '\304'; 127 else if ( ic == '\134' ) 128 ic = '\326'; 129 else if ( ic == ']' ) 130 ic = '\305'; 131 else if ( ic == '^' ) 132 ic = '\334'; 133 else if ( ic == '`' ) 134 ic = '\351'; 135 else if ( ic == '{' ) 136 ic = '\344'; 137 else if ( ic == '|' ) 138 ic = '\366'; 139 else if ( ic == '}' ) 140 ic = '\345'; 141 else if ( ic == '~' ) 142 ic = '\374'; 143 else if (ic > '\177') 144 ic = '_'; 145 146 /* switch ( ic ) 147 { 148 case '\100' : 149 ic = '\311'; 150 break; 151 case '[' : 152 ic = '\304'; 153 break; 154 case '\134' : 155 ic = '\326'; 156 break; 157 case ']' : 158 ic = '\305'; 159 break; 160 case '^' : 161 ic = '\334'; 162 break; 163 case '`' : 164 ic = '\351'; 165 break; 166 case '{' : 167 ic = '\344'; 168 break; 169 case '|' : 170 ic = '\366'; 171 break; 172 case '}' : 173 ic = '\345'; 174 break; 175 case '~' : 176 ic = '\374'; 177 break; 178 default : 179 if (ic > '\177') 180 ic = '_'; 181 break; 182 } */ 183 184 PUT(ic); 185 /* 186 Put the converted character into the output buffer, and decrement 187 the count of chars left in both the in and out buffers. 188 If we have no space left in the out buffer, but we have no reached 189 the end of the input buffer. We return what we have, and set the 190 errno (Error) to E2BIG. 191 */ 192 if ((oleft < 1) && (ileft > 0)) 193 { 194 errno = E2BIG; 195 retval = ERR_RETURN; 196 goto ret; 197 } 198 199 200 } 201 /* 202 We only get here if the end of the in buffer has been reached, we therefore return the 203 value 0 to denote that we have sucesfully converted the inbuffer. 204 */ 205 retval = ileft; 206 207 /* Taken unchanged from @(#)ISO-2022-JP%SJIS. */ 208 209 ret: 210 st->_st_cset = cset; 211 st->_st_stat = stat; 212 *inbuf = ip; 213 *inbytesleft = ileft; 214 ret2: 215 *outbuf = (char *)op; 216 *outbytesleft = oleft; 217 218 return (retval); 219 } 220