From 5fa6912da86d8f264a23c9545c1a877a31603a49 Mon Sep 17 00:00:00 2001 From: Joshua Haberman Date: Wed, 29 Jul 2009 20:36:08 -0700 Subject: Fleshed out serialization functions a bit more. --- src/upb_serialize.h | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) (limited to 'src/upb_serialize.h') diff --git a/src/upb_serialize.h b/src/upb_serialize.h index 39dc79c..de5e57d 100644 --- a/src/upb_serialize.h +++ b/src/upb_serialize.h @@ -33,6 +33,14 @@ extern "C" { INLINE upb_status_t upb_put_v_uint64_t(uint8_t *buf, uint8_t *end, uint64_t val, uint8_t **outbuf) { + do { + uint8_t byte = val & 0x7f; + val >>= 7; + if(val) byte |= 0x80; + if(buf >= end) return UPB_STATUS_NEED_MORE_DATA; + *buf++ = byte; + } while(val); + *outbuf = buf; return UPB_STATUS_OK; } @@ -47,6 +55,10 @@ INLINE upb_status_t upb_put_v_uint32_t(uint8_t *buf, uint8_t *end, INLINE upb_status_t upb_put_f_uint32_t(uint8_t *buf, uint8_t *end, uint32_t val, uint8_t **outbuf) { + uint8_t *uint32_end = buf + sizeof(uint32_t); + if(uint32_end > end) return UPB_STATUS_NEED_MORE_DATA; + *(uint32_t*)buf = val; + *outbuf = uint32_end; return UPB_STATUS_OK; } @@ -54,6 +66,10 @@ INLINE upb_status_t upb_put_f_uint32_t(uint8_t *buf, uint8_t *end, INLINE upb_status_t upb_put_f_uint64_t(uint8_t *buf, uint8_t *end, uint64_t val, uint8_t **outbuf) { + uint8_t *uint64_end = buf + sizeof(uint64_t); + if(uint64_end > end) return UPB_STATUS_NEED_MORE_DATA; + *(uint64_t*)buf = val; + *outbuf = uint64_end; return UPB_STATUS_OK; } -- cgit v1.2.3