summaryrefslogtreecommitdiff
path: root/libjava/gnu/gcj/convert/natIconv.cc
blob: ad75a505ff847e55545bc494665f1a2ecd5e8a4b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
// natIconv.cc -- Java side of iconv() reader.

/* Copyright (C) 2000, 2001, 2003, 2006  Free Software Foundation

   This file is part of libgcj.

This software is copyrighted work licensed under the terms of the
Libgcj License.  Please consult the file "LIBGCJ_LICENSE" for
details.  */

/* Author: Tom Tromey <tromey@redhat.com>.  */

#include <config.h>

#include <gcj/cni.h>
#include <jvm.h>

#include <gnu/gcj/convert/Input_iconv.h>
#include <gnu/gcj/convert/Output_iconv.h>
#include <java/io/CharConversionException.h>
#include <java/io/UnsupportedEncodingException.h>

#include <errno.h>

#ifdef HAVE_ICONV
#include <iconv.h>

template<typename T>
static inline size_t
iconv_adapter (size_t (*iconv_f) (iconv_t, T, size_t *, char **, size_t *),
	       iconv_t handle, char **inbuf, size_t *inavail,
	       char **outbuf, size_t *outavail)
{
  return (*iconv_f) (handle, (T) inbuf, inavail, outbuf, outavail);
}

#endif

void
gnu::gcj::convert::Input_iconv::init (jstring encoding)
{
#ifdef HAVE_ICONV
  jsize len = _Jv_GetStringUTFLength (encoding);
  char buffer[len + 1];
  _Jv_GetStringUTFRegion (encoding, 0, encoding->length(), buffer);
  buffer[len] = '\0';

  iconv_t h = iconv_open ("UCS-2", buffer);
  if (h == (iconv_t) -1)
    throw new ::java::io::UnsupportedEncodingException (encoding);

  JvAssert (h != NULL);
  handle = reinterpret_cast<gnu::gcj::RawData *> (h);
#else /* HAVE_ICONV */
  // If no iconv, just throw an exception.
  throw new ::java::io::UnsupportedEncodingException (encoding);
#endif /* HAVE_ICONV */
}

void
gnu::gcj::convert::Input_iconv::finalize (void)
{
#ifdef HAVE_ICONV
  if (handle != NULL)
    {
      iconv_close ((iconv_t) handle);
      handle = NULL;
    }
#endif /* HAVE_ICONV */
}

jint
gnu::gcj::convert::Input_iconv::read (jcharArray outbuffer,
				      jint outpos, jint count)
{
#ifdef HAVE_ICONV
  jbyte *bytes = elements (inbuffer);
  jchar *out = elements (outbuffer);
  size_t inavail = inlength - inpos;
  size_t old_in = inavail;
  size_t outavail = count * sizeof (jchar);
  size_t old_out = outavail;

  char *inbuf = (char *) &bytes[inpos];
  char *outbuf = (char *) &out[outpos];

  size_t r = iconv_adapter (iconv, (iconv_t) handle,
			    &inbuf, &inavail,
			    &outbuf, &outavail);

  if (r == (size_t) -1)
    {
      // If we see EINVAL then there is an incomplete sequence at the
      // end of the input buffer.  If we see E2BIG then we ran out of
      // space in the output buffer.  However, in both these cases
      // some conversion might have taken place.  So we fall through
      // to the normal case.
      if (errno != EINVAL && errno != E2BIG)
	throw new ::java::io::CharConversionException ();
    }

  if (iconv_byte_swap)
    {
      size_t max = (old_out - outavail) / sizeof (jchar);
      for (size_t i = 0; i < max; ++i)
	{
	  // Byte swap.
	  jchar c = (((out[outpos + i] & 0xff) << 8)
		     | ((out[outpos + i] >> 8) & 0xff));
	  outbuf[i] = c;
	}
    }

  inpos += old_in - inavail;
  return (old_out - outavail) / sizeof (jchar);
#else /* HAVE_ICONV */
  return -1;
#endif /* HAVE_ICONV */
}

void
gnu::gcj::convert::Input_iconv::done ()
{
#ifdef HAVE_ICONV
  // 50 bytes should be enough for any reset sequence.
  size_t avail = 50;
  char tmp[avail];
  char *p = tmp;
  // Calling iconv() with a NULL INBUF pointer will cause iconv() to
  // switch to its initial state.  We don't care about the output that
  // might be generated in that situation.
  iconv_adapter (iconv, (iconv_t) handle, NULL, NULL, &p, &avail);
  BytesToUnicode::done ();
#else /* HAVE_ICONV */
  // If no iconv, do nothing
#endif /* HAVE_ICONV */
}

void
gnu::gcj::convert::Output_iconv::init (jstring encoding)
{
#ifdef HAVE_ICONV
  jsize len = _Jv_GetStringUTFLength (encoding);
  char buffer[len + 1];
  _Jv_GetStringUTFRegion (encoding, 0, encoding->length(), buffer);
  buffer[len] = '\0';

  iconv_t h = iconv_open (buffer, "UCS-2");
  if (h == (iconv_t) -1)
    throw new ::java::io::UnsupportedEncodingException (encoding);

  JvAssert (h != NULL);
  handle = reinterpret_cast<gnu::gcj::RawData *> (h);
#else /* HAVE_ICONV */
  // If no iconv, just throw an exception.
  throw new ::java::io::UnsupportedEncodingException (encoding);
#endif /* HAVE_ICONV */
}

void
gnu::gcj::convert::Output_iconv::finalize (void)
{
#ifdef HAVE_ICONV
  if (handle != NULL)
    {
      iconv_close ((iconv_t) handle);
      handle = NULL;
    }
#endif /* HAVE_ICONV */
}

jint
gnu::gcj::convert::Output_iconv::write (jcharArray inbuffer,
					jint inpos, jint inlength)
{
#ifdef HAVE_ICONV
  jchar *chars = elements (inbuffer);
  jbyte *out = elements (buf);
  jchar *temp_buffer = NULL;

  size_t inavail = inlength * sizeof (jchar);
  size_t old_in = inavail;

  size_t outavail = buf->length - count;
  size_t old_out = outavail;

  char *inbuf = (char *) &chars[inpos];
  char *outbuf = (char *) &out[count];

  if (iconv_byte_swap)
    {
      // Ugly performance penalty -- don't use losing systems!
      temp_buffer = (jchar *) _Jv_Malloc (inlength * sizeof (jchar));
      for (int i = 0; i < inlength; ++i)
	{
	  // Byte swap.
	  jchar c = (((chars[inpos + i] & 0xff) << 8)
		     | ((chars[inpos + i] >> 8) & 0xff));
	  temp_buffer[i] = c;
	}
      inbuf = (char *) temp_buffer;
    }

  size_t loop_old_in = old_in;
  while (1)
    {
      size_t r = iconv_adapter (iconv, (iconv_t) handle,
				&inbuf, &inavail,
				&outbuf, &outavail);
      if (r == (size_t) -1)
	{
	  if (errno == EINVAL)
	    {
	      // Incomplete byte sequence at the end of the input
	      // buffer.  This shouldn't be able to happen here.
	      break;
	    }
	  else if (errno == E2BIG)
	    {
	      // Output buffer is too small.
	      break;
	    }
	  else if (errno == EILSEQ || inavail == loop_old_in)
	    {
	      // Untranslatable sequence.  Since glibc 2.1.3 doesn't
	      // properly set errno, we also assume that this is what
	      // is happening if no conversions took place.  (This can
	      // be a bogus assumption if in fact the output buffer is
	      // too small.)  We skip the first character and try
	      // again.
	      inavail -= 2;
	      if (inavail == 0)
		break;
	      loop_old_in -= 2;
	      inbuf += 2;
	    }
	}
      else
	break;
    }

  if (temp_buffer != NULL)
    _Jv_Free (temp_buffer);

  count += old_out - outavail;
  return (old_in - inavail) / sizeof (jchar);
#else /* HAVE_ICONV */
  return -1;
#endif /* HAVE_ICONV */
}

jboolean
gnu::gcj::convert::IOConverter::iconv_init (void)
{
  // Some versions of iconv() always return their UCS-2 results in
  // big-endian order, and they also require UCS-2 inputs to be in
  // big-endian order.  For instance, glibc 2.1.3 does this.  If the
  // UTF-8=>UCS-2 iconv converter has this feature, then we assume
  // that all UCS-2 converters do.  (This might not be the best
  // heuristic, but is is all we've got.)
  jboolean result = false;
#ifdef HAVE_ICONV
  iconv_t handle = iconv_open ("UCS-2", "UTF-8");
  if (handle != (iconv_t) -1)
    {
      jchar c;
      unsigned char in[3];
      char *inp, *outp;
      size_t inc, outc, r;

      // This is the UTF-8 encoding of \ufeff.
      in[0] = 0xef;
      in[1] = 0xbb;
      in[2] = 0xbf;

      inp = (char *) in;
      inc = 3;
      outp = (char *) &c;
      outc = 2;

      r = iconv_adapter (iconv, handle, &inp, &inc, &outp, &outc);
      // Conversion must be complete for us to use the result.
      if (r != (size_t) -1 && inc == 0 && outc == 0)
	result = (c != 0xfeff);

      // Release iconv handle.
      iconv_close (handle);
    }
#endif /* HAVE_ICONV */
  return result;
}

void
gnu::gcj::convert::Output_iconv::done ()
{
#ifdef HAVE_ICONV
  // 50 bytes should be enough for any reset sequence.
  size_t avail = 50;
  char tmp[avail];
  char *p = tmp;
  // Calling iconv() with a NULL INBUF pointer will cause iconv() to
  // switch to its initial state.  We don't care about the output that
  // might be generated in that situation.
  iconv_adapter (iconv, (iconv_t) handle, NULL, NULL, &p, &avail);
  UnicodeToBytes::done ();
#else /* HAVE_ICONV */
  // If no iconv, do nothing
#endif /* HAVE_ICONV */
}