예제 #1
0
        public unsafe bool Apply(ReadOnlySpan <byte> src, ref Span <byte> dst)
        {
            if (dst.Length <= src.Length + 1)
                return(false);

            fixed(byte *srcPtr = src, dstPtr = dst)
            {
                var count = CLZF2.TryCompress(srcPtr, dstPtr + 1, src.Length, src.Length);

                if (count <= 0 || count >= src.Length)
                {
                    // Compression failed. Prepend header and send.
                    *dstPtr = kHeaderUncompressed;
                    UnsafeUtility.MemCpy(dstPtr + 1, srcPtr, src.Length);
                    count = src.Length;
                }
                else
                {
                    // Compression succeeded. Prepend header and send.
                    if (count + 1 < dst.Length)
                    {
                        return(false);
                    }
                    Assert.IsTrue(count + 1 <= dst.Length);
                    *dstPtr = kHeaderCompresssed;
                }
                dst = dst.Slice(0, count + 1);
            }

            return(true);
        }
예제 #2
0
        public unsafe void Apply(ref byte[] data, ref int size)
        {
            Assert.IsTrue(data.Length >= size);
            fixed(byte *ptr = data)
            {
                var temp  = stackalloc byte[size + 1];
                var count = CLZF2.TryCompress(ptr, temp + 1, size, size);

                if (count <= 0 || count >= size)
                {
                    // Compression failed. Prepend header and send.
                    if (size + 1 >= data.Length)
                    {
                        var pool    = ArrayPool <byte> .Shared;
                        var tempBuf = pool.Rent(size + 1);
                        fixed(byte *outPtr = tempBuf)
                        {
                            UnsafeUtility.MemCpy(outPtr + 1, ptr, size);
                            *outPtr = kHeaderUncompressed;
                        }

                        pool.Return(data);
                        data = tempBuf;
                    }
                    else
                    {
                        UnsafeUtility.MemMove(ptr + 1, ptr, size);
                        *ptr = kHeaderUncompressed;
                    }
                    size++;
                }
                else
                {
                    // Compression failed. Prepend header and send.
                    Assert.IsTrue(count <= data.Length);
                    *temp = kHeaderCompresssed;
                    UnsafeUtility.MemCpy(ptr, temp, count + 1);
                    size = count + 1;
                }
            }
        }