Esempio n. 1
0
        void saveFile(int fileID)
        {
            if (fileID > 0x16D)
            {
                return;
            }
            int ovrA     = fileID << 5;
            int srcStart = Bits.getInt32(ovr, ovrA + 4) & 0x1FFFFFF;
            int srcSize  = Bits.getInt32(ovr, ovrA + 8);            // & 0x1FFFFFF;
            int srcEnd   = srcStart + srcSize;
            int desSize  = srcSize + (srcSize >> 3) + 7;            //Absolute maximum possible size... (Including header and end of data command.)

            desSize = (desSize + 0x1FF) & -0x200;
            byte[] src    = ram;         // new byte[srcSize];
            int    srcPos = srcStart;

            byte[] des    = new byte[desSize];
            int    desPos = 0;

            des[desPos++] = 0x40;
            des[desPos++] = (byte)srcSize;
            des[desPos++] = (byte)(srcSize >> 8);
            des[desPos++] = (byte)(srcSize >> 16);
            int fCur  = 0x80;
            int fAddr = desPos++;
            int flags = 0;

            while (srcPos < srcEnd)
            {
                int dist     = 0;
                int len      = 0;
                int winStart = Math.Max(srcStart, srcPos - 0xFFF);
                for (int i = winStart; i < srcPos; i++)
                {
                    for (int j = 1; j < 0x10110; j++)
                    {
                        if (src[srcPos + j] == src[i + j])
                        {
                            //if ((j + 1) >= len)
                            if (j >= len)
                            {
                                dist = i;
                                //len = j + 1;
                                len = j;
                            }
                        }
                        else
                        {
                            break;
                        }
                    }
                    if (src[srcPos] == src[i])
                    {
                        len += 1;
                    }
                    else
                    {
                        dist += 1;
                    }
                }
                if (src[srcPos] != src[dist])                 //Insert byte
                {
                    des[desPos++] = src[srcPos++];
                    fCur        >>= 1;
                    if (fCur == 0)
                    {
                        des[fAddr] = (byte)-flags;
                        fAddr      = desPos++;
                        fCur       = 0x80;
                        flags      = 0;
                    }
                }
                dist = srcPos - dist;
                if (len < 2)
                {
                    des[desPos++] = src[srcPos]; len = 1;
                }
                else if (len < 0x10)
                {
                    des[desPos++] = (byte)((dist << 4) | len);
                    des[desPos++] = (byte)(dist >> 4);
                }
                else if (len < 0x110)
                {
                    des[desPos++] = (byte)((dist << 4) | 0);
                    des[desPos++] = (byte)(dist >> 4);
                    des[desPos++] = (byte)(len - 0x10);
                }
                else                 // if (len < 0x10110)
                {
                    des[desPos++] = (byte)((dist << 4) | 1);
                    des[desPos++] = (byte)(dist >> 4);
                    des[desPos++] = (byte)(len - 0x110);
                    des[desPos++] = (byte)((len - 0x110) >> 8);
                }
                srcPos += len;
                if (len > 1)
                {
                    flags |= fCur;
                }

                fCur >>= 1;
                if (fCur == 0)
                {
                    des[fAddr] = (byte)-flags;
                    fAddr      = desPos++;
                    fCur       = 0x80;
                    flags      = 0;
                }
            }
            des[desPos++] = 0;
            des[desPos++] = 0;
            flags        |= fCur;
            des[fAddr]    = (byte)-flags;

            //Now to save to ROM.
            Bits.saveFile(@"C:\Users\Tea\Desktop\notoriginal.bin", des);
            return;

            int fatA = fileID << 3;
            int fat1 = Bits.getInt32(fat, fatA);

            Bits.setInt32(fat, fatA + 4, fat1 + desPos);
            //int fat2 = Bits.getInt32(fat, fatA + 4);
            int fat2 = Bits.getInt32(fat, fatA + 8);
            int size = fat2 - fat1;

            //For neatness....
            while (desPos < size)
            {
                des[desPos++] = 0xFF;
            }
            if (desPos != size)
            {
                MessageBox.Show("About to lose data... Have fun!");
            }
            Bits.saveFilePart(path, fat1, size, des);
        }