OSDN Git Service

* All files: Updated copyright to reflect Cygnus purchase.
[pf3gnuchains/gcc-fork.git] / libjava / gnu / gcj / convert / natInput_SJIS.cc
1 /* Copyright (C) 1999  Red Hat, Inc.
2
3    This file is part of libgcj.
4
5 This software is copyrighted work licensed under the terms of the
6 Libgcj License.  Please consult the file "LIBGCJ_LICENSE" for
7 details.  */
8
9 #include <config.h>
10 #include <gcj/cni.h>
11 #include <gnu/gcj/convert/Input_SJIS.h>
12
13 #define ERROR_CHAR 0xFFFD
14
15 extern unsigned short JIS0208_to_Unicode[84][94];
16 extern unsigned short JIS0212_to_Unicode[76][94];
17
18 jint
19 gnu::gcj::convert::Input_SJIS::read(jcharArray outbuffer, jint outpos,
20                                     jint count)
21 {
22   jint start_outpos = outpos;
23   for (;;)
24     {
25       if (outpos - start_outpos >= count)
26         break;
27       if (inpos >= inlength)
28         break;
29       int b = ((unsigned char*) elements(inbuffer))[inpos++];
30       if (first_byte == 0)
31         {
32           if (b < 128)
33             {
34 #if 1
35               // Technically, we should translate 0x5c to Yen symbol;
36               // in practice, it is not clear.
37               if (b == 0x5c)
38                 b = 0x00A5;  // Yen sign.
39 #endif
40               elements(outbuffer)[outpos++] = (char) b;
41             }
42           else if (b >= 0xA1 && b <= 0xDF)
43             {
44               b += 0xFF61 - 0xA1;
45               elements(outbuffer)[outpos++] = b;
46             }
47           else
48             first_byte = b;
49         }
50       else
51         {
52           // From Lunde: "CJKV Informatio Processing", O'Reilly, 1999, p 420:
53           bool adjust = b < 159;
54           int rowOffset = first_byte < 160 ? 112 : 176;
55           int cellOffset = adjust ? (b > 127 ? 32 : 31) : 126;
56           first_byte = ((first_byte - rowOffset) << 1) - adjust;
57           b -= cellOffset;
58
59           first_byte -= 33;
60           b -= 33;
61
62           if ((unsigned) first_byte >= 84 || (unsigned) b >= 94)
63             b = ERROR_CHAR;
64           else
65             {
66               b = JIS0208_to_Unicode[first_byte][b];
67               if (b == 0)
68                 b = ERROR_CHAR;
69             }
70           elements(outbuffer)[outpos++] = b;
71
72           first_byte = 0;
73         }
74     }
75   return outpos - start_outpos;
76 }