Mon, 06 Jan 2025 21:18:36 +0100
update ucx
5
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
1 | /* |
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
2 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
3 | * |
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
4 | * Copyright 2021 Mike Becker, Olaf Wintermann All rights reserved. |
5
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
5 | * |
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
6 | * Redistribution and use in source and binary forms, with or without |
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
7 | * modification, are permitted provided that the following conditions are met: |
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
8 | * |
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
9 | * 1. Redistributions of source code must retain the above copyright |
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
10 | * notice, this list of conditions and the following disclaimer. |
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
11 | * |
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
12 | * 2. Redistributions in binary form must reproduce the above copyright |
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
13 | * notice, this list of conditions and the following disclaimer in the |
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
14 | * documentation and/or other materials provided with the distribution. |
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
15 | * |
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
17 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
18 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
19 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
20 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
21 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
22 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
23 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
24 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
25 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
26 | * POSSIBILITY OF SUCH DAMAGE. |
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
27 | */ |
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
28 | |
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
29 | #include "cx/buffer.h" |
335
c1bc13faadaa
updates ucx to version 1.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
308
diff
changeset
|
30 | |
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
31 | #include <stdio.h> |
1
1bcaac272cdf
added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
32 | #include <string.h> |
852 | 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 | } | |
1
1bcaac272cdf
added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
45 | |
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
46 | int cxBufferInit( |
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
47 | CxBuffer *buffer, |
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
48 | void *space, |
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
49 | size_t capacity, |
852 | 50 | const CxAllocator *allocator, |
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
51 | int flags |
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
52 | ) { |
852 | 53 | if (allocator == NULL) { |
54 | allocator = cxDefaultAllocator; | |
55 | } | |
56 | if (flags & CX_BUFFER_COPY_ON_EXTEND) { | |
57 | flags |= CX_BUFFER_AUTO_EXTEND; | |
58 | } | |
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
59 | buffer->allocator = allocator; |
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
60 | buffer->flags = flags; |
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
61 | if (!space) { |
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
62 | buffer->bytes = cxMalloc(allocator, capacity); |
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
63 | if (buffer->bytes == NULL) { |
852 | 64 | return -1; // LCOV_EXCL_LINE |
1
1bcaac272cdf
added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
65 | } |
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
66 | buffer->flags |= CX_BUFFER_FREE_CONTENTS; |
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
67 | } else { |
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
68 | buffer->bytes = space; |
1
1bcaac272cdf
added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
69 | } |
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
70 | buffer->capacity = capacity; |
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
71 | buffer->size = 0; |
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
72 | buffer->pos = 0; |
1
1bcaac272cdf
added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
73 | |
852 | 74 | buffer->flush = NULL; |
1
1bcaac272cdf
added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
75 | |
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
76 | return 0; |
1
1bcaac272cdf
added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
77 | } |
1bcaac272cdf
added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
78 | |
852 | 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 | ||
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
89 | void cxBufferDestroy(CxBuffer *buffer) { |
852 | 90 | if (buffer->flags & CX_BUFFER_FREE_CONTENTS) { |
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
91 | cxFree(buffer->allocator, buffer->bytes); |
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
92 | } |
852 | 93 | free(buffer->flush); |
94 | memset(buffer, 0, sizeof(CxBuffer)); | |
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
95 | } |
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
96 | |
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
97 | CxBuffer *cxBufferCreate( |
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
98 | void *space, |
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
99 | size_t capacity, |
852 | 100 | const CxAllocator *allocator, |
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
101 | int flags |
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
102 | ) { |
852 | 103 | if (allocator == NULL) { |
104 | allocator = cxDefaultAllocator; | |
105 | } | |
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
106 | CxBuffer *buf = cxMalloc(allocator, sizeof(CxBuffer)); |
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
107 | if (buf == NULL) return NULL; |
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
108 | if (0 == cxBufferInit(buf, space, capacity, allocator, flags)) { |
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
109 | return buf; |
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
110 | } else { |
852 | 111 | // LCOV_EXCL_START |
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
112 | cxFree(allocator, buf); |
1
1bcaac272cdf
added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
113 | return NULL; |
852 | 114 | // LCOV_EXCL_STOP |
1
1bcaac272cdf
added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
115 | } |
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
116 | } |
1
1bcaac272cdf
added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
117 | |
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
118 | void cxBufferFree(CxBuffer *buffer) { |
852 | 119 | if (buffer == NULL) return; |
120 | const CxAllocator *allocator = buffer->allocator; | |
121 | cxBufferDestroy(buffer); | |
122 | cxFree(allocator, buffer); | |
1
1bcaac272cdf
added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
123 | } |
1bcaac272cdf
added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
124 | |
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
125 | int cxBufferSeek( |
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
126 | CxBuffer *buffer, |
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
127 | off_t offset, |
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
128 | int whence |
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
129 | ) { |
17
11dffb40cd91
new dav_query function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
5
diff
changeset
|
130 | size_t npos; |
1
1bcaac272cdf
added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
131 | switch (whence) { |
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
132 | case SEEK_CUR: |
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
133 | npos = buffer->pos; |
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
134 | break; |
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
135 | case SEEK_END: |
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
136 | npos = buffer->size; |
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
137 | break; |
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
138 | case SEEK_SET: |
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
139 | npos = 0; |
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
140 | break; |
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
141 | default: |
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
142 | return -1; |
1
1bcaac272cdf
added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
143 | } |
1bcaac272cdf
added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
144 | |
70 | 145 | size_t opos = npos; |
1
1bcaac272cdf
added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
146 | npos += offset; |
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
147 | |
70 | 148 | if ((offset > 0 && npos < opos) || (offset < 0 && npos > opos)) { |
852 | 149 | errno = EOVERFLOW; |
70 | 150 | return -1; |
151 | } | |
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
152 | |
852 | 153 | if (npos > buffer->size) { |
1
1bcaac272cdf
added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
154 | return -1; |
1bcaac272cdf
added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
155 | } else { |
1bcaac272cdf
added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
156 | buffer->pos = npos; |
1bcaac272cdf
added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
157 | return 0; |
1bcaac272cdf
added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
158 | } |
1bcaac272cdf
added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
159 | |
1bcaac272cdf
added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
160 | } |
1bcaac272cdf
added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
161 | |
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
162 | void cxBufferClear(CxBuffer *buffer) { |
852 | 163 | if (0 == (buffer->flags & CX_BUFFER_COPY_ON_WRITE)) { |
164 | memset(buffer->bytes, 0, buffer->size); | |
165 | } | |
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
166 | buffer->size = 0; |
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
167 | buffer->pos = 0; |
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
168 | } |
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
169 | |
816
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
170 | void cxBufferReset(CxBuffer *buffer) { |
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
171 | buffer->size = 0; |
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
172 | buffer->pos = 0; |
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
173 | } |
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
174 | |
852 | 175 | bool cxBufferEof(const CxBuffer *buffer) { |
1
1bcaac272cdf
added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
176 | return buffer->pos >= buffer->size; |
1bcaac272cdf
added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
177 | } |
1bcaac272cdf
added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
178 | |
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
179 | int cxBufferMinimumCapacity( |
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
180 | CxBuffer *buffer, |
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
181 | size_t newcap |
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
182 | ) { |
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
183 | if (newcap <= buffer->capacity) { |
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
184 | return 0; |
70 | 185 | } |
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
186 | |
852 | 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, | |
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
198 | (void **) &buffer->bytes, newcap) == 0) { |
1
1bcaac272cdf
added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
199 | buffer->capacity = newcap; |
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
200 | return 0; |
1
1bcaac272cdf
added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
201 | } else { |
852 | 202 | return -1; // LCOV_EXCL_LINE |
1
1bcaac272cdf
added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
203 | } |
1bcaac272cdf
added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
204 | } |
1bcaac272cdf
added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
205 | |
852 | 206 | static size_t cx_buffer_flush_helper( |
207 | const CxBuffer *buffer, | |
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
208 | size_t size, |
852 | 209 | const unsigned char *src, |
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
210 | size_t nitems |
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
211 | ) { |
852 | 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); | |
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
222 | if (flushed > 0) { |
852 | 223 | flushed_total += flushed; |
224 | src += flushed * size; | |
225 | nitems -= flushed; | |
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
226 | } else { |
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
227 | // if no bytes can be flushed out anymore, we give up |
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
228 | break; |
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
229 | } |
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
230 | } |
852 | 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); | |
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
252 | } |
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
253 | |
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
254 | size_t cxBufferWrite( |
852 | 255 | const void *ptr, |
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
256 | size_t size, |
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
257 | size_t nitems, |
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
258 | CxBuffer *buffer |
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
259 | ) { |
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
260 | // optimize for easy case |
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
261 | if (size == 1 && (buffer->capacity - buffer->pos) >= nitems) { |
852 | 262 | if (buffer_copy_on_write(buffer)) return 0; |
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
263 | memcpy(buffer->bytes + buffer->pos, ptr, nitems); |
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
264 | buffer->pos += nitems; |
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
265 | if (buffer->pos > buffer->size) { |
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
266 | buffer->size = buffer->pos; |
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
267 | } |
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
268 | return nitems; |
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
269 | } |
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
270 | |
505 | 271 | size_t len; |
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
272 | if (cx_szmul(size, nitems, &len)) { |
852 | 273 | errno = EOVERFLOW; |
274 | return 0; | |
275 | } | |
276 | if (buffer->pos > SIZE_MAX - len) { | |
277 | errno = EOVERFLOW; | |
505 | 278 | return 0; |
279 | } | |
70 | 280 | size_t required = buffer->pos + len; |
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
281 | |
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
282 | bool perform_flush = false; |
70 | 283 | if (required > buffer->capacity) { |
852 | 284 | if (buffer->flags & CX_BUFFER_AUTO_EXTEND) { |
285 | if (buffer->flush != NULL && required > buffer->flush->threshold) { | |
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
286 | perform_flush = true; |
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
287 | } else { |
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
288 | if (cxBufferMinimumCapacity(buffer, required)) { |
852 | 289 | return 0; // LCOV_EXCL_LINE |
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
290 | } |
1
1bcaac272cdf
added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
291 | } |
1bcaac272cdf
added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
292 | } else { |
852 | 293 | if (buffer->flush != NULL) { |
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
294 | perform_flush = true; |
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
295 | } else { |
852 | 296 | // truncate data, if we can neither extend nor flush |
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
297 | len = buffer->capacity - buffer->pos; |
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
298 | if (size > 1) { |
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
299 | len -= len % size; |
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
300 | } |
852 | 301 | nitems = len / size; |
70 | 302 | } |
1
1bcaac272cdf
added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
303 | } |
1bcaac272cdf
added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
304 | } |
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
305 | |
852 | 306 | // check here and not above because of possible truncation |
70 | 307 | if (len == 0) { |
852 | 308 | return 0; |
1
1bcaac272cdf
added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
309 | } |
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
310 | |
852 | 311 | // check if we need to copy |
312 | if (buffer_copy_on_write(buffer)) return 0; | |
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
313 | |
852 | 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; | |
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
325 | } |
852 | 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; | |
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
334 | } |
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
335 | return cxBufferWrite(ptr, size, nitems, buffer); |
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
336 | } |
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
337 | } else { |
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
338 | memcpy(buffer->bytes + buffer->pos, ptr, len); |
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
339 | buffer->pos += len; |
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
340 | if (buffer->pos > buffer->size) { |
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
341 | buffer->size = buffer->pos; |
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
342 | } |
852 | 343 | return nitems; |
1
1bcaac272cdf
added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
344 | } |
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
345 | |
1
1bcaac272cdf
added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
346 | } |
1bcaac272cdf
added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
347 | |
852 | 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 | ||
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
361 | int cxBufferPut( |
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
362 | CxBuffer *buffer, |
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
363 | int c |
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
364 | ) { |
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
365 | c &= 0xFF; |
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
366 | unsigned char const ch = c; |
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
367 | if (cxBufferWrite(&ch, 1, 1, buffer) == 1) { |
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
368 | return c; |
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
369 | } else { |
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
370 | return EOF; |
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
371 | } |
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
372 | } |
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
373 | |
852 | 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 | ||
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
385 | size_t cxBufferPutString( |
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
386 | CxBuffer *buffer, |
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
387 | const char *str |
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
388 | ) { |
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
389 | return cxBufferWrite(str, 1, strlen(str), buffer); |
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
390 | } |
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
391 | |
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
392 | size_t cxBufferRead( |
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
393 | void *ptr, |
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
394 | size_t size, |
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
395 | size_t nitems, |
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
396 | CxBuffer *buffer |
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
397 | ) { |
505 | 398 | size_t len; |
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
399 | if (cx_szmul(size, nitems, &len)) { |
852 | 400 | errno = EOVERFLOW; |
505 | 401 | return 0; |
402 | } | |
1
1bcaac272cdf
added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
403 | if (buffer->pos + len > buffer->size) { |
1bcaac272cdf
added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
404 | len = buffer->size - buffer->pos; |
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
405 | if (size > 1) len -= len % size; |
1
1bcaac272cdf
added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
406 | } |
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
407 | |
1
1bcaac272cdf
added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
408 | if (len <= 0) { |
1bcaac272cdf
added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
409 | return len; |
1bcaac272cdf
added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
410 | } |
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
411 | |
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
412 | memcpy(ptr, buffer->bytes + buffer->pos, len); |
1
1bcaac272cdf
added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
413 | buffer->pos += len; |
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
414 | |
1
1bcaac272cdf
added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
415 | return len / size; |
1bcaac272cdf
added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
416 | } |
1bcaac272cdf
added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
417 | |
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
418 | int cxBufferGet(CxBuffer *buffer) { |
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
419 | if (cxBufferEof(buffer)) { |
1
1bcaac272cdf
added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
420 | return EOF; |
1bcaac272cdf
added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
421 | } else { |
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
422 | int c = buffer->bytes[buffer->pos]; |
1
1bcaac272cdf
added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
423 | buffer->pos++; |
1bcaac272cdf
added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
424 | return c; |
1bcaac272cdf
added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
425 | } |
1bcaac272cdf
added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
426 | } |
1bcaac272cdf
added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
427 | |
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
428 | int cxBufferShiftLeft( |
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
429 | CxBuffer *buffer, |
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
430 | size_t shift |
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
431 | ) { |
505 | 432 | if (shift >= buffer->size) { |
433 | buffer->pos = buffer->size = 0; | |
434 | } else { | |
852 | 435 | if (buffer_copy_on_write(buffer)) return -1; |
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
436 | memmove(buffer->bytes, buffer->bytes + shift, buffer->size - shift); |
505 | 437 | buffer->size -= shift; |
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
438 | |
505 | 439 | if (buffer->pos >= shift) { |
440 | buffer->pos -= shift; | |
441 | } else { | |
442 | buffer->pos = 0; | |
443 | } | |
444 | } | |
445 | return 0; | |
446 | } | |
447 | ||
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
448 | int cxBufferShiftRight( |
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
449 | CxBuffer *buffer, |
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
450 | size_t shift |
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
451 | ) { |
852 | 452 | if (buffer->size > SIZE_MAX - shift) { |
453 | errno = EOVERFLOW; | |
454 | return -1; | |
455 | } | |
505 | 456 | size_t req_capacity = buffer->size + shift; |
457 | size_t movebytes; | |
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
458 | |
505 | 459 | // auto extend buffer, if required and enabled |
460 | if (buffer->capacity < req_capacity) { | |
852 | 461 | if (buffer->flags & CX_BUFFER_AUTO_EXTEND) { |
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
462 | if (cxBufferMinimumCapacity(buffer, req_capacity)) { |
852 | 463 | return -1; // LCOV_EXCL_LINE |
505 | 464 | } |
465 | movebytes = buffer->size; | |
466 | } else { | |
467 | movebytes = buffer->capacity - shift; | |
468 | } | |
469 | } else { | |
470 | movebytes = buffer->size; | |
471 | } | |
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
472 | |
852 | 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 | } | |
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
478 | |
505 | 479 | buffer->pos += shift; |
480 | if (buffer->pos > buffer->size) { | |
481 | buffer->pos = buffer->size; | |
482 | } | |
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
483 | |
505 | 484 | return 0; |
485 | } | |
486 | ||
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
487 | int cxBufferShift( |
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
488 | CxBuffer *buffer, |
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
489 | off_t shift |
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
490 | ) { |
505 | 491 | if (shift < 0) { |
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
492 | return cxBufferShiftLeft(buffer, (size_t) (-shift)); |
505 | 493 | } else if (shift > 0) { |
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
494 | return cxBufferShiftRight(buffer, (size_t) shift); |
505 | 495 | } else { |
496 | return 0; | |
497 | } | |
498 | } |