added binary_reader package. probably going to remove soon
authorBrendan Hansen <brendan.f.hansen@gmail.com>
Sun, 18 Apr 2021 16:16:17 +0000 (11:16 -0500)
committerBrendan Hansen <brendan.f.hansen@gmail.com>
Sun, 18 Apr 2021 16:16:17 +0000 (11:16 -0500)
core/io/binary_reader.onyx [new file with mode: 0644]
core/std.onyx

diff --git a/core/io/binary_reader.onyx b/core/io/binary_reader.onyx
new file mode 100644 (file)
index 0000000..df6ab70
--- /dev/null
@@ -0,0 +1,82 @@
+package core.io.binary
+
+// NOTE: This is a very experimental and not well put together package.
+// The primary reason it exists is because I was working on reading TTF
+// files which requires a big-endian binary reader, so that's what I made.
+// There will "soon" be an option to put this in little endian mode and
+// the goal will be to make this useful for serialization.
+
+// After sleeping on it, everything here shouldn't need to exist. The already
+// existing BinaryReader from binary.onyx should be able to do all of this
+// when big endian integers are added to the language.
+
+BinaryReader :: struct {
+    data : [] u8;
+    pos  : u32;
+}
+
+create_reader :: (data: [] u8, initial_pos := 0) -> BinaryReader {
+    return BinaryReader.{ data = data, pos = initial_pos };
+}
+
+seek :: (use br: ^BinaryReader, new_pos: u32) -> u32 {
+    old_pos := pos;
+    pos = new_pos;
+    return old_pos;
+}
+
+tell :: (use br: ^BinaryReader) -> u32 do return pos;
+
+read_u8 :: (use br: ^BinaryReader) -> u8 {
+    ret := data[pos];
+    pos += 1;
+    return ret;
+}
+
+read_u16 :: (use br: ^BinaryReader) -> u16 do return ~~(read_u8(br) << ~~8  | read_u8(br));
+
+read_u32 :: (use br: ^BinaryReader) -> u32 {
+    // Encoding is big endian
+    ret: u32 = 0;
+    ret |= cast(u32) read_u8(br) << 24;
+    ret |= cast(u32) read_u8(br) << 16;
+    ret |= cast(u32) read_u8(br) << 8;
+    ret |= cast(u32) read_u8(br);
+    return ret;
+}
+
+read_i16 :: (use br: ^BinaryReader) -> i16 {
+    ret := read_u16(br);
+    if ret & ~~0x8000 != ~~0 do ret -= ~~(1 << 16);
+    return ret;
+}
+
+read_i32 :: (use br: ^BinaryReader) -> i32 {
+    return ~~read_u32(br);
+}
+
+read_fword :: (use br: ^BinaryReader) -> i16 do return read_i16(br);
+
+read_2dot14 :: (use br: ^BinaryReader) -> f32 {
+    val := cast(i32) read_i16(br);
+    return ~~val / cast(f32) (1 << 14);
+}
+
+read_fixed :: (use br: ^BinaryReader) -> f32 {
+    val := read_i32(br);
+    return ~~val / cast(f32) (1 << 16);
+}
+
+read_string :: (use br: ^BinaryReader, len: i32) -> str {
+    ret := data.data[pos .. pos + len];
+    pos += len;
+    return ret;
+}
+
+read_date :: (use br: ^BinaryReader) -> u64 {
+    mac_time := (cast(u64) (read_u32(br))) << 32 |
+                (cast(u64) (read_u32(br)));
+    utf_time := mac_time - 2082844800;
+    return utf_time;
+}
+
index 7109a87ab2d98a6ef3c5f0afc947222399cd4e60..77d6c911d61c6e3a0d5f4ac80ba40a694c6aff3e 100644 (file)
@@ -24,6 +24,7 @@ package core
 #load "core/io/reader"
 #load "core/io/writer"
 #load "core/io/binary"
+#load "core/io/binary_reader"
 
 #load "core/runtime/build_opts"
 #load "core/runtime/common"