Class: Tina4Ultipa::Codec::Reader

Inherits:
Object
  • Object
show all
Defined in:
lib/tina4_ultipa/codec.rb

Overview

Walks a self-describing binary blob, reading little-endian fixed-width ints, length-prefixed strings, and TypedValueEntry composites. Mirrors Python _Reader.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data) ⇒ Reader

Returns a new instance of Reader.



203
204
205
206
# File 'lib/tina4_ultipa/codec.rb', line 203

def initialize(data)
  @d = (data || "".b).dup.force_encoding("BINARY")
  @p = 0
end

Instance Attribute Details

#dObject

Returns the value of attribute d.



201
202
203
# File 'lib/tina4_ultipa/codec.rb', line 201

def d
  @d
end

#pObject

Returns the value of attribute p.



201
202
203
# File 'lib/tina4_ultipa/codec.rb', line 201

def p
  @p
end

Instance Method Details

#propsObject

uint16 count of (uint16 keyLen + key + TypedValueEntry) property pairs.



251
252
253
254
255
256
257
258
# File 'lib/tina4_ultipa/codec.rb', line 251

def props
  out = {}
  u16.times do
    key = str
    out[key] = tve
  end
  out
end

#strObject

uint16 length prefix + that many UTF-8 bytes.



233
234
235
236
237
238
# File 'lib/tina4_ultipa/codec.rb', line 233

def str
  n = u16
  v = @d.byteslice(@p, n).dup.force_encoding("UTF-8")
  @p += n
  v
end

#tveObject

A TypedValueEntry: type u8, isNull u8, len u32, data -> decoded value (nil if null).



241
242
243
244
245
246
247
248
# File 'lib/tina4_ultipa/codec.rb', line 241

def tve
  t = u8
  is_null = u8
  n = u32
  data = @d.byteslice(@p, n)
  @p += n
  is_null.zero? ? Codec.scalar(t, data) : nil
end

#u16Object



214
215
216
217
218
# File 'lib/tina4_ultipa/codec.rb', line 214

def u16
  v = @d.byteslice(@p, 2).unpack1("S<")
  @p += 2
  v
end

#u32Object



220
221
222
223
224
# File 'lib/tina4_ultipa/codec.rb', line 220

def u32
  v = @d.byteslice(@p, 4).unpack1("L<")
  @p += 4
  v
end

#u64Object



226
227
228
229
230
# File 'lib/tina4_ultipa/codec.rb', line 226

def u64
  v = @d.byteslice(@p, 8).unpack1("Q<")
  @p += 8
  v
end

#u8Object



208
209
210
211
212
# File 'lib/tina4_ultipa/codec.rb', line 208

def u8
  v = @d.getbyte(@p)
  @p += 1
  v
end