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_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_typedef_union(self, table, typedef_name, tpm_alg_ids): self.tpm_types_h_file_content += "typedef union {\n" for row in table.rows: parameter = row[0] member_type = row[1].replace("+", "") selector = row[2] if member_type == "" or "null" in parameter: continue member_string = "" # check for !ALG (algorithm type) algs = utils.expand_alg_macro(parameter, tpm_alg_ids) for alg in algs: dependence = alg.dependence if len(dependence) == 0: dependence = alg.short_name alg_placeholder = selector.replace("TPM_ALG_", "") # !ALG.* base_type = utils.replace_alg_placeholder( member_type, alg.short_name.upper()) new_type = parameter.replace( alg_placeholder + "_DIGEST_SIZE", alg.short_name.upper() + "_DIGEST_SIZE").replace(" ", "") if alg_placeholder == "!ALG": # fix for Table 2:144 new_type = alg.short_name.lower() else: new_type = new_type.replace(alg_placeholder, alg.short_name.lower()) member_string += "#ifdef TPM_ALG_" + dependence + "\n" member_string += " " + base_type + utils.indent( member_type) + new_type + ";\n" member_string += "#endif // TPM_ALG_" + dependence + "\n" # end of loop - for alg in algs: # in case there is no !ALG if not member_string: member_string = " " + member_type + utils.indent( member_type) + parameter + ";\n" if selector: # enclose with ifdef + endif member_string = "#ifdef " + selector + "\n" + member_string member_string += "#endif // " + selector + "\n" # end of if - if not member_string: self.tpm_types_h_file_content += member_string # end of loop - for row in table.rows: self.tpm_types_h_file_content += "} " + typedef_name + ";\n" self.marshaller.handle_union_table(table, tpm_alg_ids)
def handle_typedef_union(self, table, typedef_name, tpm_alg_ids): self.tpm_types_h_file_content += "typedef union {\n" for row in table.rows: parameter = row[0] member_type = row[1].replace("+", "") selector = row[2] if member_type == "" or "null" in parameter: continue member_string = "" # check for !ALG (algorithm type) algs = utils.expand_alg_macro(parameter, tpm_alg_ids) for alg in algs: dependence = alg.dependence if len(dependence) == 0: dependence = alg.short_name alg_placeholder = selector.replace("TPM_ALG_", "") # !ALG.* base_type = utils.replace_alg_placeholder(member_type, alg.short_name.upper()) new_type = parameter.replace(alg_placeholder + "_DIGEST_SIZE", alg.short_name.upper() + "_DIGEST_SIZE").replace(" ", "") if alg_placeholder == "!ALG": # fix for Table 2:144 new_type = alg.short_name.lower() else: new_type = new_type.replace(alg_placeholder, alg.short_name.lower()) member_string += "#ifdef TPM_ALG_" + dependence + "\n" member_string += " " + base_type + utils.indent(member_type) + new_type + ";\n" member_string += "#endif // TPM_ALG_" + dependence + "\n" # end of loop - for alg in algs: # in case there is no !ALG if not member_string: member_string = " " + member_type + utils.indent(member_type) + parameter + ";\n" if selector: # enclose with ifdef + endif member_string = "#ifdef " + selector + "\n" + member_string member_string += "#endif // " + selector + "\n" # end of if - if not member_string: self.tpm_types_h_file_content += member_string # end of loop - for row in table.rows: self.tpm_types_h_file_content += "} " + typedef_name + ";\n" self.marshaller.handle_union_table(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_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"