Skip to content

Commit

Permalink
Add armpack_decode_to_map
Browse files Browse the repository at this point in the history
  • Loading branch information
luboslenco committed Sep 11, 2024
1 parent ea8abf9 commit 791f69a
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 0 deletions.
105 changes: 105 additions & 0 deletions sources/iron_armpack.c
Original file line number Diff line number Diff line change
Expand Up @@ -490,3 +490,108 @@ int armpack_size_f32() {
int armpack_size_bool() {
return 1; // u8 tag
}

static char *read_string_alloc() {
char *s = read_string();
char *allocated = gc_alloc(string_length + 1);
memcpy(allocated, s, string_length);
allocated[string_length] = '\0';
return allocated;
}

any_map_t *_armpack_decode_to_map() {
any_map_t *result = any_map_create();
int32_t count = read_i32();

for (int i = 0; i < count; i++) {
read_u8(); // 0xdb string
char *key = read_string_alloc();

uint8_t flag = read_u8();
switch (flag) {
case 0xc0: // NULL
any_map_set(result, key, NULL);
break;
case 0xc2: // false
any_map_set(result, key, (void *)0);
break;
case 0xc3: // true
any_map_set(result, key, (void *)1);
break;
case 0xca: { // f32
float *fvalue = gc_alloc(sizeof(float));
*fvalue = read_f32();
any_map_set(result, key, fvalue);
break;
}
case 0xd2: { // i32
int32_t *ivalue = gc_alloc(sizeof(int32_t));
*ivalue = read_i32();
any_map_set(result, key, ivalue);
break;
}
case 0xdb: // string
any_map_set(result, key, read_string_alloc());
break;
case 0xdf: { // map
any_map_t *nested_map = _armpack_decode_to_map();
any_map_set(result, key, nested_map);
break;
}
case 0xdd: { // array
int32_t array_count = read_i32();
uint8_t element_flag = read_u8();
if (element_flag == 0xca) { // f32
f32_array_t *array = f32_array_create(array_count);
for (int j = 0; j < array_count; j++) {
array->buffer[j] = read_f32();
}
any_map_set(result, key, array);
}
else if (element_flag == 0xd2) { // i32
i32_array_t *array = i32_array_create(array_count);
for (int j = 0; j < array_count; j++) {
array->buffer[j] = read_i32();
}
any_map_set(result, key, array);
}
else if (element_flag == 0xdb) { // string
ei--;
char_ptr_array_t *array = char_ptr_array_create(array_count);
for (int j = 0; j < array_count; j++) {
read_u8(); // flag
array->buffer[j] = read_string_alloc();
}
any_map_set(result, key, array);
}
else if (element_flag == 0xdf) { // map
ei--;
any_array_t *array = any_array_create(array_count);
for (int j = 0; j < array_count; j++) {
read_u8(); // flag
array->buffer[j] = _armpack_decode_to_map();
}
any_map_set(result, key, array);
}
break;
}
case 0xc6: { // buffer_t (deprecated)
int32_t buffer_size = read_i32();
buffer_t *buffer = buffer_create(buffer_size);
for (int j = 0; j < buffer_size; j++) {
buffer->buffer[j] = read_u8();
}
any_map_set(result, key, buffer);
}
}
}

return result;
}

any_map_t *armpack_decode_to_map(buffer_t *b) {
encoded = b->buffer;
ei = 0;
read_u8(); // Must be 0xdf for a map
return _armpack_decode_to_map();
}
2 changes: 2 additions & 0 deletions sources/iron_armpack.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <stdint.h>
#include <stdbool.h>
#include "iron_array.h"
#include "iron_map.h"

#ifdef __GNUC__
#define PACK(__Declaration__) __Declaration__ __attribute__((__packed__))
Expand All @@ -17,6 +18,7 @@
#endif

void *armpack_decode(buffer_t *b);
any_map_t *armpack_decode_to_map(buffer_t *b);

void armpack_encode_start(void *encoded);
int armpack_encode_end();
Expand Down
1 change: 1 addition & 0 deletions sources/ts/iron.ts
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,7 @@ declare function iron_language(): string;
declare function iron_obj_parse(file_bytes: buffer_t, split_code: i32, start_pos: i32, udim: bool): any;

declare function armpack_decode(b: buffer_t): any;
declare function armpack_decode_to_map(b: buffer_t): map_t<string, any>;
declare function armpack_encode_start(encoded: any): void;
declare function armpack_encode_end(): i32;
declare function armpack_encode_map(count: u32): void;
Expand Down

0 comments on commit 791f69a

Please sign in to comment.