nl 0.4.2 → 0.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (46) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +26 -0
  3. data/lib/nl/async/dispatcher.rb +2 -2
  4. data/lib/nl/attribute_set.rb +19 -153
  5. data/lib/nl/attributes.rb +193 -0
  6. data/lib/nl/blocking_transport.rb +5 -5
  7. data/lib/nl/datagram.rb +4 -4
  8. data/lib/nl/datatypes/checked.rb +66 -0
  9. data/lib/nl/datatypes/defaults.rb +13 -0
  10. data/lib/nl/datatypes/scalar.rb +28 -0
  11. data/lib/nl/datatypes.rb +97 -125
  12. data/lib/nl/decoder.rb +79 -25
  13. data/lib/nl/encoder.rb +4 -6
  14. data/lib/nl/endian.rb +5 -3
  15. data/lib/nl/exchange.rb +18 -17
  16. data/lib/nl/genl/protocol.rb +16 -3
  17. data/lib/nl/genl/wire.rb +9 -3
  18. data/lib/nl/genl.rb +0 -18
  19. data/lib/nl/message.rb +21 -0
  20. data/lib/nl/notification_router.rb +2 -5
  21. data/lib/nl/raw/protocol.rb +27 -21
  22. data/lib/nl/raw/wire.rb +20 -50
  23. data/lib/nl/raw.rb +1 -51
  24. data/lib/nl/{sub_message.rb → selector.rb} +13 -23
  25. data/lib/nl/version.rb +1 -1
  26. data/lib/nl.rb +2 -2
  27. data/sig/generated/nl/attribute_set.rbs +21 -29
  28. data/sig/generated/nl/attributes.rbs +51 -0
  29. data/sig/generated/nl/datagram.rbs +4 -4
  30. data/sig/generated/nl/datatypes/checked.rbs +31 -0
  31. data/sig/generated/nl/datatypes/defaults.rbs +11 -0
  32. data/sig/generated/nl/datatypes/scalar.rbs +11 -0
  33. data/sig/generated/nl/datatypes.rbs +38 -34
  34. data/sig/generated/nl/decoder.rbs +12 -4
  35. data/sig/generated/nl/encoder.rbs +1 -1
  36. data/sig/generated/nl/exchange.rbs +2 -2
  37. data/sig/generated/nl/genl/protocol.rbs +3 -1
  38. data/sig/generated/nl/genl.rbs +0 -15
  39. data/sig/generated/nl/message.rbs +14 -0
  40. data/sig/generated/nl/raw/protocol.rbs +29 -43
  41. data/sig/generated/nl/raw/wire.rbs +0 -36
  42. data/sig/generated/nl/raw.rbs +0 -35
  43. data/sig/generated/nl/{sub_message.rbs → selector.rbs} +8 -19
  44. metadata +14 -6
  45. data/lib/nl/structured_payload.rb +0 -61
  46. data/sig/generated/nl/structured_payload.rbs +0 -25
data/lib/nl/datatypes.rb CHANGED
@@ -1,5 +1,8 @@
1
1
  require_relative 'raw/wire'
2
2
  require_relative 'bitfield32'
3
+ require_relative 'datatypes/defaults'
4
+ require_relative 'datatypes/checked'
5
+ require_relative 'datatypes/scalar'
3
6
 
4
7
  module Nl
5
8
  # @rbs!
@@ -11,57 +14,25 @@ module Nl
11
14
  # end
12
15
 
13
16
  module DataTypes
14
- class Base
15
- def initialize(check: nil)
16
- @check = check
17
- end
18
-
19
- def nlattr_type_flags(_value = nil, context: nil)
20
- 0
21
- end
22
-
23
- def coerce(value, context: nil)
24
- value
25
- end
26
-
27
- private def checked(value)
28
- @check&.call(value)
29
- value
30
- end
31
- end
32
-
33
- class Scalar < Base
34
- def initialize(type, check:)
35
- super(check:)
36
- @type = type
37
- end
17
+ class VariableInteger
18
+ include Defaults
38
19
 
39
- def encode(encoder, value, context: nil)
40
- encoder.put_value(@type, checked(value || 0))
41
- end
42
-
43
- def decode(decoder, context: nil, nlattr_type_flags: 0)
44
- checked(decoder.get_value(@type))
45
- end
46
- end
47
-
48
- class VariableInteger < Base
49
20
  UINT32_RANGE = (0...2**32)
50
21
  UINT64_RANGE = (0...2**64)
51
22
  SINT32_RANGE = (-(2**31)...2**31)
52
23
  SINT64_RANGE = (-(2**63)...2**63)
53
24
 
54
- def initialize(byte_order, signed:, check:)
55
- super(check:)
25
+ def initialize(byte_order, signed:)
56
26
  prefix = signed ? 'S' : 'U'
57
27
  @type32 = byte_order.const_get("#{prefix}32")
58
28
  @type64 = byte_order.const_get("#{prefix}64")
29
+ @scalar32 = Scalar[@type32].new
30
+ @scalar64 = Scalar[@type64].new
59
31
  @range32 = signed ? SINT32_RANGE : UINT32_RANGE
60
32
  @range64 = signed ? SINT64_RANGE : UINT64_RANGE
61
33
  end
62
34
 
63
35
  def encode(encoder, value, context: nil)
64
- value = checked(value || 0)
65
36
  unless @range64.cover?(value)
66
37
  raise RangeError, "integer #{value.inspect} is outside the #{@range64.begin}...#{@range64.end} range"
67
38
  end
@@ -71,38 +42,36 @@ module Nl
71
42
  end
72
43
 
73
44
  def decode(decoder, context: nil, nlattr_type_flags: 0)
74
- type = case decoder.remaining
75
- when 4 then @type32
76
- when 8 then @type64
45
+ scalar = case decoder.remaining
46
+ when 4 then @scalar32
47
+ when 8 then @scalar64
77
48
  else
78
49
  raise Decoder::Error, "variable integer payload must be 4 or 8 bytes, got #{decoder.remaining}"
79
50
  end
80
- checked(decoder.get_value(type))
51
+ scalar.decode(decoder)
81
52
  end
82
53
  end
83
54
 
84
- class String < Base
85
- def initialize(check:)
86
- super(check:)
87
- end
55
+ class String
56
+ include Defaults
88
57
 
89
58
  def encode(encoder, value, context: nil)
90
- encoder.put_zstring(checked(value))
59
+ encoder.put_zstring(value)
91
60
  end
92
61
 
93
62
  def decode(decoder, context: nil, nlattr_type_flags: 0)
94
- checked(decoder.get_zstring)
63
+ decoder.get_zstring
95
64
  end
96
65
  end
97
66
 
98
- class Binary < Base
99
- def initialize(length: nil, check:)
100
- super(check:)
67
+ class Binary
68
+ include Defaults
69
+
70
+ def initialize(length: nil)
101
71
  @length = length
102
72
  end
103
73
 
104
74
  def encode(encoder, value, context: nil)
105
- value = checked(value)
106
75
  if @length && value.bytesize != @length
107
76
  raise ArgumentError, "binary value must be exactly #{@length} bytes, got #{value.bytesize}"
108
77
  end
@@ -110,27 +79,22 @@ module Nl
110
79
  end
111
80
 
112
81
  def decode(decoder, context: nil, nlattr_type_flags: 0)
113
- checked(decoder.get_string(@length || decoder.remaining))
82
+ @length ? decoder.get_string(@length) : decoder.get_string
114
83
  end
115
84
  end
116
85
 
117
86
  # An array whose elements are packed consecutively without per-element
118
87
  # headers or alignment padding.
119
- class PackedArray < Base
120
- def initialize(sub_type, check: nil)
121
- super(check:)
88
+ class PackedArray
89
+ include Defaults
90
+
91
+ def initialize(sub_type)
122
92
  @sub_type = sub_type
123
93
  end
124
94
 
125
95
  def encode(encoder, values, context: nil)
126
96
  values = coerce(values, context:)
127
- if @check
128
- temporary = Encoder.new
129
- values.each { @sub_type.encode(temporary, it, context:) }
130
- encoder.put_string(checked(temporary.buffer.get_string))
131
- else
132
- values.each { @sub_type.encode(encoder, it, context:) }
133
- end
97
+ values.each { @sub_type.encode(encoder, it, context:) }
134
98
  end
135
99
 
136
100
  def coerce(values, context: nil)
@@ -138,20 +102,16 @@ module Nl
138
102
  end
139
103
 
140
104
  def decode(decoder, context: nil, nlattr_type_flags: 0)
141
- if @check
142
- payload = checked(decoder.get_string)
143
- decoder = Decoder.new(IO::Buffer.for(payload))
144
- end
145
-
146
105
  result = []
147
106
  result << @sub_type.decode(decoder, context:, nlattr_type_flags: 0) while decoder.available?
148
107
  result
149
108
  end
150
109
  end
151
110
 
152
- class Struct < Base
153
- def initialize(type, check: nil, consume_remaining: false)
154
- super(check:)
111
+ class Struct
112
+ include Defaults
113
+
114
+ def initialize(type, consume_remaining: false)
155
115
  @type = type
156
116
  @consume_remaining = consume_remaining
157
117
  end
@@ -159,10 +119,7 @@ module Nl
159
119
  def coerce(value, context: nil)
160
120
  return value unless value.is_a?(Hash)
161
121
 
162
- unknown = value.keys - @type::MEMBERS.keys
163
- raise ArgumentError, "unknown struct members: #{unknown.join(', ')}" unless unknown.empty?
164
-
165
- @type.new(*@type::MEMBERS.map { |name, datatype| datatype.coerce(value[name], context:) })
122
+ @type.from_params(value, context:)
166
123
  end
167
124
 
168
125
  def encode(encoder, value, context: nil)
@@ -170,33 +127,19 @@ module Nl
170
127
  raise TypeError, "value must be an instance of #{@type}"
171
128
  end
172
129
 
173
- if @check
174
- temporary = Encoder.new
175
- value.encode(temporary)
176
- encoder.put_string(checked(temporary.buffer.get_string))
177
- else
178
- value.encode(encoder)
179
- end
130
+ value.encode(encoder)
180
131
  end
181
132
 
182
133
  def decode(decoder, context: nil, nlattr_type_flags: 0)
183
- unless @check
184
- value = @type.decode(decoder)
185
- decoder.skip if @consume_remaining
186
- return value
187
- end
188
-
189
- payload = checked(decoder.get_string)
190
- nested = Decoder.new(IO::Buffer.for(payload))
191
- nested.limit(payload.bytesize) do
192
- value = @type.decode(it)
193
- it.skip if @consume_remaining
194
- value
195
- end
134
+ value = @type.decode(decoder)
135
+ decoder.skip if @consume_remaining
136
+ value
196
137
  end
197
138
  end
198
139
 
199
- class Flag < Base
140
+ class Flag
141
+ include Defaults
142
+
200
143
  def encode(encoder, value, context: nil)
201
144
  # flag attribute has no payload; presence encodes true
202
145
  end
@@ -206,7 +149,9 @@ module Nl
206
149
  end
207
150
  end
208
151
 
209
- class Bitfield32 < Base
152
+ class Bitfield32
153
+ include Defaults
154
+
210
155
  def encode(encoder, value, context: nil)
211
156
  unless value.is_a?(Nl::Bitfield32)
212
157
  raise TypeError, 'bitfield32 value must be an Nl::Bitfield32'
@@ -217,13 +162,15 @@ module Nl
217
162
  end
218
163
 
219
164
  def decode(decoder, context: nil, nlattr_type_flags: 0)
220
- value = decoder.get_value(Endian::Host::U32)
221
- selector = decoder.get_value(Endian::Host::U32)
165
+ value = decoder.get_host_u32
166
+ selector = decoder.get_host_u32
222
167
  Nl::Bitfield32.new(value, selector)
223
168
  end
224
169
  end
225
170
 
226
- class Pad < Base
171
+ class Pad
172
+ include Defaults
173
+
227
174
  def initialize(length = nil)
228
175
  @length = length
229
176
  end
@@ -238,7 +185,9 @@ module Nl
238
185
  end
239
186
  end
240
187
 
241
- class NestedAttributes < Base
188
+ class NestedAttributes
189
+ include Defaults
190
+
242
191
  def initialize(attribute_set, selector_bindings: [])
243
192
  @attribute_set = attribute_set
244
193
  @selector_bindings = selector_bindings
@@ -249,7 +198,7 @@ module Nl
249
198
  end
250
199
 
251
200
  def coerce(value, context: nil)
252
- value.is_a?(Hash) ? @attribute_set.build_attributes(value, external_selectors: selectors(context)) : value
201
+ value.is_a?(Hash) ? @attribute_set.build_instance(value, external_selectors: selectors(context)) : value
253
202
  end
254
203
 
255
204
  def encode(encoder, value, context: nil)
@@ -265,11 +214,15 @@ module Nl
265
214
  end
266
215
 
267
216
  private def selectors(context)
217
+ return Selector::EMPTY_VALUES if @selector_bindings.empty?
218
+
268
219
  @selector_bindings.map { it.read(context) }
269
220
  end
270
221
  end
271
222
 
272
- class SubMessage < Base
223
+ class SubMessage
224
+ include Defaults
225
+
273
226
  Format = Data.define(:type, :selector_bindings, :nested)
274
227
 
275
228
  def initialize(selector, formats)
@@ -280,7 +233,7 @@ module Nl
280
233
  end
281
234
 
282
235
  def nlattr_type_flags(value = nil, context:)
283
- if value.is_a?(RawSubMessage)
236
+ if value.is_a?(RawAttributeValue)
284
237
  @selector.read(context)
285
238
  return value.nlattr_type_flags
286
239
  end
@@ -293,20 +246,20 @@ module Nl
293
246
  end
294
247
 
295
248
  def coerce(value, context:)
296
- if value.is_a?(RawSubMessage)
249
+ if value.is_a?(RawAttributeValue)
297
250
  @selector.read(context) # ensure selector value is present
298
251
  return value
299
252
  end
300
253
 
301
254
  format = @selector.select(context, @formats)
302
255
  return value if value.is_a?(format.type)
303
- return format.type.from_params(value, external_selectors: selectors(format, context)) if value.is_a?(Hash)
256
+ return format.type.build_instance(value, external_selectors: selectors(format, context)) if value.is_a?(Hash)
304
257
 
305
258
  raise TypeError, "value must be a Hash or an instance of #{format.type}"
306
259
  end
307
260
 
308
261
  def encode(encoder, value, context: nil)
309
- if value.is_a?(RawSubMessage)
262
+ if value.is_a?(RawAttributeValue)
310
263
  @selector.read(context)
311
264
  return value.encode(encoder)
312
265
  end
@@ -320,7 +273,7 @@ module Nl
320
273
 
321
274
  def decode(decoder, context:, nlattr_type_flags: 0)
322
275
  format = @selector.select(context, @formats) do
323
- return RawSubMessage.new(
276
+ return RawAttributeValue.new(
324
277
  decoder.get_string,
325
278
  nlattr_type_flags:,
326
279
  )
@@ -329,6 +282,8 @@ module Nl
329
282
  end
330
283
 
331
284
  private def selectors(format, context)
285
+ return Selector::EMPTY_VALUES if format.selector_bindings.empty?
286
+
332
287
  format.selector_bindings.map { it.read(context) }
333
288
  end
334
289
  end
@@ -336,7 +291,9 @@ module Nl
336
291
  # Nested attributes whose type numbers are keys rather than members of
337
292
  # an attribute set. Each level is returned as an Integer-keyed Hash;
338
293
  # values at the innermost level are decoded as @attribute_set.
339
- class NestTypeValue < Base
294
+ class NestTypeValue
295
+ include Defaults
296
+
340
297
  def initialize(attribute_set, levels, selector_bindings: [])
341
298
  raise ArgumentError, 'levels must be positive' unless levels.positive?
342
299
 
@@ -360,9 +317,9 @@ module Nl
360
317
 
361
318
  private def encode_level(encoder, values, levels, external_selectors)
362
319
  values.each do |type, value|
363
- nlattr = Raw::NlAttr.new(0, type | Raw::NLA_F_NESTED)
364
320
  encoder.measure(Endian::Host::U16) do
365
- nlattr.encode(encoder)
321
+ encoder.put_value(Endian::Host::U16, 0) # nla_len
322
+ encoder.put_value(Endian::Host::U16, type | Raw::NLA_F_NESTED) # nla_type
366
323
  if levels == 1
367
324
  if external_selectors.empty?
368
325
  value.encode(encoder)
@@ -373,7 +330,7 @@ module Nl
373
330
  encode_level(encoder, value, levels - 1, external_selectors)
374
331
  end
375
332
  end
376
- encoder.align_to(Raw::NLA_ALIGNTO)
333
+ encoder.align_to_4 # NLA_ALIGNTO
377
334
  end
378
335
  end
379
336
 
@@ -381,8 +338,8 @@ module Nl
381
338
  values.transform_values do |value|
382
339
  if levels == 1
383
340
  if value.is_a?(Hash)
384
- external_selectors.empty? ? @attribute_set.build_attributes(value) :
385
- @attribute_set.build_attributes(value, external_selectors:)
341
+ external_selectors.empty? ? @attribute_set.build_instance(value) :
342
+ @attribute_set.build_instance(value, external_selectors:)
386
343
  else
387
344
  value
388
345
  end
@@ -395,9 +352,14 @@ module Nl
395
352
  private def decode_level(decoder, levels, external_selectors)
396
353
  result = {}
397
354
  while decoder.available?(Raw::NLA_HDRLEN)
398
- nlattr = Raw::NlAttr.decode(decoder)
399
- type = nlattr.type & Raw::NLA_TYPE_MASK
400
- value = decoder.limit(nlattr.len - Raw::NLA_HDRLEN) do
355
+ # Read the attribute header without allocating a wrapper.
356
+ length = decoder.get_host_u16
357
+ type = decoder.get_host_u16 & Raw::NLA_TYPE_MASK
358
+ if length < Raw::NLA_HDRLEN
359
+ raise Decoder::Error,
360
+ "attribute length must be at least #{Raw::NLA_HDRLEN} bytes, got #{length}"
361
+ end
362
+ value = decoder.limit(length - Raw::NLA_HDRLEN) do
401
363
  if levels == 1
402
364
  external_selectors.empty? ? @attribute_set.decode(decoder) :
403
365
  @attribute_set.decode(decoder, external_selectors:)
@@ -405,30 +367,34 @@ module Nl
405
367
  decode_level(decoder, levels - 1, external_selectors)
406
368
  end
407
369
  end
408
- decoder.align_to(Raw::NLA_ALIGNTO)
370
+ decoder.align_to_4 # NLA_ALIGNTO
409
371
  result[type] = value
410
372
  end
411
373
  result
412
374
  end
413
375
 
414
376
  private def selectors(context)
377
+ return Selector::EMPTY_VALUES if @selector_bindings.empty?
378
+
415
379
  @selector_bindings.map { it.read(context) }
416
380
  end
417
381
  end
418
382
 
419
- class IndexedArray < Base
383
+ class IndexedArray
384
+ include Defaults
385
+
420
386
  def initialize(sub_type)
421
387
  @sub_type = sub_type
422
388
  end
423
389
 
424
390
  def encode(encoder, values, context: nil)
425
391
  values.each_with_index do |value, i|
426
- nlattr = Raw::NlAttr.new(0, i + 1)
427
392
  encoder.measure(Endian::Host::U16) do
428
- nlattr.encode(encoder)
393
+ encoder.put_value(Endian::Host::U16, 0) # nla_len
394
+ encoder.put_value(Endian::Host::U16, i + 1) # nla_type
429
395
  @sub_type.encode(encoder, value, context:)
430
396
  end
431
- encoder.align_to(Raw::NLA_ALIGNTO)
397
+ encoder.align_to_4 # NLA_ALIGNTO
432
398
  end
433
399
  end
434
400
 
@@ -439,12 +405,18 @@ module Nl
439
405
  def decode(decoder, context: nil, nlattr_type_flags: 0)
440
406
  result = []
441
407
  while decoder.available?
442
- nlattr = Raw::NlAttr.decode(decoder)
443
- flags = nlattr.type & (Raw::NLA_F_NESTED | Raw::NLA_F_NET_BYTEORDER)
444
- element = decoder.limit(nlattr.len - Raw::NLA_HDRLEN) do
408
+ # Read the attribute header without allocating a wrapper.
409
+ length = decoder.get_host_u16
410
+ type = decoder.get_host_u16
411
+ if length < Raw::NLA_HDRLEN
412
+ raise Decoder::Error,
413
+ "attribute length must be at least #{Raw::NLA_HDRLEN} bytes, got #{length}"
414
+ end
415
+ flags = type & (Raw::NLA_F_NESTED | Raw::NLA_F_NET_BYTEORDER)
416
+ element = decoder.limit(length - Raw::NLA_HDRLEN) do
445
417
  @sub_type.decode(decoder, context:, nlattr_type_flags: flags)
446
418
  end
447
- decoder.align_to(Raw::NLA_ALIGNTO)
419
+ decoder.align_to_4 # NLA_ALIGNTO
448
420
  result << element
449
421
  end
450
422
  result
data/lib/nl/decoder.rb CHANGED
@@ -1,3 +1,5 @@
1
+ require_relative 'endian'
2
+
1
3
  module Nl
2
4
  class Decoder
3
5
  class Error < StandardError; end
@@ -8,10 +10,57 @@ module Nl
8
10
  def initialize(message = 'unterminated string') = super
9
11
  end
10
12
 
13
+ VALUE_READERS = {
14
+ U8: :get_U8, S8: :get_S8,
15
+ u16: :get_u16, U16: :get_U16,
16
+ s16: :get_s16, S16: :get_S16,
17
+ u32: :get_u32, U32: :get_U32,
18
+ s32: :get_s32, S32: :get_S32,
19
+ u64: :get_u64, U64: :get_U64,
20
+ s64: :get_s64, S64: :get_S64,
21
+ f32: :get_f32, F32: :get_F32,
22
+ f64: :get_f64, F64: :get_F64,
23
+ }.freeze
24
+
25
+ VALUE_READERS.each do |type, reader|
26
+ # Emit a literal buffer type so the JIT can specialize the load.
27
+ class_eval <<~RUBY, __FILE__, __LINE__ + 1
28
+ def #{reader}
29
+ position = @position
30
+ nposition = position + #{IO::Buffer.size_of(type)}
31
+ raise OutOfBounds if nposition > @limit
32
+ value = @buffer.get_value(#{type.inspect}, position)
33
+ @position = nposition
34
+ value
35
+ end
36
+ RUBY
37
+ end
38
+
39
+ alias_method :get_host_u8, :"get_#{Endian::Host::U8}"
40
+ alias_method :get_host_s8, :"get_#{Endian::Host::S8}"
41
+ alias_method :get_host_u16, :"get_#{Endian::Host::U16}"
42
+ alias_method :get_host_s16, :"get_#{Endian::Host::S16}"
43
+ alias_method :get_host_u32, :"get_#{Endian::Host::U32}"
44
+ alias_method :get_host_s32, :"get_#{Endian::Host::S32}"
45
+ alias_method :get_host_u64, :"get_#{Endian::Host::U64}"
46
+ alias_method :get_host_s64, :"get_#{Endian::Host::S64}"
47
+ alias_method :get_host_f32, :"get_#{Endian::Host::F32}"
48
+ alias_method :get_host_f64, :"get_#{Endian::Host::F64}"
49
+ alias_method :get_host_uint, :"get_#{Endian::Host::UINT}"
50
+ alias_method :get_host_sint, :"get_#{Endian::Host::SINT}"
51
+ alias_method :get_host_ulong, :"get_#{Endian::Host::ULONG}"
52
+ alias_method :get_host_slong, :"get_#{Endian::Host::SLONG}"
53
+ alias_method :get_host_ullong, :"get_#{Endian::Host::ULLONG}"
54
+ alias_method :get_host_sllong, :"get_#{Endian::Host::SLLONG}"
55
+
56
+ alias get_host_int get_host_sint
57
+ alias get_host_long get_host_slong
58
+ alias get_host_llong get_host_sllong
59
+
11
60
  def initialize(buffer, offset = 0, length = buffer.size - offset)
12
61
  @buffer = buffer
13
62
  @position = offset
14
- @limit = length
63
+ @limit = offset + length
15
64
  end
16
65
 
17
66
  def available?(size = 1)
@@ -24,9 +73,12 @@ module Nl
24
73
 
25
74
  def limit(size)
26
75
  orig_limit = @limit
27
- @limit = @position + size
28
- result = yield self
29
- if @limit != @position
76
+ new_limit = @position + size
77
+ raise OutOfBounds if size < 0 || new_limit > orig_limit
78
+
79
+ @limit = new_limit
80
+ result = yield
81
+ if @position != new_limit
30
82
  raise "Not all bytes upto specified limit is consumed"
31
83
  end
32
84
  result
@@ -34,6 +86,13 @@ module Nl
34
86
  @limit = orig_limit
35
87
  end
36
88
 
89
+ def lookahead
90
+ orig_position = @position
91
+ yield self
92
+ ensure
93
+ @position = orig_position
94
+ end
95
+
37
96
  def skip(length = @limit - @position)
38
97
  nposition = @position + length
39
98
  raise OutOfBounds if nposition > @limit
@@ -41,23 +100,25 @@ module Nl
41
100
  end
42
101
 
43
102
  def get_string(length = @limit - @position)
44
- nposition = @position + length
45
- raise OutOfBounds if length.negative? || nposition > @limit
46
- value = @buffer.get_string(@position, length)
103
+ position = @position
104
+ nposition = position + length
105
+ raise OutOfBounds if length < 0 || nposition > @limit
106
+ value = @buffer.get_string(position, length)
47
107
  @position = nposition
48
108
  value
49
109
  end
50
110
 
51
- def get_buffer(length = @limit - @position)
52
- nposition = @position + length
53
- raise OutOfBounds if length.negative? || nposition > @limit
54
- value = @buffer.slice(@position, length)
111
+ def slice(length = @limit - @position)
112
+ position = @position
113
+ nposition = position + length
114
+ raise OutOfBounds if length < 0 || nposition > @limit
115
+ value = self.class.new(@buffer, position, length)
55
116
  @position = nposition
56
117
  value
57
118
  end
58
119
 
59
120
  def get_zstring(unterminated_ok: false)
60
- nposition = @position
121
+ position = nposition = @position
61
122
  nul_found = false
62
123
  while nposition < @limit
63
124
  c = @buffer.get_value(:U8, nposition)
@@ -68,29 +129,22 @@ module Nl
68
129
  end
69
130
  end
70
131
  raise Unterminated if !nul_found && !unterminated_ok
71
- value = @buffer.get_string(@position, nposition - @position - (nul_found ? 1 : 0))
72
- @position = nposition
73
- value
74
- end
75
-
76
- def get_value(type)
77
- nposition = @position + IO::Buffer.size_of(type)
78
- raise OutOfBounds if nposition > @limit
79
- value = @buffer.get_value(type, @position)
132
+ value = @buffer.get_string(position, nposition - position - (nul_found ? 1 : 0))
80
133
  @position = nposition
81
134
  value
82
135
  end
83
136
 
84
137
  def get_values(types)
85
- nposition = @position + IO::Buffer.size_of(types)
138
+ position = @position
139
+ nposition = position + IO::Buffer.size_of(types)
86
140
  raise OutOfBounds if nposition > @limit
87
- values = @buffer.get_values(types, @position)
141
+ values = @buffer.get_values(types, position)
88
142
  @position = nposition
89
143
  values
90
144
  end
91
145
 
92
- def align_to(alignment)
93
- nposition = (@position + alignment - 1) & ~(alignment - 1)
146
+ def align_to_4
147
+ nposition = (@position + 3) & -4
94
148
  raise OutOfBounds if nposition > @limit
95
149
  @position = nposition
96
150
  end
data/lib/nl/encoder.rb CHANGED
@@ -35,12 +35,10 @@ module Nl
35
35
  @position = @buffer.set_values(types, @position, values)
36
36
  end
37
37
 
38
- def align_to(alignment)
39
- if alignment > 1
40
- nposition = (@position + alignment - 1) & ~(alignment - 1)
41
- reserve(nposition - @position)
42
- @position = nposition
43
- end
38
+ def align_to_4
39
+ nposition = (@position + 3) & -4
40
+ reserve(nposition - @position)
41
+ @position = nposition
44
42
  end
45
43
 
46
44
  def put_value_at(offset, type, value)
data/lib/nl/endian.rb CHANGED
@@ -1,12 +1,14 @@
1
+ require 'rbconfig/sizeof'
2
+
1
3
  module Nl
2
4
  # Byte-order helpers
3
5
  module Endian
4
6
  # sizeof(int)
5
- SIZEOF_INT = [1].pack('i!').bytesize
7
+ SIZEOF_INT = RbConfig::SIZEOF.fetch('int')
6
8
  # sizeof(long)
7
- SIZEOF_LONG = [1].pack('l!').bytesize
9
+ SIZEOF_LONG = RbConfig::SIZEOF.fetch('long')
8
10
  # sizeof(long long)
9
- SIZEOF_LLONG = [1].pack('q!').bytesize
11
+ SIZEOF_LLONG = RbConfig::SIZEOF.fetch('long long') { RbConfig::SIZEOF.fetch('long_long') }
10
12
 
11
13
  # Little-endian scalar types.
12
14
  module Little