Path: blob/master/src/java.base/share/classes/jdk/internal/jimage/ImageStringsReader.java
41159 views
/*1* Copyright (c) 2014, 2021, 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 jdk.internal.jimage;2627import java.io.UTFDataFormatException;28import java.nio.ByteBuffer;29import java.nio.charset.StandardCharsets;30import java.util.Objects;3132/**33* @implNote This class needs to maintain JDK 8 source compatibility.34*35* It is used internally in the JDK to implement jimage/jrtfs access,36* but also compiled and delivered as part of the jrtfs.jar to support access37* to the jimage file provided by the shipped JDK by tools running on JDK 8.38*/39public class ImageStringsReader implements ImageStrings {40public static final int HASH_MULTIPLIER = 0x01000193;41public static final int POSITIVE_MASK = 0x7FFFFFFF;4243private final BasicImageReader reader;4445ImageStringsReader(BasicImageReader reader) {46this.reader = Objects.requireNonNull(reader);47}4849@Override50public String get(int offset) {51return reader.getString(offset);52}5354@Override55public int match(int offset, String string, int stringOffset) {56return reader.match(offset, string, stringOffset);57}5859@Override60public int add(final String string) {61throw new InternalError("Can not add strings at runtime");62}6364public static int hashCode(String s) {65return hashCode(s, HASH_MULTIPLIER);66}6768public static int hashCode(String s, int seed) {69return unmaskedHashCode(s, seed) & POSITIVE_MASK;70}7172public static int hashCode(String module, String name) {73return hashCode(module, name, HASH_MULTIPLIER);74}7576public static int hashCode(String module, String name, int seed) {77seed = (seed * HASH_MULTIPLIER) ^ ('/');78seed = unmaskedHashCode(module, seed);79seed = (seed * HASH_MULTIPLIER) ^ ('/');80seed = unmaskedHashCode(name, seed);81return seed & POSITIVE_MASK;82}8384public static int unmaskedHashCode(String s, int seed) {85int slen = s.length();86byte[] buffer = null;8788for (int i = 0; i < slen; i++) {89int uch = s.charAt(i);9091if ((uch & ~0x7F) != 0) {92if (buffer == null) {93buffer = new byte[8];94}95int mask = ~0x3F;96int n = 0;9798do {99buffer[n++] = (byte)(0x80 | (uch & 0x3F));100uch >>= 6;101mask >>= 1;102} while ((uch & mask) != 0);103104buffer[n] = (byte)((mask << 1) | uch);105106do {107seed = (seed * HASH_MULTIPLIER) ^ (buffer[n--] & 0xFF);108} while (0 <= n);109} else if (uch == 0) {110seed = (seed * HASH_MULTIPLIER) ^ (0xC0);111seed = (seed * HASH_MULTIPLIER) ^ (0x80);112} else {113seed = (seed * HASH_MULTIPLIER) ^ (uch);114}115}116return seed;117}118119static int charsFromMUTF8Length(byte[] bytes, int offset, int count) {120int length = 0;121122for (int i = offset; i < offset + count; i++) {123byte ch = bytes[i];124125if (ch == 0) {126break;127}128129if ((ch & 0xC0) != 0x80) {130length++;131}132}133134return length;135}136137static void charsFromMUTF8(char[] chars, byte[] bytes, int offset, int count) throws UTFDataFormatException {138int j = 0;139140for (int i = offset; i < offset + count; i++) {141byte ch = bytes[i];142143if (ch == 0) {144break;145}146147boolean is_unicode = (ch & 0x80) != 0;148int uch = ch & 0x7F;149150if (is_unicode) {151int mask = 0x40;152153while ((uch & mask) != 0) {154ch = bytes[++i];155156if ((ch & 0xC0) != 0x80) {157throw new UTFDataFormatException("bad continuation 0x" + Integer.toHexString(ch));158}159160uch = ((uch & ~mask) << 6) | (ch & 0x3F);161mask <<= 6 - 1;162}163164if ((uch & 0xFFFF) != uch) {165throw new UTFDataFormatException("character out of range \\u" + Integer.toHexString(uch));166}167}168169chars[j++] = (char)uch;170}171}172173public static String stringFromMUTF8(byte[] bytes, int offset, int count) {174int length = charsFromMUTF8Length(bytes, offset, count);175char[] chars = new char[length];176177try {178charsFromMUTF8(chars, bytes, offset, count);179} catch (UTFDataFormatException ex) {180throw new InternalError("Attempt to convert non modified UTF-8 byte sequence", ex);181}182183return new String(chars);184}185186public static String stringFromMUTF8(byte[] bytes) {187return stringFromMUTF8(bytes, 0, bytes.length);188}189190/**191* Calculates the number of characters in the String present at the192* specified offset. As an optimization, the length returned will193* be positive if the characters are all ASCII, and negative otherwise.194*/195private static int charsFromByteBufferLength(ByteBuffer buffer, int offset) {196int length = 0;197198int limit = buffer.limit();199boolean asciiOnly = true;200while (offset < limit) {201byte ch = buffer.get(offset++);202203if (ch < 0) {204asciiOnly = false;205} else if (ch == 0) {206return asciiOnly ? length : -length;207}208209if ((ch & 0xC0) != 0x80) {210length++;211}212}213throw new InternalError("No terminating zero byte for modified UTF-8 byte sequence");214}215216private static void charsFromByteBuffer(char[] chars, ByteBuffer buffer, int offset) {217int j = 0;218219int limit = buffer.limit();220while (offset < limit) {221byte ch = buffer.get(offset++);222223if (ch == 0) {224return;225}226227boolean is_unicode = (ch & 0x80) != 0;228int uch = ch & 0x7F;229230if (is_unicode) {231int mask = 0x40;232233while ((uch & mask) != 0) {234ch = buffer.get(offset++);235236if ((ch & 0xC0) != 0x80) {237throw new InternalError("Bad continuation in " +238"modified UTF-8 byte sequence: " + ch);239}240241uch = ((uch & ~mask) << 6) | (ch & 0x3F);242mask <<= 6 - 1;243}244}245246if ((uch & 0xFFFF) != uch) {247throw new InternalError("UTF-32 char in modified UTF-8 " +248"byte sequence: " + uch);249}250251chars[j++] = (char)uch;252}253254throw new InternalError("No terminating zero byte for modified UTF-8 byte sequence");255}256257public static String stringFromByteBuffer(ByteBuffer buffer) {258return stringFromByteBuffer(buffer, 0);259}260261/* package-private */262static String stringFromByteBuffer(ByteBuffer buffer, int offset) {263int length = charsFromByteBufferLength(buffer, offset);264if (length > 0) {265byte[] asciiBytes = new byte[length];266// Ideally we could use buffer.get(offset, asciiBytes, 0, length)267// here, but that was introduced in JDK 13268for (int i = 0; i < length; i++) {269asciiBytes[i] = buffer.get(offset++);270}271return new String(asciiBytes, StandardCharsets.US_ASCII);272}273char[] chars = new char[-length];274charsFromByteBuffer(chars, buffer, offset);275return new String(chars);276}277278/* package-private */279static int stringFromByteBufferMatches(ByteBuffer buffer, int offset, String string, int stringOffset) {280// ASCII fast-path281int limit = buffer.limit();282int current = offset;283int slen = string.length();284while (current < limit) {285byte ch = buffer.get(current);286if (ch <= 0) {287if (ch == 0) {288// Match289return current - offset;290}291// non-ASCII byte, run slow-path from current offset292break;293}294if (slen <= stringOffset || string.charAt(stringOffset) != (char)ch) {295// No match296return -1;297}298stringOffset++;299current++;300}301// invariant: remainder of the string starting at current is non-ASCII,302// so return value from charsFromByteBufferLength will be negative303int length = -charsFromByteBufferLength(buffer, current);304char[] chars = new char[length];305charsFromByteBuffer(chars, buffer, current);306for (int i = 0; i < length; i++) {307if (string.charAt(stringOffset++) != chars[i]) {308return -1;309}310}311return length;312}313314static int mutf8FromStringLength(String s) {315int length = 0;316int slen = s.length();317318for (int i = 0; i < slen; i++) {319char ch = s.charAt(i);320int uch = ch & 0xFFFF;321322if ((uch & ~0x7F) != 0) {323int mask = ~0x3F;324int n = 0;325326do {327n++;328uch >>= 6;329mask >>= 1;330} while ((uch & mask) != 0);331332length += n + 1;333} else if (uch == 0) {334length += 2;335} else {336length++;337}338}339340return length;341}342343static void mutf8FromString(byte[] bytes, int offset, String s) {344int j = offset;345byte[] buffer = null;346int slen = s.length();347348for (int i = 0; i < slen; i++) {349char ch = s.charAt(i);350int uch = ch & 0xFFFF;351352if ((uch & ~0x7F) != 0) {353if (buffer == null) {354buffer = new byte[8];355}356int mask = ~0x3F;357int n = 0;358359do {360buffer[n++] = (byte)(0x80 | (uch & 0x3F));361uch >>= 6;362mask >>= 1;363} while ((uch & mask) != 0);364365buffer[n] = (byte)((mask << 1) | uch);366367do {368bytes[j++] = buffer[n--];369} while (0 <= n);370} else if (uch == 0) {371bytes[j++] = (byte)0xC0;372bytes[j++] = (byte)0x80;373} else {374bytes[j++] = (byte)uch;375}376}377}378379public static byte[] mutf8FromString(String string) {380int length = mutf8FromStringLength(string);381byte[] bytes = new byte[length];382mutf8FromString(bytes, 0, string);383384return bytes;385}386}387388389