130014Sminshall /* 230014Sminshall * Copyright (c) 1984, 1985, 1986 by the Regents of the 330014Sminshall * University of California and by Gregory Glenn Minshall. 430014Sminshall * 530014Sminshall * Permission to use, copy, modify, and distribute these 630014Sminshall * programs and their documentation for any purpose and 730014Sminshall * without fee is hereby granted, provided that this 830014Sminshall * copyright and permission appear on all copies and 930014Sminshall * supporting documentation, the name of the Regents of 1030014Sminshall * the University of California not be used in advertising 1130014Sminshall * or publicity pertaining to distribution of the programs 1230014Sminshall * without specific prior permission, and notice be given in 1330014Sminshall * supporting documentation that copying and distribution is 1430014Sminshall * by permission of the Regents of the University of California 1530014Sminshall * and by Gregory Glenn Minshall. Neither the Regents of the 1630014Sminshall * University of California nor Gregory Glenn Minshall make 1730014Sminshall * representations about the suitability of this software 1830014Sminshall * for any purpose. It is provided "as is" without 1930014Sminshall * express or implied warranty. 2030014Sminshall */ 2130014Sminshall 2230014Sminshall #ifndef lint 2330014Sminshall static char sccsid[] = "@(#)genbsubs.c 3.1 10/29/86"; 2430014Sminshall #endif /* ndef lint */ 2530014Sminshall 2630014Sminshall /* The output of bunequal is the offset of the byte which didn't match; 2730014Sminshall * if all the bytes match, then we return n. 2830014Sminshall * bunequal(s1, s2, n) */ 2930014Sminshall 3030014Sminshall int 3130014Sminshall bunequal(s1, s2, n) 3230014Sminshall register char *s1, *s2; 3330014Sminshall register n; 3430014Sminshall { 3530014Sminshall register int i = 0; 3630014Sminshall 3730014Sminshall while (i++ < n) { 3830014Sminshall if (*s1++ != *s2++) { 3930014Sminshall break; 4030014Sminshall } 4130014Sminshall } 4230014Sminshall return(i-1); 4330014Sminshall } 4430014Sminshall 4530014Sminshall /* bskip(s1, n, b) : finds the first occurrence of any byte != 'b' in the 'n' 4630014Sminshall * bytes beginning at 's1'. 4730014Sminshall */ 4830014Sminshall 4930014Sminshall int 5030014Sminshall bskip(s1, n, b) 5130014Sminshall register char *s1; 5230014Sminshall register int n; 5330014Sminshall register int b; 5430014Sminshall { 5530014Sminshall register int i = 0; 5630014Sminshall 5730014Sminshall while (i++ < n) { 5830014Sminshall if (*s1++ != b) { 5930014Sminshall break; 6030014Sminshall } 6130014Sminshall } 6230014Sminshall return(i-1); 6330014Sminshall } 6431067Sminshall 6531067Sminshall /* 66*31070Sminshall * memNSchr(const void *s, int c, size_t n, int and) 6731067Sminshall * 6831067Sminshall * Like memchr, but the comparison is '((*s)&and) == c', 6931067Sminshall * and we increment our way through s by "stride" ('s += stride'). 7031067Sminshall * 7131067Sminshall * We optimize for the most used strides of +1 and -1. 7231067Sminshall */ 7331067Sminshall 7431067Sminshall unsigned char * 7531067Sminshall memNSchr(s, c, n, and, stride) 7631067Sminshall char *s; 7731067Sminshall int c; 7831067Sminshall unsigned int n; 7931067Sminshall int and; 8031067Sminshall int stride; 8131067Sminshall { 8231067Sminshall register unsigned char _c, *_s, _and; 8331067Sminshall 8431067Sminshall _and = and; 8531067Sminshall _c = (c&_and); 8631067Sminshall _s = (unsigned char *)s; 8731067Sminshall switch (stride) { 8831067Sminshall case 1: 8931067Sminshall while (n--) { 9031067Sminshall if (((*_s)&_and) == _c) { 9131067Sminshall return _s; 9231067Sminshall } 9331067Sminshall _s++; 9431067Sminshall } 9531067Sminshall break; 9631067Sminshall case -1: 9731067Sminshall while (n--) { 9831067Sminshall if (((*_s)&_and) == _c) { 9931067Sminshall return _s; 10031067Sminshall } 10131067Sminshall _s--; 10231067Sminshall } 10331067Sminshall break; 10431067Sminshall default: 10531067Sminshall while (n--) { 10631067Sminshall if (((*_s)&_and) == _c) { 10731067Sminshall return _s; 10831067Sminshall } 10931067Sminshall _s += stride; 11031067Sminshall } 11131067Sminshall } 11231067Sminshall return 0; 11331067Sminshall } 114