38499bb33c74acc6d725204e893cfc02a5890ec7Timo Sirainen/* WARNING: Be careful with functions that return pointers to data.
38499bb33c74acc6d725204e893cfc02a5890ec7Timo Sirainen With dynamic buffers they are valid only as long as buffer is not
38499bb33c74acc6d725204e893cfc02a5890ec7Timo Sirainen realloc()ed. You shouldn't rely on it being valid if you have modified
38499bb33c74acc6d725204e893cfc02a5890ec7Timo Sirainen buffer in any way. */
02752bc8d64df8cd361f464e55422f7b3f2f143eTimo Sirainen/* Create a modifiable buffer from given data. Writes past this size will
02752bc8d64df8cd361f464e55422f7b3f2f143eTimo Sirainen i_panic(). */
3281669db44d09a087a203201248abbc81b3cc1aTimo Sirainenvoid buffer_create_from_data(buffer_t *buffer, void *data, size_t size);
8d80659e504ffb34bb0c6a633184fece35751b18Timo Sirainen/* Create a non-modifiable buffer from given data. */
3281669db44d09a087a203201248abbc81b3cc1aTimo Sirainenvoid buffer_create_from_const_data(buffer_t *buffer,
0a568c1e8a8066ce9d6467d891a9717bd2a24b26Phil Carmody#if defined(__GNUC__) && (__GNUC__ * 100 + __GNUC_MINOR__) > 401
23bdbb7b1831785c6ba6df190f6369da882d2b9dTimo Sirainen (void)COMPILE_ERROR_IF_TRUE(__builtin_object_size((d),1) < ((s)>0?(s):1)); \
0a568c1e8a8066ce9d6467d891a9717bd2a24b26Phil Carmody buffer_create_from_data((b), (d), (s)); })
23bdbb7b1831785c6ba6df190f6369da882d2b9dTimo Sirainen#define buffer_create_from_const_data(b,d,s) ({ \
23bdbb7b1831785c6ba6df190f6369da882d2b9dTimo Sirainen (void)COMPILE_ERROR_IF_TRUE(__builtin_object_size((d),1) < ((s)>0?(s):1)); \
0a568c1e8a8066ce9d6467d891a9717bd2a24b26Phil Carmody buffer_create_from_const_data((b), (d), (s)); })
def516ea503a60f20d510c14d5070b7ff5bbddf4Timo Sirainen/* Creates a dynamically growing buffer. Whenever write would exceed the
def516ea503a60f20d510c14d5070b7ff5bbddf4Timo Sirainen current size it's grown. */
4b058f90f9e8a2c6b2eed275de4eb8cc5195a71dTimo Sirainenbuffer_t *buffer_create_dynamic(pool_t pool, size_t init_size);
4d0200e2055cd5507409b4ee998534e5a1479b1cAki Tuomi buffer_create_dynamic(pool_datastack_create(), (init_size))
def516ea503a60f20d510c14d5070b7ff5bbddf4Timo Sirainen/* Free the memory used by buffer. Not needed if the memory is free'd
def516ea503a60f20d510c14d5070b7ff5bbddf4Timo Sirainen directly from the memory pool. */
def516ea503a60f20d510c14d5070b7ff5bbddf4Timo Sirainen/* Free the memory used by buffer structure, but return the buffer data
0f66f12eb4cdbf47670975044c88d8f388bf92dfTimo Sirainenvoid *buffer_free_without_data(buffer_t **buf);
1345157bfafcc329b237e55a35db8c2ad368a42aTimo Sirainen/* Returns the pool buffer was created with. */
68a4946b12583b88fa802e52ebee45cd96056772Timo Sirainenpool_t buffer_get_pool(const buffer_t *buf) ATTR_PURE;
a44866b012a59906a592a8847ca43230c020fe2bTimo Sirainen/* Write data to buffer at specified position. If pos is beyond the buffer's
a44866b012a59906a592a8847ca43230c020fe2bTimo Sirainen current size, it is zero-filled up to that point (even if data_size==0). */
4b058f90f9e8a2c6b2eed275de4eb8cc5195a71dTimo Sirainen/* Append data to buffer. */
4b058f90f9e8a2c6b2eed275de4eb8cc5195a71dTimo Sirainenvoid buffer_append(buffer_t *buf, const void *data, size_t data_size);
4b058f90f9e8a2c6b2eed275de4eb8cc5195a71dTimo Sirainen/* Append character to buffer. */
4b058f90f9e8a2c6b2eed275de4eb8cc5195a71dTimo Sirainenvoid buffer_append_c(buffer_t *buf, unsigned char chr);
4b058f90f9e8a2c6b2eed275de4eb8cc5195a71dTimo Sirainen/* Insert data to buffer. */
4b058f90f9e8a2c6b2eed275de4eb8cc5195a71dTimo Sirainen/* Delete data from buffer. */
4b058f90f9e8a2c6b2eed275de4eb8cc5195a71dTimo Sirainenvoid buffer_delete(buffer_t *buf, size_t pos, size_t size);
0138d3060877805f0de0bf631642de100ff96b79Timo Sirainen/* Fill buffer with zero bytes. */
4b058f90f9e8a2c6b2eed275de4eb8cc5195a71dTimo Sirainenvoid buffer_write_zero(buffer_t *buf, size_t pos, size_t data_size);
4b058f90f9e8a2c6b2eed275de4eb8cc5195a71dTimo Sirainenvoid buffer_append_zero(buffer_t *buf, size_t data_size);
4b058f90f9e8a2c6b2eed275de4eb8cc5195a71dTimo Sirainenvoid buffer_insert_zero(buffer_t *buf, size_t pos, size_t data_size);
def516ea503a60f20d510c14d5070b7ff5bbddf4Timo Sirainen/* Copy data from buffer to another. The buffers may be same in which case
def516ea503a60f20d510c14d5070b7ff5bbddf4Timo Sirainen it's internal copying, possibly with overlapping positions (ie. memmove()
def516ea503a60f20d510c14d5070b7ff5bbddf4Timo Sirainen like functionality). copy_size may be set to (size_t)-1 to copy the rest of
4b058f90f9e8a2c6b2eed275de4eb8cc5195a71dTimo Sirainen the used data in buffer. */
4b058f90f9e8a2c6b2eed275de4eb8cc5195a71dTimo Sirainenvoid buffer_copy(buffer_t *dest, size_t dest_pos,
4b058f90f9e8a2c6b2eed275de4eb8cc5195a71dTimo Sirainen const buffer_t *src, size_t src_pos, size_t copy_size);
def516ea503a60f20d510c14d5070b7ff5bbddf4Timo Sirainen/* Append data to buffer from another. copy_size may be set to (size_t)-1 to
def516ea503a60f20d510c14d5070b7ff5bbddf4Timo Sirainen copy the rest of the used data in buffer. */
4b058f90f9e8a2c6b2eed275de4eb8cc5195a71dTimo Sirainenvoid buffer_append_buf(buffer_t *dest, const buffer_t *src,
4b058f90f9e8a2c6b2eed275de4eb8cc5195a71dTimo Sirainen/* Returns pointer to specified position in buffer. WARNING: The returned
4b058f90f9e8a2c6b2eed275de4eb8cc5195a71dTimo Sirainen address may become invalid if you add more data to buffer. */
38499bb33c74acc6d725204e893cfc02a5890ec7Timo Sirainenvoid *buffer_get_space_unsafe(buffer_t *buf, size_t pos, size_t size);
def516ea503a60f20d510c14d5070b7ff5bbddf4Timo Sirainen/* Increase the buffer usage by given size, and return a pointer to beginning
38499bb33c74acc6d725204e893cfc02a5890ec7Timo Sirainenvoid *buffer_append_space_unsafe(buffer_t *buf, size_t size);
def516ea503a60f20d510c14d5070b7ff5bbddf4Timo Sirainen/* Like buffer_get_data(), but don't return it as const. Returns NULL if the
8d80659e504ffb34bb0c6a633184fece35751b18Timo Sirainen buffer is non-modifiable. WARNING: The returned address may become invalid
38499bb33c74acc6d725204e893cfc02a5890ec7Timo Sirainen if you add more data to buffer. */
a10ed8c47534b4c6b6bf2711ccfe577e720a47b4Timo Sirainenvoid *buffer_get_modifiable_data(const buffer_t *buf, size_t *used_size_r)
def516ea503a60f20d510c14d5070b7ff5bbddf4Timo Sirainen/* Set the "used size" of buffer, ie. 0 would set the buffer empty.
94a6b3df81d2edb93460b7beca6d37e618a48262Timo Sirainen Must not be used to grow buffer. The data after the buffer's new size will
94a6b3df81d2edb93460b7beca6d37e618a48262Timo Sirainen be effectively lost, because e.g. buffer_get_space_unsafe() will zero out
94a6b3df81d2edb93460b7beca6d37e618a48262Timo Sirainen the contents. */
c0435c854a0e7246373b9752d163095cc4fbe985Timo Sirainenvoid buffer_set_used_size(buffer_t *buf, size_t used_size);
def516ea503a60f20d510c14d5070b7ff5bbddf4Timo Sirainen/* Returns the current buffer size. */
68a4946b12583b88fa802e52ebee45cd96056772Timo Sirainensize_t buffer_get_size(const buffer_t *buf) ATTR_PURE;
e915ba86f157549b7d127f92312bc487b249df7eTimo Sirainen/* Returns how many bytes we can write to buffer without increasing its size.
e915ba86f157549b7d127f92312bc487b249df7eTimo Sirainen With dynamic buffers this is buffer_get_size()-1, because the extra 1 byte
e915ba86f157549b7d127f92312bc487b249df7eTimo Sirainen is reserved for str_c()'s NUL. */
e915ba86f157549b7d127f92312bc487b249df7eTimo Sirainensize_t buffer_get_writable_size(const buffer_t *buf) ATTR_PURE;
02af13e6a41b9c3e6ad1e0b692b5b9741142c221Timo Sirainen/* Returns TRUE if buffer contents are identical. */
6ef7e31619edfaa17ed044b45861d106a86191efTimo Sirainenbool buffer_cmp(const buffer_t *buf1, const buffer_t *buf2);
b561170c84d19ef1dee3d528939d77fd38047b3fTimo Sirainen/* Returns pointer to beginning of buffer data. Current used size of buffer is
b561170c84d19ef1dee3d528939d77fd38047b3fTimo Sirainen stored in used_size if it's non-NULL. */
bbe0ee356dc610a8d054b336534d8f33c49a36b7Timo Sirainenbuffer_get_data(const buffer_t *buf, size_t *used_size_r)
b561170c84d19ef1dee3d528939d77fd38047b3fTimo Sirainen/* Returns the current used buffer size. */
a94f166ade968e8127b3eeda729417db9c6ad52fTimo Sirainen/* Crash if buffer was allocated from data stack and stack frame has changed.
a94f166ade968e8127b3eeda729417db9c6ad52fTimo Sirainen This can be used as an assert-like check to verify that it's valid to
a94f166ade968e8127b3eeda729417db9c6ad52fTimo Sirainen increase the buffer size here, instead of crashing only randomly when the
a94f166ade968e8127b3eeda729417db9c6ad52fTimo Sirainen buffer needs to be increased. */
e07b8f034b4c214e94aa5eb93d96ccbe60094b4bAki Tuomi/* This will truncate your byte buffer to contain at most
e07b8f034b4c214e94aa5eb93d96ccbe60094b4bAki Tuomi given number of bits.
e07b8f034b4c214e94aa5eb93d96ccbe60094b4bAki Tuomi 1 bits: 01 00000001
e07b8f034b4c214e94aa5eb93d96ccbe60094b4bAki Tuomi 2 bits: 03 00000011
e07b8f034b4c214e94aa5eb93d96ccbe60094b4bAki Tuomi 3 bits: 07 00000111
e07b8f034b4c214e94aa5eb93d96ccbe60094b4bAki Tuomi 4 bits: 0f 00001111
e07b8f034b4c214e94aa5eb93d96ccbe60094b4bAki Tuomi 5 bits: 1f 00011111
e07b8f034b4c214e94aa5eb93d96ccbe60094b4bAki Tuomi 6 bits: 3f 00111111
e07b8f034b4c214e94aa5eb93d96ccbe60094b4bAki Tuomi 7 bits: 7f 01111111
e07b8f034b4c214e94aa5eb93d96ccbe60094b4bAki Tuomi 8 bits: ff 11111111
e07b8f034b4c214e94aa5eb93d96ccbe60094b4bAki Tuomi 9 bits: 01ff 0000000111111111
e07b8f034b4c214e94aa5eb93d96ccbe60094b4bAki Tuomi10 bits: 03ff 0000001111111111
e07b8f034b4c214e94aa5eb93d96ccbe60094b4bAki Tuomi11 bits: 07ff 0000011111111111
e07b8f034b4c214e94aa5eb93d96ccbe60094b4bAki Tuomi12 bits: 0fff 0000111111111111
e07b8f034b4c214e94aa5eb93d96ccbe60094b4bAki Tuomi13 bits: 1fff 0001111111111111
e07b8f034b4c214e94aa5eb93d96ccbe60094b4bAki Tuomi14 bits: 3fff 0011111111111111
e07b8f034b4c214e94aa5eb93d96ccbe60094b4bAki Tuomi15 bits: 7fff 0111111111111111
e07b8f034b4c214e94aa5eb93d96ccbe60094b4bAki Tuomi16 bits: ffff 1111111111111111
e07b8f034b4c214e94aa5eb93d96ccbe60094b4bAki Tuomi and so forth