def decode_oid(self, packet): """ decode_oid(stream) -> object id Decode octet stream into ASN.1 Object ID (returned as a list of integer subIDs). """ # Make sure data types matched if self.decode_tag(ord(packet[0])) != 'OBJID': raise error.TypeMismatch('Non-Object ID type: '\ + str(ord(packet[0]))) # Create a list for objid oid = [] # Unpack the length (length, size) = self.decode_length(packet[1:]) # Set up index index = size + 1 # Get the first subid subid = ord(packet[index]) oid.append(int(subid / 40)) oid.append(int(subid % 40)) # Progress index index = index + 1 # Loop through the rest while index < length + size + 1: # Get a subid subid = ord(packet[index]) if subid < 128: oid.append(subid) index = index + 1 else: # Construct subid from a number of octets next = subid subid = 0 while next >= 128: # Collect subid subid = (subid << 7) + (next & 0x7F) # Take next octet index = index + 1 next = ord(packet[index]) # Just for sure if index > length + size: return bad_integer # Append a subid to oid list subid = (subid << 7) + next oid.append(subid) index = index + 1 # Return objid return oid
def decode_uptime(self, packet): """ decode_uptime(stream) -> integer Decode octet stream into ASN.1 uptime (integer value). """ # Make sure data types matched if self.decode_tag(ord(packet[0])) != 'UPTIME': raise error.TypeMismatch('Non-uptime type: ' + str(ord(packet[0]))) # Decode as unsigned integer return self.decode_unsigned(packet)
def decode_timeticks(self, packet): """ decode_timeticks(stream) -> timeticks Decode octet stream into ASN.1 timeticks data item (returned as integer). """ # Make sure data types matched if self.decode_tag(ord(packet[0])) != 'TIMETICKS': raise error.TypeMismatch('Non-timeticks type: '\ + str(ord(packet[0]))) # Decode as unsigned integer return self.decode_unsigned(packet)
def decode_null(self, packet): """ decode_null(stream) -> None Decode octet stream into ASN.1 NULL value. """ # Make sure data types matched if self.decode_tag(ord(packet[0])) != 'NULL': raise error.TypeMismatch('Non-NULL type: ' + str(ord(packet[0]))) # Now unpack the length (length, size) = self.decode_length(packet[1:]) # Return nothing return ''
def decode_string(self, packet): """ decode_string(stream) -> string Decode octet stream into ASN.1 octet string. """ # Make sure data types matched if self.decode_tag(ord(packet[0])) != 'OCTETSTRING': raise error.TypeMismatch('Non-string type: ' + str(ord(packet[0]))) # Unpack the length (length, size) = self.decode_length(packet[1:]) # Return the octets string return packet[size + 1:size + length + 1]
def decode_opaque(self, packet): """ decode_opaque(stream) -> octets Decode octet stream into ASN.1 opaque data item. """ # Make sure data types matched if self.decode_tag(ord(packet[0])) != 'OPAQUE': raise error.TypeMismatch('Non-opaque type: '\ + str(ord(packet[0]))) # Now unpack the length (length, size) = self.decode_length(packet[1:]) # Return the opaque string return packet[size + 1:size + length + 1]
def decode_ipaddr(self, packet): """ decode_ipaddr(stream) -> ip address Decode octet stream into ASN.1 IP address data item (returned as string in dotted numeric notation). """ # Make sure data types matched if self.decode_tag(ord(packet[0])) != 'IPADDRESS': raise error.TypeMismatch('Non-IP address type: ' + str(ord(packet[0]))) # Get the value from the packet ipaddr = self.decode_sequence(packet) # Check it is valid if len(ipaddr) != 4: raise error.BadIPAddress('Malformed IP address: ' + str(ipaddr)) # Return in dotted notation return '%d.%d.%d.%d' % \ (ord(ipaddr[0]), ord(ipaddr[1]), \ ord(ipaddr[2]), ord(ipaddr[3]))