Allow IPv6 address entry in tools>ping - Loosens valid character check
[tomato/davidwu.git] / release / src / router / dropbear / circbuffer.c
blob795373768ee87f17bbfe0369288c616502b9cf85
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_burn(cbuf->data, cbuf->size);
52 m_free(cbuf->data);
53 m_free(cbuf);
56 unsigned int cbuf_getused(circbuffer * cbuf) {
58 return cbuf->used;
62 unsigned int cbuf_getavail(circbuffer * cbuf) {
64 return cbuf->size - cbuf->used;
68 unsigned int cbuf_readlen(circbuffer *cbuf) {
70 dropbear_assert(((2*cbuf->size)+cbuf->writepos-cbuf->readpos)%cbuf->size == cbuf->used%cbuf->size);
71 dropbear_assert(((2*cbuf->size)+cbuf->readpos-cbuf->writepos)%cbuf->size == (cbuf->size-cbuf->used)%cbuf->size);
73 if (cbuf->used == 0) {
74 TRACE(("cbuf_readlen: unused buffer"))
75 return 0;
78 if (cbuf->readpos < cbuf->writepos) {
79 return cbuf->writepos - cbuf->readpos;
82 return cbuf->size - cbuf->readpos;
85 unsigned int cbuf_writelen(circbuffer *cbuf) {
87 dropbear_assert(cbuf->used <= cbuf->size);
88 dropbear_assert(((2*cbuf->size)+cbuf->writepos-cbuf->readpos)%cbuf->size == cbuf->used%cbuf->size);
89 dropbear_assert(((2*cbuf->size)+cbuf->readpos-cbuf->writepos)%cbuf->size == (cbuf->size-cbuf->used)%cbuf->size);
91 if (cbuf->used == cbuf->size) {
92 TRACE(("cbuf_writelen: full buffer"))
93 return 0; /* full */
96 if (cbuf->writepos < cbuf->readpos) {
97 return cbuf->readpos - cbuf->writepos;
100 return cbuf->size - cbuf->writepos;
103 unsigned char* cbuf_readptr(circbuffer *cbuf, unsigned int len) {
104 if (len > cbuf_readlen(cbuf)) {
105 dropbear_exit("Bad cbuf read");
108 return &cbuf->data[cbuf->readpos];
111 unsigned char* cbuf_writeptr(circbuffer *cbuf, unsigned int len) {
113 if (len > cbuf_writelen(cbuf)) {
114 dropbear_exit("Bad cbuf write");
117 return &cbuf->data[cbuf->writepos];
120 void cbuf_incrwrite(circbuffer *cbuf, unsigned int len) {
121 if (len > cbuf_writelen(cbuf)) {
122 dropbear_exit("Bad cbuf write");
125 cbuf->used += len;
126 dropbear_assert(cbuf->used <= cbuf->size);
127 cbuf->writepos = (cbuf->writepos + len) % cbuf->size;
131 void cbuf_incrread(circbuffer *cbuf, unsigned int len) {
132 if (len > cbuf_readlen(cbuf)) {
133 dropbear_exit("Bad cbuf read");
136 dropbear_assert(cbuf->used >= len);
137 cbuf->used -= len;
138 cbuf->readpos = (cbuf->readpos + len) % cbuf->size;