Exemplo n.º 1
0
Arquivo: ole2.py Projeto: Anstep/pyew
def ole2Explore(pyew, doprint=True, args=None):
    """ Get the OLE2 directory """
    if not pyew.physical:
       filename = tempfile.mkstemp("pyew")[1]
       f = file(filename, "wb")
       f.write(pyew.getBuffer())
       f.close()
    else:
        filename = pyew.filename

    ole = OleFileIO(filename, raise_defects=DEFECT_INCORRECT)
    ole.dumpdirectory()
    i = 0
    for streamname in ole.listdir():
        if streamname[-1][0] == "\005":
            print streamname, ": properties"
            props = ole.getproperties(streamname)
            props = props.items()
            props.sort()
            for k, v in props:
                #[PL]: avoid to display too large or binary values:
                if isinstance(v, basestring):
                    if len(v) > 50:
                        v = v[:50]
                    # quick and dirty binary check:
                    for c in (1,2,3,4,5,6,7,11,12,14,15,16,17,18,19,20,
                        21,22,23,24,25,26,27,28,29,30,31):
                        if chr(c) in v:
                            v = '(binary data)'
                            break
                print "   ", k, v
                
        
    # Read all streams to check if there are errors:
    print '\nChecking streams...'
    for streamname in ole.listdir():
        # print name using repr() to convert binary chars to \xNN:
        print '-', repr('/'.join(streamname)),'-',
        st_type = ole.get_type(streamname)
        if st_type == STGTY_STREAM:
            print 'size %d' % ole.get_size(streamname)
            # just try to read stream in memory:
            ole.openstream(streamname)
        else:
            print 'NOT a stream : type=%d' % st_type
    print ''

    #[PL] Test a few new methods:
    root = ole.get_rootentry_name()
    print 'Root entry name: "%s"' % root
    if ole.exists('worddocument'):
        print "This is a Word document."
        print "type of stream 'WordDocument':", ole.get_type('worddocument')
        print "size :", ole.get_size('worddocument')
        if ole.exists('macros/vba'):
            print "This document may contain VBA macros."
Exemplo n.º 2
0
def ole2Explore(pyew):
    """ Get the OLE2 directory """
    if not pyew.physical:
        filename = tempfile.mkstemp("pyew")[1]
        f = file(filename, "wb")
        f.write(pyew.getBuffer())
        f.close()
    else:
        filename = pyew.filename

    ole = OleFileIO(filename, raise_defects=DEFECT_INCORRECT)
    ole.dumpdirectory()
    i = 0
    for streamname in ole.listdir():
        if streamname[-1][0] == "\005":
            print streamname, ": properties"
            props = ole.getproperties(streamname)
            props = props.items()
            props.sort()
            for k, v in props:
                #[PL]: avoid to display too large or binary values:
                if isinstance(v, basestring):
                    if len(v) > 50:
                        v = v[:50]
                    # quick and dirty binary check:
                    for c in (1, 2, 3, 4, 5, 6, 7, 11, 12, 14, 15, 16, 17, 18,
                              19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30,
                              31):
                        if chr(c) in v:
                            v = '(binary data)'
                            break
                print "   ", k, v

    # Read all streams to check if there are errors:
    print '\nChecking streams...'
    for streamname in ole.listdir():
        # print name using repr() to convert binary chars to \xNN:
        print '-', repr('/'.join(streamname)), '-',
        st_type = ole.get_type(streamname)
        if st_type == STGTY_STREAM:
            print 'size %d' % ole.get_size(streamname)
            # just try to read stream in memory:
            ole.openstream(streamname)
        else:
            print 'NOT a stream : type=%d' % st_type
    print ''

    #[PL] Test a few new methods:
    root = ole.get_rootentry_name()
    print 'Root entry name: "%s"' % root
    if ole.exists('worddocument'):
        print "This is a Word document."
        print "type of stream 'WordDocument':", ole.get_type('worddocument')
        print "size :", ole.get_size('worddocument')
        if ole.exists('macros/vba'):
            print "This document may contain VBA macros."
Exemplo n.º 3
0
def read(file):
    """Parses an Altium ".SchDoc" schematic file and returns a Sheet object
    """
    ole = OleFileIO(file)

    stream = ole.openstream("FileHeader")
    records = iter_records(stream)
    records = (parse_properties(stream, record) for record in records)
    header = next(records)
    parse_header(header)
    header.check_unknown()

    sheet = Object(properties=next(records))
    objects = [sheet]
    for properties in records:
        obj = Object(properties=properties)
        objects[obj.properties.get_int("OWNERINDEX")].children.append(obj)
        objects.append(obj)

    if ole.exists("Additional"):
        stream = ole.openstream("Additional")
        records = iter_records(stream)
        records = (parse_properties(stream, record) for record in records)
        header = next(records)
        parse_header(header)
        header.check_unknown()
        for properties in records:
            obj = Object(properties=properties)
            owner = obj.properties.get_int("OWNERINDEX")
            objects[owner].children.append(obj)
            objects.append(obj)

    storage_stream = ole.openstream("Storage")
    records = iter_records(storage_stream)
    header = parse_properties(storage_stream, next(records))
    header.check("HEADER", b"Icon storage")
    header.get_int("WEIGHT")
    header.check_unknown()
    storage_files = dict()
    for [type, length] in records:
        if type != 1:
            warn("Unexpected record type {} in Storage".format(type))
            continue
        header = storage_stream.read(1)
        if header != b"\xD0":
            warn("Unexpected Storage record header byte " + repr(header))
            continue
        [length] = storage_stream.read(1)
        filename = storage_stream.read(length)
        pos = storage_stream.tell()
        if storage_files.setdefault(filename, pos) != pos:
            warn("Duplicate Storage record for " + repr(filename))

    streams = set(map(tuple, ole.listdir()))
    streams -= {("FileHeader", ), ("Additional", ), ("Storage", )}
    if streams:
        warn("Extra OLE file streams: " + ", ".join(map("/".join, streams)))

    return (sheet, storage_stream, storage_files)
Exemplo n.º 4
0
def read(file):
    """Parses an Altium ".SchDoc" schematic file and returns a Sheet object
    """
    ole = OleFileIO(file)
    
    stream = ole.openstream("FileHeader")
    records = iter_records(stream)
    records = (parse_properties(stream, record) for record in records)
    header = next(records)
    parse_header(header)
    header.check_unknown()
    
    sheet = Object(properties=next(records))
    objects = [sheet]
    for properties in records:
        obj = Object(properties=properties)
        objects[obj.properties.get_int("OWNERINDEX")].children.append(obj)
        objects.append(obj)
    
    if ole.exists("Additional"):
        stream = ole.openstream("Additional")
        records = iter_records(stream)
        records = (parse_properties(stream, record) for record in records)
        header = next(records)
        parse_header(header)
        header.check_unknown()
        for properties in records:
            obj = Object(properties=properties)
            owner = obj.properties.get_int("OWNERINDEX")
            objects[owner].children.append(obj)
            objects.append(obj)
    
    storage_stream = ole.openstream("Storage")
    records = iter_records(storage_stream)
    header = parse_properties(storage_stream, next(records))
    header.check("HEADER", b"Icon storage")
    header.get_int("WEIGHT")
    header.check_unknown()
    storage_files = dict()
    for [type, length] in records:
        if type != 1:
            warn("Unexpected record type {} in Storage".format(type))
            continue
        header = storage_stream.read(1)
        if header != b"\xD0":
            warn("Unexpected Storage record header byte " + repr(header))
            continue
        [length] = storage_stream.read(1)
        filename = storage_stream.read(length)
        pos = storage_stream.tell()
        if storage_files.setdefault(filename, pos) != pos:
            warn("Duplicate Storage record for " + repr(filename))
    
    streams = set(map(tuple, ole.listdir()))
    streams -= {("FileHeader",), ("Additional",), ("Storage",)}
    if streams:
        warn("Extra OLE file streams: " + ", ".join(map("/".join, streams)))
    
    return (sheet, storage_stream, storage_files)
Exemplo n.º 5
0
def is_hwp5file(filename):
    if not isOleFile(filename):
        return False
    olefile = OleFileIO(filename)
    return olefile.exists('FileHeader')