File: Buffer.c

package info (click to toggle)
mysecureshell 2.00%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 888 kB
  • sloc: ansic: 7,421; sh: 700; perl: 264; makefile: 116
file content (301 lines) | stat: -rw-r--r-- 6,323 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
/*
 MySecureShell permit to add restriction to modified sftp-server
 when using MySecureShell as shell.
 Copyright (C) 2007-2014 MySecureShell Team

 This program is free software; you can redistribute it and/or
 modify it under the terms of the GNU General Public License
 as published by the Free Software Foundation (version 2)

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program; if not, write to the Free Software
 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */

#include "../config.h"
#include <stdlib.h>
#include <string.h>
#include "Buffer.h"

/*@null@*/ tBuffer *BufferNew()
{
	tBuffer *b;

	b = malloc(sizeof(*b));
	if (b != NULL)
	{
		b->size = DEFAULT_GROW;
		b->data = malloc(b->size);
		b->length = 0;
		b->read = 0;
		b->fastClean = 0;
	}
	return (b);
}

void BufferGrow(tBuffer *b, u_int32_t toAdd)
{
	b->size += toAdd;
	b->data = realloc(b->data, b->size);
}

void BufferClean(tBuffer *b)
{
	if (b->read > 0)
	{
		if (b->length > b->read)
		{
			memmove(b->data, b->data + b->read, b->length - b->read);
			b->length -= b->read;
		}
		else
			b->length = 0;
		b->read = 0;
		if (b->fastClean == 0)
		{
			u_int32_t nextSize;

			nextSize = b->size >> 2;
			if (b->length < nextSize && nextSize >= DEFAULT_GROW)
			{
				b->size = nextSize;
				b->data = realloc(b->data, b->size);
			}
		}
	}
}

void BufferDelete(tBuffer *b)
{
	free(b->data);
	free(b);
}

void BufferPutInt8(tBuffer *b, u_int8_t nb)
{
	if ((b->length + 1) > b->size)
		BufferGrow(b, DEFAULT_GROW);
	b->data[b->length++] = nb;
}

void BufferPutInt16(tBuffer *b, u_int16_t nb)
{
	if ((b->length + 2) > b->size)
		BufferGrow(b, DEFAULT_GROW);
	b->data[b->length++] = (nb >> 8);
	b->data[b->length++] = nb;
}

void BufferPutInt32(tBuffer *b, u_int32_t nb)
{
	if ((b->length + 4) > b->size)
		BufferGrow(b, DEFAULT_GROW);
	b->data[b->length++] = (nb >> 24);
	b->data[b->length++] = (nb >> 16);
	b->data[b->length++] = (nb >> 8);
	b->data[b->length++] = nb;
}

void BufferPutInt64(tBuffer *b, u_int64_t nb)
{
	u_int32_t n1, n2;

	n1 = (u_int64_t) nb >> (u_int64_t) 32;
	n2 = (u_int64_t) nb & (u_int64_t) 0xffffffff;
	if ((b->length + 8) > b->size)
		BufferGrow(b, DEFAULT_GROW);
	b->data[b->length++] = (n1 >> 24);
	b->data[b->length++] = (n1 >> 16);
	b->data[b->length++] = (n1 >> 8);
	b->data[b->length++] = n1;
	b->data[b->length++] = (n2 >> 24);
	b->data[b->length++] = (n2 >> 16);
	b->data[b->length++] = (n2 >> 8);
	b->data[b->length++] = n2;
}

void BufferPutRawData(tBuffer *b, const void *data, u_int32_t size)
{
	if ((b->length + size) > b->size)
		BufferGrow(b, b->length + size - b->size + DEFAULT_GROW);
	memcpy(b->data + b->length, data, size);
	b->length += size;
}

void BufferPutString(tBuffer *b, const char *data)
{
	size_t size;

	size = strlen(data);
	if ((b->length + size + 4) > b->size)
		BufferGrow(b, b->length + size + 4 - b->size + DEFAULT_GROW);
	b->data[b->length++] = (size >> 24);
	b->data[b->length++] = (size >> 16);
	b->data[b->length++] = (size >> 8);
	b->data[b->length++] = size;
	memcpy(b->data + b->length, data, size);
	b->length += size;
}

void BufferPutHandle(tBuffer *b, int h)
{
	if ((b->length + BufferHandleSize) > b->size)
		BufferGrow(b, b->length + BufferHandleSize - b->size + DEFAULT_GROW);
	b->data[b->length++] = 0;
	b->data[b->length++] = 0;
	b->data[b->length++] = 0;
	b->data[b->length++] = 2;
	BufferPutInt8FAST(b, h + (int )'0');
	BufferPutInt8FAST(b, '\0');
}

#ifdef DODEBUG
#include "Log.h"

static char *ASCII = "0123456789ABCDEF";

static void dumpPacket(tBuffer *b, int trySize)
{
	char *buffer;
	int i, pos;

	mylog_printf(MYLOG_DEBUG, "[dumpPacket][length:%i][read:%i][size:%i][trySize:%i]",
			b->length, b->read, b->size, trySize);
	buffer = malloc(b->length * 2 + 1);
	for (i = b->read, pos = 0; i < b->length; i++)
	{
		unsigned char c = (unsigned char)b->data[i];

		buffer[pos++] = ASCII[c / 16];
		buffer[pos++] = ASCII[c % 16];
	}
	buffer[pos] = 0;
	mylog_printf(MYLOG_DEBUG, "[%s]", buffer);
	free(buffer);
}

#endif

void BufferReadData(tBuffer *b, u_int32_t size)
{
	if ((b->read + size) <= b->length)
		b->read += size;
#ifdef DODEBUG
	else
	dumpPacket(b, size);
#endif
}

u_int8_t BufferGetInt8(tBuffer *b)
{
	u_int8_t nb;

	if ((b->read + 1) > b->length)
	{
#ifdef DODEBUG
		dumpPacket(b, 1);
#endif
		return (0);
	}
	nb = (u_int8_t) b->data[b->read++];
	return (nb);
}

u_int32_t BufferGetInt32(tBuffer *b)
{
	u_int32_t nb;

	if ((b->read + 4) > b->length)
	{
#ifdef DODEBUG
		dumpPacket(b, 4);
#endif
		return (0);
	}
	nb = (u_int32_t) b->data[b->read++] << 24;
	nb += (u_int32_t) b->data[b->read++] << 16;
	nb += (u_int32_t) b->data[b->read++] << 8;
	nb += (u_int32_t) b->data[b->read++];
	return (nb);
}

u_int64_t BufferGetInt64(tBuffer *b)
{
	u_int64_t nb;

	if ((b->read + 8) > b->length)
	{
#ifdef DODEBUG
		dumpPacket(b, 8);
#endif
		return (0);
	}
	nb = (u_int64_t) b->data[b->read++] << 56;
	nb += (u_int64_t) b->data[b->read++] << 48;
	nb += (u_int64_t) b->data[b->read++] << 40;
	nb += (u_int64_t) b->data[b->read++] << 32;
	nb += (u_int64_t) b->data[b->read++] << 24;
	nb += (u_int64_t) b->data[b->read++] << 16;
	nb += (u_int64_t) b->data[b->read++] << 8;
	nb += (u_int64_t) b->data[b->read++];
	return (nb);
}

char *BufferGetData(tBuffer *b, u_int32_t *size)
{
	char *data;

	*size = BufferGetInt32(b);
	if ((b->read + *size) > b->length)
	{
#ifdef DODEBUG
		dumpPacket(b, *size);
#endif
		return (NULL);
	}
	data = (char *) (b->data + b->read);
	b->read += *size;
	return (data);
}

char *BufferGetString(tBuffer *b)
{
	char *data;
	u_int32_t size;

	size = BufferGetInt32(b);
	if ((b->read + size) > b->length)
	{
#ifdef DODEBUG
		dumpPacket(b, size);
#endif
		return (0);
	}
	data = malloc(size + 1);
	if (data)
	{
		memcpy(data, b->data + b->read, size);
		data[size] = 0;
		b->read += size;
	}
	return (data);
}

int BufferGetHandle(tBuffer *b)
{
	u_int32_t size;
	int hdl = -1;

	size = BufferGetInt32(b);
	if (size == 2)
	{
		hdl = BufferGetInt8FAST(b) - (int) '0';
		BufferIncrCurrentReadPosition(b, 1);
	}
	return (hdl);
}