public void Code(System.IO.Stream inStream, System.IO.Stream outStream, Int64 inSize, Int64 outSize, ICodeProgress progress) { if (m_OutWindow == null) { CreateDictionary(); } m_OutWindow.Init(outStream); if (outSize > 0) { m_OutWindow.SetLimit(outSize); } else { m_OutWindow.SetLimit(Int64.MaxValue - m_OutWindow.Total); } RangeCoder.Decoder rangeDecoder = new RangeCoder.Decoder(); rangeDecoder.Init(inStream); Code(m_DictionarySize, m_OutWindow, rangeDecoder); m_OutWindow.ReleaseStream(); rangeDecoder.ReleaseStream(); if (!rangeDecoder.IsFinished || (inSize > 0 && rangeDecoder.Total != inSize)) { throw new DataErrorException(); } if (m_OutWindow.HasPending) { throw new DataErrorException(); } m_OutWindow = null; }
void CreateDictionary() { if (m_DictionarySize < 0) { throw new InvalidParamException(); } m_OutWindow = new LZ.OutWindow(); int blockSize = Math.Max(m_DictionarySize, (1 << 12)); m_OutWindow.Create(blockSize); }
internal bool Code(int dictionarySize, LZ.OutWindow outWindow, RangeCoder.Decoder rangeDecoder) { int dictionarySizeCheck = Math.Max(dictionarySize, 1); outWindow.CopyPending(); while (outWindow.HasSpace) { uint posState = (uint)outWindow.Total & m_PosStateMask; if (m_IsMatchDecoders[(state.Index << Base.kNumPosStatesBitsMax) + posState].Decode(rangeDecoder) == 0) { byte b; byte prevByte = outWindow.GetByte(0); if (!state.IsCharState()) { b = m_LiteralDecoder.DecodeWithMatchByte(rangeDecoder, (uint)outWindow.Total, prevByte, outWindow.GetByte((int)rep0)); } else { b = m_LiteralDecoder.DecodeNormal(rangeDecoder, (uint)outWindow.Total, prevByte); } outWindow.PutByte(b); state.UpdateChar(); } else { uint len; if (m_IsRepDecoders[state.Index].Decode(rangeDecoder) == 1) { if (m_IsRepG0Decoders[state.Index].Decode(rangeDecoder) == 0) { if (m_IsRep0LongDecoders[(state.Index << Base.kNumPosStatesBitsMax) + posState].Decode(rangeDecoder) == 0) { state.UpdateShortRep(); outWindow.PutByte(outWindow.GetByte((int)rep0)); continue; } } else { UInt32 distance; if (m_IsRepG1Decoders[state.Index].Decode(rangeDecoder) == 0) { distance = rep1; } else { if (m_IsRepG2Decoders[state.Index].Decode(rangeDecoder) == 0) { distance = rep2; } else { distance = rep3; rep3 = rep2; } rep2 = rep1; } rep1 = rep0; rep0 = distance; } len = m_RepLenDecoder.Decode(rangeDecoder, posState) + Base.kMatchMinLen; state.UpdateRep(); } else { rep3 = rep2; rep2 = rep1; rep1 = rep0; len = Base.kMatchMinLen + m_LenDecoder.Decode(rangeDecoder, posState); state.UpdateMatch(); uint posSlot = m_PosSlotDecoder[Base.GetLenToPosState(len)].Decode(rangeDecoder); if (posSlot >= Base.kStartPosModelIndex) { int numDirectBits = (int)((posSlot >> 1) - 1); rep0 = ((2 | (posSlot & 1)) << numDirectBits); if (posSlot < Base.kEndPosModelIndex) { rep0 += BitTreeDecoder.ReverseDecode(m_PosDecoders, rep0 - posSlot - 1, rangeDecoder, numDirectBits); } else { rep0 += (rangeDecoder.DecodeDirectBits( numDirectBits - Base.kNumAlignBits) << Base.kNumAlignBits); rep0 += m_PosAlignDecoder.ReverseDecode(rangeDecoder); } } else { rep0 = posSlot; } } if (rep0 >= outWindow.Total || rep0 >= dictionarySizeCheck) { if (rep0 == 0xFFFFFFFF) { return(true); } throw new DataErrorException(); } outWindow.CopyBlock((int)rep0, (int)len); } } return(false); }