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 AND PERMISSION NOTICE 23 * 24 * Copyright (c) 1991-2005 Unicode, Inc. All rights reserved. Distributed 25 * under the Terms of Use in http://www.unicode.org/copyright.html. 26 * 27 * This file has been modified by Sun Microsystems, Inc. 28 * 29 */ 30 /* 31 * Copyright (c) 1994, 1995 by Sun Microsystems, Inc. 32 * Copyright (c) 1994, Nihon Sun Microsystems K.K. 33 * All Rights Reserved. 34 */ 35 36 /* 37 Converts EBCDIC IBM-850 to ASCII ISO 8859-1. 38 By Greg Nakhimovsky, Sun Microsystems. 39 April, 1996. 40 */ 41 #include <stdio.h> 42 #include <stdlib.h> 43 #include <errno.h> 44 #include <ctype.h> 45 46 #define MAGIC_NUMBER (0x216513) 47 #define ERR_RETURN (-1) /* result code on error */ 48 49 #define GET(c) ((c) = *ip, ip++, ileft--) 50 #define PUT(c) (*op = (c), op++, oleft--) 51 #define UNGET() (ip--, ileft++) 52 53 54 /* 55 * Open; called from iconv_open() 56 */ 57 void * 58 _icv_open() 59 { 60 return ((void*)MAGIC_NUMBER); 61 } 62 63 64 /* 65 * Close; called from iconv_close 66 */ 67 void 68 _icv_close(int* cd) 69 { 70 if (!cd || cd != (int*)MAGIC_NUMBER) 71 errno = EBADF; 72 } 73 74 75 /* 76 * Actual conversion; called from iconv() 77 */ 78 size_t 79 _icv_iconv(int* cd, char **inbuf, size_t *inbytesleft, 80 char **outbuf, size_t *outbytesleft) 81 { 82 unsigned char *ip, ic, *op; 83 size_t ileft, oleft; 84 size_t retval = 0; 85 86 static unsigned char map[256] = { 87 0x00 , 0x01 , 0x02 , 0x03 , 0x04 , 0x05 , 0x06 , 0x07 , 0x08 , 0x09 , 88 0x0A , 0x0B , 0x0C , 0x0D , 0x0E , 0x0F , 0x10 , 0x11 , 0x12 , 0x13 , 89 0x14 , 0x15 , 0x16 , 0x17 , 0x18 , 0x19 , 0x1C , 0x1B , 0x7F , 0x1D , 90 0x1E , 0x1F , 0x20 , 0x21 , 0x22 , 0x23 , 0x24 , 0x25 , 0x26 , 0x27 , 91 0x28 , 0x29 , 0x2A , 0x2B , 0x2C , 0x2D , 0x2E , 0x2F , 0x30 , 0x31 , 92 0x32 , 0x33 , 0x34 , 0x35 , 0x36 , 0x37 , 0x38 , 0x39 , 0x3A , 0x3B , 93 0x3C , 0x3D , 0x3E , 0x3F , 0x40 , 0x41 , 0x42 , 0x43 , 0x44 , 0x45 , 94 0x46 , 0x47 , 0x48 , 0x49 , 0x4A , 0x4B , 0x4C , 0x4D , 0x4E , 0x4F , 95 0x50 , 0x51 , 0x52 , 0x53 , 0x54 , 0x55 , 0x56 , 0x57 , 0x58 , 0x59 , 96 0x5A , 0x5B , 0x5C , 0x5D , 0x5E , 0x5F , 0x60 , 0x61 , 0x62 , 0x63 , 97 0x64 , 0x65 , 0x66 , 0x67 , 0x68 , 0x69 , 0x6A , 0x6B , 0x6C , 0x6D , 98 0x6E , 0x6F , 0x70 , 0x71 , 0x72 , 0x73 , 0x74 , 0x75 , 0x76 , 0x77 , 99 0x78 , 0x79 , 0x7A , 0x7B , 0x7C , 0x7D , 0x7E , 0x1A , 0xC7 , 0xFC , 100 0xE9 , 0xE2 , 0xE4 , 0xE0 , 0xE5 , 0xE7 , 0xEA , 0xEB , 0xE8 , 0xEF , 101 0xEE , 0xEC , 0xC4 , 0xC5 , 0xC9 , 0xE6 , 0xC6 , 0xF4 , 0xF6 , 0xF2 , 102 0xFB , 0xF9 , 0xFF , 0xD6 , 0xDC , 0xF8 , 0xA3 , 0xD8 , 0xD7 , 0x1A , 103 0xE1 , 0xED , 0xF3 , 0xFA , 0xF1 , 0xD1 , 0xAA , 0xBA , 0xBF , 0xAE , 104 0xAC , 0xBD , 0xBC , 0xA1 , 0xAB , 0xBB , 0x1A , 0x1A , 0x1A , 0x1A , 105 0x1A , 0xC1 , 0xC2 , 0xC0 , 0xA9 , 0x1A , 0x1A , 0x1A , 0x1A , 0xA2 , 106 0xA5 , 0x1A , 0x1A , 0x1A , 0x1A , 0x1A , 0x1A , 0x1A , 0xE3 , 0xC3 , 107 0x1A , 0x1A , 0x1A , 0x1A , 0x1A , 0x1A , 0x1A , 0xA4 , 0xF0 , 0xD0 , 108 0xCA , 0xCB , 0xC8 , 0x1A , 0xCD , 0xCE , 0xCF , 0x1A , 0x1A , 0x1A , 109 0x1A , 0xA6 , 0xCC , 0x1A , 0xD3 , 0xDF , 0xD4 , 0xD2 , 0xF5 , 0xD5 , 110 0xB5 , 0xFE , 0xDE , 0xDA , 0xDB , 0xD9 , 0xFD , 0xDD , 0xAF , 0xB4 , 111 0xAD , 0xB1 , 0x1A , 0xBE , 0xB6 , 0xA7 , 0xF7 , 0xB8 , 0xB0 , 0xA8 , 112 0xB7 , 0xB9 , 0xB3 , 0xB2 , 0x1A , 0xA0 }; 113 114 if (!cd || cd != (int*)MAGIC_NUMBER) 115 { 116 errno = EBADF; 117 return((size_t)ERR_RETURN); 118 } 119 120 if ((inbuf == 0) || (*inbuf == 0)) 121 return((size_t)0); 122 123 ip = (unsigned char*)*inbuf; 124 op = (unsigned char *)*outbuf; 125 ileft = *inbytesleft; 126 oleft = *outbytesleft; 127 128 /* 129 * Main loop; basically 1 loop per 1 input byte 130 */ 131 132 while (ileft > 0) { 133 GET(ic); 134 if (oleft < 1) { 135 UNGET(); 136 errno = E2BIG; 137 retval = ERR_RETURN; 138 goto ret; 139 } 140 PUT(map[ic]); 141 retval++; 142 } 143 144 ret: 145 *inbuf = (char *)ip; 146 *inbytesleft = ileft; 147 *outbuf = (char *)op; 148 *outbytesleft = oleft; 149 150 return (retval); 151 } 152