1 /* $NetBSD: ninebuf.c,v 1.6 2007/05/20 16:24:37 pooka Exp $ */
4 * Copyright (c) 2006, 2007 Antti Kantee. All Rights Reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
16 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 #include <sys/cdefs.h>
30 __RCSID("$NetBSD: ninebuf.c,v 1.6 2007/05/20 16:24:37 pooka Exp $");
33 #include <sys/types.h>
35 #include <sys/vnode.h>
43 #include "ninepuffs.h"
45 #define CHECK(v) if (!(v)) abort()
48 p9pbuf_get_type(struct puffs_framebuf
*pb
)
52 puffs_framebuf_getdata_atoff(pb
, 4, &val
, 1);
57 p9pbuf_get_tag(struct puffs_framebuf
*pb
)
61 puffs_framebuf_getdata_atoff(pb
, 5, &val
, 2);
66 p9pbuf_get_len(struct puffs_framebuf
*pb
)
70 puffs_framebuf_getdata_atoff(pb
, 0, &val
, 4);
74 #define CUROFF(pb) (puffs_framebuf_telloff(pb))
76 p9pbuf_read(struct puffs_usermount
*pu
, struct puffs_framebuf
*pb
,
81 size_t howmuch
, winlen
;
85 if ((lenstate
= (CUROFF(pb
) < 4)))
86 howmuch
= 4 - CUROFF(pb
);
88 howmuch
= p9pbuf_get_len(pb
) - CUROFF(pb
);
90 if (puffs_framebuf_reserve_space(pb
, howmuch
) == -1)
95 if (puffs_framebuf_getwindow(pb
, CUROFF(pb
), &win
, &winlen
)==-1)
97 n
= read(fd
, win
, winlen
);
107 puffs_framebuf_seekset(pb
, CUROFF(pb
) + n
);
113 puffs_framebuf_seekset(pb
, 7);
121 p9pbuf_write(struct puffs_usermount
*pu
, struct puffs_framebuf
*pb
,
126 size_t winlen
, howmuch
;
128 if (CUROFF(pb
) == 0) {
131 len
= htole32(puffs_framebuf_tellsize(pb
));
132 puffs_framebuf_putdata_atoff(pb
, 0, &len
, 4);
135 howmuch
= puffs_framebuf_tellsize(pb
) - CUROFF(pb
);
138 if (puffs_framebuf_getwindow(pb
, CUROFF(pb
), &win
, &winlen
)==-1)
140 n
= send(fd
, win
, winlen
, MSG_NOSIGNAL
);
150 puffs_framebuf_seekset(pb
, CUROFF(pb
) + n
);
161 p9pbuf_cmp(struct puffs_usermount
*pu
,
162 struct puffs_framebuf
*c1
, struct puffs_framebuf
*c2
, int *notresp
)
165 return p9pbuf_get_tag(c1
) != p9pbuf_get_tag(c2
);
168 struct puffs_framebuf
*
171 struct puffs_framebuf
*pb
;
173 pb
= puffs_framebuf_make();
174 puffs_framebuf_seekset(pb
, 4);
179 p9pbuf_recycleout(struct puffs_framebuf
*pb
)
182 puffs_framebuf_recycle(pb
);
183 puffs_framebuf_seekset(pb
, 4);
187 p9pbuf_put_1(struct puffs_framebuf
*pb
, uint8_t val
)
191 rv
= puffs_framebuf_putdata(pb
, &val
, 1);
196 p9pbuf_put_2(struct puffs_framebuf
*pb
, uint16_t val
)
201 rv
= puffs_framebuf_putdata(pb
, &val
, 2);
206 p9pbuf_put_4(struct puffs_framebuf
*pb
, uint32_t val
)
211 rv
= puffs_framebuf_putdata(pb
, &val
, 4);
216 p9pbuf_put_8(struct puffs_framebuf
*pb
, uint64_t val
)
221 rv
= puffs_framebuf_putdata(pb
, &val
, 8);
226 p9pbuf_put_data(struct puffs_framebuf
*pb
, const void *data
, uint16_t dlen
)
230 p9pbuf_put_2(pb
, dlen
);
231 rv
= puffs_framebuf_putdata(pb
, data
, dlen
);
236 p9pbuf_put_str(struct puffs_framebuf
*pb
, const char *str
)
239 p9pbuf_put_data(pb
, str
, strlen(str
));
243 p9pbuf_write_data(struct puffs_framebuf
*pb
, uint8_t *data
, uint32_t dlen
)
247 rv
= puffs_framebuf_putdata(pb
, data
, dlen
);
251 #define ERETURN(rv) return ((rv) == -1 ? errno : 0)
254 p9pbuf_get_1(struct puffs_framebuf
*pb
, uint8_t *val
)
257 ERETURN(puffs_framebuf_getdata(pb
, val
, 1));
261 p9pbuf_get_2(struct puffs_framebuf
*pb
, uint16_t *val
)
265 rv
= puffs_framebuf_getdata(pb
, val
, 2);
272 p9pbuf_get_4(struct puffs_framebuf
*pb
, uint32_t *val
)
276 rv
= puffs_framebuf_getdata(pb
, val
, 4);
283 p9pbuf_get_8(struct puffs_framebuf
*pb
, uint64_t *val
)
287 rv
= puffs_framebuf_getdata(pb
, val
, 8);
294 p9pbuf_get_data(struct puffs_framebuf
*pb
, uint8_t **dp
, uint16_t *dlenp
)
300 rv
= p9pbuf_get_2(pb
, &len
);
304 if (puffs_framebuf_remaining(pb
) < len
)
308 data
= emalloc(len
+1);
309 rv
= puffs_framebuf_getdata(pb
, data
, len
);
317 puffs_framebuf_seekset(pb
, puffs_framebuf_telloff(pb
)+len
);
326 p9pbuf_read_data(struct puffs_framebuf
*pb
, uint8_t *buf
, uint32_t dlen
)
329 ERETURN(puffs_framebuf_getdata(pb
, buf
, dlen
));
333 p9pbuf_get_str(struct puffs_framebuf
*pb
, char **dp
, uint16_t *dlenp
)
336 return p9pbuf_get_data(pb
, (uint8_t **)dp
, dlenp
);