summaryrefslogtreecommitdiff
path: root/gcc/ada/s-wchjis.adb
blob: 6b4941c8d3fe5129c1a5338f6e31a02197a40ebf (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
------------------------------------------------------------------------------
--                                                                          --
--                         GNAT RUN-TIME COMPONENTS                         --
--                                                                          --
--                       S Y S T E M . W C H _ J I S                        --
--                                                                          --
--                                 B o d y                                  --
--                                                                          --
--          Copyright (C) 1992-2013, Free Software Foundation, Inc.         --
--                                                                          --
-- GNAT is free software;  you can  redistribute it  and/or modify it under --
-- terms of the  GNU General Public License as published  by the Free Soft- --
-- ware  Foundation;  either version 3,  or (at your option) any later ver- --
-- sion.  GNAT is distributed in the hope that it will be useful, but WITH- --
-- OUT ANY WARRANTY;  without even the  implied warranty of MERCHANTABILITY --
-- or FITNESS FOR A PARTICULAR PURPOSE.                                     --
--                                                                          --
-- As a special exception under Section 7 of GPL version 3, you are granted --
-- additional permissions described in the GCC Runtime Library Exception,   --
-- version 3.1, as published by the Free Software Foundation.               --
--                                                                          --
-- You should have received a copy of the GNU General Public License and    --
-- a copy of the GCC Runtime Library Exception along with this program;     --
-- see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see    --
-- <http://www.gnu.org/licenses/>.                                          --
--                                                                          --
-- GNAT was originally developed  by the GNAT team at  New York University. --
-- Extensive contributions were provided by Ada Core Technologies Inc.      --
--                                                                          --
------------------------------------------------------------------------------

pragma Compiler_Unit_Warning;

package body System.WCh_JIS is

   type Byte is mod 256;

   EUC_Hankaku_Kana : constant Byte := 16#8E#;
   --  Prefix byte in EUC for Hankaku Kana (small Katakana). Such characters
   --  in EUC are represented by a prefix byte followed by the code, which
   --  is in the upper half (the corresponding JIS internal code is in the
   --  range 16#0080# - 16#00FF#).

   function EUC_To_JIS (EUC1, EUC2 : Character) return Wide_Character is
      EUC1B  : constant Byte := Character'Pos (EUC1);
      EUC2B  : constant Byte := Character'Pos (EUC2);

   begin
      if EUC2B not in 16#A0# .. 16#FE# then
         raise Constraint_Error;
      end if;

      if EUC1B = EUC_Hankaku_Kana then
         return Wide_Character'Val (EUC2B);

      else
         if EUC1B not in 16#A0# .. 16#FE# then
            raise Constraint_Error;
         else
            return Wide_Character'Val
              (256 * Natural (EUC1B and 16#7F#) + Natural (EUC2B and 16#7F#));
         end if;
      end if;
   end EUC_To_JIS;

   ----------------
   -- JIS_To_EUC --
   ----------------

   procedure JIS_To_EUC
     (J    : Wide_Character;
      EUC1 : out Character;
      EUC2 : out Character)
   is
      JIS1 : constant Natural := Wide_Character'Pos (J) / 256;
      JIS2 : constant Natural := Wide_Character'Pos (J) rem 256;

   begin
      --  Special case of small Katakana

      if JIS1 = 0 then

         --  The value must be in the range 16#80# to 16#FF# so that the upper
         --  bit is set in both bytes.

         if JIS2 < 16#80# then
            raise Constraint_Error;
         end if;

         EUC1 := Character'Val (EUC_Hankaku_Kana);
         EUC2 := Character'Val (JIS2);

      --  The upper bit of both characters must be clear, or this is not
      --  a valid character for representation in EUC form.

      elsif JIS1 > 16#7F# or else JIS2 > 16#7F# then
         raise Constraint_Error;

      --  Result is just the two characters with upper bits set

      else
         EUC1 := Character'Val (JIS1 + 16#80#);
         EUC2 := Character'Val (JIS2 + 16#80#);
      end if;
   end JIS_To_EUC;

   ----------------------
   -- JIS_To_Shift_JIS --
   ----------------------

   procedure JIS_To_Shift_JIS
     (J   : Wide_Character;
      SJ1 : out Character;
      SJ2 : out Character)
   is
      JIS1 : Byte;
      JIS2 : Byte;

   begin
      --  The following is the required algorithm, it's hard to make any
      --  more intelligent comments. This was copied from a public domain
      --  C program called etos.c (author unknown).

      JIS1 := Byte (Natural (Wide_Character'Pos (J) / 256));
      JIS2 := Byte (Natural (Wide_Character'Pos (J) rem 256));

      if JIS1 > 16#5F# then
         JIS1 := JIS1 + 16#80#;
      end if;

      if (JIS1 mod 2) = 0 then
         SJ1 := Character'Val ((JIS1 - 16#30#) / 2 + 16#88#);
         SJ2 := Character'Val (JIS2 + 16#7E#);

      else
         if JIS2 >= 16#60# then
            JIS2 := JIS2 + 16#01#;
         end if;

         SJ1 := Character'Val ((JIS1 - 16#31#) / 2 + 16#89#);
         SJ2 := Character'Val (JIS2 + 16#1F#);
      end if;
   end JIS_To_Shift_JIS;

   ----------------------
   -- Shift_JIS_To_JIS --
   ----------------------

   function Shift_JIS_To_JIS (SJ1, SJ2 : Character) return Wide_Character is
      SJIS1 : Byte;
      SJIS2 : Byte;
      JIS1  : Byte;
      JIS2  : Byte;

   begin
      --  The following is the required algorithm, it's hard to make any
      --  more intelligent comments. This was copied from a public domain
      --  C program called stoj.c written by shige@csk.JUNET.

      SJIS1 := Character'Pos (SJ1);
      SJIS2 := Character'Pos (SJ2);

      if SJIS1 >= 16#E0# then
         SJIS1 := SJIS1 - 16#40#;
      end if;

      if SJIS2 >= 16#9F# then
         JIS1 := (SJIS1 - 16#88#) * 2 + 16#30#;
         JIS2 := SJIS2 - 16#7E#;

      else
         if SJIS2 >= 16#7F# then
            SJIS2 := SJIS2 - 16#01#;
         end if;

         JIS1 := (SJIS1 - 16#89#) * 2 + 16#31#;
         JIS2 := SJIS2 - 16#1F#;
      end if;

      if JIS1 not in 16#20# .. 16#7E#
        or else JIS2 not in 16#20# .. 16#7E#
      then
         raise Constraint_Error;
      else
         return Wide_Character'Val (256 * Natural (JIS1) + Natural (JIS2));
      end if;
   end Shift_JIS_To_JIS;

end System.WCh_JIS;