char * type_builder_t::get_structure(char * name, char * bufferr, int buffer_size) { sort_fields(structure); char *buffer = bufferr; int offs = 0; buffer += sprintf_s(buffer, buffer_size - (int)(buffer - bufferr), "struct %s {\r\n", name); for(unsigned int i = 0 ; i < structure.size() ; i ++) { if(structure[i].offset > offs) { buffer += sprintf_s(buffer, buffer_size - (int)(buffer - bufferr), "\tchar\tfiller_%d[%d];\r\n", i, structure[i].offset - offs); offs = structure[i].offset; } if(structure[i].offset == offs) { buffer += sprintf_s(buffer, buffer_size - (int)(buffer - bufferr), "\t%s\tfield_%d;\r\n", get_type_nm(structure[i].size), i); offs += structure[i].size; } } buffer += sprintf_s(buffer, buffer_size - (int)(buffer - bufferr), "}"); return NULL; }
/* * Normally enums are required to be ascending in the schema and * therefore there is no need to sort enums. If not, we export them in * the order defined anyway becuase there is no well-defined ordering * and blindly sorting the content would just loose more information. * * In conclusion: find by enum value is only support when enums are * defined in consequtive order. * * refers to: `opts->ascending_enum` * * `size` must hold the maximum buffer size. * Returns intput buffer if successful and updates size argument. */ void *fb_codegen_bfbs_to_buffer(fb_options_t *opts, fb_schema_t *S, void *buffer, size_t *size) { flatcc_builder_t builder, *B; B = &builder; flatcc_builder_init(B); export_schema(B, opts, S); if (!flatcc_builder_copy_buffer(B, buffer, *size)) { goto done; } sort_fields(buffer); done: *size = flatcc_builder_get_buffer_size(B); flatcc_builder_clear(B); return buffer; }
/* * Like to_buffer, but returns allocated buffer. * Updates size argument with buffer size if not null. * Returned buffer must be deallocatd with `free`. * The buffer is malloc aligned which should suffice for reflection buffers. */ void *fb_codegen_bfbs_alloc_buffer(fb_options_t *opts, fb_schema_t *S, size_t *size) { flatcc_builder_t builder, *B; void *buffer = 0; B = &builder; flatcc_builder_init(B); if (export_schema(B, opts, S)) { goto done; } if (!(buffer = flatcc_builder_finalize_buffer(B, size))) { goto done; } sort_fields(buffer); done: flatcc_builder_clear(B); return buffer; }
tid_t type_builder_t::get_structure(char * name) { tid_t struct_type_id = add_struc(BADADDR, name); if (struct_type_id != 0 || struct_type_id != -1) { struc_t * struc = get_struc(struct_type_id); if(struc != NULL) { sort_fields(structure); int offs = 0; opinfo_t opinfo; opinfo.tid = struct_type_id; for(unsigned int i = 0 ; i < structure.size() ; i ++) { if(structure[i].offset > offs) { offs = structure[i].offset; } flags_t member_flgs = 0; if(structure[i].size == 1) member_flgs = byteflag(); else if (structure[i].size == 2) member_flgs = wordflag(); else if (structure[i].size == 4) member_flgs = dwrdflag(); else if (structure[i].size == 8) member_flgs = qwrdflag(); char field_name[258]; memset(field_name, 0x00, sizeof(field_name)); sprintf_s(field_name, sizeof(field_name), "field_%d", i); int iRet = add_struc_member(struc, field_name, structure[i].offset, member_flgs, NULL, structure[i].size); offs += structure[i].size; } } } return struct_type_id; }