1 /*
2  *	Copyright (c) 1984-1987 by the Regents of the
3  *	University of California and by Gregory Glenn Minshall.
4  *
5  *	Permission to use, copy, modify, and distribute these
6  *	programs and their documentation for any purpose and
7  *	without fee is hereby granted, provided that this
8  *	copyright and permission appear on all copies and
9  *	supporting documentation, the name of the Regents of
10  *	the University of California not be used in advertising
11  *	or publicity pertaining to distribution of the programs
12  *	without specific prior permission, and notice be given in
13  *	supporting documentation that copying and distribution is
14  *	by permission of the Regents of the University of California
15  *	and by Gregory Glenn Minshall.  Neither the Regents of the
16  *	University of California nor Gregory Glenn Minshall make
17  *	representations about the suitability of this software
18  *	for any purpose.  It is provided "as is" without
19  *	express or implied warranty.
20  */
21 
22 #ifndef	lint
23 static char sccsid[] = "@(#)genbsubs.c	3.1 (Berkeley) 08/11/87";
24 #endif	/* ndef lint */
25 
26 /* The output of bunequal is the offset of the byte which didn't match;
27  * if all the bytes match, then we return n.
28  * bunequal(s1, s2, n) */
29 
30 int
31 bunequal(s1, s2, n)
32 register char *s1, *s2;
33 register n;
34 {
35     register int i = 0;
36 
37     while (i++ < n) {
38 	if (*s1++ != *s2++) {
39 	    break;
40 	}
41     }
42     return(i-1);
43 }
44 
45 /* bskip(s1, n, b) : finds the first occurrence of any byte != 'b' in the 'n'
46  * bytes beginning at 's1'.
47  */
48 
49 int
50 bskip(s1, n, b)
51 register char *s1;
52 register int n;
53 register int b;
54 {
55     register int i = 0;
56 
57     while (i++ < n) {
58 	if (*s1++ != b) {
59 	    break;
60 	}
61     }
62     return(i-1);
63 }
64 
65 /*
66  * memNSchr(const void *s, int c, size_t n, int and)
67  *
68  * Like memchr, but the comparison is '((*s)&and) == c',
69  * and we increment our way through s by "stride" ('s += stride').
70  *
71  * We optimize for the most used strides of +1 and -1.
72  */
73 
74 unsigned char *
75 memNSchr(s, c, n, and, stride)
76 char *s;
77 int c;
78 unsigned int n;
79 int and;
80 int stride;
81 {
82     register unsigned char _c, *_s, _and;
83 
84     _and = and;
85     _c = (c&_and);
86     _s = (unsigned char *)s;
87     switch (stride) {
88     case 1:
89 	while (n--) {
90 	    if (((*_s)&_and) == _c) {
91 		return _s;
92 	    }
93 	    _s++;
94 	}
95 	break;
96     case -1:
97 	while (n--) {
98 	    if (((*_s)&_and) == _c) {
99 		return _s;
100 	    }
101 	    _s--;
102 	}
103 	break;
104     default:
105 	while (n--) {
106 	    if (((*_s)&_and) == _c) {
107 		return _s;
108 	    }
109 	    _s += stride;
110 	}
111     }
112     return 0;
113 }
114