2013-10-17 86 views
1

是否可以将包含Protocol Buffers描述符的字符串反编译回.proto文件?从描述符字符串恢复.proto文件。可能?

说我有一个很长的字符串像

\ n \ file.proto \ u001a \ u000ccommon.proto \“\ u00a3 \ U0001 \ n \ nMsg1Request \ u0012 \ u0017 \ n \ u0006common \ u0018 \ U0001。 ..等

我需要恢复.proto,不完全是必要的,因为它是,但编译。

感谢您的帮助

回答

2

是的,它应该可以得到一些东西接近获得原始定义我不知道有任何现有的代码可以做到这一点(希望其他人可以)。

Hava了解协议缓冲区如何处理字符串。

基本上

  1. 字符串转换为字节(字符集采用= “ISO-8859-1” 在Java),它将随后是一个协议缓冲区消息(格式= FileDescriptorProto在JAVA)。 FileDescriptorProto是作为Protocol-Buffers安装的一部分而构建的。

  2. 协议缓冲区消息

这里提取的数据是显示在Protocol-Buffer editor

enter image description here

+0

我一定会尝试一下。如果我找到一个工具,我会报告。如果不是,我/我会自己做,但不会很快... – OGP

3

在C++中一个文件描述符协议中,FileDescriptor接口具有一种方法DebugString()其格式描述符的内容在.proto语法 - 即完全wha你想要。为了使用它,首先需要编写代码,使用DescriptorPool接口将原始FileDescriptorProto转换为FileDescriptor

像这样的东西应该这样做:

#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; 
} 

你需要养活这个程序FileDescriptorProto,您可以通过使用Java您的字符串编码使用ISO-8859-字节得到的原始字节1个字符集。

另请注意,如果文件导入任何其他文件,则上述操作不起作用 - 您必须首先将这些导入装入DescriptorPool

+0

它只打印第一条消息。我做错了什么或需要修改以打印多条消息? – Poma

+0

打印FileDescriptorProto中的所有内容。 –

+0

这是一个黑客攻击。似乎DebugString()只能用proto2语法打印描述符。所以如果你不得不使用proto3语法,那将会有些麻烦。 :( – Wizmann