Esempio n. 1
0
 def __init__(self, apdu=None):
     if is_stringlike(apdu):
         # try to get the list of bytes.
         apdu = conv.toBytes(apdu.replace(' ', ''))
     elif isinstance(apdu, APDUPacket):
         apdu = apdu.to_list()
     self.apdu = apdu
Esempio n. 2
0
 def enrich_fixed(self):
     """
         fixed arguments should be enriched here into the datastream.
         as to speak: serialized.
         
         by default, it will try to serialize fixed_arguments from fixed_values
     """
     self.validate()
     ds = []
     if self.fixed_arguments and self.fixed_values:
         # we have fixed arguments here
         for i in range(len(self.fixed_arguments)):
             val = self.fixed_values.get(self.fixed_arguments[i], None)
             if val:
                 if is_stringlike(val):
                     val = conv.toBytes(val)
                 elif isinstance(val, list):
                     pass
                 else:
                     val = [
                         val,
                     ]
                 # now just save it into ds
                 ds += val
     return ds
Esempio n. 3
0
 def __init__(self, apdu=None):
     if isinstance(apdu, basestring):
         # try to get the list of bytes.
         apdu = conv.toBytes(apdu.replace(' ', ''))
     elif isinstance(apdu, APDUPacket):
         apdu = apdu.to_list()
     self.apdu = apdu
Esempio n. 4
0
 def detect(self, datastream):
     # detects which class to use.
     if is_stringlike(datastream):
         # lets convert our string into a bytelist.
         datastream = conv.toBytes(datastream[:2])
     # read the first two bytes of the stream.
     cc, ci = datastream[:2]
     #print '<| %s %s' % (hex(cc), hex(ci))
     # now look up if we got this packet class:
     return self.packets.get('%s_%s' % (hex(cc), hex(ci)),
                             self.packets.get('%s' % (hex(cc)), None))
Esempio n. 5
0
 def detect(self, datastream):
     # detects which class to use.
     if isinstance(datastream, basestring):
         # lets convert our string into a bytelist.
         datastream = conv.toBytes(datastream[:2])
     # read the first two bytes of the stream.
     cc, ci = datastream[:2]
     #print '<| %s %s' % (hex(cc), hex(ci))
     # now look up if we got this packet class:
     return self.packets.get('%s_%s' % (hex(cc), hex(ci)),
                             self.packets.get(
                             '%s' % (hex(cc)),
                             None))
Esempio n. 6
0
 def parse(cls, blob=""):
     if isinstance(blob, basestring):
         # lets convert our string into a bytelist.
         blob = conv.toBytes(blob)
     if isinstance(blob, list):
         # allright.
         # first we detect our packetclass
         Kls = Packets.detect(blob[:2])
         if Kls:
             instance = Kls()
             # fix for multipackets:
             if instance.cmd_instr == None:
                 instance.cmd_instr = blob[1]
             instance.data = blob[2:]
             if not instance.validate():
                 debug('Validation Error')
             return instance
         else:
             debug('Unknown Packet')
Esempio n. 7
0
 def parse(cls, blob=''):
     if is_stringlike(blob):
         # lets convert our string into a bytelist.
         blob = toBytes(blob)
     if type(blob) is list:
         # allright.
         # first we detect our packetclass
         PacketClass = Packets.detect(blob[:2])
         if PacketClass:
             instance = PacketClass()
             # fix for multipackets:
             if instance.cmd_instr is None:
                 instance.cmd_instr = blob[1]
             instance.data = blob[2:]
             if not instance.validate():
                 debug('Validation Error')
             return instance
         else:
             debug('Unknown Packet')
Esempio n. 8
0
def parse_represented_data(data):
    # represented data
    if isinstance(data, basestring):
        # we assume a bytelist like 10 02 03....
        data = conv.toBytes(data)
    # first of all, serial data starts with 10 02, so everything
    # starting with 10 will be assumed as "serial packet" and first "demantled"
    if data[0] == DLE:
        try:
            crc, data = dismantle_serial_packet(data)
        except common.TransportLayerException:
            pass
    elif data[0] == ACK:
        if len(data) == 1:
            return 'ACK'
    elif data[0] == NAK:
        if len(data) == 1:
            return 'NAK'
    # then we create the packet and return that.
    p = Packet.parse(data)
    return p
Esempio n. 9
0
def parse_represented_data(data):
    # represented data
    if isinstance(data, basestring):
        # we assume a bytelist like 10 02 03.... 
        data = conv.toBytes(data)
    # first of all, serial data starts with 10 02, so everything
    # starting with 10 will be assumed as "serial packet" and first "demantled"
    if data[0] == DLE:
        try:
            crc, data = dismantle_serial_packet(data)
        except common.TransportLayerException:
            pass
    elif data[0] == ACK:
        if len(data) == 1:
            return 'ACK'
    elif data[0] == NAK:
        if len(data) == 1:
            return 'NAK'
    # then we create the packet and return that.
    p = Packet.parse(data)
    return p
Esempio n. 10
0
 def enrich_fixed(self):
     """
         fixed arguments should be enriched here into the datastream.
         as to speak: serialized.
         
         by default, it will try to serialize fixed_arguments from fixed_values
     """
     self.validate()
     ds = []
     if self.fixed_arguments and self.fixed_values:
         # we have fixed arguments here
         for i in xrange(len(self.fixed_arguments)):
             val = self.fixed_values.get(self.fixed_arguments[i], None)
             if val:
                 if isinstance(val, basestring):
                     val = conv.toBytes(val)
                 elif isinstance(val, list):
                     pass
                 else:
                     val = [ val, ]
                 # now just save it into ds
                 ds += val
     return ds