def handle_advanced_type_structures(self, table): self.create_header_comment(table) base_type = utils.find_tpm_base_type_name(table.short_name) current_type = utils.find_tpm_type_name(table.short_name) config = utils.find_config(table.name) size = utils.find_base_type_size(base_type) if not size: return in_declaration = re.search('([iI])', config) out_declaration = re.search('([oO])', config) s_declaration = re.search('([S])', config) cases = [] rc_fail = None for row in table.rows: name = row[0] if "#" in name: rc_fail = name.replace("#", "") elif "TPM_CAP_FIRST" in name or "TPM_CAP_LAST" in name: continue # skip else: cases.append(name) # in if in_declaration is None: self.content += tpm2_partx_marshal_templates.COMMENT_TYPE_Unmarshal_not_req.format(current_type) self.content_fp += tpm2_partx_marshal_templates.COMMENT_TYPE_Unmarshal_not_req.format(current_type) else: if (base_type in tpm2_partx_type_mapping.dictionary.keys() or s_declaration) and not rc_fail: self.content += tpm2_partx_marshal_templates.COMMENT_TYPE_Unmarshal_defined.format(current_type) self.content_fp += self.advanced_marshaller.create_unmarshal_fp_define(current_type, base_type) else: self.content += self.advanced_marshaller.create_unmarshal_code(current_type, base_type, cases,rc_fail) self.content_fp += self.advanced_marshaller.create_unmarshal_fp(current_type) # out if out_declaration is None: self.content += tpm2_partx_marshal_templates.COMMENT_TYPE_Marshal_not_req.format(current_type) self.content_fp += tpm2_partx_marshal_templates.COMMENT_TYPE_Marshal_not_req.format(current_type) else: if base_type in tpm2_partx_type_mapping.dictionary.keys() or s_declaration: self.content += tpm2_partx_marshal_templates.COMMENT_TYPE_Marshal_defined.format(current_type) self.content_fp += self.advanced_marshaller.create_marshal_fp_define(current_type, base_type) else: self.content += self.advanced_marshaller.create_marshal_code(current_type, size) self.content_fp += self.advanced_marshaller.create_marshal_fp(current_type) tpm2_partx_type_mapping.dictionary[current_type] = [base_type, table.number]
def handle_simple_constants(self, table): self.create_header_comment(table) base_type = utils.find_tpm_base_type_name(table.short_name) current_type = utils.find_tpm_type_name(table.short_name) alg_dep = utils.find_alg_dep(table.short_name) size = utils.find_base_type_size(base_type) if not size: return if alg_dep: self.content += tpm2_partx_marshal_templates.IFDEF_ALG.format( alg_dep) self.content_fp += tpm2_partx_marshal_templates.IFDEF_ALG.format( alg_dep) already_marshaled = base_type in tpm2_partx_type_mapping.dictionary.keys( ) if already_marshaled: self.content += tpm2_partx_marshal_templates.COMMENT_TYPE_Unmarshal_defined.format( current_type) self.content += tpm2_partx_marshal_templates.COMMENT_TYPE_Marshal_defined.format( current_type) self.content_fp += self.simple_marshaller.create_unmarshal_fp_define( current_type, base_type) self.content_fp += self.simple_marshaller.create_marshal_fp_define( current_type, base_type) else: self.content += self.simple_marshaller.create_unmarshal_code( current_type, int(size) / 8) self.content += self.simple_marshaller.create_marshal_code( current_type, int(size) / 8) self.content_fp += self.simple_marshaller.create_unmarshal_fp( current_type) self.content_fp += self.simple_marshaller.create_marshal_fp( current_type) tpm2_partx_type_mapping.dictionary[current_type] = [ base_type, table.number ] if alg_dep: self.content += tpm2_partx_marshal_templates.ENDIF_ALG.format( alg_dep) self.content_fp += tpm2_partx_marshal_templates.ENDIF_ALG.format( alg_dep)
def handle_simple_constants(self, table): self.create_header_comment(table) base_type = utils.find_tpm_base_type_name(table.short_name) current_type = utils.find_tpm_type_name(table.short_name) alg_dep = utils.find_alg_dep(table.short_name) size = utils.find_base_type_size(base_type) if not size: return if alg_dep: self.content += tpm2_partx_marshal_templates.IFDEF_ALG.format(alg_dep) self.content_fp += tpm2_partx_marshal_templates.IFDEF_ALG.format(alg_dep) already_marshaled = base_type in tpm2_partx_type_mapping.dictionary.keys() if already_marshaled: self.content += tpm2_partx_marshal_templates.COMMENT_TYPE_Unmarshal_defined.format(current_type) self.content += tpm2_partx_marshal_templates.COMMENT_TYPE_Marshal_defined.format(current_type) self.content_fp += self.simple_marshaller.create_unmarshal_fp_define(current_type, base_type) self.content_fp += self.simple_marshaller.create_marshal_fp_define(current_type, base_type) else: self.content += self.simple_marshaller.create_unmarshal_code(current_type, int(size)/8) self.content += self.simple_marshaller.create_marshal_code(current_type, int(size)/8) self.content_fp += self.simple_marshaller.create_unmarshal_fp(current_type) self.content_fp += self.simple_marshaller.create_marshal_fp(current_type) tpm2_partx_type_mapping.dictionary[current_type] = [base_type, table.number] if alg_dep: self.content += tpm2_partx_marshal_templates.ENDIF_ALG.format(alg_dep) self.content_fp += tpm2_partx_marshal_templates.ENDIF_ALG.format(alg_dep)
def handle_simple_type_structures_new(self, table, tpm_alg_ids=None): self.create_header_comment(table) for row in table.rows: base_type = row[0] new_type = row[1] types_list = [] # check for !ALG alg_dep = utils.find_alg_constraints(table.short_name) algs = utils.expand_alg_macro(new_type, tpm_alg_ids, alg_dep) for alg in algs: typedef = utils.replace_alg_placeholder(new_type, alg.short_name) types_list.append(typedef) # end of loop - for alg in alg_list: # in case there is no !ALG if not algs: types_list = [new_type] # handle the (list of) typ(es) in the current row for current_type in types_list: if alg_dep: self.content += tpm2_partx_marshal_templates.IFDEF_ALG.format(alg_dep) self.content_fp += tpm2_partx_marshal_templates.IFDEF_ALG.format(alg_dep) # check if type is a C base type and already in mapping dictionary base_type, c_base_type = self.handle_base_types(base_type) already_marshaled = base_type in tpm2_partx_type_mapping.dictionary.keys() unsigned_marshaled = "u"+base_type in tpm2_partx_type_mapping.dictionary.keys() if unsigned_marshaled: base_type = "u"+base_type if c_base_type or already_marshaled or unsigned_marshaled: # if original type was already marshaled somehow # marshalling new type would be redundant - change to define for new type orig_mrshl = tpm2_partx_type_mapping.dictionary[base_type] orig_table_nr = orig_mrshl[1] # recursive exploration of most basic base_type in mapping dictionary base_type = utils.find_basic_base_type(base_type) # add a comment that points to the origin of the type definition self.content += "// " + base_type + " definition used from Table 2:" + orig_table_nr + "\n" self.content += tpm2_partx_marshal_templates.COMMENT_TYPE_Unmarshal_defined.format(current_type) self.content += tpm2_partx_marshal_templates.COMMENT_TYPE_Marshal_defined.format(current_type) self.content_fp += self.simple_marshaller.create_unmarshal_fp_define(current_type, base_type) self.content_fp += self.simple_marshaller.create_marshal_fp_define(current_type, base_type) else: # handle base types: figure out the size size = utils.find_base_type_size(base_type) if not size: # skip base type without size (i.e. 'int', which is used for 'BOOL') continue self.content += self.simple_marshaller.create_unmarshal_code(current_type, int(size)/8) self.content += self.simple_marshaller.create_marshal_code(current_type, int(size)/8) self.content_fp += self.simple_marshaller.create_unmarshal_fp(current_type) self.content_fp += self.simple_marshaller.create_marshal_fp(current_type) if unsigned_marshaled: base_type = base_type[1:] tpm2_partx_type_mapping.dictionary[current_type] = [base_type, table.number] # done with the new type if alg_dep: self.content += tpm2_partx_marshal_templates.ENDIF_ALG.format(alg_dep) self.content_fp += tpm2_partx_marshal_templates.ENDIF_ALG.format(alg_dep) self.content += "\n" self.content_fp += "\n"
def handle_simple_type_structures_new(self, table, tpm_alg_ids=None): self.create_header_comment(table) for row in table.rows: base_type = row[0] new_type = row[1] types_list = [] # check for !ALG alg_dep = utils.find_alg_constraints(table.short_name) algs = utils.expand_alg_macro(new_type, tpm_alg_ids, alg_dep) for alg in algs: typedef = utils.replace_alg_placeholder( new_type, alg.short_name) types_list.append(typedef) # end of loop - for alg in alg_list: # in case there is no !ALG if not algs: types_list = [new_type] # handle the (list of) typ(es) in the current row for current_type in types_list: if alg_dep: self.content += tpm2_partx_marshal_templates.IFDEF_ALG.format( alg_dep) self.content_fp += tpm2_partx_marshal_templates.IFDEF_ALG.format( alg_dep) # check if type is a C base type and already in mapping dictionary base_type, c_base_type = self.handle_base_types(base_type) already_marshaled = base_type in tpm2_partx_type_mapping.dictionary.keys( ) unsigned_marshaled = "u" + base_type in tpm2_partx_type_mapping.dictionary.keys( ) if unsigned_marshaled: base_type = "u" + base_type if c_base_type or already_marshaled or unsigned_marshaled: # if original type was already marshaled somehow # marshalling new type would be redundant - change to define for new type orig_mrshl = tpm2_partx_type_mapping.dictionary[base_type] orig_table_nr = orig_mrshl[1] # recursive exploration of most basic base_type in mapping dictionary base_type = utils.find_basic_base_type(base_type) # add a comment that points to the origin of the type definition self.content += "// " + base_type + " definition used from Table 2:" + orig_table_nr + "\n" self.content += tpm2_partx_marshal_templates.COMMENT_TYPE_Unmarshal_defined.format( current_type) self.content += tpm2_partx_marshal_templates.COMMENT_TYPE_Marshal_defined.format( current_type) self.content_fp += self.simple_marshaller.create_unmarshal_fp_define( current_type, base_type) self.content_fp += self.simple_marshaller.create_marshal_fp_define( current_type, base_type) else: # handle base types: figure out the size size = utils.find_base_type_size(base_type) if not size: # skip base type without size (i.e. 'int', which is used for 'BOOL') continue self.content += self.simple_marshaller.create_unmarshal_code( current_type, int(size) / 8) self.content += self.simple_marshaller.create_marshal_code( current_type, int(size) / 8) self.content_fp += self.simple_marshaller.create_unmarshal_fp( current_type) self.content_fp += self.simple_marshaller.create_marshal_fp( current_type) if unsigned_marshaled: base_type = base_type[1:] tpm2_partx_type_mapping.dictionary[current_type] = [ base_type, table.number ] # done with the new type if alg_dep: self.content += tpm2_partx_marshal_templates.ENDIF_ALG.format( alg_dep) self.content_fp += tpm2_partx_marshal_templates.ENDIF_ALG.format( alg_dep) self.content += "\n" self.content_fp += "\n"
def handle_advanced_type_structures(self, table): self.create_header_comment(table) base_type = utils.find_tpm_base_type_name(table.short_name) current_type = utils.find_tpm_type_name(table.short_name) config = utils.find_config(table.name) size = utils.find_base_type_size(base_type) if not size: return in_declaration = re.search('([iI])', config) out_declaration = re.search('([oO])', config) s_declaration = re.search('([S])', config) cases = [] rc_fail = None for row in table.rows: name = row[0] if "#" in name: rc_fail = name.replace("#", "") elif "TPM_CAP_FIRST" in name or "TPM_CAP_LAST" in name: continue # skip else: cases.append(name) # in if in_declaration is None: self.content += tpm2_partx_marshal_templates.COMMENT_TYPE_Unmarshal_not_req.format( current_type) self.content_fp += tpm2_partx_marshal_templates.COMMENT_TYPE_Unmarshal_not_req.format( current_type) else: if (base_type in tpm2_partx_type_mapping.dictionary.keys() or s_declaration) and not rc_fail: self.content += tpm2_partx_marshal_templates.COMMENT_TYPE_Unmarshal_defined.format( current_type) self.content_fp += self.advanced_marshaller.create_unmarshal_fp_define( current_type, base_type) else: self.content += self.advanced_marshaller.create_unmarshal_code( current_type, base_type, cases, rc_fail) self.content_fp += self.advanced_marshaller.create_unmarshal_fp( current_type) # out if out_declaration is None: self.content += tpm2_partx_marshal_templates.COMMENT_TYPE_Marshal_not_req.format( current_type) self.content_fp += tpm2_partx_marshal_templates.COMMENT_TYPE_Marshal_not_req.format( current_type) else: if base_type in tpm2_partx_type_mapping.dictionary.keys( ) or s_declaration: self.content += tpm2_partx_marshal_templates.COMMENT_TYPE_Marshal_defined.format( current_type) self.content_fp += self.advanced_marshaller.create_marshal_fp_define( current_type, base_type) else: self.content += self.advanced_marshaller.create_marshal_code( current_type, size) self.content_fp += self.advanced_marshaller.create_marshal_fp( current_type) tpm2_partx_type_mapping.dictionary[current_type] = [ base_type, table.number ]