< prev index next >

src/java.base/share/native/libjimage/jimage.cpp

Print this page

 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     // TBD:   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 
114     // TBD:   assert(module_name_len > 0, "module name must be non-empty");
115     assert(name_len > 0 && "resource name must be non-empty");
116 
117     // Do not attempt to lookup anything of the form /modules/... or /packages/...
118     if (strncmp(module_name, str_modules, sizeof(str_modules)) == 0
119             || strncmp(module_name, str_packages, sizeof(str_packages)) == 0) {
120         return 0L;
121     }
122     // If the preview mode version of the path string is too long for the buffer,
123     // return not found (even when not in preview mode).
124     if (1 + module_name_len + preview_infix_len + 1 + name_len + 1 > IMAGE_MAX_PATH) {
125         return 0L;
126     }
127 
128     // Concatenate to get full path
129     char name_buffer[IMAGE_MAX_PATH];
130     char* path;
131     {   // Write the buffer with room to prepend the preview mode infix
132         // at the start (saves copying the trailing name part twice).
133         size_t index = preview_infix_len;
134         name_buffer[index++] = '/';
135         memcpy(&name_buffer[index], module_name, module_name_len);
136         index += module_name_len;
137         name_buffer[index++] = '/';
138         memcpy(&name_buffer[index], name, name_len);
139         index += name_len;
140         name_buffer[index++] = '\0';
141         // Path begins at the leading '/' (not the start of the buffer).
142         path = &name_buffer[preview_infix_len];
143     }
144 
145     // find_location_index() returns the data "offset", not an index.
146     const ImageFileReader* image_file = (ImageFileReader*) image;
147     u4 locOffset = image_file->find_location_index(path, (u8*) size);
148     if (locOffset != 0) {
149         ImageLocation loc;
150         loc.set_data(image_file->get_location_offset_data(locOffset));
151 
152         u4 flags = loc.get_preview_flags();
153         // No preview flags means "a normal resource, without a preview version".
154         // This is the overwhelmingly common case, with or without preview mode.
155         if (flags == 0) {
156             return locOffset;
157         }
158         // Regardless of preview mode, don't return resources requested directly
159         // via their preview path.
160         if ((flags & ImageLocation::FLAGS_IS_PREVIEW_VERSION) != 0) {
161             return 0L;
162         }
163         // Even if there is a preview version, we might not want to return it.
164         if (!is_preview_mode || (flags & ImageLocation::FLAGS_HAS_PREVIEW_VERSION) == 0) {
165             return locOffset;
166         }
167     } else if (!is_preview_mode) {
168         // No normal resource found, and not in preview mode.
169         return 0L;
170     }
171 
172     // We are in preview mode, and the preview version of the resource is needed.
173     // This is either because:
174     // 1. The normal resource was flagged as having a preview version (rare)
175     // 2. This is a preview-only resource (there was no normal resource, very rare)
176     // 3. The requested resource doesn't exist (this should typically not happen)
177     //
178     // Since we only expect requests for resources which exist in jimage files, we
179     // expect this 2nd lookup to succeed (this is contrary to the expectations for
180     // the JRT file system, where non-existent resource lookups are common).
181 
182     {   // Rewrite the front of the name buffer to make it a preview path.
183         size_t index = 0;
184         name_buffer[index++] = '/';
185         memcpy(&name_buffer[index], module_name, module_name_len);
186         index += module_name_len;
187         memcpy(&name_buffer[index], preview_infix, preview_infix_len);
188         index += preview_infix_len;
189         // Check we copied up to the expected '/' separator.
190         assert(name_buffer[index] == '/' && "bad string concatenation");
191         // The preview path now begins at the start of the buffer.
192         path = &name_buffer[0];
193     }
194     return image_file->find_location_index(path, (u8*) size);
195 }
196 
197 /*
198  * JImageGetResource - Given an open image file (see JImageOpen), a resource's
199  * location information (see JImageFindResource), a buffer of appropriate
200  * size and the size, retrieve the bytes associated with the
201  * resource. If the size is less than the resource size then the read is truncated.
202  * If the size is greater than the resource size then the remainder of the buffer
203  * is zero filled.  The function will return the actual size of the resource.
204  *
205  * Ex.
206  *  jlong size;
207  *   JImageLocationRef location = (*JImageFindResource)(image,
208  *                                 "java.base", "9.0", "java/lang/String.class", &size);
209  *  char* buffer = new char[size];
210  *  (*JImageGetResource)(image, location, buffer, size);
211  */
212 extern "C" JNIEXPORT jlong
213 JIMAGE_GetResource(JImageFile* image, JImageLocationRef location,
214         char* buffer, jlong size) {
< prev index next >