2007-12-01 00:00:00 +00:00
|
|
|
/*
|
2016-04-04 12:57:48 -04:00
|
|
|
* Copyright (c) 1997, 2016, Oracle and/or its affiliates. All rights reserved.
|
2007-12-01 00:00:00 +00:00
|
|
|
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
|
|
|
*
|
|
|
|
* This code is free software; you can redistribute it and/or modify it
|
|
|
|
* under the terms of the GNU General Public License version 2 only, as
|
|
|
|
* published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This code is distributed in the hope that it will be useful, but WITHOUT
|
|
|
|
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
|
|
* version 2 for more details (a copy is included in the LICENSE file that
|
|
|
|
* accompanied this code).
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License version
|
|
|
|
* 2 along with this work; if not, write to the Free Software Foundation,
|
|
|
|
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
*
|
2010-05-27 19:08:38 -07:00
|
|
|
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
|
|
|
* or visit www.oracle.com if you need additional information or have any
|
|
|
|
* questions.
|
2007-12-01 00:00:00 +00:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2010-11-23 13:22:55 -08:00
|
|
|
#include "precompiled.hpp"
|
|
|
|
#include "classfile/classFileStream.hpp"
|
|
|
|
#include "classfile/vmSymbols.hpp"
|
2016-04-04 12:57:48 -04:00
|
|
|
#include "memory/resourceArea.hpp"
|
2007-12-01 00:00:00 +00:00
|
|
|
|
2015-12-08 20:04:03 +01:00
|
|
|
const bool ClassFileStream::verify = true;
|
|
|
|
const bool ClassFileStream::no_verification = false;
|
|
|
|
|
|
|
|
void ClassFileStream::truncated_file_error(TRAPS) const {
|
2007-12-01 00:00:00 +00:00
|
|
|
THROW_MSG(vmSymbols::java_lang_ClassFormatError(), "Truncated class file");
|
|
|
|
}
|
|
|
|
|
2015-12-08 20:04:03 +01:00
|
|
|
ClassFileStream::ClassFileStream(const u1* buffer,
|
|
|
|
int length,
|
|
|
|
const char* source,
|
|
|
|
bool verify_stream) :
|
|
|
|
_buffer_start(buffer),
|
|
|
|
_buffer_end(buffer + length),
|
|
|
|
_current(buffer),
|
|
|
|
_source(source),
|
|
|
|
_need_verify(verify_stream) {}
|
|
|
|
|
|
|
|
const u1* ClassFileStream::clone_buffer() const {
|
|
|
|
u1* const new_buffer_start = NEW_RESOURCE_ARRAY(u1, length());
|
|
|
|
memcpy(new_buffer_start, _buffer_start, length());
|
|
|
|
return new_buffer_start;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char* const ClassFileStream::clone_source() const {
|
|
|
|
const char* const src = source();
|
|
|
|
char* source_copy = NULL;
|
|
|
|
if (src != NULL) {
|
|
|
|
size_t source_len = strlen(src);
|
|
|
|
source_copy = NEW_RESOURCE_ARRAY(char, source_len + 1);
|
|
|
|
strncpy(source_copy, src, source_len + 1);
|
|
|
|
}
|
|
|
|
return source_copy;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Caller responsible for ResourceMark
|
|
|
|
// clone stream with a rewound position
|
|
|
|
const ClassFileStream* ClassFileStream::clone() const {
|
|
|
|
const u1* const new_buffer_start = clone_buffer();
|
|
|
|
return new ClassFileStream(new_buffer_start,
|
|
|
|
length(),
|
|
|
|
clone_source(),
|
|
|
|
need_verify());
|
2007-12-01 00:00:00 +00:00
|
|
|
}
|
|
|
|
|
2015-12-08 20:04:03 +01:00
|
|
|
u1 ClassFileStream::get_u1(TRAPS) const {
|
2007-12-01 00:00:00 +00:00
|
|
|
if (_need_verify) {
|
|
|
|
guarantee_more(1, CHECK_0);
|
|
|
|
} else {
|
|
|
|
assert(1 <= _buffer_end - _current, "buffer overflow");
|
|
|
|
}
|
|
|
|
return *_current++;
|
|
|
|
}
|
|
|
|
|
2015-12-08 20:04:03 +01:00
|
|
|
u2 ClassFileStream::get_u2(TRAPS) const {
|
2007-12-01 00:00:00 +00:00
|
|
|
if (_need_verify) {
|
|
|
|
guarantee_more(2, CHECK_0);
|
|
|
|
} else {
|
|
|
|
assert(2 <= _buffer_end - _current, "buffer overflow");
|
|
|
|
}
|
2015-12-08 20:04:03 +01:00
|
|
|
const u1* tmp = _current;
|
2007-12-01 00:00:00 +00:00
|
|
|
_current += 2;
|
2015-12-08 20:04:03 +01:00
|
|
|
return Bytes::get_Java_u2((address)tmp);
|
2007-12-01 00:00:00 +00:00
|
|
|
}
|
|
|
|
|
2015-12-08 20:04:03 +01:00
|
|
|
u4 ClassFileStream::get_u4(TRAPS) const {
|
2007-12-01 00:00:00 +00:00
|
|
|
if (_need_verify) {
|
|
|
|
guarantee_more(4, CHECK_0);
|
|
|
|
} else {
|
|
|
|
assert(4 <= _buffer_end - _current, "buffer overflow");
|
|
|
|
}
|
2015-12-08 20:04:03 +01:00
|
|
|
const u1* tmp = _current;
|
2007-12-01 00:00:00 +00:00
|
|
|
_current += 4;
|
2015-12-08 20:04:03 +01:00
|
|
|
return Bytes::get_Java_u4((address)tmp);
|
2007-12-01 00:00:00 +00:00
|
|
|
}
|
|
|
|
|
2015-12-08 20:04:03 +01:00
|
|
|
u8 ClassFileStream::get_u8(TRAPS) const {
|
2007-12-01 00:00:00 +00:00
|
|
|
if (_need_verify) {
|
|
|
|
guarantee_more(8, CHECK_0);
|
|
|
|
} else {
|
|
|
|
assert(8 <= _buffer_end - _current, "buffer overflow");
|
|
|
|
}
|
2015-12-08 20:04:03 +01:00
|
|
|
const u1* tmp = _current;
|
2007-12-01 00:00:00 +00:00
|
|
|
_current += 8;
|
2015-12-08 20:04:03 +01:00
|
|
|
return Bytes::get_Java_u8((address)tmp);
|
2007-12-01 00:00:00 +00:00
|
|
|
}
|
|
|
|
|
2015-12-08 20:04:03 +01:00
|
|
|
void ClassFileStream::skip_u1(int length, TRAPS) const {
|
2007-12-01 00:00:00 +00:00
|
|
|
if (_need_verify) {
|
|
|
|
guarantee_more(length, CHECK);
|
|
|
|
}
|
|
|
|
_current += length;
|
|
|
|
}
|
|
|
|
|
2015-12-08 20:04:03 +01:00
|
|
|
void ClassFileStream::skip_u2(int length, TRAPS) const {
|
2007-12-01 00:00:00 +00:00
|
|
|
if (_need_verify) {
|
|
|
|
guarantee_more(length * 2, CHECK);
|
|
|
|
}
|
|
|
|
_current += length * 2;
|
|
|
|
}
|
2013-01-08 14:01:36 -05:00
|
|
|
|
2015-12-08 20:04:03 +01:00
|
|
|
void ClassFileStream::skip_u4(int length, TRAPS) const {
|
2013-01-08 14:01:36 -05:00
|
|
|
if (_need_verify) {
|
|
|
|
guarantee_more(length * 4, CHECK);
|
|
|
|
}
|
|
|
|
_current += length * 4;
|
|
|
|
}
|