1 /*
2 * Copyright (c) 2014, 2025, 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.nio.IntBuffer;
30 import java.util.Objects;
31
32 /**
33 * Defines the header and version information for jimage files.
34 *
35 * <p>Version number changes must be synced in a single change across all code
36 * which reads/writes jimage files, and code which tries to open a jimage file
37 * with an unexpected version should fail.
38 *
39 * <p>Known jimage file code which needs updating on version change:
40 * <ul>
41 * <li>src/java.base/share/native/libjimage/imageFile.hpp
42 * </ul>
43 *
44 * <p>Version history:
45 * <ul>
46 * <li>{@code 1.0}: Original version.
47 * <li>{@code 1.1}: Support preview mode with new flags.
48 * </ul>
49 *
50 * @implNote This class needs to maintain JDK 8 source compatibility.
51 *
52 * It is used internally in the JDK to implement jimage/jrtfs access,
53 * but also compiled and delivered as part of the jrtfs.jar to support access
54 * to the jimage file provided by the shipped JDK by tools running on JDK 8.
55 */
56 public final class ImageHeader {
57 public static final int MAGIC = 0xCAFEDADA;
58 public static final int MAJOR_VERSION = 1;
59 public static final int MINOR_VERSION = 1;
60 private static final int HEADER_SLOTS = 7;
61
62 private final int magic;
63 private final int majorVersion;
64 private final int minorVersion;
65 private final int flags;
66 private final int resourceCount;
67 private final int tableLength;
68 private final int locationsSize;
69 private final int stringsSize;
70
71 public ImageHeader(int resourceCount, int tableCount,
72 int locationsSize, int stringsSize) {
73 this(MAGIC, MAJOR_VERSION, MINOR_VERSION, 0, resourceCount,
74 tableCount, locationsSize, stringsSize);
75 }
76
77 public ImageHeader(int magic, int majorVersion, int minorVersion,
78 int flags, int resourceCount,
79 int tableLength, int locationsSize, int stringsSize)
80 {
81 this.magic = magic;
82 this.majorVersion = majorVersion;
83 this.minorVersion = minorVersion;
84 this.flags = flags;
85 this.resourceCount = resourceCount;
86 this.tableLength = tableLength;
87 this.locationsSize = locationsSize;
88 this.stringsSize = stringsSize;
89 }
90
91 public static int getHeaderSize() {
92 return HEADER_SLOTS * 4;
93 }
94
95 static ImageHeader readFrom(IntBuffer buffer) {
96 Objects.requireNonNull(buffer);
97
98 if (buffer.capacity() != HEADER_SLOTS) {
99 throw new InternalError(
100 "jimage header not the correct size: " + buffer.capacity());
101 }
102
103 int magic = buffer.get(0);
104 int version = buffer.get(1);
105 int majorVersion = version >>> 16;
106 int minorVersion = version & 0xFFFF;
107 int flags = buffer.get(2);
108 int resourceCount = buffer.get(3);
109 int tableLength = buffer.get(4);
110 int locationsSize = buffer.get(5);
111 int stringsSize = buffer.get(6);
112
113 return new ImageHeader(magic, majorVersion, minorVersion, flags,
114 resourceCount, tableLength, locationsSize, stringsSize);
115 }
116
117 public void writeTo(ImageStream stream) {
118 Objects.requireNonNull(stream);
119 stream.ensure(getHeaderSize());
120 writeTo(stream.getBuffer());
121 }
122
123 public void writeTo(ByteBuffer buffer) {
124 Objects.requireNonNull(buffer);
125 buffer.putInt(magic);
126 buffer.putInt(majorVersion << 16 | minorVersion);
127 buffer.putInt(flags);
128 buffer.putInt(resourceCount);
129 buffer.putInt(tableLength);
130 buffer.putInt(locationsSize);
131 buffer.putInt(stringsSize);
132 }
133
134 public int getMagic() {
135 return magic;
136 }
137
138 public int getMajorVersion() {
139 return majorVersion;
140 }
141
142 public int getMinorVersion() {
143 return minorVersion;
144 }
145
146 public int getFlags() {
147 return flags;
148 }
149
150 public int getResourceCount() {
151 return resourceCount;
152 }
153
154 public int getTableLength() {
155 return tableLength;
156 }
157
158 public int getRedirectSize() {
159 return tableLength * 4;
160 }
161
162 public int getOffsetsSize() {
163 return tableLength * 4;
164 }
165
166 public int getLocationsSize() {
167 return locationsSize;
168 }
169
170 public int getStringsSize() {
171 return stringsSize;
172 }
173
174 public int getIndexSize() {
175 return getHeaderSize() +
176 getRedirectSize() +
177 getOffsetsSize() +
178 getLocationsSize() +
179 getStringsSize();
180 }
181
182 int getRedirectOffset() {
183 return getHeaderSize();
184 }
185
186 int getOffsetsOffset() {
187 return getRedirectOffset() +
188 getRedirectSize();
189 }
190
191 int getLocationsOffset() {
192 return getOffsetsOffset() +
193 getOffsetsSize();
194 }
195
196 int getStringsOffset() {
197 return getLocationsOffset() +
198 getLocationsSize();
199 }
200 }