ooo-build r11369 - in trunk: . scratch/sc-xlsutil scratch/sc-xlsutil/src



Author: kyoshida
Date: Wed Jan 23 06:49:23 2008
New Revision: 11369
URL: http://svn.gnome.org/viewvc/ooo-build?rev=11369&view=rev

Log:
2008-01-23  Kohei Yoshida  <kyoshida novell com>

	* scratch/sc-xlsutil/src/ole.py:
	* scratch/sc-xlsutil/src/stream.py:
	* scratch/sc-xlsutil/xls_dump.py: finally finished dumping the raw byte
	contents of directory entries.


Modified:
   trunk/ChangeLog
   trunk/scratch/sc-xlsutil/src/ole.py
   trunk/scratch/sc-xlsutil/src/stream.py
   trunk/scratch/sc-xlsutil/xls_dump.py

Modified: trunk/scratch/sc-xlsutil/src/ole.py
==============================================================================
--- trunk/scratch/sc-xlsutil/src/ole.py	(original)
+++ trunk/scratch/sc-xlsutil/src/ole.py	Wed Jan 23 06:49:23 2008
@@ -43,8 +43,8 @@
 
 class ByteOrder:
     LittleEndian = 0
-    BigEndian = 1
-    Unknown = 2
+    BigEndian    = 1
+    Unknown      = 2
 
 class SectorType:
     MSAT      = 0
@@ -52,6 +52,10 @@
     SSAT      = 2
     Directory = 3
 
+class StreamLocation:
+    SAT  = 0
+    SSAT = 1
+
 class Header(object):
 
     @staticmethod
@@ -106,31 +110,6 @@
         return -2
 
 
-    def dumpBytes (self, initPos=0, quitAtBOF=True):
-        # dump OLE header until it reaches BOF.
-        i, bprev = initPos, 0
-        labelPrinted = False
-        while True:
-            b = ord(self.bytes[i])
-            if quitAtBOF and bprev == 0x09 and b == 0x08:
-                # BOF reached
-                return i-1
-
-            if i%2 != 0:
-                if not labelPrinted:
-                    print("\n\nThe rest of the bytes:")
-                    labelPrinted = True
-                output("%2.2X %2.2X "%(bprev, b))
-
-            if (i+1)%16 == 0:
-                output("\n")
-
-            bprev = b
-            i += 1
-
-        return i
-
-
     def output (self):
 
         def printRawBytes (bytes):
@@ -429,7 +408,7 @@
             self.TimeCreated = None
             self.TimeModified = None
             self.StreamSectorID = -2
-            self.StreamSectorSize = 0
+            self.StreamSize = 0
 
 
     def __init__ (self, header):
@@ -441,6 +420,38 @@
         self.SAT = header.getSAT()
         self.SSAT = header.getSSAT()
         self.header = header
+        self.posRootStorage = None
+
+
+    def __getRawStream (self, entry):
+        chain = []
+        if entry.StreamLocation == StreamLocation.SAT:
+            chain = self.header.getSAT().getSectorIDChain(entry.StreamSectorID)
+        elif entry.StreamLocation == StreamLocation.SSAT:
+            chain = self.header.getSSAT().getSectorIDChain(entry.StreamSectorID)
+
+        offset = 512
+        size = self.header.getSectorSize()
+        bytes = []
+        if entry.StreamLocation == StreamLocation.SSAT:
+            # get root storage position
+            offset = self.posRootStorage
+            size = self.header.getShortSectorSize()
+
+        for id in chain:
+            pos = offset + id*size
+            bytes.extend(self.header.bytes[pos:pos+size])
+
+        return bytes
+
+
+    def getRawStreamByName (self, name):
+        bytes = []
+        for entry in self.entries:
+            if entry.Name == name:
+                bytes = self.__getRawStream(entry)
+                break
+        return bytes
 
 
     def addSector (self, id):
@@ -515,18 +526,18 @@
         if entry.StreamSectorID < 0:
             print("[empty stream]")
         else:
-            strmLoc = "SSAT"
-            if entry.Type == Directory.Type.RootStorage or entry.StreamSize >= self.minStreamSize:
-                strmLoc = "SAT"
+            strmLoc = "SAT"
+            if entry.StreamLocation == StreamLocation.SSAT:
+                strmLoc = "SSAT"
             print("(first sector ID: %d; size: %d; location: %s)"%
                   (entry.StreamSectorID, entry.StreamSize, strmLoc))
     
             satObj = None
             secSize = 0
-            if strmLoc == "SAT":
+            if entry.StreamLocation == StreamLocation.SAT:
                 satObj = self.SAT
                 secSize = self.header.getSectorSize()
-            elif strmLoc == "SSAT":
+            elif entry.StreamLocation == StreamLocation.SSAT:
                 satObj = self.SSAT
                 secSize = self.header.getShortSectorSize()
             if satObj != None:
@@ -566,7 +577,19 @@
             output("%2.2X "%ord(byte))
         print("")
 
+
+    def getDirectoryNames (self):
+        names = []
+        for entry in self.entries:
+            names.append(entry.Name)
+        return names
+
+
     def parseDirEntries (self):
+        if len(self.entries):
+            # directory entries already built
+            return
+
         # combine all sectors first.
         bytes = []
         for secID in self.sectorIDs:
@@ -603,11 +626,13 @@
 
         entry.StreamSectorID = getSignedInt(bytes[116:120])
         entry.StreamSize     = getSignedInt(bytes[120:124])
+        entry.StreamLocation = StreamLocation.SAT
+        if entry.Type != Directory.Type.RootStorage and \
+            entry.StreamSize < self.header.minStreamSize:
+            entry.StreamLocation = StreamLocation.SSAT
 
-        return entry
-
-
+        if entry.Type == Directory.Type.RootStorage and entry.StreamSectorID >= 0:
+            self.posRootStorage = 512 + entry.StreamSectorID*self.header.getSectorSize()
 
-    def dummy ():
-        pass
+        return entry
 

Modified: trunk/scratch/sc-xlsutil/src/stream.py
==============================================================================
--- trunk/scratch/sc-xlsutil/src/stream.py	(original)
+++ trunk/scratch/sc-xlsutil/src/stream.py	Wed Jan 23 06:49:23 2008
@@ -1,6 +1,7 @@
 
 import sys
 import ole, globals
+from globals import output
 
 recData = {
     0x000A: ["EOF", "End of File"],
@@ -174,8 +175,6 @@
     0x0031: ["FONT", "Font and Character Formatting"],
     0x00FF: ["EXTSST", "Extended Shared String Table"] }
 
-def output (msg):
-    sys.stdout.write(msg)
 
 class XLStream(object):
 
@@ -207,15 +206,6 @@
 
     def printMSAT (self):
         self.MSAT.output()
-#       secPosList = self.MSAT.getSATSectorPosList()
-#       secSize = self.MSAT.sectorSize
-#       for sec in secPosList:
-#           id, pos = sec[0], sec[1]
-#           print("")
-#           print("-"*68)
-#           print("Sector ID: %d  (pos = %d)"%(id, pos))
-#           print("-"*68)
-#           globals.dumpBytes(self.chars[pos:pos+secSize])
 
     def printSAT (self):
         sat = self.MSAT.getSAT()
@@ -227,20 +217,48 @@
             return
         obj.output()
 
-    def printDirectory (self):
+
+    def __getDirectoryObj (self):
         obj = self.header.getDirectory()
         if obj == None:
-            return
+            return None
         obj.parseDirEntries()
+        return obj
+
+    def printDirectory (self):
+        obj = self.__getDirectoryObj()
+        if obj == None:
+            return
         obj.output()
 
-    def dumpHeader (self):
-        oleobj = ole.Header(self.chars)
-        self.pos = oleobj.dumpBytes()
+
+    def getDirectoryNames (self):
+        obj = self.__getDirectoryObj()
+        if obj == None:
+            return
+        return obj.getDirectoryNames()
+
+
+    def getDirectoryStreamByName (self, name):
+        obj = self.__getDirectoryObj()
+        bytes = []
+        if obj != None:
+            bytes = obj.getRawStreamByName(name)
+        strm = XLDirStream(bytes)
+        return strm
+
+
+class XLDirStream(object):
+
+    def __init__ (self, bytes):
+        self.bytes = bytes
+        self.size = len(self.bytes)
+        self.pos = 0
+        return
 
     def seekBOF (self):
         while self.pos < self.size-1:
-            b1, b2 = ord(self.chars[self.pos]), ord(self.chars[self.pos+1])
+            b1, b2 = ord(self.bytes[self.pos]), ord(self.bytes[self.pos+1])
             word = b1 + b2*256
             if word == 0x0809:
                 self.version = 'BIFF5/BIFF8'
@@ -251,7 +269,7 @@
         # assume little endian
         bytes = 0
         for i in xrange(0, size):
-            b = ord(self.chars[self.pos])
+            b = ord(self.bytes[self.pos])
             if i == 0:
                 bytes = b
             else:
@@ -263,10 +281,13 @@
     def readByteArray (self, size=1):
         bytes = []
         for i in xrange(0, size):
-            bytes.append(ord(self.chars[self.pos]))
+            bytes.append(ord(self.bytes[self.pos]))
             self.pos += 1
         return bytes
 
+    def __printSep (self, c='-', w=68, prefix=''):
+        print(prefix + c*w)
+
     def readRecord (self):
         pos = self.pos
         header = self.readRaw(2)

Modified: trunk/scratch/sc-xlsutil/xls_dump.py
==============================================================================
--- trunk/scratch/sc-xlsutil/xls_dump.py	(original)
+++ trunk/scratch/sc-xlsutil/xls_dump.py	Wed Jan 23 06:49:23 2008
@@ -2,7 +2,7 @@
 
 import sys, os.path
 sys.path.append(sys.path[0]+"/src")
-import ole, stream
+import ole, stream, globals
 
 class XLDumper(object):
 
@@ -19,9 +19,25 @@
 #       strm.printSAT()
 #       strm.printSSAT()
         strm.printDirectory()
-        success = True
-        while success: 
-            success = self.__read(strm)
+        dirnames = strm.getDirectoryNames()
+        for dirname in dirnames:
+            if len(dirname) == 0 or dirname == 'Root Entry':
+                continue
+
+            dirstrm = strm.getDirectoryStreamByName(dirname)
+            if dirname == "Workbook":
+                success = True
+                while success: 
+                    success = self.__read(dirstrm)
+            else:
+                if ord(dirname[0]) <= 5:
+                    dirname = "<%2.2Xh>%s"%(ord(dirname[0]), dirname[1:])
+                print("")
+                print("="*68)
+                print("%s (size: %d bytes)"%(dirname, len(dirstrm.bytes)))
+                print("-"*68)
+                globals.dumpBytes(dirstrm.bytes, 512)
+
 
     def __read (self, strm):
         # read bytes from BOF to EOF.



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]