public void TestFileCtor_LongDisplayName() { BinaryData data = new BinaryData(new MemoryStream(new byte[] {79, 5, 15})); //any old byte array try { File file = new File(data, "ShortIsOK"); } catch { Assert.Fail("Did not expect this ctor to fail"); } try { StringBuilder tooLong = new StringBuilder(); string guid = Guid.NewGuid().ToString(); for( int i = 0; i < 100; i++) { tooLong.Append(guid); } //Why do we care about long names? //Because the File code calls a framework class that tells us if the name looks like a file path. //That code (Path.GetPathRoot()) can throw on 'long' filenames. Since there is nothing to stop display names //being longer than the legal limit of file paths, the File code should not blow up in this case. File file = new File(data, tooLong.ToString()); } catch { Assert.Fail("Did not expect this ctor to fail"); } }
public DocxDocumentReader(Workshare.Policy.BinaryData data) : base(false) { m_data = data; m_data.LockInMemory(); m_myData = false; }
public ZipOpener(BinaryData inputData, string password) { m_zip = new ZipWrapper(); m_zip.SetPassword(password); if (inputData.Length >= 85000) { m_fileName = Path.GetTempFileName(); inputData.Save(m_fileName); if (!m_zip.OpenZip(m_fileName)) { throw new System.Exception(m_zip.LastErrorText); } } else { using (Stream str = inputData.AsStream()) { byte[] data = StreamUtils.SafeButUnpleasantReadAllStreamIntoByteArray(str); if (!m_zip.OpenFromMemory(data)) { throw new System.Exception(m_zip.LastErrorText); } } } }
public LockBytesWrapper(BinaryData data) { try { m_data = data; data.LockInMemory(); if (data is OptimizedBinaryData) { OptimizedBinaryData optData = data as OptimizedBinaryData; m_lockbytesinterface = optData.AsLockBytes() as StructuredStorageInterface.ILockBytes; } else { int len = data.Length; m_hglobalHandle = Marshal.AllocHGlobal(len); data.CopyTo(m_hglobalHandle); StructuredStorageInterface.CreateILockBytesOnHGlobal(m_hglobalHandle, false, out m_lockbytesinterface); } } catch (Exception) { // if we throw in here, we most likely wont get disposed properly (the new this won't be assigned to anything) // this becomes an issue because we have locked the binary data in memory and wont be releasing it // so to keep things tidy and make sure we don't get spurious errors we will release the binary data object // and tidy up ourselves in case of an exception Dispose(true); throw; } }
public DocxDocumentReader(string filename) : base(filename) { if (filename == null || 0 == filename.Length) return; m_data = new BinaryData(filename); m_data.LockInMemory(); }
public static bool LooksLikeXML(BinaryData binaryData) { if (LooksLikeXML(DataAsString(binaryData, Encoding.Default))) return true; if (LooksLikeXML(DataAsString(binaryData, Encoding.Unicode))) return true; return false; }
public RarOpener(BinaryData inputData) { m_rar = new Chilkat.Rar(); m_fileName = Path.GetTempFileName(); inputData.Save(m_fileName); if (!m_rar.Open(m_fileName)) { throw new Exception(m_rar.LastErrorText); } }
protected virtual void Dispose(bool disposing) { if (disposing) { if (_data != null) { _data.Dispose(); } GC.SuppressFinalize(this); _data = null; } }
/// <summary> /// Constructor that takes a byte[] as a parameter /// </summary> /// <param name="memoryfile"></param> /// <param name="displayname"></param> public File(BinaryData memoryfile, string displayname) { string filename = displayname; SetDisplayName(displayname); m_fileData.Filename = filename; if (null == memoryfile) return; m_fileData.BinaryFileData = memoryfile; InitialiseFileProperties(true); }
public static bool IsDataHTML(BinaryData fileData) { byte[] byteArray = new byte[4]; using (Stream str = fileData.AsStream()) { str.Read(byteArray, 0, 4); Encoding encoding = FindEncoding(ref byteArray); string dataString = fileData.AsString(10240, encoding).ToLowerInvariant(); // Trim off byte order marks from the beginning of the string dataString = dataString.TrimStart(new char[] { '\xFEFF', '\xFFFE' }); return IsStringHTML(dataString); } }
public FileData(SerializationInfo info, StreamingContext ctxt) { m_filename = (string)info.GetValue("m_filename", typeof(string)); m_displayname = (string)info.GetValue("m_displayname", typeof(string)); m_contentId = (string)info.GetValue("m_contentId", typeof(string)); m_password = (string)info.GetValue("m_password", typeof(string)); m_readpasswordprotected = (bool)info.GetValue("m_readpasswordprotected", typeof(bool)); m_writepasswordprotected = (bool)info.GetValue("m_writepasswordprotected", typeof(bool)); m_documentProtected = (bool)info.GetValue("m_documentProtected", typeof(bool)); m_fileEncrypted = (bool)info.GetValue("m_fileEncrypted", typeof(bool)); m_readonly = (bool)info.GetValue("m_readonly", typeof(bool)); m_filetype = (FileType)info.GetValue("m_filetype", typeof(FileType)); byte[] contents = (byte[])info.GetValue("m_binaryFileData", typeof(byte[])); if (null != contents) { using (MemoryStream stream = new MemoryStream(contents)) { m_binaryFileData = new FCS.Lite.Support.OptimizedBinaryData(stream); } } }
private static string DataAsString(BinaryData binaryData, Encoding encoding) { if (null == binaryData) return string.Empty; return binaryData.AsString(16, encoding); }
protected virtual void Dispose(bool disposing) { lock (this) { if (m_bdisposed) return; m_bdisposed = true; } if (disposing) GC.SuppressFinalize(this); if (null != m_hglobalHandle) Marshal.FreeHGlobal(m_hglobalHandle); m_hglobalHandle = new IntPtr(0) ; if (null != m_lockbytesinterface) Marshal.ReleaseComObject(m_lockbytesinterface); m_lockbytesinterface = null; if (m_data != null) m_data.ReleaseFromMemory(); m_data = null; }
// this is for testing only internal RequestAttachment(byte[] data) { _data = new BinaryData(data); }
public DocxDocumentReader(byte[] byteArray) : base(byteArray) { m_data = new BinaryData(new MemoryStream(byteArray)); m_data.LockInMemory(); }
public static bool IsRtfFile(BinaryData data) { if (data.Length <= 5) { return false; } try { string headerstring = data.AsString(5, Encoding.UTF8); return headerstring.CompareTo(@"{\rtf") == 0; } catch (Exception) { return false; } }
public static bool IsOutlookPostType(BinaryData binaryData) { return IsMessageClassType(binaryData, "IPM.Post"); }
public static bool IsOutlookMSGType(BinaryData binaryData) { return IsMessageClassType(binaryData, "IPM.Note"); }
public static bool IsMessageClassType(BinaryData binaryData, string messageClass) { if (Workshare.Interop.Options.OptionApi.GetBool("UseDiskBasedFiles")) { return String.Compare(messageClass, GetMessageClassOnDisk(binaryData), true) == 0; } else { return String.Compare(messageClass, GetMessageClassNotOnDisk(binaryData), true) == 0; } }
private static string GetMessageClassOnDisk(BinaryData binaryData) { StructuredStorageInterface.IStorage iStorage = null; uint iRet = StructuredStorageInterface.StgOpenStorage(binaryData.FileName, null, StructuredStorageInterface.ReadOnlyMode, System.IntPtr.Zero, 0, out iStorage); if (0 != iRet) { ComObjectGovernor.ReleaseObject(iStorage); return String.Empty; } IStream iStream = null; Encoding encoding = Encoding.ASCII; try { iStream = iStorage.OpenStream("__substg1.0_001A001F", IntPtr.Zero, Convert.ToInt32(StructuredStorageInterface.ReadOnlyMode), 0); encoding = Encoding.Unicode; } catch (System.Runtime.InteropServices.COMException) { try { iStream = iStorage.OpenStream("__substg1.0_001A001E", IntPtr.Zero, Convert.ToInt32(StructuredStorageInterface.ReadOnlyMode), 0); } catch (System.Runtime.InteropServices.COMException) { ComObjectGovernor.ReleaseObject(iStorage); return String.Empty; } } try { System.Runtime.InteropServices.ComTypes.STATSTG stat; iStream.Stat(out stat, 0); IntPtr read = IntPtr.Zero; byte[] contents = new byte[stat.cbSize]; iStream.Read(contents, (int)stat.cbSize, read); return encoding.GetString(contents); } finally { ComObjectGovernor.ReleaseObject(iStream); ComObjectGovernor.ReleaseObject(iStorage); } }
bool IsPdfFileWithHtmlTags(BinaryData binaryData) { var content = binaryData.AsString(1024, Encoding.ASCII).ToLower(); // Find <html>...</html>%PDF-1.4 // Expect file content to start with html open tag // Expect pdf identifier to follow the html close tag return (content.StartsWith("<html>") && content.Contains("</html>%pdf")); }
public bool Represents(BinaryData data) { return m_data == data; }
public XlsxDocumentReader(string filename) : base(filename) { m_data = new BinaryData(filename); m_data.LockInMemory(); }