-/*-
- * BSD LICENSE
- *
- * Copyright(c) 2010-2012 Intel Corporation. All rights reserved.
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- *
- * * Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- * * Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in
- * the documentation and/or other materials provided with the
- * distribution.
- * * Neither the name of Intel Corporation nor the names of its
- * contributors may be used to endorse or promote products derived
- * from this software without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
- * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
- * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
- * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
- * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
- * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
- * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
- * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
- * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
- * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- *
- */
-
-/*
+/* SPDX-License-Identifier: BSD-3-Clause
+ * Copyright(c) 2010-2014 Intel Corporation.
* Copyright (c) 2009, Olivier MATZ <zer0@droids-corp.org>
* All rights reserved.
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions are met:
- *
- * * Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- * * Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in the
- * documentation and/or other materials provided with the distribution.
- * * Neither the name of the University of California, Berkeley nor the
- * names of its contributors may be used to endorse or promote products
- * derived from this software without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND ANY
- * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
- * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
- * DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS BE LIABLE FOR ANY
- * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
- * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
- * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
- * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
- * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <string.h>
#include <errno.h>
+#include <stdio.h>
#include "cmdline_cirbuf.h"
-void
+int
cirbuf_init(struct cirbuf *cbuf, char *buf, unsigned int start, unsigned int maxlen)
{
+ if (!cbuf || !buf)
+ return -EINVAL;
cbuf->maxlen = maxlen;
cbuf->len = 0;
cbuf->start = start;
cbuf->end = start;
cbuf->buf = buf;
+ return 0;
}
/* multiple add */
{
unsigned int e;
- if (!n || n > CIRBUF_GET_FREELEN(cbuf))
+ if (!cbuf || !c || !n || n > CIRBUF_GET_FREELEN(cbuf))
return -EINVAL;
e = CIRBUF_IS_EMPTY(cbuf) ? 1 : 0;
{
unsigned int e;
- if (!n || n > CIRBUF_GET_FREELEN(cbuf))
+ if (!cbuf || !c || !n || n > CIRBUF_GET_FREELEN(cbuf))
return -EINVAL;
e = CIRBUF_IS_EMPTY(cbuf) ? 1 : 0;
}
/* XXX we could do a better algorithm here... */
-void cirbuf_align_left(struct cirbuf * cbuf)
+int
+cirbuf_align_left(struct cirbuf * cbuf)
{
+ if (!cbuf)
+ return -EINVAL;
+
if (cbuf->start < cbuf->maxlen/2) {
while (cbuf->start != 0) {
__cirbuf_shift_left(cbuf);
__cirbuf_shift_right(cbuf);
}
}
+
+ return 0;
}
/* XXX we could do a better algorithm here... */
-void cirbuf_align_right(struct cirbuf * cbuf)
+int
+cirbuf_align_right(struct cirbuf * cbuf)
{
+ if (!cbuf)
+ return -EINVAL;
+
if (cbuf->start >= cbuf->maxlen/2) {
while (cbuf->end != cbuf->maxlen-1) {
__cirbuf_shift_left(cbuf);
__cirbuf_shift_right(cbuf);
}
}
+
+ return 0;
}
/* buffer del */
int
cirbuf_del_buf_head(struct cirbuf *cbuf, unsigned int size)
{
- if (!size || size > CIRBUF_GET_LEN(cbuf))
+ if (!cbuf || !size || size > CIRBUF_GET_LEN(cbuf))
return -EINVAL;
cbuf->len -= size;
int
cirbuf_del_buf_tail(struct cirbuf *cbuf, unsigned int size)
{
- if (!size || size > CIRBUF_GET_LEN(cbuf))
+ if (!cbuf || !size || size > CIRBUF_GET_LEN(cbuf))
return -EINVAL;
cbuf->len -= size;
{
unsigned int n;
+ if (!cbuf || !c)
+ return -EINVAL;
+
n = (size < CIRBUF_GET_LEN(cbuf)) ? size : CIRBUF_GET_LEN(cbuf);
if (!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);
+ /* 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);
+ }
}
return n;
}
{
unsigned int n;
+ if (!cbuf || !c)
+ return -EINVAL;
+
n = (size < CIRBUF_GET_LEN(cbuf)) ? size : CIRBUF_GET_LEN(cbuf);
if (!n)
memcpy(c, cbuf->buf + cbuf->end - n + 1, n);
}
else {
- dprintf("s[%d] -> d[%d] (%d)\n", 0,
- cbuf->maxlen - cbuf->start, cbuf->end + 1);
- dprintf("s[%d] -> d[%d] (%d)\n",
- cbuf->maxlen - n + cbuf->end + 1, 0, n - cbuf->end - 1);
-
- memcpy(c + cbuf->maxlen - cbuf->start,
- cbuf->buf, cbuf->end + 1);
- memcpy(c, cbuf->buf + cbuf->maxlen - n + cbuf->end +1,
- n - cbuf->end - 1);
+ /* check if we need to go from end to the beginning */
+ if (n <= cbuf->end + 1) {
+ dprintf("s[%d] -> d[%d] (%d)\n", 0, cbuf->end - n + 1, n);
+ memcpy(c, cbuf->buf + cbuf->end - n + 1, n);
+ }
+ else {
+ dprintf("s[%d] -> d[%d] (%d)\n", 0,
+ cbuf->maxlen - cbuf->start, cbuf->end + 1);
+ dprintf("s[%d] -> d[%d] (%d)\n",
+ cbuf->maxlen - n + cbuf->end + 1, 0, n - cbuf->end - 1);
+ memcpy(c + cbuf->maxlen - cbuf->start,
+ cbuf->buf, cbuf->end + 1);
+ memcpy(c, cbuf->buf + cbuf->maxlen - n + cbuf->end +1,
+ n - cbuf->end - 1);
+ }
}
return n;
}
{
return cbuf->buf[cbuf->end];
}
-