Module: SNMP::BER::Encode

Instance Method Summary collapse

Instance Method Details

#encode_exception(tag) ⇒ Object

Encode an exception. The encoding is simply the exception tag with no data, similar to NULL.


302
303
304
# File 'lib/snmp/ber.rb', line 302

def encode_exception(tag)
  tag.chr << "\000"
end

#encode_integer(value) ⇒ Object

Encode integer


259
260
261
# File 'lib/snmp/ber.rb', line 259

def encode_integer(value)
  encode_tagged_integer(INTEGER_TAG, value)
end

#encode_length(length) ⇒ Object

Encode the length field for TLV data. Returns the length octets as a string.

Raises:


246
247
248
249
250
251
252
253
254
# File 'lib/snmp/ber.rb', line 246

def encode_length(length)
  raise InvalidLength, length.to_s if length < 0
  if length < 0x80
    length.chr
  else
    data = integer_to_octets(length)
    (data.size | 0x80).chr << data
  end
end

#encode_nullObject


294
295
296
# File 'lib/snmp/ber.rb', line 294

def encode_null
  NULL_TAG.chr << "\000"
end

#encode_object_id(value) ⇒ Object

Encode an object id. The input is assumed to be an array of integers representing the object id.

Raises:


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
# File 'lib/snmp/ber.rb', line 334

def encode_object_id(value)
  raise InvalidObjectId, value.to_s if value.length < 1
  raise InvalidObjectId, value.to_s if value[0] > 2
  data = "".dup
  if (value.length > 1)
    raise InvalidObjectId if value[0] < 2 && value[1] > 40
    data << (40 * value[0] + value[1]).chr
    for i in 2...value.length
      if value[i] < 0x80
        data << value[i].chr
      else
        octets = ""
        n = value[i]
        begin
          octets = (n & 0x7f | 0x80).chr << octets
          n = n >> 7
        end until n == 0
        octets[-1] = (octets[-1].ord & 0x7f).chr
        data << octets
      end
    end
  elsif (value.length == 1)
    data << (40 * value[0]).chr
  end
  encode_tlv(OBJECT_IDENTIFIER_TAG, data)
end

#encode_octet_string(value) ⇒ Object

Wrap string in a octet string tag and length.


319
320
321
# File 'lib/snmp/ber.rb', line 319

def encode_octet_string(value)
  encode_tlv(OCTET_STRING_TAG, value)
end

#encode_sequence(value) ⇒ Object

Wrap value in a sequence tag and length.


326
327
328
# File 'lib/snmp/ber.rb', line 326

def encode_sequence(value)
  encode_tlv(SEQUENCE_TAG, value)
end

#encode_tagged_integer(tag, value) ⇒ Object


263
264
265
266
267
268
269
270
271
272
273
274
275
# File 'lib/snmp/ber.rb', line 263

def encode_tagged_integer(tag, value)
  if value > 0 && value < 0x80
    data = value.chr
  else
    data = integer_to_octets(value)
    if value > 0 && data[0].ord > 0x7f
      data = "\000".dup << data
    elsif value < 0 && data[0].ord < 0x80
      data = "\377".dup << data
    end
  end
  encode_tlv(tag, data)
end

#encode_tlv(tag, value) ⇒ Object

Wraps value in a tag and length. This method expects an integer tag and a string value.


310
311
312
313
314
# File 'lib/snmp/ber.rb', line 310

def encode_tlv(tag, value)
  data = tag.chr << encode_length(value.length)
  data = data << value if value.length > 0
  data
end

#integer_to_octets(i) ⇒ Object

Helper method for encoding integer-like things.


280
281
282
283
284
285
286
287
288
289
290
291
292
# File 'lib/snmp/ber.rb', line 280

def integer_to_octets(i)
  if i >= 0
    done = 0
  else
    done = -1
  end
  octets = ""
  begin
    octets = (i & 0xff).chr << octets
    i = i >> 8
  end until i == done
  octets
end