Sat, 08 Nov 2025 23:06:11 +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 | ||
| 888 | 35 | #ifdef _WIN32 |
| 36 | #include <Windows.h> | |
| 37 | #include <sysinfoapi.h> | |
| 38 | static unsigned long system_page_size() { | |
| 39 | static unsigned long ps = 0; | |
| 40 | if (ps == 0) { | |
| 41 | SYSTEM_INFO sysinfo; | |
| 42 | GetSystemInfo(&sysinfo); | |
| 43 | ps = sysinfo.dwPageSize; | |
| 44 | } | |
| 45 | return ps; | |
| 46 | } | |
| 47 | #define SYSTEM_PAGE_SIZE system_page_size() | |
| 48 | #else | |
| 49 | #include <unistd.h> | |
| 50 | #define SYSTEM_PAGE_SIZE sysconf(_SC_PAGESIZE) | |
| 51 | #endif | |
| 52 | ||
| 852 | 53 | static int buffer_copy_on_write(CxBuffer* buffer) { |
| 54 | if (0 == (buffer->flags & CX_BUFFER_COPY_ON_WRITE)) return 0; | |
| 55 | void *newspace = cxMalloc(buffer->allocator, buffer->capacity); | |
| 56 | if (NULL == newspace) return -1; | |
| 57 | memcpy(newspace, buffer->space, buffer->size); | |
| 58 | buffer->space = newspace; | |
| 59 | buffer->flags &= ~CX_BUFFER_COPY_ON_WRITE; | |
| 60 | buffer->flags |= CX_BUFFER_FREE_CONTENTS; | |
| 61 | return 0; | |
| 62 | } | |
|
1
1bcaac272cdf
added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
63 | |
|
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
64 | 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
|
65 | 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
|
66 | 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
|
67 | size_t capacity, |
| 852 | 68 | 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
|
69 | 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
|
70 | ) { |
| 852 | 71 | if (allocator == NULL) { |
| 72 | allocator = cxDefaultAllocator; | |
| 73 | } | |
| 74 | if (flags & CX_BUFFER_COPY_ON_EXTEND) { | |
| 75 | flags |= CX_BUFFER_AUTO_EXTEND; | |
| 76 | } | |
|
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
77 | 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
|
78 | 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
|
79 | 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
|
80 | 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
|
81 | if (buffer->bytes == NULL) { |
| 852 | 82 | return -1; // LCOV_EXCL_LINE |
|
1
1bcaac272cdf
added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
83 | } |
|
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
84 | 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
|
85 | } else { |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
86 | buffer->bytes = space; |
|
1
1bcaac272cdf
added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
87 | } |
|
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
88 | 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
|
89 | 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
|
90 | buffer->pos = 0; |
|
1
1bcaac272cdf
added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
91 | |
| 852 | 92 | buffer->flush = NULL; |
|
1
1bcaac272cdf
added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
93 | |
|
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
94 | return 0; |
|
1
1bcaac272cdf
added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
95 | } |
|
1bcaac272cdf
added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
96 | |
| 852 | 97 | int cxBufferEnableFlushing( |
| 98 | CxBuffer *buffer, | |
| 99 | CxBufferFlushConfig config | |
| 100 | ) { | |
| 888 | 101 | buffer->flush = cxMallocDefault(sizeof(CxBufferFlushConfig)); |
| 852 | 102 | if (buffer->flush == NULL) return -1; // LCOV_EXCL_LINE |
| 103 | memcpy(buffer->flush, &config, sizeof(CxBufferFlushConfig)); | |
| 104 | return 0; | |
| 105 | } | |
| 106 | ||
|
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
107 | void cxBufferDestroy(CxBuffer *buffer) { |
| 852 | 108 | 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
|
109 | 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
|
110 | } |
| 888 | 111 | cxFreeDefault(buffer->flush); |
| 852 | 112 | 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
|
113 | } |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
114 | |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
115 | 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
|
116 | 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
|
117 | size_t capacity, |
| 852 | 118 | 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
|
119 | 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
|
120 | ) { |
| 852 | 121 | if (allocator == NULL) { |
| 122 | allocator = cxDefaultAllocator; | |
| 123 | } | |
|
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
124 | 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
|
125 | 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
|
126 | 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
|
127 | 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
|
128 | } else { |
| 852 | 129 | // 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
|
130 | cxFree(allocator, buf); |
|
1
1bcaac272cdf
added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
131 | return NULL; |
| 852 | 132 | // LCOV_EXCL_STOP |
|
1
1bcaac272cdf
added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
133 | } |
|
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
134 | } |
|
1
1bcaac272cdf
added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
135 | |
|
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
136 | void cxBufferFree(CxBuffer *buffer) { |
| 852 | 137 | if (buffer == NULL) return; |
| 138 | const CxAllocator *allocator = buffer->allocator; | |
| 139 | cxBufferDestroy(buffer); | |
| 140 | cxFree(allocator, buffer); | |
|
1
1bcaac272cdf
added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
141 | } |
|
1bcaac272cdf
added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
142 | |
|
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
143 | 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
|
144 | 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
|
145 | 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
|
146 | 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
|
147 | ) { |
|
17
11dffb40cd91
new dav_query function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
5
diff
changeset
|
148 | size_t npos; |
|
1
1bcaac272cdf
added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
149 | 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
|
150 | 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
|
151 | 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
|
152 | break; |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
153 | 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
|
154 | 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
|
155 | break; |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
156 | 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
|
157 | 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
|
158 | break; |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
159 | default: |
| 888 | 160 | errno = EINVAL; |
|
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
161 | return -1; |
|
1
1bcaac272cdf
added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
162 | } |
|
1bcaac272cdf
added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
163 | |
| 70 | 164 | size_t opos = npos; |
|
1
1bcaac272cdf
added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
165 | 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
|
166 | |
| 70 | 167 | if ((offset > 0 && npos < opos) || (offset < 0 && npos > opos)) { |
| 888 | 168 | // to be compliant with fseek() specification |
| 169 | // we return EINVAL on underflow | |
| 170 | errno = EINVAL; | |
| 70 | 171 | return -1; |
| 172 | } | |
|
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
173 | |
| 852 | 174 | if (npos > buffer->size) { |
| 888 | 175 | // not compliant with fseek() specification |
| 176 | // but this is the better behavior for CxBuffer | |
| 177 | errno = EINVAL; | |
|
1
1bcaac272cdf
added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
178 | return -1; |
|
1bcaac272cdf
added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
179 | } else { |
|
1bcaac272cdf
added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
180 | buffer->pos = npos; |
|
1bcaac272cdf
added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
181 | return 0; |
|
1bcaac272cdf
added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
182 | } |
|
1bcaac272cdf
added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
183 | |
|
1bcaac272cdf
added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
184 | } |
|
1bcaac272cdf
added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
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 | void cxBufferClear(CxBuffer *buffer) { |
| 852 | 187 | if (0 == (buffer->flags & CX_BUFFER_COPY_ON_WRITE)) { |
| 188 | memset(buffer->bytes, 0, buffer->size); | |
| 189 | } | |
|
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
190 | 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
|
191 | 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
|
192 | } |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
193 | |
|
816
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
194 | 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
|
195 | buffer->size = 0; |
|
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
196 | buffer->pos = 0; |
|
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
197 | } |
|
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
198 | |
| 852 | 199 | bool cxBufferEof(const CxBuffer *buffer) { |
|
1
1bcaac272cdf
added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
200 | return buffer->pos >= buffer->size; |
|
1bcaac272cdf
added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
201 | } |
|
1bcaac272cdf
added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
202 | |
|
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
203 | 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
|
204 | 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
|
205 | 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
|
206 | ) { |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
207 | 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
|
208 | return 0; |
| 70 | 209 | } |
|
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 | |
| 888 | 211 | unsigned long pagesize = SYSTEM_PAGE_SIZE; |
| 212 | // if page size is larger than 64 KB - for some reason - truncate to 64 KB | |
| 213 | if (pagesize > 65536) pagesize = 65536; | |
| 214 | if (newcap < pagesize) { | |
| 215 | // when smaller as one page, map to the next power of two | |
| 216 | newcap--; | |
| 217 | newcap |= newcap >> 1; | |
| 218 | newcap |= newcap >> 2; | |
| 219 | newcap |= newcap >> 4; | |
| 220 | // last operation only needed for pages larger 4096 bytes | |
| 221 | // but if/else would be more expensive than just doing this | |
| 222 | newcap |= newcap >> 8; | |
| 223 | newcap++; | |
| 224 | } else { | |
| 225 | // otherwise, map to a multiple of the page size | |
| 226 | newcap -= newcap % pagesize; | |
| 227 | newcap += pagesize; | |
| 228 | // note: if newcap is already page aligned, | |
| 229 | // this gives a full additional page (which is good) | |
| 230 | } | |
| 231 | ||
| 232 | ||
| 852 | 233 | const int force_copy_flags = CX_BUFFER_COPY_ON_WRITE | CX_BUFFER_COPY_ON_EXTEND; |
| 234 | if (buffer->flags & force_copy_flags) { | |
| 235 | void *newspace = cxMalloc(buffer->allocator, newcap); | |
| 236 | if (NULL == newspace) return -1; | |
| 237 | memcpy(newspace, buffer->space, buffer->size); | |
| 238 | buffer->space = newspace; | |
| 239 | buffer->capacity = newcap; | |
| 240 | buffer->flags &= ~force_copy_flags; | |
| 241 | buffer->flags |= CX_BUFFER_FREE_CONTENTS; | |
| 242 | return 0; | |
| 243 | } 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
|
244 | (void **) &buffer->bytes, newcap) == 0) { |
|
1
1bcaac272cdf
added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
245 | 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
|
246 | return 0; |
|
1
1bcaac272cdf
added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
247 | } else { |
| 852 | 248 | return -1; // LCOV_EXCL_LINE |
|
1
1bcaac272cdf
added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
249 | } |
|
1bcaac272cdf
added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
250 | } |
|
1bcaac272cdf
added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
251 | |
| 888 | 252 | void cxBufferShrink( |
| 253 | CxBuffer *buffer, | |
| 254 | size_t reserve | |
| 255 | ) { | |
| 256 | // Ensure buffer is in a reallocatable state | |
| 257 | const int force_copy_flags = CX_BUFFER_COPY_ON_WRITE | CX_BUFFER_COPY_ON_EXTEND; | |
| 258 | if (buffer->flags & force_copy_flags) { | |
| 259 | // do nothing when we are not allowed to reallocate | |
| 260 | return; | |
| 261 | } | |
| 262 | ||
| 263 | // calculate new capacity | |
| 264 | size_t newCapacity = buffer->size + reserve; | |
| 265 | ||
| 266 | // If new capacity is smaller than current capacity, resize the buffer | |
| 267 | if (newCapacity < buffer->capacity) { | |
| 268 | if (0 == cxReallocate(buffer->allocator, &buffer->bytes, newCapacity)) { | |
| 269 | buffer->capacity = newCapacity; | |
| 270 | } | |
| 271 | } | |
| 272 | } | |
| 273 | ||
| 852 | 274 | static size_t cx_buffer_flush_helper( |
| 275 | const CxBuffer *buffer, | |
|
854
1c8401ece69e
update ucx to version 3.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
852
diff
changeset
|
276 | 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
|
277 | 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
|
278 | 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
|
279 | ) { |
| 852 | 280 | // flush data from an arbitrary source |
| 281 | // does not need to be the buffer's contents | |
| 282 | size_t max_items = buffer->flush->blksize / size; | |
| 283 | size_t fblocks = 0; | |
| 284 | size_t flushed_total = 0; | |
| 285 | while (nitems > 0 && fblocks < buffer->flush->blkmax) { | |
| 286 | fblocks++; | |
| 287 | size_t items = nitems > max_items ? max_items : nitems; | |
| 288 | size_t flushed = buffer->flush->wfunc( | |
| 289 | 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
|
290 | if (flushed > 0) { |
| 852 | 291 | flushed_total += flushed; |
| 292 | src += flushed * size; | |
| 293 | 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
|
294 | } else { |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
295 | // 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
|
296 | break; |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
297 | } |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
298 | } |
| 852 | 299 | return flushed_total; |
| 300 | } | |
| 301 | ||
| 302 | static size_t cx_buffer_flush_impl(CxBuffer *buffer, size_t size) { | |
| 303 | // flush the current contents of the buffer | |
| 304 | unsigned char *space = buffer->bytes; | |
| 305 | size_t remaining = buffer->pos / size; | |
| 306 | size_t flushed_total = cx_buffer_flush_helper( | |
|
854
1c8401ece69e
update ucx to version 3.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
852
diff
changeset
|
307 | buffer, space, size, remaining); |
| 852 | 308 | |
| 309 | // shift the buffer left after flushing | |
| 310 | // IMPORTANT: up to this point, copy on write must have been | |
| 311 | // performed already, because we can't do error handling here | |
| 312 | cxBufferShiftLeft(buffer, flushed_total*size); | |
| 313 | ||
| 314 | return flushed_total; | |
| 315 | } | |
| 316 | ||
| 317 | size_t cxBufferFlush(CxBuffer *buffer) { | |
| 318 | if (buffer_copy_on_write(buffer)) return 0; | |
| 319 | 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
|
320 | } |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
321 | |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
322 | size_t cxBufferWrite( |
| 852 | 323 | 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
|
324 | 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
|
325 | 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
|
326 | 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
|
327 | ) { |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
328 | // 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
|
329 | if (size == 1 && (buffer->capacity - buffer->pos) >= nitems) { |
| 852 | 330 | 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
|
331 | 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
|
332 | 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
|
333 | 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
|
334 | 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
|
335 | } |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
336 | 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
|
337 | } |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
338 | |
|
854
1c8401ece69e
update ucx to version 3.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
852
diff
changeset
|
339 | size_t len, total_flushed = 0; |
|
1c8401ece69e
update ucx to version 3.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
852
diff
changeset
|
340 | cx_buffer_write_retry: |
|
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
341 | if (cx_szmul(size, nitems, &len)) { |
| 852 | 342 | errno = EOVERFLOW; |
|
854
1c8401ece69e
update ucx to version 3.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
852
diff
changeset
|
343 | return total_flushed; |
| 852 | 344 | } |
| 345 | if (buffer->pos > SIZE_MAX - len) { | |
| 346 | errno = EOVERFLOW; | |
|
854
1c8401ece69e
update ucx to version 3.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
852
diff
changeset
|
347 | return total_flushed; |
| 505 | 348 | } |
|
854
1c8401ece69e
update ucx to version 3.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
852
diff
changeset
|
349 | |
| 70 | 350 | 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
|
351 | bool perform_flush = false; |
| 70 | 352 | if (required > buffer->capacity) { |
| 852 | 353 | if (buffer->flags & CX_BUFFER_AUTO_EXTEND) { |
| 354 | 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
|
355 | 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
|
356 | } else { |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
357 | if (cxBufferMinimumCapacity(buffer, required)) { |
|
854
1c8401ece69e
update ucx to version 3.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
852
diff
changeset
|
358 | return total_flushed; // 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
|
359 | } |
|
1
1bcaac272cdf
added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
360 | } |
|
1bcaac272cdf
added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
361 | } else { |
| 852 | 362 | 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
|
363 | 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
|
364 | } else { |
| 852 | 365 | // 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
|
366 | 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
|
367 | 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
|
368 | 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
|
369 | } |
| 852 | 370 | nitems = len / size; |
| 70 | 371 | } |
|
1
1bcaac272cdf
added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
372 | } |
|
1bcaac272cdf
added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
373 | } |
|
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
374 | |
| 852 | 375 | // check here and not above because of possible truncation |
| 70 | 376 | if (len == 0) { |
|
854
1c8401ece69e
update ucx to version 3.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
852
diff
changeset
|
377 | return total_flushed; |
|
1
1bcaac272cdf
added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
378 | } |
|
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
379 | |
| 852 | 380 | // check if we need to copy |
| 381 | 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
|
382 | |
| 852 | 383 | // perform the operation |
| 384 | if (perform_flush) { | |
|
854
1c8401ece69e
update ucx to version 3.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
852
diff
changeset
|
385 | size_t items_flushed; |
| 852 | 386 | if (buffer->pos == 0) { |
| 387 | // if we don't have data in the buffer, but are instructed | |
| 388 | // to flush, it means that we are supposed to relay the data | |
|
854
1c8401ece69e
update ucx to version 3.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
852
diff
changeset
|
389 | items_flushed = cx_buffer_flush_helper(buffer, ptr, size, nitems); |
|
1c8401ece69e
update ucx to version 3.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
852
diff
changeset
|
390 | if (items_flushed == 0) { |
|
1c8401ece69e
update ucx to version 3.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
852
diff
changeset
|
391 | // we needed to relay data, but could not flush anything |
|
1c8401ece69e
update ucx to version 3.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
852
diff
changeset
|
392 | // i.e. we have to give up to avoid endless trying |
| 852 | 393 | 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
|
394 | } |
|
854
1c8401ece69e
update ucx to version 3.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
852
diff
changeset
|
395 | nitems -= items_flushed; |
|
1c8401ece69e
update ucx to version 3.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
852
diff
changeset
|
396 | total_flushed += items_flushed; |
|
1c8401ece69e
update ucx to version 3.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
852
diff
changeset
|
397 | if (nitems > 0) { |
|
1c8401ece69e
update ucx to version 3.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
852
diff
changeset
|
398 | ptr = ((unsigned char*)ptr) + items_flushed * size; |
|
1c8401ece69e
update ucx to version 3.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
852
diff
changeset
|
399 | goto cx_buffer_write_retry; |
|
1c8401ece69e
update ucx to version 3.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
852
diff
changeset
|
400 | } |
|
1c8401ece69e
update ucx to version 3.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
852
diff
changeset
|
401 | return total_flushed; |
| 852 | 402 | } else { |
|
854
1c8401ece69e
update ucx to version 3.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
852
diff
changeset
|
403 | items_flushed = cx_buffer_flush_impl(buffer, size); |
|
1c8401ece69e
update ucx to version 3.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
852
diff
changeset
|
404 | if (items_flushed == 0) { |
|
1c8401ece69e
update ucx to version 3.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
852
diff
changeset
|
405 | // flush target is full, let's try to truncate |
|
1c8401ece69e
update ucx to version 3.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
852
diff
changeset
|
406 | size_t remaining_space; |
|
1c8401ece69e
update ucx to version 3.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
852
diff
changeset
|
407 | if (buffer->flags & CX_BUFFER_AUTO_EXTEND) { |
|
1c8401ece69e
update ucx to version 3.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
852
diff
changeset
|
408 | remaining_space = buffer->flush->threshold > buffer->pos |
|
1c8401ece69e
update ucx to version 3.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
852
diff
changeset
|
409 | ? buffer->flush->threshold - buffer->pos |
|
1c8401ece69e
update ucx to version 3.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
852
diff
changeset
|
410 | : 0; |
|
1c8401ece69e
update ucx to version 3.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
852
diff
changeset
|
411 | } else { |
|
1c8401ece69e
update ucx to version 3.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
852
diff
changeset
|
412 | remaining_space = buffer->capacity > buffer->pos |
|
1c8401ece69e
update ucx to version 3.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
852
diff
changeset
|
413 | ? buffer->capacity - buffer->pos |
|
1c8401ece69e
update ucx to version 3.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
852
diff
changeset
|
414 | : 0; |
|
1c8401ece69e
update ucx to version 3.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
852
diff
changeset
|
415 | } |
|
1c8401ece69e
update ucx to version 3.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
852
diff
changeset
|
416 | nitems = remaining_space / size; |
|
1c8401ece69e
update ucx to version 3.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
852
diff
changeset
|
417 | if (nitems == 0) { |
|
1c8401ece69e
update ucx to version 3.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
852
diff
changeset
|
418 | return total_flushed; |
|
1c8401ece69e
update ucx to version 3.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
852
diff
changeset
|
419 | } |
|
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
420 | } |
|
854
1c8401ece69e
update ucx to version 3.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
852
diff
changeset
|
421 | goto cx_buffer_write_retry; |
|
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 | } |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
423 | } else { |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
424 | 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
|
425 | 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
|
426 | 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
|
427 | 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
|
428 | } |
|
854
1c8401ece69e
update ucx to version 3.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
852
diff
changeset
|
429 | return total_flushed + nitems; |
|
1
1bcaac272cdf
added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
430 | } |
|
1bcaac272cdf
added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
431 | } |
|
1bcaac272cdf
added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
432 | |
| 852 | 433 | size_t cxBufferAppend( |
| 434 | const void *ptr, | |
| 435 | size_t size, | |
| 436 | size_t nitems, | |
| 437 | CxBuffer *buffer | |
| 438 | ) { | |
| 439 | size_t pos = buffer->pos; | |
|
854
1c8401ece69e
update ucx to version 3.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
852
diff
changeset
|
440 | size_t append_pos = buffer->size; |
|
1c8401ece69e
update ucx to version 3.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
852
diff
changeset
|
441 | buffer->pos = append_pos; |
| 852 | 442 | size_t written = cxBufferWrite(ptr, size, nitems, buffer); |
|
854
1c8401ece69e
update ucx to version 3.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
852
diff
changeset
|
443 | // the buffer might have been flushed |
|
1c8401ece69e
update ucx to version 3.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
852
diff
changeset
|
444 | // we must compute a possible delta for the position |
|
1c8401ece69e
update ucx to version 3.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
852
diff
changeset
|
445 | // expected: pos = append_pos + written |
|
1c8401ece69e
update ucx to version 3.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
852
diff
changeset
|
446 | // -> if this is not the case, there is a delta |
|
1c8401ece69e
update ucx to version 3.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
852
diff
changeset
|
447 | size_t delta = append_pos + written*size - buffer->pos; |
|
1c8401ece69e
update ucx to version 3.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
852
diff
changeset
|
448 | if (delta > pos) { |
|
1c8401ece69e
update ucx to version 3.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
852
diff
changeset
|
449 | buffer->pos = 0; |
|
1c8401ece69e
update ucx to version 3.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
852
diff
changeset
|
450 | } else { |
|
1c8401ece69e
update ucx to version 3.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
852
diff
changeset
|
451 | buffer->pos = pos - delta; |
|
1c8401ece69e
update ucx to version 3.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
852
diff
changeset
|
452 | } |
| 852 | 453 | return written; |
| 454 | } | |
| 455 | ||
|
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
456 | 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
|
457 | 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
|
458 | 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
|
459 | ) { |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
460 | 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
|
461 | 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
|
462 | 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
|
463 | 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
|
464 | } else { |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
465 | 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
|
466 | } |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
467 | } |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
468 | |
| 852 | 469 | int cxBufferTerminate(CxBuffer *buffer) { |
| 888 | 470 | if (0 == cxBufferPut(buffer, 0)) { |
| 471 | buffer->size = buffer->pos - 1; | |
| 852 | 472 | return 0; |
| 473 | } else { | |
| 474 | return -1; | |
| 475 | } | |
| 476 | } | |
| 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 | 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
|
479 | 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
|
480 | 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
|
481 | ) { |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
482 | 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
|
483 | } |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
484 | |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
485 | 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
|
486 | 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
|
487 | 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
|
488 | 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
|
489 | 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
|
490 | ) { |
| 505 | 491 | 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
|
492 | if (cx_szmul(size, nitems, &len)) { |
| 852 | 493 | errno = EOVERFLOW; |
| 505 | 494 | return 0; |
| 495 | } | |
|
1
1bcaac272cdf
added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
496 | if (buffer->pos + len > buffer->size) { |
|
1bcaac272cdf
added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
497 | 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
|
498 | if (size > 1) len -= len % size; |
|
1
1bcaac272cdf
added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
499 | } |
|
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
500 | |
|
1
1bcaac272cdf
added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
501 | if (len <= 0) { |
|
1bcaac272cdf
added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
502 | return len; |
|
1bcaac272cdf
added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
503 | } |
|
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
504 | |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
505 | memcpy(ptr, buffer->bytes + buffer->pos, len); |
|
1
1bcaac272cdf
added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
506 | 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
|
507 | |
|
1
1bcaac272cdf
added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
508 | return len / size; |
|
1bcaac272cdf
added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
509 | } |
|
1bcaac272cdf
added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
510 | |
|
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
511 | 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
|
512 | if (cxBufferEof(buffer)) { |
|
1
1bcaac272cdf
added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
513 | return EOF; |
|
1bcaac272cdf
added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
514 | } 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
|
515 | int c = buffer->bytes[buffer->pos]; |
|
1
1bcaac272cdf
added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
516 | buffer->pos++; |
|
1bcaac272cdf
added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
517 | return c; |
|
1bcaac272cdf
added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
518 | } |
|
1bcaac272cdf
added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
519 | } |
|
1bcaac272cdf
added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
520 | |
|
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
521 | 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
|
522 | 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
|
523 | 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
|
524 | ) { |
| 505 | 525 | if (shift >= buffer->size) { |
| 526 | buffer->pos = buffer->size = 0; | |
| 527 | } else { | |
| 852 | 528 | 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
|
529 | memmove(buffer->bytes, buffer->bytes + shift, buffer->size - shift); |
| 505 | 530 | 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
|
531 | |
| 505 | 532 | if (buffer->pos >= shift) { |
| 533 | buffer->pos -= shift; | |
| 534 | } else { | |
| 535 | buffer->pos = 0; | |
| 536 | } | |
| 537 | } | |
| 538 | return 0; | |
| 539 | } | |
| 540 | ||
|
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
541 | 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
|
542 | 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
|
543 | 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
|
544 | ) { |
| 852 | 545 | if (buffer->size > SIZE_MAX - shift) { |
| 546 | errno = EOVERFLOW; | |
| 547 | return -1; | |
| 548 | } | |
| 505 | 549 | size_t req_capacity = buffer->size + shift; |
| 550 | 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
|
551 | |
| 505 | 552 | // auto extend buffer, if required and enabled |
| 553 | if (buffer->capacity < req_capacity) { | |
| 852 | 554 | 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
|
555 | if (cxBufferMinimumCapacity(buffer, req_capacity)) { |
| 852 | 556 | return -1; // LCOV_EXCL_LINE |
| 505 | 557 | } |
| 558 | movebytes = buffer->size; | |
| 559 | } else { | |
| 560 | movebytes = buffer->capacity - shift; | |
| 561 | } | |
| 562 | } else { | |
| 563 | movebytes = buffer->size; | |
| 564 | } | |
|
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
565 | |
| 852 | 566 | if (movebytes > 0) { |
| 567 | if (buffer_copy_on_write(buffer)) return -1; | |
| 568 | memmove(buffer->bytes + shift, buffer->bytes, movebytes); | |
| 569 | buffer->size = shift + movebytes; | |
| 570 | } | |
|
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
571 | |
| 505 | 572 | buffer->pos += shift; |
| 573 | if (buffer->pos > buffer->size) { | |
| 574 | buffer->pos = buffer->size; | |
| 575 | } | |
|
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
576 | |
| 505 | 577 | return 0; |
| 578 | } | |
| 579 | ||
|
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
580 | 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
|
581 | 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
|
582 | 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
|
583 | ) { |
| 505 | 584 | 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
|
585 | return cxBufferShiftLeft(buffer, (size_t) (-shift)); |
| 505 | 586 | } 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
|
587 | return cxBufferShiftRight(buffer, (size_t) shift); |
| 505 | 588 | } else { |
| 589 | return 0; | |
| 590 | } | |
| 591 | } |