#include "cups-private.h"
#ifdef WIN32
# include <io.h>
#endif
static ipp_attribute_t *ipp_add_attr(ipp_t *ipp, const char *name,
ipp_tag_t group_tag, ipp_tag_t value_tag,
int num_values);
static void ipp_free_values(ipp_attribute_t *attr, int element,
int count);
static char *ipp_get_code(const char *locale, char *buffer,
size_t bufsize)
__attribute__((nonnull(1,2)));
static char *ipp_lang_code(const char *locale, char *buffer,
size_t bufsize)
__attribute__((nonnull(1,2)));
static size_t ipp_length(ipp_t *ipp, int collection);
static ssize_t ipp_read_http(http_t *http, ipp_uchar_t *buffer,
size_t length);
static ssize_t ipp_read_file(int *fd, ipp_uchar_t *buffer,
size_t length);
static _ipp_value_t *ipp_set_value(ipp_t *ipp, ipp_attribute_t **attr,
int element);
static ssize_t ipp_write_file(int *fd, ipp_uchar_t *buffer,
size_t length);
char *
_cupsBufferGet(size_t size)
{
_cups_buffer_t *buffer;
_cups_globals_t *cg = _cupsGlobals();
for (buffer = cg->cups_buffers; buffer; buffer = buffer->next)
if (!buffer->used && buffer->size >= size)
break;
if (!buffer)
{
if ((buffer = malloc(sizeof(_cups_buffer_t) + size - 1)) == NULL)
return (NULL);
buffer->next = cg->cups_buffers;
buffer->size = size;
cg->cups_buffers = buffer;
}
buffer->used = 1;
return (buffer->d);
}
void
_cupsBufferRelease(char *b)
{
_cups_buffer_t *buffer;
buffer = (_cups_buffer_t *)(b - offsetof(_cups_buffer_t, d));
buffer->used = 0;
}
ipp_attribute_t *
ippAddBoolean(ipp_t *ipp,
ipp_tag_t group,
const char *name,
char value)
{
ipp_attribute_t *attr;
DEBUG_printf(("ippAddBoolean(ipp=%p, group=%02x(%s), name=\"%s\", value=%d)",
ipp, group, ippTagString(group), name, value));
if (!ipp || !name || group < IPP_TAG_ZERO ||
group == IPP_TAG_END || group >= IPP_TAG_UNSUPPORTED_VALUE)
return (NULL);
if ((attr = ipp_add_attr(ipp, name, group, IPP_TAG_BOOLEAN, 1)) == NULL)
return (NULL);
attr->values[0].boolean = value;
return (attr);
}
ipp_attribute_t *
ippAddBooleans(ipp_t *ipp,
ipp_tag_t group,
const char *name,
int num_values,
const char *values)
{
int i;
ipp_attribute_t *attr;
_ipp_value_t *value;
DEBUG_printf(("ippAddBooleans(ipp=%p, group=%02x(%s), name=\"%s\", "
"num_values=%d, values=%p)", ipp, group, ippTagString(group),
name, num_values, values));
if (!ipp || !name || group < IPP_TAG_ZERO ||
group == IPP_TAG_END || group >= IPP_TAG_UNSUPPORTED_VALUE ||
num_values < 1)
return (NULL);
if ((attr = ipp_add_attr(ipp, name, group, IPP_TAG_BOOLEAN, num_values)) == NULL)
return (NULL);
if (values)
{
for (i = num_values, value = attr->values;
i > 0;
i --, value ++)
value->boolean = *values++;
}
return (attr);
}
ipp_attribute_t *
ippAddCollection(ipp_t *ipp,
ipp_tag_t group,
const char *name,
ipp_t *value)
{
ipp_attribute_t *attr;
DEBUG_printf(("ippAddCollection(ipp=%p, group=%02x(%s), name=\"%s\", "
"value=%p)", ipp, group, ippTagString(group), name, value));
if (!ipp || !name || group < IPP_TAG_ZERO ||
group == IPP_TAG_END || group >= IPP_TAG_UNSUPPORTED_VALUE)
return (NULL);
if ((attr = ipp_add_attr(ipp, name, group, IPP_TAG_BEGIN_COLLECTION, 1)) == NULL)
return (NULL);
attr->values[0].collection = value;
if (value)
value->use ++;
return (attr);
}
ipp_attribute_t *
ippAddCollections(
ipp_t *ipp,
ipp_tag_t group,
const char *name,
int num_values,
const ipp_t **values)
{
int i;
ipp_attribute_t *attr;
_ipp_value_t *value;
DEBUG_printf(("ippAddCollections(ipp=%p, group=%02x(%s), name=\"%s\", "
"num_values=%d, values=%p)", ipp, group, ippTagString(group),
name, num_values, values));
if (!ipp || !name || group < IPP_TAG_ZERO ||
group == IPP_TAG_END || group >= IPP_TAG_UNSUPPORTED_VALUE ||
num_values < 1)
return (NULL);
if ((attr = ipp_add_attr(ipp, name, group, IPP_TAG_BEGIN_COLLECTION,
num_values)) == NULL)
return (NULL);
if (values)
{
for (i = num_values, value = attr->values;
i > 0;
i --, value ++)
{
value->collection = (ipp_t *)*values++;
value->collection->use ++;
}
}
return (attr);
}
ipp_attribute_t *
ippAddDate(ipp_t *ipp,
ipp_tag_t group,
const char *name,
const ipp_uchar_t *value)
{
ipp_attribute_t *attr;
DEBUG_printf(("ippAddDate(ipp=%p, group=%02x(%s), name=\"%s\", value=%p)",
ipp, group, ippTagString(group), name, value));
if (!ipp || !name || !value || group < IPP_TAG_ZERO ||
group == IPP_TAG_END || group >= IPP_TAG_UNSUPPORTED_VALUE)
return (NULL);
if ((attr = ipp_add_attr(ipp, name, group, IPP_TAG_DATE, 1)) == NULL)
return (NULL);
memcpy(attr->values[0].date, value, 11);
return (attr);
}
ipp_attribute_t *
ippAddInteger(ipp_t *ipp,
ipp_tag_t group,
ipp_tag_t value_tag,
const char *name,
int value)
{
ipp_attribute_t *attr;
DEBUG_printf(("ippAddInteger(ipp=%p, group=%02x(%s), type=%02x(%s), "
"name=\"%s\", value=%d)", ipp, group, ippTagString(group),
value_tag, ippTagString(value_tag), name, value));
value_tag &= IPP_TAG_MASK;
if (value_tag >= IPP_TAG_UNSUPPORTED_VALUE && value_tag <= IPP_TAG_ADMINDEFINE)
return (ippAddOutOfBand(ipp, group, value_tag, name));
#if 0
if (!ipp || !name || group < IPP_TAG_ZERO ||
group == IPP_TAG_END || group >= IPP_TAG_UNSUPPORTED_VALUE ||
(value_tag != IPP_TAG_INTEGER && value_tag != IPP_TAG_ENUM))
return (NULL);
#else
if (!ipp || !name || group < IPP_TAG_ZERO ||
group == IPP_TAG_END || group >= IPP_TAG_UNSUPPORTED_VALUE)
return (NULL);
#endif
if ((attr = ipp_add_attr(ipp, name, group, value_tag, 1)) == NULL)
return (NULL);
attr->values[0].integer = value;
return (attr);
}
ipp_attribute_t *
ippAddIntegers(ipp_t *ipp,
ipp_tag_t group,
ipp_tag_t value_tag,
const char *name,
int num_values,
const int *values)
{
int i;
ipp_attribute_t *attr;
_ipp_value_t *value;
DEBUG_printf(("ippAddIntegers(ipp=%p, group=%02x(%s), type=%02x(%s), "
"name=\"%s\", num_values=%d, values=%p)", ipp,
group, ippTagString(group), value_tag, ippTagString(value_tag), name,
num_values, values));
value_tag &= IPP_TAG_MASK;
#if 0
if (!ipp || !name || group < IPP_TAG_ZERO ||
group == IPP_TAG_END || group >= IPP_TAG_UNSUPPORTED_VALUE ||
(value_tag != IPP_TAG_INTEGER && value_tag != IPP_TAG_ENUM) ||
num_values < 1)
return (NULL);
#else
if (!ipp || !name || group < IPP_TAG_ZERO ||
group == IPP_TAG_END || group >= IPP_TAG_UNSUPPORTED_VALUE ||
num_values < 1)
return (NULL);
#endif
if ((attr = ipp_add_attr(ipp, name, group, value_tag, num_values)) == NULL)
return (NULL);
if (values)
{
for (i = num_values, value = attr->values;
i > 0;
i --, value ++)
value->integer = *values++;
}
return (attr);
}
ipp_attribute_t *
ippAddOctetString(ipp_t *ipp,
ipp_tag_t group,
const char *name,
const void *data,
int datalen)
{
ipp_attribute_t *attr;
if (!ipp || !name || group < IPP_TAG_ZERO ||
group == IPP_TAG_END || group >= IPP_TAG_UNSUPPORTED_VALUE)
return (NULL);
if ((attr = ipp_add_attr(ipp, name, group, IPP_TAG_STRING, 1)) == NULL)
return (NULL);
attr->values[0].unknown.length = datalen;
if (data)
{
if ((attr->values[0].unknown.data = malloc(datalen)) == NULL)
{
ippDeleteAttribute(ipp, attr);
return (NULL);
}
memcpy(attr->values[0].unknown.data, data, datalen);
}
return (attr);
}
ipp_attribute_t *
ippAddOutOfBand(ipp_t *ipp,
ipp_tag_t group,
ipp_tag_t value_tag,
const char *name)
{
DEBUG_printf(("ippAddOutOfBand(ipp=%p, group=%02x(%s), value_tag=%02x(%s), "
"name=\"%s\")", ipp, group, ippTagString(group), value_tag,
ippTagString(value_tag), name));
value_tag &= IPP_TAG_MASK;
if (!ipp || !name || group < IPP_TAG_ZERO ||
group == IPP_TAG_END || group >= IPP_TAG_UNSUPPORTED_VALUE ||
(value_tag != IPP_TAG_UNSUPPORTED_VALUE &&
value_tag != IPP_TAG_DEFAULT &&
value_tag != IPP_TAG_UNKNOWN &&
value_tag != IPP_TAG_NOVALUE &&
value_tag != IPP_TAG_NOTSETTABLE &&
value_tag != IPP_TAG_DELETEATTR &&
value_tag != IPP_TAG_ADMINDEFINE))
return (NULL);
return (ipp_add_attr(ipp, name, group, value_tag, 1));
}
ipp_attribute_t *
ippAddRange(ipp_t *ipp,
ipp_tag_t group,
const char *name,
int lower,
int upper)
{
ipp_attribute_t *attr;
DEBUG_printf(("ippAddRange(ipp=%p, group=%02x(%s), name=\"%s\", lower=%d, "
"upper=%d)", ipp, group, ippTagString(group), name, lower,
upper));
if (!ipp || !name || group < IPP_TAG_ZERO ||
group == IPP_TAG_END || group >= IPP_TAG_UNSUPPORTED_VALUE)
return (NULL);
if ((attr = ipp_add_attr(ipp, name, group, IPP_TAG_RANGE, 1)) == NULL)
return (NULL);
attr->values[0].range.lower = lower;
attr->values[0].range.upper = upper;
return (attr);
}
ipp_attribute_t *
ippAddRanges(ipp_t *ipp,
ipp_tag_t group,
const char *name,
int num_values,
const int *lower,
const int *upper)
{
int i;
ipp_attribute_t *attr;
_ipp_value_t *value;
DEBUG_printf(("ippAddRanges(ipp=%p, group=%02x(%s), name=\"%s\", "
"num_values=%d, lower=%p, upper=%p)", ipp, group,
ippTagString(group), name, num_values, lower, upper));
if (!ipp || !name || group < IPP_TAG_ZERO ||
group == IPP_TAG_END || group >= IPP_TAG_UNSUPPORTED_VALUE ||
num_values < 1)
return (NULL);
if ((attr = ipp_add_attr(ipp, name, group, IPP_TAG_RANGE, num_values)) == NULL)
return (NULL);
if (lower && upper)
{
for (i = num_values, value = attr->values;
i > 0;
i --, value ++)
{
value->range.lower = *lower++;
value->range.upper = *upper++;
}
}
return (attr);
}
ipp_attribute_t *
ippAddResolution(ipp_t *ipp,
ipp_tag_t group,
const char *name,
ipp_res_t units,
int xres,
int yres)
{
ipp_attribute_t *attr;
DEBUG_printf(("ippAddResolution(ipp=%p, group=%02x(%s), name=\"%s\", "
"units=%d, xres=%d, yres=%d)", ipp, group,
ippTagString(group), name, units, xres, yres));
if (!ipp || !name || group < IPP_TAG_ZERO ||
group == IPP_TAG_END || group >= IPP_TAG_UNSUPPORTED_VALUE ||
units < IPP_RES_PER_INCH || units > IPP_RES_PER_CM ||
xres < 0 || yres < 0)
return (NULL);
if ((attr = ipp_add_attr(ipp, name, group, IPP_TAG_RESOLUTION, 1)) == NULL)
return (NULL);
attr->values[0].resolution.xres = xres;
attr->values[0].resolution.yres = yres;
attr->values[0].resolution.units = units;
return (attr);
}
ipp_attribute_t *
ippAddResolutions(ipp_t *ipp,
ipp_tag_t group,
const char *name,
int num_values,
ipp_res_t units,
const int *xres,
const int *yres)
{
int i;
ipp_attribute_t *attr;
_ipp_value_t *value;
DEBUG_printf(("ippAddResolutions(ipp=%p, group=%02x(%s), name=\"%s\", "
"num_value=%d, units=%d, xres=%p, yres=%p)", ipp, group,
ippTagString(group), name, num_values, units, xres, yres));
if (!ipp || !name || group < IPP_TAG_ZERO ||
group == IPP_TAG_END || group >= IPP_TAG_UNSUPPORTED_VALUE ||
num_values < 1 ||
units < IPP_RES_PER_INCH || units > IPP_RES_PER_CM)
return (NULL);
if ((attr = ipp_add_attr(ipp, name, group, IPP_TAG_RESOLUTION, num_values)) == NULL)
return (NULL);
if (xres && yres)
{
for (i = num_values, value = attr->values;
i > 0;
i --, value ++)
{
value->resolution.xres = *xres++;
value->resolution.yres = *yres++;
value->resolution.units = units;
}
}
return (attr);
}
ipp_attribute_t *
ippAddSeparator(ipp_t *ipp)
{
DEBUG_printf(("ippAddSeparator(ipp=%p)", ipp));
if (!ipp)
return (NULL);
return (ipp_add_attr(ipp, NULL, IPP_TAG_ZERO, IPP_TAG_ZERO, 0));
}
ipp_attribute_t *
ippAddString(ipp_t *ipp,
ipp_tag_t group,
ipp_tag_t value_tag,
const char *name,
const char *language,
const char *value)
{
ipp_tag_t temp_tag;
ipp_attribute_t *attr;
char code[32];
DEBUG_printf(("ippAddString(ipp=%p, group=%02x(%s), value_tag=%02x(%s), "
"name=\"%s\", language=\"%s\", value=\"%s\")", ipp,
group, ippTagString(group), value_tag, ippTagString(value_tag), name,
language, value));
temp_tag = (ipp_tag_t)((int)value_tag & IPP_TAG_MASK);
#if 0
if (!ipp || !name || group < IPP_TAG_ZERO ||
group == IPP_TAG_END || group >= IPP_TAG_UNSUPPORTED_VALUE ||
(temp_tag < IPP_TAG_TEXT && temp_tag != IPP_TAG_TEXTLANG &&
temp_tag != IPP_TAG_NAMELANG) || temp_tag > IPP_TAG_MIMETYPE)
return (NULL);
if ((temp_tag == IPP_TAG_TEXTLANG || temp_tag == IPP_TAG_NAMELANG)
!= (language != NULL))
return (NULL);
#else
if (!ipp || !name || group < IPP_TAG_ZERO ||
group == IPP_TAG_END || group >= IPP_TAG_UNSUPPORTED_VALUE)
return (NULL);
#endif
if (language && ((int)value_tag & IPP_TAG_COPY) &&
strcmp(language, ipp_lang_code(language, code, sizeof(code))))
value_tag = temp_tag;
else if (value && value_tag == (ipp_tag_t)(IPP_TAG_CHARSET | IPP_TAG_COPY) &&
strcmp(value, ipp_get_code(value, code, sizeof(code))))
value_tag = temp_tag;
else if (value && value_tag == (ipp_tag_t)(IPP_TAG_LANGUAGE | IPP_TAG_COPY) &&
strcmp(value, ipp_lang_code(value, code, sizeof(code))))
value_tag = temp_tag;
if ((attr = ipp_add_attr(ipp, name, group, value_tag, 1)) == NULL)
return (NULL);
if ((int)value_tag & IPP_TAG_COPY)
{
attr->values[0].string.language = (char *)language;
attr->values[0].string.text = (char *)value;
}
else
{
if (language)
attr->values[0].string.language = _cupsStrAlloc(ipp_lang_code(language, code,
sizeof(code)));
if (value)
{
if (value_tag == IPP_TAG_CHARSET)
attr->values[0].string.text = _cupsStrAlloc(ipp_get_code(value, code,
sizeof(code)));
else if (value_tag == IPP_TAG_LANGUAGE)
attr->values[0].string.text = _cupsStrAlloc(ipp_lang_code(value, code,
sizeof(code)));
else
attr->values[0].string.text = _cupsStrAlloc(value);
}
}
return (attr);
}
ipp_attribute_t *
ippAddStrings(
ipp_t *ipp,
ipp_tag_t group,
ipp_tag_t value_tag,
const char *name,
int num_values,
const char *language,
const char * const *values)
{
int i;
ipp_tag_t temp_tag;
ipp_attribute_t *attr;
_ipp_value_t *value;
char code[32];
DEBUG_printf(("ippAddStrings(ipp=%p, group=%02x(%s), value_tag=%02x(%s), "
"name=\"%s\", num_values=%d, language=\"%s\", values=%p)", ipp,
group, ippTagString(group), value_tag, ippTagString(value_tag), name,
num_values, language, values));
temp_tag = (ipp_tag_t)((int)value_tag & IPP_TAG_MASK);
#if 0
if (!ipp || !name || group < IPP_TAG_ZERO ||
group == IPP_TAG_END || group >= IPP_TAG_UNSUPPORTED_VALUE ||
(temp_tag < IPP_TAG_TEXT && temp_tag != IPP_TAG_TEXTLANG &&
temp_tag != IPP_TAG_NAMELANG) || temp_tag > IPP_TAG_MIMETYPE ||
num_values < 1)
return (NULL);
if ((temp_tag == IPP_TAG_TEXTLANG || temp_tag == IPP_TAG_NAMELANG)
!= (language != NULL))
return (NULL);
#else
if (!ipp || !name || group < IPP_TAG_ZERO ||
group == IPP_TAG_END || group >= IPP_TAG_UNSUPPORTED_VALUE ||
num_values < 1)
return (NULL);
#endif
if (language && ((int)value_tag & IPP_TAG_COPY) &&
strcmp(language, ipp_lang_code(language, code, sizeof(code))))
value_tag = temp_tag;
else if (values && value_tag == (ipp_tag_t)(IPP_TAG_CHARSET | IPP_TAG_COPY))
{
for (i = 0; i < num_values; i ++)
if (strcmp(values[i], ipp_get_code(values[i], code, sizeof(code))))
{
value_tag = temp_tag;
break;
}
}
else if (values && value_tag == (ipp_tag_t)(IPP_TAG_LANGUAGE | IPP_TAG_COPY))
{
for (i = 0; i < num_values; i ++)
if (strcmp(values[i], ipp_lang_code(values[i], code, sizeof(code))))
{
value_tag = temp_tag;
break;
}
}
if ((attr = ipp_add_attr(ipp, name, group, value_tag, num_values)) == NULL)
return (NULL);
for (i = num_values, value = attr->values;
i > 0;
i --, value ++)
{
if (language)
{
if (value == attr->values)
{
if ((int)value_tag & IPP_TAG_COPY)
value->string.language = (char *)language;
else
value->string.language = _cupsStrAlloc(ipp_lang_code(language, code,
sizeof(code)));
}
else
value->string.language = attr->values[0].string.language;
}
if (values)
{
if ((int)value_tag & IPP_TAG_COPY)
value->string.text = (char *)*values++;
else if (value_tag == IPP_TAG_CHARSET)
value->string.text = _cupsStrAlloc(ipp_get_code(*values++, code, sizeof(code)));
else if (value_tag == IPP_TAG_LANGUAGE)
value->string.text = _cupsStrAlloc(ipp_lang_code(*values++, code, sizeof(code)));
else
value->string.text = _cupsStrAlloc(*values++);
}
}
return (attr);
}
ipp_attribute_t *
ippCopyAttribute(
ipp_t *dst,
ipp_attribute_t *srcattr,
int quickcopy)
{
int i;
ipp_attribute_t *dstattr;
_ipp_value_t *srcval,
*dstval;
DEBUG_printf(("ippCopyAttribute(dst=%p, srcattr=%p, quickcopy=%d)", dst, srcattr,
quickcopy));
if (!dst || !srcattr)
return (NULL);
quickcopy = quickcopy ? IPP_TAG_COPY : 0;
switch (srcattr->value_tag & ~IPP_TAG_COPY)
{
case IPP_TAG_ZERO :
dstattr = ippAddSeparator(dst);
break;
case IPP_TAG_INTEGER :
case IPP_TAG_ENUM :
dstattr = ippAddIntegers(dst, srcattr->group_tag, srcattr->value_tag,
srcattr->name, srcattr->num_values, NULL);
if (!dstattr)
break;
for (i = srcattr->num_values, srcval = srcattr->values, dstval = dstattr->values;
i > 0;
i --, srcval ++, dstval ++)
dstval->integer = srcval->integer;
break;
case IPP_TAG_BOOLEAN :
dstattr = ippAddBooleans(dst, srcattr->group_tag, srcattr->name,
srcattr->num_values, NULL);
if (!dstattr)
break;
for (i = srcattr->num_values, srcval = srcattr->values, dstval = dstattr->values;
i > 0;
i --, srcval ++, dstval ++)
dstval->boolean = srcval->boolean;
break;
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 :
dstattr = ippAddStrings(dst, srcattr->group_tag,
(ipp_tag_t)(srcattr->value_tag | quickcopy),
srcattr->name, srcattr->num_values, NULL, NULL);
if (!dstattr)
break;
if (quickcopy)
{
for (i = srcattr->num_values, srcval = srcattr->values,
dstval = dstattr->values;
i > 0;
i --, srcval ++, dstval ++)
dstval->string.text = srcval->string.text;
}
else if (srcattr->value_tag & IPP_TAG_COPY)
{
for (i = srcattr->num_values, srcval = srcattr->values,
dstval = dstattr->values;
i > 0;
i --, srcval ++, dstval ++)
dstval->string.text = _cupsStrAlloc(srcval->string.text);
}
else
{
for (i = srcattr->num_values, srcval = srcattr->values,
dstval = dstattr->values;
i > 0;
i --, srcval ++, dstval ++)
dstval->string.text = _cupsStrRetain(srcval->string.text);
}
break;
case IPP_TAG_DATE :
if (srcattr->num_values != 1)
return (NULL);
dstattr = ippAddDate(dst, srcattr->group_tag, srcattr->name,
srcattr->values[0].date);
break;
case IPP_TAG_RESOLUTION :
dstattr = ippAddResolutions(dst, srcattr->group_tag, srcattr->name,
srcattr->num_values, IPP_RES_PER_INCH,
NULL, NULL);
if (!dstattr)
break;
for (i = srcattr->num_values, srcval = srcattr->values, dstval = dstattr->values;
i > 0;
i --, srcval ++, dstval ++)
{
dstval->resolution.xres = srcval->resolution.xres;
dstval->resolution.yres = srcval->resolution.yres;
dstval->resolution.units = srcval->resolution.units;
}
break;
case IPP_TAG_RANGE :
dstattr = ippAddRanges(dst, srcattr->group_tag, srcattr->name,
srcattr->num_values, NULL, NULL);
if (!dstattr)
break;
for (i = srcattr->num_values, srcval = srcattr->values, dstval = dstattr->values;
i > 0;
i --, srcval ++, dstval ++)
{
dstval->range.lower = srcval->range.lower;
dstval->range.upper = srcval->range.upper;
}
break;
case IPP_TAG_TEXTLANG :
case IPP_TAG_NAMELANG :
dstattr = ippAddStrings(dst, srcattr->group_tag,
(ipp_tag_t)(srcattr->value_tag | quickcopy),
srcattr->name, srcattr->num_values, NULL, NULL);
if (!dstattr)
break;
if (quickcopy)
{
for (i = srcattr->num_values, srcval = srcattr->values,
dstval = dstattr->values;
i > 0;
i --, srcval ++, dstval ++)
{
dstval->string.language = srcval->string.language;
dstval->string.text = srcval->string.text;
}
}
else if (srcattr->value_tag & IPP_TAG_COPY)
{
for (i = srcattr->num_values, srcval = srcattr->values,
dstval = dstattr->values;
i > 0;
i --, srcval ++, dstval ++)
{
if (srcval == srcattr->values)
dstval->string.language = _cupsStrAlloc(srcval->string.language);
else
dstval->string.language = dstattr->values[0].string.language;
dstval->string.text = _cupsStrAlloc(srcval->string.text);
}
}
else
{
for (i = srcattr->num_values, srcval = srcattr->values,
dstval = dstattr->values;
i > 0;
i --, srcval ++, dstval ++)
{
if (srcval == srcattr->values)
dstval->string.language = _cupsStrRetain(srcval->string.language);
else
dstval->string.language = dstattr->values[0].string.language;
dstval->string.text = _cupsStrRetain(srcval->string.text);
}
}
break;
case IPP_TAG_BEGIN_COLLECTION :
dstattr = ippAddCollections(dst, srcattr->group_tag, srcattr->name,
srcattr->num_values, NULL);
if (!dstattr)
break;
for (i = srcattr->num_values, srcval = srcattr->values, dstval = dstattr->values;
i > 0;
i --, srcval ++, dstval ++)
{
dstval->collection = srcval->collection;
srcval->collection->use ++;
}
break;
case IPP_TAG_STRING :
default :
dstattr = ippAddIntegers(dst, srcattr->group_tag, srcattr->value_tag,
srcattr->name, srcattr->num_values, NULL);
if (!dstattr)
break;
for (i = srcattr->num_values, srcval = srcattr->values, dstval = dstattr->values;
i > 0;
i --, srcval ++, dstval ++)
{
dstval->unknown.length = srcval->unknown.length;
if (dstval->unknown.length > 0)
{
if ((dstval->unknown.data = malloc(dstval->unknown.length)) == NULL)
dstval->unknown.length = 0;
else
memcpy(dstval->unknown.data, srcval->unknown.data, dstval->unknown.length);
}
}
break;
}
return (dstattr);
}
int
ippCopyAttributes(
ipp_t *dst,
ipp_t *src,
int quickcopy,
ipp_copycb_t cb,
void *context)
{
ipp_attribute_t *srcattr;
DEBUG_printf(("ippCopyAttributes(dst=%p, src=%p, quickcopy=%d, cb=%p, context=%p)",
dst, src, quickcopy, cb, context));
if (!dst || !src)
return (0);
for (srcattr = src->attrs; srcattr; srcattr = srcattr->next)
if (!cb || (*cb)(context, dst, srcattr))
if (!ippCopyAttribute(dst, srcattr, quickcopy))
return (0);
return (1);
}
time_t
ippDateToTime(const ipp_uchar_t *date)
{
struct tm unixdate;
time_t t;
if (!date)
return (0);
memset(&unixdate, 0, sizeof(unixdate));
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);
}
void
ippDelete(ipp_t *ipp)
{
ipp_attribute_t *attr,
*next;
DEBUG_printf(("ippDelete(ipp=%p)", ipp));
if (!ipp)
return;
ipp->use --;
if (ipp->use > 0)
return;
for (attr = ipp->attrs; attr != NULL; attr = next)
{
next = attr->next;
ipp_free_values(attr, 0, attr->num_values);
if (attr->name)
_cupsStrFree(attr->name);
free(attr);
}
free(ipp);
}
void
ippDeleteAttribute(
ipp_t *ipp,
ipp_attribute_t *attr)
{
ipp_attribute_t *current,
*prev;
DEBUG_printf(("ippDeleteAttribute(ipp=%p, attr=%p(%s))", ipp, attr,
attr ? attr->name : "(null)"));
if (!attr)
return;
if (ipp)
{
for (current = ipp->attrs, prev = NULL;
current;
prev = current, current = current->next)
if (current == attr)
{
if (prev)
prev->next = current->next;
else
ipp->attrs = current->next;
if (current == ipp->last)
ipp->last = prev;
break;
}
if (!current)
return;
}
ipp_free_values(attr, 0, attr->num_values);
if (attr->name)
_cupsStrFree(attr->name);
free(attr);
}
int
ippDeleteValues(
ipp_t *ipp,
ipp_attribute_t **attr,
int element,
int count)
{
if (!ipp || !attr || !*attr ||
element < 0 || element >= (*attr)->num_values || count <= 0 ||
(element + count) >= (*attr)->num_values)
return (0);
if (count == (*attr)->num_values)
{
ippDeleteAttribute(ipp, *attr);
*attr = NULL;
return (1);
}
ipp_free_values(*attr, element, count);
return (1);
}
ipp_attribute_t *
ippFindAttribute(ipp_t *ipp,
const char *name,
ipp_tag_t type)
{
DEBUG_printf(("2ippFindAttribute(ipp=%p, name=\"%s\", type=%02x(%s))", ipp,
name, type, ippTagString(type)));
if (!ipp || !name)
return (NULL);
ipp->current = NULL;
return (ippFindNextAttribute(ipp, name, type));
}
ipp_attribute_t *
ippFindNextAttribute(ipp_t *ipp,
const char *name,
ipp_tag_t type)
{
ipp_attribute_t *attr;
ipp_tag_t value_tag;
DEBUG_printf(("2ippFindNextAttribute(ipp=%p, name=\"%s\", type=%02x(%s))",
ipp, name, type, ippTagString(type)));
if (!ipp || !name)
return (NULL);
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\"", attr,
attr->name));
value_tag = (ipp_tag_t)(attr->value_tag & IPP_TAG_MASK);
if (attr->name != NULL && _cups_strcasecmp(attr->name, name) == 0 &&
(value_tag == type || type == IPP_TAG_ZERO ||
(value_tag == IPP_TAG_TEXTLANG && type == IPP_TAG_TEXT) ||
(value_tag == IPP_TAG_NAMELANG && type == IPP_TAG_NAME)))
{
ipp->current = attr;
return (attr);
}
}
ipp->current = NULL;
ipp->prev = NULL;
return (NULL);
}
ipp_attribute_t *
ippFirstAttribute(ipp_t *ipp)
{
if (!ipp)
return (NULL);
return (ipp->current = ipp->attrs);
}
int
ippGetBoolean(ipp_attribute_t *attr,
int element)
{
if (!attr || attr->value_tag != IPP_TAG_BOOLEAN ||
element < 0 || element >= attr->num_values)
return (-1);
return (attr->values[element].boolean);
}
ipp_t *
ippGetCollection(
ipp_attribute_t *attr,
int element)
{
if (!attr || attr->value_tag != IPP_TAG_BEGIN_COLLECTION ||
element < 0 || element >= attr->num_values)
return (NULL);
return (attr->values[element].collection);
}
int
ippGetCount(ipp_attribute_t *attr)
{
if (!attr)
return (-1);
return (attr->num_values);
}
const ipp_uchar_t *
ippGetDate(ipp_attribute_t *attr,
int element)
{
if (!attr || attr->value_tag != IPP_TAG_DATE ||
element < 0 || element >= attr->num_values)
return (NULL);
return (attr->values[element].date);
}
ipp_tag_t
ippGetGroupTag(ipp_attribute_t *attr)
{
if (!attr)
return (IPP_TAG_ZERO);
return (attr->group_tag);
}
int
ippGetInteger(ipp_attribute_t *attr,
int element)
{
if (!attr || (attr->value_tag != IPP_TAG_INTEGER && attr->value_tag != IPP_TAG_ENUM) ||
element < 0 || element >= attr->num_values)
return (-1);
return (attr->values[element].integer);
}
const char *
ippGetName(ipp_attribute_t *attr)
{
if (!attr)
return (NULL);
return (attr->name);
}
ipp_op_t
ippGetOperation(ipp_t *ipp)
{
if (!ipp)
return ((ipp_op_t)-1);
return (ipp->request.op.operation_id);
}
int
ippGetRange(ipp_attribute_t *attr,
int element,
int *uppervalue)
{
if (!attr || attr->value_tag != IPP_TAG_RANGE ||
element < 0 || element >= attr->num_values)
{
if (uppervalue)
*uppervalue = -1;
return (-1);
}
if (uppervalue)
*uppervalue = attr->values[element].range.upper;
return (attr->values[element].range.lower);
}
int
ippGetRequestId(ipp_t *ipp)
{
if (!ipp)
return (-1);
return (ipp->request.any.request_id);
}
int
ippGetResolution(
ipp_attribute_t *attr,
int element,
int *yres,
ipp_res_t *units)
{
if (!attr || attr->value_tag != IPP_TAG_RESOLUTION ||
element < 0 || element >= attr->num_values)
return (-1);
if (yres)
*yres = attr->values[element].resolution.yres;
if (units)
*units = attr->values[element].resolution.units;
return (attr->values[element].resolution.xres);
}
ipp_state_t
ippGetState(ipp_t *ipp)
{
if (!ipp)
return (IPP_IDLE);
return (ipp->state);
}
ipp_status_t
ippGetStatusCode(ipp_t *ipp)
{
if (!ipp)
return (IPP_INTERNAL_ERROR);
return (ipp->request.status.status_code);
}
const char *
ippGetString(ipp_attribute_t *attr,
int element,
const char **language)
{
if (!attr || element < 0 || element >= attr->num_values ||
(attr->value_tag != IPP_TAG_TEXTLANG && attr->value_tag != IPP_TAG_NAMELANG &&
(attr->value_tag < IPP_TAG_TEXT || attr->value_tag > IPP_TAG_MIMETYPE)))
return (NULL);
if (language)
*language = attr->values[element].string.language;
return (attr->values[element].string.text);
}
ipp_tag_t
ippGetValueTag(ipp_attribute_t *attr)
{
if (!attr)
return (IPP_TAG_ZERO);
return (attr->value_tag);
}
int
ippGetVersion(ipp_t *ipp,
int *minor)
{
if (!ipp)
{
if (minor)
*minor = -1;
return (-1);
}
if (minor)
*minor = ipp->request.any.version[1];
return (ipp->request.any.version[0]);
}
size_t
ippLength(ipp_t *ipp)
{
return (ipp_length(ipp, 0));
}
ipp_attribute_t *
ippNextAttribute(ipp_t *ipp)
{
if (!ipp || !ipp->current)
return (NULL);
return (ipp->current = ipp->current->next);
}
ipp_t *
ippNew(void)
{
ipp_t *temp;
_cups_globals_t *cg = _cupsGlobals();
DEBUG_puts("ippNew()");
if ((temp = (ipp_t *)calloc(1, sizeof(ipp_t))) != NULL)
{
temp->request.any.version[0] = cg->server_version / 10;
temp->request.any.version[1] = cg->server_version % 10;
temp->use = 1;
}
DEBUG_printf(("1ippNew: Returning %p", temp));
return (temp);
}
ipp_t *
ippNewRequest(ipp_op_t op)
{
ipp_t *request;
cups_lang_t *language;
static int request_id = 0;
static _cups_mutex_t request_mutex = _CUPS_MUTEX_INITIALIZER;
DEBUG_printf(("ippNewRequest(op=%02x(%s))", op, ippOpString(op)));
if ((request = ippNew()) == NULL)
return (NULL);
_cupsMutexLock(&request_mutex);
request->request.op.operation_id = op;
request->request.op.request_id = ++request_id;
_cupsMutexUnlock(&request_mutex);
ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
"attributes-charset", NULL, "utf-8");
language = cupsLangDefault();
ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
"attributes-natural-language", NULL, language->language);
return (request);
}
ipp_state_t
ippRead(http_t *http,
ipp_t *ipp)
{
DEBUG_printf(("ippRead(http=%p, ipp=%p), data_remaining=" CUPS_LLFMT,
http, ipp, CUPS_LLCAST (http ? http->data_remaining : -1)));
if (!http)
return (IPP_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));
}
ipp_state_t
ippReadFile(int fd,
ipp_t *ipp)
{
DEBUG_printf(("ippReadFile(fd=%d, ipp=%p)", fd, ipp));
return (ippReadIO(&fd, (ipp_iocb_t)ipp_read_file, 1, NULL, ipp));
}
ipp_state_t
ippReadIO(void *src,
ipp_iocb_t cb,
int blocking,
ipp_t *parent,
ipp_t *ipp)
{
int n;
unsigned char *buffer,
string[IPP_MAX_NAME],
*bufptr;
ipp_attribute_t *attr;
ipp_tag_t tag;
ipp_tag_t value_tag;
_ipp_value_t *value;
DEBUG_printf(("ippReadIO(src=%p, cb=%p, blocking=%d, parent=%p, ipp=%p)",
src, cb, blocking, parent, ipp));
DEBUG_printf(("2ippReadIO: ipp->state=%d", ipp ? ipp->state : IPP_ERROR));
if (!src || !ipp)
return (IPP_ERROR);
if ((buffer = (unsigned char *)_cupsBufferGet(IPP_BUF_SIZE)) == NULL)
{
DEBUG_puts("1ippReadIO: Unable to get read buffer.");
return (IPP_ERROR);
}
switch (ipp->state)
{
case IPP_IDLE :
ipp->state ++;
case IPP_HEADER :
if (parent == NULL)
{
if ((*cb)(src, buffer, 8) < 8)
{
DEBUG_puts("1ippReadIO: Unable to read header.");
_cupsBufferRelease((char *)buffer);
return (IPP_ERROR);
}
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_ATTRIBUTE;
ipp->current = NULL;
ipp->curtag = IPP_TAG_ZERO;
ipp->prev = ipp->last;
if (!blocking)
break;
case IPP_ATTRIBUTE :
for (;;)
{
if ((*cb)(src, buffer, 1) < 1)
{
DEBUG_puts("1ippReadIO: Callback returned EOF/error");
_cupsBufferRelease((char *)buffer);
return (IPP_ERROR);
}
DEBUG_printf(("2ippReadIO: ipp->current=%p, ipp->prev=%p",
ipp->current, ipp->prev));
tag = (ipp_tag_t)buffer[0];
if (tag == IPP_TAG_EXTENSION)
{
if ((*cb)(src, buffer, 4) < 1)
{
DEBUG_puts("1ippReadIO: Callback returned EOF/error");
_cupsBufferRelease((char *)buffer);
return (IPP_ERROR);
}
tag = (ipp_tag_t)((((((buffer[0] << 8) | buffer[1]) << 8) |
buffer[2]) << 8) | buffer[3]);
if (tag & IPP_TAG_COPY)
{
_cupsSetError(IPP_INTERNAL_ERROR, _("IPP extension tag larger than 0x7FFFFFFF."), 1);
DEBUG_printf(("1ippReadIO: bad tag 0x%x.", tag));
_cupsBufferRelease((char *)buffer);
return (IPP_ERROR);
}
}
if (tag == IPP_TAG_END)
{
DEBUG_puts("2ippReadIO: IPP_TAG_END.");
ipp->state = IPP_DATA;
break;
}
else if (tag < IPP_TAG_UNSUPPORTED_VALUE)
{
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), ipp->prev));
continue;
}
DEBUG_printf(("2ippReadIO: value tag=%x(%s)", tag,
ippTagString(tag)));
if ((*cb)(src, buffer, 2) < 2)
{
DEBUG_puts("1ippReadIO: unable to read name length.");
_cupsBufferRelease((char *)buffer);
return (IPP_ERROR);
}
n = (buffer[0] << 8) | buffer[1];
if (n >= IPP_BUF_SIZE)
{
_cupsSetError(IPP_INTERNAL_ERROR, _("IPP name larger than 32767 bytes."), 1);
DEBUG_printf(("1ippReadIO: bad name length %d.", n));
_cupsBufferRelease((char *)buffer);
return (IPP_ERROR);
}
DEBUG_printf(("2ippReadIO: name length=%d", n));
if (n == 0 && tag != IPP_TAG_MEMBERNAME &&
tag != IPP_TAG_END_COLLECTION)
{
if (ipp->current == NULL)
{
_cupsSetError(IPP_INTERNAL_ERROR, _("IPP attribute has no name."), 1);
DEBUG_puts("1ippReadIO: Attribute without name and no current.");
_cupsBufferRelease((char *)buffer);
return (IPP_ERROR);
}
attr = ipp->current;
value_tag = (ipp_tag_t)(attr->value_tag & IPP_TAG_MASK);
if (value_tag == IPP_TAG_ZERO)
{
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))
{
if (tag != IPP_TAG_TEXTLANG && tag != IPP_TAG_NAMELANG &&
(tag < IPP_TAG_TEXT || tag > IPP_TAG_MIMETYPE) &&
tag != IPP_TAG_NOVALUE)
{
_cupsSetError(IPP_INTERNAL_ERROR,
_("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_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)
{
if (tag != IPP_TAG_INTEGER && tag != IPP_TAG_RANGE)
{
_cupsSetError(IPP_INTERNAL_ERROR,
_("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_ERROR);
}
if (value_tag == IPP_TAG_INTEGER && tag == IPP_TAG_RANGE)
{
DEBUG_printf(("1ippReadIO: Converting %s attribute to "
"rangeOfInteger.", attr->name));
ippSetValueTag(ipp, &attr, IPP_TAG_RANGE);
}
}
else if (value_tag != tag)
{
_cupsSetError(IPP_INTERNAL_ERROR,
_("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_ERROR);
}
if ((value = ipp_set_value(ipp, &attr, attr->num_values)) == NULL)
{
_cupsBufferRelease((char *)buffer);
return (IPP_ERROR);
}
}
else if (tag == IPP_TAG_MEMBERNAME)
{
if (n)
{
_cupsSetError(IPP_INTERNAL_ERROR, _("IPP member name is not empty."), 1);
DEBUG_puts("1ippReadIO: member name not empty.");
_cupsBufferRelease((char *)buffer);
return (IPP_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_ERROR);
DEBUG_puts("1ippReadIO: unable to allocate attribute.");
_cupsBufferRelease((char *)buffer);
return (IPP_ERROR);
}
DEBUG_printf(("2ippReadIO: membername, ipp->current=%p, ipp->prev=%p",
ipp->current, ipp->prev));
value = attr->values;
}
else if (tag != IPP_TAG_END_COLLECTION)
{
if ((*cb)(src, buffer, n) < n)
{
DEBUG_puts("1ippReadIO: unable to read name.");
_cupsBufferRelease((char *)buffer);
return (IPP_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_ERROR);
DEBUG_puts("1ippReadIO: unable to allocate attribute.");
_cupsBufferRelease((char *)buffer);
return (IPP_ERROR);
}
DEBUG_printf(("2ippReadIO: name=\"%s\", ipp->current=%p, "
"ipp->prev=%p", buffer, ipp->current, 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_ERROR);
}
n = (buffer[0] << 8) | buffer[1];
DEBUG_printf(("2ippReadIO: value length=%d", n));
if (n >= IPP_BUF_SIZE)
{
_cupsSetError(IPP_INTERNAL_ERROR,
_("IPP value larger than 32767 bytes."), 1);
DEBUG_printf(("1ippReadIO: bad value length %d.", n));
_cupsBufferRelease((char *)buffer);
return (IPP_ERROR);
}
switch (tag)
{
case IPP_TAG_INTEGER :
case IPP_TAG_ENUM :
if (n != 4)
{
if (tag == IPP_TAG_INTEGER)
_cupsSetError(IPP_INTERNAL_ERROR,
_("IPP integer value not 4 bytes."), 1);
else
_cupsSetError(IPP_INTERNAL_ERROR,
_("IPP enum value not 4 bytes."), 1);
DEBUG_printf(("1ippReadIO: bad integer value length %d.", n));
_cupsBufferRelease((char *)buffer);
return (IPP_ERROR);
}
if ((*cb)(src, buffer, 4) < 4)
{
DEBUG_puts("1ippReadIO: Unable to read integer value.");
_cupsBufferRelease((char *)buffer);
return (IPP_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_INTERNAL_ERROR, _("IPP boolean value not 1 byte."),
1);
DEBUG_printf(("1ippReadIO: bad boolean value length %d.", n));
_cupsBufferRelease((char *)buffer);
return (IPP_ERROR);
}
if ((*cb)(src, buffer, 1) < 1)
{
DEBUG_puts("1ippReadIO: Unable to read boolean value.");
_cupsBufferRelease((char *)buffer);
return (IPP_ERROR);
}
value->boolean = buffer[0];
break;
case IPP_TAG_NOVALUE :
case IPP_TAG_NOTSETTABLE :
case IPP_TAG_DELETEATTR :
case IPP_TAG_ADMINDEFINE :
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, n) < n)
{
DEBUG_puts("1ippReadIO: unable to read string value.");
_cupsBufferRelease((char *)buffer);
return (IPP_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_INTERNAL_ERROR, _("IPP date value not 11 bytes."), 1);
DEBUG_printf(("1ippReadIO: bad date value length %d.", n));
_cupsBufferRelease((char *)buffer);
return (IPP_ERROR);
}
if ((*cb)(src, value->date, 11) < 11)
{
DEBUG_puts("1ippReadIO: Unable to read date value.");
_cupsBufferRelease((char *)buffer);
return (IPP_ERROR);
}
break;
case IPP_TAG_RESOLUTION :
if (n != 9)
{
_cupsSetError(IPP_INTERNAL_ERROR,
_("IPP resolution value not 9 bytes."), 1);
DEBUG_printf(("1ippReadIO: bad resolution value length %d.", n));
_cupsBufferRelease((char *)buffer);
return (IPP_ERROR);
}
if ((*cb)(src, buffer, 9) < 9)
{
DEBUG_puts("1ippReadIO: Unable to read resolution value.");
_cupsBufferRelease((char *)buffer);
return (IPP_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_INTERNAL_ERROR,
_("IPP rangeOfInteger value not 8 bytes."), 1);
DEBUG_printf(("1ippReadIO: bad rangeOfInteger value length "
"%d.", n));
_cupsBufferRelease((char *)buffer);
return (IPP_ERROR);
}
if ((*cb)(src, buffer, 8) < 8)
{
DEBUG_puts("1ippReadIO: Unable to read range value.");
_cupsBufferRelease((char *)buffer);
return (IPP_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_INTERNAL_ERROR,
_("IPP textWithLanguage value less than "
"minimum 4 bytes."), 1);
else
_cupsSetError(IPP_INTERNAL_ERROR,
_("IPP nameWithLanguage value less than "
"minimum 4 bytes."), 1);
DEBUG_printf(("1ippReadIO: bad stringWithLanguage value "
"length %d.", n));
_cupsBufferRelease((char *)buffer);
return (IPP_ERROR);
}
if ((*cb)(src, buffer, n) < n)
{
DEBUG_puts("1ippReadIO: Unable to read string w/language "
"value.");
_cupsBufferRelease((char *)buffer);
return (IPP_ERROR);
}
bufptr = buffer;
n = (bufptr[0] << 8) | bufptr[1];
if ((bufptr + 2 + n) >= (buffer + IPP_BUF_SIZE) ||
n >= sizeof(string))
{
_cupsSetError(IPP_INTERNAL_ERROR,
_("IPP language length overflows value."), 1);
DEBUG_printf(("1ippReadIO: bad language value length %d.",
n));
_cupsBufferRelease((char *)buffer);
return (IPP_ERROR);
}
memcpy(string, bufptr + 2, 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_INTERNAL_ERROR,
_("IPP string length overflows value."), 1);
DEBUG_printf(("1ippReadIO: bad string value length %d.", n));
_cupsBufferRelease((char *)buffer);
return (IPP_ERROR);
}
bufptr[2 + n] = '\0';
value->string.text = _cupsStrAlloc((char *)bufptr + 2);
break;
case IPP_TAG_BEGIN_COLLECTION :
value->collection = ippNew();
if (n > 0)
{
_cupsSetError(IPP_INTERNAL_ERROR,
_("IPP begCollection value not 0 bytes."), 1);
DEBUG_puts("1ippReadIO: begCollection tag with value length "
"> 0.");
_cupsBufferRelease((char *)buffer);
return (IPP_ERROR);
}
if (ippReadIO(src, cb, 1, ipp, value->collection) == IPP_ERROR)
{
DEBUG_puts("1ippReadIO: Unable to read collection value.");
_cupsBufferRelease((char *)buffer);
return (IPP_ERROR);
}
break;
case IPP_TAG_END_COLLECTION :
_cupsBufferRelease((char *)buffer);
if (n > 0)
{
_cupsSetError(IPP_INTERNAL_ERROR,
_("IPP endCollection value not 0 bytes."), 1);
DEBUG_puts("1ippReadIO: endCollection tag with value length "
"> 0.");
return (IPP_ERROR);
}
DEBUG_puts("1ippReadIO: endCollection tag...");
return (ipp->state = IPP_DATA);
case IPP_TAG_MEMBERNAME :
if (!attr)
{
_cupsSetError(IPP_INTERNAL_ERROR,
_("IPP memberName with no attribute."), 1);
DEBUG_puts("1ippReadIO: Member name without attribute.");
_cupsBufferRelease((char *)buffer);
return (IPP_ERROR);
}
else if (n == 0)
{
_cupsSetError(IPP_INTERNAL_ERROR,
_("IPP memberName value is empty."), 1);
DEBUG_puts("1ippReadIO: Empty member name value.");
_cupsBufferRelease((char *)buffer);
return (IPP_ERROR);
}
else if ((*cb)(src, buffer, n) < n)
{
DEBUG_puts("1ippReadIO: Unable to read member name value.");
_cupsBufferRelease((char *)buffer);
return (IPP_ERROR);
}
buffer[n] = '\0';
attr->name = _cupsStrAlloc((char *)buffer);
attr->num_values --;
DEBUG_printf(("2ippReadIO: member name=\"%s\"", attr->name));
break;
default :
value->unknown.length = n;
if (n > 0)
{
if ((value->unknown.data = malloc(n)) == NULL)
{
_cupsSetHTTPError(HTTP_ERROR);
DEBUG_puts("1ippReadIO: Unable to allocate value");
_cupsBufferRelease((char *)buffer);
return (IPP_ERROR);
}
if ((*cb)(src, value->unknown.data, n) < n)
{
DEBUG_puts("1ippReadIO: Unable to read unsupported value.");
_cupsBufferRelease((char *)buffer);
return (IPP_ERROR);
}
}
else
value->unknown.data = NULL;
break;
}
if (!blocking)
break;
}
break;
case IPP_DATA :
break;
default :
break;
}
DEBUG_printf(("1ippReadIO: returning ipp->state=%d.", ipp->state));
_cupsBufferRelease((char *)buffer);
return (ipp->state);
}
int
ippSetBoolean(ipp_t *ipp,
ipp_attribute_t **attr,
int element,
int boolvalue)
{
_ipp_value_t *value;
if (!ipp || !attr || !*attr || (*attr)->value_tag != IPP_TAG_BOOLEAN ||
element < 0 || element > (*attr)->num_values)
return (0);
if ((value = ipp_set_value(ipp, attr, element)) != NULL)
value->boolean = boolvalue;
return (value != NULL);
}
int
ippSetCollection(
ipp_t *ipp,
ipp_attribute_t **attr,
int element,
ipp_t *colvalue)
{
_ipp_value_t *value;
if (!ipp || !attr || !*attr || (*attr)->value_tag != IPP_TAG_BEGIN_COLLECTION ||
element < 0 || element > (*attr)->num_values || !colvalue)
return (0);
if ((value = ipp_set_value(ipp, attr, element)) != NULL)
{
if (value->collection)
ippDelete(value->collection);
value->collection = colvalue;
colvalue->use ++;
}
return (value != NULL);
}
int
ippSetDate(ipp_t *ipp,
ipp_attribute_t **attr,
int element,
const ipp_uchar_t *datevalue)
{
_ipp_value_t *value;
if (!ipp || !attr || !*attr || (*attr)->value_tag != IPP_TAG_DATE ||
element < 0 || element > (*attr)->num_values || !datevalue)
return (0);
if ((value = ipp_set_value(ipp, attr, element)) != NULL)
memcpy(value->date, datevalue, sizeof(value->date));
return (value != NULL);
}
int
ippSetGroupTag(
ipp_t *ipp,
ipp_attribute_t **attr,
ipp_tag_t group_tag)
{
if (!ipp || !attr || group_tag < IPP_TAG_ZERO || group_tag == IPP_TAG_END ||
group_tag >= IPP_TAG_UNSUPPORTED_VALUE)
return (0);
(*attr)->group_tag = group_tag;
return (1);
}
int
ippSetInteger(ipp_t *ipp,
ipp_attribute_t **attr,
int element,
int intvalue)
{
_ipp_value_t *value;
if (!ipp || !attr || !*attr ||
((*attr)->value_tag != IPP_TAG_INTEGER && (*attr)->value_tag != IPP_TAG_ENUM) ||
element < 0 || element > (*attr)->num_values)
return (0);
if ((value = ipp_set_value(ipp, attr, element)) != NULL)
value->integer = intvalue;
return (value != NULL);
}
int
ippSetName(ipp_t *ipp,
ipp_attribute_t **attr,
const char *name)
{
char *temp;
if (!ipp || !attr || !*attr)
return (0);
if ((temp = _cupsStrAlloc(name)) != NULL)
{
if ((*attr)->name)
_cupsStrFree((*attr)->name);
(*attr)->name = temp;
}
return (temp != NULL);
}
int
ippSetOperation(ipp_t *ipp,
ipp_op_t op)
{
if (!ipp)
return (0);
ipp->request.op.operation_id = op;
return (1);
}
int
ippSetRange(ipp_t *ipp,
ipp_attribute_t **attr,
int element,
int lowervalue,
int uppervalue)
{
_ipp_value_t *value;
if (!ipp || !attr || !*attr || (*attr)->value_tag != IPP_TAG_RANGE ||
element < 0 || element > (*attr)->num_values || lowervalue > uppervalue)
return (0);
if ((value = ipp_set_value(ipp, attr, element)) != NULL)
{
value->range.lower = lowervalue;
value->range.upper = uppervalue;
}
return (value != NULL);
}
int
ippSetRequestId(ipp_t *ipp,
int request_id)
{
if (!ipp)
return (0);
ipp->request.any.request_id = request_id;
return (1);
}
int
ippSetResolution(
ipp_t *ipp,
ipp_attribute_t **attr,
int element,
ipp_res_t unitsvalue,
int xresvalue,
int yresvalue)
{
_ipp_value_t *value;
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);
if ((value = ipp_set_value(ipp, attr, element)) != NULL)
{
value->resolution.units = unitsvalue;
value->resolution.xres = xresvalue;
value->resolution.yres = yresvalue;
}
return (value != NULL);
}
int
ippSetState(ipp_t *ipp,
ipp_state_t state)
{
if (!ipp)
return (0);
ipp->state = state;
ipp->current = NULL;
return (1);
}
int
ippSetStatusCode(ipp_t *ipp,
ipp_status_t status)
{
if (!ipp)
return (0);
ipp->request.status.status_code = status;
return (1);
}
int
ippSetString(ipp_t *ipp,
ipp_attribute_t **attr,
int element,
const char *strvalue)
{
char *temp;
_ipp_value_t *value;
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);
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_COPY)
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);
}
int
ippSetValueTag(
ipp_t *ipp,
ipp_attribute_t **attr,
ipp_tag_t value_tag)
{
int i;
_ipp_value_t *value;
int integer;
cups_lang_t *language;
char code[32];
ipp_tag_t temp_tag;
if (!ipp || !attr)
return (0);
if (value_tag == (*attr)->value_tag)
return (1);
temp_tag = (ipp_tag_t)((int)((*attr)->value_tag) & IPP_TAG_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 :
if ((*attr)->num_values > 0)
ipp_free_values(*attr, 0, (*attr)->num_values);
(*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_COPY));
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"))
{
(*attr)->values[0].string.language =
_cupsStrAlloc(ipp->attrs->next->values[0].string.text);
}
else
{
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_COPY)
{
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;
case IPP_TAG_KEYWORD :
if (temp_tag == IPP_TAG_NAME || temp_tag == IPP_TAG_NAMELANG)
break;
default :
return (0);
}
return (1);
}
int
ippSetVersion(ipp_t *ipp,
int major,
int minor)
{
if (!ipp || major < 0 || minor < 0)
return (0);
ipp->request.any.version[0] = major;
ipp->request.any.version[1] = minor;
return (1);
}
const ipp_uchar_t *
ippTimeToDate(time_t t)
{
struct tm *unixdate;
ipp_uchar_t *date = _cupsGlobals()->ipp_date;
unixdate = gmtime(&t);
unixdate->tm_year += 1900;
date[0] = unixdate->tm_year >> 8;
date[1] = unixdate->tm_year;
date[2] = unixdate->tm_mon + 1;
date[3] = unixdate->tm_mday;
date[4] = unixdate->tm_hour;
date[5] = unixdate->tm_min;
date[6] = unixdate->tm_sec;
date[7] = 0;
date[8] = '+';
date[9] = 0;
date[10] = 0;
return (date);
}
ipp_state_t
ippWrite(http_t *http,
ipp_t *ipp)
{
DEBUG_printf(("ippWrite(http=%p, ipp=%p)", http, ipp));
if (!http)
return (IPP_ERROR);
return (ippWriteIO(http, (ipp_iocb_t)httpWrite2, http->blocking, NULL, ipp));
}
ipp_state_t
ippWriteFile(int fd,
ipp_t *ipp)
{
DEBUG_printf(("ippWriteFile(fd=%d, ipp=%p)", fd, ipp));
ipp->state = IPP_IDLE;
return (ippWriteIO(&fd, (ipp_iocb_t)ipp_write_file, 1, NULL, ipp));
}
ipp_state_t
ippWriteIO(void *dst,
ipp_iocb_t cb,
int blocking,
ipp_t *parent,
ipp_t *ipp)
{
int i;
int n;
unsigned char *buffer,
*bufptr;
ipp_attribute_t *attr;
_ipp_value_t *value;
DEBUG_printf(("ippWriteIO(dst=%p, cb=%p, blocking=%d, parent=%p, ipp=%p)",
dst, cb, blocking, parent, ipp));
if (!dst || !ipp)
return (IPP_ERROR);
if ((buffer = (unsigned char *)_cupsBufferGet(IPP_BUF_SIZE)) == NULL)
{
DEBUG_puts("1ippWriteIO: Unable to get write buffer");
return (IPP_ERROR);
}
switch (ipp->state)
{
case IPP_IDLE :
ipp->state ++;
case IPP_HEADER :
if (parent == NULL)
{
bufptr = buffer;
*bufptr++ = ipp->request.any.version[0];
*bufptr++ = ipp->request.any.version[1];
*bufptr++ = ipp->request.any.op_status >> 8;
*bufptr++ = ipp->request.any.op_status;
*bufptr++ = ipp->request.any.request_id >> 24;
*bufptr++ = ipp->request.any.request_id >> 16;
*bufptr++ = ipp->request.any.request_id >> 8;
*bufptr++ = ipp->request.any.request_id;
DEBUG_printf(("2ippWriteIO: version=%d.%d", buffer[0], buffer[1]));
DEBUG_printf(("2ippWriteIO: op_status=%04x",
ipp->request.any.op_status));
DEBUG_printf(("2ippWriteIO: request_id=%d",
ipp->request.any.request_id));
if ((*cb)(dst, buffer, (int)(bufptr - buffer)) < 0)
{
DEBUG_puts("1ippWriteIO: Could not write IPP header...");
_cupsBufferRelease((char *)buffer);
return (IPP_ERROR);
}
}
ipp->state = IPP_ATTRIBUTE;
ipp->current = ipp->attrs;
ipp->curtag = IPP_TAG_ZERO;
DEBUG_printf(("1ippWriteIO: ipp->current=%p", ipp->current));
if (!blocking)
break;
case IPP_ATTRIBUTE :
while (ipp->current != NULL)
{
bufptr = buffer;
attr = ipp->current;
ipp->current = ipp->current->next;
if (!parent)
{
if (ipp->curtag != attr->group_tag)
{
ipp->curtag = attr->group_tag;
if (attr->group_tag == IPP_TAG_ZERO)
continue;
DEBUG_printf(("2ippWriteIO: wrote group tag=%x(%s)",
attr->group_tag, ippTagString(attr->group_tag)));
*bufptr++ = attr->group_tag;
}
else if (attr->group_tag == IPP_TAG_ZERO)
continue;
}
DEBUG_printf(("1ippWriteIO: %s (%s%s)", attr->name,
attr->num_values > 1 ? "1setOf " : "",
ippTagString(attr->value_tag)));
if (parent == NULL)
{
if ((n = (int)strlen(attr->name)) > (IPP_BUF_SIZE - 8))
{
DEBUG_printf(("1ippWriteIO: Attribute name too long (%d)", n));
_cupsBufferRelease((char *)buffer);
return (IPP_ERROR);
}
DEBUG_printf(("2ippWriteIO: writing value tag=%x(%s)",
attr->value_tag, ippTagString(attr->value_tag)));
DEBUG_printf(("2ippWriteIO: writing name=%d,\"%s\"", n,
attr->name));
if (attr->value_tag > 0xff)
{
*bufptr++ = IPP_TAG_EXTENSION;
*bufptr++ = attr->value_tag >> 24;
*bufptr++ = attr->value_tag >> 16;
*bufptr++ = attr->value_tag >> 8;
*bufptr++ = attr->value_tag;
}
else
*bufptr++ = attr->value_tag;
*bufptr++ = n >> 8;
*bufptr++ = n;
memcpy(bufptr, attr->name, n);
bufptr += n;
}
else
{
if ((n = (int)strlen(attr->name)) > (IPP_BUF_SIZE - 12))
{
DEBUG_printf(("1ippWriteIO: Attribute name too long (%d)", n));
_cupsBufferRelease((char *)buffer);
return (IPP_ERROR);
}
DEBUG_printf(("2ippWriteIO: writing value tag=%x(memberName)",
IPP_TAG_MEMBERNAME));
DEBUG_printf(("2ippWriteIO: writing name=%d,\"%s\"", n,
attr->name));
DEBUG_printf(("2ippWriteIO: writing value tag=%x(%s)",
attr->value_tag, ippTagString(attr->value_tag)));
DEBUG_puts("2ippWriteIO: writing name=0,\"\"");
*bufptr++ = IPP_TAG_MEMBERNAME;
*bufptr++ = 0;
*bufptr++ = 0;
*bufptr++ = n >> 8;
*bufptr++ = n;
memcpy(bufptr, attr->name, n);
bufptr += n;
if (attr->value_tag > 0xff)
{
*bufptr++ = IPP_TAG_EXTENSION;
*bufptr++ = attr->value_tag >> 24;
*bufptr++ = attr->value_tag >> 16;
*bufptr++ = attr->value_tag >> 8;
*bufptr++ = attr->value_tag;
}
else
*bufptr++ = attr->value_tag;
*bufptr++ = 0;
*bufptr++ = 0;
}
switch (attr->value_tag & ~IPP_TAG_COPY)
{
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 :
*bufptr++ = 0;
*bufptr++ = 0;
break;
case IPP_TAG_INTEGER :
case IPP_TAG_ENUM :
for (i = 0, value = attr->values;
i < attr->num_values;
i ++, value ++)
{
if ((IPP_BUF_SIZE - (bufptr - buffer)) < 9)
{
if ((*cb)(dst, buffer, (int)(bufptr - buffer)) < 0)
{
DEBUG_puts("1ippWriteIO: Could not write IPP "
"attribute...");
_cupsBufferRelease((char *)buffer);
return (IPP_ERROR);
}
bufptr = buffer;
}
if (i)
{
*bufptr++ = attr->value_tag;
*bufptr++ = 0;
*bufptr++ = 0;
}
*bufptr++ = 0;
*bufptr++ = 4;
*bufptr++ = value->integer >> 24;
*bufptr++ = value->integer >> 16;
*bufptr++ = value->integer >> 8;
*bufptr++ = value->integer;
}
break;
case IPP_TAG_BOOLEAN :
for (i = 0, value = attr->values;
i < attr->num_values;
i ++, value ++)
{
if ((IPP_BUF_SIZE - (bufptr - buffer)) < 6)
{
if ((*cb)(dst, buffer, (int)(bufptr - buffer)) < 0)
{
DEBUG_puts("1ippWriteIO: Could not write IPP "
"attribute...");
_cupsBufferRelease((char *)buffer);
return (IPP_ERROR);
}
bufptr = buffer;
}
if (i)
{
*bufptr++ = attr->value_tag;
*bufptr++ = 0;
*bufptr++ = 0;
}
*bufptr++ = 0;
*bufptr++ = 1;
*bufptr++ = value->boolean;
}
break;
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 :
for (i = 0, value = attr->values;
i < attr->num_values;
i ++, value ++)
{
if (i)
{
DEBUG_printf(("2ippWriteIO: writing value tag=%x(%s)",
attr->value_tag,
ippTagString(attr->value_tag)));
DEBUG_printf(("2ippWriteIO: writing name=0,\"\""));
if ((IPP_BUF_SIZE - (bufptr - buffer)) < 3)
{
if ((*cb)(dst, buffer, (int)(bufptr - buffer)) < 0)
{
DEBUG_puts("1ippWriteIO: Could not write IPP "
"attribute...");
_cupsBufferRelease((char *)buffer);
return (IPP_ERROR);
}
bufptr = buffer;
}
*bufptr++ = attr->value_tag;
*bufptr++ = 0;
*bufptr++ = 0;
}
if (value->string.text != NULL)
n = (int)strlen(value->string.text);
else
n = 0;
if (n > (IPP_BUF_SIZE - 2))
{
DEBUG_printf(("1ippWriteIO: String too long (%d)", n));
_cupsBufferRelease((char *)buffer);
return (IPP_ERROR);
}
DEBUG_printf(("2ippWriteIO: writing string=%d,\"%s\"", n,
value->string.text));
if ((int)(IPP_BUF_SIZE - (bufptr - buffer)) < (n + 2))
{
if ((*cb)(dst, buffer, (int)(bufptr - buffer)) < 0)
{
DEBUG_puts("1ippWriteIO: Could not write IPP "
"attribute...");
_cupsBufferRelease((char *)buffer);
return (IPP_ERROR);
}
bufptr = buffer;
}
*bufptr++ = n >> 8;
*bufptr++ = n;
if (n > 0)
{
memcpy(bufptr, value->string.text, n);
bufptr += n;
}
}
break;
case IPP_TAG_DATE :
for (i = 0, value = attr->values;
i < attr->num_values;
i ++, value ++)
{
if ((IPP_BUF_SIZE - (bufptr - buffer)) < 16)
{
if ((*cb)(dst, buffer, (int)(bufptr - buffer)) < 0)
{
DEBUG_puts("1ippWriteIO: Could not write IPP "
"attribute...");
_cupsBufferRelease((char *)buffer);
return (IPP_ERROR);
}
bufptr = buffer;
}
if (i)
{
*bufptr++ = attr->value_tag;
*bufptr++ = 0;
*bufptr++ = 0;
}
*bufptr++ = 0;
*bufptr++ = 11;
memcpy(bufptr, value->date, 11);
bufptr += 11;
}
break;
case IPP_TAG_RESOLUTION :
for (i = 0, value = attr->values;
i < attr->num_values;
i ++, value ++)
{
if ((IPP_BUF_SIZE - (bufptr - buffer)) < 14)
{
if ((*cb)(dst, buffer, (int)(bufptr - buffer)) < 0)
{
DEBUG_puts("1ippWriteIO: Could not write IPP "
"attribute...");
_cupsBufferRelease((char *)buffer);
return (IPP_ERROR);
}
bufptr = buffer;
}
if (i)
{
*bufptr++ = attr->value_tag;
*bufptr++ = 0;
*bufptr++ = 0;
}
*bufptr++ = 0;
*bufptr++ = 9;
*bufptr++ = value->resolution.xres >> 24;
*bufptr++ = value->resolution.xres >> 16;
*bufptr++ = value->resolution.xres >> 8;
*bufptr++ = value->resolution.xres;
*bufptr++ = value->resolution.yres >> 24;
*bufptr++ = value->resolution.yres >> 16;
*bufptr++ = value->resolution.yres >> 8;
*bufptr++ = value->resolution.yres;
*bufptr++ = value->resolution.units;
}
break;
case IPP_TAG_RANGE :
for (i = 0, value = attr->values;
i < attr->num_values;
i ++, value ++)
{
if ((IPP_BUF_SIZE - (bufptr - buffer)) < 13)
{
if ((*cb)(dst, buffer, (int)(bufptr - buffer)) < 0)
{
DEBUG_puts("1ippWriteIO: Could not write IPP "
"attribute...");
_cupsBufferRelease((char *)buffer);
return (IPP_ERROR);
}
bufptr = buffer;
}
if (i)
{
*bufptr++ = attr->value_tag;
*bufptr++ = 0;
*bufptr++ = 0;
}
*bufptr++ = 0;
*bufptr++ = 8;
*bufptr++ = value->range.lower >> 24;
*bufptr++ = value->range.lower >> 16;
*bufptr++ = value->range.lower >> 8;
*bufptr++ = value->range.lower;
*bufptr++ = value->range.upper >> 24;
*bufptr++ = value->range.upper >> 16;
*bufptr++ = value->range.upper >> 8;
*bufptr++ = value->range.upper;
}
break;
case IPP_TAG_TEXTLANG :
case IPP_TAG_NAMELANG :
for (i = 0, value = attr->values;
i < attr->num_values;
i ++, value ++)
{
if (i)
{
if ((IPP_BUF_SIZE - (bufptr - buffer)) < 3)
{
if ((*cb)(dst, buffer, (int)(bufptr - buffer)) < 0)
{
DEBUG_puts("1ippWriteIO: Could not write IPP "
"attribute...");
_cupsBufferRelease((char *)buffer);
return (IPP_ERROR);
}
bufptr = buffer;
}
*bufptr++ = attr->value_tag;
*bufptr++ = 0;
*bufptr++ = 0;
}
n = 4;
if (value->string.language != NULL)
n += (int)strlen(value->string.language);
if (value->string.text != NULL)
n += (int)strlen(value->string.text);
if (n > (IPP_BUF_SIZE - 2))
{
DEBUG_printf(("1ippWriteIO: text/nameWithLanguage value "
"too long (%d)", n));
_cupsBufferRelease((char *)buffer);
return (IPP_ERROR);
}
if ((int)(IPP_BUF_SIZE - (bufptr - buffer)) < (n + 2))
{
if ((*cb)(dst, buffer, (int)(bufptr - buffer)) < 0)
{
DEBUG_puts("1ippWriteIO: Could not write IPP "
"attribute...");
_cupsBufferRelease((char *)buffer);
return (IPP_ERROR);
}
bufptr = buffer;
}
*bufptr++ = n >> 8;
*bufptr++ = n;
if (value->string.language != NULL)
n = (int)strlen(value->string.language);
else
n = 0;
*bufptr++ = n >> 8;
*bufptr++ = n;
if (n > 0)
{
memcpy(bufptr, value->string.language, n);
bufptr += n;
}
if (value->string.text != NULL)
n = (int)strlen(value->string.text);
else
n = 0;
*bufptr++ = n >> 8;
*bufptr++ = n;
if (n > 0)
{
memcpy(bufptr, value->string.text, n);
bufptr += n;
}
}
break;
case IPP_TAG_BEGIN_COLLECTION :
for (i = 0, value = attr->values;
i < attr->num_values;
i ++, value ++)
{
if ((IPP_BUF_SIZE - (bufptr - buffer)) < 5)
{
if ((*cb)(dst, buffer, (int)(bufptr - buffer)) < 0)
{
DEBUG_puts("1ippWriteIO: Could not write IPP "
"attribute...");
_cupsBufferRelease((char *)buffer);
return (IPP_ERROR);
}
bufptr = buffer;
}
if (i)
{
*bufptr++ = attr->value_tag;
*bufptr++ = 0;
*bufptr++ = 0;
}
*bufptr++ = 0;
*bufptr++ = 0;
if ((*cb)(dst, buffer, (int)(bufptr - buffer)) < 0)
{
DEBUG_puts("1ippWriteIO: Could not write IPP "
"attribute...");
_cupsBufferRelease((char *)buffer);
return (IPP_ERROR);
}
bufptr = buffer;
value->collection->state = IPP_IDLE;
if (ippWriteIO(dst, cb, 1, ipp,
value->collection) == IPP_ERROR)
{
DEBUG_puts("1ippWriteIO: Unable to write collection value");
_cupsBufferRelease((char *)buffer);
return (IPP_ERROR);
}
}
break;
default :
for (i = 0, value = attr->values;
i < attr->num_values;
i ++, value ++)
{
if (i)
{
if ((IPP_BUF_SIZE - (bufptr - buffer)) < 3)
{
if ((*cb)(dst, buffer, (int)(bufptr - buffer)) < 0)
{
DEBUG_puts("1ippWriteIO: Could not write IPP "
"attribute...");
_cupsBufferRelease((char *)buffer);
return (IPP_ERROR);
}
bufptr = buffer;
}
*bufptr++ = attr->value_tag;
*bufptr++ = 0;
*bufptr++ = 0;
}
n = value->unknown.length;
if (n > (IPP_BUF_SIZE - 2))
{
DEBUG_printf(("1ippWriteIO: Data length too long (%d)",
n));
_cupsBufferRelease((char *)buffer);
return (IPP_ERROR);
}
if ((int)(IPP_BUF_SIZE - (bufptr - buffer)) < (n + 2))
{
if ((*cb)(dst, buffer, (int)(bufptr - buffer)) < 0)
{
DEBUG_puts("1ippWriteIO: Could not write IPP "
"attribute...");
_cupsBufferRelease((char *)buffer);
return (IPP_ERROR);
}
bufptr = buffer;
}
*bufptr++ = n >> 8;
*bufptr++ = n;
if (n > 0)
{
memcpy(bufptr, value->unknown.data, n);
bufptr += n;
}
}
break;
}
if (bufptr > buffer)
{
if ((*cb)(dst, buffer, (int)(bufptr - buffer)) < 0)
{
DEBUG_puts("1ippWriteIO: Could not write IPP attribute...");
_cupsBufferRelease((char *)buffer);
return (IPP_ERROR);
}
DEBUG_printf(("2ippWriteIO: wrote %d bytes",
(int)(bufptr - buffer)));
}
if (!blocking)
break;
}
if (ipp->current == NULL)
{
if (parent == NULL)
{
buffer[0] = IPP_TAG_END;
n = 1;
}
else
{
buffer[0] = IPP_TAG_END_COLLECTION;
buffer[1] = 0;
buffer[2] = 0;
buffer[3] = 0;
buffer[4] = 0;
n = 5;
}
if ((*cb)(dst, buffer, n) < 0)
{
DEBUG_puts("1ippWriteIO: Could not write IPP end-tag...");
_cupsBufferRelease((char *)buffer);
return (IPP_ERROR);
}
ipp->state = IPP_DATA;
}
break;
case IPP_DATA :
break;
default :
break;
}
_cupsBufferRelease((char *)buffer);
return (ipp->state);
}
static ipp_attribute_t *
ipp_add_attr(ipp_t *ipp,
const char *name,
ipp_tag_t group_tag,
ipp_tag_t value_tag,
int num_values)
{
int alloc_values;
ipp_attribute_t *attr;
DEBUG_printf(("4ipp_add_attr(ipp=%p, name=\"%s\", group_tag=0x%x, value_tag=0x%x, "
"num_values=%d)", ipp, name, group_tag, value_tag, num_values));
if (!ipp || num_values < 0)
return (NULL);
if (num_values <= 1)
alloc_values = 1;
else
alloc_values = (num_values + IPP_MAX_VALUES - 1) & ~(IPP_MAX_VALUES - 1);
attr = calloc(sizeof(ipp_attribute_t) +
(alloc_values - 1) * sizeof(_ipp_value_t), 1);
if (attr)
{
if (name)
attr->name = _cupsStrAlloc(name);
attr->group_tag = group_tag;
attr->value_tag = value_tag;
attr->num_values = num_values;
if (ipp->last)
ipp->last->next = attr;
else
ipp->attrs = attr;
ipp->prev = ipp->last;
ipp->last = ipp->current = attr;
}
DEBUG_printf(("5ipp_add_attr: Returning %p", attr));
return (attr);
}
static void
ipp_free_values(ipp_attribute_t *attr,
int element,
int count)
{
int i;
_ipp_value_t *value;
DEBUG_printf(("4ipp_free_values(attr=%p, element=%d, count=%d)", attr,
element, count));
if (!(attr->value_tag & IPP_TAG_COPY))
{
switch (attr->value_tag)
{
case IPP_TAG_TEXTLANG :
case IPP_TAG_NAMELANG :
if (element == 0 && count == attr->num_values &&
attr->values[0].string.language)
{
_cupsStrFree(attr->values[0].string.language);
attr->values[0].string.language = NULL;
}
case IPP_TAG_TEXT :
case IPP_TAG_NAME :
case IPP_TAG_RESERVED_STRING :
case IPP_TAG_KEYWORD :
case IPP_TAG_URI :
case IPP_TAG_URISCHEME :
case IPP_TAG_CHARSET :
case IPP_TAG_LANGUAGE :
case IPP_TAG_MIMETYPE :
for (i = count, value = attr->values + element;
i > 0;
i --, value ++)
{
_cupsStrFree(value->string.text);
value->string.text = NULL;
}
break;
case IPP_TAG_DEFAULT :
case IPP_TAG_UNKNOWN :
case IPP_TAG_NOVALUE :
case IPP_TAG_NOTSETTABLE :
case IPP_TAG_DELETEATTR :
case IPP_TAG_ADMINDEFINE :
case IPP_TAG_INTEGER :
case IPP_TAG_ENUM :
case IPP_TAG_BOOLEAN :
case IPP_TAG_DATE :
case IPP_TAG_RESOLUTION :
case IPP_TAG_RANGE :
break;
case IPP_TAG_BEGIN_COLLECTION :
for (i = count, value = attr->values + element;
i > 0;
i --, value ++)
{
ippDelete(value->collection);
value->collection = NULL;
}
break;
case IPP_TAG_STRING :
default :
for (i = count, value = attr->values + element;
i > 0;
i --, value ++)
{
if (value->unknown.data)
{
free(value->unknown.data);
value->unknown.data = NULL;
}
}
break;
}
}
if ((element + count) < attr->num_values)
memmove(attr->values + element, attr->values + element + count,
(attr->num_values - count - element) * sizeof(_ipp_value_t));
attr->num_values -= count;
}
static char *
ipp_get_code(const char *value,
char *buffer,
size_t bufsize)
{
char *bufptr,
*bufend;
for (bufptr = buffer, bufend = buffer + bufsize - 1;
*value && bufptr < bufend;
value ++)
if (*value == '_')
*bufptr++ = '-';
else
*bufptr++ = _cups_tolower(*value);
*bufptr = '\0';
return (buffer);
}
static char *
ipp_lang_code(const char *locale,
char *buffer,
size_t bufsize)
{
if (!_cups_strcasecmp(locale, "c"))
{
strlcpy(buffer, "en", bufsize);
return (buffer);
}
else
return (ipp_get_code(locale, buffer, bufsize));
}
static size_t
ipp_length(ipp_t *ipp,
int collection)
{
int i;
size_t bytes;
ipp_attribute_t *attr;
ipp_tag_t group;
_ipp_value_t *value;
DEBUG_printf(("3ipp_length(ipp=%p, collection=%d)", ipp, collection));
if (!ipp)
{
DEBUG_puts("4ipp_length: Returning 0 bytes");
return (0);
}
bytes = collection ? 0 : 8;
group = IPP_TAG_ZERO;
for (attr = ipp->attrs; attr != NULL; attr = attr->next)
{
if (attr->group_tag != group && !collection)
{
group = attr->group_tag;
if (group == IPP_TAG_ZERO)
continue;
bytes ++;
}
if (!attr->name)
continue;
DEBUG_printf(("5ipp_length: attr->name=\"%s\", attr->num_values=%d, "
"bytes=" CUPS_LLFMT, attr->name, attr->num_values, CUPS_LLCAST bytes));
if (attr->value_tag < IPP_TAG_EXTENSION)
bytes += attr->num_values;
else
bytes += 5 * attr->num_values;
bytes += 2 * attr->num_values;
bytes += (int)strlen(attr->name);
bytes += 2 * attr->num_values;
if (collection)
bytes += 5;
switch (attr->value_tag & ~IPP_TAG_COPY)
{
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 :
break;
case IPP_TAG_INTEGER :
case IPP_TAG_ENUM :
bytes += 4 * attr->num_values;
break;
case IPP_TAG_BOOLEAN :
bytes += attr->num_values;
break;
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 :
for (i = 0, value = attr->values;
i < attr->num_values;
i ++, value ++)
if (value->string.text)
bytes += strlen(value->string.text);
break;
case IPP_TAG_DATE :
bytes += 11 * attr->num_values;
break;
case IPP_TAG_RESOLUTION :
bytes += 9 * attr->num_values;
break;
case IPP_TAG_RANGE :
bytes += 8 * attr->num_values;
break;
case IPP_TAG_TEXTLANG :
case IPP_TAG_NAMELANG :
bytes += 4 * attr->num_values;
for (i = 0, value = attr->values;
i < attr->num_values;
i ++, value ++)
{
if (value->string.language)
bytes += strlen(value->string.language);
if (value->string.text)
bytes += strlen(value->string.text);
}
break;
case IPP_TAG_BEGIN_COLLECTION :
for (i = 0, value = attr->values;
i < attr->num_values;
i ++, value ++)
bytes += ipp_length(value->collection, 1);
break;
default :
for (i = 0, value = attr->values;
i < attr->num_values;
i ++, value ++)
bytes += value->unknown.length;
break;
}
}
if (collection)
bytes += 5;
else
bytes ++;
DEBUG_printf(("4ipp_length: Returning " CUPS_LLFMT " bytes", CUPS_LLCAST bytes));
return (bytes);
}
static ssize_t
ipp_read_http(http_t *http,
ipp_uchar_t *buffer,
size_t length)
{
int tbytes,
bytes;
DEBUG_printf(("7ipp_read_http(http=%p, buffer=%p, length=%d)",
http, buffer, (int)length));
for (tbytes = 0, bytes = 0;
tbytes < (int)length;
tbytes += bytes, buffer += bytes)
{
DEBUG_printf(("9ipp_read_http: tbytes=%d, http->state=%d", tbytes,
http->state));
if (http->state == HTTP_WAITING)
break;
if (http->used == 0 && !http->blocking)
{
if (!httpWait(http, 10000))
{
bytes = -1;
break;
}
}
if ((bytes = httpRead2(http, (char *)buffer, length - tbytes)) < 0)
{
#ifdef WIN32
break;
#else
if (errno != EAGAIN && errno != EINTR)
break;
bytes = 0;
#endif
}
else if (bytes == 0)
break;
}
if (tbytes == 0 && bytes < 0)
tbytes = -1;
DEBUG_printf(("8ipp_read_http: Returning %d bytes", tbytes));
return (tbytes);
}
static ssize_t
ipp_read_file(int *fd,
ipp_uchar_t *buffer,
size_t length)
{
#ifdef WIN32
return ((ssize_t)read(*fd, buffer, (unsigned)length));
#else
return (read(*fd, buffer, length));
#endif
}
static _ipp_value_t *
ipp_set_value(ipp_t *ipp,
ipp_attribute_t **attr,
int element)
{
ipp_attribute_t *temp,
*current,
*prev;
int alloc_values;
temp = *attr;
if (temp->num_values <= 1)
alloc_values = 1;
else
alloc_values = (temp->num_values + IPP_MAX_VALUES - 1) &
~(IPP_MAX_VALUES - 1);
if (element < alloc_values)
{
if (element >= temp->num_values)
temp->num_values = element + 1;
return (temp->values + element);
}
if (alloc_values < IPP_MAX_VALUES)
alloc_values = IPP_MAX_VALUES;
else
alloc_values += IPP_MAX_VALUES;
DEBUG_printf(("4ipp_set_value: Reallocating for up to %d values.",
alloc_values));
if ((temp = realloc(temp, sizeof(ipp_attribute_t) +
(alloc_values - 1) * sizeof(_ipp_value_t))) == NULL)
{
_cupsSetHTTPError(HTTP_ERROR);
DEBUG_puts("4ipp_set_value: Unable to resize attribute.");
return (NULL);
}
memset(temp->values + temp->num_values, 0,
(alloc_values - temp->num_values) * sizeof(_ipp_value_t));
if (temp != *attr)
{
if (ipp->current == *attr && ipp->prev)
{
prev = ipp->prev;
}
else
{
for (prev = NULL, current = ipp->attrs;
current && current != *attr;
prev = current, current = current->next);
if (!current)
{
*attr = temp;
_cupsSetError(IPP_INTERNAL_ERROR,
_("IPP attribute is not a member of the message."), 1);
DEBUG_puts("4ipp_set_value: Unable to find attribute in message.");
return (NULL);
}
}
if (prev)
prev->next = temp;
else
ipp->attrs = temp;
ipp->current = temp;
ipp->prev = prev;
if (ipp->last == *attr)
ipp->last = temp;
*attr = temp;
}
if (element >= temp->num_values)
temp->num_values = element + 1;
return (temp->values + element);
}
static ssize_t
ipp_write_file(int *fd,
ipp_uchar_t *buffer,
size_t length)
{
#ifdef WIN32
return ((ssize_t)write(*fd, buffer, (unsigned)length));
#else
return (write(*fd, buffer, length));
#endif
}