src/buffer.c

Thu, 03 May 2018 10:44:33 +0200

author
Mike Becker <universe@uap-core.de>
date
Thu, 03 May 2018 10:44:33 +0200
changeset 287
98da78a1e69a
parent 270
3d80d425543b
child 290
d5d6ab809ad3
permissions
-rw-r--r--

adds ucx_avl_free_content() function and documentation in modules.md

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
2f5dea574a75 modules documentation
Mike Becker <universe@uap-core.de>
parents: 251
diff changeset
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
af355652f271 some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 76
diff changeset
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
655020a30e77 fixed buffer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
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
655020a30e77 fixed buffer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
66 UcxBuffer* ucx_buffer_extract(
655020a30e77 fixed buffer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
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
6814aea8462d added extract function
Mike Becker <universe@uap-core.de>
parents: 61
diff changeset
71 return NULL;
6814aea8462d added extract function
Mike Becker <universe@uap-core.de>
parents: 61
diff changeset
72 }
6814aea8462d added extract function
Mike Becker <universe@uap-core.de>
parents: 61
diff changeset
73
76
655020a30e77 fixed buffer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
74 UcxBuffer *dst = (UcxBuffer*) malloc(sizeof(UcxBuffer));
62
6814aea8462d added extract function
Mike Becker <universe@uap-core.de>
parents: 61
diff changeset
75 if (dst) {
78
af355652f271 some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 76
diff changeset
76 dst->space = (char*)malloc(length);
62
6814aea8462d added extract function
Mike Becker <universe@uap-core.de>
parents: 61
diff changeset
77 if (!dst->space) {
6814aea8462d added extract function
Mike Becker <universe@uap-core.de>
parents: 61
diff changeset
78 free(dst);
6814aea8462d added extract function
Mike Becker <universe@uap-core.de>
parents: 61
diff changeset
79 return NULL;
6814aea8462d added extract function
Mike Becker <universe@uap-core.de>
parents: 61
diff changeset
80 }
76
655020a30e77 fixed buffer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
81 dst->capacity = length;
62
6814aea8462d added extract function
Mike Becker <universe@uap-core.de>
parents: 61
diff changeset
82 dst->size = length;
6814aea8462d added extract function
Mike Becker <universe@uap-core.de>
parents: 61
diff changeset
83 dst->flags = flags | UCX_BUFFER_AUTOFREE;
6814aea8462d added extract function
Mike Becker <universe@uap-core.de>
parents: 61
diff changeset
84 dst->pos = 0;
76
655020a30e77 fixed buffer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
85 memcpy(dst->space, src->space+start, length);
62
6814aea8462d added extract function
Mike Becker <universe@uap-core.de>
parents: 61
diff changeset
86 }
6814aea8462d added extract function
Mike Becker <universe@uap-core.de>
parents: 61
diff changeset
87 return dst;
6814aea8462d added extract function
Mike Becker <universe@uap-core.de>
parents: 61
diff changeset
88 }
6814aea8462d added extract function
Mike Becker <universe@uap-core.de>
parents: 61
diff changeset
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
655020a30e77 fixed buffer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
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
655020a30e77 fixed buffer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
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
6814aea8462d added extract function
Mike Becker <universe@uap-core.de>
parents: 61
diff changeset
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
655020a30e77 fixed buffer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
126 int ucx_buffer_extend(UcxBuffer *buffer, size_t len) {
655020a30e77 fixed buffer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
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
655020a30e77 fixed buffer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
139
78
af355652f271 some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 76
diff changeset
140 char *newspace = (char*)realloc(buffer->space, newcap);
76
655020a30e77 fixed buffer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
141 if (newspace) {
655020a30e77 fixed buffer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
142 memset(newspace+buffer->size, 0, newcap-buffer->size);
655020a30e77 fixed buffer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
143 buffer->space = newspace;
655020a30e77 fixed buffer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
144 buffer->capacity = newcap;
655020a30e77 fixed buffer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
145 } else {
655020a30e77 fixed buffer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
146 return -1;
655020a30e77 fixed buffer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
147 }
655020a30e77 fixed buffer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
148
655020a30e77 fixed buffer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
149 return 0;
655020a30e77 fixed buffer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
150 }
655020a30e77 fixed buffer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
151
655020a30e77 fixed buffer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
152 size_t ucx_buffer_write(const void *ptr, size_t size, size_t nitems,
655020a30e77 fixed buffer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
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
655020a30e77 fixed buffer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
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
655020a30e77 fixed buffer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
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
655020a30e77 fixed buffer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
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
655020a30e77 fixed buffer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
179
655020a30e77 fixed buffer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
180 memcpy(buffer->space + buffer->pos, ptr, len);
655020a30e77 fixed buffer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
181 buffer->pos += len;
655020a30e77 fixed buffer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
182 if(buffer->pos > buffer->size) {
655020a30e77 fixed buffer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
183 buffer->size = buffer->pos;
655020a30e77 fixed buffer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
184 }
655020a30e77 fixed buffer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
185
655020a30e77 fixed buffer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
186 return len / size;
655020a30e77 fixed buffer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
187 }
56
76caac0da4a0 added memstream to ucx - still little work to do
Mike Becker <universe@uap-core.de>
parents:
diff changeset
188
76
655020a30e77 fixed buffer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
189 size_t ucx_buffer_read(void *ptr, size_t size, size_t nitems,
655020a30e77 fixed buffer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
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
655020a30e77 fixed buffer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
195 if (buffer->pos + len > buffer->size) {
655020a30e77 fixed buffer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
196 len = buffer->size - buffer->pos;
655020a30e77 fixed buffer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
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
655020a30e77 fixed buffer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
199
655020a30e77 fixed buffer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
200 if (len <= 0) {
655020a30e77 fixed buffer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
201 return len;
655020a30e77 fixed buffer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
202 }
655020a30e77 fixed buffer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
203
655020a30e77 fixed buffer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
204 memcpy(ptr, buffer->space + buffer->pos, len);
655020a30e77 fixed buffer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
205 buffer->pos += len;
655020a30e77 fixed buffer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
206
655020a30e77 fixed buffer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
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
655020a30e77 fixed buffer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
211 if(buffer->pos >= buffer->capacity) {
655020a30e77 fixed buffer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
212 if ((buffer->flags & UCX_BUFFER_AUTOEXTEND) == UCX_BUFFER_AUTOEXTEND) {
655020a30e77 fixed buffer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
213 if(ucx_buffer_extend(buffer, 1)) {
655020a30e77 fixed buffer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
214 return EOF;
655020a30e77 fixed buffer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
215 }
655020a30e77 fixed buffer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
216 } else {
655020a30e77 fixed buffer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
217 return EOF;
655020a30e77 fixed buffer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
218 }
56
76caac0da4a0 added memstream to ucx - still little work to do
Mike Becker <universe@uap-core.de>
parents:
diff changeset
219 }
76
655020a30e77 fixed buffer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
220
655020a30e77 fixed buffer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
221 c &= 0xFF;
655020a30e77 fixed buffer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
222 buffer->space[buffer->pos] = (char) c;
655020a30e77 fixed buffer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
223 buffer->pos++;
655020a30e77 fixed buffer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
224 if(buffer->pos > buffer->size) {
655020a30e77 fixed buffer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
225 buffer->size = buffer->pos;
655020a30e77 fixed buffer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
226 }
655020a30e77 fixed buffer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
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
655020a30e77 fixed buffer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
239
140
15f871f50bfd completed documentation + changed API for buffer/stream generic copy functions
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
240 size_t ucx_buffer_puts(UcxBuffer *buffer, char *str) {
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
655020a30e77 fixed buffer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
242 }

mercurial