1 #include "ObjectFile.h"
5 using std::runtime_error;
10 ObjectFileHeader::ObjectFileHeader()
11 : versionId(FORMAT_ID)
30 void ObjectFileHeader::IntegrityCheck(unsigned int fsize) const {
34 || ident[3] != '\n') {
35 throw runtime_error("magic bytes mismatch");
37 if (versionId != FORMAT_ID) {
38 throw runtime_error("format ID mismatch");
40 if (!CheckSection(exportsBegin, exportsEnd, fsize)) {
41 throw runtime_error("exports section out of bounds");
43 if ((exportsEnd - exportsBegin) % sizeof(Export) != 0) {
44 throw runtime_error("bogus exports section end");
46 if (!CheckSection(externalsBegin, externalsEnd, fsize)) {
47 throw runtime_error("externals section out of bounds");
49 if ((externalsEnd - externalsBegin) % sizeof(External) != 0) {
50 throw runtime_error("bogus externals section end");
52 if (!CheckSection(imagesBegin, imagesEnd, fsize)) {
53 throw runtime_error("images section out of bounds");
55 if ((imagesEnd - imagesBegin) % sizeof(Image) != 0) {
56 throw runtime_error("bogus images section end");
58 if (!CheckSection(objectsBegin, objectsEnd, fsize)) {
59 throw runtime_error("objects section out of bounds");
61 if (!CheckSection(arraysBegin, arraysEnd, fsize)) {
62 throw runtime_error("arrays section out of bounds");
66 bool ObjectFileHeader::CheckSection(
69 unsigned int fsize) const {
70 return begin >= sizeof(ObjectFileHeader)
71 && (begin < fsize || begin == end)
76 Export *ObjectFileHeader::ExportsBegin() {
77 char *data = reinterpret_cast<char *>(this);
78 return reinterpret_cast<Export *>(data + exportsBegin);
81 Export *ObjectFileHeader::ExportsEnd() {
82 char *data = reinterpret_cast<char *>(this);
83 return reinterpret_cast<Export *>(data + exportsEnd);
86 External *ObjectFileHeader::ExternalsBegin() {
87 char *data = reinterpret_cast<char *>(this);
88 return reinterpret_cast<External *>(data + externalsBegin);
91 External *ObjectFileHeader::ExternalsEnd() {
92 char *data = reinterpret_cast<char *>(this);
93 return reinterpret_cast<External *>(data + externalsEnd);
96 Image *ObjectFileHeader::ImagesBegin() {
97 char *data = reinterpret_cast<char *>(this);
98 return reinterpret_cast<Image *>(data + imagesBegin);
101 Image *ObjectFileHeader::ImagesEnd() {
102 char *data = reinterpret_cast<char *>(this);
103 return reinterpret_cast<Image *>(data + imagesEnd);
106 Object *ObjectFileHeader::ObjectsBegin() {
107 char *data = reinterpret_cast<char *>(this);
108 return reinterpret_cast<Object *>(data + objectsBegin);
111 Object *ObjectFileHeader::ObjectsEnd() {
112 char *data = reinterpret_cast<char *>(this);
113 return reinterpret_cast<Object *>(data + objectsEnd);
116 Array *ObjectFileHeader::ArraysBegin() {
117 char *data = reinterpret_cast<char *>(this);
118 return reinterpret_cast<Array *>(data + arraysBegin);
121 Array *ObjectFileHeader::ArraysEnd() {
122 char *data = reinterpret_cast<char *>(this);
123 return reinterpret_cast<Array *>(data + arraysEnd);
126 Script *ObjectFileHeader::ScriptsBegin() {
127 char *data = reinterpret_cast<char *>(this);
128 return reinterpret_cast<Script *>(data + scriptsBegin);
131 Script *ObjectFileHeader::ScriptsEnd() {
132 char *data = reinterpret_cast<char *>(this);
133 return reinterpret_cast<Script *>(data + scriptsEnd);
137 char *Object::RawObject() {
138 return reinterpret_cast<char *>(this) + sizeof(Object);
141 Object *Object::Next() {
142 return reinterpret_cast<Object *>(RawObject() + size);
146 char *Array::Data() {
147 return reinterpret_cast<char *>(this) + sizeof(Array);
150 Array *Array::Next() {
151 return reinterpret_cast<Array *>(Data() + size);
154 char *Script::Text() {
155 return reinterpret_cast<char *>(this) + sizeof(Script);
158 Script *Script::Next() {
159 return reinterpret_cast<Script *>(Text() + size);