• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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:
13  *
14  * The above copyright notice and this permission notice shall be included in
15  * all copies or substantial portions of the Software.
16  *
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. */
24 
25 #include "includes.h"
26 #include "dbutil.h"
27 #include "circbuffer.h"
28 
29 #define MAX_CBUF_SIZE 100000000
30 
cbuf_new(unsigned int size)31 circbuffer * cbuf_new(unsigned int size) {
32 
33 	circbuffer *cbuf = NULL;
34 
35 	if (size > MAX_CBUF_SIZE) {
36 		dropbear_exit("bad cbuf size");
37 	}
38 
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;
45 
46 	return cbuf;
47 }
48 
cbuf_free(circbuffer * cbuf)49 void cbuf_free(circbuffer * cbuf) {
50 
51 	m_free(cbuf->data);
52 	m_free(cbuf);
53 }
54 
cbuf_getused(circbuffer * cbuf)55 unsigned int cbuf_getused(circbuffer * cbuf) {
56 
57 	return cbuf->used;
58 
59 }
60 
cbuf_getavail(circbuffer * cbuf)61 unsigned int cbuf_getavail(circbuffer * cbuf) {
62 
63 	return cbuf->size - cbuf->used;
64 
65 }
66 
cbuf_readlen(circbuffer * cbuf)67 unsigned int cbuf_readlen(circbuffer *cbuf) {
68 
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);
71 
72 	if (cbuf->used == 0) {
73 		TRACE(("cbuf_readlen: unused buffer"))
74 		return 0;
75 	}
76 
77 	if (cbuf->readpos < cbuf->writepos) {
78 		return cbuf->writepos - cbuf->readpos;
79 	}
80 
81 	return cbuf->size - cbuf->readpos;
82 }
83 
cbuf_writelen(circbuffer * cbuf)84 unsigned int cbuf_writelen(circbuffer *cbuf) {
85 
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);
89 
90 	if (cbuf->used == cbuf->size) {
91 		TRACE(("cbuf_writelen: full buffer"))
92 		return 0; /* full */
93 	}
94 
95 	if (cbuf->writepos < cbuf->readpos) {
96 		return cbuf->readpos - cbuf->writepos;
97 	}
98 
99 	return cbuf->size - cbuf->writepos;
100 }
101 
cbuf_readptr(circbuffer * cbuf,unsigned int len)102 unsigned char* cbuf_readptr(circbuffer *cbuf, unsigned int len) {
103 	if (len > cbuf_readlen(cbuf)) {
104 		dropbear_exit("bad cbuf read");
105 	}
106 
107 	return &cbuf->data[cbuf->readpos];
108 }
109 
cbuf_writeptr(circbuffer * cbuf,unsigned int len)110 unsigned char* cbuf_writeptr(circbuffer *cbuf, unsigned int len) {
111 
112 	if (len > cbuf_writelen(cbuf)) {
113 		dropbear_exit("bad cbuf write");
114 	}
115 
116 	return &cbuf->data[cbuf->writepos];
117 }
118 
cbuf_incrwrite(circbuffer * cbuf,unsigned int len)119 void cbuf_incrwrite(circbuffer *cbuf, unsigned int len) {
120 	if (len > cbuf_writelen(cbuf)) {
121 		dropbear_exit("bad cbuf write");
122 	}
123 
124 	cbuf->used += len;
125 	dropbear_assert(cbuf->used <= cbuf->size);
126 	cbuf->writepos = (cbuf->writepos + len) % cbuf->size;
127 }
128 
129 
cbuf_incrread(circbuffer * cbuf,unsigned int len)130 void cbuf_incrread(circbuffer *cbuf, unsigned int len) {
131 	if (len > cbuf_readlen(cbuf)) {
132 		dropbear_exit("bad cbuf read");
133 	}
134 
135 	dropbear_assert(cbuf->used >= len);
136 	cbuf->used -= len;
137 	cbuf->readpos = (cbuf->readpos + len) % cbuf->size;
138 }
139