174
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1
|
/*
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
2
|
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
3
|
*
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
4
|
* Copyright 2021 Mike Becker, Olaf Wintermann All rights reserved.
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
5
|
*
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
6
|
* Redistribution and use in source and binary forms, with or without
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
7
|
* modification, are permitted provided that the following conditions are met:
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
8
|
*
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
9
|
* 1. Redistributions of source code must retain the above copyright
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
10
|
* notice, this list of conditions and the following disclaimer.
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
11
|
*
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
12
|
* 2. Redistributions in binary form must reproduce the above copyright
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
13
|
* notice, this list of conditions and the following disclaimer in the
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
14
|
* documentation and/or other materials provided with the distribution.
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
15
|
*
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
16
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
17
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
18
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
19
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
20
|
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
21
|
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
22
|
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
23
|
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
24
|
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
25
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
26
|
* POSSIBILITY OF SUCH DAMAGE.
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
27
|
*/
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
28
|
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
29
|
#include "cx/buffer.h"
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
30
|
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
31
|
#include <stdio.h>
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
32
|
#include <string.h>
|
440
|
33
|
#include <errno.h>
|
|
34
|
|
|
35
|
static int buffer_copy_on_write(CxBuffer* buffer) {
|
|
36
|
if (0 == (buffer->flags & CX_BUFFER_COPY_ON_WRITE)) return 0;
|
|
37
|
void *newspace = cxMalloc(buffer->allocator, buffer->capacity);
|
|
38
|
if (NULL == newspace) return -1;
|
|
39
|
memcpy(newspace, buffer->space, buffer->size);
|
|
40
|
buffer->space = newspace;
|
|
41
|
buffer->flags &= ~CX_BUFFER_COPY_ON_WRITE;
|
|
42
|
buffer->flags |= CX_BUFFER_FREE_CONTENTS;
|
|
43
|
return 0;
|
|
44
|
}
|
174
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
45
|
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
46
|
int cxBufferInit(
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
47
|
CxBuffer *buffer,
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
48
|
void *space,
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
49
|
size_t capacity,
|
324
|
50
|
const CxAllocator *allocator,
|
174
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
51
|
int flags
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
52
|
) {
|
440
|
53
|
if (allocator == NULL) {
|
|
54
|
allocator = cxDefaultAllocator;
|
|
55
|
}
|
|
56
|
if (flags & CX_BUFFER_COPY_ON_EXTEND) {
|
|
57
|
flags |= CX_BUFFER_AUTO_EXTEND;
|
|
58
|
}
|
174
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
59
|
buffer->allocator = allocator;
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
60
|
buffer->flags = flags;
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
61
|
if (!space) {
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
62
|
buffer->bytes = cxMalloc(allocator, capacity);
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
63
|
if (buffer->bytes == NULL) {
|
440
|
64
|
return -1; // LCOV_EXCL_LINE
|
174
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
65
|
}
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
66
|
buffer->flags |= CX_BUFFER_FREE_CONTENTS;
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
67
|
} else {
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
68
|
buffer->bytes = space;
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
69
|
}
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
70
|
buffer->capacity = capacity;
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
71
|
buffer->size = 0;
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
72
|
buffer->pos = 0;
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
73
|
|
440
|
74
|
buffer->flush = NULL;
|
174
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
75
|
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
76
|
return 0;
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
77
|
}
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
78
|
|
440
|
79
|
int cxBufferEnableFlushing(
|
|
80
|
CxBuffer *buffer,
|
|
81
|
CxBufferFlushConfig config
|
|
82
|
) {
|
|
83
|
buffer->flush = malloc(sizeof(CxBufferFlushConfig));
|
|
84
|
if (buffer->flush == NULL) return -1; // LCOV_EXCL_LINE
|
|
85
|
memcpy(buffer->flush, &config, sizeof(CxBufferFlushConfig));
|
|
86
|
return 0;
|
|
87
|
}
|
|
88
|
|
174
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
89
|
void cxBufferDestroy(CxBuffer *buffer) {
|
440
|
90
|
if (buffer->flags & CX_BUFFER_FREE_CONTENTS) {
|
174
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
91
|
cxFree(buffer->allocator, buffer->bytes);
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
92
|
}
|
440
|
93
|
free(buffer->flush);
|
|
94
|
memset(buffer, 0, sizeof(CxBuffer));
|
174
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
95
|
}
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
96
|
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
97
|
CxBuffer *cxBufferCreate(
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
98
|
void *space,
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
99
|
size_t capacity,
|
324
|
100
|
const CxAllocator *allocator,
|
174
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
101
|
int flags
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
102
|
) {
|
440
|
103
|
if (allocator == NULL) {
|
|
104
|
allocator = cxDefaultAllocator;
|
|
105
|
}
|
174
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
106
|
CxBuffer *buf = cxMalloc(allocator, sizeof(CxBuffer));
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
107
|
if (buf == NULL) return NULL;
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
108
|
if (0 == cxBufferInit(buf, space, capacity, allocator, flags)) {
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
109
|
return buf;
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
110
|
} else {
|
440
|
111
|
// LCOV_EXCL_START
|
174
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
112
|
cxFree(allocator, buf);
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
113
|
return NULL;
|
440
|
114
|
// LCOV_EXCL_STOP
|
174
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
115
|
}
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
116
|
}
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
117
|
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
118
|
void cxBufferFree(CxBuffer *buffer) {
|
440
|
119
|
if (buffer == NULL) return;
|
|
120
|
const CxAllocator *allocator = buffer->allocator;
|
|
121
|
cxBufferDestroy(buffer);
|
|
122
|
cxFree(allocator, buffer);
|
174
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
123
|
}
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
124
|
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
125
|
int cxBufferSeek(
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
126
|
CxBuffer *buffer,
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
127
|
off_t offset,
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
128
|
int whence
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
129
|
) {
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
130
|
size_t npos;
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
131
|
switch (whence) {
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
132
|
case SEEK_CUR:
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
133
|
npos = buffer->pos;
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
134
|
break;
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
135
|
case SEEK_END:
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
136
|
npos = buffer->size;
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
137
|
break;
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
138
|
case SEEK_SET:
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
139
|
npos = 0;
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
140
|
break;
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
141
|
default:
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
142
|
return -1;
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
143
|
}
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
144
|
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
145
|
size_t opos = npos;
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
146
|
npos += offset;
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
147
|
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
148
|
if ((offset > 0 && npos < opos) || (offset < 0 && npos > opos)) {
|
440
|
149
|
errno = EOVERFLOW;
|
174
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
150
|
return -1;
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
151
|
}
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
152
|
|
440
|
153
|
if (npos > buffer->size) {
|
174
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
154
|
return -1;
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
155
|
} else {
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
156
|
buffer->pos = npos;
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
157
|
return 0;
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
158
|
}
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
159
|
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
160
|
}
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
161
|
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
162
|
void cxBufferClear(CxBuffer *buffer) {
|
440
|
163
|
if (0 == (buffer->flags & CX_BUFFER_COPY_ON_WRITE)) {
|
|
164
|
memset(buffer->bytes, 0, buffer->size);
|
|
165
|
}
|
174
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
166
|
buffer->size = 0;
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
167
|
buffer->pos = 0;
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
168
|
}
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
169
|
|
253
|
170
|
void cxBufferReset(CxBuffer *buffer) {
|
|
171
|
buffer->size = 0;
|
|
172
|
buffer->pos = 0;
|
|
173
|
}
|
|
174
|
|
440
|
175
|
bool cxBufferEof(const CxBuffer *buffer) {
|
174
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
176
|
return buffer->pos >= buffer->size;
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
177
|
}
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
178
|
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
179
|
int cxBufferMinimumCapacity(
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
180
|
CxBuffer *buffer,
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
181
|
size_t newcap
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
182
|
) {
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
183
|
if (newcap <= buffer->capacity) {
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
184
|
return 0;
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
185
|
}
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
186
|
|
440
|
187
|
const int force_copy_flags = CX_BUFFER_COPY_ON_WRITE | CX_BUFFER_COPY_ON_EXTEND;
|
|
188
|
if (buffer->flags & force_copy_flags) {
|
|
189
|
void *newspace = cxMalloc(buffer->allocator, newcap);
|
|
190
|
if (NULL == newspace) return -1;
|
|
191
|
memcpy(newspace, buffer->space, buffer->size);
|
|
192
|
buffer->space = newspace;
|
|
193
|
buffer->capacity = newcap;
|
|
194
|
buffer->flags &= ~force_copy_flags;
|
|
195
|
buffer->flags |= CX_BUFFER_FREE_CONTENTS;
|
|
196
|
return 0;
|
|
197
|
} else if (cxReallocate(buffer->allocator,
|
174
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
198
|
(void **) &buffer->bytes, newcap) == 0) {
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
199
|
buffer->capacity = newcap;
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
200
|
return 0;
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
201
|
} else {
|
440
|
202
|
return -1; // LCOV_EXCL_LINE
|
174
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
203
|
}
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
204
|
}
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
205
|
|
440
|
206
|
static size_t cx_buffer_flush_helper(
|
|
207
|
const CxBuffer *buffer,
|
174
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
208
|
size_t size,
|
440
|
209
|
const unsigned char *src,
|
174
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
210
|
size_t nitems
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
211
|
) {
|
440
|
212
|
// flush data from an arbitrary source
|
|
213
|
// does not need to be the buffer's contents
|
|
214
|
size_t max_items = buffer->flush->blksize / size;
|
|
215
|
size_t fblocks = 0;
|
|
216
|
size_t flushed_total = 0;
|
|
217
|
while (nitems > 0 && fblocks < buffer->flush->blkmax) {
|
|
218
|
fblocks++;
|
|
219
|
size_t items = nitems > max_items ? max_items : nitems;
|
|
220
|
size_t flushed = buffer->flush->wfunc(
|
|
221
|
src, size, items, buffer->flush->target);
|
174
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
222
|
if (flushed > 0) {
|
440
|
223
|
flushed_total += flushed;
|
|
224
|
src += flushed * size;
|
|
225
|
nitems -= flushed;
|
174
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
226
|
} else {
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
227
|
// if no bytes can be flushed out anymore, we give up
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
228
|
break;
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
229
|
}
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
230
|
}
|
440
|
231
|
return flushed_total;
|
|
232
|
}
|
|
233
|
|
|
234
|
static size_t cx_buffer_flush_impl(CxBuffer *buffer, size_t size) {
|
|
235
|
// flush the current contents of the buffer
|
|
236
|
unsigned char *space = buffer->bytes;
|
|
237
|
size_t remaining = buffer->pos / size;
|
|
238
|
size_t flushed_total = cx_buffer_flush_helper(
|
|
239
|
buffer, size, space, remaining);
|
|
240
|
|
|
241
|
// shift the buffer left after flushing
|
|
242
|
// IMPORTANT: up to this point, copy on write must have been
|
|
243
|
// performed already, because we can't do error handling here
|
|
244
|
cxBufferShiftLeft(buffer, flushed_total*size);
|
|
245
|
|
|
246
|
return flushed_total;
|
|
247
|
}
|
|
248
|
|
|
249
|
size_t cxBufferFlush(CxBuffer *buffer) {
|
|
250
|
if (buffer_copy_on_write(buffer)) return 0;
|
|
251
|
return cx_buffer_flush_impl(buffer, 1);
|
174
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
252
|
}
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
253
|
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
254
|
size_t cxBufferWrite(
|
324
|
255
|
const void *ptr,
|
174
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
256
|
size_t size,
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
257
|
size_t nitems,
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
258
|
CxBuffer *buffer
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
259
|
) {
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
260
|
// optimize for easy case
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
261
|
if (size == 1 && (buffer->capacity - buffer->pos) >= nitems) {
|
440
|
262
|
if (buffer_copy_on_write(buffer)) return 0;
|
174
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
263
|
memcpy(buffer->bytes + buffer->pos, ptr, nitems);
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
264
|
buffer->pos += nitems;
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
265
|
if (buffer->pos > buffer->size) {
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
266
|
buffer->size = buffer->pos;
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
267
|
}
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
268
|
return nitems;
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
269
|
}
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
270
|
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
271
|
size_t len;
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
272
|
if (cx_szmul(size, nitems, &len)) {
|
440
|
273
|
errno = EOVERFLOW;
|
|
274
|
return 0;
|
|
275
|
}
|
|
276
|
if (buffer->pos > SIZE_MAX - len) {
|
|
277
|
errno = EOVERFLOW;
|
174
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
278
|
return 0;
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
279
|
}
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
280
|
size_t required = buffer->pos + len;
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
281
|
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
282
|
bool perform_flush = false;
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
283
|
if (required > buffer->capacity) {
|
440
|
284
|
if (buffer->flags & CX_BUFFER_AUTO_EXTEND) {
|
|
285
|
if (buffer->flush != NULL && required > buffer->flush->threshold) {
|
174
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
286
|
perform_flush = true;
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
287
|
} else {
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
288
|
if (cxBufferMinimumCapacity(buffer, required)) {
|
440
|
289
|
return 0; // LCOV_EXCL_LINE
|
174
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
290
|
}
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
291
|
}
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
292
|
} else {
|
440
|
293
|
if (buffer->flush != NULL) {
|
174
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
294
|
perform_flush = true;
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
295
|
} else {
|
440
|
296
|
// truncate data, if we can neither extend nor flush
|
174
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
297
|
len = buffer->capacity - buffer->pos;
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
298
|
if (size > 1) {
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
299
|
len -= len % size;
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
300
|
}
|
440
|
301
|
nitems = len / size;
|
174
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
302
|
}
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
303
|
}
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
304
|
}
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
305
|
|
440
|
306
|
// check here and not above because of possible truncation
|
174
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
307
|
if (len == 0) {
|
440
|
308
|
return 0;
|
174
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
309
|
}
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
310
|
|
440
|
311
|
// check if we need to copy
|
|
312
|
if (buffer_copy_on_write(buffer)) return 0;
|
174
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
313
|
|
440
|
314
|
// perform the operation
|
|
315
|
if (perform_flush) {
|
|
316
|
size_t items_flush;
|
|
317
|
if (buffer->pos == 0) {
|
|
318
|
// if we don't have data in the buffer, but are instructed
|
|
319
|
// to flush, it means that we are supposed to relay the data
|
|
320
|
items_flush = cx_buffer_flush_helper(buffer, size, ptr, nitems);
|
|
321
|
if (items_flush == 0) {
|
|
322
|
// we needed to flush, but could not flush anything
|
|
323
|
// give up and avoid endless trying
|
|
324
|
return 0;
|
174
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
325
|
}
|
440
|
326
|
size_t ritems = nitems - items_flush;
|
|
327
|
const unsigned char *rest = ptr;
|
|
328
|
rest += items_flush * size;
|
|
329
|
return items_flush + cxBufferWrite(rest, size, ritems, buffer);
|
|
330
|
} else {
|
|
331
|
items_flush = cx_buffer_flush_impl(buffer, size);
|
|
332
|
if (items_flush == 0) {
|
|
333
|
return 0;
|
174
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
334
|
}
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
335
|
return cxBufferWrite(ptr, size, nitems, buffer);
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
336
|
}
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
337
|
} else {
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
338
|
memcpy(buffer->bytes + buffer->pos, ptr, len);
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
339
|
buffer->pos += len;
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
340
|
if (buffer->pos > buffer->size) {
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
341
|
buffer->size = buffer->pos;
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
342
|
}
|
440
|
343
|
return nitems;
|
174
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
344
|
}
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
345
|
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
346
|
}
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
347
|
|
440
|
348
|
size_t cxBufferAppend(
|
|
349
|
const void *ptr,
|
|
350
|
size_t size,
|
|
351
|
size_t nitems,
|
|
352
|
CxBuffer *buffer
|
|
353
|
) {
|
|
354
|
size_t pos = buffer->pos;
|
|
355
|
buffer->pos = buffer->size;
|
|
356
|
size_t written = cxBufferWrite(ptr, size, nitems, buffer);
|
|
357
|
buffer->pos = pos;
|
|
358
|
return written;
|
|
359
|
}
|
|
360
|
|
174
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
361
|
int cxBufferPut(
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
362
|
CxBuffer *buffer,
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
363
|
int c
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
364
|
) {
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
365
|
c &= 0xFF;
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
366
|
unsigned char const ch = c;
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
367
|
if (cxBufferWrite(&ch, 1, 1, buffer) == 1) {
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
368
|
return c;
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
369
|
} else {
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
370
|
return EOF;
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
371
|
}
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
372
|
}
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
373
|
|
440
|
374
|
int cxBufferTerminate(CxBuffer *buffer) {
|
|
375
|
bool success = 0 == cxBufferPut(buffer, 0);
|
|
376
|
if (success) {
|
|
377
|
buffer->pos--;
|
|
378
|
buffer->size--;
|
|
379
|
return 0;
|
|
380
|
} else {
|
|
381
|
return -1;
|
|
382
|
}
|
|
383
|
}
|
|
384
|
|
174
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
385
|
size_t cxBufferPutString(
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
386
|
CxBuffer *buffer,
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
387
|
const char *str
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
388
|
) {
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
389
|
return cxBufferWrite(str, 1, strlen(str), buffer);
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
390
|
}
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
391
|
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
392
|
size_t cxBufferRead(
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
393
|
void *ptr,
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
394
|
size_t size,
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
395
|
size_t nitems,
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
396
|
CxBuffer *buffer
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
397
|
) {
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
398
|
size_t len;
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
399
|
if (cx_szmul(size, nitems, &len)) {
|
440
|
400
|
errno = EOVERFLOW;
|
174
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
401
|
return 0;
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
402
|
}
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
403
|
if (buffer->pos + len > buffer->size) {
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
404
|
len = buffer->size - buffer->pos;
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
405
|
if (size > 1) len -= len % size;
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
406
|
}
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
407
|
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
408
|
if (len <= 0) {
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
409
|
return len;
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
410
|
}
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
411
|
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
412
|
memcpy(ptr, buffer->bytes + buffer->pos, len);
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
413
|
buffer->pos += len;
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
414
|
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
415
|
return len / size;
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
416
|
}
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
417
|
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
418
|
int cxBufferGet(CxBuffer *buffer) {
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
419
|
if (cxBufferEof(buffer)) {
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
420
|
return EOF;
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
421
|
} else {
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
422
|
int c = buffer->bytes[buffer->pos];
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
423
|
buffer->pos++;
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
424
|
return c;
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
425
|
}
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
426
|
}
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
427
|
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
428
|
int cxBufferShiftLeft(
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
429
|
CxBuffer *buffer,
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
430
|
size_t shift
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
431
|
) {
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
432
|
if (shift >= buffer->size) {
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
433
|
buffer->pos = buffer->size = 0;
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
434
|
} else {
|
440
|
435
|
if (buffer_copy_on_write(buffer)) return -1;
|
174
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
436
|
memmove(buffer->bytes, buffer->bytes + shift, buffer->size - shift);
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
437
|
buffer->size -= shift;
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
438
|
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
439
|
if (buffer->pos >= shift) {
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
440
|
buffer->pos -= shift;
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
441
|
} else {
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
442
|
buffer->pos = 0;
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
443
|
}
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
444
|
}
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
445
|
return 0;
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
446
|
}
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
447
|
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
448
|
int cxBufferShiftRight(
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
449
|
CxBuffer *buffer,
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
450
|
size_t shift
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
451
|
) {
|
440
|
452
|
if (buffer->size > SIZE_MAX - shift) {
|
|
453
|
errno = EOVERFLOW;
|
|
454
|
return -1;
|
|
455
|
}
|
174
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
456
|
size_t req_capacity = buffer->size + shift;
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
457
|
size_t movebytes;
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
458
|
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
459
|
// auto extend buffer, if required and enabled
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
460
|
if (buffer->capacity < req_capacity) {
|
440
|
461
|
if (buffer->flags & CX_BUFFER_AUTO_EXTEND) {
|
174
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
462
|
if (cxBufferMinimumCapacity(buffer, req_capacity)) {
|
440
|
463
|
return -1; // LCOV_EXCL_LINE
|
174
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
464
|
}
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
465
|
movebytes = buffer->size;
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
466
|
} else {
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
467
|
movebytes = buffer->capacity - shift;
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
468
|
}
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
469
|
} else {
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
470
|
movebytes = buffer->size;
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
471
|
}
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
472
|
|
440
|
473
|
if (movebytes > 0) {
|
|
474
|
if (buffer_copy_on_write(buffer)) return -1;
|
|
475
|
memmove(buffer->bytes + shift, buffer->bytes, movebytes);
|
|
476
|
buffer->size = shift + movebytes;
|
|
477
|
}
|
174
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
478
|
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
479
|
buffer->pos += shift;
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
480
|
if (buffer->pos > buffer->size) {
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
481
|
buffer->pos = buffer->size;
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
482
|
}
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
483
|
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
484
|
return 0;
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
485
|
}
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
486
|
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
487
|
int cxBufferShift(
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
488
|
CxBuffer *buffer,
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
489
|
off_t shift
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
490
|
) {
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
491
|
if (shift < 0) {
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
492
|
return cxBufferShiftLeft(buffer, (size_t) (-shift));
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
493
|
} else if (shift > 0) {
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
494
|
return cxBufferShiftRight(buffer, (size_t) shift);
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
495
|
} else {
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
496
|
return 0;
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
497
|
}
|
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
498
|
}
|