74
75 /*
76 * JImageClose - Given the supplied open image file (see JImageOpen), release
77 * memory and resources used by the open file and close the file. If the image
78 * file is shared by other uses, release and close is deferred until the last use
79 * is also closed.
80 *
81 * Ex.
82 * (*JImageClose)(image);
83 */
84 extern "C" JNIEXPORT void
85 JIMAGE_Close(JImageFile* image) {
86 ImageFileReader::close((ImageFileReader*) image);
87 }
88
89 /*
90 * JImageFindResource - Given an open image file (see JImageOpen), a module
91 * name, a version string and the name of a class/resource, return location
92 * information describing the resource and its size. If no resource is found, the
93 * function returns JIMAGE_NOT_FOUND and the value of size is undefined.
94 * The version number should be "9.0" and is not used in locating the resource.
95 * The resulting location does/should not have to be released.
96 * All strings are utf-8, zero byte terminated.
97 *
98 * Ex.
99 * jlong size;
100 * JImageLocationRef location = (*JImageFindResource)(image,
101 * "java.base", "9.0", "java/lang/String.class", &size);
102 */
103 extern "C" JNIEXPORT JImageLocationRef
104 JIMAGE_FindResource(JImageFile* image,
105 const char* module_name, const char* version, const char* name,
106 jlong* size) {
107 // Concatenate to get full path
108 char fullpath[IMAGE_MAX_PATH];
109 size_t moduleNameLen = strlen(module_name);
110 size_t nameLen = strlen(name);
111 size_t index;
112
113 assert(moduleNameLen > 0 && "module name must be non-empty");
114 assert(nameLen > 0 && "name must non-empty");
115
116 // If the concatenated string is too long for the buffer, return not found
117 if (1 + moduleNameLen + 1 + nameLen + 1 > IMAGE_MAX_PATH) {
118 return 0L;
119 }
120
121 index = 0;
122 fullpath[index++] = '/';
123 memcpy(&fullpath[index], module_name, moduleNameLen);
124 index += moduleNameLen;
125 fullpath[index++] = '/';
126 memcpy(&fullpath[index], name, nameLen);
127 index += nameLen;
128 fullpath[index++] = '\0';
129
130 JImageLocationRef loc =
131 (JImageLocationRef) ((ImageFileReader*) image)->find_location_index(fullpath, (u8*) size);
132 return loc;
133 }
134
135 /*
136 * JImageGetResource - Given an open image file (see JImageOpen), a resource's
137 * location information (see JImageFindResource), a buffer of appropriate
138 * size and the size, retrieve the bytes associated with the
139 * resource. If the size is less than the resource size then the read is truncated.
140 * If the size is greater than the resource size then the remainder of the buffer
141 * is zero filled. The function will return the actual size of the resource.
142 *
143 * Ex.
144 * jlong size;
145 * JImageLocationRef location = (*JImageFindResource)(image,
146 * "java.base", "9.0", "java/lang/String.class", &size);
147 * char* buffer = new char[size];
148 * (*JImageGetResource)(image, location, buffer, size);
149 */
150 extern "C" JNIEXPORT jlong
151 JIMAGE_GetResource(JImageFile* image, JImageLocationRef location,
152 char* buffer, jlong size) {
|
74
75 /*
76 * JImageClose - Given the supplied open image file (see JImageOpen), release
77 * memory and resources used by the open file and close the file. If the image
78 * file is shared by other uses, release and close is deferred until the last use
79 * is also closed.
80 *
81 * Ex.
82 * (*JImageClose)(image);
83 */
84 extern "C" JNIEXPORT void
85 JIMAGE_Close(JImageFile* image) {
86 ImageFileReader::close((ImageFileReader*) image);
87 }
88
89 /*
90 * JImageFindResource - Given an open image file (see JImageOpen), a module
91 * name, a version string and the name of a class/resource, return location
92 * information describing the resource and its size. If no resource is found, the
93 * function returns JIMAGE_NOT_FOUND and the value of size is undefined.
94 * The resulting location does/should not have to be released.
95 * All strings are utf-8, zero byte terminated.
96 *
97 * Ex.
98 * jlong size;
99 * JImageLocationRef location = (*JImageFindResource)(image,
100 * "java.base", "java/lang/String.class", is_preview_mode, &size);
101 */
102 extern "C" JNIEXPORT JImageLocationRef
103 JIMAGE_FindResource(JImageFile* image,
104 const char* module_name, const char* name, bool is_preview_mode,
105 jlong* size) {
106 static const char str_modules[] = "modules";
107 static const char str_packages[] = "packages";
108 static const char preview_infix[] = "/META-INF/preview";
109
110 size_t module_name_len = strlen(module_name);
111 size_t name_len = strlen(name);
112 size_t preview_infix_len = strlen(preview_infix);
113 assert(module_name_len > 0 && "module name must be non-empty");
114 assert(name_len > 0 && "name must non-empty");
115
116 // Do not attempt to lookup anything of the form /modules/... or /packages/...
117 if (strncmp(module_name, str_modules, sizeof(str_modules)) == 0
118 || strncmp(module_name, str_packages, sizeof(str_packages)) == 0) {
119 return 0L;
120 }
121 // If the preview mode version of the path string is too long for the buffer,
122 // return not found (even when not in preview mode).
123 if (1 + module_name_len + preview_infix_len + 1 + name_len + 1 > IMAGE_MAX_PATH) {
124 return 0L;
125 }
126
127 // Concatenate to get full path
128 char name_buffer[IMAGE_MAX_PATH];
129 char* path;
130 { // Write the buffer with room to prepend the preview mode infix
131 // at the start (saves copying the trailing name part twice).
132 size_t index = preview_infix_len;
133 name_buffer[index++] = '/';
134 memcpy(&name_buffer[index], module_name, module_name_len);
135 index += module_name_len;
136 name_buffer[index++] = '/';
137 memcpy(&name_buffer[index], name, name_len);
138 index += name_len;
139 name_buffer[index++] = '\0';
140 // Path begins at the leading '/' (not the start of the buffer).
141 path = &name_buffer[preview_infix_len];
142 }
143
144 // find_location_index() returns the data "offset", not an index.
145 const ImageFileReader* image_file = (ImageFileReader*) image;
146 u4 locOffset = image_file->find_location_index(path, (u8*) size);
147 if (locOffset != 0) {
148 ImageLocation loc;
149 loc.set_data(image_file->get_location_offset_data(locOffset));
150
151 u4 flags = loc.get_preview_flags();
152 // No preview flags means "a normal resource, without a preview version".
153 // This is the overwhelmingly common case, with or without preview mode.
154 if (flags == 0) {
155 return locOffset;
156 }
157 // Regardless of preview mode, don't return resources requested directly
158 // via their preview path.
159 if ((flags & ImageLocation::FLAGS_IS_PREVIEW_VERSION) != 0) {
160 return 0L;
161 }
162 // Even if there is a preview version, we might not want to return it.
163 if (!is_preview_mode || (flags & ImageLocation::FLAGS_HAS_PREVIEW_VERSION) == 0) {
164 return locOffset;
165 }
166 } else if (!is_preview_mode) {
167 // No normal resource found, and not in preview mode.
168 return 0L;
169 }
170
171 // We are in preview mode, and the preview version of the resource is needed.
172 // This is either because:
173 // 1. The normal resource was flagged as having a preview version (rare)
174 // 2. This is a preview-only resource (there was no normal resource, very rare)
175 // 3. The requested resource doesn't exist (this should typically not happen)
176 //
177 // Since we only expect requests for resources which exist in jimage files, we
178 // expect this 2nd lookup to succeed (this is contrary to the expectations for
179 // the JRT file system, where non-existent resource lookups are common).
180
181 { // Rewrite the front of the name buffer to make it a preview path.
182 size_t index = 0;
183 name_buffer[index++] = '/';
184 memcpy(&name_buffer[index], module_name, module_name_len);
185 index += module_name_len;
186 memcpy(&name_buffer[index], preview_infix, preview_infix_len);
187 index += preview_infix_len;
188 // Check we copied up to the expected '/' separator.
189 assert(name_buffer[index] == '/' && "bad string concatenation");
190 // The preview path now begins at the start of the buffer.
191 path = &name_buffer[0];
192 }
193 return image_file->find_location_index(path, (u8*) size);
194 }
195
196 /*
197 * JImageGetResource - Given an open image file (see JImageOpen), a resource's
198 * location information (see JImageFindResource), a buffer of appropriate
199 * size and the size, retrieve the bytes associated with the
200 * resource. If the size is less than the resource size then the read is truncated.
201 * If the size is greater than the resource size then the remainder of the buffer
202 * is zero filled. The function will return the actual size of the resource.
203 *
204 * Ex.
205 * jlong size;
206 * JImageLocationRef location = (*JImageFindResource)(image,
207 * "java.base", "9.0", "java/lang/String.class", &size);
208 * char* buffer = new char[size];
209 * (*JImageGetResource)(image, location, buffer, size);
210 */
211 extern "C" JNIEXPORT jlong
212 JIMAGE_GetResource(JImageFile* image, JImageLocationRef location,
213 char* buffer, jlong size) {
|