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_TCVN__ 30 #include <unicode_tcvn.h> /* Unicode to TCVN 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 --> TCVN5712 ==========\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; /* Rreset internal errno */ 93 errno = 0; /* Rreset external errno */ 94 95 /* Convert UCS-2 encoding to TCVN5712 */ 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 c2 = **inbuf; 109 (*inbuf)++; 110 (*inbytesleft) -= 1; 111 112 #if defined(UCS_2LE) 113 uni |= (unsigned long)c1; 114 uni |= (unsigned long)c2<< 8; 115 #else 116 uni |= (unsigned long)c1<< 8; 117 uni |= (unsigned long)c2; 118 #endif 119 if ( *inbytesleft > 0 && *outbytesleft <= 0 ) { 120 errno = E2BIG; 121 (*inbuf) -= 2; 122 (*inbytesleft) +=2; 123 return ((size_t)-1); 124 } 125 126 if (uni_2_tcvn(uni, &ch) == 1) { 127 **outbuf = ch; 128 } else { 129 **outbuf = NON_ID_CHAR; 130 no_id_char_num += 1; 131 } 132 (*outbuf) += 1; 133 (*outbytesleft) -= 1; 134 135 } 136 137 return ((size_t)no_id_char_num); 138 } 139