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

Print this page




 101     // Match up the strings the best we can.
 102     while ((ch1 = *string) && (ch2 = *start)) {
 103         if (ch1 != ch2) {
 104             // Mismatch, return NULL.
 105             return NULL;
 106         }
 107         // Next characters.
 108         string++, start++;
 109     }
 110     // Return remainder of string.
 111     return string;
 112 }
 113 
 114 // Inflates the attribute stream into individual values stored in the long
 115 // array _attributes. This allows an attribute value to be quickly accessed by
 116 // direct indexing.  Unspecified values default to zero (from constructor.)
 117 void ImageLocation::set_data(u1* data) {
 118     // Deflate the attribute stream into an array of attributes.
 119     u1 byte;
 120     // Repeat until end header is found.
 121     while ((byte = *data)) {
 122         // Extract kind from header byte.
 123         u1 kind = attribute_kind(byte);
 124         assert(kind < ATTRIBUTE_COUNT && "invalid image location attribute");
 125         // Extract length of data (in bytes).
 126         u1 n = attribute_length(byte);
 127         // Read value (most significant first.)
 128         _attributes[kind] = attribute_value(data + 1, n);
 129         // Position to next attribute by skipping attribute header and data bytes.
 130         data += n + 1;
 131     }
 132 }
 133 
 134 // Zero all attribute values.
 135 void ImageLocation::clear_data() {
 136     // Set defaults to zero.
 137     memset(_attributes, 0, sizeof(_attributes));
 138 }
 139 
 140 // ImageModuleData constructor maps out sub-tables for faster access.
 141 ImageModuleData::ImageModuleData(const ImageFileReader* image_file,


 174         _mtp_redirect = (s4*)(_data + mtp_redirect_offset);
 175         // Compute the address of the module to package data.
 176         _mtp_data = (MTPData*)(_data + mtp_data_offset);
 177         // Compute the address of the module to package tables.
 178         _mtp_packages = (s4*)(_data + mtp_packages_offset);
 179     } else {
 180         // No module data present.
 181         _data = NULL;
 182         _header = NULL;
 183         _ptm_redirect = NULL;
 184         _ptm_data = NULL;
 185         _mtp_redirect = NULL;
 186         _mtp_data = NULL;
 187         _mtp_packages = NULL;
 188     }
 189 }
 190 
 191 // Release module data resource.
 192 ImageModuleData::~ImageModuleData() {
 193     if (_data) {
 194         delete _data;
 195     }
 196 }
 197 
 198 // Return the name of the module data resource.  Ex. "./lib/modules/file.jimage"
 199 // yields "file.jdata"
 200 void ImageModuleData::module_data_name(char* buffer, const char* image_file_name) {
 201     // Locate the last slash in the file name path.
 202     const char* slash = strrchr(image_file_name, FileSeparator);
 203     // Trim the path to name and extension.
 204     const char* name = slash ? slash + 1 : (char *)image_file_name;
 205     // Locate the extension period.
 206     const char* dot = strrchr(name, '.');
 207     assert(dot && "missing extension on jimage name");
 208     // Trim to only base name.
 209     int length = (int)(dot - name);
 210     strncpy(buffer, name, length);
 211     buffer[length] = '\0';
 212     // Append extension.
 213     strcat(buffer, ".jdata");
 214 }


 257             s4 package_offset = data->package_offset(_endian);
 258             for (u4 i = 0; i < count; i++) {
 259                 u4 package_name_offset = mtp_package(package_offset + i);
 260                 const char* package_name = get_string(package_name_offset);
 261                 packages[i] = package_name;
 262             }
 263             packages[count] = NULL;
 264             return packages;
 265         }
 266     }
 267     return NULL;
 268 }
 269 
 270 // Manage a table of open image files.  This table allows multiple access points
 271 // to share an open image.
 272 ImageFileReaderTable::ImageFileReaderTable() : _count(0), _max(_growth) {
 273     _table = new ImageFileReader*[_max];
 274 }
 275 
 276 ImageFileReaderTable::~ImageFileReaderTable() {
 277     delete _table;
 278 }
 279 
 280 // Add a new image entry to the table.
 281 void ImageFileReaderTable::add(ImageFileReader* image) {
 282     if (_count == _max) {
 283         _max += _growth;
 284         _table = static_cast<ImageFileReader**>(realloc(_table, _max * sizeof(ImageFileReader*)));
 285     }
 286     _table[_count++] = image;
 287 }
 288 
 289 // Remove an image entry from the table.
 290 void ImageFileReaderTable::remove(ImageFileReader* image) {
 291     s4 last = _count - 1;
 292     for (s4 i = 0; _count; i++) {
 293         if (_table[i] == image) {
 294             if (i != last) {
 295                 _table[i] = _table[last];
 296                 _count = last;
 297             }


 397 }
 398 
 399 // Constructor intializes to a closed state.
 400 ImageFileReader::ImageFileReader(const char* name, bool big_endian) {
 401     // Copy the image file name.
 402      int len = (int) strlen(name) + 1;
 403     _name = new char[len];
 404     strncpy(_name, name, len);
 405     // Initialize for a closed file.
 406     _fd = -1;
 407     _endian = Endian::get_handler(big_endian);
 408     _index_data = NULL;
 409 }
 410 
 411 // Close image and free up data structures.
 412 ImageFileReader::~ImageFileReader() {
 413     // Ensure file is closed.
 414     close();
 415     // Free up name.
 416     if (_name) {
 417         delete _name;
 418         _name = NULL;
 419     }
 420 }
 421 
 422 // Open image file for read access.
 423 bool ImageFileReader::open() {
 424     char buffer[IMAGE_MAX_PATH];
 425 
 426     // If file exists open for reading.
 427     _fd = osSupport::openReadOnly(_name);
 428     if (_fd == -1) {
 429         return false;
 430     }
 431     // Retrieve the file size.
 432     _file_size = osSupport::size(_name);
 433     // Read image file header and verify it has a valid header.
 434     size_t header_size = sizeof(ImageHeader);
 435     if (_file_size < header_size ||
 436         !read_at((u1*)&_header, header_size, 0) ||
 437         _header.magic(_endian) != IMAGE_MAGIC ||


 651     // If the resource is compressed.
 652     if (compressed_size != 0) {
 653         u1* compressed_data;
 654         // If not memory mapped read in bytes.
 655         if (!MemoryMapImage) {
 656             // Allocate buffer for compression.
 657             compressed_data = new u1[(u4)compressed_size];
 658             // Read bytes from offset beyond the image index.
 659             bool is_read = read_at(compressed_data, compressed_size, _index_size + offset);
 660             assert(is_read && "error reading from image or short read");
 661         } else {
 662             compressed_data = get_data_address() + offset;
 663         }
 664         // Get image string table.
 665         const ImageStrings strings = get_strings();
 666         // Decompress resource.
 667         ImageDecompressor::decompress_resource(compressed_data, uncompressed_data, (u4)uncompressed_size,
 668                         &strings);
 669         // If not memory mapped then release temporary buffer.
 670         if (!MemoryMapImage) {
 671                 delete compressed_data;
 672         }
 673     } else {
 674         // Read bytes from offset beyond the image index.
 675         bool is_read = read_at(uncompressed_data, uncompressed_size, _index_size + offset);
 676         assert(is_read && "error reading from image or short read");
 677     }
 678 }
 679 
 680 // Return the ImageModuleData for this image
 681 ImageModuleData * ImageFileReader::get_image_module_data() {
 682         return module_data;
 683 }


 101     // Match up the strings the best we can.
 102     while ((ch1 = *string) && (ch2 = *start)) {
 103         if (ch1 != ch2) {
 104             // Mismatch, return NULL.
 105             return NULL;
 106         }
 107         // Next characters.
 108         string++, start++;
 109     }
 110     // Return remainder of string.
 111     return string;
 112 }
 113 
 114 // Inflates the attribute stream into individual values stored in the long
 115 // array _attributes. This allows an attribute value to be quickly accessed by
 116 // direct indexing.  Unspecified values default to zero (from constructor.)
 117 void ImageLocation::set_data(u1* data) {
 118     // Deflate the attribute stream into an array of attributes.
 119     u1 byte;
 120     // Repeat until end header is found.
 121     while ((data != NULL) && (byte = *data)) {
 122         // Extract kind from header byte.
 123         u1 kind = attribute_kind(byte);
 124         assert(kind < ATTRIBUTE_COUNT && "invalid image location attribute");
 125         // Extract length of data (in bytes).
 126         u1 n = attribute_length(byte);
 127         // Read value (most significant first.)
 128         _attributes[kind] = attribute_value(data + 1, n);
 129         // Position to next attribute by skipping attribute header and data bytes.
 130         data += n + 1;
 131     }
 132 }
 133 
 134 // Zero all attribute values.
 135 void ImageLocation::clear_data() {
 136     // Set defaults to zero.
 137     memset(_attributes, 0, sizeof(_attributes));
 138 }
 139 
 140 // ImageModuleData constructor maps out sub-tables for faster access.
 141 ImageModuleData::ImageModuleData(const ImageFileReader* image_file,


 174         _mtp_redirect = (s4*)(_data + mtp_redirect_offset);
 175         // Compute the address of the module to package data.
 176         _mtp_data = (MTPData*)(_data + mtp_data_offset);
 177         // Compute the address of the module to package tables.
 178         _mtp_packages = (s4*)(_data + mtp_packages_offset);
 179     } else {
 180         // No module data present.
 181         _data = NULL;
 182         _header = NULL;
 183         _ptm_redirect = NULL;
 184         _ptm_data = NULL;
 185         _mtp_redirect = NULL;
 186         _mtp_data = NULL;
 187         _mtp_packages = NULL;
 188     }
 189 }
 190 
 191 // Release module data resource.
 192 ImageModuleData::~ImageModuleData() {
 193     if (_data) {
 194         delete[] _data;
 195     }
 196 }
 197 
 198 // Return the name of the module data resource.  Ex. "./lib/modules/file.jimage"
 199 // yields "file.jdata"
 200 void ImageModuleData::module_data_name(char* buffer, const char* image_file_name) {
 201     // Locate the last slash in the file name path.
 202     const char* slash = strrchr(image_file_name, FileSeparator);
 203     // Trim the path to name and extension.
 204     const char* name = slash ? slash + 1 : (char *)image_file_name;
 205     // Locate the extension period.
 206     const char* dot = strrchr(name, '.');
 207     assert(dot && "missing extension on jimage name");
 208     // Trim to only base name.
 209     int length = (int)(dot - name);
 210     strncpy(buffer, name, length);
 211     buffer[length] = '\0';
 212     // Append extension.
 213     strcat(buffer, ".jdata");
 214 }


 257             s4 package_offset = data->package_offset(_endian);
 258             for (u4 i = 0; i < count; i++) {
 259                 u4 package_name_offset = mtp_package(package_offset + i);
 260                 const char* package_name = get_string(package_name_offset);
 261                 packages[i] = package_name;
 262             }
 263             packages[count] = NULL;
 264             return packages;
 265         }
 266     }
 267     return NULL;
 268 }
 269 
 270 // Manage a table of open image files.  This table allows multiple access points
 271 // to share an open image.
 272 ImageFileReaderTable::ImageFileReaderTable() : _count(0), _max(_growth) {
 273     _table = new ImageFileReader*[_max];
 274 }
 275 
 276 ImageFileReaderTable::~ImageFileReaderTable() {
 277     delete[] _table;
 278 }
 279 
 280 // Add a new image entry to the table.
 281 void ImageFileReaderTable::add(ImageFileReader* image) {
 282     if (_count == _max) {
 283         _max += _growth;
 284         _table = static_cast<ImageFileReader**>(realloc(_table, _max * sizeof(ImageFileReader*)));
 285     }
 286     _table[_count++] = image;
 287 }
 288 
 289 // Remove an image entry from the table.
 290 void ImageFileReaderTable::remove(ImageFileReader* image) {
 291     s4 last = _count - 1;
 292     for (s4 i = 0; _count; i++) {
 293         if (_table[i] == image) {
 294             if (i != last) {
 295                 _table[i] = _table[last];
 296                 _count = last;
 297             }


 397 }
 398 
 399 // Constructor intializes to a closed state.
 400 ImageFileReader::ImageFileReader(const char* name, bool big_endian) {
 401     // Copy the image file name.
 402      int len = (int) strlen(name) + 1;
 403     _name = new char[len];
 404     strncpy(_name, name, len);
 405     // Initialize for a closed file.
 406     _fd = -1;
 407     _endian = Endian::get_handler(big_endian);
 408     _index_data = NULL;
 409 }
 410 
 411 // Close image and free up data structures.
 412 ImageFileReader::~ImageFileReader() {
 413     // Ensure file is closed.
 414     close();
 415     // Free up name.
 416     if (_name) {
 417         delete[] _name;
 418         _name = NULL;
 419     }
 420 }
 421 
 422 // Open image file for read access.
 423 bool ImageFileReader::open() {
 424     char buffer[IMAGE_MAX_PATH];
 425 
 426     // If file exists open for reading.
 427     _fd = osSupport::openReadOnly(_name);
 428     if (_fd == -1) {
 429         return false;
 430     }
 431     // Retrieve the file size.
 432     _file_size = osSupport::size(_name);
 433     // Read image file header and verify it has a valid header.
 434     size_t header_size = sizeof(ImageHeader);
 435     if (_file_size < header_size ||
 436         !read_at((u1*)&_header, header_size, 0) ||
 437         _header.magic(_endian) != IMAGE_MAGIC ||


 651     // If the resource is compressed.
 652     if (compressed_size != 0) {
 653         u1* compressed_data;
 654         // If not memory mapped read in bytes.
 655         if (!MemoryMapImage) {
 656             // Allocate buffer for compression.
 657             compressed_data = new u1[(u4)compressed_size];
 658             // Read bytes from offset beyond the image index.
 659             bool is_read = read_at(compressed_data, compressed_size, _index_size + offset);
 660             assert(is_read && "error reading from image or short read");
 661         } else {
 662             compressed_data = get_data_address() + offset;
 663         }
 664         // Get image string table.
 665         const ImageStrings strings = get_strings();
 666         // Decompress resource.
 667         ImageDecompressor::decompress_resource(compressed_data, uncompressed_data, (u4)uncompressed_size,
 668                         &strings);
 669         // If not memory mapped then release temporary buffer.
 670         if (!MemoryMapImage) {
 671                 delete[] compressed_data;
 672         }
 673     } else {
 674         // Read bytes from offset beyond the image index.
 675         bool is_read = read_at(uncompressed_data, uncompressed_size, _index_size + offset);
 676         assert(is_read && "error reading from image or short read");
 677     }
 678 }
 679 
 680 // Return the ImageModuleData for this image
 681 ImageModuleData * ImageFileReader::get_image_module_data() {
 682         return module_data;
 683 }