Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's wrong with this protobuffer decode?

I'm decoding some buffers with Google protobuffer. I'm using the excellent @decodeIO ProtoBuf.js module.

When I try to decode this Buffer:

<Buffer 0a 0b 47 57 5f 2d 31 38 5f 39 39 32 39>

From this message:

message PaymentResponseElement {
  optional int64  pnPaymentId       = 1;
  optional string messageCode       = 2; //won't use pb enum here
  optional int64  balanceAfterTransaction   = 3;
  optional int32  version           = 4;
}

I get this error:

Error: Illegal wire type for field Message.Field .core.comm.PaymentResponseElement.messageCode: 2 (0 expected)
    at ProtoBuf.Reflect.Field.decode (/home/joojo/node_modules/protobufjs/ProtoBuf.js:2095:27)
    at ProtoBuf.Reflect.Message.decode (/home/joojo/node_modules/protobufjs/ProtoBuf.js:1748:51)
    at ProtoBuf.Reflect.Field.decode (/home/joojo/node_modules/protobufjs/ProtoBuf.js:2196:46)
    at ProtoBuf.Reflect.Message.decode (/home/joojo/node_modules/protobufjs/ProtoBuf.js:1746:51)
    at Function.Message.decode (/home/joojo/node_modules/protobufjs/ProtoBuf.js:1630:41)
    at decode (/home/joojo/public_html/api/control/protoBuffer.js:94:52)
    at Object.module.exports.decode (/home/joojo/public_html/cageapi/control/protoBuffer.js:110:10)
    at decode (/home/joojo/public_html/api/control/messageStructurer.js:82:33)
    at Object.module.exports.decode (/home/joojo/public_html/api/control/messageStructurer.js:94:10
    at CleartextStream.month (/home/joojo/public_html/api/connectionHandler.js:83:32)

Don't know whats going on, and it's happened a few times. How to fix it?

like image 738
Filipe Tagliacozzi Avatar asked Feb 15 '26 15:02

Filipe Tagliacozzi


1 Answers

I have to agree with the implementation. That data is only 1 field - field number 1, length-prefixed (could be a string, binary, or a sub-message edit or packed-array); if we speculatively interpret it as a string (which is always UTF-8 in protouf), it comes out as "GW_-18_9929", which looks sane.

However, your message declared field 1 as int64. The string/length-prefix wire-type (wire-type 2) is not valid for int64 - in fact, the only valid wire-type for that is 64-bit (wire-type 1).

So: your data is valid, but it doesn't match the schema you are claiming.

To unscramble:

  • 0a is binary 1010 - the last three bits (010) is the wire-type, 2 = length delimited; the remaining 1 is the field-number: 1
  • 0b is decimal 11, the length of the field that follows
  • 47 57 5f 2d 31 38 5f 39 39 32 39 is the payload, UTF-8 for "GW_-18_9929"
like image 169
Marc Gravell Avatar answered Feb 17 '26 04:02

Marc Gravell