ROSE  0.11.145.0
frontend/BinaryFormats/Jvm.h
1 #ifndef Jvm_H
2 #define Jvm_H
3 #ifdef ROSE_ENABLE_BINARY_ANALYSIS
4 
5 #include <Rose/BinaryAnalysis/ByteOrder.h>
6 
7 namespace Jvm {
8 
13 template <typename T>
14 size_t read_bytes(const SgAsmJvmConstantPool* pool, char* &bytes, T &length)
15 {
16  SgAsmGenericHeader* header{pool->get_header()};
17  rose_addr_t offset{header->get_offset()};
18 
19  /* read length of the array */
20  size_t count = header->read_content(offset, &length, sizeof(length));
21  if (count != sizeof(length)) {
22  //throw FormatError("Error reading JVM bytes array length");
23  ROSE_ASSERT(false && "Error reading JVM bytes array length");
24  }
25  length = Rose::BinaryAnalysis::ByteOrder::beToHost(length);
26  offset += count;
27  header->set_offset(offset);
28 
29  /* allocate memory for array */
30  bytes = new char[length];
31 
32  /* read array */
33  count = header->read_content(offset, bytes, length);
34  if (count != length) {
35  //throw FormatError("Error reading JVM bytes array");
36  ROSE_ASSERT(false && "Error reading JVM bytes array");
37  }
38  offset += count;
39  header->set_offset(offset);
40 
41  return count;
42 }
43 
44 template <typename T>
45 size_t read_value(const SgAsmJvmConstantPool* pool, T &value, bool advanceOffset=true)
46 {
47  SgAsmGenericHeader* header{pool->get_header()};
48  rose_addr_t offset{header->get_offset()};
49 
50  size_t count = header->read_content(offset, &value, sizeof(T));
51  if (count != sizeof(T)) {
52  //throw FormatError("Error reading JVM value");
53  ROSE_ASSERT(false && "Error reading JVM value");
54  }
55  value = Rose::BinaryAnalysis::ByteOrder::beToHost(value);
56  if (advanceOffset) header->set_offset(offset + count);
57  return count;
58 }
59 
60 template <typename T>
61 void writeValue(std::ostream& os, T value)
62 {
63  Rose::BinaryAnalysis::ByteOrder::hostToBe(value, &value);
64  os.write(reinterpret_cast<const char*>(&value), sizeof value);
65 }
66 
67 } // namespace Jvm
68 
69 #endif
70 #endif
Represents an JVM constant pool.
SgAsmGenericHeader *const & get_header() const
Property: File header that owns this section.
Base class for container file headers.