Banjo API 1.1.0-alpha
Low-level C99 game development API
Loading...
Searching...
No Matches
stream_custom.c
Go to the documentation of this file.
1
22
23#include <banjo/log.h>
24#include <banjo/main.h>
25#include <banjo/stream.h>
26
27#include <stdint.h>
28
29// The source's state, handed to every function as user_data.
30struct scrambled {
31 bj_stream* inner; // where the scrambled bytes come from
32 uint8_t key;
33};
34
35static size_t scrambled_read(void* buffer, size_t count, void* user_data, enum bj_stream_status* status) {
36 struct scrambled* source = (struct scrambled*)user_data;
37 uint8_t* bytes = (uint8_t*)buffer;
38 const size_t got = bj_read_stream(source->inner, bytes, count);
39 for (size_t i = 0; i < got; ++i) {
40 bytes[i] ^= source->key;
41 }
42 // Returning fewer bytes is fine: the stream asks again. Returning 0
43 // with the status left OK means the end of the data; pass an error on.
44 if (got < count && bj_stream_status(source->inner) != BJ_STREAM_STATUS_END) {
45 *status = BJ_STREAM_STATUS_ERROR;
46 }
47 return got;
48}
49
50// Read-only, unseekable: everything but read stays 0. A static table
51// can serve every stream of this kind, since the stream copies it.
53 scrambled_read, // read
54 0, // write: the source never writes
55 0, // seek: nor seeks...
56 0, // size: ...so the stream cannot seek either
57 0, // flush
58 0, // close: the inner stream stays the caller's
59};
60
61int main(int argc, char* argv[]) {
62 (void)argc; (void)argv;
63
64 // "Hello, stream!" XORed with 0x2A, as an asset packer would store it,
65 // followed by a big-endian 2026 scrambled the same way.
66 static const uint8_t asset[] = {
67 0x62, 0x4F, 0x46, 0x46, 0x45, 0x06, 0x0A, 0x59, 0x5E, 0x58, 0x4F, 0x4B, 0x47, 0x0B,
68 0x2D, 0xC0,
69 };
70 bj_stream* inner = bj_create_stream_from_read_memory(asset, sizeof(asset));
71 struct scrambled source = { inner, 0x2A };
72
73 bj_error* error = 0;
75 if (stream == 0) {
76 bj_err("Cannot create the stream: %s", bj_error_message(error));
77 bj_clear_error(&error);
79 return 1;
80 }
81 bj_info("Seekable: %s", bj_stream_is_seekable(stream) ? "yes" : "no");
82
83 // The stream functions read through the source, unscrambled.
84 char text[15] = {0};
85 uint16_t year = 0;
86 if (bj_read_stream(stream, text, 14) == 14 && bj_read_stream_u16_be(stream, &year)) {
87 bj_info("\"%s\", %u", text, (unsigned)year);
88 }
89
90 bj_destroy_stream(stream);
92 return 0;
93}
struct bj_stream bj_stream
Definition api.h:374
struct bj_error bj_error
Definition api.h:348
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.
Definition log.h:141
#define bj_err(...)
Log a message using the BJ_LOG_ERROR level.
Definition log.h:169
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.
Definition stream.h:150
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.
Definition stream.h:130
@ BJ_STREAM_STATUS_END
The data ran out, or a fixed-size stream is full.
Definition stream.h:152
@ BJ_STREAM_STATUS_ERROR
The source failed.
Definition stream.h:154
The functions of a source the program supplies.
Definition stream.h:257
Logging utility functions.
Portable main() replacement with platform-aware entry shim.
Header file for struct bj_stream struct and related functions.
uint8_t key
bj_stream * inner
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
int main(void)