added buffer_fwd_offset() so I don't have to alloc mem to ignore some chunk of a buffer

This commit is contained in:
git@daemon.de
2014-08-06 20:19:28 +02:00
parent 7542128486
commit a3f1bdaa2e
2 changed files with 24 additions and 0 deletions

View File

@@ -415,6 +415,17 @@ byte *buffer_get_remainder(Buffer *b);
*/ */
size_t buffer_extract(Buffer *b, void *buf, size_t offset, size_t len); size_t buffer_extract(Buffer *b, void *buf, size_t offset, size_t len);
/** Forward read offset of given buffer. Use to ignore a couple of bytes.
\param[in] b The Buffer object to read from.
\param[in] fwdby Bytes to forward read offset.
\return Returns the size of bytes forwarded. Returns 0 in case of
an overflow, which can be catched with fatals_ifany().
*/
size_t buffer_fwd_offset(Buffer *b, size_t fwdby);
/** Dump the Buffer contents to stderr in hex form. /** Dump the Buffer contents to stderr in hex form.
\param[in] b The Buffer object to dump. \param[in] b The Buffer object to dump.

View File

@@ -145,6 +145,19 @@ size_t buffer_get_chunk(Buffer *b, void *buf, size_t len) {
return len; return len;
} }
size_t buffer_fwd_offset(Buffer *b, size_t fwdby) {
if(fwdby > b->end - b->offset) {
final("[buffer %s] attempt to set offset %ld bytes forward data from buffer with %ld bytes left at offset %ld\n",
b->name, fwdby, b->end - b->offset, b->offset);
}
else if(fwdby == 0) {
return 0;
}
b->offset += fwdby;
return fwdby;
}
size_t buffer_get_chunk_tobuf(Buffer *b, Buffer *dst, size_t len) { size_t buffer_get_chunk_tobuf(Buffer *b, Buffer *dst, size_t len) {
if(len > b->end - b->offset) { if(len > b->end - b->offset) {
final("[buffer %s] attempt to read %ld bytes data from buffer with %ld bytes left at offset %ld\n", final("[buffer %s] attempt to read %ld bytes data from buffer with %ld bytes left at offset %ld\n",