< prev index next >

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

Print this page

302         delete[] _name;
303         _name = NULL;
304     }
305 }
306 
307 // Open image file for read access.
308 bool ImageFileReader::open() {
309     // If file exists open for reading.
310     _fd = osSupport::openReadOnly(_name);
311     if (_fd == -1) {
312         return false;
313     }
314     // Retrieve the file size.
315     _file_size = osSupport::size(_name);
316     // Read image file header and verify it has a valid header.
317     size_t header_size = sizeof(ImageHeader);
318     if (_file_size < header_size ||
319         !read_at((u1*)&_header, header_size, 0) ||
320         _header.magic(_endian) != IMAGE_MAGIC ||
321         _header.major_version(_endian) != MAJOR_VERSION ||
322         _header.minor_version(_endian) != MINOR_VERSION) {





323         close();
324         return false;
325     }
326     // Size of image index.
327     _index_size = index_size();
328     // Make sure file is large enough to contain the index.
329     if (_file_size < _index_size) {
330         return false;
331     }
332     // Memory map image (minimally the index.)
333     _index_data = (u1*)osSupport::map_memory(_fd, _name, 0, (size_t)map_size());
334     assert(_index_data && "image file not memory mapped");
335     // Retrieve length of index perfect hash table.
336     u4 length = table_length();
337     // Compute offset of the perfect hash table redirect table.
338     u4 redirect_table_offset = (u4)header_size;
339     // Compute offset of index attribute offsets.
340     u4 offsets_table_offset = redirect_table_offset + length * (u4)sizeof(s4);
341     // Compute offset of index location attribute data.
342     u4 location_bytes_offset = offsets_table_offset + length * (u4)sizeof(u4);

387         return verify_location(location, path);
388     }
389     return false;
390 }
391 
392 // Find the location index and size associated with the path.
393 // Returns the location index and size if the location is found, 0 otherwise.
394 u4 ImageFileReader::find_location_index(const char* path, u8 *size) const {
395     // Locate the entry in the index perfect hash table.
396     s4 index = ImageStrings::find(_endian, path, _redirect_table, table_length());
397     // If found.
398     if (index != ImageStrings::NOT_FOUND) {
399         // Get address of first byte of location attribute stream.
400         u4 offset = get_location_offset(index);
401         u1* data = get_location_offset_data(offset);
402         // Expand location attributes.
403         ImageLocation location(data);
404         // Make sure result is not a false positive.
405         if (verify_location(location, path)) {
406                 *size = (jlong)location.get_attribute(ImageLocation::ATTRIBUTE_UNCOMPRESSED);
407                 return offset;
408         }
409     }
410     return 0;            // not found
411 }
412 
413 // Verify that a found location matches the supplied path (without copying.)
414 bool ImageFileReader::verify_location(ImageLocation& location, const char* path) const {
415     // Manage the image string table.
416     ImageStrings strings(_string_bytes, _header.strings_size(_endian));
417     // Position to first character of the path string.
418     const char* next = path;
419     // Get module name string.
420     const char* module = location.get_attribute(ImageLocation::ATTRIBUTE_MODULE, strings);
421     // If module string is not empty.
422     if (*module != '\0') {
423         // Compare '/module/' .
424         if (*next++ != '/') return false;
425         if (!(next = ImageStrings::starts_with(next, module))) return false;
426         if (*next++ != '/') return false;
427     }
428     // Get parent (package) string
429     const char* parent = location.get_attribute(ImageLocation::ATTRIBUTE_PARENT, strings);
430     // If parent string is not empty string.
431     if (*parent != '\0') {
432         // Compare 'parent/' .
433         if (!(next = ImageStrings::starts_with(next, parent))) return false;
434         if (*next++ != '/') return false;
435     }
436     // Get base name string.
437     const char* base = location.get_attribute(ImageLocation::ATTRIBUTE_BASE, strings);
438     // Compare with basne name.
439     if (!(next = ImageStrings::starts_with(next, base))) return false;
440     // Get extension string.
441     const char* extension = location.get_attribute(ImageLocation::ATTRIBUTE_EXTENSION, strings);
442     // If extension is not empty.
443     if (*extension != '\0') {
444         // Compare '.extension' .
445         if (*next++ != '.') return false;
446         if (!(next = ImageStrings::starts_with(next, extension))) return false;
447     }
448     // True only if complete match and no more characters.
449     return *next == '\0';
450 }
451 
452 // Return the resource for the supplied location offset.
453 void ImageFileReader::get_resource(u4 offset, u1* uncompressed_data) const {
454         // Get address of first byte of location attribute stream.
455         u1* data = get_location_offset_data(offset);
456         // Expand location attributes.
457         ImageLocation location(data);
458         // Read the data

302         delete[] _name;
303         _name = NULL;
304     }
305 }
306 
307 // Open image file for read access.
308 bool ImageFileReader::open() {
309     // If file exists open for reading.
310     _fd = osSupport::openReadOnly(_name);
311     if (_fd == -1) {
312         return false;
313     }
314     // Retrieve the file size.
315     _file_size = osSupport::size(_name);
316     // Read image file header and verify it has a valid header.
317     size_t header_size = sizeof(ImageHeader);
318     if (_file_size < header_size ||
319         !read_at((u1*)&_header, header_size, 0) ||
320         _header.magic(_endian) != IMAGE_MAGIC ||
321         _header.major_version(_endian) != MAJOR_VERSION ||
322         // Temporarily, we allow either version (1.1 or 1.0) of the file to
323         // be read so this code can be committed before image writing changes
324         // for preview mode. Preview mode changes do not modify any structure,
325         // so a 1.0 file will look like a jimage without any preview resources.
326         // TODO: Restore equality check for MINOR_VERSION.
327         _header.minor_version(_endian) > MINOR_VERSION) {
328         close();
329         return false;
330     }
331     // Size of image index.
332     _index_size = index_size();
333     // Make sure file is large enough to contain the index.
334     if (_file_size < _index_size) {
335         return false;
336     }
337     // Memory map image (minimally the index.)
338     _index_data = (u1*)osSupport::map_memory(_fd, _name, 0, (size_t)map_size());
339     assert(_index_data && "image file not memory mapped");
340     // Retrieve length of index perfect hash table.
341     u4 length = table_length();
342     // Compute offset of the perfect hash table redirect table.
343     u4 redirect_table_offset = (u4)header_size;
344     // Compute offset of index attribute offsets.
345     u4 offsets_table_offset = redirect_table_offset + length * (u4)sizeof(s4);
346     // Compute offset of index location attribute data.
347     u4 location_bytes_offset = offsets_table_offset + length * (u4)sizeof(u4);

392         return verify_location(location, path);
393     }
394     return false;
395 }
396 
397 // Find the location index and size associated with the path.
398 // Returns the location index and size if the location is found, 0 otherwise.
399 u4 ImageFileReader::find_location_index(const char* path, u8 *size) const {
400     // Locate the entry in the index perfect hash table.
401     s4 index = ImageStrings::find(_endian, path, _redirect_table, table_length());
402     // If found.
403     if (index != ImageStrings::NOT_FOUND) {
404         // Get address of first byte of location attribute stream.
405         u4 offset = get_location_offset(index);
406         u1* data = get_location_offset_data(offset);
407         // Expand location attributes.
408         ImageLocation location(data);
409         // Make sure result is not a false positive.
410         if (verify_location(location, path)) {
411                 *size = (jlong)location.get_attribute(ImageLocation::ATTRIBUTE_UNCOMPRESSED);
412             return offset;
413         }
414     }
415     return 0;            // not found
416 }
417 
418 // Verify that a found location matches the supplied path (without copying.)
419 bool ImageFileReader::verify_location(ImageLocation& location, const char* path) const {
420     // Manage the image string table.
421     ImageStrings strings(_string_bytes, _header.strings_size(_endian));
422     // Position to first character of the path string.
423     const char* next = path;
424     // Get module name string.
425     const char* module = location.get_attribute(ImageLocation::ATTRIBUTE_MODULE, strings);
426     // If module string is not empty.
427     if (*module != '\0') {
428         // Compare '/module/' .
429         if (*next++ != '/') return false;
430         if (!(next = ImageStrings::starts_with(next, module))) return false;
431         if (*next++ != '/') return false;
432     }
433     // Get parent (package) string
434     const char* parent = location.get_attribute(ImageLocation::ATTRIBUTE_PARENT, strings);
435     // If parent string is not empty string.
436     if (*parent != '\0') {
437         // Compare 'parent/' .
438         if (!(next = ImageStrings::starts_with(next, parent))) return false;
439         if (*next++ != '/') return false;
440     }
441     // Get base name string.
442     const char* base = location.get_attribute(ImageLocation::ATTRIBUTE_BASE, strings);
443     // Compare with base name.
444     if (!(next = ImageStrings::starts_with(next, base))) return false;
445     // Get extension string.
446     const char* extension = location.get_attribute(ImageLocation::ATTRIBUTE_EXTENSION, strings);
447     // If extension is not empty.
448     if (*extension != '\0') {
449         // Compare '.extension' .
450         if (*next++ != '.') return false;
451         if (!(next = ImageStrings::starts_with(next, extension))) return false;
452     }
453     // True only if complete match and no more characters.
454     return *next == '\0';
455 }
456 
457 // Return the resource for the supplied location offset.
458 void ImageFileReader::get_resource(u4 offset, u1* uncompressed_data) const {
459         // Get address of first byte of location attribute stream.
460         u1* data = get_location_offset_data(offset);
461         // Expand location attributes.
462         ImageLocation location(data);
463         // Read the data
< prev index next >