示例#1
0
        private void BkDecrunch(string infile, string outfile)
        {
            SourceBuffer = File.ReadAllBytes(infile);
            uint len = ReadUInt32(SourceBuffer, 4);

            SourcePtr = ReadUInt32(SourceBuffer, 0) + Headersize;
            Checksum  = ReadUInt32(SourceBuffer, 8);
            BkNextBitStream();

            do
            {
                if (BkNextBit() == 1)
                {
                    uint type = BkReadBits(2);
                    if (type < 2)
                    {
                        BkDoDuplicate(type + 9, type + 2);
                    }
                    else if (type == 3)
                    {
                        BkDoJmp(8, 8);
                    }
                    else
                    {
                        BkDoDuplicate(12, BkReadBits(8));
                    }
                }
                else
                {
                    if (BkNextBit() == 1)
                    {
                        BkDoDuplicate(8, 1);
                    }
                    else
                    {
                        BkDoJmp(3, 0);
                    }
                }
            }while (ResultBuffer.Count < len);
            File.WriteAllBytes(outfile, ResultBuffer.ToArray());
        }
示例#2
0
        private void BkCrunch(string infile, string outfile, uint scanWidth)
        {
            SourceBuffer = File.ReadAllBytes(infile);
            uint scanend = 0, bestlen = 0, scanptr = 0, copylen = 0, copytype = 0, besttype = 0, dumpcnt = 0;
            uint copyoffs = 0, bestoffs = 0;

            while (SourcePtr < SourceBuffer.Length)
            {
                scanend = (SourcePtr + scanWidth > (uint)SourceBuffer.Length) ? (uint)SourceBuffer.Length - 1 : SourcePtr + scanWidth;
                bestlen = 1;
                scanptr = SourcePtr + 1;

                while (scanptr < scanend)
                {
                    if (SourceBuffer[SourcePtr] == SourceBuffer[scanptr] && SourceBuffer[SourcePtr + 1] == SourceBuffer[scanptr + 1])
                    {
                        while ((scanptr + copylen) < scanend && SourceBuffer[SourcePtr + copylen] == SourceBuffer[scanptr + copylen])
                        {
                            copylen++;
                        }

                        if (copylen > bestlen)
                        {
                            copyoffs = scanptr - SourcePtr;
                            if (copylen > 4)
                            {
                                copytype = 3;
                                copylen  = copylen > 0x100 ? 0x100 : copylen;
                            }
                            else
                            {
                                copytype = copylen - 2;
                            }

                            if (copyoffs < MaxOffsets[copytype])
                            {
                                bestlen  = copylen;
                                bestoffs = copyoffs;
                                besttype = copytype;
                            }
                        }

                        scanptr += copylen;
                        copylen  = 0;
                    }
                    else
                    {
                        scanptr++;
                    }
                }

                if (bestlen > 1)
                {
                    BkDump(dumpcnt);
                    dumpcnt = 0;
                    BkWriteBits(OffsBits[besttype], bestoffs);
                    if (besttype == 3)
                    {
                        BkWriteBits(8, bestlen - 1);
                    }
                    BkWriteBits(CmdBits[besttype], CmdWord[besttype]);
                    SourcePtr += bestlen;
                }
                else
                {
                    BkWriteBits(8, SourceBuffer[SourcePtr++]);
                    if (++dumpcnt >= 0x108)
                    {
                        BkDump(dumpcnt);
                        dumpcnt = 0;
                    }
                }
            }

            BkDump(dumpcnt);
            BitStream |= Convert.ToUInt32(1L << (32 - FreeBits));
            BkWriteBitStream();

            InsertIntoResultBuffer(0, Checksum);
            InsertIntoResultBuffer(0, (uint)SourceBuffer.Length);
            InsertIntoResultBuffer(0, (uint)ResultBuffer.Count - 8);

            File.WriteAllBytes(outfile, ResultBuffer.ToArray());
        }