2012-08-22 00:51:38 +00:00
|
|
|
/*
|
|
|
|
Serval Daemon
|
|
|
|
Copyright (C) 2012 Serval Project Inc.
|
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or
|
|
|
|
modify it under the terms of the GNU General Public License
|
|
|
|
as published by the Free Software Foundation; either version 2
|
|
|
|
of the License, or (at your option) any later version.
|
|
|
|
|
|
|
|
This program 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. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program; if not, write to the Free Software
|
|
|
|
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
*/
|
|
|
|
|
2013-12-04 06:44:14 +00:00
|
|
|
#ifndef __SERVAL_DNA___OVERLAY_BUFFER_H
|
|
|
|
#define __SERVAL_DNA___OVERLAY_BUFFER_H
|
2012-08-22 00:51:38 +00:00
|
|
|
|
|
|
|
struct overlay_buffer {
|
|
|
|
unsigned char *bytes;
|
|
|
|
|
|
|
|
// remembered position for rewinding
|
2013-12-10 06:33:30 +00:00
|
|
|
size_t checkpointLength;
|
2012-08-22 00:51:38 +00:00
|
|
|
|
|
|
|
// position of data read / written
|
2013-12-10 06:33:30 +00:00
|
|
|
size_t position;
|
2012-08-22 00:51:38 +00:00
|
|
|
|
|
|
|
// maximum allowed bytes for reading / writing
|
2013-12-10 06:33:30 +00:00
|
|
|
size_t sizeLimit;
|
2012-08-22 00:51:38 +00:00
|
|
|
|
|
|
|
// size of buffer
|
2013-12-10 06:33:30 +00:00
|
|
|
size_t allocSize;
|
2012-08-22 00:51:38 +00:00
|
|
|
|
|
|
|
// is this an allocated buffer? can it be resized? Should it be freed?
|
2012-11-27 04:02:10 +00:00
|
|
|
unsigned char * allocated;
|
2012-08-22 00:51:38 +00:00
|
|
|
|
2012-11-23 03:04:01 +00:00
|
|
|
// length position for later patching
|
2013-12-10 06:33:30 +00:00
|
|
|
size_t var_length_offset;
|
2012-08-22 00:51:38 +00:00
|
|
|
};
|
|
|
|
|
2013-11-22 14:29:11 +00:00
|
|
|
struct overlay_buffer *_ob_new(struct __sourceloc __whence);
|
2013-12-10 06:33:30 +00:00
|
|
|
struct overlay_buffer *_ob_static(struct __sourceloc __whence, unsigned char *bytes, size_t size);
|
|
|
|
struct overlay_buffer *_ob_slice(struct __sourceloc __whence, struct overlay_buffer *b, size_t offset, size_t length);
|
2013-11-22 14:29:11 +00:00
|
|
|
struct overlay_buffer *_ob_dup(struct __sourceloc __whence, struct overlay_buffer *b);
|
|
|
|
void _ob_free(struct __sourceloc __whence, struct overlay_buffer *b);
|
|
|
|
int _ob_checkpoint(struct __sourceloc __whence, struct overlay_buffer *b);
|
|
|
|
int _ob_rewind(struct __sourceloc __whence, struct overlay_buffer *b);
|
2013-12-10 06:33:30 +00:00
|
|
|
void _ob_limitsize(struct __sourceloc __whence, struct overlay_buffer *b, size_t bytes);
|
2013-11-22 14:29:11 +00:00
|
|
|
void _ob_flip(struct __sourceloc __whence, struct overlay_buffer *b);
|
|
|
|
void _ob_unlimitsize(struct __sourceloc __whence, struct overlay_buffer *b);
|
|
|
|
ssize_t _ob_makespace(struct __sourceloc whence, struct overlay_buffer *b, size_t bytes);
|
2013-12-10 06:33:30 +00:00
|
|
|
void _ob_set(struct __sourceloc __whence, struct overlay_buffer *b, size_t ofs, unsigned char byte);
|
|
|
|
void _ob_set_ui16(struct __sourceloc __whence, struct overlay_buffer *b, size_t offset, uint16_t v);
|
2013-11-22 14:29:11 +00:00
|
|
|
void _ob_patch_rfs(struct __sourceloc __whence, struct overlay_buffer *b);
|
2013-06-04 06:27:51 +00:00
|
|
|
|
2013-11-22 14:29:11 +00:00
|
|
|
void _ob_append_byte(struct __sourceloc whence, struct overlay_buffer *b,unsigned char byte);
|
2013-12-10 06:33:30 +00:00
|
|
|
void _ob_append_bytes(struct __sourceloc whence, struct overlay_buffer *b,const unsigned char *bytes, size_t count);
|
2013-11-22 14:29:11 +00:00
|
|
|
void _ob_append_buffer(struct __sourceloc whence, struct overlay_buffer *b,struct overlay_buffer *s);
|
2013-12-10 06:33:30 +00:00
|
|
|
unsigned char *_ob_append_space(struct __sourceloc whence, struct overlay_buffer *b, size_t count);
|
2013-11-22 14:29:11 +00:00
|
|
|
void _ob_append_ui16(struct __sourceloc whence, struct overlay_buffer *b, uint16_t v);
|
|
|
|
void _ob_append_ui32(struct __sourceloc whence, struct overlay_buffer *b, uint32_t v);
|
|
|
|
void _ob_append_ui64(struct __sourceloc whence, struct overlay_buffer *b, uint64_t v);
|
|
|
|
void _ob_append_packed_ui32(struct __sourceloc whence, struct overlay_buffer *b, uint32_t v);
|
|
|
|
void _ob_append_packed_ui64(struct __sourceloc whence, struct overlay_buffer *b, uint64_t v);
|
|
|
|
void _ob_append_rfs(struct __sourceloc whence, struct overlay_buffer *b,int l);
|
|
|
|
|
|
|
|
#define ob_new() _ob_new(__WHENCE__)
|
|
|
|
#define ob_static(bytes, size) _ob_static(__WHENCE__, bytes, size)
|
|
|
|
#define ob_slice(b, off, len) _ob_slice(__WHENCE__, b, off, len)
|
|
|
|
#define ob_dup(b) _ob_dup(__WHENCE__, b)
|
|
|
|
#define ob_free(b) _ob_free(__WHENCE__, b)
|
|
|
|
#define ob_checkpoint(b) _ob_checkpoint(__WHENCE__, b)
|
|
|
|
#define ob_rewind(b) _ob_rewind(__WHENCE__, b)
|
|
|
|
#define ob_limitsize(b, size) _ob_limitsize(__WHENCE__, b, size)
|
|
|
|
#define ob_flip(b) _ob_flip(__WHENCE__, b)
|
|
|
|
#define ob_unlimitsize(b) _ob_unlimitsize(__WHENCE__, b)
|
|
|
|
#define ob_makespace(b, bytes) _ob_makespace(__WHENCE__, b, bytes)
|
|
|
|
#define ob_set(b, off, byte) _ob_set(__WHENCE__, b, off, byte)
|
|
|
|
#define ob_set_ui16(b, off, v) _ob_set_ui16(__WHENCE__, b, off, v)
|
|
|
|
#define ob_patch_rfs(b) _ob_patch_rfs(__WHENCE__, b)
|
|
|
|
|
|
|
|
#define ob_append_byte(b, byte) _ob_append_byte(__WHENCE__, b, byte)
|
|
|
|
#define ob_append_bytes(b, bytes, count) _ob_append_bytes(__WHENCE__, b, bytes, count)
|
|
|
|
#define ob_append_buffer(b, s) _ob_append_buffer(__WHENCE__, b, s)
|
|
|
|
#define ob_append_space(b, count) _ob_append_space(__WHENCE__, b, count)
|
|
|
|
#define ob_append_ui16(b, v) _ob_append_ui16(__WHENCE__, b, v)
|
|
|
|
#define ob_append_ui32(b, v) _ob_append_ui32(__WHENCE__, b, v)
|
|
|
|
#define ob_append_ui64(b, v) _ob_append_ui64(__WHENCE__, b, v)
|
|
|
|
#define ob_append_packed_ui32(b, v) _ob_append_packed_ui32(__WHENCE__, b, v)
|
|
|
|
#define ob_append_packed_ui64(b, v) _ob_append_packed_ui64(__WHENCE__, b, v)
|
|
|
|
#define ob_append_rfs(b, l) _ob_append_rfs(__WHENCE__, b, l)
|
2013-06-04 06:27:51 +00:00
|
|
|
|
2012-08-22 00:51:38 +00:00
|
|
|
// get one byte, -ve number indicates failure
|
2013-11-28 07:14:37 +00:00
|
|
|
int ob_peek(struct overlay_buffer *b);
|
|
|
|
void ob_skip(struct overlay_buffer *b, unsigned n);
|
2012-08-22 00:51:38 +00:00
|
|
|
// get one byte from the current position, -ve number indicates failure
|
|
|
|
int ob_get(struct overlay_buffer *b);
|
2013-12-10 06:33:30 +00:00
|
|
|
int ob_get_bytes(struct overlay_buffer *b, unsigned char *buff, size_t len);
|
|
|
|
unsigned char * ob_get_bytes_ptr(struct overlay_buffer *b, size_t len);
|
2013-06-18 03:57:26 +00:00
|
|
|
uint64_t ob_get_ui64(struct overlay_buffer *b);
|
2012-08-22 00:51:38 +00:00
|
|
|
uint32_t ob_get_ui32(struct overlay_buffer *b);
|
|
|
|
uint16_t ob_get_ui16(struct overlay_buffer *b);
|
|
|
|
int ob_dump(struct overlay_buffer *b,char *desc);
|
|
|
|
|
2012-11-28 03:01:33 +00:00
|
|
|
uint32_t ob_get_packed_ui32(struct overlay_buffer *b);
|
2013-06-18 03:57:26 +00:00
|
|
|
uint64_t ob_get_packed_ui64(struct overlay_buffer *b);
|
2012-11-28 03:01:33 +00:00
|
|
|
|
2012-11-27 04:02:10 +00:00
|
|
|
// information routines
|
2013-12-10 06:33:30 +00:00
|
|
|
size_t ob_position(struct overlay_buffer *b);
|
|
|
|
size_t ob_limit(struct overlay_buffer *b);
|
|
|
|
size_t ob_remaining(struct overlay_buffer *b);
|
2013-11-22 14:29:11 +00:00
|
|
|
int _ob_overrun(struct __sourceloc, struct overlay_buffer *b);
|
2012-11-27 04:02:10 +00:00
|
|
|
unsigned char* ob_ptr(struct overlay_buffer *b);
|
2013-11-25 06:13:32 +00:00
|
|
|
|
2013-11-22 14:29:11 +00:00
|
|
|
#define ob_overrun(b) _ob_overrun(__WHENCE__, b)
|
|
|
|
|
2013-12-04 06:44:14 +00:00
|
|
|
#endif //__SERVAL_DNA___OVERLAY_BUFFER_H
|