File: encode.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 (125 lines) | stat: -rw-r--r-- 2,217 bytes parent folder | download
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
require 'beefcake/buffer/base'

module Beefcake

  class Buffer

    def append(type, val, fn)
      if fn != 0
        wire = Buffer.wire_for(type)
        append_info(fn, wire)
      end

      __send__("append_#{type}", val)
    end

    def append_info(fn, wire)
      append_uint32((fn << 3) | wire)
    end

    def append_fixed32(n, tag=false)
      if n < MinUint32 || n > MaxUint32
        raise OutOfRangeError, n
      end

      self << [n].pack("V")
    end

    def append_fixed64(n)
      if n < MinUint64 || n > MaxUint64
        raise OutOfRangeError, n
      end

      self << [n & 0xFFFFFFFF, n >> 32].pack("VV")
    end

    def append_int32(n)
      if n < MinInt32 || n > MaxInt32
        raise OutOfRangeError, n
      end

      append_int64(n)
    end

    def append_uint32(n)
      if n < MinUint32 || n > MaxUint32
        raise OutOfRangeError, n
      end

      append_uint64(n)
    end

    def append_int64(n)
      if n < MinInt64 || n > MaxInt64
        raise OutOfRangeError, n
      end

      if n < 0
        n += (1 << 64)
      end

      append_uint64(n)
    end

    def append_sint32(n)
      append_uint32((n << 1) ^ (n >> 31))
    end

    def append_sfixed32(n)
      append_fixed32((n << 1) ^ (n >> 31))
    end

    def append_sint64(n)
      append_uint64((n << 1) ^ (n >> 63))
    end

    def append_sfixed64(n)
      append_fixed64((n << 1) ^ (n >> 63))
    end

    def append_uint64(n)
      if n < MinUint64 || n > MaxUint64
        raise OutOfRangeError, n
      end

      while true
        bits = n & 0x7F
        n >>= 7
        if n == 0
          return self << bits
        end
        self << (bits | 0x80)
      end
    end

    def append_float(n)
      self << [n].pack("e")
    end

    def append_double(n)
      self << [n].pack("E")
    end

    def append_bool(n)
      append_int64(n ? 1 : 0)
    end

    def append_string(s)
      actual_string = thaw_string s
      encoded = actual_string.dup.force_encoding 'binary'
      append_uint64(encoded.length)
      self << encoded
    end
    alias :append_bytes :append_string


    private
    def thaw_string(s)
      if s.frozen?
        s = s.dup
      end
      s.to_s
    end
  end

end