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