public PpmdStream(PpmdProperties properties, Stream stream, bool compress) { _properties = properties; _stream = stream; _compress = compress; if (properties.Version == PpmdVersion.I1) { _model = new Model(); if (compress) { _model.EncodeStart(properties); } else { _model.DecodeStart(stream, properties); } } if (properties.Version == PpmdVersion.H) { _modelH = new ModelPpm(); if (compress) { throw new NotImplementedException(); } _modelH.DecodeInit(stream, properties.ModelOrder, properties.AllocatorSize); } if (properties.Version == PpmdVersion.H7Z) { _modelH = new ModelPpm(); if (compress) { throw new NotImplementedException(); } _modelH.DecodeInit(null, properties.ModelOrder, properties.AllocatorSize); _decoder = new Decoder(); _decoder.Init(stream); } }
private bool ReadTables() { byte[] bitLength = new byte[PackDef.BC]; byte[] table = new byte[PackDef.HUFF_TABLE_SIZE]; if (inAddr > readTop - 25) { if (!unpReadBuf()) { return(false); } } AddBits((8 - inBit) & 7); long bitField = GetBits() & unchecked ((int)0xffFFffFF); if ((bitField & 0x8000) != 0) { unpBlockType = BlockTypes.BLOCK_PPM; return(ppm.DecodeInit(this, PpmEscChar)); } unpBlockType = BlockTypes.BLOCK_LZ; prevLowDist = 0; lowDistRepCount = 0; if ((bitField & 0x4000) == 0) { Utility.Fill(unpOldTable, (byte)0); // memset(UnpOldTable,0,sizeof(UnpOldTable)); } AddBits(2); for (int i = 0; i < PackDef.BC; i++) { int length = (Utility.URShift(GetBits(), 12)) & 0xFF; AddBits(4); if (length == 15) { int zeroCount = (Utility.URShift(GetBits(), 12)) & 0xFF; AddBits(4); if (zeroCount == 0) { bitLength[i] = 15; } else { zeroCount += 2; while (zeroCount-- > 0 && i < bitLength.Length) { bitLength[i++] = 0; } i--; } } else { bitLength[i] = (byte)length; } } UnpackUtility.makeDecodeTables(bitLength, 0, BD, PackDef.BC); int TableSize = PackDef.HUFF_TABLE_SIZE; for (int i = 0; i < TableSize;) { if (inAddr > readTop - 5) { if (!unpReadBuf()) { return(false); } } int Number = this.decodeNumber(BD); if (Number < 16) { table[i] = (byte)((Number + unpOldTable[i]) & 0xf); i++; } else if (Number < 18) { int N; if (Number == 16) { N = (Utility.URShift(GetBits(), 13)) + 3; AddBits(3); } else { N = (Utility.URShift(GetBits(), 9)) + 11; AddBits(7); } while (N-- > 0 && i < TableSize) { table[i] = table[i - 1]; i++; } } else { int N; if (Number == 18) { N = (Utility.URShift(GetBits(), 13)) + 3; AddBits(3); } else { N = (Utility.URShift(GetBits(), 9)) + 11; AddBits(7); } while (N-- > 0 && i < TableSize) { table[i++] = 0; } } } tablesRead = true; if (inAddr > readTop) { return(false); } UnpackUtility.makeDecodeTables(table, 0, LD, PackDef.NC); UnpackUtility.makeDecodeTables(table, PackDef.NC, DD, PackDef.DC); UnpackUtility.makeDecodeTables(table, PackDef.NC + PackDef.DC, LDD, PackDef.LDC); UnpackUtility.makeDecodeTables(table, PackDef.NC + PackDef.DC + PackDef.LDC, RD, PackDef.RC); // memcpy(unpOldTable,table,sizeof(unpOldTable)); Buffer.BlockCopy(table, 0, unpOldTable, 0, unpOldTable.Length); return(true); }