Compare commits

..

8 Commits

Author SHA1 Message Date
35335c4355 remove description with mistakes 2024-02-15 13:36:16 +09:00
eaaad38fac fix issues 2024-02-15 13:34:06 +09:00
e07604dc4b add new target of an executable to unpack binary log data 2024-02-15 09:30:55 +09:00
2d71b4cec2 add acg_stream_read functions 2024-02-15 09:30:05 +09:00
a1cefa690d fix an issue in example program 2024-02-12 23:18:38 +09:00
f0ae9692fc add an example program 2024-02-12 23:14:07 +09:00
d75ef72583 update format descriptions 2024-02-12 23:13:57 +09:00
e149c87868 add functions to write chunks 2024-02-12 23:13:46 +09:00
7 changed files with 505 additions and 110 deletions

View File

@@ -33,13 +33,23 @@ target_compile_options(allcing_config INTERFACE
# ---- library header
add_library(allcing INTERFACE)
target_sources(allcing INTERFACE allcing.h)
target_link_libraries(allcing INTERFACE allcing_config)
target_include_directories(allcing SYSTEM INTERFACE .)
# ---- test binary
if (BUILD_TESTING)
add_executable(allcing_test)
target_sources(allcing_test PRIVATE test.c)
target_link_libraries(allcing_test PRIVATE allcing)
target_link_libraries(allcing_test PRIVATE allcing allcing_config)
add_test(NAME allcing_test COMMAND $<TARGET_FILE:allcing_test>)
endif()
# ---- example binary
add_executable(allcing_example EXCLUDE_FROM_ALL)
target_sources(allcing_example PRIVATE example.c)
target_link_libraries(allcing_example PRIVATE allcing allcing_config)
# ---- tool subdirs
add_subdirectory(tool EXCLUDE_FROM_ALL)

View File

@@ -9,33 +9,9 @@ ALLCING is a structured logging library featuring on lightweightness.
- written in C23
- single header
- stream abstraction
- compact log data with simple binary format
- completely-free license (WTFPLv2)
# Log Data Format
```
# [TOKEN] means TOKEN is optional
# <TOKEN> means TOKEN is constant and refer allcing.h for actual value
ROOT := CHUNK_BEGIN | CHUNK_END | CHUNK_CHECK
CHUNK_BEGIN := <ACG_CHUNK_BEGIN> [CHUNK_DATA]
CHUNK_END := <ACG_CHUNK_END> [CHUNK_DATA]
CHUNK_CHECK := <ACG_CHUNK_CHECK> [CHUNK_DATA]
CHUNK_DATA := CHUNK_PAD | CHUNK_BLOB | CHUNK_CTX | CHUNK_LOC | CHUNK_DATA
CHUNK_PAD := <ACG_CHUNK_PAD> PAD
CHUNK_BLOB := <ACG_CHUNK_BLOB> BLOB
CHUNK_CTX := <ACG_CHUNK_CTX> UINT(64) UINT(16)
CHUNK_LOC := <ACG_CHUNK_LOC> UINT(8) BLOB
PAD :=
BLOB :=
UINT(x) :=
```
# License
Do What The Fuck You Want to Public License v2

249
allcing.h
View File

@@ -5,8 +5,62 @@
#include <stdbool.h>
#include <stdint.h>
#include <string.h>
#include <time.h>
// ---- macro utilities
#define ACG_UTIL_STR(v) #v
#define ACG_UTIL_STR2(v) ACG_UTIL_STR(v)
// ---- variable points
#if !defined(ACG_NOW)
# define ACG_NOW ((uint64_t) ((double) clock()/CLOCKS_PER_SEC*1000000U))
#endif
#if !defined(ACG_TID)
# define ACG_TID (0x700FU) // you should replace ACG_TID to log an actual TID
#endif
#if !defined(ACG_LOC_FULL)
# define ACG_LOC_FULL (__FILE__ ":" ACG_UTIL_STR2(__LINE__))
#endif
#if !defined(ACG_LOC_SHORT)
# define ACG_LOC_SHORT ACG_UTIL_STR2(__LINE__)
#endif
#if !defined(ACG_FLUSH)
# define ACG_FLUSH() (true)
#endif
// ---- sugar syntax for logging
#define ACG_BLOB_LITERAL(s, data) ( \
acg_stream_write_chunk_blob((s), sizeof((data)), (const uint8_t*) (data)) && \
ACG_FLUSH((s)) \
)
#define ACG_BLOB(s, size, data) ( \
acg_stream_write_chunk_blob((s), (size), (const uint8_t*) (data)) && \
ACG_FLUSH((s)) \
)
#define ACG_BEGIN(s) ( \
acg_stream_write_chunk_begin((s)) && \
acg_stream_write_chunk_loc((s), sizeof(ACG_LOC_FULL)-1U, (const uint8_t*) ACG_LOC_FULL) && \
acg_stream_write_chunk_ctx((s), ACG_NOW, ACG_TID) && \
ACG_FLUSH((s)) \
)
#define ACG_END(s) ( \
acg_stream_write_chunk_end((s)) && \
acg_stream_write_chunk_loc((s), sizeof(ACG_LOC_SHORT)-1U,(const uint8_t*) ACG_LOC_SHORT) && \
acg_stream_write_chunk_ctx((s), ACG_NOW, ACG_TID) && \
ACG_FLUSH((s)) \
)
#define ACG_CHECK(s) ( \
acg_stream_write_chunk_check((s)) && \
acg_stream_write_chunk_loc((s), sizeof(ACG_LOC_SHORT)-1U,(const uint8_t*) ACG_LOC_SHORT) && \
acg_stream_write_chunk_ctx((s), ACG_NOW, ACG_TID) && \
ACG_FLUSH((s)) \
)
// ---- constants for binary format
#define ACG_CHUNK_PAD 0x00U
#define ACG_CHUNK_BLOB 0x01U
#define ACG_CHUNK_CTX 0x02U
@@ -18,6 +72,7 @@
#define ACG_UNITBITS_CHUNK_ID 4U
// ---- utility functions
static inline uint64_t acg_util_fullbits(uint8_t bits) {
return (((uint64_t) 1U) << bits) - 1U;
}
@@ -26,13 +81,16 @@ static inline uint64_t acg_util_umin(uint64_t a, uint64_t b) {
}
// ---- stream type definition and its functions
struct acg_stream {
uint8_t* buffer;
uint64_t size_bytes;
uint64_t cursor_bits;
void* udata;
};
#if !defined(ACG_NO_WRITE_FUNCTIONS)
static inline bool acg_stream_write_pad(struct acg_stream* s) {
assert(NULL != s);
@@ -59,64 +117,34 @@ static inline bool acg_stream_write_uint(struct acg_stream* s, uint64_t v, uint8
uint64_t* const c = &s->cursor_bits;
const uint8_t unit_data_bits = unit_bits - 1U;
while (v) {
do {
if (s->size_bytes*8U - *c < unit_bits) {
return false;
}
const uint64_t next_v = v >> unit_data_bits;
acg_stream_write_bool(s, !!next_v);
const uint8_t frag_bits = acg_util_umin(unit_data_bits, (8U - *c%8U)%8U);
if (frag_bits > 0U) {
buf[*c/8U] |= (v & acg_util_fullbits(frag_bits)) << (*c%8U);
*c += frag_bits;
}
const uint8_t remain_bits = unit_data_bits - frag_bits;
const uint64_t v_part = (v >> frag_bits) & acg_util_fullbits(remain_bits);
memcpy(&buf[*c/8U], &v_part, (remain_bits + 7U) / 8U);
*c += remain_bits;
const uint8_t remain_bits = unit_data_bits - frag_bits;
if (remain_bits > 0U) {
assert(*c%8U == 0U);
v = next_v;
}
const uint64_t v_part = (v >> frag_bits) & acg_util_fullbits(remain_bits);
memcpy(&buf[*c/8U], &v_part, (remain_bits + 7U) / 8U);
*c += remain_bits;
}
v >>= unit_data_bits;;
acg_stream_write_bool(s, !!v);
} while (v);
return true;
}
static inline bool acg_stream_write_int(struct acg_stream* s, int64_t v, uint8_t unit_bits) {
assert(NULL != s);
assert(2U <= unit_bits);
assert(65U >= unit_bits);
if (v >= 0U) {
return
acg_stream_write_bool(s, false) &&
acg_stream_write_uint(s, (uint64_t) v, unit_bits);
} else {
return
acg_stream_write_bool(s, true) &&
acg_stream_write_uint(s, (uint64_t) -v, unit_bits);
}
}
static inline bool acg_stream_write_chunk_pad(struct acg_stream* s) {
static inline bool acg_stream_write_blob(struct acg_stream* s, uint64_t size_bytes, const uint8_t* buf) {
assert(NULL != s);
if (s->cursor_bits%8U == 0U) {
return true;
}
if (s->size_bytes <= s->cursor_bits/8U) {
return false;
}
return
acg_stream_write_uint(s, ACG_CHUNK_PAD, ACG_UNITBITS_CHUNK_ID) &&
acg_stream_write_pad(s);
}
static inline bool acg_stream_write_chunk_blob(struct acg_stream* s, uint64_t size_bytes, const uint8_t* buf) {
assert(NULL != s);
if (!acg_stream_write_uint(s, ACG_CHUNK_BLOB, ACG_UNITBITS_CHUNK_ID)) {
return false;
}
if (!acg_stream_write_uint(s, size_bytes, 8U)) {
return false;
}
@@ -136,27 +164,142 @@ static inline bool acg_stream_write_chunk_blob(struct acg_stream* s, uint64_t si
*c += size_bits;
return true;
}
static inline bool acg_stream_write_chunk_begin(struct acg_stream* s) {
static inline bool acg_stream_write_chunk_pad(struct acg_stream* s) {
assert(NULL != s);
if (!acg_stream_write_uint(s, ACG_CHUNK_BEGIN, ACG_UNITBITS_CHUNK_ID)) {
if (s->cursor_bits%8U == 0U) {
return true;
}
if (s->size_bytes <= s->cursor_bits/8U) {
return false;
}
return true;
return
acg_stream_write_uint(s, ACG_CHUNK_PAD, ACG_UNITBITS_CHUNK_ID) &&
acg_stream_write_pad(s);
}
static inline bool acg_stream_write_chunk_blob(struct acg_stream* s, uint64_t size_bytes, const uint8_t* buf) {
assert(NULL != s);
return
acg_stream_write_uint(s, ACG_CHUNK_BLOB, ACG_UNITBITS_CHUNK_ID) &&
acg_stream_write_blob(s, size_bytes, buf);
}
static inline bool acg_stream_write_chunk_ctx(struct acg_stream* s, uint64_t time, uint64_t tid) {
assert(NULL != s);
return
acg_stream_write_uint(s, ACG_CHUNK_CTX, ACG_UNITBITS_CHUNK_ID) &&
acg_stream_write_uint(s, time, 32U) &&
acg_stream_write_uint(s, tid, 16U);
}
static inline bool acg_stream_write_chunk_loc(struct acg_stream* s, uint64_t size_bytes, const uint8_t* buf) {
assert(NULL != s);
return
acg_stream_write_uint(s, ACG_CHUNK_LOC, ACG_UNITBITS_CHUNK_ID) &&
acg_stream_write_blob(s, size_bytes, buf);
}
static inline bool acg_stream_write_chunk_begin(struct acg_stream* s) {
assert(NULL != s);
return acg_stream_write_uint(s, ACG_CHUNK_BEGIN, ACG_UNITBITS_CHUNK_ID);
}
static inline bool acg_stream_write_chunk_end(struct acg_stream* s) {
assert(NULL != s);
if (!acg_stream_write_uint(s, ACG_CHUNK_END, ACG_UNITBITS_CHUNK_ID)) {
return false;
}
return true;
return acg_stream_write_uint(s, ACG_CHUNK_END, ACG_UNITBITS_CHUNK_ID);
}
static inline bool acg_stream_write_chunk_check(struct acg_stream* s) {
assert(NULL != s);
return acg_stream_write_uint(s, ACG_CHUNK_CHECK, ACG_UNITBITS_CHUNK_ID);
}
#endif // !defined(ACG_NO_WRITE_FUNCTIONS)
if (!acg_stream_write_uint(s, ACG_CHUNK_CHECK, ACG_UNITBITS_CHUNK_ID)) {
#if !defined(ACG_NO_READ_FUNCTIONS)
static inline bool acg_stream_read_pad(struct acg_stream* s) {
assert(NULL != s);
s->cursor_bits = (s->cursor_bits + 7U)/8U*8U;
return true;
}
static inline bool acg_stream_read_bool(struct acg_stream* s, bool* v) {
assert(NULL != s);
assert(NULL != v);
if (s->size_bytes*8U <= s->cursor_bits) {
return false;
}
uint64_t* const c = &s->cursor_bits;
*v = (s->buffer[*c/8U] >> (*c%8U)) & 1U;
++*c;
return true;
}
static inline bool acg_stream_read_uint(struct acg_stream* s, uint64_t* v, uint8_t unit_bits) {
assert(NULL != s);
assert(NULL != v);
assert(2U <= unit_bits);
assert(65U >= unit_bits);
*v = 0U;
const uint8_t unit_data_bits = unit_bits - 1U;
const uint8_t* const buf = s->buffer;
uint64_t* const c = &s->cursor_bits;
uint8_t v_offset_bits = 0U;
bool cont_flag = true;
while (cont_flag) {
if (s->size_bytes*8U < *c+unit_data_bits) {
return false;
}
const uint8_t frag_bits = acg_util_umin((8U - *c%8U)%8U, unit_data_bits);
if (frag_bits > 0U) {
const uint64_t v_part =
(buf[*c/8U] >> (*c%8U)) & acg_util_fullbits(frag_bits);
*v |= v_part << v_offset_bits;
*c += frag_bits;
v_offset_bits += frag_bits;
}
const uint8_t remain_bits = unit_data_bits - frag_bits;
if (remain_bits > 0U) {
assert(*c%8U == 0U);
uint64_t v_part = 0U;
memcpy(&v_part, &buf[*c/8U], (remain_bits+7U)/8U);
v_part &= acg_util_fullbits(remain_bits);
*v |= v_part << v_offset_bits;
*c += remain_bits;
v_offset_bits += remain_bits;
}
if (!acg_stream_read_bool(s, &cont_flag)) {
return false;
}
}
return true;
}
static inline bool acg_stream_read_blob(struct acg_stream* s, uint64_t* size, uint8_t** buf) {
assert(NULL != s);
assert(NULL != size);
assert(NULL != buf);
if (!acg_stream_read_uint(s, size, 8U)) {
return false;
}
if (!acg_stream_read_pad(s)) {
return false;
}
uint64_t* const c = &s->cursor_bits;
const uint64_t offset = *c/8U;
if (s->size_bytes < offset + *size) {
return false;
}
*buf = &s->buffer[offset];
*c += *size *8U;
return true;
}
#endif // !defined(ACG_NO_READ_FUNCTIONS)

80
example.c Normal file
View File

@@ -0,0 +1,80 @@
// No copyright
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
// ----
#define ACG_NO_READ_FUNCTIONS
#include "allcing.h"
static inline bool acg_stream_flush(struct acg_stream* s) {
assert(NULL != s);
if (s->cursor_bits < 8U) {
return false;
}
FILE** fp = (void*) &s->udata;
if (NULL == *fp) {
*fp = fopen("log.acg", "wb");
if (NULL == *fp) {
abort();
}
}
if (1U != fwrite(s->buffer, s->cursor_bits/8U, 1U, *fp)) {
return false;
}
uint8_t frag = 0U;
if (s->cursor_bits%8U > 0U) {
frag = s->buffer[s->cursor_bits/8U];
}
memset(s->buffer, 0U, s->size_bytes);
s->buffer[0] = frag;
s->cursor_bits %= 8U;
if (0U != fflush(*fp)) {
return false;
}
return true;
}
#undef ACG_FLUSH
#define ACG_FLUSH(s) acg_stream_flush((s))
struct acg_stream logstream = {
.buffer = (uint8_t[1024U]) {0U},
.size_bytes = 1024U,
};
// ----
int collatz(int n) {
ACG_BEGIN(&logstream);
ACG_BLOB(&logstream, sizeof(n), &n);
if (n == 1U) {
ACG_CHECK(&logstream);
ACG_BLOB_LITERAL(&logstream, "it's finished!");
} else if (n%2U) {
ACG_CHECK(&logstream);
n = collatz(3U*n + 1U);
} else {
ACG_CHECK(&logstream);
n = collatz(n / 2U);
}
ACG_END(&logstream);
ACG_BLOB(&logstream, sizeof(n), &n);
return n;
}
int main(int, char**) {
ACG_BEGIN(&logstream);
collatz(100);
ACG_END(&logstream);
}

103
test.c
View File

@@ -11,9 +11,9 @@
int main(int, char**) {
# define STREAM(size, offset) \
# define STREAM(size, offset, ...) \
(struct acg_stream) { \
.buffer = (uint8_t[size]) {0U}, \
.buffer = (uint8_t[size]) {__VA_ARGS__ __VA_OPT__(,) 0U,}, \
.size_bytes = (size), \
.cursor_bits = (offset), \
}
@@ -45,37 +45,30 @@ int main(int, char**) {
/* acg_stream_write_uint / ends in a single byte */ {
struct acg_stream s = STREAM(1024U, 0U);
assert(acg_stream_write_uint(&s, 7U, 4U));
assert(s.buffer[0U] == 0x0EU);
assert(s.buffer[0U] == 0x07U);
assert(s.cursor_bits == 4U);
}
/* acg_stream_write_uint / ends in a single byte with offset */ {
struct acg_stream s = STREAM(1024U, 1U);
assert(acg_stream_write_uint(&s, 7U, 4U));
assert(s.buffer[0U] == 0x1CU);
assert(s.buffer[0U] == 0x0EU);
assert(s.cursor_bits == 5U);
}
/* acg_stream_write_uint / separated data bits */ {
struct acg_stream s = STREAM(1024U, 6U);
assert(acg_stream_write_uint(&s, 7U, 4U));
assert(s.buffer[0U] == 0x80U);
assert(s.buffer[1U] == 0x03U);
assert(s.buffer[0U] == 0xC0U);
assert(s.buffer[1U] == 0x01U);
assert(s.cursor_bits == 10U);
}
/* acg_stream_write_uint / separated cont flag */ {
struct acg_stream s = STREAM(1024U, 7U);
assert(acg_stream_write_uint(&s, 7U, 4U));
assert(s.buffer[0U] == 0x00U);
assert(s.buffer[1U] == 0x07U);
assert(s.buffer[0U] == 0x80U);
assert(s.buffer[1U] == 0x03U);
assert(s.cursor_bits == 11U);
}
/* acg_stream_write_int */ {
struct acg_stream s = STREAM(1024U, 0U);
assert(acg_stream_write_int(&s, -1, 4U));
assert(s.buffer[0U] == 0x05U);
assert(s.cursor_bits == 5U);
}
/* acg_stream_write_chunk_pad / without offset */ {
struct acg_stream s = STREAM(1024U, 0U);
assert(acg_stream_write_chunk_pad(&s));
@@ -88,8 +81,20 @@ int main(int, char**) {
assert(s.cursor_bits == 8U);
}
/* acg_stream_write_chunk_pad / without offset */ {
/* acg_stream_write_chunk_blob / without offset */ {
struct acg_stream s = STREAM(1024U, 0U);
assert(acg_stream_write_chunk_blob(
&s, 4U, (uint8_t[]) {0x00, 0x01, 0x02, 0x03}));
assert(s.buffer[0U] == 0x41);
assert(s.buffer[1U] == 0x00);
assert(s.buffer[2U] == 0x00);
assert(s.buffer[3U] == 0x01);
assert(s.buffer[4U] == 0x02);
assert(s.buffer[5U] == 0x03);
assert(s.cursor_bits == 48U);
}
/* acg_stream_write_chunk_blob / with offset=1 */ {
struct acg_stream s = STREAM(1024U, 1U);
assert(acg_stream_write_chunk_blob(
&s, 4U, (uint8_t[]) {0x00, 0x01, 0x02, 0x03}));
assert(s.buffer[0U] == 0x82);
@@ -100,29 +105,65 @@ int main(int, char**) {
assert(s.buffer[5U] == 0x03);
assert(s.cursor_bits == 48U);
}
/* acg_stream_write_chunk_pad / with offset=1 */ {
struct acg_stream s = STREAM(1024U, 1U);
assert(acg_stream_write_chunk_blob(
&s, 4U, (uint8_t[]) {0x00, 0x01, 0x02, 0x03}));
assert(s.buffer[0U] == 0x04);
assert(s.buffer[1U] == 0x01);
assert(s.buffer[2U] == 0x00);
assert(s.buffer[3U] == 0x01);
assert(s.buffer[4U] == 0x02);
assert(s.buffer[5U] == 0x03);
assert(s.cursor_bits == 48U);
}
/* acg_stream_write_chunk_pad / with offset=4 */ {
/* acg_stream_write_chunk_blob / with offset=4 */ {
struct acg_stream s = STREAM(1024U, 4U);
assert(acg_stream_write_chunk_blob(
&s, 4U, (uint8_t[]) {0x00, 0x01, 0x02, 0x03}));
assert(s.buffer[0U] == 0x20);
assert(s.buffer[1U] == 0x08);
assert(s.buffer[0U] == 0x10);
assert(s.buffer[1U] == 0x04);
assert(s.buffer[2U] == 0x00);
assert(s.buffer[3U] == 0x01);
assert(s.buffer[4U] == 0x02);
assert(s.buffer[5U] == 0x03);
assert(s.cursor_bits == 48U);
}
/* acg_stream_read_pad / without offset */ {
struct acg_stream s = STREAM(1024U, 0U);
assert(acg_stream_read_pad(&s));
assert(s.cursor_bits == 0U);
}
/* acg_stream_read_pad / with offset=1 */ {
struct acg_stream s = STREAM(1024U, 1U);
assert(acg_stream_read_pad(&s));
assert(s.cursor_bits == 8U);
}
/* acg_stream_read_bool / with value=false */ {
struct acg_stream s = STREAM(1024U, 0U);
bool v;
assert(acg_stream_read_bool(&s, &v));
assert(v == false);
assert(s.cursor_bits == 1U);
}
/* acg_stream_read_bool / with value=true */ {
struct acg_stream s = STREAM(1024U, 0U, 0x01U);
bool v;
assert(acg_stream_read_bool(&s, &v));
assert(v == true);
assert(s.cursor_bits == 1U);
}
/* acg_stream_read_uint / with a single unit */ {
struct acg_stream s = STREAM(1024U, 0U, 0x04U);
uint64_t v;
assert(acg_stream_read_uint(&s, &v, 4U));
assert(v == 4U);
assert(s.cursor_bits == 4U);
}
/* acg_stream_read_uint / with multi units */ {
struct acg_stream s = STREAM(1024U, 0U, 0x48U);
uint64_t v;
assert(acg_stream_read_uint(&s, &v, 4U));
assert(v == 32U);
assert(s.cursor_bits == 8U);
}
/* acg_stream_read_uint / with multi bytes */ {
struct acg_stream s = STREAM(1024U, 0U, 0x88U, 0x04U);
uint64_t v;
assert(acg_stream_read_uint(&s, &v, 4U));
assert(v == 256U);
assert(s.cursor_bits == 12U);
}
return EXIT_SUCCESS;
}

4
tool/CMakeLists.txt Normal file
View File

@@ -0,0 +1,4 @@
# ---- allcing_unpack: converts binary format to human readable text
add_executable(allcing_unpack)
target_sources(allcing_unpack PRIVATE unpack.c)
target_link_libraries(allcing_unpack PRIVATE allcing allcing_config)

141
tool/unpack.c Normal file
View File

@@ -0,0 +1,141 @@
// No copyright
#include <inttypes.h>
#include <stdio.h>
#include <stdlib.h>
#include <allcing.h>
#define READ_CHUNK_SIZE 1024U
static inline bool acg_stream_refill(struct acg_stream* s) {
assert(NULL != s);
FILE* fp = s->udata;
assert(NULL != fp);
uint8_t* const buf = s->buffer;
uint64_t* const c = &s->cursor_bits;
uint64_t* const size = &s->size_bytes;
const uint64_t remain = *size - *c/8U;
memmove(buf, &buf[*c/8U], remain);
*c %= 8U;
const uint64_t expect_size = READ_CHUNK_SIZE - remain;
const uint64_t actual_size = fread(&buf[remain], 1U, expect_size, fp);
*size = remain + actual_size;
return true;
}
int main(int argc, char** argv) {
if (argc != 2U) {
fprintf(stderr, "invalid args\n");
return EXIT_FAILURE;
}
FILE* fp = fopen(argv[1], "rb");
if (NULL == fp) {
fprintf(stderr, "failed to open: %s\n", argv[1]);
return EXIT_FAILURE;
}
struct acg_stream s = {
.buffer = (uint8_t[READ_CHUNK_SIZE]) {0U},
.udata = fp,
};
uint64_t last_event_chunk_id = 0U;
uint32_t indent = 0U;
# define INDENT_PRINT(FMT, ...) \
printf("%*s" FMT "\n", indent, "" __VA_OPT__(,) __VA_ARGS__)
while (true) {
if (!acg_stream_refill(&s)) {
fprintf(stderr, "failed to read: %s\n", argv[1]);
return EXIT_FAILURE;
}
if (0U == s.size_bytes) {
break;
}
uint64_t chunk_id;
if (!acg_stream_read_uint(&s, &chunk_id, ACG_UNITBITS_CHUNK_ID)) {
fprintf(stderr, "failed to read chunk id\n");
return EXIT_FAILURE;
}
const bool is_event_chunk = chunk_id >= ACG_CHUNK_BEGIN;
if (is_event_chunk) {
if (ACG_CHUNK_BEGIN == last_event_chunk_id) {
++indent;
} else if (ACG_CHUNK_END == last_event_chunk_id) {
--indent;
}
last_event_chunk_id = chunk_id;
}
switch (chunk_id) {
case ACG_CHUNK_PAD:
acg_stream_read_pad(&s);
break;
case ACG_CHUNK_BLOB: {
uint64_t size;
uint8_t* buf;
if (!acg_stream_read_blob(&s, &size, &buf)) {
fprintf(stderr, "failed to read BLOB in BLOB chunk\n");
return EXIT_FAILURE;
}
if (size <= 8U) {
uint64_t v = 0U;
memcpy(&v, buf, size);
INDENT_PRINT("-BLOB: 0x%" PRIu64 " (size:%" PRIu64 ")", v, size);
} else {
INDENT_PRINT("-BLOB: %.*s", (int) size, (const char*) buf);
}
} break;
case ACG_CHUNK_CTX: {
uint64_t time;
if (!acg_stream_read_uint(&s, &time, 32U)) {
fprintf(stderr, "failed to read TIME in CTX chunk\n");
return EXIT_FAILURE;
}
uint64_t tid;
if (!acg_stream_read_uint(&s, &tid, 16U)) {
fprintf(stderr, "failed to read TID in CTX chunk\n");
return EXIT_FAILURE;
}
INDENT_PRINT("-CTX: TIME=%" PRIu64 " / TID=%" PRIx64, time, tid);
} break;
case ACG_CHUNK_LOC: {
uint64_t size;
uint8_t* buf;
if (!acg_stream_read_blob(&s, &size, &buf)) {
fprintf(stderr, "failed to read BLOB in BLOB chunk\n");
return EXIT_FAILURE;
}
INDENT_PRINT("-LOC: %.*s", (int) size, (const char*) buf);
} break;
case ACG_CHUNK_BEGIN:
INDENT_PRINT("BEGIN:");
break;
case ACG_CHUNK_END:
if (0U == indent) {
fprintf(stderr, "too many END chunk\n");
return EXIT_FAILURE;
}
INDENT_PRINT("END:");
break;
case ACG_CHUNK_CHECK:
INDENT_PRINT("CHECK:");
break;
default:
fprintf(stderr, "unknown chunk id: %" PRIu64 "\n", chunk_id);
return EXIT_FAILURE;
}
}
fclose(fp);
return EXIT_SUCCESS;
}