- util_endianswap(&code_header.version, 1, sizeof(code_header.version));
- util_endianswap(&code_header.crc16, 1, sizeof(code_header.crc16));
- util_endianswap(&code_header.statements, 2, sizeof(code_header.statements.offset));
- util_endianswap(&code_header.defs, 2, sizeof(code_header.statements.offset));
- util_endianswap(&code_header.fields, 2, sizeof(code_header.statements.offset));
- util_endianswap(&code_header.functions, 2, sizeof(code_header.statements.offset));
- util_endianswap(&code_header.strings, 2, sizeof(code_header.statements.offset));
- util_endianswap(&code_header.globals, 2, sizeof(code_header.statements.offset));
- util_endianswap(&code_header.entfield, 1, sizeof(code_header.entfield));
- util_endianswap(code_statements, vec_size(code_statements), sizeof(prog_section_statement));
- util_endianswap(code_defs, vec_size(code_defs), sizeof(prog_section_def));
- util_endianswap(code_fields, vec_size(code_fields), sizeof(prog_section_field));
- util_endianswap(code_functions, vec_size(code_functions), sizeof(prog_section_function));
- util_endianswap(code_globals, vec_size(code_globals), sizeof(int32_t));
+ util_endianswap(&code_header->version, 1, sizeof(code_header->version));
+ util_endianswap(&code_header->crc16, 1, sizeof(code_header->crc16));
+ util_endianswap(&code_header->statements, 2, sizeof(code_header->statements.offset));
+ util_endianswap(&code_header->defs, 2, sizeof(code_header->statements.offset));
+ util_endianswap(&code_header->fields, 2, sizeof(code_header->statements.offset));
+ util_endianswap(&code_header->functions, 2, sizeof(code_header->statements.offset));
+ util_endianswap(&code_header->strings, 2, sizeof(code_header->statements.offset));
+ util_endianswap(&code_header->globals, 2, sizeof(code_header->statements.offset));
+ util_endianswap(&code_header->entfield, 1, sizeof(code_header->entfield));
+
+ /*
+ * These are not part of the header but we ensure LE format here to save on duplicated
+ * code.
+ */
+ util_endianswap(code->statements, vec_size(code->statements), sizeof(prog_section_statement));
+ util_endianswap(code->defs, vec_size(code->defs), sizeof(prog_section_def));
+ util_endianswap(code->fields, vec_size(code->fields), sizeof(prog_section_field));
+ util_endianswap(code->functions, vec_size(code->functions), sizeof(prog_section_function));
+ util_endianswap(code->globals, vec_size(code->globals), sizeof(int32_t));
+}
+
+/*
+ * Same principle except this one allocates memory and writes the lno(optional) and the dat file
+ * directly out to allocated memory. Which is actually very useful for the future library support
+ * we're going to add.
+ */
+bool code_write_memory(code_t *code, uint8_t **datmem, size_t *sizedat, uint8_t **lnomem, size_t *sizelno) {
+ prog_header code_header;
+ uint32_t offset = 0;
+
+ if (!datmem)
+ return false;
+
+ code_create_header(code, &code_header);
+
+ #define WRITE_CHUNK(C,X,S) \
+ do { \
+ memcpy((void*)(&(*C)[offset]), (const void*)(X), (S)); \
+ offset += (S); \
+ } while (0)
+
+ /* Calculate size required to store entire file out to memory */
+ if (lnomem) {
+ uint32_t version = 1;
+
+ *sizelno += 4; /* LNOF */
+ *sizelno += sizeof(version);
+ *sizelno += sizeof(code_header.defs.length);
+ *sizelno += sizeof(code_header.globals.length);
+ *sizelno += sizeof(code_header.fields.length);
+ *sizelno += sizeof(code_header.statements.length);
+ *sizelno += sizeof(code->linenums[0]) * vec_size(code->linenums);
+
+ *lnomem = (uint8_t*)mem_a(*sizelno);
+
+ WRITE_CHUNK(lnomem, "LNOF", 4);
+ WRITE_CHUNK(lnomem, &version, sizeof(version));
+ WRITE_CHUNK(lnomem, &code_header.defs.length, sizeof(code_header.defs.length));
+ WRITE_CHUNK(lnomem, &code_header.globals.length, sizeof(code_header.globals.length));
+ WRITE_CHUNK(lnomem, &code_header.fields.length, sizeof(code_header.fields.length));
+ WRITE_CHUNK(lnomem, &code_header.statements.length, sizeof(code_header.statements.length));
+
+ /* something went terribly wrong */
+ if (offset != *sizelno) {
+ mem_d(*lnomem);
+ *sizelno = 0;
+ return false;
+ }
+ offset = 0;
+ }
+
+ /* Write out the dat */
+ *sizedat += sizeof(prog_header);
+ *sizedat += sizeof(prog_section_statement) * vec_size(code->statements);
+ *sizedat += sizeof(prog_section_def) * vec_size(code->defs);
+ *sizedat += sizeof(prog_section_field) * vec_size(code->fields);
+ *sizedat += sizeof(prog_section_function) * vec_size(code->functions);
+ *sizedat += sizeof(int32_t) * vec_size(code->globals);
+ *sizedat += 1 * vec_size(code->chars);
+
+ *datmem = (uint8_t*)mem_a(*sizedat);
+
+ WRITE_CHUNK(datmem, &code_header, sizeof(prog_header));
+ WRITE_CHUNK(datmem, code->statements, sizeof(prog_section_statement) * vec_size(code->statements));
+ WRITE_CHUNK(datmem, code->defs, sizeof(prog_section_def) * vec_size(code->defs));
+ WRITE_CHUNK(datmem, code->fields, sizeof(prog_section_field) * vec_size(code->fields));
+ WRITE_CHUNK(datmem, code->functions, sizeof(prog_section_function) * vec_size(code->functions));
+ WRITE_CHUNK(datmem, code->globals, sizeof(int32_t) * vec_size(code->globals));
+ WRITE_CHUNK(datmem, code->chars, 1 * vec_size(code->chars));
+
+ #undef WRITE_CHUNK
+
+ vec_free(code->statements);
+ vec_free(code->linenums);
+ vec_free(code->defs);
+ vec_free(code->fields);
+ vec_free(code->functions);
+ vec_free(code->globals);
+ vec_free(code->chars);
+
+ util_htdel(code->string_cache);
+ mem_d(code);
+
+ return true;
+}
+
+bool code_write(code_t *code, const char *filename, const char *lnofile) {
+ prog_header code_header;
+ FILE *fp = NULL;
+ size_t it = 2;
+
+ code_create_header(code, &code_header);