Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/make/src/classes/build/tools/charsetmapping/JIS0213.java
32287 views
/*1* Copyright (c) 2008, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package build.tools.charsetmapping;2627import java.io.*;28import java.util.regex.*;29import java.util.*;30import static build.tools.charsetmapping.Utils.*;3132public class JIS0213 {3334// regex pattern to parse the "jis0213.map" file35static Pattern sjis0213 = Pattern.compile("0x(\\p{XDigit}++)\\s++U\\+(\\p{XDigit}++)(?:\\+(\\p{XDigit}++))?\\s++#.*");3637static void genClass(String argv[]) throws IOException38{39InputStream in = new FileInputStream(argv[0]) ;40OutputStream out = new FileOutputStream(argv[1]);4142int[] sb = new int[0x100]; // singlebyte43int[] db = new int[0x10000]; // doublebyte44int[] indexC2B = new int[256];45Entry[] supp = new Entry[0x10000];46Entry[] comp = new Entry[0x100];47int suppTotal = 0;48int compTotal = 0;4950int b1Min1 = 0x81;51int b1Max1 = 0x9f;52int b1Min2 = 0xe0;53int b1Max2 = 0xfc;54int b2Min = 0x40;55int b2Max = 0xfe;5657//init58for (int i = 0; i < 0x80; i++) sb[i] = i;59for (int i = 0x80; i < 0x100; i++) sb[i] = UNMAPPABLE_DECODING;60for (int i = 0; i < 0x10000; i++) db[i] = UNMAPPABLE_DECODING;61try {62Parser p = new Parser(in, sjis0213);63Entry e = null;64while ((e = p.next()) != null) {65if (e.cp2 != 0) {66comp[compTotal++] = e;67} else {68if (e.cp <= 0xffff) {69if (e.bs <= 0xff)70sb[e.bs] = e.cp;71else72db[e.bs] = e.cp;73indexC2B[e.cp>>8] = 1;74} else {75supp[suppTotal++] = e;76}77}78}79ByteArrayOutputStream baos = new ByteArrayOutputStream();80// c2b Index Table, always the first one81writeINDEXC2B(baos, indexC2B);82writeSINGLEBYTE(baos, sb);83writeDOUBLEBYTE1(baos, db, b1Min1, b1Max1, b2Min, b2Max);84writeDOUBLEBYTE2(baos, db, b1Min2, b1Max2, b2Min, b2Max);85writeSUPPLEMENT(baos, supp, suppTotal);86writeCOMPOSITE(baos, comp, compTotal);87writeSIZE(out, baos.size());88baos.writeTo(out);89out.close();90} catch (Exception x) {91x.printStackTrace();92}93}9495static Comparator<Entry> comparatorCP =96new Comparator<Entry>() {97public int compare(Entry m1, Entry m2) {98return m1.cp - m2.cp;99}100public boolean equals(Object obj) {101return this == obj;102}103};104105// tags of different charset mapping tables106private final static int MAP_SINGLEBYTE = 0x1; // 0..256 : c107private final static int MAP_DOUBLEBYTE1 = 0x2; // min..max: c108private final static int MAP_DOUBLEBYTE2 = 0x3; // min..max: c [DB2]109private final static int MAP_SUPPLEMENT = 0x5; // db,c110private final static int MAP_SUPPLEMENT_C2B = 0x6; // c,db111private final static int MAP_COMPOSITE = 0x7; // db,base,cc112private final static int MAP_INDEXC2B = 0x8; // index table of c->bb113114private static final void writeShort(OutputStream out, int data)115throws IOException116{117out.write((data >>> 8) & 0xFF);118out.write((data ) & 0xFF);119}120121private static final void writeShortArray(OutputStream out,122int type,123int[] array,124int off,125int size) // exclusive126throws IOException127{128writeShort(out, type);129writeShort(out, size);130for (int i = off; i < size; i++) {131writeShort(out, array[off+i]);132}133}134135private static final void writeSIZE(OutputStream out, int data)136throws IOException137{138out.write((data >>> 24) & 0xFF);139out.write((data >>> 16) & 0xFF);140out.write((data >>> 8) & 0xFF);141out.write((data ) & 0xFF);142}143144private static void writeINDEXC2B(OutputStream out, int[] indexC2B)145throws IOException146{147writeShort(out, MAP_INDEXC2B);148writeShort(out, indexC2B.length);149int off = 0;150for (int i = 0; i < indexC2B.length; i++) {151if (indexC2B[i] != 0) {152writeShort(out, off);153off += 256;154} else {155writeShort(out, -1);156}157}158}159160private static void writeSINGLEBYTE(OutputStream out, int[] sb)161throws IOException162{163writeShortArray(out, MAP_SINGLEBYTE, sb, 0, 256);164}165166private static void writeDOUBLEBYTE(OutputStream out,167int type,168int[] db,169int b1Min, int b1Max,170int b2Min, int b2Max)171throws IOException172{173writeShort(out, type);174writeShort(out, b1Min);175writeShort(out, b1Max);176writeShort(out, b2Min);177writeShort(out, b2Max);178writeShort(out, (b1Max - b1Min + 1) * (b2Max - b2Min + 1));179180for (int b1 = b1Min; b1 <= b1Max; b1++) {181for (int b2 = b2Min; b2 <= b2Max; b2++) {182writeShort(out, db[b1 * 256 + b2]);183}184}185}186187private static void writeDOUBLEBYTE1(OutputStream out,188int[] db,189int b1Min, int b1Max,190int b2Min, int b2Max)191throws IOException192{193writeDOUBLEBYTE(out, MAP_DOUBLEBYTE1, db, b1Min, b1Max, b2Min, b2Max);194}195196private static void writeDOUBLEBYTE2(OutputStream out,197int[] db,198int b1Min, int b1Max,199int b2Min, int b2Max)200throws IOException201{202writeDOUBLEBYTE(out, MAP_DOUBLEBYTE2, db, b1Min, b1Max, b2Min, b2Max);203}204205// the c2b table is output as well206private static void writeSUPPLEMENT(OutputStream out, Entry[] supp, int size)207throws IOException208{209writeShort(out, MAP_SUPPLEMENT);210writeShort(out, size * 2);211// db at first half, cc at the low half212for (int i = 0; i < size; i++) {213writeShort(out, supp[i].bs);214}215for (int i = 0; i < size; i++) {216writeShort(out, supp[i].cp);217}218219//c2b220writeShort(out, MAP_SUPPLEMENT_C2B);221writeShort(out, size*2);222Arrays.sort(supp, 0, size, comparatorCP);223for (int i = 0; i < size; i++) {224writeShort(out, supp[i].cp);225}226for (int i = 0; i < size; i++) {227writeShort(out, supp[i].bs);228}229}230231private static void writeCOMPOSITE(OutputStream out, Entry[] comp, int size)232throws IOException233{234writeShort(out, MAP_COMPOSITE);235writeShort(out, size*3);236// comp is sorted already237for (int i = 0; i < size; i++) {238writeShort(out, (char)comp[i].bs);239writeShort(out, (char)comp[i].cp);240writeShort(out, (char)comp[i].cp2);241}242}243}244245246