Merging upstream version 1.8.
Signed-off-by: Daniel Baumann <daniel@debian.org>
This commit is contained in:
parent
787b505bfa
commit
480ba29206
28 changed files with 638 additions and 425 deletions
71
cbuffer.c
71
cbuffer.c
|
@ -1,5 +1,5 @@
|
|||
/* Lzlib - Compression library for the lzip format
|
||||
Copyright (C) 2009-2015 Antonio Diaz Diaz.
|
||||
Copyright (C) 2009-2016 Antonio Diaz Diaz.
|
||||
|
||||
This library is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
|
@ -28,56 +28,68 @@
|
|||
struct Circular_buffer
|
||||
{
|
||||
uint8_t * buffer;
|
||||
int buffer_size; /* capacity == buffer_size - 1 */
|
||||
int get; /* buffer is empty when get == put */
|
||||
int put;
|
||||
unsigned buffer_size; /* capacity == buffer_size - 1 */
|
||||
unsigned get; /* buffer is empty when get == put */
|
||||
unsigned put;
|
||||
};
|
||||
|
||||
static inline void Cb_reset( struct Circular_buffer * const cb )
|
||||
{ cb->get = 0; cb->put = 0; }
|
||||
|
||||
static inline bool Cb_init( struct Circular_buffer * const cb,
|
||||
const int buf_size )
|
||||
const unsigned buf_size )
|
||||
{
|
||||
cb->buffer = (uint8_t *)malloc( buf_size + 1 );
|
||||
cb->buffer_size = buf_size + 1;
|
||||
cb->get = 0;
|
||||
cb->put = 0;
|
||||
cb->buffer =
|
||||
( cb->buffer_size > 1 ) ? (uint8_t *)malloc( cb->buffer_size ) : 0;
|
||||
return ( cb->buffer != 0 );
|
||||
}
|
||||
|
||||
static inline void Cb_free( struct Circular_buffer * const cb )
|
||||
{ free( cb->buffer ); cb->buffer = 0; }
|
||||
|
||||
static inline int Cb_used_bytes( const struct Circular_buffer * const cb )
|
||||
{ return ( (cb->get <= cb->put) ? 0 : cb->buffer_size ) + cb->put - cb->get; }
|
||||
static inline unsigned Cb_used_bytes( const struct Circular_buffer * const cb )
|
||||
{ return ( (cb->get <= cb->put) ? 0 : cb->buffer_size ) + cb->put - cb->get; }
|
||||
|
||||
static inline int Cb_free_bytes( const struct Circular_buffer * const cb )
|
||||
{ return ( (cb->get <= cb->put) ? cb->buffer_size : 0 ) - cb->put + cb->get - 1; }
|
||||
static inline unsigned Cb_free_bytes( const struct Circular_buffer * const cb )
|
||||
{ return ( (cb->get <= cb->put) ? cb->buffer_size : 0 ) - cb->put + cb->get - 1; }
|
||||
|
||||
static inline uint8_t Cb_get_byte( struct Circular_buffer * const cb )
|
||||
{
|
||||
const uint8_t b = cb->buffer[cb->get];
|
||||
if( ++cb->get >= cb->buffer_size ) cb->get = 0;
|
||||
return b;
|
||||
}
|
||||
{
|
||||
const uint8_t b = cb->buffer[cb->get];
|
||||
if( ++cb->get >= cb->buffer_size ) cb->get = 0;
|
||||
return b;
|
||||
}
|
||||
|
||||
static inline void Cb_put_byte( struct Circular_buffer * const cb,
|
||||
const uint8_t b )
|
||||
{
|
||||
cb->buffer[cb->put] = b;
|
||||
if( ++cb->put >= cb->buffer_size ) cb->put = 0;
|
||||
}
|
||||
{
|
||||
cb->buffer[cb->put] = b;
|
||||
if( ++cb->put >= cb->buffer_size ) cb->put = 0;
|
||||
}
|
||||
|
||||
|
||||
static bool Cb_unread_data( struct Circular_buffer * const cb,
|
||||
const unsigned size )
|
||||
{
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
/* Copies up to 'out_size' bytes to 'out_buffer' and updates 'get'.
|
||||
Returns the number of bytes copied.
|
||||
*/
|
||||
static int Cb_read_data( struct Circular_buffer * const cb,
|
||||
uint8_t * const out_buffer, const int out_size )
|
||||
static unsigned Cb_read_data( struct Circular_buffer * const cb,
|
||||
uint8_t * const out_buffer,
|
||||
const unsigned out_size )
|
||||
{
|
||||
int size = 0;
|
||||
if( out_size <= 0 ) return 0;
|
||||
unsigned size = 0;
|
||||
if( out_size == 0 ) return 0;
|
||||
if( cb->get > cb->put )
|
||||
{
|
||||
size = min( cb->buffer_size - cb->get, out_size );
|
||||
|
@ -90,7 +102,7 @@ static int Cb_read_data( struct Circular_buffer * const cb,
|
|||
}
|
||||
if( cb->get < cb->put )
|
||||
{
|
||||
const int size2 = min( cb->put - cb->get, out_size - size );
|
||||
const unsigned size2 = min( cb->put - cb->get, out_size - size );
|
||||
if( size2 > 0 )
|
||||
{
|
||||
memcpy( out_buffer + size, cb->buffer + cb->get, size2 );
|
||||
|
@ -105,11 +117,12 @@ static int Cb_read_data( struct Circular_buffer * const cb,
|
|||
/* Copies up to 'in_size' bytes from 'in_buffer' and updates 'put'.
|
||||
Returns the number of bytes copied.
|
||||
*/
|
||||
static int Cb_write_data( struct Circular_buffer * const cb,
|
||||
const uint8_t * const in_buffer, const int in_size )
|
||||
static unsigned Cb_write_data( struct Circular_buffer * const cb,
|
||||
const uint8_t * const in_buffer,
|
||||
const unsigned in_size )
|
||||
{
|
||||
int size = 0;
|
||||
if( in_size < 0 ) return 0;
|
||||
unsigned size = 0;
|
||||
if( in_size == 0 ) return 0;
|
||||
if( cb->put >= cb->get )
|
||||
{
|
||||
size = min( cb->buffer_size - cb->put - (cb->get == 0), in_size );
|
||||
|
@ -122,7 +135,7 @@ static int Cb_write_data( struct Circular_buffer * const cb,
|
|||
}
|
||||
if( cb->put < cb->get )
|
||||
{
|
||||
const int size2 = min( cb->get - cb->put - 1, in_size - size );
|
||||
const unsigned size2 = min( cb->get - cb->put - 1, in_size - size );
|
||||
if( size2 > 0 )
|
||||
{
|
||||
memcpy( cb->buffer + cb->put, in_buffer + size, size2 );
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue