CACAO
suck.hpp
Go to the documentation of this file.
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 {
47 };
48 
50  Mutex *mutex; /* mutex locking on zip/jar files */
52  char *path;
54 #if defined(ENABLE_ZLIB)
56 #endif
57 };
58 
59 /**
60  * Classpath entries list.
61  */
62 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 
75 };
76 
77 /* classbuffer ****************************************************************/
78 
79 namespace cacao {
80  struct ClassBuffer {
81  /// Locate and load class file for class
82  ClassBuffer(Utf8String classname);
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  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  classinfo *get_class() const { return clazz; }
118  const uint8_t *get_data() const { return pos; }
119  const char *get_path() const { return path; }
120 
121  ClassFileVersion version() const;
122  private:
123  ClassBuffer(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  inline bool ClassBuffer::check_size(size_t sz) {
136 #ifdef ENABLE_VERIFIER
137  if (remaining() < sz) {
138  exceptions_throw_classformaterror(clazz, "Truncated class file");
139  return false;
140  }
141 #endif
142  return true;
143  }
144 
145  inline uint8_t ClassBuffer::read_u1() {
146  uint8_t u = read_u1_be(pos);
147  skip_nbytes(sizeof(uint8_t));
148  return u;
149  }
150  inline uint16_t ClassBuffer::read_u2() {
151  uint16_t u = read_u2_be(pos);
152  skip_nbytes(sizeof(uint16_t));
153  return u;
154  }
155  inline uint32_t ClassBuffer::read_u4() {
156  uint32_t u = read_u4_be(pos);
157  skip_nbytes(sizeof(uint32_t));
158  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  inline int32_t ClassBuffer::read_s4() {
166  int32_t u = read_s4_be(pos);
167  skip_nbytes(sizeof(int32_t));
168  return u;
169  }
170  inline int64_t ClassBuffer::read_s8() {
171  int64_t u = read_s8_be(pos);
172  skip_nbytes(sizeof(int64_t));
173  return u;
174  }
175  inline float ClassBuffer::read_float() {
176  float u = read_float_be(pos);
177  skip_nbytes(sizeof(float));
178  return u;
179  }
180  inline double ClassBuffer::read_double() {
181  double u = read_double_be(pos);
182  skip_nbytes(sizeof(double));
183  return u;
184  }
185 
186  inline void ClassBuffer::read_nbytes(u1 *dst, size_t num_bytes) {
187  MCOPY(dst, pos, u1, num_bytes);
188  skip_nbytes(num_bytes);
189  }
190  inline void ClassBuffer::skip_nbytes(size_t num_bytes) {
191  pos += num_bytes;
192  }
193 
194  inline size_t ClassBuffer::remaining() {
195  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  */
void init(classinfo *, uint8_t *, size_t, const char *)
Definition: suck.cpp:332
const char * get_path() const
Definition: suck.hpp:119
static int64_t read_s8_be(const uint8_t *src)
read int64_t from pointer big endian
Definition: endianess.hpp:135
ZipFile * zip
Definition: suck.hpp:55
ClassBuffer(Utf8String classname)
Locate and load class file for class.
void skip_nbytes(size_t num_bytes)
Skip block of classfile data.
Definition: suck.hpp:190
char * path
Definition: suck.hpp:52
static uint64_t read_u8_be(const uint8_t *src)
read uint64_t from pointer big endian
Definition: endianess.hpp:112
Dummy implementation of a mutex.
Definition: mutex-none.hpp:33
void add_from_property(const char *key)
Adds a classpath form a property entry to the global classpath entries list.
Definition: suck.cpp:186
void exceptions_throw_classformaterror(classinfo *c, const char *message,...)
Definition: exceptions.cpp:634
uint8_t u1
Definition: types.hpp:40
Definition: zip.hpp:98
uint8_t * end
Definition: suck.hpp:131
JNIEnv jthread jobject jclass jlong size
Definition: jvmti.h:387
ClassFileVersion version() const
Definition: suck.cpp:344
ClassBuffer & operator=(const ClassBuffer &)
uint16_t read_u2()
Definition: suck.hpp:150
A version of the Java class file format.
Definition: loader.hpp:108
uint8_t * data
Definition: suck.hpp:129
static uint8_t read_u1_be(const uint8_t *src)
read uint8_t from pointer big endian
Definition: endianess.hpp:93
void read_nbytes(uint8_t *dst, size_t num_bytes)
Transfer block of classfile into a buffer.
Definition: suck.hpp:186
Classpath entries list.
Definition: suck.hpp:62
uint8_t read_u1()
Definition: suck.hpp:145
uint64_t read_u8()
Definition: suck.hpp:160
int32_t read_s4()
Definition: suck.hpp:165
int32_t s4
Definition: types.hpp:45
s4 type
Definition: suck.hpp:51
static double read_double_be(const uint8_t *src)
Definition: endianess.hpp:158
Mutex * mutex
Definition: suck.hpp:50
static uint16_t read_u2_be(const uint8_t *src)
read uint16_t from pointer big endian
Definition: endianess.hpp:98
uint8_t * pos
Definition: suck.hpp:130
void free()
Free memory held by this classbuffer.
Definition: suck.cpp:438
Definition: suck.hpp:49
uint32_t read_u4()
Definition: suck.hpp:155
classinfo * get_class() const
Definition: suck.hpp:117
void add(char *classpath)
Adds a classpath to the global classpath entries list.
Definition: suck.cpp:82
static float read_float_be(const uint8_t *src)
Definition: endianess.hpp:139
s4 pathlen
Definition: suck.hpp:53
const char * path
Definition: suck.hpp:132
size_t remaining()
The number of unread bytes in the buffer.
Definition: suck.hpp:194
int64_t read_s8()
Definition: suck.hpp:170
float read_float()
Definition: suck.hpp:175
bool check_size(size_t sz)
Assert that at least &lt;sz&gt; bytes are left to read.
Definition: suck.hpp:135
const uint8_t * get_data() const
Definition: suck.hpp:118
double read_double()
Definition: suck.hpp:180
#define MCOPY(dest, src, type, num)
Definition: memory.hpp:103
static int32_t read_s4_be(const uint8_t *src)
read int32_t from pointer big endian
Definition: endianess.hpp:132
classinfo * clazz
Definition: suck.hpp:128
static uint32_t read_u4_be(const uint8_t *src)
read uint32_t from pointer big endian
Definition: endianess.hpp:104
size_t num_bytes(const uint16_t *, size_t)
Definition: utf8.cpp:463