1 /* 2 * Copyright (c) 1997, 2019, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 * 23 */ 24 25 #ifndef SHARE_VM_CLASSFILE_CLASSFILESTREAM_HPP 26 #define SHARE_VM_CLASSFILE_CLASSFILESTREAM_HPP 27 28 #include "utilities/top.hpp" 29 #ifdef TARGET_ARCH_x86 30 # include "bytes_x86.hpp" 31 #endif 32 #ifdef TARGET_ARCH_sparc 33 # include "bytes_sparc.hpp" 34 #endif 35 #ifdef TARGET_ARCH_zero 36 # include "bytes_zero.hpp" 37 #endif 38 #ifdef TARGET_ARCH_arm 39 # include "bytes_arm.hpp" 40 #endif 41 #ifdef TARGET_ARCH_ppc 42 # include "bytes_ppc.hpp" 43 #endif 44 45 // Input stream for reading .class file 46 // 47 // The entire input stream is present in a buffer allocated by the caller. 48 // The caller is responsible for deallocating the buffer and for using 49 // ResourceMarks appropriately when constructing streams. 50 51 class ClassFileStream: public ResourceObj { 52 private: 53 u1* _buffer_start; // Buffer bottom 54 u1* _buffer_end; // Buffer top (one past last element) 55 u1* _current; // Current buffer position 56 const char* _source; // Source of stream (directory name, ZIP/JAR archive name) 57 bool _need_verify; // True if verification is on for the class file 58 59 void truncated_file_error(TRAPS); 60 protected: 61 const u1* clone_buffer() const; 62 const char* const clone_source() const; 63 public: 64 // Constructor 65 ClassFileStream(u1* buffer, int length, const char* source); 66 67 virtual const ClassFileStream* clone() const; 68 69 // Buffer access 70 u1* buffer() const { return _buffer_start; } 71 int length() const { return _buffer_end - _buffer_start; } 72 u1* current() const { return _current; } 73 void set_current(u1* pos) { _current = pos; } 74 // for relative positioning 75 juint current_offset() const { 76 return (juint)(_current - _buffer_start); 77 } 78 const char* source() const { return _source; } 79 void set_verify(bool flag) { _need_verify = flag; } 80 81 void check_truncated_file(bool b, TRAPS) { 82 if (b) { 83 truncated_file_error(THREAD); 84 } 85 } 86 87 void guarantee_more(int size, TRAPS) { 88 size_t remaining = (size_t)(_buffer_end - _current); 89 unsigned int usize = (unsigned int)size; 90 check_truncated_file(usize > remaining, CHECK); 91 } 92 93 // Read u1 from stream 94 u1 get_u1(TRAPS); 95 u1 get_u1_fast() { 96 return *_current++; 97 } 98 99 // Read u2 from stream 100 u2 get_u2(TRAPS); 101 u2 get_u2_fast() { 102 u2 res = Bytes::get_Java_u2(_current); 103 _current += 2; 104 return res; 105 } 106 107 // Read u4 from stream 108 u4 get_u4(TRAPS); 109 u4 get_u4_fast() { 110 u4 res = Bytes::get_Java_u4(_current); 111 _current += 4; 112 return res; 113 } 114 115 // Read u8 from stream 116 u8 get_u8(TRAPS); 117 u8 get_u8_fast() { 118 u8 res = Bytes::get_Java_u8(_current); 119 _current += 8; 120 return res; 121 } 122 123 // Get direct pointer into stream at current position. 124 // Returns NULL if length elements are not remaining. The caller is 125 // responsible for calling skip below if buffer contents is used. 126 u1* get_u1_buffer() { 127 return _current; 128 } 129 130 u2* get_u2_buffer() { 131 return (u2*) _current; 132 } 133 134 // Skip length u1 or u2 elements from stream 135 void skip_u1(int length, TRAPS); 136 void skip_u1_fast(int length) { 137 _current += length; 138 } 139 140 void skip_u2(int length, TRAPS); 141 void skip_u2_fast(int length) { 142 _current += 2 * length; 143 } 144 145 void skip_u4(int length, TRAPS); 146 void skip_u4_fast(int length) { 147 _current += 4 * length; 148 } 149 150 // Tells whether eos is reached 151 bool at_eos() const { return _current == _buffer_end; } 152 }; 153 154 #endif // SHARE_VM_CLASSFILE_CLASSFILESTREAM_HPP