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 the above copyright notice and this paragraph are
7  * duplicated in all such forms and that any documentation,
8  * advertising materials, and other materials related to such
9  * distribution and use acknowledge that the software was developed
10  * by the University of California, Berkeley.  The name of the
11  * University may not be used to endorse or promote products derived
12  * from this software without specific prior written permission.
13  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
14  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
16  */
17 
18 #ifndef lint
19 static char sccsid[] = "@(#)genbsubs.c	4.1 (Berkeley) 12/04/88";
20 #endif /* not lint */
21 
22 /* The output of bunequal is the offset of the byte which didn't match;
23  * if all the bytes match, then we return n.
24  * bunequal(s1, s2, n) */
25 
26 int
27 bunequal(s1, s2, n)
28 register char *s1, *s2;
29 register n;
30 {
31     register int i = 0;
32 
33     while (i++ < n) {
34 	if (*s1++ != *s2++) {
35 	    break;
36 	}
37     }
38     return(i-1);
39 }
40 
41 /* bskip(s1, n, b) : finds the first occurrence of any byte != 'b' in the 'n'
42  * bytes beginning at 's1'.
43  */
44 
45 int
46 bskip(s1, n, b)
47 register char *s1;
48 register int n;
49 register int b;
50 {
51     register int i = 0;
52 
53     while (i++ < n) {
54 	if (*s1++ != b) {
55 	    break;
56 	}
57     }
58     return(i-1);
59 }
60 
61 /*
62  * memNSchr(const void *s, int c, size_t n, int and)
63  *
64  * Like memchr, but the comparison is '((*s)&and) == c',
65  * and we increment our way through s by "stride" ('s += stride').
66  *
67  * We optimize for the most used strides of +1 and -1.
68  */
69 
70 unsigned char *
71 memNSchr(s, c, n, and, stride)
72 char *s;
73 int c;
74 unsigned int n;
75 int and;
76 int stride;
77 {
78     register unsigned char _c, *_s, _and;
79 
80     _and = and;
81     _c = (c&_and);
82     _s = (unsigned char *)s;
83     switch (stride) {
84     case 1:
85 	while (n--) {
86 	    if (((*_s)&_and) == _c) {
87 		return _s;
88 	    }
89 	    _s++;
90 	}
91 	break;
92     case -1:
93 	while (n--) {
94 	    if (((*_s)&_and) == _c) {
95 		return _s;
96 	    }
97 	    _s--;
98 	}
99 	break;
100     default:
101 	while (n--) {
102 	    if (((*_s)&_and) == _c) {
103 		return _s;
104 	    }
105 	    _s += stride;
106 	}
107     }
108     return 0;
109 }
110