Exemple #1
0
        /// <summary>
        /// Reads a message from a message file.
        /// </summary>
        /// <param name="msgID">The message ID.</param>
        /// <param name="path">The file path.</param>
        /// <returns>A <see cref="QueuedMsg" />.</returns>
        internal QueuedMsg ReadMessage(Guid msgID, string path)
        {
            QueuedMsgInfo msgInfo;

            byte[] body;
            byte[] md5Hash;
            long   savePos;

            using (var fsMsg = new EnhancedFileStream(path, FileMode.Open, FileAccess.ReadWrite))
            {
                try
                {
                    // Read the message file header

                    if (fsMsg.ReadInt32() != MsgMagic)          // Magic Number
                    {
                        throw new Exception();
                    }

                    if (fsMsg.ReadInt32() != 0)                 // Format Version
                    {
                        throw new Exception();
                    }

                    fsMsg.ReadInt32();                          // Reserved

                    // Verify that the MD5 hash saved in then file matches the
                    // hash computed for the remainder of the file as it exists
                    // right now.

                    md5Hash = fsMsg.ReadBytes(MD5Hasher.DigestSize);
                    savePos = fsMsg.Position;

                    if (!Helper.ArrayEquals(md5Hash, MD5Hasher.Compute(fsMsg, fsMsg.Length - fsMsg.Position)))
                    {
                        throw new FormatException(string.Format("Message file [{0}] is corrupt. MD5 digests do not match.", path));
                    }

                    fsMsg.Position = savePos;

                    // Read the message body

                    body = fsMsg.ReadBytes32();

                    // Read the metadata and add the provider specific information

                    msgInfo              = new QueuedMsgInfo(fsMsg.ReadString16());
                    msgInfo.PersistID    = msgInfo.ID;
                    msgInfo.ProviderData = path;

                    if (msgID != msgInfo.ID)
                    {
                        throw new FormatException("Message ID does not match the metadata.");
                    }

                    return(new QueuedMsg(msgInfo, body, false));
                }
                catch
                {
                    throw new FormatException(string.Format("Bad message file [{0}].", path));
                }
            }
        }
Exemple #2
0
        public void Package_AddFile()
        {
            Package              package;
            EnhancedStream       fs;
            EnhancedMemoryStream es = new EnhancedMemoryStream();
            PackageEntry         entry;

            byte[] buf;
            string inputFile;
            string outputFile;

            inputFile  = Path.GetTempFileName();
            outputFile = Path.GetTempFileName();
            try
            {
                buf = new byte[37000];
                for (int i = 0; i < buf.Length; i++)
                {
                    buf[i] = (byte)i;
                }

                fs = new EnhancedFileStream(inputFile, FileMode.OpenOrCreate);
                fs.WriteBytes32(buf);
                fs.Close();

                //-------------------------

                package = new Package();
                package.Create(es);

                entry = package.AddFile("/Foo/Bar/Test1.dat", inputFile);
                Assert.IsTrue(entry.IsFile);
                Assert.IsTrue(package["/Foo"].IsFolder);
                Assert.IsTrue(package["/Foo/Bar"].IsFolder);
                Assert.IsTrue(package["/Foo/Bar/Test1.dat"].IsFile);

                package.Close(true);

                //-------------------------

                package = new Package(es);

                Assert.IsTrue(entry.IsFile);
                Assert.IsTrue(package["/Foo"].IsFolder);
                Assert.IsTrue(package["/Foo/Bar"].IsFolder);
                Assert.IsTrue(package["/Foo/Bar/Test1.dat"].IsFile);

                package["/Foo/Bar/Test1.dat"].GetContents(outputFile);

                fs = new EnhancedFileStream(outputFile, FileMode.Open);
                try
                {
                    CollectionAssert.AreEqual(buf, fs.ReadBytes32());
                }
                finally
                {
                    fs.Close();
                }

                package.Close();
            }
            finally
            {
                File.Delete(inputFile);
                File.Delete(outputFile);
            }
        }