Example #1
0
        public void Test_COMPRESS_SPACE()
        {
            String FILENAME = "MultipleStorage3.cfs"; // 22Kb

            FileInfo srcFile = new FileInfo(FILENAME);

            File.Copy(FILENAME, "MultipleStorage_Deleted_Compress.cfs", true);

            CompoundFile cf = new CompoundFile("MultipleStorage_Deleted_Compress.cfs", UpdateMode.Update, true, true);

            ICFStorage st = cf.RootStorage.GetStorage("MyStorage");
            st = st.GetStorage("AnotherStorage");

            Assert.IsNotNull(st);
            st.Delete("Another2Stream");
            cf.Commit();
            cf.Close();

            CompoundFile.ShrinkCompoundFile("MultipleStorage_Deleted_Compress.cfs"); // -> 7Kb

            FileInfo dstFile = new FileInfo("MultipleStorage_Deleted_Compress.cfs");

            Assert.IsTrue(srcFile.Length > dstFile.Length);

        }
Example #2
0
        /// <summary>
        /// Gets the raw byte data.
        /// </summary>
        /// <param name="compoundFile">The compound file.</param>
        /// <param name="oleStream">The OLE stream.</param>
        /// <returns></returns>
        /// <exception cref="ArgumentNullException">compoundFile</exception>
        /// <exception cref="ArgumentException">oleStream</exception>
        /// <exception cref="InvalidDataException">
        /// The following stream \"{oleStream}\" does not exist
        /// or
        /// Failed to get raw OLE byte data
        /// </exception>
        private static byte[] GetRawBytes(CompoundFile compoundFile, string oleStream)
        {
            if (compoundFile is null)
            {
                throw new ArgumentNullException(nameof(compoundFile));
            }

            if (string.IsNullOrWhiteSpace(oleStream))
            {
                throw new ArgumentException(nameof(oleStream));
            }

            byte[] bytes;
            try
            {
                var rootStorage     = compoundFile.RootStorage;
                var basicInfoStream = rootStorage.GetStream(oleStream);
                if (basicInfoStream == null)
                {
                    throw new InvalidDataException($"The following stream \"{oleStream}\" does not exist");
                }

                bytes = basicInfoStream.GetData();
            }
            catch (Exception ex)
            {
                throw new InvalidDataException($"Failed to get raw OLE byte data", ex);
            }
            finally
            {
                compoundFile?.Close();
            }

            return(bytes);
        }
        /// <summary>
        /// Gets the raw byte data.
        /// </summary>
        /// <param name="pathToFile">The path to file.</param>
        /// <param name="oleStream">The name of the OLE stream.</param>
        /// <returns></returns>
        /// <exception cref="InvalidDataException">
        /// The following stream \"{oleStream}\" does not exist in this file \"{pathToFile}\
        /// or
        /// Failed to get raw OLE byte data from the following file {pathToFile}
        /// </exception>
        public static byte[] GetRawBytes(string pathToFile, string oleStream)
        {
            byte[]       bytes;
            CompoundFile compoundFile = null;

            try
            {
                compoundFile = new CompoundFile(pathToFile);
                var rootStorage     = compoundFile.RootStorage;
                var basicInfoStream = rootStorage.GetStream(oleStream);
                if (basicInfoStream == null)
                {
                    throw new InvalidDataException($"The following stream \"{oleStream}\" does not exist in this file \"{pathToFile}\"");
                }

                bytes = basicInfoStream.GetData();
            }
            catch (Exception ex)
            {
                throw new InvalidDataException($"Failed to get raw OLE byte data from the following file \"{pathToFile}\"", ex);
            }
            finally
            {
                compoundFile?.Close();
            }

            return(bytes);
        }
Example #4
0
 private static void CreateFile(String fn)
 {
     CompoundFile cf = new CompoundFile();
     for (int i = 0; i < MAX_STREAM_COUNT; i++)
     {
         cf.RootStorage.AddStream("Test" + i.ToString()).SetData(Helpers.GetBuffer(300));
     }
     cf.Save(fileName);
     cf.Close();
 }
Example #5
0
        public void Test_DELETE_STREAM_2()
        {
            String filename = "MultipleStorage.cfs";

            CompoundFile cf  = new CompoundFile(filename);
            CFStorage    cfs = cf.RootStorage.GetStorage("MyStorage").GetStorage("AnotherStorage");

            cfs.Delete("AnotherStream");

            cf.Save(TestContext.TestDir + "MultipleStorage_REMOVED_STREAM_2.cfs");

            cf.Close();
        }
Example #6
0
        public void Test_ZERO_LENGTH_RE_WRITE_STREAM()
        {
            byte[] b = new byte[0];

            CompoundFile cf       = new CompoundFile();
            CFStream     myStream = cf.RootStorage.AddStream("MyStream");

            Assert.IsNotNull(myStream);

            try
            {
                myStream.SetData(b);
            }
            catch
            {
                Assert.Fail("Failed setting zero length stream");
            }

            cf.Save("ZERO_LENGTH_STREAM_RE.cfs");
            cf.Close();

            CompoundFile cfo     = new CompoundFile("ZERO_LENGTH_STREAM_RE.cfs");
            CFStream     oStream = cfo.RootStorage.GetStream("MyStream");

            Assert.IsNotNull(oStream);
            Assert.IsTrue(oStream.Size == 0);

            try
            {
                oStream.SetData(Helpers.GetBuffer(30));
                cfo.Save("ZERO_LENGTH_STREAM_RE2.cfs");
            }
            catch
            {
                Assert.Fail("Failed re-writing zero length stream");
            }
            finally
            {
                cfo.Close();
            }

            if (File.Exists("ZERO_LENGTH_STREAM_RE.cfs"))
            {
                File.Delete("ZERO_LENGTH_STREAM_RE.cfs");
            }

            if (File.Exists("ZERO_LENGTH_STREAM_RE2.cfs"))
            {
                File.Delete("ZERO_LENGTH_STREAM_RE2.cfs");
            }
        }
Example #7
0
        public AltiumParser(string filepath)
        {
            FilePath = filepath;

            var recordCountPattern = new Regex(@"(WEIGHT=(?<Weight>\d+)?)");

            var file = new CompoundFile(FilePath);

            var mainStream = file.RootStorage.GetStream("FileHeader");
            //var storageStream = file.RootStorage.GetStream("Storage");
            // var additionalStream = file.RootStorage.GetStream("Additional");

            var fileHeader = Encoding.Default.GetString(mainStream.GetData());

            //var additional = Encoding.Default.GetString(additionalStream.GetData());
            //var storage = Encoding.Default.GetString(storageStream.GetData());

            file.Close();

            Records = fileHeader.Split(new[] { "RECORD=" }, StringSplitOptions.None);

            var match = recordCountPattern.Match(Records[0]);

            if (match.Success)
            {
                RecordCount = Convert.ToInt32(match.Groups["Weight"].Value);
            }

            Components   = new List <Component>();
            Designators  = new List <Designator>();
            Pins         = new List <Pin>();
            Wires        = new List <Wire>();
            Nets         = new List <Net>();
            Junctions    = new List <Junction>();
            PowerPorts   = new List <PowerPort>();
            Ports        = new List <Port>();
            SheetSymbols = new List <SheetSymbol>();
            SheetEntries = new List <SheetEntry>();
            SheetsNames  = new List <SheetName>();
            SheetFiles   = new List <SheetFile>();
            Parameters   = new List <Parameter>();

            BuildOfMaterials = new List <BomEntry>();
            SubParts         = new List <SubPart>();

            ParseRecords();
            BuildComponents();
            GetBom();
            GetSubParts();
            CombineBoms();
        }
Example #8
0
        public void Test_OPEN_FROM_STREAM()
        {
            String filename = "reportREAD.xls";
            File.Copy(filename, "reportOPENFROMSTREAM.xls");
            FileStream fs = new FileStream(filename, FileMode.Open);
            CompoundFile cf = new CompoundFile(fs);
            CFStream foundStream = cf.RootStorage.GetStream("Workbook");

            byte[] temp = foundStream.GetData();

            Assert.IsNotNull(temp);

            cf.Close();
        }
Example #9
0
        public void Test_ENTRY_NAME_LENGTH()
        {
            //Thanks to Mark Bosold for bug fix and unit

            CompoundFile cf = new CompoundFile();

            // Cannot be equal.
            string maxCharactersStreamName = "1234567890123456789A12345678901"; // 31 chars
            string maxCharactersStorageName = "1234567890123456789012345678901"; // 31 chars

            // Try Storage entry name with max characters.
            Assert.IsNotNull(cf.RootStorage.AddStorage(maxCharactersStorageName));
            CFStorage strg = cf.RootStorage.GetStorage(maxCharactersStorageName);
            Assert.IsNotNull(strg);
            Assert.IsTrue(strg.Name == maxCharactersStorageName);


            // Try Stream entry name with max characters.
            Assert.IsNotNull(cf.RootStorage.AddStream(maxCharactersStreamName));
            CFStream strm = cf.RootStorage.GetStream(maxCharactersStreamName);
            Assert.IsNotNull(strm);
            Assert.IsTrue(strm.Name == maxCharactersStreamName);

            string tooManyCharactersEntryName = "12345678901234567890123456789012"; // 32 chars

            try
            {
                // Try Storage entry name with too many characters.
                cf.RootStorage.AddStorage(tooManyCharactersEntryName);
                Assert.Fail();
            }
            catch (Exception ex)
            {
                Assert.IsTrue(ex is CFException);
            }

            try
            {
                // Try Stream entry name with too many characters.
                cf.RootStorage.AddStream(tooManyCharactersEntryName);
                Assert.Fail();
            }
            catch (Exception ex)
            {
                Assert.IsTrue(ex is CFException);
            }

            cf.Save("EntryNameLength");
            cf.Close();
        }
Example #10
0
        public void Test_READ_STREAM()
        {
            String filename = "report.xls";

            CompoundFile cf = new CompoundFile(filename);
            ICFStream foundStream = cf.RootStorage.GetStream("Workbook");

            byte[] temp = foundStream.GetData();

            Assert.IsNotNull(temp);
            Assert.IsTrue(temp.Length > 0);

            cf.Close();
        }
Example #11
0
        public void Test_TRANSACTED_ADD_REMOVE_MULTIPLE_STREAM_TO_EXISTING_FILE()
        {
            String srcFilename = "report.xls";
            String dstFilename = "reportOverwriteMultiple.xls";

            File.Copy(srcFilename, dstFilename, true);

            CompoundFile cf = new CompoundFile(dstFilename, CFSUpdateMode.ReadOnly, CFSConfiguration.SectorRecycle);

            //CompoundFile cf = new CompoundFile();

            Random r = new Random();

            for (int i = 0; i < 254; i++)
            {
                //byte[] buffer = Helpers.GetBuffer(r.Next(100, 3500), (byte)i);
                byte[] buffer = Helpers.GetBuffer(1995, 1);

                //if (i > 0)
                //{
                //    if (r.Next(0, 100) > 50)
                //    {
                //        cf.RootStorage.Delete("MyNewStream" + (i - 1).ToString());
                //    }
                //}

                CFStream addedStream = cf.RootStorage.AddStream("MyNewStream" + i.ToString());
                Assert.IsNotNull(addedStream, "Stream not found");
                addedStream.SetData(buffer);

                Assert.IsTrue(Helpers.CompareBuffer(addedStream.GetData(), buffer), "Data buffer corrupted");

                // Random commit, not on single addition
                //if (r.Next(0, 100) > 50)
                //    cf.UpdateFile();
            }

            cf.Save(dstFilename + "PP");
            cf.Close();

            if (File.Exists("reportOverwriteMultiple.xls"))
            {
                File.Delete("reportOverwriteMultiple.xls");
            }

            if (File.Exists("reportOverwriteMultiple.xlsPP"))
            {
                File.Delete("reportOverwriteMultiple.xlsPP");
            }
        }
Example #12
0
        public void Test_READ_STREAM()
        {
            String filename = "report.xls";

            CompoundFile cf          = new CompoundFile(filename);
            CFStream     foundStream = cf.RootStorage.GetStream("Workbook");

            byte[] temp = foundStream.GetData();

            Assert.IsNotNull(temp);
            Assert.IsTrue(temp.Length > 0);

            cf.Close();
        }
Example #13
0
        public void Test_DIFAT_CHECK()
        {
            CompoundFile f = null;

            try
            {
                f = new CompoundFile();
                CFStream st = f.RootStorage.AddStream("LargeStream");
                st.Append(Helpers.GetBuffer(20000000, 0x0A));       //Forcing creation of two DIFAT sectors
                byte[] b1 = Helpers.GetBuffer(3, 0x0B);
                st.Append(b1);                                      //Forcing creation of two DIFAT sectors

                f.Save("$OpenMcdf$LargeFile.cfs");

                f.Close();

                int cnt = 3;
                f = new CompoundFile("$OpenMcdf$LargeFile.cfs");

                byte[] b2 = new byte[cnt];
                cnt = f.RootStorage.GetStream("LargeStream").Read(b2, 20000000, cnt);
                f.Close();
                Assert.IsTrue(Helpers.CompareBuffer(b1, b2));
            }
            finally
            {
                if (f != null)
                {
                    f.Close();
                }

                if (File.Exists("$OpenMcdf$LargeFile.cfs"))
                {
                    File.Delete("$OpenMcdf$LargeFile.cfs");
                }
            }
        }
Example #14
0
        public void Test_OPEN_FROM_STREAM()
        {
            const string filename = "reportREAD.xls";

            using (var fs = new FileStream(filename, FileMode.Open))
            {
                using (var cf = new CompoundFile(fs))
                {
                    var foundStream = cf.RootStorage.GetStream("Workbook");
                    var temp        = foundStream.GetData();
                    Assert.IsNotNull(temp);
                    cf.Close();
                }
            }
        }
Example #15
0
        public void Test_DELETE_DIRECTORY()
        {
            String       FILENAME = "MultipleStorage2.cfs";
            CompoundFile cf       = new CompoundFile(FILENAME, CFSUpdateMode.ReadOnly, CFSConfiguration.Default);

            CFStorage st = cf.RootStorage.GetStorage("MyStorage");

            Assert.IsNotNull(st);

            st.Delete("AnotherStorage");

            cf.Save("MultipleStorage_Delete.cfs");

            cf.Close();
        }
Example #16
0
        public void Test_CREATE_STORAGE_WITH_CREATION_DATE()
        {
            const String STORAGE_NAME = "NewStorage1";
            CompoundFile cf           = new CompoundFile();

            CFStorage st = cf.RootStorage.AddStorage(STORAGE_NAME);

            st.CreationDate = DateTime.Now;

            Assert.IsNotNull(st);
            Assert.AreEqual(STORAGE_NAME, st.Name, false);

            cf.Save("ProvaData.cfs");
            cf.Close();
        }
Example #17
0
        public void Test_FIX_BUG_GH_14()
        {
            String filename       = "MyFile.dat";
            String storageName    = "MyStorage";
            String streamName     = "MyStream";
            int    BUFFER_SIZE    = 800 * Mb;
            int    iterationCount = 3;
            int    streamCount    = 3;

            CompoundFile compoundFileInit = new CompoundFile(CFSVersion.Ver_4, CFSConfiguration.Default);

            compoundFileInit.Save(filename);
            compoundFileInit.Close();

            CompoundFile compoundFile = new CompoundFile(filename, CFSUpdateMode.Update, CFSConfiguration.Default);
            CFStorage    st           = compoundFile.RootStorage.AddStorage(storageName);
            byte         b            = 0X0A;

            for (int streamId = 0; streamId < streamCount; ++streamId)
            {
                CFStream sm = st.AddStream(streamName + streamId);
                for (int iteration = 0; iteration < iterationCount; ++iteration)
                {
                    sm.Append(Helpers.GetBuffer(BUFFER_SIZE, b));
                    compoundFile.Commit();
                }

                b++;
            }
            compoundFile.Close();

            compoundFile = new CompoundFile(filename, CFSUpdateMode.ReadOnly, CFSConfiguration.Default);
            byte[] testBuffer = new byte[100];
            byte   t          = 0x0A;

            for (int streamId = 0; streamId < streamCount; ++streamId)
            {
                compoundFile.RootStorage.GetStorage(storageName).GetStream(streamName + streamId).Read(testBuffer, BUFFER_SIZE / 2, 100);
                Assert.IsTrue(testBuffer.All(g => g == t));
                compoundFile.RootStorage.GetStorage(storageName).GetStream(streamName + streamId).Read(testBuffer, BUFFER_SIZE - 101, 100);
                Assert.IsTrue(testBuffer.All(g => g == t));
                compoundFile.RootStorage.GetStorage(storageName).GetStream(streamName + streamId).Read(testBuffer, 0, 100);
                Assert.IsTrue(testBuffer.All(g => g == t));
                t++;
            }

            compoundFile.Close();
        }
Example #18
0
        public void Test_VISIT_STORAGE()
        {
            String FILENAME = "testVisiting.xls";

            // Remove...
            if (File.Exists(FILENAME))
            {
                File.Delete(FILENAME);
            }

            //Create...

            CompoundFile ncf = new CompoundFile();

            CFStorage l1 = ncf.RootStorage.AddStorage("Storage Level 1");

            l1.AddStream("l1ns1");
            l1.AddStream("l1ns2");
            l1.AddStream("l1ns3");

            CFStorage l2 = l1.AddStorage("Storage Level 2");

            l2.AddStream("l2ns1");
            l2.AddStream("l2ns2");

            ncf.Save(FILENAME);
            ncf.Close();


            // Read...

            CompoundFile cf = new CompoundFile(FILENAME);

            FileStream output = new FileStream("reportVisit.txt", FileMode.Create);
            TextWriter sw     = new StreamWriter(output);

            Console.SetOut(sw);

            Action <CFItem> va = delegate(CFItem target)
            {
                sw.WriteLine(target.Name);
            };

            cf.RootStorage.VisitEntries(va, true);

            cf.Close();
            sw.Close();
        }
Example #19
0
        public void Test_COMPARE_DIR_ENTRY_NAME_BUG_FIX_ID_3487353()
        {
            var      f   = new CompoundFile("report_name_fix.xls", CFSUpdateMode.Update, CFSConfiguration.SectorRecycle | CFSConfiguration.EraseFreeSectors);
            CFStream cfs = f.RootStorage.AddStream("Poorbook");

            cfs.Append(Helpers.GetBuffer(20));
            f.Commit();
            f.Close();

            f   = new CompoundFile("report_name_fix.xls", CFSUpdateMode.Update, CFSConfiguration.SectorRecycle | CFSConfiguration.EraseFreeSectors);
            cfs = f.RootStorage.GetStream("Workbook");
            Assert.IsTrue(cfs.Name == "Workbook");
            f.RootStorage.Delete("PoorBook");
            f.Commit();
            f.Close();
        }
Example #20
0
 /// <summary>
 /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
 /// </summary>
 public void Dispose()
 {
     if (_compoundFile == null)
     {
         return;
     }
     try
     {
         _compoundFile.Close();
     }
     catch
     {
         // Ignore
     }
     _compoundFile = null;
 }
Example #21
0
        public void Test_COMPARE_DIR_ENTRY_NAME_BUG_FIX_ID_3487353()
        {
            var       f   = new CompoundFile("report_name_fix.xls", UpdateMode.Update, true, true);
            ICFStream cfs = f.RootStorage.AddStream("Poorbook");

            cfs.AppendData(Helpers.GetBuffer(20));
            f.Commit();
            f.Close();

            f   = new CompoundFile("report_name_fix.xls", UpdateMode.Update, true, true);
            cfs = f.RootStorage.GetStream("Workbook");
            Assert.IsTrue(cfs.Name == "Workbook");
            f.RootStorage.Delete("PoorBook");
            f.Commit();
            f.Close();
        }
Example #22
0
 public void Test_PR_GH_18()
 {
     try
     {
         var f  = new CompoundFile("MultipleStorage4.cfs", CFSUpdateMode.Update, CFSConfiguration.Default);
         var st = f.RootStorage.GetStorage("MyStorage").GetStorage("AnotherStorage").GetStream("MyStream");
         st.Write(Helpers.GetBuffer(100, 0x02), 100);
         f.Commit(true);
         Assert.IsTrue(st.GetData().Count() == 31220);
         f.Close();
     }
     catch (Exception ex)
     {
         Assert.Fail("Release Memory flag caused error");
     }
 }
Example #23
0
        static void Main(string[] args)
        {
            //Test_READ_STREAM();
            String srcFile = "a.hwp";
            String dstFile = "poc.hwp";

            File.Copy(srcFile, dstFile, true);
            CompoundFile cf = new CompoundFile(dstFile, CFSUpdateMode.Update, CFSConfiguration.SectorRecycle | CFSConfiguration.EraseFreeSectors);

            extract_docinfo(cf);
            Console.Write("wait for \"docinfo.compress\"\n");
            Console.ReadKey();
            gene_poc_hwp(cf, "docinfo.compress");
            cf.Commit();
            Console.Write("All Done");
            cf.Close();
        }
Example #24
0
        /// <summary>
        /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
        /// </summary>
        public void Dispose()
        {
            if (!this.disposed)
            {
                //ensure only disposed once
                this.disposed = true;

                //call virtual disposing method to let sub classes clean up
                this.Disposing();

                if (_file != null)
                {
                    _file.Close();
                    _file = null;
                }
            }
        }
Example #25
0
        public void Test_FIX_BUG_16_CORRUPTED_AFTER_RESIZE()
        {
            const string FILE_PATH = @"BUG_16_.xls";

            CompoundFile cf = new CompoundFile(FILE_PATH);

            CFStream dirStream = cf.RootStorage.GetStorage("_VBA_PROJECT_CUR").GetStorage("VBA").GetStream("dir");

            byte[] currentData = dirStream.GetData();

            Array.Resize(ref currentData, currentData.Length - 50);

            dirStream.SetData(currentData);

            cf.Save(FILE_PATH + ".edited");
            cf.Close();
        }
Example #26
0
                         8 * 1024 * 1024)]                                        // max 8 Mb in RAM
        public void PerfMem_MultipleStreamCommit()
        {
            File.Copy("report.xls", "reportOverwriteMultiple.xls", true);

            Stopwatch sw = new Stopwatch();

            using (var cf = new CompoundFile("reportOverwriteMultiple.xls", CFSUpdateMode.Update, CFSConfiguration.SectorRecycle))
            {
                sw.Start();

                Random r = new Random();

                for (int i = 0; i < 1000; i++)
                {
                    byte[] buffer = Helpers.GetBuffer(r.Next(100, 3500), 0x0A);

                    if (i > 0)
                    {
                        if (r.Next(0, 100) > 50)
                        {
                            cf.RootStorage.Delete("MyNewStream" + (i - 1).ToString());
                        }
                    }

                    CFStream addedStream = cf.RootStorage.AddStream("MyNewStream" + i.ToString());

                    addedStream.SetData(buffer);

                    // Random commit, not on single addition
                    if (r.Next(0, 100) > 50)
                    {
                        cf.Commit();
                    }
                }

                cf.Close();
                sw.Stop();
            }

            Console.WriteLine(sw.ElapsedMilliseconds);

            for (int i = 0; i < sw.ElapsedMilliseconds; i++)
            {
                _testCounter.Increment();
            }
        }
Example #27
0
        public static void SetHistFileDataPaths(string historyfile, string rawpath, string headerpath = null)
        {
            if (Path.GetExtension(historyfile) != ".ehst2")
            {
                throw new ArgumentException($"History file {historyfile} extension isn't .ehst2");
            }
            var hf = new CompoundFile(historyfile, CFSUpdateMode.Update, CFSConfiguration.Default);

            SetPaths(hf, "DataPath", rawpath);
            if (headerpath != null)
            {
                SetPaths(hf, "HeaderPath", headerpath);
            }
            hf.Commit();
            hf.Close();
            Console.WriteLine($"Wrote new paths to {historyfile}");
        }
Example #28
0
        public void Test_DELETE_ZERO_LENGTH_STREAM()
        {
            byte[] b = new byte[0];

            CompoundFile cf = new CompoundFile();

            string   zeroLengthName = "MyZeroStream";
            CFStream myStream       = cf.RootStorage.AddStream(zeroLengthName);

            Assert.IsNotNull(myStream);

            try
            {
                myStream.SetData(b);
            }
            catch
            {
                Assert.Fail("Failed setting zero length stream");
            }

            string filename = "DeleteZeroLengthStream.cfs";

            cf.Save(filename);
            cf.Close();

            CompoundFile cf2 = new CompoundFile(filename);

            // Execption in next line!
            cf2.RootStorage.Delete(zeroLengthName);

            CFStream zeroStream2 = null;

            try
            {
                zeroStream2 = cf2.RootStorage.GetStream(zeroLengthName);
            }
            catch (Exception ex)
            {
                Assert.IsNull(zeroStream2);
                Assert.IsInstanceOf <CFItemNotFound>(ex);
            }

            cf2.Save("MultipleDeleteMiniStream.cfs");
            cf2.Close();
        }
        public static byte[][] ReadGameItems(string fileName, int numGameItems)
        {
            var gameItemData = new byte[numGameItems][];
            var cf           = new CompoundFile(fileName);

            try {
                var storage = cf.RootStorage.GetStorage("GameStg");
                for (var i = 0; i < numGameItems; i++)
                {
                    var itemName   = $"GameItem{i}";
                    var itemStream = storage.GetStream(itemName);
                    gameItemData[i] = itemStream.GetData();
                }
            } finally {
                cf.Close();
            }
            return(gameItemData);
        }
Example #30
0
        public void Test_COPY_FROM_STREAM()
        {
            byte[]       b  = Helpers.GetBuffer(100);
            MemoryStream ms = new MemoryStream(b);

            CompoundFile cf = new CompoundFile();
            CFStream     st = cf.RootStorage.AddStream("MyImportedStream");

            st.CopyFrom(ms);
            ms.Close();
            cf.Save("COPY_FROM_STREAM.cfs");
            cf.Close();

            cf = new CompoundFile("COPY_FROM_STREAM.cfs");
            byte[] data = cf.RootStorage.GetStream("MyImportedStream").GetData();

            Assert.IsTrue(Helpers.CompareBuffer(b, data));
        }
Example #31
0
        public void Test_WRITE_MINI_STREAM()
        {
            const int BUFFER_LENGTH = 1023; // < 4096

            byte[] b = Helpers.GetBuffer(BUFFER_LENGTH);

            CompoundFile cf       = new CompoundFile();
            CFStream     myStream = cf.RootStorage.AddStream("MyMiniStream");

            Assert.IsNotNull(myStream);
            Assert.IsTrue(myStream.Size == 0);

            myStream.SetData(b);

            Assert.IsTrue(myStream.Size == BUFFER_LENGTH, "Mini Stream size differs from buffer size");

            cf.Close();
        }
Example #32
0
        public void Test_WRONG_CORRUPTED_EXCEPTION()
        {
            var cf = new CompoundFile();

            for (int i = 0; i < 100; i++)
            {
                cf.RootStorage.AddStream("Stream" + i).SetData(Helpers.GetBuffer(100000, 0xAA));
            }

            cf.RootStorage.AddStream("BigStream").SetData(Helpers.GetBuffer(5250000, 0xAA));

            using (var stream = new MemoryStream())
            {
                cf.Save(stream);
            }

            cf.Close();
        }
Example #33
0
        public void Test_CORRUPTED_CYCLIC_FAT_CHECK()
        {
            CompoundFile f = null;
            try
            {
                f = new CompoundFile("CyclicFAT.cfs");

            }
            catch (Exception ex)
            {
                Assert.IsTrue(ex is CFCorruptedFileException);
            }
            finally
            {
                if (f != null)
                    f.Close();
            }
        }
Example #34
0
        public void Test_DELETE_STREAM()
        {
            String FILENAME = "MultipleStorage3.cfs";
            CompoundFile cf = new CompoundFile(FILENAME);

            ICFStorage found = null;
            VisitedEntryAction action = delegate(ICFItem item) { if (item.Name == "AnotherStorage") found = item as ICFStorage; };
            cf.RootStorage.VisitEntries(action, true);

            Assert.IsNotNull(found);

            found.Delete("Another2Stream");

            cf.Save("MultipleDeleteStream");
            cf.Close();
        }
Example #35
0
        public void Test_TRANSACTED_ADD_REMOVE_MULTIPLE_STREAM_TO_EXISTING_FILE()
        {
            String srcFilename = "report.xls";
            String dstFilename = "reportOverwriteMultiple.xls";

            File.Copy(srcFilename, dstFilename, true);

            CompoundFile cf = new CompoundFile(dstFilename, UpdateMode.ReadOnly, true, false);

            //CompoundFile cf = new CompoundFile();

            Random r = new Random();

            for (int i = 0; i < 254; i++)
            {
                //byte[] buffer = Helpers.GetBuffer(r.Next(100, 3500), (byte)i);
                byte[] buffer = Helpers.GetBuffer(1995, 1);

                //if (i > 0)
                //{
                //    if (r.Next(0, 100) > 50)
                //    {
                //        cf.RootStorage.Delete("MyNewStream" + (i - 1).ToString());
                //    }
                //}

                ICFStream addedStream = cf.RootStorage.AddStream("MyNewStream" + i.ToString());
                Assert.IsNotNull(addedStream, "Stream not found");
                addedStream.SetData(buffer);

                Assert.IsTrue(Helpers.CompareBuffer(addedStream.GetData(), buffer), "Data buffer corrupted");

                // Random commit, not on single addition
                //if (r.Next(0, 100) > 50)
                //    cf.UpdateFile();

            }

            cf.Save(dstFilename + "PP");
            cf.Close();

            if (File.Exists("reportOverwriteMultiple.xls"))
                File.Delete("reportOverwriteMultiple.xls");

            if (File.Exists("reportOverwriteMultiple.xlsPP"))
                File.Delete("reportOverwriteMultiple.xlsPP");


        }
Example #36
0
        public void Test_WRITE_AND_READ_CFS()
        {
            String filename = "WRITE_AND_READ_CFS.cfs";

            CompoundFile cf = new CompoundFile();

            ICFStorage st = cf.RootStorage.AddStorage("MyStorage");
            ICFStream sm = st.AddStream("MyStream");
            byte[] b = Helpers.GetBuffer(220, 0x0A);
            sm.SetData(b);

            cf.Save(filename);
            cf.Close();

            CompoundFile cf2 = new CompoundFile(filename);
            ICFStorage st2 = cf2.RootStorage.GetStorage("MyStorage");
            ICFStream sm2 = st2.GetStream("MyStream");
            cf2.Close();

            Assert.IsNotNull(sm2);
            Assert.IsTrue(sm2.Size == 220);


            if (File.Exists(filename))
                File.Delete(filename);


        }
Example #37
0
        public void Test_TRANSACTED_ADD_MINISTREAM_TO_EXISTING_FILE()
        {
            String srcFilename = "report.xls";
            String dstFilename = "reportOverwriteMultiple.xls";

            File.Copy(srcFilename, dstFilename, true);

            CompoundFile cf = new CompoundFile(dstFilename, UpdateMode.Update, true, true);

            Random r = new Random();

            byte[] buffer = Helpers.GetBuffer(31, 0x0A);

            cf.RootStorage.AddStream("MyStream").SetData(buffer);
            cf.Commit();
            cf.Close();
            FileStream larger = new FileStream(dstFilename, FileMode.Open);
            FileStream smaller = new FileStream(srcFilename, FileMode.Open);

            // Equal condition if minisector can be "allocated"
            // within the existing standard sector border
            Assert.IsTrue(larger.Length >= smaller.Length);

            larger.Close();
            smaller.Close();

            if (File.Exists("reportOverwriteMultiple.xlsPP"))
                File.Delete("reportOverwriteMultiple.xlsPP");


        }
Example #38
0
        public void Test_CREATE_STORAGE_WITH_CREATION_DATE()
        {
            const String STORAGE_NAME = "NewStorage1";
            CompoundFile cf = new CompoundFile();

            ICFStorage st = cf.RootStorage.AddStorage(STORAGE_NAME);
            st.CreationDate = DateTime.Now;

            Assert.IsNotNull(st);
            Assert.AreEqual(STORAGE_NAME, st.Name, false);

            cf.Save("ProvaData.cfs");
            cf.Close();
        }
Example #39
0
        public void Test_DELETE_STREAM_SECTOR_REUSE()
        {
            CompoundFile cf = null;
            ICFStream st = null;

            byte[] b = Helpers.GetBuffer(1024 * 1024 * 2); //2MB buffer
            //byte[] b = Helpers.GetBuffer(5000); 
            byte[] cmp = new byte[] { 0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7 };

            cf = new CompoundFile(CFSVersion.Ver_4, false, false);
            st = cf.RootStorage.AddStream("AStream");
            st.AppendData(b);
            cf.Save("SectorRecycle.cfs");
            cf.Close();


            cf = new CompoundFile("SectorRecycle.cfs", UpdateMode.Update, true, false);
            cf.RootStorage.Delete("AStream");
            cf.Commit(true);
            cf.Close();

            cf = new CompoundFile("SectorRecycle.cfs", UpdateMode.ReadOnly, false, false); //No sector recycle
            st = cf.RootStorage.AddStream("BStream");
            st.AppendData(Helpers.GetBuffer(1024 * 1024 * 1));
            cf.Save("SectorRecycleLarger.cfs");
            cf.Close();

            Assert.IsFalse((new FileInfo("SectorRecycle.cfs").Length) >= (new FileInfo("SectorRecycleLarger.cfs").Length));

            cf = new CompoundFile("SectorRecycle.cfs", UpdateMode.ReadOnly, true, false);
            st = cf.RootStorage.AddStream("BStream");
            st.AppendData(Helpers.GetBuffer(1024 * 1024 * 1));
            cf.Save("SectorRecycleSmaller.cfs");
            cf.Close();

            Assert.IsTrue((new FileInfo("SectorRecycle.cfs").Length) >= (new FileInfo("SectorRecycleSmaller.cfs").Length));

        }
Example #40
0
        public void Test_COPY_FROM_STREAM()
        {
            byte[] b = Helpers.GetBuffer(100);
            MemoryStream ms = new MemoryStream(b);

            CompoundFile cf = new CompoundFile();
            CFStream st = cf.RootStorage.AddStream("MyImportedStream") as CFStream;
            st.CopyFrom(ms);
            ms.Close();
            cf.Save("COPY_FROM_STREAM.cfs");
            cf.Close();

            cf = new CompoundFile("COPY_FROM_STREAM.cfs");
            byte[] data = cf.RootStorage.GetStream("MyImportedStream").GetData();

            Assert.IsTrue(Helpers.CompareBuffer(b, data));

        }
Example #41
0
        public void Test_APPEND_DATA_TO_STREAM()
        {
            MemoryStream ms = new MemoryStream();

            byte[] b = new byte[] { 0x0, 0x1, 0x2, 0x3 };
            byte[] b2 = new byte[] { 0x4, 0x5, 0x6, 0x7 };

            CompoundFile cf = new CompoundFile();
            ICFStream st = cf.RootStorage.AddStream("MyLargeStream");
            st.SetData(b);
            st.AppendData(b2);

            cf.Save(ms);
            cf.Close();

            byte[] cmp = new byte[] { 0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7 };
            cf = new CompoundFile(ms);
            byte[] data = cf.RootStorage.GetStream("MyLargeStream").GetData();
            Assert.IsTrue(Helpers.CompareBuffer(cmp, data));

        }
Example #42
0
        private void SingleWriteReadMatchingSTREAMED(int size)
        {
            MemoryStream ms = new MemoryStream(size);

            CompoundFile cf = new CompoundFile();
            ICFStorage st = cf.RootStorage.AddStorage("MyStorage");
            ICFStream sm = st.AddStream("MyStream");

            byte[] b = Helpers.GetBuffer(size);

            sm.SetData(b);
            cf.Save(ms);
            cf.Close();

            CompoundFile cf2 = new CompoundFile(ms);
            ICFStorage st2 = cf2.RootStorage.GetStorage("MyStorage");
            ICFStream sm2 = st2.GetStream("MyStream");

            Assert.IsNotNull(sm2);
            Assert.IsTrue(sm2.Size == size);
            Assert.IsTrue(Helpers.CompareBuffer(sm2.GetData(), b));

            cf2.Close();
        }
Example #43
0
        private void SingleWriteReadMatching(int size)
        {

            String filename = "INCREMENTAL_SIZE_MULTIPLE_WRITE_AND_READ_CFS.cfs";

            if (File.Exists(filename))
                File.Delete(filename);

            CompoundFile cf = new CompoundFile();
            ICFStorage st = cf.RootStorage.AddStorage("MyStorage");
            ICFStream sm = st.AddStream("MyStream");

            byte[] b = Helpers.GetBuffer(size);

            sm.SetData(b);
            cf.Save(filename);
            cf.Close();

            CompoundFile cf2 = new CompoundFile(filename);
            ICFStorage st2 = cf2.RootStorage.GetStorage("MyStorage");
            ICFStream sm2 = st2.GetStream("MyStream");

            Assert.IsNotNull(sm2);
            Assert.IsTrue(sm2.Size == size);
            Assert.IsTrue(Helpers.CompareBuffer(sm2.GetData(), b));

            cf2.Close();
        }
Example #44
0
        public void Test_CHECK_DISPOSED_()
        {
            const String FILENAME = "MultipleStorage.cfs";
            CompoundFile cf = new CompoundFile(FILENAME);

            ICFStorage st = cf.RootStorage.GetStorage("MyStorage");
            cf.Close();

            try
            {
                byte[] temp = st.GetStream("MyStream").GetData();
                Assert.Fail("Stream without media");
            }
            catch (Exception ex)
            {
                Assert.IsTrue(ex is CFDisposedException);
            }
        }
Example #45
0
        public void Test_LAZY_LOAD_CHILDREN_()
        {
            CompoundFile cf = new CompoundFile();
            cf.RootStorage.AddStorage("Level_1")
                .AddStorage("Level_2")
                .AddStream("Level2Stream")
                .SetData(Helpers.GetBuffer(100));

            cf.Save("$Hel1");

            cf.Close();

            cf = new CompoundFile("$Hel1");
            IList<ICFItem> i = cf.GetAllNamedEntries("Level2Stream");
            Assert.IsNotNull(i[0]);
            Assert.IsTrue(i[0] is ICFStream);
            Assert.IsTrue((i[0] as ICFStream).GetData().Length == 100);
            cf.Save("$Hel2");
            cf.Close();

            if (File.Exists("$Hel1"))
            {
                File.Delete("$Hel1");
            }
               if (File.Exists("$Hel2"))
            {
                File.Delete("$Hel2");
            }
        }
Example #46
0
        public void Test_RE_WRITE_SMALLER_STREAM()
        {
            const int BUFFER_LENGTH = 8000;

            String filename = "report.xls";

            byte[] b = Helpers.GetBuffer(BUFFER_LENGTH);

            CompoundFile cf = new CompoundFile(filename);
            ICFStream foundStream = cf.RootStorage.GetStream("Workbook");
            foundStream.SetData(b);
            cf.Save("reportRW_SMALL.xls");
            cf.Close();

            cf = new CompoundFile("reportRW_SMALL.xls");
            byte[] c = cf.RootStorage.GetStream("Workbook").GetData();
            Assert.IsTrue(c.Length == BUFFER_LENGTH);
            cf.Close();

            if (File.Exists("reportRW_SMALL.xls"))
                File.Delete("reportRW_SMALL.xls");

        }
Example #47
0
        public void Test_WRITE_STREAM_WITH_DIFAT()
        {
            //const int SIZE = 15388609; //Incredible condition of 'resonance' between FAT and DIFAT sec number
            const int SIZE = 15345665; // 64 -> 65 NOT working (in the past ;-)  )
            byte[] b = Helpers.GetBuffer(SIZE, 0);

            CompoundFile cf = new CompoundFile();
            ICFStream myStream = cf.RootStorage.AddStream("MyStream");
            Assert.IsNotNull(myStream);
            myStream.SetData(b);

            cf.Save("WRITE_STREAM_WITH_DIFAT.cfs");
            cf.Close();


            CompoundFile cf2 = new CompoundFile("WRITE_STREAM_WITH_DIFAT.cfs");
            ICFStream st = cf2.RootStorage.GetStream("MyStream");

            Assert.IsNotNull(cf2);
            Assert.IsTrue(st.Size == SIZE);

            Assert.IsTrue(Helpers.CompareBuffer(b, st.GetData()));

            cf2.Close();

            if (File.Exists("WRITE_STREAM_WITH_DIFAT.cfs"))
                File.Delete("WRITE_STREAM_WITH_DIFAT.cfs");

        }
Example #48
0
        public void Test_TRANSACTED_REMOVE_MINI_STREAM_ADD_MINISTREAM_TO_EXISTING_FILE()
        {
            String srcFilename = "report.xls";
            String dstFilename = "reportOverwrite2.xls";

            File.Copy(srcFilename, dstFilename, true);

            CompoundFile cf = new CompoundFile(dstFilename, UpdateMode.Update, true, true);

            cf.RootStorage.Delete("\x05SummaryInformation");

            byte[] buffer = Helpers.GetBuffer(2000);

            ICFStream addedStream = cf.RootStorage.AddStream("MyNewStream");
            addedStream.SetData(buffer);

            cf.Commit();
            cf.Close();

            if (File.Exists("reportOverwrite2.xlsPP"))
                File.Delete("reportOverwrite2.xlsPP");


        }
Example #49
0
        public void Test_WRITE_STREAM()
        {
            const int BUFFER_LENGTH = 10000;

            byte[] b = Helpers.GetBuffer(BUFFER_LENGTH);

            CompoundFile cf = new CompoundFile();
            ICFStream myStream = cf.RootStorage.AddStream("MyStream");

            Assert.IsNotNull(myStream);
            Assert.IsTrue(myStream.Size == 0);

            myStream.SetData(b);

            Assert.IsTrue(myStream.Size == BUFFER_LENGTH, "Stream size differs from buffer size");

            cf.Close();
        }
Example #50
0
        public void Test_DELETE_ZERO_LENGTH_STREAM()
        {
            byte[] b = new byte[0];

            CompoundFile cf = new CompoundFile();

            string zeroLengthName = "MyZeroStream";
            ICFStream myStream = cf.RootStorage.AddStream(zeroLengthName);

            Assert.IsNotNull(myStream);

            try
            {
                myStream.SetData(b);
            }
            catch
            {
                Assert.Fail("Failed setting zero length stream");
            }

            string filename = "DeleteZeroLengthStream.cfs";
            cf.Save(filename);
            cf.Close();

            CompoundFile cf2 = new CompoundFile(filename);

            // Execption in next line!
            cf2.RootStorage.Delete(zeroLengthName);

            ICFStream zeroStream2 = null;

            try
            {
                zeroStream2 = cf2.RootStorage.GetStream(zeroLengthName);
            }
            catch (Exception ex)
            {
                Assert.IsNull(zeroStream2);
                Assert.IsInstanceOfType(ex, typeof(CFItemNotFound));
            }

            cf2.Save("MultipleDeleteMiniStream.cfs");
            cf2.Close();
        }
Example #51
0
        public void Test_WRITE_MINISTREAM_READ_REWRITE_STREAM()
        {
            const int BIGGER_SIZE = 350;
            //const int SMALLER_SIZE = 290;
            const int MEGA_SIZE = 18000000;

            byte[] ba1 = Helpers.GetBuffer(BIGGER_SIZE, 1);
            byte[] ba2 = Helpers.GetBuffer(BIGGER_SIZE, 2);
            byte[] ba3 = Helpers.GetBuffer(BIGGER_SIZE, 3);
            byte[] ba4 = Helpers.GetBuffer(BIGGER_SIZE, 4);
            byte[] ba5 = Helpers.GetBuffer(BIGGER_SIZE, 5);

            //WRITE 5 (mini)streams in a compound file --

            CompoundFile cfa = new CompoundFile();

            ICFStream myStream = cfa.RootStorage.AddStream("MyFirstStream");
            Assert.IsNotNull(myStream);

            myStream.SetData(ba1);
            Assert.IsTrue(myStream.Size == BIGGER_SIZE);

            ICFStream myStream2 = cfa.RootStorage.AddStream("MySecondStream");
            Assert.IsNotNull(myStream2);

            myStream2.SetData(ba2);
            Assert.IsTrue(myStream2.Size == BIGGER_SIZE);

            ICFStream myStream3 = cfa.RootStorage.AddStream("MyThirdStream");
            Assert.IsNotNull(myStream3);

            myStream3.SetData(ba3);
            Assert.IsTrue(myStream3.Size == BIGGER_SIZE);

            ICFStream myStream4 = cfa.RootStorage.AddStream("MyFourthStream");
            Assert.IsNotNull(myStream4);

            myStream4.SetData(ba4);
            Assert.IsTrue(myStream4.Size == BIGGER_SIZE);

            ICFStream myStream5 = cfa.RootStorage.AddStream("MyFifthStream");
            Assert.IsNotNull(myStream5);

            myStream5.SetData(ba5);
            Assert.IsTrue(myStream5.Size == BIGGER_SIZE);

            cfa.Save("WRITE_MINISTREAM_READ_REWRITE_STREAM.cfs");

            cfa.Close();

            // Now get the second stream and rewrite it smaller
            byte[] bb = Helpers.GetBuffer(MEGA_SIZE);
            CompoundFile cfb = new CompoundFile("WRITE_MINISTREAM_READ_REWRITE_STREAM.cfs");
            ICFStream myStreamB = cfb.RootStorage.GetStream("MySecondStream");
            Assert.IsNotNull(myStreamB);
            myStreamB.SetData(bb);
            Assert.IsTrue(myStreamB.Size == MEGA_SIZE);

            byte[] bufferB = myStreamB.GetData();
            cfb.Save("WRITE_MINISTREAM_READ_REWRITE_STREAM_2ND.cfs");
            cfb.Close();

            CompoundFile cfc = new CompoundFile("WRITE_MINISTREAM_READ_REWRITE_STREAM_2ND.cfs");
            ICFStream myStreamC = cfc.RootStorage.GetStream("MySecondStream");
            Assert.IsTrue(myStreamC.Size == MEGA_SIZE, "DATA SIZE FAILED");

            byte[] bufferC = myStreamC.GetData();
            Assert.IsTrue(Helpers.CompareBuffer(bufferB, bufferC), "DATA INTEGRITY FAILED");

            cfc.Close();

            if (File.Exists("WRITE_MINISTREAM_READ_REWRITE_STREAM.cfs"))
                File.Delete("WRITE_MINISTREAM_READ_REWRITE_STREAM.cfs");


            if (File.Exists("WRITE_MINISTREAM_READ_REWRITE_STREAM_2ND.cfs"))
                File.Delete("WRITE_MINISTREAM_READ_REWRITE_STREAM_2ND.cfs");

        }
Example #52
0
        public IVisualPinballManager SetTableScript(string path, string tableScript)
        {
            _logger.Info("Saving table script to {0}", path);

            var cf = new CompoundFile(path, CFSUpdateMode.Update, CFSConfiguration.Default);
            var storage = cf.RootStorage.GetStorage("GameStg");

            // 1. verify that we can do it
            _logger.Info("Checking checksum capabilities...");
            var computedChecksum = ComputeChecksum(cf);
            var storedChecksum = ReadChecksum(storage);
            if (!computedChecksum.SequenceEqual(storedChecksum)) {
                throw new FormatException("Could not recompute checksum.");
            }
            _logger.Info("Checkum looks good, computed the same as found in file.");
            _logger.Info("Checking BIFF capabilities...");
            var gameData = new BiffSerializer(storage.GetStream("GameData").GetData());
            var parsers = new Dictionary<string, BiffSerializer.IBiffTagSerializer> {
                    { "CODE", new BiffSerializer.SimpleSerializer() }
            };
            gameData.Deserialize(parsers);
            if (!gameData.CheckConsistency()) {
                throw new FormatException("Could not re-serialize BIFF data.");
            }
            _logger.Info("Un- and re-serialized BIFF data and got the same result. We're good to go!");

            try {

                // 2. update table script
                _logger.Info("Updating CODE data in BIFF stream...");
                gameData.SetString("CODE", tableScript);
                storage.GetStream("GameData").SetData(gameData.Serialize());
                cf.Commit();
                cf.Close();

                // 3. update hash
                cf = new CompoundFile(path, CFSUpdateMode.Update, CFSConfiguration.Default);
                storage = cf.RootStorage.GetStorage("GameStg");
                var hash = ComputeChecksum(cf);
                _logger.Info("Setting new checksum {0}...", BitConverter.ToString(hash));
                storage.GetStream("MAC").SetData(hash);
                cf.Commit();
                cf.Close();
                _logger.Info("Done!");

            } catch (CFItemNotFound e) {
                _logger.Error(e, "Error patching file!");
                _crashManager.Report(e, "vpt");
            }

            return this;
        }
Example #53
0
        public void Test_ZERO_LENGTH_RE_WRITE_STREAM()
        {
            byte[] b = new byte[0];

            CompoundFile cf = new CompoundFile();
            ICFStream myStream = cf.RootStorage.AddStream("MyStream");

            Assert.IsNotNull(myStream);

            try
            {
                myStream.SetData(b);
            }
            catch
            {
                Assert.Fail("Failed setting zero length stream");
            }

            cf.Save("ZERO_LENGTH_STREAM_RE.cfs");
            cf.Close();

            CompoundFile cfo = new CompoundFile("ZERO_LENGTH_STREAM_RE.cfs");
            ICFStream oStream = cfo.RootStorage.GetStream("MyStream");

            Assert.IsNotNull(oStream);
            Assert.IsTrue(oStream.Size == 0);

            try
            {
                oStream.SetData(Helpers.GetBuffer(30));
                cfo.Save("ZERO_LENGTH_STREAM_RE2.cfs");
            }
            catch
            {
                Assert.Fail("Failed re-writing zero length stream");
            }
            finally
            {
                cfo.Close();
            }

            if (File.Exists("ZERO_LENGTH_STREAM_RE.cfs"))
                File.Delete("ZERO_LENGTH_STREAM_RE.cfs");

            if (File.Exists("ZERO_LENGTH_STREAM_RE2.cfs"))
                File.Delete("ZERO_LENGTH_STREAM_RE2.cfs");

        }
Example #54
0
        private void DeleteUnwantedJumpListEntries()
        {
            try
            {
                foreach (JumpList JumpListObj in this.JumpListFiles.JumpLists)
                {
                    foreach (string Keyword in this.KeywordsList)
                    {
                        if (JumpListObj.DestListEntry.NetBiosName.Contains(Keyword))
                        {
                            JumpListObj.DestListEntry.PendingDelete = true;
                        }
                        else if (JumpListObj.DestListEntry.Data.Contains(Keyword))
                        {
                            JumpListObj.DestListEntry.PendingDelete = true;
                        }
                    }
                }
                byte[] dl2 = this.CreateDestListBinaryFromDestListObj(this.JumpListFiles, true);
                CompoundFile JumpListFileCompound;

                try
                {
                    JumpListFileCompound = new CompoundFile(this.PathToJumpListFile, UpdateMode.Update, false, true);
                }
                catch (IOException e)
                {
                    this.ScrubberGUIInst.DebugPrint("Error (JumpList): Error opening JumpList database file");
                    this.ScrubberGUIInst.DebugPrint("Error (JumpList): Details - " + e.Message);

                    return;
                }

                foreach (JumpList JumpListObj in this.JumpListFiles.JumpLists)
                {
                    if (JumpListObj.DestListEntry.PendingDelete) {
                        try
                        {
                            this.ScrubberGUIInst.DebugPrint("Scrubbing (JumpList): " + JumpListObj.DestListEntry.Data);
                            JumpListFileCompound.RootStorage.Delete(JumpListObj.DestListEntry.StreamNo);
                        }
                        catch (NullReferenceException e)
                        {
                            //this.ScrubberGUIInst.DebugPrint("Error (JumpList): List seems empty. List: " + JumpListObj.Name);
                            this.ScrubberGUIInst.DebugPrint("Error (JumpList): Details - " + e.Message);
                        }
                        catch (CFItemNotFound e)
                        {
                            this.ScrubberGUIInst.DebugPrint("Error (JumpList): Details - " + e.Message);
                        }
                    }
                }

                JumpListFileCompound.RootStorage.GetStream("DestList").SetData(dl2);
                JumpListFileCompound.Commit();
                JumpListFileCompound.Close();
            }
            catch (NullReferenceException e)
            {
                //this.ScrubberGUIInst.DebugPrint("Error (JumpList): List seems empty.");
                this.ScrubberGUIInst.DebugPrint("Error (JumpList): Details - "+e.Message);
            }
        }
Example #55
0
        public void Test_ZERO_LENGTH_WRITE_STREAM()
        {
            byte[] b = new byte[0];

            CompoundFile cf = new CompoundFile();
            ICFStream myStream = cf.RootStorage.AddStream("MyStream");

            Assert.IsNotNull(myStream);

            try
            {
                myStream.SetData(b); cf.Save("ZERO_LENGTH_STREAM.cfs");
            }
            catch
            {
                Assert.Fail("Failed setting zero length stream");
            }
            finally
            {
                if (cf != null)
                    cf.Close();
            }

            if (File.Exists("ZERO_LENGTH_STREAM.cfs"))
                File.Delete("ZERO_LENGTH_STREAM.cfs");

        }
Example #56
0
        private bool ParseJumpList(string JumpListFilePath)
        {
            this.DestListObj = new DestList();
            JumpListFile JumpListFileObj = new JumpListFile
            {
                FilePath = JumpListFilePath,
                FileName = System.IO.Path.GetFileName(JumpListFilePath)
            };
            CompoundFile JumpListFileCompound = new CompoundFile(JumpListFilePath);
            CFStream JumpListDestListStream = JumpListFileCompound.RootStorage.GetStream("DestList");
            JumpListFileObj.DestListSize = JumpListDestListStream.Size;
            List<DestListEntry> JumpListEntries = this.ParseDestList(JumpListDestListStream.GetData());
            JumpListFileObj.DestListEntries = JumpListEntries;
            List<DestListEntry> JumpListEntriesToRemove = new List<DestListEntry>();
            foreach (DestListEntry JumpListEntry in JumpListEntries)
            {
                CFStream JumpListEntryStream = null;
                bool err = false;
                try
                {
                    try
                    {
                        JumpListEntryStream = JumpListFileCompound.RootStorage.GetStream(JumpListEntry.StreamNo);
                    }
                    catch (OpenMcdf.CFItemNotFound e)
                    {
                        this.ScrubberGUIInst.DebugPrint("Error (JumpList): List seems empty. List: " + JumpListFilePath + " StreamNo: " + JumpListEntry.StreamNo);
                        JumpListFileCompound.Close();
                        JumpListEntriesToRemove.Add(JumpListEntry);
                        err = true;
                        continue;
                    }
                    JumpList JumpListObj = new JumpList
                    {
                        Name = JumpListEntry.StreamNo,
                        Size = JumpListEntryStream.GetData().Length,
                        DestListEntry = JumpListEntry
                    };
                    JumpListFileObj.JumpLists.Add(JumpListObj);
                    this.JumpListFiles = JumpListFileObj;
                    if (!err)
                    {
                        JumpListFileObj.JumpLists.Add(JumpListObj);
                    }
                    this.JumpListFiles = JumpListFileObj;
                }

                catch (Exception e)
                {
                    this.ScrubberGUIInst.DebugPrint(e.ToString());
                    continue;
                }
            }JumpListFileCompound.Close();
            foreach (DestListEntry JumpListEntry in JumpListEntriesToRemove)
            {
                JumpListEntries.Remove(JumpListEntry);
                JumpListFileObj.DestListEntries.Remove(JumpListEntry);
                //File.Delete(JumpListFilePath);
            }
            if (JumpListEntries.Count == 0)
                return false;
            this._jumpListFiles.Add(JumpListFileObj);
            return true;
        }
Example #57
0
        public void Test_VISIT_STORAGE()
        {
            String FILENAME = "testVisiting.xls";

            // Remove...
            if (File.Exists(FILENAME))
                File.Delete(FILENAME);

            //Create...

            CompoundFile ncf = new CompoundFile();

            ICFStorage l1 = ncf.RootStorage.AddStorage("Storage Level 1");
            l1.AddStream("l1ns1");
            l1.AddStream("l1ns2");
            l1.AddStream("l1ns3");

            ICFStorage l2 = l1.AddStorage("Storage Level 2");
            l2.AddStream("l2ns1");
            l2.AddStream("l2ns2");

            ncf.Save(FILENAME);
            ncf.Close();


            // Read...

            CompoundFile cf = new CompoundFile(FILENAME);

            FileStream output = new FileStream("reportVisit.txt", FileMode.Create);
            TextWriter sw = new StreamWriter(output);

            Console.SetOut(sw);

            VisitedEntryAction va = delegate(ICFItem target)
            {
                sw.WriteLine(target.Name);
            };

            cf.RootStorage.VisitEntries(va, true);

            cf.Close();
            sw.Close();
        }
Example #58
0
        public void Test_DELETE_DIRECTORY()
        {
            String FILENAME = "MultipleStorage2.cfs";
            CompoundFile cf = new CompoundFile(FILENAME, UpdateMode.ReadOnly, false, false);

            ICFStorage st = cf.RootStorage.GetStorage("MyStorage");

            Assert.IsNotNull(st);

            st.Delete("AnotherStorage");

            cf.Save("MultipleStorage_Delete.cfs");

            cf.Close();
        }
Example #59
0
        public void Test_APPEND_DATA_TO_CREATE_LARGE_STREAM()
        {
            byte[] b = Helpers.GetBuffer(1024 * 1024 * 50); //2GB buffer
            byte[] cmp = new byte[] { 0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7 };

            CompoundFile cf = new CompoundFile(CFSVersion.Ver_4, false, false);
            CFStream st = cf.RootStorage.AddStream("MySuperLargeStream");
            cf.Save("MEGALARGESSIMUSFILE.cfs");
            cf.Close();


            cf = new CompoundFile("MEGALARGESSIMUSFILE.cfs", UpdateMode.Update, false, false);
            CFStream cfst = cf.RootStorage.GetStream("MySuperLargeStream");
            for (int i = 0; i < 42; i++)
            {
                cfst.AppendData(b);
                cf.Commit(true);
            }

            cfst.AppendData(cmp);
            cf.Commit(true);

            cf.Close();


            cf = new CompoundFile("MEGALARGESSIMUSFILE.cfs");
            int count = 8;
            byte[] data = cf.RootStorage.GetStream("MySuperLargeStream").GetData((long)b.Length * 42L, ref count);
            Assert.IsTrue(Helpers.CompareBuffer(cmp, data));
            cf.Close();

        }
Example #60
0
        public void Test_RE_WRITE_SMALLER_MINI_STREAM()
        {
            String filename = "report.xls";

            CompoundFile cf = new CompoundFile(filename);
            ICFStream foundStream = cf.RootStorage.GetStream("\x05SummaryInformation");
            int TEST_LENGTH = (int)foundStream.Size - 20;
            byte[] b = Helpers.GetBuffer(TEST_LENGTH);
            foundStream.SetData(b);

            cf.Save("RE_WRITE_SMALLER_MINI_STREAM.xls");
            cf.Close();

            cf = new CompoundFile("RE_WRITE_SMALLER_MINI_STREAM.xls");
            byte[] c = cf.RootStorage.GetStream("\x05SummaryInformation").GetData();
            Assert.IsTrue(c.Length == TEST_LENGTH);
            cf.Close();

            if (File.Exists("RE_WRITE_SMALLER_MINI_STREAM.xls"))
                File.Delete("RE_WRITE_SMALLER_MINI_STREAM.xls");

        }