Sha256: afa66ccef36ff1fa23890a79904e48f9e795f89c169aeac676e8eef06366e2ff

Contents?: true

Size: 1.99 KB

Versions: 13

Compression:

Stored size: 1.99 KB

Contents

require 'protobuf/wire_type'
require 'protobuf/exceptions'

module Protobuf
  class Decoder

    # Read bytes from +stream+ and pass to +message+ object.
    def self.decode_each_field(stream, &block)
      until stream.eof?
        tag, bytes = read_field(stream)
        block.call(tag, bytes)
      end
    end

    def self.read_field(stream)
      tag, wire_type = read_key(stream)
      bytes = case wire_type
              when ::Protobuf::WireType::VARINT then
                read_varint(stream)
              when ::Protobuf::WireType::FIXED64 then
                read_fixed64(stream)
              when ::Protobuf::WireType::LENGTH_DELIMITED then
                read_length_delimited(stream)
              when ::Protobuf::WireType::FIXED32 then
                read_fixed32(stream)
              when ::Protobuf::WireType::START_GROUP then
                fail NotImplementedError, 'Group is deprecated.'
              when ::Protobuf::WireType::END_GROUP then
                fail NotImplementedError, 'Group is deprecated.'
              else
                fail InvalidWireType, wire_type
              end

      [tag, bytes]
    end

    # Read 32-bit string value from +stream+.
    def self.read_fixed32(stream)
      stream.read(4)
    end

    # Read 64-bit string value from +stream+.
    def self.read_fixed64(stream)
      stream.read(8)
    end

    # Read key pair (tag and wire-type) from +stream+.
    def self.read_key(stream)
      bits = read_varint(stream)
      wire_type = bits & 0x07
      tag = bits >> 3
      [tag, wire_type]
    end

    # Read length-delimited string value from +stream+.
    def self.read_length_delimited(stream)
      value_length = read_varint(stream)
      stream.read(value_length)
    end

    # Read varint integer value from +stream+.
    def self.read_varint(stream)
      value = index = 0
      begin
        byte = stream.readbyte
        value |= (byte & 0x7f) << (7 * index)
        index += 1
      end while (byte & 0x80).nonzero?
      value
    end

  end
end

Version data entries

13 entries across 13 versions & 3 rubygems

Version Path
protobuf-core-3.5.0 lib/protobuf/decoder.rb
protobuf-3.5.4 lib/protobuf/decoder.rb
protobuf-3.5.3 lib/protobuf/decoder.rb
protobuf-3.5.2 lib/protobuf/decoder.rb
protobuf-3.5.1 lib/protobuf/decoder.rb
prepor-protobuf-3.5.1 lib/protobuf/decoder.rb
prepor-protobuf-3.5.0 lib/protobuf/decoder.rb
protobuf-3.5.0 lib/protobuf/decoder.rb
protobuf-3.4.4 lib/protobuf/decoder.rb
protobuf-3.4.3 lib/protobuf/decoder.rb
protobuf-3.4.2 lib/protobuf/decoder.rb
protobuf-3.4.1 lib/protobuf/decoder.rb
protobuf-3.4.0 lib/protobuf/decoder.rb