From de665f7cf9324a10be0b904a4174fe2d46c5fc51 Mon Sep 17 00:00:00 2001 From: jifl Date: Mon, 26 Feb 2007 19:49:49 +0000 Subject: [PATCH] 2007-02-26 Jonathan Larmour (based on patch from Simon Goldschmidt) * api_lib.c: Use memcpy in netbuf_copy_partial. --- CHANGELOG | 3 +++ src/api/api_lib.c | 25 ++++++++++++++----------- 2 files changed, 17 insertions(+), 11 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index 7aa5609b..3a607f4b 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -27,6 +27,9 @@ HISTORY ++ New features: + 2007-02-26 Jonathan Larmour (based on patch from Simon Goldschmidt) + * api_lib.c: Use memcpy in netbuf_copy_partial. + ++ Bug fixes: (STABLE-1.2.0) diff --git a/src/api/api_lib.c b/src/api/api_lib.c index 01683b0d..0d7dd206 100644 --- a/src/api/api_lib.c +++ b/src/api/api_lib.c @@ -150,7 +150,8 @@ void netbuf_copy_partial(struct netbuf *buf, void *dataptr, u16_t len, u16_t offset) { struct pbuf *p; - u16_t i, left; + u16_t left; + u16_t buf_copy_len; left = 0; @@ -158,18 +159,20 @@ netbuf_copy_partial(struct netbuf *buf, void *dataptr, u16_t len, u16_t offset) return; } - /* This implementation is bad. It should use bcopy - instead. */ - for(p = buf->p; left < len && p != NULL; p = p->next) { + /* Note some systems use byte copy if dataptr or one of the pbuf payload pointers are unaligned. */ + for(p = buf->p; len != 0 && p != NULL; p = p->next) { if (offset != 0 && offset >= p->len) { + /* don't copy from this buffer -> on to the next */ offset -= p->len; - } else { - for(i = offset; i < p->len; ++i) { - ((u8_t *)dataptr)[left] = ((u8_t *)p->payload)[i]; - if (++left >= len) { - return; - } - } + } else { + /* copy from this buffer. maybe only partially. */ + buf_copy_len = p->len - offset; + if (buf_copy_len > len) + buf_copy_len = len; + /* copy the necessary parts of the buffer */ + memcpy(&((char*)dataptr)[left], &((char*)p->payload)[offset], buf_copy_len); + left += buf_copy_len; + len -= buf_copy_len; offset = 0; } }