Module: Protobug::BinaryEncoding

Defined in:
lib/protobug/binary_encoding.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.decode_length(binary) ⇒ Object

Raises:

  • (EOFError)


131
132
133
134
135
136
137
# File 'lib/protobug/binary_encoding.rb', line 131

def decode_length(binary)
  length = decode_varint(binary) || raise(EOFError, "unexpected EOF")
  string = binary.read(length) || raise(EOFError, "unexpected EOF")
  raise EOFError, "expected #{length} bytes, got #{string.bytesize}" unless string.bytesize == length

  string
end

.decode_varint(binary) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/protobug/binary_encoding.rb', line 58

def decode_varint(binary)
  # Only the first byte may legitimately be absent (clean EOF, signaled by
  # returning nil). Once a continuation bit is set, any subsequent missing
  # byte is a truncated varint and must be rejected.
  if (byte0 = binary.getbyte || return) < 0x80
    byte0
  elsif (byte1 = binary.getbyte || varint_eof!) < 0x80
    (byte1 << 7) | (byte0 & 0x7F)
  elsif (byte2 = binary.getbyte || varint_eof!) < 0x80
    (byte2 << 14) |
      ((byte1 & 0x7F) << 7) |
      (byte0 & 0x7F)
  elsif (byte3 = binary.getbyte || varint_eof!) < 0x80
    (byte3 << 21) |
      ((byte2 & 0x7F) << 14) |
      ((byte1 & 0x7F) << 7) |
      (byte0 & 0x7F)
  elsif (byte4 = binary.getbyte || varint_eof!) < 0x80
    (byte4 << 28) |
      ((byte3 & 0x7F) << 21) |
      ((byte2 & 0x7F) << 14) |
      ((byte1 & 0x7F) << 7) |
      (byte0 & 0x7F)
  elsif (byte5 = binary.getbyte || varint_eof!) < 0x80
    (byte5 << 35) |
      ((byte4 & 0x7F) << 28) |
      ((byte3 & 0x7F) << 21) |
      ((byte2 & 0x7F) << 14) |
      ((byte1 & 0x7F) << 7) |
      (byte0 & 0x7F)
  elsif (byte6 = binary.getbyte || varint_eof!) < 0x80
    (byte6 << 42) |
      ((byte5 & 0x7F) << 35) |
      ((byte4 & 0x7F) << 28) |
      ((byte3 & 0x7F) << 21) |
      ((byte2 & 0x7F) << 14) |
      ((byte1 & 0x7F) << 7) |
      (byte0 & 0x7F)
  elsif (byte7 = binary.getbyte || varint_eof!) < 0x80
    (byte7 << 49) |
      ((byte6 & 0x7F) << 42) |
      ((byte5 & 0x7F) << 35) |
      ((byte4 & 0x7F) << 28) |
      ((byte3 & 0x7F) << 21) |
      ((byte2 & 0x7F) << 14) |
      ((byte1 & 0x7F) << 7) |
      (byte0 & 0x7F)
  elsif (byte8 = binary.getbyte || varint_eof!) < 0x80
    (byte8 << 56) |
      ((byte7 & 0x7F) << 49) |
      ((byte6 & 0x7F) << 42) |
      ((byte5 & 0x7F) << 35) |
      ((byte4 & 0x7F) << 28) |
      ((byte3 & 0x7F) << 21) |
      ((byte2 & 0x7F) << 14) |
      ((byte1 & 0x7F) << 7) |
      (byte0 & 0x7F)
  elsif (byte9 = binary.getbyte || varint_eof!) < 0x80
    raise DecodeError, "varint overflow: 10th byte #{byte9} exceeds 64 bits" if byte9 > 1

    (byte9 << 63) |
      ((byte8 & 0x7F) << 56) |
      ((byte7 & 0x7F) << 49) |
      ((byte6 & 0x7F) << 42) |
      ((byte5 & 0x7F) << 35) |
      ((byte4 & 0x7F) << 28) |
      ((byte3 & 0x7F) << 21) |
      ((byte2 & 0x7F) << 14) |
      ((byte1 & 0x7F) << 7) |
      (byte0 & 0x7F)
  end
end

.decode_zigzag(size, value) ⇒ Object



139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/protobug/binary_encoding.rb', line 139

def decode_zigzag(size, value)
  negative = value & 1 == 1
  value %= 2**size
  value >>= 1
  value = -value - 1 if negative

  unless value.bit_length <= size
    raise DecodeError,
          "bitlength too large for #{size}-bit integer: #{value.bit_length}"
  end

  value
end

.encode_length(contents, outbuf) ⇒ Object



49
50
51
52
# File 'lib/protobug/binary_encoding.rb', line 49

def encode_length(contents, outbuf)
  encode_varint contents.bytesize, outbuf
  outbuf << contents
end

.encode_varint(value, outbuf) ⇒ Object

Raises:



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/protobug/binary_encoding.rb', line 9

def encode_varint(value, outbuf)
  raise EncodeError, "expected integer, got #{value.inspect}" unless value.is_a? Integer
  raise RangeError, "expected 64-bit integer" if value > (2**64) - 1 || value < -2**63

  value += 2**64 if value < 0
  loop do
    if value.bit_length > 7
      outbuf << (0b1000_0000 | (value & 0b0111_1111))
      value >>= 7
    else
      outbuf << (value & 0b0111_1111)
      break
    end
  end
end

.encode_zigzag(size, value, outbuf) ⇒ Object

Raises:



36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/protobug/binary_encoding.rb', line 36

def encode_zigzag(size, value, outbuf)
  raise EncodeError, "expected integer, got #{value.inspect}" unless value.is_a? Integer

  unless value.bit_length <= size
    raise EncodeError,
          "bitlength too large for #{size}-bit integer: #{value.bit_length}"
  end

  encoded = 2 * value.abs
  encoded -= 1 if value < 0
  encode_varint encoded, outbuf
end

.read_field_value(binary, wire_type) ⇒ Object



153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'lib/protobug/binary_encoding.rb', line 153

def read_field_value(binary, wire_type)
  case wire_type
  when 0
    decode_varint(binary) || raise(EOFError, "unexpected EOF")
  when 1
    value = binary.read(8)
    raise EOFError, "unexpected EOF" if value&.bytesize != 8

    value
  when 2
    decode_length(binary)
  when 3, 4
    raise UnsupportedFeatureError.new(:group, "reading groups from binary protos (in #{self})")
  when 5
    value = binary.read(4)
    raise EOFError, "unexpected EOF" if value&.bytesize != 4

    value
  else
    raise DecodeError, "unknown wire_type: #{wire_type}"
  end
end

.varint_eof!Object

Raises:

  • (EOFError)


54
55
56
# File 'lib/protobug/binary_encoding.rb', line 54

def varint_eof!
  raise EOFError, "unexpected EOF in the middle of a varint"
end

Instance Method Details

#pack(ary, format, buffer:) ⇒ Object



27
28
29
# File 'lib/protobug/binary_encoding.rb', line 27

def pack(ary, format, buffer:)
  buffer.concat ary.pack(format)
end