Пример #1
0
        public void Write(BinaryWriter bw)
        {
            bw.Write(LxrsFooter.s_magic);
            bw.Write(LxrsFooter.s_version);

            using var ms     = new MemoryStream();
            using var tempBw = new BinaryWriter(ms);
            foreach (var s in FileInfos)
            {
                tempBw.WriteCR2WString(s);
            }

            var inbuffer = ms.ToByteArray();

            IEnumerable <byte> outBuffer = new List <byte>();
            var r = OodleHelper.Compress(
                inbuffer,
                inbuffer.Length,
                ref outBuffer,
                OodleNative.OodleLZ_Compressor.Kraken,
                OodleNative.OodleLZ_Compression.Normal,
                false);

            bw.Write(inbuffer.Length);      //size
            bw.Write(outBuffer.Count());    //zsize
            bw.Write(FileInfos.Count());    //count
            bw.Write(outBuffer.ToArray());
        }
Пример #2
0
        /// <summary>
        /// Kraken-compresses a buffer and writes it to a stream.
        /// </summary>
        /// <param name="bw"></param>
        /// <param name="inbuffer"></param>
        /// <returns></returns>
        public static (uint, uint) CompressAndWrite(this BinaryWriter bw, byte[] inbuffer)
        {
            var size = (uint)inbuffer.Length;

            if (size < 255)
            {
                var crc = Crc32Algorithm.Compute(inbuffer);
                bw.Write(inbuffer);

                return(size, crc);
            }
            else
            {
                IEnumerable <byte> outBuffer = new List <byte>();
                var r = OodleHelper.Compress(
                    inbuffer,
                    (int)size,
                    ref outBuffer,
                    OodleNative.OodleLZ_Compressor.Kraken,
                    OodleNative.OodleLZ_Compression.Normal);

                var b = outBuffer.ToArray();

                var crc = Crc32Algorithm.Compute(b);
                bw.Write(b);

                return((uint)r, crc);
            }
        }
Пример #3
0
        private static int OodleTask(string path, string outpath, bool decompress, bool compress)
        {
            if (string.IsNullOrEmpty(path))
            {
                return(0);
            }

            if (string.IsNullOrEmpty(outpath))
            {
                outpath = Path.ChangeExtension(path, ".kark");
            }

            if (decompress)
            {
                var file = File.ReadAllBytes(path);
                using var ms = new MemoryStream(file);
                using var br = new BinaryReader(ms);

                var oodleCompression = br.ReadBytes(4);
                if (!(oodleCompression.SequenceEqual(new byte[] { 0x4b, 0x41, 0x52, 0x4b })))
                {
                    throw new NotImplementedException();
                }

                var size = br.ReadUInt32();

                var buffer = br.ReadBytes(file.Length - 8);

                byte[] unpacked     = new byte[size];
                long   unpackedSize = OodleHelper.Decompress(buffer, unpacked);

                using var msout = new MemoryStream();
                using var bw    = new BinaryWriter(msout);
                bw.Write(unpacked);

                File.WriteAllBytes($"{outpath}", msout.ToArray());
            }

            if (compress)
            {
                var inbuffer = File.ReadAllBytes(path);
                IEnumerable <byte> outBuffer = new List <byte>();

                var r = OodleHelper.Compress(
                    inbuffer,
                    inbuffer.Length,
                    ref outBuffer,
                    OodleNative.OodleLZ_Compressor.Kraken,
                    OodleNative.OodleLZ_Compression.Normal,
                    true);

                File.WriteAllBytes(outpath, outBuffer.ToArray());
            }

            return(1);
        }