dropbear: update to 2013.62
[tomato.git] / release / src / router / dropbear / circbuffer.c
blob55c442280709c4369771cbc5746ac73674ad0b11
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 if (size > 0) {
41 cbuf->data = (unsigned char*)m_malloc(size);
43 cbuf->used = 0;
44 cbuf->readpos = 0;
45 cbuf->writepos = 0;
46 cbuf->size = size;
48 return cbuf;
51 void cbuf_free(circbuffer * cbuf) {
53 m_burn(cbuf->data, cbuf->size);
54 m_free(cbuf->data);
55 m_free(cbuf);
58 unsigned int cbuf_getused(circbuffer * cbuf) {
60 return cbuf->used;
64 unsigned int cbuf_getavail(circbuffer * cbuf) {
66 return cbuf->size - cbuf->used;
70 unsigned int cbuf_readlen(circbuffer *cbuf) {
72 dropbear_assert(((2*cbuf->size)+cbuf->writepos-cbuf->readpos)%cbuf->size == cbuf->used%cbuf->size);
73 dropbear_assert(((2*cbuf->size)+cbuf->readpos-cbuf->writepos)%cbuf->size == (cbuf->size-cbuf->used)%cbuf->size);
75 if (cbuf->used == 0) {
76 TRACE(("cbuf_readlen: unused buffer"))
77 return 0;
80 if (cbuf->readpos < cbuf->writepos) {
81 return cbuf->writepos - cbuf->readpos;
84 return cbuf->size - cbuf->readpos;
87 unsigned int cbuf_writelen(circbuffer *cbuf) {
89 dropbear_assert(cbuf->used <= cbuf->size);
90 dropbear_assert(((2*cbuf->size)+cbuf->writepos-cbuf->readpos)%cbuf->size == cbuf->used%cbuf->size);
91 dropbear_assert(((2*cbuf->size)+cbuf->readpos-cbuf->writepos)%cbuf->size == (cbuf->size-cbuf->used)%cbuf->size);
93 if (cbuf->used == cbuf->size) {
94 TRACE(("cbuf_writelen: full buffer"))
95 return 0; /* full */
98 if (cbuf->writepos < cbuf->readpos) {
99 return cbuf->readpos - cbuf->writepos;
102 return cbuf->size - cbuf->writepos;
105 unsigned char* cbuf_readptr(circbuffer *cbuf, unsigned int len) {
106 if (len > cbuf_readlen(cbuf)) {
107 dropbear_exit("Bad cbuf read");
110 return &cbuf->data[cbuf->readpos];
113 unsigned char* cbuf_writeptr(circbuffer *cbuf, unsigned int len) {
115 if (len > cbuf_writelen(cbuf)) {
116 dropbear_exit("Bad cbuf write");
119 return &cbuf->data[cbuf->writepos];
122 void cbuf_incrwrite(circbuffer *cbuf, unsigned int len) {
123 if (len > cbuf_writelen(cbuf)) {
124 dropbear_exit("Bad cbuf write");
127 cbuf->used += len;
128 dropbear_assert(cbuf->used <= cbuf->size);
129 cbuf->writepos = (cbuf->writepos + len) % cbuf->size;
133 void cbuf_incrread(circbuffer *cbuf, unsigned int len) {
134 if (len > cbuf_readlen(cbuf)) {
135 dropbear_exit("Bad cbuf read");
138 dropbear_assert(cbuf->used >= len);
139 cbuf->used -= len;
140 cbuf->readpos = (cbuf->readpos + len) % cbuf->size;