annotate src/solaris/npt/utf_md.c @ 682:4e51997582ef

Merge
author tbell
date Thu, 23 Oct 2008 21:55:13 -0700
parents
children 00cd9dc3c2b5
rev   line source
duke@0 1 /*
duke@0 2 * Copyright 2004-2005 Sun Microsystems, Inc. All Rights Reserved.
duke@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
duke@0 4 *
duke@0 5 * This code is free software; you can redistribute it and/or modify it
duke@0 6 * under the terms of the GNU General Public License version 2 only, as
duke@0 7 * published by the Free Software Foundation. Sun designates this
duke@0 8 * particular file as subject to the "Classpath" exception as provided
duke@0 9 * by Sun in the LICENSE file that accompanied this code.
duke@0 10 *
duke@0 11 * This code is distributed in the hope that it will be useful, but WITHOUT
duke@0 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
duke@0 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
duke@0 14 * version 2 for more details (a copy is included in the LICENSE file that
duke@0 15 * accompanied this code).
duke@0 16 *
duke@0 17 * You should have received a copy of the GNU General Public License version
duke@0 18 * 2 along with this work; if not, write to the Free Software Foundation,
duke@0 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
duke@0 20 *
duke@0 21 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
duke@0 22 * CA 95054 USA or visit www.sun.com if you need additional information or
duke@0 23 * have any questions.
duke@0 24 */
duke@0 25
duke@0 26 #include <stdio.h>
duke@0 27 #include <stdlib.h>
duke@0 28 #include <string.h>
duke@0 29 #include <locale.h>
duke@0 30 #include <langinfo.h>
duke@0 31 #include <iconv.h>
duke@0 32
duke@0 33 #include "utf.h"
duke@0 34
duke@0 35 /* Global variables */
duke@0 36
duke@0 37 /*
duke@0 38 * Initialize all utf processing.
duke@0 39 */
duke@0 40 struct UtfInst *JNICALL
duke@0 41 utfInitialize(char *options)
duke@0 42 {
duke@0 43 struct UtfInst *ui;
duke@0 44 char *codeset;
duke@0 45
duke@0 46 ui = (struct UtfInst*)calloc(sizeof(struct UtfInst), 1);
duke@0 47 ui->iconvToPlatform = (void *)-1;
duke@0 48 ui->iconvFromPlatform = (void *)-1;
duke@0 49
duke@0 50 /* Set the locale from the environment */
duke@0 51 (void)setlocale(LC_ALL, "");
duke@0 52
duke@0 53 /* Get the codeset name */
duke@0 54 codeset = (char*)nl_langinfo(CODESET);
duke@0 55 if ( codeset == NULL || codeset[0] == 0 ) {
duke@0 56 return ui;
duke@0 57 }
duke@0 58
duke@0 59 /* If we don't need this, skip it */
duke@0 60 if (strcmp(codeset, "UTF-8") == 0 || strcmp(codeset, "utf8") == 0 ) {
duke@0 61 return ui;
duke@0 62 }
duke@0 63
duke@0 64 /* Open conversion descriptors */
duke@0 65 ui->iconvToPlatform = iconv_open(codeset, "UTF-8");
duke@0 66 if ( ui->iconvToPlatform == (void *)-1 ) {
duke@0 67 UTF_ERROR("Failed to complete iconv_open() setup");
duke@0 68 }
duke@0 69 ui->iconvFromPlatform = iconv_open("UTF-8", codeset);
duke@0 70 if ( ui->iconvFromPlatform == (void *)-1 ) {
duke@0 71 UTF_ERROR("Failed to complete iconv_open() setup");
duke@0 72 }
duke@0 73 return ui;
duke@0 74 }
duke@0 75
duke@0 76 /*
duke@0 77 * Terminate all utf processing
duke@0 78 */
duke@0 79 void JNICALL
duke@0 80 utfTerminate(struct UtfInst *ui, char *options)
duke@0 81 {
duke@0 82 if ( ui->iconvFromPlatform != (void *)-1 ) {
duke@0 83 (void)iconv_close(ui->iconvFromPlatform);
duke@0 84 }
duke@0 85 if ( ui->iconvToPlatform != (void *)-1 ) {
duke@0 86 (void)iconv_close(ui->iconvToPlatform);
duke@0 87 }
duke@0 88 ui->iconvToPlatform = (void *)-1;
duke@0 89 ui->iconvFromPlatform = (void *)-1;
duke@0 90 (void)free(ui);
duke@0 91 }
duke@0 92
duke@0 93 /*
duke@0 94 * Do iconv() conversion.
duke@0 95 * Returns length or -1 if output overflows.
duke@0 96 */
duke@0 97 static int
duke@0 98 iconvConvert(iconv_t ic, char *bytes, int len, char *output, int outputMaxLen)
duke@0 99 {
duke@0 100 int outputLen = 0;
duke@0 101
duke@0 102 UTF_ASSERT(bytes);
duke@0 103 UTF_ASSERT(len>=0);
duke@0 104 UTF_ASSERT(output);
duke@0 105 UTF_ASSERT(outputMaxLen>len);
duke@0 106
duke@0 107 output[0] = 0;
duke@0 108 outputLen = 0;
duke@0 109
duke@0 110 if ( ic != (iconv_t)(void *)-1 ) {
duke@0 111 int returnValue;
duke@0 112 size_t inLeft;
duke@0 113 size_t outLeft;
duke@0 114 char *inbuf;
duke@0 115 char *outbuf;
duke@0 116
duke@0 117 inbuf = bytes;
duke@0 118 outbuf = output;
duke@0 119 inLeft = len;
duke@0 120 outLeft = outputMaxLen;
duke@0 121 returnValue = iconv(ic, (void*)&inbuf, &inLeft, &outbuf, &outLeft);
duke@0 122 if ( returnValue >= 0 && inLeft==0 ) {
duke@0 123 outputLen = outputMaxLen-outLeft;
duke@0 124 output[outputLen] = 0;
duke@0 125 return outputLen;
duke@0 126 }
duke@0 127
duke@0 128 /* Failed to do the conversion */
duke@0 129 return -1;
duke@0 130 }
duke@0 131
duke@0 132 /* Just copy bytes */
duke@0 133 outputLen = len;
duke@0 134 (void)memcpy(output, bytes, len);
duke@0 135 output[len] = 0;
duke@0 136 return outputLen;
duke@0 137 }
duke@0 138
duke@0 139 /*
duke@0 140 * Convert UTF-8 to Platform Encoding.
duke@0 141 * Returns length or -1 if output overflows.
duke@0 142 */
duke@0 143 int JNICALL
duke@0 144 utf8ToPlatform(struct UtfInst*ui, jbyte *utf8, int len, char *output, int outputMaxLen)
duke@0 145 {
duke@0 146 /* Negative length is an error */
duke@0 147 if ( len < 0 ) {
duke@0 148 return -1;
duke@0 149 }
duke@0 150
duke@0 151 /* Zero length is ok, but we don't need to do much */
duke@0 152 if ( len == 0 ) {
duke@0 153 output[0] = 0;
duke@0 154 return 0;
duke@0 155 }
duke@0 156
duke@0 157 return iconvConvert(ui->iconvToPlatform, (char*)utf8, len, output, outputMaxLen);
duke@0 158 }
duke@0 159
duke@0 160 /*
duke@0 161 * Convert Platform Encoding to UTF-8.
duke@0 162 * Returns length or -1 if output overflows.
duke@0 163 */
duke@0 164 int JNICALL
duke@0 165 utf8FromPlatform(struct UtfInst*ui, char *str, int len, jbyte *output, int outputMaxLen)
duke@0 166 {
duke@0 167 /* Negative length is an error */
duke@0 168 if ( len < 0 ) {
duke@0 169 return -1;
duke@0 170 }
duke@0 171
duke@0 172 /* Zero length is ok, but we don't need to do much */
duke@0 173 if ( len == 0 ) {
duke@0 174 output[0] = 0;
duke@0 175 return 0;
duke@0 176 }
duke@0 177
duke@0 178 return iconvConvert(ui->iconvFromPlatform, str, len, (char*)output, outputMaxLen);
duke@0 179 }