#include "journal.h" #include #include typedef struct { uint8_t bytes[JOURNAL_RECORD_MAX]; size_t length; uint64_t position; uint32_t sequence; } append_record; static uint32_t checksum_byte(uint32_t running, uint8_t byte) { running ^= byte; for (unsigned bit = 0; bit != 8; ++bit) { uint32_t mask = 0u - (running & 1u); running = (running >> 1) ^ (mask & UINT32_C(0xedb88320)); } return running; } static uint32_t checksum_span(uint32_t running, const uint8_t *bytes, size_t length) { for (size_t index = 0; index < length; ++index) { running = checksum_byte(running, bytes[index]); } return running; } uint32_t journal_checksum(const uint8_t *prefix, size_t prefix_length, const uint8_t *payload, size_t payload_length) { uint32_t value = UINT32_C(0xffffffff); value = checksum_span(value, prefix, prefix_length); value = checksum_span(value, payload, payload_length); return value ^ UINT32_C(0xffffffff); } static int append_allowed(const journal_state *state, size_t length) { if (!state->opened) { return -ENODEV; } if (length > JOURNAL_PAYLOAD_MAX) { return -EINVAL; } return 0; } static void encode_prefix(uint8_t *bytes, uint32_t sequence, size_t length) { memcpy(bytes, "LOG1", 4); journal_store16(bytes + 4, (uint16_t)length); journal_store16(bytes + 6, 0); journal_store32(bytes + 8, sequence); } static void encode_body(append_record *record, const uint8_t *payload, size_t length) { if (length != 0) { memcpy(record->bytes + JOURNAL_HEADER_SIZE, payload, length); } uint32_t checksum = journal_checksum(record->bytes, JOURNAL_PREFIX_SIZE, payload, length); journal_store32(record->bytes + 12, checksum); journal_store32(record->bytes + JOURNAL_HEADER_SIZE + length, record->sequence ^ UINT32_C(0xffffffff)); } static append_record prepare_append(const journal_state *state, const uint8_t *payload, size_t length) { append_record record; memset(&record, 0, sizeof(record)); record.position = state->end; record.sequence = state->sequence; record.length = JOURNAL_HEADER_SIZE + length + JOURNAL_TRAILER_SIZE; encode_prefix(record.bytes, record.sequence, length); encode_body(&record, payload, length); return record; } static int write_append(const journal_io *io, int descriptor, const append_record *record) { return journal_write_exact(io, descriptor, record->bytes, record->length, record->position); } static void publish_append(journal_state *state, const append_record *record) { journal_state committed = { 1, record->position + record->length, record->sequence + 1u, 0 }; *state = committed; } int journal_append(journal_state *state, const journal_io *io, int descriptor, const uint8_t *payload, size_t length) { int status = append_allowed(state, length); if (status != 0) { return status; } append_record record = prepare_append(state, payload, length); state->dirty = 1; status = journal_resize(io, descriptor, state->end); if (status != 0) { return status; } status = write_append(io, descriptor, &record); if (status != 0) { return status; } status = journal_synchronize(io, descriptor); if (status != 0) { return status; } publish_append(state, &record); return 0; }