whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2000 Silicon Graphics, Inc. All Rights Reserved. |
vapier | 45a8ba0 | 2009-07-20 10:59:32 +0000 | [diff] [blame] | 3 | * |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 4 | * 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. |
vapier | 45a8ba0 | 2009-07-20 10:59:32 +0000 | [diff] [blame] | 7 | * |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 8 | * 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. |
vapier | 45a8ba0 | 2009-07-20 10:59:32 +0000 | [diff] [blame] | 11 | * |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 12 | * 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. |
vapier | 45a8ba0 | 2009-07-20 10:59:32 +0000 | [diff] [blame] | 18 | * |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 19 | * You should have received a copy of the GNU General Public License along |
Wanlong Gao | fed9641 | 2012-10-24 10:10:29 +0800 | [diff] [blame] | 20 | * with this program; if not, write the Free Software Foundation, Inc., |
| 21 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
vapier | 45a8ba0 | 2009-07-20 10:59:32 +0000 | [diff] [blame] | 22 | * |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 23 | * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy, |
| 24 | * Mountain View, CA 94043, or: |
vapier | 45a8ba0 | 2009-07-20 10:59:32 +0000 | [diff] [blame] | 25 | * |
| 26 | * http://www.sgi.com |
| 27 | * |
| 28 | * For further information regarding this notice, see: |
| 29 | * |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 30 | * http://oss.sgi.com/projects/GenInfo/NoticeExplan/ |
| 31 | */ |
alaffin | cc2e555 | 2000-07-27 17:13:18 +0000 | [diff] [blame] | 32 | #include <string.h> |
| 33 | #include "pattern.h" |
| 34 | |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 35 | /* |
| 36 | * The routines in this module are used to fill/check a data buffer |
| 37 | * with/against a known pattern. |
| 38 | */ |
| 39 | |
Stanislav Kholmanskikh | 87aa2dc | 2014-04-11 11:45:19 +0400 | [diff] [blame] | 40 | int pattern_check(char *buf, int buflen, char *pat, int patlen, int patshift) |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 41 | { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 42 | int nb, ncmp, nleft; |
| 43 | char *cp; |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 44 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 45 | if (patlen) |
| 46 | patshift = patshift % patlen; |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 47 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 48 | cp = buf; |
| 49 | nleft = buflen; |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 50 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 51 | /* |
| 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 | */ |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 57 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 58 | nb = patlen - patshift; |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 59 | if (nleft < nb) { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 60 | return (memcmp(cp, pat + patshift, nleft) ? -1 : 0); |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 61 | } else { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 62 | if (memcmp(cp, pat + patshift, nb)) |
| 63 | return -1; |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 64 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 65 | nleft -= nb; |
| 66 | cp += nb; |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 67 | } |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 68 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 69 | 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; |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 76 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 77 | nleft -= nb; |
| 78 | cp += nb; |
| 79 | } |
| 80 | } |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 81 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 82 | /* |
| 83 | * Now, verify the rest of the buffer using the algorithm described |
| 84 | * in the function header. |
| 85 | */ |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 86 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 87 | 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; |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 99 | } |
| 100 | |
Stanislav Kholmanskikh | 87aa2dc | 2014-04-11 11:45:19 +0400 | [diff] [blame] | 101 | int pattern_fill(char *buf, int buflen, char *pat, int patlen, int patshift) |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 102 | { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 103 | int trans, ncopied, nleft; |
| 104 | char *cp; |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 105 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 106 | if (patlen) |
| 107 | patshift = patshift % patlen; |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 108 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 109 | cp = buf; |
| 110 | nleft = buflen; |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 111 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 112 | /* |
| 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 | */ |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 118 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 119 | trans = patlen - patshift; |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 120 | if (nleft < trans) { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 121 | memcpy(cp, pat + patshift, nleft); |
| 122 | return 0; |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 123 | } else { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 124 | memcpy(cp, pat + patshift, trans); |
| 125 | nleft -= trans; |
| 126 | cp += trans; |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 127 | } |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 128 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 129 | 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 | } |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 140 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 141 | /* |
| 142 | * Now, fill the rest of the buffer using the algorithm described |
| 143 | * in the function header comment. |
| 144 | */ |
whr | b973f2b | 2000-05-05 19:34:50 +0000 | [diff] [blame] | 145 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 146 | 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 Dearman | ec6edca | 2012-10-17 19:54:01 -0700 | [diff] [blame] | 156 | } |