Restoring .proto file from descriptor string. Possible?

前端 未结 2 493
面向向阳花
面向向阳花 2021-01-03 10:06

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\

2条回答
  •  北荒
    北荒 (楼主)
    2021-01-03 10:29

    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 
    #include 
    #include 
    
    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.

提交回复
热议问题