1 /*
   2  * Copyright (c) 2014, 2016, 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 jdk.internal.jimage;
  27 
  28 import java.nio.ByteBuffer;
  29 import java.util.Objects;
  30 
  31 /**
  32  * @implNote This class needs to maintain JDK 8 source compatibility.
  33  *
  34  * It is used internally in the JDK to implement jimage/jrtfs access,
  35  * but also compiled and delivered as part of the jrtfs.jar to support access
  36  * to the jimage file provided by the shipped JDK by tools running on JDK 8.
  37  */
  38 public class ImageLocation {
  39     public static final int ATTRIBUTE_END = 0;
  40     public static final int ATTRIBUTE_MODULE = 1;
  41     public static final int ATTRIBUTE_PARENT = 2;
  42     public static final int ATTRIBUTE_BASE = 3;
  43     public static final int ATTRIBUTE_EXTENSION = 4;
  44     public static final int ATTRIBUTE_OFFSET = 5;
  45     public static final int ATTRIBUTE_COMPRESSED = 6;
  46     public static final int ATTRIBUTE_UNCOMPRESSED = 7;
  47     public static final int ATTRIBUTE_COUNT = 8;
  48 
  49     protected final long[] attributes;
  50 
  51     protected final ImageStrings strings;
  52 
  53     public ImageLocation(long[] attributes, ImageStrings strings) {
  54         this.attributes = Objects.requireNonNull(attributes);
  55         this.strings = Objects.requireNonNull(strings);
  56     }
  57 
  58     ImageStrings getStrings() {
  59         return strings;
  60     }
  61 
  62     private static int attributeLength(int data) {
  63         return (data & 0x7) + 1;
  64     }
  65 
  66     private static int attributeKind(int data) {
  67         return data >>> 3;
  68     }
  69 
  70     static long[] decompress(ByteBuffer bytes) {
  71         Objects.requireNonNull(bytes);
  72         long[] attributes = new long[ATTRIBUTE_COUNT];
  73 
  74         if (bytes != null) {
  75             while (bytes.hasRemaining()) {
  76                 int data = bytes.get() & 0xFF;
  77                 int kind = attributeKind(data);
  78 
  79                 if (kind == ATTRIBUTE_END) {
  80                     break;
  81                 }
  82 
  83                 if (kind < ATTRIBUTE_END || ATTRIBUTE_COUNT <= kind) {
  84                     throw new InternalError(
  85                         "Invalid jimage attribute kind: " + kind);
  86                 }
  87 
  88                 int length = attributeLength(data);
  89                 long value = 0;
  90 
  91                 for (int j = 0; j < length; j++) {
  92                     value <<= 8;
  93 
  94                     if (!bytes.hasRemaining()) {
  95                         throw new InternalError("Missing jimage attribute data");
  96                     }
  97 
  98                     value |= bytes.get() & 0xFF;
  99                 }
 100 
 101                  attributes[kind] = value;
 102             }
 103         }
 104 
 105         return attributes;
 106     }
 107 
 108     public static byte[] compress(long[] attributes) {
 109         Objects.requireNonNull(attributes);
 110         ImageStream stream = new ImageStream(16);
 111 
 112         for (int kind = ATTRIBUTE_END + 1; kind < ATTRIBUTE_COUNT; kind++) {
 113             long value = attributes[kind];
 114 
 115             if (value != 0) {
 116                 int n = (63 - Long.numberOfLeadingZeros(value)) >> 3;
 117                 stream.put((kind << 3) | n);
 118 
 119                 for (int i = n; i >= 0; i--) {
 120                     stream.put((int)(value >> (i << 3)));
 121                 }
 122             }
 123         }
 124 
 125         stream.put(ATTRIBUTE_END << 3);
 126 
 127         return stream.toArray();
 128      }
 129 
 130     public boolean verify(String name) {
 131         Objects.requireNonNull(name);
 132 
 133         return name.equals(getFullName());
 134     }
 135 
 136     long getAttribute(int kind) {
 137         if (kind < ATTRIBUTE_END || ATTRIBUTE_COUNT <= kind) {
 138             throw new InternalError(
 139                 "Invalid jimage attribute kind: " + kind);
 140         }
 141 
 142         return attributes[kind];
 143     }
 144 
 145     String getAttributeString(int kind) {
 146         if (kind < ATTRIBUTE_END || ATTRIBUTE_COUNT <= kind) {
 147             throw new InternalError(
 148                 "Invalid jimage attribute kind: " + kind);
 149         }
 150 
 151         return getStrings().get((int)attributes[kind]);
 152     }
 153 
 154     public String getModule() {
 155         return getAttributeString(ATTRIBUTE_MODULE);
 156     }
 157 
 158     public int getModuleOffset() {
 159         return (int)getAttribute(ATTRIBUTE_MODULE);
 160     }
 161 
 162     public String getBase() {
 163         return getAttributeString(ATTRIBUTE_BASE);
 164     }
 165 
 166     public int getBaseOffset() {
 167         return (int)getAttribute(ATTRIBUTE_BASE);
 168     }
 169 
 170     public String getParent() {
 171         return getAttributeString(ATTRIBUTE_PARENT);
 172     }
 173 
 174     public int getParentOffset() {
 175         return (int)getAttribute(ATTRIBUTE_PARENT);
 176     }
 177 
 178     public String getExtension() {
 179         return getAttributeString(ATTRIBUTE_EXTENSION);
 180     }
 181 
 182     public int getExtensionOffset() {
 183         return (int)getAttribute(ATTRIBUTE_EXTENSION);
 184     }
 185 
 186     public String getFullName() {
 187         return getFullName(false);
 188     }
 189 
 190     public String getFullName(boolean modulesPrefix) {
 191         StringBuilder builder = new StringBuilder();
 192 
 193         if (getModuleOffset() != 0) {
 194             if (modulesPrefix) {
 195                 builder.append("/modules");
 196             }
 197 
 198             builder.append('/');
 199             builder.append(getModule());
 200             builder.append('/');
 201         }
 202 
 203         if (getParentOffset() != 0) {
 204             builder.append(getParent());
 205             builder.append('/');
 206         }
 207 
 208         builder.append(getBase());
 209 
 210         if (getExtensionOffset() != 0) {
 211             builder.append('.');
 212             builder.append(getExtension());
 213         }
 214 
 215         return builder.toString();
 216     }
 217 
 218     String buildName(boolean includeModule, boolean includeParent,
 219             boolean includeName) {
 220         StringBuilder builder = new StringBuilder();
 221 
 222         if (includeModule && getModuleOffset() != 0) {
 223             builder.append("/modules/");
 224             builder.append(getModule());
 225          }
 226 
 227         if (includeParent && getParentOffset() != 0) {
 228             builder.append('/');
 229             builder.append(getParent());
 230         }
 231 
 232         if (includeName) {
 233             if (includeModule || includeParent) {
 234                 builder.append('/');
 235             }
 236 
 237             builder.append(getBase());
 238 
 239             if (getExtensionOffset() != 0) {
 240                 builder.append('.');
 241                 builder.append(getExtension());
 242             }
 243         }
 244 
 245         return builder.toString();
 246    }
 247 
 248     public long getContentOffset() {
 249         return getAttribute(ATTRIBUTE_OFFSET);
 250     }
 251 
 252     public long getCompressedSize() {
 253         return getAttribute(ATTRIBUTE_COMPRESSED);
 254     }
 255 
 256     public long getUncompressedSize() {
 257         return getAttribute(ATTRIBUTE_UNCOMPRESSED);
 258     }
 259 
 260     static ImageLocation readFrom(BasicImageReader reader, int offset) {
 261         Objects.requireNonNull(reader);
 262         long[] attributes = reader.getAttributes(offset);
 263         ImageStringsReader strings = reader.getStrings();
 264 
 265         return new ImageLocation(attributes, strings);
 266     }
 267 }