#include "http-private.h"
#include "globals.h"
#include "debug.h"
#include <stdlib.h>
#include <errno.h>
#ifdef WIN32
# include <io.h>
#endif
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 ssize_t ipp_write_file(int *fd, ipp_uchar_t *buffer,
size_t length);
ipp_attribute_t *
ippAddBoolean(ipp_t *ipp,
ipp_tag_t group,
const char *name,
char value)
{
ipp_attribute_t *attr;
DEBUG_printf(("ippAddBoolean(%p, %02x, \'%s\', %d)\n", ipp, group, name, value));
if (ipp == NULL || name == NULL)
return (NULL);
if ((attr = _ippAddAttr(ipp, 1)) == NULL)
return (NULL);
attr->name = _cupsStrAlloc(name);
attr->group_tag = group;
attr->value_tag = IPP_TAG_BOOLEAN;
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(%p, %02x, \'%s\', %d, %p)\n", ipp,
group, name, num_values, values));
if (ipp == NULL || name == NULL || num_values < 1)
return (NULL);
if ((attr = _ippAddAttr(ipp, num_values)) == NULL)
return (NULL);
attr->name = _cupsStrAlloc(name);
attr->group_tag = group;
attr->value_tag = IPP_TAG_BOOLEAN;
if (values != NULL)
for (i = 0, value = attr->values;
i < num_values;
i ++, value ++)
value->boolean = values[i];
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(%p, %02x, \'%s\', %p)\n", ipp, group, name,
value));
if (ipp == NULL || name == NULL)
return (NULL);
if ((attr = _ippAddAttr(ipp, 1)) == NULL)
return (NULL);
attr->name = _cupsStrAlloc(name);
attr->group_tag = group;
attr->value_tag = IPP_TAG_BEGIN_COLLECTION;
attr->values[0].collection = value;
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(%p, %02x, \'%s\', %d, %p)\n", ipp,
group, name, num_values, values));
if (ipp == NULL || name == NULL || num_values < 1)
return (NULL);
if ((attr = _ippAddAttr(ipp, num_values)) == NULL)
return (NULL);
attr->name = _cupsStrAlloc(name);
attr->group_tag = group;
attr->value_tag = IPP_TAG_BEGIN_COLLECTION;
if (values != NULL)
for (i = 0, value = attr->values;
i < num_values;
i ++, value ++)
value->collection = (ipp_t *)values[i];
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(%p, %02x, \'%s\', %p)\n", ipp, group, name,
value));
if (ipp == NULL || name == NULL || value == NULL)
return (NULL);
if ((attr = _ippAddAttr(ipp, 1)) == NULL)
return (NULL);
attr->name = _cupsStrAlloc(name);
attr->group_tag = group;
attr->value_tag = IPP_TAG_DATE;
memcpy(attr->values[0].date, value, 11);
return (attr);
}
ipp_attribute_t *
ippAddInteger(ipp_t *ipp,
ipp_tag_t group,
ipp_tag_t type,
const char *name,
int value)
{
ipp_attribute_t *attr;
DEBUG_printf(("ippAddInteger(%p, %d, \'%s\', %d)\n", ipp, group, name,
value));
if (ipp == NULL || name == NULL)
return (NULL);
if ((attr = _ippAddAttr(ipp, 1)) == NULL)
return (NULL);
attr->name = _cupsStrAlloc(name);
attr->group_tag = group;
attr->value_tag = type;
attr->values[0].integer = value;
return (attr);
}
ipp_attribute_t *
ippAddIntegers(ipp_t *ipp,
ipp_tag_t group,
ipp_tag_t type,
const char *name,
int num_values,
const int *values)
{
int i;
ipp_attribute_t *attr;
ipp_value_t *value;
if (ipp == NULL || name == NULL || num_values < 1)
return (NULL);
if ((attr = _ippAddAttr(ipp, num_values)) == NULL)
return (NULL);
attr->name = _cupsStrAlloc(name);
attr->group_tag = group;
attr->value_tag = type;
if (values != NULL)
for (i = 0, value = attr->values;
i < num_values;
i ++, value ++)
value->integer = values[i];
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 == NULL || name == NULL)
return (NULL);
if ((attr = _ippAddAttr(ipp, 1)) == NULL)
return (NULL);
attr->name = _cupsStrAlloc(name);
attr->group_tag = group;
attr->value_tag = IPP_TAG_STRING;
attr->values[0].unknown.length = datalen;
if (data)
{
attr->values[0].unknown.data = malloc(datalen);
memcpy(attr->values[0].unknown.data, data, datalen);
}
return (attr);
}
ipp_attribute_t *
ippAddString(ipp_t *ipp,
ipp_tag_t group,
ipp_tag_t type,
const char *name,
const char *charset,
const char *value)
{
ipp_attribute_t *attr;
char buffer[1024],
*bufptr;
if (ipp == NULL || name == NULL)
return (NULL);
if ((attr = _ippAddAttr(ipp, 1)) == NULL)
return (NULL);
if (type == IPP_TAG_LANGUAGE && !strcasecmp(value, "C"))
value = "en";
if ((type == IPP_TAG_LANGUAGE || type == IPP_TAG_CHARSET) && value)
{
strlcpy(buffer, value, sizeof(buffer));
value = buffer;
for (bufptr = buffer; *bufptr; bufptr ++)
if (*bufptr == '_')
*bufptr = '-';
else
*bufptr = tolower(*bufptr & 255);
}
attr->name = _cupsStrAlloc(name);
attr->group_tag = group;
attr->value_tag = type;
attr->values[0].string.charset = ((int)type & IPP_TAG_COPY) ? (char *)charset :
charset ? _cupsStrAlloc(charset) : NULL;
attr->values[0].string.text = ((int)type & IPP_TAG_COPY) ? (char *)value :
value ? _cupsStrAlloc(value) : NULL;
return (attr);
}
ipp_attribute_t *
ippAddStrings(
ipp_t *ipp,
ipp_tag_t group,
ipp_tag_t type,
const char *name,
int num_values,
const char *charset,
const char * const *values)
{
int i;
ipp_attribute_t *attr;
ipp_value_t *value;
if (ipp == NULL || name == NULL || num_values < 1)
return (NULL);
if ((attr = _ippAddAttr(ipp, num_values)) == NULL)
return (NULL);
attr->name = _cupsStrAlloc(name);
attr->group_tag = group;
attr->value_tag = type;
for (i = 0, value = attr->values;
i < num_values;
i ++, value ++)
{
if (i == 0)
value->string.charset = ((int)type & IPP_TAG_COPY) ? (char *)charset :
charset ? _cupsStrAlloc(charset) : NULL;
else
value->string.charset = attr->values[0].string.charset;
if (values != NULL)
{
if (type == IPP_TAG_LANGUAGE && !strcasecmp(values[i], "C"))
value->string.text = ((int)type & IPP_TAG_COPY) ? "en" :
_cupsStrAlloc("en");
else
value->string.text = ((int)type & IPP_TAG_COPY) ? (char *)values[i] :
_cupsStrAlloc(values[i]);
}
}
return (attr);
}
ipp_attribute_t *
ippAddRange(ipp_t *ipp,
ipp_tag_t group,
const char *name,
int lower,
int upper)
{
ipp_attribute_t *attr;
if (ipp == NULL || name == NULL)
return (NULL);
if ((attr = _ippAddAttr(ipp, 1)) == NULL)
return (NULL);
attr->name = _cupsStrAlloc(name);
attr->group_tag = group;
attr->value_tag = IPP_TAG_RANGE;
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;
if (ipp == NULL || name == NULL || num_values < 1)
return (NULL);
if ((attr = _ippAddAttr(ipp, num_values)) == NULL)
return (NULL);
attr->name = _cupsStrAlloc(name);
attr->group_tag = group;
attr->value_tag = IPP_TAG_RANGE;
if (lower != NULL && upper != NULL)
for (i = 0, value = attr->values;
i < num_values;
i ++, value ++)
{
value->range.lower = lower[i];
value->range.upper = upper[i];
}
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;
if (ipp == NULL || name == NULL)
return (NULL);
if ((attr = _ippAddAttr(ipp, 1)) == NULL)
return (NULL);
attr->name = _cupsStrAlloc(name);
attr->group_tag = group;
attr->value_tag = IPP_TAG_RESOLUTION;
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;
if (ipp == NULL || name == NULL || num_values < 1)
return (NULL);
if ((attr = _ippAddAttr(ipp, num_values)) == NULL)
return (NULL);
attr->name = _cupsStrAlloc(name);
attr->group_tag = group;
attr->value_tag = IPP_TAG_RESOLUTION;
if (xres != NULL && yres != NULL)
for (i = 0, value = attr->values;
i < num_values;
i ++, value ++)
{
value->resolution.xres = xres[i];
value->resolution.yres = yres[i];
value->resolution.units = units;
}
return (attr);
}
ipp_attribute_t *
ippAddSeparator(ipp_t *ipp)
{
ipp_attribute_t *attr;
DEBUG_printf(("ippAddSeparator(%p)\n", ipp));
if (ipp == NULL)
return (NULL);
if ((attr = _ippAddAttr(ipp, 0)) == NULL)
return (NULL);
attr->group_tag = IPP_TAG_ZERO;
attr->value_tag = IPP_TAG_ZERO;
return (attr);
}
time_t
ippDateToTime(const ipp_uchar_t *date)
{
struct tm unixdate;
time_t t;
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(): %p\n", ipp));
if (ipp == NULL)
return;
for (attr = ipp->attrs; attr != NULL; attr = next)
{
next = attr->next;
_ippFreeAttr(attr);
}
free(ipp);
}
void
ippDeleteAttribute(
ipp_t *ipp,
ipp_attribute_t *attr)
{
ipp_attribute_t *current,
*prev;
for (current = ipp->attrs, prev = NULL;
current != NULL && current != attr;
prev = current, current = current->next);
if (current)
{
if (prev)
prev->next = current->next;
else
ipp->attrs = current->next;
if (current == ipp->last)
ipp->last = prev;
_ippFreeAttr(current);
}
}
ipp_attribute_t *
ippFindAttribute(ipp_t *ipp,
const char *name,
ipp_tag_t type)
{
DEBUG_printf(("ippFindAttribute(%p, \'%s\')\n", ipp, name));
if (ipp == NULL || name == NULL)
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(("ippFindNextAttribute(%p, \'%s\')\n", ipp, name));
if (ipp == NULL || name == NULL)
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(("ippFindAttribute: attr = %p, name = \'%s\'\n", attr,
attr->name));
value_tag = (ipp_tag_t)(attr->value_tag & IPP_TAG_MASK);
if (attr->name != NULL && 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);
}
size_t
ippLength(ipp_t *ipp)
{
return (ipp_length(ipp, 0));
}
ipp_t *
ippNew(void)
{
ipp_t *temp;
DEBUG_puts("ippNew()");
if ((temp = (ipp_t *)calloc(1, sizeof(ipp_t))) != NULL)
{
temp->request.any.version[0] = 1;
temp->request.any.version[1] = 1;
}
DEBUG_printf(("ippNew: %p\n", temp));
return (temp);
}
ipp_t *
ippNewRequest(ipp_op_t op)
{
ipp_t *request;
cups_lang_t *language;
if ((request = ippNew()) == NULL)
return (NULL);
request->request.op.operation_id = op;
request->request.op.request_id = 1;
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 "\n",
http, ipp, CUPS_LLCAST (http ? http->data_remaining : -1)));
if (http == NULL)
return (IPP_ERROR);
DEBUG_printf(("http->state = %d\n", http->state));
return (ippReadIO(http, (ipp_iocb_t)ipp_read_http,
http->blocking || http->used != 0, NULL, ipp));
}
ipp_state_t
ippReadFile(int fd,
ipp_t *ipp)
{
DEBUG_printf(("ippReadFile(%d, %p)\n", 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[32768],
string[255],
*bufptr;
ipp_attribute_t *attr;
ipp_tag_t tag;
ipp_tag_t value_tag;
ipp_value_t *value;
DEBUG_printf(("ippReadIO(%p, %p, %d, %p, %p)\n", src, cb, blocking,
parent, ipp));
DEBUG_printf(("ippReadIO: ipp->state=%d\n", ipp->state));
if (src == NULL || ipp == NULL)
return (IPP_ERROR);
switch (ipp->state)
{
case IPP_IDLE :
ipp->state ++;
case IPP_HEADER :
if (parent == NULL)
{
if ((n = (*cb)(src, buffer, 8)) < 8)
{
DEBUG_printf(("ippReadIO: Unable to read header (%d bytes read)!\n", n));
return (IPP_ERROR);
}
if (buffer[0] != 1)
{
DEBUG_printf(("ippReadIO: version number (%d.%d) is bad.\n", buffer[0],
buffer[1]));
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(("ippReadIO: version=%d.%d\n", buffer[0], buffer[1]));
DEBUG_printf(("ippReadIO: op_status=%04x\n",
ipp->request.any.op_status));
DEBUG_printf(("ippReadIO: request_id=%d\n",
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)
return (IPP_ERROR);
DEBUG_printf(("ippReadIO: ipp->current=%p, ipp->prev=%p\n",
ipp->current, ipp->prev));
tag = (ipp_tag_t)buffer[0];
if (tag == IPP_TAG_END)
{
DEBUG_puts("ippReadIO: 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(("ippReadIO: group tag = %x, ipp->prev=%p\n", tag,
ipp->prev));
continue;
}
DEBUG_printf(("ippReadIO: value tag = %x\n", tag));
if ((*cb)(src, buffer, 2) < 2)
{
DEBUG_puts("ippReadIO: unable to read name length!");
return (IPP_ERROR);
}
n = (buffer[0] << 8) | buffer[1];
if (n > (sizeof(buffer) - 1))
{
DEBUG_printf(("ippReadIO: bad name length %d!\n", n));
return (IPP_ERROR);
}
DEBUG_printf(("ippReadIO: name length = %d\n", n));
if (n == 0 && tag != IPP_TAG_MEMBERNAME &&
tag != IPP_TAG_END_COLLECTION)
{
if (ipp->current == NULL)
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_STRING ||
(value_tag >= IPP_TAG_TEXTLANG &&
value_tag <= IPP_TAG_MIMETYPE))
{
if (tag != IPP_TAG_STRING &&
(tag < IPP_TAG_TEXTLANG || tag > IPP_TAG_MIMETYPE))
return (IPP_ERROR);
}
else if (value_tag != tag)
return (IPP_ERROR);
if (attr->num_values == 1 ||
(attr->num_values > 0 &&
(attr->num_values & (IPP_MAX_VALUES - 1)) == 0))
{
ipp_attribute_t *temp;
DEBUG_printf(("ippReadIO: reallocating for up to %d values...\n",
attr->num_values + IPP_MAX_VALUES));
if ((temp = realloc(attr, sizeof(ipp_attribute_t) +
(attr->num_values + IPP_MAX_VALUES - 1) *
sizeof(ipp_value_t))) == NULL)
return (IPP_ERROR);
if (temp != attr)
{
if (ipp->prev)
ipp->prev->next = temp;
else
ipp->attrs = temp;
attr = ipp->current = ipp->last = temp;
}
}
}
else if (tag == IPP_TAG_MEMBERNAME)
{
if (n)
{
DEBUG_puts("ippReadIO: member name not empty!");
return (IPP_ERROR);
}
if (ipp->current)
ipp->prev = ipp->current;
attr = ipp->current = _ippAddAttr(ipp, 1);
DEBUG_printf(("ippReadIO: membername, ipp->current=%p, ipp->prev=%p\n",
ipp->current, ipp->prev));
attr->group_tag = ipp->curtag;
attr->value_tag = IPP_TAG_ZERO;
attr->num_values = 0;
}
else if (tag != IPP_TAG_END_COLLECTION)
{
if ((*cb)(src, buffer, n) < n)
{
DEBUG_puts("ippReadIO: unable to read name!");
return (IPP_ERROR);
}
buffer[n] = '\0';
if (ipp->current)
ipp->prev = ipp->current;
attr = ipp->current = _ippAddAttr(ipp, 1);
DEBUG_printf(("ippReadIO: name=\'%s\', ipp->current=%p, ipp->prev=%p\n",
buffer, ipp->current, ipp->prev));
attr->group_tag = ipp->curtag;
attr->value_tag = tag;
attr->name = _cupsStrAlloc((char *)buffer);
attr->num_values = 0;
}
else
attr = NULL;
if (tag != IPP_TAG_END_COLLECTION)
value = attr->values + attr->num_values;
else
value = NULL;
if ((*cb)(src, buffer, 2) < 2)
{
DEBUG_puts("ippReadIO: unable to read value length!");
return (IPP_ERROR);
}
n = (buffer[0] << 8) | buffer[1];
DEBUG_printf(("ippReadIO: value length = %d\n", n));
switch (tag)
{
case IPP_TAG_INTEGER :
case IPP_TAG_ENUM :
if ((*cb)(src, buffer, 4) < 4)
{
DEBUG_puts("ippReadIO: Unable to read integer value!");
return (IPP_ERROR);
}
n = (((((buffer[0] << 8) | buffer[1]) << 8) | buffer[2]) << 8) |
buffer[3];
value->integer = n;
break;
case IPP_TAG_BOOLEAN :
if ((*cb)(src, buffer, 1) < 1)
{
DEBUG_puts("ippReadIO: Unable to read boolean value!");
return (IPP_ERROR);
}
value->boolean = buffer[0];
break;
case IPP_TAG_TEXT :
case IPP_TAG_NAME :
case IPP_TAG_KEYWORD :
case IPP_TAG_STRING :
case IPP_TAG_URI :
case IPP_TAG_URISCHEME :
case IPP_TAG_CHARSET :
case IPP_TAG_LANGUAGE :
case IPP_TAG_MIMETYPE :
if ((*cb)(src, buffer, n) < n)
{
DEBUG_puts("ippReadIO: unable to read name!");
return (IPP_ERROR);
}
buffer[n] = '\0';
value->string.text = _cupsStrAlloc((char *)buffer);
DEBUG_printf(("ippReadIO: value = \'%s\'\n",
value->string.text));
break;
case IPP_TAG_DATE :
if ((*cb)(src, value->date, 11) < 11)
{
DEBUG_puts("ippReadIO: Unable to date integer value!");
return (IPP_ERROR);
}
break;
case IPP_TAG_RESOLUTION :
if ((*cb)(src, buffer, 9) < 9)
{
DEBUG_puts("ippReadIO: Unable to read resolution value!");
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 ((*cb)(src, buffer, 8) < 8)
{
DEBUG_puts("ippReadIO: Unable to read range value!");
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 > sizeof(buffer) || n < 4)
{
DEBUG_printf(("ippReadIO: bad value length %d!\n", n));
return (IPP_ERROR);
}
if ((*cb)(src, buffer, n) < n)
{
DEBUG_puts("ippReadIO: Unable to read string w/language value!");
return (IPP_ERROR);
}
bufptr = buffer;
n = (bufptr[0] << 8) | bufptr[1];
if (n >= sizeof(string))
{
memcpy(string, bufptr + 2, sizeof(string) - 1);
string[sizeof(string) - 1] = '\0';
}
else
{
memcpy(string, bufptr + 2, n);
string[n] = '\0';
}
value->string.charset = _cupsStrAlloc((char *)string);
bufptr += 2 + n;
n = (bufptr[0] << 8) | bufptr[1];
bufptr[2 + n] = '\0';
value->string.text = _cupsStrAlloc((char *)bufptr + 2);
break;
case IPP_TAG_BEGIN_COLLECTION :
value->collection = ippNew();
if (n > 0)
{
DEBUG_puts("ippReadIO: begCollection tag with value length > 0!");
return (IPP_ERROR);
}
if (ippReadIO(src, cb, 1, ipp, value->collection) == IPP_ERROR)
{
DEBUG_puts("ippReadIO: Unable to read collection value!");
return (IPP_ERROR);
}
break;
case IPP_TAG_END_COLLECTION :
if (n > 0)
{
DEBUG_puts("ippReadIO: endCollection tag with value length > 0!");
return (IPP_ERROR);
}
DEBUG_puts("ippReadIO: endCollection tag...");
return (ipp->state = IPP_DATA);
case IPP_TAG_MEMBERNAME :
if ((*cb)(src, buffer, n) < n)
{
DEBUG_puts("ippReadIO: Unable to read member name value!");
return (IPP_ERROR);
}
buffer[n] = '\0';
attr->name = _cupsStrAlloc((char *)buffer);
attr->num_values --;
DEBUG_printf(("ippReadIO: member name = \"%s\"\n", attr->name));
break;
default :
value->unknown.length = n;
if (n > 0)
{
value->unknown.data = malloc(n);
if ((*cb)(src, value->unknown.data, n) < n)
{
DEBUG_puts("ippReadIO: Unable to read unsupported value!");
return (IPP_ERROR);
}
}
else
value->unknown.data = NULL;
break;
}
attr->num_values ++;
if (!blocking)
break;
}
break;
case IPP_DATA :
break;
default :
break;
}
DEBUG_printf(("ippReadIO: returning ipp->state=%d!\n", ipp->state));
return (ipp->state);
}
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(%p, %p)\n", http, ipp));
if (http == NULL)
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(%d, %p)\n", 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[32768],
*bufptr;
ipp_attribute_t *attr;
ipp_value_t *value;
DEBUG_printf(("ippWriteIO(%p, %p, %d, %p, %p)\n", dst, cb, blocking,
parent, ipp));
if (dst == NULL || ipp == NULL)
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;
if ((*cb)(dst, buffer, (int)(bufptr - buffer)) < 0)
{
DEBUG_puts("ippWriteIO: Could not write IPP header...");
return (IPP_ERROR);
}
}
ipp->state = IPP_ATTRIBUTE;
ipp->current = ipp->attrs;
ipp->curtag = IPP_TAG_ZERO;
DEBUG_printf(("ippWriteIO: version=%d.%d\n", buffer[0], buffer[1]));
DEBUG_printf(("ippWriteIO: op_status=%04x\n", ipp->request.any.op_status));
DEBUG_printf(("ippWriteIO: request_id=%d\n", ipp->request.any.request_id));
if (!blocking)
break;
case IPP_ATTRIBUTE :
while (ipp->current != NULL)
{
bufptr = buffer;
attr = ipp->current;
ipp->current = ipp->current->next;
if (ipp->curtag != attr->group_tag && parent == NULL)
{
ipp->curtag = attr->group_tag;
if (attr->group_tag == IPP_TAG_ZERO)
continue;
DEBUG_printf(("ippWriteIO: wrote group tag = %x\n", attr->group_tag));
*bufptr++ = attr->group_tag;
}
else if (attr->group_tag == IPP_TAG_ZERO)
continue;
if (parent == NULL)
{
if ((n = (int)strlen(attr->name)) > (sizeof(buffer) - 4))
return (IPP_ERROR);
DEBUG_printf(("ippWriteIO: writing value tag = %x\n", attr->value_tag));
DEBUG_printf(("ippWriteIO: writing name = %d, \'%s\'\n", n, attr->name));
*bufptr++ = attr->value_tag;
*bufptr++ = n >> 8;
*bufptr++ = n;
memcpy(bufptr, attr->name, n);
bufptr += n;
}
else
{
if ((n = (int)strlen(attr->name)) > (sizeof(buffer) - 7))
return (IPP_ERROR);
DEBUG_printf(("ippWriteIO: writing value tag = %x\n",
IPP_TAG_MEMBERNAME));
DEBUG_printf(("ippWriteIO: writing name = %d, \'%s\'\n", n, attr->name));
DEBUG_printf(("ippWriteIO: writing value tag = %x\n", attr->value_tag));
DEBUG_puts("ippWriteIO: writing name = 0, \'\'\n");
*bufptr++ = IPP_TAG_MEMBERNAME;
*bufptr++ = 0;
*bufptr++ = 0;
*bufptr++ = n >> 8;
*bufptr++ = n;
memcpy(bufptr, attr->name, n);
bufptr += n;
*bufptr++ = attr->value_tag;
*bufptr++ = 0;
*bufptr++ = 0;
}
switch (attr->value_tag & ~IPP_TAG_COPY)
{
case IPP_TAG_INTEGER :
case IPP_TAG_ENUM :
for (i = 0, value = attr->values;
i < attr->num_values;
i ++, value ++)
{
if ((sizeof(buffer) - (bufptr - buffer)) < 9)
{
if ((*cb)(dst, buffer, (int)(bufptr - buffer)) < 0)
{
DEBUG_puts("ippWriteIO: Could not write IPP attribute...");
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 ((sizeof(buffer) - (bufptr - buffer)) < 6)
{
if ((*cb)(dst, buffer, (int)(bufptr - buffer)) < 0)
{
DEBUG_puts("ippWriteIO: Could not write IPP attribute...");
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_STRING :
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(("ippWriteIO: writing value tag = %x\n",
attr->value_tag));
DEBUG_printf(("ippWriteIO: writing name = 0, \'\'\n"));
if ((sizeof(buffer) - (bufptr - buffer)) < 3)
{
if ((*cb)(dst, buffer, (int)(bufptr - buffer)) < 0)
{
DEBUG_puts("ippWriteIO: Could not write IPP attribute...");
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 > (sizeof(buffer) - 2))
return (IPP_ERROR);
DEBUG_printf(("ippWriteIO: writing string = %d, \'%s\'\n", n,
value->string.text));
if ((int)(sizeof(buffer) - (bufptr - buffer)) < (n + 2))
{
if ((*cb)(dst, buffer, (int)(bufptr - buffer)) < 0)
{
DEBUG_puts("ippWriteIO: Could not write IPP attribute...");
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 ((sizeof(buffer) - (bufptr - buffer)) < 16)
{
if ((*cb)(dst, buffer, (int)(bufptr - buffer)) < 0)
{
DEBUG_puts("ippWriteIO: Could not write IPP attribute...");
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 ((sizeof(buffer) - (bufptr - buffer)) < 14)
{
if ((*cb)(dst, buffer, (int)(bufptr - buffer)) < 0)
{
DEBUG_puts("ippWriteIO: Could not write IPP attribute...");
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 ((sizeof(buffer) - (bufptr - buffer)) < 13)
{
if ((*cb)(dst, buffer, (int)(bufptr - buffer)) < 0)
{
DEBUG_puts("ippWriteIO: Could not write IPP attribute...");
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 ((sizeof(buffer) - (bufptr - buffer)) < 3)
{
if ((*cb)(dst, buffer, (int)(bufptr - buffer)) < 0)
{
DEBUG_puts("ippWriteIO: Could not write IPP attribute...");
return (IPP_ERROR);
}
bufptr = buffer;
}
*bufptr++ = attr->value_tag;
*bufptr++ = 0;
*bufptr++ = 0;
}
n = 4;
if (value->string.charset != NULL)
n += (int)strlen(value->string.charset);
if (value->string.text != NULL)
n += (int)strlen(value->string.text);
if (n > (sizeof(buffer) - 2))
return (IPP_ERROR);
if ((int)(sizeof(buffer) - (bufptr - buffer)) < (n + 2))
{
if ((*cb)(dst, buffer, (int)(bufptr - buffer)) < 0)
{
DEBUG_puts("ippWriteIO: Could not write IPP attribute...");
return (IPP_ERROR);
}
bufptr = buffer;
}
*bufptr++ = n >> 8;
*bufptr++ = n;
if (value->string.charset != NULL)
n = (int)strlen(value->string.charset);
else
n = 0;
*bufptr++ = n >> 8;
*bufptr++ = n;
if (n > 0)
{
memcpy(bufptr, value->string.charset, 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 ((sizeof(buffer) - (bufptr - buffer)) < 5)
{
if ((*cb)(dst, buffer, (int)(bufptr - buffer)) < 0)
{
DEBUG_puts("ippWriteIO: Could not write IPP attribute...");
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("ippWriteIO: Could not write IPP attribute...");
return (IPP_ERROR);
}
bufptr = buffer;
value->collection->state = IPP_IDLE;
if (ippWriteIO(dst, cb, 1, ipp, value->collection) == IPP_ERROR)
return (IPP_ERROR);
}
break;
default :
for (i = 0, value = attr->values;
i < attr->num_values;
i ++, value ++)
{
if (i)
{
if ((sizeof(buffer) - (bufptr - buffer)) < 3)
{
if ((*cb)(dst, buffer, (int)(bufptr - buffer)) < 0)
{
DEBUG_puts("ippWriteIO: Could not write IPP attribute...");
return (IPP_ERROR);
}
bufptr = buffer;
}
*bufptr++ = attr->value_tag;
*bufptr++ = 0;
*bufptr++ = 0;
}
n = value->unknown.length;
if (n > (sizeof(buffer) - 2))
return (IPP_ERROR);
if ((int)(sizeof(buffer) - (bufptr - buffer)) < (n + 2))
{
if ((*cb)(dst, buffer, (int)(bufptr - buffer)) < 0)
{
DEBUG_puts("ippWriteIO: Could not write IPP attribute...");
return (IPP_ERROR);
}
bufptr = buffer;
}
*bufptr++ = n >> 8;
*bufptr++ = n;
if (n > 0)
{
memcpy(bufptr, value->unknown.data, n);
bufptr += n;
}
}
break;
}
if ((*cb)(dst, buffer, (int)(bufptr - buffer)) < 0)
{
DEBUG_puts("ippWriteIO: Could not write IPP attribute...");
return (IPP_ERROR);
}
DEBUG_printf(("ippWriteIO: wrote %d bytes\n", 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("ippWriteIO: Could not write IPP end-tag...");
return (IPP_ERROR);
}
ipp->state = IPP_DATA;
}
break;
case IPP_DATA :
break;
default :
break;
}
return (ipp->state);
}
ipp_attribute_t *
_ippAddAttr(ipp_t *ipp,
int num_values)
{
ipp_attribute_t *attr;
DEBUG_printf(("_ippAddAttr(%p, %d)\n", ipp, num_values));
if (ipp == NULL || num_values < 0)
return (NULL);
attr = calloc(sizeof(ipp_attribute_t) +
(num_values - 1) * sizeof(ipp_value_t), 1);
if (attr != NULL)
{
attr->num_values = num_values;
if (ipp->last == NULL)
ipp->attrs = attr;
else
ipp->last->next = attr;
ipp->last = attr;
}
DEBUG_printf(("_ippAddAttr(): %p\n", attr));
return (attr);
}
void
_ippFreeAttr(ipp_attribute_t *attr)
{
int i;
ipp_value_t *value;
DEBUG_printf(("_ippFreeAttr(): %p\n", attr));
switch (attr->value_tag)
{
case IPP_TAG_TEXT :
case IPP_TAG_NAME :
case IPP_TAG_KEYWORD :
case IPP_TAG_STRING :
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 ++)
_cupsStrFree(value->string.text);
break;
case IPP_TAG_TEXTLANG :
case IPP_TAG_NAMELANG :
for (i = 0, value = attr->values;
i < attr->num_values;
i ++, value ++)
{
if (value->string.charset && i == 0)
_cupsStrFree(value->string.charset);
_cupsStrFree(value->string.text);
}
break;
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 = 0, value = attr->values;
i < attr->num_values;
i ++, value ++)
ippDelete(value->collection);
break;
default :
if (!((int)attr->value_tag & IPP_TAG_COPY))
{
for (i = 0, value = attr->values;
i < attr->num_values;
i ++, value ++)
if (value->unknown.data)
free(value->unknown.data);
}
break;
}
if (attr->name)
_cupsStrFree(attr->name);
free(attr);
}
static size_t
ipp_length(ipp_t *ipp,
int collection)
{
int i;
int bytes;
ipp_attribute_t *attr;
ipp_tag_t group;
ipp_value_t *value;
if (ipp == NULL)
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(("attr->name = %s, attr->num_values = %d, bytes = %d\n",
attr->name, attr->num_values, bytes));
bytes += (int)strlen(attr->name);
bytes += attr->num_values;
bytes += 2 * attr->num_values;
bytes += 2 * attr->num_values;
if (collection)
bytes += 5;
switch (attr->value_tag & ~IPP_TAG_COPY)
{
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_STRING :
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 != NULL)
bytes += (int)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.charset != NULL)
bytes += (int)strlen(value->string.charset);
if (value->string.text != NULL)
bytes += (int)strlen(value->string.text);
}
break;
case IPP_TAG_BEGIN_COLLECTION :
for (i = 0, value = attr->values;
i < attr->num_values;
i ++, value ++)
bytes += (int)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(("bytes = %d\n", bytes));
return (bytes);
}
static ssize_t
ipp_read_http(http_t *http,
ipp_uchar_t *buffer,
size_t length)
{
int tbytes,
bytes;
char len[32];
DEBUG_printf(("ipp_read_http(http=%p, buffer=%p, length=%d)\n",
http, buffer, length));
for (tbytes = 0, bytes = 0; tbytes < (int)length; tbytes += bytes, buffer += bytes)
{
DEBUG_printf(("tbytes = %d, http->state = %d\n", tbytes, http->state));
if (http->state == HTTP_WAITING)
break;
if (http->used > 0 && http->data_encoding == HTTP_ENCODE_LENGTH)
{
if (http->used > (int)(length - tbytes))
bytes = (int)(length - tbytes);
else
bytes = http->used;
if (bytes == 1)
buffer[0] = http->buffer[0];
else
memcpy(buffer, http->buffer, bytes);
http->used -= bytes;
http->data_remaining -= bytes;
if (http->data_remaining <= INT_MAX)
http->_data_remaining = (int)http->data_remaining;
else
http->_data_remaining = INT_MAX;
if (http->used > 0)
memmove(http->buffer, http->buffer + bytes, http->used);
if (http->data_remaining == 0)
{
if (http->data_encoding == HTTP_ENCODE_CHUNKED)
{
if (!httpGets(len, sizeof(len), http))
return (-1);
}
if (http->data_encoding != HTTP_ENCODE_CHUNKED)
{
if (http->state == HTTP_POST_RECV)
http->state ++;
else
http->state = HTTP_WAITING;
}
}
}
else
{
if (!http->blocking)
{
if (!httpWait(http, 10000))
{
bytes = -1;
break;
}
}
if ((bytes = httpRead2(http, (char *)buffer, length - tbytes)) <= 0)
break;
}
}
if (tbytes == 0 && bytes < 0)
tbytes = -1;
DEBUG_printf(("returning %d bytes...\n", 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 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
}
#ifdef __linux
ipp_attribute_t *
_ipp_add_attr(ipp_t *ipp,
int num_values)
{
return (_ippAddAttr(ipp, num_values));
}
void
_ipp_free_attr(ipp_attribute_t *attr)
{
_ippFreeAttr(attr);
}
#endif