ucx/buffer.c

Mon, 06 Jan 2025 21:18:36 +0100

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Mon, 06 Jan 2025 21:18:36 +0100
changeset 852
83fdf679df99
parent 816
839fefbdedc7
permissions
-rw-r--r--

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
83fdf679df99 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 816
diff changeset
33 #include <errno.h>
83fdf679df99 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 816
diff changeset
34
83fdf679df99 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 816
diff changeset
35 static int buffer_copy_on_write(CxBuffer* buffer) {
83fdf679df99 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 816
diff changeset
36 if (0 == (buffer->flags & CX_BUFFER_COPY_ON_WRITE)) return 0;
83fdf679df99 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 816
diff changeset
37 void *newspace = cxMalloc(buffer->allocator, buffer->capacity);
83fdf679df99 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 816
diff changeset
38 if (NULL == newspace) return -1;
83fdf679df99 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 816
diff changeset
39 memcpy(newspace, buffer->space, buffer->size);
83fdf679df99 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 816
diff changeset
40 buffer->space = newspace;
83fdf679df99 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 816
diff changeset
41 buffer->flags &= ~CX_BUFFER_COPY_ON_WRITE;
83fdf679df99 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 816
diff changeset
42 buffer->flags |= CX_BUFFER_FREE_CONTENTS;
83fdf679df99 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 816
diff changeset
43 return 0;
83fdf679df99 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 816
diff changeset
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
83fdf679df99 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 816
diff changeset
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
83fdf679df99 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 816
diff changeset
53 if (allocator == NULL) {
83fdf679df99 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 816
diff changeset
54 allocator = cxDefaultAllocator;
83fdf679df99 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 816
diff changeset
55 }
83fdf679df99 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 816
diff changeset
56 if (flags & CX_BUFFER_COPY_ON_EXTEND) {
83fdf679df99 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 816
diff changeset
57 flags |= CX_BUFFER_AUTO_EXTEND;
83fdf679df99 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 816
diff changeset
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
83fdf679df99 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 816
diff changeset
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
83fdf679df99 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 816
diff changeset
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
83fdf679df99 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 816
diff changeset
79 int cxBufferEnableFlushing(
83fdf679df99 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 816
diff changeset
80 CxBuffer *buffer,
83fdf679df99 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 816
diff changeset
81 CxBufferFlushConfig config
83fdf679df99 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 816
diff changeset
82 ) {
83fdf679df99 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 816
diff changeset
83 buffer->flush = malloc(sizeof(CxBufferFlushConfig));
83fdf679df99 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 816
diff changeset
84 if (buffer->flush == NULL) return -1; // LCOV_EXCL_LINE
83fdf679df99 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 816
diff changeset
85 memcpy(buffer->flush, &config, sizeof(CxBufferFlushConfig));
83fdf679df99 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 816
diff changeset
86 return 0;
83fdf679df99 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 816
diff changeset
87 }
83fdf679df99 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 816
diff changeset
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
83fdf679df99 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 816
diff changeset
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
83fdf679df99 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 816
diff changeset
93 free(buffer->flush);
83fdf679df99 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 816
diff changeset
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
83fdf679df99 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 816
diff changeset
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
83fdf679df99 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 816
diff changeset
103 if (allocator == NULL) {
83fdf679df99 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 816
diff changeset
104 allocator = cxDefaultAllocator;
83fdf679df99 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 816
diff changeset
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
83fdf679df99 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 816
diff changeset
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
83fdf679df99 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 816
diff changeset
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
83fdf679df99 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 816
diff changeset
119 if (buffer == NULL) return;
83fdf679df99 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 816
diff changeset
120 const CxAllocator *allocator = buffer->allocator;
83fdf679df99 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 816
diff changeset
121 cxBufferDestroy(buffer);
83fdf679df99 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 816
diff changeset
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
88092b88ec00 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 17
diff changeset
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
88092b88ec00 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 17
diff changeset
148 if ((offset > 0 && npos < opos) || (offset < 0 && npos > opos)) {
852
83fdf679df99 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 816
diff changeset
149 errno = EOVERFLOW;
70
88092b88ec00 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 17
diff changeset
150 return -1;
88092b88ec00 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 17
diff changeset
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
83fdf679df99 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 816
diff changeset
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
83fdf679df99 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 816
diff changeset
163 if (0 == (buffer->flags & CX_BUFFER_COPY_ON_WRITE)) {
83fdf679df99 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 816
diff changeset
164 memset(buffer->bytes, 0, buffer->size);
83fdf679df99 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 816
diff changeset
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
83fdf679df99 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 816
diff changeset
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
88092b88ec00 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 17
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
852
83fdf679df99 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 816
diff changeset
187 const int force_copy_flags = CX_BUFFER_COPY_ON_WRITE | CX_BUFFER_COPY_ON_EXTEND;
83fdf679df99 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 816
diff changeset
188 if (buffer->flags & force_copy_flags) {
83fdf679df99 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 816
diff changeset
189 void *newspace = cxMalloc(buffer->allocator, newcap);
83fdf679df99 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 816
diff changeset
190 if (NULL == newspace) return -1;
83fdf679df99 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 816
diff changeset
191 memcpy(newspace, buffer->space, buffer->size);
83fdf679df99 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 816
diff changeset
192 buffer->space = newspace;
83fdf679df99 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 816
diff changeset
193 buffer->capacity = newcap;
83fdf679df99 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 816
diff changeset
194 buffer->flags &= ~force_copy_flags;
83fdf679df99 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 816
diff changeset
195 buffer->flags |= CX_BUFFER_FREE_CONTENTS;
83fdf679df99 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 816
diff changeset
196 return 0;
83fdf679df99 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 816
diff changeset
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
83fdf679df99 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 816
diff changeset
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
83fdf679df99 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 816
diff changeset
206 static size_t cx_buffer_flush_helper(
83fdf679df99 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 816
diff changeset
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
83fdf679df99 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 816
diff changeset
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
83fdf679df99 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 816
diff changeset
212 // flush data from an arbitrary source
83fdf679df99 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 816
diff changeset
213 // does not need to be the buffer's contents
83fdf679df99 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 816
diff changeset
214 size_t max_items = buffer->flush->blksize / size;
83fdf679df99 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 816
diff changeset
215 size_t fblocks = 0;
83fdf679df99 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 816
diff changeset
216 size_t flushed_total = 0;
83fdf679df99 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 816
diff changeset
217 while (nitems > 0 && fblocks < buffer->flush->blkmax) {
83fdf679df99 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 816
diff changeset
218 fblocks++;
83fdf679df99 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 816
diff changeset
219 size_t items = nitems > max_items ? max_items : nitems;
83fdf679df99 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 816
diff changeset
220 size_t flushed = buffer->flush->wfunc(
83fdf679df99 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 816
diff changeset
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
83fdf679df99 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 816
diff changeset
223 flushed_total += flushed;
83fdf679df99 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 816
diff changeset
224 src += flushed * size;
83fdf679df99 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 816
diff changeset
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
83fdf679df99 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 816
diff changeset
231 return flushed_total;
83fdf679df99 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 816
diff changeset
232 }
83fdf679df99 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 816
diff changeset
233
83fdf679df99 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 816
diff changeset
234 static size_t cx_buffer_flush_impl(CxBuffer *buffer, size_t size) {
83fdf679df99 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 816
diff changeset
235 // flush the current contents of the buffer
83fdf679df99 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 816
diff changeset
236 unsigned char *space = buffer->bytes;
83fdf679df99 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 816
diff changeset
237 size_t remaining = buffer->pos / size;
83fdf679df99 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 816
diff changeset
238 size_t flushed_total = cx_buffer_flush_helper(
83fdf679df99 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 816
diff changeset
239 buffer, size, space, remaining);
83fdf679df99 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 816
diff changeset
240
83fdf679df99 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 816
diff changeset
241 // shift the buffer left after flushing
83fdf679df99 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 816
diff changeset
242 // IMPORTANT: up to this point, copy on write must have been
83fdf679df99 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 816
diff changeset
243 // performed already, because we can't do error handling here
83fdf679df99 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 816
diff changeset
244 cxBufferShiftLeft(buffer, flushed_total*size);
83fdf679df99 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 816
diff changeset
245
83fdf679df99 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 816
diff changeset
246 return flushed_total;
83fdf679df99 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 816
diff changeset
247 }
83fdf679df99 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 816
diff changeset
248
83fdf679df99 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 816
diff changeset
249 size_t cxBufferFlush(CxBuffer *buffer) {
83fdf679df99 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 816
diff changeset
250 if (buffer_copy_on_write(buffer)) return 0;
83fdf679df99 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 816
diff changeset
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
83fdf679df99 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 816
diff changeset
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
83fdf679df99 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 816
diff changeset
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
481802342fdf ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 335
diff changeset
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
83fdf679df99 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 816
diff changeset
273 errno = EOVERFLOW;
83fdf679df99 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 816
diff changeset
274 return 0;
83fdf679df99 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 816
diff changeset
275 }
83fdf679df99 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 816
diff changeset
276 if (buffer->pos > SIZE_MAX - len) {
83fdf679df99 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 816
diff changeset
277 errno = EOVERFLOW;
505
481802342fdf ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 335
diff changeset
278 return 0;
481802342fdf ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 335
diff changeset
279 }
70
88092b88ec00 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 17
diff changeset
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
88092b88ec00 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 17
diff changeset
283 if (required > buffer->capacity) {
852
83fdf679df99 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 816
diff changeset
284 if (buffer->flags & CX_BUFFER_AUTO_EXTEND) {
83fdf679df99 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 816
diff changeset
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
83fdf679df99 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 816
diff changeset
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
83fdf679df99 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 816
diff changeset
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
83fdf679df99 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 816
diff changeset
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
83fdf679df99 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 816
diff changeset
301 nitems = len / size;
70
88092b88ec00 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 17
diff changeset
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
83fdf679df99 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 816
diff changeset
306 // check here and not above because of possible truncation
70
88092b88ec00 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 17
diff changeset
307 if (len == 0) {
852
83fdf679df99 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 816
diff changeset
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
83fdf679df99 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 816
diff changeset
311 // check if we need to copy
83fdf679df99 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 816
diff changeset
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
83fdf679df99 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 816
diff changeset
314 // perform the operation
83fdf679df99 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 816
diff changeset
315 if (perform_flush) {
83fdf679df99 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 816
diff changeset
316 size_t items_flush;
83fdf679df99 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 816
diff changeset
317 if (buffer->pos == 0) {
83fdf679df99 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 816
diff changeset
318 // if we don't have data in the buffer, but are instructed
83fdf679df99 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 816
diff changeset
319 // to flush, it means that we are supposed to relay the data
83fdf679df99 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 816
diff changeset
320 items_flush = cx_buffer_flush_helper(buffer, size, ptr, nitems);
83fdf679df99 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 816
diff changeset
321 if (items_flush == 0) {
83fdf679df99 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 816
diff changeset
322 // we needed to flush, but could not flush anything
83fdf679df99 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 816
diff changeset
323 // give up and avoid endless trying
83fdf679df99 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 816
diff changeset
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
83fdf679df99 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 816
diff changeset
326 size_t ritems = nitems - items_flush;
83fdf679df99 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 816
diff changeset
327 const unsigned char *rest = ptr;
83fdf679df99 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 816
diff changeset
328 rest += items_flush * size;
83fdf679df99 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 816
diff changeset
329 return items_flush + cxBufferWrite(rest, size, ritems, buffer);
83fdf679df99 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 816
diff changeset
330 } else {
83fdf679df99 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 816
diff changeset
331 items_flush = cx_buffer_flush_impl(buffer, size);
83fdf679df99 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 816
diff changeset
332 if (items_flush == 0) {
83fdf679df99 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 816
diff changeset
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
83fdf679df99 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 816
diff changeset
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
83fdf679df99 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 816
diff changeset
348 size_t cxBufferAppend(
83fdf679df99 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 816
diff changeset
349 const void *ptr,
83fdf679df99 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 816
diff changeset
350 size_t size,
83fdf679df99 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 816
diff changeset
351 size_t nitems,
83fdf679df99 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 816
diff changeset
352 CxBuffer *buffer
83fdf679df99 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 816
diff changeset
353 ) {
83fdf679df99 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 816
diff changeset
354 size_t pos = buffer->pos;
83fdf679df99 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 816
diff changeset
355 buffer->pos = buffer->size;
83fdf679df99 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 816
diff changeset
356 size_t written = cxBufferWrite(ptr, size, nitems, buffer);
83fdf679df99 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 816
diff changeset
357 buffer->pos = pos;
83fdf679df99 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 816
diff changeset
358 return written;
83fdf679df99 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 816
diff changeset
359 }
83fdf679df99 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 816
diff changeset
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
83fdf679df99 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 816
diff changeset
374 int cxBufferTerminate(CxBuffer *buffer) {
83fdf679df99 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 816
diff changeset
375 bool success = 0 == cxBufferPut(buffer, 0);
83fdf679df99 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 816
diff changeset
376 if (success) {
83fdf679df99 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 816
diff changeset
377 buffer->pos--;
83fdf679df99 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 816
diff changeset
378 buffer->size--;
83fdf679df99 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 816
diff changeset
379 return 0;
83fdf679df99 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 816
diff changeset
380 } else {
83fdf679df99 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 816
diff changeset
381 return -1;
83fdf679df99 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 816
diff changeset
382 }
83fdf679df99 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 816
diff changeset
383 }
83fdf679df99 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 816
diff changeset
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
481802342fdf ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 335
diff changeset
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
83fdf679df99 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 816
diff changeset
400 errno = EOVERFLOW;
505
481802342fdf ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 335
diff changeset
401 return 0;
481802342fdf ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 335
diff changeset
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
481802342fdf ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 335
diff changeset
432 if (shift >= buffer->size) {
481802342fdf ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 335
diff changeset
433 buffer->pos = buffer->size = 0;
481802342fdf ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 335
diff changeset
434 } else {
852
83fdf679df99 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 816
diff changeset
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
481802342fdf ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 335
diff changeset
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
481802342fdf ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 335
diff changeset
439 if (buffer->pos >= shift) {
481802342fdf ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 335
diff changeset
440 buffer->pos -= shift;
481802342fdf ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 335
diff changeset
441 } else {
481802342fdf ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 335
diff changeset
442 buffer->pos = 0;
481802342fdf ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 335
diff changeset
443 }
481802342fdf ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 335
diff changeset
444 }
481802342fdf ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 335
diff changeset
445 return 0;
481802342fdf ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 335
diff changeset
446 }
481802342fdf ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 335
diff changeset
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
83fdf679df99 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 816
diff changeset
452 if (buffer->size > SIZE_MAX - shift) {
83fdf679df99 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 816
diff changeset
453 errno = EOVERFLOW;
83fdf679df99 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 816
diff changeset
454 return -1;
83fdf679df99 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 816
diff changeset
455 }
505
481802342fdf ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 335
diff changeset
456 size_t req_capacity = buffer->size + shift;
481802342fdf ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 335
diff changeset
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
481802342fdf ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 335
diff changeset
459 // auto extend buffer, if required and enabled
481802342fdf ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 335
diff changeset
460 if (buffer->capacity < req_capacity) {
852
83fdf679df99 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 816
diff changeset
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
83fdf679df99 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 816
diff changeset
463 return -1; // LCOV_EXCL_LINE
505
481802342fdf ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 335
diff changeset
464 }
481802342fdf ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 335
diff changeset
465 movebytes = buffer->size;
481802342fdf ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 335
diff changeset
466 } else {
481802342fdf ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 335
diff changeset
467 movebytes = buffer->capacity - shift;
481802342fdf ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 335
diff changeset
468 }
481802342fdf ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 335
diff changeset
469 } else {
481802342fdf ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 335
diff changeset
470 movebytes = buffer->size;
481802342fdf ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 335
diff changeset
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
83fdf679df99 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 816
diff changeset
473 if (movebytes > 0) {
83fdf679df99 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 816
diff changeset
474 if (buffer_copy_on_write(buffer)) return -1;
83fdf679df99 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 816
diff changeset
475 memmove(buffer->bytes + shift, buffer->bytes, movebytes);
83fdf679df99 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 816
diff changeset
476 buffer->size = shift + movebytes;
83fdf679df99 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 816
diff changeset
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
481802342fdf ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 335
diff changeset
479 buffer->pos += shift;
481802342fdf ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 335
diff changeset
480 if (buffer->pos > buffer->size) {
481802342fdf ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 335
diff changeset
481 buffer->pos = buffer->size;
481802342fdf ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 335
diff changeset
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
481802342fdf ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 335
diff changeset
484 return 0;
481802342fdf ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 335
diff changeset
485 }
481802342fdf ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 335
diff changeset
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
481802342fdf ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 335
diff changeset
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
481802342fdf ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 335
diff changeset
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
481802342fdf ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 335
diff changeset
495 } else {
481802342fdf ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 335
diff changeset
496 return 0;
481802342fdf ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 335
diff changeset
497 }
481802342fdf ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 335
diff changeset
498 }

mercurial