summaryrefslogtreecommitdiff
path: root/other/openssh-reverse/buffer.c
diff options
context:
space:
mode:
Diffstat (limited to 'other/openssh-reverse/buffer.c')
-rw-r--r--other/openssh-reverse/buffer.c162
1 files changed, 162 insertions, 0 deletions
diff --git a/other/openssh-reverse/buffer.c b/other/openssh-reverse/buffer.c
new file mode 100644
index 0000000..468d2a0
--- /dev/null
+++ b/other/openssh-reverse/buffer.c
@@ -0,0 +1,162 @@
1/*
2 *
3 * buffer.c
4 *
5 * Author: Tatu Ylonen <ylo@cs.hut.fi>
6 *
7 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
8 * All rights reserved
9 *
10 * Created: Sat Mar 18 04:15:33 1995 ylo
11 *
12 * Functions for manipulating fifo buffers (that can grow if needed).
13 *
14 */
15
16#include "includes.h"
17RCSID("$OpenBSD: buffer.c,v 1.7 2000/06/20 01:39:39 markus Exp $");
18
19#include "xmalloc.h"
20#include "buffer.h"
21#include "ssh.h"
22
23/* Initializes the buffer structure. */
24
25void
26buffer_init(Buffer *buffer)
27{
28 buffer->alloc = 4096;
29 buffer->buf = xmalloc(buffer->alloc);
30 memset(buffer->buf, 0, 4096);
31 buffer->offset = 0;
32 buffer->end = 0;
33}
34
35/* Frees any memory used for the buffer. */
36
37void
38buffer_free(Buffer *buffer)
39{
40 memset(buffer->buf, 0, buffer->alloc);
41 xfree(buffer->buf);
42}
43
44/*
45 * Clears any data from the buffer, making it empty. This does not actually
46 * zero the memory.
47 */
48
49void
50buffer_clear(Buffer *buffer)
51{
52 buffer->offset = 0;
53 buffer->end = 0;
54}
55
56/* Appends data to the buffer, expanding it if necessary. */
57
58void
59buffer_append(Buffer *buffer, const char *data, unsigned int len)
60{
61 char *cp;
62 buffer_append_space(buffer, &cp, len);
63 memcpy(cp, data, len);
64}
65
66/*
67 * Appends space to the buffer, expanding the buffer if necessary. This does
68 * not actually copy the data into the buffer, but instead returns a pointer
69 * to the allocated region.
70 */
71
72void
73buffer_append_space(Buffer *buffer, char **datap, unsigned int len)
74{
75 /* If the buffer is empty, start using it from the beginning. */
76 if (buffer->offset == buffer->end) {
77 buffer->offset = 0;
78 buffer->end = 0;
79 }
80restart:
81 /* If there is enough space to store all data, store it now. */
82 if (buffer->end + len < buffer->alloc) {
83 *datap = buffer->buf + buffer->end;
84 buffer->end += len;
85 return;
86 }
87 /*
88 * If the buffer is quite empty, but all data is at the end, move the
89 * data to the beginning and retry.
90 */
91 if (buffer->offset > buffer->alloc / 2) {
92 memmove(buffer->buf, buffer->buf + buffer->offset,
93 buffer->end - buffer->offset);
94 buffer->end -= buffer->offset;
95 buffer->offset = 0;
96 goto restart;
97 }
98 /* Increase the size of the buffer and retry. */
99 buffer->alloc += len + 32768;
100 buffer->buf = xrealloc(buffer->buf, buffer->alloc);
101 goto restart;
102}
103
104/* Returns the number of bytes of data in the buffer. */
105
106unsigned int
107buffer_len(Buffer *buffer)
108{
109 return buffer->end - buffer->offset;
110}
111
112/* Gets data from the beginning of the buffer. */
113
114void
115buffer_get(Buffer *buffer, char *buf, unsigned int len)
116{
117 if (len > buffer->end - buffer->offset)
118 fatal("buffer_get: trying to get more bytes than in buffer");
119 memcpy(buf, buffer->buf + buffer->offset, len);
120 buffer->offset += len;
121}
122
123/* Consumes the given number of bytes from the beginning of the buffer. */
124
125void
126buffer_consume(Buffer *buffer, unsigned int bytes)
127{
128 if (bytes > buffer->end - buffer->offset)
129 fatal("buffer_consume: trying to get more bytes than in buffer");
130 buffer->offset += bytes;
131}
132
133/* Consumes the given number of bytes from the end of the buffer. */
134
135void
136buffer_consume_end(Buffer *buffer, unsigned int bytes)
137{
138 if (bytes > buffer->end - buffer->offset)
139 fatal("buffer_consume_end: trying to get more bytes than in buffer");
140 buffer->end -= bytes;
141}
142
143/* Returns a pointer to the first used byte in the buffer. */
144
145char *
146buffer_ptr(Buffer *buffer)
147{
148 return buffer->buf + buffer->offset;
149}
150
151/* Dumps the contents of the buffer to stderr. */
152
153void
154buffer_dump(Buffer *buffer)
155{
156 int i;
157 unsigned char *ucp = (unsigned char *) buffer->buf;
158
159 for (i = buffer->offset; i < buffer->end; i++)
160 fprintf(stderr, " %02x", ucp[i]);
161 fprintf(stderr, "\n");
162}