130014Sminshall /* 2*33815Sbostic * Copyright (c) 1988 Regents of the University of California. 3*33815Sbostic * All rights reserved. 430014Sminshall * 5*33815Sbostic * Redistribution and use in source and binary forms are permitted 6*33815Sbostic * provided that this notice is preserved and that due credit is given 7*33815Sbostic * to the University of California at Berkeley. The name of the University 8*33815Sbostic * may not be used to endorse or promote products derived from this 9*33815Sbostic * software without specific prior written permission. This software 10*33815Sbostic * is provided ``as is'' without express or implied warranty. 1130014Sminshall */ 1230014Sminshall 13*33815Sbostic #ifndef lint 14*33815Sbostic static char sccsid[] = "@(#)genbsubs.c 3.2 (Berkeley) 03/28/88"; 15*33815Sbostic #endif /* not lint */ 1630014Sminshall 1730014Sminshall /* The output of bunequal is the offset of the byte which didn't match; 1830014Sminshall * if all the bytes match, then we return n. 1930014Sminshall * bunequal(s1, s2, n) */ 2030014Sminshall 2130014Sminshall int 2230014Sminshall bunequal(s1, s2, n) 2330014Sminshall register char *s1, *s2; 2430014Sminshall register n; 2530014Sminshall { 2630014Sminshall register int i = 0; 2730014Sminshall 2830014Sminshall while (i++ < n) { 2930014Sminshall if (*s1++ != *s2++) { 3030014Sminshall break; 3130014Sminshall } 3230014Sminshall } 3330014Sminshall return(i-1); 3430014Sminshall } 3530014Sminshall 3630014Sminshall /* bskip(s1, n, b) : finds the first occurrence of any byte != 'b' in the 'n' 3730014Sminshall * bytes beginning at 's1'. 3830014Sminshall */ 3930014Sminshall 4030014Sminshall int 4130014Sminshall bskip(s1, n, b) 4230014Sminshall register char *s1; 4330014Sminshall register int n; 4430014Sminshall register int b; 4530014Sminshall { 4630014Sminshall register int i = 0; 4730014Sminshall 4830014Sminshall while (i++ < n) { 4930014Sminshall if (*s1++ != b) { 5030014Sminshall break; 5130014Sminshall } 5230014Sminshall } 5330014Sminshall return(i-1); 5430014Sminshall } 5531067Sminshall 5631067Sminshall /* 5731070Sminshall * memNSchr(const void *s, int c, size_t n, int and) 5831067Sminshall * 5931067Sminshall * Like memchr, but the comparison is '((*s)&and) == c', 6031067Sminshall * and we increment our way through s by "stride" ('s += stride'). 6131067Sminshall * 6231067Sminshall * We optimize for the most used strides of +1 and -1. 6331067Sminshall */ 6431067Sminshall 6531067Sminshall unsigned char * 6631067Sminshall memNSchr(s, c, n, and, stride) 6731067Sminshall char *s; 6831067Sminshall int c; 6931067Sminshall unsigned int n; 7031067Sminshall int and; 7131067Sminshall int stride; 7231067Sminshall { 7331067Sminshall register unsigned char _c, *_s, _and; 7431067Sminshall 7531067Sminshall _and = and; 7631067Sminshall _c = (c&_and); 7731067Sminshall _s = (unsigned char *)s; 7831067Sminshall switch (stride) { 7931067Sminshall case 1: 8031067Sminshall while (n--) { 8131067Sminshall if (((*_s)&_and) == _c) { 8231067Sminshall return _s; 8331067Sminshall } 8431067Sminshall _s++; 8531067Sminshall } 8631067Sminshall break; 8731067Sminshall case -1: 8831067Sminshall while (n--) { 8931067Sminshall if (((*_s)&_and) == _c) { 9031067Sminshall return _s; 9131067Sminshall } 9231067Sminshall _s--; 9331067Sminshall } 9431067Sminshall break; 9531067Sminshall default: 9631067Sminshall while (n--) { 9731067Sminshall if (((*_s)&_and) == _c) { 9831067Sminshall return _s; 9931067Sminshall } 10031067Sminshall _s += stride; 10131067Sminshall } 10231067Sminshall } 10331067Sminshall return 0; 10431067Sminshall } 105