15129044dSC.J. Collier/*-
25129044dSC.J. Collier *   BSD LICENSE
35129044dSC.J. Collier *
45129044dSC.J. Collier *   Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
55129044dSC.J. Collier *   All rights reserved.
65129044dSC.J. Collier *
75129044dSC.J. Collier *   Redistribution and use in source and binary forms, with or without
85129044dSC.J. Collier *   modification, are permitted provided that the following conditions
95129044dSC.J. Collier *   are met:
105129044dSC.J. Collier *
115129044dSC.J. Collier *     * Redistributions of source code must retain the above copyright
125129044dSC.J. Collier *       notice, this list of conditions and the following disclaimer.
135129044dSC.J. Collier *     * Redistributions in binary form must reproduce the above copyright
145129044dSC.J. Collier *       notice, this list of conditions and the following disclaimer in
155129044dSC.J. Collier *       the documentation and/or other materials provided with the
165129044dSC.J. Collier *       distribution.
175129044dSC.J. Collier *     * Neither the name of Intel Corporation nor the names of its
185129044dSC.J. Collier *       contributors may be used to endorse or promote products derived
195129044dSC.J. Collier *       from this software without specific prior written permission.
205129044dSC.J. Collier *
215129044dSC.J. Collier *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
225129044dSC.J. Collier *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
235129044dSC.J. Collier *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
245129044dSC.J. Collier *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
255129044dSC.J. Collier *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
265129044dSC.J. Collier *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
275129044dSC.J. Collier *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
285129044dSC.J. Collier *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
295129044dSC.J. Collier *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
305129044dSC.J. Collier *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
315129044dSC.J. Collier *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
325129044dSC.J. Collier */
335129044dSC.J. Collier
345129044dSC.J. Collier/*
355129044dSC.J. Collier * Copyright (c) 2009, Olivier MATZ <zer0@droids-corp.org>
365129044dSC.J. Collier * All rights reserved.
375129044dSC.J. Collier * Redistribution and use in source and binary forms, with or without
385129044dSC.J. Collier * modification, are permitted provided that the following conditions are met:
395129044dSC.J. Collier *
405129044dSC.J. Collier *     * Redistributions of source code must retain the above copyright
415129044dSC.J. Collier *       notice, this list of conditions and the following disclaimer.
425129044dSC.J. Collier *     * Redistributions in binary form must reproduce the above copyright
435129044dSC.J. Collier *       notice, this list of conditions and the following disclaimer in the
445129044dSC.J. Collier *       documentation and/or other materials provided with the distribution.
455129044dSC.J. Collier *     * Neither the name of the University of California, Berkeley nor the
465129044dSC.J. Collier *       names of its contributors may be used to endorse or promote products
475129044dSC.J. Collier *       derived from this software without specific prior written permission.
485129044dSC.J. Collier *
495129044dSC.J. Collier * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND ANY
505129044dSC.J. Collier * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
515129044dSC.J. Collier * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
525129044dSC.J. Collier * DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS BE LIABLE FOR ANY
535129044dSC.J. Collier * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
545129044dSC.J. Collier * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
555129044dSC.J. Collier * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
565129044dSC.J. Collier * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
575129044dSC.J. Collier * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
585129044dSC.J. Collier * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
595129044dSC.J. Collier */
605129044dSC.J. Collier
615129044dSC.J. Collier#include <string.h>
625129044dSC.J. Collier#include <errno.h>
635129044dSC.J. Collier#include <stdio.h>
645129044dSC.J. Collier
655129044dSC.J. Collier#include "cmdline_cirbuf.h"
665129044dSC.J. Collier
675129044dSC.J. Collier
685129044dSC.J. Collierint
695129044dSC.J. Colliercirbuf_init(struct cirbuf *cbuf, char *buf, unsigned int start, unsigned int maxlen)
705129044dSC.J. Collier{
715129044dSC.J. Collier	if (!cbuf || !buf)
725129044dSC.J. Collier		return -EINVAL;
735129044dSC.J. Collier	cbuf->maxlen = maxlen;
745129044dSC.J. Collier	cbuf->len = 0;
755129044dSC.J. Collier	cbuf->start = start;
765129044dSC.J. Collier	cbuf->end = start;
775129044dSC.J. Collier	cbuf->buf = buf;
785129044dSC.J. Collier	return 0;
795129044dSC.J. Collier}
805129044dSC.J. Collier
815129044dSC.J. Collier/* multiple add */
825129044dSC.J. Collier
835129044dSC.J. Collierint
845129044dSC.J. Colliercirbuf_add_buf_head(struct cirbuf *cbuf, const char *c, unsigned int n)
855129044dSC.J. Collier{
865129044dSC.J. Collier	unsigned int e;
875129044dSC.J. Collier
885129044dSC.J. Collier	if (!cbuf || !c || !n || n > CIRBUF_GET_FREELEN(cbuf))
895129044dSC.J. Collier		return -EINVAL;
905129044dSC.J. Collier
915129044dSC.J. Collier	e = CIRBUF_IS_EMPTY(cbuf) ? 1 : 0;
925129044dSC.J. Collier
935129044dSC.J. Collier	if (n < cbuf->start + e) {
945129044dSC.J. Collier		dprintf("s[%d] -> d[%d] (%d)\n", 0, cbuf->start - n + e, n);
955129044dSC.J. Collier		memcpy(cbuf->buf + cbuf->start - n + e, c, n);
965129044dSC.J. Collier	}
975129044dSC.J. Collier	else {
985129044dSC.J. Collier		dprintf("s[%d] -> d[%d] (%d)\n", + n - (cbuf->start + e), 0,
995129044dSC.J. Collier			cbuf->start + e);
1005129044dSC.J. Collier		dprintf("s[%d] -> d[%d] (%d)\n", cbuf->maxlen - n +
1015129044dSC.J. Collier			(cbuf->start + e), 0, n - (cbuf->start + e));
1025129044dSC.J. Collier		memcpy(cbuf->buf, c  + n - (cbuf->start + e) , cbuf->start + e);
1035129044dSC.J. Collier		memcpy(cbuf->buf + cbuf->maxlen - n + (cbuf->start + e), c,
1045129044dSC.J. Collier		       n - (cbuf->start + e));
1055129044dSC.J. Collier	}
1065129044dSC.J. Collier	cbuf->len += n;
1075129044dSC.J. Collier	cbuf->start += (cbuf->maxlen - n + e);
1085129044dSC.J. Collier	cbuf->start %= cbuf->maxlen;
1095129044dSC.J. Collier	return n;
1105129044dSC.J. Collier}
1115129044dSC.J. Collier
1125129044dSC.J. Collier/* multiple add */
1135129044dSC.J. Collier
1145129044dSC.J. Collierint
1155129044dSC.J. Colliercirbuf_add_buf_tail(struct cirbuf *cbuf, const char *c, unsigned int n)
1165129044dSC.J. Collier{
1175129044dSC.J. Collier	unsigned int e;
1185129044dSC.J. Collier
1195129044dSC.J. Collier	if (!cbuf || !c || !n || n > CIRBUF_GET_FREELEN(cbuf))
1205129044dSC.J. Collier		return -EINVAL;
1215129044dSC.J. Collier
1225129044dSC.J. Collier	e = CIRBUF_IS_EMPTY(cbuf) ? 1 : 0;
1235129044dSC.J. Collier
1245129044dSC.J. Collier	if (n < cbuf->maxlen - cbuf->end - 1 + e) {
1255129044dSC.J. Collier		dprintf("s[%d] -> d[%d] (%d)\n", 0, cbuf->end + !e, n);
1265129044dSC.J. Collier		memcpy(cbuf->buf + cbuf->end + !e, c, n);
1275129044dSC.J. Collier	}
1285129044dSC.J. Collier	else {
1295129044dSC.J. Collier		dprintf("s[%d] -> d[%d] (%d)\n", cbuf->end + !e, 0,
1305129044dSC.J. Collier			cbuf->maxlen - cbuf->end - 1 + e);
1315129044dSC.J. Collier		dprintf("s[%d] -> d[%d] (%d)\n", cbuf->maxlen - cbuf->end - 1 +
1325129044dSC.J. Collier			e, 0, n - cbuf->maxlen + cbuf->end + 1 - e);
1335129044dSC.J. Collier		memcpy(cbuf->buf + cbuf->end + !e, c, cbuf->maxlen -
1345129044dSC.J. Collier		       cbuf->end - 1 + e);
1355129044dSC.J. Collier		memcpy(cbuf->buf, c + cbuf->maxlen - cbuf->end - 1 + e,
1365129044dSC.J. Collier		       n - cbuf->maxlen + cbuf->end + 1 - e);
1375129044dSC.J. Collier	}
1385129044dSC.J. Collier	cbuf->len += n;
1395129044dSC.J. Collier	cbuf->end += n - e;
1405129044dSC.J. Collier	cbuf->end %= cbuf->maxlen;
1415129044dSC.J. Collier	return n;
1425129044dSC.J. Collier}
1435129044dSC.J. Collier
1445129044dSC.J. Collier/* add at head */
1455129044dSC.J. Collier
1465129044dSC.J. Collierstatic inline void
1475129044dSC.J. Collier__cirbuf_add_head(struct cirbuf * cbuf, char c)
1485129044dSC.J. Collier{
1495129044dSC.J. Collier	if (!CIRBUF_IS_EMPTY(cbuf)) {
1505129044dSC.J. Collier		cbuf->start += (cbuf->maxlen - 1);
1515129044dSC.J. Collier		cbuf->start %= cbuf->maxlen;
1525129044dSC.J. Collier	}
1535129044dSC.J. Collier	cbuf->buf[cbuf->start] = c;
1545129044dSC.J. Collier	cbuf->len ++;
1555129044dSC.J. Collier}
1565129044dSC.J. Collier
1575129044dSC.J. Collierint
1585129044dSC.J. Colliercirbuf_add_head_safe(struct cirbuf * cbuf, char c)
1595129044dSC.J. Collier{
1605129044dSC.J. Collier	if (cbuf && !CIRBUF_IS_FULL(cbuf)) {
1615129044dSC.J. Collier		__cirbuf_add_head(cbuf, c);
1625129044dSC.J. Collier		return 0;
1635129044dSC.J. Collier	}
1645129044dSC.J. Collier	return -EINVAL;
1655129044dSC.J. Collier}
1665129044dSC.J. Collier
1675129044dSC.J. Colliervoid
1685129044dSC.J. Colliercirbuf_add_head(struct cirbuf * cbuf, char c)
1695129044dSC.J. Collier{
1705129044dSC.J. Collier	__cirbuf_add_head(cbuf, c);
1715129044dSC.J. Collier}
1725129044dSC.J. Collier
1735129044dSC.J. Collier/* add at tail */
1745129044dSC.J. Collier
1755129044dSC.J. Collierstatic inline void
1765129044dSC.J. Collier__cirbuf_add_tail(struct cirbuf * cbuf, char c)
1775129044dSC.J. Collier{
1785129044dSC.J. Collier	if (!CIRBUF_IS_EMPTY(cbuf)) {
1795129044dSC.J. Collier		cbuf->end ++;
1805129044dSC.J. Collier		cbuf->end %= cbuf->maxlen;
1815129044dSC.J. Collier	}
1825129044dSC.J. Collier	cbuf->buf[cbuf->end] = c;
1835129044dSC.J. Collier	cbuf->len ++;
1845129044dSC.J. Collier}
1855129044dSC.J. Collier
1865129044dSC.J. Collierint
1875129044dSC.J. Colliercirbuf_add_tail_safe(struct cirbuf * cbuf, char c)
1885129044dSC.J. Collier{
1895129044dSC.J. Collier	if (cbuf && !CIRBUF_IS_FULL(cbuf)) {
1905129044dSC.J. Collier		__cirbuf_add_tail(cbuf, c);
1915129044dSC.J. Collier		return 0;
1925129044dSC.J. Collier	}
1935129044dSC.J. Collier	return -EINVAL;
1945129044dSC.J. Collier}
1955129044dSC.J. Collier
1965129044dSC.J. Colliervoid
1975129044dSC.J. Colliercirbuf_add_tail(struct cirbuf * cbuf, char c)
1985129044dSC.J. Collier{
1995129044dSC.J. Collier	__cirbuf_add_tail(cbuf, c);
2005129044dSC.J. Collier}
2015129044dSC.J. Collier
2025129044dSC.J. Collier
2035129044dSC.J. Collierstatic inline void
2045129044dSC.J. Collier__cirbuf_shift_left(struct cirbuf *cbuf)
2055129044dSC.J. Collier{
2065129044dSC.J. Collier	unsigned int i;
2075129044dSC.J. Collier	char tmp = cbuf->buf[cbuf->start];
2085129044dSC.J. Collier
2095129044dSC.J. Collier	for (i=0 ; i<cbuf->len ; i++) {
2105129044dSC.J. Collier		cbuf->buf[(cbuf->start+i)%cbuf->maxlen] =
2115129044dSC.J. Collier			cbuf->buf[(cbuf->start+i+1)%cbuf->maxlen];
2125129044dSC.J. Collier	}
2135129044dSC.J. Collier	cbuf->buf[(cbuf->start-1+cbuf->maxlen)%cbuf->maxlen] = tmp;
2145129044dSC.J. Collier	cbuf->start += (cbuf->maxlen - 1);
2155129044dSC.J. Collier	cbuf->start %= cbuf->maxlen;
2165129044dSC.J. Collier	cbuf->end += (cbuf->maxlen - 1);
2175129044dSC.J. Collier	cbuf->end %= cbuf->maxlen;
2185129044dSC.J. Collier}
2195129044dSC.J. Collier
2205129044dSC.J. Collierstatic inline void
2215129044dSC.J. Collier__cirbuf_shift_right(struct cirbuf *cbuf)
2225129044dSC.J. Collier{
2235129044dSC.J. Collier	unsigned int i;
2245129044dSC.J. Collier	char tmp = cbuf->buf[cbuf->end];
2255129044dSC.J. Collier
2265129044dSC.J. Collier	for (i=0 ; i<cbuf->len ; i++) {
2275129044dSC.J. Collier		cbuf->buf[(cbuf->end+cbuf->maxlen-i)%cbuf->maxlen] =
2285129044dSC.J. Collier			cbuf->buf[(cbuf->end+cbuf->maxlen-i-1)%cbuf->maxlen];
2295129044dSC.J. Collier	}
2305129044dSC.J. Collier	cbuf->buf[(cbuf->end+1)%cbuf->maxlen] = tmp;
2315129044dSC.J. Collier	cbuf->start += 1;
2325129044dSC.J. Collier	cbuf->start %= cbuf->maxlen;
2335129044dSC.J. Collier	cbuf->end += 1;
2345129044dSC.J. Collier	cbuf->end %= cbuf->maxlen;
2355129044dSC.J. Collier}
2365129044dSC.J. Collier
2375129044dSC.J. Collier/* XXX we could do a better algorithm here... */
2385129044dSC.J. Collierint
2395129044dSC.J. Colliercirbuf_align_left(struct cirbuf * cbuf)
2405129044dSC.J. Collier{
2415129044dSC.J. Collier	if (!cbuf)
2425129044dSC.J. Collier		return -EINVAL;
2435129044dSC.J. Collier
2445129044dSC.J. Collier	if (cbuf->start < cbuf->maxlen/2) {
2455129044dSC.J. Collier		while (cbuf->start != 0) {
2465129044dSC.J. Collier			__cirbuf_shift_left(cbuf);
2475129044dSC.J. Collier		}
2485129044dSC.J. Collier	}
2495129044dSC.J. Collier	else {
2505129044dSC.J. Collier		while (cbuf->start != 0) {
2515129044dSC.J. Collier			__cirbuf_shift_right(cbuf);
2525129044dSC.J. Collier		}
2535129044dSC.J. Collier	}
2545129044dSC.J. Collier
2555129044dSC.J. Collier	return 0;
2565129044dSC.J. Collier}
2575129044dSC.J. Collier
2585129044dSC.J. Collier/* XXX we could do a better algorithm here... */
2595129044dSC.J. Collierint
2605129044dSC.J. Colliercirbuf_align_right(struct cirbuf * cbuf)
2615129044dSC.J. Collier{
2625129044dSC.J. Collier	if (!cbuf)
2635129044dSC.J. Collier		return -EINVAL;
2645129044dSC.J. Collier
2655129044dSC.J. Collier	if (cbuf->start >= cbuf->maxlen/2) {
2665129044dSC.J. Collier		while (cbuf->end != cbuf->maxlen-1) {
2675129044dSC.J. Collier			__cirbuf_shift_left(cbuf);
2685129044dSC.J. Collier		}
2695129044dSC.J. Collier	}
2705129044dSC.J. Collier	else {
2715129044dSC.J. Collier		while (cbuf->start != cbuf->maxlen-1) {
2725129044dSC.J. Collier			__cirbuf_shift_right(cbuf);
2735129044dSC.J. Collier		}
2745129044dSC.J. Collier	}
2755129044dSC.J. Collier
2765129044dSC.J. Collier	return 0;
2775129044dSC.J. Collier}
2785129044dSC.J. Collier
2795129044dSC.J. Collier/* buffer del */
2805129044dSC.J. Collier
2815129044dSC.J. Collierint
2825129044dSC.J. Colliercirbuf_del_buf_head(struct cirbuf *cbuf, unsigned int size)
2835129044dSC.J. Collier{
2845129044dSC.J. Collier	if (!cbuf || !size || size > CIRBUF_GET_LEN(cbuf))
2855129044dSC.J. Collier		return -EINVAL;
2865129044dSC.J. Collier
2875129044dSC.J. Collier	cbuf->len -= size;
2885129044dSC.J. Collier	if (CIRBUF_IS_EMPTY(cbuf)) {
2895129044dSC.J. Collier		cbuf->start += size - 1;
2905129044dSC.J. Collier		cbuf->start %= cbuf->maxlen;
2915129044dSC.J. Collier	}
2925129044dSC.J. Collier	else {
2935129044dSC.J. Collier		cbuf->start += size;
2945129044dSC.J. Collier		cbuf->start %= cbuf->maxlen;
2955129044dSC.J. Collier	}
2965129044dSC.J. Collier	return 0;
2975129044dSC.J. Collier}
2985129044dSC.J. Collier
2995129044dSC.J. Collier/* buffer del */
3005129044dSC.J. Collier
3015129044dSC.J. Collierint
3025129044dSC.J. Colliercirbuf_del_buf_tail(struct cirbuf *cbuf, unsigned int size)
3035129044dSC.J. Collier{
3045129044dSC.J. Collier	if (!cbuf || !size || size > CIRBUF_GET_LEN(cbuf))
3055129044dSC.J. Collier		return -EINVAL;
3065129044dSC.J. Collier
3075129044dSC.J. Collier	cbuf->len -= size;
3085129044dSC.J. Collier	if (CIRBUF_IS_EMPTY(cbuf)) {
3095129044dSC.J. Collier		cbuf->end  += (cbuf->maxlen - size + 1);
3105129044dSC.J. Collier		cbuf->end %= cbuf->maxlen;
3115129044dSC.J. Collier	}
3125129044dSC.J. Collier	else {
3135129044dSC.J. Collier		cbuf->end  += (cbuf->maxlen - size);
3145129044dSC.J. Collier		cbuf->end %= cbuf->maxlen;
3155129044dSC.J. Collier	}
3165129044dSC.J. Collier	return 0;
3175129044dSC.J. Collier}
3185129044dSC.J. Collier
3195129044dSC.J. Collier/* del at head */
3205129044dSC.J. Collier
3215129044dSC.J. Collierstatic inline void
3225129044dSC.J. Collier__cirbuf_del_head(struct cirbuf * cbuf)
3235129044dSC.J. Collier{
3245129044dSC.J. Collier	cbuf->len --;
3255129044dSC.J. Collier	if (!CIRBUF_IS_EMPTY(cbuf)) {
3265129044dSC.J. Collier		cbuf->start ++;
3275129044dSC.J. Collier		cbuf->start %= cbuf->maxlen;
3285129044dSC.J. Collier	}
3295129044dSC.J. Collier}
3305129044dSC.J. Collier
3315129044dSC.J. Collierint
3325129044dSC.J. Colliercirbuf_del_head_safe(struct cirbuf * cbuf)
3335129044dSC.J. Collier{
3345129044dSC.J. Collier	if (cbuf && !CIRBUF_IS_EMPTY(cbuf)) {
3355129044dSC.J. Collier		__cirbuf_del_head(cbuf);
3365129044dSC.J. Collier		return 0;
3375129044dSC.J. Collier	}
3385129044dSC.J. Collier	return -EINVAL;
3395129044dSC.J. Collier}
3405129044dSC.J. Collier
3415129044dSC.J. Colliervoid
3425129044dSC.J. Colliercirbuf_del_head(struct cirbuf * cbuf)
3435129044dSC.J. Collier{
3445129044dSC.J. Collier	__cirbuf_del_head(cbuf);
3455129044dSC.J. Collier}
3465129044dSC.J. Collier
3475129044dSC.J. Collier/* del at tail */
3485129044dSC.J. Collier
3495129044dSC.J. Collierstatic inline void
3505129044dSC.J. Collier__cirbuf_del_tail(struct cirbuf * cbuf)
3515129044dSC.J. Collier{
3525129044dSC.J. Collier	cbuf->len --;
3535129044dSC.J. Collier	if (!CIRBUF_IS_EMPTY(cbuf)) {
3545129044dSC.J. Collier		cbuf->end  += (cbuf->maxlen - 1);
3555129044dSC.J. Collier		cbuf->end %= cbuf->maxlen;
3565129044dSC.J. Collier	}
3575129044dSC.J. Collier}
3585129044dSC.J. Collier
3595129044dSC.J. Collierint
3605129044dSC.J. Colliercirbuf_del_tail_safe(struct cirbuf * cbuf)
3615129044dSC.J. Collier{
3625129044dSC.J. Collier	if (cbuf && !CIRBUF_IS_EMPTY(cbuf)) {
3635129044dSC.J. Collier		__cirbuf_del_tail(cbuf);
3645129044dSC.J. Collier		return 0;
3655129044dSC.J. Collier	}
3665129044dSC.J. Collier	return -EINVAL;
3675129044dSC.J. Collier}
3685129044dSC.J. Collier
3695129044dSC.J. Colliervoid
3705129044dSC.J. Colliercirbuf_del_tail(struct cirbuf * cbuf)
3715129044dSC.J. Collier{
3725129044dSC.J. Collier	__cirbuf_del_tail(cbuf);
3735129044dSC.J. Collier}
3745129044dSC.J. Collier
3755129044dSC.J. Collier/* convert to buffer */
3765129044dSC.J. Collier
3775129044dSC.J. Collierint
3785129044dSC.J. Colliercirbuf_get_buf_head(struct cirbuf *cbuf, char *c, unsigned int size)
3795129044dSC.J. Collier{
3805129044dSC.J. Collier	unsigned int n;
3815129044dSC.J. Collier
3825129044dSC.J. Collier	if (!cbuf || !c)
3835129044dSC.J. Collier		return -EINVAL;
3845129044dSC.J. Collier
3855129044dSC.J. Collier	n = (size < CIRBUF_GET_LEN(cbuf)) ? size : CIRBUF_GET_LEN(cbuf);
3865129044dSC.J. Collier
3875129044dSC.J. Collier	if (!n)
3885129044dSC.J. Collier		return 0;
3895129044dSC.J. Collier
3905129044dSC.J. Collier	if (cbuf->start <= cbuf->end) {
3915129044dSC.J. Collier		dprintf("s[%d] -> d[%d] (%d)\n", cbuf->start, 0, n);
3925129044dSC.J. Collier		memcpy(c, cbuf->buf + cbuf->start , n);
3935129044dSC.J. Collier	}
3945129044dSC.J. Collier	else {
3955129044dSC.J. Collier		/* check if we need to go from end to the beginning */
3965129044dSC.J. Collier		if (n <= cbuf->maxlen - cbuf->start) {
3975129044dSC.J. Collier			dprintf("s[%d] -> d[%d] (%d)\n", 0, cbuf->start, n);
3985129044dSC.J. Collier			memcpy(c, cbuf->buf + cbuf->start , n);
3995129044dSC.J. Collier		}
4005129044dSC.J. Collier		else {
4015129044dSC.J. Collier			dprintf("s[%d] -> d[%d] (%d)\n", cbuf->start, 0,
4025129044dSC.J. Collier				cbuf->maxlen - cbuf->start);
4035129044dSC.J. Collier			dprintf("s[%d] -> d[%d] (%d)\n", 0, cbuf->maxlen - cbuf->start,
4045129044dSC.J. Collier				n - cbuf->maxlen + cbuf->start);
4055129044dSC.J. Collier			memcpy(c, cbuf->buf + cbuf->start , cbuf->maxlen - cbuf->start);
4065129044dSC.J. Collier			memcpy(c + cbuf->maxlen - cbuf->start, cbuf->buf,
4075129044dSC.J. Collier				   n - cbuf->maxlen + cbuf->start);
4085129044dSC.J. Collier		}
4095129044dSC.J. Collier	}
4105129044dSC.J. Collier	return n;
4115129044dSC.J. Collier}
4125129044dSC.J. Collier
4135129044dSC.J. Collier/* convert to buffer */
4145129044dSC.J. Collier
4155129044dSC.J. Collierint
4165129044dSC.J. Colliercirbuf_get_buf_tail(struct cirbuf *cbuf, char *c, unsigned int size)
4175129044dSC.J. Collier{
4185129044dSC.J. Collier	unsigned int n;
4195129044dSC.J. Collier
4205129044dSC.J. Collier	if (!cbuf || !c)
4215129044dSC.J. Collier		return -EINVAL;
4225129044dSC.J. Collier
4235129044dSC.J. Collier	n = (size < CIRBUF_GET_LEN(cbuf)) ? size : CIRBUF_GET_LEN(cbuf);
4245129044dSC.J. Collier
4255129044dSC.J. Collier	if (!n)
4265129044dSC.J. Collier		return 0;
4275129044dSC.J. Collier
4285129044dSC.J. Collier	if (cbuf->start <= cbuf->end) {
4295129044dSC.J. Collier		dprintf("s[%d] -> d[%d] (%d)\n", cbuf->end - n + 1, 0, n);
4305129044dSC.J. Collier		memcpy(c, cbuf->buf + cbuf->end - n + 1, n);
4315129044dSC.J. Collier	}
4325129044dSC.J. Collier	else {
4335129044dSC.J. Collier		/* check if we need to go from end to the beginning */
4345129044dSC.J. Collier		if (n <= cbuf->end + 1) {
4355129044dSC.J. Collier			dprintf("s[%d] -> d[%d] (%d)\n", 0, cbuf->end - n + 1, n);
4365129044dSC.J. Collier			memcpy(c, cbuf->buf + cbuf->end - n + 1, n);
4375129044dSC.J. Collier		}
4385129044dSC.J. Collier		else {
4395129044dSC.J. Collier			dprintf("s[%d] -> d[%d] (%d)\n", 0,
4405129044dSC.J. Collier				cbuf->maxlen - cbuf->start, cbuf->end + 1);
4415129044dSC.J. Collier			dprintf("s[%d] -> d[%d] (%d)\n",
4425129044dSC.J. Collier				cbuf->maxlen - n + cbuf->end + 1, 0, n - cbuf->end - 1);
4435129044dSC.J. Collier			memcpy(c + cbuf->maxlen - cbuf->start,
4445129044dSC.J. Collier					       cbuf->buf, cbuf->end + 1);
4455129044dSC.J. Collier			memcpy(c, cbuf->buf + cbuf->maxlen - n + cbuf->end +1,
4465129044dSC.J. Collier				   n - cbuf->end - 1);
4475129044dSC.J. Collier		}
4485129044dSC.J. Collier	}
4495129044dSC.J. Collier	return n;
4505129044dSC.J. Collier}
4515129044dSC.J. Collier
4525129044dSC.J. Collier/* get head or get tail */
4535129044dSC.J. Collier
4545129044dSC.J. Collierchar
4555129044dSC.J. Colliercirbuf_get_head(struct cirbuf * cbuf)
4565129044dSC.J. Collier{
4575129044dSC.J. Collier	return cbuf->buf[cbuf->start];
4585129044dSC.J. Collier}
4595129044dSC.J. Collier
4605129044dSC.J. Collier/* get head or get tail */
4615129044dSC.J. Collier
4625129044dSC.J. Collierchar
4635129044dSC.J. Colliercirbuf_get_tail(struct cirbuf * cbuf)
4645129044dSC.J. Collier{
4655129044dSC.J. Collier	return cbuf->buf[cbuf->end];
4665129044dSC.J. Collier}
467