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