Line data Source code
1 : /* src/vm/suck.hpp - functions to read LE ordered types from a buffer
2 :
3 : Copyright (C) 1996-2005, 2006, 2007, 2008
4 : CACAOVM - Verein zur Foerderung der freien virtuellen Maschine CACAO
5 :
6 : This file is part of CACAO.
7 :
8 : This program is free software; you can redistribute it and/or
9 : modify it under the terms of the GNU General Public License as
10 : published by the Free Software Foundation; either version 2, or (at
11 : your option) any later version.
12 :
13 : This program is distributed in the hope that it will be useful, but
14 : WITHOUT ANY WARRANTY; without even the implied warranty of
15 : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 : General Public License for more details.
17 :
18 : You should have received a copy of the GNU General Public License
19 : along with this program; if not, write to the Free Software
20 : Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 : 02110-1301, USA.
22 :
23 : */
24 :
25 :
26 : #ifndef SUCK_HPP_
27 : #define SUCK_HPP_ 1
28 :
29 : #include "config.h"
30 : #include <list>
31 :
32 : #include "toolbox/endianess.hpp"
33 : #include "vm/exceptions.hpp"
34 : #include "vm/loader.hpp"
35 : #include "vm/types.hpp"
36 :
37 : struct classinfo;
38 : struct hashtable;
39 : class Mutex;
40 : class ZipFile;
41 :
42 : /* list_classpath_entry *******************************************************/
43 :
44 : enum {
45 : CLASSPATH_PATH,
46 : CLASSPATH_ARCHIVE
47 : };
48 :
49 : struct list_classpath_entry {
50 : Mutex *mutex; /* mutex locking on zip/jar files */
51 : s4 type;
52 : char *path;
53 : s4 pathlen;
54 : #if defined(ENABLE_ZLIB)
55 : ZipFile *zip;
56 : #endif
57 : };
58 :
59 : /**
60 : * Classpath entries list.
61 : */
62 165 : class SuckClasspath : protected std::list<list_classpath_entry*> {
63 : public:
64 : void add(char *classpath);
65 : void add_from_property(const char *key);
66 :
67 : // make iterator of std::list visible
68 : using std::list<list_classpath_entry*>::iterator;
69 :
70 : // make functions of std::list visible
71 : using std::list<list_classpath_entry*>::begin;
72 : using std::list<list_classpath_entry*>::end;
73 :
74 : using std::list<list_classpath_entry*>::size;
75 : };
76 :
77 : /* classbuffer ****************************************************************/
78 :
79 : namespace cacao {
80 : struct ClassBuffer {
81 : /// Locate and load class file for class
82 : ClassBuffer(Utf8String classname);
83 : ClassBuffer(classinfo *clazz);
84 :
85 : /// Initialize with an already loaded class file
86 : ClassBuffer(classinfo *clazz, uint8_t *data, size_t sz, const char *path = NULL);
87 :
88 : /// Check if an error occured while creating this classbuffer
89 36497 : operator bool() { return data != NULL; }
90 :
91 : /// Assert that at least <sz> bytes are left to read
92 : bool check_size(size_t sz);
93 :
94 : uint8_t read_u1();
95 : uint16_t read_u2();
96 : uint32_t read_u4();
97 : uint64_t read_u8();
98 :
99 : int32_t read_s4();
100 : int64_t read_s8();
101 :
102 : float read_float();
103 : double read_double();
104 :
105 : /// Transfer block of classfile into a buffer
106 : void read_nbytes(uint8_t *dst, size_t num_bytes);
107 :
108 : /// Skip block of classfile data
109 : void skip_nbytes(size_t num_bytes);
110 :
111 : /// The number of unread bytes in the buffer
112 : size_t remaining();
113 :
114 : /// Free memory held by this classbuffer
115 : void free();
116 :
117 998547 : classinfo *get_class() const { return clazz; }
118 2045480 : const uint8_t *get_data() const { return pos; }
119 0 : const char *get_path() const { return path; }
120 :
121 : ClassFileVersion version() const;
122 : private:
123 : ClassBuffer(const ClassBuffer&);
124 : ClassBuffer& operator=(const ClassBuffer&);
125 :
126 : void init(classinfo*, uint8_t*, size_t, const char*);
127 :
128 : classinfo *clazz; // pointer to classinfo structure
129 : uint8_t *data; // pointer to start of buffer
130 : uint8_t *pos; // pointer to current position in buffer
131 : uint8_t *end; // pointer to end of buffer
132 : const char *path; // path to file (for debugging)
133 : };
134 :
135 17062778 : inline bool ClassBuffer::check_size(size_t sz) {
136 : #ifdef ENABLE_VERIFIER
137 17062778 : if (remaining() < sz) {
138 0 : exceptions_throw_classformaterror(clazz, "Truncated class file");
139 0 : return false;
140 : }
141 : #endif
142 17062778 : return true;
143 : }
144 :
145 4722225 : inline uint8_t ClassBuffer::read_u1() {
146 4722225 : uint8_t u = read_u1_be(pos);
147 4722225 : skip_nbytes(sizeof(uint8_t));
148 4722225 : return u;
149 : }
150 16062431 : inline uint16_t ClassBuffer::read_u2() {
151 16062431 : uint16_t u = read_u2_be(pos);
152 16062431 : skip_nbytes(sizeof(uint16_t));
153 16062431 : return u;
154 : }
155 1615850 : inline uint32_t ClassBuffer::read_u4() {
156 1615850 : uint32_t u = read_u4_be(pos);
157 1615850 : skip_nbytes(sizeof(uint32_t));
158 1615850 : return u;
159 : }
160 : inline uint64_t ClassBuffer::read_u8() {
161 : uint64_t u = read_u8_be(pos);
162 : skip_nbytes(sizeof(uint64_t));
163 : return u;
164 : }
165 28453 : inline int32_t ClassBuffer::read_s4() {
166 28453 : int32_t u = read_s4_be(pos);
167 28453 : skip_nbytes(sizeof(int32_t));
168 28453 : return u;
169 : }
170 10958 : inline int64_t ClassBuffer::read_s8() {
171 10958 : int64_t u = read_s8_be(pos);
172 10958 : skip_nbytes(sizeof(int64_t));
173 10958 : return u;
174 : }
175 2501 : inline float ClassBuffer::read_float() {
176 2501 : float u = read_float_be(pos);
177 2501 : skip_nbytes(sizeof(float));
178 2501 : return u;
179 : }
180 2033 : inline double ClassBuffer::read_double() {
181 2033 : double u = read_double_be(pos);
182 2033 : skip_nbytes(sizeof(double));
183 2033 : return u;
184 : }
185 :
186 398685 : inline void ClassBuffer::read_nbytes(u1 *dst, size_t num_bytes) {
187 398685 : MCOPY(dst, pos, u1, num_bytes);
188 398685 : skip_nbytes(num_bytes);
189 398685 : }
190 24895787 : inline void ClassBuffer::skip_nbytes(size_t num_bytes) {
191 24895787 : pos += num_bytes;
192 24895787 : }
193 :
194 17098566 : inline size_t ClassBuffer::remaining() {
195 17098566 : return end - pos;
196 : }
197 : }
198 :
199 : #endif // SUCK_HPP_
200 :
201 :
202 : /*
203 : * These are local overrides for various environment variables in Emacs.
204 : * Please do not remove this and leave it at the end of the file, where
205 : * Emacs will automagically detect them.
206 : * ---------------------------------------------------------------------
207 : * Local variables:
208 : * mode: c++
209 : * indent-tabs-mode: t
210 : * c-basic-offset: 4
211 : * tab-width: 4
212 : * End:
213 : * vim:noexpandtab:sw=4:ts=4:
214 : */
|