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