mbuf: additional functions
authorMarko Kreen <markokr@gmail.com>
Wed, 13 Jan 2010 07:58:05 +0000 (09:58 +0200)
committerMarko Kreen <markokr@gmail.com>
Wed, 13 Jan 2010 07:58:05 +0000 (09:58 +0200)
- mbuf_get_char
- mbuf_get_chars

same as _byte/_bytes but for char type

- mbuf_get_uint64be
- mbuf_slice
- mbuf_copy

usual/mbuf.h

index 789d83c38eccc70834807a1c68e422cbdc061b17..164a02d7acdbcf38743aae92c9572420ef1075a1 100644 (file)
@@ -137,6 +137,15 @@ static inline bool mbuf_get_byte(struct MBuf *buf, uint8_t *dst_p)
        return true;
 }
 
+_MUSTCHECK
+static inline bool mbuf_get_char(struct MBuf *buf, char *dst_p)
+{
+       if (buf->read_pos + 1 > buf->write_pos)
+               return false;
+       *dst_p = buf->data[buf->read_pos++];
+       return true;
+}
+
 _MUSTCHECK
 static inline bool mbuf_get_uint16be(struct MBuf *buf, uint16_t *dst_p)
 {
@@ -163,6 +172,17 @@ static inline bool mbuf_get_uint32be(struct MBuf *buf, uint32_t *dst_p)
        return true;
 }
 
+_MUSTCHECK
+static inline bool mbuf_get_uint64be(struct MBuf *buf, uint64_t *dst_p)
+{
+       uint32_t a, b;
+       if (!mbuf_get_uint32be(buf, &a)
+           || !mbuf_get_uint32be(buf, &b))
+               return false;
+       *dst_p = ((uint64_t)a << 32) | b;
+       return true;
+}
+
 _MUSTCHECK
 static inline bool mbuf_get_bytes(struct MBuf *buf, unsigned len, const uint8_t **dst_p)
 {
@@ -173,6 +193,16 @@ static inline bool mbuf_get_bytes(struct MBuf *buf, unsigned len, const uint8_t
        return true;
 }
 
+_MUSTCHECK
+static inline bool mbuf_get_chars(struct MBuf *buf, unsigned len, const char **dst_p)
+{
+       if (buf->read_pos + len > buf->write_pos)
+               return false;
+       *dst_p = (char *)buf->data + buf->read_pos;
+       buf->read_pos += len;
+       return true;
+}
+
 _MUSTCHECK
 static inline bool mbuf_get_string(struct MBuf *buf, const char **dst_p)
 {
@@ -261,5 +291,20 @@ static inline bool mbuf_cut(struct MBuf *buf, unsigned ofs, unsigned len)
        return true;
 }
 
+static inline void mbuf_copy(const struct MBuf *src, struct MBuf *dst)
+{
+       *dst = *src;
+}
+
+_MUSTCHECK
+static inline bool mbuf_slice(struct MBuf *src, unsigned len, struct MBuf *dst)
+{
+       if (len > mbuf_avail_for_read(src))
+               return false;
+       mbuf_init_fixed_reader(dst, src->data + src->read_pos, len);
+       src->read_pos += len;
+       return true;
+}
+
 #endif