解码 protobuf 二进制文件——“无法解析输入"; [英] Decode protobuf binary -- getting "Failed to parse input"

查看:33
本文介绍了解码 protobuf 二进制文件——“无法解析输入";的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用 protoc 解码 protobuf 二进制消息,但出现解析输入失败"错误.我尝试了 protoc --decode_raw <proto.binprotoc --decode package.MessageType MySchema.proto proto.bin.

I tried to decode a protobuf binary message using protoc and I am getting "Failed to parse input" error. I tried both protoc --decode_raw < proto.bin and protoc --decode package.MessageType MySchema.proto < proto.bin.

我在 SO 中阅读了两个线程,其中一个表明 protoc --decode 不是 100% 可靠的,另一个表明需要手动剥离开头的消息长度数据.我的问题是

I read two threads in SO, one of which suggests that protoc --decode is not 100% reliable and another one which suggests that message length data at the beginning needs to be stripped manually. My question is

  • 如何使用 protoc 工具解码 proto 二进制文件?
  • 该工具是否能够 100% 可靠地解码(即使我提供类型)或消息在反序列化之前需要一些黑客攻击,这是真的吗?(这就引出了一个问题,protoc --decode 选项的目的是什么?
  • How to use protoc tool for decoding proto binaries?
  • Is it true that the tool is not 100% reliable to be capable of decoding (even if I supply type) or messages need some hacking before they can be deserialized? (which begs the question, what is the purpose of protoc --decode option?

P.S:我使用的是 protoc 3.6.1,消息是在 Java 程序中创建的 (syntax=proto2)

P.S: I am using protoc 3.6.1 and messages are created in a Java program (syntax=proto2)

推荐答案

如果你把你谈论的话题链接起来会很有帮助.

It would have helped if you had linked the threads you talk about.

但一般来说,是的,在用protoc解码之前需要去掉长度前缀.关于如何为 protobuf 消息添加长度前缀没有标准,有些使用 fixed32 前缀,有些使用 varint,有些使用更长的标头.因此 protoc 工具无法解析长度标头.

But in general, yes, you need to strip the length prefix before decoding with protoc. There is no standard about how to prefix length to protobuf messages, some use fixed32 prefix and others varint, some have a longer header. Thus the protoc tool cannot parse the length header.

而且我还没有看到任何 protoc --decode 无法解码有效消息的情况.但是它对错误的容忍度非常低,例如截断的数据 - 即使只有一个字段损坏,它也总是拒绝整个消息.

And I haven't seen any case where protoc --decode wouldn't decode a valid message. But it has very little tolerance for errors, such as truncated data - it always rejects the whole message even if only one field is corrupted.

为了更好的调试,您可以使用例如此工具逐字节解码并显示发生的第一个错误:https://protogen.marcgravell.com/decode

For better debugging, you can use e.g. this tool, which decodes byte by byte and shows the first error that occurs: https://protogen.marcgravell.com/decode

这篇关于解码 protobuf 二进制文件——“无法解析输入";的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆