Example #1
0
        public void LzmaUncompress(SharedSettings s)
        {
            if(s.Enc.Length != 1)
                throw new ArgumentException("Settings must contain a single byte.", "propLength");

            if(s.Variant == 1)
            {
                LZMA.CLzma2Dec dec = new LZMA.CLzma2Dec();
                dec.Lzma2Dec_Construct();
                var res = dec.Lzma2Dec_Allocate(s.Enc[0], LZMA.ISzAlloc.SmallAlloc);
                if(res != LZMA.SZ_OK)
                    throw new Exception("Lzma2Dec_Allocate failed: " + res);
                dec.Lzma2Dec_Init();
                for(; ; )
                {
                    long s_WrittenSize = s.Dst.Length - s.Dst.Offset;
                    long s_UsedSize = s.Src.Length - s.Src.Offset;

                    LZMA.ELzmaStatus status;
                    res = dec.Lzma2Dec_DecodeToBuf(P.From(s.Dst), ref s_WrittenSize, P.From(s.Src), ref s_UsedSize, LZMA.ELzmaFinishMode.LZMA_FINISH_END, out status);
                    if(res != LZMA.SZ_OK)
                        throw new Exception("Lzma2Dec_DecodeToBuf failed: " + res);

                    s.Dst.Offset += checked((int)s_WrittenSize);
                    s.Src.Offset += checked((int)s_UsedSize);

                    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);
                }
                s.WrittenSize = s.Dst.Offset;
                s.Dst.Offset = 0;
                s.UsedSize = s.Src.Offset;
                s.Src.Offset = 0;
            }
            else
            {
                long s_WrittenSize = s.Dst.Length;
                long s_UsedSize = s.Src.Length;

                LZMA.ELzmaStatus status;
                var res = LZMA.CLzma2Dec.Lzma2Decode(
                    P.From(s.Dst), ref s_WrittenSize,
                    P.From(s.Src), ref s_UsedSize,
                    s.Enc[0], LZMA.ELzmaFinishMode.LZMA_FINISH_END, out status,
                    LZMA.ISzAlloc.SmallAlloc);
                if(res != LZMA.SZ_OK)
                    throw new Exception("LzmaUncompress failed: " + res);
                switch(status)
                {
                case LZMA.ELzmaStatus.LZMA_STATUS_NEEDS_MORE_INPUT:
                    throw new EndOfStreamException();
                case LZMA.ELzmaStatus.LZMA_STATUS_FINISHED_WITH_MARK:
                    if(s.ActualWriteEndMark == 0)
                        throw new InvalidDataException();
                    break;
                case LZMA.ELzmaStatus.LZMA_STATUS_MAYBE_FINISHED_WITHOUT_MARK:
                    if(s.ActualWriteEndMark != 0)
                        throw new InvalidDataException();
                    break;
                default:
                    throw new Exception(status.ToString());
                }

                s.WrittenSize = checked((int)s_WrittenSize);
                s.UsedSize = checked((int)s_UsedSize);
            }
        }
Example #2
0
        public void LzmaUncompress(SharedSettings s)
        {
            if (s.Enc.Length != 1)
            {
                throw new ArgumentException("Settings must contain a single byte.", "propLength");
            }

            if (s.Variant == 1)
            {
                LZMA.CLzma2Dec dec = new LZMA.CLzma2Dec();
                dec.Lzma2Dec_Construct();
                var res = dec.Lzma2Dec_Allocate(s.Enc[0], LZMA.ISzAlloc.SmallAlloc);
                if (res != LZMA.SZ_OK)
                {
                    throw new Exception("Lzma2Dec_Allocate failed: " + res);
                }
                dec.Lzma2Dec_Init();
                for (;;)
                {
                    long s_WrittenSize = s.Dst.Length - s.Dst.Offset;
                    long s_UsedSize    = s.Src.Length - s.Src.Offset;

                    LZMA.ELzmaStatus status;
                    res = dec.Lzma2Dec_DecodeToBuf(P.From(s.Dst), ref s_WrittenSize, P.From(s.Src), ref s_UsedSize, LZMA.ELzmaFinishMode.LZMA_FINISH_END, out status);
                    if (res != LZMA.SZ_OK)
                    {
                        throw new Exception("Lzma2Dec_DecodeToBuf failed: " + res);
                    }

                    s.Dst.Offset += checked ((int)s_WrittenSize);
                    s.Src.Offset += checked ((int)s_UsedSize);

                    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);
                }
                s.WrittenSize = s.Dst.Offset;
                s.Dst.Offset  = 0;
                s.UsedSize    = s.Src.Offset;
                s.Src.Offset  = 0;
            }
            else
            {
                long s_WrittenSize = s.Dst.Length;
                long s_UsedSize    = s.Src.Length;

                LZMA.ELzmaStatus status;
                var res = LZMA.CLzma2Dec.Lzma2Decode(
                    P.From(s.Dst), ref s_WrittenSize,
                    P.From(s.Src), ref s_UsedSize,
                    s.Enc[0], LZMA.ELzmaFinishMode.LZMA_FINISH_END, out status,
                    LZMA.ISzAlloc.SmallAlloc);
                if (res != LZMA.SZ_OK)
                {
                    throw new Exception("LzmaUncompress failed: " + res);
                }
                switch (status)
                {
                case LZMA.ELzmaStatus.LZMA_STATUS_NEEDS_MORE_INPUT:
                    throw new EndOfStreamException();

                case LZMA.ELzmaStatus.LZMA_STATUS_FINISHED_WITH_MARK:
                    if (s.ActualWriteEndMark == 0)
                    {
                        throw new InvalidDataException();
                    }
                    break;

                case LZMA.ELzmaStatus.LZMA_STATUS_MAYBE_FINISHED_WITHOUT_MARK:
                    if (s.ActualWriteEndMark != 0)
                    {
                        throw new InvalidDataException();
                    }
                    break;

                default:
                    throw new Exception(status.ToString());
                }

                s.WrittenSize = checked ((int)s_WrittenSize);
                s.UsedSize    = checked ((int)s_UsedSize);
            }
        }