341 // Read directly from the file.
342 bool ImageFileReader::read_at(u1* data, u8 size, u8 offset) const {
343 return (u8)osSupport::read(_fd, (char*)data, size, offset) == size;
344 }
345
346 // Find the location index and size associated with the path.
347 // Returns the location index and size if the location is found, 0 otherwise.
348 u4 ImageFileReader::find_location_index(const char* path, u8 *size) const {
349 // Locate the entry in the index perfect hash table.
350 s4 index = ImageStrings::find(_endian, path, _redirect_table, table_length());
351 // If found.
352 if (index != ImageStrings::NOT_FOUND) {
353 // Get address of first byte of location attribute stream.
354 u4 offset = get_location_offset(index);
355 u1* data = get_location_offset_data(offset);
356 // Expand location attributes.
357 ImageLocation location(data);
358 // Make sure result is not a false positive.
359 if (verify_location(location, path)) {
360 *size = (jlong)location.get_attribute(ImageLocation::ATTRIBUTE_UNCOMPRESSED);
361 return offset;
362 }
363 }
364 return 0; // not found
365 }
366
367 // Verify that a found location matches the supplied path (without copying.)
368 bool ImageFileReader::verify_location(ImageLocation& location, const char* path) const {
369 // Manage the image string table.
370 ImageStrings strings(_string_bytes, _header.strings_size(_endian));
371 // Position to first character of the path string.
372 const char* next = path;
373 // Get module name string.
374 const char* module = location.get_attribute(ImageLocation::ATTRIBUTE_MODULE, strings);
375 // If module string is not empty.
376 if (*module != '\0') {
377 // Compare '/module/' .
378 if (*next++ != '/') return false;
379 if (!(next = ImageStrings::starts_with(next, module))) return false;
380 if (*next++ != '/') return false;
381 }
382 // Get parent (package) string
383 const char* parent = location.get_attribute(ImageLocation::ATTRIBUTE_PARENT, strings);
384 // If parent string is not empty string.
385 if (*parent != '\0') {
386 // Compare 'parent/' .
387 if (!(next = ImageStrings::starts_with(next, parent))) return false;
388 if (*next++ != '/') return false;
389 }
390 // Get base name string.
391 const char* base = location.get_attribute(ImageLocation::ATTRIBUTE_BASE, strings);
392 // Compare with basne name.
393 if (!(next = ImageStrings::starts_with(next, base))) return false;
394 // Get extension string.
395 const char* extension = location.get_attribute(ImageLocation::ATTRIBUTE_EXTENSION, strings);
396 // If extension is not empty.
397 if (*extension != '\0') {
398 // Compare '.extension' .
399 if (*next++ != '.') return false;
400 if (!(next = ImageStrings::starts_with(next, extension))) return false;
401 }
402 // True only if complete match and no more characters.
403 return *next == '\0';
404 }
405
406 // Return the resource for the supplied location offset.
407 void ImageFileReader::get_resource(u4 offset, u1* uncompressed_data) const {
408 // Get address of first byte of location attribute stream.
409 u1* data = get_location_offset_data(offset);
410 // Expand location attributes.
411 ImageLocation location(data);
412 // Read the data
|
341 // Read directly from the file.
342 bool ImageFileReader::read_at(u1* data, u8 size, u8 offset) const {
343 return (u8)osSupport::read(_fd, (char*)data, size, offset) == size;
344 }
345
346 // Find the location index and size associated with the path.
347 // Returns the location index and size if the location is found, 0 otherwise.
348 u4 ImageFileReader::find_location_index(const char* path, u8 *size) const {
349 // Locate the entry in the index perfect hash table.
350 s4 index = ImageStrings::find(_endian, path, _redirect_table, table_length());
351 // If found.
352 if (index != ImageStrings::NOT_FOUND) {
353 // Get address of first byte of location attribute stream.
354 u4 offset = get_location_offset(index);
355 u1* data = get_location_offset_data(offset);
356 // Expand location attributes.
357 ImageLocation location(data);
358 // Make sure result is not a false positive.
359 if (verify_location(location, path)) {
360 *size = (jlong)location.get_attribute(ImageLocation::ATTRIBUTE_UNCOMPRESSED);
361 return offset;
362 }
363 }
364 return 0; // not found
365 }
366
367 // Verify that a found location matches the supplied path (without copying.)
368 bool ImageFileReader::verify_location(ImageLocation& location, const char* path) const {
369 // Manage the image string table.
370 ImageStrings strings(_string_bytes, _header.strings_size(_endian));
371 // Position to first character of the path string.
372 const char* next = path;
373 // Get module name string.
374 const char* module = location.get_attribute(ImageLocation::ATTRIBUTE_MODULE, strings);
375 // If module string is not empty.
376 if (*module != '\0') {
377 // Compare '/module/' .
378 if (*next++ != '/') return false;
379 if (!(next = ImageStrings::starts_with(next, module))) return false;
380 if (*next++ != '/') return false;
381 }
382 // Get parent (package) string
383 const char* parent = location.get_attribute(ImageLocation::ATTRIBUTE_PARENT, strings);
384 // If parent string is not empty string.
385 if (*parent != '\0') {
386 // Compare 'parent/' .
387 if (!(next = ImageStrings::starts_with(next, parent))) return false;
388 if (*next++ != '/') return false;
389 }
390 // Get base name string.
391 const char* base = location.get_attribute(ImageLocation::ATTRIBUTE_BASE, strings);
392 // Compare with base name.
393 if (!(next = ImageStrings::starts_with(next, base))) return false;
394 // Get extension string.
395 const char* extension = location.get_attribute(ImageLocation::ATTRIBUTE_EXTENSION, strings);
396 // If extension is not empty.
397 if (*extension != '\0') {
398 // Compare '.extension' .
399 if (*next++ != '.') return false;
400 if (!(next = ImageStrings::starts_with(next, extension))) return false;
401 }
402 // True only if complete match and no more characters.
403 return *next == '\0';
404 }
405
406 // Return the resource for the supplied location offset.
407 void ImageFileReader::get_resource(u4 offset, u1* uncompressed_data) const {
408 // Get address of first byte of location attribute stream.
409 u1* data = get_location_offset_data(offset);
410 // Expand location attributes.
411 ImageLocation location(data);
412 // Read the data
|