Tue, 17 Oct 2017 15:15:54 +0200
updates license
1 /*
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3 *
4 * Copyright 2017 Olaf Wintermann. All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 *
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 */
29 #include "buffer.h"
30 #include <stdarg.h>
31 #include <stdlib.h>
32 #include <string.h>
34 UcxBuffer *ucx_buffer_new(void *space, size_t capacity, int flags) {
35 UcxBuffer *buffer = (UcxBuffer*) malloc(sizeof(UcxBuffer));
36 if (buffer) {
37 buffer->flags = flags;
38 if (!space) {
39 buffer->space = (char*)malloc(capacity);
40 if (!buffer->space) {
41 free(buffer);
42 return NULL;
43 }
44 memset(buffer->space, 0, capacity);
45 buffer->flags |= UCX_BUFFER_AUTOFREE;
46 } else {
47 buffer->space = (char*)space;
48 }
49 buffer->capacity = capacity;
50 buffer->size = 0;
52 buffer->pos = 0;
53 }
55 return buffer;
56 }
58 void ucx_buffer_free(UcxBuffer *buffer) {
59 if ((buffer->flags & UCX_BUFFER_AUTOFREE) == UCX_BUFFER_AUTOFREE) {
60 free(buffer->space);
61 }
62 free(buffer);
63 }
65 UcxBuffer* ucx_buffer_extract(
66 UcxBuffer *src, size_t start, size_t length, int flags) {
67 if (src->size == 0 || length == 0 ||
68 ((size_t)-1) - start < length || start+length > src->capacity)
69 {
70 return NULL;
71 }
73 UcxBuffer *dst = (UcxBuffer*) malloc(sizeof(UcxBuffer));
74 if (dst) {
75 dst->space = (char*)malloc(length);
76 if (!dst->space) {
77 free(dst);
78 return NULL;
79 }
80 dst->capacity = length;
81 dst->size = length;
82 dst->flags = flags | UCX_BUFFER_AUTOFREE;
83 dst->pos = 0;
84 memcpy(dst->space, src->space+start, length);
85 }
86 return dst;
87 }
89 int ucx_buffer_seek(UcxBuffer *buffer, off_t offset, int whence) {
90 size_t npos;
91 switch (whence) {
92 case SEEK_CUR:
93 npos = buffer->pos;
94 break;
95 case SEEK_END:
96 npos = buffer->size;
97 break;
98 case SEEK_SET:
99 npos = 0;
100 break;
101 default:
102 return -1;
103 }
105 size_t opos = npos;
106 npos += offset;
108 if ((offset > 0 && npos < opos) || (offset < 0 && npos > opos)) {
109 return -1;
110 }
112 if (npos >= buffer->size) {
113 return -1;
114 } else {
115 buffer->pos = npos;
116 return 0;
117 }
119 }
121 int ucx_buffer_eof(UcxBuffer *buffer) {
122 return buffer->pos >= buffer->size;
123 }
125 int ucx_buffer_extend(UcxBuffer *buffer, size_t len) {
126 size_t newcap = buffer->capacity;
128 if (buffer->capacity + len < buffer->capacity) {
129 return -1;
130 }
132 while (buffer->capacity + len > newcap) {
133 newcap <<= 1;
134 if (newcap < buffer->capacity) {
135 return -1;
136 }
137 }
139 char *newspace = (char*)realloc(buffer->space, newcap);
140 if (newspace) {
141 memset(newspace+buffer->size, 0, newcap-buffer->size);
142 buffer->space = newspace;
143 buffer->capacity = newcap;
144 } else {
145 return -1;
146 }
148 return 0;
149 }
151 size_t ucx_buffer_write(const void *ptr, size_t size, size_t nitems,
152 UcxBuffer *buffer) {
153 size_t len = size * nitems;
154 size_t required = buffer->pos + len;
155 if (buffer->pos > required) {
156 return 0;
157 }
159 if (required > buffer->capacity) {
160 if ((buffer->flags & UCX_BUFFER_AUTOEXTEND) == UCX_BUFFER_AUTOEXTEND) {
161 if (ucx_buffer_extend(buffer, required - buffer->capacity)) {
162 return 0;
163 }
164 } else {
165 len = buffer->capacity - buffer->pos;
166 if (size > 1) {
167 len -= len%size;
168 }
169 }
170 }
172 if (len == 0) {
173 return len;
174 }
176 memcpy(buffer->space + buffer->pos, ptr, len);
177 buffer->pos += len;
178 if(buffer->pos > buffer->size) {
179 buffer->size = buffer->pos;
180 }
182 return len / size;
183 }
185 size_t ucx_buffer_read(void *ptr, size_t size, size_t nitems,
186 UcxBuffer *buffer) {
187 size_t len = size * nitems;
188 if (buffer->pos + len > buffer->size) {
189 len = buffer->size - buffer->pos;
190 if (size > 1) len -= len%size;
191 }
193 if (len <= 0) {
194 return len;
195 }
197 memcpy(ptr, buffer->space + buffer->pos, len);
198 buffer->pos += len;
200 return len / size;
201 }
203 int ucx_buffer_putc(UcxBuffer *buffer, int c) {
204 if(buffer->pos >= buffer->capacity) {
205 if ((buffer->flags & UCX_BUFFER_AUTOEXTEND) == UCX_BUFFER_AUTOEXTEND) {
206 if(ucx_buffer_extend(buffer, 1)) {
207 return EOF;
208 }
209 } else {
210 return EOF;
211 }
212 }
214 c &= 0xFF;
215 buffer->space[buffer->pos] = (char) c;
216 buffer->pos++;
217 if(buffer->pos > buffer->size) {
218 buffer->size = buffer->pos;
219 }
220 return c;
221 }
223 int ucx_buffer_getc(UcxBuffer *buffer) {
224 if (ucx_buffer_eof(buffer)) {
225 return EOF;
226 } else {
227 int c = buffer->space[buffer->pos];
228 buffer->pos++;
229 return c;
230 }
231 }
233 size_t ucx_buffer_puts(UcxBuffer *buffer, char *str) {
234 return ucx_buffer_write((const void*)str, 1, strlen(str), buffer);
235 }