WriteInt32ToBuffer() private static method

private static WriteInt32ToBuffer ( int value, byte buffer, int index ) : void
value int
buffer byte
index int
return void
        private static void EndSubItem(SubItemToken token, ProtoWriter writer, PrefixStyle style)
        {
            if (writer == null)
            {
                throw new ArgumentNullException("writer");
            }
            if (writer.wireType != WireType.None)
            {
                throw CreateException(writer);
            }
            int value = token.value;

            if (writer.depth <= 0)
            {
                throw CreateException(writer);
            }
            if (writer.depth-- > RecursionCheckDepth)
            {
                writer.PopRecursionStack();
            }
            writer.packedFieldNumber = 0; // ending the sub-item always wipes packed encoding
            if (value < 0)
            {                             // group - very simple append
                WriteHeaderCore(-value, WireType.EndGroup, writer);
                writer.wireType = WireType.None;
                return;
            }

            // so we're backfilling the length into an existing sequence
            int len;

            switch (style)
            {
            case PrefixStyle.Fixed32:
                len = (int)((writer.ioIndex - value) - 4);
                ProtoWriter.WriteInt32ToBuffer(len, writer.ioBuffer, value);
                break;

            case PrefixStyle.Fixed32BigEndian:
                len = (int)((writer.ioIndex - value) - 4);
                byte[] buffer = writer.ioBuffer;
                ProtoWriter.WriteInt32ToBuffer(len, buffer, value);
                // and swap the byte order
                byte b = buffer[value];
                buffer[value]     = buffer[value + 3];
                buffer[value + 3] = b;
                b = buffer[value + 1];
                buffer[value + 1] = buffer[value + 2];
                buffer[value + 2] = b;
                break;

            case PrefixStyle.Base128:
                // string - complicated because we only reserved one byte;
                // if the prefix turns out to need more than this then
                // we need to shuffle the existing data
                len = (int)((writer.ioIndex - value) - 1);
                int  offset = 0;
                uint tmp    = (uint)len;
                while ((tmp >>= 7) != 0)
                {
                    offset++;
                }
                if (offset == 0)
                {
                    writer.ioBuffer[value] = (byte)(len & 0x7F);
                }
                else
                {
                    DemandSpace(offset, writer);
                    byte[] blob = writer.ioBuffer;
                    Helpers.BlockCopy(blob, value + 1, blob, value + 1 + offset, len);
                    tmp = (uint)len;
                    do
                    {
                        blob[value++] = (byte)((tmp & 0x7F) | 0x80);
                    } while ((tmp >>= 7) != 0);
                    blob[value - 1]  = (byte)(blob[value - 1] & ~0x80);
                    writer.position += offset;
                    writer.ioIndex  += offset;
                }
                break;

            default:
                throw new ArgumentOutOfRangeException("style");
            }
            // and this object is no longer a blockage - also flush if sensible
            const int ADVISORY_FLUSH_SIZE = 1024;

            if (--writer.flushLock == 0 && writer.ioIndex >= ADVISORY_FLUSH_SIZE)
            {
                ProtoWriter.Flush(writer);
            }
        }
Exemplo n.º 2
0
        public static void WriteInt32(int value, ProtoWriter writer)
        {
            byte[] numArray;
            int    num;

            if (writer == null)
            {
                throw new ArgumentNullException("writer");
            }
            ProtoBuf.WireType wireType = writer.wireType;
            switch (wireType)
            {
            case ProtoBuf.WireType.Variant:
            {
                if (value >= 0)
                {
                    ProtoWriter.WriteUInt32Variant((uint)value, writer);
                    writer.wireType = ProtoBuf.WireType.None;
                    return;
                }
                ProtoWriter.DemandSpace(10, writer);
                numArray          = writer.ioBuffer;
                num               = writer.ioIndex;
                numArray[num]     = (byte)(value | 128);
                numArray[num + 1] = (byte)(value >> 7 | 128);
                numArray[num + 2] = (byte)(value >> 14 | 128);
                numArray[num + 3] = (byte)(value >> 21 | 128);
                numArray[num + 4] = (byte)(value >> 28 | 128);
                int  num1 = 255;
                byte num2 = (byte)num1;
                numArray[num + 8] = (byte)num1;
                byte num3 = num2;
                byte num4 = num3;
                numArray[num + 7] = num3;
                byte num5 = num4;
                byte num6 = num5;
                numArray[num + 6] = num5;
                numArray[num + 5] = num6;
                numArray[num + 9] = 1;
                ProtoWriter.IncrementedAndReset(10, writer);
                return;
            }

            case ProtoBuf.WireType.Fixed64:
            {
                ProtoWriter.DemandSpace(8, writer);
                numArray          = writer.ioBuffer;
                num               = writer.ioIndex;
                numArray[num]     = (byte)value;
                numArray[num + 1] = (byte)(value >> 8);
                numArray[num + 2] = (byte)(value >> 16);
                numArray[num + 3] = (byte)(value >> 24);
                int  num7 = 0;
                byte num8 = (byte)num7;
                numArray[num + 7] = (byte)num7;
                byte num9  = num8;
                byte num10 = num9;
                numArray[num + 6] = num9;
                byte num11 = num10;
                byte num12 = num11;
                numArray[num + 5] = num11;
                numArray[num + 4] = num12;
                ProtoWriter.IncrementedAndReset(8, writer);
                return;
            }

            default:
            {
                if (wireType == ProtoBuf.WireType.Fixed32)
                {
                    break;
                }
                else
                {
                    if (wireType != ProtoBuf.WireType.SignedVariant)
                    {
                        throw ProtoWriter.CreateException(writer);
                    }
                    ProtoWriter.WriteUInt32Variant(ProtoWriter.Zig(value), writer);
                    writer.wireType = ProtoBuf.WireType.None;
                    return;
                }
            }
            }
            ProtoWriter.DemandSpace(4, writer);
            ProtoWriter.WriteInt32ToBuffer(value, writer.ioBuffer, writer.ioIndex);
            ProtoWriter.IncrementedAndReset(4, writer);
        }
Exemplo n.º 3
0
        private static void EndSubItem(SubItemToken token, ProtoWriter writer, PrefixStyle style)
        {
            int    num;
            UInt32 num1;

            if (writer == null)
            {
                throw new ArgumentNullException("writer");
            }
            if (writer.wireType != ProtoBuf.WireType.None)
            {
                throw ProtoWriter.CreateException(writer);
            }
            int num2 = token.@value;

            if (writer.depth <= 0)
            {
                throw ProtoWriter.CreateException(writer);
            }
            ProtoWriter protoWriter = writer;
            int         num3        = protoWriter.depth;
            int         num4        = num3;

            protoWriter.depth = num3 - 1;
            if (num4 > 25)
            {
                writer.PopRecursionStack();
            }
            writer.packedFieldNumber = 0;
            if (num2 < 0)
            {
                ProtoWriter.WriteHeaderCore(-num2, ProtoBuf.WireType.EndGroup, writer);
                writer.wireType = ProtoBuf.WireType.None;
                return;
            }
            switch (style)
            {
            case PrefixStyle.Base128:
            {
                num = writer.ioIndex - num2 - 1;
                int  num5 = 0;
                uint num6 = (uint)num;
                while (true)
                {
                    UInt32 num7 = num6 >> 7;
                    num6 = num7;
                    if (num7 == 0)
                    {
                        break;
                    }
                    num5++;
                }
                if (num5 != 0)
                {
                    ProtoWriter.DemandSpace(num5, writer);
                    byte[] numArray = writer.ioBuffer;
                    Helpers.BlockCopy(numArray, num2 + 1, numArray, num2 + 1 + num5, num);
                    num6 = (uint)num;
                    do
                    {
                        int num8 = num2;
                        num2           = num8 + 1;
                        numArray[num8] = (byte)(num6 & 127 | 128);
                        num1           = num6 >> 7;
                        num6           = num1;
                    }while (num1 != 0);
                    numArray[num2 - 1] = (byte)(numArray[num2 - 1] & -129);
                    writer.position   += num5;
                    writer.ioIndex    += num5;
                    break;
                }
                else
                {
                    writer.ioBuffer[num2] = (byte)(num & 127);
                    break;
                }
            }

            case PrefixStyle.Fixed32:
            {
                num = writer.ioIndex - num2 - 4;
                ProtoWriter.WriteInt32ToBuffer(num, writer.ioBuffer, num2);
                break;
            }

            case PrefixStyle.Fixed32BigEndian:
            {
                num = writer.ioIndex - num2 - 4;
                byte[] numArray1 = writer.ioBuffer;
                ProtoWriter.WriteInt32ToBuffer(num, numArray1, num2);
                byte num9 = numArray1[num2];
                numArray1[num2]     = numArray1[num2 + 3];
                numArray1[num2 + 3] = num9;
                num9 = numArray1[num2 + 1];
                numArray1[num2 + 1] = numArray1[num2 + 2];
                numArray1[num2 + 2] = num9;
                break;
            }

            default:
            {
                throw new ArgumentOutOfRangeException("style");
            }
            }
            ProtoWriter protoWriter1 = writer;
            int         num10        = protoWriter1.flushLock - 1;
            int         num11        = num10;

            protoWriter1.flushLock = num10;
            if (num11 == 0 && writer.ioIndex >= 1024)
            {
                ProtoWriter.Flush(writer);
            }
        }
Exemplo n.º 4
0
        public static void WriteInt32(int value, ProtoWriter writer)
        {
            if (writer == null)
            {
                throw new ArgumentNullException("writer");
            }
            WireType wireType = writer.wireType;

            switch (wireType)
            {
            case WireType.Variant:
                if (value >= 0)
                {
                    ProtoWriter.WriteUInt32Variant((uint)value, writer);
                    writer.wireType = WireType.None;
                }
                else
                {
                    ProtoWriter.DemandSpace(10, writer);
                    byte[] array = writer.ioBuffer;
                    int    num   = writer.ioIndex;
                    array[num]     = (byte)(value | 128);
                    array[num + 1] = (byte)(value >> 7 | 128);
                    array[num + 2] = (byte)(value >> 14 | 128);
                    array[num + 3] = (byte)(value >> 21 | 128);
                    array[num + 4] = (byte)(value >> 28 | 128);
                    array[num + 5] = (array[num + 6] = (array[num + 7] = (array[num + 8] = 255)));
                    array[num + 9] = 1;
                    ProtoWriter.IncrementedAndReset(10, writer);
                }
                return;

            case WireType.Fixed64:
            {
                ProtoWriter.DemandSpace(8, writer);
                byte[] array = writer.ioBuffer;
                int    num   = writer.ioIndex;
                array[num]     = (byte)value;
                array[num + 1] = (byte)(value >> 8);
                array[num + 2] = (byte)(value >> 16);
                array[num + 3] = (byte)(value >> 24);
                array[num + 4] = (array[num + 5] = (array[num + 6] = (array[num + 7] = 0)));
                ProtoWriter.IncrementedAndReset(8, writer);
                return;
            }

            case WireType.String:
            case WireType.StartGroup:
            case WireType.EndGroup:
IL_36:
                if (wireType != WireType.SignedVariant)
                {
                    throw ProtoWriter.CreateException(writer);
                }
                ProtoWriter.WriteUInt32Variant(ProtoWriter.Zig(value), writer);
                writer.wireType = WireType.None;
                return;

            case WireType.Fixed32:
                ProtoWriter.DemandSpace(4, writer);
                ProtoWriter.WriteInt32ToBuffer(value, writer.ioBuffer, writer.ioIndex);
                ProtoWriter.IncrementedAndReset(4, writer);
                return;
            }
            goto IL_36;
        }
Exemplo n.º 5
0
        private static void EndSubItem(SubItemToken token, ProtoWriter writer, PrefixStyle style)
        {
            if (writer == null)
            {
                throw new ArgumentNullException("writer");
            }
            if (writer.wireType != WireType.None)
            {
                throw ProtoWriter.CreateException(writer);
            }
            int value = token.value;

            if (writer.depth <= 0)
            {
                throw ProtoWriter.CreateException(writer);
            }
            if (writer.depth-- > 25)
            {
                writer.PopRecursionStack();
            }
            writer.packedFieldNumber = 0;
            if (value < 0)
            {
                ProtoWriter.WriteHeaderCore(-value, WireType.EndGroup, writer);
                writer.wireType = WireType.None;
                return;
            }
            switch (style)
            {
            case PrefixStyle.Base128:
            {
                int  num  = writer.ioIndex - value - 1;
                int  num2 = 0;
                uint num3 = (uint)num;
                while ((num3 >>= 7) != 0u)
                {
                    num2++;
                }
                if (num2 == 0)
                {
                    writer.ioBuffer[value] = (byte)(num & 127);
                }
                else
                {
                    ProtoWriter.DemandSpace(num2, writer);
                    byte[] array = writer.ioBuffer;
                    Helpers.BlockCopy(array, value + 1, array, value + 1 + num2, num);
                    num3 = (uint)num;
                    do
                    {
                        array[value++] = (byte)((num3 & 127u) | 128u);
                    }while ((num3 >>= 7) != 0u);
                    array[value - 1] = (byte)((int)array[value - 1] & -129);
                    writer.position += num2;
                    writer.ioIndex  += num2;
                }
                break;
            }

            case PrefixStyle.Fixed32:
            {
                int num = writer.ioIndex - value - 4;
                ProtoWriter.WriteInt32ToBuffer(num, writer.ioBuffer, value);
                break;
            }

            case PrefixStyle.Fixed32BigEndian:
            {
                int    num    = writer.ioIndex - value - 4;
                byte[] array2 = writer.ioBuffer;
                ProtoWriter.WriteInt32ToBuffer(num, array2, value);
                byte b = array2[value];
                array2[value]     = array2[value + 3];
                array2[value + 3] = b;
                b = array2[value + 1];
                array2[value + 1] = array2[value + 2];
                array2[value + 2] = b;
                break;
            }

            default:
                throw new ArgumentOutOfRangeException("style");
            }
            if (--writer.flushLock == 0 && writer.ioIndex >= 1024)
            {
                ProtoWriter.Flush(writer);
            }
        }
Exemplo n.º 6
0
        // Token: 0x06000160 RID: 352 RVA: 0x0000DAB0 File Offset: 0x0000BCB0
        public static void WriteInt32(int value, ProtoWriter writer)
        {
            if (writer == null)
            {
                throw new ArgumentNullException("writer");
            }
            WireType wireType = writer.wireType;

            if (wireType <= WireType.Fixed64)
            {
                if (wireType != WireType.Variant)
                {
                    if (wireType == WireType.Fixed64)
                    {
                        ProtoWriter.DemandSpace(8, writer);
                        byte[] array = writer.ioBuffer;
                        int    num   = writer.ioIndex;
                        array[num]     = (byte)value;
                        array[num + 1] = (byte)(value >> 8);
                        array[num + 2] = (byte)(value >> 16);
                        array[num + 3] = (byte)(value >> 24);
                        array[num + 4] = (array[num + 5] = (array[num + 6] = (array[num + 7] = 0)));
                        ProtoWriter.IncrementedAndReset(8, writer);
                        return;
                    }
                }
                else
                {
                    if (value >= 0)
                    {
                        ProtoWriter.WriteUInt32Variant((uint)value, writer);
                        writer.wireType = WireType.None;
                        return;
                    }
                    ProtoWriter.DemandSpace(10, writer);
                    byte[] array = writer.ioBuffer;
                    int    num   = writer.ioIndex;
                    array[num]     = (byte)(value | 128);
                    array[num + 1] = (byte)(value >> 7 | 128);
                    array[num + 2] = (byte)(value >> 14 | 128);
                    array[num + 3] = (byte)(value >> 21 | 128);
                    array[num + 4] = (byte)(value >> 28 | 128);
                    array[num + 5] = (array[num + 6] = (array[num + 7] = (array[num + 8] = byte.MaxValue)));
                    array[num + 9] = 1;
                    ProtoWriter.IncrementedAndReset(10, writer);
                    return;
                }
            }
            else
            {
                if (wireType == WireType.Fixed32)
                {
                    ProtoWriter.DemandSpace(4, writer);
                    ProtoWriter.WriteInt32ToBuffer(value, writer.ioBuffer, writer.ioIndex);
                    ProtoWriter.IncrementedAndReset(4, writer);
                    return;
                }
                if (wireType == WireType.SignedVariant)
                {
                    ProtoWriter.WriteUInt32Variant(ProtoWriter.Zig(value), writer);
                    writer.wireType = WireType.None;
                    return;
                }
            }
            throw ProtoWriter.CreateException(writer);
        }