Thu, 11 Jul 2019 10:11:43 +0200
resets revision number
103
08018864fb91
added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents:
95
diff
changeset
|
1 | /* |
08018864fb91
added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents:
95
diff
changeset
|
2 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
08018864fb91
added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents:
95
diff
changeset
|
3 | * |
259 | 4 | * Copyright 2017 Mike Becker, Olaf Wintermann All rights reserved. |
103
08018864fb91
added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents:
95
diff
changeset
|
5 | * |
08018864fb91
added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents:
95
diff
changeset
|
6 | * Redistribution and use in source and binary forms, with or without |
08018864fb91
added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents:
95
diff
changeset
|
7 | * modification, are permitted provided that the following conditions are met: |
08018864fb91
added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents:
95
diff
changeset
|
8 | * |
08018864fb91
added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents:
95
diff
changeset
|
9 | * 1. Redistributions of source code must retain the above copyright |
08018864fb91
added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents:
95
diff
changeset
|
10 | * notice, this list of conditions and the following disclaimer. |
08018864fb91
added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents:
95
diff
changeset
|
11 | * |
08018864fb91
added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents:
95
diff
changeset
|
12 | * 2. Redistributions in binary form must reproduce the above copyright |
08018864fb91
added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents:
95
diff
changeset
|
13 | * notice, this list of conditions and the following disclaimer in the |
08018864fb91
added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents:
95
diff
changeset
|
14 | * documentation and/or other materials provided with the distribution. |
08018864fb91
added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents:
95
diff
changeset
|
15 | * |
08018864fb91
added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents:
95
diff
changeset
|
16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
08018864fb91
added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents:
95
diff
changeset
|
17 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
08018864fb91
added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents:
95
diff
changeset
|
18 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
08018864fb91
added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents:
95
diff
changeset
|
19 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
08018864fb91
added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents:
95
diff
changeset
|
20 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
08018864fb91
added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents:
95
diff
changeset
|
21 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
08018864fb91
added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents:
95
diff
changeset
|
22 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
08018864fb91
added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents:
95
diff
changeset
|
23 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
08018864fb91
added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents:
95
diff
changeset
|
24 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
08018864fb91
added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents:
95
diff
changeset
|
25 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
08018864fb91
added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents:
95
diff
changeset
|
26 | * POSSIBILITY OF SUCH DAMAGE. |
08018864fb91
added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents:
95
diff
changeset
|
27 | */ |
08018864fb91
added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents:
95
diff
changeset
|
28 | |
251
fae240d633fc
changes source directory structure in preperation for autotools rollout
Mike Becker <universe@uap-core.de>
parents:
250
diff
changeset
|
29 | #include "ucx/buffer.h" |
fae240d633fc
changes source directory structure in preperation for autotools rollout
Mike Becker <universe@uap-core.de>
parents:
250
diff
changeset
|
30 | |
56
76caac0da4a0
added memstream to ucx - still little work to do
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
31 | #include <stdarg.h> |
76caac0da4a0
added memstream to ucx - still little work to do
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
32 | #include <stdlib.h> |
76caac0da4a0
added memstream to ucx - still little work to do
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
33 | #include <string.h> |
76caac0da4a0
added memstream to ucx - still little work to do
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
34 | |
169
279dd3ca7a77
fixed issues revealed by buffer tests
Mike Becker <universe@uap-core.de>
parents:
140
diff
changeset
|
35 | UcxBuffer *ucx_buffer_new(void *space, size_t capacity, int flags) { |
60
abae4669fba7
renamed UcxMemstream to UcxBuffer
Mike Becker <universe@uap-core.de>
parents:
58
diff
changeset
|
36 | UcxBuffer *buffer = (UcxBuffer*) malloc(sizeof(UcxBuffer)); |
abae4669fba7
renamed UcxMemstream to UcxBuffer
Mike Becker <universe@uap-core.de>
parents:
58
diff
changeset
|
37 | if (buffer) { |
61
fb07a0ab9a17
added flag field to buffer
Mike Becker <universe@uap-core.de>
parents:
60
diff
changeset
|
38 | buffer->flags = flags; |
56
76caac0da4a0
added memstream to ucx - still little work to do
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
39 | if (!space) { |
169
279dd3ca7a77
fixed issues revealed by buffer tests
Mike Becker <universe@uap-core.de>
parents:
140
diff
changeset
|
40 | buffer->space = (char*)malloc(capacity); |
60
abae4669fba7
renamed UcxMemstream to UcxBuffer
Mike Becker <universe@uap-core.de>
parents:
58
diff
changeset
|
41 | if (!buffer->space) { |
abae4669fba7
renamed UcxMemstream to UcxBuffer
Mike Becker <universe@uap-core.de>
parents:
58
diff
changeset
|
42 | free(buffer); |
56
76caac0da4a0
added memstream to ucx - still little work to do
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
43 | return NULL; |
76caac0da4a0
added memstream to ucx - still little work to do
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
44 | } |
169
279dd3ca7a77
fixed issues revealed by buffer tests
Mike Becker <universe@uap-core.de>
parents:
140
diff
changeset
|
45 | memset(buffer->space, 0, capacity); |
61
fb07a0ab9a17
added flag field to buffer
Mike Becker <universe@uap-core.de>
parents:
60
diff
changeset
|
46 | buffer->flags |= UCX_BUFFER_AUTOFREE; |
56
76caac0da4a0
added memstream to ucx - still little work to do
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
47 | } else { |
78 | 48 | buffer->space = (char*)space; |
56
76caac0da4a0
added memstream to ucx - still little work to do
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
49 | } |
169
279dd3ca7a77
fixed issues revealed by buffer tests
Mike Becker <universe@uap-core.de>
parents:
140
diff
changeset
|
50 | buffer->capacity = capacity; |
76 | 51 | buffer->size = 0; |
56
76caac0da4a0
added memstream to ucx - still little work to do
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
52 | |
60
abae4669fba7
renamed UcxMemstream to UcxBuffer
Mike Becker <universe@uap-core.de>
parents:
58
diff
changeset
|
53 | buffer->pos = 0; |
56
76caac0da4a0
added memstream to ucx - still little work to do
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
54 | } |
76caac0da4a0
added memstream to ucx - still little work to do
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
55 | |
60
abae4669fba7
renamed UcxMemstream to UcxBuffer
Mike Becker <universe@uap-core.de>
parents:
58
diff
changeset
|
56 | return buffer; |
56
76caac0da4a0
added memstream to ucx - still little work to do
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
57 | } |
76caac0da4a0
added memstream to ucx - still little work to do
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
58 | |
60
abae4669fba7
renamed UcxMemstream to UcxBuffer
Mike Becker <universe@uap-core.de>
parents:
58
diff
changeset
|
59 | void ucx_buffer_free(UcxBuffer *buffer) { |
63
1d3500806565
discarded fascism from buffer
Mike Becker <universe@uap-core.de>
parents:
62
diff
changeset
|
60 | if ((buffer->flags & UCX_BUFFER_AUTOFREE) == UCX_BUFFER_AUTOFREE) { |
60
abae4669fba7
renamed UcxMemstream to UcxBuffer
Mike Becker <universe@uap-core.de>
parents:
58
diff
changeset
|
61 | free(buffer->space); |
56
76caac0da4a0
added memstream to ucx - still little work to do
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
62 | } |
60
abae4669fba7
renamed UcxMemstream to UcxBuffer
Mike Becker <universe@uap-core.de>
parents:
58
diff
changeset
|
63 | free(buffer); |
56
76caac0da4a0
added memstream to ucx - still little work to do
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
64 | } |
76caac0da4a0
added memstream to ucx - still little work to do
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
65 | |
76 | 66 | UcxBuffer* ucx_buffer_extract( |
67 | UcxBuffer *src, size_t start, size_t length, int flags) { | |
239
1634c3ea89da
fixes integer overflow in ucx_buffer_extract
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
225
diff
changeset
|
68 | if (src->size == 0 || length == 0 || |
1634c3ea89da
fixes integer overflow in ucx_buffer_extract
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
225
diff
changeset
|
69 | ((size_t)-1) - start < length || start+length > src->capacity) |
1634c3ea89da
fixes integer overflow in ucx_buffer_extract
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
225
diff
changeset
|
70 | { |
62 | 71 | return NULL; |
72 | } | |
73 | ||
76 | 74 | UcxBuffer *dst = (UcxBuffer*) malloc(sizeof(UcxBuffer)); |
62 | 75 | if (dst) { |
78 | 76 | dst->space = (char*)malloc(length); |
62 | 77 | if (!dst->space) { |
78 | free(dst); | |
79 | return NULL; | |
80 | } | |
76 | 81 | dst->capacity = length; |
62 | 82 | dst->size = length; |
83 | dst->flags = flags | UCX_BUFFER_AUTOFREE; | |
84 | dst->pos = 0; | |
76 | 85 | memcpy(dst->space, src->space+start, length); |
62 | 86 | } |
87 | return dst; | |
88 | } | |
89 | ||
60
abae4669fba7
renamed UcxMemstream to UcxBuffer
Mike Becker <universe@uap-core.de>
parents:
58
diff
changeset
|
90 | int ucx_buffer_seek(UcxBuffer *buffer, off_t offset, int whence) { |
140
15f871f50bfd
completed documentation + changed API for buffer/stream generic copy functions
Mike Becker <universe@uap-core.de>
parents:
103
diff
changeset
|
91 | size_t npos; |
56
76caac0da4a0
added memstream to ucx - still little work to do
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
92 | switch (whence) { |
76caac0da4a0
added memstream to ucx - still little work to do
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
93 | case SEEK_CUR: |
60
abae4669fba7
renamed UcxMemstream to UcxBuffer
Mike Becker <universe@uap-core.de>
parents:
58
diff
changeset
|
94 | npos = buffer->pos; |
56
76caac0da4a0
added memstream to ucx - still little work to do
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
95 | break; |
76caac0da4a0
added memstream to ucx - still little work to do
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
96 | case SEEK_END: |
76 | 97 | npos = buffer->size; |
56
76caac0da4a0
added memstream to ucx - still little work to do
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
98 | break; |
169
279dd3ca7a77
fixed issues revealed by buffer tests
Mike Becker <universe@uap-core.de>
parents:
140
diff
changeset
|
99 | case SEEK_SET: |
279dd3ca7a77
fixed issues revealed by buffer tests
Mike Becker <universe@uap-core.de>
parents:
140
diff
changeset
|
100 | npos = 0; |
279dd3ca7a77
fixed issues revealed by buffer tests
Mike Becker <universe@uap-core.de>
parents:
140
diff
changeset
|
101 | break; |
140
15f871f50bfd
completed documentation + changed API for buffer/stream generic copy functions
Mike Becker <universe@uap-core.de>
parents:
103
diff
changeset
|
102 | default: |
169
279dd3ca7a77
fixed issues revealed by buffer tests
Mike Becker <universe@uap-core.de>
parents:
140
diff
changeset
|
103 | return -1; |
56
76caac0da4a0
added memstream to ucx - still little work to do
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
104 | } |
76caac0da4a0
added memstream to ucx - still little work to do
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
105 | |
169
279dd3ca7a77
fixed issues revealed by buffer tests
Mike Becker <universe@uap-core.de>
parents:
140
diff
changeset
|
106 | size_t opos = npos; |
56
76caac0da4a0
added memstream to ucx - still little work to do
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
107 | npos += offset; |
76 | 108 | |
169
279dd3ca7a77
fixed issues revealed by buffer tests
Mike Becker <universe@uap-core.de>
parents:
140
diff
changeset
|
109 | if ((offset > 0 && npos < opos) || (offset < 0 && npos > opos)) { |
279dd3ca7a77
fixed issues revealed by buffer tests
Mike Becker <universe@uap-core.de>
parents:
140
diff
changeset
|
110 | return -1; |
279dd3ca7a77
fixed issues revealed by buffer tests
Mike Becker <universe@uap-core.de>
parents:
140
diff
changeset
|
111 | } |
279dd3ca7a77
fixed issues revealed by buffer tests
Mike Becker <universe@uap-core.de>
parents:
140
diff
changeset
|
112 | |
279dd3ca7a77
fixed issues revealed by buffer tests
Mike Becker <universe@uap-core.de>
parents:
140
diff
changeset
|
113 | if (npos >= buffer->size) { |
56
76caac0da4a0
added memstream to ucx - still little work to do
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
114 | return -1; |
76caac0da4a0
added memstream to ucx - still little work to do
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
115 | } else { |
60
abae4669fba7
renamed UcxMemstream to UcxBuffer
Mike Becker <universe@uap-core.de>
parents:
58
diff
changeset
|
116 | buffer->pos = npos; |
56
76caac0da4a0
added memstream to ucx - still little work to do
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
117 | return 0; |
76caac0da4a0
added memstream to ucx - still little work to do
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
118 | } |
76caac0da4a0
added memstream to ucx - still little work to do
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
119 | |
76caac0da4a0
added memstream to ucx - still little work to do
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
120 | } |
76caac0da4a0
added memstream to ucx - still little work to do
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
121 | |
60
abae4669fba7
renamed UcxMemstream to UcxBuffer
Mike Becker <universe@uap-core.de>
parents:
58
diff
changeset
|
122 | int ucx_buffer_eof(UcxBuffer *buffer) { |
62 | 123 | return buffer->pos >= buffer->size; |
56
76caac0da4a0
added memstream to ucx - still little work to do
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
124 | } |
76caac0da4a0
added memstream to ucx - still little work to do
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
125 | |
76 | 126 | int ucx_buffer_extend(UcxBuffer *buffer, size_t len) { |
127 | size_t newcap = buffer->capacity; | |
169
279dd3ca7a77
fixed issues revealed by buffer tests
Mike Becker <universe@uap-core.de>
parents:
140
diff
changeset
|
128 | |
279dd3ca7a77
fixed issues revealed by buffer tests
Mike Becker <universe@uap-core.de>
parents:
140
diff
changeset
|
129 | if (buffer->capacity + len < buffer->capacity) { |
279dd3ca7a77
fixed issues revealed by buffer tests
Mike Becker <universe@uap-core.de>
parents:
140
diff
changeset
|
130 | return -1; |
279dd3ca7a77
fixed issues revealed by buffer tests
Mike Becker <universe@uap-core.de>
parents:
140
diff
changeset
|
131 | } |
279dd3ca7a77
fixed issues revealed by buffer tests
Mike Becker <universe@uap-core.de>
parents:
140
diff
changeset
|
132 | |
279dd3ca7a77
fixed issues revealed by buffer tests
Mike Becker <universe@uap-core.de>
parents:
140
diff
changeset
|
133 | while (buffer->capacity + len > newcap) { |
279dd3ca7a77
fixed issues revealed by buffer tests
Mike Becker <universe@uap-core.de>
parents:
140
diff
changeset
|
134 | newcap <<= 1; |
279dd3ca7a77
fixed issues revealed by buffer tests
Mike Becker <universe@uap-core.de>
parents:
140
diff
changeset
|
135 | if (newcap < buffer->capacity) { |
279dd3ca7a77
fixed issues revealed by buffer tests
Mike Becker <universe@uap-core.de>
parents:
140
diff
changeset
|
136 | return -1; |
279dd3ca7a77
fixed issues revealed by buffer tests
Mike Becker <universe@uap-core.de>
parents:
140
diff
changeset
|
137 | } |
279dd3ca7a77
fixed issues revealed by buffer tests
Mike Becker <universe@uap-core.de>
parents:
140
diff
changeset
|
138 | } |
76 | 139 | |
78 | 140 | char *newspace = (char*)realloc(buffer->space, newcap); |
76 | 141 | if (newspace) { |
142 | memset(newspace+buffer->size, 0, newcap-buffer->size); | |
143 | buffer->space = newspace; | |
144 | buffer->capacity = newcap; | |
145 | } else { | |
146 | return -1; | |
147 | } | |
148 | ||
149 | return 0; | |
150 | } | |
151 | ||
152 | size_t ucx_buffer_write(const void *ptr, size_t size, size_t nitems, | |
153 | UcxBuffer *buffer) { | |
270
3d80d425543b
adds integer overflow checks
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
269
diff
changeset
|
154 | size_t len; |
3d80d425543b
adds integer overflow checks
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
269
diff
changeset
|
155 | if(ucx_szmul(size, nitems, &len)) { |
3d80d425543b
adds integer overflow checks
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
269
diff
changeset
|
156 | return 0; |
3d80d425543b
adds integer overflow checks
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
269
diff
changeset
|
157 | } |
169
279dd3ca7a77
fixed issues revealed by buffer tests
Mike Becker <universe@uap-core.de>
parents:
140
diff
changeset
|
158 | size_t required = buffer->pos + len; |
279dd3ca7a77
fixed issues revealed by buffer tests
Mike Becker <universe@uap-core.de>
parents:
140
diff
changeset
|
159 | if (buffer->pos > required) { |
279dd3ca7a77
fixed issues revealed by buffer tests
Mike Becker <universe@uap-core.de>
parents:
140
diff
changeset
|
160 | return 0; |
279dd3ca7a77
fixed issues revealed by buffer tests
Mike Becker <universe@uap-core.de>
parents:
140
diff
changeset
|
161 | } |
279dd3ca7a77
fixed issues revealed by buffer tests
Mike Becker <universe@uap-core.de>
parents:
140
diff
changeset
|
162 | |
279dd3ca7a77
fixed issues revealed by buffer tests
Mike Becker <universe@uap-core.de>
parents:
140
diff
changeset
|
163 | if (required > buffer->capacity) { |
76 | 164 | if ((buffer->flags & UCX_BUFFER_AUTOEXTEND) == UCX_BUFFER_AUTOEXTEND) { |
169
279dd3ca7a77
fixed issues revealed by buffer tests
Mike Becker <universe@uap-core.de>
parents:
140
diff
changeset
|
165 | if (ucx_buffer_extend(buffer, required - buffer->capacity)) { |
279dd3ca7a77
fixed issues revealed by buffer tests
Mike Becker <universe@uap-core.de>
parents:
140
diff
changeset
|
166 | return 0; |
64
16590c9c497c
added autoextend feature to buffer
Mike Becker <universe@uap-core.de>
parents:
63
diff
changeset
|
167 | } |
16590c9c497c
added autoextend feature to buffer
Mike Becker <universe@uap-core.de>
parents:
63
diff
changeset
|
168 | } else { |
76 | 169 | len = buffer->capacity - buffer->pos; |
169
279dd3ca7a77
fixed issues revealed by buffer tests
Mike Becker <universe@uap-core.de>
parents:
140
diff
changeset
|
170 | if (size > 1) { |
279dd3ca7a77
fixed issues revealed by buffer tests
Mike Becker <universe@uap-core.de>
parents:
140
diff
changeset
|
171 | len -= len%size; |
279dd3ca7a77
fixed issues revealed by buffer tests
Mike Becker <universe@uap-core.de>
parents:
140
diff
changeset
|
172 | } |
64
16590c9c497c
added autoextend feature to buffer
Mike Becker <universe@uap-core.de>
parents:
63
diff
changeset
|
173 | } |
56
76caac0da4a0
added memstream to ucx - still little work to do
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
174 | } |
76 | 175 | |
169
279dd3ca7a77
fixed issues revealed by buffer tests
Mike Becker <universe@uap-core.de>
parents:
140
diff
changeset
|
176 | if (len == 0) { |
64
16590c9c497c
added autoextend feature to buffer
Mike Becker <universe@uap-core.de>
parents:
63
diff
changeset
|
177 | return len; |
56
76caac0da4a0
added memstream to ucx - still little work to do
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
178 | } |
76 | 179 | |
180 | memcpy(buffer->space + buffer->pos, ptr, len); | |
181 | buffer->pos += len; | |
182 | if(buffer->pos > buffer->size) { | |
183 | buffer->size = buffer->pos; | |
184 | } | |
185 | ||
186 | return len / size; | |
187 | } | |
56
76caac0da4a0
added memstream to ucx - still little work to do
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
188 | |
76 | 189 | size_t ucx_buffer_read(void *ptr, size_t size, size_t nitems, |
190 | UcxBuffer *buffer) { | |
270
3d80d425543b
adds integer overflow checks
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
269
diff
changeset
|
191 | size_t len; |
3d80d425543b
adds integer overflow checks
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
269
diff
changeset
|
192 | if(ucx_szmul(size, nitems, &len)) { |
3d80d425543b
adds integer overflow checks
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
269
diff
changeset
|
193 | return 0; |
3d80d425543b
adds integer overflow checks
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
269
diff
changeset
|
194 | } |
76 | 195 | if (buffer->pos + len > buffer->size) { |
196 | len = buffer->size - buffer->pos; | |
197 | if (size > 1) len -= len%size; | |
56
76caac0da4a0
added memstream to ucx - still little work to do
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
198 | } |
76 | 199 | |
200 | if (len <= 0) { | |
201 | return len; | |
202 | } | |
203 | ||
204 | memcpy(ptr, buffer->space + buffer->pos, len); | |
205 | buffer->pos += len; | |
206 | ||
207 | return len / size; | |
56
76caac0da4a0
added memstream to ucx - still little work to do
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
208 | } |
76caac0da4a0
added memstream to ucx - still little work to do
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
209 | |
60
abae4669fba7
renamed UcxMemstream to UcxBuffer
Mike Becker <universe@uap-core.de>
parents:
58
diff
changeset
|
210 | int ucx_buffer_putc(UcxBuffer *buffer, int c) { |
76 | 211 | if(buffer->pos >= buffer->capacity) { |
212 | if ((buffer->flags & UCX_BUFFER_AUTOEXTEND) == UCX_BUFFER_AUTOEXTEND) { | |
213 | if(ucx_buffer_extend(buffer, 1)) { | |
214 | return EOF; | |
215 | } | |
216 | } else { | |
217 | return EOF; | |
218 | } | |
56
76caac0da4a0
added memstream to ucx - still little work to do
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
219 | } |
76 | 220 | |
221 | c &= 0xFF; | |
222 | buffer->space[buffer->pos] = (char) c; | |
223 | buffer->pos++; | |
224 | if(buffer->pos > buffer->size) { | |
225 | buffer->size = buffer->pos; | |
226 | } | |
227 | return c; | |
56
76caac0da4a0
added memstream to ucx - still little work to do
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
228 | } |
76caac0da4a0
added memstream to ucx - still little work to do
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
229 | |
60
abae4669fba7
renamed UcxMemstream to UcxBuffer
Mike Becker <universe@uap-core.de>
parents:
58
diff
changeset
|
230 | int ucx_buffer_getc(UcxBuffer *buffer) { |
abae4669fba7
renamed UcxMemstream to UcxBuffer
Mike Becker <universe@uap-core.de>
parents:
58
diff
changeset
|
231 | if (ucx_buffer_eof(buffer)) { |
56
76caac0da4a0
added memstream to ucx - still little work to do
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
232 | return EOF; |
76caac0da4a0
added memstream to ucx - still little work to do
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
233 | } else { |
269
591473851c95
ucx_buffere_getc should return an unsigned char (0-255) or EOF (-1)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
259
diff
changeset
|
234 | int c = ((unsigned char*)buffer->space)[buffer->pos]; |
60
abae4669fba7
renamed UcxMemstream to UcxBuffer
Mike Becker <universe@uap-core.de>
parents:
58
diff
changeset
|
235 | buffer->pos++; |
56
76caac0da4a0
added memstream to ucx - still little work to do
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
236 | return c; |
76caac0da4a0
added memstream to ucx - still little work to do
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
237 | } |
76caac0da4a0
added memstream to ucx - still little work to do
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
238 | } |
76 | 239 | |
290
d5d6ab809ad3
adds new shift operations for UcxBuffer (including tests and a usage example in modules.md)
Mike Becker <universe@uap-core.de>
parents:
270
diff
changeset
|
240 | size_t ucx_buffer_puts(UcxBuffer *buffer, const char *str) { |
140
15f871f50bfd
completed documentation + changed API for buffer/stream generic copy functions
Mike Becker <universe@uap-core.de>
parents:
103
diff
changeset
|
241 | return ucx_buffer_write((const void*)str, 1, strlen(str), buffer); |
76 | 242 | } |
290
d5d6ab809ad3
adds new shift operations for UcxBuffer (including tests and a usage example in modules.md)
Mike Becker <universe@uap-core.de>
parents:
270
diff
changeset
|
243 | |
d5d6ab809ad3
adds new shift operations for UcxBuffer (including tests and a usage example in modules.md)
Mike Becker <universe@uap-core.de>
parents:
270
diff
changeset
|
244 | int ucx_buffer_shift_left(UcxBuffer* buffer, size_t shift) { |
d5d6ab809ad3
adds new shift operations for UcxBuffer (including tests and a usage example in modules.md)
Mike Becker <universe@uap-core.de>
parents:
270
diff
changeset
|
245 | if (shift >= buffer->size) { |
d5d6ab809ad3
adds new shift operations for UcxBuffer (including tests and a usage example in modules.md)
Mike Becker <universe@uap-core.de>
parents:
270
diff
changeset
|
246 | buffer->pos = buffer->size = 0; |
d5d6ab809ad3
adds new shift operations for UcxBuffer (including tests and a usage example in modules.md)
Mike Becker <universe@uap-core.de>
parents:
270
diff
changeset
|
247 | } else { |
d5d6ab809ad3
adds new shift operations for UcxBuffer (including tests and a usage example in modules.md)
Mike Becker <universe@uap-core.de>
parents:
270
diff
changeset
|
248 | memmove(buffer->space, buffer->space + shift, buffer->size - shift); |
d5d6ab809ad3
adds new shift operations for UcxBuffer (including tests and a usage example in modules.md)
Mike Becker <universe@uap-core.de>
parents:
270
diff
changeset
|
249 | buffer->size -= shift; |
d5d6ab809ad3
adds new shift operations for UcxBuffer (including tests and a usage example in modules.md)
Mike Becker <universe@uap-core.de>
parents:
270
diff
changeset
|
250 | |
d5d6ab809ad3
adds new shift operations for UcxBuffer (including tests and a usage example in modules.md)
Mike Becker <universe@uap-core.de>
parents:
270
diff
changeset
|
251 | if (buffer->pos >= shift) { |
d5d6ab809ad3
adds new shift operations for UcxBuffer (including tests and a usage example in modules.md)
Mike Becker <universe@uap-core.de>
parents:
270
diff
changeset
|
252 | buffer->pos -= shift; |
d5d6ab809ad3
adds new shift operations for UcxBuffer (including tests and a usage example in modules.md)
Mike Becker <universe@uap-core.de>
parents:
270
diff
changeset
|
253 | } else { |
d5d6ab809ad3
adds new shift operations for UcxBuffer (including tests and a usage example in modules.md)
Mike Becker <universe@uap-core.de>
parents:
270
diff
changeset
|
254 | buffer->pos = 0; |
d5d6ab809ad3
adds new shift operations for UcxBuffer (including tests and a usage example in modules.md)
Mike Becker <universe@uap-core.de>
parents:
270
diff
changeset
|
255 | } |
d5d6ab809ad3
adds new shift operations for UcxBuffer (including tests and a usage example in modules.md)
Mike Becker <universe@uap-core.de>
parents:
270
diff
changeset
|
256 | } |
d5d6ab809ad3
adds new shift operations for UcxBuffer (including tests and a usage example in modules.md)
Mike Becker <universe@uap-core.de>
parents:
270
diff
changeset
|
257 | return 0; |
d5d6ab809ad3
adds new shift operations for UcxBuffer (including tests and a usage example in modules.md)
Mike Becker <universe@uap-core.de>
parents:
270
diff
changeset
|
258 | } |
d5d6ab809ad3
adds new shift operations for UcxBuffer (including tests and a usage example in modules.md)
Mike Becker <universe@uap-core.de>
parents:
270
diff
changeset
|
259 | |
d5d6ab809ad3
adds new shift operations for UcxBuffer (including tests and a usage example in modules.md)
Mike Becker <universe@uap-core.de>
parents:
270
diff
changeset
|
260 | int ucx_buffer_shift_right(UcxBuffer* buffer, size_t shift) { |
d5d6ab809ad3
adds new shift operations for UcxBuffer (including tests and a usage example in modules.md)
Mike Becker <universe@uap-core.de>
parents:
270
diff
changeset
|
261 | size_t req_capacity = buffer->size + shift; |
d5d6ab809ad3
adds new shift operations for UcxBuffer (including tests and a usage example in modules.md)
Mike Becker <universe@uap-core.de>
parents:
270
diff
changeset
|
262 | size_t movebytes; |
d5d6ab809ad3
adds new shift operations for UcxBuffer (including tests and a usage example in modules.md)
Mike Becker <universe@uap-core.de>
parents:
270
diff
changeset
|
263 | |
d5d6ab809ad3
adds new shift operations for UcxBuffer (including tests and a usage example in modules.md)
Mike Becker <universe@uap-core.de>
parents:
270
diff
changeset
|
264 | // auto extend buffer, if required and enabled |
d5d6ab809ad3
adds new shift operations for UcxBuffer (including tests and a usage example in modules.md)
Mike Becker <universe@uap-core.de>
parents:
270
diff
changeset
|
265 | if (buffer->capacity < req_capacity) { |
d5d6ab809ad3
adds new shift operations for UcxBuffer (including tests and a usage example in modules.md)
Mike Becker <universe@uap-core.de>
parents:
270
diff
changeset
|
266 | if ((buffer->flags & UCX_BUFFER_AUTOEXTEND) == UCX_BUFFER_AUTOEXTEND) { |
d5d6ab809ad3
adds new shift operations for UcxBuffer (including tests and a usage example in modules.md)
Mike Becker <universe@uap-core.de>
parents:
270
diff
changeset
|
267 | if (ucx_buffer_extend(buffer, req_capacity - buffer->capacity)) { |
d5d6ab809ad3
adds new shift operations for UcxBuffer (including tests and a usage example in modules.md)
Mike Becker <universe@uap-core.de>
parents:
270
diff
changeset
|
268 | return 1; |
d5d6ab809ad3
adds new shift operations for UcxBuffer (including tests and a usage example in modules.md)
Mike Becker <universe@uap-core.de>
parents:
270
diff
changeset
|
269 | } |
d5d6ab809ad3
adds new shift operations for UcxBuffer (including tests and a usage example in modules.md)
Mike Becker <universe@uap-core.de>
parents:
270
diff
changeset
|
270 | movebytes = buffer->size; |
d5d6ab809ad3
adds new shift operations for UcxBuffer (including tests and a usage example in modules.md)
Mike Becker <universe@uap-core.de>
parents:
270
diff
changeset
|
271 | } else { |
d5d6ab809ad3
adds new shift operations for UcxBuffer (including tests and a usage example in modules.md)
Mike Becker <universe@uap-core.de>
parents:
270
diff
changeset
|
272 | movebytes = buffer->capacity - shift; |
d5d6ab809ad3
adds new shift operations for UcxBuffer (including tests and a usage example in modules.md)
Mike Becker <universe@uap-core.de>
parents:
270
diff
changeset
|
273 | } |
d5d6ab809ad3
adds new shift operations for UcxBuffer (including tests and a usage example in modules.md)
Mike Becker <universe@uap-core.de>
parents:
270
diff
changeset
|
274 | } else { |
d5d6ab809ad3
adds new shift operations for UcxBuffer (including tests and a usage example in modules.md)
Mike Becker <universe@uap-core.de>
parents:
270
diff
changeset
|
275 | movebytes = buffer->size; |
d5d6ab809ad3
adds new shift operations for UcxBuffer (including tests and a usage example in modules.md)
Mike Becker <universe@uap-core.de>
parents:
270
diff
changeset
|
276 | } |
d5d6ab809ad3
adds new shift operations for UcxBuffer (including tests and a usage example in modules.md)
Mike Becker <universe@uap-core.de>
parents:
270
diff
changeset
|
277 | |
d5d6ab809ad3
adds new shift operations for UcxBuffer (including tests and a usage example in modules.md)
Mike Becker <universe@uap-core.de>
parents:
270
diff
changeset
|
278 | memmove(buffer->space + shift, buffer->space, movebytes); |
d5d6ab809ad3
adds new shift operations for UcxBuffer (including tests and a usage example in modules.md)
Mike Becker <universe@uap-core.de>
parents:
270
diff
changeset
|
279 | buffer->size = shift+movebytes; |
d5d6ab809ad3
adds new shift operations for UcxBuffer (including tests and a usage example in modules.md)
Mike Becker <universe@uap-core.de>
parents:
270
diff
changeset
|
280 | |
d5d6ab809ad3
adds new shift operations for UcxBuffer (including tests and a usage example in modules.md)
Mike Becker <universe@uap-core.de>
parents:
270
diff
changeset
|
281 | buffer->pos += shift; |
d5d6ab809ad3
adds new shift operations for UcxBuffer (including tests and a usage example in modules.md)
Mike Becker <universe@uap-core.de>
parents:
270
diff
changeset
|
282 | if (buffer->pos > buffer->size) { |
d5d6ab809ad3
adds new shift operations for UcxBuffer (including tests and a usage example in modules.md)
Mike Becker <universe@uap-core.de>
parents:
270
diff
changeset
|
283 | buffer->pos = buffer->size; |
d5d6ab809ad3
adds new shift operations for UcxBuffer (including tests and a usage example in modules.md)
Mike Becker <universe@uap-core.de>
parents:
270
diff
changeset
|
284 | } |
d5d6ab809ad3
adds new shift operations for UcxBuffer (including tests and a usage example in modules.md)
Mike Becker <universe@uap-core.de>
parents:
270
diff
changeset
|
285 | |
d5d6ab809ad3
adds new shift operations for UcxBuffer (including tests and a usage example in modules.md)
Mike Becker <universe@uap-core.de>
parents:
270
diff
changeset
|
286 | return 0; |
d5d6ab809ad3
adds new shift operations for UcxBuffer (including tests and a usage example in modules.md)
Mike Becker <universe@uap-core.de>
parents:
270
diff
changeset
|
287 | } |
d5d6ab809ad3
adds new shift operations for UcxBuffer (including tests and a usage example in modules.md)
Mike Becker <universe@uap-core.de>
parents:
270
diff
changeset
|
288 | |
d5d6ab809ad3
adds new shift operations for UcxBuffer (including tests and a usage example in modules.md)
Mike Becker <universe@uap-core.de>
parents:
270
diff
changeset
|
289 | int ucx_buffer_shift(UcxBuffer* buffer, off_t shift) { |
d5d6ab809ad3
adds new shift operations for UcxBuffer (including tests and a usage example in modules.md)
Mike Becker <universe@uap-core.de>
parents:
270
diff
changeset
|
290 | if (shift < 0) { |
d5d6ab809ad3
adds new shift operations for UcxBuffer (including tests and a usage example in modules.md)
Mike Becker <universe@uap-core.de>
parents:
270
diff
changeset
|
291 | return ucx_buffer_shift_left(buffer, (size_t) (-shift)); |
d5d6ab809ad3
adds new shift operations for UcxBuffer (including tests and a usage example in modules.md)
Mike Becker <universe@uap-core.de>
parents:
270
diff
changeset
|
292 | } else if (shift > 0) { |
d5d6ab809ad3
adds new shift operations for UcxBuffer (including tests and a usage example in modules.md)
Mike Becker <universe@uap-core.de>
parents:
270
diff
changeset
|
293 | return ucx_buffer_shift_right(buffer, (size_t) shift); |
d5d6ab809ad3
adds new shift operations for UcxBuffer (including tests and a usage example in modules.md)
Mike Becker <universe@uap-core.de>
parents:
270
diff
changeset
|
294 | } else { |
d5d6ab809ad3
adds new shift operations for UcxBuffer (including tests and a usage example in modules.md)
Mike Becker <universe@uap-core.de>
parents:
270
diff
changeset
|
295 | return 0; |
d5d6ab809ad3
adds new shift operations for UcxBuffer (including tests and a usage example in modules.md)
Mike Becker <universe@uap-core.de>
parents:
270
diff
changeset
|
296 | } |
d5d6ab809ad3
adds new shift operations for UcxBuffer (including tests and a usage example in modules.md)
Mike Becker <universe@uap-core.de>
parents:
270
diff
changeset
|
297 | } |