diff options
author | Colin Watson <cjwatson@debian.org> | 2005-05-25 11:01:01 +0000 |
---|---|---|
committer | Colin Watson <cjwatson@debian.org> | 2005-05-25 11:01:01 +0000 |
commit | e88de75a1a236779a10e8ccbcc51d25308be8840 (patch) | |
tree | 7495477a2a7d0cac17a9fcded020b6ea816182ef /buffer.c | |
parent | 30a0f9443782cd9d7308acd09430bf586186aa55 (diff) | |
parent | 5d05471f6657646d1d6500c7c43134462c407ee6 (diff) |
Merge 4.0p1 to the trunk.
Diffstat (limited to 'buffer.c')
-rw-r--r-- | buffer.c | 48 |
1 files changed, 38 insertions, 10 deletions
@@ -12,7 +12,7 @@ | |||
12 | */ | 12 | */ |
13 | 13 | ||
14 | #include "includes.h" | 14 | #include "includes.h" |
15 | RCSID("$OpenBSD: buffer.c,v 1.21 2003/11/21 11:57:03 djm Exp $"); | 15 | RCSID("$OpenBSD: buffer.c,v 1.22 2004/10/29 23:56:17 djm Exp $"); |
16 | 16 | ||
17 | #include "xmalloc.h" | 17 | #include "xmalloc.h" |
18 | #include "buffer.h" | 18 | #include "buffer.h" |
@@ -126,34 +126,62 @@ buffer_len(Buffer *buffer) | |||
126 | 126 | ||
127 | /* Gets data from the beginning of the buffer. */ | 127 | /* Gets data from the beginning of the buffer. */ |
128 | 128 | ||
129 | void | 129 | int |
130 | buffer_get(Buffer *buffer, void *buf, u_int len) | 130 | buffer_get_ret(Buffer *buffer, void *buf, u_int len) |
131 | { | 131 | { |
132 | if (len > buffer->end - buffer->offset) | 132 | if (len > buffer->end - buffer->offset) { |
133 | fatal("buffer_get: trying to get more bytes %d than in buffer %d", | 133 | error("buffer_get_ret: trying to get more bytes %d than in buffer %d", |
134 | len, buffer->end - buffer->offset); | 134 | len, buffer->end - buffer->offset); |
135 | return (-1); | ||
136 | } | ||
135 | memcpy(buf, buffer->buf + buffer->offset, len); | 137 | memcpy(buf, buffer->buf + buffer->offset, len); |
136 | buffer->offset += len; | 138 | buffer->offset += len; |
139 | return (0); | ||
140 | } | ||
141 | |||
142 | void | ||
143 | buffer_get(Buffer *buffer, void *buf, u_int len) | ||
144 | { | ||
145 | if (buffer_get_ret(buffer, buf, len) == -1) | ||
146 | fatal("buffer_get: buffer error"); | ||
137 | } | 147 | } |
138 | 148 | ||
139 | /* Consumes the given number of bytes from the beginning of the buffer. */ | 149 | /* Consumes the given number of bytes from the beginning of the buffer. */ |
140 | 150 | ||
151 | int | ||
152 | buffer_consume_ret(Buffer *buffer, u_int bytes) | ||
153 | { | ||
154 | if (bytes > buffer->end - buffer->offset) { | ||
155 | error("buffer_consume_ret: trying to get more bytes than in buffer"); | ||
156 | return (-1); | ||
157 | } | ||
158 | buffer->offset += bytes; | ||
159 | return (0); | ||
160 | } | ||
161 | |||
141 | void | 162 | void |
142 | buffer_consume(Buffer *buffer, u_int bytes) | 163 | buffer_consume(Buffer *buffer, u_int bytes) |
143 | { | 164 | { |
144 | if (bytes > buffer->end - buffer->offset) | 165 | if (buffer_consume_ret(buffer, bytes) == -1) |
145 | fatal("buffer_consume: trying to get more bytes than in buffer"); | 166 | fatal("buffer_consume: buffer error"); |
146 | buffer->offset += bytes; | ||
147 | } | 167 | } |
148 | 168 | ||
149 | /* Consumes the given number of bytes from the end of the buffer. */ | 169 | /* Consumes the given number of bytes from the end of the buffer. */ |
150 | 170 | ||
171 | int | ||
172 | buffer_consume_end_ret(Buffer *buffer, u_int bytes) | ||
173 | { | ||
174 | if (bytes > buffer->end - buffer->offset) | ||
175 | return (-1); | ||
176 | buffer->end -= bytes; | ||
177 | return (0); | ||
178 | } | ||
179 | |||
151 | void | 180 | void |
152 | buffer_consume_end(Buffer *buffer, u_int bytes) | 181 | buffer_consume_end(Buffer *buffer, u_int bytes) |
153 | { | 182 | { |
154 | if (bytes > buffer->end - buffer->offset) | 183 | if (buffer_consume_end_ret(buffer, bytes) == -1) |
155 | fatal("buffer_consume_end: trying to get more bytes than in buffer"); | 184 | fatal("buffer_consume_end: trying to get more bytes than in buffer"); |
156 | buffer->end -= bytes; | ||
157 | } | 185 | } |
158 | 186 | ||
159 | /* Returns a pointer to the first used byte in the buffer. */ | 187 | /* Returns a pointer to the first used byte in the buffer. */ |