ucx/buffer.c

Tue, 23 Jul 2024 14:40:20 +0200

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Tue, 23 Jul 2024 14:40:20 +0200
changeset 825
ca568c209dfd
parent 816
839fefbdedc7
permissions
-rw-r--r--

also remove \r from user name

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"
efbd59642577 ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 505
diff changeset
30 #include "cx/utils.h"
335
c1bc13faadaa updates ucx to version 1.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 308
diff changeset
31
747
efbd59642577 ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 505
diff changeset
32 #include <stdio.h>
1
1bcaac272cdf added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
33 #include <string.h>
1bcaac272cdf added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
34
747
efbd59642577 ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 505
diff changeset
35 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
36 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
37 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
38 size_t capacity,
efbd59642577 ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 505
diff changeset
39 CxAllocator const *allocator,
efbd59642577 ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 505
diff changeset
40 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
41 ) {
efbd59642577 ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 505
diff changeset
42 if (allocator == NULL) allocator = cxDefaultAllocator;
efbd59642577 ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 505
diff changeset
43 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
44 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
45 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
46 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
47 if (buffer->bytes == NULL) {
efbd59642577 ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 505
diff changeset
48 return 1;
1
1bcaac272cdf added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
49 }
747
efbd59642577 ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 505
diff changeset
50 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
51 } else {
efbd59642577 ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 505
diff changeset
52 buffer->bytes = space;
1
1bcaac272cdf added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
53 }
747
efbd59642577 ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 505
diff changeset
54 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
55 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
56 buffer->pos = 0;
1
1bcaac272cdf added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
57
747
efbd59642577 ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 505
diff changeset
58 buffer->flush_func = NULL;
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->flush_target = NULL;
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->flush_blkmax = 0;
efbd59642577 ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 505
diff changeset
61 buffer->flush_blksize = 4096;
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->flush_threshold = SIZE_MAX;
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 return 0;
1
1bcaac272cdf added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
65 }
1bcaac272cdf added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
66
747
efbd59642577 ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 505
diff changeset
67 void cxBufferDestroy(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
68 if ((buffer->flags & CX_BUFFER_FREE_CONTENTS) == 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
69 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
70 }
efbd59642577 ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 505
diff changeset
71 }
efbd59642577 ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 505
diff changeset
72
efbd59642577 ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 505
diff changeset
73 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
74 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
75 size_t capacity,
efbd59642577 ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 505
diff changeset
76 CxAllocator const *allocator,
efbd59642577 ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 505
diff changeset
77 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
78 ) {
efbd59642577 ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 505
diff changeset
79 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
80 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
81 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
82 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
83 } else {
efbd59642577 ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 505
diff changeset
84 cxFree(allocator, buf);
1
1bcaac272cdf added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
85 return NULL;
1bcaac272cdf added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
86 }
747
efbd59642577 ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 505
diff changeset
87 }
1
1bcaac272cdf added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
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 cxBufferFree(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
90 if ((buffer->flags & CX_BUFFER_FREE_CONTENTS) == 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
91 cxFree(buffer->allocator, buffer->bytes);
1
1bcaac272cdf added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
92 }
747
efbd59642577 ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 505
diff changeset
93 cxFree(buffer->allocator, buffer);
1
1bcaac272cdf added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
94 }
1bcaac272cdf added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
95
747
efbd59642577 ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 505
diff changeset
96 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
97 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
98 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
99 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
100 ) {
17
11dffb40cd91 new dav_query function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
101 size_t npos;
1
1bcaac272cdf added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
102 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
103 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
104 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
105 break;
efbd59642577 ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 505
diff changeset
106 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
107 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
108 break;
efbd59642577 ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 505
diff changeset
109 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
110 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
111 break;
efbd59642577 ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 505
diff changeset
112 default:
efbd59642577 ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 505
diff changeset
113 return -1;
1
1bcaac272cdf added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
114 }
1bcaac272cdf added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
115
70
88092b88ec00 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 17
diff changeset
116 size_t opos = npos;
1
1bcaac272cdf added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
117 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
118
70
88092b88ec00 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 17
diff changeset
119 if ((offset > 0 && npos < opos) || (offset < 0 && npos > opos)) {
88092b88ec00 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 17
diff changeset
120 return -1;
88092b88ec00 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 17
diff changeset
121 }
747
efbd59642577 ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 505
diff changeset
122
70
88092b88ec00 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 17
diff changeset
123 if (npos >= buffer->size) {
1
1bcaac272cdf added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
124 return -1;
1bcaac272cdf added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
125 } else {
1bcaac272cdf added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
126 buffer->pos = npos;
1bcaac272cdf added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
127 return 0;
1bcaac272cdf added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
128 }
1bcaac272cdf added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
129
1bcaac272cdf added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
130 }
1bcaac272cdf added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
131
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 void cxBufferClear(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
133 memset(buffer->bytes, 0, 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
134 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
135 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
136 }
efbd59642577 ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 505
diff changeset
137
816
839fefbdedc7 compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents: 747
diff changeset
138 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
139 buffer->size = 0;
839fefbdedc7 compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents: 747
diff changeset
140 buffer->pos = 0;
839fefbdedc7 compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents: 747
diff changeset
141 }
839fefbdedc7 compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents: 747
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 cxBufferEof(CxBuffer const *buffer) {
1
1bcaac272cdf added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
144 return buffer->pos >= buffer->size;
1bcaac272cdf added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
145 }
1bcaac272cdf added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
146
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 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
148 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
149 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
150 ) {
efbd59642577 ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 505
diff changeset
151 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
152 return 0;
70
88092b88ec00 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 17
diff changeset
153 }
747
efbd59642577 ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 505
diff changeset
154
efbd59642577 ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 505
diff changeset
155 if (cxReallocate(buffer->allocator,
efbd59642577 ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 505
diff changeset
156 (void **) &buffer->bytes, newcap) == 0) {
1
1bcaac272cdf added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
157 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
158 return 0;
1
1bcaac272cdf added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
159 } else {
1bcaac272cdf added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
160 return -1;
1bcaac272cdf added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
161 }
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
747
efbd59642577 ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 505
diff changeset
164 /**
efbd59642577 ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 505
diff changeset
165 * Helps flushing data to the flush target of a buffer.
efbd59642577 ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 505
diff changeset
166 *
efbd59642577 ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 505
diff changeset
167 * @param buffer the buffer containing the config
efbd59642577 ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 505
diff changeset
168 * @param space the data to flush
efbd59642577 ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 505
diff changeset
169 * @param size the element size
efbd59642577 ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 505
diff changeset
170 * @param nitems the number of items
efbd59642577 ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 505
diff changeset
171 * @return the number of items flushed
efbd59642577 ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 505
diff changeset
172 */
efbd59642577 ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 505
diff changeset
173 static size_t cx_buffer_write_flush_helper(
efbd59642577 ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 505
diff changeset
174 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
175 unsigned char const *space,
efbd59642577 ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 505
diff changeset
176 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
177 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
178 ) {
efbd59642577 ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 505
diff changeset
179 size_t 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
180 size_t remaining = nitems;
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 max_items = buffer->flush_blksize / size;
efbd59642577 ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 505
diff changeset
182 while (remaining > 0) {
efbd59642577 ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 505
diff changeset
183 size_t items = remaining > max_items ? max_items : remaining;
efbd59642577 ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 505
diff changeset
184 size_t flushed = buffer->flush_func(
efbd59642577 ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 505
diff changeset
185 space + pos,
efbd59642577 ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 505
diff changeset
186 size, items,
efbd59642577 ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 505
diff changeset
187 buffer->flush_target);
efbd59642577 ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 505
diff changeset
188 if (flushed > 0) {
efbd59642577 ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 505
diff changeset
189 pos += (flushed * size);
efbd59642577 ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 505
diff changeset
190 remaining -= flushed;
efbd59642577 ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 505
diff changeset
191 } else {
efbd59642577 ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 505
diff changeset
192 // 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
193 break;
efbd59642577 ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 505
diff changeset
194 }
efbd59642577 ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 505
diff changeset
195 }
efbd59642577 ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 505
diff changeset
196 return nitems - remaining;
efbd59642577 ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 505
diff changeset
197 }
efbd59642577 ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 505
diff changeset
198
efbd59642577 ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 505
diff changeset
199 size_t cxBufferWrite(
efbd59642577 ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 505
diff changeset
200 void const *ptr,
efbd59642577 ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 505
diff changeset
201 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
202 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
203 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
204 ) {
efbd59642577 ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 505
diff changeset
205 // 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
206 if (size == 1 && (buffer->capacity - 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
207 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
208 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
209 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
210 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
211 }
efbd59642577 ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 505
diff changeset
212 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
213 }
efbd59642577 ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 505
diff changeset
214
505
481802342fdf ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 335
diff changeset
215 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
216 size_t nitems_out = nitems;
efbd59642577 ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 505
diff changeset
217 if (cx_szmul(size, nitems, &len)) {
505
481802342fdf ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 335
diff changeset
218 return 0;
481802342fdf ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 335
diff changeset
219 }
70
88092b88ec00 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 17
diff changeset
220 size_t required = buffer->pos + len;
88092b88ec00 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 17
diff changeset
221 if (buffer->pos > required) {
88092b88ec00 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 17
diff changeset
222 return 0;
88092b88ec00 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 17
diff changeset
223 }
747
efbd59642577 ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 505
diff changeset
224
efbd59642577 ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 505
diff changeset
225 bool perform_flush = false;
70
88092b88ec00 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 17
diff changeset
226 if (required > buffer->capacity) {
747
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 ((buffer->flags & CX_BUFFER_AUTO_EXTEND) == CX_BUFFER_AUTO_EXTEND && required) {
efbd59642577 ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 505
diff changeset
228 if (buffer->flush_blkmax > 0 && required > buffer->flush_threshold) {
efbd59642577 ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 505
diff changeset
229 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
230 } else {
efbd59642577 ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 505
diff changeset
231 if (cxBufferMinimumCapacity(buffer, required)) {
efbd59642577 ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 505
diff changeset
232 return 0;
efbd59642577 ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 505
diff changeset
233 }
1
1bcaac272cdf added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
234 }
1bcaac272cdf added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
235 } 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
236 if (buffer->flush_blkmax > 0) {
efbd59642577 ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 505
diff changeset
237 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
238 } else {
efbd59642577 ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 505
diff changeset
239 // truncate data to be written, if we can neither extend nor flush
efbd59642577 ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 505
diff changeset
240 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
241 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
242 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
243 }
efbd59642577 ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 505
diff changeset
244 nitems_out = len / size;
70
88092b88ec00 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 17
diff changeset
245 }
1
1bcaac272cdf added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
246 }
1bcaac272cdf added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
247 }
747
efbd59642577 ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 505
diff changeset
248
70
88092b88ec00 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 17
diff changeset
249 if (len == 0) {
1
1bcaac272cdf added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
250 return len;
1bcaac272cdf added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
251 }
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 if (perform_flush) {
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 flush_max;
efbd59642577 ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 505
diff changeset
255 if (cx_szmul(buffer->flush_blkmax, buffer->flush_blksize, &flush_max)) {
efbd59642577 ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 505
diff changeset
256 return 0;
efbd59642577 ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 505
diff changeset
257 }
efbd59642577 ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 505
diff changeset
258 size_t flush_pos = buffer->flush_func == NULL || buffer->flush_target == NULL
efbd59642577 ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 505
diff changeset
259 ? 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
260 : cx_buffer_write_flush_helper(buffer, buffer->bytes, 1, 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
261 if (flush_pos == 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
262 // entire buffer has been flushed, we can reset
efbd59642577 ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 505
diff changeset
263 buffer->size = 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
264
efbd59642577 ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 505
diff changeset
265 size_t items_flush; // how many items can also be directly flushed
efbd59642577 ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 505
diff changeset
266 size_t items_keep; // how many items have to be written to the buffer
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 items_flush = flush_max >= required ? nitems : (flush_max - flush_pos) / size;
efbd59642577 ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 505
diff changeset
269 if (items_flush > 0) {
efbd59642577 ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 505
diff changeset
270 items_flush = cx_buffer_write_flush_helper(buffer, ptr, size, items_flush / size);
efbd59642577 ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 505
diff changeset
271 // in case we could not flush everything, keep the rest
efbd59642577 ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 505
diff changeset
272 }
efbd59642577 ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 505
diff changeset
273 items_keep = nitems - items_flush;
efbd59642577 ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 505
diff changeset
274 if (items_keep > 0) {
efbd59642577 ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 505
diff changeset
275 // try again with the remaining stuff
efbd59642577 ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 505
diff changeset
276 unsigned char const *new_ptr = ptr;
efbd59642577 ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 505
diff changeset
277 new_ptr += items_flush * 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 // report the directly flushed items as written plus the remaining stuff
efbd59642577 ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 505
diff changeset
279 return items_flush + cxBufferWrite(new_ptr, size, items_keep, buffer);
efbd59642577 ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 505
diff changeset
280 } else {
efbd59642577 ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 505
diff changeset
281 // all items have been flushed - report them as written
efbd59642577 ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 505
diff changeset
282 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
283 }
efbd59642577 ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 505
diff changeset
284 } else if (flush_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
285 // nothing could be flushed at all, we immediately give up without writing any data
efbd59642577 ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 505
diff changeset
286 return 0;
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 // we were partially successful, we shift left and try again
efbd59642577 ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 505
diff changeset
289 cxBufferShiftLeft(buffer, flush_pos);
efbd59642577 ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 505
diff changeset
290 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
291 }
efbd59642577 ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 505
diff changeset
292 } else {
efbd59642577 ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 505
diff changeset
293 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
294 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
295 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
296 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
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 return nitems_out;
1
1bcaac272cdf added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
299 }
747
efbd59642577 ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 505
diff changeset
300
1
1bcaac272cdf added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
301 }
1bcaac272cdf added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
302
747
efbd59642577 ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 505
diff changeset
303 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
304 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
305 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
306 ) {
efbd59642577 ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 505
diff changeset
307 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
308 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
309 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
310 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
311 } else {
efbd59642577 ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 505
diff changeset
312 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
313 }
efbd59642577 ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 505
diff changeset
314 }
efbd59642577 ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 505
diff changeset
315
efbd59642577 ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 505
diff changeset
316 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
317 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
318 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
319 ) {
efbd59642577 ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 505
diff changeset
320 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
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
efbd59642577 ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 505
diff changeset
323 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
324 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
325 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
326 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
327 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
328 ) {
505
481802342fdf ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 335
diff changeset
329 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
330 if (cx_szmul(size, nitems, &len)) {
505
481802342fdf ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 335
diff changeset
331 return 0;
481802342fdf ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 335
diff changeset
332 }
1
1bcaac272cdf added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
333 if (buffer->pos + len > buffer->size) {
1bcaac272cdf added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
334 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
335 if (size > 1) len -= len % size;
1
1bcaac272cdf added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
336 }
747
efbd59642577 ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 505
diff changeset
337
1
1bcaac272cdf added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
338 if (len <= 0) {
1bcaac272cdf added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
339 return len;
1bcaac272cdf added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
340 }
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
efbd59642577 ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 505
diff changeset
342 memcpy(ptr, buffer->bytes + buffer->pos, len);
1
1bcaac272cdf added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
343 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
344
1
1bcaac272cdf added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
345 return len / size;
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
747
efbd59642577 ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 505
diff changeset
348 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
349 if (cxBufferEof(buffer)) {
1
1bcaac272cdf added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
350 return EOF;
1bcaac272cdf added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
351 } 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
352 int c = buffer->bytes[buffer->pos];
1
1bcaac272cdf added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
353 buffer->pos++;
1bcaac272cdf added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
354 return c;
1bcaac272cdf added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
355 }
1bcaac272cdf added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
356 }
1bcaac272cdf added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
357
747
efbd59642577 ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 505
diff changeset
358 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
359 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
360 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
361 ) {
505
481802342fdf ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 335
diff changeset
362 if (shift >= buffer->size) {
481802342fdf ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 335
diff changeset
363 buffer->pos = buffer->size = 0;
481802342fdf ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 335
diff changeset
364 } 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
365 memmove(buffer->bytes, buffer->bytes + shift, buffer->size - shift);
505
481802342fdf ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 335
diff changeset
366 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
367
505
481802342fdf ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 335
diff changeset
368 if (buffer->pos >= shift) {
481802342fdf ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 335
diff changeset
369 buffer->pos -= shift;
481802342fdf ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 335
diff changeset
370 } else {
481802342fdf ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 335
diff changeset
371 buffer->pos = 0;
481802342fdf ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 335
diff changeset
372 }
481802342fdf ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 335
diff changeset
373 }
481802342fdf ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 335
diff changeset
374 return 0;
481802342fdf ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 335
diff changeset
375 }
481802342fdf ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 335
diff changeset
376
747
efbd59642577 ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 505
diff changeset
377 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
378 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
379 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
380 ) {
505
481802342fdf ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 335
diff changeset
381 size_t req_capacity = buffer->size + shift;
481802342fdf ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 335
diff changeset
382 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
383
505
481802342fdf ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 335
diff changeset
384 // auto extend buffer, if required and enabled
481802342fdf ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 335
diff changeset
385 if (buffer->capacity < req_capacity) {
747
efbd59642577 ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 505
diff changeset
386 if ((buffer->flags & CX_BUFFER_AUTO_EXTEND) == CX_BUFFER_AUTO_EXTEND) {
efbd59642577 ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 505
diff changeset
387 if (cxBufferMinimumCapacity(buffer, req_capacity)) {
505
481802342fdf ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 335
diff changeset
388 return 1;
481802342fdf ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 335
diff changeset
389 }
481802342fdf ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 335
diff changeset
390 movebytes = buffer->size;
481802342fdf ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 335
diff changeset
391 } else {
481802342fdf ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 335
diff changeset
392 movebytes = buffer->capacity - shift;
481802342fdf ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 335
diff changeset
393 }
481802342fdf ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 335
diff changeset
394 } else {
481802342fdf ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 335
diff changeset
395 movebytes = buffer->size;
481802342fdf ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 335
diff changeset
396 }
747
efbd59642577 ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 505
diff changeset
397
efbd59642577 ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 505
diff changeset
398 memmove(buffer->bytes + shift, buffer->bytes, movebytes);
efbd59642577 ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 505
diff changeset
399 buffer->size = shift + movebytes;
efbd59642577 ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 505
diff changeset
400
505
481802342fdf ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 335
diff changeset
401 buffer->pos += shift;
481802342fdf ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 335
diff changeset
402 if (buffer->pos > buffer->size) {
481802342fdf ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 335
diff changeset
403 buffer->pos = buffer->size;
481802342fdf ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 335
diff changeset
404 }
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
505
481802342fdf ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 335
diff changeset
406 return 0;
481802342fdf ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 335
diff changeset
407 }
481802342fdf ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 335
diff changeset
408
747
efbd59642577 ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 505
diff changeset
409 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
410 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
411 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
412 ) {
505
481802342fdf ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 335
diff changeset
413 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
414 return cxBufferShiftLeft(buffer, (size_t) (-shift));
505
481802342fdf ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 335
diff changeset
415 } 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
416 return cxBufferShiftRight(buffer, (size_t) shift);
505
481802342fdf ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 335
diff changeset
417 } else {
481802342fdf ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 335
diff changeset
418 return 0;
481802342fdf ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 335
diff changeset
419 }
481802342fdf ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 335
diff changeset
420 }

mercurial