- dprintf("s[%d] -> d[%d] (%d)\n", cbuf->start, 0,
- cbuf->maxlen - cbuf->start);
- dprintf("s[%d] -> d[%d] (%d)\n", 0,cbuf->maxlen - cbuf->start,
- n - cbuf->maxlen + cbuf->start);
- memcpy(c, cbuf->buf + cbuf->start , cbuf->maxlen - cbuf->start);
- memcpy(c + cbuf->maxlen - cbuf->start, cbuf->buf,
- n - cbuf->maxlen + cbuf->start);
+ /* check if we need to go from end to the beginning */
+ if (n <= cbuf->maxlen - cbuf->start) {
+ dprintf("s[%d] -> d[%d] (%d)\n", 0, cbuf->start, n);
+ memcpy(c, cbuf->buf + cbuf->start , n);
+ }
+ else {
+ dprintf("s[%d] -> d[%d] (%d)\n", cbuf->start, 0,
+ cbuf->maxlen - cbuf->start);
+ dprintf("s[%d] -> d[%d] (%d)\n", 0, cbuf->maxlen - cbuf->start,
+ n - cbuf->maxlen + cbuf->start);
+ memcpy(c, cbuf->buf + cbuf->start , cbuf->maxlen - cbuf->start);
+ memcpy(c + cbuf->maxlen - cbuf->start, cbuf->buf,
+ n - cbuf->maxlen + cbuf->start);
+ }