< prev index next >

src/java.base/share/classes/jdk/internal/jimage/ImageLocation.java

Print this page
rev 14631 : 8156209: Add argument checks to BasicImageReader calls
Reviewed-by: sundar


   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 
  30 /**
  31  * @implNote This class needs to maintain JDK 8 source compatibility.
  32  *
  33  * It is used internally in the JDK to implement jimage/jrtfs access,
  34  * but also compiled and delivered as part of the jrtfs.jar to support access
  35  * to the jimage file provided by the shipped JDK by tools running on JDK 8.
  36  */
  37 public class ImageLocation {
  38     public static final int ATTRIBUTE_END = 0;
  39     public static final int ATTRIBUTE_MODULE = 1;
  40     public static final int ATTRIBUTE_PARENT = 2;
  41     public static final int ATTRIBUTE_BASE = 3;
  42     public static final int ATTRIBUTE_EXTENSION = 4;
  43     public static final int ATTRIBUTE_OFFSET = 5;
  44     public static final int ATTRIBUTE_COMPRESSED = 6;
  45     public static final int ATTRIBUTE_UNCOMPRESSED = 7;
  46     public static final int ATTRIBUTE_COUNT = 8;
  47 
  48     protected final long[] attributes;
  49 
  50     protected final ImageStrings strings;
  51 
  52     public ImageLocation(long[] attributes, ImageStrings strings) {


  53         this.attributes = attributes;
  54         this.strings = strings;
  55     }
  56 
  57     ImageStrings getStrings() {
  58         return strings;
  59     }
  60 
  61     private static int attributeLength(int data) {
  62         return (data & 0x7) + 1;
  63     }
  64 
  65     private static int attributeKind(int data) {
  66         return data >>> 3;
  67     }
  68 
  69     static long[] decompress(ByteBuffer bytes) {

  70         long[] attributes = new long[ATTRIBUTE_COUNT];
  71 
  72         if (bytes != null) {
  73             while (bytes.hasRemaining()) {
  74                 int data = bytes.get() & 0xFF;
  75                 int kind = attributeKind(data);
  76 
  77                 if (kind == ATTRIBUTE_END) {
  78                     break;
  79                 }
  80 
  81                 if (kind < ATTRIBUTE_END || ATTRIBUTE_COUNT <= kind) {
  82                     throw new InternalError("Invalid jimage attribute kind");
  83                 }
  84 
  85                 int length = attributeLength(data);
  86                 long value = 0;
  87 
  88                 for (int j = 0; j < length; j++) {
  89                     value <<= 8;
  90 
  91                     if (!bytes.hasRemaining()) {
  92                         throw new InternalError("\"Missing jimage attribute datad");
  93                     }
  94 
  95                     value |= bytes.get() & 0xFF;
  96                 }
  97 
  98                  attributes[kind] = value;
  99             }
 100         }
 101 
 102         return attributes;
 103     }
 104 
 105     public static byte[] compress(long[] attributes) {

 106         ImageStream stream = new ImageStream(16);
 107 
 108         for (int kind = ATTRIBUTE_END + 1; kind < ATTRIBUTE_COUNT; kind++) {
 109             long value = attributes[kind];
 110 
 111             if (value != 0) {
 112                 int n = (63 - Long.numberOfLeadingZeros(value)) >> 3;
 113                 stream.put((kind << 3) | n);
 114 
 115                 for (int i = n; i >= 0; i--) {
 116                     stream.put((int)(value >> (i << 3)));
 117                 }
 118             }
 119         }
 120 
 121         stream.put(ATTRIBUTE_END << 3);
 122 
 123         return stream.toArray();
 124      }
 125 
 126     public boolean verify(String name) {


 127         return name.equals(getFullName());
 128     }
 129 
 130     long getAttribute(int kind) {
 131         if (kind < ATTRIBUTE_END || ATTRIBUTE_COUNT <= kind) {
 132             throw new InternalError("Invalid jimage attribute kind");
 133         }
 134 
 135         return attributes[kind];
 136     }
 137 
 138     String getAttributeString(int kind) {
 139         if (kind < ATTRIBUTE_END || ATTRIBUTE_COUNT <= kind) {
 140             throw new InternalError("Invalid jimage attribute kind");
 141         }
 142 
 143         return getStrings().get((int)attributes[kind]);
 144     }
 145 
 146     public String getModule() {


 233                 builder.append(getExtension());
 234             }
 235         }
 236 
 237         return builder.toString();
 238    }
 239 
 240     public long getContentOffset() {
 241         return getAttribute(ATTRIBUTE_OFFSET);
 242     }
 243 
 244     public long getCompressedSize() {
 245         return getAttribute(ATTRIBUTE_COMPRESSED);
 246     }
 247 
 248     public long getUncompressedSize() {
 249         return getAttribute(ATTRIBUTE_UNCOMPRESSED);
 250     }
 251 
 252     static ImageLocation readFrom(BasicImageReader reader, int offset) {

 253         long[] attributes = reader.getAttributes(offset);
 254         ImageStringsReader strings = reader.getStrings();
 255 
 256         return new ImageLocation(attributes, strings);
 257     }
 258 }


   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         Objects.requireNonNull(attributes);
  55         Objects.requireNonNull(strings);
  56         this.attributes = attributes;
  57         this.strings = strings;
  58     }
  59 
  60     ImageStrings getStrings() {
  61         return strings;
  62     }
  63 
  64     private static int attributeLength(int data) {
  65         return (data & 0x7) + 1;
  66     }
  67 
  68     private static int attributeKind(int data) {
  69         return data >>> 3;
  70     }
  71 
  72     static long[] decompress(ByteBuffer bytes) {
  73         Objects.requireNonNull(bytes);
  74         long[] attributes = new long[ATTRIBUTE_COUNT];
  75 
  76         if (bytes != null) {
  77             while (bytes.hasRemaining()) {
  78                 int data = bytes.get() & 0xFF;
  79                 int kind = attributeKind(data);
  80 
  81                 if (kind == ATTRIBUTE_END) {
  82                     break;
  83                 }
  84 
  85                 if (kind < ATTRIBUTE_END || ATTRIBUTE_COUNT <= kind) {
  86                     throw new InternalError("Invalid jimage attribute kind");
  87                 }
  88 
  89                 int length = attributeLength(data);
  90                 long value = 0;
  91 
  92                 for (int j = 0; j < length; j++) {
  93                     value <<= 8;
  94 
  95                     if (!bytes.hasRemaining()) {
  96                         throw new InternalError("\"Missing jimage attribute datad");
  97                     }
  98 
  99                     value |= bytes.get() & 0xFF;
 100                 }
 101 
 102                  attributes[kind] = value;
 103             }
 104         }
 105 
 106         return attributes;
 107     }
 108 
 109     public static byte[] compress(long[] attributes) {
 110         Objects.requireNonNull(attributes);
 111         ImageStream stream = new ImageStream(16);
 112 
 113         for (int kind = ATTRIBUTE_END + 1; kind < ATTRIBUTE_COUNT; kind++) {
 114             long value = attributes[kind];
 115 
 116             if (value != 0) {
 117                 int n = (63 - Long.numberOfLeadingZeros(value)) >> 3;
 118                 stream.put((kind << 3) | n);
 119 
 120                 for (int i = n; i >= 0; i--) {
 121                     stream.put((int)(value >> (i << 3)));
 122                 }
 123             }
 124         }
 125 
 126         stream.put(ATTRIBUTE_END << 3);
 127 
 128         return stream.toArray();
 129      }
 130 
 131     public boolean verify(String name) {
 132         Objects.requireNonNull(name);
 133         
 134         return name.equals(getFullName());
 135     }
 136 
 137     long getAttribute(int kind) {
 138         if (kind < ATTRIBUTE_END || ATTRIBUTE_COUNT <= kind) {
 139             throw new InternalError("Invalid jimage attribute 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("Invalid jimage attribute kind");
 148         }
 149 
 150         return getStrings().get((int)attributes[kind]);
 151     }
 152 
 153     public String getModule() {


 240                 builder.append(getExtension());
 241             }
 242         }
 243 
 244         return builder.toString();
 245    }
 246 
 247     public long getContentOffset() {
 248         return getAttribute(ATTRIBUTE_OFFSET);
 249     }
 250 
 251     public long getCompressedSize() {
 252         return getAttribute(ATTRIBUTE_COMPRESSED);
 253     }
 254 
 255     public long getUncompressedSize() {
 256         return getAttribute(ATTRIBUTE_UNCOMPRESSED);
 257     }
 258 
 259     static ImageLocation readFrom(BasicImageReader reader, int offset) {
 260         Objects.requireNonNull(reader);
 261         long[] attributes = reader.getAttributes(offset);
 262         ImageStringsReader strings = reader.getStrings();
 263 
 264         return new ImageLocation(attributes, strings);
 265     }
 266 }
< prev index next >