2 * "$Id: ipp.c 7847 2008-08-19 04:22:14Z mike $"
4 * Internet Printing Protocol support functions for the Common UNIX
5 * Printing System (CUPS).
7 * Copyright 2007-2009 by Apple Inc.
8 * Copyright 1997-2007 by Easy Software Products, all rights reserved.
10 * These coded instructions, statements, and computer programs are the
11 * property of Apple Inc. and are protected by Federal copyright
12 * law. Distribution and use rights are outlined in the file "LICENSE.txt"
13 * which should have been included with this file. If this file is
14 * file is missing or damaged, see the license at "http://www.cups.org/".
16 * This file is subject to the Apple OS-Developed Software exception.
20 * ippAddBoolean() - Add a boolean attribute to an IPP message.
21 * ippAddBooleans() - Add an array of boolean values.
22 * ippAddDate() - Add a date attribute to an IPP message.
23 * ippAddInteger() - Add a integer attribute to an IPP message.
24 * ippAddIntegers() - Add an array of integer values.
25 * ippAddOctetString() - Add an octetString value to an IPP message.
26 * ippAddString() - Add a language-encoded string to an IPP message.
27 * ippAddStrings() - Add language-encoded strings to an IPP message.
28 * ippAddRange() - Add a range of values to an IPP message.
29 * ippAddRanges() - Add ranges of values to an IPP message.
30 * ippAddResolution() - Add a resolution value to an IPP message.
31 * ippAddResolutions() - Add resolution values to an IPP message.
32 * ippAddSeparator() - Add a group separator to an IPP message.
33 * ippDateToTime() - Convert from RFC 1903 Date/Time format to
34 * UNIX time in seconds.
35 * ippDelete() - Delete an IPP message.
36 * ippDeleteAttribute() - Delete a single attribute in an IPP message.
37 * ippFindAttribute() - Find a named attribute in a request...
38 * ippFindNextAttribute() - Find the next named attribute in a request...
39 * ippLength() - Compute the length of an IPP message.
40 * ippNew() - Allocate a new IPP message.
41 * ippNewRequest() - Allocate a new IPP message.
42 * ippRead() - Read data for an IPP message from a HTTP
44 * ippReadFile() - Read data for an IPP message from a file.
45 * ippReadIO() - Read data for an IPP message.
46 * ippTimeToDate() - Convert from UNIX time to RFC 1903 format.
47 * ippWrite() - Write data for an IPP message to a HTTP
49 * ippWriteFile() - Write data for an IPP message to a file.
50 * ippWriteIO() - Write data for an IPP message.
51 * _ippAddAttr() - Add a new attribute to the request.
52 * _ippFreeAttr() - Free an attribute.
53 * ipp_length() - Compute the length of an IPP message or
55 * ipp_read_http() - Semi-blocking read on a HTTP connection...
56 * ipp_read_file() - Read IPP data from a file.
57 * ipp_write_file() - Write IPP data to a file.
61 * Include necessary headers...
64 #include "http-private.h"
78 static unsigned char *ipp_buffer_get(void);
79 static void ipp_buffer_release(unsigned char *b
);
80 static size_t ipp_length(ipp_t
*ipp
, int collection
);
81 static ssize_t
ipp_read_http(http_t
*http
, ipp_uchar_t
*buffer
,
83 static ssize_t
ipp_read_file(int *fd
, ipp_uchar_t
*buffer
,
85 static ssize_t
ipp_write_file(int *fd
, ipp_uchar_t
*buffer
,
90 * 'ippAddBoolean()' - Add a boolean attribute to an IPP message.
93 ipp_attribute_t
* /* O - New attribute */
94 ippAddBoolean(ipp_t
*ipp
, /* I - IPP message */
95 ipp_tag_t group
, /* I - IPP group */
96 const char *name
, /* I - Name of attribute */
97 char value
) /* I - Value of attribute */
99 ipp_attribute_t
*attr
; /* New attribute */
102 DEBUG_printf(("ippAddBoolean(ipp=%p, group=%02x(%s), name=\"%s\", value=%d)\n",
103 ipp
, group
, ippTagString(group
), name
, value
));
108 if ((attr
= _ippAddAttr(ipp
, 1)) == NULL
)
111 attr
->name
= _cupsStrAlloc(name
);
112 attr
->group_tag
= group
;
113 attr
->value_tag
= IPP_TAG_BOOLEAN
;
114 attr
->values
[0].boolean
= value
;
121 * 'ippAddBooleans()' - Add an array of boolean values.
124 ipp_attribute_t
* /* O - New attribute */
125 ippAddBooleans(ipp_t
*ipp
, /* I - IPP message */
126 ipp_tag_t group
, /* I - IPP group */
127 const char *name
, /* I - Name of attribute */
128 int num_values
, /* I - Number of values */
129 const char *values
) /* I - Values */
131 int i
; /* Looping var */
132 ipp_attribute_t
*attr
; /* New attribute */
133 ipp_value_t
*value
; /* Current value */
136 DEBUG_printf(("ippAddBooleans(ipp=%p, group=%02x(%s), name=\"%s\", "
137 "num_values=%d, values=%p)\n", ipp
, group
, ippTagString(group
),
138 name
, num_values
, values
));
140 if (!ipp
|| !name
|| num_values
< 1)
143 if ((attr
= _ippAddAttr(ipp
, num_values
)) == NULL
)
146 attr
->name
= _cupsStrAlloc(name
);
147 attr
->group_tag
= group
;
148 attr
->value_tag
= IPP_TAG_BOOLEAN
;
151 for (i
= 0, value
= attr
->values
;
154 value
->boolean
= values
[i
];
161 * 'ippAddCollection()' - Add a collection value.
163 * @since CUPS 1.1.19/Mac OS X 10.3@
166 ipp_attribute_t
* /* O - New attribute */
167 ippAddCollection(ipp_t
*ipp
, /* I - IPP message */
168 ipp_tag_t group
, /* I - IPP group */
169 const char *name
, /* I - Name of attribute */
170 ipp_t
*value
) /* I - Value */
172 ipp_attribute_t
*attr
; /* New attribute */
175 DEBUG_printf(("ippAddCollection(ipp=%p, group=%02x(%s), name=\"%s\", "
176 "value=%p)\n", ipp
, group
, ippTagString(group
), name
, value
));
181 if ((attr
= _ippAddAttr(ipp
, 1)) == NULL
)
184 attr
->name
= _cupsStrAlloc(name
);
185 attr
->group_tag
= group
;
186 attr
->value_tag
= IPP_TAG_BEGIN_COLLECTION
;
187 attr
->values
[0].collection
= value
;
194 * 'ippAddCollections()' - Add an array of collection values.
196 * @since CUPS 1.1.19/Mac OS X 10.3@
199 ipp_attribute_t
* /* O - New attribute */
201 ipp_t
*ipp
, /* I - IPP message */
202 ipp_tag_t group
, /* I - IPP group */
203 const char *name
, /* I - Name of attribute */
204 int num_values
, /* I - Number of values */
205 const ipp_t
**values
) /* I - Values */
207 int i
; /* Looping var */
208 ipp_attribute_t
*attr
; /* New attribute */
209 ipp_value_t
*value
; /* Current value */
212 DEBUG_printf(("ippAddCollections(ipp=%p, group=%02x(%s), name=\"%s\", "
213 "num_values=%d, values=%p)\n", ipp
, group
, ippTagString(group
),
214 name
, num_values
, values
));
216 if (!ipp
|| !name
|| num_values
< 1)
219 if ((attr
= _ippAddAttr(ipp
, num_values
)) == NULL
)
222 attr
->name
= _cupsStrAlloc(name
);
223 attr
->group_tag
= group
;
224 attr
->value_tag
= IPP_TAG_BEGIN_COLLECTION
;
227 for (i
= 0, value
= attr
->values
;
230 value
->collection
= (ipp_t
*)values
[i
];
237 * 'ippAddDate()' - Add a date attribute to an IPP message.
240 ipp_attribute_t
* /* O - New attribute */
241 ippAddDate(ipp_t
*ipp
, /* I - IPP message */
242 ipp_tag_t group
, /* I - IPP group */
243 const char *name
, /* I - Name of attribute */
244 const ipp_uchar_t
*value
) /* I - Value */
246 ipp_attribute_t
*attr
; /* New attribute */
249 DEBUG_printf(("ippAddDate(ipp=%p, group=%02x(%s), name=\"%s\", value=%p)\n",
250 ipp
, group
, ippTagString(group
), name
, value
));
252 if (!ipp
|| !name
|| !value
)
255 if ((attr
= _ippAddAttr(ipp
, 1)) == NULL
)
258 attr
->name
= _cupsStrAlloc(name
);
259 attr
->group_tag
= group
;
260 attr
->value_tag
= IPP_TAG_DATE
;
261 memcpy(attr
->values
[0].date
, value
, 11);
268 * 'ippAddInteger()' - Add a integer attribute to an IPP message.
271 ipp_attribute_t
* /* O - New attribute */
272 ippAddInteger(ipp_t
*ipp
, /* I - IPP message */
273 ipp_tag_t group
, /* I - IPP group */
274 ipp_tag_t type
, /* I - Type of attribute */
275 const char *name
, /* I - Name of attribute */
276 int value
) /* I - Value of attribute */
278 ipp_attribute_t
*attr
; /* New attribute */
281 DEBUG_printf(("ippAddInteger(ipp=%p, group=%02x(%s), type=%02x(%s), "
282 "name=\"%s\", value=%d)\n", ipp
, group
, ippTagString(group
),
283 type
, ippTagString(type
), name
, value
));
288 if ((attr
= _ippAddAttr(ipp
, 1)) == NULL
)
291 attr
->name
= _cupsStrAlloc(name
);
292 attr
->group_tag
= group
;
293 attr
->value_tag
= type
;
294 attr
->values
[0].integer
= value
;
301 * 'ippAddIntegers()' - Add an array of integer values.
304 ipp_attribute_t
* /* O - New attribute */
305 ippAddIntegers(ipp_t
*ipp
, /* I - IPP message */
306 ipp_tag_t group
, /* I - IPP group */
307 ipp_tag_t type
, /* I - Type of attribute */
308 const char *name
, /* I - Name of attribute */
309 int num_values
, /* I - Number of values */
310 const int *values
) /* I - Values */
312 int i
; /* Looping var */
313 ipp_attribute_t
*attr
; /* New attribute */
314 ipp_value_t
*value
; /* Current value */
317 DEBUG_printf(("ippAddIntegers(ipp=%p, group=%02x(%s), type=%02x(%s), "
318 "name=\"%s\", num_values=%d, values=%p)\n", ipp
,
319 group
, ippTagString(group
), type
, ippTagString(type
), name
,
320 num_values
, values
));
322 if (!ipp
|| !name
|| num_values
< 1)
325 if ((attr
= _ippAddAttr(ipp
, num_values
)) == NULL
)
328 attr
->name
= _cupsStrAlloc(name
);
329 attr
->group_tag
= group
;
330 attr
->value_tag
= type
;
333 for (i
= 0, value
= attr
->values
;
336 value
->integer
= values
[i
];
343 * 'ippAddOctetString()' - Add an octetString value to an IPP message.
345 * @since CUPS 1.2/Mac OS X 10.5@
348 ipp_attribute_t
* /* O - New attribute */
349 ippAddOctetString(ipp_t
*ipp
, /* I - IPP message */
350 ipp_tag_t group
, /* I - IPP group */
351 const char *name
, /* I - Name of attribute */
352 const void *data
, /* I - octetString data */
353 int datalen
) /* I - Length of data in bytes */
355 ipp_attribute_t
*attr
; /* New attribute */
358 if (ipp
== NULL
|| name
== NULL
)
361 if ((attr
= _ippAddAttr(ipp
, 1)) == NULL
)
365 * Initialize the attribute data...
368 attr
->name
= _cupsStrAlloc(name
);
369 attr
->group_tag
= group
;
370 attr
->value_tag
= IPP_TAG_STRING
;
371 attr
->values
[0].unknown
.length
= datalen
;
375 if ((attr
->values
[0].unknown
.data
= malloc(datalen
)) == NULL
)
377 ippDeleteAttribute(ipp
, attr
);
381 memcpy(attr
->values
[0].unknown
.data
, data
, datalen
);
385 * Return the new attribute...
393 * 'ippAddString()' - Add a language-encoded string to an IPP message.
396 ipp_attribute_t
* /* O - New attribute */
397 ippAddString(ipp_t
*ipp
, /* I - IPP message */
398 ipp_tag_t group
, /* I - IPP group */
399 ipp_tag_t type
, /* I - Type of attribute */
400 const char *name
, /* I - Name of attribute */
401 const char *charset
, /* I - Character set */
402 const char *value
) /* I - Value */
404 ipp_attribute_t
*attr
; /* New attribute */
405 char buffer
[1024], /* Language/charset value buffer */
406 *bufptr
; /* Pointer into buffer */
409 DEBUG_printf(("ippAddString(ipp=%p, group=%02x(%s), type=%02x(%s), "
410 "name=\"%s\", charset=\"%s\", value=\"%s\")\n", ipp
,
411 group
, ippTagString(group
), type
, ippTagString(type
), name
,
417 if ((attr
= _ippAddAttr(ipp
, 1)) == NULL
)
421 * Force value to be English for the POSIX locale...
424 if (type
== IPP_TAG_LANGUAGE
&& !strcasecmp(value
, "C"))
428 * Convert language values to lowercase and change _ to - as needed...
431 if ((type
== IPP_TAG_LANGUAGE
|| type
== IPP_TAG_CHARSET
) && value
)
433 strlcpy(buffer
, value
, sizeof(buffer
));
436 for (bufptr
= buffer
; *bufptr
; bufptr
++)
440 *bufptr
= tolower(*bufptr
& 255);
444 * Initialize the attribute data...
447 attr
->name
= _cupsStrAlloc(name
);
448 attr
->group_tag
= group
;
449 attr
->value_tag
= type
;
450 attr
->values
[0].string
.charset
= ((int)type
& IPP_TAG_COPY
) ? (char *)charset
:
451 charset
? _cupsStrAlloc(charset
) : NULL
;
452 attr
->values
[0].string
.text
= ((int)type
& IPP_TAG_COPY
) ? (char *)value
:
453 value
? _cupsStrAlloc(value
) : NULL
;
460 * 'ippAddStrings()' - Add language-encoded strings to an IPP message.
463 ipp_attribute_t
* /* O - New attribute */
465 ipp_t
*ipp
, /* I - IPP message */
466 ipp_tag_t group
, /* I - IPP group */
467 ipp_tag_t type
, /* I - Type of attribute */
468 const char *name
, /* I - Name of attribute */
469 int num_values
, /* I - Number of values */
470 const char *charset
, /* I - Character set */
471 const char * const *values
) /* I - Values */
473 int i
; /* Looping var */
474 ipp_attribute_t
*attr
; /* New attribute */
475 ipp_value_t
*value
; /* Current value */
478 DEBUG_printf(("ippAddStrings(ipp=%p, group=%02x(%s), type=%02x(%s), "
479 "name=\"%s\", num_values=%d, charset=\"%s\", values=%p)\n", ipp
,
480 group
, ippTagString(group
), type
, ippTagString(type
), name
,
481 num_values
, charset
, values
));
483 if (!ipp
|| !name
|| num_values
< 1)
486 if ((attr
= _ippAddAttr(ipp
, num_values
)) == NULL
)
490 * Initialize the attribute data...
493 attr
->name
= _cupsStrAlloc(name
);
494 attr
->group_tag
= group
;
495 attr
->value_tag
= type
;
497 for (i
= 0, value
= attr
->values
;
502 value
->string
.charset
= ((int)type
& IPP_TAG_COPY
) ? (char *)charset
:
503 charset
? _cupsStrAlloc(charset
) : NULL
;
505 value
->string
.charset
= attr
->values
[0].string
.charset
;
510 * Force language to be English for the POSIX locale...
513 if (type
== IPP_TAG_LANGUAGE
&& !strcasecmp(values
[i
], "C"))
514 value
->string
.text
= ((int)type
& IPP_TAG_COPY
) ? "en" :
517 value
->string
.text
= ((int)type
& IPP_TAG_COPY
) ? (char *)values
[i
] :
518 _cupsStrAlloc(values
[i
]);
527 * 'ippAddRange()' - Add a range of values to an IPP message.
530 ipp_attribute_t
* /* O - New attribute */
531 ippAddRange(ipp_t
*ipp
, /* I - IPP message */
532 ipp_tag_t group
, /* I - IPP group */
533 const char *name
, /* I - Name of attribute */
534 int lower
, /* I - Lower value */
535 int upper
) /* I - Upper value */
537 ipp_attribute_t
*attr
; /* New attribute */
540 DEBUG_printf(("ippAddRange(ipp=%p, group=%02x(%s), name=\"%s\", lower=%d, "
541 "upper=%d)\n", ipp
, group
, ippTagString(group
), name
, lower
,
547 if ((attr
= _ippAddAttr(ipp
, 1)) == NULL
)
550 attr
->name
= _cupsStrAlloc(name
);
551 attr
->group_tag
= group
;
552 attr
->value_tag
= IPP_TAG_RANGE
;
553 attr
->values
[0].range
.lower
= lower
;
554 attr
->values
[0].range
.upper
= upper
;
561 * 'ippAddRanges()' - Add ranges of values to an IPP message.
564 ipp_attribute_t
* /* O - New attribute */
565 ippAddRanges(ipp_t
*ipp
, /* I - IPP message */
566 ipp_tag_t group
, /* I - IPP group */
567 const char *name
, /* I - Name of attribute */
568 int num_values
, /* I - Number of values */
569 const int *lower
, /* I - Lower values */
570 const int *upper
) /* I - Upper values */
572 int i
; /* Looping var */
573 ipp_attribute_t
*attr
; /* New attribute */
574 ipp_value_t
*value
; /* Current value */
577 DEBUG_printf(("ippAddRanges(ipp=%p, group=%02x(%s), name=\"%s\", "
578 "num_values=%d, lower=%p, upper=%p)\n", ipp
, group
,
579 ippTagString(group
), name
, num_values
, lower
, upper
));
581 if (!ipp
|| !name
|| num_values
< 1)
584 if ((attr
= _ippAddAttr(ipp
, num_values
)) == NULL
)
587 attr
->name
= _cupsStrAlloc(name
);
588 attr
->group_tag
= group
;
589 attr
->value_tag
= IPP_TAG_RANGE
;
591 if (lower
!= NULL
&& upper
!= NULL
)
592 for (i
= 0, value
= attr
->values
;
596 value
->range
.lower
= lower
[i
];
597 value
->range
.upper
= upper
[i
];
605 * 'ippAddResolution()' - Add a resolution value to an IPP message.
608 ipp_attribute_t
* /* O - New attribute */
609 ippAddResolution(ipp_t
*ipp
, /* I - IPP message */
610 ipp_tag_t group
, /* I - IPP group */
611 const char *name
, /* I - Name of attribute */
612 ipp_res_t units
, /* I - Units for resolution */
613 int xres
, /* I - X resolution */
614 int yres
) /* I - Y resolution */
616 ipp_attribute_t
*attr
; /* New attribute */
619 DEBUG_printf(("ippAddResolution(ipp=%p, group=%02x(%s), name=\"%s\", "
620 "units=%d, xres=%d, yres=%d)\n", ipp
, group
,
621 ippTagString(group
), name
, units
, xres
, yres
));
626 if ((attr
= _ippAddAttr(ipp
, 1)) == NULL
)
629 attr
->name
= _cupsStrAlloc(name
);
630 attr
->group_tag
= group
;
631 attr
->value_tag
= IPP_TAG_RESOLUTION
;
632 attr
->values
[0].resolution
.xres
= xres
;
633 attr
->values
[0].resolution
.yres
= yres
;
634 attr
->values
[0].resolution
.units
= units
;
641 * 'ippAddResolutions()' - Add resolution values to an IPP message.
644 ipp_attribute_t
* /* O - New attribute */
645 ippAddResolutions(ipp_t
*ipp
, /* I - IPP message */
646 ipp_tag_t group
, /* I - IPP group */
647 const char *name
, /* I - Name of attribute */
648 int num_values
,/* I - Number of values */
649 ipp_res_t units
, /* I - Units for resolution */
650 const int *xres
, /* I - X resolutions */
651 const int *yres
) /* I - Y resolutions */
653 int i
; /* Looping var */
654 ipp_attribute_t
*attr
; /* New attribute */
655 ipp_value_t
*value
; /* Current value */
658 DEBUG_printf(("ippAddResolutions(ipp=%p, group=%02x(%s), name=\"%s\", "
659 "num_value=%d, units=%d, xres=%p, yres=%p)\n", ipp
, group
,
660 ippTagString(group
), name
, num_values
, units
, xres
, yres
));
662 if (!ipp
|| !name
|| num_values
< 1)
665 if ((attr
= _ippAddAttr(ipp
, num_values
)) == NULL
)
668 attr
->name
= _cupsStrAlloc(name
);
669 attr
->group_tag
= group
;
670 attr
->value_tag
= IPP_TAG_RESOLUTION
;
672 if (xres
!= NULL
&& yres
!= NULL
)
673 for (i
= 0, value
= attr
->values
;
677 value
->resolution
.xres
= xres
[i
];
678 value
->resolution
.yres
= yres
[i
];
679 value
->resolution
.units
= units
;
687 * 'ippAddSeparator()' - Add a group separator to an IPP message.
690 ipp_attribute_t
* /* O - New attribute */
691 ippAddSeparator(ipp_t
*ipp
) /* I - IPP message */
693 ipp_attribute_t
*attr
; /* New attribute */
696 DEBUG_printf(("ippAddSeparator(ipp=%p)\n", ipp
));
701 if ((attr
= _ippAddAttr(ipp
, 0)) == NULL
)
704 attr
->group_tag
= IPP_TAG_ZERO
;
705 attr
->value_tag
= IPP_TAG_ZERO
;
712 * 'ippDateToTime()' - Convert from RFC 1903 Date/Time format to UNIX time
716 time_t /* O - UNIX time value */
717 ippDateToTime(const ipp_uchar_t
*date
) /* I - RFC 1903 date info */
719 struct tm unixdate
; /* UNIX date/time info */
720 time_t t
; /* Computed time */
726 memset(&unixdate
, 0, sizeof(unixdate
));
729 * RFC-1903 date/time format is:
731 * Byte(s) Description
732 * ------- -----------
733 * 0-1 Year (0 to 65535)
737 * 5 Minutes (0 to 59)
738 * 6 Seconds (0 to 60, 60 = "leap second")
739 * 7 Deciseconds (0 to 9)
741 * 9 UTC hours (0 to 11)
742 * 10 UTC minutes (0 to 59)
745 unixdate
.tm_year
= ((date
[0] << 8) | date
[1]) - 1900;
746 unixdate
.tm_mon
= date
[2] - 1;
747 unixdate
.tm_mday
= date
[3];
748 unixdate
.tm_hour
= date
[4];
749 unixdate
.tm_min
= date
[5];
750 unixdate
.tm_sec
= date
[6];
752 t
= mktime(&unixdate
);
755 t
+= date
[9] * 3600 + date
[10] * 60;
757 t
-= date
[9] * 3600 + date
[10] * 60;
764 * 'ippDelete()' - Delete an IPP message.
768 ippDelete(ipp_t
*ipp
) /* I - IPP message */
770 ipp_attribute_t
*attr
, /* Current attribute */
771 *next
; /* Next attribute */
774 DEBUG_printf(("ippDelete(ipp=%p)\n", ipp
));
779 for (attr
= ipp
->attrs
; attr
!= NULL
; attr
= next
)
790 * 'ippDeleteAttribute()' - Delete a single attribute in an IPP message.
792 * @since CUPS 1.1.19/Mac OS X 10.3@
797 ipp_t
*ipp
, /* I - IPP message */
798 ipp_attribute_t
*attr
) /* I - Attribute to delete */
800 ipp_attribute_t
*current
, /* Current attribute */
801 *prev
; /* Previous attribute */
804 DEBUG_printf(("ippDeleteAttribute(ipp=%p, attr=%p)\n", ipp
, attr
));
807 * Find the attribute in the list...
810 for (current
= ipp
->attrs
, prev
= NULL
;
811 current
!= NULL
&& current
!= attr
;
812 prev
= current
, current
= current
->next
);
817 * Found it, remove the attribute from the list...
821 prev
->next
= current
->next
;
823 ipp
->attrs
= current
->next
;
825 if (current
== ipp
->last
)
829 * Free memory used by the attribute...
832 _ippFreeAttr(current
);
838 * 'ippFindAttribute()' - Find a named attribute in a request...
841 ipp_attribute_t
* /* O - Matching attribute */
842 ippFindAttribute(ipp_t
*ipp
, /* I - IPP message */
843 const char *name
, /* I - Name of attribute */
844 ipp_tag_t type
) /* I - Type of attribute */
846 DEBUG_printf(("ippFindAttribute(ipp=%p, name=\"%s\", type=%02x(%s))\n", ipp
,
847 name
, type
, ippTagString(type
)));
853 * Reset the current pointer...
859 * Search for the attribute...
862 return (ippFindNextAttribute(ipp
, name
, type
));
867 * 'ippFindNextAttribute()' - Find the next named attribute in a request...
870 ipp_attribute_t
* /* O - Matching attribute */
871 ippFindNextAttribute(ipp_t
*ipp
, /* I - IPP message */
872 const char *name
, /* I - Name of attribute */
873 ipp_tag_t type
) /* I - Type of attribute */
875 ipp_attribute_t
*attr
; /* Current atttribute */
876 ipp_tag_t value_tag
; /* Value tag */
879 DEBUG_printf(("ippFindNextAttribute(ipp=%p, name=\"%s\", type=%02x(%s))\n",
880 ipp
, name
, type
, ippTagString(type
)));
887 ipp
->prev
= ipp
->current
;
888 attr
= ipp
->current
->next
;
896 for (; attr
!= NULL
; ipp
->prev
= attr
, attr
= attr
->next
)
898 DEBUG_printf(("ippFindAttribute: attr=%p, name=\"%s\"\n", attr
,
901 value_tag
= (ipp_tag_t
)(attr
->value_tag
& IPP_TAG_MASK
);
903 if (attr
->name
!= NULL
&& strcasecmp(attr
->name
, name
) == 0 &&
904 (value_tag
== type
|| type
== IPP_TAG_ZERO
||
905 (value_tag
== IPP_TAG_TEXTLANG
&& type
== IPP_TAG_TEXT
) ||
906 (value_tag
== IPP_TAG_NAMELANG
&& type
== IPP_TAG_NAME
)))
922 * 'ippLength()' - Compute the length of an IPP message.
925 size_t /* O - Size of IPP message */
926 ippLength(ipp_t
*ipp
) /* I - IPP message */
928 return (ipp_length(ipp
, 0));
933 * 'ippNew()' - Allocate a new IPP message.
936 ipp_t
* /* O - New IPP message */
939 ipp_t
*temp
; /* New IPP message */
942 DEBUG_puts("ippNew()");
944 if ((temp
= (ipp_t
*)calloc(1, sizeof(ipp_t
))) != NULL
)
947 * Default to IPP 1.1...
950 temp
->request
.any
.version
[0] = 1;
951 temp
->request
.any
.version
[1] = 1;
954 DEBUG_printf(("ippNew: %p\n", temp
));
961 * 'ippNewRequest()' - Allocate a new IPP request message.
963 * The new request message is initialized with the attributes-charset and
964 * attributes-natural-language attributes added. The
965 * attributes-natural-language value is derived from the current locale.
967 * @since CUPS 1.2/Mac OS X 10.5@
970 ipp_t
* /* O - IPP request message */
971 ippNewRequest(ipp_op_t op
) /* I - Operation code */
973 ipp_t
*request
; /* IPP request message */
974 cups_lang_t
*language
; /* Current language localization */
977 DEBUG_printf(("ippNewRequest(op=%02x(%s))\n", op
, ippOpString(op
)));
980 * Create a new IPP message...
983 if ((request
= ippNew()) == NULL
)
987 * Set the operation and request ID...
990 request
->request
.op
.operation_id
= op
;
991 request
->request
.op
.request_id
= 1;
994 * Use UTF-8 as the character set...
997 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_CHARSET
,
998 "attributes-charset", NULL
, "utf-8");
1001 * Get the language from the current locale...
1004 language
= cupsLangDefault();
1006 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_LANGUAGE
,
1007 "attributes-natural-language", NULL
, language
->language
);
1010 * Return the new request...
1018 * 'ippRead()' - Read data for an IPP message from a HTTP connection.
1021 ipp_state_t
/* O - Current state */
1022 ippRead(http_t
*http
, /* I - HTTP connection */
1023 ipp_t
*ipp
) /* I - IPP data */
1025 DEBUG_printf(("ippRead(http=%p, ipp=%p), data_remaining=" CUPS_LLFMT
"\n",
1026 http
, ipp
, CUPS_LLCAST (http
? http
->data_remaining
: -1)));
1031 DEBUG_printf(("ippRead: http->state=%d, http->used=%d\n", http
->state
,
1034 return (ippReadIO(http
, (ipp_iocb_t
)ipp_read_http
, http
->blocking
, NULL
,
1040 * 'ippReadFile()' - Read data for an IPP message from a file.
1042 * @since CUPS 1.1.19/Mac OS X 10.3@
1045 ipp_state_t
/* O - Current state */
1046 ippReadFile(int fd
, /* I - HTTP data */
1047 ipp_t
*ipp
) /* I - IPP data */
1049 DEBUG_printf(("ippReadFile(fd=%d, ipp=%p)\n", fd
, ipp
));
1051 return (ippReadIO(&fd
, (ipp_iocb_t
)ipp_read_file
, 1, NULL
, ipp
));
1056 * 'ippReadIO()' - Read data for an IPP message.
1058 * @since CUPS 1.2/Mac OS X 10.5@
1061 ipp_state_t
/* O - Current state */
1062 ippReadIO(void *src
, /* I - Data source */
1063 ipp_iocb_t cb
, /* I - Read callback function */
1064 int blocking
, /* I - Use blocking IO? */
1065 ipp_t
*parent
, /* I - Parent request, if any */
1066 ipp_t
*ipp
) /* I - IPP data */
1068 int n
; /* Length of data */
1069 unsigned char *buffer
, /* Data buffer */
1070 string
[IPP_MAX_NAME
],
1071 /* Small string buffer */
1072 *bufptr
; /* Pointer into buffer */
1073 ipp_attribute_t
*attr
; /* Current attribute */
1074 ipp_tag_t tag
; /* Current tag */
1075 ipp_tag_t value_tag
; /* Current value tag */
1076 ipp_value_t
*value
; /* Current value */
1079 DEBUG_printf(("ippReadIO(src=%p, cb=%p, blocking=%d, parent=%p, ipp=%p)\n",
1080 src
, cb
, blocking
, parent
, ipp
));
1081 DEBUG_printf(("ippReadIO: ipp->state=%d\n", ipp
->state
));
1086 if ((buffer
= ipp_buffer_get()) == NULL
)
1088 DEBUG_puts("ippReadIO: Unable to get read buffer!");
1095 ipp
->state
++; /* Avoid common problem... */
1101 * Get the request header...
1104 if ((*cb
)(src
, buffer
, 8) < 8)
1106 DEBUG_puts("ippReadIO: Unable to read header!");
1107 ipp_buffer_release(buffer
);
1112 * Then copy the request header over...
1115 ipp
->request
.any
.version
[0] = buffer
[0];
1116 ipp
->request
.any
.version
[1] = buffer
[1];
1117 ipp
->request
.any
.op_status
= (buffer
[2] << 8) | buffer
[3];
1118 ipp
->request
.any
.request_id
= (((((buffer
[4] << 8) | buffer
[5]) << 8) |
1119 buffer
[6]) << 8) | buffer
[7];
1121 DEBUG_printf(("ippReadIO: version=%d.%d\n", buffer
[0], buffer
[1]));
1122 DEBUG_printf(("ippReadIO: op_status=%04x\n",
1123 ipp
->request
.any
.op_status
));
1124 DEBUG_printf(("ippReadIO: request_id=%d\n",
1125 ipp
->request
.any
.request_id
));
1128 ipp
->state
= IPP_ATTRIBUTE
;
1129 ipp
->current
= NULL
;
1130 ipp
->curtag
= IPP_TAG_ZERO
;
1131 ipp
->prev
= ipp
->last
;
1134 * If blocking is disabled, stop here...
1140 case IPP_ATTRIBUTE
:
1143 if ((*cb
)(src
, buffer
, 1) < 1)
1145 DEBUG_puts("ippReadIO: Callback returned EOF/error");
1146 ipp_buffer_release(buffer
);
1150 DEBUG_printf(("ippReadIO: ipp->current=%p, ipp->prev=%p\n",
1151 ipp
->current
, ipp
->prev
));
1154 * Read this attribute...
1157 tag
= (ipp_tag_t
)buffer
[0];
1159 if (tag
== IPP_TAG_END
)
1162 * No more attributes left...
1165 DEBUG_puts("ippReadIO: IPP_TAG_END!");
1167 ipp
->state
= IPP_DATA
;
1170 else if (tag
< IPP_TAG_UNSUPPORTED_VALUE
)
1173 * Group tag... Set the current group and continue...
1176 if (ipp
->curtag
== tag
)
1177 ipp
->prev
= ippAddSeparator(ipp
);
1178 else if (ipp
->current
)
1179 ipp
->prev
= ipp
->current
;
1182 ipp
->current
= NULL
;
1183 DEBUG_printf(("ippReadIO: group tag=%x(%s), ipp->prev=%p\n", tag
,
1184 ippTagString(tag
), ipp
->prev
));
1188 DEBUG_printf(("ippReadIO: value tag=%x(%s)\n", tag
,
1189 ippTagString(tag
)));
1195 if ((*cb
)(src
, buffer
, 2) < 2)
1197 DEBUG_puts("ippReadIO: unable to read name length!");
1198 ipp_buffer_release(buffer
);
1202 n
= (buffer
[0] << 8) | buffer
[1];
1204 if (n
>= IPP_BUF_SIZE
)
1206 DEBUG_printf(("ippReadIO: bad name length %d!\n", n
));
1207 ipp_buffer_release(buffer
);
1211 DEBUG_printf(("ippReadIO: name length=%d\n", n
));
1213 if (n
== 0 && tag
!= IPP_TAG_MEMBERNAME
&&
1214 tag
!= IPP_TAG_END_COLLECTION
)
1217 * More values for current attribute...
1220 if (ipp
->current
== NULL
)
1222 DEBUG_puts("ippReadIO: Attribute without name and no current");
1223 ipp_buffer_release(buffer
);
1227 attr
= ipp
->current
;
1228 value_tag
= (ipp_tag_t
)(attr
->value_tag
& IPP_TAG_MASK
);
1231 * Make sure we aren't adding a new value of a different
1235 if (value_tag
== IPP_TAG_ZERO
)
1238 * Setting the value of a collection member...
1241 attr
->value_tag
= tag
;
1243 else if ((value_tag
>= IPP_TAG_TEXTLANG
&&
1244 value_tag
<= IPP_TAG_MIMETYPE
))
1247 * String values can sometimes come across in different
1248 * forms; accept sets of differing values...
1251 if ((tag
< IPP_TAG_TEXTLANG
|| tag
> IPP_TAG_MIMETYPE
) &&
1252 tag
!= IPP_TAG_NOVALUE
)
1254 DEBUG_printf(("ippReadIO: 1setOf value tag %x(%s) != %x(%s)\n",
1255 value_tag
, ippTagString(value_tag
), tag
,
1256 ippTagString(tag
)));
1257 ipp_buffer_release(buffer
);
1261 else if (value_tag
!= tag
)
1263 DEBUG_printf(("ippReadIO: value tag %x(%s) != %x(%s)\n",
1264 value_tag
, ippTagString(value_tag
), tag
,
1265 ippTagString(tag
)));
1266 ipp_buffer_release(buffer
);
1271 * Finally, reallocate the attribute array as needed...
1274 if (attr
->num_values
== 1 ||
1275 (attr
->num_values
> 0 &&
1276 (attr
->num_values
& (IPP_MAX_VALUES
- 1)) == 0))
1278 ipp_attribute_t
*temp
; /* Pointer to new buffer */
1281 DEBUG_printf(("ippReadIO: reallocating for up to %d values...\n",
1282 attr
->num_values
+ IPP_MAX_VALUES
));
1285 * Reallocate memory...
1288 if ((temp
= realloc(attr
, sizeof(ipp_attribute_t
) +
1289 (attr
->num_values
+ IPP_MAX_VALUES
- 1) *
1290 sizeof(ipp_value_t
))) == NULL
)
1292 DEBUG_puts("ippReadIO: Unable to resize attribute");
1293 ipp_buffer_release(buffer
);
1300 * Reset pointers in the list...
1304 ipp
->prev
->next
= temp
;
1308 attr
= ipp
->current
= ipp
->last
= temp
;
1312 else if (tag
== IPP_TAG_MEMBERNAME
)
1315 * Name must be length 0!
1320 DEBUG_puts("ippReadIO: member name not empty!");
1321 ipp_buffer_release(buffer
);
1326 ipp
->prev
= ipp
->current
;
1328 attr
= ipp
->current
= _ippAddAttr(ipp
, 1);
1330 DEBUG_printf(("ippReadIO: membername, ipp->current=%p, "
1331 "ipp->prev=%p\n", ipp
->current
, ipp
->prev
));
1333 attr
->group_tag
= ipp
->curtag
;
1334 attr
->value_tag
= IPP_TAG_ZERO
;
1335 attr
->num_values
= 0;
1337 else if (tag
!= IPP_TAG_END_COLLECTION
)
1340 * New attribute; read the name and add it...
1343 if ((*cb
)(src
, buffer
, n
) < n
)
1345 DEBUG_puts("ippReadIO: unable to read name!");
1346 ipp_buffer_release(buffer
);
1353 ipp
->prev
= ipp
->current
;
1355 if ((attr
= ipp
->current
= _ippAddAttr(ipp
, 1)) == NULL
)
1357 DEBUG_puts("ippReadIO: unable to allocate attribute!");
1358 ipp_buffer_release(buffer
);
1362 DEBUG_printf(("ippReadIO: name=\"%s\", ipp->current=%p, "
1363 "ipp->prev=%p\n", buffer
, ipp
->current
, ipp
->prev
));
1365 attr
->group_tag
= ipp
->curtag
;
1366 attr
->value_tag
= tag
;
1367 attr
->name
= _cupsStrAlloc((char *)buffer
);
1368 attr
->num_values
= 0;
1373 if (tag
!= IPP_TAG_END_COLLECTION
)
1374 value
= attr
->values
+ attr
->num_values
;
1378 if ((*cb
)(src
, buffer
, 2) < 2)
1380 DEBUG_puts("ippReadIO: unable to read value length!");
1381 ipp_buffer_release(buffer
);
1385 n
= (buffer
[0] << 8) | buffer
[1];
1386 DEBUG_printf(("ippReadIO: value length=%d\n", n
));
1390 case IPP_TAG_INTEGER
:
1394 DEBUG_printf(("ippReadIO: bad value length %d!\n", n
));
1395 ipp_buffer_release(buffer
);
1399 if ((*cb
)(src
, buffer
, 4) < 4)
1401 DEBUG_puts("ippReadIO: Unable to read integer value!");
1402 ipp_buffer_release(buffer
);
1406 n
= (((((buffer
[0] << 8) | buffer
[1]) << 8) | buffer
[2]) << 8) |
1412 case IPP_TAG_BOOLEAN
:
1415 DEBUG_printf(("ippReadIO: bad value length %d!\n", n
));
1416 ipp_buffer_release(buffer
);
1420 if ((*cb
)(src
, buffer
, 1) < 1)
1422 DEBUG_puts("ippReadIO: Unable to read boolean value!");
1423 ipp_buffer_release(buffer
);
1427 value
->boolean
= buffer
[0];
1430 case IPP_TAG_NOVALUE
:
1431 case IPP_TAG_NOTSETTABLE
:
1432 case IPP_TAG_DELETEATTR
:
1433 case IPP_TAG_ADMINDEFINE
:
1434 if (attr
->value_tag
== IPP_TAG_NOVALUE
)
1439 attr
->value_tag
= IPP_TAG_TEXT
;
1444 case IPP_TAG_KEYWORD
:
1446 case IPP_TAG_URISCHEME
:
1447 case IPP_TAG_CHARSET
:
1448 case IPP_TAG_LANGUAGE
:
1449 case IPP_TAG_MIMETYPE
:
1450 if (n
>= IPP_BUF_SIZE
)
1452 DEBUG_printf(("ippReadIO: bad value length %d!\n", n
));
1453 ipp_buffer_release(buffer
);
1457 if ((*cb
)(src
, buffer
, n
) < n
)
1459 DEBUG_puts("ippReadIO: unable to read name!");
1460 ipp_buffer_release(buffer
);
1465 value
->string
.text
= _cupsStrAlloc((char *)buffer
);
1466 DEBUG_printf(("ippReadIO: value=\"%s\"\n",
1467 value
->string
.text
));
1473 DEBUG_printf(("ippReadIO: bad value length %d!\n", n
));
1474 ipp_buffer_release(buffer
);
1478 if ((*cb
)(src
, value
->date
, 11) < 11)
1480 DEBUG_puts("ippReadIO: Unable to date integer value!");
1481 ipp_buffer_release(buffer
);
1486 case IPP_TAG_RESOLUTION
:
1489 DEBUG_printf(("ippReadIO: bad value length %d!\n", n
));
1490 ipp_buffer_release(buffer
);
1494 if ((*cb
)(src
, buffer
, 9) < 9)
1496 DEBUG_puts("ippReadIO: Unable to read resolution value!");
1497 ipp_buffer_release(buffer
);
1501 value
->resolution
.xres
=
1502 (((((buffer
[0] << 8) | buffer
[1]) << 8) | buffer
[2]) << 8) |
1504 value
->resolution
.yres
=
1505 (((((buffer
[4] << 8) | buffer
[5]) << 8) | buffer
[6]) << 8) |
1507 value
->resolution
.units
=
1508 (ipp_res_t
)buffer
[8];
1511 case IPP_TAG_RANGE
:
1514 DEBUG_printf(("ippReadIO: bad value length %d!\n", n
));
1515 ipp_buffer_release(buffer
);
1519 if ((*cb
)(src
, buffer
, 8) < 8)
1521 DEBUG_puts("ippReadIO: Unable to read range value!");
1522 ipp_buffer_release(buffer
);
1526 value
->range
.lower
=
1527 (((((buffer
[0] << 8) | buffer
[1]) << 8) | buffer
[2]) << 8) |
1529 value
->range
.upper
=
1530 (((((buffer
[4] << 8) | buffer
[5]) << 8) | buffer
[6]) << 8) |
1534 case IPP_TAG_TEXTLANG
:
1535 case IPP_TAG_NAMELANG
:
1536 if (n
>= IPP_BUF_SIZE
|| n
< 4)
1538 DEBUG_printf(("ippReadIO: bad value length %d!\n", n
));
1539 ipp_buffer_release(buffer
);
1543 if ((*cb
)(src
, buffer
, n
) < n
)
1545 DEBUG_puts("ippReadIO: Unable to read string w/language value!");
1546 ipp_buffer_release(buffer
);
1553 * text-with-language and name-with-language are composite
1562 n
= (bufptr
[0] << 8) | bufptr
[1];
1564 if ((bufptr
+ 2 + n
) >= (buffer
+ IPP_BUF_SIZE
) ||
1565 n
>= sizeof(string
))
1567 DEBUG_printf(("ippReadIO: bad value length %d!\n", n
));
1568 ipp_buffer_release(buffer
);
1572 memcpy(string
, bufptr
+ 2, n
);
1575 value
->string
.charset
= _cupsStrAlloc((char *)string
);
1578 n
= (bufptr
[0] << 8) | bufptr
[1];
1580 if ((bufptr
+ 2 + n
) >= (buffer
+ IPP_BUF_SIZE
))
1582 DEBUG_printf(("ippReadIO: bad value length %d!\n", n
));
1583 ipp_buffer_release(buffer
);
1587 bufptr
[2 + n
] = '\0';
1588 value
->string
.text
= _cupsStrAlloc((char *)bufptr
+ 2);
1591 case IPP_TAG_BEGIN_COLLECTION
:
1593 * Oh, boy, here comes a collection value, so read it...
1596 value
->collection
= ippNew();
1600 DEBUG_puts("ippReadIO: begCollection tag with value length "
1602 ipp_buffer_release(buffer
);
1606 if (ippReadIO(src
, cb
, 1, ipp
, value
->collection
) == IPP_ERROR
)
1608 DEBUG_puts("ippReadIO: Unable to read collection value!");
1609 ipp_buffer_release(buffer
);
1614 case IPP_TAG_END_COLLECTION
:
1615 ipp_buffer_release(buffer
);
1619 DEBUG_puts("ippReadIO: endCollection tag with value length "
1624 DEBUG_puts("ippReadIO: endCollection tag...");
1625 return (ipp
->state
= IPP_DATA
);
1627 case IPP_TAG_MEMBERNAME
:
1629 * The value the name of the member in the collection, which
1630 * we need to carry over...
1633 if (n
>= IPP_BUF_SIZE
)
1635 DEBUG_printf(("ippReadIO: bad value length %d!\n", n
));
1636 ipp_buffer_release(buffer
);
1640 if ((*cb
)(src
, buffer
, n
) < n
)
1642 DEBUG_puts("ippReadIO: Unable to read member name value!");
1643 ipp_buffer_release(buffer
);
1648 attr
->name
= _cupsStrAlloc((char *)buffer
);
1651 * Since collection members are encoded differently than
1652 * regular attributes, make sure we don't start with an
1656 attr
->num_values
--;
1658 DEBUG_printf(("ippReadIO: member name=\"%s\"\n", attr
->name
));
1661 default : /* Other unsupported values */
1662 if (n
> IPP_MAX_LENGTH
)
1664 DEBUG_printf(("ippReadIO: bad value length %d!\n", n
));
1665 ipp_buffer_release(buffer
);
1671 DEBUG_puts("ippReadIO: NULL value!");
1672 ipp_buffer_release(buffer
);
1676 value
->unknown
.length
= n
;
1679 if ((value
->unknown
.data
= malloc(n
)) == NULL
)
1681 DEBUG_puts("ippReadIO: Unable to allocate value");
1682 ipp_buffer_release(buffer
);
1686 if ((*cb
)(src
, value
->unknown
.data
, n
) < n
)
1688 DEBUG_puts("ippReadIO: Unable to read unsupported value!");
1689 ipp_buffer_release(buffer
);
1694 value
->unknown
.data
= NULL
;
1698 attr
->num_values
++;
1701 * If blocking is disabled, stop here...
1713 break; /* anti-compiler-warning-code */
1716 DEBUG_printf(("ippReadIO: returning ipp->state=%d!\n", ipp
->state
));
1717 ipp_buffer_release(buffer
);
1719 return (ipp
->state
);
1724 * 'ippTimeToDate()' - Convert from UNIX time to RFC 1903 format.
1727 const ipp_uchar_t
* /* O - RFC-1903 date/time data */
1728 ippTimeToDate(time_t t
) /* I - UNIX time value */
1730 struct tm
*unixdate
; /* UNIX unixdate/time info */
1731 ipp_uchar_t
*date
= _cupsGlobals()->ipp_date
;
1732 /* RFC-1903 date/time data */
1736 * RFC-1903 date/time format is:
1738 * Byte(s) Description
1739 * ------- -----------
1740 * 0-1 Year (0 to 65535)
1744 * 5 Minutes (0 to 59)
1745 * 6 Seconds (0 to 60, 60 = "leap second")
1746 * 7 Deciseconds (0 to 9)
1748 * 9 UTC hours (0 to 11)
1749 * 10 UTC minutes (0 to 59)
1752 unixdate
= gmtime(&t
);
1753 unixdate
->tm_year
+= 1900;
1755 date
[0] = unixdate
->tm_year
>> 8;
1756 date
[1] = unixdate
->tm_year
;
1757 date
[2] = unixdate
->tm_mon
+ 1;
1758 date
[3] = unixdate
->tm_mday
;
1759 date
[4] = unixdate
->tm_hour
;
1760 date
[5] = unixdate
->tm_min
;
1761 date
[6] = unixdate
->tm_sec
;
1772 * 'ippWrite()' - Write data for an IPP message to a HTTP connection.
1775 ipp_state_t
/* O - Current state */
1776 ippWrite(http_t
*http
, /* I - HTTP connection */
1777 ipp_t
*ipp
) /* I - IPP data */
1779 DEBUG_printf(("ippWrite(http=%p, ipp=%p)\n", http
, ipp
));
1784 return (ippWriteIO(http
, (ipp_iocb_t
)httpWrite2
,
1785 http
->blocking
, NULL
, ipp
));
1790 * 'ippWriteFile()' - Write data for an IPP message to a file.
1792 * @since CUPS 1.1.19/Mac OS X 10.3@
1795 ipp_state_t
/* O - Current state */
1796 ippWriteFile(int fd
, /* I - HTTP data */
1797 ipp_t
*ipp
) /* I - IPP data */
1799 DEBUG_printf(("ippWriteFile(fd=%d, ipp=%p)\n", fd
, ipp
));
1801 ipp
->state
= IPP_IDLE
;
1803 return (ippWriteIO(&fd
, (ipp_iocb_t
)ipp_write_file
, 1, NULL
, ipp
));
1808 * 'ippWriteIO()' - Write data for an IPP message.
1810 * @since CUPS 1.2/Mac OS X 10.5@
1813 ipp_state_t
/* O - Current state */
1814 ippWriteIO(void *dst
, /* I - Destination */
1815 ipp_iocb_t cb
, /* I - Write callback function */
1816 int blocking
, /* I - Use blocking IO? */
1817 ipp_t
*parent
, /* I - Parent IPP message */
1818 ipp_t
*ipp
) /* I - IPP data */
1820 int i
; /* Looping var */
1821 int n
; /* Length of data */
1822 unsigned char *buffer
, /* Data buffer */
1823 *bufptr
; /* Pointer into buffer */
1824 ipp_attribute_t
*attr
; /* Current attribute */
1825 ipp_value_t
*value
; /* Current value */
1828 DEBUG_printf(("ippWriteIO(dst=%p, cb=%p, blocking=%d, parent=%p, ipp=%p)\n",
1829 dst
, cb
, blocking
, parent
, ipp
));
1834 if ((buffer
= ipp_buffer_get()) == NULL
)
1836 DEBUG_puts("ippWriteIO: Unable to get write buffer");
1843 ipp
->state
++; /* Avoid common problem... */
1849 * Send the request header:
1852 * Operation/Status Code = 2 bytes
1853 * Request ID = 4 bytes
1859 *bufptr
++ = ipp
->request
.any
.version
[0];
1860 *bufptr
++ = ipp
->request
.any
.version
[1];
1861 *bufptr
++ = ipp
->request
.any
.op_status
>> 8;
1862 *bufptr
++ = ipp
->request
.any
.op_status
;
1863 *bufptr
++ = ipp
->request
.any
.request_id
>> 24;
1864 *bufptr
++ = ipp
->request
.any
.request_id
>> 16;
1865 *bufptr
++ = ipp
->request
.any
.request_id
>> 8;
1866 *bufptr
++ = ipp
->request
.any
.request_id
;
1868 if ((*cb
)(dst
, buffer
, (int)(bufptr
- buffer
)) < 0)
1870 DEBUG_puts("ippWriteIO: Could not write IPP header...");
1871 ipp_buffer_release(buffer
);
1877 * Reset the state engine to point to the first attribute
1878 * in the request/response, with no current group.
1881 ipp
->state
= IPP_ATTRIBUTE
;
1882 ipp
->current
= ipp
->attrs
;
1883 ipp
->curtag
= IPP_TAG_ZERO
;
1885 DEBUG_printf(("ippWriteIO: version=%d.%d\n", buffer
[0], buffer
[1]));
1886 DEBUG_printf(("ippWriteIO: op_status=%04x\n",
1887 ipp
->request
.any
.op_status
));
1888 DEBUG_printf(("ippWriteIO: request_id=%d\n",
1889 ipp
->request
.any
.request_id
));
1892 * If blocking is disabled, stop here...
1898 case IPP_ATTRIBUTE
:
1899 while (ipp
->current
!= NULL
)
1902 * Write this attribute...
1906 attr
= ipp
->current
;
1908 ipp
->current
= ipp
->current
->next
;
1910 if (ipp
->curtag
!= attr
->group_tag
&& parent
== NULL
)
1913 * Send a group tag byte...
1916 ipp
->curtag
= attr
->group_tag
;
1918 if (attr
->group_tag
== IPP_TAG_ZERO
)
1921 DEBUG_printf(("ippWriteIO: wrote group tag=%x(%s)\n",
1922 attr
->group_tag
, ippTagString(attr
->group_tag
)));
1923 *bufptr
++ = attr
->group_tag
;
1925 else if (attr
->group_tag
== IPP_TAG_ZERO
)
1929 * Write the attribute tag and name. The current implementation
1930 * does not support the extension value tags above 0x7f, so all
1931 * value tags are 1 byte.
1933 * The attribute name length does not include the trailing nul
1934 * character in the source string.
1936 * Collection values (parent != NULL) are written differently...
1942 * Get the length of the attribute name, and make sure it won't
1943 * overflow the buffer...
1946 if ((n
= (int)strlen(attr
->name
)) > (IPP_BUF_SIZE
- 4))
1948 DEBUG_printf(("ippWriteIO: Attribute name too long (%d)\n", n
));
1949 ipp_buffer_release(buffer
);
1954 * Write the value tag, name length, and name string...
1957 DEBUG_printf(("ippWriteIO: writing value tag=%x(%s)\n",
1958 attr
->value_tag
, ippTagString(attr
->value_tag
)));
1959 DEBUG_printf(("ippWriteIO: writing name=%d,\"%s\"\n", n
,
1962 *bufptr
++ = attr
->value_tag
;
1965 memcpy(bufptr
, attr
->name
, n
);
1971 * Get the length of the attribute name, and make sure it won't
1972 * overflow the buffer...
1975 if ((n
= (int)strlen(attr
->name
)) > (IPP_BUF_SIZE
- 7))
1977 DEBUG_printf(("ippWriteIO: Attribute name too long (%d)\n", n
));
1978 ipp_buffer_release(buffer
);
1983 * Write the member name tag, name length, name string, value tag,
1984 * and empty name for the collection member attribute...
1987 DEBUG_printf(("ippWriteIO: writing value tag=%x(memberName)\n",
1988 IPP_TAG_MEMBERNAME
));
1989 DEBUG_printf(("ippWriteIO: writing name=%d,\"%s\"\n", n
,
1991 DEBUG_printf(("ippWriteIO: writing value tag=%x(%s)\n",
1992 attr
->value_tag
, ippTagString(attr
->value_tag
)));
1993 DEBUG_puts("ippWriteIO: writing name=0,\"\"\n");
1995 *bufptr
++ = IPP_TAG_MEMBERNAME
;
2000 memcpy(bufptr
, attr
->name
, n
);
2003 *bufptr
++ = attr
->value_tag
;
2009 * Now write the attribute value(s)...
2012 switch (attr
->value_tag
& ~IPP_TAG_COPY
)
2014 case IPP_TAG_INTEGER
:
2016 for (i
= 0, value
= attr
->values
;
2017 i
< attr
->num_values
;
2020 if ((IPP_BUF_SIZE
- (bufptr
- buffer
)) < 9)
2022 if ((*cb
)(dst
, buffer
, (int)(bufptr
- buffer
)) < 0)
2024 DEBUG_puts("ippWriteIO: Could not write IPP "
2026 ipp_buffer_release(buffer
);
2036 * Arrays and sets are done by sending additional
2037 * values with a zero-length name...
2040 *bufptr
++ = attr
->value_tag
;
2046 * Integers and enumerations are both 4-byte signed
2047 * (twos-complement) values.
2049 * Put the 2-byte length and 4-byte value into the buffer...
2054 *bufptr
++ = value
->integer
>> 24;
2055 *bufptr
++ = value
->integer
>> 16;
2056 *bufptr
++ = value
->integer
>> 8;
2057 *bufptr
++ = value
->integer
;
2061 case IPP_TAG_BOOLEAN
:
2062 for (i
= 0, value
= attr
->values
;
2063 i
< attr
->num_values
;
2066 if ((IPP_BUF_SIZE
- (bufptr
- buffer
)) < 6)
2068 if ((*cb
)(dst
, buffer
, (int)(bufptr
- buffer
)) < 0)
2070 DEBUG_puts("ippWriteIO: Could not write IPP "
2072 ipp_buffer_release(buffer
);
2082 * Arrays and sets are done by sending additional
2083 * values with a zero-length name...
2086 *bufptr
++ = attr
->value_tag
;
2092 * Boolean values are 1-byte; 0 = false, 1 = true.
2094 * Put the 2-byte length and 1-byte value into the buffer...
2099 *bufptr
++ = value
->boolean
;
2105 case IPP_TAG_KEYWORD
:
2107 case IPP_TAG_URISCHEME
:
2108 case IPP_TAG_CHARSET
:
2109 case IPP_TAG_LANGUAGE
:
2110 case IPP_TAG_MIMETYPE
:
2111 for (i
= 0, value
= attr
->values
;
2112 i
< attr
->num_values
;
2118 * Arrays and sets are done by sending additional
2119 * values with a zero-length name...
2122 DEBUG_printf(("ippWriteIO: writing value tag=%x(%s)\n",
2124 ippTagString(attr
->value_tag
)));
2125 DEBUG_printf(("ippWriteIO: writing name=0,\"\"\n"));
2127 if ((IPP_BUF_SIZE
- (bufptr
- buffer
)) < 3)
2129 if ((*cb
)(dst
, buffer
, (int)(bufptr
- buffer
)) < 0)
2131 DEBUG_puts("ippWriteIO: Could not write IPP "
2133 ipp_buffer_release(buffer
);
2140 *bufptr
++ = attr
->value_tag
;
2145 if (value
->string
.text
!= NULL
)
2146 n
= (int)strlen(value
->string
.text
);
2150 if (n
> (IPP_BUF_SIZE
- 2))
2152 DEBUG_printf(("ippWriteIO: String too long (%d)\n", n
));
2153 ipp_buffer_release(buffer
);
2157 DEBUG_printf(("ippWriteIO: writing string=%d,\"%s\"\n", n
,
2158 value
->string
.text
));
2160 if ((int)(IPP_BUF_SIZE
- (bufptr
- buffer
)) < (n
+ 2))
2162 if ((*cb
)(dst
, buffer
, (int)(bufptr
- buffer
)) < 0)
2164 DEBUG_puts("ippWriteIO: Could not write IPP "
2166 ipp_buffer_release(buffer
);
2174 * All simple strings consist of the 2-byte length and
2175 * character data without the trailing nul normally found
2176 * in C strings. Also, strings cannot be longer than IPP_MAX_LENGTH
2177 * bytes since the 2-byte length is a signed (twos-complement)
2180 * Put the 2-byte length and string characters in the buffer.
2188 memcpy(bufptr
, value
->string
.text
, n
);
2195 for (i
= 0, value
= attr
->values
;
2196 i
< attr
->num_values
;
2199 if ((IPP_BUF_SIZE
- (bufptr
- buffer
)) < 16)
2201 if ((*cb
)(dst
, buffer
, (int)(bufptr
- buffer
)) < 0)
2203 DEBUG_puts("ippWriteIO: Could not write IPP "
2205 ipp_buffer_release(buffer
);
2215 * Arrays and sets are done by sending additional
2216 * values with a zero-length name...
2219 *bufptr
++ = attr
->value_tag
;
2225 * Date values consist of a 2-byte length and an
2226 * 11-byte date/time structure defined by RFC 1903.
2228 * Put the 2-byte length and 11-byte date/time
2229 * structure in the buffer.
2234 memcpy(bufptr
, value
->date
, 11);
2239 case IPP_TAG_RESOLUTION
:
2240 for (i
= 0, value
= attr
->values
;
2241 i
< attr
->num_values
;
2244 if ((IPP_BUF_SIZE
- (bufptr
- buffer
)) < 14)
2246 if ((*cb
)(dst
, buffer
, (int)(bufptr
- buffer
)) < 0)
2248 DEBUG_puts("ippWriteIO: Could not write IPP "
2250 ipp_buffer_release(buffer
);
2260 * Arrays and sets are done by sending additional
2261 * values with a zero-length name...
2264 *bufptr
++ = attr
->value_tag
;
2270 * Resolution values consist of a 2-byte length,
2271 * 4-byte horizontal resolution value, 4-byte vertical
2272 * resolution value, and a 1-byte units value.
2274 * Put the 2-byte length and resolution value data
2280 *bufptr
++ = value
->resolution
.xres
>> 24;
2281 *bufptr
++ = value
->resolution
.xres
>> 16;
2282 *bufptr
++ = value
->resolution
.xres
>> 8;
2283 *bufptr
++ = value
->resolution
.xres
;
2284 *bufptr
++ = value
->resolution
.yres
>> 24;
2285 *bufptr
++ = value
->resolution
.yres
>> 16;
2286 *bufptr
++ = value
->resolution
.yres
>> 8;
2287 *bufptr
++ = value
->resolution
.yres
;
2288 *bufptr
++ = value
->resolution
.units
;
2292 case IPP_TAG_RANGE
:
2293 for (i
= 0, value
= attr
->values
;
2294 i
< attr
->num_values
;
2297 if ((IPP_BUF_SIZE
- (bufptr
- buffer
)) < 13)
2299 if ((*cb
)(dst
, buffer
, (int)(bufptr
- buffer
)) < 0)
2301 DEBUG_puts("ippWriteIO: Could not write IPP "
2303 ipp_buffer_release(buffer
);
2313 * Arrays and sets are done by sending additional
2314 * values with a zero-length name...
2317 *bufptr
++ = attr
->value_tag
;
2323 * Range values consist of a 2-byte length,
2324 * 4-byte lower value, and 4-byte upper value.
2326 * Put the 2-byte length and range value data
2332 *bufptr
++ = value
->range
.lower
>> 24;
2333 *bufptr
++ = value
->range
.lower
>> 16;
2334 *bufptr
++ = value
->range
.lower
>> 8;
2335 *bufptr
++ = value
->range
.lower
;
2336 *bufptr
++ = value
->range
.upper
>> 24;
2337 *bufptr
++ = value
->range
.upper
>> 16;
2338 *bufptr
++ = value
->range
.upper
>> 8;
2339 *bufptr
++ = value
->range
.upper
;
2343 case IPP_TAG_TEXTLANG
:
2344 case IPP_TAG_NAMELANG
:
2345 for (i
= 0, value
= attr
->values
;
2346 i
< attr
->num_values
;
2352 * Arrays and sets are done by sending additional
2353 * values with a zero-length name...
2356 if ((IPP_BUF_SIZE
- (bufptr
- buffer
)) < 3)
2358 if ((*cb
)(dst
, buffer
, (int)(bufptr
- buffer
)) < 0)
2360 DEBUG_puts("ippWriteIO: Could not write IPP "
2362 ipp_buffer_release(buffer
);
2369 *bufptr
++ = attr
->value_tag
;
2375 * textWithLanguage and nameWithLanguage values consist
2376 * of a 2-byte length for both strings and their
2377 * individual lengths, a 2-byte length for the
2378 * character string, the character string without the
2379 * trailing nul, a 2-byte length for the character
2380 * set string, and the character set string without
2386 if (value
->string
.charset
!= NULL
)
2387 n
+= (int)strlen(value
->string
.charset
);
2389 if (value
->string
.text
!= NULL
)
2390 n
+= (int)strlen(value
->string
.text
);
2392 if (n
> (IPP_BUF_SIZE
- 2))
2394 DEBUG_printf(("ippWriteIO: text/nameWithLanguage value "
2395 "too long (%d)\n", n
));
2396 ipp_buffer_release(buffer
);
2400 if ((int)(IPP_BUF_SIZE
- (bufptr
- buffer
)) < (n
+ 2))
2402 if ((*cb
)(dst
, buffer
, (int)(bufptr
- buffer
)) < 0)
2404 DEBUG_puts("ippWriteIO: Could not write IPP "
2406 ipp_buffer_release(buffer
);
2413 /* Length of entire value */
2417 /* Length of charset */
2418 if (value
->string
.charset
!= NULL
)
2419 n
= (int)strlen(value
->string
.charset
);
2429 memcpy(bufptr
, value
->string
.charset
, n
);
2433 /* Length of text */
2434 if (value
->string
.text
!= NULL
)
2435 n
= (int)strlen(value
->string
.text
);
2445 memcpy(bufptr
, value
->string
.text
, n
);
2451 case IPP_TAG_BEGIN_COLLECTION
:
2452 for (i
= 0, value
= attr
->values
;
2453 i
< attr
->num_values
;
2457 * Collections are written with the begin-collection
2458 * tag first with a value of 0 length, followed by the
2459 * attributes in the collection, then the end-collection
2463 if ((IPP_BUF_SIZE
- (bufptr
- buffer
)) < 5)
2465 if ((*cb
)(dst
, buffer
, (int)(bufptr
- buffer
)) < 0)
2467 DEBUG_puts("ippWriteIO: Could not write IPP "
2469 ipp_buffer_release(buffer
);
2479 * Arrays and sets are done by sending additional
2480 * values with a zero-length name...
2483 *bufptr
++ = attr
->value_tag
;
2489 * Write a data length of 0 and flush the buffer...
2495 if ((*cb
)(dst
, buffer
, (int)(bufptr
- buffer
)) < 0)
2497 DEBUG_puts("ippWriteIO: Could not write IPP "
2499 ipp_buffer_release(buffer
);
2506 * Then write the collection attribute...
2509 value
->collection
->state
= IPP_IDLE
;
2511 if (ippWriteIO(dst
, cb
, 1, ipp
,
2512 value
->collection
) == IPP_ERROR
)
2514 DEBUG_puts("ippWriteIO: Unable to write collection value");
2515 ipp_buffer_release(buffer
);
2522 for (i
= 0, value
= attr
->values
;
2523 i
< attr
->num_values
;
2529 * Arrays and sets are done by sending additional
2530 * values with a zero-length name...
2533 if ((IPP_BUF_SIZE
- (bufptr
- buffer
)) < 3)
2535 if ((*cb
)(dst
, buffer
, (int)(bufptr
- buffer
)) < 0)
2537 DEBUG_puts("ippWriteIO: Could not write IPP "
2539 ipp_buffer_release(buffer
);
2546 *bufptr
++ = attr
->value_tag
;
2552 * An unknown value might some new value that a
2553 * vendor has come up with. It consists of a
2554 * 2-byte length and the bytes in the unknown
2558 n
= value
->unknown
.length
;
2560 if (n
> (IPP_BUF_SIZE
- 2))
2562 DEBUG_printf(("ippWriteIO: Data length too long (%d)\n",
2564 ipp_buffer_release(buffer
);
2568 if ((int)(IPP_BUF_SIZE
- (bufptr
- buffer
)) < (n
+ 2))
2570 if ((*cb
)(dst
, buffer
, (int)(bufptr
- buffer
)) < 0)
2572 DEBUG_puts("ippWriteIO: Could not write IPP "
2574 ipp_buffer_release(buffer
);
2581 /* Length of unknown value */
2588 memcpy(bufptr
, value
->unknown
.data
, n
);
2596 * Write the data out...
2599 if ((*cb
)(dst
, buffer
, (int)(bufptr
- buffer
)) < 0)
2601 DEBUG_puts("ippWriteIO: Could not write IPP attribute...");
2602 ipp_buffer_release(buffer
);
2606 DEBUG_printf(("ippWriteIO: wrote %d bytes\n",
2607 (int)(bufptr
- buffer
)));
2610 * If blocking is disabled, stop here...
2617 if (ipp
->current
== NULL
)
2620 * Done with all of the attributes; add the end-of-attributes
2621 * tag or end-collection attribute...
2626 buffer
[0] = IPP_TAG_END
;
2631 buffer
[0] = IPP_TAG_END_COLLECTION
;
2632 buffer
[1] = 0; /* empty name */
2634 buffer
[3] = 0; /* empty value */
2639 if ((*cb
)(dst
, buffer
, n
) < 0)
2641 DEBUG_puts("ippWriteIO: Could not write IPP end-tag...");
2642 ipp_buffer_release(buffer
);
2646 ipp
->state
= IPP_DATA
;
2654 break; /* anti-compiler-warning-code */
2657 ipp_buffer_release(buffer
);
2659 return (ipp
->state
);
2664 * '_ippAddAttr()' - Add a new attribute to the request.
2667 ipp_attribute_t
* /* O - New attribute */
2668 _ippAddAttr(ipp_t
*ipp
, /* I - IPP message */
2669 int num_values
) /* I - Number of values */
2671 ipp_attribute_t
*attr
; /* New attribute */
2674 DEBUG_printf(("_ippAddAttr(ipp=%p, num_values=%d)\n", ipp
, num_values
));
2676 if (!ipp
|| num_values
< 0)
2679 attr
= calloc(sizeof(ipp_attribute_t
) +
2680 (num_values
- 1) * sizeof(ipp_value_t
), 1);
2684 attr
->num_values
= num_values
;
2686 if (ipp
->last
== NULL
)
2689 ipp
->last
->next
= attr
;
2694 DEBUG_printf(("_ippAddAttr(): %p\n", attr
));
2701 * '_ippFreeAttr()' - Free an attribute.
2705 _ippFreeAttr(ipp_attribute_t
*attr
) /* I - Attribute to free */
2707 int i
; /* Looping var */
2708 ipp_value_t
*value
; /* Current value */
2711 DEBUG_printf(("_ippFreeAttr(attr=%p)\n", attr
));
2713 switch (attr
->value_tag
)
2717 case IPP_TAG_KEYWORD
:
2719 case IPP_TAG_URISCHEME
:
2720 case IPP_TAG_CHARSET
:
2721 case IPP_TAG_LANGUAGE
:
2722 case IPP_TAG_MIMETYPE
:
2723 for (i
= 0, value
= attr
->values
;
2724 i
< attr
->num_values
;
2726 _cupsStrFree(value
->string
.text
);
2729 case IPP_TAG_TEXTLANG
:
2730 case IPP_TAG_NAMELANG
:
2731 for (i
= 0, value
= attr
->values
;
2732 i
< attr
->num_values
;
2735 if (value
->string
.charset
&& i
== 0)
2736 _cupsStrFree(value
->string
.charset
);
2737 _cupsStrFree(value
->string
.text
);
2741 case IPP_TAG_INTEGER
:
2743 case IPP_TAG_BOOLEAN
:
2745 case IPP_TAG_RESOLUTION
:
2746 case IPP_TAG_RANGE
:
2749 case IPP_TAG_BEGIN_COLLECTION
:
2750 for (i
= 0, value
= attr
->values
;
2751 i
< attr
->num_values
;
2753 ippDelete(value
->collection
);
2756 case IPP_TAG_STRING
:
2757 for (i
= 0, value
= attr
->values
;
2758 i
< attr
->num_values
;
2760 free(value
->unknown
.data
);
2764 if (!((int)attr
->value_tag
& IPP_TAG_COPY
))
2766 for (i
= 0, value
= attr
->values
;
2767 i
< attr
->num_values
;
2769 if (value
->unknown
.data
)
2770 free(value
->unknown
.data
);
2776 _cupsStrFree(attr
->name
);
2783 * 'ipp_buffer_get()' - Get a read/write buffer.
2786 static unsigned char * /* O - Buffer */
2787 ipp_buffer_get(void)
2789 _ipp_buffer_t
*buffer
; /* Current buffer */
2790 _cups_globals_t
*cg
= _cupsGlobals();
2794 for (buffer
= cg
->ipp_buffers
; buffer
; buffer
= buffer
->next
)
2801 if ((buffer
= malloc(sizeof(_ipp_buffer_t
))) == NULL
)
2805 buffer
->next
= cg
->ipp_buffers
;
2806 cg
->ipp_buffers
= buffer
;
2813 * 'ipp_buffer_release()' - Release a read/write buffer.
2817 ipp_buffer_release(unsigned char *b
) /* I - Buffer to release */
2819 ((_ipp_buffer_t
*)b
)->used
= 0;
2824 * 'ipp_length()' - Compute the length of an IPP message or collection value.
2827 static size_t /* O - Size of IPP message */
2828 ipp_length(ipp_t
*ipp
, /* I - IPP message or collection */
2829 int collection
) /* I - 1 if a collection, 0 otherwise */
2831 int i
; /* Looping var */
2832 int bytes
; /* Number of bytes */
2833 ipp_attribute_t
*attr
; /* Current attribute */
2834 ipp_tag_t group
; /* Current group */
2835 ipp_value_t
*value
; /* Current value */
2842 * Start with 8 bytes for the IPP message header...
2845 bytes
= collection
? 0 : 8;
2848 * Then add the lengths of each attribute...
2851 group
= IPP_TAG_ZERO
;
2853 for (attr
= ipp
->attrs
; attr
!= NULL
; attr
= attr
->next
)
2855 if (attr
->group_tag
!= group
&& !collection
)
2857 group
= attr
->group_tag
;
2858 if (group
== IPP_TAG_ZERO
)
2861 bytes
++; /* Group tag */
2867 DEBUG_printf(("ipp_length: attr->name=\"%s\", attr->num_values=%d, "
2868 "bytes=%d\n", attr
->name
, attr
->num_values
, bytes
));
2870 bytes
+= (int)strlen(attr
->name
); /* Name */
2871 bytes
+= attr
->num_values
; /* Value tag for each value */
2872 bytes
+= 2 * attr
->num_values
; /* Name lengths */
2873 bytes
+= 2 * attr
->num_values
; /* Value lengths */
2876 bytes
+= 5; /* Add membername overhead */
2878 switch (attr
->value_tag
& ~IPP_TAG_COPY
)
2880 case IPP_TAG_INTEGER
:
2882 bytes
+= 4 * attr
->num_values
;
2885 case IPP_TAG_BOOLEAN
:
2886 bytes
+= attr
->num_values
;
2891 case IPP_TAG_KEYWORD
:
2893 case IPP_TAG_URISCHEME
:
2894 case IPP_TAG_CHARSET
:
2895 case IPP_TAG_LANGUAGE
:
2896 case IPP_TAG_MIMETYPE
:
2897 for (i
= 0, value
= attr
->values
;
2898 i
< attr
->num_values
;
2900 if (value
->string
.text
!= NULL
)
2901 bytes
+= (int)strlen(value
->string
.text
);
2905 bytes
+= 11 * attr
->num_values
;
2908 case IPP_TAG_RESOLUTION
:
2909 bytes
+= 9 * attr
->num_values
;
2912 case IPP_TAG_RANGE
:
2913 bytes
+= 8 * attr
->num_values
;
2916 case IPP_TAG_TEXTLANG
:
2917 case IPP_TAG_NAMELANG
:
2918 bytes
+= 4 * attr
->num_values
;/* Charset + text length */
2920 for (i
= 0, value
= attr
->values
;
2921 i
< attr
->num_values
;
2924 if (value
->string
.charset
!= NULL
)
2925 bytes
+= (int)strlen(value
->string
.charset
);
2927 if (value
->string
.text
!= NULL
)
2928 bytes
+= (int)strlen(value
->string
.text
);
2932 case IPP_TAG_BEGIN_COLLECTION
:
2933 for (i
= 0, value
= attr
->values
;
2934 i
< attr
->num_values
;
2936 bytes
+= (int)ipp_length(value
->collection
, 1);
2940 for (i
= 0, value
= attr
->values
;
2941 i
< attr
->num_values
;
2943 bytes
+= value
->unknown
.length
;
2949 * Finally, add 1 byte for the "end of attributes" tag or 5 bytes
2950 * for the "end of collection" tag and return...
2958 DEBUG_printf(("ipp_length: bytes=%d\n", bytes
));
2965 * 'ipp_read_http()' - Semi-blocking read on a HTTP connection...
2968 static ssize_t
/* O - Number of bytes read */
2969 ipp_read_http(http_t
*http
, /* I - Client connection */
2970 ipp_uchar_t
*buffer
, /* O - Buffer for data */
2971 size_t length
) /* I - Total length */
2973 int tbytes
, /* Total bytes read */
2974 bytes
; /* Bytes read this pass */
2975 char len
[32]; /* Length string */
2978 DEBUG_printf(("ipp_read_http(http=%p, buffer=%p, length=%d)\n",
2979 http
, buffer
, (int)length
));
2982 * Loop until all bytes are read...
2985 for (tbytes
= 0, bytes
= 0;
2986 tbytes
< (int)length
;
2987 tbytes
+= bytes
, buffer
+= bytes
)
2989 DEBUG_printf(("ipp_read_http: tbytes=%d, http->state=%d\n", tbytes
,
2992 if (http
->state
== HTTP_WAITING
)
2995 if (http
->used
> 0 && http
->data_encoding
== HTTP_ENCODE_LENGTH
)
2998 * Do "fast read" from HTTP buffer directly...
3001 if (http
->used
> (int)(length
- tbytes
))
3002 bytes
= (int)(length
- tbytes
);
3007 buffer
[0] = http
->buffer
[0];
3009 memcpy(buffer
, http
->buffer
, bytes
);
3011 http
->used
-= bytes
;
3012 http
->data_remaining
-= bytes
;
3014 if (http
->data_remaining
<= INT_MAX
)
3015 http
->_data_remaining
= (int)http
->data_remaining
;
3017 http
->_data_remaining
= INT_MAX
;
3020 memmove(http
->buffer
, http
->buffer
+ bytes
, http
->used
);
3022 if (http
->data_remaining
== 0)
3024 if (http
->data_encoding
== HTTP_ENCODE_CHUNKED
)
3027 * Get the trailing CR LF after the chunk...
3030 if (!httpGets(len
, sizeof(len
), http
))
3034 if (http
->data_encoding
!= HTTP_ENCODE_CHUNKED
)
3036 if (http
->state
== HTTP_POST_RECV
)
3039 http
->state
= HTTP_WAITING
;
3046 * Wait a maximum of 1 second for data...
3049 if (!http
->blocking
)
3052 * Wait up to 10 seconds for more data on non-blocking sockets...
3055 if (!httpWait(http
, 10000))
3066 if ((bytes
= httpRead2(http
, (char *)buffer
, length
- tbytes
)) < 0)
3071 if (errno
!= EAGAIN
&& errno
!= EINTR
)
3077 else if (bytes
== 0)
3083 * Return the number of bytes read...
3086 if (tbytes
== 0 && bytes
< 0)
3089 DEBUG_printf(("ipp_read_http: returning %d bytes...\n", tbytes
));
3096 * 'ipp_read_file()' - Read IPP data from a file.
3099 static ssize_t
/* O - Number of bytes read */
3100 ipp_read_file(int *fd
, /* I - File descriptor */
3101 ipp_uchar_t
*buffer
, /* O - Read buffer */
3102 size_t length
) /* I - Number of bytes to read */
3105 return ((ssize_t
)read(*fd
, buffer
, (unsigned)length
));
3107 return (read(*fd
, buffer
, length
));
3113 * 'ipp_write_file()' - Write IPP data to a file.
3116 static ssize_t
/* O - Number of bytes written */
3117 ipp_write_file(int *fd
, /* I - File descriptor */
3118 ipp_uchar_t
*buffer
, /* I - Data to write */
3119 size_t length
) /* I - Number of bytes to write */
3122 return ((ssize_t
)write(*fd
, buffer
, (unsigned)length
));
3124 return (write(*fd
, buffer
, length
));
3131 * The following symbol definitions are provided only for KDE
3132 * compatibility during the CUPS 1.2 testing period and will be
3133 * removed in a future release of CUPS. These are PRIVATE APIs
3134 * from CUPS 1.1.x that the KDE developers chose to use...
3137 ipp_attribute_t
* /* O - New attribute */
3138 _ipp_add_attr(ipp_t
*ipp
, /* I - IPP message */
3139 int num_values
) /* I - Number of values */
3141 return (_ippAddAttr(ipp
, num_values
));
3145 _ipp_free_attr(ipp_attribute_t
*attr
) /* I - Attribute to free */
3149 #endif /* __linux */
3153 * End of "$Id: ipp.c 7847 2008-08-19 04:22:14Z mike $".