This library was created to assist with parsing endian sensitive content. It allows us to parse the data normally as though it was native endianness. Then when we need the value we can cast the value to the desired endianness.
This saves us from having to create conditional parsing using T::from_xx_bytes, and instead allows us to lazily parse the values. then convert them when we need them. this acts kinda like a endian “superposition”.
Since this also wraps all the basic scalar types, it gives us a common type to pass to functions that tags the data as endian sensitive.
Also included are helper functions to make loading values from a stream quicker.
Let’s look at a basic use case. We have a file that could of been created on a big or little endian system. The implementation of the file specification states the first byte in the file will signal if the rest of the file is big or little endian. Let’s say 0x00 for little endian, and 0x01 for big.
Let’s create a program that parses the first byte and stores the endianness of the file. Then using that endianness cast a value read from the file.
use Endian;
use ;
We can also work the other way around! Let’s create the file we just parsed by using Endian. We could easily use to_le_bytes and to_be_bytes; however, doing so would mean we need to alternate between the two depending on the system we’re targeting. Instead, we can just store the target endianness and use the same code. Dynamically switching the endianness with a stored variable instead. This is a lot cleaner.
Instead of using to_le_bytes or to_be_bytes; we’ll call the to_ne_bytes, and let Endian handle the converting.
use Endian;
use ;
use File;
use Path;
This was created to assist me with creating modding tools for video games. As some games share the same data, but the endianness changes based off the console it was built for. Working this way allows me to use the same code for all systems, and cast the values to the native endianness dynamically when needed. I figured I would share it on the off chance other people may find it useful.