#ifndef SUIT_USAGE_H
#define SUIT_USAGE_H
#include "conf.h"
#include <stddef.h>
#include <stdint.h>
// The record reader (docs/usage.md): walks the JSON Lines a harness keeps of
// its sessions and reports, for each `use` rule, what the rule finds. It knows
// nothing of any harness; a rule says where to look. It uses no memory that
// grows with a line: a file is read in chunks of USAGE_CHUNK bytes, a string is
// kept only up to USAGE_STR_KEEP bytes, nesting is cut off at USAGE_DEPTH_MAX
// and every other buffer has a fixed size named here.
constexpr size_t USAGE_CHUNK = 65536;
constexpr size_t USAGE_STR_KEEP = 512; // the first bytes of a string that are kept
constexpr size_t USAGE_DEPTH_MAX = 64; // containers inside one another
constexpr uint32_t USAGE_LINE_HITS_MAX = 32; // matches kept from one line
constexpr uint32_t USAGE_CAND_MAX = 8; // names one rule takes from one matched thing
constexpr size_t USAGE_ID_MAX = 128;
constexpr size_t USAGE_TIME_MAX = 40;
constexpr size_t USAGE_PROJECT_MAX = USAGE_STR_KEEP - 1;
constexpr uint32_t USAGE_VALUES_MAX = CONF_USE_MAX * USAGE_WHERE_MAX;
// Per rule: the path in, USAGE_WHERE_MAX tests, two name paths and the id; and the time and the project.
constexpr uint32_t USAGE_PER_RULE = 1 + USAGE_WHERE_MAX + 3;
constexpr uint32_t USAGE_TRACK_MAX = 2 + CONF_USE_MAX * USAGE_PER_RULE;
// One thing a rule found. TIME, ID and PROJECT are empty when the record has
// none (or it is longer than the constants above).
typedef struct {
UseKind kind;
char name[USAGE_NAME_MAX + 1];
char id[USAGE_ID_MAX + 1];
char time[USAGE_TIME_MAX + 1];
char project[USAGE_PROJECT_MAX + 1];
} UsageEvent;
typedef void (*UsageSink)(void *state, const UsageEvent *ev);
// Where lines come from: a file read in chunks, or a block of memory. After a
// failed read ERR holds the errno.
typedef struct {
int fd; // -1: memory
const uint8_t *mem;
uint64_t memlen;
uint64_t base; // offset of buf[0]
size_t at;
size_t have;
int err;
uint8_t buf[USAGE_CHUNK];
} UsageSource;
void usage_source_fd(UsageSource *s, int fd);
void usage_source_mem(UsageSource *s, const void *data, size_t len);
typedef struct {
const UsagePath *path;
uint8_t n;
} UsageTrack;
typedef struct {
uint8_t st[USAGE_TRACK_MAX];
uint16_t arrived;
} UsageLevel;
typedef struct {
bool ok; // false: the name was found but is not a name
char name[USAGE_NAME_MAX + 1];
} UsageCand;
typedef struct {
uint32_t where_ok;
uint32_t ncand;
UsageCand cand[USAGE_CAND_MAX];
uint8_t id_state;
char id[USAGE_ID_MAX + 1];
} UsageCap;
typedef struct {
UseKind kind;
char name[USAGE_NAME_MAX + 1];
char id[USAGE_ID_MAX + 1];
} UsageHit;
// The rule set and the work areas of one reader. Large: keep it static.
typedef struct {
const UsageRule *rules;
uint32_t nrules;
UsageTrack track[USAGE_TRACK_MAX];
uint32_t ntrack;
const char *vals[USAGE_VALUES_MAX]; // every where VALUE once, for the pre-test
uint8_t vlen[USAGE_VALUES_MAX];
uint32_t nv;
size_t vmax;
uint64_t rmask[CONF_USE_MAX]; // the values each rule needs, one bit each
bool always; // a rule has no where: every line gets walked
UsageSource *src;
uint64_t seen;
uint8_t tail[USAGE_VALUE_MAX];
size_t ntail;
bool pass;
UsageLevel lv[USAGE_DEPTH_MAX + 1];
bool is_obj[USAGE_DEPTH_MAX];
UsageCap cap[CONF_USE_MAX];
UsageHit hits[USAGE_LINE_HITS_MAX];
uint32_t nhits;
uint32_t line_skipped;
char str[USAGE_STR_KEEP];
char key[USAGE_KEY_MAX + 1];
char time[USAGE_TIME_MAX + 1];
char project[USAGE_PROJECT_MAX + 1];
uint64_t lines; // lines walked
uint64_t skipped; // matches that are no event, and lines that were not JSON
} Usage;
// Prepare U for N rules (at most CONF_USE_MAX). TIME and PROJECT say where in a
// record those are; either may be null. The rules must outlive U.
void usage_init(Usage *u, const UsageRule *rules, uint32_t n, const UsagePath *time, const UsagePath *project);
// Read every complete line of S from offset START, calling SINK for each thing
// found. *END is the offset of the first byte not read: the start of a last
// line that has no newline yet, or the end of the data. False if a read failed
// (S->err). Lines that are not JSON, and lines too deeply nested, are skipped.
[[nodiscard]] bool usage_read(Usage *u, UsageSource *s, uint64_t start, uint64_t *end, UsageSink sink, void *state);
#endif