+ vec_push(code->chars, '\0'); /* > */
+ vec_push(code->chars, '\0'); /* = */
+ vec_push(code->chars, '\0'); /* P */
+ }
+
+ /* ensure all data is in LE format */
+ 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;