Esempio n. 1
0
        protected byte[] CompressMap()
        {
            int uncompressedSize = DynaBuffer.GetBytesUsed();

            MemoryStream ms = new MemoryStream();

            Ionic.Zlib.ZlibStream ws = new Ionic.Zlib.ZlibStream(ms, Ionic.Zlib.CompressionMode.Compress, Ionic.Zlib.CompressionLevel.BestCompression, true);

            ws.Write(DynaBuffer.GetRawBuffer(), 0, uncompressedSize);
            ws.Flush();

            ws.Close();

            int len = (int)ms.Length;

            byte[] compressedData = ms.GetBuffer();
            // ms.Length();
            Array.Resize(ref compressedData, (int)ms.Length);

            DynamicOutputBuffer header = DynamicOutputBuffer.GetTempBuffer(64);

            header.WriteUInt16(Constants.WorldCodeHeaderSize);
            header.WriteUInt16(Constants.WorldCodeHeader);
            header.WriteUInt16(1);

            header.WriteUInt32(uncompressedSize);
            header.WriteUInt32(compressedData.Length);

            header.WriteBytes(compressedData);
            header.WriteUInt16(Constants.WorldCodeEndSize);
            header.WriteUInt16(Constants.WorldCodeEnd);

            return(header.GetFinalBuffer());
        }
        /// <summary>
        /// Decompress a byte array into another byte array of the specified size
        /// </summary>
        /// <param name="to_decompress">Data to decompress</param>
        /// <param name="size_uncompressed">Size of the data once decompressed</param>
        /// <returns>Decompressed data as a byte array</returns>

        public static byte[] Decompress(byte[] to_decompress, int size_uncompressed)
        {
            ZlibStream stream = new ZlibStream(new System.IO.MemoryStream(to_decompress, false), CompressionMode.Decompress);
            byte[] packetData_decompressed = new byte[size_uncompressed];
            stream.Read(packetData_decompressed, 0, size_uncompressed);
            stream.Close();
            return packetData_decompressed;
        }
Esempio n. 3
0
        public static MemoryStream Compress(MemoryStream data, CompressionLevel level)
        {
            var msCompressed = new MemoryStream();
            var zOut         = new ZlibStream(msCompressed, CompressionMode.Compress, level);

            zOut.Write(data.GetBuffer(), 0, (int)data.Length);
            zOut.Close();
            return(msCompressed);
        }
Esempio n. 4
0
        public static byte[] Decompress(MemoryStream data, int size)
        {
            byte[] msDecompressed = new byte[size];
            var    zOut           = new ZlibStream(data, CompressionMode.Decompress);

            zOut.Read(msDecompressed, 0, msDecompressed.Length);
            zOut.Close();
            return(msDecompressed);
        }
Esempio n. 5
0
        public void compress(Stream inStream, Stream outStream)
        {

            ZlibStream compressionStream = new ZlibStream(outStream, CompressionMode.Compress, true);

            inStream.CopyTo(compressionStream);

            compressionStream.Close();

        }
Esempio n. 6
0
        public static byte[] ZlibCompress(byte[] data)
        {
            MemoryStream res = new MemoryStream();
            var          fw  = new Ionic.Zlib.ZlibStream(res, Ionic.Zlib.CompressionMode.Compress);

            fw.Write(data, 0, data.Length);
            fw.Close();
            res.Flush();
            return(res.ToArray());
        }
Esempio n. 7
0
 public static byte[] CompressBuffer(byte[] buffer)
 {
     byte[] compressed;
     using (var input = new MemoryStream(buffer))
     using (var compressStream = new MemoryStream())
     using (var compressor = new ZlibStream(compressStream, CompressionMode.Compress, CompressionLevel.Default, true))
     {
         input.CopyTo(compressor);
         compressor.Close();
         compressed = compressStream.ToArray();
     }
     return compressed;
 }
        /// <summary>
        /// Compress a byte array into another bytes array using Zlib compression
        /// </summary>
        /// <param name="to_compress">Data to compress</param>
        /// <returns>Compressed data as a byte array</returns>

        public static byte[] Compress(byte[] to_compress)
        {
            ZlibStream stream = new ZlibStream(new System.IO.MemoryStream(to_compress, false), CompressionMode.Compress);
            List<byte> temp_compression_list = new List<byte>();
            byte[] b = new byte[1];
            while (true)
            {
                int read = stream.Read(b, 0, 1);
                if (read > 0) { temp_compression_list.Add(b[0]); }
                else break;
            }
            stream.Close();
            return temp_compression_list.ToArray();
        }
Esempio n. 9
0
        public static OSD ZDecompressBytesToOsd(byte[] input)
        {
            OSD osd = null;

            using (MemoryStream msSinkUnCompressed = new MemoryStream())
            {
                using (Ionic.Zlib.ZlibStream zOut = new Ionic.Zlib.ZlibStream(msSinkUnCompressed, CompressionMode.Decompress, true))
                {
                    CopyStream(new MemoryStream(input), zOut);
                    zOut.Close();
                }
                msSinkUnCompressed.Seek(0L, SeekOrigin.Begin);
                osd = OSDParser.DeserializeLLSDBinary(msSinkUnCompressed.ToArray());
            }

            return(osd);
        }
Esempio n. 10
0
        public static OSD ZCompressOSD(OSD inOsd, bool useHeader)
        {
            OSD osd = null;

            using (MemoryStream msSinkCompressed = new MemoryStream())
            {
                using (Ionic.Zlib.ZlibStream zOut = new Ionic.Zlib.ZlibStream(msSinkCompressed,
                                                                              Ionic.Zlib.CompressionMode.Compress, CompressionLevel.BestCompression, true))
                {
                    CopyStream(new MemoryStream(OSDParser.SerializeLLSDBinary(inOsd, useHeader)), zOut);
                    zOut.Close();
                }

                msSinkCompressed.Seek(0L, SeekOrigin.Begin);
                osd = OSD.FromBinary(msSinkCompressed.ToArray());
            }

            return(osd);
        }
Esempio n. 11
0
 /// <summary>
 /// Compress the contents of the provided array
 /// </summary>
 /// <param name="data">An uncompressed byte array</param>
 /// <returns></returns>
 public static byte[] Compress(byte[] data)
 {
     MemoryStream out1 = new MemoryStream();
     ZlibStream deflaterOutputStream = new ZlibStream(out1, CompressionMode.Compress);
     try
     {
         //for (int i = 0; i < data.Length; i++)
         //deflaterOutputStream.WriteByte(data[i]);
         deflaterOutputStream.Write(data, 0, data.Length);   //Tony Qu changed the code
         return out1.ToArray();
     }
     catch (IOException e)
     {
         throw new RecordFormatException(e.ToString());
     }
     finally
     {
         out1.Close();
         if (deflaterOutputStream != null)
         {
             deflaterOutputStream.Close();
         }
     }
 }
Esempio n. 12
0
        public void Zlib_ZlibStream_CompressWhileWriting()
        {
            System.IO.MemoryStream msSinkCompressed;
            System.IO.MemoryStream msSinkDecompressed;
            ZlibStream zOut;

            // first, compress:
            msSinkCompressed = new System.IO.MemoryStream();
            zOut = new ZlibStream(msSinkCompressed, CompressionMode.Compress, CompressionLevel.BestCompression, true);
            CopyStream(StringToMemoryStream(IhaveaDream), zOut);
            zOut.Close();

            // at this point, msSinkCompressed contains the compressed bytes

            // now, decompress:
            msSinkDecompressed = new System.IO.MemoryStream();
            zOut = new ZlibStream(msSinkDecompressed, CompressionMode.Decompress);
            msSinkCompressed.Position = 0;
            CopyStream(msSinkCompressed, zOut);

            string result = MemoryStreamToString(msSinkDecompressed);
            TestContext.WriteLine("decompressed: {0}", result);
            Assert.AreEqual<String>(IhaveaDream, result);
        }
Esempio n. 13
0
        public void Zlib_DisposedException_ZlibStream()
        {
            string TextToCompress =  IhaveaDream;

            MemoryStream ms1= new MemoryStream();

            Stream compressor= new ZlibStream(ms1, CompressionMode.Compress, false);

            TestContext.WriteLine("Text to compress is {0} bytes: '{1}'",
                                  TextToCompress.Length, TextToCompress);
            TestContext.WriteLine("using compressor: {0}", compressor.GetType().FullName);

            StreamWriter sw = new StreamWriter(compressor, Encoding.ASCII);
            sw.Write(TextToCompress);
            sw.Close(); // implicitly closes compressor
            sw.Close(); // implicitly closes compressor, again

            compressor.Close(); // explicitly closes compressor
            var a = ms1.ToArray();
            TestContext.WriteLine("Compressed stream is {0} bytes long", a.Length);

            var ms2 = new MemoryStream(a);
            Stream decompressor  = new ZlibStream(ms2, CompressionMode.Decompress, false);

            TestContext.WriteLine("using decompressor: {0}", decompressor.GetType().FullName);

            var sr = new StreamReader(decompressor, Encoding.ASCII);
            string DecompressedText = sr.ReadToEnd();
            sr.Close();

            TestContext.WriteLine("decompressor.CanRead = {0}",decompressor.CanRead);

            TestContext.WriteLine("Read {0} characters: '{1}'", DecompressedText.Length, DecompressedText);
            TestContext.WriteLine("\n");
            Assert.AreEqual<String>(TextToCompress, DecompressedText);
        }
Esempio n. 14
0
        private byte[] InternalExtract()
        {
            _stream.Position = _manifest.Offset;

            var sizeBytes = new byte[4];
            _stream.Read(sizeBytes, 0, 4);

            int uncompressedSize = BitConverter.ToInt32(sizeBytes, 0);
            if (uncompressedSize <= 0)
            {
                return new byte[0];
            }

            _stream.Position += 4;

            var buffer = new byte[_manifest.UncompressedSize];

            var zlibStream = new ZlibStream(_stream, CompressionMode.Decompress, true);

            zlibStream.Read(buffer, 0, buffer.Length);

            zlibStream.Close();
            zlibStream.Dispose();

            return buffer;
        }
Esempio n. 15
0
        public static OSD ZCompressOSD(OSD inOsd, bool useHeader)
        {
            OSD osd = null;

            using (MemoryStream msSinkCompressed = new MemoryStream())
            {
                using (Ionic.Zlib.ZlibStream zOut = new Ionic.Zlib.ZlibStream(msSinkCompressed, 
                    Ionic.Zlib.CompressionMode.Compress, CompressionLevel.BestCompression, true))
                {
                    CopyStream(new MemoryStream(OSDParser.SerializeLLSDBinary(inOsd, useHeader)), zOut);
                    zOut.Close();
                }

                msSinkCompressed.Seek(0L, SeekOrigin.Begin);
                osd = OSD.FromBinary( msSinkCompressed.ToArray());
            }

            return osd;
        }
Esempio n. 16
0
        /// <summary>
        /// Decompresses the provided data, returning the inflated result.
        /// </summary>
        /// <param name="data">the deflated picture data.</param>
        /// <returns>the inflated picture data.</returns>
        private static byte[] InflatePictureData(byte[] data)
        {
            MemoryStream out1 = new MemoryStream();
            ZlibStream in1 = null;
            try
            {
                in1 = new ZlibStream(
                    new MemoryStream(data), CompressionMode.Decompress);

                byte[] buf = new byte[4096];
                int ReadBytes;
                while ((ReadBytes = in1.Read(buf, 0, buf.Length)) > 0)
                {
                    out1.Write(buf, 0, ReadBytes);
                }
                return out1.ToArray();
            }
            catch (IOException e)
            {
                log.Log(POILogger.INFO, "Possibly corrupt compression or non-compressed data", e);
                return data;
            }
            finally
            {
                out1.Close();
                if (in1 != null)
                {
                    in1.Close();
                }
            }
        }
Esempio n. 17
0
        /// <summary>
        /// This version of dump is a translation from the open office escher dump routine.
        /// </summary>
        /// <param name="maxLength">The number of bytes to Read</param>
        /// <param name="in1">An input stream to Read from.</param>
        public void DumpOld(long maxLength, Stream in1)
        {
            long remainingBytes = maxLength;
            short options;      // 4 bits for the version and 12 bits for the instance
            short recordId;
            int recordBytesRemaining;       // including enclosing records
            StringBuilder stringBuf = new StringBuilder();
            short nDumpSize;
            String recordName;

            bool atEOF = false;

            while (!atEOF && (remainingBytes > 0))
            {
                stringBuf = new StringBuilder();
                options = LittleEndian.ReadShort(in1);
                recordId = LittleEndian.ReadShort(in1);
                recordBytesRemaining = LittleEndian.ReadInt(in1);

                remainingBytes -= 2 + 2 + 4;

                switch (recordId)
                {
                    case unchecked((short)0xF000):
                        recordName = "MsofbtDggContainer";
                        break;
                    case unchecked((short)0xF006):
                        recordName = "MsofbtDgg";
                        break;
                    case unchecked((short)0xF016):
                        recordName = "MsofbtCLSID";
                        break;
                    case unchecked((short)0xF00B):
                        recordName = "MsofbtOPT";
                        break;
                    case unchecked((short)0xF11A):
                        recordName = "MsofbtColorMRU";
                        break;
                    case unchecked((short)0xF11E):
                        recordName = "MsofbtSplitMenuColors";
                        break;
                    case unchecked((short)0xF001):
                        recordName = "MsofbtBstoreContainer";
                        break;
                    case unchecked((short)0xF007):
                        recordName = "MsofbtBSE";
                        break;
                    case unchecked((short)0xF002):
                        recordName = "MsofbtDgContainer";
                        break;
                    case unchecked((short)0xF008):
                        recordName = "MsofbtDg";
                        break;
                    case unchecked((short)0xF118):
                        recordName = "MsofbtRegroupItem";
                        break;
                    case unchecked((short)0xF120):
                        recordName = "MsofbtColorScheme";
                        break;
                    case unchecked((short)0xF003):
                        recordName = "MsofbtSpgrContainer";
                        break;
                    case unchecked((short)0xF004):
                        recordName = "MsofbtSpContainer";
                        break;
                    case unchecked((short)0xF009):
                        recordName = "MsofbtSpgr";
                        break;
                    case unchecked((short)0xF00A):
                        recordName = "MsofbtSp";
                        break;
                    case unchecked((short)0xF00C):
                        recordName = "MsofbtTextbox";
                        break;
                    case unchecked((short)0xF00D):
                        recordName = "MsofbtClientTextbox";
                        break;
                    case unchecked((short)0xF00E):
                        recordName = "MsofbtAnchor";
                        break;
                    case unchecked((short)0xF00F):
                        recordName = "MsofbtChildAnchor";
                        break;
                    case unchecked((short)0xF010):
                        recordName = "MsofbtClientAnchor";
                        break;
                    case unchecked((short)0xF011):
                        recordName = "MsofbtClientData";
                        break;
                    case unchecked((short)0xF11F):
                        recordName = "MsofbtOleObject";
                        break;
                    case unchecked((short)0xF11D):
                        recordName = "MsofbtDeletedPspl";
                        break;
                    case unchecked((short)0xF005):
                        recordName = "MsofbtSolverContainer";
                        break;
                    case unchecked((short)0xF012):
                        recordName = "MsofbtConnectorRule";
                        break;
                    case unchecked((short)0xF013):
                        recordName = "MsofbtAlignRule";
                        break;
                    case unchecked((short)0xF014):
                        recordName = "MsofbtArcRule";
                        break;
                    case unchecked((short)0xF015):
                        recordName = "MsofbtClientRule";
                        break;
                    case unchecked((short)0xF017):
                        recordName = "MsofbtCalloutRule";
                        break;
                    case unchecked((short)0xF119):
                        recordName = "MsofbtSelection";
                        break;
                    case unchecked((short)0xF122):
                        recordName = "MsofbtUDefProp";
                        break;
                    default:
                        if (recordId >= unchecked((short)0xF018) && recordId <= unchecked((short)0xF117))
                            recordName = "MsofbtBLIP";
                        else if ((options & (short)0x000F) == (short)0x000F)
                            recordName = "UNKNOWN container";
                        else
                            recordName = "UNKNOWN ID";
                        break;
                }

                stringBuf.Append("  ");
                stringBuf.Append(HexDump.ToHex(recordId));
                stringBuf.Append("  ").Append(recordName).Append(" [");
                stringBuf.Append(HexDump.ToHex(options));
                stringBuf.Append(',');
                stringBuf.Append(HexDump.ToHex(recordBytesRemaining));
                stringBuf.Append("]  instance: ");
                stringBuf.Append(HexDump.ToHex(((short)(options >> 4))));
                Console.WriteLine(stringBuf.ToString());


                if (recordId == (unchecked((short)0xF007)) && 36 <= remainingBytes && 36 <= recordBytesRemaining)
                {	// BSE, FBSE
                    //                ULONG nP = pIn->GetRecPos();

                    byte n8;
                    //                short n16;
                    //                int n32;

                    stringBuf = new StringBuilder("    btWin32: ");
                    n8 = (byte)in1.ReadByte();
                    stringBuf.Append(HexDump.ToHex(n8));
                    stringBuf.Append(GetBlipType(n8));
                    stringBuf.Append("  btMacOS: ");
                    n8 = (byte)in1.ReadByte();
                    stringBuf.Append(HexDump.ToHex(n8));
                    stringBuf.Append(GetBlipType(n8));
                    Console.WriteLine(stringBuf.ToString());

                    Console.WriteLine("    rgbUid:");
                    HexDump.Dump(in1, 0, 16);

                    Console.Write("    tag: ");
                    OutHex(2, in1);
                    Console.WriteLine();
                    Console.Write("    size: ");
                    OutHex(4, in1);
                    Console.WriteLine();
                    Console.Write("    cRef: ");
                    OutHex(4, in1);
                    Console.WriteLine();
                    Console.Write("    offs: ");
                    OutHex(4, in1);
                    Console.WriteLine();
                    Console.Write("    usage: ");
                    OutHex(4, in1);
                    Console.WriteLine();
                    Console.Write("    cbName: ");
                    OutHex(4, in1);
                    Console.WriteLine();
                    Console.Write("    unused2: ");
                    OutHex(4, in1);
                    Console.WriteLine();
                    Console.Write("    unused3: ");
                    OutHex(4, in1);
                    Console.WriteLine();

                    // subtract the number of bytes we've Read
                    remainingBytes -= 36;
                    //n -= pIn->GetRecPos() - nP;
                    recordBytesRemaining = 0;		// loop to MsofbtBLIP
                }
                else if (recordId == unchecked((short)0xF010) && 0x12 <= remainingBytes && 0x12 <= recordBytesRemaining)
                {	// ClientAnchor
                    //ULONG nP = pIn->GetRecPos();
                    //                short n16;

                    Console.Write("    Flag: ");
                    OutHex(2, in1);
                    Console.WriteLine();
                    Console.Write("    Col1: ");
                    OutHex(2, in1);
                    Console.Write("    dX1: ");
                    OutHex(2, in1);
                    Console.Write("    Row1: ");
                    OutHex(2, in1);
                    Console.Write("    dY1: ");
                    OutHex(2, in1);
                    Console.WriteLine();
                    Console.Write("    Col2: ");
                    OutHex(2, in1);
                    Console.Write("    dX2: ");
                    OutHex(2, in1);
                    Console.Write("    Row2: ");
                    OutHex(2, in1);
                    Console.Write("    dY2: ");
                    OutHex(2, in1);
                    Console.WriteLine();

                    remainingBytes -= 18;
                    recordBytesRemaining -= 18;

                }
                else if (recordId == unchecked((short)0xF00B) || recordId == unchecked((short)0xF122))
                {	// OPT
                    int nComplex = 0;
                    Console.WriteLine("    PROPID        VALUE");
                    while (recordBytesRemaining >= 6 + nComplex && remainingBytes >= 6 + nComplex)
                    {
                        short n16;
                        int n32;
                        n16 = LittleEndian.ReadShort(in1);
                        n32 = LittleEndian.ReadInt(in1);

                        recordBytesRemaining -= 6;
                        remainingBytes -= 6;
                        Console.Write("    ");
                        Console.Write(HexDump.ToHex(n16));
                        Console.Write(" (");
                        int propertyId = n16 & (short)0x3FFF;
                        Console.Write(" " + propertyId);
                        if ((n16 & unchecked((short)0x8000)) == 0)
                        {
                            if ((n16 & (short)0x4000) != 0)
                                Console.Write(", fBlipID");
                            Console.Write(")  ");

                            Console.Write(HexDump.ToHex(n32));

                            if ((n16 & (short)0x4000) == 0)
                            {
                                Console.Write(" (");
                                Console.Write(Dec1616(n32));
                                Console.Write(')');
                                Console.Write(" {" + PropertyName((short)propertyId) + "}");
                            }
                            Console.WriteLine();
                        }
                        else
                        {
                            Console.Write(", fComplex)  ");
                            Console.Write(HexDump.ToHex(n32));
                            Console.Write(" - Complex prop len");
                            Console.WriteLine(" {" + PropertyName((short)propertyId) + "}");

                            nComplex += n32;
                        }

                    }
                    // complex property data
                    while ((nComplex & remainingBytes) > 0)
                    {
                        nDumpSize = (nComplex > (int)remainingBytes) ? (short)remainingBytes : (short)nComplex;
                        HexDump.Dump(in1, 0, nDumpSize);
                        nComplex -= nDumpSize;
                        recordBytesRemaining -= nDumpSize;
                        remainingBytes -= nDumpSize;
                    }
                }
                else if (recordId == (unchecked((short)0xF012)))
                {
                    Console.Write("    Connector rule: ");
                    Console.Write(LittleEndian.ReadInt(in1));
                    Console.Write("    ShapeID A: ");
                    Console.Write(LittleEndian.ReadInt(in1));
                    Console.Write("   ShapeID B: ");
                    Console.Write(LittleEndian.ReadInt(in1));
                    Console.Write("    ShapeID connector: ");
                    Console.Write(LittleEndian.ReadInt(in1));
                    Console.Write("   Connect pt A: ");
                    Console.Write(LittleEndian.ReadInt(in1));
                    Console.Write("   Connect pt B: ");
                    Console.WriteLine(LittleEndian.ReadInt(in1));

                    recordBytesRemaining -= 24;
                    remainingBytes -= 24;
                }
                else if (recordId >= unchecked((short)0xF018) && recordId < unchecked((short)0xF117))
                {
                    Console.WriteLine("    Secondary UID: ");
                    HexDump.Dump(in1, 0, 16);
                    Console.WriteLine("    Cache of size: " + HexDump.ToHex(LittleEndian.ReadInt(in1)));
                    Console.WriteLine("    Boundary top: " + HexDump.ToHex(LittleEndian.ReadInt(in1)));
                    Console.WriteLine("    Boundary left: " + HexDump.ToHex(LittleEndian.ReadInt(in1)));
                    Console.WriteLine("    Boundary width: " + HexDump.ToHex(LittleEndian.ReadInt(in1)));
                    Console.WriteLine("    Boundary height: " + HexDump.ToHex(LittleEndian.ReadInt(in1)));
                    Console.WriteLine("    X: " + HexDump.ToHex(LittleEndian.ReadInt(in1)));
                    Console.WriteLine("    Y: " + HexDump.ToHex(LittleEndian.ReadInt(in1)));
                    Console.WriteLine("    Cache of saved size: " + HexDump.ToHex(LittleEndian.ReadInt(in1)));
                    Console.WriteLine("    Compression Flag: " + HexDump.ToHex((byte)in1.ReadByte()));
                    Console.WriteLine("    Filter: " + HexDump.ToHex((byte)in1.ReadByte()));
                    Console.WriteLine("    Data (after decompression): ");

                    recordBytesRemaining -= 34 + 16;
                    remainingBytes -= 34 + 16;

                    nDumpSize = (recordBytesRemaining > (int)remainingBytes) ? (short)remainingBytes : (short)recordBytesRemaining;


                    byte[] buf = new byte[nDumpSize];
                    int Read = in1.Read(buf,0,buf.Length);
                    while (Read != -1 && Read < nDumpSize)
                        Read += in1.Read(buf, Read, buf.Length);

                    MemoryStream bin = new MemoryStream(buf);
                    
                    ZlibStream in2 = new ZlibStream(bin, CompressionMode.Decompress, false);
                    int bytesToDump = -1;
                    HexDump.Dump(in2, 0, bytesToDump);

                    recordBytesRemaining -= nDumpSize;
                    remainingBytes -= nDumpSize;
                    in2.Close();
                }

                bool isContainer = (options & (short)0x000F) == (short)0x000F;
                if (isContainer && remainingBytes >= 0)
                {	// Container
                    if (recordBytesRemaining <= (int)remainingBytes)
                        Console.WriteLine("            completed within");
                    else
                        Console.WriteLine("            continued elsewhere");
                }
                else if (remainingBytes >= 0)     // -> 0x0000 ... 0x0FFF
                {
                    nDumpSize = (recordBytesRemaining > (int)remainingBytes) ? (short)remainingBytes : (short)recordBytesRemaining;

                    if (nDumpSize != 0)
                    {
                        HexDump.Dump(in1, 0, nDumpSize);
                        remainingBytes -= nDumpSize;
                    }
                }
                else
                    Console.WriteLine(" >> OVERRUN <<");
            }
        }
Esempio n. 18
0
        public void Write(String path, bool force = false)
        {
            if (!force && !Dirty)
                return;
            byte[] header = new byte[8192];
            Array.Clear(header, 0, 8192);

            Int32 sectorOffset = 2;
            using (BinaryWriter file = new BinaryWriter(File.Exists(path) ? File.Open(path, FileMode.Truncate) : File.Open(path, FileMode.Create)))
            {
                file.Write(header, 0, 8192);

                for (int chunkX = 0; chunkX < 32; chunkX++)
                {
                    for (int chunkZ = 0; chunkZ < 32; chunkZ++)
                    {
                        Chunk c = Chunks[chunkX, chunkZ];
                        if (c == null)
                            continue;

                        int i = 4 * (chunkX + chunkZ * 32);

                        byte[] temp = BitConverter.GetBytes(c.Timestamp);
                        if (BitConverter.IsLittleEndian)
                            Array.Reverse(temp);
                        Array.Copy(temp, 0, header, i + 4096, 4);

                        if (c.Root == null)
                        {
                            Array.Clear(temp, 0, 4);
                            Array.Copy(temp, 0, header, i, 4);
                            continue;
                        }

                        temp = BitConverter.GetBytes(sectorOffset);
                        if (BitConverter.IsLittleEndian)
                            Array.Reverse(temp);
                        Array.Copy(temp, 1, header, i, 3);

                        if (c.RawData == null || force || c.Dirty)
                        {
                            //this is the performance bottleneck when doing 1024 chunks in a row;
                            //trying to only do when necessary
                            MemoryStream mem = new MemoryStream();
                            ZlibStream zlib = new ZlibStream(mem, CompressionMode.Compress);
                            c.Root.Write(zlib);
                            zlib.Close();
                            c.RawData = mem.ToArray();
                            c.CompressionType = 2;
                        }

                        temp = BitConverter.GetBytes(c.RawData.Length + 1);
                        if (BitConverter.IsLittleEndian)
                            Array.Reverse(temp);

                        file.Write(temp, 0, 4);
                        file.Write(c.CompressionType);
                        file.Write(c.RawData, 0, c.RawData.Length);

                        byte[] padding = new byte[(4096 - ((c.RawData.Length + 5) % 4096))];
                        Array.Clear(padding, 0, padding.Length);
                        file.Write(padding);

                        header[i + 3] = (byte)((c.RawData.Length + 5) / 4096 + 1);
                        sectorOffset += (c.RawData.Length + 5) / 4096 + 1;
                        c.Dirty = false;
                    }
                }

                file.Seek(0, SeekOrigin.Begin);
                file.Write(header, 0, 8192);
                file.Flush();
                file.Close();
                Dirty = false;
            }
        }
Esempio n. 19
0
 /// <summary>
 /// Decompresses the specified data.
 /// </summary>
 /// <param name="data">The compressed byte array.</param>
 /// <param name="pos">The starting position into the byte array.</param>
 /// <param name="Length">The number of compressed bytes to decompress.</param>
 /// <returns>An uncompressed byte array</returns>
 public static byte[] Decompress(byte[] data, int pos, int Length)
 {
     byte[] compressedData = new byte[Length];
     Array.Copy(data, pos + 50, compressedData, 0, Length);
     ZlibStream inflaterInputStream = new ZlibStream(new MemoryStream(compressedData),CompressionMode.Decompress);
     MemoryStream out1 = new MemoryStream();
     int c;
     try
     {
         while ((c = inflaterInputStream.ReadByte()) != -1)
             out1.WriteByte((byte)c);
         return out1.ToArray();
     }
     catch (IOException e)
     {
         throw new RecordFormatException(e.ToString());
     }
     finally
     {
         out1.Close();
         if (inflaterInputStream != null)
         {
             inflaterInputStream.Close();
         }
     }
 }
Esempio n. 20
0
        public static OSD ZDecompressBytesToOsd(byte[] input)
        {
            OSD osd = null;

            using (MemoryStream msSinkUnCompressed = new MemoryStream())
            {
                using (Ionic.Zlib.ZlibStream zOut = new Ionic.Zlib.ZlibStream(msSinkUnCompressed, CompressionMode.Decompress, true))
                {
                    CopyStream(new MemoryStream(input), zOut);
                    zOut.Close();
                }
                msSinkUnCompressed.Seek(0L, SeekOrigin.Begin);
                osd = OSDParser.DeserializeLLSDBinary(msSinkUnCompressed.ToArray());
            }

            return osd;
        }
Esempio n. 21
0
        void DoCompressTests()
        {
            byte[] data1 = new byte[512];
            byte[] data2 = new byte[512];
            MemoryStream mstrm1;
            MemoryStream mstrm2;
            byte[] out1;
            byte[] out2;
            Random rnd = new Random();
            int csize1, csize2, osize;

            for (int i = 0; i < 512; i++)
            {
                data1[i] = (byte)(i >> 4);
                data2[i] = (byte)rnd.Next();
            }

            mstrm1 = new MemoryStream(data1, false);
            mstrm2 = new MemoryStream(data2, false);

            Console.Write("Compressing 512 non-random bytes down...");
            out1 = new byte[1024];
            MemoryStream mo1 = new MemoryStream(out1, 0, 1024, true);
            ZlibStream zstrm = new ZlibStream(mo1, CompressionMode.Compress, true);
            mstrm1.CopyTo(zstrm);
            zstrm.Close();
            csize1 = (int)mo1.Position;
            Console.WriteLine("{0} bytes", mo1.Position);

            Console.Write("Compressing 512 random bytes down...");
            out2 = new byte[1024];
            MemoryStream mo2 = new MemoryStream(out2, 0, 1024, true);
            zstrm = new ZlibStream(mo2, CompressionMode.Compress, true);
            mstrm2.CopyTo(zstrm);
            zstrm.Close();
            csize2 = (int)mo2.Position;
            Console.WriteLine("{0} bytes", mo2.Position);

            // decompress again, using ZLibDataTransformer
            byte[] final1 = new byte[1024];
            byte[] final2 = new byte[1024];

            ZLibDecompressor trns = new ZLibDecompressor();
            osize = 512;
            trns.TransformData(out1, final1, csize1, ref osize);
            Console.WriteLine("Decompressed buffer 1 back to {0} bytes", osize);
            osize = 512;
            trns.TransformData(out2, final2, csize2, ref osize);
            Console.WriteLine("Decompressed buffer 2 back to {0} bytes", osize);

            for (int i = 0; i < 512; i++)
            {
                if (final1[i] != data1[i])
                {
                    Console.WriteLine("Decompression failed for buffer 1, byte {0}", i);
                    break;
                }
                if (final2[i] != data2[i])
                {
                    Console.WriteLine("Decompression failed for buffer 2, byte {0}", i);
                    break;
                }
            }
            Console.WriteLine("Buffer comparison finished");
        }
Esempio n. 22
0
        public static void Main(System.String[] args)
        {
            try
            {
                System.IO.MemoryStream msSinkCompressed;
                System.IO.MemoryStream msSinkDecompressed;
                ZlibStream zOut;
                String originalText = "Hello, World!  This String will be compressed... ";
                
                System.Console.Out.WriteLine("original:     {0}", originalText);

                // first, compress:
                msSinkCompressed = new System.IO.MemoryStream();
                zOut = new ZlibStream(msSinkCompressed, CompressionMode.Compress, CompressionLevel.BestCompression, true);
                CopyStream(StringToMemoryStream(originalText), zOut);
                zOut.Close();

                // at this point, msSinkCompressed contains the compressed bytes

                // now, decompress:
                msSinkCompressed.Seek(0, System.IO.SeekOrigin.Begin);
                msSinkDecompressed = new System.IO.MemoryStream();
                zOut = new ZlibStream(msSinkDecompressed, CompressionMode.Decompress, true);
                CopyStream(msSinkCompressed, zOut);

                // at this point, msSinkDecompressed contains the decompressed bytes
                string decompressed = MemoryStreamToString(msSinkDecompressed);
                System.Console.Out.WriteLine("decompressed: {0}", decompressed);
                System.Console.WriteLine();

                if (originalText == decompressed)
                    System.Console.WriteLine("A-OK. Compression followed by decompression gets the original text.");
                else
                    System.Console.WriteLine("The compression/decompression cycle failed.");
            }
            catch (System.Exception e1)
            {
                Console.WriteLine("Exception: " + e1);
            }
        }
Esempio n. 23
0
        public void Zlib_CloseTwice()
        {
            string TextToCompress = LetMeDoItNow;

            for (int i = 0; i < 3; i++)
            {
                MemoryStream ms1= new MemoryStream();

                Stream compressor = null;
                switch (i)
                {
                case 0:
                    compressor= new DeflateStream(ms1, CompressionMode.Compress, CompressionLevel.BestCompression, false);
                    break;
                case 1:
                    compressor = new GZipStream(ms1, CompressionMode.Compress, false);
                    break;
                case 2:
                    compressor = new ZlibStream(ms1, CompressionMode.Compress, false);
                    break;
                }

                TestContext.WriteLine("Text to compress is {0} bytes: '{1}'",
                                      TextToCompress.Length, TextToCompress);
                TestContext.WriteLine("using compressor: {0}", compressor.GetType().FullName);

                StreamWriter sw = new StreamWriter(compressor, Encoding.ASCII);
                sw.Write(TextToCompress);
                sw.Close(); // implicitly closes compressor
                sw.Close();// implicitly closes compressor, again

                compressor.Close(); // explicitly closes compressor
                var a = ms1.ToArray();
                TestContext.WriteLine("Compressed stream is {0} bytes long", a.Length);

                var ms2 = new MemoryStream(a);
                Stream decompressor = null;

                switch (i)
                {
                case 0:
                    decompressor = new DeflateStream(ms2, CompressionMode.Decompress, false);
                    break;
                case 1:
                    decompressor = new GZipStream(ms2, CompressionMode.Decompress, false);
                    break;
                case 2:
                    decompressor = new ZlibStream(ms2, CompressionMode.Decompress, false);
                    break;
                }

                TestContext.WriteLine("using decompressor: {0}", decompressor.GetType().FullName);

                var sr = new StreamReader(decompressor, Encoding.ASCII);
                string DecompressedText = sr.ReadToEnd();

                // verify that multiple calls to Close() do not throw
                sr.Close();
                sr.Close();
                decompressor.Close();

                TestContext.WriteLine("Read {0} characters: '{1}'", DecompressedText.Length, DecompressedText);
                TestContext.WriteLine("\n");
                Assert.AreEqual<String>(TextToCompress, DecompressedText);
            }
        }