+ memset(&unixdate, 0, sizeof(unixdate));
+
+ /*
+ * RFC-2579 date/time format is:
+ *
+ * Byte(s) Description
+ * ------- -----------
+ * 0-1 Year (0 to 65535)
+ * 2 Month (1 to 12)
+ * 3 Day (1 to 31)
+ * 4 Hours (0 to 23)
+ * 5 Minutes (0 to 59)
+ * 6 Seconds (0 to 60, 60 = "leap second")
+ * 7 Deciseconds (0 to 9)
+ * 8 +/- UTC
+ * 9 UTC hours (0 to 11)
+ * 10 UTC minutes (0 to 59)
+ */
+
+ unixdate.tm_year = ((date[0] << 8) | date[1]) - 1900;
+ unixdate.tm_mon = date[2] - 1;
+ unixdate.tm_mday = date[3];
+ unixdate.tm_hour = date[4];
+ unixdate.tm_min = date[5];
+ unixdate.tm_sec = date[6];
+
+ t = mktime(&unixdate);
+
+ if (date[8] == '-')
+ t += date[9] * 3600 + date[10] * 60;
+ else
+ t -= date[9] * 3600 + date[10] * 60;
+
+ return (t);
+}
+
+
+/*
+ * 'ippDelete()' - Delete an IPP message.
+ */
+
+void
+ippDelete(ipp_t *ipp) /* I - IPP message */
+{
+ ipp_attribute_t *attr, /* Current attribute */
+ *next; /* Next attribute */
+
+
+ DEBUG_printf(("ippDelete(ipp=%p)", (void *)ipp));
+
+ if (!ipp)
+ return;
+
+ ipp->use --;
+ if (ipp->use > 0)
+ {
+ DEBUG_printf(("4debug_retain: %p IPP message (use=%d)", (void *)ipp, ipp->use));
+ return;
+ }
+
+ DEBUG_printf(("4debug_free: %p IPP message", (void *)ipp));
+
+ for (attr = ipp->attrs; attr != NULL; attr = next)
+ {
+ next = attr->next;
+
+ DEBUG_printf(("4debug_free: %p %s %s%s (%d values)", (void *)attr, attr->name, attr->num_values > 1 ? "1setOf " : "", ippTagString(attr->value_tag), attr->num_values));
+
+ ipp_free_values(attr, 0, attr->num_values);
+
+ if (attr->name)
+ _cupsStrFree(attr->name);
+
+ free(attr);
+ }
+
+ free(ipp);
+}
+
+
+/*
+ * 'ippDeleteAttribute()' - Delete a single attribute in an IPP message.
+ *
+ * @since CUPS 1.1.19/macOS 10.3@
+ */
+
+void
+ippDeleteAttribute(
+ ipp_t *ipp, /* I - IPP message */
+ ipp_attribute_t *attr) /* I - Attribute to delete */
+{
+ ipp_attribute_t *current, /* Current attribute */
+ *prev; /* Previous attribute */
+
+
+ DEBUG_printf(("ippDeleteAttribute(ipp=%p, attr=%p(%s))", (void *)ipp, (void *)attr, attr ? attr->name : "(null)"));
+
+ /*
+ * Range check input...
+ */
+
+ if (!attr)
+ return;
+
+ DEBUG_printf(("4debug_free: %p %s %s%s (%d values)", (void *)attr, attr->name, attr->num_values > 1 ? "1setOf " : "", ippTagString(attr->value_tag), attr->num_values));
+
+ /*
+ * Find the attribute in the list...
+ */
+
+ if (ipp)
+ {
+ for (current = ipp->attrs, prev = NULL;
+ current;
+ prev = current, current = current->next)
+ if (current == attr)
+ {
+ /*
+ * Found it, remove the attribute from the list...
+ */
+
+ if (prev)
+ prev->next = current->next;
+ else
+ ipp->attrs = current->next;
+
+ if (current == ipp->last)
+ ipp->last = prev;
+
+ break;
+ }
+
+ if (!current)
+ return;
+ }
+
+ /*
+ * Free memory used by the attribute...
+ */
+
+ ipp_free_values(attr, 0, attr->num_values);
+
+ if (attr->name)
+ _cupsStrFree(attr->name);
+
+ free(attr);
+}
+
+
+/*
+ * 'ippDeleteValues()' - Delete values in an attribute.
+ *
+ * The @code element@ parameter specifies the first value to delete, starting at
+ * 0. It must be less than the number of values returned by @link ippGetCount@.
+ *
+ * The @code attr@ parameter may be modified as a result of setting the value.
+ *
+ * Deleting all values in an attribute deletes the attribute.
+ *
+ * @since CUPS 1.6/macOS 10.8@
+ */
+
+int /* O - 1 on success, 0 on failure */
+ippDeleteValues(
+ ipp_t *ipp, /* I - IPP message */
+ ipp_attribute_t **attr, /* IO - Attribute */
+ int element, /* I - Index of first value to delete (0-based) */
+ int count) /* I - Number of values to delete */
+{
+ /*
+ * Range check input...
+ */
+
+ if (!ipp || !attr || !*attr ||
+ element < 0 || element >= (*attr)->num_values || count <= 0 ||
+ (element + count) >= (*attr)->num_values)
+ return (0);
+
+ /*
+ * If we are deleting all values, just delete the attribute entirely.
+ */
+
+ if (count == (*attr)->num_values)
+ {
+ ippDeleteAttribute(ipp, *attr);
+ *attr = NULL;
+ return (1);
+ }
+
+ /*
+ * Otherwise free the values in question and return.
+ */
+
+ ipp_free_values(*attr, element, count);
+
+ return (1);
+}
+
+
+/*
+ * 'ippFindAttribute()' - Find a named attribute in a request.
+ *
+ * Starting with CUPS 2.0, the attribute name can contain a hierarchical list
+ * of attribute and member names separated by slashes, for example
+ * "media-col/media-size".
+ */
+
+ipp_attribute_t * /* O - Matching attribute */
+ippFindAttribute(ipp_t *ipp, /* I - IPP message */
+ const char *name, /* I - Name of attribute */
+ ipp_tag_t type) /* I - Type of attribute */
+{
+ DEBUG_printf(("2ippFindAttribute(ipp=%p, name=\"%s\", type=%02x(%s))", (void *)ipp, name, type, ippTagString(type)));
+
+ if (!ipp || !name)
+ return (NULL);
+
+ /*
+ * Reset the current pointer...
+ */
+
+ ipp->current = NULL;
+ ipp->atend = 0;
+
+ /*
+ * Search for the attribute...
+ */
+
+ return (ippFindNextAttribute(ipp, name, type));
+}
+
+
+/*
+ * 'ippFindNextAttribute()' - Find the next named attribute in a request.
+ *
+ * Starting with CUPS 2.0, the attribute name can contain a hierarchical list
+ * of attribute and member names separated by slashes, for example
+ * "media-col/media-size".
+ */
+
+ipp_attribute_t * /* O - Matching attribute */
+ippFindNextAttribute(ipp_t *ipp, /* I - IPP message */
+ const char *name, /* I - Name of attribute */
+ ipp_tag_t type) /* I - Type of attribute */
+{
+ ipp_attribute_t *attr, /* Current atttribute */
+ *childattr; /* Child attribute */
+ ipp_tag_t value_tag; /* Value tag */
+ char parent[1024], /* Parent attribute name */
+ *child = NULL; /* Child attribute name */
+
+
+ DEBUG_printf(("2ippFindNextAttribute(ipp=%p, name=\"%s\", type=%02x(%s))", (void *)ipp, name, type, ippTagString(type)));
+
+ if (!ipp || !name)
+ return (NULL);
+
+ DEBUG_printf(("3ippFindNextAttribute: atend=%d", ipp->atend));
+
+ if (ipp->atend)
+ return (NULL);
+
+ if (strchr(name, '/'))
+ {
+ /*
+ * Search for child attribute...
+ */
+
+ strlcpy(parent, name, sizeof(parent));
+ if ((child = strchr(parent, '/')) == NULL)
+ {
+ DEBUG_puts("3ippFindNextAttribute: Attribute name too long.");
+ return (NULL);
+ }
+
+ *child++ = '\0';
+
+ if (ipp->current && ipp->current->name && ipp->current->value_tag == IPP_TAG_BEGIN_COLLECTION && !strcmp(parent, ipp->current->name))
+ {
+ while (ipp->curindex < ipp->current->num_values)
+ {
+ if ((childattr = ippFindNextAttribute(ipp->current->values[ipp->curindex].collection, child, type)) != NULL)
+ return (childattr);
+
+ ipp->curindex ++;
+ if (ipp->curindex < ipp->current->num_values && ipp->current->values[ipp->curindex].collection)
+ ipp->current->values[ipp->curindex].collection->current = NULL;
+ }
+
+ ipp->prev = ipp->current;
+ ipp->current = ipp->current->next;
+ ipp->curindex = 0;
+
+ if (!ipp->current)
+ {
+ ipp->atend = 1;
+ return (NULL);
+ }
+ }
+
+ if (!ipp->current)
+ {
+ ipp->prev = NULL;
+ ipp->current = ipp->attrs;
+ ipp->curindex = 0;
+ }
+
+ name = parent;
+ attr = ipp->current;
+ }
+ else if (ipp->current)
+ {
+ ipp->prev = ipp->current;
+ attr = ipp->current->next;
+ }
+ else
+ {
+ ipp->prev = NULL;
+ attr = ipp->attrs;
+ }
+
+ for (; attr != NULL; ipp->prev = attr, attr = attr->next)
+ {
+ DEBUG_printf(("4ippFindAttribute: attr=%p, name=\"%s\"", (void *)attr, attr->name));
+
+ value_tag = (ipp_tag_t)(attr->value_tag & IPP_TAG_CUPS_MASK);
+
+ if (attr->name != NULL && _cups_strcasecmp(attr->name, name) == 0 &&
+ (value_tag == type || type == IPP_TAG_ZERO || name == parent ||
+ (value_tag == IPP_TAG_TEXTLANG && type == IPP_TAG_TEXT) ||
+ (value_tag == IPP_TAG_NAMELANG && type == IPP_TAG_NAME)))
+ {
+ ipp->current = attr;
+
+ if (name == parent && attr->value_tag == IPP_TAG_BEGIN_COLLECTION)
+ {
+ int i; /* Looping var */
+
+ for (i = 0; i < attr->num_values; i ++)
+ {
+ if ((childattr = ippFindAttribute(attr->values[i].collection, child, type)) != NULL)
+ {
+ attr->values[0].collection->curindex = i;
+ return (childattr);
+ }
+ }
+ }
+ else
+ return (attr);
+ }
+ }
+
+ ipp->current = NULL;
+ ipp->prev = NULL;
+ ipp->atend = 1;
+
+ return (NULL);
+}
+
+
+/*
+ * 'ippFirstAttribute()' - Return the first attribute in the message.
+ *
+ * @since CUPS 1.6/macOS 10.8@
+ */
+
+ipp_attribute_t * /* O - First attribute or @code NULL@ if none */
+ippFirstAttribute(ipp_t *ipp) /* I - IPP message */
+{
+ /*
+ * Range check input...
+ */
+
+ if (!ipp)
+ return (NULL);
+
+ /*
+ * Return the first attribute...
+ */
+
+ return (ipp->current = ipp->attrs);
+}
+
+
+/*
+ * 'ippGetBoolean()' - Get a boolean value for an attribute.
+ *
+ * The @code element@ parameter specifies which value to get from 0 to
+ * @code ippGetCount(attr)@ - 1.
+ *
+ * @since CUPS 1.6/macOS 10.8@
+ */
+
+int /* O - Boolean value or 0 on error */
+ippGetBoolean(ipp_attribute_t *attr, /* I - IPP attribute */
+ int element) /* I - Value number (0-based) */
+{
+ /*
+ * Range check input...
+ */
+
+ if (!attr || attr->value_tag != IPP_TAG_BOOLEAN ||
+ element < 0 || element >= attr->num_values)
+ return (0);
+
+ /*
+ * Return the value...
+ */
+
+ return (attr->values[element].boolean);
+}
+
+
+/*
+ * 'ippGetCollection()' - Get a collection value for an attribute.
+ *
+ * The @code element@ parameter specifies which value to get from 0 to
+ * @code ippGetCount(attr)@ - 1.
+ *
+ * @since CUPS 1.6/macOS 10.8@
+ */
+
+ipp_t * /* O - Collection value or @code NULL@ on error */
+ippGetCollection(
+ ipp_attribute_t *attr, /* I - IPP attribute */
+ int element) /* I - Value number (0-based) */
+{
+ /*
+ * Range check input...
+ */
+
+ if (!attr || attr->value_tag != IPP_TAG_BEGIN_COLLECTION ||
+ element < 0 || element >= attr->num_values)
+ return (NULL);
+
+ /*
+ * Return the value...
+ */
+
+ return (attr->values[element].collection);
+}
+
+
+/*
+ * 'ippGetCount()' - Get the number of values in an attribute.
+ *
+ * @since CUPS 1.6/macOS 10.8@
+ */
+
+int /* O - Number of values or 0 on error */
+ippGetCount(ipp_attribute_t *attr) /* I - IPP attribute */
+{
+ /*
+ * Range check input...
+ */
+
+ if (!attr)
+ return (0);
+
+ /*
+ * Return the number of values...
+ */
+
+ return (attr->num_values);
+}
+
+
+/*
+ * 'ippGetDate()' - Get a dateTime value for an attribute.
+ *
+ * The @code element@ parameter specifies which value to get from 0 to
+ * @code ippGetCount(attr)@ - 1.
+ *
+ * @since CUPS 1.6/macOS 10.8@
+ */
+
+const ipp_uchar_t * /* O - dateTime value or @code NULL@ */
+ippGetDate(ipp_attribute_t *attr, /* I - IPP attribute */
+ int element) /* I - Value number (0-based) */
+{
+ /*
+ * Range check input...
+ */
+
+ if (!attr || attr->value_tag != IPP_TAG_DATE ||
+ element < 0 || element >= attr->num_values)
+ return (NULL);
+
+ /*
+ * Return the value...
+ */
+
+ return (attr->values[element].date);
+}
+
+
+/*
+ * 'ippGetGroupTag()' - Get the group associated with an attribute.
+ *
+ * @since CUPS 1.6/macOS 10.8@
+ */
+
+ipp_tag_t /* O - Group tag or @code IPP_TAG_ZERO@ on error */
+ippGetGroupTag(ipp_attribute_t *attr) /* I - IPP attribute */
+{
+ /*
+ * Range check input...
+ */
+
+ if (!attr)
+ return (IPP_TAG_ZERO);
+
+ /*
+ * Return the group...
+ */
+
+ return (attr->group_tag);
+}
+
+
+/*
+ * 'ippGetInteger()' - Get the integer/enum value for an attribute.
+ *
+ * The @code element@ parameter specifies which value to get from 0 to
+ * @code ippGetCount(attr)@ - 1.
+ *
+ * @since CUPS 1.6/macOS 10.8@
+ */
+
+int /* O - Value or 0 on error */
+ippGetInteger(ipp_attribute_t *attr, /* I - IPP attribute */
+ int element) /* I - Value number (0-based) */
+{
+ /*
+ * Range check input...
+ */
+
+ if (!attr || (attr->value_tag != IPP_TAG_INTEGER && attr->value_tag != IPP_TAG_ENUM) ||
+ element < 0 || element >= attr->num_values)
+ return (0);
+
+ /*
+ * Return the value...
+ */
+
+ return (attr->values[element].integer);
+}
+
+
+/*
+ * 'ippGetName()' - Get the attribute name.
+ *
+ * @since CUPS 1.6/macOS 10.8@
+ */
+
+const char * /* O - Attribute name or @code NULL@ for separators */
+ippGetName(ipp_attribute_t *attr) /* I - IPP attribute */
+{
+ /*
+ * Range check input...
+ */
+
+ if (!attr)
+ return (NULL);
+
+ /*
+ * Return the name...
+ */
+
+ return (attr->name);
+}
+
+
+/*
+ * 'ippGetOctetString()' - Get an octetString value from an IPP attribute.
+ *
+ * The @code element@ parameter specifies which value to get from 0 to
+ * @code ippGetCount(attr)@ - 1.
+ *
+ * @since CUPS 1.7/macOS 10.9@
+ */
+
+void * /* O - Pointer to octetString data */
+ippGetOctetString(
+ ipp_attribute_t *attr, /* I - IPP attribute */
+ int element, /* I - Value number (0-based) */
+ int *datalen) /* O - Length of octetString data */
+{
+ /*
+ * Range check input...
+ */
+
+ if (!attr || attr->value_tag != IPP_TAG_STRING ||
+ element < 0 || element >= attr->num_values)
+ {
+ if (datalen)
+ *datalen = 0;
+
+ return (NULL);
+ }
+
+ /*
+ * Return the values...
+ */
+
+ if (datalen)
+ *datalen = attr->values[element].unknown.length;
+
+ return (attr->values[element].unknown.data);
+}
+
+
+/*
+ * 'ippGetOperation()' - Get the operation ID in an IPP message.
+ *
+ * @since CUPS 1.6/macOS 10.8@
+ */
+
+ipp_op_t /* O - Operation ID or 0 on error */
+ippGetOperation(ipp_t *ipp) /* I - IPP request message */
+{
+ /*
+ * Range check input...
+ */
+
+ if (!ipp)
+ return ((ipp_op_t)0);
+
+ /*
+ * Return the value...
+ */
+
+ return (ipp->request.op.operation_id);
+}
+
+
+/*
+ * 'ippGetRange()' - Get a rangeOfInteger value from an attribute.
+ *
+ * The @code element@ parameter specifies which value to get from 0 to
+ * @code ippGetCount(attr)@ - 1.
+ *
+ * @since CUPS 1.6/macOS 10.8@
+ */
+
+int /* O - Lower value of range or 0 */
+ippGetRange(ipp_attribute_t *attr, /* I - IPP attribute */
+ int element, /* I - Value number (0-based) */
+ int *uppervalue)/* O - Upper value of range */
+{
+ /*
+ * Range check input...
+ */
+
+ if (!attr || attr->value_tag != IPP_TAG_RANGE ||
+ element < 0 || element >= attr->num_values)
+ {
+ if (uppervalue)
+ *uppervalue = 0;
+
+ return (0);
+ }
+
+ /*
+ * Return the values...
+ */
+
+ if (uppervalue)
+ *uppervalue = attr->values[element].range.upper;
+
+ return (attr->values[element].range.lower);
+}
+
+
+/*
+ * 'ippGetRequestId()' - Get the request ID from an IPP message.
+ *
+ * @since CUPS 1.6/macOS 10.8@
+ */
+
+int /* O - Request ID or 0 on error */
+ippGetRequestId(ipp_t *ipp) /* I - IPP message */
+{
+ /*
+ * Range check input...
+ */
+
+ if (!ipp)
+ return (0);
+
+ /*
+ * Return the request ID...
+ */
+
+ return (ipp->request.any.request_id);
+}
+
+
+/*
+ * 'ippGetResolution()' - Get a resolution value for an attribute.
+ *
+ * The @code element@ parameter specifies which value to get from 0 to
+ * @code ippGetCount(attr)@ - 1.
+ *
+ * @since CUPS 1.6/macOS 10.8@
+ */
+
+int /* O - Horizontal/cross feed resolution or 0 */
+ippGetResolution(
+ ipp_attribute_t *attr, /* I - IPP attribute */
+ int element, /* I - Value number (0-based) */
+ int *yres, /* O - Vertical/feed resolution */
+ ipp_res_t *units) /* O - Units for resolution */
+{
+ /*
+ * Range check input...
+ */
+
+ if (!attr || attr->value_tag != IPP_TAG_RESOLUTION ||
+ element < 0 || element >= attr->num_values)
+ {
+ if (yres)
+ *yres = 0;
+
+ if (units)
+ *units = (ipp_res_t)0;
+
+ return (0);
+ }
+
+ /*
+ * Return the value...
+ */
+
+ if (yres)
+ *yres = attr->values[element].resolution.yres;
+
+ if (units)
+ *units = attr->values[element].resolution.units;
+
+ return (attr->values[element].resolution.xres);
+}
+
+
+/*
+ * 'ippGetState()' - Get the IPP message state.
+ *
+ * @since CUPS 1.6/macOS 10.8@
+ */
+
+ipp_state_t /* O - IPP message state value */
+ippGetState(ipp_t *ipp) /* I - IPP message */
+{
+ /*
+ * Range check input...
+ */
+
+ if (!ipp)
+ return (IPP_STATE_IDLE);
+
+ /*
+ * Return the value...
+ */
+
+ return (ipp->state);
+}
+
+
+/*
+ * 'ippGetStatusCode()' - Get the status code from an IPP response or event message.
+ *
+ * @since CUPS 1.6/macOS 10.8@
+ */
+
+ipp_status_t /* O - Status code in IPP message */
+ippGetStatusCode(ipp_t *ipp) /* I - IPP response or event message */
+{
+ /*
+ * Range check input...
+ */
+
+ if (!ipp)
+ return (IPP_STATUS_ERROR_INTERNAL);
+
+ /*
+ * Return the value...
+ */
+
+ return (ipp->request.status.status_code);
+}
+
+
+/*
+ * 'ippGetString()' - Get the string and optionally the language code for an attribute.
+ *
+ * The @code element@ parameter specifies which value to get from 0 to
+ * @code ippGetCount(attr)@ - 1.
+ *
+ * @since CUPS 1.6/macOS 10.8@
+ */
+
+const char *
+ippGetString(ipp_attribute_t *attr, /* I - IPP attribute */
+ int element, /* I - Value number (0-based) */
+ const char **language)/* O - Language code (@code NULL@ for don't care) */
+{
+ ipp_tag_t tag; /* Value tag */
+
+
+ /*
+ * Range check input...
+ */
+
+ tag = ippGetValueTag(attr);
+
+ if (!attr || element < 0 || element >= attr->num_values || (tag != IPP_TAG_TEXTLANG && tag != IPP_TAG_NAMELANG && (tag < IPP_TAG_TEXT || tag > IPP_TAG_MIMETYPE)))
+ return (NULL);
+
+ /*
+ * Return the value...
+ */
+
+ if (language)
+ *language = attr->values[element].string.language;
+
+ return (attr->values[element].string.text);
+}
+
+
+/*
+ * 'ippGetValueTag()' - Get the value tag for an attribute.
+ *
+ * @since CUPS 1.6/macOS 10.8@
+ */
+
+ipp_tag_t /* O - Value tag or @code IPP_TAG_ZERO@ on error */
+ippGetValueTag(ipp_attribute_t *attr) /* I - IPP attribute */
+{
+ /*
+ * Range check input...
+ */
+
+ if (!attr)
+ return (IPP_TAG_ZERO);
+
+ /*
+ * Return the value...
+ */
+
+ return (attr->value_tag & IPP_TAG_CUPS_MASK);
+}
+
+
+/*
+ * 'ippGetVersion()' - Get the major and minor version number from an IPP message.
+ *
+ * @since CUPS 1.6/macOS 10.8@
+ */
+
+int /* O - Major version number or 0 on error */
+ippGetVersion(ipp_t *ipp, /* I - IPP message */
+ int *minor) /* O - Minor version number or @code NULL@ for don't care */
+{
+ /*
+ * Range check input...
+ */
+
+ if (!ipp)
+ {
+ if (minor)
+ *minor = 0;
+
+ return (0);
+ }
+
+ /*
+ * Return the value...
+ */
+
+ if (minor)
+ *minor = ipp->request.any.version[1];
+
+ return (ipp->request.any.version[0]);
+}
+
+
+/*
+ * 'ippLength()' - Compute the length of an IPP message.
+ */
+
+size_t /* O - Size of IPP message */
+ippLength(ipp_t *ipp) /* I - IPP message */
+{
+ return (ipp_length(ipp, 0));
+}
+
+
+/*
+ * 'ippNextAttribute()' - Return the next attribute in the message.
+ *
+ * @since CUPS 1.6/macOS 10.8@
+ */
+
+ipp_attribute_t * /* O - Next attribute or @code NULL@ if none */
+ippNextAttribute(ipp_t *ipp) /* I - IPP message */
+{
+ /*
+ * Range check input...
+ */
+
+ if (!ipp || !ipp->current)
+ return (NULL);
+
+ /*
+ * Return the next attribute...
+ */
+
+ return (ipp->current = ipp->current->next);
+}
+
+
+/*
+ * 'ippNew()' - Allocate a new IPP message.
+ */
+
+ipp_t * /* O - New IPP message */
+ippNew(void)
+{
+ ipp_t *temp; /* New IPP message */
+ _cups_globals_t *cg = _cupsGlobals();
+ /* Global data */
+
+
+ DEBUG_puts("ippNew()");
+
+ if ((temp = (ipp_t *)calloc(1, sizeof(ipp_t))) != NULL)
+ {
+ /*
+ * Set default version - usually 2.0...
+ */
+
+ DEBUG_printf(("4debug_alloc: %p IPP message", (void *)temp));
+
+ if (cg->server_version == 0)
+ _cupsSetDefaults();
+
+ temp->request.any.version[0] = (ipp_uchar_t)(cg->server_version / 10);
+ temp->request.any.version[1] = (ipp_uchar_t)(cg->server_version % 10);
+ temp->use = 1;
+ }
+
+ DEBUG_printf(("1ippNew: Returning %p", (void *)temp));
+
+ return (temp);
+}
+
+
+/*
+ * 'ippNewRequest()' - Allocate a new IPP request message.
+ *
+ * The new request message is initialized with the "attributes-charset" and
+ * "attributes-natural-language" attributes added. The
+ * "attributes-natural-language" value is derived from the current locale.
+ *
+ * @since CUPS 1.2/macOS 10.5@
+ */
+
+ipp_t * /* O - IPP request message */
+ippNewRequest(ipp_op_t op) /* I - Operation code */
+{
+ ipp_t *request; /* IPP request message */
+ cups_lang_t *language; /* Current language localization */
+ static int request_id = 0; /* Current request ID */
+ static _cups_mutex_t request_mutex = _CUPS_MUTEX_INITIALIZER;
+ /* Mutex for request ID */
+
+
+ DEBUG_printf(("ippNewRequest(op=%02x(%s))", op, ippOpString(op)));
+
+ /*
+ * Create a new IPP message...
+ */
+
+ if ((request = ippNew()) == NULL)
+ return (NULL);
+
+ /*
+ * Set the operation and request ID...
+ */
+
+ _cupsMutexLock(&request_mutex);
+
+ request->request.op.operation_id = op;
+ request->request.op.request_id = ++request_id;
+
+ _cupsMutexUnlock(&request_mutex);
+
+ /*
+ * Use UTF-8 as the character set...
+ */
+
+ ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
+ "attributes-charset", NULL, "utf-8");
+
+ /*
+ * Get the language from the current locale...
+ */
+
+ language = cupsLangDefault();
+
+ ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
+ "attributes-natural-language", NULL, language->language);
+
+ /*
+ * Return the new request...
+ */
+
+ return (request);
+}
+
+
+/*
+ * 'ippNewResponse()' - Allocate a new IPP response message.
+ *
+ * The new response message is initialized with the same "version-number",
+ * "request-id", "attributes-charset", and "attributes-natural-language" as the
+ * provided request message. If the "attributes-charset" or
+ * "attributes-natural-language" attributes are missing from the request,
+ * 'utf-8' and a value derived from the current locale are substituted,
+ * respectively.
+ *
+ * @since CUPS 1.7/macOS 10.9@
+ */
+
+ipp_t * /* O - IPP response message */
+ippNewResponse(ipp_t *request) /* I - IPP request message */
+{
+ ipp_t *response; /* IPP response message */
+ ipp_attribute_t *attr; /* Current attribute */
+
+
+ /*
+ * Range check input...
+ */
+
+ if (!request)
+ return (NULL);
+
+ /*
+ * Create a new IPP message...
+ */
+
+ if ((response = ippNew()) == NULL)
+ return (NULL);
+
+ /*
+ * Copy the request values over to the response...
+ */
+
+ response->request.status.version[0] = request->request.op.version[0];
+ response->request.status.version[1] = request->request.op.version[1];
+ response->request.status.request_id = request->request.op.request_id;
+
+ /*
+ * The first attribute MUST be attributes-charset...
+ */
+
+ attr = request->attrs;
+
+ if (attr && attr->name && !strcmp(attr->name, "attributes-charset") &&
+ attr->group_tag == IPP_TAG_OPERATION &&
+ attr->value_tag == IPP_TAG_CHARSET &&
+ attr->num_values == 1)
+ {
+ /*
+ * Copy charset from request...
+ */
+
+ ippAddString(response, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
+ "attributes-charset", NULL, attr->values[0].string.text);
+ }
+ else
+ {
+ /*
+ * Use "utf-8" as the default...
+ */
+
+ ippAddString(response, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
+ "attributes-charset", NULL, "utf-8");
+ }
+
+ /*
+ * Then attributes-natural-language...
+ */
+
+ if (attr)
+ attr = attr->next;
+
+ if (attr && attr->name &&
+ !strcmp(attr->name, "attributes-natural-language") &&
+ attr->group_tag == IPP_TAG_OPERATION &&
+ attr->value_tag == IPP_TAG_LANGUAGE &&
+ attr->num_values == 1)
+ {
+ /*
+ * Copy language from request...
+ */
+
+ ippAddString(response, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
+ "attributes-natural-language", NULL,
+ attr->values[0].string.text);
+ }
+ else
+ {
+ /*
+ * Use the language from the current locale...
+ */
+
+ cups_lang_t *language = cupsLangDefault();
+ /* Current locale */
+
+ ippAddString(response, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
+ "attributes-natural-language", NULL, language->language);
+ }
+
+ return (response);
+}
+
+
+/*
+ * 'ippRead()' - Read data for an IPP message from a HTTP connection.
+ */
+
+ipp_state_t /* O - Current state */
+ippRead(http_t *http, /* I - HTTP connection */
+ ipp_t *ipp) /* I - IPP data */
+{
+ DEBUG_printf(("ippRead(http=%p, ipp=%p), data_remaining=" CUPS_LLFMT, (void *)http, (void *)ipp, CUPS_LLCAST (http ? http->data_remaining : -1)));
+
+ if (!http)
+ return (IPP_STATE_ERROR);
+
+ DEBUG_printf(("2ippRead: http->state=%d, http->used=%d", http->state, http->used));
+
+ return (ippReadIO(http, (ipp_iocb_t)ipp_read_http, http->blocking, NULL,
+ ipp));
+}
+
+
+/*
+ * 'ippReadFile()' - Read data for an IPP message from a file.
+ *
+ * @since CUPS 1.1.19/macOS 10.3@
+ */
+
+ipp_state_t /* O - Current state */
+ippReadFile(int fd, /* I - HTTP data */
+ ipp_t *ipp) /* I - IPP data */
+{
+ DEBUG_printf(("ippReadFile(fd=%d, ipp=%p)", fd, (void *)ipp));
+
+ return (ippReadIO(&fd, (ipp_iocb_t)ipp_read_file, 1, NULL, ipp));
+}
+
+
+/*
+ * 'ippReadIO()' - Read data for an IPP message.
+ *
+ * @since CUPS 1.2/macOS 10.5@
+ */
+
+ipp_state_t /* O - Current state */
+ippReadIO(void *src, /* I - Data source */
+ ipp_iocb_t cb, /* I - Read callback function */
+ int blocking, /* I - Use blocking IO? */
+ ipp_t *parent, /* I - Parent request, if any */
+ ipp_t *ipp) /* I - IPP data */
+{
+ int n; /* Length of data */
+ unsigned char *buffer, /* Data buffer */
+ string[IPP_MAX_TEXT],
+ /* Small string buffer */
+ *bufptr; /* Pointer into buffer */
+ ipp_attribute_t *attr; /* Current attribute */
+ ipp_tag_t tag; /* Current tag */
+ ipp_tag_t value_tag; /* Current value tag */
+ _ipp_value_t *value; /* Current value */
+
+
+ DEBUG_printf(("ippReadIO(src=%p, cb=%p, blocking=%d, parent=%p, ipp=%p)", (void *)src, (void *)cb, blocking, (void *)parent, (void *)ipp));
+ DEBUG_printf(("2ippReadIO: ipp->state=%d", ipp ? ipp->state : IPP_STATE_ERROR));
+
+ if (!src || !ipp)
+ return (IPP_STATE_ERROR);
+
+ if ((buffer = (unsigned char *)_cupsBufferGet(IPP_BUF_SIZE)) == NULL)
+ {
+ DEBUG_puts("1ippReadIO: Unable to get read buffer.");
+ return (IPP_STATE_ERROR);
+ }
+
+ switch (ipp->state)
+ {
+ case IPP_STATE_IDLE :
+ ipp->state ++; /* Avoid common problem... */
+
+ case IPP_STATE_HEADER :
+ if (parent == NULL)
+ {
+ /*
+ * Get the request header...
+ */
+
+ if ((*cb)(src, buffer, 8) < 8)
+ {
+ DEBUG_puts("1ippReadIO: Unable to read header.");
+ _cupsBufferRelease((char *)buffer);
+ return (IPP_STATE_ERROR);
+ }
+
+ /*
+ * Then copy the request header over...
+ */
+
+ ipp->request.any.version[0] = buffer[0];
+ ipp->request.any.version[1] = buffer[1];
+ ipp->request.any.op_status = (buffer[2] << 8) | buffer[3];
+ ipp->request.any.request_id = (((((buffer[4] << 8) | buffer[5]) << 8) |
+ buffer[6]) << 8) | buffer[7];
+
+ DEBUG_printf(("2ippReadIO: version=%d.%d", buffer[0], buffer[1]));
+ DEBUG_printf(("2ippReadIO: op_status=%04x",
+ ipp->request.any.op_status));
+ DEBUG_printf(("2ippReadIO: request_id=%d",
+ ipp->request.any.request_id));
+ }
+
+ ipp->state = IPP_STATE_ATTRIBUTE;
+ ipp->current = NULL;
+ ipp->curtag = IPP_TAG_ZERO;
+ ipp->prev = ipp->last;
+
+ /*
+ * If blocking is disabled, stop here...
+ */
+
+ if (!blocking)
+ break;
+
+ case IPP_STATE_ATTRIBUTE :
+ for (;;)
+ {
+ if ((*cb)(src, buffer, 1) < 1)
+ {
+ DEBUG_puts("1ippReadIO: Callback returned EOF/error");
+ _cupsBufferRelease((char *)buffer);
+ return (IPP_STATE_ERROR);
+ }
+
+ DEBUG_printf(("2ippReadIO: ipp->current=%p, ipp->prev=%p", (void *)ipp->current, (void *)ipp->prev));
+
+ /*
+ * Read this attribute...
+ */
+
+ tag = (ipp_tag_t)buffer[0];
+ if (tag == IPP_TAG_EXTENSION)
+ {
+ /*
+ * Read 32-bit "extension" tag...
+ */
+
+ if ((*cb)(src, buffer, 4) < 1)
+ {
+ DEBUG_puts("1ippReadIO: Callback returned EOF/error");
+ _cupsBufferRelease((char *)buffer);
+ return (IPP_STATE_ERROR);
+ }
+
+ tag = (ipp_tag_t)((((((buffer[0] << 8) | buffer[1]) << 8) |
+ buffer[2]) << 8) | buffer[3]);
+
+ if (tag & IPP_TAG_CUPS_CONST)
+ {
+ /*
+ * Fail if the high bit is set in the tag...
+ */
+
+ _cupsSetError(IPP_STATUS_ERROR_INTERNAL, _("IPP extension tag larger than 0x7FFFFFFF."), 1);
+ DEBUG_printf(("1ippReadIO: bad tag 0x%x.", tag));
+ _cupsBufferRelease((char *)buffer);
+ return (IPP_STATE_ERROR);
+ }
+ }
+
+ if (tag == IPP_TAG_END)
+ {
+ /*
+ * No more attributes left...
+ */
+
+ DEBUG_puts("2ippReadIO: IPP_TAG_END.");
+
+ ipp->state = IPP_STATE_DATA;
+ break;
+ }
+ else if (tag < IPP_TAG_UNSUPPORTED_VALUE)
+ {
+ /*
+ * Group tag... Set the current group and continue...
+ */
+
+ if (ipp->curtag == tag)
+ ipp->prev = ippAddSeparator(ipp);
+ else if (ipp->current)
+ ipp->prev = ipp->current;
+
+ ipp->curtag = tag;
+ ipp->current = NULL;
+ DEBUG_printf(("2ippReadIO: group tag=%x(%s), ipp->prev=%p", tag, ippTagString(tag), (void *)ipp->prev));
+ continue;
+ }
+
+ DEBUG_printf(("2ippReadIO: value tag=%x(%s)", tag,
+ ippTagString(tag)));
+
+ /*
+ * Get the name...
+ */
+
+ if ((*cb)(src, buffer, 2) < 2)
+ {
+ DEBUG_puts("1ippReadIO: unable to read name length.");
+ _cupsBufferRelease((char *)buffer);
+ return (IPP_STATE_ERROR);
+ }
+
+ n = (buffer[0] << 8) | buffer[1];
+
+ if (n >= IPP_BUF_SIZE)
+ {
+ _cupsSetError(IPP_STATUS_ERROR_INTERNAL, _("IPP name larger than 32767 bytes."), 1);
+ DEBUG_printf(("1ippReadIO: bad name length %d.", n));
+ _cupsBufferRelease((char *)buffer);
+ return (IPP_STATE_ERROR);
+ }
+
+ DEBUG_printf(("2ippReadIO: name length=%d", n));
+
+ if (n == 0 && tag != IPP_TAG_MEMBERNAME &&
+ tag != IPP_TAG_END_COLLECTION)
+ {
+ /*
+ * More values for current attribute...
+ */
+
+ if (ipp->current == NULL)
+ {
+ _cupsSetError(IPP_STATUS_ERROR_INTERNAL, _("IPP attribute has no name."), 1);
+ DEBUG_puts("1ippReadIO: Attribute without name and no current.");
+ _cupsBufferRelease((char *)buffer);
+ return (IPP_STATE_ERROR);
+ }
+
+ attr = ipp->current;
+ value_tag = (ipp_tag_t)(attr->value_tag & IPP_TAG_CUPS_MASK);
+
+ /*
+ * Make sure we aren't adding a new value of a different
+ * type...
+ */
+
+ if (value_tag == IPP_TAG_ZERO)
+ {
+ /*
+ * Setting the value of a collection member...
+ */
+
+ attr->value_tag = tag;
+ }
+ else if (value_tag == IPP_TAG_TEXTLANG ||
+ value_tag == IPP_TAG_NAMELANG ||
+ (value_tag >= IPP_TAG_TEXT &&
+ value_tag <= IPP_TAG_MIMETYPE))
+ {
+ /*
+ * String values can sometimes come across in different
+ * forms; accept sets of differing values...
+ */
+
+ if (tag != IPP_TAG_TEXTLANG && tag != IPP_TAG_NAMELANG &&
+ (tag < IPP_TAG_TEXT || tag > IPP_TAG_MIMETYPE) &&
+ tag != IPP_TAG_NOVALUE)
+ {
+ _cupsSetError(IPP_STATUS_ERROR_INTERNAL,
+ _("IPP 1setOf attribute with incompatible value "
+ "tags."), 1);
+ DEBUG_printf(("1ippReadIO: 1setOf value tag %x(%s) != %x(%s)",
+ value_tag, ippTagString(value_tag), tag,
+ ippTagString(tag)));
+ _cupsBufferRelease((char *)buffer);
+ return (IPP_STATE_ERROR);
+ }
+
+ if (value_tag != tag)
+ {
+ DEBUG_printf(("1ippReadIO: Converting %s attribute from %s to %s.",
+ attr->name, ippTagString(value_tag), ippTagString(tag)));
+ ippSetValueTag(ipp, &attr, tag);
+ }
+ }
+ else if (value_tag == IPP_TAG_INTEGER ||
+ value_tag == IPP_TAG_RANGE)
+ {
+ /*
+ * Integer and rangeOfInteger values can sometimes be mixed; accept
+ * sets of differing values...
+ */
+
+ if (tag != IPP_TAG_INTEGER && tag != IPP_TAG_RANGE)
+ {
+ _cupsSetError(IPP_STATUS_ERROR_INTERNAL,
+ _("IPP 1setOf attribute with incompatible value "
+ "tags."), 1);
+ DEBUG_printf(("1ippReadIO: 1setOf value tag %x(%s) != %x(%s)",
+ value_tag, ippTagString(value_tag), tag,
+ ippTagString(tag)));
+ _cupsBufferRelease((char *)buffer);
+ return (IPP_STATE_ERROR);
+ }
+
+ if (value_tag == IPP_TAG_INTEGER && tag == IPP_TAG_RANGE)
+ {
+ /*
+ * Convert integer values to rangeOfInteger values...
+ */
+
+ DEBUG_printf(("1ippReadIO: Converting %s attribute to "
+ "rangeOfInteger.", attr->name));
+ ippSetValueTag(ipp, &attr, IPP_TAG_RANGE);
+ }
+ }
+ else if (value_tag != tag)
+ {
+ _cupsSetError(IPP_STATUS_ERROR_INTERNAL,
+ _("IPP 1setOf attribute with incompatible value "
+ "tags."), 1);
+ DEBUG_printf(("1ippReadIO: value tag %x(%s) != %x(%s)",
+ value_tag, ippTagString(value_tag), tag,
+ ippTagString(tag)));
+ _cupsBufferRelease((char *)buffer);
+ return (IPP_STATE_ERROR);
+ }
+
+ /*
+ * Finally, reallocate the attribute array as needed...
+ */
+
+ if ((value = ipp_set_value(ipp, &attr, attr->num_values)) == NULL)
+ {
+ _cupsBufferRelease((char *)buffer);
+ return (IPP_STATE_ERROR);
+ }
+ }
+ else if (tag == IPP_TAG_MEMBERNAME)
+ {
+ /*
+ * Name must be length 0!
+ */
+
+ if (n)
+ {
+ _cupsSetError(IPP_STATUS_ERROR_INTERNAL, _("IPP member name is not empty."), 1);
+ DEBUG_puts("1ippReadIO: member name not empty.");
+ _cupsBufferRelease((char *)buffer);
+ return (IPP_STATE_ERROR);
+ }
+
+ if (ipp->current)
+ ipp->prev = ipp->current;
+
+ attr = ipp->current = ipp_add_attr(ipp, NULL, ipp->curtag, IPP_TAG_ZERO, 1);
+ if (!attr)
+ {
+ _cupsSetHTTPError(HTTP_STATUS_ERROR);
+ DEBUG_puts("1ippReadIO: unable to allocate attribute.");
+ _cupsBufferRelease((char *)buffer);
+ return (IPP_STATE_ERROR);
+ }
+
+ DEBUG_printf(("2ippReadIO: membername, ipp->current=%p, ipp->prev=%p", (void *)ipp->current, (void *)ipp->prev));
+
+ value = attr->values;
+ }
+ else if (tag != IPP_TAG_END_COLLECTION)
+ {
+ /*
+ * New attribute; read the name and add it...
+ */
+
+ if ((*cb)(src, buffer, (size_t)n) < n)
+ {
+ DEBUG_puts("1ippReadIO: unable to read name.");
+ _cupsBufferRelease((char *)buffer);
+ return (IPP_STATE_ERROR);
+ }
+
+ buffer[n] = '\0';
+
+ if (ipp->current)
+ ipp->prev = ipp->current;
+
+ if ((attr = ipp->current = ipp_add_attr(ipp, (char *)buffer, ipp->curtag, tag,
+ 1)) == NULL)
+ {
+ _cupsSetHTTPError(HTTP_STATUS_ERROR);
+ DEBUG_puts("1ippReadIO: unable to allocate attribute.");
+ _cupsBufferRelease((char *)buffer);
+ return (IPP_STATE_ERROR);
+ }
+
+ DEBUG_printf(("2ippReadIO: name=\"%s\", ipp->current=%p, ipp->prev=%p", buffer, (void *)ipp->current, (void *)ipp->prev));
+
+ value = attr->values;
+ }
+ else
+ {
+ attr = NULL;
+ value = NULL;
+ }
+
+ if ((*cb)(src, buffer, 2) < 2)
+ {
+ DEBUG_puts("1ippReadIO: unable to read value length.");
+ _cupsBufferRelease((char *)buffer);
+ return (IPP_STATE_ERROR);
+ }
+
+ n = (buffer[0] << 8) | buffer[1];
+ DEBUG_printf(("2ippReadIO: value length=%d", n));
+
+ if (n >= IPP_BUF_SIZE)
+ {
+ _cupsSetError(IPP_STATUS_ERROR_INTERNAL,
+ _("IPP value larger than 32767 bytes."), 1);
+ DEBUG_printf(("1ippReadIO: bad value length %d.", n));
+ _cupsBufferRelease((char *)buffer);
+ return (IPP_STATE_ERROR);
+ }
+
+ switch (tag)
+ {
+ case IPP_TAG_INTEGER :
+ case IPP_TAG_ENUM :
+ if (n != 4)
+ {
+ if (tag == IPP_TAG_INTEGER)
+ _cupsSetError(IPP_STATUS_ERROR_INTERNAL,
+ _("IPP integer value not 4 bytes."), 1);
+ else
+ _cupsSetError(IPP_STATUS_ERROR_INTERNAL,
+ _("IPP enum value not 4 bytes."), 1);
+ DEBUG_printf(("1ippReadIO: bad integer value length %d.", n));
+ _cupsBufferRelease((char *)buffer);
+ return (IPP_STATE_ERROR);
+ }
+
+ if ((*cb)(src, buffer, 4) < 4)
+ {
+ DEBUG_puts("1ippReadIO: Unable to read integer value.");
+ _cupsBufferRelease((char *)buffer);
+ return (IPP_STATE_ERROR);
+ }
+
+ n = (((((buffer[0] << 8) | buffer[1]) << 8) | buffer[2]) << 8) |
+ buffer[3];
+
+ if (attr->value_tag == IPP_TAG_RANGE)
+ value->range.lower = value->range.upper = n;
+ else
+ value->integer = n;
+ break;
+
+ case IPP_TAG_BOOLEAN :
+ if (n != 1)
+ {
+ _cupsSetError(IPP_STATUS_ERROR_INTERNAL, _("IPP boolean value not 1 byte."),
+ 1);
+ DEBUG_printf(("1ippReadIO: bad boolean value length %d.", n));
+ _cupsBufferRelease((char *)buffer);
+ return (IPP_STATE_ERROR);
+ }
+
+ if ((*cb)(src, buffer, 1) < 1)
+ {
+ DEBUG_puts("1ippReadIO: Unable to read boolean value.");
+ _cupsBufferRelease((char *)buffer);
+ return (IPP_STATE_ERROR);
+ }
+
+ value->boolean = (char)buffer[0];
+ break;
+
+ case IPP_TAG_NOVALUE :
+ case IPP_TAG_NOTSETTABLE :
+ case IPP_TAG_DELETEATTR :
+ case IPP_TAG_ADMINDEFINE :
+ /*
+ * These value types are not supposed to have values, however
+ * some vendors (Brother) do not implement IPP correctly and so
+ * we need to map non-empty values to text...
+ */
+
+ if (attr->value_tag == tag)
+ {
+ if (n == 0)
+ break;
+
+ attr->value_tag = IPP_TAG_TEXT;
+ }
+
+ case IPP_TAG_TEXT :
+ case IPP_TAG_NAME :
+ case IPP_TAG_KEYWORD :
+ case IPP_TAG_URI :
+ case IPP_TAG_URISCHEME :
+ case IPP_TAG_CHARSET :
+ case IPP_TAG_LANGUAGE :
+ case IPP_TAG_MIMETYPE :
+ if (n > 0)
+ {
+ if ((*cb)(src, buffer, (size_t)n) < n)
+ {
+ DEBUG_puts("1ippReadIO: unable to read string value.");
+ _cupsBufferRelease((char *)buffer);
+ return (IPP_STATE_ERROR);
+ }
+ }
+
+ buffer[n] = '\0';
+ value->string.text = _cupsStrAlloc((char *)buffer);
+ DEBUG_printf(("2ippReadIO: value=\"%s\"", value->string.text));
+ break;
+
+ case IPP_TAG_DATE :
+ if (n != 11)
+ {
+ _cupsSetError(IPP_STATUS_ERROR_INTERNAL, _("IPP date value not 11 bytes."), 1);
+ DEBUG_printf(("1ippReadIO: bad date value length %d.", n));
+ _cupsBufferRelease((char *)buffer);
+ return (IPP_STATE_ERROR);
+ }
+
+ if ((*cb)(src, value->date, 11) < 11)
+ {
+ DEBUG_puts("1ippReadIO: Unable to read date value.");
+ _cupsBufferRelease((char *)buffer);
+ return (IPP_STATE_ERROR);
+ }
+ break;
+
+ case IPP_TAG_RESOLUTION :
+ if (n != 9)
+ {
+ _cupsSetError(IPP_STATUS_ERROR_INTERNAL,
+ _("IPP resolution value not 9 bytes."), 1);
+ DEBUG_printf(("1ippReadIO: bad resolution value length %d.", n));
+ _cupsBufferRelease((char *)buffer);
+ return (IPP_STATE_ERROR);
+ }
+
+ if ((*cb)(src, buffer, 9) < 9)
+ {
+ DEBUG_puts("1ippReadIO: Unable to read resolution value.");
+ _cupsBufferRelease((char *)buffer);
+ return (IPP_STATE_ERROR);
+ }
+
+ value->resolution.xres =
+ (((((buffer[0] << 8) | buffer[1]) << 8) | buffer[2]) << 8) |
+ buffer[3];
+ value->resolution.yres =
+ (((((buffer[4] << 8) | buffer[5]) << 8) | buffer[6]) << 8) |
+ buffer[7];
+ value->resolution.units =
+ (ipp_res_t)buffer[8];
+ break;
+
+ case IPP_TAG_RANGE :
+ if (n != 8)
+ {
+ _cupsSetError(IPP_STATUS_ERROR_INTERNAL,
+ _("IPP rangeOfInteger value not 8 bytes."), 1);
+ DEBUG_printf(("1ippReadIO: bad rangeOfInteger value length "
+ "%d.", n));
+ _cupsBufferRelease((char *)buffer);
+ return (IPP_STATE_ERROR);
+ }
+
+ if ((*cb)(src, buffer, 8) < 8)
+ {
+ DEBUG_puts("1ippReadIO: Unable to read range value.");
+ _cupsBufferRelease((char *)buffer);
+ return (IPP_STATE_ERROR);
+ }
+
+ value->range.lower =
+ (((((buffer[0] << 8) | buffer[1]) << 8) | buffer[2]) << 8) |
+ buffer[3];
+ value->range.upper =
+ (((((buffer[4] << 8) | buffer[5]) << 8) | buffer[6]) << 8) |
+ buffer[7];
+ break;
+
+ case IPP_TAG_TEXTLANG :
+ case IPP_TAG_NAMELANG :
+ if (n < 4)
+ {
+ if (tag == IPP_TAG_TEXTLANG)
+ _cupsSetError(IPP_STATUS_ERROR_INTERNAL,
+ _("IPP textWithLanguage value less than "
+ "minimum 4 bytes."), 1);
+ else
+ _cupsSetError(IPP_STATUS_ERROR_INTERNAL,
+ _("IPP nameWithLanguage value less than "
+ "minimum 4 bytes."), 1);
+ DEBUG_printf(("1ippReadIO: bad stringWithLanguage value "
+ "length %d.", n));
+ _cupsBufferRelease((char *)buffer);
+ return (IPP_STATE_ERROR);
+ }
+
+ if ((*cb)(src, buffer, (size_t)n) < n)
+ {
+ DEBUG_puts("1ippReadIO: Unable to read string w/language "
+ "value.");
+ _cupsBufferRelease((char *)buffer);
+ return (IPP_STATE_ERROR);
+ }
+
+ bufptr = buffer;
+
+ /*
+ * text-with-language and name-with-language are composite
+ * values:
+ *
+ * language-length
+ * language
+ * text-length
+ * text
+ */
+
+ n = (bufptr[0] << 8) | bufptr[1];
+
+ if ((bufptr + 2 + n) >= (buffer + IPP_BUF_SIZE) || n >= (int)sizeof(string))
+ {
+ _cupsSetError(IPP_STATUS_ERROR_INTERNAL,
+ _("IPP language length overflows value."), 1);
+ DEBUG_printf(("1ippReadIO: bad language value length %d.",
+ n));
+ _cupsBufferRelease((char *)buffer);
+ return (IPP_STATE_ERROR);
+ }
+ else if (n >= IPP_MAX_LANGUAGE)
+ {
+ _cupsSetError(IPP_STATUS_ERROR_INTERNAL,
+ _("IPP language length too large."), 1);
+ DEBUG_printf(("1ippReadIO: bad language value length %d.",
+ n));
+ _cupsBufferRelease((char *)buffer);
+ return (IPP_STATE_ERROR);
+ }
+
+ memcpy(string, bufptr + 2, (size_t)n);
+ string[n] = '\0';
+
+ value->string.language = _cupsStrAlloc((char *)string);
+
+ bufptr += 2 + n;
+ n = (bufptr[0] << 8) | bufptr[1];
+
+ if ((bufptr + 2 + n) >= (buffer + IPP_BUF_SIZE))
+ {
+ _cupsSetError(IPP_STATUS_ERROR_INTERNAL,
+ _("IPP string length overflows value."), 1);
+ DEBUG_printf(("1ippReadIO: bad string value length %d.", n));
+ _cupsBufferRelease((char *)buffer);
+ return (IPP_STATE_ERROR);
+ }
+
+ bufptr[2 + n] = '\0';
+ value->string.text = _cupsStrAlloc((char *)bufptr + 2);
+ break;
+
+ case IPP_TAG_BEGIN_COLLECTION :
+ /*
+ * Oh, boy, here comes a collection value, so read it...
+ */
+
+ value->collection = ippNew();
+
+ if (n > 0)
+ {
+ _cupsSetError(IPP_STATUS_ERROR_INTERNAL,
+ _("IPP begCollection value not 0 bytes."), 1);
+ DEBUG_puts("1ippReadIO: begCollection tag with value length "
+ "> 0.");
+ _cupsBufferRelease((char *)buffer);
+ return (IPP_STATE_ERROR);
+ }
+
+ if (ippReadIO(src, cb, 1, ipp, value->collection) == IPP_STATE_ERROR)
+ {
+ DEBUG_puts("1ippReadIO: Unable to read collection value.");
+ _cupsBufferRelease((char *)buffer);
+ return (IPP_STATE_ERROR);
+ }
+ break;
+
+ case IPP_TAG_END_COLLECTION :
+ _cupsBufferRelease((char *)buffer);
+
+ if (n > 0)
+ {
+ _cupsSetError(IPP_STATUS_ERROR_INTERNAL,
+ _("IPP endCollection value not 0 bytes."), 1);
+ DEBUG_puts("1ippReadIO: endCollection tag with value length "
+ "> 0.");
+ return (IPP_STATE_ERROR);
+ }
+
+ DEBUG_puts("1ippReadIO: endCollection tag...");
+ return (ipp->state = IPP_STATE_DATA);
+
+ case IPP_TAG_MEMBERNAME :
+ /*
+ * The value the name of the member in the collection, which
+ * we need to carry over...
+ */
+
+ if (!attr)
+ {
+ _cupsSetError(IPP_STATUS_ERROR_INTERNAL,
+ _("IPP memberName with no attribute."), 1);
+ DEBUG_puts("1ippReadIO: Member name without attribute.");
+ _cupsBufferRelease((char *)buffer);
+ return (IPP_STATE_ERROR);
+ }
+ else if (n == 0)
+ {
+ _cupsSetError(IPP_STATUS_ERROR_INTERNAL,
+ _("IPP memberName value is empty."), 1);
+ DEBUG_puts("1ippReadIO: Empty member name value.");
+ _cupsBufferRelease((char *)buffer);
+ return (IPP_STATE_ERROR);
+ }
+ else if ((*cb)(src, buffer, (size_t)n) < n)
+ {
+ DEBUG_puts("1ippReadIO: Unable to read member name value.");
+ _cupsBufferRelease((char *)buffer);
+ return (IPP_STATE_ERROR);
+ }
+
+ buffer[n] = '\0';
+ attr->name = _cupsStrAlloc((char *)buffer);
+
+ /*
+ * Since collection members are encoded differently than
+ * regular attributes, make sure we don't start with an
+ * empty value...
+ */
+
+ attr->num_values --;
+
+ DEBUG_printf(("2ippReadIO: member name=\"%s\"", attr->name));
+ break;
+
+ default : /* Other unsupported values */
+ if (tag == IPP_TAG_STRING && n > IPP_MAX_LENGTH)
+ {
+ _cupsSetError(IPP_STATUS_ERROR_INTERNAL,
+ _("IPP octetString length too large."), 1);
+ DEBUG_printf(("1ippReadIO: bad octetString value length %d.",
+ n));
+ _cupsBufferRelease((char *)buffer);
+ return (IPP_STATE_ERROR);
+ }
+
+ value->unknown.length = n;
+
+ if (n > 0)
+ {
+ if ((value->unknown.data = malloc((size_t)n)) == NULL)
+ {
+ _cupsSetHTTPError(HTTP_STATUS_ERROR);
+ DEBUG_puts("1ippReadIO: Unable to allocate value");
+ _cupsBufferRelease((char *)buffer);
+ return (IPP_STATE_ERROR);
+ }
+
+ if ((*cb)(src, value->unknown.data, (size_t)n) < n)
+ {
+ DEBUG_puts("1ippReadIO: Unable to read unsupported value.");
+ _cupsBufferRelease((char *)buffer);
+ return (IPP_STATE_ERROR);
+ }
+ }
+ else
+ value->unknown.data = NULL;
+ break;
+ }
+
+ /*
+ * If blocking is disabled, stop here...
+ */
+
+ if (!blocking)
+ break;
+ }
+ break;
+
+ case IPP_STATE_DATA :
+ break;
+
+ default :
+ break; /* anti-compiler-warning-code */
+ }
+
+ DEBUG_printf(("1ippReadIO: returning ipp->state=%d.", ipp->state));
+ _cupsBufferRelease((char *)buffer);
+
+ return (ipp->state);
+}
+
+
+/*
+ * 'ippSetBoolean()' - Set a boolean value in an attribute.
+ *
+ * The @code ipp@ parameter refers to an IPP message previously created using
+ * the @link ippNew@, @link ippNewRequest@, or @link ippNewResponse@ functions.
+ *
+ * The @code attr@ parameter may be modified as a result of setting the value.
+ *
+ * The @code element@ parameter specifies which value to set from 0 to
+ * @code ippGetCount(attr)@.
+ *
+ * @since CUPS 1.6/macOS 10.8@
+ */
+
+int /* O - 1 on success, 0 on failure */
+ippSetBoolean(ipp_t *ipp, /* I - IPP message */
+ ipp_attribute_t **attr, /* IO - IPP attribute */
+ int element, /* I - Value number (0-based) */
+ int boolvalue)/* I - Boolean value */
+{
+ _ipp_value_t *value; /* Current value */
+
+
+ /*
+ * Range check input...
+ */
+
+ if (!ipp || !attr || !*attr || (*attr)->value_tag != IPP_TAG_BOOLEAN ||
+ element < 0 || element > (*attr)->num_values)
+ return (0);
+
+ /*
+ * Set the value and return...
+ */
+
+ if ((value = ipp_set_value(ipp, attr, element)) != NULL)
+ value->boolean = (char)boolvalue;
+
+ return (value != NULL);
+}
+
+
+/*
+ * 'ippSetCollection()' - Set a collection value in an attribute.
+ *
+ * The @code ipp@ parameter refers to an IPP message previously created using
+ * the @link ippNew@, @link ippNewRequest@, or @link ippNewResponse@ functions.
+ *
+ * The @code attr@ parameter may be modified as a result of setting the value.
+ *
+ * The @code element@ parameter specifies which value to set from 0 to
+ * @code ippGetCount(attr)@.
+ *
+ * @since CUPS 1.6/macOS 10.8@
+ */
+
+int /* O - 1 on success, 0 on failure */
+ippSetCollection(
+ ipp_t *ipp, /* I - IPP message */
+ ipp_attribute_t **attr, /* IO - IPP attribute */
+ int element, /* I - Value number (0-based) */
+ ipp_t *colvalue) /* I - Collection value */
+{
+ _ipp_value_t *value; /* Current value */
+
+
+ /*
+ * Range check input...
+ */
+
+ if (!ipp || !attr || !*attr || (*attr)->value_tag != IPP_TAG_BEGIN_COLLECTION ||
+ element < 0 || element > (*attr)->num_values || !colvalue)
+ return (0);
+
+ /*
+ * Set the value and return...
+ */
+
+ if ((value = ipp_set_value(ipp, attr, element)) != NULL)
+ {
+ if (value->collection)
+ ippDelete(value->collection);
+
+ value->collection = colvalue;
+ colvalue->use ++;
+ }
+
+ return (value != NULL);
+}
+
+
+/*
+ * 'ippSetDate()' - Set a dateTime value in an attribute.
+ *
+ * The @code ipp@ parameter refers to an IPP message previously created using
+ * the @link ippNew@, @link ippNewRequest@, or @link ippNewResponse@ functions.
+ *
+ * The @code attr@ parameter may be modified as a result of setting the value.
+ *
+ * The @code element@ parameter specifies which value to set from 0 to
+ * @code ippGetCount(attr)@.
+ *
+ * @since CUPS 1.6/macOS 10.8@
+ */
+
+int /* O - 1 on success, 0 on failure */
+ippSetDate(ipp_t *ipp, /* I - IPP message */
+ ipp_attribute_t **attr, /* IO - IPP attribute */
+ int element, /* I - Value number (0-based) */
+ const ipp_uchar_t *datevalue)/* I - dateTime value */
+{
+ _ipp_value_t *value; /* Current value */
+
+
+ /*
+ * Range check input...
+ */
+
+ if (!ipp || !attr || !*attr || (*attr)->value_tag != IPP_TAG_DATE ||
+ element < 0 || element > (*attr)->num_values || !datevalue)
+ return (0);
+
+ /*
+ * Set the value and return...
+ */
+
+ if ((value = ipp_set_value(ipp, attr, element)) != NULL)
+ memcpy(value->date, datevalue, sizeof(value->date));
+
+ return (value != NULL);
+}
+
+
+/*
+ * 'ippSetGroupTag()' - Set the group tag of an attribute.
+ *
+ * The @code ipp@ parameter refers to an IPP message previously created using
+ * the @link ippNew@, @link ippNewRequest@, or @link ippNewResponse@ functions.
+ *
+ * The @code attr@ parameter may be modified as a result of setting the value.
+ *
+ * The @code group@ parameter specifies the IPP attribute group tag: none
+ * (@code IPP_TAG_ZERO@, for member attributes), document (@code IPP_TAG_DOCUMENT@),
+ * event notification (@code IPP_TAG_EVENT_NOTIFICATION@), operation
+ * (@code IPP_TAG_OPERATION@), printer (@code IPP_TAG_PRINTER@), subscription
+ * (@code IPP_TAG_SUBSCRIPTION@), or unsupported (@code IPP_TAG_UNSUPPORTED_GROUP@).
+ *
+ * @since CUPS 1.6/macOS 10.8@
+ */
+
+int /* O - 1 on success, 0 on failure */
+ippSetGroupTag(
+ ipp_t *ipp, /* I - IPP message */
+ ipp_attribute_t **attr, /* IO - Attribute */
+ ipp_tag_t group_tag) /* I - Group tag */
+{
+ /*
+ * Range check input - group tag must be 0x01 to 0x0F, per RFC 8011...
+ */
+
+ if (!ipp || !attr || !*attr ||
+ group_tag < IPP_TAG_ZERO || group_tag == IPP_TAG_END ||
+ group_tag >= IPP_TAG_UNSUPPORTED_VALUE)
+ return (0);
+
+ /*
+ * Set the group tag and return...
+ */
+
+ (*attr)->group_tag = group_tag;
+
+ return (1);
+}
+
+
+/*
+ * 'ippSetInteger()' - Set an integer or enum value in an attribute.
+ *
+ * The @code ipp@ parameter refers to an IPP message previously created using
+ * the @link ippNew@, @link ippNewRequest@, or @link ippNewResponse@ functions.
+ *
+ * The @code attr@ parameter may be modified as a result of setting the value.
+ *
+ * The @code element@ parameter specifies which value to set from 0 to
+ * @code ippGetCount(attr)@.
+ *
+ * @since CUPS 1.6/macOS 10.8@
+ */
+
+int /* O - 1 on success, 0 on failure */
+ippSetInteger(ipp_t *ipp, /* I - IPP message */
+ ipp_attribute_t **attr, /* IO - IPP attribute */
+ int element, /* I - Value number (0-based) */
+ int intvalue) /* I - Integer/enum value */
+{
+ _ipp_value_t *value; /* Current value */
+
+
+ /*
+ * Range check input...
+ */
+
+ if (!ipp || !attr || !*attr ||
+ ((*attr)->value_tag != IPP_TAG_INTEGER && (*attr)->value_tag != IPP_TAG_ENUM) ||
+ element < 0 || element > (*attr)->num_values)
+ return (0);
+
+ /*
+ * Set the value and return...
+ */
+
+ if ((value = ipp_set_value(ipp, attr, element)) != NULL)
+ value->integer = intvalue;
+
+ return (value != NULL);
+}
+
+
+/*
+ * 'ippSetName()' - Set the name of an attribute.
+ *
+ * The @code ipp@ parameter refers to an IPP message previously created using
+ * the @link ippNew@, @link ippNewRequest@, or @link ippNewResponse@ functions.
+ *
+ * The @code attr@ parameter may be modified as a result of setting the value.
+ *
+ * @since CUPS 1.6/macOS 10.8@
+ */
+
+int /* O - 1 on success, 0 on failure */
+ippSetName(ipp_t *ipp, /* I - IPP message */
+ ipp_attribute_t **attr, /* IO - IPP attribute */
+ const char *name) /* I - Attribute name */
+{
+ char *temp; /* Temporary name value */
+
+
+ /*
+ * Range check input...
+ */
+
+ if (!ipp || !attr || !*attr)
+ return (0);
+
+ /*
+ * Set the value and return...
+ */
+
+ if ((temp = _cupsStrAlloc(name)) != NULL)
+ {
+ if ((*attr)->name)
+ _cupsStrFree((*attr)->name);
+
+ (*attr)->name = temp;
+ }
+
+ return (temp != NULL);
+}
+
+
+/*
+ * 'ippSetOctetString()' - Set an octetString value in an IPP attribute.
+ *
+ * The @code ipp@ parameter refers to an IPP message previously created using
+ * the @link ippNew@, @link ippNewRequest@, or @link ippNewResponse@ functions.
+ *
+ * The @code attr@ parameter may be modified as a result of setting the value.
+ *
+ * The @code element@ parameter specifies which value to set from 0 to
+ * @code ippGetCount(attr)@.
+ *
+ * @since CUPS 1.7/macOS 10.9@
+ */
+
+int /* O - 1 on success, 0 on failure */
+ippSetOctetString(
+ ipp_t *ipp, /* I - IPP message */
+ ipp_attribute_t **attr, /* IO - IPP attribute */
+ int element, /* I - Value number (0-based) */
+ const void *data, /* I - Pointer to octetString data */
+ int datalen) /* I - Length of octetString data */
+{
+ _ipp_value_t *value; /* Current value */
+
+
+ /*
+ * Range check input...
+ */
+
+ if (!ipp || !attr || !*attr || (*attr)->value_tag != IPP_TAG_STRING ||
+ element < 0 || element > (*attr)->num_values ||
+ datalen < 0 || datalen > IPP_MAX_LENGTH)
+ return (0);
+
+ /*
+ * Set the value and return...
+ */
+
+ if ((value = ipp_set_value(ipp, attr, element)) != NULL)
+ {
+ if ((int)((*attr)->value_tag) & IPP_TAG_CUPS_CONST)
+ {
+ /*
+ * Just copy the pointer...
+ */
+
+ value->unknown.data = (void *)data;
+ value->unknown.length = datalen;
+ }
+ else
+ {
+ /*
+ * Copy the data...
+ */
+
+ if (value->unknown.data)
+ {
+ /*
+ * Free previous data...
+ */
+
+ free(value->unknown.data);
+
+ value->unknown.data = NULL;
+ value->unknown.length = 0;
+ }
+
+ if (datalen > 0)
+ {
+ void *temp; /* Temporary data pointer */
+
+ if ((temp = malloc((size_t)datalen)) != NULL)
+ {
+ memcpy(temp, data, (size_t)datalen);
+
+ value->unknown.data = temp;
+ value->unknown.length = datalen;
+ }
+ else
+ return (0);
+ }
+ }
+ }
+
+ return (value != NULL);
+}
+
+
+/*
+ * 'ippSetOperation()' - Set the operation ID in an IPP request message.
+ *
+ * The @code ipp@ parameter refers to an IPP message previously created using
+ * the @link ippNew@, @link ippNewRequest@, or @link ippNewResponse@ functions.
+ *
+ * @since CUPS 1.6/macOS 10.8@
+ */
+
+int /* O - 1 on success, 0 on failure */
+ippSetOperation(ipp_t *ipp, /* I - IPP request message */
+ ipp_op_t op) /* I - Operation ID */
+{
+ /*
+ * Range check input...
+ */
+
+ if (!ipp)
+ return (0);
+
+ /*
+ * Set the operation and return...
+ */
+
+ ipp->request.op.operation_id = op;
+
+ return (1);
+}
+
+
+/*
+ * 'ippSetRange()' - Set a rangeOfInteger value in an attribute.
+ *
+ * The @code ipp@ parameter refers to an IPP message previously created using
+ * the @link ippNew@, @link ippNewRequest@, or @link ippNewResponse@ functions.
+ *
+ * The @code attr@ parameter may be modified as a result of setting the value.
+ *
+ * The @code element@ parameter specifies which value to set from 0 to
+ * @code ippGetCount(attr)@.
+ *
+ * @since CUPS 1.6/macOS 10.8@
+ */
+
+int /* O - 1 on success, 0 on failure */
+ippSetRange(ipp_t *ipp, /* I - IPP message */
+ ipp_attribute_t **attr, /* IO - IPP attribute */
+ int element, /* I - Value number (0-based) */
+ int lowervalue, /* I - Lower bound for range */
+ int uppervalue) /* I - Upper bound for range */
+{
+ _ipp_value_t *value; /* Current value */
+
+
+ /*
+ * Range check input...
+ */
+
+ if (!ipp || !attr || !*attr || (*attr)->value_tag != IPP_TAG_RANGE ||
+ element < 0 || element > (*attr)->num_values || lowervalue > uppervalue)
+ return (0);
+
+ /*
+ * Set the value and return...
+ */
+
+ if ((value = ipp_set_value(ipp, attr, element)) != NULL)
+ {
+ value->range.lower = lowervalue;
+ value->range.upper = uppervalue;
+ }
+
+ return (value != NULL);
+}
+
+
+/*
+ * 'ippSetRequestId()' - Set the request ID in an IPP message.
+ *
+ * The @code ipp@ parameter refers to an IPP message previously created using
+ * the @link ippNew@, @link ippNewRequest@, or @link ippNewResponse@ functions.
+ *
+ * The @code request_id@ parameter must be greater than 0.
+ *
+ * @since CUPS 1.6/macOS 10.8@
+ */
+
+int /* O - 1 on success, 0 on failure */
+ippSetRequestId(ipp_t *ipp, /* I - IPP message */
+ int request_id) /* I - Request ID */
+{
+ /*
+ * Range check input; not checking request_id values since ipptool wants to send
+ * invalid values for conformance testing and a bad request_id does not affect the
+ * encoding of a message...
+ */
+
+ if (!ipp)
+ return (0);
+
+ /*
+ * Set the request ID and return...
+ */
+
+ ipp->request.any.request_id = request_id;
+
+ return (1);
+}
+
+
+/*
+ * 'ippSetResolution()' - Set a resolution value in an attribute.
+ *
+ * The @code ipp@ parameter refers to an IPP message previously created using
+ * the @link ippNew@, @link ippNewRequest@, or @link ippNewResponse@ functions.
+ *
+ * The @code attr@ parameter may be modified as a result of setting the value.
+ *
+ * The @code element@ parameter specifies which value to set from 0 to
+ * @code ippGetCount(attr)@.
+ *
+ * @since CUPS 1.6/macOS 10.8@
+ */
+
+int /* O - 1 on success, 0 on failure */
+ippSetResolution(
+ ipp_t *ipp, /* I - IPP message */
+ ipp_attribute_t **attr, /* IO - IPP attribute */
+ int element, /* I - Value number (0-based) */
+ ipp_res_t unitsvalue, /* I - Resolution units */
+ int xresvalue, /* I - Horizontal/cross feed resolution */
+ int yresvalue) /* I - Vertical/feed resolution */
+{
+ _ipp_value_t *value; /* Current value */
+
+
+ /*
+ * Range check input...
+ */
+
+ if (!ipp || !attr || !*attr || (*attr)->value_tag != IPP_TAG_RESOLUTION ||
+ element < 0 || element > (*attr)->num_values || xresvalue <= 0 || yresvalue <= 0 ||
+ unitsvalue < IPP_RES_PER_INCH || unitsvalue > IPP_RES_PER_CM)
+ return (0);
+
+ /*
+ * Set the value and return...
+ */
+
+ if ((value = ipp_set_value(ipp, attr, element)) != NULL)
+ {
+ value->resolution.units = unitsvalue;
+ value->resolution.xres = xresvalue;
+ value->resolution.yres = yresvalue;
+ }
+
+ return (value != NULL);
+}
+
+
+/*
+ * 'ippSetState()' - Set the current state of the IPP message.
+ *
+ * @since CUPS 1.6/macOS 10.8@
+ */
+
+int /* O - 1 on success, 0 on failure */
+ippSetState(ipp_t *ipp, /* I - IPP message */
+ ipp_state_t state) /* I - IPP state value */
+{
+ /*
+ * Range check input...
+ */
+
+ if (!ipp)
+ return (0);
+
+ /*
+ * Set the state and return...
+ */
+
+ ipp->state = state;
+ ipp->current = NULL;
+
+ return (1);
+}
+
+
+/*
+ * 'ippSetStatusCode()' - Set the status code in an IPP response or event message.
+ *
+ * The @code ipp@ parameter refers to an IPP message previously created using
+ * the @link ippNew@, @link ippNewRequest@, or @link ippNewResponse@ functions.
+ *
+ * @since CUPS 1.6/macOS 10.8@
+ */
+
+int /* O - 1 on success, 0 on failure */
+ippSetStatusCode(ipp_t *ipp, /* I - IPP response or event message */
+ ipp_status_t status) /* I - Status code */
+{
+ /*
+ * Range check input...
+ */
+
+ if (!ipp)
+ return (0);
+
+ /*
+ * Set the status code and return...
+ */
+
+ ipp->request.status.status_code = status;
+
+ return (1);
+}
+
+
+/*
+ * 'ippSetString()' - Set a string value in an attribute.
+ *
+ * The @code ipp@ parameter refers to an IPP message previously created using
+ * the @link ippNew@, @link ippNewRequest@, or @link ippNewResponse@ functions.
+ *
+ * The @code attr@ parameter may be modified as a result of setting the value.
+ *
+ * The @code element@ parameter specifies which value to set from 0 to
+ * @code ippGetCount(attr)@.
+ *
+ * @since CUPS 1.6/macOS 10.8@
+ */
+
+int /* O - 1 on success, 0 on failure */
+ippSetString(ipp_t *ipp, /* I - IPP message */
+ ipp_attribute_t **attr, /* IO - IPP attribute */
+ int element, /* I - Value number (0-based) */
+ const char *strvalue) /* I - String value */
+{
+ char *temp; /* Temporary string */
+ _ipp_value_t *value; /* Current value */
+
+
+ /*
+ * Range check input...
+ */
+
+ if (!ipp || !attr || !*attr ||
+ ((*attr)->value_tag != IPP_TAG_TEXTLANG &&
+ (*attr)->value_tag != IPP_TAG_NAMELANG &&
+ ((*attr)->value_tag < IPP_TAG_TEXT ||
+ (*attr)->value_tag > IPP_TAG_MIMETYPE)) ||
+ element < 0 || element > (*attr)->num_values || !strvalue)
+ return (0);
+
+ /*
+ * Set the value and return...
+ */
+
+ if ((value = ipp_set_value(ipp, attr, element)) != NULL)
+ {
+ if (element > 0)
+ value->string.language = (*attr)->values[0].string.language;
+
+ if ((int)((*attr)->value_tag) & IPP_TAG_CUPS_CONST)
+ value->string.text = (char *)strvalue;
+ else if ((temp = _cupsStrAlloc(strvalue)) != NULL)
+ {
+ if (value->string.text)
+ _cupsStrFree(value->string.text);
+
+ value->string.text = temp;
+ }
+ else
+ return (0);
+ }
+
+ return (value != NULL);
+}
+
+
+/*
+ * 'ippSetStringf()' - Set a formatted string value of an attribute.
+ *
+ * The @code ipp@ parameter refers to an IPP message previously created using
+ * the @link ippNew@, @link ippNewRequest@, or @link ippNewResponse@ functions.
+ *
+ * The @code attr@ parameter may be modified as a result of setting the value.
+ *
+ * The @code element@ parameter specifies which value to set from 0 to
+ * @code ippGetCount(attr)@.
+ *
+ * The @code format@ parameter uses formatting characters compatible with the
+ * printf family of standard functions. Additional arguments follow it as
+ * needed. The formatted string is truncated as needed to the maximum length of
+ * the corresponding value type.
+ *
+ * @since CUPS 1.7/macOS 10.9@
+ */
+
+int /* O - 1 on success, 0 on failure */
+ippSetStringf(ipp_t *ipp, /* I - IPP message */
+ ipp_attribute_t **attr, /* IO - IPP attribute */
+ int element, /* I - Value number (0-based) */
+ const char *format, /* I - Printf-style format string */
+ ...) /* I - Additional arguments as needed */
+{
+ int ret; /* Return value */
+ va_list ap; /* Pointer to additional arguments */
+
+
+ va_start(ap, format);
+ ret = ippSetStringfv(ipp, attr, element, format, ap);
+ va_end(ap);
+
+ return (ret);
+}
+
+
+/*
+ * 'ippSetStringf()' - Set a formatted string value of an attribute.
+ *
+ * The @code ipp@ parameter refers to an IPP message previously created using
+ * the @link ippNew@, @link ippNewRequest@, or @link ippNewResponse@ functions.
+ *
+ * The @code attr@ parameter may be modified as a result of setting the value.
+ *
+ * The @code element@ parameter specifies which value to set from 0 to
+ * @code ippGetCount(attr)@.
+ *
+ * The @code format@ parameter uses formatting characters compatible with the
+ * printf family of standard functions. Additional arguments follow it as
+ * needed. The formatted string is truncated as needed to the maximum length of
+ * the corresponding value type.
+ *
+ * @since CUPS 1.7/macOS 10.9@
+ */
+
+int /* O - 1 on success, 0 on failure */
+ippSetStringfv(ipp_t *ipp, /* I - IPP message */
+ ipp_attribute_t **attr, /* IO - IPP attribute */
+ int element, /* I - Value number (0-based) */
+ const char *format, /* I - Printf-style format string */
+ va_list ap) /* I - Pointer to additional arguments */
+{
+ ipp_tag_t value_tag; /* Value tag */
+ char buffer[IPP_MAX_TEXT + 4];
+ /* Formatted text string */
+ ssize_t bytes, /* Length of formatted value */
+ max_bytes; /* Maximum number of bytes for value */
+
+
+ /*
+ * Range check input...
+ */
+
+ if (attr && *attr)
+ value_tag = (*attr)->value_tag & IPP_TAG_CUPS_MASK;
+ else
+ value_tag = IPP_TAG_ZERO;
+
+ if (!ipp || !attr || !*attr ||
+ (value_tag < IPP_TAG_TEXT && value_tag != IPP_TAG_TEXTLANG &&
+ value_tag != IPP_TAG_NAMELANG) || value_tag > IPP_TAG_MIMETYPE ||
+ !format)
+ return (0);
+
+ /*
+ * Format the string...
+ */
+
+ if (!strcmp(format, "%s"))
+ {
+ /*
+ * Optimize the simple case...
+ */
+
+ const char *s = va_arg(ap, char *);
+
+ if (!s)
+ s = "(null)";
+
+ bytes = (ssize_t)strlen(s);
+ strlcpy(buffer, s, sizeof(buffer));
+ }
+ else
+ {
+ /*
+ * Do a full formatting of the message...
+ */
+
+ if ((bytes = vsnprintf(buffer, sizeof(buffer), format, ap)) < 0)
+ return (0);
+ }
+
+ /*
+ * Limit the length of the string...
+ */
+
+ switch (value_tag)
+ {
+ default :
+ case IPP_TAG_TEXT :
+ case IPP_TAG_TEXTLANG :
+ max_bytes = IPP_MAX_TEXT;
+ break;
+
+ case IPP_TAG_NAME :
+ case IPP_TAG_NAMELANG :
+ max_bytes = IPP_MAX_NAME;
+ break;
+
+ case IPP_TAG_CHARSET :
+ max_bytes = IPP_MAX_CHARSET;
+ break;
+
+ case IPP_TAG_KEYWORD :
+ max_bytes = IPP_MAX_KEYWORD;
+ break;
+
+ case IPP_TAG_LANGUAGE :
+ max_bytes = IPP_MAX_LANGUAGE;
+ break;
+
+ case IPP_TAG_MIMETYPE :
+ max_bytes = IPP_MAX_MIMETYPE;
+ break;
+
+ case IPP_TAG_URI :
+ max_bytes = IPP_MAX_URI;
+ break;
+
+ case IPP_TAG_URISCHEME :
+ max_bytes = IPP_MAX_URISCHEME;
+ break;
+ }
+
+ if (bytes >= max_bytes)
+ {
+ char *bufmax, /* Buffer at max_bytes */
+ *bufptr; /* Pointer into buffer */
+
+ bufptr = buffer + strlen(buffer) - 1;
+ bufmax = buffer + max_bytes - 1;
+
+ while (bufptr > bufmax)
+ {
+ if (*bufptr & 0x80)
+ {
+ while ((*bufptr & 0xc0) == 0x80 && bufptr > buffer)
+ bufptr --;
+ }
+
+ bufptr --;
+ }
+
+ *bufptr = '\0';
+ }
+
+ /*
+ * Set the formatted string and return...
+ */
+
+ return (ippSetString(ipp, attr, element, buffer));
+}
+
+
+/*
+ * 'ippSetValueTag()' - Set the value tag of an attribute.
+ *
+ * The @code ipp@ parameter refers to an IPP message previously created using
+ * the @link ippNew@, @link ippNewRequest@, or @link ippNewResponse@ functions.
+ *
+ * The @code attr@ parameter may be modified as a result of setting the value.
+ *
+ * Integer (@code IPP_TAG_INTEGER@) values can be promoted to rangeOfInteger
+ * (@code IPP_TAG_RANGE@) values, the various string tags can be promoted to name
+ * (@code IPP_TAG_NAME@) or nameWithLanguage (@code IPP_TAG_NAMELANG@) values, text
+ * (@code IPP_TAG_TEXT@) values can be promoted to textWithLanguage
+ * (@code IPP_TAG_TEXTLANG@) values, and all values can be demoted to the various
+ * out-of-band value tags such as no-value (@code IPP_TAG_NOVALUE@). All other changes
+ * will be rejected.
+ *
+ * Promoting a string attribute to nameWithLanguage or textWithLanguage adds the language
+ * code in the "attributes-natural-language" attribute or, if not present, the language
+ * code for the current locale.
+ *
+ * @since CUPS 1.6/macOS 10.8@
+ */
+
+int /* O - 1 on success, 0 on failure */
+ippSetValueTag(
+ ipp_t *ipp, /* I - IPP message */
+ ipp_attribute_t **attr, /* IO - IPP attribute */
+ ipp_tag_t value_tag) /* I - Value tag */
+{
+ int i; /* Looping var */
+ _ipp_value_t *value; /* Current value */
+ int integer; /* Current integer value */
+ cups_lang_t *language; /* Current language */
+ char code[32]; /* Language code */
+ ipp_tag_t temp_tag; /* Temporary value tag */
+
+
+ /*
+ * Range check input...
+ */
+
+ if (!ipp || !attr || !*attr)
+ return (0);
+
+ /*
+ * If there is no change, return immediately...
+ */
+
+ if (value_tag == (*attr)->value_tag)
+ return (1);
+
+ /*
+ * Otherwise implement changes as needed...
+ */
+
+ temp_tag = (ipp_tag_t)((int)((*attr)->value_tag) & IPP_TAG_CUPS_MASK);
+
+ switch (value_tag)
+ {
+ case IPP_TAG_UNSUPPORTED_VALUE :
+ case IPP_TAG_DEFAULT :
+ case IPP_TAG_UNKNOWN :
+ case IPP_TAG_NOVALUE :
+ case IPP_TAG_NOTSETTABLE :
+ case IPP_TAG_DELETEATTR :
+ case IPP_TAG_ADMINDEFINE :
+ /*
+ * Free any existing values...
+ */
+
+ if ((*attr)->num_values > 0)
+ ipp_free_values(*attr, 0, (*attr)->num_values);
+
+ /*
+ * Set out-of-band value...
+ */
+
+ (*attr)->value_tag = value_tag;
+ break;
+
+ case IPP_TAG_RANGE :
+ if (temp_tag != IPP_TAG_INTEGER)
+ return (0);
+
+ for (i = (*attr)->num_values, value = (*attr)->values;
+ i > 0;
+ i --, value ++)
+ {
+ integer = value->integer;
+ value->range.lower = value->range.upper = integer;
+ }
+
+ (*attr)->value_tag = IPP_TAG_RANGE;
+ break;
+
+ case IPP_TAG_NAME :
+ if (temp_tag != IPP_TAG_KEYWORD && temp_tag != IPP_TAG_URI &&
+ temp_tag != IPP_TAG_URISCHEME && temp_tag != IPP_TAG_LANGUAGE &&
+ temp_tag != IPP_TAG_MIMETYPE)
+ return (0);
+
+ (*attr)->value_tag = (ipp_tag_t)(IPP_TAG_NAME | ((*attr)->value_tag & IPP_TAG_CUPS_CONST));
+ break;
+
+ case IPP_TAG_NAMELANG :
+ case IPP_TAG_TEXTLANG :
+ if (value_tag == IPP_TAG_NAMELANG &&
+ (temp_tag != IPP_TAG_NAME && temp_tag != IPP_TAG_KEYWORD &&
+ temp_tag != IPP_TAG_URI && temp_tag != IPP_TAG_URISCHEME &&
+ temp_tag != IPP_TAG_LANGUAGE && temp_tag != IPP_TAG_MIMETYPE))
+ return (0);
+
+ if (value_tag == IPP_TAG_TEXTLANG && temp_tag != IPP_TAG_TEXT)
+ return (0);
+
+ if (ipp->attrs && ipp->attrs->next && ipp->attrs->next->name &&
+ !strcmp(ipp->attrs->next->name, "attributes-natural-language"))
+ {
+ /*
+ * Use the language code from the IPP message...
+ */
+
+ (*attr)->values[0].string.language =
+ _cupsStrAlloc(ipp->attrs->next->values[0].string.text);
+ }
+ else
+ {
+ /*
+ * Otherwise, use the language code corresponding to the locale...
+ */
+
+ language = cupsLangDefault();
+ (*attr)->values[0].string.language = _cupsStrAlloc(ipp_lang_code(language->language,
+ code,
+ sizeof(code)));
+ }
+
+ for (i = (*attr)->num_values - 1, value = (*attr)->values + 1;
+ i > 0;
+ i --, value ++)
+ value->string.language = (*attr)->values[0].string.language;
+
+ if ((int)(*attr)->value_tag & IPP_TAG_CUPS_CONST)
+ {
+ /*
+ * Make copies of all values...
+ */
+
+ for (i = (*attr)->num_values, value = (*attr)->values;
+ i > 0;
+ i --, value ++)
+ value->string.text = _cupsStrAlloc(value->string.text);
+ }
+
+ (*attr)->value_tag = IPP_TAG_NAMELANG;
+ break;