su_bm.c 5.6 KB
Newer Older
Pekka Pessi's avatar
Pekka Pessi committed
1 2 3 4 5 6 7
/*
 * This file is part of the Sofia-SIP package
 *
 * Copyright (C) 2005 Nokia Corporation.
 *
 * Contact: Pekka Pessi <pekka.pessi@nokia.com>
 *
8
 * This library is free software; you can redistribute it and/or
Pekka Pessi's avatar
Pekka Pessi committed
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
 * modify it under the terms of the GNU Lesser General Public License
 * as published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA
 *
 */

/**
26
 * @file su_bm.c
27
 * @brief Search with Boyer-Moore algorithm
Pekka Pessi's avatar
Pekka Pessi committed
28 29 30 31 32 33 34 35 36
 *  
 * @author Pekka Pessi <Pekka.Pessi@nokia.com>
 *
 * @date Created: Mon Apr 11 16:35:16 2005 ppessi
 * 
 */

#include "config.h"

37
#include <sofia-sip/su_bm.h>
38

Pekka Pessi's avatar
Pekka Pessi committed
39 40 41 42
#include <sys/types.h>
#include <stddef.h>
#include <limits.h>
#include <ctype.h>
43
#include <stdlib.h>
Pekka Pessi's avatar
Pekka Pessi committed
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80

#ifndef TORTURELOG
#define TORTURELOG(x) (void)0
#endif

struct bw_fwd_table { 
  unsigned char table[UCHAR_MAX];
};

/** Build forward skip table for Boyer-Moore algorithm */
static
bm_fwd_table_t *
bm_memmem_study0(char const *needle, size_t nlen, bm_fwd_table_t *fwd)
{
  size_t i;

  if (nlen >= UCHAR_MAX) {
    needle += nlen - UCHAR_MAX;
    nlen = UCHAR_MAX;
  }

  for (i = 0; i < UCHAR_MAX; i++)
    fwd->table[i] = (unsigned char)nlen;

  for (i = 0; i < nlen; i++) {
    fwd->table[(unsigned int)needle[i]] = (unsigned char)(nlen - i - 1);
  }

  return fwd;
}

/** Build forward skip table for Boyer-Moore algorithm */
bm_fwd_table_t *
bm_memmem_study(char const *needle, size_t nlen)
{
  bm_fwd_table_t *fwd = malloc(sizeof *fwd);

81 82 83 84
  if (fwd)
    bm_memmem_study0(needle, nlen, fwd);

  return fwd;
Pekka Pessi's avatar
Pekka Pessi committed
85 86 87 88 89 90 91 92 93
}

/** Search for a substring using Boyer-Moore algorithm. */
char const*
bm_memmem(char const *haystack, size_t hlen,
	  char const *needle, size_t nlen,
	  bm_fwd_table_t *fwd)
{
  size_t i, j;
94
  bm_fwd_table_t fwd0[1];
Pekka Pessi's avatar
Pekka Pessi committed
95 96 97 98 99 100 101 102 103 104 105 106 107

  if (nlen == 0)
    return haystack;
  if (needle == NULL || haystack == NULL || nlen > hlen)
    return NULL;

  if (nlen == 1) {
    for (i = 0; i < hlen; i++)
      if (haystack[i] == needle[0])
	return haystack + i;
    return NULL;
  }

108
  if (!fwd)
Pekka Pessi's avatar
Pekka Pessi committed
109 110 111 112 113 114
    fwd = bm_memmem_study0(needle, nlen, fwd0);

  for (i = j = nlen - 1; i < hlen;) {
    unsigned char h = haystack[i];
    if (h == needle[j]) {
      TORTURELOG(("match \"%s\" at %u\nwith  %*s\"%.*s*%s\": %s\n", 
115 116
		  haystack, (unsigned)i, 
		  (int)(i - j), "", (int)j, needle, needle + j + 1, 
Pekka Pessi's avatar
Pekka Pessi committed
117 118 119 120 121 122 123 124
		  j == 0 ? "match!" : "back by 1"));
      if (j == 0)
	return haystack + i;
      i--, j--;
    }
    else {
      if (fwd->table[h] > nlen - j) {
	TORTURELOG(("match \"%s\" at %u\n"
125 126 127 128
		    "last  %*s\"%.*s*%s\": (by %u)\n", 
		    haystack, (unsigned)i,
		    (int)(i - j), "", 
		    (int)j, needle, needle + j + 1, fwd->table[h]));
Pekka Pessi's avatar
Pekka Pessi committed
129 130 131 132
      	i += fwd->table[h];
      }
      else {
	TORTURELOG(("match \"%s\" at %u\n"
133 134 135 136
		    "2nd   %*s\"%.*s*%s\": (by %u)\n", 
		    haystack, (unsigned)i,
		    (int)(i - j), "", 
		    (int)j, needle, needle + j + 1, (unsigned)(nlen - j)));
Pekka Pessi's avatar
Pekka Pessi committed
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
	i += nlen - j;
      }
      j = nlen - 1;
    }
  }
  
  return NULL;
}


/** Build forward skip table for Boyer-Moore algorithm */
static
bm_fwd_table_t *
bm_memcasemem_study0(char const *needle, size_t nlen, bm_fwd_table_t *fwd)
{
  size_t i;

  if (nlen >= UCHAR_MAX) {
    needle += nlen - UCHAR_MAX;
    nlen = UCHAR_MAX;
  }

  for (i = 0; i < UCHAR_MAX; i++)
    fwd->table[i] = (unsigned char)nlen;

  for (i = 0; i < nlen; i++) {
    unsigned char n = tolower(needle[i]);
    fwd->table[n] = (unsigned char)(nlen - i - 1);
  }

  return fwd;
}

/** Build forward skip table for Boyer-Moore algorithm */
bm_fwd_table_t *
bm_memcasemem_study(char const *needle, size_t nlen)
{
  bm_fwd_table_t *fwd = malloc(sizeof *fwd);

176 177 178 179
  if (fwd)
    bm_memcasemem_study0(needle, nlen, fwd);

  return fwd;
Pekka Pessi's avatar
Pekka Pessi committed
180 181 182 183 184 185 186 187 188
}

/** Search for substring using Boyer-Moore algorithm. */
char const*
bm_memcasemem(char const *haystack, size_t hlen,
	  char const *needle, size_t nlen,
	  bm_fwd_table_t *fwd)
{
  size_t i, j;
189
  bm_fwd_table_t fwd0[1];
Pekka Pessi's avatar
Pekka Pessi committed
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215

  if (nlen == 0)
    return haystack;
  if (needle == 0 || haystack == 0 || nlen > hlen)
    return NULL;

  if (nlen == 1) {
    for (i = 0; i < hlen; i++)
      if (haystack[i] == needle[0])
	return haystack + i;
    return NULL;
  }

  if (!fwd) {
    fwd = bm_memcasemem_study0(needle, nlen, fwd0);
  }

  for (i = j = nlen - 1; i < hlen;) {
    unsigned char h = haystack[i], n = needle[j];
    if (isupper(h)) 
      h = tolower(h);
    if (isupper(n))
      n = tolower(n);

    if (h == n) {
      TORTURELOG(("match \"%s\" at %u\n"
216 217 218
		  "with  %*s\"%.*s*%s\": %s\n",
		  haystack, (unsigned)i,
		  (int)(i - j), "", (int)j, needle, needle + j + 1, 
Pekka Pessi's avatar
Pekka Pessi committed
219 220 221 222 223 224 225 226
		  j == 0 ? "match!" : "back by 1"));
      if (j == 0)
	return haystack + i;
      i--, j--;
    }
    else {
      if (fwd->table[h] > nlen - j) {
	TORTURELOG(("match \"%s\" at %u\n"
227 228 229 230
		    "last  %*s\"%.*s*%s\": (by %u)\n",
		    haystack, (unsigned)i,
		    (int)(i - j), "", (int)j, needle, needle + j + 1, 
		    fwd->table[h]));
Pekka Pessi's avatar
Pekka Pessi committed
231 232 233 234
      	i += fwd->table[h];
      }
      else {
	TORTURELOG(("match \"%s\" at %u\n"
235 236 237 238
		    "2nd   %*s\"%.*s*%s\": (by %u)\n", 
		    haystack, (unsigned)i,
		    (int)(i - j), "", (int)j, needle, needle + j + 1, 
		    (unsigned)(nlen - j)));
Pekka Pessi's avatar
Pekka Pessi committed
239 240 241 242 243 244 245 246
	i += nlen - j;
      }
      j = nlen - 1;
    }
  }
  
  return NULL;
}