Skip to content

Instantly share code, notes, and snippets.

@avibryant
Created March 23, 2011 03:13
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save avibryant/882550 to your computer and use it in GitHub Desktop.
Save avibryant/882550 to your computer and use it in GitHub Desktop.
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
@headius
Copy link

headius commented Mar 23, 2011

Avi offered as input the string "CKeTAxCmmfRHGK6trLv1prbkEiCglsTnBDgBMAA="

@avibryant
Copy link
Author

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