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
|
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 base 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
|