internal static bool Match(IByteContainer container, int offset, object[] values) { long realOffset = offset; if (realOffset < 0) { realOffset += container.Length; } if (realOffset < 0) { realOffset = 0; } byte[] fromContainer = container.Read(realOffset, values.Length); if (values.Length != fromContainer.Length) { return(false); } for (int index = 0; index < values.Length; index++) { object value = values[index]; byte[] bytes = value as byte[]; if (bytes != null) { if (!bytes.Any(b => fromContainer[index] == b)) { return(false); } } else if (value is int) { var i = (int)value; if (i != -1 && fromContainer[index] != i) { return(false); } } } return(true); }
/// <summary> /// Identifies the contents of the specified container. /// </summary> /// <param name="container"> /// The <see cref="IByteContainer"/> with the contents to identify. /// </param> /// <returns> /// An collection of <see cref="ContentFormat"/> objects that identify the <paramref name="container"/> contents. /// </returns> /// <remarks> /// Note that the returned collection contains the <see cref="ContentFormat"/> objects /// ordered in such a way that the first one is deemed the one most likely to be /// correct, the next one less likely, the next one after that even less likely, /// and so on. /// </remarks> /// <exception cref="ArgumentNullException"> /// <para><paramref name="container"/> is <c>null</c>.</para> /// </exception> public ContentFormat[] Identify(IByteContainer container) { if (container == null) { throw new ArgumentNullException("container"); } var formats = new List <ContentFormat>(); foreach (Type type in _Identifiers) { var identifier = (IContentIdentifier)Activator.CreateInstance(type); using (identifier as IDisposable) { formats.AddRange(identifier.Identify(container)); } } formats = formats.OrderBy(f => f.ToString()).ToList(); // combine duplicates int index = 0; while (index < formats.Count - 1) { if (formats[index].IsSameFormat(formats[index + 1])) { formats[index] = ContentFormat.Combine(formats[index], formats[index + 1]); formats.RemoveAt(index + 1); } else { index++; } } return(formats.OrderByDescending(f => f.Confidence).ToArray()); }
public void Append(IByteContainer bytes) { _data.Append(bytes); }
/// <summary> /// /// </summary> /// <param name="sbBuffered"></param> /// <param name="currentLineLength"></param> /// <param name="previousLineLength"> /// is needed because when we change the start of a line, the starting position of the next line /// will be relative to this new position /// </param> /// <returns></returns> private IByteContainer AlignRight(IByteContainer sbBuffered, double currentLineLength, ref double previousLineLength) { double xOffset; if (previousLineLength < 0) xOffset = _size.Width - (currentLineLength); else xOffset = (previousLineLength - currentLineLength); sbBuffered.Append(string.Format("{0:0.###} 0 Td ", xOffset).Replace(',', '.')); previousLineLength = currentLineLength; return sbBuffered; }
public IEnumerable <ContentFormat> Identify(IByteContainer container) { var toRead = (int)Math.Min(container.Length, 1024); byte[] bytes = container.Read(0, toRead); if (bytes.Length >= 4 && bytes[0] == 0 && bytes[1] == 0 && bytes[2] == 0xfe && bytes[3] == 0xff) { // UTF-32, big-endian var decoder = new UTF32Encoding(true, false, false); var chars = new char[2048]; int decoded = decoder.GetChars(bytes, 4, bytes.Length - 4, chars, 0); if (decoded > (int)(((toRead - 4) / 4) * 0.9)) { yield return(CreateTextFormat("utf-32/big-endian", decoder, bytes, (decoded * 4) + 4)); yield break; } } else if (bytes.Length >= 4 && bytes[0] == 0 && bytes[1] == 0 && bytes[2] == 0xfe && bytes[3] == 0xff) { // UTF-32, little-endian var decoder = new UTF32Encoding(false, false, false); var chars = new char[2048]; int decoded = decoder.GetChars(bytes, 4, bytes.Length - 4, chars, 0); if (decoded > (int)(((toRead - 4) / 4) * 0.9)) { yield return(CreateTextFormat("utf-32/little-endian", decoder, bytes, (decoded * 4) + 4)); yield break; } } else if (bytes.Length >= 2 && bytes[0] == 0xfe && bytes[1] == 0xff) { // UTF-16, big-endian var decoder = new UnicodeEncoding(true, false, false); var chars = new char[2048]; int decoded = decoder.GetChars(bytes, 2, bytes.Length - 2, chars, 0); if (decoded > (int)(((toRead - 2) / 2) * 0.9)) { yield return(CreateTextFormat("utf-16/big-endian", decoder, bytes, (decoded * 2) + 2)); yield break; } } else if (bytes.Length >= 2 && bytes[0] == 0xff && bytes[1] == 0xfe) { // UTF-16, little-endian var decoder = new UnicodeEncoding(false, false, false); var chars = new char[2048]; int decoded = decoder.GetChars(bytes, 2, bytes.Length - 2, chars, 0); if (decoded > (int)(((toRead - 2) / 2) * 0.9)) { yield return(CreateTextFormat("utf-16/little-endian", decoder, bytes, (decoded * 2) + 2)); yield break; } } else if (bytes.Length >= 3 && bytes[0] == 0xef && bytes[1] == 0xbb && bytes[2] == 0xbf) { // UTF-8 var decoder = new UTF8Encoding(false, false); var chars = new char[2048]; int decoded = decoder.GetChars(bytes, 3, bytes.Length - 3, chars, 0); if (decoded > (int)(((toRead - 3) / 2) * 0.8)) { yield return(CreateTextFormat("utf-8", decoder, bytes, (decoded * 2) + 2)); yield break; } } var encoders = new Dictionary <string, Encoding> { { "utf-8", new UTF8Encoding(true, true) }, { "cp1252", Encoding.GetEncoding("Windows-1252") }, { "iso-8859-1", Encoding.GetEncoding("iso-8859-1") }, { "utf-16/big-endian", new UnicodeEncoding(true, false, true) }, { "utf-16/little-endian", new UnicodeEncoding(false, false, true) }, { "utf-32/big-endian", new UTF32Encoding(true, false, true) }, { "utf-32/little-endian", new UTF32Encoding(false, false, true) }, }; foreach (var encoder in encoders) { byte[] bytes2; try { string s = encoder.Value.GetString(bytes, 0, bytes.Length); bytes2 = encoder.Value.GetBytes(s.Substring(0, (int)(s.Length * 0.9))); } catch { continue; } if (bytes2.Length == 0) { continue; } if (bytes.Any(b => b == 0)) { continue; } bool isMatch = true; for (int index = 0; index < bytes2.Length; index++) { if (bytes[index] != bytes2[index]) { isMatch = false; break; } } if (isMatch) { yield return(CreateTextFormat(encoder.Key, encoder.Value, bytes, bytes2.Length)); yield break; } } }
private static IByteContainer AlignLeft(IByteContainer sbBuffered, int horizontalMargin, int yPosition, ref double previousLineLength) { if (previousLineLength >= 0) { sbBuffered.Append(string.Format("1 0 0 1 {0} {1} Tm ", horizontalMargin, yPosition)); previousLineLength = -1; } sbBuffered.Append("100 Tz "); return sbBuffered; }
private IByteContainer AlignJustify(IByteContainer sbBuffered, double currentLineLength, bool isLastLineOfParagraph, int horizontalMargin, int yPosition, ref double previousLineLength) { if (previousLineLength >= 0) { sbBuffered.Append(string.Format("1 0 0 1 {0} {1} Tm ", horizontalMargin, yPosition)); previousLineLength = -1; } if (currentLineLength > 0) { double scaleFactor; if (isLastLineOfParagraph) scaleFactor = 100; else scaleFactor = _size.Width / currentLineLength * 100; sbBuffered.Append(string.Format("{0:0.###} Tz ", scaleFactor).Replace(',', '.')); } return sbBuffered; }
public IEnumerable <ContentFormat> Identify(IByteContainer container) { bool isMatch; // archive/zip (application/zip) .zip isMatch = Match( container, 0x00000000, new object[] { 0x50, 0x4b, 0x03, 0x04 }); if (isMatch) { yield return(new ContentFormat("archive", "zip", string.Empty, 4, "application/zip", ".zip")); } // archive/7z (application/x-7z-compressed) .7z isMatch = Match( container, 0x00000000, new object[] { 0x37, 0x7a, 0xbc, 0xaf, 0x27, 0x1c }); if (isMatch) { yield return(new ContentFormat("archive", "7z", string.Empty, 6, "application/x-7z-compressed", ".7z")); } // archive/cab (application/vnd.ms-cab-compressed) .cab isMatch = Match( container, 0x00000000, new object[] { 0x4d, 0x53, 0x43, 0x46, 0x00, 0x00, 0x00, 0x00 }); if (isMatch) { yield return(new ContentFormat("archive", "cab", string.Empty, 8, "application/vnd.ms-cab-compressed", ".cab")); } // archive/bzip/2 (application/x-bzip) .bz2 isMatch = Match( container, 0x00000000, new object[] { 0x42, 0x5a, 0x68, -1, 0x31, 0x41, 0x59, 0x26, 0x53, 0x59 }); if (isMatch) { yield return(new ContentFormat("archive", "bzip", "2", 9, "application/x-bzip", ".bz2")); } // archive/bzip/1 .bz isMatch = Match( container, 0x00000000, new object[] { 0x42, 0x5a, 0x30, -1, 0x31, 0x41, 0x59, 0x26, 0x53, 0x59 }); if (isMatch) { yield return(new ContentFormat("archive", "bzip", "1", 9, string.Empty, ".bz")); } // archive/gzip (application/x-gzip) .gz isMatch = Match( container, 0x00000000, new object[] { 0x1f, 0x8b }); if (isMatch) { yield return(new ContentFormat("archive", "gzip", string.Empty, 2, "application/x-gzip", ".gz")); } // archive/wim .wim isMatch = Match( container, 0x00000000, new object[] { 0x4d, 0x53, 0x57, 0x49, 0x4d, 0x00 }); if (isMatch) { yield return(new ContentFormat("archive", "wim", string.Empty, 6, string.Empty, ".wim")); } // archive/xz (application/x-xz) .xm isMatch = Match( container, 0x00000000, new object[] { 0xfd, 0x37, 0x7a, 0x58, 0x5a, 0x00 }); if (isMatch) { yield return(new ContentFormat("archive", "xz", string.Empty, 6, "application/x-xz", ".xm")); } // archive/iso (application/x-iso9660-image) .iso isMatch = Match( container, 0x00008000, new object[] { -1, 0x43, 0x44, 0x30, 0x30, 0x31 }); if (isMatch) { yield return(new ContentFormat("archive", "iso", string.Empty, 5, "application/x-iso9660-image", ".iso")); } // archive/iso (application/x-iso9660-image) .iso isMatch = Match( container, 0x00008800, new object[] { -1, 0x43, 0x44, 0x30, 0x30, 0x31 }); if (isMatch) { yield return(new ContentFormat("archive", "iso", string.Empty, 5, "application/x-iso9660-image", ".iso")); } // archive/iso (application/x-iso9660-image) .iso isMatch = Match( container, 0x00009000, new object[] { -1, 0x43, 0x44, 0x30, 0x30, 0x31 }); if (isMatch) { yield return(new ContentFormat("archive", "iso", string.Empty, 5, "application/x-iso9660-image", ".iso")); } // image/bmp (*) .bmp isMatch = Match( container, 0x00000000, new object[] { 0x42, 0x4d }); if (isMatch) { yield return(new ContentFormat("image", "bmp", string.Empty, 2, "image/bmp", ".bmp")); } // image/jpeg (*) .jpg isMatch = Match( container, 0x00000000, new object[] { 0xff, 0xd8 }); if (isMatch) { yield return(new ContentFormat("image", "jpeg", string.Empty, 2, "image/jpeg", ".jpg")); } // image/jpeg2000 (image/jp2) .jp2 isMatch = Match( container, 0x00000000, new object[] { 0x00, 0x00, 0x00, 0x0C, 0x6A, 0x50, 0x20, 0x20, 0x0D, 0x0A, 0x87, 0x0A, 0x00, 0x00, 0x00, 0x14, 0x66, 0x74, 0x79, 0x70, 0x6A, 0x70, 0x32 }); if (isMatch) { yield return(new ContentFormat("image", "jpeg2000", string.Empty, 23, "image/jp2", ".jp2")); } // image/pcx/2.5 (image/x-pcx) .pcx isMatch = Match( container, 0x00000000, new object[] { 0x0a, 0x00 }); if (isMatch) { yield return(new ContentFormat("image", "pcx", "2.5", 2, "image/x-pcx", ".pcx")); } // image/pcx/2.8 (image/x-pcx) .pcx isMatch = Match( container, 0x00000000, new object[] { 0x0a, 0x02 }); if (isMatch) { yield return(new ContentFormat("image", "pcx", "2.8", 2, "image/x-pcx", ".pcx")); } // image/pcx/2.8 (image/x-pcx) .pcx isMatch = Match( container, 0x00000000, new object[] { 0x0a, 0x03 }); if (isMatch) { yield return(new ContentFormat("image", "pcx", "2.8", 2, "image/x-pcx", ".pcx")); } // image/pcx/4 (image/x-pcx) .pcx isMatch = Match( container, 0x00000000, new object[] { 0x0a, 0x04 }); if (isMatch) { yield return(new ContentFormat("image", "pcx", "4", 2, "image/x-pcx", ".pcx")); } // image/pcx/5 (image/x-pcx) .pcx isMatch = Match( container, 0x00000000, new object[] { 0x0a, 0x05, 0x01 }); if (isMatch) { yield return(new ContentFormat("image", "pcx", "5", 3, "image/x-pcx", ".pcx")); } // image/png (*) .png isMatch = Match( container, 0x00000000, new object[] { 0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a }); if (isMatch) { yield return(new ContentFormat("image", "png", string.Empty, 8, "image/png", ".png")); } // image/gif (*) .gif isMatch = Match( container, 0x00000000, new object[] { 0x47, 0x49, 0x46, 0x38, 0x39, 0x61 }); if (isMatch) { yield return(new ContentFormat("image", "gif", string.Empty, 6, "image/gif", ".gif")); } // image/pbm/ascii (image/x-portable-bitmap) .pbm isMatch = Match( container, 0x00000000, new object[] { 0x50, 0x31 }); if (isMatch) { yield return(new ContentFormat("image", "pbm", "ascii", 2, "image/x-portable-bitmap", ".pbm")); } // image/pgm/ascii (image/x-portable-graymap) .pgm isMatch = Match( container, 0x00000000, new object[] { 0x50, 0x32 }); if (isMatch) { yield return(new ContentFormat("image", "pgm", "ascii", 2, "image/x-portable-graymap", ".pgm")); } // image/ppm/ascii (image/x-portable-pixmap) .ppm isMatch = Match( container, 0x00000000, new object[] { 0x50, 0x33 }); if (isMatch) { yield return(new ContentFormat("image", "ppm", "ascii", 2, "image/x-portable-pixmap", ".ppm")); } // image/pbm/binary (image/x-portable-bitmap) .pbm isMatch = Match( container, 0x00000000, new object[] { 0x50, 0x34 }); if (isMatch) { yield return(new ContentFormat("image", "pbm", "binary", 2, "image/x-portable-bitmap", ".pbm")); } // image/pgm/binary (image/x-portable-graymap) .pgm isMatch = Match( container, 0x00000000, new object[] { 0x50, 0x35 }); if (isMatch) { yield return(new ContentFormat("image", "pgm", "binary", 2, "image/x-portable-graymap", ".pgm")); } // image/ppm/binary (image/x-portable-pixmap) .ppm isMatch = Match( container, 0x00000000, new object[] { 0x50, 0x36 }); if (isMatch) { yield return(new ContentFormat("image", "ppm", "binary", 2, "image/x-portable-pixmap", ".ppm")); } // image/tiff/motorola (image/tiff) .tif isMatch = Match( container, 0x00000000, new object[] { 0x4d, 0x4d, 0x00, 0x2a }); if (isMatch) { yield return(new ContentFormat("image", "tiff", "motorola", 4, "image/tiff", ".tif")); } // image/tiff/intel (image/tiff) .tif isMatch = Match( container, 0x00000000, new object[] { 0x49, 0x49, 0x2a, 0x00 }); if (isMatch) { yield return(new ContentFormat("image", "tiff", "intel", 4, "image/tiff", ".tif")); } // image/tga (image/x-tga) .tga isMatch = Match( container, -0x0000012, new object[] { 0x54, 0x52, 0x55, 0x45, 0x56, 0x49, 0x53, 0x49, 0x4f, 0x4e, 0x2d, 0x58, 0x46, 0x49, 0x4c, 0x45, 0x2e, 0x00 }); if (isMatch) { yield return(new ContentFormat("image", "tga", string.Empty, 18, "image/x-tga", ".tga")); } // image/ico (image/vnd.microsoft.icon) .ico isMatch = Match( container, 0x00000000, new object[] { 0x00, 0x00, 0x01, 0x00 }); if (isMatch) { yield return(new ContentFormat("image", "ico", string.Empty, 4, "image/vnd.microsoft.icon", ".ico")); } // video/wmv (video/x-ms-wmv) .wmv isMatch = Match( container, 0x00000000, new object[] { 0x30, 0x26, 0xB2, 0x75, 0x8E, 0x66, 0xCF, 0x11, 0xA6, 0xD9, 0x00, 0xAA, 0x00, 0x62, 0xCE, 0x6C }); if (isMatch) { yield return(new ContentFormat("video", "wmv", string.Empty, 16, "video/x-ms-wmv", ".wmv")); } // video/mkv (video/x-matroska) .mkv isMatch = Match( container, 0x00000000, new object[] { 0x1A, 0x45, 0xDF, 0xA3, 0x93, 0x42, 0x82, 0x88, 0x6D, 0x61, 0x74, 0x72, 0x6F, 0x73, 0x6B, 0x61 }); if (isMatch) { yield return(new ContentFormat("video", "mkv", string.Empty, 16, "video/x-matroska", ".mkv")); } // video/avi (video/avi) .avi isMatch = Match( container, 0x00000000, new object[] { 0x52, 0x49, 0x46, 0x46, -1, -1, -1, -1, 0x41, 0x56, 0x49, 0x20, 0x4c, 0x49, 0x53, 0x54 }); if (isMatch) { yield return(new ContentFormat("video", "avi", string.Empty, 12, "video/avi", ".avi")); } // video/mp4 .mp4 isMatch = Match( container, 0x00000000, new object[] { 0x00, 0x00, 0x00, new byte[] { 0x14, 0x20 }, 0x66, 0x74, 0x79, 0x70, 0x33, 0x67, 0x70, 0x35 }); if (isMatch) { yield return(new ContentFormat("video", "mp4", string.Empty, 12, string.Empty, ".mp4")); } // video/m4v .m4v isMatch = Match( container, 0x00000000, new object[] { 0x00, 0x00, 0x00, 0x20, 0x66, 0x74, 0x79, 0x70, 0x4d, 0x34, 0x56, 0x20 }); if (isMatch) { yield return(new ContentFormat("video", "m4v", string.Empty, 12, string.Empty, ".m4v")); } // video/mp4 .mp4 isMatch = Match( container, 0x00000000, new object[] { 0x00, 0x00, 0x00, new byte[] { 0x20, 0x18 }, 0x66, 0x74, 0x79, 0x70, 0x69, 0x73, 0x6f, 0x6d }); if (isMatch) { yield return(new ContentFormat("video", "mp4", string.Empty, 12, string.Empty, ".mp4")); } // video/mpg (video/mpg) .mpg isMatch = Match( container, 0x00000000, new object[] { 0x00, 0x00, 0x01, new byte[] { 0xB0, 0xB1, 0xB2, 0xB3, 0xB4, 0xB5, 0xB6, 0xB7, 0xB8, 0xB9, 0xBa, 0xBb, 0xBc, 0xBd, 0xBe, 0xBf } }); if (isMatch) { yield return(new ContentFormat("video", "mpg", string.Empty, 4, "video/mpg", ".mpg")); } // executable/portable .exe isMatch = Match( container, 0x00000000, new object[] { 0x4d, 0x5a }); if (isMatch) { yield return(new ContentFormat("executable", "portable", string.Empty, 2, string.Empty, ".exe")); } // audio/wave (audio/x-wav) .wav isMatch = Match( container, 0x00000000, new object[] { 0x52, 0x49, 0x46, 0x46, -1, -1, -1, -1, 0x57, 0x41, 0x56, 0x45 }); if (isMatch) { yield return(new ContentFormat("audio", "wave", string.Empty, 8, "audio/x-wav", ".wav")); } // audio/ogg (application/ogg) .ogg isMatch = Match( container, 0x00000000, new object[] { 0x4f, 0x67, 0x67, 0x53 }); if (isMatch) { yield return(new ContentFormat("audio", "ogg", string.Empty, 4, "application/ogg", ".ogg")); } // audio/midi (audio/mid) .mid isMatch = Match( container, 0x00000000, new object[] { 0x4d, 0x54, 0x68, 0x64 }); if (isMatch) { yield return(new ContentFormat("audio", "midi", string.Empty, 4, "audio/mid", ".mid")); } // document/pdf (application/pdf) .pdf isMatch = Match( container, 0x00000000, new object[] { 0x25, 0x50, 0x44, 0x46 }); isMatch = isMatch && Match( container, -0x0000006, new object[] { 0x25, 0x25, 0x45, 0x4f, 0x46, new byte[] { 0x0a, 0x0d } }); if (isMatch) { yield return(new ContentFormat("document", "pdf", string.Empty, 10, "application/pdf", ".pdf")); } // document/pdf (application/pdf) .pdf isMatch = Match( container, 0x00000000, new object[] { 0x25, 0x50, 0x44, 0x46 }); isMatch = isMatch && Match( container, -0x0000007, new object[] { 0x25, 0x25, 0x45, 0x4f, 0x46, 0x0d, 0x0a }); if (isMatch) { yield return(new ContentFormat("document", "pdf", string.Empty, 11, "application/pdf", ".pdf")); } // document/pdf (application/pdf) .pdf isMatch = Match( container, 0x00000000, new object[] { 0x25, 0x50, 0x44, 0x46 }); isMatch = isMatch && Match( container, -0x0000005, new object[] { 0x25, 0x25, 0x45, 0x4f, 0x46 }); if (isMatch) { yield return(new ContentFormat("document", "pdf", string.Empty, 9, "application/pdf", ".pdf")); } // document/chm (application/x-chm) .chm isMatch = Match( container, 0x00000000, new object[] { 0x49, 0x54, 0x53, 0x46 }); if (isMatch) { yield return(new ContentFormat("document", "chm", string.Empty, 4, "application/x-chm", ".chm")); } // debug/pdb .pdb isMatch = Match( container, 0x00000000, new object[] { 0x4d, 0x69, 0x63, 0x72, 0x6f, 0x73, 0x6f, 0x66, 0x74, 0x20, 0x43, 0x2f, 0x43, 0x2b, 0x2b }); if (isMatch) { yield return(new ContentFormat("debug", "pdb", string.Empty, 15, string.Empty, ".pdb")); } // development/snk (application/octet-stream) .snk isMatch = Match( container, 0x00000000, new object[] { 0x07, 0x02, 0x00, 0x00, 0x00, 0x24, 0x00, 0x00 }); if (isMatch) { yield return(new ContentFormat("development", "snk", string.Empty, 8, "application/octet-stream", ".snk")); } // misc/par2 .par2 isMatch = Match( container, 0x00000000, new object[] { 0x50, 0x41, 0x52, 0x32, 0x00, 0x50, 0x4b, 0x54 }); if (isMatch) { yield return(new ContentFormat("misc", "par2", string.Empty, 8, string.Empty, ".par2")); } // misc/uce .uce isMatch = Match( container, 0x00000000, new object[] { 0x55, 0x43, 0x45 }); if (isMatch) { yield return(new ContentFormat("misc", "uce", string.Empty, 3, string.Empty, ".uce")); } // misc/sfm .sfm isMatch = Match( container, 0x00000000, new object[] { 0x52, 0x53, 0x46, 0x4d }); if (isMatch) { yield return(new ContentFormat("misc", "sfm", string.Empty, 4, string.Empty, ".sfm")); } yield break; }
protected void SetContent(IByteContainer newContent) { _objectContent = newContent; }
protected BasePdfObject(int objectNumber) { _objectNumber = objectNumber; _objectContent = null; }
public void Append(IByteContainer bytes) { Append(bytes.GetBytes()); }
public PdfGenerator() { _xref = new Dictionary<int, int>(); _content = ByteContainerFactory.CreateByteContainer("%PDF-1.7" + StringConstants.NewLine); _offset = 9; }