Simple tools for networking / objects in plain C Snapshot
Classes | Defines | Typedefs | Functions
BF

protocol buffer. idea similar to skb_buff, does not own it's data. The following invariants hold More...

Classes

struct  tagBF

Defines

#define BF_DECLARE_PUT_MACRO(type)
#define BF_DECLARE_GET_MACRO(type)
#define BF_DECLARE_PUT_UNSAFE_MACRO(type)
#define BF_DECLARE_GET_UNSAFE_MACRO(type)

Typedefs

typedef struct tagBF BF

Functions

M_INLINE void BF_init (BF *bf, void *buffer, size_t buffer_size)
 initialise an empty protocol buffer object. start and end cover the whole memory range. nitiay get and put pointer are set to start of range.
int BF_check (BF *bf)
 validates a buffer - checks all invariants.
M_INLINE void BF_get_mode (BF *bf)
 set pointers so that we can 'get' the whole range from start to end.
M_INLINE void BF_put_mode (BF *bf)
 set pointers so that we can 'put' the whole range from start to end.
M_INLINE size_t BF_get_size (BF *bf)
 returns number of bytes that can be returned by _get_
M_INLINE size_t BF_put_size (BF *bf)
 returns number of bytes that can be written by _put_
int BF_set_start (BF *bf, size_t pos)
 set start position, position is relative to start of memory range.
int BF_set_end (BF *bf, size_t pos)
 set end position, position is relative to start of memory range.
int BF_set_get (BF *bf, size_t pos)
 set get position, position is relative to 'start' pointer.
int BF_set_put (BF *bf, size_t pos)
 set put position, position is relative to 'start' pointer.
M_INLINE size_t BF_can_get (BF *bf)
 returns number of bytes that cam be read from this buffer (i.e. that BF_get_(type) return )
M_INLINE size_t BF_can_put (BF *bf)
 returns number of bytes that cam be placed into this buffer (i.e. that BF_put_(type) places into this buffer )
M_INLINE int BF_is_full (BF *bf)
 return 1 if the whole buffer is fulled with data (get_pos == start && put_pos == end)
int BF_compact (BF *bf)
 moves get_pos to start position; moves content between get_pos and put_pos if not empty.a Always succeeds returns 0 if bytes were moved, 1 if buffer was empty to begin with.
char * BF_get_line (BF *bf, int eof_line)
char * BF_get_line_ext (BF *bf, const char *eof_line, size_t eof_line_size)
M_INLINE int BF_putn (BF *bf, void *data, size_t data_size)
 add memory block to buffer

Detailed Description

protocol buffer. idea similar to skb_buff, does not own it's data. The following invariants hold

START_OF_BUFFER <= start <= get_pos <= put_pos <= end <= END_OF_BUFFER.

The effective buffer ranges from start up to end pointers - the range [start end) start may be larger than START_OF_BUFFER, as a way of reserving room for a header. end may be less than END_OF_BUFFER, as a way of reserving root for a footer.

the get_pos position will advance as data is returned from the buffer the pus_pos position will advance, as data is placed int the buffer.


Define Documentation

#define BF_DECLARE_GET_MACRO (   type)
Value:
M_INLINE int BF_get_##type ( BF *bf, type *pval ) \
{ \
   if ((bf->put_pos - bf->get_pos) < (ptrdiff_t) sizeof( type ) ) { \
     return -1 ; \
   } \
   *pval = n2h_##type( *( ( type *) bf->get_pos) ); \
   bf->get_pos += sizeof( type ); \
   return 0; \
}

Definition at line 192 of file bf.h.

#define BF_DECLARE_GET_UNSAFE_MACRO (   type)
Value:
M_INLINE type BF_get_unsafe_##type ( BF *bf ) { \
   type rval = n2h_##type( *( ( type *) bf->get_pos) ); \
   bf->get_pos += sizeof( type ); \
   return rval; \
}

Definition at line 229 of file bf.h.

#define BF_DECLARE_PUT_MACRO (   type)
Value:
M_INLINE int BF_put_##type ( BF *bf, type val ) \
{ \
   if ((bf->end - bf->put_pos) < (ptrdiff_t) sizeof( type ) ) { \
     return -1 ; \
   } \
   do { \
     type nval = h2n_##type ( val ); \
     *( ( type *) bf->put_pos) = nval; \
     bf->put_pos += sizeof ( type ); \
   } while(0); \
   return 0; \
}

Definition at line 154 of file bf.h.

#define BF_DECLARE_PUT_UNSAFE_MACRO (   type)
Value:
M_INLINE void BF_put_unsafe_##type ( BF *bf, type val ) \
{ \
  type nval = h2n_##type ( val ); \
  *( ( type *) bf->put_pos) = nval; \
  bf->put_pos += sizeof ( type ); \
}

Definition at line 212 of file bf.h.


Typedef Documentation

typedef struct tagBF BF

Function Documentation

M_INLINE size_t BF_can_get ( BF bf)

returns number of bytes that cam be read from this buffer (i.e. that BF_get_(type) return )

Definition at line 114 of file bf.h.

{
    return bf->put_pos - bf->get_pos;
}
M_INLINE size_t BF_can_put ( BF bf)

returns number of bytes that cam be placed into this buffer (i.e. that BF_put_(type) places into this buffer )

Definition at line 122 of file bf.h.

{
    return bf->end - bf->put_pos;
}
int BF_check ( BF bf)

validates a buffer - checks all invariants.

Definition at line 68 of file bf.c.

{
    uint8_t * eof;
    
    if (bf->bf > bf->start) {
      return 1;
    }
    if (bf->start > bf->get_pos)  {
      return 2;
    }
    if (bf->get_pos > bf->put_pos) {
      return 3;
    }
    if (bf->put_pos > bf->end) {
      return 4;
    }
    eof = bf->bf + bf->bf_size;
    if (bf->end > eof) {
      return 5;
    }
    return 0;
}
int BF_compact ( BF bf)

moves get_pos to start position; moves content between get_pos and put_pos if not empty.a Always succeeds returns 0 if bytes were moved, 1 if buffer was empty to begin with.

Definition at line 5 of file bf.c.

{
    size_t bs;

    if (bf->get_pos == bf->put_pos) {
      bf->get_pos = bf->put_pos = bf->start;
      return 1;
    }
    if (bf->get_pos != bf->start) {
      bs = bf->put_pos - bf->get_pos;     
      memmove( bf->start, bf->get_pos, bs );
      bf->get_pos = bf->start;
      bf->put_pos = bf->start + bs;
      return 0;
    } 
    return 1;
}
char* BF_get_line ( BF bf,
int  eof_line 
)

@ brief get the next line.

Definition at line 23 of file bf.c.

{  
   char *eof_line, *ret;

   eof_line =  memchr(bf->get_pos, eof, bf->put_pos - bf->get_pos );
   if (eof_line) {
      ret = (char *) bf->get_pos;
      *eof_line = '\0';
      bf->get_pos = (uint8_t *) eof_line + 1;
      return ret;
   }
   return 0;
}
char* BF_get_line_ext ( BF bf,
const char *  eof_line,
size_t  eof_line_size 
)

@ brief get the next line.

Definition at line 37 of file bf.c.

{
   char *pos,*next,*ret;
   size_t nsearch;
   
   nsearch = bf->put_pos - bf->get_pos;

   if (nsearch  < eof_line_size) {
     return 0;
   }
   
   nsearch -= eof_line_size - 1; 
   pos = (char *) bf->get_pos;

   do { 
     next = (char *) memchr( pos, (int) *eof_line, nsearch);
     if (!next) {
       return 0;
     }
     if (memcmp(next, eof_line, eof_line_size) == 0) {
        ret = (char *) bf->get_pos;
        *next = '\0';
        bf->get_pos = (uint8_t *) (next + eof_line_size);
        return (char *) ret;
     }
     pos = next + 1;
   } while( pos < (char *) bf->put_pos );

   return 0;
}
M_INLINE void BF_get_mode ( BF bf)

set pointers so that we can 'get' the whole range from start to end.

Definition at line 56 of file bf.h.

{
    bf->get_pos = bf->start;
    bf->put_pos = bf->end;
}
M_INLINE size_t BF_get_size ( BF bf)

returns number of bytes that can be returned by _get_

Definition at line 75 of file bf.h.

{
  return bf->put_pos - bf->get_pos;
}
M_INLINE void BF_init ( BF bf,
void *  buffer,
size_t  buffer_size 
)

initialise an empty protocol buffer object. start and end cover the whole memory range. nitiay get and put pointer are set to start of range.

Definition at line 37 of file bf.h.

{
   bf->bf =  (uint8_t *) buffer;
   bf->bf_size = buffer_size;

   bf->start =   (uint8_t *) buffer;
   bf->end = buffer + buffer_size;

   bf->get_pos = bf->put_pos = bf->start;
}
M_INLINE int BF_is_full ( BF bf)

return 1 if the whole buffer is fulled with data (get_pos == start && put_pos == end)

Definition at line 130 of file bf.h.

{
    return bf->get_pos == bf->start && bf->end == bf->put_pos;
}
M_INLINE void BF_put_mode ( BF bf)

set pointers so that we can 'put' the whole range from start to end.

Definition at line 66 of file bf.h.

{
    bf->get_pos = bf->start;
    bf->put_pos = bf->start;
}
M_INLINE size_t BF_put_size ( BF bf)

returns number of bytes that can be written by _put_

Definition at line 84 of file bf.h.

{
  return bf->end - bf->put_pos;
}
M_INLINE int BF_putn ( BF bf,
void *  data,
size_t  data_size 
)

add memory block to buffer

Returns:
-1 not enough room left, 0 on success.

Definition at line 181 of file bf.h.

{ 
   if ((bf->end - bf->put_pos) < (ptrdiff_t) data_size ) { 
     return -1 ; 
   } 
   memcpy( bf->put_pos, data, data_size );
   bf->put_pos += data_size;
   return 0;
} 
int BF_set_end ( BF bf,
size_t  pos 
)

set end position, position is relative to start of memory range.

Definition at line 111 of file bf.c.

{
  if (pos > bf->bf_size) {
    return -1;
  }
  bf->end = bf->bf + pos;

  if (bf->end < bf->get_pos) {
     bf->get_pos = bf->end;
  }
  if (bf->end < bf->put_pos) {
     bf->put_pos = bf->end;
  }
  if (bf->end <  bf->start) {
    bf->start = bf->end; 
  }
  return 0;
}
int BF_set_get ( BF bf,
size_t  pos 
)

set get position, position is relative to 'start' pointer.

Definition at line 130 of file bf.c.

{
  size_t bfs = bf->end - bf->start; 
  if (pos > bfs) {
    return -1;
  }
  bf->get_pos = bf->start + pos;
  if (bf->get_pos > bf->put_pos) {
     bf->put_pos = bf->get_pos;
  }
  return 0;
}
int BF_set_put ( BF bf,
size_t  pos 
)

set put position, position is relative to 'start' pointer.

Definition at line 143 of file bf.c.

{
  size_t bfs = bf->end - bf->start; 
  if (pos > bfs) {
    return -1;
  }
  bf->put_pos = bf->start + pos;
  if (bf->put_pos < bf->get_pos) {
     bf->get_pos = bf->put_pos;
  }
  return 0;
}
int BF_set_start ( BF bf,
size_t  pos 
)

set start position, position is relative to start of memory range.

Definition at line 92 of file bf.c.

{
  if (pos > bf->bf_size) {
    return -1;
  }
  bf->start = bf->bf + pos;

  if (bf->start > bf->get_pos) {
     bf->get_pos = bf->start;
  }
  if (bf->start > bf->put_pos) {
     bf->put_pos = bf->start;
  }
  if (bf->start > bf->end) {
    bf->end = bf->start; 
  }
  return 0;
}