DerrickBrayanClayton / protobuf-net

Automatically exported from code.google.com/p/protobuf-net
0 stars 0 forks source link

TryReadLengthPrefix gives bad size for ProtoBuf.PrefixStyle.Base128 #80

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
int len;

ProtoBuf.Serializer.SerializeWithLengthPrefix<Message>(stream, message,
ProtoBuf.PrefixStyle.Base128);

bool xap =
ProtoBuf.Serializer.TryReadLengthPrefix(textStream.GetBuffer(),0,4,ProtoBuf.Pref
ixStyle.Base128,
out len);

What steps will reproduce the problem?
1. Use tryReadLengthPrefix with the Base128 style

What is the expected output? What do you see instead?
2. len will recive a bad value. If you use Fixed32, it works.

The problem is in Serializer.TryReadLengthPrefix. Both legs of the switch
do the same thing. Starting at line 495 of Serializer.

switch (style)
{
     case PrefixStyle.Fixed32:
          result = SerializationContext.TryDecodeUInt32Fixed(source, out len);
          break;
     case PrefixStyle.Base128:
          result = SerializationContext.TryDecodeUInt32Fixed(source, out len);
          break;
     default:
          throw new ArgumentOutOfRangeException("style", "Invalid prefix
style: " + style);
     }
     length = (int) len;
     return result;
     }
}

What version of the product are you using? On what operating system?

Please provide any additional information below.

Original issue reported on code.google.com by abraham....@gmail.com on 16 Oct 2009 at 3:45

GoogleCodeExporter commented 8 years ago
Was the data serialized with a length prefix? That code is fairly well tested, 
so I'm a 
little surprised... what volume of data are we talking? (i.e. what is the 
length?) - as 
the prefix can *in theory* be 5 bytes long (unlikely though). Have you got any 
complete 
example I might be able to use to reproduce this?

Original comment by marc.gravell on 16 Oct 2009 at 9:13

GoogleCodeExporter commented 8 years ago
Thanks for replying to my poorly formatted report. Here is a class that shows 
where I
have the problem. It is a short message. I did not include my proto message, 
but that
should not be a factor.

using System;
using System.IO;
using System.Collections.Generic;
using System.Collections;
using System.Windows.Forms;
using biz.cgta.proto.oms;

namespace ProtoTest
{
    public class ProtoTest
    {

/*==============================================================================
=================*/

        public ProtoTest()
        {
            int len32;
            int len128;
            bool test;

            //CreateParams a proto message.
            OmsMessage omsMessage = new OmsMessage();

            omsMessage.message_type = OmsMessage.MessageType.MSG_TYPE_CONFIRMATION;
            omsMessage.application_id = "application_id";
            omsMessage.symbol = "symbol";
            omsMessage.initial_qty = "initial_qty";
            omsMessage.limit_price = "limit_price";
            omsMessage.last_fill_qty = "last_fill_qty";
            omsMessage.last_fill_price = "last_fill_price";
            omsMessage.trader_id = "trader_hid";

            MemoryStream textStream = new MemoryStream();

            ProtoBuf.Serializer.SerializeWithLengthPrefix<OmsMessage>(textStream,
omsMessage, ProtoBuf.PrefixStyle.Fixed32);

            textStream.Position = 0;
            test = ProtoBuf.Serializer.TryReadLengthPrefix(textStream.GetBuffer(), 0,
5, ProtoBuf.PrefixStyle.Fixed32, out len32);

            textStream = new MemoryStream();

            ProtoBuf.Serializer.SerializeWithLengthPrefix<OmsMessage>(textStream,
omsMessage, ProtoBuf.PrefixStyle.Base128);

            textStream.Position = 0;
            test = ProtoBuf.Serializer.TryReadLengthPrefix(textStream.GetBuffer(), 0,
5, ProtoBuf.PrefixStyle.Base128, out len128);

            Console.WriteLine("Fixed32 len: " + len32 + " Base128 len: " + len128);
        }

    }
}

Output:
       Fixed32 len: 103 Base128 len: -1039857561

Original comment by abraham....@gmail.com on 19 Oct 2009 at 3:43

GoogleCodeExporter commented 8 years ago
You were right; there was a bug in TryReadLengthPrefix (fixed r275) [a 
**really** 
stupid bug; sorry] - but note that to use it in this way you must also 
explicitly 
specify zero for the "tag" parameter:

    ProtoBuf.Serializer.SerializeWithLengthPrefix<OmsMessage>(textStream, omsMessage, 
ProtoBuf.PrefixStyle.Base128,0);

Otherwise, and **only** in base128-mode (the existing documentation did mention 
this), it includes a tag-prefix to make the stream a fully valid proto message 
on the 
wire. (It sounds like you don't need this).

To make this usage simpler, I've also added a TryReadLengthPrefix that accepts 
a 
Stream; see Examples/Issues/Issue80.cs for usage (but it should be pretty 
obvious).

I'm on the train, so I don't know if the new zip will upload at the minute; 
I'll try, 
but I may have to do this later.

Original comment by marc.gravell on 21 Oct 2009 at 6:00

GoogleCodeExporter commented 8 years ago
Cool! That did the trick. Thanks for the fast response! 

Original comment by abraham....@gmail.com on 21 Oct 2009 at 1:24

GoogleCodeExporter commented 8 years ago

Original comment by marc.gravell on 21 Oct 2009 at 2:02