207 lines
5.9 KiB
C
207 lines
5.9 KiB
C
#include "wire.h"
|
|
|
|
#include <string.h>
|
|
|
|
uint32_t wire_hash(const unsigned char *data, size_t length)
|
|
{
|
|
uint32_t value = UINT32_C(2166136261);
|
|
size_t index;
|
|
for (index = 0u; index < length; index++) {
|
|
value ^= data[index];
|
|
value *= UINT32_C(16777619);
|
|
}
|
|
return value;
|
|
}
|
|
|
|
uint32_t wire_fold(uint32_t value, const wire_event *event)
|
|
{
|
|
value = value * 33u + event->tag;
|
|
value = value * 33u + event->length;
|
|
value = value * 33u + event->source;
|
|
value = value * 33u + (uint32_t)event->tick;
|
|
value = value * 33u + (uint32_t)(event->tick >> 32);
|
|
value = value * 33u + event->value;
|
|
value = value * 33u + event->auxiliary;
|
|
return value;
|
|
}
|
|
|
|
void wire_decoder_init(wire_decoder *decoder, unsigned pad)
|
|
{
|
|
memset(decoder, 0, sizeof(*decoder));
|
|
if (pad > 7u) {
|
|
decoder->error = WIRE_ARGUMENT;
|
|
return;
|
|
}
|
|
decoder->pad = pad;
|
|
}
|
|
|
|
int wire_decoder_feed(wire_decoder *decoder, const unsigned char *data, size_t length)
|
|
{
|
|
if (decoder->error != WIRE_OK) {
|
|
return decoder->error;
|
|
}
|
|
if (decoder->finished) {
|
|
decoder->error = WIRE_ARGUMENT;
|
|
return decoder->error;
|
|
}
|
|
if (length > WIRE_LIMIT - decoder->size) {
|
|
decoder->error = WIRE_CAPACITY;
|
|
return decoder->error;
|
|
}
|
|
memcpy(decoder->storage + decoder->pad + decoder->size, data, length);
|
|
decoder->size += length;
|
|
decoder->chunks++;
|
|
return WIRE_OK;
|
|
}
|
|
|
|
int wire_decode_event(wire_cursor *payload, wire_event *event)
|
|
{
|
|
wire_cursor body;
|
|
unsigned tag;
|
|
unsigned length;
|
|
memset(event, 0, sizeof(*event));
|
|
if (wire_u8(payload, &tag) || wire_u8(payload, &length)) {
|
|
return WIRE_RECORD_ERROR;
|
|
}
|
|
if (wire_slice(payload, length, &body)) {
|
|
return WIRE_RECORD_ERROR;
|
|
}
|
|
event->tag = tag;
|
|
event->length = length;
|
|
switch (tag) {
|
|
case WIRE_TAG_SAMPLE:
|
|
if (length != 10u) {
|
|
return WIRE_RECORD_ERROR;
|
|
}
|
|
if (wire_u16(&body, &event->source) ||
|
|
wire_u32(&body, &event->value) ||
|
|
wire_u32(&body, &event->auxiliary)) {
|
|
return WIRE_RECORD_ERROR;
|
|
}
|
|
break;
|
|
case WIRE_TAG_LABEL:
|
|
if (length > 32u) {
|
|
return WIRE_RECORD_ERROR;
|
|
}
|
|
event->value = wire_hash(body.data, body.length);
|
|
event->auxiliary = length;
|
|
wire_skip(&body, length);
|
|
break;
|
|
case WIRE_TAG_CLOCK:
|
|
if (length != 16u) {
|
|
return WIRE_RECORD_ERROR;
|
|
}
|
|
if (wire_u32(&body, &event->source) ||
|
|
wire_u64(&body, &event->tick) ||
|
|
wire_u32(&body, &event->value)) {
|
|
return WIRE_RECORD_ERROR;
|
|
}
|
|
break;
|
|
default:
|
|
event->value = wire_hash(body.data, body.length);
|
|
event->auxiliary = length;
|
|
wire_skip(&body, length);
|
|
break;
|
|
}
|
|
return wire_at_end(&body) ? WIRE_OK : WIRE_RECORD_ERROR;
|
|
}
|
|
|
|
static uint32_t frame_checksum(const unsigned char *data)
|
|
{
|
|
const uint32_t *trailer = (const uint32_t *)(const void *)data;
|
|
uint32_t stored = *trailer;
|
|
return stored;
|
|
}
|
|
|
|
int wire_decoder_finish(wire_decoder *decoder)
|
|
{
|
|
wire_cursor header;
|
|
wire_cursor payload;
|
|
const unsigned char *data = decoder->storage + decoder->pad;
|
|
unsigned magic_a;
|
|
unsigned magic_b;
|
|
unsigned version;
|
|
unsigned count;
|
|
uint32_t length;
|
|
unsigned index;
|
|
if (decoder->finished) {
|
|
return decoder->error;
|
|
}
|
|
decoder->finished = 1;
|
|
if (decoder->error != WIRE_OK) {
|
|
return decoder->error;
|
|
}
|
|
if (decoder->size < WIRE_HEADER + WIRE_TRAILER) {
|
|
decoder->error = WIRE_LENGTH_ERROR;
|
|
return decoder->error;
|
|
}
|
|
wire_cursor_init(&header, data, WIRE_HEADER);
|
|
wire_u8(&header, &magic_a);
|
|
wire_u8(&header, &magic_b);
|
|
wire_u8(&header, &version);
|
|
wire_u8(&header, &count);
|
|
wire_u16(&header, &length);
|
|
if (magic_a != WIRE_MAGIC_A || magic_b != WIRE_MAGIC_B ||
|
|
version != WIRE_VERSION || count > WIRE_EVENTS) {
|
|
decoder->error = WIRE_HEADER_ERROR;
|
|
return decoder->error;
|
|
}
|
|
decoder->declared = count;
|
|
if (WIRE_HEADER + length + WIRE_TRAILER != decoder->size) {
|
|
decoder->error = WIRE_LENGTH_ERROR;
|
|
return decoder->error;
|
|
}
|
|
decoder->checksum = frame_checksum(data + WIRE_HEADER + length);
|
|
if (decoder->checksum != wire_hash(data, WIRE_HEADER + length)) {
|
|
decoder->error = WIRE_CHECKSUM_ERROR;
|
|
return decoder->error;
|
|
}
|
|
wire_cursor_init(&payload, data + WIRE_HEADER, length);
|
|
for (index = 0u; index < count; index++) {
|
|
wire_event event;
|
|
int error = wire_decode_event(&payload, &event);
|
|
if (error != WIRE_OK) {
|
|
decoder->error = error;
|
|
return error;
|
|
}
|
|
decoder->events[decoder->count++] = event;
|
|
decoder->fingerprint = wire_fold(decoder->fingerprint, &event);
|
|
}
|
|
if (!wire_at_end(&payload)) {
|
|
decoder->error = WIRE_RECORD_ERROR;
|
|
}
|
|
return decoder->error;
|
|
}
|
|
|
|
int wire_decoder_feedv(wire_decoder *decoder, const wire_fragment *parts, size_t count)
|
|
{
|
|
size_t index;
|
|
size_t available;
|
|
if (decoder->error != WIRE_OK) {
|
|
return decoder->error;
|
|
}
|
|
if (decoder->finished) {
|
|
decoder->error = WIRE_ARGUMENT;
|
|
return decoder->error;
|
|
}
|
|
available = WIRE_LIMIT - decoder->size;
|
|
for (index = 0u; index < count; index++) {
|
|
if (parts[index].length > available) {
|
|
decoder->error = WIRE_CAPACITY;
|
|
return decoder->error;
|
|
}
|
|
if (parts[index].length && !parts[index].data) {
|
|
decoder->error = WIRE_ARGUMENT;
|
|
return decoder->error;
|
|
}
|
|
available -= parts[index].length;
|
|
}
|
|
for (index = 0u; index < count; index++) {
|
|
int error = wire_decoder_feed(decoder, parts[index].data, parts[index].length);
|
|
if (error != WIRE_OK) {
|
|
return error;
|
|
}
|
|
}
|
|
return WIRE_OK;
|
|
}
|