| 1 |
------------------------------------------------------------------------------ |
| 2 |
-- -- |
| 3 |
-- GNAT RUN-TIME COMPONENTS -- |
| 4 |
-- -- |
| 5 |
-- S Y S T E M . W C H _ J I S -- |
| 6 |
-- -- |
| 7 |
-- B o d y -- |
| 8 |
-- -- |
| 9 |
-- Copyright (C) 1992-2025, Free Software Foundation, Inc. -- |
| 10 |
-- -- |
| 11 |
-- GNAT is free software; you can redistribute it and/or modify it under -- |
| 12 |
-- terms of the GNU General Public License as published by the Free Soft- -- |
| 13 |
-- ware Foundation; either version 3, or (at your option) any later ver- -- |
| 14 |
-- sion. GNAT is distributed in the hope that it will be useful, but WITH- -- |
| 15 |
-- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY -- |
| 16 |
-- or FITNESS FOR A PARTICULAR PURPOSE. -- |
| 17 |
-- -- |
| 18 |
-- As a special exception under Section 7 of GPL version 3, you are granted -- |
| 19 |
-- additional permissions described in the GCC Runtime Library Exception, -- |
| 20 |
-- version 3.1, as published by the Free Software Foundation. -- |
| 21 |
-- -- |
| 22 |
-- You should have received a copy of the GNU General Public License and -- |
| 23 |
-- a copy of the GCC Runtime Library Exception along with this program; -- |
| 24 |
-- see the files COPYING3 and COPYING.RUNTIME respectively. If not, see -- |
| 25 |
-- <http://www.gnu.org/licenses/>. -- |
| 26 |
-- -- |
| 27 |
-- GNAT was originally developed by the GNAT team at New York University. -- |
| 28 |
-- Extensive contributions were provided by Ada Core Technologies Inc. -- |
| 29 |
-- -- |
| 30 |
------------------------------------------------------------------------------ |
| 31 |
|
| 32 |
package body System.WCh_JIS is |
| 33 |
|
| 34 |
type Byte is mod 256; |
| 35 |
|
| 36 |
EUC_Hankaku_Kana : constant Byte := 16#8E#; |
| 37 |
-- Prefix byte in EUC for Hankaku Kana (small Katakana). Such characters |
| 38 |
-- in EUC are represented by a prefix byte followed by the code, which |
| 39 |
-- is in the upper half (the corresponding JIS internal code is in the |
| 40 |
-- range 16#0080# - 16#00FF#). |
| 41 |
|
| 42 |
function EUC_To_JIS (EUC1, EUC2 : Character) return Wide_Character is |
| 43 |
EUC1B : constant Byte := Character'Pos (EUC1); |
| 44 |
EUC2B : constant Byte := Character'Pos (EUC2); |
| 45 |
|
| 46 |
begin |
| 47 |
if EUC2B not in 16#A0# .. 16#FE# then |
| 48 |
raise Constraint_Error; |
| 49 |
end if; |
| 50 |
|
| 51 |
if EUC1B = EUC_Hankaku_Kana then |
| 52 |
return Wide_Character'Val (EUC2B); |
| 53 |
|
| 54 |
else |
| 55 |
if EUC1B not in 16#A0# .. 16#FE# then |
| 56 |
raise Constraint_Error; |
| 57 |
else |
| 58 |
return Wide_Character'Val |
| 59 |
(256 * Natural (EUC1B and 16#7F#) + Natural (EUC2B and 16#7F#)); |
| 60 |
end if; |
| 61 |
end if; |
| 62 |
end EUC_To_JIS; |
| 63 |
|
| 64 |
---------------- |
| 65 |
-- JIS_To_EUC -- |
| 66 |
---------------- |
| 67 |
|
| 68 |
procedure JIS_To_EUC |
| 69 |
(J : Wide_Character; |
| 70 |
EUC1 : out Character; |
| 71 |
EUC2 : out Character) |
| 72 |
is |
| 73 |
JIS1 : constant Natural := Wide_Character'Pos (J) / 256; |
| 74 |
JIS2 : constant Natural := Wide_Character'Pos (J) rem 256; |
| 75 |
|
| 76 |
begin |
| 77 |
-- Special case of small Katakana |
| 78 |
|
| 79 |
if JIS1 = 0 then |
| 80 |
|
| 81 |
-- The value must be in the range 16#80# to 16#FF# so that the upper |
| 82 |
-- bit is set in both bytes. |
| 83 |
|
| 84 |
if JIS2 < 16#80# then |
| 85 |
raise Constraint_Error; |
| 86 |
end if; |
| 87 |
|
| 88 |
EUC1 := Character'Val (EUC_Hankaku_Kana); |
| 89 |
EUC2 := Character'Val (JIS2); |
| 90 |
|
| 91 |
-- The upper bit of both characters must be clear, or this is not |
| 92 |
-- a valid character for representation in EUC form. |
| 93 |
|
| 94 |
elsif JIS1 > 16#7F# or else JIS2 > 16#7F# then |
| 95 |
raise Constraint_Error; |
| 96 |
|
| 97 |
-- Result is just the two characters with upper bits set |
| 98 |
|
| 99 |
else |
| 100 |
EUC1 := Character'Val (JIS1 + 16#80#); |
| 101 |
EUC2 := Character'Val (JIS2 + 16#80#); |
| 102 |
end if; |
| 103 |
end JIS_To_EUC; |
| 104 |
|
| 105 |
---------------------- |
| 106 |
-- JIS_To_Shift_JIS -- |
| 107 |
---------------------- |
| 108 |
|
| 109 |
procedure JIS_To_Shift_JIS |
| 110 |
(J : Wide_Character; |
| 111 |
SJ1 : out Character; |
| 112 |
SJ2 : out Character) |
| 113 |
is |
| 114 |
JIS1 : Byte; |
| 115 |
JIS2 : Byte; |
| 116 |
|
| 117 |
begin |
| 118 |
-- The following is the required algorithm, it's hard to make any |
| 119 |
-- more intelligent comments. This was copied from a public domain |
| 120 |
-- C program called etos.c (author unknown). |
| 121 |
|
| 122 |
JIS1 := Byte (Natural (Wide_Character'Pos (J) / 256)); |
| 123 |
JIS2 := Byte (Natural (Wide_Character'Pos (J) rem 256)); |
| 124 |
|
| 125 |
if JIS1 > 16#5F# then |
| 126 |
JIS1 := JIS1 + 16#80#; |
| 127 |
end if; |
| 128 |
|
| 129 |
if (JIS1 mod 2) = 0 then |
| 130 |
SJ1 := Character'Val ((JIS1 - 16#30#) / 2 + 16#88#); |
| 131 |
SJ2 := Character'Val (JIS2 + 16#7E#); |
| 132 |
|
| 133 |
else |
| 134 |
if JIS2 >= 16#60# then |
| 135 |
JIS2 := JIS2 + 16#01#; |
| 136 |
end if; |
| 137 |
|
| 138 |
SJ1 := Character'Val ((JIS1 - 16#31#) / 2 + 16#89#); |
| 139 |
SJ2 := Character'Val (JIS2 + 16#1F#); |
| 140 |
end if; |
| 141 |
end JIS_To_Shift_JIS; |
| 142 |
|
| 143 |
---------------------- |
| 144 |
-- Shift_JIS_To_JIS -- |
| 145 |
---------------------- |
| 146 |
|
| 147 |
function Shift_JIS_To_JIS (SJ1, SJ2 : Character) return Wide_Character is |
| 148 |
SJIS1 : Byte; |
| 149 |
SJIS2 : Byte; |
| 150 |
JIS1 : Byte; |
| 151 |
JIS2 : Byte; |
| 152 |
|
| 153 |
begin |
| 154 |
-- The following is the required algorithm, it's hard to make any |
| 155 |
-- more intelligent comments. This was copied from a public domain |
| 156 |
-- C program called stoj.c written by [email protected]. |
| 157 |
|
| 158 |
SJIS1 := Character'Pos (SJ1); |
| 159 |
SJIS2 := Character'Pos (SJ2); |
| 160 |
|
| 161 |
if SJIS1 >= 16#E0# then |
| 162 |
SJIS1 := SJIS1 - 16#40#; |
| 163 |
end if; |
| 164 |
|
| 165 |
if SJIS2 >= 16#9F# then |
| 166 |
JIS1 := (SJIS1 - 16#88#) * 2 + 16#30#; |
| 167 |
JIS2 := SJIS2 - 16#7E#; |
| 168 |
|
| 169 |
else |
| 170 |
if SJIS2 >= 16#7F# then |
| 171 |
SJIS2 := SJIS2 - 16#01#; |
| 172 |
end if; |
| 173 |
|
| 174 |
JIS1 := (SJIS1 - 16#89#) * 2 + 16#31#; |
| 175 |
JIS2 := SJIS2 - 16#1F#; |
| 176 |
end if; |
| 177 |
|
| 178 |
if JIS1 not in 16#20# .. 16#7E# |
| 179 |
or else JIS2 not in 16#20# .. 16#7E# |
| 180 |
then |
| 181 |
raise Constraint_Error; |
| 182 |
else |
| 183 |
return Wide_Character'Val (256 * Natural (JIS1) + Natural (JIS2)); |
| 184 |
end if; |
| 185 |
end Shift_JIS_To_JIS; |
| 186 |
|
| 187 |
end System.WCh_JIS; |