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
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)
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
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)
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]
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 = {}
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
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
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
def readRoot(self): if not self.header: raise OleError("Header not read.") data = self.readFatStream(self.header.dirloc) return self.readDir(data, 0)