Created
March 23, 2011 03:13
-
-
Save avibryant/882550 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'stringio' | |
require 'base64' | |
def read_varint(io) | |
value = index = 0 | |
begin | |
byte = io.readchar | |
value |= (byte & 0x7f) << (7 * index) | |
index += 1 | |
end while (byte & 0x80).nonzero? | |
value | |
end | |
def decode_base64_protobuf(string) | |
values = [] | |
io = StringIO.new(Base64.decode64(string)) | |
until io.eof? | |
bits = read_varint(io) | |
values[(bits >> 3) - 1] = | |
case bits & 0x07 | |
when 0; read_varint(io) | |
when 2; io.read(read_varint(io)) | |
end | |
end | |
values | |
end | |
if __FILE__ == $0 | |
STDIN.each do |line| | |
puts decode_base64_protobuf(line.split[-1]).join("\t") | |
end | |
end |
For context: this is a minimal decoder for a tiny (but useful) subset of Google's Protocol Buffers format
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Avi offered as input the string "CKeTAxCmmfRHGK6trLv1prbkEiCglsTnBDgBMAA="