Main Page | Modules | Alphabetical List | Data Structures | File List | Data Fields | Globals | Related Pages

cobs.c File Reference


Detailed Description

Consistant Overhead Byte Stuffing (COBS).

COBS is an escaping algorithm, taking input in the [0,255] range and producing output in the [1,255] range. In other words, it escapes all NUL bytes.

Author:
Raphael Manfredi
Date:
2002-2004

#include "common.h"
#include "cobs.h"
#include "walloc.h"
#include "override.h"

Defines

#define FINISH(x)
#define FINISH()

Functions

 RCSID ("$Id:cobs.c, v 1.13 2005/09/10 10:10:36 daichik Exp $")
gchar * cobs_encodev (struct iovec *iov, gint iovcnt, size_t *retlen)
 Encode vector `iov' of `iovcnt' elements, whose size is held in `retlen'.

gchar * cobs_encode (gchar *buf, size_t len, size_t *retlen)
 Encode `len' bytes starting at `buf' into new allocated buffer.

gboolean cobs_decode_into (const gchar *buf, size_t len, gchar *out, size_t outlen, size_t *retlen)
 Decode `len' bytes starting at `buf' into decoding buffer `out', which is `outlen' bytes long.

gboolean cobs_is_valid (const gchar *buf, size_t len)
 Check whether supplied buffer forms a valid COBS encoding.

gchar * cobs_decode (gchar *buf, size_t len, size_t *retlen, gboolean inplace)
 Decode `len' bytes starting at `buf' into new allocated buffer, unless `inplace' is true in which case decoding is done inplace.

void cobs_stream_init (cobs_stream_t *cs, gpointer data, size_t len)
 Initialize an incremental COBS context, where data will be written in the supplied buffer.

gboolean cobs_stream_write (cobs_stream_t *cs, gpointer data, size_t len)
 Add data to the COBS stream.

size_t cobs_stream_close (cobs_stream_t *cs, gboolean *saw_nul)
 Close COBS stream: we have no more data to write.

void cobs_stream_invalidate (cobs_stream_t *cs)
 Empty the descriptor, making it invalid.

gboolean cobs_stream_is_valid (cobs_stream_t *cs)
 Check whether the stream descriptor is valid, for assertions.


Define Documentation

 
#define FINISH  ) 
 

Value:

do {                    \
    g_assert(cs->cp < cs->end);         \
    *cs->cp = cs->last_code = cs->code; \
    cs->cp = cs->o++;                   \
    cs->code = 0x1;                     \
} while (0)

#define FINISH  ) 
 

Value:

do {            \
    *cp = last_code = (x);      \
    cp = o++;                   \
    code = 0x1;                 \
} while (0)


Function Documentation

gchar* cobs_decode gchar *  buf,
size_t  len,
size_t *  retlen,
gboolean  inplace
 

Decode `len' bytes starting at `buf' into new allocated buffer, unless `inplace' is true in which case decoding is done inplace.

Returns:
the new decoded buffer, or NULL if the input was not valid COBS encoding. The length of the decoded buffer is in `retlen'.

gboolean cobs_decode_into const gchar *  buf,
size_t  len,
gchar *  out,
size_t  outlen,
size_t *  retlen
 

Decode `len' bytes starting at `buf' into decoding buffer `out', which is `outlen' bytes long.

Returns:
whether the input was valid COBS encoding. The length of the decoded buffer is returned in `retlen'.

gchar* cobs_encode gchar *  buf,
size_t  len,
size_t *  retlen
 

Encode `len' bytes starting at `buf' into new allocated buffer.

Returns:
the new encoded buffer, and its length in `retlen'.

gchar* cobs_encodev struct iovec *  iov,
gint  iovcnt,
size_t *  retlen
 

Encode vector `iov' of `iovcnt' elements, whose size is held in `retlen'.

Returns:
the new encoded buffer, and its length in `retlen'.
Attention:
NB: the output is a linear buffer, not a vector.

gboolean cobs_is_valid const gchar *  buf,
size_t  len
 

Check whether supplied buffer forms a valid COBS encoding.

size_t cobs_stream_close cobs_stream_t cs,
gboolean *  saw_nul
 

Close COBS stream: we have no more data to write.

Parameters:
cs the stream to close
saw_nul if non-NULL, writes whether we saw a NUL in the input
Returns:
the final length of the stream on success, 0 on error.

void cobs_stream_init cobs_stream_t cs,
gpointer  data,
size_t  len
 

Initialize an incremental COBS context, where data will be written in the supplied buffer.

Parameters:
cs the COBS stream to initialize.
data start of buffer where data will be written
len length of supplied buffer

void cobs_stream_invalidate cobs_stream_t cs  ) 
 

Empty the descriptor, making it invalid.

gboolean cobs_stream_is_valid cobs_stream_t cs  ) 
 

Check whether the stream descriptor is valid, for assertions.

gboolean cobs_stream_write cobs_stream_t cs,
gpointer  data,
size_t  len
 

Add data to the COBS stream.

Returns:
TRUE if OK, FALSE if we cannot put the data any more.

RCSID "$Id:cobs.  c,
v 1.13 2005/09/10 10:10:36 daichik Exp $" 
 


Generated on Sun Feb 12 10:49:59 2006 for Gtk-Gnutella by doxygen 1.3.6