Path: blob/master/src/java.base/share/native/libjimage/imageDecompressor.hpp
41152 views
/*1* Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved.2*3* Redistribution and use in source and binary forms, with or without4* modification, are permitted provided that the following conditions5* are met:6*7* - Redistributions of source code must retain the above copyright8* notice, this list of conditions and the following disclaimer.9*10* - Redistributions in binary form must reproduce the above copyright11* notice, this list of conditions and the following disclaimer in the12* documentation and/or other materials provided with the distribution.13*14* - Neither the name of Oracle nor the names of its15* contributors may be used to endorse or promote products derived16* from this software without specific prior written permission.17*18* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS19* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,20* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR21* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR22* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,23* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,24* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR25* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF26* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING27* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS28* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.29*/3031#ifndef LIBJIMAGE_IMAGEDECOMPRESSOR_HPP32#define LIBJIMAGE_IMAGEDECOMPRESSOR_HPP3334#include <assert.h>35#include <string.h>3637#include "imageFile.hpp"38#include "inttypes.hpp"39#include "jni.h"4041/*42* Compressed resources located in image have an header.43* This header contains:44* - _magic: A magic u4, required to retrieved the header in the compressed content45* - _size: The size of the compressed resource.46* - _uncompressed_size: The uncompressed size of the compressed resource.47* - _decompressor_name_offset: The ImageDecompressor instance name StringsTable offset.48* - _decompressor_config_offset: StringsTable offset of configuration that could be needed by49* the decompressor in order to decompress.50* - _is_terminal: 1: the compressed content is terminal. Uncompressing it would51* create the actual resource. 0: the compressed content is not terminal. Uncompressing it52* will result in a compressed content to be decompressed (This occurs when a stack of compressors53* have been used to compress the resource.54*/55struct ResourceHeader {56/* magic bytes that identifies a compressed resource header*/57static const u4 resource_header_magic = 0xCAFEFAFA;58u4 _magic; // Resource header59u8 _size; // Resource size60u8 _uncompressed_size; // Expected uncompressed size61u4 _decompressor_name_offset; // Strings table decompressor offset62u4 _decompressor_config_offset; // Strings table config offset63u1 _is_terminal; // Last decompressor 1, otherwise 0.64};6566/*67* Resources located in jimage file can be compressed. Compression occurs at68* jimage file creation time. When compressed a resource is added an header that69* contains the name of the compressor that compressed it.70* Various compression strategies can be applied to compress a resource.71* The same resource can even be compressed multiple time by a stack of compressors.72* At runtime, a resource is decompressed in a loop until there is no more header73* meaning that the resource is equivalent to the not compressed resource.74* In each iteration, the name of the compressor located in the current header75* is used to retrieve the associated instance of ImageDecompressor.76* For example "zip" is the name of the compressor that compresses resources77* using the zip algorithm. The ZipDecompressor class name is also "zip".78* ImageDecompressor instances are retrieved from a static array in which79* they are registered.80*/81class ImageDecompressor {8283private:84const char* _name;8586/*87* Array of concrete decompressors. This array is used to retrieve the decompressor88* that can handle resource decompression.89*/90static ImageDecompressor** _decompressors;91/**92* Num of decompressors93*/94static int _decompressors_num;95/*96* Identifier of a decompressor. This name is the identification key to retrieve97* decompressor from a resource header.98*/99inline const char* get_name() const { return _name; }100101static u8 getU8(u1* ptr, Endian *endian);102static u4 getU4(u1* ptr, Endian *endian);103104protected:105ImageDecompressor(const char* name) : _name(name) {106}107virtual void decompress_resource(u1* data, u1* uncompressed,108ResourceHeader* header, const ImageStrings* strings) = 0;109110public:111static void image_decompressor_init();112static void image_decompressor_close();113static ImageDecompressor* get_decompressor(const char * decompressor_name) ;114static void decompress_resource(u1* compressed, u1* uncompressed,115u8 uncompressed_size, const ImageStrings* strings, Endian* _endian);116};117118/**119* Zip decompressor.120*/121class ZipDecompressor : public ImageDecompressor {122public:123ZipDecompressor(const char* sym) : ImageDecompressor(sym) { }124void decompress_resource(u1* data, u1* uncompressed, ResourceHeader* header,125const ImageStrings* strings);126static jboolean decompress(void *in, u8 inSize, void *out, u8 outSize, char **pmsg);127};128129/*130* Shared Strings decompressor. This decompressor reconstruct the class131* constant pool UTF_U entries by retrieving strings stored in jimage strings table.132* In addition, if the UTF_8 entry is a descriptor, the descriptor has to be rebuilt,133* all java type having been removed from the descriptor and added to the sting table.134* eg: "(Ljava/lang/String;I)V" ==> "(L;I)V" and "java/lang", "String"135* stored in string table. offsets to the 2 strings are compressed and stored in the136* constantpool entry.137*/138class SharedStringDecompressor : public ImageDecompressor {139private:140// the constant pool tag for UTF8 string located in strings table141static const int externalized_string = 23;142// the constant pool tag for UTF8 descriptors string located in strings table143static const int externalized_string_descriptor = 25;144// the constant pool tag for UTF8145static const int constant_utf8 = 1;146// the constant pool tag for long147static const int constant_long = 5;148// the constant pool tag for double149static const int constant_double = 6;150// array index is the constant pool tag. value is size.151// eg: array[5] = 8; means size of long is 8 bytes.152static const u1 sizes[];153// bit 5 and 6 are used to store the length of the compressed integer.154// size can be 1 (01), 2 (10), 3 (11).155// 0x60 ==> 0110000156static const int compressed_index_size_mask = 0x60;157/*158* mask the length bits (5 and 6) and move to the right 5 bits.159*/160inline static int get_compressed_length(char c) {161return ((char) (c & compressed_index_size_mask) >> 5);162}163inline static bool is_compressed(signed char b1) { return b1 < 0; }164static int decompress_int(unsigned char*& value);165public:166SharedStringDecompressor(const char* sym) : ImageDecompressor(sym){}167void decompress_resource(u1* data, u1* uncompressed, ResourceHeader* header,168const ImageStrings* strings);169};170#endif // LIBJIMAGE_IMAGEDECOMPRESSOR_HPP171172173