File: decode.rb

package info (click to toggle)
ruby-beefcake 1.2.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 196 kB
  • sloc: ruby: 1,655; makefile: 4
file content (105 lines) | stat: -rw-r--r-- 1,674 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
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
require 'beefcake/buffer/base'

module Beefcake

  class Buffer

    def read_info
      n    = read_uint64
      fn   = n >> 3
      wire = n & 0x7

      [fn, wire]
    end

    def read_bytes
      read(read_uint64)
    end

    def read_string
      read_bytes.force_encoding Encoding.find('utf-8')
    end

    def read_fixed32
      bytes = read(4)
      bytes.unpack("V").first
    end

    def read_fixed64
      bytes = read(8)
      x, y = bytes.unpack("VV")
      x + (y << 32)
    end

    def read_int64
      n = read_uint64
      if n > MaxInt64
        n -= (1 << 64)
      end
      n
    end
    alias :read_int32 :read_int64

    def read_uint64
      n = shift = 0
      while true
        if shift >= 64
          raise BufferOverflowError, "varint"
        end
        b = read(1).ord

        n |= ((b & 0x7F) << shift)
        shift += 7
        if (b & 0x80) == 0
          return n
        end
      end
    end
    alias :read_uint32 :read_uint64

    def read_sint64
      decode_zigzag(read_uint64)
    end
    alias :read_sint32 :read_sint64

    def read_sfixed32
      decode_zigzag(read_fixed32)
    end

    def read_sfixed64
      decode_zigzag(read_fixed64)
    end

    def read_float
      bytes = read(4)
      bytes.unpack("e").first
    end

    def read_double
      bytes = read(8)
      bytes.unpack("E").first
    end

    def read_bool
      read_int32 != 0
    end

    def skip(wire)
      case wire
      when 0 then read_uint64
      when 1 then read_fixed64
      when 2 then read_string
      when 5 then read_fixed32
      end
    end


    private

    def decode_zigzag(n)
      (n >> 1) ^ -(n & 1)
    end

  end

end