示例#1
0
 def readDir(self, data, idx, parent=None):
     nd = OleFile.Node()
     nd.parent = parent
     isRoot = parent is None
     pos = idx * OleFile.DIR_SIZE
     (left, right, child,
      size) = nd.read(data[pos:pos + OleFile.DIR_SIZE])
     if isRoot != (nd.etype == OleFile.Node.STGTY_ROOT):
         raise OleError("Wrong root node.")
     if nd.sector is not None and size:
         if isRoot:
             self.ministream = self.readFatStream(nd.sector, size)
         elif nd.etype == OleFile.Node.STGTY_STREAM:
             if size < self.header.minicutoff:
                 nd.data = self.readMinifatStream(nd.sector, size)
             else:
                 nd.data = self.readFatStream(nd.sector, size)
         else:
             raise OleError("Dont know how to read sectors of " +
                            str(nd.etype))
     if left and left != OleFile.Node.NOSTREAM:
         node = self.readDir(data, left, nd.parent)
         nd.parent.subnodes[node.name] = node
     if right and right != OleFile.Node.NOSTREAM:
         node = self.readDir(data, right, nd.parent)
         nd.parent.subnodes[node.name] = node
     if child and child != OleFile.Node.NOSTREAM:
         ch = self.readDir(data, child, nd)
         nd.subnodes[ch.name] = ch
     return nd
示例#2
0
 def open(self, path, mode="r"):
     nd = self.node(path, 'w' in mode)
     if not nd or nd.etype != OleFile.Node.STGTY_STREAM:
         raise OleError("File not exists: " + path)
     if not nd.data:
         nd.data = ''
     return OleFile.IOProxy(nd)
示例#3
0
 def mkdir(self, path, clsid=None):
     nd = self.node(path, True)
     if not nd:
         raise OleError("Path not found: " + path)
     nd.etype = OleFile.Node.STGTY_STORAGE
     if clsid:
         nd.clsid = clsid
     nd.data = None
     return nd
示例#4
0
 def __init__(self, fp, isBin=None, **kwargs):
     if fp is None and isBin is None:
         raise OleError("Need file or isBin parameter")
     if isBin is None:
         isBin = olefile.OleFile.detect(fp)
     self.isBin = isBin
     if isBin:
         self.file = olefile.OleFile(fp, **kwargs)
     else:
         self.file = xfile.XFile(fp, **kwargs)
示例#5
0
 def readMinifatStream(self, secnum, size=None):
     if not self.ministream:
         raise OleError("Ministream not loaded")
     ret = ''
     sz = self.header.secsize(True)
     while True:
         pos = sz * secnum
         ret += self.ministream[pos:pos + sz]
         if size and len(ret) >= size:
             return ret[:size]
         if self.minifat[secnum] > OleFile.MAXREGSECT:
             return ret
         secnum = self.minifat[secnum]
示例#6
0
 def __init__(self, name=None, clsid=None, etype=STGTY_STREAM):
     if name and len(name) > 31:
         raise OleError("Name too long: " + name)
     self.name = unicode(name) if name else None
     self.clsid = clsid or Utils.GUID_NULL
     self.etype = etype
     self.flags = 0
     self.ctime = None
     self.mtime = None
     self.sector = None
     self.data = None
     self.parent = None
     self.subnodes = {}
示例#7
0
 def read(self, fp):
     mag = fp.read(8)
     if mag != self.MAGIC:
         raise OleError("Wrong magic")
     self.clsid = uuid.UUID(bytes_le=fp.read(16))
     (self.minor, self.major, self.order, self.sshift, self.minishift,
      self.reserved1, self.reserved2, self.dirnum, self.fatnum,
      self.dirloc, self.transnum, self.minicutoff, self.miniloc,
      self.mininum, self.difatloc,
      self.difatnum) = struct.unpack("<HHHHHHLLLLLLLLLL", fp.read(52))
     self.difattbl = []
     for x in struct.unpack("<109L", fp.read(436)):
         if x != OleFile.FREESECT:
             self.difattbl += [x]
     return self
示例#8
0
 def setFile(self, path, data):
     nd = self.node(path, True)
     if not nd or nd.etype != OleFile.Node.STGTY_STREAM:
         raise OleError("Cant create file: " + path)
     nd.data = data
     return nd
示例#9
0
 def getFile(self, path):
     nd = self.node(path)
     if not nd or nd.etype != OleFile.Node.STGTY_STREAM:
         raise OleError("File not exists: " + path)
     return nd.data
示例#10
0
 def readRoot(self):
     if not self.header:
         raise OleError("Header not read.")
     data = self.readFatStream(self.header.dirloc)
     return self.readDir(data, 0)