/*
- * "$Id: search.c 5963 2006-09-17 19:01:47Z mike $"
+ * "$Id$"
*
- * Search routines for the Common UNIX Printing System (CUPS).
+ * Search routines for CUPS.
*
- * Copyright 1997-2006 by Easy Software Products.
+ * Copyright 2007-2014 by Apple Inc.
+ * Copyright 1997-2006 by Easy Software Products.
*
- * These coded instructions, statements, and computer programs are the
- * property of Easy Software Products and are protected by Federal
- * copyright law. Distribution and use rights are outlined in the file
- * "LICENSE.txt" which should have been included with this file. If this
- * file is missing or damaged please contact Easy Software Products
- * at:
- *
- * Attn: CUPS Licensing Information
- * Easy Software Products
- * 44141 Airport View Drive, Suite 204
- * Hollywood, Maryland 20636 USA
- *
- * Voice: (301) 373-9600
- * EMail: cups-info@cups.org
- * WWW: http://www.cups.org
- *
- * Contents:
- *
- * cgiCompileSearch() - Compile a search string.
- * cgiDoSearch() - Do a search of some text.
- * cgiFreeSearch() - Free a compiled search context.
+ * These coded instructions, statements, and computer programs are the
+ * property of Apple Inc. and are protected by Federal copyright
+ * law. Distribution and use rights are outlined in the file "LICENSE.txt"
+ * which should have been included with this file. If this file is
+ * file is missing or damaged, see the license at "http://www.cups.org/".
*/
/*
char *s, /* Regular expression string */
*sptr, /* Pointer into RE string */
*sword; /* Pointer to start of word */
- int slen; /* Allocated size of RE string */
+ size_t slen; /* Allocated size of RE string */
const char *qptr, /* Pointer into query string */
*qend; /* End of current word */
const char *prefix; /* Prefix to add to next word */
int quoted; /* Word is quoted */
- int wlen; /* Word length */
+ size_t wlen; /* Word length */
char *lword; /* Last word in query */
- DEBUG_printf(("help_compile_search(query=\"%s\")\n", query ? query : "(nil)"));
+ DEBUG_printf(("cgiCompileSearch(query=\"%s\")\n", query));
+
+ /*
+ * Range check input...
+ */
+
+ if (!query)
+ return (NULL);
/*
* Allocate a regular expression storage structure...
*/
- re = (regex_t *)calloc(1, sizeof(regex_t));
+ if ((re = (regex_t *)calloc(1, sizeof(regex_t))) == NULL)
+ return (NULL);
/*
* Allocate a buffer to hold the regular expression string, starting
if (slen < 1024)
slen = 1024;
- s = (char *)malloc(slen);
+ if ((s = (char *)malloc(slen)) == NULL)
+ {
+ free(re);
+ return (NULL);
+ }
/*
* Copy the query string to the regular expression, handling basic
for (qend = qptr + 1; *qend && !isspace(*qend); qend ++);
}
- wlen = qend - qptr;
+ wlen = (size_t)(qend - qptr);
/*
* Look for logic words: AND, OR
*/
- if (wlen == 3 && !strncasecmp(qptr, "AND", 3))
+ if (wlen == 3 && !_cups_strncasecmp(qptr, "AND", 3))
{
/*
* Logical AND with the following text...
qptr = qend;
}
- else if (wlen == 2 && !strncasecmp(qptr, "OR", 2))
+ else if (wlen == 2 && !_cups_strncasecmp(qptr, "OR", 2))
{
/*
* Logical OR with the following text...
* string + RE overhead...
*/
- wlen = (sptr - s) + 4 * wlen + 2 * strlen(prefix) + 4;
+ wlen = (size_t)(sptr - s) + 2 * 4 * wlen + 2 * strlen(prefix) + 11;
+ if (lword)
+ wlen += strlen(lword);
if (wlen > slen)
{
* Add the prefix string...
*/
- strcpy(sptr, prefix);
+ memcpy(sptr, prefix, strlen(prefix) + 1);
sptr += strlen(sptr);
/*
char *lword2; /* New "last word" */
- lword2 = strdup(sword);
+ if ((lword2 = strdup(sword)) == NULL)
+ {
+ free(lword);
+ free(s);
+ free(re);
+ return (NULL);
+ }
- strcpy(sptr, ".*|.*");
+ memcpy(sptr, ".*|.*", 6);
sptr += 5;
- strcpy(sptr, lword2);
+ memcpy(sptr, lword2, strlen(lword2) + 1);
sptr += strlen(sptr);
- strcpy(sptr, ".*");
+ memcpy(sptr, ".*", 3);
sptr += 2;
- strcpy(sptr, lword);
+ memcpy(sptr, lword, strlen(lword) + 1);
sptr += strlen(sptr);
free(lword);
free(lword);
if (sptr > s)
- strcpy(sptr, ".*");
+ memcpy(sptr, ".*", 3);
else
{
/*
/*
- * End of "$Id: search.c 5963 2006-09-17 19:01:47Z mike $".
+ * End of "$Id$".
*/