1 /* 2 * Copyright (c) 1997, 2016, 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 #include "precompiled.hpp" 26 #include "classfile/classFileStream.hpp" 27 #include "classfile/vmSymbols.hpp" 28 #include "memory/resourceArea.hpp" 29 30 const bool ClassFileStream::verify = true; 31 const bool ClassFileStream::no_verification = false; 32 33 void ClassFileStream::truncated_file_error(TRAPS) const { 34 THROW_MSG(vmSymbols::java_lang_ClassFormatError(), "Truncated class file"); 35 } 36 37 ClassFileStream::ClassFileStream(const u1* buffer, 38 int length, 39 const char* source, 40 bool verify_stream) : 41 _buffer_start(buffer), 42 _buffer_end(buffer + length), 43 _current(buffer), 44 _source(source), 45 _need_verify(verify_stream) {} 46 47 const u1* ClassFileStream::clone_buffer() const { 48 u1* const new_buffer_start = NEW_RESOURCE_ARRAY(u1, length()); 49 memcpy(new_buffer_start, _buffer_start, length()); 50 return new_buffer_start; 51 } 52 53 const char* const ClassFileStream::clone_source() const { 54 const char* const src = source(); 55 char* source_copy = NULL; 56 if (src != NULL) { 57 size_t source_len = strlen(src); 58 source_copy = NEW_RESOURCE_ARRAY(char, source_len + 1); 59 strncpy(source_copy, src, source_len + 1); 60 } 61 return source_copy; 62 } 63 64 // Caller responsible for ResourceMark 65 // clone stream with a rewound position 66 const ClassFileStream* ClassFileStream::clone() const { 67 const u1* const new_buffer_start = clone_buffer(); 68 return new ClassFileStream(new_buffer_start, 69 length(), 70 clone_source(), 71 need_verify()); 72 } 73 74 u1 ClassFileStream::get_u1(TRAPS) const { 75 if (_need_verify) { 76 guarantee_more(1, CHECK_0); 77 } else { 78 assert(1 <= _buffer_end - _current, "buffer overflow"); 79 } 80 return *_current++; 81 } 82 83 u2 ClassFileStream::get_u2(TRAPS) const { 84 if (_need_verify) { 85 guarantee_more(2, CHECK_0); 86 } else { 87 assert(2 <= _buffer_end - _current, "buffer overflow"); 88 } 89 const u1* tmp = _current; 90 _current += 2; 91 return Bytes::get_Java_u2((address)tmp); 92 } 93 94 u4 ClassFileStream::get_u4(TRAPS) const { 95 if (_need_verify) { 96 guarantee_more(4, CHECK_0); 97 } else { 98 assert(4 <= _buffer_end - _current, "buffer overflow"); 99 } 100 const u1* tmp = _current; 101 _current += 4; 102 return Bytes::get_Java_u4((address)tmp); 103 } 104 105 u8 ClassFileStream::get_u8(TRAPS) const { 106 if (_need_verify) { 107 guarantee_more(8, CHECK_0); 108 } else { 109 assert(8 <= _buffer_end - _current, "buffer overflow"); 110 } 111 const u1* tmp = _current; 112 _current += 8; 113 return Bytes::get_Java_u8((address)tmp); 114 } 115 116 void ClassFileStream::skip_u1(int length, TRAPS) const { 117 if (_need_verify) { 118 guarantee_more(length, CHECK); 119 } 120 _current += length; 121 } 122 123 void ClassFileStream::skip_u2(int length, TRAPS) const { 124 if (_need_verify) { 125 guarantee_more(length * 2, CHECK); 126 } 127 _current += length * 2; 128 } 129 130 void ClassFileStream::skip_u4(int length, TRAPS) const { 131 if (_need_verify) { 132 guarantee_more(length * 4, CHECK); 133 } 134 _current += length * 4; 135 }