From 972b502ecaf3b919b7e89c13257dd6ec97aaafb7 Mon Sep 17 00:00:00 2001 From: Harald Welte Date: Sat, 8 Sep 2012 19:58:59 +0200 Subject: msgb: msgb_get() is supposed to get bytes from END, msgb_pull() from HEAD msgb_get() has been wrong all the time, despite the documentation being correct. If you've used the broken msgb_get() before, you have to change your code now, sorry. --- include/osmocom/core/msgb.h | 37 ++++++++++++++++++++++++++++++++++--- 1 file changed, 34 insertions(+), 3 deletions(-) diff --git a/include/osmocom/core/msgb.h b/include/osmocom/core/msgb.h index 5457a07d..36c7c0f9 100644 --- a/include/osmocom/core/msgb.h +++ b/include/osmocom/core/msgb.h @@ -72,6 +72,7 @@ extern void msgb_free(struct msgb *m); extern void msgb_enqueue(struct llist_head *queue, struct msgb *msg); extern struct msgb *msgb_dequeue(struct llist_head *queue); extern void msgb_reset(struct msgb *m); +uint16_t msgb_length(const struct msgb *msg); #ifdef MSGB_DEBUG #include @@ -226,8 +227,11 @@ static inline void msgb_put_u32(struct msgb *msgb, uint32_t word) */ static inline unsigned char *msgb_get(struct msgb *msgb, unsigned int len) { - unsigned char *tmp = msgb->data; - msgb->data += len; + unsigned char *tmp = msgb->data - len; + if (msgb_length(msgb) < len) + MSGB_ABORT(msgb, "msgb too small to get %u (len %u)\n", + len, msgb_length(msgb)); + msgb->tail -= len; msgb->len -= len; return tmp; } @@ -295,6 +299,34 @@ static inline unsigned char *msgb_pull(struct msgb *msgb, unsigned int len) return msgb->data += len; } +/*! \brief remove uint8 from front of message + * \param[in] msgb message buffer + * \returns 8bit value taken from end of msgb + */ +static inline uint8_t msgb_pull_u8(struct msgb *msgb) +{ + uint8_t *space = msgb_pull(msgb, 1); + return space[0]; +} +/*! \brief remove uint16 from front of message + * \param[in] msgb message buffer + * \returns 16bit value taken from end of msgb + */ +static inline uint16_t msgb_pull_u16(struct msgb *msgb) +{ + uint8_t *space = msgb_pull(msgb, 2); + return space[0] << 8 | space[1]; +} +/*! \brief remove uint32 from front of message + * \param[in] msgb message buffer + * \returns 32bit value taken from end of msgb + */ +static inline uint32_t msgb_pull_u32(struct msgb *msgb) +{ + uint8_t *space = msgb_pull(msgb, 4); + return space[0] << 24 | space[1] << 16 | space[2] << 8 | space[3]; +} + /*! \brief Increase headroom of empty msgb, reducing the tailroom * \param[in] msg message buffer * \param[in] len amount of extra octets to be reserved as headroom @@ -362,7 +394,6 @@ static inline struct msgb *msgb_alloc_headroom(int size, int headroom, /* non inline functions to ease binding */ uint8_t *msgb_data(const struct msgb *msg); -uint16_t msgb_length(const struct msgb *msg); void msgb_set_talloc_ctx(void *ctx); /*! @} */ -- cgit v1.2.3