, exportsEnd(0)
, externalsBegin(0)
, externalsEnd(0)
+, imagesBegin(0)
+, imagesEnd(0)
, objectsBegin(0)
, objectsEnd(0)
, arraysBegin(0)
-, arraysEnd(0) {
+, arraysEnd(0)
+, scriptsBegin(0)
+, scriptsEnd(0) {
ident[0] = 'L';
ident[1] = '2';
ident[2] = 'E';
if (versionId != FORMAT_ID) {
throw runtime_error("format ID mismatch");
}
- if (exportsBegin < sizeof(ObjectFileHeader)
- || exportsBegin > fsize) {
+ if (!CheckSection(exportsBegin, exportsEnd, fsize)) {
throw runtime_error("exports section out of bounds");
}
- if (exportsEnd < exportsBegin
- || exportsEnd > fsize
- || (exportsEnd - exportsBegin) % sizeof(Export) != 0) {
+ if ((exportsEnd - exportsBegin) % sizeof(Export) != 0) {
throw runtime_error("bogus exports section end");
}
- if (externalsBegin < sizeof(ObjectFileHeader)
- || externalsBegin >= fsize) {
+ if (!CheckSection(externalsBegin, externalsEnd, fsize)) {
throw runtime_error("externals section out of bounds");
}
- if (externalsEnd < externalsBegin
- || externalsEnd > fsize
- || (externalsEnd - externalsBegin) % sizeof(External) != 0) {
+ if ((externalsEnd - externalsBegin) % sizeof(External) != 0) {
throw runtime_error("bogus externals section end");
}
- if (objectsBegin < sizeof(ObjectFileHeader)
- || objectsBegin >= fsize
- || objectsEnd < objectsBegin
- || objectsEnd > fsize) {
+ if (!CheckSection(imagesBegin, imagesEnd, fsize)) {
+ throw runtime_error("images section out of bounds");
+ }
+ if ((imagesEnd - imagesBegin) % sizeof(Image) != 0) {
+ throw runtime_error("bogus images section end");
+ }
+ if (!CheckSection(objectsBegin, objectsEnd, fsize)) {
throw runtime_error("objects section out of bounds");
}
- if (arraysBegin < sizeof(ObjectFileHeader)
- || arraysBegin >= fsize
- || arraysEnd < arraysBegin
- || arraysEnd > fsize) {
+ if (!CheckSection(arraysBegin, arraysEnd, fsize)) {
throw runtime_error("arrays section out of bounds");
}
}
+bool ObjectFileHeader::CheckSection(
+ unsigned int begin,
+ unsigned int end,
+ unsigned int fsize) const {
+ return begin >= sizeof(ObjectFileHeader)
+ && (begin < fsize || begin == end)
+ && end >= begin
+ && end <= fsize;
+}
+
Export *ObjectFileHeader::ExportsBegin() {
char *data = reinterpret_cast<char *>(this);
return reinterpret_cast<Export *>(data + exportsBegin);
return reinterpret_cast<External *>(data + externalsEnd);
}
+Image *ObjectFileHeader::ImagesBegin() {
+ char *data = reinterpret_cast<char *>(this);
+ return reinterpret_cast<Image *>(data + imagesBegin);
+}
+
+Image *ObjectFileHeader::ImagesEnd() {
+ char *data = reinterpret_cast<char *>(this);
+ return reinterpret_cast<Image *>(data + imagesEnd);
+}
+
Object *ObjectFileHeader::ObjectsBegin() {
char *data = reinterpret_cast<char *>(this);
return reinterpret_cast<Object *>(data + objectsBegin);
return reinterpret_cast<Array *>(data + arraysEnd);
}
+Script *ObjectFileHeader::ScriptsBegin() {
+ char *data = reinterpret_cast<char *>(this);
+ return reinterpret_cast<Script *>(data + scriptsBegin);
+}
+
+Script *ObjectFileHeader::ScriptsEnd() {
+ char *data = reinterpret_cast<char *>(this);
+ return reinterpret_cast<Script *>(data + scriptsEnd);
+}
+
char *Object::RawObject() {
return reinterpret_cast<char *>(this) + sizeof(Object);
return reinterpret_cast<Array *>(Data() + size);
}
+char *Script::Text() {
+ return reinterpret_cast<char *>(this) + sizeof(Script);
+}
+
+Script *Script::Next() {
+ return reinterpret_cast<Script *>(Text() + size);
+}
+
}