xref: /aosp_15_r20/external/ltp/testcases/kernel/fs/doio/pattern.c (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
1*49cdfc7eSAndroid Build Coastguard Worker /*
2*49cdfc7eSAndroid Build Coastguard Worker  * Copyright (c) 2000 Silicon Graphics, Inc.  All Rights Reserved.
3*49cdfc7eSAndroid Build Coastguard Worker  *
4*49cdfc7eSAndroid Build Coastguard Worker  * This program is free software; you can redistribute it and/or modify it
5*49cdfc7eSAndroid Build Coastguard Worker  * under the terms of version 2 of the GNU General Public License as
6*49cdfc7eSAndroid Build Coastguard Worker  * published by the Free Software Foundation.
7*49cdfc7eSAndroid Build Coastguard Worker  *
8*49cdfc7eSAndroid Build Coastguard Worker  * This program is distributed in the hope that it would be useful, but
9*49cdfc7eSAndroid Build Coastguard Worker  * WITHOUT ANY WARRANTY; without even the implied warranty of
10*49cdfc7eSAndroid Build Coastguard Worker  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11*49cdfc7eSAndroid Build Coastguard Worker  *
12*49cdfc7eSAndroid Build Coastguard Worker  * Further, this software is distributed without any warranty that it is
13*49cdfc7eSAndroid Build Coastguard Worker  * free of the rightful claim of any third person regarding infringement
14*49cdfc7eSAndroid Build Coastguard Worker  * or the like.  Any license provided herein, whether implied or
15*49cdfc7eSAndroid Build Coastguard Worker  * otherwise, applies only to this software file.  Patent licenses, if
16*49cdfc7eSAndroid Build Coastguard Worker  * any, provided herein do not apply to combinations of this program with
17*49cdfc7eSAndroid Build Coastguard Worker  * other software, or any other product whatsoever.
18*49cdfc7eSAndroid Build Coastguard Worker  *
19*49cdfc7eSAndroid Build Coastguard Worker  * You should have received a copy of the GNU General Public License along
20*49cdfc7eSAndroid Build Coastguard Worker  * with this program; if not, write the Free Software Foundation, Inc.,
21*49cdfc7eSAndroid Build Coastguard Worker  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22*49cdfc7eSAndroid Build Coastguard Worker  *
23*49cdfc7eSAndroid Build Coastguard Worker  * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
24*49cdfc7eSAndroid Build Coastguard Worker  * Mountain View, CA  94043, or:
25*49cdfc7eSAndroid Build Coastguard Worker  *
26*49cdfc7eSAndroid Build Coastguard Worker  * http://www.sgi.com
27*49cdfc7eSAndroid Build Coastguard Worker  *
28*49cdfc7eSAndroid Build Coastguard Worker  * For further information regarding this notice, see:
29*49cdfc7eSAndroid Build Coastguard Worker  *
30*49cdfc7eSAndroid Build Coastguard Worker  * http://oss.sgi.com/projects/GenInfo/NoticeExplan/
31*49cdfc7eSAndroid Build Coastguard Worker  */
32*49cdfc7eSAndroid Build Coastguard Worker #include <string.h>
33*49cdfc7eSAndroid Build Coastguard Worker #include "pattern.h"
34*49cdfc7eSAndroid Build Coastguard Worker 
35*49cdfc7eSAndroid Build Coastguard Worker /*
36*49cdfc7eSAndroid Build Coastguard Worker  * The routines in this module are used to fill/check a data buffer
37*49cdfc7eSAndroid Build Coastguard Worker  * with/against a known pattern.
38*49cdfc7eSAndroid Build Coastguard Worker  */
39*49cdfc7eSAndroid Build Coastguard Worker 
pattern_check(char * buf,int buflen,char * pat,int patlen,int patshift)40*49cdfc7eSAndroid Build Coastguard Worker int pattern_check(char *buf, int buflen, char *pat, int patlen, int patshift)
41*49cdfc7eSAndroid Build Coastguard Worker {
42*49cdfc7eSAndroid Build Coastguard Worker 	int nb, ncmp, nleft;
43*49cdfc7eSAndroid Build Coastguard Worker 	char *cp;
44*49cdfc7eSAndroid Build Coastguard Worker 
45*49cdfc7eSAndroid Build Coastguard Worker 	if (patlen)
46*49cdfc7eSAndroid Build Coastguard Worker 		patshift = patshift % patlen;
47*49cdfc7eSAndroid Build Coastguard Worker 
48*49cdfc7eSAndroid Build Coastguard Worker 	cp = buf;
49*49cdfc7eSAndroid Build Coastguard Worker 	nleft = buflen;
50*49cdfc7eSAndroid Build Coastguard Worker 
51*49cdfc7eSAndroid Build Coastguard Worker 	/*
52*49cdfc7eSAndroid Build Coastguard Worker 	 * The following 2 blocks of code are to compare the first patlen
53*49cdfc7eSAndroid Build Coastguard Worker 	 * bytes of buf.  We need 2 checks if patshift is > 0 since we
54*49cdfc7eSAndroid Build Coastguard Worker 	 * must check the last (patlen - patshift) bytes, and then the
55*49cdfc7eSAndroid Build Coastguard Worker 	 * first (patshift) bytes.
56*49cdfc7eSAndroid Build Coastguard Worker 	 */
57*49cdfc7eSAndroid Build Coastguard Worker 
58*49cdfc7eSAndroid Build Coastguard Worker 	nb = patlen - patshift;
59*49cdfc7eSAndroid Build Coastguard Worker 	if (nleft < nb) {
60*49cdfc7eSAndroid Build Coastguard Worker 		return (memcmp(cp, pat + patshift, nleft) ? -1 : 0);
61*49cdfc7eSAndroid Build Coastguard Worker 	} else {
62*49cdfc7eSAndroid Build Coastguard Worker 		if (memcmp(cp, pat + patshift, nb))
63*49cdfc7eSAndroid Build Coastguard Worker 			return -1;
64*49cdfc7eSAndroid Build Coastguard Worker 
65*49cdfc7eSAndroid Build Coastguard Worker 		nleft -= nb;
66*49cdfc7eSAndroid Build Coastguard Worker 		cp += nb;
67*49cdfc7eSAndroid Build Coastguard Worker 	}
68*49cdfc7eSAndroid Build Coastguard Worker 
69*49cdfc7eSAndroid Build Coastguard Worker 	if (patshift > 0) {
70*49cdfc7eSAndroid Build Coastguard Worker 		nb = patshift;
71*49cdfc7eSAndroid Build Coastguard Worker 		if (nleft < nb) {
72*49cdfc7eSAndroid Build Coastguard Worker 			return (memcmp(cp, pat, nleft) ? -1 : 0);
73*49cdfc7eSAndroid Build Coastguard Worker 		} else {
74*49cdfc7eSAndroid Build Coastguard Worker 			if (memcmp(cp, pat, nb))
75*49cdfc7eSAndroid Build Coastguard Worker 				return -1;
76*49cdfc7eSAndroid Build Coastguard Worker 
77*49cdfc7eSAndroid Build Coastguard Worker 			nleft -= nb;
78*49cdfc7eSAndroid Build Coastguard Worker 			cp += nb;
79*49cdfc7eSAndroid Build Coastguard Worker 		}
80*49cdfc7eSAndroid Build Coastguard Worker 	}
81*49cdfc7eSAndroid Build Coastguard Worker 
82*49cdfc7eSAndroid Build Coastguard Worker 	/*
83*49cdfc7eSAndroid Build Coastguard Worker 	 * Now, verify the rest of the buffer using the algorithm described
84*49cdfc7eSAndroid Build Coastguard Worker 	 * in the function header.
85*49cdfc7eSAndroid Build Coastguard Worker 	 */
86*49cdfc7eSAndroid Build Coastguard Worker 
87*49cdfc7eSAndroid Build Coastguard Worker 	ncmp = cp - buf;
88*49cdfc7eSAndroid Build Coastguard Worker 	while (ncmp < buflen) {
89*49cdfc7eSAndroid Build Coastguard Worker 		nb = (ncmp < nleft) ? ncmp : nleft;
90*49cdfc7eSAndroid Build Coastguard Worker 		if (memcmp(buf, cp, nb))
91*49cdfc7eSAndroid Build Coastguard Worker 			return -1;
92*49cdfc7eSAndroid Build Coastguard Worker 
93*49cdfc7eSAndroid Build Coastguard Worker 		cp += nb;
94*49cdfc7eSAndroid Build Coastguard Worker 		ncmp += nb;
95*49cdfc7eSAndroid Build Coastguard Worker 		nleft -= nb;
96*49cdfc7eSAndroid Build Coastguard Worker 	}
97*49cdfc7eSAndroid Build Coastguard Worker 
98*49cdfc7eSAndroid Build Coastguard Worker 	return 0;
99*49cdfc7eSAndroid Build Coastguard Worker }
100*49cdfc7eSAndroid Build Coastguard Worker 
pattern_fill(char * buf,int buflen,char * pat,int patlen,int patshift)101*49cdfc7eSAndroid Build Coastguard Worker int pattern_fill(char *buf, int buflen, char *pat, int patlen, int patshift)
102*49cdfc7eSAndroid Build Coastguard Worker {
103*49cdfc7eSAndroid Build Coastguard Worker 	int trans, ncopied, nleft;
104*49cdfc7eSAndroid Build Coastguard Worker 	char *cp;
105*49cdfc7eSAndroid Build Coastguard Worker 
106*49cdfc7eSAndroid Build Coastguard Worker 	if (patlen)
107*49cdfc7eSAndroid Build Coastguard Worker 		patshift = patshift % patlen;
108*49cdfc7eSAndroid Build Coastguard Worker 
109*49cdfc7eSAndroid Build Coastguard Worker 	cp = buf;
110*49cdfc7eSAndroid Build Coastguard Worker 	nleft = buflen;
111*49cdfc7eSAndroid Build Coastguard Worker 
112*49cdfc7eSAndroid Build Coastguard Worker 	/*
113*49cdfc7eSAndroid Build Coastguard Worker 	 * The following 2 blocks of code are to fill the first patlen
114*49cdfc7eSAndroid Build Coastguard Worker 	 * bytes of buf.  We need 2 sections if patshift is > 0 since we
115*49cdfc7eSAndroid Build Coastguard Worker 	 * must first copy the last (patlen - patshift) bytes into buf[0]...,
116*49cdfc7eSAndroid Build Coastguard Worker 	 * and then the first (patshift) bytes of pattern following them.
117*49cdfc7eSAndroid Build Coastguard Worker 	 */
118*49cdfc7eSAndroid Build Coastguard Worker 
119*49cdfc7eSAndroid Build Coastguard Worker 	trans = patlen - patshift;
120*49cdfc7eSAndroid Build Coastguard Worker 	if (nleft < trans) {
121*49cdfc7eSAndroid Build Coastguard Worker 		memcpy(cp, pat + patshift, nleft);
122*49cdfc7eSAndroid Build Coastguard Worker 		return 0;
123*49cdfc7eSAndroid Build Coastguard Worker 	} else {
124*49cdfc7eSAndroid Build Coastguard Worker 		memcpy(cp, pat + patshift, trans);
125*49cdfc7eSAndroid Build Coastguard Worker 		nleft -= trans;
126*49cdfc7eSAndroid Build Coastguard Worker 		cp += trans;
127*49cdfc7eSAndroid Build Coastguard Worker 	}
128*49cdfc7eSAndroid Build Coastguard Worker 
129*49cdfc7eSAndroid Build Coastguard Worker 	if (patshift > 0) {
130*49cdfc7eSAndroid Build Coastguard Worker 		trans = patshift;
131*49cdfc7eSAndroid Build Coastguard Worker 		if (nleft < trans) {
132*49cdfc7eSAndroid Build Coastguard Worker 			memcpy(cp, pat, nleft);
133*49cdfc7eSAndroid Build Coastguard Worker 			return 0;
134*49cdfc7eSAndroid Build Coastguard Worker 		} else {
135*49cdfc7eSAndroid Build Coastguard Worker 			memcpy(cp, pat, trans);
136*49cdfc7eSAndroid Build Coastguard Worker 			nleft -= trans;
137*49cdfc7eSAndroid Build Coastguard Worker 			cp += trans;
138*49cdfc7eSAndroid Build Coastguard Worker 		}
139*49cdfc7eSAndroid Build Coastguard Worker 	}
140*49cdfc7eSAndroid Build Coastguard Worker 
141*49cdfc7eSAndroid Build Coastguard Worker 	/*
142*49cdfc7eSAndroid Build Coastguard Worker 	 * Now, fill the rest of the buffer using the algorithm described
143*49cdfc7eSAndroid Build Coastguard Worker 	 * in the function header comment.
144*49cdfc7eSAndroid Build Coastguard Worker 	 */
145*49cdfc7eSAndroid Build Coastguard Worker 
146*49cdfc7eSAndroid Build Coastguard Worker 	ncopied = cp - buf;
147*49cdfc7eSAndroid Build Coastguard Worker 	while (ncopied < buflen) {
148*49cdfc7eSAndroid Build Coastguard Worker 		trans = (ncopied < nleft) ? ncopied : nleft;
149*49cdfc7eSAndroid Build Coastguard Worker 		memcpy(cp, buf, trans);
150*49cdfc7eSAndroid Build Coastguard Worker 		cp += trans;
151*49cdfc7eSAndroid Build Coastguard Worker 		ncopied += trans;
152*49cdfc7eSAndroid Build Coastguard Worker 		nleft -= trans;
153*49cdfc7eSAndroid Build Coastguard Worker 	}
154*49cdfc7eSAndroid Build Coastguard Worker 
155*49cdfc7eSAndroid Build Coastguard Worker 	return (0);
156*49cdfc7eSAndroid Build Coastguard Worker }
157