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.



125
126
127
128
# File 'lib/cbor-pure.rb', line 125

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

Instance Attribute Details

#bufferObject (readonly)

Returns the value of attribute buffer.



124
125
126
# File 'lib/cbor-pure.rb', line 124

def buffer
  @buffer
end

#warningsObject

Returns the value of attribute warnings.



46
47
48
# File 'lib/cbor-pretty.rb', line 46

def warnings
  @warnings
end

Instance Method Details

#add(d) ⇒ Object



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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
# File 'lib/cbor-pure.rb', line 190

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 warning = d.cbor_map_lost_warning
      (@buffer.cbor_warnings ||= {})[@buffer.bytesize] = warning
    end
    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



150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/cbor-pure.rb', line 150

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:



262
263
264
265
# File 'lib/cbor-pure.rb', line 262

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

#bignum_to_bytes(d) ⇒ Object



181
182
183
184
185
186
187
188
# File 'lib/cbor-pure.rb', line 181

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



310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
# File 'lib/cbor-pure.rb', line 310

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;
      v = s.unpack("g").first # cannot go directly from val in Ruby
      if v.nan?
        # work around quiet bit always set with unpack("g"):
        # https://bugs.ruby-lang.org/issues/20662
        qbit = s.getbyte(1)[6]
        vbytes = [v].pack("G")
        vbytes.setbyte(1, vbytes.getbyte(1) & 0xf7 | qbit << 3)
        v = vbytes.unpack("G").first
      end
      v
    when 27; s.unpack("G").first # cannot go directly from val 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.cbor_from_entries(Array.new(val) {[decode_item, decode_item]})
  end
end

#decode_item_finalObject



364
365
366
367
368
# File 'lib/cbor-pure.rb', line 364

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



276
277
278
279
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
# File 'lib/cbor-pure.rb', line 276

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!
    entries = []
    while (key = decode_item(true)) != BREAK
      value = decode_item
      result.cbor_map_push(key, value, entries)
    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



370
371
372
373
# File 'lib/cbor-pure.rb', line 370

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

#decode_itemsObject



375
376
377
378
379
380
381
# File 'lib/cbor-pure.rb', line 375

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

#empty?Boolean

Returns:

  • (Boolean)


383
384
385
# File 'lib/cbor-pure.rb', line 383

def empty?
  @pos == buffer.size
end

#head(ib, n) ⇒ Object



130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/cbor-pure.rb', line 130

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



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

def pretty_item
  @item_pos = @pos
  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})#{warning_message}\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



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/cbor-pretty.rb', line 108

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



61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/cbor-pretty.rb', line 61

def pretty_item_streaming(ib)
  res = nil
  @out << " # #{MT_NAMES[ib >> 5]}(*)#{warning_message}\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:



267
268
269
270
271
272
# File 'lib/cbor-pure.rb', line 267

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



48
49
50
51
52
53
# File 'lib/cbor-pretty.rb', line 48

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

#warning_messageObject



55
56
57
58
59
# File 'lib/cbor-pretty.rb', line 55

def warning_message
  if warnings
    warnings[@item_pos]
  end
end