1 /*
   2  * Copyright (c) 2003, 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 /*
  27  *******************************************************************************
  28  * Copyright (C) 1996-2014, International Business Machines Corporation and
  29  * others. All Rights Reserved.
  30  *******************************************************************************
  31  */
  32 
  33 package sun.text.normalizer;
  34 
  35 import java.io.BufferedInputStream;
  36 import java.io.DataInputStream;
  37 import java.io.InputStream;
  38 import java.io.IOException;
  39 import java.io.UncheckedIOException;
  40 import java.nio.ByteBuffer;
  41 import java.nio.ByteOrder;
  42 import java.util.Arrays;
  43 import java.security.AccessController;
  44 import java.security.PrivilegedAction;
  45 
  46 public final class ICUBinary {
  47 
  48     private static final class IsAcceptable implements Authenticate {
  49         @Override
  50         public boolean isDataVersionAcceptable(byte version[]) {
  51             return version[0] == 1;
  52         }
  53     }
  54 
  55     // public inner interface ------------------------------------------------
  56 
  57     /**
  58      * Special interface for data authentication
  59      */
  60     public static interface Authenticate
  61     {
  62         /**
  63          * Method used in ICUBinary.readHeader() to provide data format
  64          * authentication.
  65          * @param version version of the current data
  66          * @return true if dataformat is an acceptable version, false otherwise
  67          */
  68         public boolean isDataVersionAcceptable(byte version[]);
  69     }
  70 
  71     // public methods --------------------------------------------------------
  72 
  73     /**
  74      * Loads an ICU binary data file and returns it as a ByteBuffer.
  75      * The buffer contents is normally read-only, but its position etc. can be modified.
  76      *
  77      * @param itemPath Relative ICU data item path, for example "root.res" or "coll/ucadata.icu".
  78      * @return The data as a read-only ByteBuffer.
  79      */
  80     public static ByteBuffer getRequiredData(String itemPath) {
  81         final Class<ICUBinary> root = ICUBinary.class;
  82 
  83         try (InputStream is = AccessController.doPrivileged(new PrivilegedAction<InputStream>() {
  84                 public InputStream run() {
  85                     return root.getResourceAsStream(itemPath);
  86                 }
  87             })) {
  88 
  89             // is.available() may return 0, or 1, or the total number of bytes in the stream,
  90             // or some other number.
  91             // Do not try to use is.available() == 0 to find the end of the stream!
  92             byte[] bytes;
  93             int avail = is.available();
  94             if (avail > 32) {
  95                 // There are more bytes available than just the ICU data header length.
  96                 // With luck, it is the total number of bytes.
  97                 bytes = new byte[avail];
  98             } else {
  99                 bytes = new byte[128];  // empty .res files are even smaller
 100             }
 101             // Call is.read(...) until one returns a negative value.
 102             int length = 0;
 103             for(;;) {
 104                 if (length < bytes.length) {
 105                     int numRead = is.read(bytes, length, bytes.length - length);
 106                     if (numRead < 0) {
 107                         break;  // end of stream
 108                     }
 109                     length += numRead;
 110                 } else {
 111                     // See if we are at the end of the stream before we grow the array.
 112                     int nextByte = is.read();
 113                     if (nextByte < 0) {
 114                         break;
 115                     }
 116                     int capacity = 2 * bytes.length;
 117                     if (capacity < 128) {
 118                         capacity = 128;
 119                     } else if (capacity < 0x4000) {
 120                         capacity *= 2;  // Grow faster until we reach 16kB.
 121                     }
 122                     // TODO Java 6 replace new byte[] and arraycopy(): bytes = Arrays.copyOf(bytes, capacity);
 123                     byte[] newBytes = new byte[capacity];
 124                     System.arraycopy(bytes, 0, newBytes, 0, length);
 125                     bytes = newBytes;
 126                     bytes[length++] = (byte) nextByte;
 127                 }
 128            }
 129             return ByteBuffer.wrap(bytes, 0, length);
 130         }
 131         catch (IOException e) {
 132             throw new UncheckedIOException(e);
 133         }
 134     }
 135 
 136     /**
 137      * Same as readHeader(), but returns a VersionInfo rather than a compact int.
 138      */
 139     public static VersionInfo readHeaderAndDataVersion(ByteBuffer bytes,
 140                                                              int dataFormat,
 141                                                              Authenticate authenticate)
 142                                                                 throws IOException {
 143         return getVersionInfoFromCompactInt(readHeader(bytes, dataFormat, authenticate));
 144     }
 145 
 146     private static final byte BIG_ENDIAN_ = 1;
 147     public static final byte[] readHeader(InputStream inputStream,
 148                                         byte dataFormatIDExpected[],
 149                                         Authenticate authenticate)
 150                                                           throws IOException
 151     {
 152         DataInputStream input = new DataInputStream(inputStream);
 153         char headersize = input.readChar();
 154         int readcount = 2;
 155         //reading the header format
 156         byte magic1 = input.readByte();
 157         readcount ++;
 158         byte magic2 = input.readByte();
 159         readcount ++;
 160         if (magic1 != MAGIC1 || magic2 != MAGIC2) {
 161             throw new IOException(MAGIC_NUMBER_AUTHENTICATION_FAILED_);
 162         }
 163 
 164         input.readChar(); // reading size
 165         readcount += 2;
 166         input.readChar(); // reading reserved word
 167         readcount += 2;
 168         byte bigendian    = input.readByte();
 169         readcount ++;
 170         byte charset      = input.readByte();
 171         readcount ++;
 172         byte charsize     = input.readByte();
 173         readcount ++;
 174         input.readByte(); // reading reserved byte
 175         readcount ++;
 176 
 177         byte dataFormatID[] = new byte[4];
 178         input.readFully(dataFormatID);
 179         readcount += 4;
 180         byte dataVersion[] = new byte[4];
 181         input.readFully(dataVersion);
 182         readcount += 4;
 183         byte unicodeVersion[] = new byte[4];
 184         input.readFully(unicodeVersion);
 185         readcount += 4;
 186         if (headersize < readcount) {
 187             throw new IOException("Internal Error: Header size error");
 188         }
 189         input.skipBytes(headersize - readcount);
 190 
 191         if (bigendian != BIG_ENDIAN_ || charset != CHAR_SET_
 192             || charsize != CHAR_SIZE_
 193             || !Arrays.equals(dataFormatIDExpected, dataFormatID)
 194             || (authenticate != null
 195                 && !authenticate.isDataVersionAcceptable(dataVersion))) {
 196             throw new IOException(HEADER_AUTHENTICATION_FAILED_);
 197         }
 198         return unicodeVersion;
 199     }
 200 
 201     /**
 202      * Reads an ICU data header, checks the data format, and returns the data version.
 203      *
 204      * <p>Assumes that the ByteBuffer position is 0 on input.
 205      * The buffer byte order is set according to the data.
 206      * The buffer position is advanced past the header (including UDataInfo and comment).
 207      *
 208      * <p>See C++ ucmndata.h and unicode/udata.h.
 209      *
 210      * @return dataVersion
 211      * @throws IOException if this is not a valid ICU data item of the expected dataFormat
 212      */
 213     public static int readHeader(ByteBuffer bytes, int dataFormat, Authenticate authenticate)
 214             throws IOException {
 215         assert bytes.position() == 0;
 216         byte magic1 = bytes.get(2);
 217         byte magic2 = bytes.get(3);
 218         if (magic1 != MAGIC1 || magic2 != MAGIC2) {
 219             throw new IOException(MAGIC_NUMBER_AUTHENTICATION_FAILED_);
 220         }
 221 
 222         byte isBigEndian = bytes.get(8);
 223         byte charsetFamily = bytes.get(9);
 224         byte sizeofUChar = bytes.get(10);
 225         if (isBigEndian < 0 || 1 < isBigEndian ||
 226                 charsetFamily != CHAR_SET_ || sizeofUChar != CHAR_SIZE_) {
 227             throw new IOException(HEADER_AUTHENTICATION_FAILED_);
 228         }
 229         bytes.order(isBigEndian != 0 ? ByteOrder.BIG_ENDIAN : ByteOrder.LITTLE_ENDIAN);
 230 
 231         int headerSize = bytes.getChar(0);
 232         int sizeofUDataInfo = bytes.getChar(4);
 233         if (sizeofUDataInfo < 20 || headerSize < (sizeofUDataInfo + 4)) {
 234             throw new IOException("Internal Error: Header size error");
 235         }
 236         // TODO: Change Authenticate to take int major, int minor, int milli, int micro
 237         // to avoid array allocation.
 238         byte[] formatVersion = new byte[] {
 239             bytes.get(16), bytes.get(17), bytes.get(18), bytes.get(19)
 240         };
 241         if (bytes.get(12) != (byte)(dataFormat >> 24) ||
 242                 bytes.get(13) != (byte)(dataFormat >> 16) ||
 243                 bytes.get(14) != (byte)(dataFormat >> 8) ||
 244                 bytes.get(15) != (byte)dataFormat ||
 245                 (authenticate != null && !authenticate.isDataVersionAcceptable(formatVersion))) {
 246             throw new IOException(HEADER_AUTHENTICATION_FAILED_ +
 247                     String.format("; data format %02x%02x%02x%02x, format version %d.%d.%d.%d",
 248                             bytes.get(12), bytes.get(13), bytes.get(14), bytes.get(15),
 249                             formatVersion[0] & 0xff, formatVersion[1] & 0xff,
 250                             formatVersion[2] & 0xff, formatVersion[3] & 0xff));
 251         }
 252 
 253         bytes.position(headerSize);
 254         return  // dataVersion
 255                 ((int)bytes.get(20) << 24) |
 256                 ((bytes.get(21) & 0xff) << 16) |
 257                 ((bytes.get(22) & 0xff) << 8) |
 258                 (bytes.get(23) & 0xff);
 259     }
 260 
 261     public static void skipBytes(ByteBuffer bytes, int skipLength) {
 262         if (skipLength > 0) {
 263             bytes.position(bytes.position() + skipLength);
 264         }
 265     }
 266 
 267     /**
 268      * Returns a VersionInfo for the bytes in the compact version integer.
 269      */
 270     public static VersionInfo getVersionInfoFromCompactInt(int version) {
 271         return VersionInfo.getInstance(
 272                 version >>> 24, (version >> 16) & 0xff, (version >> 8) & 0xff, version & 0xff);
 273     }
 274 
 275     // private variables -------------------------------------------------
 276 
 277     /**
 278     * Magic numbers to authenticate the data file
 279     */
 280     private static final byte MAGIC1 = (byte)0xda;
 281     private static final byte MAGIC2 = (byte)0x27;
 282 
 283     /**
 284     * File format authentication values
 285     */
 286     private static final byte CHAR_SET_ = 0;
 287     private static final byte CHAR_SIZE_ = 2;
 288 
 289     /**
 290     * Error messages
 291     */
 292     private static final String MAGIC_NUMBER_AUTHENTICATION_FAILED_ =
 293                        "ICUBinary data file error: Magic number authentication failed";
 294     private static final String HEADER_AUTHENTICATION_FAILED_ =
 295         "ICUBinary data file error: Header authentication failed";
 296 }