chibipub

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README | LICENSE

commit c56608039b134849dbb2f332e0b5d76621ea73a2
parent 2d18f6357c050bc811310b4ffeb97f10426e0283
Author: William Casarin <jb55@jb55.com>
Date:   Sun,  6 Dec 2020 12:12:37 -0800

inline cursor

Diffstat:
MMakefile | 4++--
Msrc/cursor.h | 183+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++--------
2 files changed, 168 insertions(+), 19 deletions(-)

diff --git a/Makefile b/Makefile @@ -1,7 +1,7 @@ -CFLAGS = -Wall -Werror -std=c89 -Isrc +CFLAGS = -Wall -Werror -std=c99 -Isrc -OBJS = src/cursor.o src/http.o src/base64.o +OBJS = src/http.o src/base64.o wolfsocks: src/wolfsocks.c $(OBJS) $(CC) $(CFLAGS) $^ $(LDFLAGS) -o $@ diff --git a/src/cursor.h b/src/cursor.h @@ -2,33 +2,182 @@ #ifndef WOLFSOCKS_CURSOR_H #define WOLFSOCKS_CURSOR_H +#include <stdio.h> +#include <string.h> + +#define unlikely(x) __builtin_expect((x),0) + struct cursor { unsigned char *start; unsigned char *p; unsigned char *end; }; +static inline int cursor_eof(struct cursor *c) +{ + return c->p == c->end; +} + +static inline void *cursor_alloc(struct cursor *mem, unsigned long size) +{ + void *ret; + + if (mem->p + size > mem->end) { + return NULL; + } + + ret = mem->p; + memset(ret, 0, size); + mem->p += size; + + return ret; +} + +static inline void copy_cursor(struct cursor *src, struct cursor *dest) +{ + dest->start = src->start; + dest->p = src->p; + dest->end = src->end; +} + +static inline void make_cursor(unsigned char *start, unsigned char *end, struct cursor *cursor) +{ + cursor->start = start; + cursor->p = start; + cursor->end = end; +} + +static inline int cursor_peek_byte(struct cursor *cursor, unsigned char *c, int offset) +{ + if (unlikely(cursor->p + offset >= cursor->end)) + return 0; + *c = *(cursor->p + offset); + return 1; +} + +static inline int cursor_index(struct cursor *cursor, int elem_size) +{ + return (cursor->p - cursor->start) / elem_size; +} + + +static inline int pull_byte(struct cursor *cursor, unsigned char *c) +{ + if (unlikely(cursor->p + 1 > cursor->end)) + return 0; + + *c = *cursor->p; + cursor->p++; + + return 1; +} + + +static inline int push_byte(struct cursor *cursor, unsigned char c) +{ + if (unlikely(cursor->p + 1 > cursor->end)) { + return 0; + } + + *cursor->p = c; + cursor->p++; + + return 1; +} + +static inline int pull_data(struct cursor *cursor, unsigned char *data, int len) +{ + if (unlikely(cursor->p + len > cursor->end)) { + return 0; + } + + memcpy(data, cursor->p, len); + cursor->p += len; + + return 1; +} + +static inline int pull_data_into_cursor(struct cursor *cursor, + struct cursor *dest, + unsigned char **data, + int len) +{ + int ok; + + if (unlikely(dest->p + len > dest->end)) { + printf("not enough room in dest buffer\n"); + return 0; + } + + ok = pull_data(cursor, dest->p, len); + if (!ok) return 0; + + *data = dest->p; + dest->p += len; + + return 1; +} + + +static inline int push_data(struct cursor *cursor, unsigned char *data, int len) +{ + if (unlikely(cursor->p + len > cursor->end)) { + printf("push_data oob\n"); + return 0; + } + + memcpy(cursor->p, data, len); + cursor->p += len; + + return 1; +} + +static inline int push_int(struct cursor *cursor, int i) +{ + return push_data(cursor, (unsigned char*)&i, sizeof(i)); +} + + +static inline int pull_int(struct cursor *cursor, int *i) +{ + return pull_data(cursor, (unsigned char*)i, sizeof(*i)); +} + +static inline int push_u16(struct cursor *cursor, unsigned short i) +{ + return push_data(cursor, (unsigned char*)&i, sizeof(i)); +} + +static inline void *index_cursor(struct cursor *cursor, unsigned short index, int elem_size) +{ + unsigned char *p; + p = &cursor->start[elem_size * index]; + + if (unlikely(p > cursor->end)) + return NULL; + + return (void*)p; +} -void copy_cursor(struct cursor *src, struct cursor *dest); -int cursor_index(struct cursor *cursor, int elem_size); -void make_cursor(unsigned char *start, unsigned char *end, struct cursor *cursor); -int cursor_remaining_capacity(struct cursor *cursor); -void *index_cursor(struct cursor *cursor, unsigned short index, int elem_size); -int push_u16(struct cursor *cursor, unsigned short i); -int push_int(struct cursor *cursor, int i); -int push_data(struct cursor *cursor, unsigned char *data, int len); -int push_byte(struct cursor *cursor, unsigned char c); +static inline int push_sized_str(struct cursor *cursor, const char *str, int len) +{ + return push_data(cursor, (unsigned char*)str, len); +} -int pull_data_into_cursor(struct cursor *cursor, struct cursor *dest, unsigned char **data, int len); -int pull_data(struct cursor *cursor, unsigned char *data, int len); -int pull_byte(struct cursor *cursor, unsigned char *c); -int pull_int(struct cursor *cursor, int *i); +static inline int push_str(struct cursor *cursor, const char *str) +{ + return push_data(cursor, (unsigned char*)str, strlen(str)); +} -int push_prefixed_str(struct cursor *cursor, const char *str); -int push_str(struct cursor *cursor, const char *str); -int push_sized_str(struct cursor *cursor, const char *str, int len); +static inline int push_c_str(struct cursor *cursor, const char *str) +{ + return push_str(cursor, str) && push_byte(cursor, 0); +} -int pull_prefixed_str(struct cursor *cursor, struct cursor *dest_buf, const char **str); +static inline int cursor_remaining_capacity(struct cursor *cursor) +{ + return cursor->end - cursor->p; +} #endif