xref: /titanic_41/usr/src/uts/common/inet/ipf/misc.c (revision ba2e4443695ee6a6f420a35cd4fc3d3346d22932)
1 /*
2  * Copyright (C) 2000 by Darren Reed.
3  */
4 #ifndef __hpux
5 #pragma ident "@(#)$Id: misc.c,v 1.9 2003/07/20 15:36:27 darrenr Exp $"
6 #else
7 struct uio;
8 #endif
9 
10 #include <sys/systm.h>
11 #include <sys/types.h>
12 #include <sys/stream.h>
13 
14 #include "compat.h"
15 
16 #ifdef	__hpux
17 # define	BCOPY(a,b,c)	bcopy((caddr_t)a, (caddr_t)b, c)
18 #endif
19 #ifdef	sun
20 # define	BCOPY(a,b,c)	bcopy((char *)a, (char *)b, c)
21 #endif
22 
23 void mb_copydata(min, off, len, buf)
24 mblk_t *min;
25 size_t off, len;
26 char *buf;
27 {
28 	u_char *s, *bp = (u_char *)buf;
29 	size_t mlen, olen, clen;
30 	mblk_t *m;
31 
32 	for (m = min; (m != NULL) && (len > 0); m = m->b_cont) {
33 		if (MTYPE(m) != M_DATA)
34 			continue;
35 		s = m->b_rptr;
36 		mlen = m->b_wptr - s;
37 		olen = MIN(off, mlen);
38 		if ((olen == mlen) || (olen < off)) {
39 			off -= olen;
40 			continue;
41 		} else if (olen) {
42 			off -= olen;
43 			s += olen;
44 			mlen -= olen;
45 		}
46 		clen = MIN(mlen, len);
47 		BCOPY(s, bp, clen);
48 		len -= clen;
49 		bp += clen;
50 	}
51 }
52 
53 
54 void mb_copyback(min, off, len, buf)
55 mblk_t *min;
56 size_t off, len;
57 char *buf;
58 {
59 	u_char *s, *bp = (u_char *)buf;
60 	size_t mlen, olen, clen;
61 	mblk_t *m, *mp;
62 
63 	for (m = min, mp = NULL; (m != NULL) && (len > 0); m = m->b_cont) {
64 		mp = m;
65 		if (MTYPE(m) != M_DATA)
66 			continue;
67 
68 		s = m->b_rptr;
69 		mlen = m->b_wptr - s;
70 		olen = MIN(off, mlen);
71 		if ((olen == mlen) || (olen < off)) {
72 			off -= olen;
73 			continue;
74 		} else if (olen) {
75 			off -= olen;
76 			s += olen;
77 			mlen -= olen;
78 		}
79 		clen = MIN(mlen, len);
80 		BCOPY(bp, s, clen);
81 		len -= clen;
82 		bp += clen;
83 	}
84 
85 	if ((m == NULL) && (mp != NULL)) {
86 		if (len > 0) {
87 			mlen = mp->b_datap->db_lim - mp->b_wptr;
88 			if (mlen > 0) {
89 				if (mlen > len)
90 					mlen = len;
91 				bcopy((char *)bp, (char *)mp->b_wptr, mlen);
92 				bp += mlen;
93 				len -= mlen;
94 				mp->b_wptr += mlen;
95 #ifdef  STRUIO_IP
96 # if SOLARIS2 < 10
97 				mp->b_datap->db_struiolim = mp->b_wptr;
98 # endif
99 				mp->b_datap->db_struioflag &= ~STRUIO_IP;
100 #endif
101 			}
102 		}
103 
104 		if (len > 0) {
105 			m = allocb(len, BPRI_MED);
106 			if (m != NULL) {
107 				bcopy((char *)bp, (char *)m->b_wptr, len);
108 				m->b_band = mp->b_band;
109 				m->b_wptr += len;
110 				linkb(mp, m);
111 			}
112 		}
113 	}
114 }
115