1N/A/***********************************************************************
1N/A* *
1N/A* This software is part of the ast package *
1N/A* Copyright (c) 1985-2011 AT&T Intellectual Property *
1N/A* and is licensed under the *
1N/A* Common Public License, Version 1.0 *
1N/A* by AT&T Intellectual Property *
1N/A* *
1N/A* A copy of the License is available at *
1N/A* http://www.opensource.org/licenses/cpl1.0.txt *
1N/A* (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9) *
1N/A* *
1N/A* Information and Software Systems Research *
1N/A* AT&T Research *
1N/A* Florham Park NJ *
1N/A* *
1N/A* Glenn Fowler <gsf@research.att.com> *
1N/A* David Korn <dgk@research.att.com> *
1N/A* Phong Vo <kpv@research.att.com> *
1N/A* *
1N/A***********************************************************************/
1N/A#pragma prototyped
1N/A
1N/A/*
1N/A * posix regex record executor
1N/A * multiple record sized-buffer interface
1N/A */
1N/A
1N/A#include "reglib.h"
1N/A
1N/A/*
1N/A * call regnexec() on records selected by Boyer-Moore
1N/A */
1N/A
1N/Aint
1N/Aregrexec(const regex_t* p, const char* s, size_t len, size_t nmatch, regmatch_t* match, regflags_t flags, int sep, void* handle, regrecord_t record)
1N/A{
1N/A register unsigned char* buf = (unsigned char*)s;
1N/A register unsigned char* beg;
1N/A register unsigned char* l;
1N/A register unsigned char* r;
1N/A register unsigned char* x;
1N/A register size_t* skip;
1N/A register size_t* fail;
1N/A register Bm_mask_t** mask;
1N/A register size_t index;
1N/A register int n;
1N/A unsigned char* end;
1N/A size_t mid;
1N/A int complete;
1N/A int exactlen;
1N/A int leftlen;
1N/A int rightlen;
1N/A int inv;
1N/A Bm_mask_t m;
1N/A Env_t* env;
1N/A Rex_t* e;
1N/A
1N/A if (!s || !p || !(env = p->env) || (e = env->rex)->type != REX_BM)
1N/A return REG_BADPAT;
1N/A inv = (flags & REG_INVERT) != 0;
1N/A buf = beg = (unsigned char*)s;
1N/A end = buf + len;
1N/A mid = (len < e->re.bm.right) ? 0 : (len - e->re.bm.right);
1N/A skip = e->re.bm.skip;
1N/A fail = e->re.bm.fail;
1N/A mask = e->re.bm.mask;
1N/A complete = e->re.bm.complete && !nmatch;
1N/A exactlen = e->re.bm.size;
1N/A leftlen = e->re.bm.left + exactlen;
1N/A rightlen = exactlen + e->re.bm.right;
1N/A index = leftlen++;
1N/A for (;;)
1N/A {
1N/A while ((index += skip[buf[index]]) < mid);
1N/A if (index < HIT)
1N/A goto impossible;
1N/A index -= HIT;
1N/A m = mask[n = exactlen - 1][buf[index]];
1N/A do
1N/A {
1N/A if (!n--)
1N/A goto possible;
1N/A } while (m &= mask[n][buf[--index]]);
1N/A if ((index += fail[n + 1]) < len)
1N/A continue;
1N/A impossible:
1N/A if (inv)
1N/A {
1N/A l = r = buf + len;
1N/A goto invert;
1N/A }
1N/A n = 0;
1N/A goto done;
1N/A possible:
1N/A r = (l = buf + index) + exactlen;
1N/A while (l > beg)
1N/A if (*--l == sep)
1N/A {
1N/A l++;
1N/A break;
1N/A }
1N/A if ((r - l) < leftlen)
1N/A goto spanned;
1N/A while (r < end && *r != sep)
1N/A r++;
1N/A if ((r - (buf + index)) < rightlen)
1N/A goto spanned;
1N/A if (complete || (env->rex = ((r - l) > 128) ? e : e->next) && !(n = regnexec(p, (char*)l, r - l, nmatch, match, flags)))
1N/A {
1N/A if (inv)
1N/A {
1N/A invert:
1N/A x = beg;
1N/A while (beg < l)
1N/A {
1N/A while (x < l && *x != sep)
1N/A x++;
1N/A if (n = (*record)(handle, (char*)beg, x - beg))
1N/A goto done;
1N/A beg = ++x;
1N/A }
1N/A }
1N/A else if (n = (*record)(handle, (char*)l, r - l))
1N/A goto done;
1N/A if ((index = (r - buf) + leftlen) >= len)
1N/A {
1N/A n = (inv && (++r - buf) < len) ? (*record)(handle, (char*)r, (buf + len) - r): 0;
1N/A goto done;
1N/A }
1N/A beg = r + 1;
1N/A }
1N/A else if (n != REG_NOMATCH)
1N/A goto done;
1N/A else
1N/A {
1N/A spanned:
1N/A if ((index += exactlen) >= mid)
1N/A goto impossible;
1N/A }
1N/A }
1N/A done:
1N/A env->rex = e;
1N/A return n;
1N/A}