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



Author: kyoshida
Date: Thu Jan 24 21:47:08 2008
New Revision: 11412
URL: http://svn.gnome.org/viewvc/ooo-build?rev=11412&view=rev

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

	* scratch/sc-xlsutil/src/record.py: new file to store record handler
	classes.

	* scratch/sc-xlsutil/src/globals.py:
	* scratch/sc-xlsutil/src/ole.py:
	* scratch/sc-xlsutil/src/stream.py: use record handlers to further parse
	each record.


Added:
   trunk/scratch/sc-xlsutil/src/record.py
Modified:
   trunk/ChangeLog
   trunk/scratch/sc-xlsutil/src/globals.py
   trunk/scratch/sc-xlsutil/src/ole.py
   trunk/scratch/sc-xlsutil/src/stream.py

Modified: trunk/scratch/sc-xlsutil/src/globals.py
==============================================================================
--- trunk/scratch/sc-xlsutil/src/globals.py	(original)
+++ trunk/scratch/sc-xlsutil/src/globals.py	Thu Jan 24 21:47:08 2008
@@ -4,6 +4,7 @@
 def output (msg):
     sys.stdout.write(msg)
 
+
 def dumpBytes (chars, subDivide=None):
     line = 0
     subDivideLine = None
@@ -24,6 +25,51 @@
     return 512 + secID*secSize
 
 
+def char2byte (chars):
+    bytes = []
+    for c in chars:
+        bytes.append(ord(c))
+    return bytes
+
+
+def getRawBytes (bytes, spaced=True):
+    text = ''
+    for b in bytes:
+        if type(b) == type(''):
+            b = ord(b)
+        if len(text) == 0:
+            text = "%2.2X"%b
+        elif spaced:
+            text = "%2.2X "%b + text
+        else:
+            text = "%2.2X"%b + text
+    return text
+
+
+def getSignedInt (bytes):
+    # little endian
+    n = len(bytes)
+    if n == 0:
+        return 0
+
+    if type(bytes[0]) == type('c'):
+        bytes = char2byte(bytes)
+
+    isNegative = (bytes[-1] & 0x80) == 0x80
+
+    num, ff = 0, 0
+    for i in xrange(0, n):
+        num += bytes[i]*(256**i)
+        ff += 0xFF*(256**i)
+        i += 1
+
+    if isNegative:
+        # perform two's compliment.
+        num = -((num^ff) + 1)
+
+    return num
+
+
 def getUTF8FromUTF16 (bytes):
     # little endian utf-16 strings
     byteCount = len(bytes)

Modified: trunk/scratch/sc-xlsutil/src/ole.py
==============================================================================
--- trunk/scratch/sc-xlsutil/src/ole.py	(original)
+++ trunk/scratch/sc-xlsutil/src/ole.py	Thu Jan 24 21:47:08 2008
@@ -1,7 +1,7 @@
 
 import sys
 import stream, globals
-
+from globals import getSignedInt
 # ----------------------------------------------------------------------------
 # Reference: The Microsoft Compound Document File Format by Daniel Rentz
 # http://sc.openoffice.org/compdocfileformat.pdf
@@ -12,34 +12,6 @@
 def printSep (c='-', w=68, prefix=''):
     print(prefix + c*w)
 
-def char2byte (chars):
-    bytes = []
-    for c in chars:
-        bytes.append(ord(c))
-    return bytes
-
-def getSignedInt (bytes):
-    # little endian
-    n = len(bytes)
-    if n == 0:
-        return 0
-
-    if type(bytes[0]) == type('c'):
-        bytes = char2byte(bytes)
-
-    isNegative = (bytes[-1] & 0x80) == 0x80
-
-    num, ff = 0, 0
-    for i in xrange(0, n):
-        num += bytes[i]*(256**i)
-        ff += 0xFF*(256**i)
-        i += 1
-
-    if isNegative:
-        # perform two's compliment.
-        num = -((num^ff) + 1)
-
-    return num
 
 class ByteOrder:
     LittleEndian = 0

Added: trunk/scratch/sc-xlsutil/src/record.py
==============================================================================
--- (empty file)
+++ trunk/scratch/sc-xlsutil/src/record.py	Thu Jan 24 21:47:08 2008
@@ -0,0 +1,69 @@
+
+import globals
+
+#--------------------------------------------------------------------
+# record handler classes
+
+class BaseRecordHandler(object):
+
+    def __init__ (self, header, size, bytes):
+        self.header = header
+        self.size = size
+        self.bytes = bytes
+        self.lines = []
+
+    def parseBytes (self):
+        pass
+
+    def output (self):
+        self.parseBytes()
+        print("%4.4Xh: %s"%(self.header, "-"*61))
+        for line in self.lines:
+            print("%4.4Xh: %s"%(self.header, line))
+
+    def appendLine (self, line):
+        self.lines.append(line)
+
+
+class BOF(BaseRecordHandler):
+
+    Type = {
+        0x0005: "Workbook globals",
+        0x0006: "Visual Basic module",
+        0x0010: "Sheet or dialog",
+        0x0020: "Chart",
+        0x0040: "Macro sheet",
+        0x0100: "Workspace"
+    }
+
+    def __init__ (self, header, size, bytes):
+        BaseRecordHandler.__init__(self, header, size, bytes)
+
+    def parseBytes (self):
+        ver = globals.getSignedInt(self.bytes[0:2])
+        dataType = globals.getSignedInt(self.bytes[2:4])
+        buildID = globals.getSignedInt(self.bytes[4:6])
+        buildYear = globals.getSignedInt(self.bytes[6:8])
+        fileHistoryFlags = globals.getSignedInt(self.bytes[8:12])
+        lowestExcelVer = globals.getSignedInt(self.bytes[12:16])
+
+        self.appendLine("BIFF version: %d"%ver)
+        self.appendLine("build ID: %d"%buildID)
+        self.appendLine("build year: %d"%buildYear)
+        self.appendLine("type: %s"%BOF.Type[dataType])
+        self.appendLine("file history flags: " + globals.getRawBytes(self.bytes[8:12]))
+        self.appendLine("lowest Excel version: %d"%lowestExcelVer)
+        
+
+
+class CTCellContent(BaseRecordHandler):
+
+    def __init__ (self, header, size, bytes):
+        BaseRecordHandler.__init__(self, header, size, bytes)
+
+    def output (self):
+        BaseRecordHandler.output(self)
+
+
+
+

Modified: trunk/scratch/sc-xlsutil/src/stream.py
==============================================================================
--- trunk/scratch/sc-xlsutil/src/stream.py	(original)
+++ trunk/scratch/sc-xlsutil/src/stream.py	Thu Jan 24 21:47:08 2008
@@ -1,10 +1,12 @@
 
 import sys
-import ole, globals
+import ole, globals, record
 from globals import output
 
 class EndOfStream(Exception): pass
 
+    # opcode: [canonical name, description, handler (optional)]
+
 recData = {
     0x0006: ["FORMULA", "Formula Token Array and Result"],
     0x000A: ["EOF", "End of File"],
@@ -176,7 +178,7 @@
     0x0293: ["STYLE", "Style Information"],
     0x0406: ["FORMULA", "Cell Formula"],
     0x041E: ["FORMAT", "Number Format"],
-    0x0809: ["BOF", "Beginning of File"],
+    0x0809: ["BOF", "Beginning of File", record.BOF],
     0x0862: ["SHEETLAYOUT", "Tab Color below Sheet Name"],
     0x0867: ["SHEETPROTECTION", "Sheet Protection Options"],
     0x0868: ["RANGEPROTECTION", "Protected Range on Protected Sheet"]
@@ -185,7 +187,7 @@
 recDataRev = {
     0x0137: ["INSERT", "Change Track Insert"],
     0x0138: ["INFO", "Change Track Info"],
-    0x013B: ["CELLCONTENT", "Change Track Cell Content"],
+    0x013B: ["CELLCONTENT", "Change Track Cell Content", record.CTCellContent],
     0x013D: ["SHEETID", "Change Track Sheet Identifier"],
     0x0140: ["MOVERANGE", "Change Track Move Range"],
     0x014D: ["INSERTSHEET", "Change Track Insert Sheet"],
@@ -319,20 +321,28 @@
         if header == 0x0000:
             raise EndOfStream
         size = self.readRaw(2)
+        bytes = self.readByteArray(size)
+
+        # record handler that parses the raw bytes and displays more 
+        # meaningful information.
+        handler = None 
 
         print("")
         self.__printSep('=', 61, "%4.4Xh: "%header)
         if recData.has_key(header):
             print("%4.4Xh: %s - %s (%4.4Xh)"%
                   (header, recData[header][0], recData[header][1], header))
+            if len(recData[header]) >= 3:
+                handler = recData[header][2](header, size, bytes)
         elif self.type == DirType.RevisionLog and recDataRev.has_key(header):
             print("%4.4Xh: %s - %s (%4.4Xh)"%
                   (header, recDataRev[header][0], recDataRev[header][1], header))
+            if len(recDataRev[header]) >= 3:
+                handler = recDataRev[header][2](header, size, bytes)
         else:
             print("%4.4Xh: [unknown record name] (%4.4Xh)"%(header, header))
         print("%4.4Xh:   size = %d; pos = %d"%(header, size, pos))
         self.__printSep('-', 61, "%4.4Xh: "%header)
-        bytes = self.readByteArray(size)
         for i in xrange(0, size):
             if (i+1) % 16 == 1:
                 output("%4.4Xh: "%header)
@@ -342,4 +352,8 @@
         if size > 0:
             print("")
 
+        if handler != None:
+            # record handler exists.  Parse the record and display more info.
+            handler.output()
+
         return header



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