0

If I save the packet data as a binary file, I can run it through protoc -decode to dump the data into a formatted textual representation.

I am wondering if there is any function available to dump the binary data as formatted text programmatically? My code is in JavaScript but C++ is fine as well.

One way would be to spawn protoc as a background process and get the results back. However, it is not an option for me to bundle protoc executable itself with my code. Regards.

Peter
  • 11,260
  • 14
  • 78
  • 155

2 Answers2

0

One way how to do this could be to create a stream in JS and then post process with protoc decode, this would work since you could bind the stream to the protoc process or thread (the thread can read a buffer, and post process). This would work if you do not have any performance requirements. To solve the problem you could create a c++ binding to the JavaScript app. This would ensure efficiency to some extend.

Marko Bencik
  • 368
  • 2
  • 13
0

You can use the google::protobuf::compiler::CommandLineInterface and google::protobuf::compiler::cpp::CppGenerator interface to achieve whatever protoc can do.

#include <google/protobuf/compiler/command_line_interface.h>
#include <google/protobuf/compiler/cpp/cpp_generator.h>

int dump() {
    const char *argv[] = {"dumper", "--decode_raw"};
    google::protobuf::compiler::CommandLineInterface cli;
    google::protobuf::compiler::cpp::CppGenerator cpp_generator;
    cli.RegisterGenerator("--cpp_out", &cpp_generator, "Generate C++ source and header.");
    return cli.Run(sizeof(argv) / sizeof(char*), argv);
}

int main() {
    return dump();
}

Build the above code into a bin: dumper, and run it like this:

cat your-binary-file | ./dumper
for_stack
  • 21,012
  • 4
  • 35
  • 48