Пример #1
0
        private static LinkedNode Decode(BitStream Input, LinkedNode Head)
        {
            LinkedNode node = Head;

            while (node.Child0 != null)
            {
                int bit = Input.ReadBits(1);
                if (bit == -1)
                {
                    throw new MpqParserException("Unexpected end of file");
                }

                node = bit == 0 ? node.Child0 : node.Child1;
            }
            return(node);
        }
Пример #2
0
        // Return values:
        // 0x000 - 0x0FF : One byte from compressed file.
        // 0x100 - 0x305 : Copy previous block (0x100 = 1 byte)
        // -1            : EOF
        private int DecodeLit()
        {
            switch (mStream.ReadBits(1))
            {
            case -1:
                return(-1);

            case 1:
                // The next bits are position in buffers
                int pos = sPosition2[mStream.PeekByte()];

                // Skip the bits we just used
                if (mStream.ReadBits(sLenBits[pos]) == -1)
                {
                    return(-1);
                }

                int nbits = sExLenBits[pos];
                if (nbits != 0)
                {
                    // TODO: Verify this conversion
                    int val2 = mStream.ReadBits(nbits);
                    if (val2 == -1 && (pos + val2 != 0x10e))
                    {
                        return(-1);
                    }

                    pos = sLenBase[pos] + val2;
                }
                return(pos + 0x100);                        // Return number of bytes to repeat

            case 0:
                if (mCType == CompressionType.Binary)
                {
                    return(mStream.ReadBits(8));
                }

                // TODO: Text mode
                throw new NotImplementedException("Text mode is not yet implemented");

            default:
                return(0);
            }
        }
Пример #3
0
        public static byte[] Decompress(Stream Data)
        {
            int comptype = Data.ReadByte();

            if (comptype == 0)
            {
                throw new NotImplementedException("Compression type 0 is not currently supported");
            }

            LinkedNode tail = BuildList(sPrime[comptype]);
            LinkedNode head = BuildTree(tail);

            MemoryStream outputstream = new MemoryStream();
            BitStream    bitstream    = new BitStream(Data);
            int          decoded;

            do
            {
                LinkedNode node = Decode(bitstream, head);
                decoded = node.DecompressedValue;
                switch (decoded)
                {
                case 256:
                    break;

                case 257:
                    int newvalue = bitstream.ReadBits(8);
                    outputstream.WriteByte((byte)newvalue);
                    tail = InsertNode(tail, newvalue);
                    break;

                default:
                    outputstream.WriteByte((byte)decoded);
                    break;
                }
            } while (decoded != 256);

            return(outputstream.ToArray());
        }
Пример #4
0
		private static LinkedNode Decode(BitStream Input, LinkedNode Head)
		{
			LinkedNode node = Head;

			while(node.Child0 != null)
			{
				int bit = Input.ReadBits(1);
				if (bit == -1)
                    throw new MpqParserException("Unexpected end of file");

				node = bit == 0 ? node.Child0 : node.Child1;
			}
			return node;
		}
Пример #5
0
		public static byte[] Decompress(Stream Data)
		{
			int comptype = Data.ReadByte();

			if (comptype == 0)
				throw new NotImplementedException("Compression type 0 is not currently supported");

			LinkedNode tail = BuildList(sPrime[comptype]);
			LinkedNode head = BuildTree(tail);

			MemoryStream outputstream = new MemoryStream();
			BitStream bitstream = new BitStream(Data);
			int decoded;
			do
			{
				LinkedNode node = Decode(bitstream, head);
				decoded = node.DecompressedValue;
				switch(decoded)
				{
					case 256:
						break;
					case 257:
						int newvalue = bitstream.ReadBits(8);
						outputstream.WriteByte((byte)newvalue);
						tail = InsertNode(tail, newvalue);
						break;
					default:
						outputstream.WriteByte((byte)decoded);
						break;
				}
			} while (decoded != 256);
			
			return outputstream.ToArray();
		}