annotate src/lib/buffer.c @ 903:fd8888f6f037 HEAD

Naming style changes, finally got tired of most of the typedefs. Also the previous enum -> macro change reverted so that we don't use the highest bit anymore, that's incompatible with old indexes so they will be rebuilt.
author Timo Sirainen <tss@iki.fi>
date Sun, 05 Jan 2003 15:09:51 +0200
parents e73389a2324a
children 4d6b69558add
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
903
fd8888f6f037 Naming style changes, finally got tired of most of the typedefs. Also the
Timo Sirainen <tss@iki.fi>
parents: 851
diff changeset
29 struct buffer {
fd8888f6f037 Naming style changes, finally got tired of most of the typedefs. Also the
Timo Sirainen <tss@iki.fi>
parents: 851
diff changeset
30 pool_t 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;
851
e73389a2324a Several fixes. I think it's beginning to look safe.
Timo Sirainen <tss@iki.fi>
parents: 832
diff changeset
34
e73389a2324a Several fixes. I think it's beginning to look safe.
Timo Sirainen <tss@iki.fi>
parents: 832
diff changeset
35 /* buffer_set_start_pos() modifies start_pos, but internally we deal
e73389a2324a Several fixes. I think it's beginning to look safe.
Timo Sirainen <tss@iki.fi>
parents: 832
diff changeset
36 only with absolute positions. buffer_check_read|write modifies
e73389a2324a Several fixes. I think it's beginning to look safe.
Timo Sirainen <tss@iki.fi>
parents: 832
diff changeset
37 given position to absolute one.
e73389a2324a Several fixes. I think it's beginning to look safe.
Timo Sirainen <tss@iki.fi>
parents: 832
diff changeset
38
e73389a2324a Several fixes. I think it's beginning to look safe.
Timo Sirainen <tss@iki.fi>
parents: 832
diff changeset
39 start_pos <= used <= alloc <= max_alloc.
e73389a2324a Several fixes. I think it's beginning to look safe.
Timo Sirainen <tss@iki.fi>
parents: 832
diff changeset
40 start_pos <= limit <= max_alloc */
e73389a2324a Several fixes. I think it's beginning to look safe.
Timo Sirainen <tss@iki.fi>
parents: 832
diff changeset
41 size_t start_pos, used, alloc, max_alloc, 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
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 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
44 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
45 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
46 };
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
47
903
fd8888f6f037 Naming style changes, finally got tired of most of the typedefs. Also the
Timo Sirainen <tss@iki.fi>
parents: 851
diff changeset
48 static void buffer_alloc(buffer_t *buf, size_t 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
49 {
851
e73389a2324a Several fixes. I think it's beginning to look safe.
Timo Sirainen <tss@iki.fi>
parents: 832
diff changeset
50 i_assert(buf->w_buffer == NULL || buf->alloced);
e73389a2324a Several fixes. I think it's beginning to look safe.
Timo Sirainen <tss@iki.fi>
parents: 832
diff changeset
51
e73389a2324a Several fixes. I think it's beginning to look safe.
Timo Sirainen <tss@iki.fi>
parents: 832
diff changeset
52 if (size == buf->alloc)
765
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
53 return;
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
54
851
e73389a2324a Several fixes. I think it's beginning to look safe.
Timo Sirainen <tss@iki.fi>
parents: 832
diff changeset
55 i_assert(size > buf->alloc);
830
27774d0493dd Fixed several bugs, added a small testsuite. Still requires careful auditing.
Timo Sirainen <tss@iki.fi>
parents: 805
diff changeset
56
851
e73389a2324a Several fixes. I think it's beginning to look safe.
Timo Sirainen <tss@iki.fi>
parents: 832
diff changeset
57 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
58 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
59 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
60 else
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
61 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
62 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
63 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
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
903
fd8888f6f037 Naming style changes, finally got tired of most of the typedefs. Also the
Timo Sirainen <tss@iki.fi>
parents: 851
diff changeset
66 static int buffer_check_read(const buffer_t *buf,
fd8888f6f037 Naming style changes, finally got tired of most of the typedefs. Also the
Timo Sirainen <tss@iki.fi>
parents: 851
diff changeset
67 size_t *pos, size_t *data_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
68 {
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
69 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
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 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
72 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
73 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
74
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
75 *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
76 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
77 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
78 *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
79 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
80 }
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
81
903
fd8888f6f037 Naming style changes, finally got tired of most of the typedefs. Also the
Timo Sirainen <tss@iki.fi>
parents: 851
diff changeset
82 static int buffer_check_write(buffer_t *buf, size_t *pos,
fd8888f6f037 Naming style changes, finally got tired of most of the typedefs. Also the
Timo Sirainen <tss@iki.fi>
parents: 851
diff changeset
83 size_t *data_size, int accept_partial)
765
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
84 {
831
0ae9b03a0f05 Last update broke used size.
Timo Sirainen <tss@iki.fi>
parents: 830
diff changeset
85 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
86
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
87 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
88 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
89
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
90 /* check that we don't overflow size_t */
851
e73389a2324a Several fixes. I think it's beginning to look safe.
Timo Sirainen <tss@iki.fi>
parents: 832
diff changeset
91 if (*pos >= (size_t)-1 - buf->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
92 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
93 *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
94
851
e73389a2324a Several fixes. I think it's beginning to look safe.
Timo Sirainen <tss@iki.fi>
parents: 832
diff changeset
95 max_size = (size_t)-1 - *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
96 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
97 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
98 else {
851
e73389a2324a Several fixes. I think it's beginning to look safe.
Timo Sirainen <tss@iki.fi>
parents: 832
diff changeset
99 if (max_size == 0 || !accept_partial)
e73389a2324a Several fixes. I think it's beginning to look safe.
Timo Sirainen <tss@iki.fi>
parents: 832
diff changeset
100 return FALSE;
e73389a2324a Several fixes. I think it's beginning to look safe.
Timo Sirainen <tss@iki.fi>
parents: 832
diff changeset
101
765
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
102 new_size = *pos + max_size;
851
e73389a2324a Several fixes. I think it's beginning to look safe.
Timo Sirainen <tss@iki.fi>
parents: 832
diff changeset
103 *data_size = max_size;
e73389a2324a Several fixes. I think it's beginning to look safe.
Timo Sirainen <tss@iki.fi>
parents: 832
diff changeset
104 }
e73389a2324a Several fixes. I think it's beginning to look safe.
Timo Sirainen <tss@iki.fi>
parents: 832
diff changeset
105
e73389a2324a Several fixes. I think it's beginning to look safe.
Timo Sirainen <tss@iki.fi>
parents: 832
diff changeset
106 /* make sure we're within our limits */
e73389a2324a Several fixes. I think it's beginning to look safe.
Timo Sirainen <tss@iki.fi>
parents: 832
diff changeset
107 if (new_size > buf->limit) {
e73389a2324a Several fixes. I think it's beginning to look safe.
Timo Sirainen <tss@iki.fi>
parents: 832
diff changeset
108 if (buf->hard) {
e73389a2324a Several fixes. I think it's beginning to look safe.
Timo Sirainen <tss@iki.fi>
parents: 832
diff changeset
109 i_panic("Buffer full (%"PRIuSIZE_T" > "
e73389a2324a Several fixes. I think it's beginning to look safe.
Timo Sirainen <tss@iki.fi>
parents: 832
diff changeset
110 "%"PRIuSIZE_T")",
e73389a2324a Several fixes. I think it's beginning to look safe.
Timo Sirainen <tss@iki.fi>
parents: 832
diff changeset
111 new_size, buf->limit);
e73389a2324a Several fixes. I think it's beginning to look safe.
Timo Sirainen <tss@iki.fi>
parents: 832
diff changeset
112 }
e73389a2324a Several fixes. I think it's beginning to look safe.
Timo Sirainen <tss@iki.fi>
parents: 832
diff changeset
113
e73389a2324a Several fixes. I think it's beginning to look safe.
Timo Sirainen <tss@iki.fi>
parents: 832
diff changeset
114 if (!accept_partial || *pos >= 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
115 return FALSE;
851
e73389a2324a Several fixes. I think it's beginning to look safe.
Timo Sirainen <tss@iki.fi>
parents: 832
diff changeset
116
e73389a2324a Several fixes. I think it's beginning to look safe.
Timo Sirainen <tss@iki.fi>
parents: 832
diff changeset
117 new_size = buf->limit;
e73389a2324a Several fixes. I think it's beginning to look safe.
Timo Sirainen <tss@iki.fi>
parents: 832
diff changeset
118 *data_size = new_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
119 }
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 /* 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
122 if (new_size > buf->alloc) {
831
0ae9b03a0f05 Last update broke used size.
Timo Sirainen <tss@iki.fi>
parents: 830
diff changeset
123 alloc_size = nearest_power(new_size);
851
e73389a2324a Several fixes. I think it's beginning to look safe.
Timo Sirainen <tss@iki.fi>
parents: 832
diff changeset
124 if (alloc_size > buf->limit)
831
0ae9b03a0f05 Last update broke used size.
Timo Sirainen <tss@iki.fi>
parents: 830
diff changeset
125 alloc_size = buf->limit;
0ae9b03a0f05 Last update broke used size.
Timo Sirainen <tss@iki.fi>
parents: 830
diff changeset
126
0ae9b03a0f05 Last update broke used size.
Timo Sirainen <tss@iki.fi>
parents: 830
diff changeset
127 if (alloc_size != buf->alloc)
0ae9b03a0f05 Last update broke used size.
Timo Sirainen <tss@iki.fi>
parents: 830
diff changeset
128 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
129 }
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 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
132 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
133 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
134 }
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
135
903
fd8888f6f037 Naming style changes, finally got tired of most of the typedefs. Also the
Timo Sirainen <tss@iki.fi>
parents: 851
diff changeset
136 buffer_t *buffer_create_static(pool_t pool, size_t 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
137 {
903
fd8888f6f037 Naming style changes, finally got tired of most of the typedefs. Also the
Timo Sirainen <tss@iki.fi>
parents: 851
diff changeset
138 buffer_t *buf;
765
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
139
903
fd8888f6f037 Naming style changes, finally got tired of most of the typedefs. Also the
Timo Sirainen <tss@iki.fi>
parents: 851
diff changeset
140 buf = p_new(pool, buffer_t, 1);
765
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
141 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
142 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
143 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
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
903
fd8888f6f037 Naming style changes, finally got tired of most of the typedefs. Also the
Timo Sirainen <tss@iki.fi>
parents: 851
diff changeset
147 buffer_t *buffer_create_static_hard(pool_t pool, size_t 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
148 {
903
fd8888f6f037 Naming style changes, finally got tired of most of the typedefs. Also the
Timo Sirainen <tss@iki.fi>
parents: 851
diff changeset
149 buffer_t *buf;
765
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 = 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
152 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
153 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
154 }
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
155
903
fd8888f6f037 Naming style changes, finally got tired of most of the typedefs. Also the
Timo Sirainen <tss@iki.fi>
parents: 851
diff changeset
156 buffer_t *buffer_create_data(pool_t pool, void *data, size_t 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
157 {
903
fd8888f6f037 Naming style changes, finally got tired of most of the typedefs. Also the
Timo Sirainen <tss@iki.fi>
parents: 851
diff changeset
158 buffer_t *buf;
765
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
159
903
fd8888f6f037 Naming style changes, finally got tired of most of the typedefs. Also the
Timo Sirainen <tss@iki.fi>
parents: 851
diff changeset
160 buf = p_new(pool, buffer_t, 1);
765
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
161 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
162 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
163 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
164 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
165 }
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
166
903
fd8888f6f037 Naming style changes, finally got tired of most of the typedefs. Also the
Timo Sirainen <tss@iki.fi>
parents: 851
diff changeset
167 buffer_t *buffer_create_const_data(pool_t pool, const void *data, size_t 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
168 {
903
fd8888f6f037 Naming style changes, finally got tired of most of the typedefs. Also the
Timo Sirainen <tss@iki.fi>
parents: 851
diff changeset
169 buffer_t *buf;
765
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
170
903
fd8888f6f037 Naming style changes, finally got tired of most of the typedefs. Also the
Timo Sirainen <tss@iki.fi>
parents: 851
diff changeset
171 buf = p_new(pool, buffer_t, 1);
765
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
172 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
173 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
174 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
175 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
176 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
177 }
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
178
903
fd8888f6f037 Naming style changes, finally got tired of most of the typedefs. Also the
Timo Sirainen <tss@iki.fi>
parents: 851
diff changeset
179 buffer_t *buffer_create_dynamic(pool_t pool, size_t init_size, size_t max_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
180 {
903
fd8888f6f037 Naming style changes, finally got tired of most of the typedefs. Also the
Timo Sirainen <tss@iki.fi>
parents: 851
diff changeset
181 buffer_t *buf;
765
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
182
903
fd8888f6f037 Naming style changes, finally got tired of most of the typedefs. Also the
Timo Sirainen <tss@iki.fi>
parents: 851
diff changeset
183 buf = p_new(pool, buffer_t, 1);
765
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
184 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
185 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
186 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
187 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
188 }
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
189
903
fd8888f6f037 Naming style changes, finally got tired of most of the typedefs. Also the
Timo Sirainen <tss@iki.fi>
parents: 851
diff changeset
190 void buffer_free(buffer_t *buf)
765
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 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
193 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
194 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
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
903
fd8888f6f037 Naming style changes, finally got tired of most of the typedefs. Also the
Timo Sirainen <tss@iki.fi>
parents: 851
diff changeset
197 void *buffer_free_without_data(buffer_t *buf)
765
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
198 {
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
199 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
200
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
201 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
202 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
203 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
204 }
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
205
903
fd8888f6f037 Naming style changes, finally got tired of most of the typedefs. Also the
Timo Sirainen <tss@iki.fi>
parents: 851
diff changeset
206 size_t buffer_write(buffer_t *buf, size_t 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
207 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 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
210 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
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 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
213 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
214 }
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
215
903
fd8888f6f037 Naming style changes, finally got tired of most of the typedefs. Also the
Timo Sirainen <tss@iki.fi>
parents: 851
diff changeset
216 size_t buffer_append(buffer_t *buf, const void *data, size_t data_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
217 {
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 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
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
903
fd8888f6f037 Naming style changes, finally got tired of most of the typedefs. Also the
Timo Sirainen <tss@iki.fi>
parents: 851
diff changeset
221 size_t buffer_append_c(buffer_t *buf, char chr)
765
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
222 {
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
223 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
224
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
225 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
226 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
227 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
228
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
229 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
230 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
231 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
232 }
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
233
903
fd8888f6f037 Naming style changes, finally got tired of most of the typedefs. Also the
Timo Sirainen <tss@iki.fi>
parents: 851
diff changeset
234 size_t buffer_insert(buffer_t *buf, size_t pos,
805
5ac361acb316 Marked all non-trivial buffer modifications with @UNSAFE tag. Several
Timo Sirainen <tss@iki.fi>
parents: 765
diff changeset
235 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
236 {
5ac361acb316 Marked all non-trivial buffer modifications with @UNSAFE tag. Several
Timo Sirainen <tss@iki.fi>
parents: 765
diff changeset
237 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
238
851
e73389a2324a Several fixes. I think it's beginning to look safe.
Timo Sirainen <tss@iki.fi>
parents: 832
diff changeset
239 /* move_size == number of bytes we have to move forward to make space */
e73389a2324a Several fixes. I think it's beginning to look safe.
Timo Sirainen <tss@iki.fi>
parents: 832
diff changeset
240 move_size = I_MIN(buf->used, buf->limit) - buf->start_pos;
e73389a2324a Several fixes. I think it's beginning to look safe.
Timo Sirainen <tss@iki.fi>
parents: 832
diff changeset
241 if (pos >= move_size)
e73389a2324a Several fixes. I think it's beginning to look safe.
Timo Sirainen <tss@iki.fi>
parents: 832
diff changeset
242 return 0;
805
5ac361acb316 Marked all non-trivial buffer modifications with @UNSAFE tag. Several
Timo Sirainen <tss@iki.fi>
parents: 765
diff changeset
243 move_size -= pos;
5ac361acb316 Marked all non-trivial buffer modifications with @UNSAFE tag. Several
Timo Sirainen <tss@iki.fi>
parents: 765
diff changeset
244
851
e73389a2324a Several fixes. I think it's beginning to look safe.
Timo Sirainen <tss@iki.fi>
parents: 832
diff changeset
245 /* size == number of bytes we want to modify after pos */
805
5ac361acb316 Marked all non-trivial buffer modifications with @UNSAFE tag. Several
Timo Sirainen <tss@iki.fi>
parents: 765
diff changeset
246 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
247 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
248 else
5ac361acb316 Marked all non-trivial buffer modifications with @UNSAFE tag. Several
Timo Sirainen <tss@iki.fi>
parents: 765
diff changeset
249 size = (size_t)-1;
5ac361acb316 Marked all non-trivial buffer modifications with @UNSAFE tag. Several
Timo Sirainen <tss@iki.fi>
parents: 765
diff changeset
250
5ac361acb316 Marked all non-trivial buffer modifications with @UNSAFE tag. Several
Timo Sirainen <tss@iki.fi>
parents: 765
diff changeset
251 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
252 return 0;
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 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
255 size -= move_size;
851
e73389a2324a Several fixes. I think it's beginning to look safe.
Timo Sirainen <tss@iki.fi>
parents: 832
diff changeset
256 /* size == number of bytes we actually inserted. data_size usually. */
805
5ac361acb316 Marked all non-trivial buffer modifications with @UNSAFE tag. Several
Timo Sirainen <tss@iki.fi>
parents: 765
diff changeset
257
5ac361acb316 Marked all non-trivial buffer modifications with @UNSAFE tag. Several
Timo Sirainen <tss@iki.fi>
parents: 765
diff changeset
258 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
259 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
260 return size;
5ac361acb316 Marked all non-trivial buffer modifications with @UNSAFE tag. Several
Timo Sirainen <tss@iki.fi>
parents: 765
diff changeset
261 }
5ac361acb316 Marked all non-trivial buffer modifications with @UNSAFE tag. Several
Timo Sirainen <tss@iki.fi>
parents: 765
diff changeset
262
903
fd8888f6f037 Naming style changes, finally got tired of most of the typedefs. Also the
Timo Sirainen <tss@iki.fi>
parents: 851
diff changeset
263 size_t buffer_delete(buffer_t *buf, size_t pos, size_t size)
805
5ac361acb316 Marked all non-trivial buffer modifications with @UNSAFE tag. Several
Timo Sirainen <tss@iki.fi>
parents: 765
diff changeset
264 {
5ac361acb316 Marked all non-trivial buffer modifications with @UNSAFE tag. Several
Timo Sirainen <tss@iki.fi>
parents: 765
diff changeset
265 size_t end_size;
5ac361acb316 Marked all non-trivial buffer modifications with @UNSAFE tag. Several
Timo Sirainen <tss@iki.fi>
parents: 765
diff changeset
266
5ac361acb316 Marked all non-trivial buffer modifications with @UNSAFE tag. Several
Timo Sirainen <tss@iki.fi>
parents: 765
diff changeset
267 if (buf->readonly)
5ac361acb316 Marked all non-trivial buffer modifications with @UNSAFE tag. Several
Timo Sirainen <tss@iki.fi>
parents: 765
diff changeset
268 return 0;
5ac361acb316 Marked all non-trivial buffer modifications with @UNSAFE tag. Several
Timo Sirainen <tss@iki.fi>
parents: 765
diff changeset
269
851
e73389a2324a Several fixes. I think it's beginning to look safe.
Timo Sirainen <tss@iki.fi>
parents: 832
diff changeset
270 end_size = I_MIN(buf->used, buf->limit) - buf->start_pos;
e73389a2324a Several fixes. I think it's beginning to look safe.
Timo Sirainen <tss@iki.fi>
parents: 832
diff changeset
271 if (pos >= end_size)
e73389a2324a Several fixes. I think it's beginning to look safe.
Timo Sirainen <tss@iki.fi>
parents: 832
diff changeset
272 return 0;
805
5ac361acb316 Marked all non-trivial buffer modifications with @UNSAFE tag. Several
Timo Sirainen <tss@iki.fi>
parents: 765
diff changeset
273 end_size -= pos;
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 if (size < end_size) {
5ac361acb316 Marked all non-trivial buffer modifications with @UNSAFE tag. Several
Timo Sirainen <tss@iki.fi>
parents: 765
diff changeset
276 /* 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
277 end_size -= size;
805
5ac361acb316 Marked all non-trivial buffer modifications with @UNSAFE tag. Several
Timo Sirainen <tss@iki.fi>
parents: 765
diff changeset
278 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
279 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
280 } else {
5ac361acb316 Marked all non-trivial buffer modifications with @UNSAFE tag. Several
Timo Sirainen <tss@iki.fi>
parents: 765
diff changeset
281 /* 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
282 size = end_size;
5ac361acb316 Marked all non-trivial buffer modifications with @UNSAFE tag. Several
Timo Sirainen <tss@iki.fi>
parents: 765
diff changeset
283 end_size = 0;
5ac361acb316 Marked all non-trivial buffer modifications with @UNSAFE tag. Several
Timo Sirainen <tss@iki.fi>
parents: 765
diff changeset
284 }
5ac361acb316 Marked all non-trivial buffer modifications with @UNSAFE tag. Several
Timo Sirainen <tss@iki.fi>
parents: 765
diff changeset
285
5ac361acb316 Marked all non-trivial buffer modifications with @UNSAFE tag. Several
Timo Sirainen <tss@iki.fi>
parents: 765
diff changeset
286 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
287 return size;
5ac361acb316 Marked all non-trivial buffer modifications with @UNSAFE tag. Several
Timo Sirainen <tss@iki.fi>
parents: 765
diff changeset
288 }
5ac361acb316 Marked all non-trivial buffer modifications with @UNSAFE tag. Several
Timo Sirainen <tss@iki.fi>
parents: 765
diff changeset
289
903
fd8888f6f037 Naming style changes, finally got tired of most of the typedefs. Also the
Timo Sirainen <tss@iki.fi>
parents: 851
diff changeset
290 size_t buffer_copy(buffer_t *dest, size_t dest_pos,
fd8888f6f037 Naming style changes, finally got tired of most of the typedefs. Also the
Timo Sirainen <tss@iki.fi>
parents: 851
diff changeset
291 const buffer_t *src, size_t src_pos, size_t copy_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
292 {
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
293 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
294 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
295
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
296 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
297 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
298
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
299 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
300 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
301 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
302 } else {
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
303 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
304 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
305 }
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
306 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
307 }
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
308
903
fd8888f6f037 Naming style changes, finally got tired of most of the typedefs. Also the
Timo Sirainen <tss@iki.fi>
parents: 851
diff changeset
309 size_t buffer_append_buf(buffer_t *dest, const buffer_t *src,
765
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
310 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
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_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
313 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
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
903
fd8888f6f037 Naming style changes, finally got tired of most of the typedefs. Also the
Timo Sirainen <tss@iki.fi>
parents: 851
diff changeset
316 void *buffer_get_space(buffer_t *buf, size_t pos, size_t 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
317 {
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
318 if (!buffer_check_write(buf, &pos, &size, FALSE))
851
e73389a2324a Several fixes. I think it's beginning to look safe.
Timo Sirainen <tss@iki.fi>
parents: 832
diff changeset
319 return NULL;
765
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 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
322 }
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
323
903
fd8888f6f037 Naming style changes, finally got tired of most of the typedefs. Also the
Timo Sirainen <tss@iki.fi>
parents: 851
diff changeset
324 void *buffer_append_space(buffer_t *buf, size_t 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
325 {
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 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
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
903
fd8888f6f037 Naming style changes, finally got tired of most of the typedefs. Also the
Timo Sirainen <tss@iki.fi>
parents: 851
diff changeset
329 const void *buffer_get_data(const buffer_t *buf, size_t *used_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
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 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
332 *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
333 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
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
903
fd8888f6f037 Naming style changes, finally got tired of most of the typedefs. Also the
Timo Sirainen <tss@iki.fi>
parents: 851
diff changeset
336 void *buffer_get_modifyable_data(const buffer_t *buf, size_t *used_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
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 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
339 *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
340 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
341 }
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
342
903
fd8888f6f037 Naming style changes, finally got tired of most of the typedefs. Also the
Timo Sirainen <tss@iki.fi>
parents: 851
diff changeset
343 void buffer_set_used_size(buffer_t *buf, size_t used_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
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(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
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->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
348 }
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
349
903
fd8888f6f037 Naming style changes, finally got tired of most of the typedefs. Also the
Timo Sirainen <tss@iki.fi>
parents: 851
diff changeset
350 size_t buffer_get_used_size(const buffer_t *buf)
765
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
351 {
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
352 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
353 }
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
354
903
fd8888f6f037 Naming style changes, finally got tired of most of the typedefs. Also the
Timo Sirainen <tss@iki.fi>
parents: 851
diff changeset
355 size_t buffer_set_start_pos(buffer_t *buf, size_t abs_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
356 {
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
357 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
358
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
359 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
360
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
361 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
362 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
363 }
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
364
903
fd8888f6f037 Naming style changes, finally got tired of most of the typedefs. Also the
Timo Sirainen <tss@iki.fi>
parents: 851
diff changeset
365 size_t buffer_get_start_pos(const buffer_t *buf)
765
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
366 {
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
367 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
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
903
fd8888f6f037 Naming style changes, finally got tired of most of the typedefs. Also the
Timo Sirainen <tss@iki.fi>
parents: 851
diff changeset
370 size_t buffer_set_limit(buffer_t *buf, size_t 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
371 {
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
372 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
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 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
375 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
376 else
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
377 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
378
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
379 buf->limit = I_MIN(limit, buf->max_alloc);
851
e73389a2324a Several fixes. I think it's beginning to look safe.
Timo Sirainen <tss@iki.fi>
parents: 832
diff changeset
380 return old - buf->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
381 }
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
382
903
fd8888f6f037 Naming style changes, finally got tired of most of the typedefs. Also the
Timo Sirainen <tss@iki.fi>
parents: 851
diff changeset
383 size_t buffer_get_limit(const buffer_t *buf)
765
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
384 {
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
385 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
386 }
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
387
903
fd8888f6f037 Naming style changes, finally got tired of most of the typedefs. Also the
Timo Sirainen <tss@iki.fi>
parents: 851
diff changeset
388 size_t buffer_get_size(const buffer_t *buf)
765
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
389 {
553f050c8313 Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
390 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
391 }
830
27774d0493dd Fixed several bugs, added a small testsuite. Still requires careful auditing.
Timo Sirainen <tss@iki.fi>
parents: 805
diff changeset
392
27774d0493dd Fixed several bugs, added a small testsuite. Still requires careful auditing.
Timo Sirainen <tss@iki.fi>
parents: 805
diff changeset
393 #ifdef BUFFER_TEST
851
e73389a2324a Several fixes. I think it's beginning to look safe.
Timo Sirainen <tss@iki.fi>
parents: 832
diff changeset
394 /* gcc buffer.c -o testbuffer liblib.a -Wall -DHAVE_CONFIG_H -DBUFFER_TEST -g */
830
27774d0493dd Fixed several bugs, added a small testsuite. Still requires careful auditing.
Timo Sirainen <tss@iki.fi>
parents: 805
diff changeset
395 int main(void)
27774d0493dd Fixed several bugs, added a small testsuite. Still requires careful auditing.
Timo Sirainen <tss@iki.fi>
parents: 805
diff changeset
396 {
903
fd8888f6f037 Naming style changes, finally got tired of most of the typedefs. Also the
Timo Sirainen <tss@iki.fi>
parents: 851
diff changeset
397 buffer_t *buf;
830
27774d0493dd Fixed several bugs, added a small testsuite. Still requires careful auditing.
Timo Sirainen <tss@iki.fi>
parents: 805
diff changeset
398 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
399 size_t bufsize;
27774d0493dd Fixed several bugs, added a small testsuite. Still requires careful auditing.
Timo Sirainen <tss@iki.fi>
parents: 805
diff changeset
400
851
e73389a2324a Several fixes. I think it's beginning to look safe.
Timo Sirainen <tss@iki.fi>
parents: 832
diff changeset
401 lib_init();
e73389a2324a Several fixes. I think it's beginning to look safe.
Timo Sirainen <tss@iki.fi>
parents: 832
diff changeset
402
830
27774d0493dd Fixed several bugs, added a small testsuite. Still requires careful auditing.
Timo Sirainen <tss@iki.fi>
parents: 805
diff changeset
403 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
404 bufdata = data + 1;
27774d0493dd Fixed several bugs, added a small testsuite. Still requires careful auditing.
Timo Sirainen <tss@iki.fi>
parents: 805
diff changeset
405 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
406
27774d0493dd Fixed several bugs, added a small testsuite. Still requires careful auditing.
Timo Sirainen <tss@iki.fi>
parents: 805
diff changeset
407 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
408 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
409 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
410 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
411 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
412
27774d0493dd Fixed several bugs, added a small testsuite. Still requires careful auditing.
Timo Sirainen <tss@iki.fi>
parents: 805
diff changeset
413 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
414 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
415 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
416 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
417 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
418
27774d0493dd Fixed several bugs, added a small testsuite. Still requires careful auditing.
Timo Sirainen <tss@iki.fi>
parents: 805
diff changeset
419 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
420 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
421 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
422 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
423 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
424
851
e73389a2324a Several fixes. I think it's beginning to look safe.
Timo Sirainen <tss@iki.fi>
parents: 832
diff changeset
425 buf = buffer_create_data(system_pool, data, sizeof(data));
e73389a2324a Several fixes. I think it's beginning to look safe.
Timo Sirainen <tss@iki.fi>
parents: 832
diff changeset
426 buffer_set_used_size(buf, 1);
e73389a2324a Several fixes. I think it's beginning to look safe.
Timo Sirainen <tss@iki.fi>
parents: 832
diff changeset
427 buffer_set_start_pos(buf, 1);
e73389a2324a Several fixes. I think it's beginning to look safe.
Timo Sirainen <tss@iki.fi>
parents: 832
diff changeset
428 buffer_set_limit(buf, sizeof(data)-2);
830
27774d0493dd Fixed several bugs, added a small testsuite. Still requires careful auditing.
Timo Sirainen <tss@iki.fi>
parents: 805
diff changeset
429 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
430 i_assert(buffer_insert(buf, 2, "123456", 6) == 5);
851
e73389a2324a Several fixes. I think it's beginning to look safe.
Timo Sirainen <tss@iki.fi>
parents: 832
diff changeset
431 i_assert(buf->used == 11);
e73389a2324a Several fixes. I think it's beginning to look safe.
Timo Sirainen <tss@iki.fi>
parents: 832
diff changeset
432 i_assert(memcmp(buf->r_buffer, "!1212345345", 11) == 0);
830
27774d0493dd Fixed several bugs, added a small testsuite. Still requires careful auditing.
Timo Sirainen <tss@iki.fi>
parents: 805
diff changeset
433 i_assert(buffer_delete(buf, 2, 5) == 5);
851
e73389a2324a Several fixes. I think it's beginning to look safe.
Timo Sirainen <tss@iki.fi>
parents: 832
diff changeset
434 i_assert(buf->used == 6);
e73389a2324a Several fixes. I think it's beginning to look safe.
Timo Sirainen <tss@iki.fi>
parents: 832
diff changeset
435 i_assert(memcmp(buf->r_buffer, "!12345", 6) == 0);
830
27774d0493dd Fixed several bugs, added a small testsuite. Still requires careful auditing.
Timo Sirainen <tss@iki.fi>
parents: 805
diff changeset
436 i_assert(buffer_delete(buf, 3, 5) == 2);
851
e73389a2324a Several fixes. I think it's beginning to look safe.
Timo Sirainen <tss@iki.fi>
parents: 832
diff changeset
437 i_assert(buf->used == 4);
e73389a2324a Several fixes. I think it's beginning to look safe.
Timo Sirainen <tss@iki.fi>
parents: 832
diff changeset
438 i_assert(memcmp(buf->r_buffer, "!123", 4) == 0);
830
27774d0493dd Fixed several bugs, added a small testsuite. Still requires careful auditing.
Timo Sirainen <tss@iki.fi>
parents: 805
diff changeset
439
27774d0493dd Fixed several bugs, added a small testsuite. Still requires careful auditing.
Timo Sirainen <tss@iki.fi>
parents: 805
diff changeset
440 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
441 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
442 return 0;
27774d0493dd Fixed several bugs, added a small testsuite. Still requires careful auditing.
Timo Sirainen <tss@iki.fi>
parents: 805
diff changeset
443 }
27774d0493dd Fixed several bugs, added a small testsuite. Still requires careful auditing.
Timo Sirainen <tss@iki.fi>
parents: 805
diff changeset
444 #endif