Browse Source

loader fixes

master
Martin Dørum 8 months ago
parent
commit
d13946f97d
1 changed files with 37 additions and 28 deletions
  1. 37
    28
      lib/loader.c

+ 37
- 28
lib/loader.c View File

@@ -1,5 +1,7 @@
#include "loader.h"

#include <stdint.h>

int l2_bc_serialize(FILE *outf, l2_word *data, size_t len) {
char header[4] = { 0x1b, 0x6c, 0x32, 0x63 };
if (fwrite(header, 1, 4, outf) < 4) {
@@ -7,48 +9,54 @@ int l2_bc_serialize(FILE *outf, l2_word *data, size_t len) {
return -1;
}

char version[4] = {
(l2_bytecode_version & 0xff000000ul) >> 24,
(l2_bytecode_version & 0x00ff0000ul) >> 16,
(l2_bytecode_version & 0x0000ff00ul) >> 8,
(l2_bytecode_version & 0x000000fful) >> 0,
uint32_t version = l2_bytecode_version;

unsigned char version_buf[4] = {
(version & 0xff000000ull) >> 24,
(version & 0x00ff0000ull) >> 16,
(version & 0x0000ff00ull) >> 8,
(version & 0x000000ffull) >> 0,
};
if (fwrite(version, 1, 4, outf) < 4) {
if (fwrite(version_buf, 1, 4, outf) < 4) {
fprintf(stderr, "Write error\n");
return -1;
}

for (size_t i = 0; i < len; ++i) {
l2_word word = data[i];
char *dest = (char *)&data[i];
dest[0] = (word & 0xff000000ul) >> 24;
dest[1] = (word & 0x00ff0000ul) >> 16;
dest[2] = (word & 0x0000ff00ul) >> 8;
dest[3] = (word & 0x000000fful) >> 0;
}
struct l2_io_file_writer w = {
.f = outf,
};

if (fwrite(data, 4, len, outf) < len) {
fprintf(stderr, "Write error\n");
return -1;
struct l2_bufio_writer writer;
l2_bufio_writer_init(&writer, &w.w);

unsigned char word_buf[4];
for (size_t i = 0; i < len; ++i) {
uint32_t word = data[i];
word_buf[0] = (word & 0xff000000ull) >> 24;
word_buf[1] = (word & 0x00ff0000ull) >> 16;
word_buf[2] = (word & 0x0000ff00ull) >> 8;
word_buf[3] = (word & 0x000000ffull) >> 0;
l2_bufio_put_n(&writer, word_buf, 4);
}

l2_bufio_flush(&writer);
return 0;
}

int l2_bc_load(FILE *inf, struct l2_io_writer *w) {
// Header is already read by main

char version_buf[4];
unsigned char version_buf[4];
if (fread(version_buf, 1, 4, inf) < 4) {
fprintf(stderr, "Read error\n");
return -1;
}

int version = 0 |
((unsigned int)version_buf[0]) << 24 |
((unsigned int)version_buf[1]) << 16 |
((unsigned int)version_buf[2]) << 8 |
((unsigned int)version_buf[3]) << 0;
uint32_t version = 0 |
((uint32_t)version_buf[0]) << 24 |
((uint32_t)version_buf[1]) << 16 |
((uint32_t)version_buf[2]) << 8 |
((uint32_t)version_buf[3]) << 0;
if (version != l2_bytecode_version) {
fprintf(
stderr, "Version mismatch! Bytecode file uses bytecode version %i"
@@ -60,7 +68,8 @@ int l2_bc_load(FILE *inf, struct l2_io_writer *w) {
struct l2_bufio_writer writer;
l2_bufio_writer_init(&writer, w);

char buffer[4096];
// Must be divisible by 4
unsigned char buffer[4096];

while (1) {
size_t n = fread(buffer, 1, sizeof(buffer), inf);
@@ -71,10 +80,10 @@ int l2_bc_load(FILE *inf, struct l2_io_writer *w) {

for (size_t i = 0; i < n; i += 4) {
l2_word word = 0 |
((unsigned int)buffer[i + 0]) << 24 |
((unsigned int)buffer[i + 1]) << 16 |
((unsigned int)buffer[i + 2]) << 8 |
((unsigned int)buffer[i + 3]) << 0;
((uint32_t)buffer[i + 0]) << 24 |
((uint32_t)buffer[i + 1]) << 16 |
((uint32_t)buffer[i + 2]) << 8 |
((uint32_t)buffer[i + 3]) << 0;
l2_bufio_put_n(&writer, &word, 4);
}
}

Loading…
Cancel
Save