1 /*
2 * Copyright (c) 2015, 2023, Oracle and/or its affiliates. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 *
8 * - Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 *
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * - Neither the name of Oracle nor the names of its
16 * contributors may be used to endorse or promote products derived
17 * from this software without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
20 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
21 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
285 // Inflates the attribute stream into individual values stored in the long
286 // array _attributes. This allows an attribute value to be quickly accessed by
287 // direct indexing. Unspecified values default to zero.
288 void set_data(u1* data);
289
290 // Zero all attribute values.
291 void clear_data();
292
293 // Retrieve an attribute value from the inflated array.
294 inline u8 get_attribute(u1 kind) const {
295 assert(ATTRIBUTE_END < kind && kind < ATTRIBUTE_COUNT && "invalid attribute kind");
296 return _attributes[kind];
297 }
298
299 // Retrieve an attribute string value from the inflated array.
300 inline const char* get_attribute(u4 kind, const ImageStrings& strings) const {
301 return strings.get((u4)get_attribute(kind));
302 }
303 };
304
305 //
306 // Manage the image module meta data.
307 class ImageModuleData {
308 const ImageFileReader* _image_file; // Source image file
309 Endian* _endian; // Endian handler
310
311 public:
312 ImageModuleData(const ImageFileReader* image_file);
313 ~ImageModuleData();
314
315 // Return the module in which a package resides. Returns NULL if not found.
316 const char* package_to_module(const char* package_name);
317 };
318
319 // Image file header, starting at offset 0.
320 class ImageHeader {
321 private:
322 u4 _magic; // Image file marker
323 u4 _version; // Image file major version number
324 u4 _flags; // Image file flags
325 u4 _resource_count; // Number of resources in file
326 u4 _table_length; // Number of slots in index tables
327 u4 _locations_size; // Number of bytes in attribute table
328 u4 _strings_size; // Number of bytes in string table
329
330 public:
331 u4 magic() const { return _magic; }
332 u4 magic(Endian* endian) const { return endian->get(_magic); }
333 void set_magic(Endian* endian, u4 magic) { return endian->set(_magic, magic); }
334
335 u4 major_version(Endian* endian) const { return endian->get(_version) >> 16; }
336 u4 minor_version(Endian* endian) const { return endian->get(_version) & 0xFFFF; }
337 void set_version(Endian* endian, u4 major_version, u4 minor_version) {
338 return endian->set(_version, major_version << 16 | minor_version);
411 private:
412 // Manage a number of image files such that an image can be shared across
413 // multiple uses (ex. loader.)
414 static ImageFileReaderTable _reader_table;
415
416 // true if image should be fully memory mapped.
417 static bool memory_map_image;
418
419 char* _name; // Name of image
420 s4 _use; // Use count
421 int _fd; // File descriptor
422 Endian* _endian; // Endian handler
423 u8 _file_size; // File size in bytes
424 ImageHeader _header; // Image header
425 size_t _index_size; // Total size of index
426 u1* _index_data; // Raw index data
427 s4* _redirect_table; // Perfect hash redirect table
428 u4* _offsets_table; // Location offset table
429 u1* _location_bytes; // Location attributes
430 u1* _string_bytes; // String table
431 ImageModuleData *_module_data; // The ImageModuleData for this image
432
433 ImageFileReader(const char* name, bool big_endian);
434 ~ImageFileReader();
435
436 // Compute number of bytes in image file index.
437 inline size_t index_size() {
438 return sizeof(ImageHeader) +
439 table_length() * sizeof(u4) * 2 + locations_size() + strings_size();
440 }
441
442 public:
443 enum {
444 // Image file marker.
445 IMAGE_MAGIC = 0xCAFEDADA,
446 // Endian inverted Image file marker.
447 IMAGE_MAGIC_INVERT = 0xDADAFECA,
448 // Image file major version number.
449 MAJOR_VERSION = 1,
450 // Image file minor version number.
451 MINOR_VERSION = 0
560 return _endian->get(_offsets_table[index]);
561 }
562
563 // Find the location attributes associated with the path. Returns true if
564 // the location is found, false otherwise.
565 bool find_location(const char* path, ImageLocation& location) const;
566
567 // Find the location index and size associated with the path.
568 // Returns the location index and size if the location is found,
569 // ImageFileReader::NOT_FOUND otherwise.
570 u4 find_location_index(const char* path, u8 *size) const;
571
572 // Verify that a found location matches the supplied path.
573 bool verify_location(ImageLocation& location, const char* path) const;
574
575 // Return the resource for the supplied location index.
576 void get_resource(u4 index, u1* uncompressed_data) const;
577
578 // Return the resource for the supplied path.
579 void get_resource(ImageLocation& location, u1* uncompressed_data) const;
580
581 // Return the ImageModuleData for this image
582 ImageModuleData * get_image_module_data();
583
584 };
585 #endif // LIBJIMAGE_IMAGEFILE_HPP
|
1 /*
2 * Copyright (c) 2015, 2025, Oracle and/or its affiliates. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 *
8 * - Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 *
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * - Neither the name of Oracle nor the names of its
16 * contributors may be used to endorse or promote products derived
17 * from this software without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
20 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
21 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
285 // Inflates the attribute stream into individual values stored in the long
286 // array _attributes. This allows an attribute value to be quickly accessed by
287 // direct indexing. Unspecified values default to zero.
288 void set_data(u1* data);
289
290 // Zero all attribute values.
291 void clear_data();
292
293 // Retrieve an attribute value from the inflated array.
294 inline u8 get_attribute(u1 kind) const {
295 assert(ATTRIBUTE_END < kind && kind < ATTRIBUTE_COUNT && "invalid attribute kind");
296 return _attributes[kind];
297 }
298
299 // Retrieve an attribute string value from the inflated array.
300 inline const char* get_attribute(u4 kind, const ImageStrings& strings) const {
301 return strings.get((u4)get_attribute(kind));
302 }
303 };
304
305 // Image file header, starting at offset 0.
306 class ImageHeader {
307 private:
308 u4 _magic; // Image file marker
309 u4 _version; // Image file major version number
310 u4 _flags; // Image file flags
311 u4 _resource_count; // Number of resources in file
312 u4 _table_length; // Number of slots in index tables
313 u4 _locations_size; // Number of bytes in attribute table
314 u4 _strings_size; // Number of bytes in string table
315
316 public:
317 u4 magic() const { return _magic; }
318 u4 magic(Endian* endian) const { return endian->get(_magic); }
319 void set_magic(Endian* endian, u4 magic) { return endian->set(_magic, magic); }
320
321 u4 major_version(Endian* endian) const { return endian->get(_version) >> 16; }
322 u4 minor_version(Endian* endian) const { return endian->get(_version) & 0xFFFF; }
323 void set_version(Endian* endian, u4 major_version, u4 minor_version) {
324 return endian->set(_version, major_version << 16 | minor_version);
397 private:
398 // Manage a number of image files such that an image can be shared across
399 // multiple uses (ex. loader.)
400 static ImageFileReaderTable _reader_table;
401
402 // true if image should be fully memory mapped.
403 static bool memory_map_image;
404
405 char* _name; // Name of image
406 s4 _use; // Use count
407 int _fd; // File descriptor
408 Endian* _endian; // Endian handler
409 u8 _file_size; // File size in bytes
410 ImageHeader _header; // Image header
411 size_t _index_size; // Total size of index
412 u1* _index_data; // Raw index data
413 s4* _redirect_table; // Perfect hash redirect table
414 u4* _offsets_table; // Location offset table
415 u1* _location_bytes; // Location attributes
416 u1* _string_bytes; // String table
417
418 ImageFileReader(const char* name, bool big_endian);
419 ~ImageFileReader();
420
421 // Compute number of bytes in image file index.
422 inline size_t index_size() {
423 return sizeof(ImageHeader) +
424 table_length() * sizeof(u4) * 2 + locations_size() + strings_size();
425 }
426
427 public:
428 enum {
429 // Image file marker.
430 IMAGE_MAGIC = 0xCAFEDADA,
431 // Endian inverted Image file marker.
432 IMAGE_MAGIC_INVERT = 0xDADAFECA,
433 // Image file major version number.
434 MAJOR_VERSION = 1,
435 // Image file minor version number.
436 MINOR_VERSION = 0
545 return _endian->get(_offsets_table[index]);
546 }
547
548 // Find the location attributes associated with the path. Returns true if
549 // the location is found, false otherwise.
550 bool find_location(const char* path, ImageLocation& location) const;
551
552 // Find the location index and size associated with the path.
553 // Returns the location index and size if the location is found,
554 // ImageFileReader::NOT_FOUND otherwise.
555 u4 find_location_index(const char* path, u8 *size) const;
556
557 // Verify that a found location matches the supplied path.
558 bool verify_location(ImageLocation& location, const char* path) const;
559
560 // Return the resource for the supplied location index.
561 void get_resource(u4 index, u1* uncompressed_data) const;
562
563 // Return the resource for the supplied path.
564 void get_resource(ImageLocation& location, u1* uncompressed_data) const;
565 };
566 #endif // LIBJIMAGE_IMAGEFILE_HPP
|