/*
- * "$Id: var.c 5548 2006-05-19 19:38:31Z mike $"
+ * "$Id: var.c 7460 2008-04-16 02:19:54Z mike $"
*
* CGI form variable and array functions.
*
+ * Copyright 2007-2009 by Apple Inc.
* Copyright 1997-2005 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
+ * 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/".
*
* Contents:
*
if ((var = cgi_find_variable(name)) == NULL)
return (NULL);
- if (var->nvalues == 1)
- return (var->values[0]);
-
if (element < 0 || element >= var->nvalues)
return (NULL);
#ifdef DEBUG
if (var == NULL)
- printf("cgiGetVariable(\"%s\") is returning NULL...\n", name);
+ DEBUG_printf(("cgiGetVariable(\"%s\") is returning NULL...\n", name));
else
- printf("cgiGetVariable(\"%s\") is returning \"%s\"...\n", name,
- var->values[var->nvalues - 1]);
+ DEBUG_printf(("cgiGetVariable(\"%s\") is returning \"%s\"...\n", name,
+ var->values[var->nvalues - 1]));
#endif /* DEBUG */
return ((var == NULL) ? NULL : var->values[var->nvalues - 1]);
*/
setbuf(stdout, NULL);
- puts("Content-type: text/plain\n");
#endif /* DEBUG */
/*
{
if (element >= var->avalues)
{
+ const char **temp; /* Temporary pointer */
+
+ temp = (const char **)realloc((void *)(var->values),
+ sizeof(char *) * (element + 16));
+ if (!temp)
+ return;
+
var->avalues = element + 16;
- var->values = (const char **)realloc((void *)(var->values),
- sizeof(char *) * var->avalues);
+ var->values = temp;
}
if (element >= var->nvalues)
if (size >= var->avalues)
{
+ const char **temp; /* Temporary pointer */
+
+ temp = (const char **)realloc((void *)(var->values),
+ sizeof(char *) * (size + 16));
+ if (!temp)
+ return;
+
var->avalues = size + 16;
- var->values = (const char **)realloc((void *)(var->values),
- sizeof(char *) * var->avalues);
+ var->values = temp;
}
if (size > var->nvalues)
int element, /* I - Array element number */
const char *value) /* I - Variable value */
{
- _cgi_var_t *var; /* New variable */
+ _cgi_var_t *var; /* New variable */
if (name == NULL || value == NULL || element < 0 || element > 100000)
return;
-#ifdef DEBUG
- printf("Adding variable \'%s\' with value \'%s\'...\n", name, value);
-#endif /* DEBUG */
+ DEBUG_printf(("cgi_add_variable: Adding variable \'%s\' with value "
+ "\'%s\'...\n", name, value));
if (form_count >= form_alloc)
{
+ _cgi_var_t *temp_vars; /* Temporary form pointer */
+
+
if (form_alloc == 0)
- form_vars = malloc(sizeof(_cgi_var_t) * 16);
+ temp_vars = malloc(sizeof(_cgi_var_t) * 16);
else
- form_vars = realloc(form_vars, (form_alloc + 16) * sizeof(_cgi_var_t));
+ temp_vars = realloc(form_vars, (form_alloc + 16) * sizeof(_cgi_var_t));
+ if (!temp_vars)
+ return;
+
+ form_vars = temp_vars;
form_alloc += 16;
}
- var = form_vars + form_count;
+ var = form_vars + form_count;
+
+ if ((var->values = calloc(element + 1, sizeof(char *))) == NULL)
+ return;
+
var->name = strdup(name);
var->nvalues = element + 1;
var->avalues = element + 1;
- var->values = calloc(element + 1, sizeof(char *));
var->values[element] = strdup(value);
form_count ++;
char *data; /* Pointer to form data string */
-#ifdef DEBUG
- puts("Initializing variables using GET method...");
-#endif /* DEBUG */
+ DEBUG_puts("cgi_initialize_get: Initializing variables using GET method...");
/*
* Check to see if there is anything for us to read...
status; /* Return status */
-#ifdef DEBUG
- puts("Initializing variables using POST method...");
-#endif /* DEBUG */
+ DEBUG_puts("cgi_initialize_post: Initializing variables using POST method...");
/*
* Check to see if there is anything for us to read...
for (tbytes = 0; tbytes < length; tbytes += nbytes)
if ((nbytes = read(0, data + tbytes, length - tbytes)) < 0)
+ {
if (errno != EAGAIN)
{
free(data);
return (0);
}
+ else
+ nbytes = 0;
+ }
+ else if (nbytes == 0)
+ {
+ /*
+ * CUPS STR #3176: OpenBSD: Early end-of-file on POST data causes 100% CPU
+ *
+ * This should never happen, but does on OpenBSD. If we see early end-of-
+ * file, treat this as an error and process no data.
+ */
+
+ free(data);
+ return (0);
+ }
data[length] = '\0';
if (s > value)
s --;
- while (s >= value && *s == ' ')
+ while (s >= value && isspace(*s & 255))
*s-- = '\0';
/*
int i;
- puts("Sorting variables...");
+ DEBUG_puts("cgi_sort_variables: Sorting variables...");
#endif /* DEBUG */
if (form_count < 2)
(int (*)(const void *, const void *))cgi_compare_variables);
#ifdef DEBUG
- puts("Sorted variable list is:");
+ DEBUG_puts("cgi_sort_variables: Sorted variable list is:");
for (i = 0; i < form_count; i ++)
- printf("%d: %s (%d) = \"%s\" ...\n", i, form_vars[i].name,
- form_vars[i].nvalues, form_vars[i].values[0]);
+ DEBUG_printf(("cgi_sort_variables: %d: %s (%d) = \"%s\" ...\n", i,
+ form_vars[i].name, form_vars[i].nvalues,
+ form_vars[i].values[0]));
#endif /* DEBUG */
}
/*
- * End of "$Id: var.c 5548 2006-05-19 19:38:31Z mike $".
+ * End of "$Id: var.c 7460 2008-04-16 02:19:54Z mike $".
*/