예제 #1
0
        public void Kraken_Compress(int level)
        {
            var path     = Path.GetFullPath(Path.Combine("Resources", s_uncompressedFile));
            var inbuffer = File.ReadAllBytes(path);
            var compressedBufferSizeNeeded = Oodle.GetCompressedBufferSizeNeeded(inbuffer.Length);
            var outdir = Path.Combine(Environment.CurrentDirectory, "ooz");

            Directory.CreateDirectory(outdir);

            // oodle
            //IEnumerable<byte> outBuffer = new List<byte>();
            //var r = Oodle.Compress(inbuffer, ref outBuffer, true, (Oodle.CompressionLevel)level);
            //var outpath = Path.Combine(outdir, $"{level}_oodle.kark");
            //var final = outBuffer.ToArray();
            //File.WriteAllBytes(outpath, final);

#if _WINDOWS
            // kraken
            var outBuffer2 = new byte[compressedBufferSizeNeeded];
            //var outBuffer2 = new byte[inbuffer.Length + 65536];
            KrakenLib.Load();
            var r2       = KrakenLib.Compress(inbuffer, outBuffer2, (int)level);
            var outpath2 = Path.Combine(outdir, $"{(int)level}_kraken.kark");
            var final2   = outBuffer2.Take(r2).ToArray();
            File.WriteAllBytes(outpath2, final2);
#endif

            var compareFile  = Path.GetFullPath(Path.Combine("Resources", $"{level}_oodle.kark"));
            var compareBytes = File.ReadAllBytes(compareFile);
            Assert.IsTrue(Enumerable.SequenceEqual(final2, compareBytes.Skip(8)));
        }
예제 #2
0
파일: Oodle.cs 프로젝트: flibX0r/WolvenKit
    public static int Compress(byte[] inputBuffer, ref IEnumerable <byte> outputBuffer, bool useRedHeader,
                               CompressionLevel level = CompressionLevel.Normal, Compressor compressor = Compressor.Kraken)
    {
        if (inputBuffer == null)
        {
            throw new ArgumentNullException(nameof(inputBuffer));
        }
        var inputCount = inputBuffer.Length;

        if (inputCount <= 0 || inputCount > inputBuffer.Length)
        {
            throw new ArgumentOutOfRangeException(nameof(inputCount));
        }
        if (outputBuffer == null)
        {
            throw new ArgumentNullException(nameof(outputBuffer));
        }

        var result = 0;
        var compressedBufferSizeNeeded = Oodle.GetCompressedBufferSizeNeeded(inputCount);
        var compressedBuffer           = new byte[compressedBufferSizeNeeded];

        if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
        {
            result = CompressionSettings.Get().UseOodle
                ? OodleLib.OodleLZ_Compress(inputBuffer, compressedBuffer, compressor, level)
                : KrakenLib.Compress(inputBuffer, compressedBuffer, (int)level);
        }
        else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
        {
            result = KrakenLib.Compress(inputBuffer, compressedBuffer, (int)level);
        }
        else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
        {
            result = OozNative.Kraken_Compress(inputBuffer, compressedBuffer, (int)level);
        }
        else
        {
            throw new NotImplementedException();
        }


        if (result == 0 || inputCount <= (result + 8))
        {
            outputBuffer = inputBuffer;
            return(outputBuffer.Count());
        }

        if (useRedHeader)
        {
            // write KARK header
            var writelist = new List <byte>()
            {
                0x4B, 0x41, 0x52, 0x4B          //KARK, TODO: make this dependent on the compression algo
            };
            // write size
            writelist.AddRange(BitConverter.GetBytes(inputCount));
            // write compressed data
            writelist.AddRange(compressedBuffer.Take(result));

            outputBuffer = writelist;
        }
        else
        {
            outputBuffer = compressedBuffer.Take(result);
        }

        return(outputBuffer.Count());
    }