From f004854d5f2a0bc246a2524ddb843e369b21de99 Mon Sep 17 00:00:00 2001
From: itsme <itsme@xs4all.nl>
Date: Tue, 13 Jul 2021 21:19:11 +0200
Subject: now the enumerate_XXX functions actually work

---
 crodump.py | 42 +++++++++++++++++++++++++-----------------
 1 file changed, 25 insertions(+), 17 deletions(-)

diff --git a/crodump.py b/crodump.py
index 5b495de..984770f 100644
--- a/crodump.py
+++ b/crodump.py
@@ -78,9 +78,16 @@ class Datafile:
         if self.use64bit:
             # 01.03 has 64 bit file offsets
             self.tadidx = [ struct.unpack_from("<QLL", indexdata, 16*_) for _ in range(len(indexdata)//16) ]
+            if len(indexdata)%16:
+                print("WARN: leftover data in .tad")
         else:
             # 01.02  and 01.04  have 32 bit offsets.
             self.tadidx = [ struct.unpack_from("<LLL", indexdata, 12*_) for _ in range(len(indexdata)//12) ]
+            if len(indexdata)%12:
+                print("WARN: leftover data in .tad")
+
+    def nrofrecords(self):
+        return len(self.tadidx)
 
     def readdata(self, ofs, size):
         self.dat.seek(ofs)
@@ -269,14 +276,14 @@ class TableDefinition:
         self.version = rd.readbyte()
         if self.version > 1:
           _ = rd.readbyte()  # always 0 anyway
+
         self.unk2 = rd.readbyte() # if this is not 5 (but 9), there's another 4 bytes inserted, this could be a length-byte.
         self.unk3 = rd.readbyte()
-
         if self.unk2 > 5:            # seen only 5 and 9 for now with 9 implying an extra dword
            _ = rd.readdword()
+        self.unk4 = rd.readdword()
 
         self.tableid = rd.readdword()
-        self.unk5 = rd.readdword()
 
         self.tablename = rd.readname()
         self.abbrev = rd.readname()
@@ -293,10 +300,13 @@ class TableDefinition:
 
         self.remainingdata = rd.readbytes()
 
+    def __str__(self):
+        return "%d,%d<%d,%d,%d>%d  %d,%d '%s'  '%s'" % ( self.unk1, self.version, self.unk2, self.unk3, self.unk4, self.tableid, self.unk7, len(self.fields), self.tablename, self.abbrev)
     def dump(self, args):
         if args.verbose:
             print("table: %s" % tohex(self.headerdata))
-        print("%d,%d,%d,%d,%d,%d  %d,%d '%s'  '%s'" % ( self.unk1, self.version, self.unk2, self.unk3, self.tableid, self.unk5, self.unk7, len(self.fields), self.tablename, self.abbrev))
+
+        print(str(self))
 
         for field in self.fields:
             if args.verbose:
@@ -345,6 +355,8 @@ class Database:
         self.sys = self.getfile("Sys")
         # BankTemp, Int
 
+    def nrofrecords(self):
+        return len(self.bank.tadidx)
 
     def getfile(self, name):
         try:
@@ -440,26 +452,26 @@ class Database:
             for rec in db.enumerate_records(tab):
                 print(sqlformatter(tab, rec))
         """
-        for i in range(1, args.maxrecs+1):
-            data = db.readrec(i)
-            if data and struct.unpack_from("<B", data, 0) == table.tableid:
+        for i in range(self.nrofrecords()):
+            data = self.bank.readrec(i+1)
+            if data and data[0] == table.tableid:
                 yield data[1:].split(b"\x1e")
 
 
     def recdump(self, args):
         if args.index:
-            db = self.index
+            dbfile = self.index
         elif args.sys:
-            db = self.sys
+            dbfile = self.sys
         elif args.stru:
-            db = self.stru
+            dbfile = self.stru
         else:
-            db = self.bank
+            dbfile = self.bank
 
-        if not db:
+        if not dbfile:
             print(".dat not found")
             return
-        if args.skipencrypted and db.encoding==3:
+        if args.skipencrypted and dbfile.encoding==3:
             print("Skipping encrypted CroBank")
             return
         nerr = 0
@@ -469,7 +481,7 @@ class Database:
         bytexref = [0] * 256
         for i in range(1, args.maxrecs+1):
             try:
-                data = db.readrec(i)
+                data = dbfile.readrec(i)
                 if args.find1d:
                     if data and (data.find(b"\x1d")>0 or data.find(b"\x1b")>0):
                         print("%d -> %s" % (i, b2a_hex(data)))
@@ -509,10 +521,6 @@ class Database:
                 if v:
                     print("%5d * %02x" % (v, k))
 
-    def readrec(self, sysnum):
-        data = self.bank.readrec(sysnum)
-        tabnum, = struct.unpack_from("<B", data, 0)
-        fields = data[1:].split(b"\x1e")
 
 def incdata(data, s):
     """
-- 
cgit v1.2.3