/** * Performs a recursive search for pictures in the given list of escher records. * * @param escherRecords the escher records. * @param pictures the list to populate with the pictures. */ private void SearchForPictures(IList escherRecords, List <Picture> pictures) { foreach (EscherRecord escherRecord in escherRecords) { if (escherRecord is EscherBSERecord) { EscherBSERecord bse = (EscherBSERecord)escherRecord; EscherBlipRecord blip = bse.BlipRecord; if (blip != null) { pictures.Add(new Picture(blip.PictureData)); } else if (bse.Offset > 0) { // Blip stored in delay stream, which in a word doc, is the main stream EscherRecordFactory recordFactory = new DefaultEscherRecordFactory(); EscherRecord record = recordFactory.CreateRecord(_mainStream, bse.Offset); if (record is EscherBlipRecord) { record.FillFields(_mainStream, bse.Offset, recordFactory); blip = (EscherBlipRecord)record; pictures.Add(new Picture(blip.PictureData)); } } } // Recursive call. SearchForPictures(escherRecord.ChildRecords, pictures); } }
public void TestDetectContainer() { Random rnd = new Random(); Assert.AreEqual(true, DefaultEscherRecordFactory.IsContainer((short)0x0, EscherContainerRecord.DG_CONTAINER)); Assert.AreEqual(true, DefaultEscherRecordFactory.IsContainer((short)0x0, EscherContainerRecord.SOLVER_CONTAINER)); Assert.AreEqual(true, DefaultEscherRecordFactory.IsContainer((short)0x0, EscherContainerRecord.SP_CONTAINER)); Assert.AreEqual(true, DefaultEscherRecordFactory.IsContainer((short)0x0, EscherContainerRecord.DGG_CONTAINER)); Assert.AreEqual(true, DefaultEscherRecordFactory.IsContainer((short)0x0, EscherContainerRecord.BSTORE_CONTAINER)); Assert.AreEqual(true, DefaultEscherRecordFactory.IsContainer((short)0x0, EscherContainerRecord.SPGR_CONTAINER)); for (short i = EscherContainerRecord.DGG_CONTAINER; i <= EscherContainerRecord.SOLVER_CONTAINER; i++) { Assert.AreEqual(true, DefaultEscherRecordFactory.IsContainer((short)rnd.Next(short.MaxValue), i)); } Assert.AreEqual(false, DefaultEscherRecordFactory.IsContainer((short)0x0, EscherContainerRecord.DGG_CONTAINER - 1)); Assert.AreEqual(false, DefaultEscherRecordFactory.IsContainer((short)0x0, EscherContainerRecord.SOLVER_CONTAINER + 1)); Assert.AreEqual(true, DefaultEscherRecordFactory.IsContainer((short)0x000F, EscherContainerRecord.DGG_CONTAINER - 1)); Assert.AreEqual(true, DefaultEscherRecordFactory.IsContainer(unchecked ((short)0xFFFF), EscherContainerRecord.DGG_CONTAINER - 1)); Assert.AreEqual(false, DefaultEscherRecordFactory.IsContainer((short)0x000C, EscherContainerRecord.DGG_CONTAINER - 1)); Assert.AreEqual(false, DefaultEscherRecordFactory.IsContainer(unchecked ((short)0xCCCC), EscherContainerRecord.DGG_CONTAINER - 1)); Assert.AreEqual(false, DefaultEscherRecordFactory.IsContainer((short)0x000F, EscherTextboxRecord.RECORD_ID)); Assert.AreEqual(false, DefaultEscherRecordFactory.IsContainer(unchecked ((short)0xCCCC), EscherTextboxRecord.RECORD_ID)); }
public void TestFillFields() { string TestData = "0F 02 " + // options "11 F1 " + // record id "00 00 00 00"; // remaining bytes UnknownEscherRecord r = new UnknownEscherRecord(); IEscherRecordFactory factory = new DefaultEscherRecordFactory(); r.FillFields(HexRead.ReadFromString(TestData), factory); Assert.AreEqual(0x020F, r.Options); Assert.AreEqual(unchecked ((short)0xF111), r.RecordId); Assert.IsTrue(r.IsContainerRecord); Assert.AreEqual(8, r.RecordSize); Assert.AreEqual(0, r.ChildRecords.Count); Assert.AreEqual(0, r.Data.Length); TestData = "00 02 " + // options "11 F1 " + // record id "04 00 00 00 " + // remaining bytes "01 02 03 04"; r = new UnknownEscherRecord(); r.FillFields(HexRead.ReadFromString(TestData), factory); Assert.AreEqual(0x0200, r.Options); Assert.AreEqual(unchecked ((short)0xF111), r.RecordId); Assert.AreEqual(12, r.RecordSize); Assert.IsFalse(r.IsContainerRecord); Assert.AreEqual(0, r.ChildRecords.Count); Assert.AreEqual(4, r.Data.Length); Assert.AreEqual(1, r.Data[0]); Assert.AreEqual(2, r.Data[1]); Assert.AreEqual(3, r.Data[2]); Assert.AreEqual(4, r.Data[3]); TestData = "0F 02 " + // options "11 F1 " + // record id "08 00 00 00 " + // remaining bytes "00 02 " + // options "FF FF " + // record id "00 00 00 00"; // remaining bytes r = new UnknownEscherRecord(); r.FillFields(HexRead.ReadFromString(TestData), factory); Assert.AreEqual(0x020F, r.Options); Assert.AreEqual(unchecked ((short)0xF111), r.RecordId); Assert.AreEqual(8, r.RecordSize); Assert.IsTrue(r.IsContainerRecord); Assert.AreEqual(1, r.ChildRecords.Count); Assert.AreEqual(unchecked ((short)0xFFFF), r.GetChild(0).RecordId); }
private EscherBlipRecord GetBitmapRecord(int bitmapIndex) { List <EscherContainerRecord> bContainers = _escherRecordHolder .GetBStoreContainers(); if (bContainers == null || bContainers.Count != 1) { return(null); } EscherContainerRecord bContainer = bContainers[0]; IList bitmapRecords = bContainer.ChildRecords; if (bitmapRecords.Count < bitmapIndex) { return(null); } EscherRecord imageRecord = (EscherRecord)bitmapRecords[bitmapIndex - 1]; if (imageRecord is EscherBlipRecord) { return((EscherBlipRecord)imageRecord); } if (imageRecord is EscherBSERecord) { EscherBSERecord bseRecord = (EscherBSERecord)imageRecord; EscherBlipRecord blip = bseRecord.BlipRecord; if (blip != null) { return(blip); } if (bseRecord.Offset > 0) { /* * Blip stored in delay stream, which in a word doc, is the main * stream */ EscherRecordFactory recordFactory = new DefaultEscherRecordFactory(); EscherRecord record = recordFactory.CreateRecord(_mainStream, bseRecord.Offset); if (record is EscherBlipRecord) { record.FillFields(_mainStream, bseRecord.Offset, recordFactory); return((EscherBlipRecord)record); } } } return(null); }
private void ConvertToEscherRecords(int offset, int size, byte[] data) { EscherRecordFactory recordFactory = new DefaultEscherRecordFactory(); int pos = offset; while (pos < offset + size) { EscherRecord r = recordFactory.CreateRecord(data, pos); int bytesRead = r.FillFields(data, pos, recordFactory); escherRecords.Add(r); pos += bytesRead; } }
private void FillEscherRecords(byte[] data, int offset, int size) { IEscherRecordFactory recordFactory = new DefaultEscherRecordFactory(); int pos = offset; while (pos < offset + size) { EscherRecord r = recordFactory.CreateRecord(data, pos); escherRecords.Add(r); int bytesRead = r.FillFields(data, pos, recordFactory); pos += bytesRead + 1; // There Is an empty byte between each top-level record in a Word doc } }
/** * Performs a recursive search for pictures in the given list of escher records. * * @param escherRecords the escher records. * @param pictures the list to populate with the pictures. */ private void SearchForPictures(IList escherRecords, List <Picture> pictures) { foreach (EscherRecord escherRecord in escherRecords) { if (escherRecord is EscherBSERecord) { EscherBSERecord bse = (EscherBSERecord)escherRecord; EscherBlipRecord blip = bse.BlipRecord; if (blip != null) { pictures.Add(new Picture(blip.PictureData)); } else if (bse.Offset > 0) { try { // Blip stored in delay stream, which in a word doc, is the main stream IEscherRecordFactory recordFactory = new DefaultEscherRecordFactory(); EscherRecord record = recordFactory.CreateRecord(_mainStream, bse.Offset); if (record is EscherBlipRecord) { record.FillFields(_mainStream, bse.Offset, recordFactory); blip = (EscherBlipRecord)record; pictures.Add(new Picture(blip.PictureData)); } } catch (Exception exc) { logger.Log( POILogger.WARN, "Unable to load picture from BLIB record at offset #", bse.Offset, exc); } } } // Recursive call. SearchForPictures(escherRecord.ChildRecords, pictures); } }
public void TestFillFields() { IEscherRecordFactory f = new DefaultEscherRecordFactory(); byte[] data = HexRead.ReadFromString("0F 02 11 F1 00 00 00 00"); EscherRecord r = f.CreateRecord(data, 0); r.FillFields(data, 0, f); Assert.IsTrue(r is EscherContainerRecord); Assert.AreEqual((short)0x020F, r.Options); Assert.AreEqual(unchecked ((short)0xF111), r.RecordId); data = HexRead.ReadFromString("0F 02 11 F1 08 00 00 00" + " 02 00 22 F2 00 00 00 00"); r = f.CreateRecord(data, 0); r.FillFields(data, 0, f); EscherRecord c = r.GetChild(0); Assert.IsFalse(c is EscherContainerRecord); Assert.AreEqual(unchecked ((short)0x0002), c.Options); Assert.AreEqual(unchecked ((short)0xF222), c.RecordId); }