Skip to content

Commit

Permalink
Update buffer.c(h)
Browse files Browse the repository at this point in the history
Signed-off-by: Jianhui Zhao <jianhuizhao329@gmail.com>
  • Loading branch information
Jianhui Zhao committed Sep 30, 2018
1 parent 3cda2be commit 6e1465b
Showing 1 changed file with 41 additions and 1 deletion.
42 changes: 41 additions & 1 deletion src/buffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@

#include <stdint.h>
#include <stdarg.h>
#include <stdbool.h>
#include <string.h>
#include <stdbool.h>
#include <sys/types.h>

/* Test for GCC < 2.96 */
Expand Down Expand Up @@ -235,6 +235,46 @@ static inline uint64_t buffer_pull_u64(struct buffer *b)
return val;
}

static inline uint8_t buffer_get_u8(struct buffer *b, ssize_t offset)
{
uint8_t val = 0;

if (likely(buffer_length(b) > offset))
val = b->data[offset];

return val;
}

static inline uint16_t buffer_get_u16(struct buffer *b, ssize_t offset)
{
uint16_t val = 0;

if (likely(buffer_length(b) > offset + 1))
val = *((uint16_t *)(b->data + offset));

return val;
}

static inline uint32_t buffer_get_u32(struct buffer *b, ssize_t offset)
{
uint32_t val = 0;

if (likely(buffer_length(b) > offset + 3))
val = *((uint32_t *)(b->data + offset));

return val;
}

static inline uint64_t buffer_get_u64(struct buffer *b, ssize_t offset)
{
uint64_t val = 0;

if (likely(buffer_length(b) > offset + 7))
val = *((uint64_t *)(b->data + offset));

return val;
}

int buffer_pull_to_fd(struct buffer *b, int fd, size_t len,
int (*wr)(int fd, void *buf, size_t count, void *arg), void *arg);

Expand Down

0 comments on commit 6e1465b

Please sign in to comment.