aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/build.c25
-rw-r--r--src/main.c2
-rw-r--r--src/parse.c9
3 files changed, 30 insertions, 6 deletions
diff --git a/src/build.c b/src/build.c
index e396407..d90b714 100644
--- a/src/build.c
+++ b/src/build.c
@@ -46,9 +46,28 @@ static void rec_write_bblc(struct tree *tree, FILE *file, char *byte, int *bit)
write_bit(1, file, byte, bit);
write_bit(1, file, byte, bit);
- // TODO: The bit-order of encoded shorts is kinda arbitrary
- short ref = tree->u.ref.table_index;
- for (int i = 0; i < 16; i++)
+ int ref = tree->u.ref.table_index;
+ int bits = 0;
+
+ // write index length bit prefixes
+ if (ref < 2 << 7) {
+ bits = 8;
+ write_bit(0, file, byte, bit);
+ write_bit(0, file, byte, bit);
+ } else if (ref < 2 << 15) {
+ bits = 16;
+ write_bit(0, file, byte, bit);
+ write_bit(1, file, byte, bit);
+ } else if (ref < 2 << 31) {
+ bits = 32;
+ write_bit(1, file, byte, bit);
+ write_bit(0, file, byte, bit);
+ } else if (ref < 2 << 63) {
+ bits = 64; // i wanna see that program lol
+ write_bit(1, file, byte, bit);
+ write_bit(1, file, byte, bit);
+ }
+ for (int i = 0; i < bits; i++)
write_bit((ref >> i) & 1, file, byte, bit);
break;
default:
diff --git a/src/main.c b/src/main.c
index 1d0a36f..785a70d 100644
--- a/src/main.c
+++ b/src/main.c
@@ -107,7 +107,7 @@ static void test(char *input)
char *input_2 = read_file(temp_blc);
struct term *parsed_2 = parse_blc(input_2);
fseek(temp_blc, 0, SEEK_END);
- fprintf(stderr, "size blc: ~%lu\n", ftell(temp_blc) / 8);
+ fprintf(stderr, "size blc: %lu\n", ftell(temp_blc) / 8 + 1);
fclose(temp_blc);
free(input_2);
debug("parsed reconstructed blc\n");
diff --git a/src/parse.c b/src/parse.c
index f6e047d..cf873c6 100644
--- a/src/parse.c
+++ b/src/parse.c
@@ -71,9 +71,14 @@ static struct term *parse_bloc_bblc(const char *term, size_t *bit)
(*bit)++;
} else if (!BIT_AT(*bit) && BIT_AT(*bit + 1) && BIT_AT(*bit + 2)) {
(*bit) += 3;
+
+ // selected bit pattern, see readme
+ int sel = (2 << (BIT_AT(*bit) * 2 + BIT_AT(*bit + 1) + 2));
+ (*bit) += 2;
+
res = new_term(REF);
- short index = 0;
- for (int i = 0; i < 16; i++) {
+ size_t index = 0;
+ for (int i = 0; i < sel; i++) {
index |= (BIT_AT(*bit) >> (7 - (*bit % 8))) << i;
(*bit) += 1;
}