X-Git-Url: https://review.openocd.org/gitweb?a=blobdiff_plain;f=src%2Fhelper%2Fbinarybuffer.c;h=8275d12476ad40ad55eacb1e9d188c8bb0cc5b32;hb=d8d8c5d8c3e47a38059952c3407bb819c5c33d05;hp=865d3a3725b10bda5af219836d73e64b1eb2ce43;hpb=e4ee891759b08d3edb258b34f00b4ae8e3298d06;p=openocd.git diff --git a/src/helper/binarybuffer.c b/src/helper/binarybuffer.c index 865d3a3725..8275d12476 100644 --- a/src/helper/binarybuffer.c +++ b/src/helper/binarybuffer.c @@ -48,21 +48,22 @@ const unsigned char bit_reverse_table256[] = }; -uint8_t* buf_cpy(const uint8_t *from, uint8_t *to, int size) +void* buf_cpy(const void *from, void *_to, unsigned size) { - if (from == NULL) + if (NULL == from || NULL == _to) return NULL; - for (unsigned i = 0, num_bytes = CEIL(size, 8); i < num_bytes; i++) - to[i] = from[i]; + // copy entire buffer + memcpy(_to, from, CEIL(size, 8)); /* mask out bits that don't belong to the buffer */ - if (size % 8) + unsigned trailing_bits = size % 8; + if (trailing_bits) { - to[size / 8] &= (0xff >> (8 - (size % 8))); + uint8_t *to = _to; + to[size / 8] &= (1 << trailing_bits) - 1; } - - return to; + return _to; } static bool buf_cmp_masked(uint8_t a, uint8_t b, uint8_t m) @@ -112,17 +113,17 @@ bool buf_cmp_mask(const void *_buf1, const void *_buf2, } -uint8_t* buf_set_ones(uint8_t *buf, int count) +void* buf_set_ones(void *_buf, unsigned size) { - for (unsigned i = 0, num_bytes = CEIL(count, 8); i < num_bytes; i++) - { - if (count >= 8) - buf[i] = 0xff; - else - buf[i] = (1 << count) - 1; + uint8_t *buf = _buf; + if (!buf) + return NULL; - count -= 8; - } + memset(buf, 0xff, size / 8); + + unsigned trailing_bits = size % 8; + if (trailing_bits) + buf[size / 8] = (1 << trailing_bits) - 1; return buf; }