def download_resource(connection, platform, file_path, resource_id, auth_flag, file_type, description = '', block_size = 2048, timeout = 0, key = None): """ TODO. """ authent = '\x00\x00\x00\x00' with open(file_path, 'rb') as file: data = file.read() # if file already resmarked mark = data.find('/resMark') if mark > 0: if (len(data) - mark) != 34: raise Exception('The Resource file is not resmarked correctly!') data = data[0:mark] if auth_flag in [1, 2, 3, 4] and key is not None: sha1digest = hashlib.sha1(data).digest() if platform == 'VGD': return False if platform == 'M3': if auth_flag == 1: sha1digest += 'PIN' elif auth_flag == 2: sha1digest += 'CLEAR' authent = crypto.mac_x919(sha1digest, key) elif platform == 'NGP': digest = '\x00' for i in sha1digest: digest += "%02X" % ord(i) if auth_flag == 3: digest += '\x00PIN\x00application\x00' elif auth_flag == 4: digest += '\x00CLEAR\x00application\x00' digest += os.urandom(243 - len(digest)) CRC32_digest = crc32(digest) & 0xffffffff digest += pack("!L", CRC32_digest) + '\x00' priv_key = crypto.import_RSAkey(key, asFile = False) authent = crypto.rsa_decrypt(digest, priv_key) file_size = len(data) CRC32 = crc32(data) & 0xffffffff Protocol.send_file_download_request_cmd(connection, file_type, resource_id, file_size, CRC32, auth_flag, authent, description[:16]) ack_params = Protocol.recv_file_download_request_cmd(connection, timeout) if ack_params['ack_code'] != 0x00: raise Exception('Download Request failed with ack_code: 0x{0:02x}'.format(ack_params['ack_code'])) while ack_params['ack_code'] == 0x00: offset = ack_params['offset'] if 'offset' in ack_params else 0 resource_block = data[offset:min(offset + block_size, file_size)] Protocol.send_file_block_download_cmd(connection, offset, resource_block) ack_params = Protocol.recv_file_block_download_cmd(connection, timeout) if ack_params['ack_code'] == 0x02: return True raise Exception('Download Block failed with ack_code: 0x{0:02x}'.format(ack_params['ack_code']))