Home | History | Annotate | Line # | Download | only in string
bm.c revision 1.4.2.1
      1 /*-
      2  * Copyright (c) 1994
      3  *	The Regents of the University of California.  All rights reserved.
      4  *
      5  * This code is derived from software contributed to Berkeley by
      6  * Andrew Hume of AT&T Bell Laboratories.
      7  *
      8  * Redistribution and use in source and binary forms, with or without
      9  * modification, are permitted provided that the following conditions
     10  * are met:
     11  * 1. Redistributions of source code must retain the above copyright
     12  *    notice, this list of conditions and the following disclaimer.
     13  * 2. Redistributions in binary form must reproduce the above copyright
     14  *    notice, this list of conditions and the following disclaimer in the
     15  *    documentation and/or other materials provided with the distribution.
     16  * 3. All advertising materials mentioning features or use of this software
     17  *    must display the following acknowledgement:
     18  *	This product includes software developed by the University of
     19  *	California, Berkeley and its contributors.
     20  * 4. Neither the name of the University nor the names of its contributors
     21  *    may be used to endorse or promote products derived from this software
     22  *    without specific prior written permission.
     23  *
     24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     34  * SUCH DAMAGE.
     35  */
     36 
     37 #if defined(LIBC_SCCS) && !defined(lint)
     38 /* from: static char sccsid[] = "@(#)bm.c	8.7 (Berkeley) 6/21/94"; */
     39 static char *rcsid = "$Id: bm.c,v 1.4.2.1 1996/09/20 17:00:58 jtc Exp $";
     40 #endif /* LIBC_SCCS && not lint */
     41 
     42 #include "namespace.h"
     43 #include <sys/types.h>
     44 
     45 #include <bm.h>
     46 #include <errno.h>
     47 #include <stdlib.h>
     48 #include <string.h>
     49 
     50 #ifdef __weak_alias
     51 __weak_alias(bm_comp,_bm_comp);
     52 __weak_alias(bm_exec,_bm_exec);
     53 __weak_alias(bm_free,_bm_free);
     54 #endif
     55 
     56 /*
     57  * XXX
     58  * The default frequency table starts at 99 and counts down.  The default
     59  * table should probably be oriented toward text, and will necessarily be
     60  * locale specific.  This one is for English.  It was derived from the
     61  * OSF/1 and 4.4BSD formatted and unformatted manual pages, and about 100Mb
     62  * of email and random text.  Change it if you can find something better.
     63  */
     64 static u_char const freq_def[256] = {
     65 	 0,  0,  0,  0,  0,  0,  0,  0,
     66 	 0, 77, 90,  0,  0,  0,  0,  0,
     67 	 0,  0,  0,  0,  0,  0,  0,  0,
     68 	 0,  0,  0,  0,  0,  0,  0,  0,
     69 	99, 28, 42, 27, 16, 14, 20, 51,
     70 	66, 65, 59, 24, 75, 76, 84, 56,
     71 	72, 74, 64, 55, 54, 47, 41, 37,
     72 	44, 61, 70, 43, 23, 53, 49, 22,
     73 	33, 58, 40, 46, 45, 57, 60, 26,
     74 	30, 63, 21, 12, 32, 50, 38, 39,
     75 	34, 11, 48, 67, 62, 35, 15, 29,
     76 	71, 18,  9, 17, 25, 13, 10, 52,
     77 	36, 95, 78, 86, 87, 98, 82, 80,
     78 	88, 94, 19, 68, 89, 83, 93, 96,
     79 	81,  7, 91, 92, 97, 85, 69, 73,
     80 	31, 79,  8,  5,  4,  6,  3,  0,
     81 	 0,  0,  0,  0,  0,  0,  0,  0,
     82 	 0,  0,  0,  0,  0,  0,  0,  0,
     83 	 0,  0,  0,  0,  0,  0,  0,  0,
     84 	 0,  0,  0,  0,  0,  0,  0,  0,
     85 	 0,  0,  0,  0,  0,  0,  0,  0,
     86 	 0,  0,  0,  0,  0,  0,  0,  0,
     87 	 0,  0,  0,  0,  0,  0,  0,  0,
     88 	 0,  0,  0,  0,  0,  0,  0,  0,
     89 	 0,  0,  0,  0,  0,  0,  0,  0,
     90 	 0,  0,  0,  0,  0,  0,  0,  0,
     91 	 0,  0,  0,  0,  0,  0,  0,  0,
     92 	 0,  0,  0,  0,  0,  0,  0,  0,
     93 	 0,  0,  0,  0,  0,  0,  0,  0,
     94 	 0,  0,  0,  0,  0,  0,  0,  0,
     95 	 0,  0,  0,  0,  0,  0,  0,  0,
     96 	 0,  0,  0,  0,  0,  0,  0,  0,
     97 };
     98 
     99 bm_pat *
    100 bm_comp(pb, len, freq)
    101 	u_char const *pb;
    102 	size_t len;
    103 	u_char const *freq;
    104 {
    105 	register u_char const *pe, *p;
    106 	register size_t *d, r;
    107 	register int j;
    108 	int sv_errno;
    109 	bm_pat *pat;
    110 
    111 	if (len == 0) {
    112 		errno = EINVAL;
    113 		return (NULL);
    114 	}
    115 	if ((pat = malloc(sizeof(*pat))) == NULL)
    116 		return (NULL);
    117 	pat->pat = NULL;
    118 	pat->delta = NULL;
    119 
    120 	pat->patlen = len;			/* copy pattern */
    121 	if ((pat->pat = malloc(pat->patlen)) == NULL)
    122 		goto mem;
    123 	memcpy(pat->pat, pb, pat->patlen);
    124 						/* get skip delta */
    125 	if ((pat->delta = malloc(256 * sizeof(*d))) == NULL)
    126 		goto mem;
    127 	for (j = 0, d = pat->delta; j < 256; j++)
    128 		d[j] = pat->patlen;
    129 	for (pe = pb + pat->patlen - 1; pb <= pe; pb++)
    130 		d[*pb] = pe - pb;
    131 
    132 	if (freq == NULL)			/* default freq table */
    133 		freq = freq_def;
    134 	r = 0;					/* get guard */
    135 	for (pb = pat->pat, pe = pb + pat->patlen - 1; pb < pe; pb++)
    136 		if (freq[*pb] < freq[pat->pat[r]])
    137 			r = pb - pat->pat;
    138 	pat->rarec = pat->pat[r];
    139 	pat->rareoff = r - (pat->patlen - 1);
    140 
    141 						/* get md2 shift */
    142 	for (pe = pat->pat + pat->patlen - 1, p = pe - 1; p >= pat->pat; p--)
    143 		if (*p == *pe)
    144 			break;
    145 
    146 	/* *p is first leftward reoccurrence of *pe */
    147 	pat->md2 = pe - p;
    148 	return (pat);
    149 
    150 mem:	sv_errno = errno;
    151 	bm_free(pat);
    152 	errno = sv_errno;
    153 	return (NULL);
    154 }
    155 
    156 void
    157 bm_free(pat)
    158 	bm_pat *pat;
    159 {
    160 	if (pat->pat != NULL)
    161 		free(pat->pat);
    162 	if (pat->delta != NULL)
    163 		free(pat->delta);
    164 	free(pat);
    165 }
    166 
    167 u_char *
    168 bm_exec(pat, base, n)
    169 	bm_pat *pat;
    170 	u_char *base;
    171 	size_t n;
    172 {
    173 	register u_char *e, *ep, *p, *q, *s;
    174 	register size_t *d0, k, md2, n1, ro;
    175 	register int rc;
    176 
    177 	if (n == 0)
    178 		return (NULL);
    179 
    180 	d0 = pat->delta;
    181 	n1 = pat->patlen - 1;
    182 	md2 = pat->md2;
    183 	ro = pat->rareoff;
    184 	rc = pat->rarec;
    185 	ep = pat->pat + pat->patlen - 1;
    186 	s = base + (pat->patlen - 1);
    187 
    188 	/* fast loop up to n - 3 * patlen */
    189 	e = base + n - 3 * pat->patlen;
    190 	while (s < e) {
    191 		k = d0[*s];		/* ufast skip loop */
    192 		while (k) {
    193 			k = d0[*(s += k)];
    194 			k = d0[*(s += k)];
    195 		}
    196 		if (s >= e)
    197 			break;
    198 		if (s[ro] != rc)	/* guard test */
    199 			goto mismatch1;
    200 					/* fwd match */
    201 		for (p = pat->pat, q = s - n1; p < ep;)
    202 			if (*q++ != *p++)
    203 				goto mismatch1;
    204 		return (s - n1);
    205 
    206 mismatch1:	s += md2;		/* md2 shift */
    207 	}
    208 
    209 	/* slow loop up to end */
    210 	e = base + n;
    211 	while (s < e) {
    212 		s += d0[*s];		/* step */
    213 		if (s >= e)
    214 			break;
    215 		if (s[ro] != rc)	/* guard test */
    216 			goto mismatch2;
    217 					/* fwd match */
    218 		for (p = pat->pat, q = s - n1; p <= ep;)
    219 			if (*q++ != *p++)
    220 				goto mismatch2;
    221 		return (s - n1);
    222 
    223 mismatch2:	s += md2;		/* md2 shift */
    224 	}
    225 
    226 	return (NULL);
    227 }
    228