private bool InitialiserMethodForWordFileOnDisk() { StructuredStorageInterface.IStorage iStorage; IStream iStream = GetStream(out iStorage, "WordDocument"); if (null == iStream) return false; StreamWrapper stream; FIB fib; DOPHeader dopHeader; using (new ComObjectGovernor(iStream)) using (new ComObjectGovernor(iStorage)) { stream = new StreamWrapper(iStream); fib = new FIB(); dopHeader = new DOPHeader(); stream.GetDOP(ref dopHeader); object fibObject = fib as object; stream.GetHeaderStructure(ref fibObject); fib = (FIB)fibObject; } // \ / // .\-/. // /\ () () /\ // / \ /~-~\ / \ // y Y V // ,-^-./ | \,-^-. /// { | } \ // \ | / // /\ A /\ // / \/ \/ \ // / \ // // int numLols = 1 // increment every time you see this for the first time if (fib.magicNumber != 0) //THIS ZERO-CHECK IS A HACK FOR PROTECT MOBILE - IT SHOULD BE REMOVED ASAP { //THIS is the proper code to check for Word file versioning. This should stay. if (fib.magicNumber != 0xA5DC && fib.magicNumber != 0xA5EC) return false; } //we will get this far with a docx/docm if it is encrypted but the FIB is empty then so we can drop out now if (fib.bitData == 0 && fib.lid == 0 && fib.magicNumber == 0 && fib.version == 0 && fib.nProduct == 0) { return false; } m_fileData.FileType = FileType.WordDocument; if ((fib.bitData & StreamWrapper.fEncrypted) == StreamWrapper.fEncrypted) m_fileData.ReadPasswordProtected = true; if ((fib.bitData & StreamWrapper.fWriteReservation) == StreamWrapper.fWriteReservation) m_fileData.WritePasswordProtected = true; // only try to find the document protection flags within the DOP if we know that the // file is not encrypted. Otherwise the offsets are not valid. if (!m_fileData.ReadPasswordProtected && !m_fileData.WritePasswordProtected) { //DOPHeader dopHeader = new DOPHeader(); //stream.GetDOP(ref dopHeader); m_fileData.DocumentProtected = HasDocumentProtection(fib, dopHeader); } m_impl = new FileImpl(m_fileData); return true; //} }
private bool InitialiserMethodForExcelFile() { StructuredStorageInterface.IStorage iStorage; IStream iStream = GetStream(out iStorage, "Workbook"); if (null == iStream) { iStream = GetStream(out iStorage, "Book"); if (null == iStream) return false; } m_fileData.FileType = FileType.ExcelSheet; using (new ComObjectGovernor(iStream)) using (new ComObjectGovernor(iStorage)) { StreamWrapper stream = new StreamWrapper(iStream); XLBOF xlbof = new XLBOF(); object bofObject = xlbof; stream.GetHeaderStructure(ref bofObject); xlbof = (XLBOF) bofObject; if (xlbof.vers == 0x08) { switch (xlbof.vers2) { case 0x0500: break; case 0x0600: break; case 0x1333: break; default: return false; } } StructuredStorageInterface.IPropertyStorage ipropertyStorage = GetPropertyStorage(iStorage, StructuredStorageInterface.FMTID_SummaryInformation); if (ipropertyStorage != null) // Not all excel documents have summary information // Content detail reports only have a Workbook section. { using (new ComObjectGovernor(ipropertyStorage)) using (new ComObjectGovernor(iStorage)) { StructuredStorageInterface.IEnumSTATPROPSTG iEnumSTATPROPSTG; ipropertyStorage.Enum(out iEnumSTATPROPSTG); if (null != iEnumSTATPROPSTG) { StructuredStorageInterface.STATPROPSTG statprop; while (1 == iEnumSTATPROPSTG.Next(1, out statprop)) { if (statprop.PROPID == StructuredStorageInterface.PIDSI_DOC_SECURITY) { StructuredStorageInterface.PROPSPEC propspec = new StructuredStorageInterface.PROPSPEC { PROPID = StructuredStorageInterface.PIDSI_DOC_SECURITY, ulKind = 1 }; try { StructuredStorageInterface.PROPVARIANT propval; ipropertyStorage.ReadMultiple(1, ref propspec, out propval); if (1 == propval.value) { m_fileData.ReadPasswordProtected = true; } } catch (COMException) { // above check can throw in 64 bit, not sure what to do about it } } } } } } // Check if file is also write protected... // (A write protected workbook that is not read protected, will not have the PIDSI_DOC_SECURITY flag) m_fileData.WritePasswordProtected = ExcelDocumentReader.IsWriteProtected(m_fileData.BinaryFileData); m_impl = new FileImpl(m_fileData); return true; } }
private bool InitialiseMethodForEncryptedDocXFileOnDisk() { StructuredStorageInterface.IStorage iStorage; IStream iStream = GetStream(out iStorage, "WordDocument"); bool bIstream = false; using (new ComObjectGovernor(iStream)) using (new ComObjectGovernor(iStorage)) { bIstream = iStream == null ? false : true; } if (!bIstream && (iStorage == null)) return false; // Office2007 SP2 does not have "WordDocument" stream if (!bIstream && (iStorage != null)) { bool bIstream1 = false; IStream iStream1 = GetStream(out iStorage, "EncryptionInfo"); using (new ComObjectGovernor(iStream1)) using (new ComObjectGovernor(iStorage)) { bIstream1 = iStream1 == null ? false : true; } bool bIstream2 = false; IStream iStream2 = GetStream(out iStorage, "EncryptedPackage"); using (new ComObjectGovernor(iStream2)) using (new ComObjectGovernor(iStorage)) { bIstream2 = iStream2 == null ? false : true; } string filename = m_fileData.Filename.ToLower(); if ((bIstream1 && bIstream2) && (filename.EndsWith("docx", StringComparison.InvariantCultureIgnoreCase) || filename.EndsWith("docm", StringComparison.InvariantCultureIgnoreCase) || filename.EndsWith("doc", StringComparison.InvariantCultureIgnoreCase))) { m_fileData.FileType = FileType.WordDocumentX; m_fileData.ReadPasswordProtected = true; m_fileData.WritePasswordProtected = true; m_impl = new FileImpl(m_fileData); return true; } else return false; } iStream = GetStream(out iStorage, "WordDocument"); FIB fib = new FIB(); using (new ComObjectGovernor(iStream)) using (new ComObjectGovernor(iStorage)) { StreamWrapper stream = new StreamWrapper(iStream); object fibObject = fib as object; stream.GetHeaderStructure(ref fibObject); fib = (FIB) fibObject; } if (fib.bitData == 0 && fib.lid == 0 && fib.magicNumber == 0 && fib.version == 0 && fib.nProduct == 0) { bool bIstream1 = false; IStream iStream1 = GetStream(out iStorage, "EncryptionInfo"); using (new ComObjectGovernor(iStream1)) using (new ComObjectGovernor(iStorage)) { bIstream1 = iStream1 == null ? false : true; } bool bIstream2 = false; IStream iStream2 = GetStream(out iStorage, "EncryptedPackage"); using (new ComObjectGovernor(iStream2)) using (new ComObjectGovernor(iStorage)) { bIstream2 = iStream2 == null ? false : true; } if (bIstream1 && bIstream2) { m_fileData.FileType = FileType.WordDocumentX; m_fileData.ReadPasswordProtected = true; m_fileData.WritePasswordProtected = true; m_impl = new FileImpl(m_fileData); return true; } } return false; }