0

I have an encoded binary as follows:

0a 16 0a 06 72 63 6e 33 31 72 12 0a 65 37 36 30 34 32 33 35 32 37 1a 00
20 01 2a 06 34 34 38 37 38

I am not sure how to write the proto file reflecting the binary. I know the message contents.

1. First two bytes indicate an embedded message with 22 bytes in length.
2. Inside the embedded message there are three fields (three strings).
3. Then the rest of the message with two fields.

How to write the .proto file to reflect the above binary ?

message UserInfo
{
        required string username = 1;
        required string usserId  = 2;
        optional string extraInfo= 3;
}

message SendUserRequest
{
        required UserInfo uinfo = 1;
        ...
}
Bart
  • 1,405
  • 6
  • 32
CodeWeed
  • 971
  • 2
  • 21
  • 40

2 Answers2

1

The bytes you provided appear to be missing a byte. I added a 0 to the end, then ran it through protoc --decode_raw and got this:

1 {
  1: "rcn31r"
  2: "e760423527"
  3: ""
}
4: 1
5: "44878\000"  // (my added zero byte shows up here)

So in addition to what you wrote, your SendUserRequest should include two more fields:

optional int32 a = 4;
optional string b = 5;

Note that there's no way to tell what the correct names of these fields should be, nor whether they're required or optional. Moreover, a could technically be any of the integer types.

Kenton Varda
  • 41,353
  • 8
  • 121
  • 105
0

Sorry, but this isn't what Protobuf is for. Protobuf can only decode Protobuf format; it cannot match arbitrary ad-hoc byte formats.

EDIT: I misunderstood the question. Sometimes people think Protobufs can describe arbitrary binary, and the exact bytes given were rejected by protoc --decode_raw, but it turns out there was a missing byte. I added a better answer above.

Kenton Varda
  • 41,353
  • 8
  • 121
  • 105
  • It is a protobuf encoded message. I have explained the contents of the message in bullets in the question. – CodeWeed Jun 22 '15 at 13:01
  • I see, sorry, I misunderstood your question. It sounded like you were describing some other non-protobuf binary format, and then trying to write a proto file for it. (It's a mistake people sometimes make.) I'll try answering again. – Kenton Varda Jun 24 '15 at 17:37