def decode_block(bytes): (type, typelen) = sdnv.sdnv_decode(bytes) bytes = bytes[typelen:] (flags, flagslen) = sdnv.sdnv_decode(bytes) bytes = bytes[flagslen:] if (flags & BLOCK_FLAG_EID_REFS): raise Exception("Block type %d has EID refs" % type) (length, lengthlen) = sdnv.sdnv_decode(bytes) bytes = bytes[lengthlen:] return (type, flags, bytes[:length], typelen + flagslen + lengthlen + length)
def unserialize_acs(acs_string): """Counterpart to BpAcs.serialize(); takes a string of bytes that are the payload of an aggregate custody signal and turns them into an instance of the BpAcs class. acs_string must be the payload of the payload block of an aggregate custody signal bundle (i.e. acs_string must not include a bundle primary block, or the payload block header). """ toReturn = BpAcs() ( adminrecordheader, status, ) = struct.unpack("!BB", acs_string[0:2]) acs_string = acs_string[2:] # Parse the administrative record header byte. if (adminrecordheader & 0xF0) != 0x40: # Not an aggregate custody signal. return None if (adminrecordheader & 0x0F) != 0x00: print "Administrative record flags are %x, not 0x00" % ( adminrecordheader & 0x0F) raise TypeError # Parse the status byte if (status & 0x80) == 0: toReturn.succeeded = 0 else: toReturn.succeeded = 1 if status & 0x7F: toReturn.reason = status & 0x7F # Parse the fills lengthBlocks = [] while acs_string != "": (offset, n) = sdnv_decode(acs_string) acs_string = acs_string[n:] (length, n) = sdnv_decode(acs_string) acs_string = acs_string[n:] lengthBlocks += (offset, length), for k in lengthBlocksToList(lengthBlocks): toReturn.add(k) return toReturn
def unserialize_acs(acs_string): """Counterpart to BpAcs.serialize(); takes a string of bytes that are the payload of an aggregate custody signal and turns them into an instance of the BpAcs class. acs_string must be the payload of the payload block of an aggregate custody signal bundle (i.e. acs_string must not include a bundle primary block, or the payload block header). """ toReturn = BpAcs() (adminrecordheader, status, ) = struct.unpack("!BB", acs_string[0:2]) acs_string = acs_string[2:] # Parse the administrative record header byte. if (adminrecordheader & 0xF0) != 0x40: # Not an aggregate custody signal. return None if (adminrecordheader & 0x0F) != 0x00: print "Administrative record flags are %x, not 0x00" % (adminrecordheader & 0x0F) raise TypeError # Parse the status byte if (status & 0x80) == 0: toReturn.succeeded = 0 else: toReturn.succeeded = 1 if status & 0x7F: toReturn.reason = status & 0x7F # Parse the fills lengthBlocks = [] while acs_string != "": (offset, n) = sdnv_decode(acs_string) acs_string = acs_string[n:] (length, n) = sdnv_decode(acs_string) acs_string = acs_string[n:] lengthBlocks += (offset, length), for k in lengthBlocksToList(lengthBlocks): toReturn.add(k) return toReturn
def __init__(self, bundle = None): # See if the bundle parser found one CTEB. if bundle == None or not bundle.blocks.has_key(CTE_BLOCK): self.custodyId = None self.custodian = None self.ctebPresent = False return if len(bundle.blocks[CTE_BLOCK]) != 1: raise Exception("Bundle has %d CTEBs, cannot handle." % (len(bundle.blocks[CTE_BLOCK])) ) # Parse the CTEB. cteb_bytes = bundle.blocks[CTE_BLOCK][0]["payload"] i = 0 self.ctebPresent = True # Custody ID (custodyId, sdnv_len) = sdnv_decode(cteb_bytes[i:]) i += sdnv_len self.custodyId = custodyId # Custodian EID self.custodian = cteb_bytes[i:]
def __init__(self, bundle=None): # See if the bundle parser found one CTEB. if bundle == None or not bundle.blocks.has_key(CTE_BLOCK): self.custodyId = None self.custodian = None self.ctebPresent = False return if len(bundle.blocks[CTE_BLOCK]) != 1: raise Exception("Bundle has %d CTEBs, cannot handle." % (len(bundle.blocks[CTE_BLOCK]))) # Parse the CTEB. cteb_bytes = bundle.blocks[CTE_BLOCK][0]["payload"] i = 0 self.ctebPresent = True # Custody ID (custodyId, sdnv_len) = sdnv_decode(cteb_bytes[i:]) i += sdnv_len self.custodyId = custodyId # Custodian EID self.custodian = cteb_bytes[i:]
def __decode_assist(len, array): #this is not efficient (one, temp_len) = sdnv.sdnv_decode(array_into_string(array[len:])) len += temp_len (two, temp_len) = sdnv.sdnv_decode(array_into_string(array[len:])) len += temp_len return (one, two, len)
def sdnvdecode(self, data): value, length = sdnv.sdnv_decode(data) # print "sdnv: "+toHex(data[0:5])+".. = "+str(value)+"/"+str(length) self.moveOffset(length) return value