Sync usage with man page.
[netbsd-mini2440.git] / dist / smbfs / lib / smb / mbuf.c
blob0b3402fb5d20cf4dd5e846b2fab47779204bdb10
1 /*
2 * Copyright (c) 2000, Boris Popov
3 * All rights reserved.
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 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by Boris Popov.
16 * 4. Neither the name of the author nor the names of any co-contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
32 * from: Id: mbuf.c,v 1.6 2001/02/24 15:56:04 bp Exp
35 #include <sys/cdefs.h>
36 __RCSID("$NetBSD: mbuf.c,v 1.7 2006/10/16 03:38:08 christos Exp $");
38 #include <sys/types.h>
39 #include <sys/endian.h>
40 #include <ctype.h>
41 #include <errno.h>
42 #include <stdio.h>
43 #include <stdlib.h>
44 #include <string.h>
46 #include <netsmb/smb_lib.h>
48 #define MBERROR(x) printf x
50 static int
51 m_get(size_t len, struct mbuf **mpp)
53 struct mbuf *m;
55 len = M_ALIGN(len);
56 if (len < M_MINSIZE)
57 len = M_MINSIZE;
58 m = malloc(M_BASESIZE + len);
59 if (m == NULL)
60 return ENOMEM;
61 bzero(m, M_BASESIZE + len);
62 m->m_maxlen = len;
63 m->m_data = M_TOP(m);
64 *mpp = m;
65 return 0;
68 static void
69 m_free(struct mbuf *m)
71 free(m);
74 static void
75 m_freem(struct mbuf *m0)
77 struct mbuf *m;
79 while (m0) {
80 m = m0->m_next;
81 m_free(m0);
82 m0 = m;
86 static size_t
87 m_totlen(struct mbuf *m0)
89 struct mbuf *m = m0;
90 int len = 0;
92 while (m) {
93 len += m->m_len;
94 m = m->m_next;
96 return len;
99 int
100 m_lineup(struct mbuf *m0, struct mbuf **mpp)
102 struct mbuf *nm, *m;
103 char *dp;
104 size_t len;
105 int error;
107 if (m0->m_next == NULL) {
108 *mpp = m0;
109 return 0;
111 if ((error = m_get(m_totlen(m0), &nm)) != 0)
112 return error;
113 dp = mtod(nm, char *);
114 while (m0) {
115 len = m0->m_len;
116 bcopy(m0->m_data, dp, len);
117 dp += len;
118 m = m0->m_next;
119 m_free(m0);
120 m0 = m;
122 *mpp = nm;
123 return 0;
127 mb_init(struct mbdata *mbp, size_t size)
129 struct mbuf *m;
130 int error;
132 if ((error = m_get(size, &m)) != 0)
133 return error;
134 return mb_initm(mbp, m);
138 mb_initm(struct mbdata *mbp, struct mbuf *m)
140 bzero(mbp, sizeof(*mbp));
141 mbp->mb_top = mbp->mb_cur = m;
142 mbp->mb_pos = mtod(m, char *);
143 return 0;
147 mb_done(struct mbdata *mbp)
149 if (mbp->mb_top) {
150 m_freem(mbp->mb_top);
151 mbp->mb_top = NULL;
153 return 0;
158 mb_fixhdr(struct mbdata *mbp)
160 struct mbuf *m = mbp->mb_top;
161 int len = 0;
163 while (m) {
164 len += m->m_len;
165 m = m->m_next;
167 mbp->mb_top->m_pkthdr.len = len;
168 return len;
172 m_getm(struct mbuf *top, size_t len, struct mbuf **mpp)
174 struct mbuf *m, *mp;
175 int error;
177 for (mp = top; ; mp = mp->m_next) {
178 len -= M_TRAILINGSPACE(mp);
179 if (mp->m_next == NULL)
180 break;
183 if (len > 0) {
184 if ((error = m_get(len, &m)) != 0)
185 return error;
186 mp->m_next = m;
188 *mpp = top;
189 return 0;
193 * Routines to put data in a buffer
195 #define MB_PUT(t) int error; t *p; \
196 if ((error = mb_fit(mbp, sizeof(t), \
197 (void **)(void *)&p)) != 0) \
198 return error
201 * Check if object of size 'size' fit to the current position and
202 * allocate new mbuf if not. Advance pointers and increase length of mbuf(s).
203 * Return pointer to the object placeholder or NULL if any error occured.
206 mb_fit(struct mbdata *mbp, size_t size, void **pp)
208 struct mbuf *m, *mn;
209 int error;
211 m = mbp->mb_cur;
212 if (M_TRAILINGSPACE(m) < (int)size) {
213 if ((error = m_get(size, &mn)) != 0)
214 return error;
215 mbp->mb_pos = mtod(mn, char *);
216 mbp->mb_cur = m->m_next = mn;
217 m = mn;
219 m->m_len += size;
220 *pp = mbp->mb_pos;
221 mbp->mb_pos += size;
222 mbp->mb_count += size;
223 return 0;
227 mb_put_uint8(struct mbdata *mbp, u_int8_t x)
229 MB_PUT(u_int8_t);
230 *p = x;
231 return 0;
235 mb_put_uint16be(struct mbdata *mbp, u_int16_t x)
237 MB_PUT(u_int16_t);
238 setwbe(p, 0, x);
239 return 0;
243 mb_put_uint16le(struct mbdata *mbp, u_int16_t x)
245 MB_PUT(u_int16_t);
246 setwle(p, 0, x);
247 return 0;
251 mb_put_uint32be(struct mbdata *mbp, u_int32_t x)
253 MB_PUT(u_int32_t);
254 setdbe(p, 0, x);
255 return 0;
259 mb_put_uint32le(struct mbdata *mbp, u_int32_t x)
261 MB_PUT(u_int32_t);
262 setdle(p, 0, x);
263 return 0;
267 mb_put_int64be(struct mbdata *mbp, int64_t x)
269 MB_PUT(int64_t);
270 *p = htobe64(x);
271 return 0;
275 mb_put_int64le(struct mbdata *mbp, int64_t x)
277 MB_PUT(int64_t);
278 *p = htole64(x);
279 return 0;
283 mb_put_mem(struct mbdata *mbp, const char *source, size_t size)
285 struct mbuf *m;
286 char * dst;
287 size_t cplen;
288 int error;
290 if (size == 0)
291 return 0;
292 m = mbp->mb_cur;
293 if ((error = m_getm(m, size, &m)) != 0)
294 return error;
295 while (size > 0) {
296 cplen = M_TRAILINGSPACE(m);
297 if (cplen == 0) {
298 m = m->m_next;
299 continue;
301 if (cplen > size)
302 cplen = size;
303 dst = mtod(m, char *) + m->m_len;
304 if (source) {
305 bcopy(source, dst, cplen);
306 source += cplen;
307 } else
308 bzero(dst, cplen);
309 size -= cplen;
310 m->m_len += cplen;
311 mbp->mb_count += cplen;
313 mbp->mb_pos = mtod(m, char *) + m->m_len;
314 mbp->mb_cur = m;
315 return 0;
319 mb_put_mbuf(struct mbdata *mbp, struct mbuf *m)
321 mbp->mb_cur->m_next = m;
322 while (m) {
323 mbp->mb_count += m->m_len;
324 if (m->m_next == NULL)
325 break;
326 m = m->m_next;
328 mbp->mb_pos = mtod(m, char *) + m->m_len;
329 mbp->mb_cur = m;
330 return 0;
333 int
334 mb_put_pstring(struct mbdata *mbp, const char *s)
336 int error, len = strlen(s);
338 if (len > 255) {
339 len = 255;
341 if ((error = mb_put_uint8(mbp, len)) != 0)
342 return error;
343 return mb_put_mem(mbp, s, len);
347 * Routines for fetching data from an mbuf chain
349 #define mb_left(m,p) (mtod(m, char *) + (m)->m_len - (p))
352 mb_get_uint8(struct mbdata *mbp, u_int8_t *x)
354 return mb_get_mem(mbp, x, 1);
358 mb_get_uint16(struct mbdata *mbp, u_int16_t *x)
360 return mb_get_mem(mbp, (char *)x, 2);
364 mb_get_uint16le(struct mbdata *mbp, u_int16_t *x)
366 u_int16_t v;
367 int error = mb_get_uint16(mbp, &v);
369 *x = le16toh(v);
370 return error;
374 mb_get_uint16be(struct mbdata *mbp, u_int16_t *x) {
375 u_int16_t v;
376 int error = mb_get_uint16(mbp, &v);
378 *x = be16toh(v);
379 return error;
383 mb_get_uint32(struct mbdata *mbp, u_int32_t *x)
385 return mb_get_mem(mbp, (char *)x, 4);
389 mb_get_uint32be(struct mbdata *mbp, u_int32_t *x)
391 u_int32_t v;
392 int error;
394 error = mb_get_uint32(mbp, &v);
395 *x = be32toh(v);
396 return error;
400 mb_get_uint32le(struct mbdata *mbp, u_int32_t *x)
402 u_int32_t v;
403 int error;
405 error = mb_get_uint32(mbp, &v);
406 *x = le32toh(v);
407 return error;
411 mb_get_int64(struct mbdata *mbp, int64_t *x)
413 return mb_get_mem(mbp, (char *)x, 8);
417 mb_get_int64be(struct mbdata *mbp, int64_t *x)
419 int64_t v;
420 int error;
422 error = mb_get_int64(mbp, &v);
423 *x = be64toh(v);
424 return error;
428 mb_get_int64le(struct mbdata *mbp, int64_t *x)
430 int64_t v;
431 int error;
433 error = mb_get_int64(mbp, &v);
434 *x = le64toh(v);
435 return error;
439 mb_get_mem(struct mbdata *mbp, char * target, size_t size)
441 struct mbuf *m = mbp->mb_cur;
442 u_int count;
444 while (size > 0) {
445 if (m == NULL) {
446 MBERROR(("incomplete copy\n"));
447 return EBADRPC;
449 count = mb_left(m, mbp->mb_pos);
450 if (count == 0) {
451 mbp->mb_cur = m = m->m_next;
452 if (m)
453 mbp->mb_pos = mtod(m, char *);
454 continue;
456 if (count > size)
457 count = size;
458 size -= count;
459 if (target) {
460 if (count == 1) {
461 *target++ = *mbp->mb_pos;
462 } else {
463 bcopy(mbp->mb_pos, target, count);
464 target += count;
467 mbp->mb_pos += count;
469 return 0;