0

I have two programs that exchange information via protocol buffers serialization. One of my data fields is in protobuf's bytes format, and it somehow has a few extra bytes after decoding.

That said, 0xDEADBEEF0123456789 becomes 0xFFFFFFDEFFFFFFADFFFFFFBEFFFFFFEF01234567FFFFFF89.

My messages.proto file:

syntax = "proto3";

message RawPacket {
    uint32 version = 1;
    oneof proto_trait {
        string protocol = 2;
        uint32 tcpport = 4;
        uint32 udpport = 5;
    };
    bytes data = 3;
}

The encoder code in python:

import sys
import protocol.messages_pb2 as pb

...

payload = bytes.fromhex('deadbeef0123456789')

msg = pb.RawPacket()
msg.version = 1
msg.protocol = "Frame"
msg.data = payload

with open(sys.argv[1], 'wb') as f:
    f.write(msg.SerializeToString())

The decoder code in C++:

#include <fstream>
#include <iostream>
#include "protocol/messages.pb.h"

std::fstream inputfile{"some filename", std::ios::in | std::ios::binary};

RawPacket input;
if (!input.ParseFromIstream(&inputfile)) {
    std::cerr << "Could not parse raw packet." << std::endl;
    return 2;
}

// input.data() is a const std::string&, and I think that's suspicious.
for (char c : input.data()) {
    printf("%.2X", c);
}
std::cout << std::endl;

Any idea is appreciated.

sfphoton
  • 127
  • 1
  • 7
  • `9` is not a valid hex digit. I'm not sure what Python is doing in that case, but I'd check the length of `bytes` before passing it to the proto – Mooing Duck May 03 '21 at 15:17
  • 1
    9 is not a valid hex digit? Of course it is. – g01d May 03 '21 at 15:20
  • `9` is a valid hex digit, it 0-9 and a-f are. The length is also correct, it's even. – sfphoton May 03 '21 at 15:23
  • `for (char c : input.data()) ` can not possibly compile. Please provide MCVE. – SergeyA May 03 '21 at 15:33
  • It compiles since C++11, because input.data() is a string. (See: https://www.geeksforgeeks.org/g-fact-40-foreach-in-c-and-java/ ) Also sorry, but what is an MCVE? – sfphoton May 03 '21 at 15:36
  • 1
    `%.2X` expects an integer, as `char` is signed, characters over `7f` cast to a negative integer – Alan Birtles May 03 '21 at 15:50

0 Answers0