Class: CBOR::Buffer

Inherits:
Object show all
Defined in:
lib/cbor-pure.rb,
lib/cbor-pretty.rb

Constant Summary collapse

HALF_NAN_BYTES =
("\xf9".force_encoding(Encoding::BINARY) + Half::NAN_BYTES).freeze
MT_TO_ENCODING =
{2 => Encoding::BINARY, 3 => Encoding::UTF_8}
MT_NAMES =
["unsigned", "negative", "bytes", "text", "array", "map", "tag", "primitive"]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(s = String.new) ⇒ Buffer

Returns a new instance of Buffer.



100
101
102
103
# File 'lib/cbor-pure.rb', line 100

def initialize(s = String.new)
  @buffer = s
  @pos = 0
end

Instance Attribute Details

#bufferObject (readonly)

Returns the value of attribute buffer.



99
100
101
# File 'lib/cbor-pure.rb', line 99

def buffer
  @buffer
end

Instance Method Details

#add(d) ⇒ Object



165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
# File 'lib/cbor-pure.rb', line 165

def add(d)
  case d
  when Integer
    ib = if d < 0
           d = -1-d
           0x20
         else
           0x00
         end
    head(ib, d) {             # block is called if things do not fit
      s = bignum_to_bytes(d)
      head(0xc0, TAG_BIGNUM_BASE + (ib >> 5))
      head(0x40, s.bytesize)
      s
    }
  when Numeric; addfloat(d)
  when Symbol; add(d.to_s)    # hack: this should really be tagged
  when Simple; head(0xe0, d.value)
  when false; head(0xe0, 20)
  when true; head(0xe0, 21)
  when nil; head(0xe0, 22)
  when Tagged                 # we don't handle :simple here
    head(0xc0, d.tag)
    add(d.value)
  when String
    lengths = d.cbor_stream?
    e = d
    ib = if d.encoding == Encoding::BINARY
           0x40
         else
           d = d.encode(Encoding::UTF_8).force_encoding(Encoding::BINARY)
           0x60
         end
    if lengths
      @buffer << (ib + 31)
      pos = 0
      lengths.each do |r|
        add(e[pos, r])
        pos += r
      end
      @buffer << 0xff
    else
      head(ib, d.bytesize)
      @buffer << d
    end
  when Array
    if d.cbor_stream?
      @buffer << 0x9f
      d.each {|di| add(di)}
      @buffer << 0xff
    else
      head(0x80, d.size)
      d.each {|di| add(di)}
    end
  when Hash
    if d.cbor_stream?
      @buffer << 0xbf
      d.each {|k, v| add(k); add(v)}
      @buffer << 0xff
    else
      head(0xa0, d.size)
      d.each {|k, v| add(k); add(v)}
    end
  else
    raise("Don't know how to encode »#{d.inspect}« (#{d.class})")
  end
  self
end

#addfloat(fv) ⇒ Object



125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/cbor-pure.rb', line 125

def addfloat(fv)
  if fv.nan?
    # | Format   | Sign bit | Exponent | Significand | Zero
    # | binary16 |        1 |        5 |          10 | 42
    # | binary32 |        1 |        8 |          23 | 29
    # | binary64 |        1 |       11 |          52 | 0
    ds = [fv].pack("G")
    firstword = ds.unpack("n").first
    raise "NaN exponent error #{firstword}" unless firstword & 0x7FF0 == 0x7FF0
    iv = ds.unpack("Q>").first
    if iv & 0x3ffffffffff == 0 # 42 zero, 10 bits fit in half
      @buffer << [0xf9, (firstword & 0xFC00) + ((iv >> 42) & 0x3ff)].pack("Cn")
    elsif iv & 0x1fffffff == 0 # 29 zero, 23 bits fit in single
      @buffer << [0xfa, (ds.getbyte(0) << 24) + ((iv >> 29) & 0xffffff)].pack("CN")
    else
      @buffer << 0xfb << ds
    end
  else
    ss = [fv].pack("g")         # single-precision
    if ss.unpack("g").first == fv
      if hs = Half.encode_from_single(fv, ss)
        @buffer << 0xf9 << hs
      else
        @buffer << 0xfa << ss
      end
    else
      @buffer << [0xfb, fv].pack("CG") # double-precision
    end
  end
end

#atleast(n) ⇒ Object

Raises:



234
235
236
237
# File 'lib/cbor-pure.rb', line 234

def atleast(n)
  left = @buffer.bytesize - @pos
  raise OutOfBytesError.new(n - left) if n > left
end

#bignum_to_bytes(d) ⇒ Object



156
157
158
159
160
161
162
163
# File 'lib/cbor-pure.rb', line 156

def bignum_to_bytes(d)
  s = String.new
  while (d != 0)
    s << (d & 0xFF)
    d >>= 8
  end
  s.reverse!
end

#decode_item(breakable = false) ⇒ Object



280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
# File 'lib/cbor-pure.rb', line 280

def decode_item(breakable = false)
  ib = take(1).ord
  ai = ib & 0x1F
  val = case ai
        when 0...24; ai
        when 24; take(1).ord
        when 25; take(2).unpack("n").first
        when 26; (s = take(4)).unpack("N").first
        when 27; (s = take(8)).unpack("Q>").first
        when 31; return decode_item_streaming(ib, breakable)
        else raise "unknown additional information #{ai} in ib #{ib}"
        end
  case ib >>= 5
  when 0; val
  when 1; -1-val
  when 7
    case ai
    when 20; false
    when 21; true
    when 22; nil
    # when 23; Simple.new(23)   # Ruby does not have Undefined
    when 24;
      raise "two-byte simple is #{val} but must be between 32 and 255" unless val >= 32;
      Simple.new(val)
    when 25; Half.decode(val)
    when 26; s.unpack("g").first # cannot go directly from val
    when 27; s.unpack("G").first #   in Ruby
    else
      Simple.new(val)
    end
  when 6
    di = decode_item
    if String === di && (val & ~1) == TAG_BIGNUM_BASE
      (TAG_BIGNUM_BASE - val) ^ di.bytes.inject(0) {|sum, b| sum <<= 8; sum += b }
    else
      Tagged.new(val, di)
    end
  when 2; take(val).force_encoding(Encoding::BINARY)
  when 3; take(val).force_encoding(Encoding::UTF_8)
  when 4; atleast(val); Array.new(val) { decode_item }
  when 5; atleast(val<<1); Hash[Array.new(val) {[decode_item, decode_item]}]
  end
end

#decode_item_finalObject



324
325
326
327
328
# File 'lib/cbor-pure.rb', line 324

def decode_item_final
  val = decode_item
  raise "extra bytes follow after a deserialized object of #@pos bytes" if @pos != @buffer.size
  val
end

#decode_item_streaming(ib, breakable) ⇒ Object



248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
# File 'lib/cbor-pure.rb', line 248

def decode_item_streaming(ib, breakable)
  case ib >>= 5
  when 2, 3
    want_encoding = MT_TO_ENCODING[ib]
    subs = []
    while (element = decode_item(true)) != BREAK
      raise "non-string (#{element.inspect}) in streaming string" unless String === element
      raise "bytes/text mismatch (#{element.encoding} != #{want_encoding}) in streaming string" unless element.encoding == want_encoding
      subs << element
    end
    result = subs.join.cbor_stream!(subs.map(&:length)).force_encoding(want_encoding)
  when 4
    result = Array.new;
    result.cbor_stream!
    while (element = decode_item(true)) != BREAK
      result << element
    end
    result
  when 5
    result = Hash.new
    result.cbor_stream!
    while (key = decode_item(true)) != BREAK
      result[key] = decode_item
    end
    result
  when 7
    raise "break stop code outside indefinite length item" unless breakable
    BREAK
  else raise "unknown ib #{ib} for additional information 31"
  end
end

#decode_item_with_restObject



330
331
332
333
# File 'lib/cbor-pure.rb', line 330

def decode_item_with_rest
  val = decode_item
  [val, @buffer[@pos..-1]]
end

#decode_itemsObject



335
336
337
338
339
340
341
# File 'lib/cbor-pure.rb', line 335

def decode_items
  ret = []
  while @pos != buffer.size
    ret << decode_item
  end
  ret
end

#empty?Boolean

Returns:

  • (Boolean)


343
344
345
# File 'lib/cbor-pure.rb', line 343

def empty?
  @pos == buffer.size
end

#head(ib, n) ⇒ Object



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/cbor-pure.rb', line 105

def head(ib, n)
  @buffer <<
    case n
    when 0...24
      [ib + n].pack("C")
    when 0...256
      [ib + 24, n].pack("CC")
    when 0...65536
      [ib + 25, n].pack("Cn")
    when 0...4294967296
      [ib + 26, n].pack("CN")
    when 0...18446744073709551616
      [ib + 27, n].pack("CQ>")
    else
      yield                   # throw back to caller
    end
end

#pretty_itemObject



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
# File 'lib/cbor-pretty.rb', line 64

def pretty_item
  ib = take_and_print(1, '   ' * @indent).ord
  ai = ib & 0x1F
  val = case ai
        when 0...24; ai
        when 24; take_and_print(1, ' ').ord
        when 25; take_and_print(2, ' ').unpack("n").first
        when 26; (s = take_and_print(4, ' ')).unpack("N").first
        when 27; (s = take_and_print(8, ' ')).unpack("Q>").first
        when 31; return pretty_item_streaming(ib)
        else raise "unknown additional information #{ai} in ib #{ib}"
        end
  @out << " # #{MT_NAMES[ib >> 5]}(#{val})\n"
  @indent += 1
  case ib >>= 5
  when 6
    pretty_item
  when 2, 3
    @out << '   ' * (@indent)
    s = take_and_print(val)
    @out << " # #{s.force_encoding(Encoding::UTF_8).to_json_514_workaround rescue s.inspect}"
    @out << "\n"
  when 4; val.times { pretty_item }
  when 5; val.times { pretty_item; pretty_item}
  end
  @indent -= 1
  nil
end

#pretty_item_final(indent = 0, max_target = 40, seq = false) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/cbor-pretty.rb', line 93

def pretty_item_final(indent = 0, max_target = 40, seq = false)
  @out = ''
  @indent = indent
  pretty_item
  unless seq
    raise if @pos != @buffer.size
  end
  target = [@out.each_line.map {|ln| ln.index('#') || 0}.max, max_target].min
  @out.each_line.map {|ln|
    col = ln.index('#')
    if col && col < target
      ln[col, 0] = ' ' * (target - col)
    end
    ln
  }.join
end

#pretty_item_streaming(ib) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/cbor-pretty.rb', line 47

def pretty_item_streaming(ib)
  res = nil
  @out << " # #{MT_NAMES[ib >> 5]}(*)\n"
  @indent += 1
  case ib >>= 5
  when 2, 3, 4, 5
    while (element = pretty_item) != BREAK
    end
  when 7; res = BREAK
  else raise "unknown ib #{ib} for additional information 31"
  end
  @indent -= 1
  res
end

#take(n) ⇒ Object

Raises:



239
240
241
242
243
244
# File 'lib/cbor-pure.rb', line 239

def take(n)
  opos = @pos
  @pos += n
  raise OutOfBytesError.new(@pos - @buffer.bytesize) if @pos > @buffer.bytesize
  @buffer[opos, n]
end

#take_and_print(n, prefix = '') ⇒ Object



40
41
42
43
44
45
# File 'lib/cbor-pretty.rb', line 40

def take_and_print(n, prefix = '')
  s = take(n)
  @out << prefix
  @out << s.hexbytes
  s
end