Do not record expanded size before attempting to reallocate
[dragonfly.git] / crypto / openssh / buffer.c
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  * $DragonFly: src/crypto/openssh/Attic/buffer.c,v 1.2 2003/09/16 16:12:00 drhodus Exp $
14  */
15
16 #include "includes.h"
17 RCSID("$OpenBSD: buffer.c,v 1.16 2002/06/26 08:54:18 markus Exp $");
18
19 #include "xmalloc.h"
20 #include "buffer.h"
21 #include "log.h"
22
23 /* Initializes the buffer structure. */
24
25 void
26 buffer_init(Buffer *buffer)
27 {
28         buffer->alloc = 4096;
29         buffer->buf = xmalloc(buffer->alloc);
30         buffer->offset = 0;
31         buffer->end = 0;
32 }
33
34 /* Frees any memory used for the buffer. */
35
36 void
37 buffer_free(Buffer *buffer)
38 {
39         memset(buffer->buf, 0, buffer->alloc);
40         xfree(buffer->buf);
41 }
42
43 /*
44  * Clears any data from the buffer, making it empty.  This does not actually
45  * zero the memory.
46  */
47
48 void
49 buffer_clear(Buffer *buffer)
50 {
51         buffer->offset = 0;
52         buffer->end = 0;
53 }
54
55 /* Appends data to the buffer, expanding it if necessary. */
56
57 void
58 buffer_append(Buffer *buffer, const void *data, u_int len)
59 {
60         void *p;
61         p = buffer_append_space(buffer, len);
62         memcpy(p, data, len);
63 }
64
65 /*
66  * Appends space to the buffer, expanding the buffer if necessary. This does
67  * not actually copy the data into the buffer, but instead returns a pointer
68  * to the allocated region.
69  */
70
71 void *
72 buffer_append_space(Buffer *buffer, u_int len)
73 {
74         u_int newlen;
75         void *p;
76
77         if (len > 0x100000)
78                 fatal("buffer_append_space: len %u not supported", len);
79
80         /* If the buffer is empty, start using it from the beginning. */
81         if (buffer->offset == buffer->end) {
82                 buffer->offset = 0;
83                 buffer->end = 0;
84         }
85 restart:
86         /* If there is enough space to store all data, store it now. */
87         if (buffer->end + len < buffer->alloc) {
88                 p = buffer->buf + buffer->end;
89                 buffer->end += len;
90                 return p;
91         }
92         /*
93          * If the buffer is quite empty, but all data is at the end, move the
94          * data to the beginning and retry.
95          */
96         if (buffer->offset > buffer->alloc / 2) {
97                 memmove(buffer->buf, buffer->buf + buffer->offset,
98                         buffer->end - buffer->offset);
99                 buffer->end -= buffer->offset;
100                 buffer->offset = 0;
101                 goto restart;
102         }
103         /* Increase the size of the buffer and retry. */
104
105         newlen = buffer->alloc + len + 32768;
106         if (newlen > 0xa00000)
107                 fatal("buffer_append_space: alloc %u not supported",
108                    newlen);
109         buffer->buf = xrealloc(buffer->buf, newlen);
110         buffer->alloc = newlen;
111         goto restart;
112         /* NOTREACHED */
113 }
114
115 /* Returns the number of bytes of data in the buffer. */
116
117 u_int
118 buffer_len(Buffer *buffer)
119 {
120         return buffer->end - buffer->offset;
121 }
122
123 /* Gets data from the beginning of the buffer. */
124
125 void
126 buffer_get(Buffer *buffer, void *buf, u_int len)
127 {
128         if (len > buffer->end - buffer->offset)
129                 fatal("buffer_get: trying to get more bytes %d than in buffer %d",
130                     len, buffer->end - buffer->offset);
131         memcpy(buf, buffer->buf + buffer->offset, len);
132         buffer->offset += len;
133 }
134
135 /* Consumes the given number of bytes from the beginning of the buffer. */
136
137 void
138 buffer_consume(Buffer *buffer, u_int bytes)
139 {
140         if (bytes > buffer->end - buffer->offset)
141                 fatal("buffer_consume: trying to get more bytes than in buffer");
142         buffer->offset += bytes;
143 }
144
145 /* Consumes the given number of bytes from the end of the buffer. */
146
147 void
148 buffer_consume_end(Buffer *buffer, u_int bytes)
149 {
150         if (bytes > buffer->end - buffer->offset)
151                 fatal("buffer_consume_end: trying to get more bytes than in buffer");
152         buffer->end -= bytes;
153 }
154
155 /* Returns a pointer to the first used byte in the buffer. */
156
157 void *
158 buffer_ptr(Buffer *buffer)
159 {
160         return buffer->buf + buffer->offset;
161 }
162
163 /* Dumps the contents of the buffer to stderr. */
164
165 void
166 buffer_dump(Buffer *buffer)
167 {
168         int i;
169         u_char *ucp = buffer->buf;
170
171         for (i = buffer->offset; i < buffer->end; i++) {
172                 fprintf(stderr, "%02x", ucp[i]);
173                 if ((i-buffer->offset)%16==15)
174                         fprintf(stderr, "\r\n");
175                 else if ((i-buffer->offset)%2==1)
176                         fprintf(stderr, " ");
177         }
178         fprintf(stderr, "\r\n");
179 }