1 /*
   2  * Copyright (c) 2002, 2018, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package $PACKAGE$;
  27 
  28 import java.nio.ByteBuffer;
  29 import java.nio.CharBuffer;
  30 import java.nio.charset.Charset;
  31 import java.nio.charset.CharsetDecoder;
  32 import java.nio.charset.CharsetEncoder;
  33 import java.nio.charset.CoderResult;
  34 import sun.nio.cs.HistoricallyNamedCharset;
  35 import sun.nio.cs.DelegatableDecoder;
  36 import sun.nio.cs.DoubleByte;
  37 import sun.nio.cs.Surrogate;
  38 import sun.nio.cs.SingleByte;
  39 import sun.nio.cs.*;
  40 import static sun.nio.cs.CharsetMapping.*;
  41 
  42 public class IBM33722
  43     extends Charset
  44     implements HistoricallyNamedCharset
  45 {
  46     public IBM33722() {
  47         super("x-IBM33722",  $ALIASES$);
  48     }
  49 
  50     public String historicalName() {
  51         return "Cp33722";
  52     }
  53 
  54     public boolean contains(Charset cs) {
  55         return ((cs.name().equals("US-ASCII"))
  56                 || (cs instanceof IBM33722));
  57     }
  58 
  59     public CharsetDecoder newDecoder() {
  60         return new Decoder(this);
  61     }
  62 
  63     public CharsetEncoder newEncoder() {
  64         return new Encoder(this);
  65     }
  66 
  67     private static class Decoder extends IBM29626C.Decoder {
  68 
  69         private final String G2_b = "\uA1F1\uA1F2\uA2CC";
  70 
  71         private final String G2_c = "\uFFE0\uFFE1\uFFE2";
  72 
  73         private final String G3_b =
  74             "\uF3B8\uF3B9\uF3AB\uF3AC\uF3AD\uF3AE\uF3AF\uF3B0\uF3B1\uF3B2"+
  75             "\uF3B3\uF3B4\uF3A1\uF3A2\uF3A3\uF3A4\uF3A5\uF3A6\uF3A7\uF3A8"+
  76             "\uF3A9\uF3AA\uF3B7\uF4A2\uF4A3\uF4A4\uF4A5\uF4A6\uF4A8\uF4A9"+
  77             "\uF4AC\uF4AE\uF4AF\uF4B0\uF4B2\uF4B3\uF4B4\uF4B5\uF4B6\uF4B7"+
  78             "\uF4BA\uF4BD\uF4BE\uF4C0\uF4BF\uF4C2\uF4A1\uF4C6\uF4C7\uF4C8"+
  79             "\uF4CB\uF4D0\uF4D4\uF4D5\uF4D7\uF4D9\uF4DC\uF4DF\uF4E0\uF4E1"+
  80             "\uF4E5\uF4E7\uF4EA\uF4ED\uF4EE\uF4EF\uF4F4\uF4F5\uF4F6\uF4F8"+
  81             "\uF4B9\uF4EB\uF4A7\uF4AA\uF4AB\uF4B1\uF4B8\uF4BB\uF4BC\uF4C4"+
  82             "\uF4C5\uF4C9\uF4CC\uF4CD\uF4CE\uF4CF\uF4D1\uF4D3\uF4D6\uF4D8"+
  83             "\uF4DA\uF4DB\uF4DE\uF4E2\uF4E3\uF4E4\uF4E6\uF4E8\uF4E9\uF4EC"+
  84             "\uF4F1\uF4F2\uF4F3\uF4F7\uF3B6\uF3B5";
  85 
  86         private final String G3_c =
  87             "\u2116\u2121\u2160\u2161\u2162\u2163\u2164\u2165\u2166\u2167"+
  88             "\u2168\u2169\u2170\u2171\u2172\u2173\u2174\u2175\u2176\u2177"+
  89             "\u2178\u2179\u3231\u4EFC\u50F4\u51EC\u5307\u5324\u548A\u5759"+
  90             "\u589E\u5BEC\u5CF5\u5D53\u5FB7\u6085\u6120\u654E\u663B\u6665"+
  91             "\u6801\u6A6B\u6AE2\u6DF2\u6DF8\u7028\u70BB\u7501\u7682\u769E"+
  92             "\u7930\u7AE7\u7DA0\u7DD6\u8362\u85B0\u8807\u8B7F\u8CF4\u8D76"+
  93             "\u90DE\u9115\u9592\u973B\u974D\u9751\u999E\u9AD9\u9B72\u9ED1"+
  94             "\uF929\uF9DC\uFA0E\uFA0F\uFA10\uFA11\uFA12\uFA13\uFA14\uFA15"+
  95             "\uFA16\uFA17\uFA18\uFA19\uFA1A\uFA1B\uFA1C\uFA1D\uFA1E\uFA1F"+
  96             "\uFA20\uFA21\uFA22\uFA23\uFA24\uFA25\uFA26\uFA27\uFA28\uFA29"+
  97             "\uFA2A\uFA2B\uFA2C\uFA2D\uFF02\uFF07";
  98 
  99         private final SingleByte.Decoder dec0201;
 100         private final DoubleByte.Decoder dec0208;
 101         private final DoubleByte.Decoder dec0212;
 102 
 103         protected Decoder(Charset cs) {
 104             super(cs);
 105             this.dec0201 = DEC0201;
 106             this.dec0208 = DEC0208;
 107             this.dec0212 = DEC0212;
 108         }
 109 
 110         protected char decodeSingle(int b) {
 111             if (b == 0x5c)
 112                 return '\u00A5';
 113             if (b == 0x7e)
 114                 return '\u203E';
 115             if (b < 0x8e)
 116                 return (char) b;
 117             if (b < 0x90)
 118                 return UNMAPPABLE_DECODING;
 119             if (b < 0xa0)
 120                 return (char) b;
 121             return UNMAPPABLE_DECODING;
 122         }
 123 
 124         final static String g1_c = "\u00a2\u00a3\u00ac\\\u007e";
 125 
 126         protected char decodeDouble(int byte1, int byte2) {
 127             if (byte1 == 0x8e) {
 128                 if (byte2 < 0x80)
 129                     return UNMAPPABLE_DECODING;
 130                 char c = dec0201.decode((byte)byte2);
 131                 if (byte2 >= 0xe0 && byte2 <= 0xe4)
 132                     c = g1_c.charAt(byte2 - 0xe0);
 133                 return c;
 134             }
 135             if ((byte1 >= 0xa1 && byte1 <= 0xfe)
 136                 && (byte2 >= 0xa1 && byte2 <= 0xfe)) {
 137                 char c = (char)((byte1 << 8) + byte2);
 138                 int idx = G2_b.indexOf(c);
 139                 if (idx > -1)
 140                     return G2_c.charAt(idx);
 141             }
 142             char ch = dec0208.decodeDouble(byte1 - 0x80, byte2 - 0x80);
 143             if (ch == UNMAPPABLE_DECODING)
 144                 ch = decodeUDC(byte1, byte2, 0xe000);
 145             return ch;
 146         }
 147 
 148         protected char decodeDoubleG3(int byte1, int byte2) {
 149             if ((byte1 >= 0xa1 && byte1 <= 0xfe)
 150                 && (byte2 >= 0xa1 && byte2 <= 0xfe)) {
 151                 char c = (char)((byte1 << 8) + byte2);
 152                 int idx = G3_b.indexOf(c);
 153                 if (idx > -1)
 154                     return G3_c.charAt(idx);
 155             }
 156             char ch = dec0212.decodeDouble(byte1 - 0x80, byte2 - 0x80);
 157             if (ch == '\u2116')
 158                 ch = UNMAPPABLE_DECODING;
 159             if (ch != UNMAPPABLE_DECODING)
 160                 ch =  ibm943.canEncode(ch) ? ch : UNMAPPABLE_DECODING;
 161             if (ch == UNMAPPABLE_DECODING)
 162                 ch = decodeUDC(byte1, byte2, 0xe3ac);
 163             return ch;
 164         }
 165     }
 166 
 167     private static class Encoder extends IBM29626C.Encoder {
 168 
 169         private final String G2_c = 
 170             "\u2015\u2225\u4FE0\u525D\u551E\u555E\u5699\u56CA\u5861\u5C5B"+
 171             "\u5C62\u6414\u6451\u6522\u6805\u688E\u6D00\u6F1E\u6F51\u7006"+
 172             "\u70FF\u7130\u7626\u79B1\u7C1E\u7E48\u7E61\u7E6B\u8141\u8346"+
 173             "\u840A\u8523\u8741\u881F\u8EC0\u91AC\u91B1\u92CA\u9830\u9839"+
 174             "\u985A\u9A52\u9DD7\u9E7C\u9EB4\u9EB5\uFF0D\uFF5E\uFFE0\uFFE1"+
 175             "\uFFE2\uFFE4";
 176 
 177         private final String G2_b =
 178             "\uA1BD\uA1C2\uB6A2\uC7ED\uC0E6\uB0A2\uB3FA\uC7B9\uC5B6\uD6A2"+
 179             "\uBCC8\uC1DF\uC4CF\uDAB9\uBAF4\uDBF4\uC6C2\uC2CD\uC8AE\uC6C2"+
 180             "\uB6A2\uB1EB\uC1E9\uC5F8\uC3BD\uE5DA\uBDAB\uB7D2\uE7A6\uB7D5"+
 181             "\uCDE9\uBED5\uB0A2\uCFB9\uB6ED\uBEDF\uC8B0\uB3FA\uCBCB\uF0F8"+
 182             "\uC5BF\uC2CD\uB2AA\uB8B4\uB9ED\uCCCD\uA1DD\uA1C1\uA1F1\uA1F2"+
 183             "\uA2CC\uA2C3";
 184 
 185         private final String G3_c =
 186             "\u2116\u2121\u2160\u2161\u2162\u2163\u2164\u2165\u2166\u2167"+
 187             "\u2168\u2169\u2170\u2171\u2172\u2173\u2174\u2175\u2176\u2177"+
 188             "\u2178\u2179\u3231\u4EFC\u50F4\u51EC\u5307\u5324\u548A\u5759"+
 189             "\u589E\u5BEC\u5CF5\u5D53\u5FB7\u6085\u6120\u654E\u663B\u6665"+
 190             "\u6801\u6A6B\u6AE2\u6DF2\u6DF8\u7028\u70BB\u7501\u7682\u769E"+
 191             "\u7930\u7AE7\u7DA0\u7DD6\u8362\u85B0\u8807\u8B7F\u8CF4\u8D76"+
 192             "\u8F91\u90DE\u9115\u9592\u973B\u974D\u9751\u999E\u9AD9\u9B72"+
 193             "\u9ED1\uF86F\uF929\uF9DC\uFA0E\uFA0F\uFA10\uFA11\uFA12\uFA13"+
 194             "\uFA14\uFA15\uFA16\uFA17\uFA18\uFA19\uFA1A\uFA1B\uFA1C\uFA1D"+
 195             "\uFA1E\uFA1F\uFA20\uFA21\uFA22\uFA23\uFA24\uFA25\uFA26\uFA27"+
 196             "\uFA28\uFA29\uFA2A\uFA2B\uFA2C\uFA2D\uFF02\uFF07";
 197 
 198         private final String G3_b =
 199             "\uF3B8\uF3B9\uF3AB\uF3AC\uF3AD\uF3AE\uF3AF\uF3B0\uF3B1\uF3B2"+
 200             "\uF3B3\uF3B4\uF3A1\uF3A2\uF3A3\uF3A4\uF3A5\uF3A6\uF3A7\uF3A8"+
 201             "\uF3A9\uF3AA\uF3B7\uF4A2\uF4A3\uF4A4\uF4A5\uF4A6\uF4A8\uF4A9"+
 202             "\uF4AC\uF4AE\uF4AF\uF4B0\uF4B2\uF4B3\uF4B4\uF4B5\uF4B6\uF4B7"+
 203             "\uF4BA\uF4BD\uF4BE\uF4C0\uF4BF\uF4C2\uF4A1\uF4C6\uF4C7\uF4C8"+
 204             "\uF4CB\uF4D0\uF4D4\uF4D5\uF4D7\uF4D9\uF4DC\uF4DF\uF4E0\uF4E1"+
 205             "\uF3B8\uF4E5\uF4E7\uF4EA\uF4ED\uF4EE\uF4EF\uF4F4\uF4F5\uF4F6"+
 206             "\uF4F8\uF3B8\uF4B9\uF4EB\uF4A7\uF4AA\uF4AB\uF4B1\uF4B8\uF4BB"+
 207             "\uF4BC\uF4C4\uF4C5\uF4C9\uF4CC\uF4CD\uF4CE\uF4CF\uF4D1\uF4D3"+
 208             "\uF4D6\uF4D8\uF4DA\uF4DB\uF4DE\uF4E2\uF4E3\uF4E4\uF4E6\uF4E8"+
 209             "\uF4E9\uF4EC\uF4F1\uF4F2\uF4F3\uF4F7\uF3B6\uF3B5";
 210 
 211         private final SingleByte.Encoder enc0201;
 212         private final DoubleByte.Encoder enc0208;
 213         private final DoubleByte.Encoder enc0212;
 214 
 215         protected Encoder(Charset cs) {
 216             super(cs);
 217             this.enc0201 = ENC0201;
 218             this.enc0208 = ENC0208;
 219             this.enc0212 = ENC0212;
 220         }
 221 
 222         private final static String G1_c = "\u00A2\u00A3\u00AC\\\u007e";
 223 
 224         protected int encodeSingle(char inputChar, byte[] outputByte) {
 225             if (inputChar == 0xa5) {
 226                 outputByte[0] = (byte)0x5c;
 227                 return 1;
 228             }
 229             if (inputChar == 0x203e) {
 230                 outputByte[0] = (byte)0x7e;
 231                 return 1;
 232             }
 233             if (inputChar >= 0x80 && inputChar < 0x8e) {
 234                 outputByte[0] = (byte)inputChar;
 235                 return 1;
 236             }
 237             if (inputChar >= 0x90 && inputChar < 0xa0) {
 238                 outputByte[0] = (byte)inputChar;
 239                 return 1;
 240             }
 241             int b = enc0201.encode(inputChar);
 242             if (inputChar == 0x5c || inputChar == 0x7e) {
 243                 b = UNMAPPABLE_ENCODING;
 244             }
 245             if (b == UNMAPPABLE_ENCODING) {
 246                 int idx = G1_c.indexOf(inputChar);
 247                 if (idx > -1)
 248                     b = 0xe0 + idx;
 249             }
 250             if (b == UNMAPPABLE_ENCODING)
 251                 return 0;
 252             if (b >= 0 && b < 128) {
 253                 outputByte[0] = (byte)b;
 254                 return 1;
 255             }
 256             outputByte[0] = (byte)0x8e;
 257             outputByte[1] = (byte)b;
 258             return 2;
 259         }
 260 
 261         protected int encodeDouble(char ch) {
 262             int idx = G2_c.indexOf(ch);
 263             if (idx > -1)
 264                 return (int)G2_b.charAt(idx);
 265             idx = G3_c.indexOf(ch);
 266             if (idx > -1)
 267                 return (int)G3_b.charAt(idx) + 0x8f0000;
 268             int b = enc0208.encodeChar(ch);
 269             if (b != UNMAPPABLE_ENCODING)
 270                 return b + 0x8080;
 271             b = encodeUDC(ch);
 272             if (b != UNMAPPABLE_ENCODING)
 273                 return b;
 274             if (ibm943.canEncode(ch)) {
 275                 b = enc0212.encodeChar(ch);
 276                 if (b != UNMAPPABLE_ENCODING) {
 277                     b += 0x8f8080;
 278                     return b;
 279                 }
 280             }
 281             return b;
 282         }
 283     }
 284 }