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 #include "precompiled.hpp"
26 #include "classfile/classFileStream.hpp"
27 #include "classfile/classLoader.hpp"
28 #include "classfile/dictionary.hpp"
29 #include "classfile/vmSymbols.hpp"
30 #include "memory/resourceArea.hpp"
31 
32 const bool ClassFileStream::verify = true;
33 
truncated_file_error(TRAPS) const34 void ClassFileStream::truncated_file_error(TRAPS) const {
35   THROW_MSG(vmSymbols::java_lang_ClassFormatError(), "Truncated class file");
36 }
37 
ClassFileStream(const u1 * buffer,int length,const char * source,bool verify_stream,bool from_boot_loader_modules_image)38 ClassFileStream::ClassFileStream(const u1* buffer,
39                                  int length,
40                                  const char* source,
41                                  bool verify_stream,
42                                  bool from_boot_loader_modules_image) :
43   _buffer_start(buffer),
44   _buffer_end(buffer + length),
45   _current(buffer),
46   _source(source),
47   _need_verify(verify_stream),
48   _from_boot_loader_modules_image(from_boot_loader_modules_image) {}
49 
clone_buffer() const50 const u1* ClassFileStream::clone_buffer() const {
51   u1* const new_buffer_start = NEW_RESOURCE_ARRAY(u1, length());
52   memcpy(new_buffer_start, _buffer_start, length());
53   return new_buffer_start;
54 }
55 
clone_source() const56 const char* const ClassFileStream::clone_source() const {
57   const char* const src = source();
58   char* source_copy = NULL;
59   if (src != NULL) {
60     size_t source_len = strlen(src);
61     source_copy = NEW_RESOURCE_ARRAY(char, source_len + 1);
62     strncpy(source_copy, src, source_len + 1);
63   }
64   return source_copy;
65 }
66 
67 // Caller responsible for ResourceMark
68 // clone stream with a rewound position
clone() const69 const ClassFileStream* ClassFileStream::clone() const {
70   const u1* const new_buffer_start = clone_buffer();
71   return new ClassFileStream(new_buffer_start,
72                              length(),
73                              clone_source(),
74                              need_verify(),
75                              from_boot_loader_modules_image());
76 }
77 
compute_fingerprint() const78 uint64_t ClassFileStream::compute_fingerprint() const {
79   int classfile_size = length();
80   int classfile_crc = ClassLoader::crc32(0, (const char*)buffer(), length());
81   uint64_t fingerprint = (uint64_t(classfile_size) << 32) | uint64_t(uint32_t(classfile_crc));
82   assert(fingerprint != 0, "must not be zero");
83 
84   return fingerprint;
85 }
86