def handle_interface_table(self, table, tpm_alg_ids): base_type = utils.find_tpm_base_type_name(table.short_name) new_type = utils.find_tpm_type_name(table.short_name) typedef_statement = "typedef " + base_type + utils.indent(base_type) + new_type + ";\n" alg_dep = utils.find_alg_constraints(table.name) if alg_dep: alg_name = "TPM_ALG_" + alg_dep typedef_statement_alg_dep = "" # check for !ALG alg_ids_list = utils.expand_alg_macro(new_type, tpm_alg_ids, alg_dep) # if new type name contains algorithm type, create type definition for all corresponding IDs for alg in alg_ids_list: alg_name = alg.name final_new_type = utils.replace_alg_placeholder(new_type, alg.short_name) typedef_statement_alg_dep += "#ifdef " + alg_name + "\n" # for each algorithm typedef_statement_alg_dep += "typedef " + base_type + utils.indent(base_type) + final_new_type + ";\n" typedef_statement_alg_dep += "#endif // " + alg_name + "\n" # for each algorithm # in case there is no !ALG, create type definition for type found in table name if not typedef_statement_alg_dep: typedef_statement_alg_dep = "#ifdef " + alg_name + "\n" typedef_statement_alg_dep += "typedef " + base_type + utils.indent(base_type) + new_type + ";\n" typedef_statement_alg_dep += "#endif // " + alg_name + "\n" typedef_statement = typedef_statement_alg_dep self.tpm_types_h_file_content += typedef_statement self.marshaller.handle_interface_table(table, tpm_alg_ids)
def handle_typedef_union_struct(self, table, typedef_name): self.tpm_types_h_file_content += "typedef union {\n" self.tpm_types_h_file_content += " struct {\n" for row in table.rows: parameter = row[0].replace("=", "") member_type = row[1].replace("+", "") if "#" in parameter: continue # find constraints and remove them if found constraints = utils.find_alg_constraints(parameter) if constraints: parameter = parameter.replace("{" + constraints + "}", "").replace(" ", "") constraints = constraints.replace(":", "") # find array size and include it in typedef struct if found size = utils.find_array_size(parameter) if size: parameter = parameter.replace(size, "") self.tpm_types_h_file_content += " " + member_type + utils.indent(member_type, 1) + \ parameter + "[" + constraints.strip() + "]" + ";\n" else: self.tpm_types_h_file_content += " " + member_type + utils.indent(member_type, 1) + \ parameter + ";\n" # end of loop - for row in table.rows: self.tpm_types_h_file_content += " } t;\n" self.tpm_types_h_file_content += " " + "TPM2B" + " b;\n" self.tpm_types_h_file_content += "} " + typedef_name + ";\n" self.marshaller.handle_structures_table(typedef_name, table)
def handle_typedef_table(self, table, tpm_alg_ids): table_dependence = utils.find_alg_constraints(table.short_name) if table_dependence: # if table dependence {*} found in table name self.tpm_types_h_file_content += "#ifdef TPM_ALG_" + table_dependence + "\n" for row in table.rows: base_type = row[0] name = row[1] # check for !ALG alg_ids_list = utils.expand_alg_macro(name, tpm_alg_ids, table_dependence) # if new type name contains algorithm type, create type definition for all corresponding IDs if alg_ids_list: for alg in alg_ids_list: new_type = utils.replace_alg_placeholder(name, alg.short_name) self.tpm_types_h_file_content += "typedef " + base_type + utils.indent(base_type) + new_type + ";\n" else: # else create type definition for table name self.tpm_types_h_file_content += "typedef " + base_type + utils.indent(base_type) + name + ";\n" # end of loop - for row in table.rows: if table_dependence: # if table dependence {*} found in table name self.tpm_types_h_file_content += "#endif // TPM_ALG_" + table_dependence + "\n" # end of if else - if table dependence found self.marshaller.handle_simple_type_structures_new(table, tpm_alg_ids)
def handle_typedef_table(self, table, tpm_alg_ids): table_dependence = utils.find_alg_constraints(table.short_name) if table_dependence: # if table dependence {*} found in table name self.tpm_types_h_file_content += "#ifdef TPM_ALG_" + table_dependence + "\n" for row in table.rows: base_type = row[0] name = row[1] # check for !ALG alg_ids_list = utils.expand_alg_macro(name, tpm_alg_ids, table_dependence) # if new type name contains algorithm type, create type definition for all corresponding IDs if alg_ids_list: for alg in alg_ids_list: new_type = utils.replace_alg_placeholder( name, alg.short_name) self.tpm_types_h_file_content += "typedef " + base_type + utils.indent( base_type) + new_type + ";\n" else: # else create type definition for table name self.tpm_types_h_file_content += "typedef " + base_type + utils.indent( base_type) + name + ";\n" # end of loop - for row in table.rows: if table_dependence: # if table dependence {*} found in table name self.tpm_types_h_file_content += "#endif // TPM_ALG_" + table_dependence + "\n" # end of if else - if table dependence found self.marshaller.handle_simple_type_structures_new(table, tpm_alg_ids)
def handle_interface_table(self, table, tpm_alg_ids): base_type = utils.find_tpm_base_type_name(table.short_name) new_type = utils.find_tpm_type_name(table.short_name) typedef_statement = "typedef " + base_type + utils.indent( base_type) + new_type + ";\n" alg_dep = utils.find_alg_constraints(table.name) if alg_dep: alg_name = "TPM_ALG_" + alg_dep typedef_statement_alg_dep = "" # check for !ALG alg_ids_list = utils.expand_alg_macro(new_type, tpm_alg_ids, alg_dep) # if new type name contains algorithm type, create type definition for all corresponding IDs for alg in alg_ids_list: alg_name = alg.name final_new_type = utils.replace_alg_placeholder( new_type, alg.short_name) typedef_statement_alg_dep += "#ifdef " + alg_name + "\n" # for each algorithm typedef_statement_alg_dep += "typedef " + base_type + utils.indent( base_type) + final_new_type + ";\n" typedef_statement_alg_dep += "#endif // " + alg_name + "\n" # for each algorithm # in case there is no !ALG, create type definition for type found in table name if not typedef_statement_alg_dep: typedef_statement_alg_dep = "#ifdef " + alg_name + "\n" typedef_statement_alg_dep += "typedef " + base_type + utils.indent( base_type) + new_type + ";\n" typedef_statement_alg_dep += "#endif // " + alg_name + "\n" typedef_statement = typedef_statement_alg_dep self.tpm_types_h_file_content += typedef_statement self.marshaller.handle_interface_table(table, tpm_alg_ids)
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_structures_table(self, typedef_name, table): self.create_header_comment(table) bool_flag = False result = re.search('<(.*)>', table.name) if result: config = result.group(1) else: config = "IN/OUT" in_declaration = re.search('([iI])', config) out_declaration = re.search('([oO])', config) alg_dep = utils.find_alg_constraints(table.name) for row in table.rows: parameter = row[0] member_type = row[1] if "+TPM" in member_type: self.function_prototypes_with_flag[typedef_name] = "" bool_flag = True if "[count]" in parameter or "[size]" in parameter: self.array_functions[member_type] = [alg_dep, config] # end of loop - for row in table.rows: 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) if in_declaration is None: self.content += tpm2_partx_marshal_templates.COMMENT_TYPE_Unmarshal_not_req.format(typedef_name) self.content_fp += tpm2_partx_marshal_templates.COMMENT_TYPE_Unmarshal_not_req.format(typedef_name) if len(table.rows) == 1: self.content += tpm2_partx_marshal_templates.COMMENT_TYPE_Unmarshal_defined.format(typedef_name) self.content += tpm2_partx_marshal_templates.COMMENT_TYPE_Marshal_defined.format(typedef_name) only_row = table.rows[0] parameter = only_row[0] member_type = only_row[1] if member_type.endswith("+"): self.content_fp += self.structure_table_marshaller.create_unmarshal_fp_define( typedef_name, member_type.replace("+", ""), parameter, 1) self.content_fp += self.structure_table_marshaller.create_marshal_fp_define( typedef_name, member_type.replace("+", ""), parameter, 1) else: self.content_fp += self.structure_table_marshaller.create_unmarshal_fp_define( typedef_name, member_type, parameter, 0) self.content_fp += self.structure_table_marshaller.create_marshal_fp_define( typedef_name, member_type, parameter, 0) else: # in if in_declaration: self.content += self.structure_table_marshaller.create_unmarshal_code( typedef_name, table, self.function_prototypes_with_flag) self.content_fp += self.structure_table_marshaller.create_unmarshal_fp(typedef_name, bool_flag) # out if out_declaration: self.content += self.structure_table_marshaller.create_marshal_code( typedef_name, table, self.function_prototypes_with_flag) self.content_fp += self.structure_table_marshaller.create_marshal_fp(typedef_name) if out_declaration is None: self.content += tpm2_partx_marshal_templates.COMMENT_TYPE_Marshal_not_req.format(typedef_name) self.content_fp += tpm2_partx_marshal_templates.COMMENT_TYPE_Marshal_not_req.format(typedef_name) 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) tpm2_partx_type_mapping.dictionary[typedef_name] = [None, table.number]
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_structures_table(self, typedef_name, table): self.create_header_comment(table) bool_flag = False result = re.search('<(.*)>', table.name) if result: config = result.group(1) else: config = "IN/OUT" in_declaration = re.search('([iI])', config) out_declaration = re.search('([oO])', config) alg_dep = utils.find_alg_constraints(table.name) for row in table.rows: parameter = row[0] member_type = row[1] if "+TPM" in member_type: self.function_prototypes_with_flag[typedef_name] = "" bool_flag = True if "[count]" in parameter or "[size]" in parameter: self.array_functions[member_type] = [alg_dep, config] # end of loop - for row in table.rows: 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) if in_declaration is None: self.content += tpm2_partx_marshal_templates.COMMENT_TYPE_Unmarshal_not_req.format( typedef_name) self.content_fp += tpm2_partx_marshal_templates.COMMENT_TYPE_Unmarshal_not_req.format( typedef_name) if len(table.rows) == 1: self.content += tpm2_partx_marshal_templates.COMMENT_TYPE_Unmarshal_defined.format( typedef_name) self.content += tpm2_partx_marshal_templates.COMMENT_TYPE_Marshal_defined.format( typedef_name) only_row = table.rows[0] parameter = only_row[0] member_type = only_row[1] if member_type.endswith("+"): self.content_fp += self.structure_table_marshaller.create_unmarshal_fp_define( typedef_name, member_type.replace("+", ""), parameter, 1) self.content_fp += self.structure_table_marshaller.create_marshal_fp_define( typedef_name, member_type.replace("+", ""), parameter, 1) else: self.content_fp += self.structure_table_marshaller.create_unmarshal_fp_define( typedef_name, member_type, parameter, 0) self.content_fp += self.structure_table_marshaller.create_marshal_fp_define( typedef_name, member_type, parameter, 0) else: # in if in_declaration: self.content += self.structure_table_marshaller.create_unmarshal_code( typedef_name, table, self.function_prototypes_with_flag) self.content_fp += self.structure_table_marshaller.create_unmarshal_fp( typedef_name, bool_flag) # out if out_declaration: self.content += self.structure_table_marshaller.create_marshal_code( typedef_name, table, self.function_prototypes_with_flag) self.content_fp += self.structure_table_marshaller.create_marshal_fp( typedef_name) if out_declaration is None: self.content += tpm2_partx_marshal_templates.COMMENT_TYPE_Marshal_not_req.format( typedef_name) self.content_fp += tpm2_partx_marshal_templates.COMMENT_TYPE_Marshal_not_req.format( typedef_name) 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) tpm2_partx_type_mapping.dictionary[typedef_name] = [None, table.number]