OleFileIO_PL: Experimental write features

Since version 0.32, OleFileIO_PL comes with experimental write features. For now it is possible to write sectors, and to write over an existing stream. More features will be added over time.

Before OleFileIO_PL v0.32 is released, you may get the latest code from Bitbucket using Mercurial, or use the link "Download Repository" from the dowload page.

This code is brand new and has not been thoroughly tested yet. Please send me your feedback or report issues.

Open an OLE file in write mode

Before using the write features, the OLE file must be opened in read/write mode:

    import OleFileIO_PL
    ole = OleFileIO_PL.OleFileIO('test.doc', write_mode=True)

Overwriting a sector

The write_sect method can overwrite any sector of the file. If the provided data is smaller than the sector size (normally 512 bytes, sometimes 4KB), data is padded with null characters.

Here is an example:

    ole.write_sect(0x17, b'TEST')

Note: following the MS-CFB specifications, sector 0 is actually the second sector of the file. You may use -1 as index to write the first sector.

Overwriting a stream

The write_stream method can overwrite an existing stream in the file. The new stream data must be the exact same size as the existing one. For now, write_stream can only write streams of 4KB or larger (stored in the main FAT).

For example, you may change text in a MS Word document:

    ole = OleFileIO_PL.OleFileIO('test.doc', write_mode=True)
    data = ole.openstream('WordDocument').read()
    data = data.replace(b'foo', b'bar')
    ole.write_stream('WordDocument', data)
    ole.close()

Next steps

See issue #6 for more details. The next features to be implemented should be:

  1. write a single FAT sector in the file. => done in v0.32 (commit 54c820c)
  2. write stream data for an existing stream in FAT, same size. => done in v0.32 (commit ef34c24)
  3. write a single MiniFAT sector in the file
  4. write stream data for an existing stream in MiniFAT, same size.
  5. write the header
  6. write the FAT (and DIFAT), same size
  7. write the MiniFAT, same size
  8. write the directory, same size
  9. rename a stream/storage (no change to red-black tree)
  10. release a sector as unused
  11. allocate a new sector, extending the OLE file size and/or the FAT/MiniFAT if necessary
  12. trim down the file, removing unused sectors at the end
  13. write stream data for an existing stream, changing its size
  14. delete a stream/storage (requires to update the red-black tree)
  15. add a new storage in the directory
  16. add a new stream
  17. create a new OLE file from scratch