如何解码 protobuf 二进制响应
我创建了一个测试应用程序,可以使用 Goggle Goggles 识别一些图像。它对我有用,但我收到二进制 protobuf 响应。我没有原始文件,只有二进制响应。我怎样才能从中获取数据? (已发送一些带有熊瓶的图像并得到下一个响应):
A
TuborgLogo9 HoaniText���;�)b���2d8e991bff16229f6"�
+TR=T=AQBd6Cl4Kd8:X=OqSEi:S=_rSozFBgfKt5d9b0
+TR=T=6rLQxKE2xdA:X=OqSEi:S=gd6Aqb28X0ltBU9V
+TR=T=uGPf9zJDWe0:X=OqSEi:S=32zTfdIOdI6kuUTa
+TR=T=RLkVoGVd92I:X=OqSEi:S=P7yOhvSAOQW6SRHN
+TR=T=J1FMvNmcyMk:X=OqSEi:S=5Z631_rd2ijo_iuf�
需要获取字符串“Tuborg”,如果可能的话,输入 - “Logo”
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用
protoc
进行解码:You can decode with
protoc
:这将向您显示顶级字段。不幸的是它无法知道字段类型的确切细节。 long/int/bool/enum 等都被编码为 Varint 并且看起来都一样。字符串、字节数组和子消息都是长度分隔的,并且也是无法区分的。
这里有一些有用的细节: https ://github.com/dcodeIO/protobuf.js/wiki/How-to-reverse-engineer-a-buffer-by-hand
如果您遵循 UnknownFieldSet.mergeFrom() 中的代码您将看到如何尝试解码子消息并在失败时返回到字符串 - 但它不会非常可靠。
协议中的线型有 2 个备用值 - 如果 google 使用其中之一来表示子消息,那将会非常有帮助。 (另一个可能是空值。)
这是一些非常粗糙的仓促代码,试图生成对诊断有用的东西。它会猜测数据类型,对于字符串和子消息,它会在某些情况下打印两种选择。请不要相信它打印的任何值:
This will show you the top level fields. Unfortunately it can't know the exact details of field types. long/int/bool/enum etc are all encoded as Varint and all look the same. Strings, byte-arrays and sub-messages are length-delimited and are also indistinguishable.
Some useful details here: https://github.com/dcodeIO/protobuf.js/wiki/How-to-reverse-engineer-a-buffer-by-hand
If you follow the code in the UnknownFieldSet.mergeFrom() you'll see how you could try decode sub-messages and falling back to strings if that fails - but it's not going to be very reliable.
There are 2 spare values for the wiretype in the protocol - it would have been really helpful if google had used one of these to denote sub-messages. (And the other for null values perhaps.)
Here's some very crude rushed code which attempts to produce a something useful for diagnostics. It guesses at the data types and in the case of strings and sub-messages it will print both alternatives in some cases. Please don't trust any values it prints:
我假设真正的问题是如何解码 protobuf,而不是如何使用 Java 从线路读取二进制文件。
您的问题的答案可以在此处找到。
简单来说,protobufs被编码为
的 3 元组,其中:.proto
架构中字段的字段编号,I'm going to assume the real question is how to decode protobufs and not how to read binary from the wire using Java.
The answer to your question can be found here
Briefly, on the wire, protobufs are encoded as 3-tuples of
<key,type,value>
, where:.proto
schema<Varint, int32, length-delimited, start-group, end-group,int64
. It contains just enough information to decode the value of the 3-tuple, namely it tells you how long the value is.