예제 #1
0
		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;
			}
		}
예제 #2
0
		private bool InitialiserMethodForPPTFile()
		{
			StructuredStorageInterface.IStorage iStorage;
			IStream encryptedStream;

			encryptedStream = GetStream(out iStorage, "EncryptedSummary");
			using (new ComObjectGovernor(encryptedStream))
			using (new ComObjectGovernor(iStorage))
			{
				if (null != encryptedStream)
				{
					m_fileData.FileEncrypted = true;
					m_fileData.FileType = FileType.PowerPoint;
					m_fileData.ReadPasswordProtected = true;

					m_impl = new FileImpl(m_fileData);
					return true;
				}
			}

			IStream iStream = GetStream(out iStorage, "PowerPoint Document");

			using (new ComObjectGovernor(iStream))
			using (new ComObjectGovernor(iStorage))
			{
				if (null == iStream)
					return false;

				m_fileData.FileType = FileType.PowerPoint;

                if (PptStreamContainsEncryptedLabel(iStream))//encrypted .ppt created via PP2013 for some reason do not have "EncryptedSummary" stream
                {
                    m_fileData.FileEncrypted = true;
                    m_fileData.ReadPasswordProtected = true;
                }

				m_impl = new FileImpl(m_fileData);
				return true;
			}
		}
예제 #3
0
		private bool InitialiseMethodForEncryptedXlsxFileOnDisk()
		{
			StructuredStorageInterface.IStorage iStorage;
			//make sure it isn't an encrypted docx/docm
			IStream iStream = GetStream(out iStorage, "WordDocument");
            using (new ComObjectGovernor(iStream))
            using (new ComObjectGovernor(iStorage))
            {
                if (null != iStream)
                    return false;
            }

			using (new ComObjectGovernor(iStorage))
			{
                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(iStream1))
                using (new ComObjectGovernor(iStorage))
                {
                    bIstream2 = iStream2 == null ? false : true;
                }

				string filename = m_fileData.Filename.ToLower();
				//encrypted pptx/pptm have these streams as well, but there is no
				//distinguishing characteristics that I have been able to identify
				//so can but check the extensions
				if ((bIstream1 && bIstream2) &&
					(filename.EndsWith("xlsx", StringComparison.InvariantCultureIgnoreCase) 
                    || filename.EndsWith("xlsm", StringComparison.InvariantCultureIgnoreCase)
                    || filename.EndsWith("xls", StringComparison.InvariantCultureIgnoreCase)
                    ))
				{
					m_fileData.FileType = FileType.ExcelSheetX;
					m_fileData.ReadPasswordProtected = true;
					m_fileData.WritePasswordProtected = true;

					m_impl = new FileImpl(m_fileData);

					return true;
				}
			}
			return false;
		}
예제 #4
0
        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;
            //}
        }
예제 #5
0
		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;
		}
예제 #6
0
		private bool InitialiserMethodForXlsXFile()
		{
			if (!IsZipFile())
			{
                return Workshare.Interop.Options.OptionApi.GetBool("UseDiskBasedFiles") ? InitialiseMethodForEncryptedXlsxFileOnDisk() :
                                                                                          InitialiseMethodForEncryptedXlsxFile();
			}

			using (Stream stream = m_fileData.BinaryFileData.AsStream())
			using (ZipEntryFinder zipEntryFinder = new ZipEntryFinder(stream, false))
			{
				if (!zipEntryFinder.HasEntry("[content_types].xml"))
					return false;

				ZipEntry zipEntry = zipEntryFinder.GetEntry(@"xl\workbook.xml");
				if (zipEntry == null)
					return false;
				//Method to look at the XML within the document and determine the correct file type.
				//If we don't find a file type, then we assume that the type is ExcelSheetX
				FileType type = DetermineFileTypeForOfficeXMLFiles(zipEntryFinder, stream);
				switch (type)
				{
				case FileType.Unknown:
					type = FileType.ExcelSheetX;
					break;
				//Office 2003 compatibility packs don't support 2007 template types...so we don't either.
				case FileType.ExcelSheetTemplateX:
				case FileType.ExcelSheetMacroTemplateX:
					if (OfficeApplicationVersion.IsWord2003())
					{
						type = FileType.Unknown;
					}
					break;
				}
				m_fileData.FileType = type;
				GetXlsXWriteProtection(zipEntry, stream);
				GetXlsXSheetsProtection(zipEntryFinder, stream);
				m_impl = new FileImpl(m_fileData);

				Logger.LogInfo(".xlsx;.xlsm;.xltx;.xltm file detected");

				return true;
			}
		}
예제 #7
0
		[InitialiserMethod(Order = 13, Extensions = ".txt")] //Text must run after everything else
		private bool InitialiserMethodForTextFile()
		{
			if (0 == m_fileData.BinaryFileData.Length)
				return false;

			if (m_fileData.Encoding == System.Text.Encoding.Unicode ||
				m_fileData.Encoding == System.Text.Encoding.BigEndianUnicode)
			{
				m_fileData.FileType = FileType.TextDocument;
				m_impl = new FileImpl(m_fileData);
			}
			else
			{

				int ibyte = 0;
				while (ibyte < Math.Min(m_fileData.BinaryFileData.Length, 2048))
				{
					byte currentByte = m_fileData.BinaryFileData[ibyte];
					if ((currentByte > 0) && (currentByte < 9))
						return false;

					ibyte++;
				}

				m_fileData.FileType = FileType.TextDocument;
				m_impl = new FileImpl(m_fileData);
			}

			return true;
		}
예제 #8
0
		private bool InitialiserMethodForXMLFile()
		{
			if (!Utils.LooksLikeXML(m_fileData.BinaryFileData))
				return false;

			byte[] byteArray = new byte[4];
			using (Stream str = m_fileData.BinaryFileData.AsStream())
			{
				str.Read(byteArray, 0, 4);
				Encoding encoding = Utils.FindEncoding(ref byteArray);
				string stringData = m_fileData.BinaryFileData.AsString(10240, encoding);

				string xmlPattern = @"(?i)<(.*)\?(.*)xml(.*)version";

				// is xml
				if (!System.Text.RegularExpressions.Regex.IsMatch(stringData, xmlPattern))
					return false;
			}

			try
			{
				using (Stream stream = m_fileData.BinaryFileData.AsStream())
				{
					XmlDocument document = new XmlDocument();
					document.XmlResolver = null;
					document.Load(stream);
				}
			}
			catch (XmlException)
			{
				// Not well formated.
				return false;
			}


			m_fileData.FileType = FileType.XMLDocument;

			m_impl = new FileImpl(m_fileData);

			return true;
		}
예제 #9
0
		private bool InitialiserMethodForHTMLFile()
		{
			bool ishtml = Utils.IsDataHTML(m_fileData.BinaryFileData);
			if (ishtml)
			{
				m_fileData.FileType = FileType.HTMLDocument;
			}

			m_impl = new FileImpl(m_fileData);

			return ishtml;
		}
예제 #10
0
		private bool InitialiserMethodForRTFFile()
		{
			if (!IsRtfFile(m_fileData.BinaryFileData))
			{
				return false;
			}

			m_fileData.FileType = FileType.RTFDocument;
			GetRtfDocumentProtection();
			m_impl = new FileImpl(m_fileData);

			return true;
		}
예제 #11
0
		private bool InitialiserMethodForPdfFile()
		{
			if (5 >= m_fileData.BinaryFileData.Length)
				return false;

			BinaryData binaryData = m_fileData.BinaryFileData;
            if (((0x25 == binaryData[0]) && (0x50 == binaryData[1]) && (0x44 == binaryData[2]) && (0x46 == binaryData[3])) ||
                IsPdfFileWithHtmlTags(binaryData))
            {
                m_fileData.FileType = FileType.PDFDocument;
                try
                {
                    using (FCS.Lite.PdfDocumentReader pdfReader = new FCS.Lite.PdfDocumentReader(m_fileData.Filename))
                    {
                        m_fileData.ReadPasswordProtected = pdfReader.IsEncrypted();
                    }
                }
                catch (System.Exception)
                {
                    m_fileData.ReadPasswordProtected = true;
                }

                m_impl = new FileImpl(m_fileData);
                return true;
            }
            return false;
		}