annotate src/lib/buffer.c @ 832:e1ca4ef76ab4 HEAD

whops, reverted unwanted changes
author Timo Sirainen <tss@iki.fi>
date Sun, 22 Dec 2002 00:02:20 +0200
parents 0ae9b03a0f05
children e73389a2324a
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
765
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1 /*
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
2 Copyright (c) 2002 Timo Sirainen
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
3
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
4 Permission is hereby granted, free of charge, to any person obtaining
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
5 a copy of this software and associated documentation files (the
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
6 "Software"), to deal in the Software without restriction, including
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
7 without limitation the rights to use, copy, modify, merge, publish,
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
8 distribute, sublicense, and/or sell copies of the Software, and to
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
9 permit persons to whom the Software is furnished to do so, subject to
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
10 the following conditions:
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
11
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
12 The above copyright notice and this permission notice shall be
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
13 included in all copies or substantial portions of the Software.
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
14
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
15 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
16 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
17 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
18 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
19 CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
20 TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
21 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
22 */
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
23
805
5ac361acb316 Marked all non-trivial buffer modifications with @UNSAFE tag. Several
Timo Sirainen <tss@iki.fi>
parents: 765
diff changeset
24 /* @UNSAFE: whole file */
5ac361acb316 Marked all non-trivial buffer modifications with @UNSAFE tag. Several
Timo Sirainen <tss@iki.fi>
parents: 765
diff changeset
25
765
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
26 #include "lib.h"
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
27 #include "buffer.h"
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
28
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
29 struct _Buffer {
832
e1ca4ef76ab4 whops, reverted unwanted changes
Timo Sirainen <tss@iki.fi>
parents: 831
diff changeset
30 Pool pool;
831
0ae9b03a0f05 Last update broke used size.
Timo Sirainen <tss@iki.fi>
parents: 830
diff changeset
31
832
e1ca4ef76ab4 whops, reverted unwanted changes
Timo Sirainen <tss@iki.fi>
parents: 831
diff changeset
32 const unsigned char *r_buffer;
765
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
33 unsigned char *w_buffer;
832
e1ca4ef76ab4 whops, reverted unwanted changes
Timo Sirainen <tss@iki.fi>
parents: 831
diff changeset
34 size_t used, alloc, max_alloc, limit, start_pos;
765
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
35
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
36 unsigned int alloced:1;
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
37 unsigned int readonly:1;
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
38 unsigned int hard:1;
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
39 };
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
40
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
41 static void buffer_alloc(Buffer *buf, size_t min_size)
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
42 {
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
43 if (min_size == 0)
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
44 return;
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
45
830
27774d0493dd Fixed several bugs, added a small testsuite. Still requires careful auditing.
Timo Sirainen <tss@iki.fi>
parents: 805
diff changeset
46 i_assert(buf->w_buffer == NULL || buf->alloced);
27774d0493dd Fixed several bugs, added a small testsuite. Still requires careful auditing.
Timo Sirainen <tss@iki.fi>
parents: 805
diff changeset
47
765
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
48 buf->alloc = min_size;
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
49 if (buf->w_buffer == NULL)
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
50 buf->w_buffer = p_malloc(buf->pool, buf->alloc);
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
51 else
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
52 buf->w_buffer = p_realloc(buf->pool, buf->w_buffer, buf->alloc);
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
53 buf->r_buffer = buf->w_buffer;
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
54 buf->alloced = TRUE;
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
55 }
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
56
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
57 static int buffer_check_read(const Buffer *buf, size_t *pos, size_t *data_size)
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
58 {
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
59 size_t used_size, max_size;
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
60
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
61 used_size = I_MIN(buf->used, buf->limit);
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
62 if (*pos >= used_size - buf->start_pos)
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
63 return FALSE;
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
64
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
65 *pos += buf->start_pos;
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
66 max_size = used_size - *pos;
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
67 if (*data_size > max_size)
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
68 *data_size = max_size;
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
69 return TRUE;
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
70 }
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
71
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
72 static int buffer_check_write(Buffer *buf, size_t *pos, size_t *data_size,
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
73 int accept_partial)
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
74 {
831
0ae9b03a0f05 Last update broke used size.
Timo Sirainen <tss@iki.fi>
parents: 830
diff changeset
75 size_t max_size, new_size, alloc_size;
765
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
76
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
77 if (buf->readonly)
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
78 return FALSE;
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
79
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
80 /* check that we don't overflow size_t */
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
81 max_size = (size_t)-1 - *pos;
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
82 if (buf->start_pos >= max_size)
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
83 return FALSE;
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
84 *pos += buf->start_pos;
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
85
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
86 if (*data_size <= max_size)
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
87 new_size = *pos + *data_size;
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
88 else {
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
89 new_size = *pos + max_size;
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
90 if (new_size <= *pos || !accept_partial)
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
91 return FALSE;
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
92 *data_size = max_size;
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
93 }
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
94
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
95 /* see if we need to grow the buffer */
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
96 if (new_size > buf->alloc) {
831
0ae9b03a0f05 Last update broke used size.
Timo Sirainen <tss@iki.fi>
parents: 830
diff changeset
97 alloc_size = nearest_power(new_size);
0ae9b03a0f05 Last update broke used size.
Timo Sirainen <tss@iki.fi>
parents: 830
diff changeset
98 if (alloc_size > buf->limit) {
765
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
99 if (buf->hard) {
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
100 i_panic("Buffer full (%"PRIuSIZE_T" > "
831
0ae9b03a0f05 Last update broke used size.
Timo Sirainen <tss@iki.fi>
parents: 830
diff changeset
101 "%"PRIuSIZE_T")",
0ae9b03a0f05 Last update broke used size.
Timo Sirainen <tss@iki.fi>
parents: 830
diff changeset
102 alloc_size, buf->limit);
765
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
103 }
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
104
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
105 if (!accept_partial)
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
106 return FALSE;
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
107
831
0ae9b03a0f05 Last update broke used size.
Timo Sirainen <tss@iki.fi>
parents: 830
diff changeset
108 alloc_size = buf->limit;
0ae9b03a0f05 Last update broke used size.
Timo Sirainen <tss@iki.fi>
parents: 830
diff changeset
109 if (*pos >= alloc_size)
765
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
110 return FALSE;
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
111
831
0ae9b03a0f05 Last update broke used size.
Timo Sirainen <tss@iki.fi>
parents: 830
diff changeset
112 *data_size = alloc_size - *pos;
765
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
113 }
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
114
831
0ae9b03a0f05 Last update broke used size.
Timo Sirainen <tss@iki.fi>
parents: 830
diff changeset
115 if (new_size > alloc_size)
0ae9b03a0f05 Last update broke used size.
Timo Sirainen <tss@iki.fi>
parents: 830
diff changeset
116 new_size = alloc_size;
0ae9b03a0f05 Last update broke used size.
Timo Sirainen <tss@iki.fi>
parents: 830
diff changeset
117
0ae9b03a0f05 Last update broke used size.
Timo Sirainen <tss@iki.fi>
parents: 830
diff changeset
118 if (alloc_size != buf->alloc)
0ae9b03a0f05 Last update broke used size.
Timo Sirainen <tss@iki.fi>
parents: 830
diff changeset
119 buffer_alloc(buf, alloc_size);
765
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
120 }
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
121
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
122 if (new_size > buf->used)
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
123 buf->used = new_size;
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
124 return TRUE;
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
125 }
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
126
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
127 Buffer *buffer_create_static(Pool pool, size_t size)
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
128 {
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
129 Buffer *buf;
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
130
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
131 buf = p_new(pool, Buffer, 1);
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
132 buf->pool = pool;
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
133 buf->max_alloc = buf->limit = size;
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
134 buffer_alloc(buf, size);
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
135 return buf;
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
136 }
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
137
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
138 Buffer *buffer_create_static_hard(Pool pool, size_t size)
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
139 {
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
140 Buffer *buf;
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
141
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
142 buf = buffer_create_static(pool, size);
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
143 buf->hard = TRUE;
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
144 return buf;
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
145 }
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
146
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
147 Buffer *buffer_create_data(Pool pool, void *data, size_t size)
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
148 {
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
149 Buffer *buf;
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
150
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
151 buf = p_new(pool, Buffer, 1);
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
152 buf->pool = pool;
830
27774d0493dd Fixed several bugs, added a small testsuite. Still requires careful auditing.
Timo Sirainen <tss@iki.fi>
parents: 805
diff changeset
153 buf->alloc = buf->max_alloc = buf->limit = size;
765
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
154 buf->r_buffer = buf->w_buffer = data;
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
155 return buf;
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
156 }
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
157
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
158 Buffer *buffer_create_const_data(Pool pool, const void *data, size_t size)
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
159 {
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
160 Buffer *buf;
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
161
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
162 buf = p_new(pool, Buffer, 1);
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
163 buf->pool = pool;
830
27774d0493dd Fixed several bugs, added a small testsuite. Still requires careful auditing.
Timo Sirainen <tss@iki.fi>
parents: 805
diff changeset
164 buf->used = buf->alloc = buf->max_alloc = buf->limit = size;
765
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
165 buf->r_buffer = data;
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
166 buf->readonly = TRUE;
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
167 return buf;
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
168 }
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
169
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
170 Buffer *buffer_create_dynamic(Pool pool, size_t init_size, size_t max_size)
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
171 {
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
172 Buffer *buf;
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
173
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
174 buf = p_new(pool, Buffer, 1);
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
175 buf->pool = pool;
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
176 buf->max_alloc = buf->limit = max_size;
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
177 buffer_alloc(buf, init_size);
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
178 return buf;
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
179 }
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
180
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
181 void buffer_free(Buffer *buf)
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
182 {
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
183 if (buf->alloced)
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
184 p_free(buf->pool, buf->w_buffer);
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
185 p_free(buf->pool, buf);
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
186 }
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
187
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
188 void *buffer_free_without_data(Buffer *buf)
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
189 {
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
190 void *data;
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
191
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
192 data = buf->w_buffer;
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
193 p_free(buf->pool, buf);
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
194 return data;
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
195 }
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
196
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
197 size_t buffer_write(Buffer *buf, size_t pos,
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
198 const void *data, size_t data_size)
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
199 {
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
200 if (!buffer_check_write(buf, &pos, &data_size, TRUE))
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
201 return 0;
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
202
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
203 memcpy(buf->w_buffer + pos, data, data_size);
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
204 return data_size;
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
205 }
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
206
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
207 size_t buffer_append(Buffer *buf, const void *data, size_t data_size)
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
208 {
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
209 return buffer_write(buf, buf->used - buf->start_pos, data, data_size);
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
210 }
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
211
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
212 size_t buffer_append_c(Buffer *buf, char chr)
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
213 {
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
214 size_t pos, data_size = 1;
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
215
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
216 pos = buf->used - buf->start_pos;
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
217 if (!buffer_check_write(buf, &pos, &data_size, TRUE))
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
218 return 0;
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
219
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
220 if (data_size == 1)
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
221 buf->w_buffer[pos] = chr;
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
222 return data_size;
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
223 }
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
224
805
5ac361acb316 Marked all non-trivial buffer modifications with @UNSAFE tag. Several
Timo Sirainen <tss@iki.fi>
parents: 765
diff changeset
225 size_t buffer_insert(Buffer *buf, size_t pos,
5ac361acb316 Marked all non-trivial buffer modifications with @UNSAFE tag. Several
Timo Sirainen <tss@iki.fi>
parents: 765
diff changeset
226 const void *data, size_t data_size)
5ac361acb316 Marked all non-trivial buffer modifications with @UNSAFE tag. Several
Timo Sirainen <tss@iki.fi>
parents: 765
diff changeset
227 {
5ac361acb316 Marked all non-trivial buffer modifications with @UNSAFE tag. Several
Timo Sirainen <tss@iki.fi>
parents: 765
diff changeset
228 size_t move_size, size;
5ac361acb316 Marked all non-trivial buffer modifications with @UNSAFE tag. Several
Timo Sirainen <tss@iki.fi>
parents: 765
diff changeset
229
5ac361acb316 Marked all non-trivial buffer modifications with @UNSAFE tag. Several
Timo Sirainen <tss@iki.fi>
parents: 765
diff changeset
230 move_size = buf->used - buf->start_pos;
5ac361acb316 Marked all non-trivial buffer modifications with @UNSAFE tag. Several
Timo Sirainen <tss@iki.fi>
parents: 765
diff changeset
231 i_assert(pos <= move_size);
5ac361acb316 Marked all non-trivial buffer modifications with @UNSAFE tag. Several
Timo Sirainen <tss@iki.fi>
parents: 765
diff changeset
232 move_size -= pos;
5ac361acb316 Marked all non-trivial buffer modifications with @UNSAFE tag. Several
Timo Sirainen <tss@iki.fi>
parents: 765
diff changeset
233
5ac361acb316 Marked all non-trivial buffer modifications with @UNSAFE tag. Several
Timo Sirainen <tss@iki.fi>
parents: 765
diff changeset
234 if (data_size < (size_t)-1 - move_size)
5ac361acb316 Marked all non-trivial buffer modifications with @UNSAFE tag. Several
Timo Sirainen <tss@iki.fi>
parents: 765
diff changeset
235 size = data_size + move_size;
5ac361acb316 Marked all non-trivial buffer modifications with @UNSAFE tag. Several
Timo Sirainen <tss@iki.fi>
parents: 765
diff changeset
236 else
5ac361acb316 Marked all non-trivial buffer modifications with @UNSAFE tag. Several
Timo Sirainen <tss@iki.fi>
parents: 765
diff changeset
237 size = (size_t)-1;
5ac361acb316 Marked all non-trivial buffer modifications with @UNSAFE tag. Several
Timo Sirainen <tss@iki.fi>
parents: 765
diff changeset
238
5ac361acb316 Marked all non-trivial buffer modifications with @UNSAFE tag. Several
Timo Sirainen <tss@iki.fi>
parents: 765
diff changeset
239 if (!buffer_check_write(buf, &pos, &size, TRUE))
5ac361acb316 Marked all non-trivial buffer modifications with @UNSAFE tag. Several
Timo Sirainen <tss@iki.fi>
parents: 765
diff changeset
240 return 0;
5ac361acb316 Marked all non-trivial buffer modifications with @UNSAFE tag. Several
Timo Sirainen <tss@iki.fi>
parents: 765
diff changeset
241
5ac361acb316 Marked all non-trivial buffer modifications with @UNSAFE tag. Several
Timo Sirainen <tss@iki.fi>
parents: 765
diff changeset
242 i_assert(size >= move_size);
5ac361acb316 Marked all non-trivial buffer modifications with @UNSAFE tag. Several
Timo Sirainen <tss@iki.fi>
parents: 765
diff changeset
243 size -= move_size;
5ac361acb316 Marked all non-trivial buffer modifications with @UNSAFE tag. Several
Timo Sirainen <tss@iki.fi>
parents: 765
diff changeset
244
5ac361acb316 Marked all non-trivial buffer modifications with @UNSAFE tag. Several
Timo Sirainen <tss@iki.fi>
parents: 765
diff changeset
245 memmove(buf->w_buffer + pos + size, buf->w_buffer + pos, move_size);
5ac361acb316 Marked all non-trivial buffer modifications with @UNSAFE tag. Several
Timo Sirainen <tss@iki.fi>
parents: 765
diff changeset
246 memcpy(buf->w_buffer + pos, data, size);
5ac361acb316 Marked all non-trivial buffer modifications with @UNSAFE tag. Several
Timo Sirainen <tss@iki.fi>
parents: 765
diff changeset
247 return size;
5ac361acb316 Marked all non-trivial buffer modifications with @UNSAFE tag. Several
Timo Sirainen <tss@iki.fi>
parents: 765
diff changeset
248 }
5ac361acb316 Marked all non-trivial buffer modifications with @UNSAFE tag. Several
Timo Sirainen <tss@iki.fi>
parents: 765
diff changeset
249
5ac361acb316 Marked all non-trivial buffer modifications with @UNSAFE tag. Several
Timo Sirainen <tss@iki.fi>
parents: 765
diff changeset
250 size_t buffer_delete(Buffer *buf, size_t pos, size_t size)
5ac361acb316 Marked all non-trivial buffer modifications with @UNSAFE tag. Several
Timo Sirainen <tss@iki.fi>
parents: 765
diff changeset
251 {
5ac361acb316 Marked all non-trivial buffer modifications with @UNSAFE tag. Several
Timo Sirainen <tss@iki.fi>
parents: 765
diff changeset
252 size_t end_size;
5ac361acb316 Marked all non-trivial buffer modifications with @UNSAFE tag. Several
Timo Sirainen <tss@iki.fi>
parents: 765
diff changeset
253
5ac361acb316 Marked all non-trivial buffer modifications with @UNSAFE tag. Several
Timo Sirainen <tss@iki.fi>
parents: 765
diff changeset
254 if (buf->readonly)
5ac361acb316 Marked all non-trivial buffer modifications with @UNSAFE tag. Several
Timo Sirainen <tss@iki.fi>
parents: 765
diff changeset
255 return 0;
5ac361acb316 Marked all non-trivial buffer modifications with @UNSAFE tag. Several
Timo Sirainen <tss@iki.fi>
parents: 765
diff changeset
256
5ac361acb316 Marked all non-trivial buffer modifications with @UNSAFE tag. Several
Timo Sirainen <tss@iki.fi>
parents: 765
diff changeset
257 end_size = buf->used - buf->start_pos;
5ac361acb316 Marked all non-trivial buffer modifications with @UNSAFE tag. Several
Timo Sirainen <tss@iki.fi>
parents: 765
diff changeset
258 i_assert(pos <= end_size);
5ac361acb316 Marked all non-trivial buffer modifications with @UNSAFE tag. Several
Timo Sirainen <tss@iki.fi>
parents: 765
diff changeset
259 end_size -= pos;
5ac361acb316 Marked all non-trivial buffer modifications with @UNSAFE tag. Several
Timo Sirainen <tss@iki.fi>
parents: 765
diff changeset
260
5ac361acb316 Marked all non-trivial buffer modifications with @UNSAFE tag. Several
Timo Sirainen <tss@iki.fi>
parents: 765
diff changeset
261 if (size < end_size) {
5ac361acb316 Marked all non-trivial buffer modifications with @UNSAFE tag. Several
Timo Sirainen <tss@iki.fi>
parents: 765
diff changeset
262 /* delete from between */
830
27774d0493dd Fixed several bugs, added a small testsuite. Still requires careful auditing.
Timo Sirainen <tss@iki.fi>
parents: 805
diff changeset
263 end_size -= size;
805
5ac361acb316 Marked all non-trivial buffer modifications with @UNSAFE tag. Several
Timo Sirainen <tss@iki.fi>
parents: 765
diff changeset
264 memmove(buf->w_buffer + buf->start_pos + pos,
830
27774d0493dd Fixed several bugs, added a small testsuite. Still requires careful auditing.
Timo Sirainen <tss@iki.fi>
parents: 805
diff changeset
265 buf->w_buffer + buf->start_pos + pos + size, end_size);
805
5ac361acb316 Marked all non-trivial buffer modifications with @UNSAFE tag. Several
Timo Sirainen <tss@iki.fi>
parents: 765
diff changeset
266 } else {
5ac361acb316 Marked all non-trivial buffer modifications with @UNSAFE tag. Several
Timo Sirainen <tss@iki.fi>
parents: 765
diff changeset
267 /* delete the rest of the buffer */
5ac361acb316 Marked all non-trivial buffer modifications with @UNSAFE tag. Several
Timo Sirainen <tss@iki.fi>
parents: 765
diff changeset
268 size = end_size;
5ac361acb316 Marked all non-trivial buffer modifications with @UNSAFE tag. Several
Timo Sirainen <tss@iki.fi>
parents: 765
diff changeset
269 end_size = 0;
5ac361acb316 Marked all non-trivial buffer modifications with @UNSAFE tag. Several
Timo Sirainen <tss@iki.fi>
parents: 765
diff changeset
270 }
5ac361acb316 Marked all non-trivial buffer modifications with @UNSAFE tag. Several
Timo Sirainen <tss@iki.fi>
parents: 765
diff changeset
271
5ac361acb316 Marked all non-trivial buffer modifications with @UNSAFE tag. Several
Timo Sirainen <tss@iki.fi>
parents: 765
diff changeset
272 buffer_set_used_size(buf, pos + end_size);
5ac361acb316 Marked all non-trivial buffer modifications with @UNSAFE tag. Several
Timo Sirainen <tss@iki.fi>
parents: 765
diff changeset
273 return size;
5ac361acb316 Marked all non-trivial buffer modifications with @UNSAFE tag. Several
Timo Sirainen <tss@iki.fi>
parents: 765
diff changeset
274 }
5ac361acb316 Marked all non-trivial buffer modifications with @UNSAFE tag. Several
Timo Sirainen <tss@iki.fi>
parents: 765
diff changeset
275
765
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
276 size_t buffer_copy(Buffer *dest, size_t dest_pos,
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
277 const Buffer *src, size_t src_pos, size_t copy_size)
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
278 {
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
279 if (!buffer_check_read(src, &src_pos, &copy_size))
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
280 return 0;
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
281
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
282 if (!buffer_check_write(dest, &dest_pos, &copy_size, TRUE))
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
283 return 0;
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
284
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
285 if (src == dest) {
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
286 memmove(dest->w_buffer + dest_pos,
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
287 src->r_buffer + src_pos, copy_size);
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
288 } else {
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
289 memcpy(dest->w_buffer + dest_pos,
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
290 src->r_buffer + src_pos, copy_size);
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
291 }
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
292 return copy_size;
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
293 }
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
294
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
295 size_t buffer_append_buf(Buffer *dest, const Buffer *src,
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
296 size_t src_pos, size_t copy_size)
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
297 {
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
298 return buffer_copy(dest, dest->used - dest->start_pos,
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
299 src, src_pos, copy_size);
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
300 }
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
301
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
302 void *buffer_get_space(Buffer *buf, size_t pos, size_t size)
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
303 {
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
304 if (!buffer_check_write(buf, &pos, &size, FALSE))
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
305 return 0;
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
306
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
307 return buf->w_buffer + pos;
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
308 }
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
309
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
310 void *buffer_append_space(Buffer *buf, size_t size)
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
311 {
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
312 return buffer_get_space(buf, buf->used - buf->start_pos, size);
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
313 }
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
314
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
315 const void *buffer_get_data(const Buffer *buf, size_t *used_size)
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
316 {
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
317 if (used_size != NULL)
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
318 *used_size = I_MIN(buf->used, buf->limit) - buf->start_pos;
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
319 return buf->r_buffer + buf->start_pos;
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
320 }
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
321
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
322 void *buffer_get_modifyable_data(const Buffer *buf, size_t *used_size)
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
323 {
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
324 if (used_size != NULL)
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
325 *used_size = I_MIN(buf->used, buf->limit) - buf->start_pos;
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
326 return buf->w_buffer + buf->start_pos;
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
327 }
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
328
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
329 void buffer_set_used_size(Buffer *buf, size_t used_size)
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
330 {
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
331 i_assert(used_size <= I_MIN(buf->alloc, buf->limit) - buf->start_pos);
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
332
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
333 buf->used = used_size + buf->start_pos;
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
334 }
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
335
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
336 size_t buffer_get_used_size(const Buffer *buf)
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
337 {
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
338 return I_MIN(buf->used, buf->limit) - buf->start_pos;
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
339 }
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
340
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
341 size_t buffer_set_start_pos(Buffer *buf, size_t abs_pos)
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
342 {
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
343 size_t old = buf->start_pos;
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
344
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
345 i_assert(abs_pos <= I_MIN(buf->used, buf->limit));
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
346
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
347 buf->start_pos = abs_pos;
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
348 return old;
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
349 }
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
350
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
351 size_t buffer_get_start_pos(const Buffer *buf)
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
352 {
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
353 return buf->start_pos;
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
354 }
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
355
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
356 size_t buffer_set_limit(Buffer *buf, size_t limit)
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
357 {
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
358 size_t old = buf->limit;
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
359
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
360 if (limit > (size_t)-1 - buf->start_pos)
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
361 limit = (size_t)-1;
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
362 else
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
363 limit += buf->start_pos;
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
364
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
365 buf->limit = I_MIN(limit, buf->max_alloc);
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
366 return old;
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
367 }
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
368
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
369 size_t buffer_get_limit(const Buffer *buf)
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
370 {
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
371 return buf->limit - buf->start_pos;
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
372 }
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
373
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
374 size_t buffer_get_size(const Buffer *buf)
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
375 {
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
376 return buf->alloc - buf->start_pos;
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
377 }
830
27774d0493dd Fixed several bugs, added a small testsuite. Still requires careful auditing.
Timo Sirainen <tss@iki.fi>
parents: 805
diff changeset
378
27774d0493dd Fixed several bugs, added a small testsuite. Still requires careful auditing.
Timo Sirainen <tss@iki.fi>
parents: 805
diff changeset
379 #ifdef BUFFER_TEST
27774d0493dd Fixed several bugs, added a small testsuite. Still requires careful auditing.
Timo Sirainen <tss@iki.fi>
parents: 805
diff changeset
380 /* gcc buffer.c -o buffer liblib.a -Wall -DHAVE_CONFIG_H -DBUFFER_TEST -g */
27774d0493dd Fixed several bugs, added a small testsuite. Still requires careful auditing.
Timo Sirainen <tss@iki.fi>
parents: 805
diff changeset
381 int main(void)
27774d0493dd Fixed several bugs, added a small testsuite. Still requires careful auditing.
Timo Sirainen <tss@iki.fi>
parents: 805
diff changeset
382 {
27774d0493dd Fixed several bugs, added a small testsuite. Still requires careful auditing.
Timo Sirainen <tss@iki.fi>
parents: 805
diff changeset
383 Buffer *buf;
27774d0493dd Fixed several bugs, added a small testsuite. Still requires careful auditing.
Timo Sirainen <tss@iki.fi>
parents: 805
diff changeset
384 char data[12], *bufdata;
27774d0493dd Fixed several bugs, added a small testsuite. Still requires careful auditing.
Timo Sirainen <tss@iki.fi>
parents: 805
diff changeset
385 size_t bufsize;
27774d0493dd Fixed several bugs, added a small testsuite. Still requires careful auditing.
Timo Sirainen <tss@iki.fi>
parents: 805
diff changeset
386
27774d0493dd Fixed several bugs, added a small testsuite. Still requires careful auditing.
Timo Sirainen <tss@iki.fi>
parents: 805
diff changeset
387 memset(data, '!', sizeof(data));
27774d0493dd Fixed several bugs, added a small testsuite. Still requires careful auditing.
Timo Sirainen <tss@iki.fi>
parents: 805
diff changeset
388 bufdata = data + 1;
27774d0493dd Fixed several bugs, added a small testsuite. Still requires careful auditing.
Timo Sirainen <tss@iki.fi>
parents: 805
diff changeset
389 bufsize = sizeof(data)-2;
27774d0493dd Fixed several bugs, added a small testsuite. Still requires careful auditing.
Timo Sirainen <tss@iki.fi>
parents: 805
diff changeset
390
27774d0493dd Fixed several bugs, added a small testsuite. Still requires careful auditing.
Timo Sirainen <tss@iki.fi>
parents: 805
diff changeset
391 buf = buffer_create_data(system_pool, bufdata, bufsize);
27774d0493dd Fixed several bugs, added a small testsuite. Still requires careful auditing.
Timo Sirainen <tss@iki.fi>
parents: 805
diff changeset
392 i_assert(buffer_write(buf, 5, "12345", 5) == 5);
27774d0493dd Fixed several bugs, added a small testsuite. Still requires careful auditing.
Timo Sirainen <tss@iki.fi>
parents: 805
diff changeset
393 i_assert(buf->used == 10);
27774d0493dd Fixed several bugs, added a small testsuite. Still requires careful auditing.
Timo Sirainen <tss@iki.fi>
parents: 805
diff changeset
394 i_assert(buffer_write(buf, 6, "12345", 5) == 4);
27774d0493dd Fixed several bugs, added a small testsuite. Still requires careful auditing.
Timo Sirainen <tss@iki.fi>
parents: 805
diff changeset
395 i_assert(buf->used == 10);
27774d0493dd Fixed several bugs, added a small testsuite. Still requires careful auditing.
Timo Sirainen <tss@iki.fi>
parents: 805
diff changeset
396
27774d0493dd Fixed several bugs, added a small testsuite. Still requires careful auditing.
Timo Sirainen <tss@iki.fi>
parents: 805
diff changeset
397 buf = buffer_create_data(system_pool, bufdata, bufsize);
27774d0493dd Fixed several bugs, added a small testsuite. Still requires careful auditing.
Timo Sirainen <tss@iki.fi>
parents: 805
diff changeset
398 i_assert(buffer_write(buf, 0, "1234567890", 10) == 10);
27774d0493dd Fixed several bugs, added a small testsuite. Still requires careful auditing.
Timo Sirainen <tss@iki.fi>
parents: 805
diff changeset
399 i_assert(buffer_write(buf, 0, "12345678901", 11) == 10);
27774d0493dd Fixed several bugs, added a small testsuite. Still requires careful auditing.
Timo Sirainen <tss@iki.fi>
parents: 805
diff changeset
400 i_assert(buffer_append(buf, "1", 1) == 0);
27774d0493dd Fixed several bugs, added a small testsuite. Still requires careful auditing.
Timo Sirainen <tss@iki.fi>
parents: 805
diff changeset
401 i_assert(buf->used == 10);
27774d0493dd Fixed several bugs, added a small testsuite. Still requires careful auditing.
Timo Sirainen <tss@iki.fi>
parents: 805
diff changeset
402
27774d0493dd Fixed several bugs, added a small testsuite. Still requires careful auditing.
Timo Sirainen <tss@iki.fi>
parents: 805
diff changeset
403 buf = buffer_create_data(system_pool, bufdata, bufsize);
27774d0493dd Fixed several bugs, added a small testsuite. Still requires careful auditing.
Timo Sirainen <tss@iki.fi>
parents: 805
diff changeset
404 i_assert(buffer_append(buf, "12345", 5) == 5);
27774d0493dd Fixed several bugs, added a small testsuite. Still requires careful auditing.
Timo Sirainen <tss@iki.fi>
parents: 805
diff changeset
405 i_assert(buf->used == 5);
27774d0493dd Fixed several bugs, added a small testsuite. Still requires careful auditing.
Timo Sirainen <tss@iki.fi>
parents: 805
diff changeset
406 i_assert(buffer_append(buf, "123456", 6) == 5);
27774d0493dd Fixed several bugs, added a small testsuite. Still requires careful auditing.
Timo Sirainen <tss@iki.fi>
parents: 805
diff changeset
407 i_assert(buf->used == 10);
27774d0493dd Fixed several bugs, added a small testsuite. Still requires careful auditing.
Timo Sirainen <tss@iki.fi>
parents: 805
diff changeset
408
27774d0493dd Fixed several bugs, added a small testsuite. Still requires careful auditing.
Timo Sirainen <tss@iki.fi>
parents: 805
diff changeset
409 buf = buffer_create_data(system_pool, bufdata, bufsize);
27774d0493dd Fixed several bugs, added a small testsuite. Still requires careful auditing.
Timo Sirainen <tss@iki.fi>
parents: 805
diff changeset
410 i_assert(buffer_append(buf, "12345", 5) == 5);
27774d0493dd Fixed several bugs, added a small testsuite. Still requires careful auditing.
Timo Sirainen <tss@iki.fi>
parents: 805
diff changeset
411 i_assert(buffer_insert(buf, 2, "123456", 6) == 5);
27774d0493dd Fixed several bugs, added a small testsuite. Still requires careful auditing.
Timo Sirainen <tss@iki.fi>
parents: 805
diff changeset
412 i_assert(buf->used == 10);
27774d0493dd Fixed several bugs, added a small testsuite. Still requires careful auditing.
Timo Sirainen <tss@iki.fi>
parents: 805
diff changeset
413 i_assert(memcmp(buf->r_buffer, "1212345345", 10) == 0);
27774d0493dd Fixed several bugs, added a small testsuite. Still requires careful auditing.
Timo Sirainen <tss@iki.fi>
parents: 805
diff changeset
414 i_assert(buffer_delete(buf, 2, 5) == 5);
27774d0493dd Fixed several bugs, added a small testsuite. Still requires careful auditing.
Timo Sirainen <tss@iki.fi>
parents: 805
diff changeset
415 i_assert(buf->used == 5);
27774d0493dd Fixed several bugs, added a small testsuite. Still requires careful auditing.
Timo Sirainen <tss@iki.fi>
parents: 805
diff changeset
416 i_assert(memcmp(buf->r_buffer, "12345", 5) == 0);
27774d0493dd Fixed several bugs, added a small testsuite. Still requires careful auditing.
Timo Sirainen <tss@iki.fi>
parents: 805
diff changeset
417 i_assert(buffer_delete(buf, 3, 5) == 2);
27774d0493dd Fixed several bugs, added a small testsuite. Still requires careful auditing.
Timo Sirainen <tss@iki.fi>
parents: 805
diff changeset
418 i_assert(buf->used == 3);
27774d0493dd Fixed several bugs, added a small testsuite. Still requires careful auditing.
Timo Sirainen <tss@iki.fi>
parents: 805
diff changeset
419 i_assert(memcmp(buf->r_buffer, "123", 3) == 0);
27774d0493dd Fixed several bugs, added a small testsuite. Still requires careful auditing.
Timo Sirainen <tss@iki.fi>
parents: 805
diff changeset
420
27774d0493dd Fixed several bugs, added a small testsuite. Still requires careful auditing.
Timo Sirainen <tss@iki.fi>
parents: 805
diff changeset
421 i_assert(data[0] == '!');
27774d0493dd Fixed several bugs, added a small testsuite. Still requires careful auditing.
Timo Sirainen <tss@iki.fi>
parents: 805
diff changeset
422 i_assert(data[sizeof(data)-1] == '!');
27774d0493dd Fixed several bugs, added a small testsuite. Still requires careful auditing.
Timo Sirainen <tss@iki.fi>
parents: 805
diff changeset
423 return 0;
27774d0493dd Fixed several bugs, added a small testsuite. Still requires careful auditing.
Timo Sirainen <tss@iki.fi>
parents: 805
diff changeset
424 }
27774d0493dd Fixed several bugs, added a small testsuite. Still requires careful auditing.
Timo Sirainen <tss@iki.fi>
parents: 805
diff changeset
425 #endif