summaryrefslogtreecommitdiff
path: root/src/xutf8/utf8Input.c
blob: 1809e30521ef421867d484a78853e4196f9cd8ac (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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
/* "$Id: $"
 *
 * Author: Jean-Marc Lienher ( http://oksid.ch )
 * Copyright 2000-2003 by O'ksi'D.
 *
 * This library is free software. Distribution and use rights are outlined in
 * the file "COPYING" which should have been included with this file.  If this
 * file is missing or damaged, see the license at:
 *
 *     http://www.fltk.org/COPYING.php
 *
 * Please report all bugs and problems on the following page:
 *
 *     http://www.fltk.org/str.php
 */

#if !defined(WIN32) && !defined(__APPLE__)

#include <config.h>
#include "../../FL/Xutf8.h"
#include <X11/X.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <string.h>
#include <stdlib.h>

#if HAVE_LIBC_ICONV
#include <iconv.h>
#endif
/*
  I haven't found much doc on the web about EUC encodings, so I've used
  GNU libiconv source code as a reference.
  http://clisp.cons.org/~haible/packages-libiconv.html
*/

#define RET_ILSEQ -1
#define RET_TOOFEW(x) (-10 - x)
#define RET_TOOSMALL -2
#define conv_t void*
#define ucs4_t unsigned int
typedef struct {
  unsigned short indx;
  unsigned short used;
} Summary16;

#define NEED_TOWC /* indicates what part of these include files is needed here (avoid compilation warnings) */
#include "lcUniConv/big5.h"
#include "lcUniConv/gb2312.h"
#include "lcUniConv/cp936ext.h"
#include "lcUniConv/jisx0201.h"
#include "lcUniConv/jisx0208.h"
#include "lcUniConv/jisx0212.h"
#include "lcUniConv/ksc5601.h"

int 
XConvertEucTwToUtf8(char* buffer_return, int len) {
  /* FIXME */
#if HAVE_LIBC_ICONV
  iconv_t cd;
  int cdl;
#else
  int i = 0;
#endif
  int l = 0;
  char *buf; // , *b;

  if (len < 1) return 0;
  /*b = */ buf = (char*) malloc((unsigned)len);
  memcpy(buf, buffer_return, (unsigned) len);

#if HAVE_LIBC_ICONV
  l = cdl = len;
  cd = iconv_open("EUC-TW", "UTF-8");
  iconv(cd, &b, &len, &buffer_return, &cdl);
  iconv_close(cd);
  l -= cdl;	
#else
  while (i < len) {
    unsigned int ucs;
    unsigned char c; 
    c = (unsigned char) buf[i];
    if (c < 0x80) {
      ucs = c;	
      i++;
    } else if (c >= 0xa1 && c < 0xff && len - i > 1 ) {

#if 0 
      unsigned char b[2];
      b[0] = (unsigned char) c - 0x80;
      b[1] = (unsigned char) buf[i + 1] - 0x80;
#endif
      ucs = ' '; i += 2;
    } else if (c == 0x8e &&  len - i > 3) {
      unsigned char c1 =  buf[i + 1];
      unsigned char c2 =  buf[i + 2];
      unsigned char c3 =  buf[i + 3];
#if 0
      unsigned char b[2];
      b[0] = (unsigned char)  buf[i + 2] - 0x80;
      b[1] = (unsigned char)  buf[i + 3] - 0x80;
#endif
      if (c1 >= 0xa1 && c1 <= 0xb0) {
	if (c2 >= 0xa1 && c2 < 0xff && c3 >= 0xa1 && c3 < 0xff) {
	  ucs = ' '; i += 4;
	} else {
	  ucs = '?'; i++;
	}
      } else {
	ucs = '?'; i++;
      }
    } else {
      ucs = '?';
      i++;
    }
    l += XConvertUcsToUtf8(ucs, buffer_return + l);
  }
#endif
  free(buf);
  return l;
}

int 
XConvertEucKrToUtf8(char* buffer_return, int len) {
  int i = 0, l = 0;
  char *buf;

  if (len < 1) return 0;

  buf = (char*) malloc((unsigned)len);
  memcpy(buf, buffer_return, (unsigned)len);

  while (i < len) {
    unsigned int ucs;
    unsigned char c, c1;
    c = (unsigned char) buf[i];
    if (c < 0x80) {
      ucs = c;	
      i++;
    } else if (c >= 0xA1 && c < 0xFF && len - i > 1) {
      c1 = (unsigned char) buf[i + 1];
      if (c1 >= 0xa1 && c1 < 0xff) {
	unsigned char b[2];
	b[0] = c - 0x80;
	b[1] = c1 - 0x80;
	if (ksc5601_mbtowc(NULL, &ucs, b, 2) < 1) {
	  ucs = '?';
	}
      } else {
	ucs = '?';
      }
      i += 2;
    } else {
      ucs = '?';
      i++;
    }
    l += XConvertUcsToUtf8(ucs, buffer_return + l);
  }
  free(buf);
  return l;
}

int 
XConvertBig5ToUtf8(char* buffer_return, int len) {
  int i = 0, l = 0;
  char *buf;

  if (len < 1) return 0;
  buf = (char*) malloc((unsigned)len);
  memcpy(buf, buffer_return, (unsigned)len);

  if (len == 1) {
    l += XConvertUcsToUtf8((unsigned int)buf[i], buffer_return + l);
  }
  while (i + 1 < len) {
    unsigned int ucs;
    unsigned char b[2];
    b[0] = (unsigned char) buf[i];
    b[1] = (unsigned char) buf[i + 1];
    if (big5_mbtowc(NULL, &ucs, b, 2) == 2) {
      i += 2;
    } else {
      ucs = '?';
      i++;
    }
    l += XConvertUcsToUtf8(ucs, buffer_return + l);
  }
  free(buf);
  return l;
}

int 
XConvertCp936extToUtf8(char* buffer_return, int len)
{
  int i = 0, l = 0;
  char *buf;

  if (len < 1) return 0;
  buf = (char*) malloc((unsigned)len);
  memcpy(buf, buffer_return, (unsigned)len);

  if (len == 1) {
	  l += XConvertUcsToUtf8((unsigned int)buf[i], buffer_return + l);
  }
  while (i + 1 < len) {
	  unsigned int ucs;
	  unsigned char b[2];
	  b[0] = (unsigned char) buf[i];
	  b[1] = (unsigned char) buf[i + 1];
	  if (cp936ext_mbtowc(NULL, &ucs, b, 2) == 2) {
		  i += 2;
	  } else {
	      if ( b[0] < 0x80) {
		    ucs = b[0];
		}else{
			      ucs = '?';
		  }
			  i++;
		  }
	  l += XConvertUcsToUtf8(ucs, buffer_return + l);
  }
  if(i + 1 == len) { 
      l += XConvertUcsToUtf8((unsigned int)buf[i], buffer_return + l);
  }
  free(buf);
  return l;
}

int 
XConvertGb2312ToUtf8(char* buffer_return, int len) {
  int i = 0, l = 0;
  char *buf;

  if (len < 1) return 0;
  buf = (char*) malloc((unsigned)len);
  memcpy(buf, buffer_return, (unsigned)len);

  if (len == 1) {
    l += XConvertUcsToUtf8((unsigned int)buf[i], buffer_return + l);
  }
  while (i + 1 < len) {
    unsigned int ucs;
    unsigned char b[2];
    b[0] = (unsigned char) buf[i];
    b[1] = (unsigned char) buf[i + 1];
    if ( b[0] < 0x80 ) {
      ucs = b[0];
      i++;
    } else if (gb2312_mbtowc(NULL, &ucs, b, 2) == 2) {
      i += 2;
    } else {
      ucs = '?';
      i++;
    }
    l += XConvertUcsToUtf8(ucs, buffer_return + l);
  }
  if (i + 1 == len) {
    l += XConvertUcsToUtf8((unsigned int)buf[i], buffer_return + l);
  }
  free(buf);
  return l;
}

int 
XConvertEucCnToUtf8(char* buffer_return, int len) {
  int i = 0, l = 0;
  char *buf;

  if (len < 1) return 0;
  buf = (char*) malloc((unsigned)len);
  memcpy(buf, buffer_return, (unsigned)len);

  while (i < len) {
    unsigned int ucs;
    unsigned char c, c1;
    c = (unsigned char) buf[i];
    if (c < 0x80) {
      ucs = c;	
      i++;
    } else if (c >= 0xA1 && c < 0xFF && len - i > 1) {
      c1 = (unsigned char) buf[i + 1];
      if (c1 >= 0xa1 && c1 < 0xff) {	
	unsigned char b[2];
	b[0] = (unsigned char) c;
	b[1] = (unsigned char) c1;
	if (gb2312_mbtowc(NULL, &ucs, b, 2) < 1) {
	  ucs = '?';
	}	
      } else {
	ucs = '?';
      }
      i += 2;
    } else {
      ucs = '?';
      i++;
    }
    l += XConvertUcsToUtf8(ucs, buffer_return + l);
  }
  free(buf);
  return l;
}

int 
XConvertEucJpToUtf8(char* buffer_return, int len) {
  int i = 0, l = 0;
  char *buf;

  if (len < 1) return 0;
  buf = (char*) malloc((unsigned)len);
  memcpy(buf, buffer_return, (unsigned)len);

  while (i < len) {
    unsigned int ucs;
    unsigned char c, c1;
    c = (unsigned char) buf[i];
    if (c < 0x80) {
      ucs = c;	
      i++;
    } else if (c >= 0xA1 && c < 0xFF && len - i > 1) {
      c1 = (unsigned char) buf[i + 1];		
      if (c < 0xF5 && c1 >= 0xa1) {
	unsigned char b[2];
	b[0] = c - 0x80;
	b[1] = c1 - 0x80;
	if (jisx0208_mbtowc(NULL, &ucs, b, 2) < 1) { 
	  ucs = '?';
	}
      } else if (c1 >= 0xA1 && c1 < 0xFF) {
	ucs = 0xE000 + 94 * (c - 0xF5) + (c1 - 0xA1);
      } else {
	ucs = '?';
      }
      i += 2;
    } else if (c == 0x8E && len - i > 1) {
      c1 = (unsigned char) buf[i + 1];		
      if (c1 >= 0xa1 && c1 <= 0xe0) {
	if (jisx0201_mbtowc(NULL, &ucs, &c1, 1) != 1) {
	  ucs = '?';
	}
      } else {
	ucs = '?';
      }
      i += 2;
    } else if (c == 0x8F && len - i > 2) {
      c = (unsigned char) buf[i + 1];		
      c1 = (unsigned char) buf[i + 2];	
      if (c >= 0xa1 && c < 0xff) {
	if (c < 0xf5 && c1 >= 0xa1 && c1 < 0xff) {
	  unsigned char b[2];
	  b[0] = c - 0x80;
	  b[1] = c1 - 0x80;
	  if (jisx0212_mbtowc(NULL, &ucs, b, 2) < 1) {
	    ucs = '?';
	  }
	} else {
	  ucs = '?';
	}
      } else {
	if (c1 >= 0xa1 && c1 < 0xff) {
	  ucs = 0xe3ac + 94 * (c - 0xF5) + (c1 - 0xA1);
	} else {
	  ucs = '?';
	}
      }
      i += 3;
    } else {
      ucs = '?';
      i++;
    }
    l += XConvertUcsToUtf8(ucs, buffer_return + l);
  }
  free(buf);
  return l;
}

int
XConvertEucToUtf8(const char*	locale,
		  char*		buffer_return, 
		  int		len, 
		  int		bytes_buffer) {

  /* if (!locale) { */
  /* if (!locale || strstr(locale, "UTF") || strstr(locale, "utf")) { */
  if (!locale || strstr(locale, "UTF") || strstr(locale, "utf")) {
    return len;
  }

  if (strstr(locale, "ja")) {	
    return XConvertEucJpToUtf8(buffer_return, len);
  } else if (strstr(locale, "Big5") || strstr(locale, "big5")) { /* BIG5 */
    return XConvertBig5ToUtf8(buffer_return, len);
  } else if (strstr(locale, "GBK") || strstr(locale, "gbk")) {
    return XConvertCp936extToUtf8(buffer_return, len);
  } else if (strstr(locale, "zh") || strstr(locale, "chinese-")) {
    if (strstr(locale, "TW") || strstr(locale, "chinese-t")) {
      if (strstr(locale, "EUC") || strstr(locale, "euc") || strstr(locale, "chinese-t")) {
	return XConvertEucTwToUtf8(buffer_return, len);
      }
      return XConvertBig5ToUtf8(buffer_return, len);
    }
    if (strstr(locale, "EUC") || strstr(locale, "euc")) {
      return XConvertEucCnToUtf8(buffer_return, len);
    }
    return XConvertGb2312ToUtf8(buffer_return, len);
  } else if (strstr(locale, "ko")) { 
    return XConvertEucKrToUtf8(buffer_return, len);
  }
  return len;
}

int
XUtf8LookupString(XIC                 ic,
		  XKeyPressedEvent*   event,
		  char*               buffer_return,
		  int                 bytes_buffer,
		  KeySym*             keysym,
		  Status*             status_return) {

  long ucs = -1;
  int len;
  len = XmbLookupString(ic, event, buffer_return, bytes_buffer / 5,
		        keysym, status_return);
  if (*status_return == XBufferOverflow) {
    return len * 5;
  }
  if (*keysym > 0 && *keysym < 0x100 && len == 1) {
    if (*keysym < 0x80) {
      ucs = (unsigned char)buffer_return[0];
    } else {
      ucs = *keysym;
    }
  } else  if (((*keysym >= 0x100 && *keysym <= 0xf000) ||
	      (*keysym & 0xff000000U) == 0x01000000))
  {
    ucs = XKeysymToUcs(*keysym);
  } else {
    ucs = -2;
  }

  if (ucs > 0) {
    len = XConvertUcsToUtf8((unsigned)ucs, (char *)buffer_return);
  } else if (len > 0) {
    XIM im;
    if (!ic) return 0;
    im = XIMOfIC(ic);
    if (!im) return 0;
    len = XConvertEucToUtf8(XLocaleOfIM(im), buffer_return, len, bytes_buffer);	
  }
  return len;
}

#endif /* X11 only */

/*
 * End of "$Id$".
 */