Deleted Added
full compact
buffer.c (65668) buffer.c (76259)
1/*
2 * Author: Tatu Ylonen <ylo@cs.hut.fi>
3 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
4 * All rights reserved
5 * Functions for manipulating fifo buffers (that can grow if needed).
6 *
7 * As far as I am concerned, the code I have written for this software
8 * can be used freely for any purpose. Any derived versions of this
9 * software must be clearly marked as such, and if the derived work is
10 * incompatible with the protocol description in the RFC file, it must be
11 * called by a name other than "ssh" or "Secure Shell".
12 */
13
14#include "includes.h"
1/*
2 * Author: Tatu Ylonen <ylo@cs.hut.fi>
3 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
4 * All rights reserved
5 * Functions for manipulating fifo buffers (that can grow if needed).
6 *
7 * As far as I am concerned, the code I have written for this software
8 * can be used freely for any purpose. Any derived versions of this
9 * software must be clearly marked as such, and if the derived work is
10 * incompatible with the protocol description in the RFC file, it must be
11 * called by a name other than "ssh" or "Secure Shell".
12 */
13
14#include "includes.h"
15RCSID("$OpenBSD: buffer.c,v 1.8 2000/09/07 20:27:50 deraadt Exp $");
15RCSID("$OpenBSD: buffer.c,v 1.13 2001/04/12 19:15:24 markus Exp $");
16
17#include "xmalloc.h"
18#include "buffer.h"
16
17#include "xmalloc.h"
18#include "buffer.h"
19#include "ssh.h"
19#include "log.h"
20
21/* Initializes the buffer structure. */
22
23void
24buffer_init(Buffer *buffer)
25{
26 buffer->alloc = 4096;
27 buffer->buf = xmalloc(buffer->alloc);

--- 20 unchanged lines hidden (view full) ---

48{
49 buffer->offset = 0;
50 buffer->end = 0;
51}
52
53/* Appends data to the buffer, expanding it if necessary. */
54
55void
20
21/* Initializes the buffer structure. */
22
23void
24buffer_init(Buffer *buffer)
25{
26 buffer->alloc = 4096;
27 buffer->buf = xmalloc(buffer->alloc);

--- 20 unchanged lines hidden (view full) ---

48{
49 buffer->offset = 0;
50 buffer->end = 0;
51}
52
53/* Appends data to the buffer, expanding it if necessary. */
54
55void
56buffer_append(Buffer *buffer, const char *data, unsigned int len)
56buffer_append(Buffer *buffer, const char *data, u_int len)
57{
58 char *cp;
59 buffer_append_space(buffer, &cp, len);
60 memcpy(cp, data, len);
61}
62
63/*
64 * Appends space to the buffer, expanding the buffer if necessary. This does
65 * not actually copy the data into the buffer, but instead returns a pointer
66 * to the allocated region.
67 */
68
69void
57{
58 char *cp;
59 buffer_append_space(buffer, &cp, len);
60 memcpy(cp, data, len);
61}
62
63/*
64 * Appends space to the buffer, expanding the buffer if necessary. This does
65 * not actually copy the data into the buffer, but instead returns a pointer
66 * to the allocated region.
67 */
68
69void
70buffer_append_space(Buffer *buffer, char **datap, unsigned int len)
70buffer_append_space(Buffer *buffer, char **datap, u_int len)
71{
72 /* If the buffer is empty, start using it from the beginning. */
73 if (buffer->offset == buffer->end) {
74 buffer->offset = 0;
75 buffer->end = 0;
76 }
77restart:
78 /* If there is enough space to store all data, store it now. */

--- 16 unchanged lines hidden (view full) ---

95 /* Increase the size of the buffer and retry. */
96 buffer->alloc += len + 32768;
97 buffer->buf = xrealloc(buffer->buf, buffer->alloc);
98 goto restart;
99}
100
101/* Returns the number of bytes of data in the buffer. */
102
71{
72 /* If the buffer is empty, start using it from the beginning. */
73 if (buffer->offset == buffer->end) {
74 buffer->offset = 0;
75 buffer->end = 0;
76 }
77restart:
78 /* If there is enough space to store all data, store it now. */

--- 16 unchanged lines hidden (view full) ---

95 /* Increase the size of the buffer and retry. */
96 buffer->alloc += len + 32768;
97 buffer->buf = xrealloc(buffer->buf, buffer->alloc);
98 goto restart;
99}
100
101/* Returns the number of bytes of data in the buffer. */
102
103unsigned int
103u_int
104buffer_len(Buffer *buffer)
105{
106 return buffer->end - buffer->offset;
107}
108
109/* Gets data from the beginning of the buffer. */
110
111void
104buffer_len(Buffer *buffer)
105{
106 return buffer->end - buffer->offset;
107}
108
109/* Gets data from the beginning of the buffer. */
110
111void
112buffer_get(Buffer *buffer, char *buf, unsigned int len)
112buffer_get(Buffer *buffer, char *buf, u_int len)
113{
114 if (len > buffer->end - buffer->offset)
113{
114 if (len > buffer->end - buffer->offset)
115 fatal("buffer_get: trying to get more bytes than in buffer");
115 fatal("buffer_get: trying to get more bytes %d than in buffer %d",
116 len, buffer->end - buffer->offset);
116 memcpy(buf, buffer->buf + buffer->offset, len);
117 buffer->offset += len;
118}
119
120/* Consumes the given number of bytes from the beginning of the buffer. */
121
122void
117 memcpy(buf, buffer->buf + buffer->offset, len);
118 buffer->offset += len;
119}
120
121/* Consumes the given number of bytes from the beginning of the buffer. */
122
123void
123buffer_consume(Buffer *buffer, unsigned int bytes)
124buffer_consume(Buffer *buffer, u_int bytes)
124{
125 if (bytes > buffer->end - buffer->offset)
126 fatal("buffer_consume: trying to get more bytes than in buffer");
127 buffer->offset += bytes;
128}
129
130/* Consumes the given number of bytes from the end of the buffer. */
131
132void
125{
126 if (bytes > buffer->end - buffer->offset)
127 fatal("buffer_consume: trying to get more bytes than in buffer");
128 buffer->offset += bytes;
129}
130
131/* Consumes the given number of bytes from the end of the buffer. */
132
133void
133buffer_consume_end(Buffer *buffer, unsigned int bytes)
134buffer_consume_end(Buffer *buffer, u_int bytes)
134{
135 if (bytes > buffer->end - buffer->offset)
136 fatal("buffer_consume_end: trying to get more bytes than in buffer");
137 buffer->end -= bytes;
138}
139
140/* Returns a pointer to the first used byte in the buffer. */
141

--- 4 unchanged lines hidden (view full) ---

146}
147
148/* Dumps the contents of the buffer to stderr. */
149
150void
151buffer_dump(Buffer *buffer)
152{
153 int i;
135{
136 if (bytes > buffer->end - buffer->offset)
137 fatal("buffer_consume_end: trying to get more bytes than in buffer");
138 buffer->end -= bytes;
139}
140
141/* Returns a pointer to the first used byte in the buffer. */
142

--- 4 unchanged lines hidden (view full) ---

147}
148
149/* Dumps the contents of the buffer to stderr. */
150
151void
152buffer_dump(Buffer *buffer)
153{
154 int i;
154 unsigned char *ucp = (unsigned char *) buffer->buf;
155 u_char *ucp = (u_char *) buffer->buf;
155
156
156 for (i = buffer->offset; i < buffer->end; i++)
157 fprintf(stderr, " %02x", ucp[i]);
158 fprintf(stderr, "\n");
157 for (i = buffer->offset; i < buffer->end; i++) {
158 fprintf(stderr, "%02x", ucp[i]);
159 if ((i-buffer->offset)%16==15)
160 fprintf(stderr, "\r\n");
161 else if ((i-buffer->offset)%2==1)
162 fprintf(stderr, " ");
163 }
164 fprintf(stderr, "\r\n");
159}
165}