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 if ( ic == '\100' ) 123 ic = '_'; 124 else if ( ic == '[' ) 125 ic = '_'; 126 else if ( ic == '\134' ) 127 ic = '_'; 128 else if ( ic == ']' ) 129 ic = '_'; 130 else if ( ic == '^' ) 131 ic = '_'; 132 else if ( ic == '`' ) 133 ic = '_'; 134 else if ( ic == '{' ) 135 ic = '_'; 136 else if ( ic == '|' ) 137 ic = '_'; 138 else if ( ic == '}' ) 139 ic = '_'; 140 else if ( ic == '~' ) 141 ic = '_'; 142 else if ( ic == 252 ) 143 ic = '~'; 144 else if ( ic == 196 ) 145 ic = '['; 146 else if ( ic == 197 ) 147 ic = ']'; 148 else if ( ic == 201 ) 149 ic = '\100'; 150 else if ( ic == 214 ) 151 ic = '\134'; 152 else if ( ic == 220 ) 153 ic = '^'; 154 else if ( ic == 228 ) 155 ic = '{'; 156 else if ( ic == 229 ) 157 ic = '}'; 158 else if ( ic == 233 ) 159 ic = '\140'; 160 else if ( ic == 246 ) 161 ic = '|'; 162 else if (ic > '\177') 163 ic = '_'; 164 165 166 167 168 /* switch ( ic ) 169 { 170 case '\100' : 171 ic = '_'; 172 break; 173 case '[' : 174 ic = '_'; 175 break; 176 case '\134' : 177 ic = '_'; 178 break; 179 case ']' : 180 ic = '_'; 181 break; 182 case '^' : 183 ic = '_'; 184 break; 185 case '`' : 186 ic = '_'; 187 break; 188 case '{' : 189 ic = '_'; 190 break; 191 case '|' : 192 ic = '_'; 193 break; 194 case '}' : 195 ic = '_'; 196 break; 197 case '~' : 198 ic = '_'; 199 break; 200 case 252 : 201 ic = '~'; 202 break; 203 case 196 : 204 ic = '['; 205 break; 206 case 197 : 207 ic = ']'; 208 break; 209 case 201 : 210 ic = '\100'; 211 break; 212 case 214 : 213 ic = '\134'; 214 break; 215 case 220 : 216 ic = '^'; 217 break; 218 case 228 : 219 ic = '{'; 220 break; 221 case 229 : 222 ic = '}'; 223 break; 224 case 233 : 225 ic = '\140'; 226 break; 227 case 246 : 228 ic = '|'; 229 break; 230 default : 231 if (ic > '\177') 232 ic = '_'; 233 break; 234 } */ 235 236 PUT(ic); 237 /* 238 Put the converted character into the output buffer, and decrement 239 the count of chars left in both the in and out buffers. 240 If we have no space left in the out buffer, but we have no reached 241 the end of the input buffer. We return what we have, and set the 242 errno (Error) to E2BIG. 243 */ 244 if ((oleft < 1) && (ileft > 0)) 245 { 246 errno = E2BIG; 247 retval = ERR_RETURN; 248 goto ret; 249 } 250 251 252 } 253 /* 254 We only get here if the end of the in buffer has been reached, we therefore return the 255 value 0 to denote that we have sucesfully converted the inbuffer. 256 */ 257 retval = ileft; 258 259 /* Taken unchanged from @(#)ISO-2022-JP%SJIS. */ 260 261 ret: 262 st->_st_cset = cset; 263 st->_st_stat = stat; 264 *inbuf = ip; 265 *inbytesleft = ileft; 266 ret2: 267 *outbuf = (char *)op; 268 *outbytesleft = oleft; 269 270 return (retval); 271 } 272