Files
std/io/byte_writer.sakura
T
2026-10-28 00:51:45 -05:00

285 lines
9.7 KiB
Plaintext

#! @file byte_writer.sakura
#! @author John Christman (sorakatadzuma@gmail.com)
#! @copyright Malunal Studios, LLC.
#! @license MIT
#!
#! Contains `byte_writer`, the stream that writes to memory: it lays out bytes
#! one after another, as a file or a block of machine code is laid out, and is
#! `writable`, so whatever writes a stream writes one.
import system;
module std::io;
#! A `byte_writer` holds bytes written one after another: integers least
#! significant byte first, as x86-64 stores them, views of bytes, zeros up to an
#! alignment, and words written over places already written, such as a length
#! known only once what it counts is written.
#!
#! They are useful wherever a binary format is made, such as an object file, a
#! table of debugging information, or machine code.
#! @remarks Integers are written as the processor stores them, so the bytes are
#! least significant first only where it is, as x86-64 is.
pub struct byte_writer
: owning
, moveable
, writable
{
var _dat: vector<u8>;
}
#! Provides read only access to how many bytes are written.
#! @param this The writer.
#! @returns The number of bytes, which is where the next one goes.
pub val byte_writer::length: u64 {
get => this._dat.length;
}
#! Creates a writer with no allocator, holding no bytes.
#! @returns The created writer.
#! @remarks Will use the built in `raw_allocate`, `raw_release` and `raw_resize`
#! for memory management.
pub def byte_writer::new(): byte_writer {
return byte_writer::new(nothing{});
}
#! Creates a writer with an optional allocator, holding no bytes.
#! @param alloc The optional allocator for the memory backing of the writer.
#! @returns The created writer.
pub def byte_writer::new(alloc: maybe<&mut allocator>): byte_writer {
return byte_writer { _dat = vector::<u8>::new(alloc) };
}
#! Creates a writer by moving the bytes of another into it.
#! @param other The writer to move from, which holds nothing after.
#! @returns The moved writer, which takes the memory and allocator with it.
pub def byte_writer::move_of(other: &mut byte_writer): byte_writer {
return byte_writer { _dat = move(&mut other._dat) };
}
#! Lends a view of the bytes written.
#! @param this The writer.
#! @returns The view, which is only good until the writer grows or closes.
pub def byte_writer::view(&this): span<u8> {
return this._dat.view();
}
#! Lends a view of the bytes written that writes them where they are.
#! @param this The writer.
#! @returns The view, which is only good until the writer grows or closes.
pub def byte_writer::slice(&mut this): slice<u8> {
return this._dat.slice();
}
#! Makes room for bytes after the last, giving where they begin, for the caller
#! to write.
#! @param this The writer.
#! @param count How many bytes.
#! @returns Where the first of them is, which is only good until the writer
#! grows or closes.
#! @remarks An allocator that gives no more memory stops the program.
pub def byte_writer::room(&mut this, count: u64): *mut u8 {
val at := this._dat.length;
this._dat.resize(at + count);
return this._dat.data + at;
}
#! Adds a byte.
#! @param this The writer.
#! @param value The byte.
pub def byte_writer::byte(&mut this, value: u8): void {
this._dat.append(value);
}
#! Adds a 16-bit word, least significant byte first.
#! @param this The writer.
#! @param value The word, of which the low 16 bits are written.
pub def byte_writer::u16(&mut this, value: u64): void {
*(this.room(2) as *mut u16) = value as u16;
}
#! Adds a 32-bit word, least significant byte first.
#! @param this The writer.
#! @param value The word, of which the low 32 bits are written.
pub def byte_writer::u32(&mut this, value: u64): void {
*(this.room(4) as *mut u32) = (value & 0xFFFFFFFF) as u32;
}
#! Adds a 64-bit word, least significant byte first.
#! @param this The writer.
#! @param value The word.
pub def byte_writer::u64(&mut this, value: u64): void {
*(this.room(8) as *mut u64) = value;
}
#! Adds bytes.
#! @param this The writer.
#! @param data The bytes.
pub def byte_writer::put(&mut this, data: span<u8>): void {
return if data.length == 0;
memcpy(this.room(data.length), data.data, data.length);
}
#! Writes bytes after the last, as a stream is written, so a byte writer is
#! given where anything `writable` is wanted.
#! @param this The writer.
#! @param data The bytes to write.
#! @returns Nothing, since a writer always takes them: an allocator that gives
#! no more memory stops the program.
pub def byte_writer::write(&mut this, data: span<u8>): result<void, io_error> {
this.put(data);
return;
}
#! Adds bytes, then a zero after them, as C ends its strings.
#! @param this The writer.
#! @param text The bytes, which hold no zero of their own where the result is
#! to be read as one string.
pub def byte_writer::put_terminated(&mut this, text: span<u8>): void {
this.put(text);
this._dat.append(0);
}
#! Adds zeros.
#! @param this The writer.
#! @param count How many.
pub def byte_writer::zeros(&mut this, count: u64): void {
this._dat.resize(this._dat.length + count);
}
#! Adds zeros until the length is a multiple of an alignment.
#! @param this The writer.
#! @param align The alignment, which is not zero.
#! @returns The length after, which is where what is aligned begins.
pub def byte_writer::align_to(&mut this, align: u64): u64 {
val over := this._dat.length % align;
if over != 0 {
this.zeros(align - over);
}
return this._dat.length;
}
#! Adds a 32-bit length that is not known yet, as zero, to be written by
#! `end_length` once what it counts is.
#! @param this The writer.
#! @returns Where the length is, which `end_length` is given.
pub def byte_writer::begin_length(&mut this): u64 {
val at := this._dat.length;
this.u32(0);
return at;
}
#! Writes a length `begin_length` began: how many bytes follow it.
#! @param this The writer.
#! @param at Where the length is, as `begin_length` gave it.
pub def byte_writer::end_length(&mut this, at: u64): void {
this.set_u32(at, this._dat.length - at - 4);
}
#! Writes a byte over the one at a place.
#! @param this The writer.
#! @param at The place, below the length, past which a checked program
#! stops.
#! @param value The byte.
pub def byte_writer::set_byte(&mut this, at: u64, value: u8): void {
this._dat[at] = value;
}
#! Writes a 16-bit word over the two bytes at a place.
#! @param this The writer.
#! @param at The place, whose bytes are written, or else a checked program
#! stops.
#! @param value The word, of which the low 16 bits are written.
pub def byte_writer::set_u16(&mut this, at: u64, value: u64): void {
*(this.place(at, 2) as *mut u16) = value as u16;
}
#! Writes a 32-bit word over the four bytes at a place.
#! @param this The writer.
#! @param at The place, whose bytes are written, or else a checked program
#! stops.
#! @param value The word, of which the low 32 bits are written.
pub def byte_writer::set_u32(&mut this, at: u64, value: u64): void {
*(this.place(at, 4) as *mut u32) = (value & 0xFFFFFFFF) as u32;
}
#! Writes a 64-bit word over the eight bytes at a place.
#! @param this The writer.
#! @param at The place, whose bytes are written, or else a checked program
#! stops.
#! @param value The word.
pub def byte_writer::set_u64(&mut this, at: u64, value: u64): void {
*(this.place(at, 8) as *mut u64) = value;
}
#! Reads the byte at a place.
#! @param this The writer.
#! @param at The place, below the length, past which a checked program
#! stops.
#! @returns The byte.
pub def byte_writer::get_byte(&this, at: u64): u8 {
return this._dat[at];
}
#! Reads the 32-bit word at a place.
#! @param this The writer.
#! @param at The place, whose bytes are written, or else a checked program
#! stops.
#! @returns The word.
pub def byte_writer::get_u32(&this, at: u64): u32 {
return *(this.place(at, 4) as *u32);
}
#! Reads the 64-bit word at a place.
#! @param this The writer.
#! @param at The place, whose bytes are written, or else a checked program
#! stops.
#! @returns The word.
pub def byte_writer::get_u64(&this, at: u64): u64 {
return *(this.place(at, 8) as *u64);
}
#! Removes the bytes from a place on, so that what is written next goes there,
#! as code made again shorter is.
#! @param this The writer.
#! @param length How many bytes to keep, no more than the length, or else a
#! checked program stops.
#! @remarks The memory is kept for the bytes written after.
pub def byte_writer::rewind(&mut this, length: u64): void {
if const checking() {
if length > this._dat.length {
past_end(length, this._dat.length);
}
}
this._dat.resize(length);
}
#! Removes every byte, keeping the memory.
#! @param this The writer to empty.
pub def byte_writer::clear(&mut this): void {
this._dat.clear();
}
#! Removes every byte and gives back the memory the writer holds.
#! @param this The writer to close.
pub def byte_writer::close(&mut this): void {
this._dat.close();
}
# Where bytes written before begin, stopping the program where they reach past
# the length, as indexing them one at a time would.
def byte_writer::place(&this, at: u64, size: u64): *mut u8 {
if const checking() {
if at + size > this._dat.length {
past_end(at + size - 1, this._dat.length);
}
}
return this._dat.data + at;
}
# Stops the program where a writer is asked for bytes past its end, as indexing
# a view outside its length does. It's called only where the check fails, so
# that where the check holds, it costs a compare.
def past_end(index: u64, length: u64): void {
fault($"Index {index} is out of bounds: the length is {length}.");
}