DEADSOFTWARE

Testing odcread against a bunch of existing .odc files
[odcread.git] / reader.cc
1 #include <reader.h>
2 #include <alien.h>
4 #include <string>
5 #include <assert.h>
7 namespace odc {
9 Reader::Reader(std::istream &rider): d_rider(rider), d_cancelled(false), d_readAlien(false), d_typeList(),
10 d_state(new ReaderState()) {}
12 SHORTCHAR Reader::readSChar() {
13 SHORTCHAR out;
14 d_rider.read(&out, 1);
15 return out;
16 }
18 void Reader::readSChar(SHORTCHAR *buf, size_t len) {
19 d_rider.read(buf, len);
20 }
22 CHAR Reader::readLChar() {
23 CHAR buf;
24 char *bufPtr = (char *)&buf;
25 d_rider.read(bufPtr, 2);
26 if (isLittleEndian()) {
27 return buf;
28 } else {
29 CHAR out;
30 char *outPtr = (char *)&out;
31 outPtr[0] = bufPtr[1]; outPtr[1] = bufPtr[0];
32 return out;
33 }
34 }
36 void Reader::readLChar(CHAR *buf, size_t len) {
37 char *bufPtr = (char *)buf;
38 int len2 = len * 2;
39 d_rider.read(bufPtr, len2);
40 if (isBigEndian()) {
41 char tmp;
42 for (int i = 0; i < len2; i += 2) {
43 tmp = bufPtr[i];
44 bufPtr[i] = bufPtr[i + 1];
45 bufPtr[i + 1] = tmp;
46 }
47 }
48 }
50 BYTE Reader::readByte() {
51 BYTE out;
52 d_rider.read((char*)&out, 1);
53 return out;
54 }
56 SHORTINT Reader::readSInt() {
57 SHORTINT buf;
58 char *bufPtr = (char*)&buf;
59 d_rider.read(bufPtr, 2);
60 if (isLittleEndian()) {
61 return buf;
62 } else {
63 SHORTINT out;
64 char *outPtr = (char *)&out;
65 outPtr[0] = bufPtr[1]; outPtr[1] = bufPtr[0];
66 return out;
67 }
68 }
70 INTEGER Reader::readInt() {
71 INTEGER buf;
72 char *bufPtr = (char*)&buf;
73 d_rider.read(bufPtr, 4);
74 if (isLittleEndian()) {
75 return buf;
76 } else {
77 INTEGER out;
78 char *outPtr = (char *)&out;
79 outPtr[0] = bufPtr[3]; outPtr[1] = bufPtr[2]; outPtr[2] = bufPtr[1]; outPtr[3] = bufPtr[0];
80 return out;
81 }
82 }
84 void Reader::readSString(SHORTCHAR *out) {
85 while (*out = readSChar()) {
86 ++out;
87 }
88 }
90 void Reader::turnIntoAlien(int cause) {
91 assert(cause > 0);
92 d_cancelled = true;
93 d_cause = cause;
94 d_readAlien = true;
95 }
97 bool Reader::isCancelled() {
98 return d_cancelled;
99 }
101 INTEGER Reader::readVersion(INTEGER min, INTEGER max) {
102 INTEGER version = readByte();
103 if (version < min || version > max) {
104 turnIntoAlien(ALIENVERSION);
106 return version;
109 Store* Reader::readStore() {
110 SHORTCHAR kind = readSChar();
111 if (kind == Store::NIL) {
112 //std::cout << "NIL STORE" << std::endl;
113 return readNilStore();
114 } else if (kind == Store::LINK) {
115 //std::cout << "LINK STORE" << std::endl;
116 return readLinkStore();
117 } else if (kind == Store::NEWLINK) {
118 //std::cout << "NEWLINK STORE" << std::endl;
119 return readNewLinkStore();
120 } else if (kind == Store::STORE) {
121 //std::cout << "STORE STORE" << std::endl;
122 return readStoreOrElemStore(false);
123 } else if (kind == Store::ELEM) {
124 //std::cout << "ELEM STORE" << std::endl;
125 return readStoreOrElemStore(true);
126 } else {
127 //std::cout << std::hex << (unsigned int)kind << std::endl;
128 throw 20;
131 // PROCEDURE (VAR rd: Reader) ReadStore* (OUT x: Store), NEW;
132 // VAR a: Alien; t: Kernel.Type;
133 // len, pos, pos1, id, comment, next, down, downPos, nextTypeId, nextElemId, nextStoreId: INTEGER;
134 // kind: SHORTCHAR; path: TypePath; type: TypeName;
135 // save: ReaderState;
137 Store *Reader::readNilStore() {
138 INTEGER comment = readInt();
139 std::streamoff next = readInt();
140 d_state->end = d_rider.tellg();
141 if (next > 0 || (next == 0 && comment % 2 == 1)) {
142 d_state->next = d_state->end + next;
143 } else {
144 d_state->next = 0;
146 return 0;
148 // IF kind = nil THEN
149 // rd.ReadInt(comment); rd.ReadInt(next);
150 // rd.st.end := rd.Pos();
151 // IF (next > 0) OR ((next = 0) & ODD(comment)) THEN rd.st.next := rd.st.end + next ELSE rd.st.next := 0 END;
152 // x := NIL
154 Store *Reader::readLinkStore() {
155 throw "Reader::readLinkStore() not implemented";
157 // ELSIF kind = link THEN
158 // rd.ReadInt(id); rd.ReadInt(comment); rd.ReadInt(next);
159 // rd.st.end := rd.Pos();
160 // IF (next > 0) OR ((next = 0) & ODD(comment)) THEN rd.st.next := rd.st.end + next ELSE rd.st.next := 0 END;
161 // x := ThisStore(rd.eDict, id)
163 Store *Reader::readNewLinkStore() {
164 throw "Reader::readNewLinkStore() not implemented";
166 // ELSIF kind = newlink THEN
167 // rd.ReadInt(id); rd.ReadInt(comment); rd.ReadInt(next);
168 // rd.st.end := rd.Pos();
169 // IF (next > 0) OR ((next = 0) & ODD(comment)) THEN rd.st.next := rd.st.end + next ELSE rd.st.next := 0 END;
170 // x := ThisStore(rd.sDict, id)
172 Store *Reader::readStoreOrElemStore(bool isElem) {
173 INTEGER id = isElem ? d_elemList.size() : d_storeList.size();
174 TypePath path = readPath();
175 //std::cout << path.toString() << std::endl;
176 const std::string &type = path[0];
177 INTEGER comment = readInt();
178 std::streampos pos1 = d_rider.tellg();
179 std::streamoff next = readInt();
180 std::streamoff down = readInt();
181 std::streamoff len = readInt();
182 std::streampos pos = d_rider.tellg();
183 if (next > 0) {
184 d_state->next = pos1 + next + (std::streamoff)4;
185 } else {
186 d_state->next = 0;
188 int downPos = 0;
189 if (down > 0) {
190 downPos = pos1 + down + (std::streamoff)8;
192 d_state->end = pos + len;
193 d_cause = 0;
194 assert(len >= 0);
195 if (next != 0) {
196 assert(d_state->next > pos1);
197 if (down != 0) {
198 assert(downPos < d_state->next);
201 if (down > 0) {
202 assert(downPos > pos1);
203 assert(downPos < d_state->end);
206 const TypeProxyBase *t = TypeRegister::getInstance().get(type); // FIXME type lookup here
207 Store *x = 0;
208 if (t != 0) {
209 x = t->newInstance(id);
210 } else {
211 d_cause = TYPENOTFOUND;
214 if (x != 0) { // IF READING SUCCEEDS, INSERT MORE CHECKS HERE
215 if (true) { // samePath(x, path)
216 ReaderState *save = d_state;
217 d_state = new ReaderState();
218 x->internalize(*this);
219 delete d_state;
220 d_state = save;
222 if (d_cause != 0) {
223 x = 0;
225 assert(d_rider.tellg() == d_state->end);
226 assert(!d_rider.eof());
227 } else {
228 // rd.cause := inconsistentType; AlienTypeReport(rd.cause, type);
229 x = 0;
233 if (x != 0) {
234 if (d_store == 0) {
235 d_store = x;
236 } else {
237 // join(d_store, x)
239 if (isElem) {
240 d_elemList.push_back(x);
241 } else {
242 d_storeList.push_back(x);
244 } else {
245 d_rider.seekg(pos); // x->internalize() could have left us anywhere
246 assert(d_cause != 0);
247 Alien *alien = new Alien(id, path); //, d_cause); //,file
248 if (d_store == 0) {
249 d_store = alien;
250 } else {
251 // join(d_store, alien)
253 if (isElem) {
254 d_elemList.push_back(alien);
255 } else {
256 d_storeList.push_back(alien);
258 ReaderState *save = d_state;
259 d_state = new ReaderState();
260 internalizeAlien(alien, downPos, save->end);
261 delete d_state;
262 d_state = save;
263 assert(d_rider.tellg() == d_state->end);
265 // we've just read the alien, so reset the state
266 d_cause = 0;
267 d_cancelled = false;
268 d_readAlien = true;
269 return alien;
272 return x;
276 void Reader::internalizeAlien(Alien *alien, std::streampos down, std::streampos end) {
277 std::streampos next = down != 0 ? down : end;
278 while (d_rider.tellg() < end) {
279 if (d_rider.tellg() < next) { // for some reason, this means its a piece (unstructured)
280 size_t len = next - d_rider.tellg();
281 char *buf = new char[len];
282 d_rider.read(buf, len);
283 AlienComponent *comp = new AlienPiece(buf, len);
284 alien->getComponents().push_back(comp);
285 } else { // that means we've got a store
286 d_rider.seekg(next);
287 AlienComponent *comp = new AlienPart(readStore());
288 alien->getComponents().push_back(comp);
289 next = d_state->next > 0 ? d_state->next : end;
294 std::string &Reader::fixTypeName(std::string &name) {
295 size_t pos = name.size() - 4;
296 if (pos > 0 && name.substr(pos, 4).compare("Desc") == 0) {
297 return name.replace(pos, 4, "^");
299 return name;
302 TypePath Reader::readPath() {
303 TypePath path;
304 SHORTCHAR kind = readSChar();
305 SHORTCHAR *buf = new SHORTCHAR[64]; // TypeName has a maximum of length 64 (including terminator).
306 int i;
307 for (i = 0; kind == Store::NEWEXT; ++i) {
308 readSString(buf);
309 std::string name(buf);
310 path.push_back(fixTypeName(name));
311 addPathComponent(i == 0, path[i]);
312 // IF path[i] # elemTName THEN INC(i) END;
313 kind = readSChar();
316 if (kind == Store::NEWBASE) {
317 readSString(buf);
318 std::string name(buf);
319 path.push_back(fixTypeName(name));
320 addPathComponent(i == 0, path[i]);
321 ++i;
322 } else if (kind == Store::OLDTYPE) {
323 int id = readInt();
324 if (i > 0) {
325 d_typeList[d_typeList.size() - 1]->baseId = id;
327 while (id != -1) {
328 path.push_back(d_typeList[id]->name);
329 id = d_typeList[id]->baseId;
330 // IF path[i] # elemTName THEN INC(i) END
331 ++i;
333 } else {
334 throw 100;
336 return path;
339 void Reader::addPathComponent(bool first, const std::string &typeName) {
340 int next = d_typeList.size();
341 int curr = next - 1;
342 if (!first) {
343 d_typeList[curr]->baseId = next;
345 d_typeList.push_back(new TypeEntry(typeName));
348 } // namespace odc