Exemplo n.º 1
0
        public static async Task ConvertNcnnModel(string modelPath)
        {
            string modelName = Path.GetFileName(modelPath);

            ncnnDir = Path.Combine(Config.Get("modelPath"), ".ncnn");
            Directory.CreateDirectory(ncnnDir);
            string outPath = Path.Combine(ncnnDir, Path.ChangeExtension(modelName, null));

            Logger.Log("Checking for NCNN model: " + outPath);
            if (IOUtils.GetAmountOfFiles(outPath, false) < 2)
            {
                Logger.Log("Running model converter...");
                DialogForm dialog = new DialogForm("Converting ESRGAN model to NCNN format...");
                await RunConverter(modelPath);

                string moveFrom = Path.Combine(Config.Get("esrganPath"), Path.ChangeExtension(modelName, null));
                Logger.Log("Moving " + moveFrom + " to " + outPath);
                IOUtils.Copy(moveFrom, outPath, "*", true);
                Directory.Delete(moveFrom, true);
                dialog.Close();
            }
            else
            {
                Logger.Log("NCNN Model is cached - Skipping conversion.");
            }
            ESRGAN.currentNcnnModel = outPath;
        }
Exemplo n.º 2
0
        public void Bug57080()
        {
            // the test file Contains a wrong ole entry size, produced by extenxls
            // the fix limits the available size and tries to read all entries
            FileStream       f    = POIDataSamples.GetPOIFSInstance().GetFile("extenxls_pwd123.xlsx");
            NPOIFSFileSystem fs   = new NPOIFSFileSystem(f, true);
            EncryptionInfo   info = new EncryptionInfo(fs);
            Decryptor        d    = Decryptor.GetInstance(info);

            d.VerifyPassword("pwd123");
            MemoryStream   bos = new MemoryStream();
            ZipInputStream zis = new ZipInputStream(d.GetDataStream(fs));
            ZipEntry       ze;

            while ((ze = zis.GetNextEntry()) != null)
            {
                //bos.Reset();
                bos.Seek(0, SeekOrigin.Begin);
                bos.SetLength(0);
                IOUtils.Copy(zis, bos);
                Assert.AreEqual(ze.Size, bos.Length);
            }

            zis.Close();
            fs.Close();
        }
Exemplo n.º 3
0
        /// <exception cref="System.IO.IOException"></exception>
        private void AddDocWithId(string docId, string attachmentName, bool gzipped)
        {
            string docJson;

            if (attachmentName != null)
            {
                // add attachment to document
                InputStream           attachmentStream = GetAsset(attachmentName);
                ByteArrayOutputStream baos             = new ByteArrayOutputStream();
                IOUtils.Copy(attachmentStream, baos);
                if (gzipped == false)
                {
                    string attachmentBase64 = Base64.EncodeBytes(baos.ToByteArray());
                    docJson = string.Format("{\"foo\":1,\"bar\":false, \"_attachments\": { \"%s\": { \"content_type\": \"image/png\", \"data\": \"%s\" } } }"
                                            , attachmentName, attachmentBase64);
                }
                else
                {
                    byte[] bytes            = baos.ToByteArray();
                    string attachmentBase64 = Base64.EncodeBytes(bytes, Base64.Gzip);
                    docJson = string.Format("{\"foo\":1,\"bar\":false, \"_attachments\": { \"%s\": { \"content_type\": \"image/png\", \"data\": \"%s\", \"encoding\": \"gzip\", \"length\":%d } } }"
                                            , attachmentName, attachmentBase64, bytes.Length);
                }
            }
            else
            {
                docJson = "{\"foo\":1,\"bar\":false}";
            }
            PushDocumentToSyncGateway(docId, docJson);
            WorkaroundSyncGatewayRaceCondition();
        }
Exemplo n.º 4
0
        public void TestMain()
        {
            FileInfo file = TempFile.CreateTempFile("HexDump", ".dat");

            try
            {
                FileStream out1 = new FileStream(file.FullName, FileMode.Create, FileAccess.ReadWrite);
                try
                {
                    IOUtils.Copy(new MemoryStream(Encoding.UTF8.GetBytes("teststring")), out1);
                }
                finally
                {
                    out1.Close();
                }
                Assert.IsTrue(file.Exists);
                Assert.IsTrue(file.Length > 0);

                //HexDump.Main(new String[] { file.AbsolutePath });
            }
            finally
            {
                file.Delete();
                //Assert.IsTrue(file.Exists);
            }
        }
Exemplo n.º 5
0
            public void ProcessPOIFSWriterEvent(POIFSWriterEvent event1)
            {
                try
                {
                    LittleEndianOutputStream leos = new LittleEndianOutputStream(event1.Stream);

                    // StreamSize (8 bytes): An unsigned integer that specifies the number of bytes used by data
                    // encrypted within the EncryptedData field, not including the size of the StreamSize field.
                    // Note that the actual size of the \EncryptedPackage stream (1) can be larger than this
                    // value, depending on the block size of the chosen encryption algorithm
                    leos.WriteLong(countBytes);
                    long rawPos = rawStream.Position;
                    //FileStream fis = new FileStream(fileOut.FullName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
                    IOUtils.Copy(rawStream, leos.out1);
                    //fis.Close();
                    rawStream.Position = rawPos;
                    //File.Delete(fileOut.FullName + ".copy");
                    fileOut.Delete();


                    leos.Close();
                }
                catch (IOException e)
                {
                    throw new EncryptedDocumentException(e);
                }
            }
Exemplo n.º 6
0
        private void CheckVolumeExists()
        {
            if (checkValid)
            {
                return;
            }
            if (User == null || (Path == VolumePath.Home && Image == null))
            {
                return;
            }
            switch (Path)
            {
            case VolumePath.Data:
                IOUtils.AddDirectoryIfNotExists(System.IO.Path.Combine(Consts.PublicRootPath, User.Id, "data"));
                break;

            case VolumePath.Home:
                var dir = IOUtils.AddDirectoryIfNotExists(System.IO.Path.Combine(Consts.PublicRootPath, User.Id, Image.Tag));
                IOUtils.Copy(new System.IO.DirectoryInfo("/etc/skel"), dir, false);
                break;

            case VolumePath.Public:
                // no necessary to check
                break;

            default:
                return;
            }
            checkValid = true;
        }
Exemplo n.º 7
0
        public void InPlaceReWrite()
        {
            FileInfo f = TempFile.CreateTempFile("protected_agile", ".docx");
            // File f = new File("protected_agile.docx");
            FileStream fos = f.Create();
            Stream     fis = POIDataSamples.GetPOIFSInstance().OpenResourceAsStream("protected_agile.docx");

            IOUtils.Copy(fis, fos);
            fis.Close();
            fos.Close();

            NPOIFSFileSystem fs = new NPOIFSFileSystem(f, false);

            // decrypt the protected file - in this case it was encrypted with the default password
            EncryptionInfo encInfo = new EncryptionInfo(fs);
            Decryptor      d       = encInfo.Decryptor;
            bool           b       = d.VerifyPassword(Decryptor.DEFAULT_PASSWORD);

            Assert.IsTrue(b);

            // do some strange things with it ;)
            XWPFDocument docx = new XWPFDocument(d.GetDataStream(fs));

            docx.GetParagraphArray(0).InsertNewRun(0).SetText("POI was here! All your base are belong to us!");
            docx.GetParagraphArray(0).InsertNewRun(1).AddBreak();

            // and encrypt it again
            Encryptor e = encInfo.Encryptor;

            e.ConfirmPassword("AYBABTU");
            docx.Write(e.GetDataStream(fs));

            fs.Close();
        }
Exemplo n.º 8
0
        /// <exception cref="System.IO.IOException"/>
        private string InputStreamToString(InputStream stream)
        {
            StringWriter writer = new StringWriter();

            IOUtils.Copy(stream, writer);
            return(writer.ToString());
        }
Exemplo n.º 9
0
            /// <exception cref="Javax.Servlet.ServletException"/>
            /// <exception cref="System.IO.IOException"/>
            protected override void DoPost(HttpServletRequest req, HttpServletResponse resp)
            {
                TextWriter writer = resp.GetWriter();

                writer.Write("ping: ");
                IOUtils.Copy(req.GetReader(), writer);
                resp.SetStatus(HttpServletResponse.ScOk);
            }
Exemplo n.º 10
0
            public void Read(Stream in1)
            {
                MemoryStream out1 = new MemoryStream();

                IOUtils.Copy(in1, out1);
                out1.Close();
                buf = out1.ToArray();
            }
Exemplo n.º 11
0
        /**
         * Decrypt the Document-/SummaryInformation and other optionally streams.
         * Opposed to other crypto modes, cryptoapi is record based and can't be used
         * to stream-decrypt a whole file
         *
         * @see <a href="http://msdn.microsoft.com/en-us/library/dd943321(v=office.12).aspx">2.3.5.4 RC4 CryptoAPI Encrypted Summary Stream</a>
         */

        public override InputStream GetDataStream(DirectoryNode dir)
        {
            NPOIFSFileSystem    fsOut = new NPOIFSFileSystem();
            DocumentNode        es    = (DocumentNode)dir.GetEntry("EncryptedSummary");
            DocumentInputStream dis   = dir.CreateDocumentInputStream(es);
            MemoryStream        bos   = new MemoryStream();

            IOUtils.Copy(dis, bos);
            dis.Close();
            SeekableMemoryStream    sbis    = new SeekableMemoryStream(bos.ToArray());
            LittleEndianInputStream leis    = new LittleEndianInputStream(sbis);
            int streamDescriptorArrayOffset = (int)leis.ReadUInt();
            int streamDescriptorArraySize   = (int)leis.ReadUInt();

            sbis.Seek(streamDescriptorArrayOffset - 8, SeekOrigin.Current);// sbis.Skip(streamDescriptorArrayOffset - 8);

            sbis.SetBlock(0);
            int encryptedStreamDescriptorCount = (int)leis.ReadUInt();

            StreamDescriptorEntry[] entries = new StreamDescriptorEntry[encryptedStreamDescriptorCount];
            for (int i = 0; i < encryptedStreamDescriptorCount; i++)
            {
                StreamDescriptorEntry entry = new StreamDescriptorEntry();
                entries[i]         = entry;
                entry.streamOffset = (int)leis.ReadUInt();
                entry.streamSize   = (int)leis.ReadUInt();
                entry.block        = leis.ReadUShort();
                int nameSize = leis.ReadUByte();
                entry.flags = leis.ReadUByte();
                bool IsStream = StreamDescriptorEntry.flagStream.IsSet(entry.flags);
                entry.reserved2  = leis.ReadInt();
                entry.streamName = StringUtil.ReadUnicodeLE(leis, nameSize);
                leis.ReadShort();
                Debug.Assert(entry.streamName.Length == nameSize);
            }

            foreach (StreamDescriptorEntry entry in entries)
            {
                sbis.Seek(entry.streamOffset);
                sbis.SetBlock(entry.block);
                Stream is1 = new BufferedStream(sbis, entry.streamSize);
                fsOut.CreateDocument(is1, entry.streamName);
            }

            leis.Close();
            sbis = null;

            bos.Seek(0, SeekOrigin.Begin); //bos.Reset();
            fsOut.WriteFileSystem(bos);
            fsOut.Close();
            _length = bos.Length;
            ByteArrayInputStream bis = new ByteArrayInputStream(bos.ToArray());

            throw new NotImplementedException("ByteArrayInputStream should be derived from InputStream");
        }
Exemplo n.º 12
0
        public int AddPicture(Stream picStream, int format)
        {
            int             idx          = this.GetAllPictures().Count + 1;
            XSSFPictureData relationship = (XSSFPictureData)this.CreateRelationship(XSSFPictureData.RELATIONS[format], (POIXMLFactory)XSSFFactory.GetInstance(), idx, true);
            Stream          outputStream = relationship.GetPackagePart().GetOutputStream();

            IOUtils.Copy(picStream, outputStream);
            outputStream.Close();
            this.pictures.Add(relationship);
            return(idx - 1);
        }
Exemplo n.º 13
0
        public static void Init()
        {
            if (Config.GetInt("pythonRuntime") != 1)
            {
                return;
            }
            string shippedPath = ShippedFiles.path;

            IOUtils.Copy(Path.Combine(shippedPath, "utils"), Path.Combine(shippedPath, "py", "utils"));
            File.Copy(Path.Combine(shippedPath, "esrlupscale.py"), Path.Combine(shippedPath, "py", "esrlupscale.py"), true);
            File.Copy(Path.Combine(shippedPath, "esrlmodel.py"), Path.Combine(shippedPath, "py", "esrlmodel.py"), true);
            File.Copy(Path.Combine(shippedPath, "esrlrrdbnet.py"), Path.Combine(shippedPath, "py", "esrlrrdbnet.py"), true);
        }
Exemplo n.º 14
0
        /// <exception cref="System.IO.IOException"></exception>
        /// <exception cref="Couchbase.Lite.CouchbaseLiteException"></exception>
        private void AddDocWithId(string docId, string attachmentName, bool gzipped)
        {
            string docJson;
            IDictionary <string, object> documentProperties = new Dictionary <string, object>();

            if (attachmentName == null)
            {
                documentProperties.Put("foo", 1);
                documentProperties.Put("bar", false);
                Document doc = database.GetDocument(docId);
                doc.PutProperties(documentProperties);
            }
            else
            {
                // add attachment to document
                InputStream           attachmentStream = GetAsset(attachmentName);
                ByteArrayOutputStream baos             = new ByteArrayOutputStream();
                IOUtils.Copy(attachmentStream, baos);
                if (gzipped == false)
                {
                    string attachmentBase64 = Base64.EncodeBytes(baos.ToByteArray());
                    documentProperties.Put("foo", 1);
                    documentProperties.Put("bar", false);
                    IDictionary <string, object> attachment = new Dictionary <string, object>();
                    attachment.Put("content_type", "image/png");
                    attachment.Put("data", attachmentBase64);
                    IDictionary <string, object> attachments = new Dictionary <string, object>();
                    attachments.Put(attachmentName, attachment);
                    documentProperties.Put("_attachments", attachments);
                    Document doc = database.GetDocument(docId);
                    doc.PutProperties(documentProperties);
                }
                else
                {
                    byte[] bytes            = baos.ToByteArray();
                    string attachmentBase64 = Base64.EncodeBytes(bytes, Base64.Gzip);
                    documentProperties.Put("foo", 1);
                    documentProperties.Put("bar", false);
                    IDictionary <string, object> attachment = new Dictionary <string, object>();
                    attachment.Put("content_type", "image/png");
                    attachment.Put("data", attachmentBase64);
                    attachment.Put("encoding", "gzip");
                    attachment.Put("length", bytes.Length);
                    IDictionary <string, object> attachments = new Dictionary <string, object>();
                    attachments.Put(attachmentName, attachment);
                    documentProperties.Put("_attachments", attachments);
                    Document doc = database.GetDocument(docId);
                    doc.PutProperties(documentProperties);
                }
            }
        }
Exemplo n.º 15
0
        public static void Init()
        {
            if (!IsEnabled())
            {
                return;
            }
            string shippedPath = Installer.path;

            IOUtils.TryDeleteIfExists(Path.Combine(shippedPath, "py", "utils"));
            IOUtils.Copy(Path.Combine(shippedPath, "utils"), Path.Combine(shippedPath, "py", "utils"));
            File.Copy(Path.Combine(shippedPath, "esrlupscale.py"), Path.Combine(shippedPath, "py", "esrlupscale.py"), true);
            File.Copy(Path.Combine(shippedPath, "esrlmodel.py"), Path.Combine(shippedPath, "py", "esrlmodel.py"), true);
            File.Copy(Path.Combine(shippedPath, "esrlrrdbnet.py"), Path.Combine(shippedPath, "py", "esrlrrdbnet.py"), true);
        }
Exemplo n.º 16
0
        private void ListEntry(DocumentNode de, string ext, string path)
        {
            path += "\\" + de.Name.Replace("[\\p{Cntrl}]", "_");
            Console.WriteLine(ext + ": " + path + " (" + de.Size + " bytes)");

            string name = de.Name.Replace("[\\p{Cntrl}]", "_");

            Stream     is1 = ((DirectoryNode)de.Parent).CreateDocumentInputStream(de);
            FileStream fos = new FileStream("solr." + name + "." + ext, FileMode.OpenOrCreate, FileAccess.ReadWrite);

            IOUtils.Copy(is1, fos);
            fos.Close();
            is1.Close();
        }
Exemplo n.º 17
0
        /// <exception cref="System.IO.IOException"></exception>
        public virtual void TestDocWithAttachment()
        {
            string inlineTextString = "Inline text string created by cblite functional test";

            Send("PUT", "/db", Status.Created, null);
            IDictionary <string, object> attachment = new Dictionary <string, object>();

            attachment["content_type"] = "text/plain";
            attachment["data"]         = "SW5saW5lIHRleHQgc3RyaW5nIGNyZWF0ZWQgYnkgY2JsaXRlIGZ1bmN0aW9uYWwgdGVzdA=="
            ;
            IDictionary <string, object> attachments = new Dictionary <string, object>();

            attachments["inline.txt"] = attachment;
            IDictionary <string, object> docWithAttachment = new Dictionary <string, object>();

            docWithAttachment["_id"]          = "docWithAttachment";
            docWithAttachment["text"]         = inlineTextString;
            docWithAttachment["_attachments"] = attachments;
            IDictionary <string, object> result = (IDictionary <string, object>)SendBody("PUT",
                                                                                         "/db/docWithAttachment", docWithAttachment, Status.Created, null);

            result = (IDictionary <string, object>)Send("GET", "/db/docWithAttachment", Status
                                                        .Ok, null);
            IDictionary <string, object> attachmentsResult = (IDictionary <string, object>)result
                                                             ["_attachments"];
            IDictionary <string, object> attachmentResult = (IDictionary <string, object>)attachmentsResult
                                                            .Get("inline.txt");
            // there should be either a content_type or content-type field.
            //https://github.com/couchbase/couchbase-lite-android-core/issues/12
            //content_type becomes null for attachments in responses, should be as set in Content-Type
            string contentTypeField = (string)attachmentResult["content_type"];

            NUnit.Framework.Assert.IsTrue(attachmentResult.ContainsKey("content_type"));
            NUnit.Framework.Assert.IsNotNull(contentTypeField);
            URLConnection conn = SendRequest("GET", "/db/docWithAttachment/inline.txt", null,
                                             null);
            string contentType = conn.GetHeaderField("Content-Type");

            NUnit.Framework.Assert.IsNotNull(contentType);
            NUnit.Framework.Assert.IsTrue(contentType.Contains("text/plain"));
            StringWriter writer = new StringWriter();

            IOUtils.Copy(conn.GetInputStream(), writer, "UTF-8");
            string responseString = writer.ToString();

            NUnit.Framework.Assert.IsTrue(responseString.Contains(inlineTextString));
        }
Exemplo n.º 18
0
        public static async Task CopyImagesTo(string path)
        {
            Program.lastOutputDir = path;
            Program.mainForm.AfterFirstUpscale();
            if (overwriteMode == Overwrite.Yes)
            {
                Logger.Log("Overwrite mode - removing suffix from filenames");
                IOUtils.ReplaceInFilenamesDir(Paths.imgOutPath, "-" + GetLastModelName(), "");
            }
            else
            {
                Logger.Log("Overwrite is off - keeping suffix.");
            }
            IOUtils.Copy(Paths.imgOutPath, path);
            await Task.Delay(1);

            IOUtils.ClearDir(Paths.imgInPath);
            IOUtils.ClearDir(Paths.imgOutPath);
        }
Exemplo n.º 19
0
        public void TestNoCoreProperties_saveInPlace()
        {
            String sampleFileName = "OPCCompliance_NoCoreProperties.xlsx";

            // Copy this into a temp file, so we can play with it
            FileInfo   tmp  = TempFile.CreateTempFile("poi-test", ".opc");
            FileStream out1 = new FileStream(tmp.FullName, FileMode.Create, FileAccess.ReadWrite);
            Stream     in1  = POIDataSamples.GetOpenXML4JInstance().OpenResourceAsStream(sampleFileName);

            IOUtils.Copy(
                in1,
                out1);
            out1.Close();
            in1.Close();

            // Open it from that temp file
            OPCPackage pkg = OPCPackage.Open(tmp);

            // Empty properties
            Assert.AreEqual(0, pkg.GetPartsByContentType(ContentTypes.CORE_PROPERTIES_PART).Count);
            Assert.IsNotNull(pkg.GetPackageProperties());
            Assert.IsNull(pkg.GetPackageProperties().GetLanguageProperty());
            //Assert.IsNull(pkg.GetPackageProperties().GetLanguageProperty().Value);

            // Save and close
            pkg.Close();


            // Re-open and check
            pkg = OPCPackage.Open(tmp);

            // An Empty Properties part has been Added in the save/load
            Assert.AreEqual(1, pkg.GetPartsByContentType(ContentTypes.CORE_PROPERTIES_PART).Count);
            Assert.IsNotNull(pkg.GetPackageProperties());
            Assert.IsNull(pkg.GetPackageProperties().GetLanguageProperty());
            //Assert.IsNull(pkg.GetPackageProperties().GetLanguageProperty().Value);

            // Finish and tidy
            pkg.Revert();
            tmp.Delete();

            Assert.AreEqual(0, Directory.GetFiles(AppDomain.CurrentDomain.BaseDirectory, "*.tmp").Length, "At Last: There are no temporary files.");
        }
Exemplo n.º 20
0
        public void TestCertificateEncryption1()
        {
            POIFSFileSystem         fs   = new POIFSFileSystem();
            EncryptionInfo          info = new EncryptionInfo(EncryptionMode.Agile, CipherAlgorithm.aes128, HashAlgorithm.sha1, -1, -1, ChainingMode.cbc);
            AgileEncryptionVerifier aev  = (AgileEncryptionVerifier)info.Verifier;
            CertData certData            = loadKeystore();

            aev.AddCertificate(certData.x509);

            Encryptor enc = info.Encryptor;

            enc.ConfirmPassword("foobaa");

            FileStream file = POIDataSamples.GetDocumentInstance().GetFile("VariousPictures.docx");

            //InputStream fis = new FileInputStream(file);
            byte[] byteExpected = IOUtils.ToByteArray(file);
            //fis.Close();

            Stream os = enc.GetDataStream(fs);

            IOUtils.Copy(new MemoryStream(byteExpected), os);
            os.Close();

            MemoryStream bos = new MemoryStream();

            fs.WriteFileSystem(bos);
            bos.Close();

            fs   = new POIFSFileSystem(new MemoryStream(bos.ToArray()));
            info = new EncryptionInfo(fs);
            AgileDecryptor agDec  = (AgileDecryptor)info.Decryptor;
            bool           passed = agDec.VerifyPassword(certData.keypair, certData.x509);

            Assert.IsTrue(passed, "certificate verification failed");

            Stream fis = agDec.GetDataStream(fs);

            byte[] byteActual = IOUtils.ToByteArray(fis);
            fis.Close();

            Assert.That(byteExpected, Is.EqualTo(byteActual));
        }
Exemplo n.º 21
0
        public void BinaryRC4Encryption()
        {
            // please contribute a real sample file, which is binary rc4 encrypted
            // ... at least the output can be opened in Excel Viewer
            String password = "******";

            Stream       is1             = POIDataSamples.GetSpreadSheetInstance().OpenResourceAsStream("SimpleMultiCell.xlsx");
            MemoryStream payloadExpected = new MemoryStream();

            IOUtils.Copy(is1, payloadExpected);
            is1.Close();

            POIFSFileSystem fs  = new POIFSFileSystem();
            EncryptionInfo  ei  = new EncryptionInfo(EncryptionMode.BinaryRC4);
            Encryptor       enc = ei.Encryptor;

            enc.ConfirmPassword(password);

            Stream os = enc.GetDataStream(fs.Root);

            payloadExpected.WriteTo(os);
            os.Close();

            MemoryStream bos = new MemoryStream();

            fs.WriteFileSystem(bos);

            fs = new POIFSFileSystem(new MemoryStream(bos.ToArray()));
            ei = new EncryptionInfo(fs);
            Decryptor dec = ei.Decryptor;
            bool      b   = dec.VerifyPassword(password);

            Assert.IsTrue(b);

            MemoryStream payloadActual = new MemoryStream();

            is1 = dec.GetDataStream(fs.Root);
            IOUtils.Copy(is1, payloadActual);
            is1.Close();

            Assert.IsTrue(Arrays.Equals(payloadExpected.ToArray(), payloadActual.ToArray()));
            //assertArrayEquals(payloadExpected.ToArray(), payloadActual.ToArray());
        }
Exemplo n.º 22
0
        private void WriteDataToFile(FileInfo temp)
        {
            FileStream str = temp.Create();

            try
            {
                Stream in1 = data.OpenResourceAsStream("Notes.ole2");
                try
                {
                    IOUtils.Copy(in1, str);
                }
                finally
                {
                    in1.Close();
                }
            }
            finally
            {
                str.Close();
            }
        }
        /// <exception cref="System.IO.IOException"></exception>
        private void AddDocWithId(string docId, string attachmentName)
        {
            string docJson;

            if (attachmentName != null)
            {
                // add attachment to document
                InputStream           attachmentStream = GetAsset(attachmentName);
                ByteArrayOutputStream baos             = new ByteArrayOutputStream();
                IOUtils.Copy(attachmentStream, baos);
                string attachmentBase64 = Base64.EncodeBytes(baos.ToByteArray());
                docJson = string.Format("{\"foo\":1,\"bar\":false, \"_attachments\": { \"i_use_couchdb.png\": { \"content_type\": \"image/png\", \"data\": \"%s\" } } }"
                                        , attachmentBase64);
            }
            else
            {
                docJson = "{\"foo\":1,\"bar\":false}";
            }
            // push a document to server
            Uri replicationUrlTrailingDoc1 = new Uri(string.Format("%s/%s", GetReplicationURL
                                                                       ().ToExternalForm(), docId));
            Uri pathToDoc1 = new Uri(replicationUrlTrailingDoc1, docId);

            Log.D(Tag, "Send http request to " + pathToDoc1);
            CountDownLatch httpRequestDoneSignal = new CountDownLatch(1);
            BackgroundTask getDocTask            = new _BackgroundTask_376(pathToDoc1, docJson, httpRequestDoneSignal
                                                                           );

            getDocTask.Execute();
            Log.D(Tag, "Waiting for http request to finish");
            try
            {
                httpRequestDoneSignal.Await(300, TimeUnit.Seconds);
                Log.D(Tag, "http request finished");
            }
            catch (Exception e)
            {
                Sharpen.Runtime.PrintStackTrace(e);
            }
        }
Exemplo n.º 24
0
        /**
         * Constructor for a new Document
         *
         * @param name the name of the POIFSDocument
         * @param stream the InputStream we read data from
         */
        public NPOIFSDocument(String name, NPOIFSFileSystem filesystem, Stream stream)
        {
            this._filesystem = filesystem;

            // Buffer the contents into memory. This is a bit icky...
            // TODO Replace with a buffer up to the mini stream size, then streaming write
            byte[] contents;
            if (stream is MemoryStream)
            {
                MemoryStream bais = (MemoryStream)stream;
                contents = new byte[bais.Length];
                bais.Read(contents, 0, contents.Length);
            }
            else
            {
                MemoryStream baos = new MemoryStream();
                IOUtils.Copy(stream, baos);
                contents = baos.ToArray();
            }

            // Do we need to store as a mini stream or a full one?
            if (contents.Length <= POIFSConstants.BIG_BLOCK_MINIMUM_DOCUMENT_SIZE)
            {
                _stream     = new NPOIFSStream(filesystem.GetMiniStore());
                _block_size = _filesystem.GetMiniStore().GetBlockStoreBlockSize();
            }
            else
            {
                _stream     = new NPOIFSStream(filesystem);
                _block_size = _filesystem.GetBlockStoreBlockSize();
            }

            // Store it
            _stream.UpdateContents(contents);

            // And build the property for it
            this._property       = new DocumentProperty(name, contents.Length);
            _property.StartBlock = _stream.GetStartBlock();
        }
Exemplo n.º 25
0
        public void TestFiles()
        {
            FileStream[] files =
            {
                // bug 51891
                POIDataSamples.GetPOIFSInstance().GetFile("multimedia.doc"),
                // tika bug 1072
                POIDataSamples.GetPOIFSInstance().GetFile("20-Force-on-a-current-S00.doc"),
                // other files Containing ole10native records ...
                POIDataSamples.GetDocumentInstance().GetFile("Bug53380_3.doc"),
                POIDataSamples.GetDocumentInstance().GetFile("Bug47731.doc")
            };

            foreach (FileStream f in files)
            {
                NPOIFSFileSystem fs      = new NPOIFSFileSystem(f, true);
                List <Entry>     entries = new List <Entry>();
                FindOle10(entries, fs.Root, "/", "");

                foreach (Entry e in entries)
                {
                    MemoryStream bosExp = new MemoryStream();
                    Stream       is1    = ((DirectoryNode)e.Parent).CreateDocumentInputStream(e);
                    IOUtils.Copy(is1, bosExp);
                    is1.Close();

                    Ole10Native ole = Ole10Native.CreateFromEmbeddedOleObject((DirectoryNode)e.Parent);

                    MemoryStream bosAct = new MemoryStream();
                    ole.WriteOut(bosAct);

                    //assertThat(bosExp.ToByteArray(), EqualTo(bosAct.ToByteArray()));
                    Assert.IsTrue(Arrays.Equals(bosExp.ToArray(), bosAct.ToArray()));
                }

                fs.Close();
            }
        }
Exemplo n.º 26
0
        private static void CheckRLEDecompression(String expected, byte[] RunLengthEncodedData)
        {
            Stream       compressedStream = new MemoryStream(RunLengthEncodedData);
            MemoryStream out1             = new MemoryStream();

            try
            {
                InputStream stream = new RLEDecompressingInputStream(compressedStream);
                try
                {
                    IOUtils.Copy(stream, out1);
                }
                finally
                {
                    out1.Close();
                    stream.Close();
                }
            }
            catch (IOException e)
            {
                //throw new Exception(e);
                throw e;
            }
            String expanded;

            try
            {
                //expanded = out1.ToString(StringUtil.UTF8.Name());
                expanded = Encoding.UTF8.GetString(out1.ToArray());
            }
            catch (EncoderFallbackException e)
            {
                //throw new Exception(e);
                throw e;
            }
            Assert.AreEqual(expected, expanded);
        }
Exemplo n.º 27
0
        /**
         * Creates an instance of this class from an embedded OLE Object. The OLE Object is expected
         * to include a stream &quot;{01}Ole10Native&quot; which Contains the actual
         * data relevant for this class.
         *
         * @param poifs POI Filesystem object
         * @return Returns an instance of this class
         * @throws IOException on IO error
         * @throws Ole10NativeException on invalid or unexcepted data format
         */
        public static Ole10Native CreateFromEmbeddedOleObject(POIFSFileSystem poifs)
        {
            bool plain = false;

            try
            {
                poifs.Root.GetEntry("\x0001Ole10ItemName");
                plain = true;
            }
            catch (FileNotFoundException)
            {
                plain = false;
            }

            DocumentInputStream dis = poifs.CreateDocumentInputStream(OLE10_NATIVE);

            using (MemoryStream bos = new MemoryStream())
            {
                IOUtils.Copy(dis, bos);
                byte[] data = bos.ToArray();

                return(new Ole10Native(data, 0, plain));
            }
        }
Exemplo n.º 28
0
        public void AgileEncryption()
        {
            int maxKeyLen = Cipher.GetMaxAllowedKeyLength("AES");

            Assume.That(maxKeyLen == 2147483647, "Please install JCE Unlimited Strength Jurisdiction Policy files for AES 256");

            FileStream       file = POIDataSamples.GetDocumentInstance().GetFile("bug53475-password-is-pass.docx");
            String           pass = "******";
            NPOIFSFileSystem nfs  = new NPOIFSFileSystem(file);

            // Check the encryption details
            EncryptionInfo infoExpected = new EncryptionInfo(nfs);
            Decryptor      decExpected  = Decryptor.GetInstance(infoExpected);
            bool           passed       = decExpected.VerifyPassword(pass);

            Assert.IsTrue(passed, "Unable to Process: document is encrypted");

            // extract the payload
            Stream is1 = decExpected.GetDataStream(nfs);

            byte[] payloadExpected = IOUtils.ToByteArray(is1);
            is1.Close();

            long decPackLenExpected = decExpected.GetLength();

            Assert.AreEqual(decPackLenExpected, payloadExpected.Length);

            is1 = nfs.Root.CreateDocumentInputStream(Decryptor.DEFAULT_POIFS_ENTRY);
            ///is1 = new BoundedInputStream(is1, is1.Available() - 16); // ignore pAdding block
            ///throw new NotImplementedException(BoundedInputStream);
            byte[] encPackExpected = IOUtils.ToByteArray(is1);
            is1.Close();

            // listDir(nfs.Root, "orig", "");

            nfs.Close();

            // check that same verifier/salt lead to same hashes
            byte[] verifierSaltExpected = infoExpected.Verifier.Salt;
            byte[] verifierExpected     = decExpected.GetVerifier();
            byte[] keySalt       = infoExpected.Header.KeySalt;
            byte[] keySpec       = decExpected.GetSecretKey().GetEncoded();
            byte[] integritySalt = decExpected.GetIntegrityHmacKey();
            // the hmacs of the file always differ, as we use PKCS5-pAdding to pad the bytes
            // whereas office just uses random bytes
            // byte integrityHash[] = d.IntegrityHmacValue;

            POIFSFileSystem fs         = new POIFSFileSystem();
            EncryptionInfo  infoActual = new EncryptionInfo(
                EncryptionMode.Agile
                , infoExpected.Verifier.CipherAlgorithm
                , infoExpected.Verifier.HashAlgorithm
                , infoExpected.Header.KeySize
                , infoExpected.Header.BlockSize
                , infoExpected.Verifier.ChainingMode
                );

            Encryptor e = Encryptor.GetInstance(infoActual);

            e.ConfirmPassword(pass, keySpec, keySalt, verifierExpected, verifierSaltExpected, integritySalt);

            Stream os = e.GetDataStream(fs);

            IOUtils.Copy(new MemoryStream(payloadExpected), os);
            os.Close();

            MemoryStream bos = new MemoryStream();

            fs.WriteFileSystem(bos);

            nfs        = new NPOIFSFileSystem(new MemoryStream(bos.ToArray()));
            infoActual = new EncryptionInfo(nfs.Root);
            Decryptor decActual = Decryptor.GetInstance(infoActual);

            passed = decActual.VerifyPassword(pass);
            Assert.IsTrue(passed, "Unable to Process: document is encrypted");

            // extract the payload
            is1 = decActual.GetDataStream(nfs);
            byte[] payloadActual = IOUtils.ToByteArray(is1);
            is1.Close();

            long decPackLenActual = decActual.GetLength();

            is1 = nfs.Root.CreateDocumentInputStream(Decryptor.DEFAULT_POIFS_ENTRY);
            ///is1 = new BoundedInputStream(is1, is1.Available() - 16); // ignore pAdding block
            ///throw new NotImplementedException(BoundedInputStream);
            byte[] encPackActual = IOUtils.ToByteArray(is1);
            is1.Close();

            // listDir(nfs.Root, "copy", "");

            nfs.Close();

            AgileEncryptionHeader aehExpected = (AgileEncryptionHeader)infoExpected.Header;
            AgileEncryptionHeader aehActual   = (AgileEncryptionHeader)infoActual.Header;

            CollectionAssert.AreEqual(aehExpected.GetEncryptedHmacKey(), aehActual.GetEncryptedHmacKey());
            Assert.AreEqual(decPackLenExpected, decPackLenActual);
            CollectionAssert.AreEqual(payloadExpected, payloadActual);
            CollectionAssert.AreEqual(encPackExpected, encPackActual);
        }
Exemplo n.º 29
0
        public void ZipBombCreateAndHandle()
        {
            // #50090 / #56865
            ZipFile zipFile = ZipHelper.OpenZipFile(OpenXml4NetTestDataSamples.GetSampleFile("sample.xlsx"));

            Assert.IsNotNull(zipFile);

            ByteArrayOutputStream bos    = new ByteArrayOutputStream(2500000);
            ZipOutputStream       append = new ZipOutputStream(bos);
            // first, copy contents from existing war
            IEnumerator entries = zipFile.GetEnumerator();

            while (entries.MoveNext())
            {
                ZipEntry e2 = (ZipEntry)entries.Current;
                ZipEntry e  = new ZipEntry(e2.Name);

                e.DateTime = (e2.DateTime);
                e.Comment  = (e2.Comment);
                e.Size     = (e2.Size);

                append.PutNextEntry(e);
                if (!e.IsDirectory)
                {
                    Stream is1 = zipFile.GetInputStream(e);
                    if (e.Name.Equals("[Content_Types].xml"))
                    {
                        ByteArrayOutputStream bos2 = new ByteArrayOutputStream();
                        IOUtils.Copy(is1, bos2);
                        long size = bos2.Length - "</Types>".Length;
                        append.Write(bos2.ToByteArray(), 0, (int)size);
                        byte[] spam = new byte[0x7FFF];
                        for (int i = 0; i < spam.Length; i++)
                        {
                            spam[i] = (byte)' ';
                        }
                        // 0x7FFF0000 is the maximum for 32-bit zips, but less still works
                        while (size < 0x7FFF0000)
                        {
                            append.Write(spam, 0, spam.Length);
                            size += spam.Length;
                        }
                        append.Write(Encoding.ASCII.GetBytes("</Types>"), 0, 8);
                        size  += 8;
                        e.Size = (size);
                    }
                    else
                    {
                        IOUtils.Copy(is1, append);
                    }
                }
                append.CloseEntry();
            }

            append.Close();
            zipFile.Close();

            byte[] buf = bos.ToByteArray();
            bos = null;

            IWorkbook wb = WorkbookFactory.Create(new ByteArrayInputStream(buf));

            wb.GetSheetAt(0);
            wb.Close();
            zipFile.Close();
        }
Exemplo n.º 30
0
        public void TestInPlaceNPOIFSWrite()
        {
            NPOIFSFileSystem           fs      = null;
            DirectoryEntry             root    = null;
            DocumentNode               sinfDoc = null;
            DocumentNode               dinfDoc = null;
            SummaryInformation         sinf    = null;
            DocumentSummaryInformation dinf    = null;

            // We need to work on a File for in-place changes, so create a temp one
            FileInfo copy = TempFile.CreateTempFile("Test-HPSF", "ole2");
            //copy.DeleteOnExit();

            // Copy a test file over to a temp location
            Stream     inp  = _samples.OpenResourceAsStream("TestShiftJIS.doc");
            FileStream out1 = new FileStream(copy.FullName, FileMode.Create);

            IOUtils.Copy(inp, out1);
            inp.Close();
            out1.Close();

            // Open the copy in Read/write mode
            fs = new NPOIFSFileSystem(new FileStream(copy.FullName, FileMode.Open, FileAccess.ReadWrite),
                                      null, false, true);
            root = fs.Root;

            // Read the properties in there
            sinfDoc = (DocumentNode)root.GetEntry(SummaryInformation.DEFAULT_STREAM_NAME);
            dinfDoc = (DocumentNode)root.GetEntry(DocumentSummaryInformation.DEFAULT_STREAM_NAME);

            sinf = (SummaryInformation)PropertySetFactory.Create(new NDocumentInputStream(sinfDoc));
            Assert.AreEqual(131077, sinf.OSVersion);

            dinf = (DocumentSummaryInformation)PropertySetFactory.Create(new NDocumentInputStream(dinfDoc));
            Assert.AreEqual(131077, dinf.OSVersion);

            // Check they start as we expect
            Assert.AreEqual("Reiichiro Hori", sinf.Author);
            Assert.AreEqual("Microsoft Word 9.0", sinf.ApplicationName);
            Assert.AreEqual("\u7b2c1\u7ae0", sinf.Title);

            Assert.AreEqual("", dinf.Company);
            Assert.AreEqual(null, dinf.Manager);

            // Do an in-place replace via an InputStream
            new NPOIFSDocument(sinfDoc).ReplaceContents(sinf.ToInputStream());
            new NPOIFSDocument(dinfDoc).ReplaceContents(dinf.ToInputStream());


            // Check it didn't Get Changed
            sinfDoc = (DocumentNode)root.GetEntry(SummaryInformation.DEFAULT_STREAM_NAME);
            dinfDoc = (DocumentNode)root.GetEntry(DocumentSummaryInformation.DEFAULT_STREAM_NAME);

            sinf = (SummaryInformation)PropertySetFactory.Create(new NDocumentInputStream(sinfDoc));
            Assert.AreEqual(131077, sinf.OSVersion);

            dinf = (DocumentSummaryInformation)PropertySetFactory.Create(new NDocumentInputStream(dinfDoc));
            Assert.AreEqual(131077, dinf.OSVersion);


            // Start again!
            fs.Close();
            inp  = _samples.OpenResourceAsStream("TestShiftJIS.doc");
            out1 = new FileStream(copy.FullName, FileMode.Open, FileAccess.ReadWrite);
            IOUtils.Copy(inp, out1);
            inp.Close();
            out1.Close();

            fs = new NPOIFSFileSystem(new FileStream(copy.FullName, FileMode.Open, FileAccess.ReadWrite),
                                      null, false, true);
            root = fs.Root;

            // Read the properties in once more
            sinfDoc = (DocumentNode)root.GetEntry(SummaryInformation.DEFAULT_STREAM_NAME);
            dinfDoc = (DocumentNode)root.GetEntry(DocumentSummaryInformation.DEFAULT_STREAM_NAME);

            sinf = (SummaryInformation)PropertySetFactory.Create(new NDocumentInputStream(sinfDoc));
            Assert.AreEqual(131077, sinf.OSVersion);

            dinf = (DocumentSummaryInformation)PropertySetFactory.Create(new NDocumentInputStream(dinfDoc));
            Assert.AreEqual(131077, dinf.OSVersion);


            // Have them write themselves in-place with no Changes
            sinf.Write(new NDocumentOutputStream(sinfDoc));
            dinf.Write(new NDocumentOutputStream(dinfDoc));

            // And also write to some bytes for Checking
            MemoryStream sinfBytes = new MemoryStream();

            sinf.Write(sinfBytes);
            MemoryStream dinfBytes = new MemoryStream();

            dinf.Write(dinfBytes);


            // Check that the filesystem can give us back the same bytes
            sinfDoc = (DocumentNode)root.GetEntry(SummaryInformation.DEFAULT_STREAM_NAME);
            dinfDoc = (DocumentNode)root.GetEntry(DocumentSummaryInformation.DEFAULT_STREAM_NAME);

            byte[] sinfData = IOUtils.ToByteArray(new NDocumentInputStream(sinfDoc));
            byte[] dinfData = IOUtils.ToByteArray(new NDocumentInputStream(dinfDoc));
            Assert.That(sinfBytes.ToArray(), new EqualConstraint(sinfData));
            Assert.That(dinfBytes.ToArray(), new EqualConstraint(dinfData));


            // Read back in as-is
            sinf = (SummaryInformation)PropertySetFactory.Create(new NDocumentInputStream(sinfDoc));
            Assert.AreEqual(131077, sinf.OSVersion);

            dinf = (DocumentSummaryInformation)PropertySetFactory.Create(new NDocumentInputStream(dinfDoc));
            Assert.AreEqual(131077, dinf.OSVersion);

            Assert.AreEqual("Reiichiro Hori", sinf.Author);
            Assert.AreEqual("Microsoft Word 9.0", sinf.ApplicationName);
            Assert.AreEqual("\u7b2c1\u7ae0", sinf.Title);

            Assert.AreEqual("", dinf.Company);
            Assert.AreEqual(null, dinf.Manager);


            // Now alter a few of them
            sinf.Author  = (/*setter*/ "Changed Author");
            sinf.Title   = (/*setter*/ "Le titre \u00e9tait chang\u00e9");
            dinf.Manager = (/*setter*/ "Changed Manager");


            // Save this into the filesystem
            sinf.Write(new NDocumentOutputStream(sinfDoc));
            dinf.Write(new NDocumentOutputStream(dinfDoc));


            // Read them back in again
            sinfDoc = (DocumentNode)root.GetEntry(SummaryInformation.DEFAULT_STREAM_NAME);
            sinf    = (SummaryInformation)PropertySetFactory.Create(new NDocumentInputStream(sinfDoc));
            Assert.AreEqual(131077, sinf.OSVersion);

            dinfDoc = (DocumentNode)root.GetEntry(DocumentSummaryInformation.DEFAULT_STREAM_NAME);
            dinf    = (DocumentSummaryInformation)PropertySetFactory.Create(new NDocumentInputStream(dinfDoc));
            Assert.AreEqual(131077, dinf.OSVersion);

            Assert.AreEqual("Changed Author", sinf.Author);
            Assert.AreEqual("Microsoft Word 9.0", sinf.ApplicationName);
            Assert.AreEqual("Le titre \u00e9tait chang\u00e9", sinf.Title);

            Assert.AreEqual("", dinf.Company);
            Assert.AreEqual("Changed Manager", dinf.Manager);


            // Close the whole filesystem, and open it once more
            fs.WriteFileSystem();
            fs.Close();

            fs   = new NPOIFSFileSystem(new FileStream(copy.FullName, FileMode.Open));
            root = fs.Root;

            // Re-check on load
            sinfDoc = (DocumentNode)root.GetEntry(SummaryInformation.DEFAULT_STREAM_NAME);
            sinf    = (SummaryInformation)PropertySetFactory.Create(new NDocumentInputStream(sinfDoc));
            Assert.AreEqual(131077, sinf.OSVersion);

            dinfDoc = (DocumentNode)root.GetEntry(DocumentSummaryInformation.DEFAULT_STREAM_NAME);
            dinf    = (DocumentSummaryInformation)PropertySetFactory.Create(new NDocumentInputStream(dinfDoc));
            Assert.AreEqual(131077, dinf.OSVersion);

            Assert.AreEqual("Changed Author", sinf.Author);
            Assert.AreEqual("Microsoft Word 9.0", sinf.ApplicationName);
            Assert.AreEqual("Le titre \u00e9tait chang\u00e9", sinf.Title);

            Assert.AreEqual("", dinf.Company);
            Assert.AreEqual("Changed Manager", dinf.Manager);


            // Tidy up
            fs.Close();
            copy.Delete();
        }