Blame


1 74b37681 2019-02-07 stsp /* $OpenBSD: buf.h,v 1.13 2011/07/06 15:36:52 nicm Exp $ */
2 74b37681 2019-02-07 stsp /*
3 74b37681 2019-02-07 stsp * Copyright (c) 2003 Jean-Francois Brousseau <jfb@openbsd.org>
4 74b37681 2019-02-07 stsp * All rights reserved.
5 74b37681 2019-02-07 stsp *
6 74b37681 2019-02-07 stsp * Redistribution and use in source and binary forms, with or without
7 74b37681 2019-02-07 stsp * modification, are permitted provided that the following conditions
8 74b37681 2019-02-07 stsp * are met:
9 74b37681 2019-02-07 stsp *
10 74b37681 2019-02-07 stsp * 1. Redistributions of source code must retain the above copyright
11 74b37681 2019-02-07 stsp * notice, this list of conditions and the following disclaimer.
12 74b37681 2019-02-07 stsp * 2. The name of the author may not be used to endorse or promote products
13 74b37681 2019-02-07 stsp * derived from this software without specific prior written permission.
14 74b37681 2019-02-07 stsp *
15 74b37681 2019-02-07 stsp * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
16 74b37681 2019-02-07 stsp * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
17 74b37681 2019-02-07 stsp * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
18 74b37681 2019-02-07 stsp * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19 74b37681 2019-02-07 stsp * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20 74b37681 2019-02-07 stsp * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
21 74b37681 2019-02-07 stsp * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
22 74b37681 2019-02-07 stsp * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
23 74b37681 2019-02-07 stsp * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
24 74b37681 2019-02-07 stsp * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 74b37681 2019-02-07 stsp *
26 74b37681 2019-02-07 stsp * Buffer management
27 74b37681 2019-02-07 stsp * -----------------
28 74b37681 2019-02-07 stsp *
29 74b37681 2019-02-07 stsp * This code provides an API to generic memory buffer management. All
30 74b37681 2019-02-07 stsp * operations are performed on a buf structure, which is kept opaque to the
31 74b37681 2019-02-07 stsp * API user in order to avoid corruption of the fields and make sure that only
32 74b37681 2019-02-07 stsp * the internals can modify the fields.
33 74b37681 2019-02-07 stsp *
34 74b37681 2019-02-07 stsp * The first step is to allocate a new buffer using the buf_alloc()
35 74b37681 2019-02-07 stsp * function, which returns a pointer to a new buffer.
36 74b37681 2019-02-07 stsp */
37 74b37681 2019-02-07 stsp
38 74b37681 2019-02-07 stsp #ifndef BUF_H
39 74b37681 2019-02-07 stsp #define BUF_H
40 74b37681 2019-02-07 stsp
41 74b37681 2019-02-07 stsp #include <sys/types.h>
42 74b37681 2019-02-07 stsp
43 74b37681 2019-02-07 stsp typedef struct buf BUF;
44 74b37681 2019-02-07 stsp
45 54415d85 2020-01-15 tracey struct buf {
46 54415d85 2020-01-15 tracey /* buffer handle, buffer size, and data length */
47 54415d85 2020-01-15 tracey u_char *cb_buf;
48 54415d85 2020-01-15 tracey size_t cb_size;
49 54415d85 2020-01-15 tracey size_t cb_len;
50 54415d85 2020-01-15 tracey };
51 54415d85 2020-01-15 tracey
52 575e8218 2019-10-07 stsp const struct got_error *buf_alloc(BUF **, size_t);
53 db590691 2021-06-02 stsp const struct got_error *buf_load(BUF **, FILE *);
54 1bd8e3b1 2022-07-12 jrick const struct got_error *buf_load_fd(BUF **, int fd);
55 74b37681 2019-02-07 stsp void buf_free(BUF *);
56 74b37681 2019-02-07 stsp void *buf_release(BUF *);
57 74b37681 2019-02-07 stsp u_char buf_getc(BUF *, size_t);
58 74b37681 2019-02-07 stsp void buf_empty(BUF *);
59 13b2bc37 2022-10-23 stsp const struct got_error *buf_discard(BUF *, size_t);
60 af45e626 2019-02-08 stsp const struct got_error *buf_append(size_t *, BUF *, const void *, size_t);
61 af45e626 2019-02-08 stsp const struct got_error *buf_putc(BUF *, int);
62 af45e626 2019-02-08 stsp const struct got_error *buf_puts(size_t *, BUF *b, const char *str);
63 74b37681 2019-02-07 stsp size_t buf_len(BUF *);
64 74b37681 2019-02-07 stsp int buf_write_fd(BUF *, int);
65 af45e626 2019-02-08 stsp const struct got_error *buf_write(BUF *, const char *, mode_t);
66 96cbb597 2019-10-09 stsp const struct got_error *buf_write_stmp(BUF *, char *);
67 74b37681 2019-02-07 stsp u_char *buf_get(BUF *b);
68 74b37681 2019-02-07 stsp
69 74b37681 2019-02-07 stsp #endif /* BUF_H */