Example #1
0
        public static Int64 Decode(DecodeBuffer input)
        {
            // This was difficult to get right, translated from https://github.com/protocolbuffers/protobuf/blob/master/src/google/protobuf/wire_format_lite.h
            var n = UnsignedVlq.Decode(input);

            return((Int64)((n >> 1) ^ (~(n & 1) + 1)));
        }
Example #2
0
        public override Object Decode(DecodeBuffer input)
        {
            throw new NotImplementedException();

            /*  // Read header
             *  var header = UnsignedVlq.Decode(input);
             *
             *  // Handle nulls
             *  if (header == 0)
             *  {
             *      return null;
             *  }
             *
             *  // Determine length
             *  var count = (Int32)header - 1;
             *
             *  // Instantiate list
             *  var output = (IList)Activator.CreateInstance(type); //typeof(List<>).MakeGenericType(type.GenericTypeArguments)
             *
             *  // Deserialize until we reach length limit
             *  for (var i = 0; i < count; i++)
             *  {
             *      // Deserialize element
             *      var element = valueDecoder.DynamicInvokeTransparent(input);
             *
             *      // Add to output
             *      output.Add(element);
             *  }
             *
             *  return output;*/
        }
Example #3
0
        /// <summary>
        /// Unpacks next section of the compressed replay
        /// </summary>
        /// <param name="length">The length of the section</param>
        /// <returns>The decoded data</returns>
        protected byte[] UnpackNextSection(int length)
        {
            byte[] result = new byte[length];

            Decoder decoder = new Decoder();
            DecodeBuffer buffer = new DecodeBuffer(result);

            decoder.DecodeBuffer = buffer;

            int checksum = _reader.ReadInt32();
            int blocks = _reader.ReadInt32();

            for (int block = 0; block < blocks; block++)
            {

                // read the length of the next block of encoded data
                int encodedLength = _reader.ReadInt32();

                // we error if there is no space for the next block of encoded data
                if (encodedLength > result.Length - buffer.ResultOffset)
                {
                    throw new Exception("Insufficient space in decode buffer");
                }

                // read the block of encoded data into the result (decoded data will overwrite).
                _reader.Read(result, buffer.ResultOffset, encodedLength);

                // skip decoding if the encoded data filled the remaining space
                if (encodedLength == Math.Min(result.Length - buffer.ResultOffset, buffer.BufferLength))
                {
                    continue;
                }

                // set the decode buffer parameters
                buffer.EncodedOffset = 0;
                buffer.DecodedLength = 0;
                buffer.EncodedLength = encodedLength;

                // decode the block
                if (decoder.DecodeBlock() != 0)
                {
                    throw new Exception("Error decoding block offset " + block);
                }

                // sanity check the decoded length
                if (buffer.DecodedLength == 0 ||
                        buffer.DecodedLength > buffer.BufferLength ||
                        buffer.DecodedLength > result.Length)
                {
                    throw new Exception("Decode data length mismatch");
                }

                // flush the decoded bytes into the result
                buffer.WriteDecodedBytes();
            }

            return result;
        }
Example #4
0
        public override Object Decode(DecodeBuffer input)
        {
            var v = input.ReadByte();

            switch (v)
            {
            case Zero: return(false);

            case One: return(true);

            case Two: return(null);

            default: throw new SchemaMismatchException($"Boolean value must be either {Zero}, {One} or {Two}, but {v} given.");
            }
        }
Example #5
0
        public override Object Decode(DecodeBuffer input)
        {
            var v = SignedVlq.Decode(input);

            if (IsNullable)
            {
                if (v == 0)
                {
                    return(null);
                }
                v--;
            }
            v *= Increment;

            return(Convert.ChangeType(v, UnderlyingType));
        }
Example #6
0
        public override Object Decode(DecodeBuffer input)
        {
            // Decode header
            var header = UnsignedVlq.Decode(input);

            // Handle nulls
            if (IsNullable)
            {
                if (header == 0)
                {
                    return(null);
                }
                header--;
            }

            // Decode value
            return(Encoding.UTF8.GetString(input.Underlying, input.GetIncrementOffset((Int32)header), (Int32)header));
        }
Example #7
0
        public static UInt64 Decode(DecodeBuffer input)
        {
#if DEBUG
            if (null == input)
            {
                throw new ArgumentNullException(nameof(input));
            }
#endif


            // Setup symbol
            UInt64 symbol = 0;
            var    bit    = 0;

            Int32 b;
            do
            {
                // Read byte
                b = input.ReadByte();

                // Add input bits to output
                var chunk = (UInt64)(b & Mask);
                var pre   = symbol;
                symbol += (chunk + 1) << bit;

#if DEBUG
                // Check for overflow
                if (symbol < pre)
                {
                    throw new OverflowException($"Symbol is corrupt, or larger than maximum supported value {UnsignedVlq.MaxValue}.");
                }
#endif

                // Increment bit offset
                bit += PacketSize;
            } while ((b & More) > 0); // If not final byte

            // Remove zero offset
            symbol--;

            // Add to output
            return(symbol);
        }
Example #8
0
        public override Object Decode(DecodeBuffer input)
        {
#if DEBUG
            if (null == Records)
            {
                throw new InvalidOperationException("Coder has not yet been prepared.");
            }
#endif

            // Read the length header
            var header = (Int32)UnsignedVlq.Decode(input);

            // Handle nulls
            if (IsNullable)
            {
                if (header == 0)
                {
                    return(null);
                }
                header--;
            }

            // Instantiate output
            var output = Activator.CreateInstance(UnderlyingType);

            // Extract an inner buffer so that if fields are added to the class in the future we ignore them, being backwards compatible
            var innerInput = input.Extract(header);

            // Isolate bytes for body
            foreach (var record in Records)
            {
                // Deserialize value
                var v = record.Coder.Decode(innerInput);

                // Set it on property
                record.Field.SetValue(output, v);
            }

            return(output);
        }
Example #9
0
        public override Object Decode(DecodeBuffer input)
        {
            throw new NotImplementedException();

            /*// Get deserializer for sub items
             * var keyDecoder = recurse(type.GenericTypeArguments[0]);
             * var valueDecoder = recurse(type.GenericTypeArguments[1]);
             *
             * return new Func<DecodeBuffer, IDictionary>(input =>
             * {
             *  // Read header
             *  var header = UnsignedVlq.Decode(input);
             *
             *  if (header == 0)
             *  {
             *      return null;
             *  }
             *
             *  // Get count
             *  var count = (Int32)header - 1;
             *
             *  // Instantiate dictionary
             *  var output = (IDictionary)Activator.CreateInstance(type);
             *
             *  // Loop through input buffer until depleted
             *  for (var i = 0; i < count; i++)
             *  {
             *      // Deserialize key
             *      var keyValue = keyDecoder.DynamicInvokeTransparent(input);
             *
             *      // Deserialize value
             *      var valueValue = valueDecoder.DynamicInvokeTransparent(input);
             *
             *      // Add to output
             *      output[keyValue] = valueValue;
             *  }
             *
             *  return output;
             * });*/
        }
Example #10
0
 public abstract Object Decode(DecodeBuffer input);
Example #11
0
 public override Object Decode(DecodeBuffer input)
 {
     throw new NotImplementedException();
 }