Restoring .proto file from descriptor string. Poss

2019-02-16 01:34发布

问题:

Is it possible to decompile a string containing Protocol Buffers descriptor back to .proto file?

Say I have a long string like

\n\file.proto\u001a\u000ccommon.proto\"\u00a3\u0001\n\nMsg1Request\u0012\u0017\n\u0006common\u0018\u0001 ... etc.

I need to restore .proto, not necessary exactly as it was but compilable.

回答1:

Yes it should be possible to get some thing close get original definition. I do not know of any existing code to do it (hopefully some one else will).

Hava a look at how protocol buffers itself handles the String.

Basically

  1. convert the string to bytes (using charset="ISO-8859-1" in java), it will then be a Protocol-Buffer message(format=FileDescriptorProto in java). The FileDescriptorProto is built as part of the Protocol-Buffers install.

  2. Extract the data in the Protocol-Buffer message

Here is a File-Descriptor protocol displayed in the Protocol-Buffer editor



回答2:

In C++, the FileDescriptor interface has a method DebugString() which formats the descriptor contents in .proto syntax -- i.e. exactly what you want. In order to use it, you first need to write code to convert the raw FileDescriptorProto to a FileDescriptor, using the DescriptorPool interface.

Something like this should do it:

#include <google/protobuf/descriptor.h>
#include <google/protobuf/descriptor.pb.h>
#include <iostream>

int main() {
  google::protobuf::FileDescriptorProto fileProto;
  fileProto.ParseFromFileDescriptor(0);
  google::protobuf::DescriptorPool pool;
  const google::protobuf::FileDescriptor* desc =
      pool.BuildFile(fileProto);
  std::cout << desc->DebugString() << std::endl;
  return 0;
}

You need to feed this program the raw bytes of the FileDescriptorProto, which you can get by using Java to encode your string to bytes using the ISO-8859-1 charset.

Also note that the above doesn't work if the file imports any other files -- you would have to load those imports into the DescriptorPool first.