bool LoadText(OleStorage storage, out string text) { text = string.Empty; if (storage == null) { return(false); } BinaryReader documentReader = GetDocumentStreamReader(storage); if (documentReader == null) { return(false); } int pdcOffset; uint pdcLength; string tableName; GetDataFromFib(documentReader, out tableName, out pdcOffset, out pdcLength); BinaryReader tableReader = GetTableStreamReader(storage, tableName); if (tableReader == null) { return(false); } PieceDescriptorCollection pieces = GetPieceDescriptors(tableReader, pdcOffset, pdcLength); if (pieces == null) { return(false); } int count = pieces.Count; for (int i = 0; i < count; i++) { uint pieceStart; uint pieceEnd; bool isUnicode = pieces.GetPieceFileBounds(i, out pieceStart, out pieceEnd); documentReader.BaseStream.Seek(pieceStart, SeekOrigin.Begin); text += ReadString(documentReader, pieceEnd - pieceStart, isUnicode); } return(true); }
private bool LoadText(out string text) { text = string.Empty; if (NativeMethods.StgIsStorageFile(_Path) != 0) { return(false); } OleStorage oleStorage = OleStorage.CreateInstance(_Path); try { return(LoadText(oleStorage, out text)); } finally { oleStorage.Close(); } }
/// <summary> /// Encryptes the package to a stream, using the given password. /// </summary> /// <param name="packageContents">Plaintext contents of the package.</param> /// <param name="password">Password to use to encrypt.</param> /// <param name="encryptedFilename">Name of the encrypted stream write to</param> public void EncryptToStream(byte[] packageContents, string password, Stream encryptedStream) { byte[] encryptionInfo; byte[] encryptedPackage; EncryptPackage(packageContents, password, out encryptionInfo, out encryptedPackage); OleStorage storage = new OleStorage(); storage.WriteStream(csEncryptionInfoStreamName, encryptionInfo); storage.WriteStream(csEncryptedPackageStreamName, encryptedPackage); storage.Save(encryptedStream); }
/// <summary> /// Reads the encryption information and encrypted package /// Parses the encryption information /// Generates a decrypytion key and validates it against the password /// Decrypts the encrypted package and creates and Packaging.Package. /// </summary> /// <param name="filename">The storage file containing the encrypted package</param> /// <param name="password">The password to decrypt the package</param> /// <returns>System.IO.MemoryStream instance</returns> public MemoryStream DecryptToStream(OleStorage stgRoot, string password) { return CreateStream(DecryptToArray(stgRoot, password)); }
/// <summary> /// Reads the encryption information and encrypted package /// Parses the encryption information /// Generates a decrypytion key and validates it against the password /// Decrypts the encrypted package and creates and Packaging.Package. /// </summary> /// <param name="filename">The storage file containing the encrypted package</param> /// <param name="password">The password to decrypt the package</param> /// <returns>System.IO.Packaging.Package instance</returns> public Package DecryptToPackage(OleStorage stgRoot, string password) { return CreatePackage(DecryptToArray(stgRoot, password)); }
/// <summary> /// Reads the encryption information and encrypted package /// Parses the encryption information /// Generates a decrypytion key and validates it against the password /// Decrypts the encrypted package and creates and Packaging.Package. /// </summary> /// <param name="filename">The storage file containing the encrypted package</param> /// <param name="password">The password to decrypt the package</param> /// <returns>System.IO.Packaging.Package instance</returns> public byte[] DecryptToArray(OleStorage stgRoot, string password) { // TODO: check if the EncryptedPackage exists (if it's a valid doc) encryptedPackage = stgRoot.ReadStream(csEncryptedPackageStreamName); // TODO: check if the EncryptionInfo exists (if it's a valid doc) byte[] encryptionInfo = stgRoot.ReadStream(csEncryptionInfoStreamName); // Delegate the rest to this common function return DecryptInternal(password, encryptionInfo, encryptedPackage); }
/// <summary> /// Validates and opens the storage containing the encrypted package. /// Reads the encryption information and encrypted package /// Parses the encryption information /// Generates a decrypytion key and validates it against the password /// Decrypts the encrypted package and creates and Packaging.Package. /// </summary> /// <param name="filename">The name of the storage file containing the encrypted package</param> /// <param name="password">The password to decrypt the package</param> /// <returns>System.IO.Packaging.Package instance</returns> public byte[] DecryptToArray(byte[] contents, string password) { Console.WriteLine("Open the storage"); OleStorage stgRoot = new OleStorage(contents); return DecryptToArray(stgRoot, password); }
/// <summary> /// Validates and opens the storage containing the encrypted package. /// Reads the encryption information and encrypted package /// Parses the encryption information /// Generates a decrypytion key and validates it against the password /// Decrypts the encrypted package and creates and Packaging.Package. /// </summary> /// <param name="filename">The name of the storage file containing the encrypted package</param> /// <param name="password">The password to decrypt the package</param> /// <returns>System.IO.Packaging.Package instance</returns> public byte[] DecryptToArray(string filename, string password) { Console.WriteLine("Open the storage"); OleStorage stgRoot = new OleStorage(filename); return DecryptToArray(stgRoot, password); }
BinaryReader GetTableStreamReader(OleStorage storage, string tableName) { return(GetStreamReader(storage, tableName)); }
BinaryReader GetDocumentStreamReader(OleStorage storage) { return(GetStreamReader(storage, "WordDocument")); }
bool LoadText(OleStorage storage, out string text) { text = string.Empty; if (storage == null) { return(false); } BinaryReader documentReader = GetDocumentStreamReader(storage); if (documentReader == null) { return(false); } int pdcOffset; uint pdcLength; string tableName; GetDataFromFib(documentReader, out tableName, out pdcOffset, out pdcLength); BinaryReader tableReader = GetTableStreamReader(storage, tableName); if (tableReader == null) { return(false); } PieceDescriptorCollection pieces = GetPieceDescriptors(tableReader, pdcOffset, pdcLength); if (pieces == null) { return(false); } //EDIT: Chris Wilson 04/01/16 //We have had some performance issues with legacy word docs, and I suspect that //it's the use of overloaded operator concatenation in the below loop //rather than using a stringbuilder //change is to address this speed issue as we are encountering documents with word counts //in the several 100ks //ADDED: as per comment above StringBuilder builder = new StringBuilder(); int count = pieces.Count; for (int i = 0; i < count; i++) { uint pieceStart; uint pieceEnd; bool isUnicode = pieces.GetPieceFileBounds(i, out pieceStart, out pieceEnd); documentReader.BaseStream.Seek(pieceStart, SeekOrigin.Begin); //REMOVED: Chris Wilson as per comment above //text += ReadString(documentReader, pieceEnd - pieceStart, isUnicode); //ADDED: Chris Wilson as per comment above builder.Append(ReadString(documentReader, pieceEnd - pieceStart, isUnicode)); } //ADDED: Chris Wilson as per comment above text = builder.ToString(); return(true); }