Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 10 additions & 16 deletions GKeyComp.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
ignoring the result of a call to RingBuffer_copy.
CJB: 02-Aug-26: Avoid expression *(out_buffer++) that caused Clang's
_Optional analyzer to emit a spurious diagnostic message.
CJB: 21-Sep-26: Declare variables when they are first assigned.
*/

/* ISO library header files */
Expand Down Expand Up @@ -126,9 +127,7 @@ static bool write_bits(GKeyComp *comp, GKeyParameters *params,
{
bool success = true;
_Optional char *out_buffer;
size_t out_size, out_total;
unsigned int acc_nbits;
unsigned long acc;
size_t out_size;

assert(comp != NULL);
assert(params != NULL);
Expand All @@ -144,10 +143,10 @@ static bool write_bits(GKeyComp *comp, GKeyParameters *params,

out_buffer = params->out_buffer;
out_size = params->out_size;
out_total = comp->out_total;
size_t out_total = comp->out_total;

acc = comp->acc;
acc_nbits = comp->acc_nbits;
unsigned long acc = comp->acc;
unsigned int acc_nbits = comp->acc_nbits;
DEBUG_VERBOSEF("GKeyComp: Accumulator is 0x%lx (%u bits)\n", acc, acc_nbits);

/* Special case to allow remaining bits to be flushed out */
Expand All @@ -162,7 +161,6 @@ static bool write_bits(GKeyComp *comp, GKeyParameters *params,
significant 8 bits to the output buffer. */
while (acc_nbits >= CHAR_BIT)
{
unsigned long old_acc;

if (out_buffer != NULL && out_size == 0)
{
Expand All @@ -175,7 +173,7 @@ static bool write_bits(GKeyComp *comp, GKeyParameters *params,

/* Shift down upper bits of accumulator to take the place
of those about to be output */
old_acc = acc;
unsigned long old_acc = acc;
acc >>= CHAR_BIT;
acc_nbits -= CHAR_BIT;
DEBUG_VERBOSEF("GKeyComp: Accumulator is 0x%lx (%u bits)\n", acc,
Expand Down Expand Up @@ -223,16 +221,14 @@ static size_t ring_writer(void *arg, const void *src, size_t n)
assert(arg);
RingWriterParams *rwp = arg;
size_t nout;
GKeyComp *comp;
GKeyParameters *params;
const unsigned char *literals = (const unsigned char *)src;

assert(rwp != NULL);
assert(src != NULL || n == 0);
DEBUG_VERBOSEF("GKeyComp: copying %zu bytes from %p\n", n, src);

comp = rwp->comp;
params = rwp->params;
GKeyComp *comp = rwp->comp;
GKeyParameters *params = rwp->params;

/* Write as many literal byte values to the output buffer as will fit. */
for (nout = 0; nout < n; ++nout)
Expand Down Expand Up @@ -333,10 +329,9 @@ static bool find_sequence(GKeyComp *comp, GKeyParameters *params)
{
/* Allow the most recently compressed byte to be copied provided that
sufficient bits are allocated for the sequence size */
size_t bits_limit;
unsigned int nbits =
GKey_get_read_size_bits(comp->history_log_2, read_offset);
bits_limit = ((size_t)1 << nbits) - 1;
size_t bits_limit = ((size_t)1 << nbits) - 1;
if (max_read_size > bits_limit)
{
/* If the current sequence can't grow longer than the longest
Expand Down Expand Up @@ -506,7 +501,6 @@ void gkeycomp_reset(GKeyComp *comp)
GKeyStatus gkeycomp_compress(GKeyComp *comp, GKeyParameters *params)
{
GKeyStatus status = GKeyStatus_OK;
GKeyCompState state;
bool flush, input = true;
const unsigned char *in_buffer;
RingWriterParams rwp;
Expand All @@ -517,7 +511,7 @@ GKeyStatus gkeycomp_compress(GKeyComp *comp, GKeyParameters *params)
assert(comp != NULL);
assert(params != NULL);

state = comp->state;
GKeyCompState state = comp->state;

/* Treat no input as a special case that force-completes the current
sequence then flushes any bits lingering in the accumulator. */
Expand Down
23 changes: 9 additions & 14 deletions GKeyDecomp.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
Shift (size_t)1 instead of 1ul to avoid an MSVC warning.
CJB: 02-Aug-26: Add missing _Optional qualifier to the declaration of
out_buffer in ring_writer.
CJB: 21-Sep-26: Declare variables when they are first assigned.
*/

/* ISO library header files */
Expand Down Expand Up @@ -104,14 +105,13 @@ typedef struct

static size_t ring_writer(void *arg, const void *src, size_t n)
{
GKeyParameters *params;
assert(arg);
RingWriterParams *rwp = arg;

assert(rwp != NULL);
assert(src != NULL || n == 0);

params = rwp->params;
GKeyParameters *params = rwp->params;
_Optional char *const out_buffer = params->out_buffer;

if (out_buffer == NULL)
Expand Down Expand Up @@ -149,9 +149,7 @@ static bool read_bits(GKeyDecomp *decomp, GKeyParameters *params,
{
bool success = true;
const unsigned char *in_buffer;
size_t in_size, in_total;
unsigned int acc_nbits;
unsigned long acc;
size_t in_size;

assert(decomp != NULL);
assert(params != NULL);
Expand All @@ -163,15 +161,14 @@ static bool read_bits(GKeyDecomp *decomp, GKeyParameters *params,
params->in_buffer, params->in_size);
in_buffer = params->in_buffer;
in_size = params->in_size;
acc = decomp->acc;
acc_nbits = decomp->acc_nbits;
in_total = decomp->in_total;
unsigned long acc = decomp->acc;
unsigned int acc_nbits = decomp->acc_nbits;
size_t in_total = decomp->in_total;

/* While we don't have enough bits in the accumulator and there is more input
available... */
while (acc_nbits < nbits)
{
unsigned long byte;

if (in_size == 0)
{
Expand All @@ -181,7 +178,7 @@ static bool read_bits(GKeyDecomp *decomp, GKeyParameters *params,
}

/* Consume a byte of input */
byte = *(in_buffer++);
unsigned long byte = *(in_buffer++);
DEBUG_VERBOSEF("GKeyDecomp: Read byte %zu (0x%02lx) from input buffer\n",
in_total, byte);
++in_total;
Expand Down Expand Up @@ -280,8 +277,6 @@ void gkeydecomp_reset(GKeyDecomp *decomp)
GKeyStatus gkeydecomp_decompress(GKeyDecomp *decomp, GKeyParameters *params)
{
GKeyStatus status = GKeyStatus_OK;
GKeyDecompState state;
_Optional GKeyProgressFn *prog_cb;
unsigned long bits;
unsigned int nbits;
bool stop = false;
Expand All @@ -291,8 +286,8 @@ GKeyStatus gkeydecomp_decompress(GKeyDecomp *decomp, GKeyParameters *params)
assert(decomp != NULL);
assert(params != NULL);

state = decomp->state;
prog_cb = params->prog_cb;
GKeyDecompState state = decomp->state;
_Optional GKeyProgressFn *prog_cb = params->prog_cb;

do
{
Expand Down
12 changes: 7 additions & 5 deletions RingBuffer.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
CJB: 08-Apr-25: Dogfooding the _Optional qualifier.
CJB: 10-May-25: Forbid a null context argument to the ring buffer callback.
CJB: 19-May-26: Don't bother storing size_log_2 because it was unused.
CJB: 21-Sep-26: Declare variables when they are first assigned.
*/

/* ISO library header files */
Expand Down Expand Up @@ -81,13 +82,13 @@ void RingBuffer_reset(RingBuffer *ring)

void RingBuffer_write(RingBuffer *ring, const void *s, size_t n)
{
size_t to_copy, nleft, write_pos, ring_size;
size_t to_copy, nleft;

assert(ring != NULL);
assert(s != NULL || n == 0);

write_pos = ring->write_pos;
ring_size = ring->size;
size_t write_pos = ring->write_pos;
size_t ring_size = ring->size;

/* While we have copied more bytes to the output buffer than we have copied
into the ring buffer... */
Expand Down Expand Up @@ -126,7 +127,7 @@ void RingBuffer_write(RingBuffer *ring, const void *s, size_t n)
size_t RingBuffer_copy(RingBuffer *ring, _Optional RingBufferWriteFn *write_cb,
void *cb_arg, size_t offset, size_t n)
{
size_t copied, to_copy, total;
size_t total;

assert(ring != NULL);
assert(offset + n <= ring->size);
Expand All @@ -138,7 +139,8 @@ size_t RingBuffer_copy(RingBuffer *ring, _Optional RingBufferWriteFn *write_cb,
"RingBuffer: Copying up to %zu bytes from %zu to %zu in ring buffer\n", n,
ring->write_pos + offset, ring->write_pos);

copied = to_copy = 0;
size_t to_copy = 0;
size_t copied = 0;

for (total = 0; total < n && copied >= to_copy; total += copied)
{
Expand Down
22 changes: 10 additions & 12 deletions RingSearch.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
to cast the matching parameters.
CJB: 13-Apr-25: Fix warnings when a format specifies type 'void *' but the
argument has type 'char *'.
CJB: 21-Sep-26: Declare variables when they are first assigned.
*/

/* ISO library header files */
Expand All @@ -41,13 +42,12 @@

int RingBuffer_read_char(const RingBuffer *ring, size_t offset)
{
int c;

assert(ring != NULL);
assert(offset < ring->size);

offset = (ring->write_pos + offset) & (ring->size - 1);
c = ring->buffer[offset];
int c = ring->buffer[offset];

DEBUG_VERBOSEF("RingBuffer: read 0x%02x from position %zu\n", c, offset);
return c;
Expand All @@ -56,9 +56,8 @@ int RingBuffer_read_char(const RingBuffer *ring, size_t offset)
size_t RingBuffer_find_char(const RingBuffer *ring, size_t offset, size_t n,
int c)
{
size_t to_search, abs_read, found;
size_t to_search, found;
_Optional const unsigned char *match;
const unsigned char *start;
bool search;

assert(ring != NULL);
Expand All @@ -69,9 +68,9 @@ size_t RingBuffer_find_char(const RingBuffer *ring, size_t offset, size_t n,
n, c, offset, ring->write_pos + offset);

/* Calculate absolute read position within the buffer */
abs_read = (ring->write_pos + offset) & (ring->size - 1);
size_t abs_read = (ring->write_pos + offset) & (ring->size - 1);

start = ring->buffer + abs_read;
const unsigned char *start = ring->buffer + abs_read;
if (ring->write_pos > abs_read)
{
/* Check characters between start position and write position */
Expand Down Expand Up @@ -151,8 +150,7 @@ size_t RingBuffer_find_char(const RingBuffer *ring, size_t offset, size_t n,
int RingBuffer_compare(const RingBuffer *ring, size_t offset1, size_t offset2,
size_t n)
{
size_t len1, len2, abs_read1, abs_read2, to_compare, nleft;
const unsigned char *start1, *start2;
size_t len1, len2, to_compare, nleft;
int diff = 0;

assert(ring != NULL);
Expand All @@ -165,11 +163,11 @@ int RingBuffer_compare(const RingBuffer *ring, size_t offset1, size_t offset2,
ring->write_pos + offset2);

/* Calculate absolute read positions within the buffer */
abs_read1 = (ring->write_pos + offset1) & (ring->size - 1);
abs_read2 = (ring->write_pos + offset2) & (ring->size - 1);
size_t abs_read1 = (ring->write_pos + offset1) & (ring->size - 1);
size_t abs_read2 = (ring->write_pos + offset2) & (ring->size - 1);

start1 = ring->buffer + abs_read1;
start2 = ring->buffer + abs_read2;
const unsigned char *start1 = ring->buffer + abs_read1;
const unsigned char *start2 = ring->buffer + abs_read2;

if (n == 1)
{
Expand Down
Loading