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) 2008, by Sun Microsystems, Inc. 23 * All rights reserved. 24 */ 25 #include <stdio.h> 26 #include <errno.h> 27 #include <stdlib.h> 28 #include <sys/types.h> 29 #define __NEED_UNI_2_VISCII__ 30 #include <unicode_viscii.h> /* Unicode to VISCII mapping table */ 31 #include "common_defs.h" 32 33 #define NON_ID_CHAR '?' /* non-identified character */ 34 35 typedef struct _icv_state { 36 int _errno; /* internal errno */ 37 } _iconv_st; 38 39 40 /* 41 * Open; called from iconv_open() 42 */ 43 void * 44 _icv_open() 45 { 46 _iconv_st *st; 47 48 if ((st = (_iconv_st *)malloc(sizeof(_iconv_st))) == NULL) { 49 errno = ENOMEM; 50 return ((void *) -1); 51 } 52 53 st->_errno = 0; 54 return ((void *) st); 55 } 56 57 58 /* 59 * Close; called from iconv_close() 60 */ 61 void 62 _icv_close(_iconv_st *st) 63 { 64 if (!st) 65 errno = EBADF; 66 else 67 free(st); 68 } 69 70 71 /* 72 * Actual conversion; called from iconv() 73 */ 74 size_t 75 _icv_iconv(_iconv_st *st, char **inbuf, size_t *inbytesleft, 76 char **outbuf, size_t *outbytesleft) 77 { 78 int no_id_char_num = 0; 79 #ifdef DEBUG 80 fprintf(stderr, "========== iconv(): UCS-2 --> viscii ==========\n"); 81 #endif 82 if (st == NULL) { 83 errno = EBADF; 84 return ((size_t) -1); 85 } 86 87 if (inbuf == NULL || *inbuf == NULL) { /* Reset request. */ 88 st->_errno = 0; 89 return ((size_t) 0); 90 } 91 92 st->_errno = 0; /* reset internal errno */ 93 errno = 0; /* reset external errno */ 94 95 /* convert UCS-2 encoding to viscii */ 96 while (*inbytesleft > 0 && *outbytesleft > 0) { 97 unsigned long uni = 0; 98 unsigned char c1 = 0, c2 = 0; 99 unsigned char ch = 0; 100 101 c1 = **inbuf; 102 if (*inbytesleft <= 1) { 103 errno = EINVAL; 104 return ((size_t)-1); 105 } 106 (*inbuf)++; 107 (*inbytesleft) -= 1; 108 109 c2 = **inbuf; 110 (*inbuf)++; 111 (*inbytesleft) -= 1; 112 113 #if defined(UCS_2LE) 114 uni |= (unsigned long)c1; 115 uni |= (unsigned long)c2 << 8; 116 #else 117 uni |= (unsigned long)c1 << 8; 118 uni |= (unsigned long)c2; 119 #endif 120 if ( *inbytesleft > 0 && *outbytesleft <= 0 ) { 121 errno = E2BIG; 122 return ((size_t)-1); 123 } 124 125 if (uni_2_viscii(uni, &ch) == 1) { 126 **outbuf = ch; 127 } else { 128 **outbuf = NON_ID_CHAR; 129 no_id_char_num += 1; 130 } 131 (*outbuf) += 1; 132 (*outbytesleft) -= 1; 133 } 134 135 return ((size_t)no_id_char_num); 136 } 137