$Log$
+Revision 654.25 2005/09/09 19:44:21 ajc
+* New bmstrcasestr() function to perform very fast case-insensitive
+ substring searches using the Boyer-Moore algorithm. Like its predecessor,
+ it is based on the one written by Urs Jannsen; unlike its predecessor, it
+ actually works.
+
Revision 654.24 2005/09/08 03:25:09 ajc
* messages.c: break up long lines (preferably by substituting spaces with
newlines) to avoid sending messages to the server containing lines with
Fri Jul 10 1998 Art Cancro <ajc@uncensored.citadel.org>
* Initial CVS import
-
}
fieldptr = rfc822_fetch_field(msg->cm_fields['M'], "Bcc");
if (fieldptr != NULL) {
- if (strcasestr(fieldptr, itemlist[pos+1])) {
+ if (bmstrcasestr(fieldptr, itemlist[pos+1])) {
match = 1;
}
free(fieldptr);
msg = CtdlFetchMessage(IMAP->msgids[seq-1], 1);
need_to_free_msg = 1;
}
- if (strcasestr(msg->cm_fields['M'], itemlist[pos+1])) {
+ if (bmstrcasestr(msg->cm_fields['M'], itemlist[pos+1])) {
match = 1;
}
pos += 2;
}
fieldptr = rfc822_fetch_field(msg->cm_fields['M'], "Cc");
if (fieldptr != NULL) {
- if (strcasestr(fieldptr, itemlist[pos+1])) {
+ if (bmstrcasestr(fieldptr, itemlist[pos+1])) {
match = 1;
}
free(fieldptr);
msg = CtdlFetchMessage(IMAP->msgids[seq-1], 1);
need_to_free_msg = 1;
}
- if (strcasestr(msg->cm_fields['A'], itemlist[pos+1])) {
+ if (bmstrcasestr(msg->cm_fields['A'], itemlist[pos+1])) {
match = 1;
}
pos += 2;
msg = CtdlFetchMessage(IMAP->msgids[seq-1], 1);
need_to_free_msg = 1;
}
- if (strcasestr(msg->cm_fields['U'], itemlist[pos+1])) {
+ if (bmstrcasestr(msg->cm_fields['U'], itemlist[pos+1])) {
match = 1;
}
pos += 2;
need_to_free_msg = 1;
}
for (i='A'; i<='Z'; ++i) {
- if (strcasestr(msg->cm_fields[i], itemlist[pos+1])) {
+ if (bmstrcasestr(msg->cm_fields[i], itemlist[pos+1])) {
match = 1;
}
}
msg = CtdlFetchMessage(IMAP->msgids[seq-1], 1);
need_to_free_msg = 1;
}
- if (strcasestr(msg->cm_fields['R'], itemlist[pos+1])) {
+ if (bmstrcasestr(msg->cm_fields['R'], itemlist[pos+1])) {
match = 1;
}
pos += 2;
snprintf(fieldhdr, sizeof fieldhdr, "%s:", fieldname);
/* Locate the end of the headers, so we don't run past that point */
- end_of_headers = strcasestr(rfc822, "\n\r\n");
+ end_of_headers = bmstrcasestr(rfc822, "\n\r\n");
if (end_of_headers == NULL) {
- end_of_headers = strcasestr(rfc822, "\n\n");
+ end_of_headers = bmstrcasestr(rfc822, "\n\n");
}
if (end_of_headers == NULL) return (NULL);
- field_start = strcasestr(rfc822, fieldhdr);
+ field_start = bmstrcasestr(rfc822, fieldhdr);
if (field_start == NULL) return(NULL);
if (field_start > end_of_headers) return(NULL);
break;
case 4:
strcpy(content_type, "text/plain");
- mptr = strcasestr(msg->cm_fields['M'], "Content-type: ");
+ mptr = bmstrcasestr(msg->cm_fields['M'], "Content-type: ");
if (mptr != NULL) {
safestrncpy(content_type, &mptr[14],
sizeof content_type);
if (IMAP->authstate == imap_as_expecting_password) {
lprintf(CTDL_INFO, "IMAP: <password>\n");
}
- else if (strcasestr(cmdbuf, " LOGIN ")) {
+ else if (bmstrcasestr(cmdbuf, " LOGIN ")) {
lprintf(CTDL_INFO, "IMAP: LOGIN...\n");
}
else {
);
}
+/*
+ * bmstrcasestr() -- case-insensitive substring search
+ *
+ * This uses the Boyer-Moore search algorithm and is therefore quite fast.
+ * The code is roughly based on the strstr() replacement from 'tin' written
+ * by Urs Jannsen.
+ */
+char *bmstrcasestr(char *text, char *pattern) {
+
+ register unsigned char *p, *t;
+ register int i, j, *delta;
+ register size_t p1;
+ int deltaspace[256];
+ size_t textlen;
+ size_t patlen;
+
+ textlen = strlen (text);
+ patlen = strlen (pattern);
+
+ /* algorithm fails if pattern is empty */
+ if ((p1 = patlen) == 0)
+ return (text);
+
+ /* code below fails (whenever i is unsigned) if pattern too long */
+ if (p1 > textlen)
+ return (NULL);
+
+ /* set up deltas */
+ delta = deltaspace;
+ for (i = 0; i <= 255; i++)
+ delta[i] = p1;
+ for (p = (unsigned char *) pattern, i = p1; --i > 0;)
+ delta[tolower(*p++)] = i;
+
+ /*
+ * From now on, we want patlen - 1.
+ * In the loop below, p points to the end of the pattern,
+ * t points to the end of the text to be tested against the
+ * pattern, and i counts the amount of text remaining, not
+ * including the part to be tested.
+ */
+ p1--;
+ p = (unsigned char *) pattern + p1;
+ t = (unsigned char *) text + p1;
+ i = textlen - patlen;
+ while(1) {
+ if (tolower(p[0]) == tolower(t[0])) {
+ if (strncasecmp ((const char *)(p - p1), (const char *)(t - p1), p1) == 0) {
+ return ((char *)t - p1);
+ }
+ }
+ j = delta[tolower(t[0])];
+ if (i < j)
+ break;
+ i -= j;
+ t += j;
+ }
+ return (NULL);
+}
+
+
+
FILE *CtdlTempFile(void);
char *ascmonths[12];
void generate_uuid(char *buf);
+char *bmstrcasestr(char *text, char *pattern);
char *searchstring;
searchstring = (char *)data;
- if (strcasestr(usbuf->fullname, searchstring) == NULL) {
+ if (bmstrcasestr(usbuf->fullname, searchstring) == NULL) {
return;
}