blob: 7f46aef26ebc946f8959c30bb4b4d76ce154cf1d [file] [log] [blame]
whrb973f2b2000-05-05 19:34:50 +00001/*
2 * Copyright (c) 2000 Silicon Graphics, Inc. All Rights Reserved.
vapier45a8ba02009-07-20 10:59:32 +00003 *
whrb973f2b2000-05-05 19:34:50 +00004 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of version 2 of the GNU General Public License as
6 * published by the Free Software Foundation.
vapier45a8ba02009-07-20 10:59:32 +00007 *
whrb973f2b2000-05-05 19:34:50 +00008 * This program is distributed in the hope that it would be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
vapier45a8ba02009-07-20 10:59:32 +000011 *
whrb973f2b2000-05-05 19:34:50 +000012 * Further, this software is distributed without any warranty that it is
13 * free of the rightful claim of any third person regarding infringement
14 * or the like. Any license provided herein, whether implied or
15 * otherwise, applies only to this software file. Patent licenses, if
16 * any, provided herein do not apply to combinations of this program with
17 * other software, or any other product whatsoever.
vapier45a8ba02009-07-20 10:59:32 +000018 *
whrb973f2b2000-05-05 19:34:50 +000019 * You should have received a copy of the GNU General Public License along
Wanlong Gaofed96412012-10-24 10:10:29 +080020 * with this program; if not, write the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
vapier45a8ba02009-07-20 10:59:32 +000022 *
whrb973f2b2000-05-05 19:34:50 +000023 * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
24 * Mountain View, CA 94043, or:
vapier45a8ba02009-07-20 10:59:32 +000025 *
26 * http://www.sgi.com
27 *
28 * For further information regarding this notice, see:
29 *
whrb973f2b2000-05-05 19:34:50 +000030 * http://oss.sgi.com/projects/GenInfo/NoticeExplan/
31 */
alaffincc2e5552000-07-27 17:13:18 +000032#include <string.h>
33#include "pattern.h"
34
whrb973f2b2000-05-05 19:34:50 +000035/*
36 * The routines in this module are used to fill/check a data buffer
37 * with/against a known pattern.
38 */
39
Stanislav Kholmanskikh87aa2dc2014-04-11 11:45:19 +040040int pattern_check(char *buf, int buflen, char *pat, int patlen, int patshift)
whrb973f2b2000-05-05 19:34:50 +000041{
Wanlong Gao354ebb42012-12-07 10:10:04 +080042 int nb, ncmp, nleft;
43 char *cp;
whrb973f2b2000-05-05 19:34:50 +000044
Wanlong Gao354ebb42012-12-07 10:10:04 +080045 if (patlen)
46 patshift = patshift % patlen;
whrb973f2b2000-05-05 19:34:50 +000047
Wanlong Gao354ebb42012-12-07 10:10:04 +080048 cp = buf;
49 nleft = buflen;
whrb973f2b2000-05-05 19:34:50 +000050
Wanlong Gao354ebb42012-12-07 10:10:04 +080051 /*
52 * The following 2 blocks of code are to compare the first patlen
53 * bytes of buf. We need 2 checks if patshift is > 0 since we
54 * must check the last (patlen - patshift) bytes, and then the
55 * first (patshift) bytes.
56 */
whrb973f2b2000-05-05 19:34:50 +000057
Wanlong Gao354ebb42012-12-07 10:10:04 +080058 nb = patlen - patshift;
whrb973f2b2000-05-05 19:34:50 +000059 if (nleft < nb) {
Wanlong Gao354ebb42012-12-07 10:10:04 +080060 return (memcmp(cp, pat + patshift, nleft) ? -1 : 0);
whrb973f2b2000-05-05 19:34:50 +000061 } else {
Wanlong Gao354ebb42012-12-07 10:10:04 +080062 if (memcmp(cp, pat + patshift, nb))
63 return -1;
whrb973f2b2000-05-05 19:34:50 +000064
Wanlong Gao354ebb42012-12-07 10:10:04 +080065 nleft -= nb;
66 cp += nb;
whrb973f2b2000-05-05 19:34:50 +000067 }
whrb973f2b2000-05-05 19:34:50 +000068
Wanlong Gao354ebb42012-12-07 10:10:04 +080069 if (patshift > 0) {
70 nb = patshift;
71 if (nleft < nb) {
72 return (memcmp(cp, pat, nleft) ? -1 : 0);
73 } else {
74 if (memcmp(cp, pat, nb))
75 return -1;
whrb973f2b2000-05-05 19:34:50 +000076
Wanlong Gao354ebb42012-12-07 10:10:04 +080077 nleft -= nb;
78 cp += nb;
79 }
80 }
whrb973f2b2000-05-05 19:34:50 +000081
Wanlong Gao354ebb42012-12-07 10:10:04 +080082 /*
83 * Now, verify the rest of the buffer using the algorithm described
84 * in the function header.
85 */
whrb973f2b2000-05-05 19:34:50 +000086
Wanlong Gao354ebb42012-12-07 10:10:04 +080087 ncmp = cp - buf;
88 while (ncmp < buflen) {
89 nb = (ncmp < nleft) ? ncmp : nleft;
90 if (memcmp(buf, cp, nb))
91 return -1;
92
93 cp += nb;
94 ncmp += nb;
95 nleft -= nb;
96 }
97
98 return 0;
whrb973f2b2000-05-05 19:34:50 +000099}
100
Stanislav Kholmanskikh87aa2dc2014-04-11 11:45:19 +0400101int pattern_fill(char *buf, int buflen, char *pat, int patlen, int patshift)
whrb973f2b2000-05-05 19:34:50 +0000102{
Wanlong Gao354ebb42012-12-07 10:10:04 +0800103 int trans, ncopied, nleft;
104 char *cp;
whrb973f2b2000-05-05 19:34:50 +0000105
Wanlong Gao354ebb42012-12-07 10:10:04 +0800106 if (patlen)
107 patshift = patshift % patlen;
whrb973f2b2000-05-05 19:34:50 +0000108
Wanlong Gao354ebb42012-12-07 10:10:04 +0800109 cp = buf;
110 nleft = buflen;
whrb973f2b2000-05-05 19:34:50 +0000111
Wanlong Gao354ebb42012-12-07 10:10:04 +0800112 /*
113 * The following 2 blocks of code are to fill the first patlen
114 * bytes of buf. We need 2 sections if patshift is > 0 since we
115 * must first copy the last (patlen - patshift) bytes into buf[0]...,
116 * and then the first (patshift) bytes of pattern following them.
117 */
whrb973f2b2000-05-05 19:34:50 +0000118
Wanlong Gao354ebb42012-12-07 10:10:04 +0800119 trans = patlen - patshift;
whrb973f2b2000-05-05 19:34:50 +0000120 if (nleft < trans) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800121 memcpy(cp, pat + patshift, nleft);
122 return 0;
whrb973f2b2000-05-05 19:34:50 +0000123 } else {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800124 memcpy(cp, pat + patshift, trans);
125 nleft -= trans;
126 cp += trans;
whrb973f2b2000-05-05 19:34:50 +0000127 }
whrb973f2b2000-05-05 19:34:50 +0000128
Wanlong Gao354ebb42012-12-07 10:10:04 +0800129 if (patshift > 0) {
130 trans = patshift;
131 if (nleft < trans) {
132 memcpy(cp, pat, nleft);
133 return 0;
134 } else {
135 memcpy(cp, pat, trans);
136 nleft -= trans;
137 cp += trans;
138 }
139 }
whrb973f2b2000-05-05 19:34:50 +0000140
Wanlong Gao354ebb42012-12-07 10:10:04 +0800141 /*
142 * Now, fill the rest of the buffer using the algorithm described
143 * in the function header comment.
144 */
whrb973f2b2000-05-05 19:34:50 +0000145
Wanlong Gao354ebb42012-12-07 10:10:04 +0800146 ncopied = cp - buf;
147 while (ncopied < buflen) {
148 trans = (ncopied < nleft) ? ncopied : nleft;
149 memcpy(cp, buf, trans);
150 cp += trans;
151 ncopied += trans;
152 nleft -= trans;
153 }
154
155 return (0);
Chris Dearmanec6edca2012-10-17 19:54:01 -0700156}