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 (c) 1994, 1995 by Sun Microsystems, Inc. 23 *91e1e26aSAlexander Pyhalov * Copyright (c) 1994, Nihon Sun Microsystems K.K. 24 *91e1e26aSAlexander Pyhalov * All Rights Reserved. 25 *91e1e26aSAlexander Pyhalov */ 26 *91e1e26aSAlexander Pyhalov 27 *91e1e26aSAlexander Pyhalov #include <stdio.h> 28 *91e1e26aSAlexander Pyhalov #include <stdlib.h> 29 *91e1e26aSAlexander Pyhalov #include <errno.h> 30 *91e1e26aSAlexander Pyhalov #include <ctype.h> 31 *91e1e26aSAlexander Pyhalov 32 *91e1e26aSAlexander Pyhalov #define MAGIC_NUMBER (0x216513) 33 *91e1e26aSAlexander Pyhalov #define ERR_RETURN (-1) /* result code on error */ 34 *91e1e26aSAlexander Pyhalov 35 *91e1e26aSAlexander Pyhalov #define GET(c) ((c) = *ip, ip++, ileft--) 36 *91e1e26aSAlexander Pyhalov #define PUT(c) (*op = (c), op++, oleft--) 37 *91e1e26aSAlexander Pyhalov #define UNGET() (ip--, ileft++) 38 *91e1e26aSAlexander Pyhalov 39 *91e1e26aSAlexander Pyhalov 40 *91e1e26aSAlexander Pyhalov /* 41 *91e1e26aSAlexander Pyhalov * Open; called from iconv_open() 42 *91e1e26aSAlexander Pyhalov */ 43 *91e1e26aSAlexander Pyhalov void * 44 *91e1e26aSAlexander Pyhalov _icv_open() 45 *91e1e26aSAlexander Pyhalov { 46 *91e1e26aSAlexander Pyhalov return ((void*)MAGIC_NUMBER); 47 *91e1e26aSAlexander Pyhalov } 48 *91e1e26aSAlexander Pyhalov 49 *91e1e26aSAlexander Pyhalov 50 *91e1e26aSAlexander Pyhalov /* 51 *91e1e26aSAlexander Pyhalov * Close; called from iconv_close 52 *91e1e26aSAlexander Pyhalov */ 53 *91e1e26aSAlexander Pyhalov void 54 *91e1e26aSAlexander Pyhalov _icv_close(int* cd) 55 *91e1e26aSAlexander Pyhalov { 56 *91e1e26aSAlexander Pyhalov if (!cd || cd != (int*)MAGIC_NUMBER) 57 *91e1e26aSAlexander Pyhalov errno = EBADF; 58 *91e1e26aSAlexander Pyhalov } 59 *91e1e26aSAlexander Pyhalov 60 *91e1e26aSAlexander Pyhalov 61 *91e1e26aSAlexander Pyhalov /* 62 *91e1e26aSAlexander Pyhalov * Actual conversion; called from iconv() 63 *91e1e26aSAlexander Pyhalov */ 64 *91e1e26aSAlexander Pyhalov size_t 65 *91e1e26aSAlexander Pyhalov _icv_iconv(int* cd, char **inbuf, size_t *inbytesleft, 66 *91e1e26aSAlexander Pyhalov char **outbuf, size_t *outbytesleft) 67 *91e1e26aSAlexander Pyhalov { 68 *91e1e26aSAlexander Pyhalov unsigned char *ip, ic, *op; 69 *91e1e26aSAlexander Pyhalov size_t ileft, oleft; 70 *91e1e26aSAlexander Pyhalov size_t retval = 0; 71 *91e1e26aSAlexander Pyhalov 72 *91e1e26aSAlexander Pyhalov if (!cd || cd != (int*)MAGIC_NUMBER) 73 *91e1e26aSAlexander Pyhalov { 74 *91e1e26aSAlexander Pyhalov errno = EBADF; 75 *91e1e26aSAlexander Pyhalov return((size_t)ERR_RETURN); 76 *91e1e26aSAlexander Pyhalov } 77 *91e1e26aSAlexander Pyhalov 78 *91e1e26aSAlexander Pyhalov if ((inbuf == 0) || (*inbuf == 0)) 79 *91e1e26aSAlexander Pyhalov return((size_t)0); 80 *91e1e26aSAlexander Pyhalov 81 *91e1e26aSAlexander Pyhalov ip = (unsigned char*)*inbuf; 82 *91e1e26aSAlexander Pyhalov op = (unsigned char *)*outbuf; 83 *91e1e26aSAlexander Pyhalov ileft = *inbytesleft; 84 *91e1e26aSAlexander Pyhalov oleft = *outbytesleft; 85 *91e1e26aSAlexander Pyhalov 86 *91e1e26aSAlexander Pyhalov /* 87 *91e1e26aSAlexander Pyhalov * Main loop; basically 1 loop per 1 input byte 88 *91e1e26aSAlexander Pyhalov */ 89 *91e1e26aSAlexander Pyhalov 90 *91e1e26aSAlexander Pyhalov while (ileft > 0) { 91 *91e1e26aSAlexander Pyhalov GET(ic); 92 *91e1e26aSAlexander Pyhalov if (oleft < 1) { 93 *91e1e26aSAlexander Pyhalov UNGET(); 94 *91e1e26aSAlexander Pyhalov errno = E2BIG; 95 *91e1e26aSAlexander Pyhalov retval = ERR_RETURN; 96 *91e1e26aSAlexander Pyhalov goto ret; 97 *91e1e26aSAlexander Pyhalov } 98 *91e1e26aSAlexander Pyhalov if (isascii(ic)) 99 *91e1e26aSAlexander Pyhalov PUT(ic); 100 *91e1e26aSAlexander Pyhalov else { 101 *91e1e26aSAlexander Pyhalov PUT('_'); 102 *91e1e26aSAlexander Pyhalov retval++; 103 *91e1e26aSAlexander Pyhalov } 104 *91e1e26aSAlexander Pyhalov } 105 *91e1e26aSAlexander Pyhalov 106 *91e1e26aSAlexander Pyhalov ret: 107 *91e1e26aSAlexander Pyhalov *inbuf = (char *)ip; 108 *91e1e26aSAlexander Pyhalov *inbytesleft = ileft; 109 *91e1e26aSAlexander Pyhalov *outbuf = (char *)op; 110 *91e1e26aSAlexander Pyhalov *outbytesleft = oleft; 111 *91e1e26aSAlexander Pyhalov 112 *91e1e26aSAlexander Pyhalov return (retval); 113 *91e1e26aSAlexander Pyhalov } 114