|
Lines 66-71
Link Here
|
| 66 |
memcpy(p, data, len); |
66 |
memcpy(p, data, len); |
| 67 |
} |
67 |
} |
| 68 |
|
68 |
|
|
|
69 |
/* Shuffle data to the start of the buffer. */ |
| 70 |
|
| 71 |
static void |
| 72 |
buffer_defragment(Buffer *buffer) |
| 73 |
{ |
| 74 |
memmove(buffer->buf, buffer->buf + buffer->offset, |
| 75 |
buffer->end - buffer->offset); |
| 76 |
buffer->end -= buffer->offset; |
| 77 |
buffer->offset = 0; |
| 78 |
} |
| 79 |
|
| 69 |
/* |
80 |
/* |
| 70 |
* Appends space to the buffer, expanding the buffer if necessary. This does |
81 |
* Appends space to the buffer, expanding the buffer if necessary. This does |
| 71 |
* not actually copy the data into the buffer, but instead returns a pointer |
82 |
* not actually copy the data into the buffer, but instead returns a pointer |
|
Lines 98-115
Link Here
|
| 98 |
* data to the beginning and retry. |
109 |
* data to the beginning and retry. |
| 99 |
*/ |
110 |
*/ |
| 100 |
if (buffer->offset > MIN(buffer->alloc, BUFFER_MAX_CHUNK)) { |
111 |
if (buffer->offset > MIN(buffer->alloc, BUFFER_MAX_CHUNK)) { |
| 101 |
memmove(buffer->buf, buffer->buf + buffer->offset, |
112 |
buffer_defragment(buffer); |
| 102 |
buffer->end - buffer->offset); |
|
|
| 103 |
buffer->end -= buffer->offset; |
| 104 |
buffer->offset = 0; |
| 105 |
goto restart; |
113 |
goto restart; |
| 106 |
} |
114 |
} |
| 107 |
/* Increase the size of the buffer and retry. */ |
|
|
| 108 |
|
115 |
|
|
|
116 |
/* Increase the size of the buffer and retry. */ |
| 109 |
newlen = buffer->alloc + len + 32768; |
117 |
newlen = buffer->alloc + len + 32768; |
| 110 |
if (newlen > BUFFER_MAX_LEN) |
118 |
if (newlen > BUFFER_MAX_LEN) { |
| 111 |
fatal("buffer_append_space: alloc %u not supported", |
119 |
if (buffer->offset > 0) { |
| 112 |
newlen); |
120 |
buffer_defragment(buffer); |
|
|
121 |
goto restart; |
| 122 |
} else { |
| 123 |
fatal("buffer_append_space: alloc %u not supported", |
| 124 |
newlen); |
| 125 |
} |
| 126 |
} |
| 113 |
buffer->buf = xrealloc(buffer->buf, newlen); |
127 |
buffer->buf = xrealloc(buffer->buf, newlen); |
| 114 |
buffer->alloc = newlen; |
128 |
buffer->alloc = newlen; |
| 115 |
goto restart; |
129 |
goto restart; |
|
Lines 124-129
Link Here
|
| 124 |
return buffer->end - buffer->offset; |
138 |
return buffer->end - buffer->offset; |
| 125 |
} |
139 |
} |
| 126 |
|
140 |
|
|
|
141 |
/* The maximum potential space left in buffer. */ |
| 142 |
|
| 143 |
u_int |
| 144 |
buffer_potential_free_space(Buffer *buffer) |
| 145 |
{ |
| 146 |
return BUFFER_MAX_LEN - buffer_len(buffer); |
| 147 |
} |
| 148 |
|
| 127 |
/* Gets data from the beginning of the buffer. */ |
149 |
/* Gets data from the beginning of the buffer. */ |
| 128 |
|
150 |
|
| 129 |
int |
151 |
int |