OpenMetaverse.Helpers.ZeroDecode C# (CSharp) Method

ZeroDecode() public static method

Decode a zerocoded byte array, used to decompress packets marked with the zerocoded flag
Any time a zero is encountered, the next byte is a count of how many zeroes to expand. One zero is encoded with 0x00 0x01, two zeroes is 0x00 0x02, three zeroes is 0x00 0x03, etc. The first four bytes are copied directly to the output buffer.
public static ZeroDecode ( byte src, int srclen, byte dest ) : int
src byte The byte array to decode
srclen int The length of the byte array to decode. This /// would be the length of the packet up to (but not including) any /// appended ACKs
dest byte The output byte array to decode to
return int
        public static int ZeroDecode(byte[] src, int srclen, byte[] dest)
        {
            if (srclen > src.Length)
                throw new ArgumentException("srclen cannot be greater than src.Length");

            uint zerolen = 0;
            int bodylen = 0;
            uint i = 0;

            try
            {
                Buffer.BlockCopy(src, 0, dest, 0, 6);
                zerolen = 6;
                bodylen = srclen;

                for (i = zerolen; i < bodylen; i++)
                {
                    if (src[i] == 0x00)
                    {
                        for (byte j = 0; j < src[i + 1]; j++)
                        {
                            dest[zerolen++] = 0x00;
                        }

                        i++;
                    }
                    else
                    {
                        dest[zerolen++] = src[i];
                    }
                }

                // Copy appended ACKs
                for (; i < srclen; i++)
                {
                    dest[zerolen++] = src[i];
                }

                return (int)zerolen;
            }
            catch (Exception)
            {
                Logger.Log(String.Format("Zerodecoding error: i={0}, srclen={1}, bodylen={2}, zerolen={3}\n{4}",
                    i, srclen, bodylen, zerolen, Utils.BytesToHexString(src, srclen, null)), LogLevel.Error);
            }

            return 0;
        }