Initial import from FreeBSD RELENG_4:
[dragonfly.git] / sys / opencrypto / crmbuf.c
1 /*
2  * Copyright (c) 2002 Somebody. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
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. All advertising materials mentioning features or use of this software
13  *    must display the following acknowledgement:
14  *      This product includes software developed by the University of
15  *      California, Berkeley and its contributors.
16  * 4. Neither the name of the University nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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.
31  *
32  * $FreeBSD: src/sys/opencrypto/crmbuf.c,v 1.1.2.1 2002/11/21 23:34:23 sam Exp $
33  */
34
35 #include "opt_param.h"
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/mbuf.h>
39 #include <sys/kernel.h>
40
41 /*
42  * Miscellaneous mbuf routines that belong in uipc_mbuf.c but
43  * are kept private to the crypto support for now.
44  */
45 extern  int m_apply(struct mbuf *m, int off, int len,
46                     int (*f)(caddr_t, caddr_t, unsigned int), caddr_t fstate);
47 extern  struct mbuf * m_getptr(struct mbuf *m, int loc, int *off);
48
49 /*
50  * Apply function f to the data in an mbuf chain starting "off" bytes from the
51  * beginning, continuing for "len" bytes.
52  */
53 int
54 m_apply(struct mbuf *m, int off, int len, int (*f)(caddr_t, caddr_t, unsigned int), caddr_t fstate)
55 {
56         int rval;
57         unsigned int count;
58
59         if (len < 0)
60                 panic("m_apply: len %d < 0", len);
61         if (off < 0)
62                 panic("m_apply: off %d < 0", off);
63         while (off > 0) {
64                 if (m == NULL)
65                         panic("m_apply: null mbuf in skip");
66                 if (off < m->m_len)
67                         break;
68                 off -= m->m_len;
69                 m = m->m_next;
70         }
71         while (len > 0) {
72                 if (m == NULL)
73                         panic("m_apply: null mbuf");
74                 count = min(m->m_len - off, len);
75
76                 rval = f(fstate, mtod(m, caddr_t) + off, count);
77                 if (rval)
78                         return (rval);
79
80                 len -= count;
81                 off = 0;
82                 m = m->m_next;
83         }
84
85         return (0);
86 }
87
88 /*
89  * Return a pointer to mbuf/offset of location in mbuf chain.
90  */
91 struct mbuf *
92 m_getptr(struct mbuf *m, int loc, int *off)
93 {
94         while (loc >= 0) {
95                 /* Normal end of search */
96                 if (m->m_len > loc) {
97                         *off = loc;
98                         return (m);
99                 }
100                 else {
101                         loc -= m->m_len;
102
103                         if (m->m_next == NULL) {
104                                 if (loc == 0) {
105                                         /* Point at the end of valid data */
106                                         *off = m->m_len;
107                                         return (m);
108                                 }
109                                 else
110                                         return (NULL);
111                         }
112                         else
113                                 m = m->m_next;
114                 }
115         }
116
117         return (NULL);
118 }