2025-02-20 21:31:06 +01:00
|
|
|
/* Lzlib - Compression library for the lzip format
|
2025-02-20 21:35:19 +01:00
|
|
|
Copyright (C) 2009-2025 Antonio Diaz Diaz.
|
2025-02-20 20:13:15 +01:00
|
|
|
|
2025-02-20 21:31:06 +01:00
|
|
|
This library is free software. Redistribution and use in source and
|
|
|
|
binary forms, with or without modification, are permitted provided
|
|
|
|
that the following conditions are met:
|
2025-02-20 20:13:15 +01:00
|
|
|
|
2025-02-20 21:31:06 +01:00
|
|
|
1. Redistributions of source code must retain the above copyright
|
|
|
|
notice, this list of conditions, and the following disclaimer.
|
2025-02-20 20:13:15 +01:00
|
|
|
|
2025-02-20 21:31:06 +01:00
|
|
|
2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
notice, this list of conditions, and the following disclaimer in the
|
|
|
|
documentation and/or other materials provided with the distribution.
|
2025-02-20 20:13:15 +01:00
|
|
|
|
2025-02-20 21:31:06 +01:00
|
|
|
This library is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
2025-02-20 20:13:15 +01:00
|
|
|
*/
|
|
|
|
|
2025-02-20 21:34:58 +01:00
|
|
|
typedef struct Circular_buffer
|
2025-02-20 20:13:15 +01:00
|
|
|
{
|
|
|
|
uint8_t * buffer;
|
2025-02-20 21:21:14 +01:00
|
|
|
unsigned buffer_size; /* capacity == buffer_size - 1 */
|
|
|
|
unsigned get; /* buffer is empty when get == put */
|
|
|
|
unsigned put;
|
2025-02-20 21:34:58 +01:00
|
|
|
} Circular_buffer;
|
2025-02-20 20:13:15 +01:00
|
|
|
|
2025-02-20 21:34:58 +01:00
|
|
|
static inline bool Cb_init( Circular_buffer * const cb,
|
2025-02-20 21:21:14 +01:00
|
|
|
const unsigned buf_size )
|
2025-02-20 20:13:15 +01:00
|
|
|
{
|
|
|
|
cb->buffer_size = buf_size + 1;
|
|
|
|
cb->get = 0;
|
|
|
|
cb->put = 0;
|
2025-02-20 21:21:14 +01:00
|
|
|
cb->buffer =
|
|
|
|
( cb->buffer_size > 1 ) ? (uint8_t *)malloc( cb->buffer_size ) : 0;
|
2025-02-20 21:33:45 +01:00
|
|
|
return cb->buffer != 0;
|
2025-02-20 20:13:15 +01:00
|
|
|
}
|
|
|
|
|
2025-02-20 21:34:58 +01:00
|
|
|
static inline void Cb_free( Circular_buffer * const cb )
|
2025-02-20 20:13:15 +01:00
|
|
|
{ free( cb->buffer ); cb->buffer = 0; }
|
|
|
|
|
2025-02-20 21:34:58 +01:00
|
|
|
static inline void Cb_reset( Circular_buffer * const cb )
|
2025-02-20 21:28:08 +01:00
|
|
|
{ cb->get = 0; cb->put = 0; }
|
|
|
|
|
2025-02-20 21:34:58 +01:00
|
|
|
static inline unsigned Cb_empty( const Circular_buffer * const cb )
|
2025-02-20 21:28:08 +01:00
|
|
|
{ return cb->get == cb->put; }
|
|
|
|
|
2025-02-20 21:34:58 +01:00
|
|
|
static inline unsigned Cb_used_bytes( const Circular_buffer * const cb )
|
2025-02-20 21:21:14 +01:00
|
|
|
{ return ( (cb->get <= cb->put) ? 0 : cb->buffer_size ) + cb->put - cb->get; }
|
2025-02-20 20:13:15 +01:00
|
|
|
|
2025-02-20 21:34:58 +01:00
|
|
|
static inline unsigned Cb_free_bytes( const Circular_buffer * const cb )
|
2025-02-20 21:21:14 +01:00
|
|
|
{ return ( (cb->get <= cb->put) ? cb->buffer_size : 0 ) - cb->put + cb->get - 1; }
|
2025-02-20 20:13:15 +01:00
|
|
|
|
2025-02-20 21:34:58 +01:00
|
|
|
static inline uint8_t Cb_get_byte( Circular_buffer * const cb )
|
2025-02-20 21:21:14 +01:00
|
|
|
{
|
|
|
|
const uint8_t b = cb->buffer[cb->get];
|
|
|
|
if( ++cb->get >= cb->buffer_size ) cb->get = 0;
|
|
|
|
return b;
|
|
|
|
}
|
2025-02-20 20:13:15 +01:00
|
|
|
|
2025-02-20 21:34:58 +01:00
|
|
|
static inline void Cb_put_byte( Circular_buffer * const cb, const uint8_t b )
|
2025-02-20 21:21:14 +01:00
|
|
|
{
|
|
|
|
cb->buffer[cb->put] = b;
|
|
|
|
if( ++cb->put >= cb->buffer_size ) cb->put = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2025-02-20 21:34:58 +01:00
|
|
|
static bool Cb_unread_data( Circular_buffer * const cb, const unsigned size )
|
2025-02-20 21:21:14 +01:00
|
|
|
{
|
|
|
|
if( size > Cb_free_bytes( cb ) ) return false;
|
|
|
|
if( cb->get >= size ) cb->get -= size;
|
|
|
|
else cb->get = cb->buffer_size - size + cb->get;
|
|
|
|
return true;
|
|
|
|
}
|
2025-02-20 20:13:15 +01:00
|
|
|
|
|
|
|
|
2025-02-20 21:32:02 +01:00
|
|
|
/* Copy up to 'out_size' bytes to 'out_buffer' and update 'get'.
|
2025-02-20 21:28:08 +01:00
|
|
|
If 'out_buffer' is null, the bytes are discarded.
|
2025-02-20 21:32:02 +01:00
|
|
|
Return the number of bytes copied or discarded.
|
2025-02-20 20:13:15 +01:00
|
|
|
*/
|
2025-02-20 21:34:58 +01:00
|
|
|
static unsigned Cb_read_data( Circular_buffer * const cb,
|
2025-02-20 21:21:14 +01:00
|
|
|
uint8_t * const out_buffer,
|
|
|
|
const unsigned out_size )
|
2025-02-20 20:13:15 +01:00
|
|
|
{
|
2025-02-20 21:21:14 +01:00
|
|
|
unsigned size = 0;
|
|
|
|
if( out_size == 0 ) return 0;
|
2025-02-20 20:13:15 +01:00
|
|
|
if( cb->get > cb->put )
|
|
|
|
{
|
|
|
|
size = min( cb->buffer_size - cb->get, out_size );
|
|
|
|
if( size > 0 )
|
|
|
|
{
|
2025-02-20 21:28:08 +01:00
|
|
|
if( out_buffer ) memcpy( out_buffer, cb->buffer + cb->get, size );
|
2025-02-20 20:13:15 +01:00
|
|
|
cb->get += size;
|
|
|
|
if( cb->get >= cb->buffer_size ) cb->get = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if( cb->get < cb->put )
|
|
|
|
{
|
2025-02-20 21:21:14 +01:00
|
|
|
const unsigned size2 = min( cb->put - cb->get, out_size - size );
|
2025-02-20 20:13:15 +01:00
|
|
|
if( size2 > 0 )
|
|
|
|
{
|
2025-02-20 21:28:08 +01:00
|
|
|
if( out_buffer ) memcpy( out_buffer + size, cb->buffer + cb->get, size2 );
|
2025-02-20 20:13:15 +01:00
|
|
|
cb->get += size2;
|
|
|
|
size += size2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2025-02-20 21:32:02 +01:00
|
|
|
/* Copy up to 'in_size' bytes from 'in_buffer' and update 'put'.
|
|
|
|
Return the number of bytes copied.
|
2025-02-20 20:13:15 +01:00
|
|
|
*/
|
2025-02-20 21:34:58 +01:00
|
|
|
static unsigned Cb_write_data( Circular_buffer * const cb,
|
2025-02-20 21:21:14 +01:00
|
|
|
const uint8_t * const in_buffer,
|
|
|
|
const unsigned in_size )
|
2025-02-20 20:13:15 +01:00
|
|
|
{
|
2025-02-20 21:21:14 +01:00
|
|
|
unsigned size = 0;
|
|
|
|
if( in_size == 0 ) return 0;
|
2025-02-20 20:13:15 +01:00
|
|
|
if( cb->put >= cb->get )
|
|
|
|
{
|
|
|
|
size = min( cb->buffer_size - cb->put - (cb->get == 0), in_size );
|
|
|
|
if( size > 0 )
|
|
|
|
{
|
|
|
|
memcpy( cb->buffer + cb->put, in_buffer, size );
|
|
|
|
cb->put += size;
|
|
|
|
if( cb->put >= cb->buffer_size ) cb->put = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if( cb->put < cb->get )
|
|
|
|
{
|
2025-02-20 21:21:14 +01:00
|
|
|
const unsigned size2 = min( cb->get - cb->put - 1, in_size - size );
|
2025-02-20 20:13:15 +01:00
|
|
|
if( size2 > 0 )
|
|
|
|
{
|
|
|
|
memcpy( cb->buffer + cb->put, in_buffer + size, size2 );
|
|
|
|
cb->put += size2;
|
|
|
|
size += size2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return size;
|
|
|
|
}
|