1 /*
2  * Copyright (c) 1988 Regents of the University of California.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms are permitted
6  * provided that this notice is preserved and that due credit is given
7  * to the University of California at Berkeley. The name of the University
8  * may not be used to endorse or promote products derived from this
9  * software without specific prior written permission. This software
10  * is provided ``as is'' without express or implied warranty.
11  */
12 
13 #ifndef lint
14 static char sccsid[] = "@(#)genbsubs.c	3.2 (Berkeley) 03/28/88";
15 #endif /* not lint */
16 
17 /* The output of bunequal is the offset of the byte which didn't match;
18  * if all the bytes match, then we return n.
19  * bunequal(s1, s2, n) */
20 
21 int
22 bunequal(s1, s2, n)
23 register char *s1, *s2;
24 register n;
25 {
26     register int i = 0;
27 
28     while (i++ < n) {
29 	if (*s1++ != *s2++) {
30 	    break;
31 	}
32     }
33     return(i-1);
34 }
35 
36 /* bskip(s1, n, b) : finds the first occurrence of any byte != 'b' in the 'n'
37  * bytes beginning at 's1'.
38  */
39 
40 int
41 bskip(s1, n, b)
42 register char *s1;
43 register int n;
44 register int b;
45 {
46     register int i = 0;
47 
48     while (i++ < n) {
49 	if (*s1++ != b) {
50 	    break;
51 	}
52     }
53     return(i-1);
54 }
55 
56 /*
57  * memNSchr(const void *s, int c, size_t n, int and)
58  *
59  * Like memchr, but the comparison is '((*s)&and) == c',
60  * and we increment our way through s by "stride" ('s += stride').
61  *
62  * We optimize for the most used strides of +1 and -1.
63  */
64 
65 unsigned char *
66 memNSchr(s, c, n, and, stride)
67 char *s;
68 int c;
69 unsigned int n;
70 int and;
71 int stride;
72 {
73     register unsigned char _c, *_s, _and;
74 
75     _and = and;
76     _c = (c&_and);
77     _s = (unsigned char *)s;
78     switch (stride) {
79     case 1:
80 	while (n--) {
81 	    if (((*_s)&_and) == _c) {
82 		return _s;
83 	    }
84 	    _s++;
85 	}
86 	break;
87     case -1:
88 	while (n--) {
89 	    if (((*_s)&_and) == _c) {
90 		return _s;
91 	    }
92 	    _s--;
93 	}
94 	break;
95     default:
96 	while (n--) {
97 	    if (((*_s)&_and) == _c) {
98 		return _s;
99 	    }
100 	    _s += stride;
101 	}
102     }
103     return 0;
104 }
105