Beispiel #1
0
        public int ReadFrom(byte[] Buffer, int StartIndex = 0)
        {
            int cursor = StartIndex;

            uint sig = BitConverter.ToUInt32(Buffer, cursor);

            cursor += TypeSizes.INT;

            if (sig == SIGNATURE)
            {
                uint ver = BitConverter.ToUInt32(Buffer, cursor);
                cursor += TypeSizes.INT;

                uint entries = BitConverter.ToUInt32(Buffer, cursor);
                cursor += TypeSizes.INT;

                // decrypt old versions first (v4 first unencryted)
                // note: this might need different passwords for other versions
                if (ver <= VERSION3)
                {
#if WINCLR && X86
                    uint streamlength = BitConverter.ToUInt32(Buffer, cursor);
                    cursor += TypeSizes.INT;

                    uint expectedresponse = BitConverter.ToUInt32(Buffer, cursor);
                    cursor += TypeSizes.INT;

                    Crush32.Decrypt(Buffer, cursor, (int)streamlength, ver, expectedresponse, PASSWORDV3);
#else
                    throw new Exception(RooFile.ERRORCRUSHPLATFORM);
#endif
                }

                // now load strings
                StringResources.Clear();
                for (int i = 0; i < entries; i++)
                {
                    uint ID = BitConverter.ToUInt32(Buffer, cursor);
                    cursor += TypeSizes.INT;

                    // look for terminating 0x00 (NULL)
                    ushort strlen = 0;
                    while ((Buffer.Length > cursor + strlen) && Buffer[cursor + strlen] != 0x00)
                    {
                        strlen++;
                    }

                    string Value = Encoding.Default.GetString(Buffer, cursor, strlen);
                    cursor += strlen + TypeSizes.BYTE;

                    StringResources.TryAdd(ID, Value);
                }
            }
            else
            {
                throw new Exception("Wrong RSC file signature: " + sig + " (expected " + SIGNATURE + ").");
            }

            return(cursor - StartIndex);
        }
        /// <summary>
        /// Old Decompress method not using .NET compressor
        /// </summary>
        /// <param name="Data"></param>
        /// <returns></returns>
        public byte[] DecompressOld(byte[] Data)
        {
            byte[] decompressedPixelData = new byte[UncompressedLength];

            // ZLIB
            if (version > BgfFile.VERSION9)
            {
                // init streams
                MemoryStream  destStream = new MemoryStream(decompressedPixelData, true);
                ZOutputStream destZ      = new ZOutputStream(destStream);

                // decompress
                destZ.Write(Data, 0, Data.Length);
                destZ.Flush();
                destZ.finish();

                // cleanup
                destStream.Dispose();
                destZ.Dispose();
            }
            // CRUSH
            else
            {
#if WINCLR && X86
                // decompress
                Crush32.Decompress(Data, 0, decompressedPixelData, 0, (int)UncompressedLength, CompressedLength);
#else
                throw new Exception(ERRORCRUSHPLATFORM);
#endif
            }

            // set decompressed array to pixeldata
            return(decompressedPixelData);
        }
Beispiel #3
0
        public int ReadFrom(byte[] Buffer, int StartIndex = 0)
        {
            int cursor = StartIndex;

            uint sig = BitConverter.ToUInt32(Buffer, cursor);

            cursor += TypeSizes.INT;

            if (sig == SIGNATURE)
            {
                version = BitConverter.ToUInt32(Buffer, cursor);
                cursor += TypeSizes.INT;

                uint entries = BitConverter.ToUInt32(Buffer, cursor);
                cursor += TypeSizes.INT;

                // decrypt old versions first (v4 first unencryted)
                // note: this might need different passwords for other versions
                if (version <= VERSION3)
                {
#if WINCLR && X86
                    uint streamlength = BitConverter.ToUInt32(Buffer, cursor);
                    cursor += TypeSizes.INT;

                    uint expectedresponse = BitConverter.ToUInt32(Buffer, cursor);
                    cursor += TypeSizes.INT;

                    Crush32.Decrypt(Buffer, cursor, (int)streamlength, version, expectedresponse, PASSWORDV3);
#else
                    throw new Exception(RooFile.ERRORCRUSHPLATFORM);
#endif
                }

                // now load strings
                StringResources.Clear();
                StringResources.Capacity = (int)entries + 100;
                for (int i = 0; i < entries; i++)
                {
                    RsbResourceID entry = new RsbResourceID(version, Buffer, cursor);
                    cursor += entry.ByteLength;

                    StringResources.Add(entry);
                }
            }
            else
            {
                throw new Exception("Wrong RSC file signature: " + sig + " (expected " + SIGNATURE + ").");
            }

            return(cursor - StartIndex);
        }
        /// <summary>
        /// Returns a compressed byte[] of PixelData argument
        /// </summary>
        /// <param name="Data"></param>
        /// <returns></returns>
        public byte[] Compress(byte[] Data)
        {
            // allocate a buffer with uncompressed length to write compressed stream to
            byte[] tempBuffer = new byte[UncompressedLength];
            int    compressedLength;

            // ZLIB
            if (version > BgfFile.VERSION9)
            {
                // init streams
                MemoryStream  destStream = new MemoryStream(tempBuffer, true);
                ZOutputStream destZ      = new ZOutputStream(destStream, zlibConst.Z_BEST_COMPRESSION);

                // compress
                destZ.Write(Data, 0, Data.Length);
                destZ.Flush();
                destZ.finish();

                // update compressed length
                compressedLength = (int)destZ.TotalOut;

                // cleanup
                destStream.Dispose();
                destZ.Dispose();
            }
            // CRUSH
            else
            {
#if WINCLR && X86
                // compress to tempBuffer
                compressedLength = Crush32.Compress(Data, 0, tempBuffer, 0, (int)UncompressedLength);
#else
                throw new Exception(ERRORCRUSHPLATFORM);
#endif
            }

            // copy all bytes we actually used from tempBuffer to new PixelData
            byte[] newPixelData = new byte[compressedLength];
            Array.Copy(tempBuffer, 0, newPixelData, 0, compressedLength);

            return(newPixelData);
        }
        /// <summary>
        /// Returns a decompressed byte[] of PixelData argument
        /// </summary>
        /// <param name="Data"></param>
        /// <returns></returns>
        public byte[] Decompress(byte[] Data)
        {
            byte[] decompressedPixelData = new byte[UncompressedLength];

            // ZLIB
            if (version > BgfFile.VERSION9)
            {
                // init sourcestream
                MemoryStream srcStream = new MemoryStream(Data, false);

                // must skip two bytes not part of deflate but used by zlib
                srcStream.ReadByte();
                srcStream.ReadByte();

                // init .net decompressor
                DeflateStream destZ = new DeflateStream(srcStream, CompressionMode.Decompress);

                // decompress
                destZ.Read(decompressedPixelData, 0, UncompressedLength);

                // cleanup
                destZ.Dispose();
                srcStream.Dispose();
            }
            // CRUSH
            else
            {
#if WINCLR && X86
                // decompress
                Crush32.Decompress(Data, 0, decompressedPixelData, 0, (int)UncompressedLength, CompressedLength);
#else
                throw new Exception(ERRORCRUSHPLATFORM);
#endif
            }

            // set decompressed array to pixeldata
            return(decompressedPixelData);
        }