merge of '48fdaa8706d1acda35e9d564adc9a1fbc96c18c8'
[dropbear.git] / circbuffer.c
blobe70087ad72e29666a5772f3d139f9da3ad57bf48
1 /*
2 * Dropbear SSH
3 *
4 * Copyright (c) 2002-2004 Matt Johnston
5 * All rights reserved.
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and associated documentation files (the "Software"), to deal
9 * in the Software without restriction, including without limitation the rights
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 * SOFTWARE. */
25 #include "includes.h"
26 #include "dbutil.h"
27 #include "circbuffer.h"
29 #define MAX_CBUF_SIZE 100000000
31 circbuffer * cbuf_new(unsigned int size) {
33 circbuffer *cbuf = NULL;
35 if (size > MAX_CBUF_SIZE) {
36 dropbear_exit("bad cbuf size");
39 cbuf = (circbuffer*)m_malloc(sizeof(circbuffer));
40 cbuf->data = (unsigned char*)m_malloc(size);
41 cbuf->used = 0;
42 cbuf->readpos = 0;
43 cbuf->writepos = 0;
44 cbuf->size = size;
46 return cbuf;
49 void cbuf_free(circbuffer * cbuf) {
51 m_free(cbuf->data);
52 m_free(cbuf);
55 unsigned int cbuf_getused(circbuffer * cbuf) {
57 return cbuf->used;
61 unsigned int cbuf_getavail(circbuffer * cbuf) {
63 return cbuf->size - cbuf->used;
67 unsigned int cbuf_readlen(circbuffer *cbuf) {
69 dropbear_assert(((2*cbuf->size)+cbuf->writepos-cbuf->readpos)%cbuf->size == cbuf->used%cbuf->size);
70 dropbear_assert(((2*cbuf->size)+cbuf->readpos-cbuf->writepos)%cbuf->size == (cbuf->size-cbuf->used)%cbuf->size);
72 if (cbuf->used == 0) {
73 TRACE(("cbuf_readlen: unused buffer"))
74 return 0;
77 if (cbuf->readpos < cbuf->writepos) {
78 return cbuf->writepos - cbuf->readpos;
81 return cbuf->size - cbuf->readpos;
84 unsigned int cbuf_writelen(circbuffer *cbuf) {
86 dropbear_assert(cbuf->used <= cbuf->size);
87 dropbear_assert(((2*cbuf->size)+cbuf->writepos-cbuf->readpos)%cbuf->size == cbuf->used%cbuf->size);
88 dropbear_assert(((2*cbuf->size)+cbuf->readpos-cbuf->writepos)%cbuf->size == (cbuf->size-cbuf->used)%cbuf->size);
90 if (cbuf->used == cbuf->size) {
91 TRACE(("cbuf_writelen: full buffer"))
92 return 0; /* full */
95 if (cbuf->writepos < cbuf->readpos) {
96 return cbuf->readpos - cbuf->writepos;
99 return cbuf->size - cbuf->writepos;
102 unsigned char* cbuf_readptr(circbuffer *cbuf, unsigned int len) {
103 if (len > cbuf_readlen(cbuf)) {
104 dropbear_exit("bad cbuf read");
107 return &cbuf->data[cbuf->readpos];
110 unsigned char* cbuf_writeptr(circbuffer *cbuf, unsigned int len) {
112 if (len > cbuf_writelen(cbuf)) {
113 dropbear_exit("bad cbuf write");
116 return &cbuf->data[cbuf->writepos];
119 void cbuf_incrwrite(circbuffer *cbuf, unsigned int len) {
120 if (len > cbuf_writelen(cbuf)) {
121 dropbear_exit("bad cbuf write");
124 cbuf->used += len;
125 dropbear_assert(cbuf->used <= cbuf->size);
126 cbuf->writepos = (cbuf->writepos + len) % cbuf->size;
130 void cbuf_incrread(circbuffer *cbuf, unsigned int len) {
131 if (len > cbuf_readlen(cbuf)) {
132 dropbear_exit("bad cbuf read");
135 dropbear_assert(cbuf->used >= len);
136 cbuf->used -= len;
137 cbuf->readpos = (cbuf->readpos + len) % cbuf->size;