~ubuntu-branches/ubuntu/saucy/varnish/saucy

« back to all changes in this revision

Viewing changes to lib/libvarnish/vsb.3

  • Committer: Bazaar Package Importer
  • Author(s): Stig Sandbeck Mathisen
  • Date: 2011-03-21 10:16:07 UTC
  • mfrom: (24.1.2 experimental)
  • Revision ID: james.westby@ubuntu.com-20110321101607-528fzl583fqanas5
Tags: 2.1.5-2
ReleaseĀ forĀ unstable

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
.\"-
 
2
.\" Copyright (c) 2000 Poul Henning Kamp and Dag-Erling SmĆørgrav
 
3
.\" All rights reserved.
 
4
.\"
 
5
.\" Redistribution and use in source and binary forms, with or without
 
6
.\" modification, are permitted provided that the following conditions
 
7
.\" are met:
 
8
.\" 1. Redistributions of source code must retain the above copyright
 
9
.\"    notice, this list of conditions and the following disclaimer.
 
10
.\" 2. Redistributions in binary form must reproduce the above copyright
 
11
.\"    notice, this list of conditions and the following disclaimer in the
 
12
.\"    documentation and/or other materials provided with the distribution.
 
13
.\"
 
14
.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 
15
.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 
16
.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 
17
.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 
18
.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 
19
.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 
20
.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 
21
.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 
22
.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 
23
.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 
24
.\" SUCH DAMAGE.
 
25
.\"
 
26
.\" $Id$
 
27
.\" $FreeBSD: src/share/man/man9/vsb.9,v 1.25 2005/12/23 11:49:52 phk Exp $
 
28
.\"
 
29
.\" XXX
 
30
.ds str-Lb-libvarnish Varnish Library (libvarnish, \-lvarnish)
 
31
.\" XXX
 
32
.Dd March 14, 2006
 
33
.Dt VSB 3
 
34
.Os
 
35
.Sh NAME
 
36
.Nm vsb_new ,
 
37
.Nm vsb_clear ,
 
38
.Nm vsb_setpos ,
 
39
.Nm vsb_bcat ,
 
40
.Nm vsb_bcpy ,
 
41
.Nm vsb_cat ,
 
42
.Nm vsb_cpy ,
 
43
.Nm vsb_printf ,
 
44
.Nm vsb_vprintf ,
 
45
.Nm vsb_putc ,
 
46
.Nm vsb_trim ,
 
47
.Nm vsb_overflowed ,
 
48
.Nm vsb_finish ,
 
49
.Nm vsb_data ,
 
50
.Nm vsb_len ,
 
51
.Nm vsb_done ,
 
52
.Nm vsb_delete
 
53
.Nd safe string formatting
 
54
.Sh LIBRARY
 
55
.Lb libvarnish
 
56
.Sh SYNOPSIS
 
57
.In vsb.h
 
58
.Ft struct vsb *
 
59
.Fn vsb_new "struct vsb *s" "char *buf" "int length" "int flags"
 
60
.Ft void
 
61
.Fn vsb_clear "struct vsb *s"
 
62
.Ft int
 
63
.Fn vsb_setpos "struct vsb *s" "int pos"
 
64
.Ft int
 
65
.Fn vsb_bcat "struct vsb *s" "const void *buf" "size_t len"
 
66
.Ft int
 
67
.Fn vsb_bcpy "struct vsb *s" "const void *buf" "size_t len"
 
68
.Ft int
 
69
.Fn vsb_cat "struct vsb *s" "const char *str"
 
70
.Ft int
 
71
.Fn vsb_cpy "struct vsb *s" "const char *str"
 
72
.Ft int
 
73
.Fn vsb_printf "struct vsb *s" "const char *fmt" "..."
 
74
.Ft int
 
75
.Fn vsb_vprintf "struct vsb *s" "const char *fmt" "va_list ap"
 
76
.Ft int
 
77
.Fn vsb_putc "struct vsb *s" "int c"
 
78
.Ft int
 
79
.Fn vsb_trim "struct vsb *s"
 
80
.Ft int
 
81
.Fn vsb_overflowed "struct vsb *s"
 
82
.Ft void
 
83
.Fn vsb_finish "struct vsb *s"
 
84
.Ft char *
 
85
.Fn vsb_data "struct vsb *s"
 
86
.Ft int
 
87
.Fn vsb_len "struct vsb *s"
 
88
.Ft int
 
89
.Fn vsb_done "struct vsb *s"
 
90
.Ft void
 
91
.Fn vsb_delete "struct vsb *s"
 
92
.Sh DESCRIPTION
 
93
The
 
94
.Nm vsb
 
95
family of functions allows one to safely allocate, construct and
 
96
release bounded null-terminated strings in kernel space.
 
97
Instead of arrays of characters, these functions operate on structures
 
98
called
 
99
.Fa vsbs ,
 
100
defined in
 
101
.In sys/vsb.h .
 
102
.Pp
 
103
The
 
104
.Fn vsb_new
 
105
function initializes the
 
106
.Fa vsb
 
107
pointed to by its first argument.
 
108
If that pointer is
 
109
.Dv NULL ,
 
110
.Fn vsb_new
 
111
allocates a
 
112
.Vt struct vsb
 
113
using
 
114
.Xr malloc 9 .
 
115
The
 
116
.Fa buf
 
117
argument is a pointer to a buffer in which to store the actual string;
 
118
if it is
 
119
.Dv NULL ,
 
120
.Fn vsb_new
 
121
will allocate one using
 
122
.Xr malloc 9 .
 
123
The
 
124
.Fa length
 
125
is the initial size of the storage buffer.
 
126
The fourth argument,
 
127
.Fa flags ,
 
128
may be comprised of the following flags:
 
129
.Bl -tag -width ".Dv VSB_AUTOEXTEND"
 
130
.It Dv VSB_FIXEDLEN
 
131
The storage buffer is fixed at its initial size.
 
132
Attempting to extend the vsb beyond this size results in an overflow condition.
 
133
.It Dv VSB_AUTOEXTEND
 
134
This indicates that the storage buffer may be extended as necessary, so long
 
135
as resources allow, to hold additional data.
 
136
.El
 
137
.Pp
 
138
Note that if
 
139
.Fa buf
 
140
is not
 
141
.Dv NULL ,
 
142
it must point to an array of at least
 
143
.Fa length
 
144
characters.
 
145
The result of accessing that array directly while it is in use by the
 
146
vsb is undefined.
 
147
.Pp
 
148
The
 
149
.Fn vsb_delete
 
150
function clears the
 
151
.Fa vsb
 
152
and frees any memory allocated for it.
 
153
There must be a call to
 
154
.Fn vsb_delete
 
155
for every call to
 
156
.Fn vsb_new .
 
157
Any attempt to access the vsb after it has been deleted will fail.
 
158
.Pp
 
159
The
 
160
.Fn vsb_clear
 
161
function invalidates the contents of the
 
162
.Fa vsb
 
163
and resets its position to zero.
 
164
.Pp
 
165
The
 
166
.Fn vsb_setpos
 
167
function sets the
 
168
.Fa vsb Ns 's
 
169
end position to
 
170
.Fa pos ,
 
171
which is a value between zero and one less than the size of the
 
172
storage buffer.
 
173
This effectively truncates the vsb at the new position.
 
174
.Pp
 
175
The
 
176
.Fn vsb_bcat
 
177
function appends the first
 
178
.Fa len
 
179
bytes from the buffer
 
180
.Fa buf
 
181
to the
 
182
.Fa vsb .
 
183
.Pp
 
184
The
 
185
.Fn vsb_bcpy
 
186
function replaces the contents of the
 
187
.Fa vsb
 
188
with the first
 
189
.Fa len
 
190
bytes from the buffer
 
191
.Fa buf .
 
192
.Pp
 
193
The
 
194
.Fn vsb_cat
 
195
function appends the NUL-terminated string
 
196
.Fa str
 
197
to the
 
198
.Fa vsb
 
199
at the current position.
 
200
.Pp
 
201
The
 
202
.Fn vsb_cpy
 
203
function replaces the contents of the
 
204
.Fa vsb
 
205
with those of the NUL-terminated string
 
206
.Fa str .
 
207
This is equivalent to calling
 
208
.Fn vsb_cat
 
209
with a fresh
 
210
.Fa vsb
 
211
or one which position has been reset to zero with
 
212
.Fn vsb_clear
 
213
or
 
214
.Fn vsb_setpos .
 
215
.Pp
 
216
The
 
217
.Fn vsb_printf
 
218
function formats its arguments according to the format string pointed
 
219
to by
 
220
.Fa fmt
 
221
and appends the resulting string to the
 
222
.Fa vsb
 
223
at the current position.
 
224
.Pp
 
225
The
 
226
.Fn vsb_vprintf
 
227
function behaves the same as
 
228
.Fn vsb_printf
 
229
except that the arguments are obtained from the variable-length argument list
 
230
.Fa ap .
 
231
.Pp
 
232
The
 
233
.Fn vsb_putc
 
234
function appends the character
 
235
.Fa c
 
236
to the
 
237
.Fa vsb
 
238
at the current position.
 
239
.Pp
 
240
The
 
241
.Fn vsb_trim
 
242
function removes trailing whitespace from the
 
243
.Fa vsb .
 
244
.Pp
 
245
The
 
246
.Fn vsb_overflowed
 
247
function returns a non-zero value if the
 
248
.Fa vsb
 
249
overflowed.
 
250
.Pp
 
251
The
 
252
.Fn vsb_finish
 
253
function null-terminates the
 
254
.Fa vsb
 
255
and marks it as finished, which means that it may no longer be
 
256
modified using
 
257
.Fn vsb_setpos ,
 
258
.Fn vsb_cat ,
 
259
.Fn vsb_cpy ,
 
260
.Fn vsb_printf
 
261
or
 
262
.Fn vsb_putc .
 
263
.Pp
 
264
The
 
265
.Fn vsb_data
 
266
and
 
267
.Fn vsb_len
 
268
functions return the actual string and its length, respectively;
 
269
.Fn vsb_data
 
270
only works on a finished
 
271
.Fa vsb .
 
272
.Fn vsb_done
 
273
returns non-zero if the vsb is finished.
 
274
.Sh NOTES
 
275
If an operation caused an
 
276
.Fa vsb
 
277
to overflow, most subsequent operations on it will fail until the
 
278
.Fa vsb
 
279
is finished using
 
280
.Fn vsb_finish
 
281
or reset using
 
282
.Fn vsb_clear ,
 
283
or its position is reset to a value between 0 and one less than the
 
284
size of its storage buffer using
 
285
.Fn vsb_setpos ,
 
286
or it is reinitialized to a sufficiently short string using
 
287
.Fn vsb_cpy .
 
288
.Sh RETURN VALUES
 
289
.Fn vsb_new
 
290
returns
 
291
.Dv NULL
 
292
if it failed to allocate a storage buffer, and a pointer to the new
 
293
.Fa vsb
 
294
otherwise.
 
295
.Pp
 
296
.Fn vsb_setpos
 
297
returns \-1 if
 
298
.Fa pos
 
299
was invalid, and zero otherwise.
 
300
.Pp
 
301
.Fn vsb_cat ,
 
302
.Fn vsb_cpy ,
 
303
.Fn vsb_printf ,
 
304
.Fn vsb_putc ,
 
305
and
 
306
.Fn vsb_trim
 
307
all return \-1 if the buffer overflowed, and zero otherwise.
 
308
.Pp
 
309
.Fn vsb_overflowed
 
310
returns a non-zero value if the buffer overflowed, and zero otherwise.
 
311
.Pp
 
312
.Fn vsb_data
 
313
and
 
314
.Fn vsb_len
 
315
return
 
316
.Dv NULL
 
317
and \-1, respectively, if the buffer overflowed.
 
318
.Sh SEE ALSO
 
319
.Xr printf 3 ,
 
320
.Xr strcat 3 ,
 
321
.Xr strcpy 3
 
322
.Sh HISTORY
 
323
The
 
324
.Nm vsb
 
325
family of functions first appeared in
 
326
.Fx 4.4 .
 
327
.Sh AUTHORS
 
328
.An -nosplit
 
329
The
 
330
.Nm vsb
 
331
family of functions was designed by
 
332
.An Poul-Henning Kamp Aq phk@FreeBSD.org
 
333
and implemented by
 
334
.An Dag-Erling Sm\(/orgrav Aq des@FreeBSD.org .
 
335
Additional improvements were suggested by
 
336
.An Justin T. Gibbs Aq gibbs@FreeBSD.org .
 
337
Auto-extend support added by
 
338
.An Kelly Yancey Aq kbyanc@FreeBSD.org .
 
339
.Pp
 
340
This manual page was written by
 
341
.An Dag-Erling Sm\(/orgrav Aq des@FreeBSD.org .