Пример #1
0
        public static byte[] Encode(byte[] source, int format)
        {
            var dest = new byte[source.Length * 2];
            var src  = new MemoryFile(source);

            int w = 0;

            while (!src.Eof)
            {
                var  cb_in     = (short)Math.Min(src.Remaining, 8192);
                var  chunk_in  = src.Read(cb_in);
                var  chunk_out = format == 80 ? Format80.Encode(chunk_in) : EncodeSection(chunk_in);
                uint cb_out    = (ushort)chunk_out.Length;

                Array.Copy(BitConverter.GetBytes(cb_out), 0, dest, w, 2);
                w += 2;
                Array.Copy(BitConverter.GetBytes(cb_in), 0, dest, w, 2);
                w += 2;
                Array.Copy(chunk_out, 0, dest, w, chunk_out.Length);
                w += chunk_out.Length;
            }
            Array.Resize(ref dest, w);
            return(dest);
        }
Пример #2
0
        public static int DecodeInto(byte[] src, byte[] dest)
        {
            VirtualFile ctx       = new MemoryFile(src);
            int         destIndex = 0;

            while (true)
            {
                byte i = ctx.ReadByte();
                if ((i & 0x80) == 0)
                {
                    // case 2
                    byte secondByte = ctx.ReadByte();
                    int  count      = ((i & 0x70) >> 4) + 3;
                    int  rpos       = ((i & 0xf) << 8) + secondByte;

                    ReplicatePrevious(dest, destIndex, destIndex - rpos, count);
                    destIndex += count;
                }
                else if ((i & 0x40) == 0)
                {
                    // case 1
                    int count = i & 0x3F;
                    if (count == 0)
                    {
                        return(destIndex);
                    }

                    ctx.Read(dest, destIndex, count);
                    destIndex += count;
                }
                else
                {
                    int count3 = i & 0x3F;
                    if (count3 == 0x3E)
                    {
                        // case 4
                        int  count = ctx.ReadInt16();
                        byte color = ctx.ReadByte();

                        for (int end = destIndex + count; destIndex < end; destIndex++)
                        {
                            dest[destIndex] = color;
                        }
                    }
                    else if (count3 == 0x3F)
                    {
                        // case 5
                        int count    = ctx.ReadInt16();
                        int srcIndex = ctx.ReadInt16();
                        if (srcIndex >= destIndex)
                        {
                            throw new NotImplementedException(string.Format("srcIndex >= destIndex  {0}  {1}", srcIndex, destIndex));
                        }

                        for (int end = destIndex + count; destIndex < end; destIndex++)
                        {
                            dest[destIndex] = dest[srcIndex++];
                        }
                    }
                    else
                    {
                        // case 3
                        int count    = count3 + 3;
                        int srcIndex = ctx.ReadInt16();
                        if (srcIndex >= destIndex)
                        {
                            throw new NotImplementedException(string.Format("srcIndex >= destIndex  {0}  {1}", srcIndex, destIndex));
                        }

                        for (int end = destIndex + count; destIndex < end; destIndex++)
                        {
                            dest[destIndex] = dest[srcIndex++];
                        }
                    }
                }
            }
        }