Custom sources: plug your own bytes into the stream API.
Custom sources: plug your own bytes into the stream API.Archives, decompressors, encrypted assets: when the bytes come from somewhere Banjo does not know, give it a table of functions. Every stream function then works on your source: the byte-order readers, views when it can seek, and the loaders that take a stream, such as bj_create_bitmap_from_stream.
This example writes a source that unscrambles another stream as it reads: each byte is XORed with a key. It cannot seek, so it leaves seek and size out, and the stream says so.
#include <stdint.h>
};
uint8_t* bytes = (uint8_t*)buffer;
for (size_t i = 0; i < got; ++i) {
}
}
return got;
}
0,
0,
0,
0,
0,
};
int main(
int argc,
char* argv[]) {
(void)argc; (void)argv;
static const uint8_t asset[] = {
0x62, 0x4F, 0x46, 0x46, 0x45, 0x06, 0x0A, 0x59, 0x5E, 0x58, 0x4F, 0x4B, 0x47, 0x0B,
0x2D, 0xC0,
};
if (stream == 0) {
return 1;
}
char text[15] = {0};
uint16_t year = 0;
bj_info(
"\"%s\", %u", text, (
unsigned)year);
}
return 0;
}
struct bj_stream bj_stream
const char * bj_error_message(const struct bj_error *error)
Gets the error message from an error object.
void bj_clear_error(struct bj_error **error)
Frees an error and sets the pointer to NULL.
#define bj_info(...)
Log a message using the BJ_LOG_INFO level.
#define bj_err(...)
Log a message using the BJ_LOG_ERROR level.
void bj_destroy_stream(struct bj_stream *stream)
Destroy a stream and release its memory.
bj_bool bj_read_stream_u16_be(struct bj_stream *stream, uint16_t *value)
Reads a big-endian uint16_t.
struct bj_stream * bj_create_stream_from_callbacks(const struct bj_stream_callbacks *callbacks, uint32_t flags, void *user_data, struct bj_error **error)
Create a stream over a source the program supplies.
struct bj_stream * bj_create_stream_from_read_memory(const void *data, size_t length)
Create a read-only stream that reads from an in-memory buffer.
bj_bool bj_stream_is_seekable(const struct bj_stream *stream)
Whether the stream can move its cursor.
bj_stream_status
Why the last read or write of a stream fell short.
size_t bj_read_stream(struct bj_stream *stream, void *dest, size_t count)
Reads data from the stream into a destination buffer.
@ BJ_STREAM_READ
The stream reads.
@ BJ_STREAM_STATUS_END
The data ran out, or a fixed-size stream is full.
@ BJ_STREAM_STATUS_ERROR
The source failed.
The functions of a source the program supplies.
Logging utility functions.
Portable main() replacement with platform-aware entry shim.
Header file for struct bj_stream struct and related functions.
static size_t scrambled_read(void *buffer, size_t count, void *user_data, enum bj_stream_status *status)
static const struct bj_stream_callbacks scrambled_callbacks