| 359 | /* convert to buffer */ |
| 360 | |
| 361 | int |
| 362 | cirbuf_get_buf_tail(struct cirbuf *cbuf, char *c, unsigned int size) |
| 363 | { |
| 364 | unsigned int n; |
| 365 | |
| 366 | if (!cbuf || !c) |
| 367 | return -EINVAL; |
| 368 | |
| 369 | n = (size < CIRBUF_GET_LEN(cbuf)) ? size : CIRBUF_GET_LEN(cbuf); |
| 370 | |
| 371 | if (!n) |
| 372 | return 0; |
| 373 | |
| 374 | if (cbuf->start <= cbuf->end) { |
| 375 | dprintf("s[%d] -> d[%d] (%d)\n", cbuf->end - n + 1, 0, n); |
| 376 | memcpy(c, cbuf->buf + cbuf->end - n + 1, n); |
| 377 | } |
| 378 | else { |
| 379 | /* check if we need to go from end to the beginning */ |
| 380 | if (n <= cbuf->end + 1) { |
| 381 | dprintf("s[%d] -> d[%d] (%d)\n", 0, cbuf->end - n + 1, n); |
| 382 | memcpy(c, cbuf->buf + cbuf->end - n + 1, n); |
| 383 | } |
| 384 | else { |
| 385 | dprintf("s[%d] -> d[%d] (%d)\n", 0, |
| 386 | cbuf->maxlen - cbuf->start, cbuf->end + 1); |
| 387 | dprintf("s[%d] -> d[%d] (%d)\n", |
| 388 | cbuf->maxlen - n + cbuf->end + 1, 0, n - cbuf->end - 1); |
| 389 | memcpy(c + cbuf->maxlen - cbuf->start, |
| 390 | cbuf->buf, cbuf->end + 1); |
| 391 | memcpy(c, cbuf->buf + cbuf->maxlen - n + cbuf->end +1, |
| 392 | n - cbuf->end - 1); |
| 393 | } |
| 394 | } |
| 395 | return n; |
| 396 | } |
| 397 | |
| 398 | /* get head or get tail */ |
| 399 | |