Ejemplo n.º 1
0
        public void LzmaUncompress(SharedSettings s)
        {
            switch(s.Variant)
            {
            case 1:
                {
                    var decoder = new LZMA.CLzmaDec();
                    decoder.LzmaDec_Construct();

                    var res = decoder.LzmaDec_Allocate(P.From(s.Enc), checked((uint)s.Enc.Length), LZMA.ISzAlloc.SmallAlloc);
                    if(res != LZMA.SZ_OK)
                        throw new Exception("Allocate failed: " + res);

                    decoder.LzmaDec_Init();

                    P<byte> dstPtr = P.From(s.Dst);
                    long s_WrittenSize = s.Dst.Length;
                    s.WrittenSize = 0;

                    P<byte> srcPtr = P.From(s.Src);
                    long s_UsedSize = s.Src.Length;
                    s.UsedSize = 0;

                    for(; ; )
                    {
                        LZMA.ELzmaStatus status;
                        res = decoder.LzmaDec_DecodeToBuf(dstPtr, ref s_WrittenSize, srcPtr, ref s_UsedSize, LZMA.ELzmaFinishMode.LZMA_FINISH_END, out status);
                        if(res != LZMA.SZ_OK)
                            throw new Exception("DecodeToBuf failed: " + res);

                        s.WrittenSize += checked((int)s_WrittenSize);
                        s.UsedSize += checked((int)s_UsedSize);
                        dstPtr += s_WrittenSize;
                        srcPtr += s_UsedSize;
                        s_WrittenSize = dstPtr.mBuffer.Length - dstPtr.mOffset;
                        s_UsedSize = srcPtr.mBuffer.Length - srcPtr.mOffset;

                        if(status == LZMA.ELzmaStatus.LZMA_STATUS_NEEDS_MORE_INPUT
                            || status == LZMA.ELzmaStatus.LZMA_STATUS_NOT_FINISHED)
                            continue;

                        if(status == LZMA.ELzmaStatus.LZMA_STATUS_FINISHED_WITH_MARK)
                        {
                            if(s.ActualWriteEndMark == 0)
                                throw new Exception("Finished with mark even though we didn't want to write one.");
                            break;
                        }

                        if(status == LZMA.ELzmaStatus.LZMA_STATUS_MAYBE_FINISHED_WITHOUT_MARK)
                        {
                            if(s.ActualWriteEndMark != 0)
                                break;
                        }

                        throw new NotSupportedException("Unsupported status case: " + status);
                    }

                    decoder.LzmaDec_Free(LZMA.ISzAlloc.SmallAlloc);

                    s.WrittenSize = (int)dstPtr.mOffset;
                    s.UsedSize = (int)srcPtr.mOffset;
                }
                break;
            default:
                {
                    long s_WrittenSize = s.Dst.Length;
                    long s_UsedSize = s.Src.Length;
                    var res = LZMA.LzmaUncompress(
                        P.From(s.Dst), ref s_WrittenSize,
                        P.From(s.Src), ref s_UsedSize,
                        P.From(s.Enc), s.Enc.Length);
                    if(res != LZMA.SZ_OK)
                        throw new Exception("LzmaUcompress failed: " + res);
                    s.WrittenSize = (int)s_WrittenSize;
                    s.UsedSize = (int)s_UsedSize;
                }
                break;
            }
        }
Ejemplo n.º 2
0
        public void LzmaUncompress(SharedSettings s)
        {
            switch (s.Variant)
            {
            case 1:
            {
                var decoder = new LZMA.CLzmaDec();
                decoder.LzmaDec_Construct();

                var res = decoder.LzmaDec_Allocate(P.From(s.Enc), checked ((uint)s.Enc.Length), LZMA.ISzAlloc.SmallAlloc);
                if (res != LZMA.SZ_OK)
                {
                    throw new Exception("Allocate failed: " + res);
                }

                decoder.LzmaDec_Init();

                P <byte> dstPtr        = P.From(s.Dst);
                long     s_WrittenSize = s.Dst.Length;
                s.WrittenSize = 0;

                P <byte> srcPtr     = P.From(s.Src);
                long     s_UsedSize = s.Src.Length;
                s.UsedSize = 0;

                for (;;)
                {
                    LZMA.ELzmaStatus status;
                    res = decoder.LzmaDec_DecodeToBuf(dstPtr, ref s_WrittenSize, srcPtr, ref s_UsedSize, LZMA.ELzmaFinishMode.LZMA_FINISH_END, out status);
                    if (res != LZMA.SZ_OK)
                    {
                        throw new Exception("DecodeToBuf failed: " + res);
                    }

                    s.WrittenSize += checked ((int)s_WrittenSize);
                    s.UsedSize    += checked ((int)s_UsedSize);
                    dstPtr        += s_WrittenSize;
                    srcPtr        += s_UsedSize;
                    s_WrittenSize  = dstPtr.mBuffer.Length - dstPtr.mOffset;
                    s_UsedSize     = srcPtr.mBuffer.Length - srcPtr.mOffset;

                    if (status == LZMA.ELzmaStatus.LZMA_STATUS_NEEDS_MORE_INPUT ||
                        status == LZMA.ELzmaStatus.LZMA_STATUS_NOT_FINISHED)
                    {
                        continue;
                    }

                    if (status == LZMA.ELzmaStatus.LZMA_STATUS_FINISHED_WITH_MARK)
                    {
                        if (s.ActualWriteEndMark == 0)
                        {
                            throw new Exception("Finished with mark even though we didn't want to write one.");
                        }
                        break;
                    }

                    if (status == LZMA.ELzmaStatus.LZMA_STATUS_MAYBE_FINISHED_WITHOUT_MARK)
                    {
                        if (s.ActualWriteEndMark != 0)
                        {
                            break;
                        }
                    }

                    throw new NotSupportedException("Unsupported status case: " + status);
                }

                decoder.LzmaDec_Free(LZMA.ISzAlloc.SmallAlloc);

                s.WrittenSize = (int)dstPtr.mOffset;
                s.UsedSize    = (int)srcPtr.mOffset;
            }
            break;

            default:
            {
                long s_WrittenSize = s.Dst.Length;
                long s_UsedSize    = s.Src.Length;
                var  res           = LZMA.LzmaUncompress(
                    P.From(s.Dst), ref s_WrittenSize,
                    P.From(s.Src), ref s_UsedSize,
                    P.From(s.Enc), s.Enc.Length);
                if (res != LZMA.SZ_OK)
                {
                    throw new Exception("LzmaUcompress failed: " + res);
                }
                s.WrittenSize = (int)s_WrittenSize;
                s.UsedSize    = (int)s_UsedSize;
            }
            break;
            }
        }