2 * Copyright © 2011, 2013 Guillem Jover
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 * 3. The name of the author may not be used to endorse or promote products
13 * derived from this software without specific prior written permission.
15 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
16 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
17 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
18 * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
21 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
22 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
23 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
24 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 #include <sys/cdefs.h>
28 #include <sys/types.h>
33 #ifdef HAVE_FOPENCOOKIE
34 struct funopen_cookie {
37 int (*readfn)(void *cookie, char *buf, int size);
38 int (*writefn)(void *cookie, const char *buf, int size);
39 off_t (*seekfn)(void *cookie, off_t offset, int whence);
40 int (*closefn)(void *cookie);
44 funopen_read(void *cookie, char *buf, size_t size)
46 struct funopen_cookie *cookiewrap = cookie;
48 if (cookiewrap->readfn == NULL) {
53 return cookiewrap->readfn(cookiewrap->orig_cookie, buf, size);
57 funopen_write(void *cookie, const char *buf, size_t size)
59 struct funopen_cookie *cookiewrap = cookie;
61 if (cookiewrap->writefn == NULL)
64 return cookiewrap->writefn(cookiewrap->orig_cookie, buf, size);
68 funopen_seek(void *cookie, off64_t *offset, int whence)
70 struct funopen_cookie *cookiewrap = cookie;
73 if (cookiewrap->seekfn == NULL) {
78 soff = cookiewrap->seekfn(cookiewrap->orig_cookie, soff, whence);
85 funopen_close(void *cookie)
87 struct funopen_cookie *cookiewrap = cookie;
90 if (cookiewrap->closefn == NULL)
93 rc = cookiewrap->closefn(cookiewrap->orig_cookie);
101 funopen(const void *cookie,
102 int (*readfn)(void *cookie, char *buf, int size),
103 int (*writefn)(void *cookie, const char *buf, int size),
104 off_t (*seekfn)(void *cookie, off_t offset, int whence),
105 int (*closefn)(void *cookie))
107 struct funopen_cookie *cookiewrap;
108 cookie_io_functions_t funcswrap = {
109 .read = funopen_read,
110 .write = funopen_write,
111 .seek = funopen_seek,
112 .close = funopen_close,
121 } else if (writefn) {
128 cookiewrap = malloc(sizeof(*cookiewrap));
129 if (cookiewrap == NULL)
132 cookiewrap->orig_cookie = (void *)cookie;
133 cookiewrap->readfn = readfn;
134 cookiewrap->writefn = writefn;
135 cookiewrap->seekfn = seekfn;
136 cookiewrap->closefn = closefn;
138 return fopencookie(cookiewrap, mode, funcswrap);
141 #error "Function funopen() needs to be ported."