2 * "$Id: ipp.c 6649 2007-07-11 21:46:42Z mike $"
4 * Internet Printing Protocol support functions for the Common UNIX
5 * Printing System (CUPS).
7 * Copyright 2007-2008 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 size_t ipp_length(ipp_t
*ipp
, int collection
);
79 static ssize_t
ipp_read_http(http_t
*http
, ipp_uchar_t
*buffer
,
81 static ssize_t
ipp_read_file(int *fd
, ipp_uchar_t
*buffer
,
83 static ssize_t
ipp_write_file(int *fd
, ipp_uchar_t
*buffer
,
88 * 'ippAddBoolean()' - Add a boolean attribute to an IPP message.
91 ipp_attribute_t
* /* O - New attribute */
92 ippAddBoolean(ipp_t
*ipp
, /* I - IPP message */
93 ipp_tag_t group
, /* I - IPP group */
94 const char *name
, /* I - Name of attribute */
95 char value
) /* I - Value of attribute */
97 ipp_attribute_t
*attr
; /* New attribute */
100 DEBUG_printf(("ippAddBoolean(%p, %02x, \'%s\', %d)\n", ipp
, group
, name
, value
));
102 if (ipp
== NULL
|| name
== NULL
)
105 if ((attr
= _ippAddAttr(ipp
, 1)) == NULL
)
108 attr
->name
= _cupsStrAlloc(name
);
109 attr
->group_tag
= group
;
110 attr
->value_tag
= IPP_TAG_BOOLEAN
;
111 attr
->values
[0].boolean
= value
;
118 * 'ippAddBooleans()' - Add an array of boolean values.
121 ipp_attribute_t
* /* O - New attribute */
122 ippAddBooleans(ipp_t
*ipp
, /* I - IPP message */
123 ipp_tag_t group
, /* I - IPP group */
124 const char *name
, /* I - Name of attribute */
125 int num_values
, /* I - Number of values */
126 const char *values
) /* I - Values */
128 int i
; /* Looping var */
129 ipp_attribute_t
*attr
; /* New attribute */
130 ipp_value_t
*value
; /* Current value */
133 DEBUG_printf(("ippAddBooleans(%p, %02x, \'%s\', %d, %p)\n", ipp
,
134 group
, name
, num_values
, values
));
136 if (ipp
== NULL
|| name
== NULL
|| num_values
< 1)
139 if ((attr
= _ippAddAttr(ipp
, num_values
)) == NULL
)
142 attr
->name
= _cupsStrAlloc(name
);
143 attr
->group_tag
= group
;
144 attr
->value_tag
= IPP_TAG_BOOLEAN
;
147 for (i
= 0, value
= attr
->values
;
150 value
->boolean
= values
[i
];
157 * 'ippAddCollection()' - Add a collection value.
159 * @since CUPS 1.1.19@
162 ipp_attribute_t
* /* O - New attribute */
163 ippAddCollection(ipp_t
*ipp
, /* I - IPP message */
164 ipp_tag_t group
, /* I - IPP group */
165 const char *name
, /* I - Name of attribute */
166 ipp_t
*value
) /* I - Value */
168 ipp_attribute_t
*attr
; /* New attribute */
171 DEBUG_printf(("ippAddCollection(%p, %02x, \'%s\', %p)\n", ipp
, group
, name
,
174 if (ipp
== NULL
|| name
== NULL
)
177 if ((attr
= _ippAddAttr(ipp
, 1)) == NULL
)
180 attr
->name
= _cupsStrAlloc(name
);
181 attr
->group_tag
= group
;
182 attr
->value_tag
= IPP_TAG_BEGIN_COLLECTION
;
183 attr
->values
[0].collection
= value
;
190 * 'ippAddCollections()' - Add an array of collection values.
192 * @since CUPS 1.1.19@
195 ipp_attribute_t
* /* O - New attribute */
197 ipp_t
*ipp
, /* I - IPP message */
198 ipp_tag_t group
, /* I - IPP group */
199 const char *name
, /* I - Name of attribute */
200 int num_values
, /* I - Number of values */
201 const ipp_t
**values
) /* I - Values */
203 int i
; /* Looping var */
204 ipp_attribute_t
*attr
; /* New attribute */
205 ipp_value_t
*value
; /* Current value */
208 DEBUG_printf(("ippAddCollections(%p, %02x, \'%s\', %d, %p)\n", ipp
,
209 group
, name
, num_values
, values
));
211 if (ipp
== NULL
|| name
== NULL
|| num_values
< 1)
214 if ((attr
= _ippAddAttr(ipp
, num_values
)) == NULL
)
217 attr
->name
= _cupsStrAlloc(name
);
218 attr
->group_tag
= group
;
219 attr
->value_tag
= IPP_TAG_BEGIN_COLLECTION
;
222 for (i
= 0, value
= attr
->values
;
225 value
->collection
= (ipp_t
*)values
[i
];
232 * 'ippAddDate()' - Add a date attribute to an IPP message.
235 ipp_attribute_t
* /* O - New attribute */
236 ippAddDate(ipp_t
*ipp
, /* I - IPP message */
237 ipp_tag_t group
, /* I - IPP group */
238 const char *name
, /* I - Name of attribute */
239 const ipp_uchar_t
*value
) /* I - Value */
241 ipp_attribute_t
*attr
; /* New attribute */
244 DEBUG_printf(("ippAddDate(%p, %02x, \'%s\', %p)\n", ipp
, group
, name
,
247 if (ipp
== NULL
|| name
== NULL
|| value
== NULL
)
250 if ((attr
= _ippAddAttr(ipp
, 1)) == NULL
)
253 attr
->name
= _cupsStrAlloc(name
);
254 attr
->group_tag
= group
;
255 attr
->value_tag
= IPP_TAG_DATE
;
256 memcpy(attr
->values
[0].date
, value
, 11);
263 * 'ippAddInteger()' - Add a integer attribute to an IPP message.
266 ipp_attribute_t
* /* O - New attribute */
267 ippAddInteger(ipp_t
*ipp
, /* I - IPP message */
268 ipp_tag_t group
, /* I - IPP group */
269 ipp_tag_t type
, /* I - Type of attribute */
270 const char *name
, /* I - Name of attribute */
271 int value
) /* I - Value of attribute */
273 ipp_attribute_t
*attr
; /* New attribute */
276 DEBUG_printf(("ippAddInteger(%p, %d, \'%s\', %d)\n", ipp
, group
, name
,
279 if (ipp
== NULL
|| name
== NULL
)
282 if ((attr
= _ippAddAttr(ipp
, 1)) == NULL
)
285 attr
->name
= _cupsStrAlloc(name
);
286 attr
->group_tag
= group
;
287 attr
->value_tag
= type
;
288 attr
->values
[0].integer
= value
;
295 * 'ippAddIntegers()' - Add an array of integer values.
298 ipp_attribute_t
* /* O - New attribute */
299 ippAddIntegers(ipp_t
*ipp
, /* I - IPP message */
300 ipp_tag_t group
, /* I - IPP group */
301 ipp_tag_t type
, /* I - Type of attribute */
302 const char *name
, /* I - Name of attribute */
303 int num_values
, /* I - Number of values */
304 const int *values
) /* I - Values */
306 int i
; /* Looping var */
307 ipp_attribute_t
*attr
; /* New attribute */
308 ipp_value_t
*value
; /* Current value */
311 if (ipp
== NULL
|| name
== NULL
|| num_values
< 1)
314 if ((attr
= _ippAddAttr(ipp
, num_values
)) == NULL
)
317 attr
->name
= _cupsStrAlloc(name
);
318 attr
->group_tag
= group
;
319 attr
->value_tag
= type
;
322 for (i
= 0, value
= attr
->values
;
325 value
->integer
= values
[i
];
332 * 'ippAddOctetString()' - Add an octetString value to an IPP message.
337 ipp_attribute_t
* /* O - New attribute */
338 ippAddOctetString(ipp_t
*ipp
, /* I - IPP message */
339 ipp_tag_t group
, /* I - IPP group */
340 const char *name
, /* I - Name of attribute */
341 const void *data
, /* I - octetString data */
342 int datalen
) /* I - Length of data in bytes */
344 ipp_attribute_t
*attr
; /* New attribute */
347 if (ipp
== NULL
|| name
== NULL
)
350 if ((attr
= _ippAddAttr(ipp
, 1)) == NULL
)
354 * Initialize the attribute data...
357 attr
->name
= _cupsStrAlloc(name
);
358 attr
->group_tag
= group
;
359 attr
->value_tag
= IPP_TAG_STRING
;
360 attr
->values
[0].unknown
.length
= datalen
;
364 if ((attr
->values
[0].unknown
.data
= malloc(datalen
)) == NULL
)
366 ippDeleteAttribute(ipp
, attr
);
370 memcpy(attr
->values
[0].unknown
.data
, data
, datalen
);
374 * Return the new attribute...
382 * 'ippAddString()' - Add a language-encoded string to an IPP message.
385 ipp_attribute_t
* /* O - New attribute */
386 ippAddString(ipp_t
*ipp
, /* I - IPP message */
387 ipp_tag_t group
, /* I - IPP group */
388 ipp_tag_t type
, /* I - Type of attribute */
389 const char *name
, /* I - Name of attribute */
390 const char *charset
, /* I - Character set */
391 const char *value
) /* I - Value */
393 ipp_attribute_t
*attr
; /* New attribute */
394 char buffer
[1024], /* Language/charset value buffer */
395 *bufptr
; /* Pointer into buffer */
398 if (ipp
== NULL
|| name
== NULL
)
401 if ((attr
= _ippAddAttr(ipp
, 1)) == NULL
)
405 * Force value to be English for the POSIX locale...
408 if (type
== IPP_TAG_LANGUAGE
&& !strcasecmp(value
, "C"))
412 * Convert language values to lowercase and change _ to - as needed...
415 if ((type
== IPP_TAG_LANGUAGE
|| type
== IPP_TAG_CHARSET
) && value
)
417 strlcpy(buffer
, value
, sizeof(buffer
));
420 for (bufptr
= buffer
; *bufptr
; bufptr
++)
424 *bufptr
= tolower(*bufptr
& 255);
428 * Initialize the attribute data...
431 attr
->name
= _cupsStrAlloc(name
);
432 attr
->group_tag
= group
;
433 attr
->value_tag
= type
;
434 attr
->values
[0].string
.charset
= ((int)type
& IPP_TAG_COPY
) ? (char *)charset
:
435 charset
? _cupsStrAlloc(charset
) : NULL
;
436 attr
->values
[0].string
.text
= ((int)type
& IPP_TAG_COPY
) ? (char *)value
:
437 value
? _cupsStrAlloc(value
) : NULL
;
444 * 'ippAddStrings()' - Add language-encoded strings to an IPP message.
447 ipp_attribute_t
* /* O - New attribute */
449 ipp_t
*ipp
, /* I - IPP message */
450 ipp_tag_t group
, /* I - IPP group */
451 ipp_tag_t type
, /* I - Type of attribute */
452 const char *name
, /* I - Name of attribute */
453 int num_values
, /* I - Number of values */
454 const char *charset
, /* I - Character set */
455 const char * const *values
) /* I - Values */
457 int i
; /* Looping var */
458 ipp_attribute_t
*attr
; /* New attribute */
459 ipp_value_t
*value
; /* Current value */
462 if (ipp
== NULL
|| name
== NULL
|| num_values
< 1)
465 if ((attr
= _ippAddAttr(ipp
, num_values
)) == NULL
)
469 * Initialize the attribute data...
472 attr
->name
= _cupsStrAlloc(name
);
473 attr
->group_tag
= group
;
474 attr
->value_tag
= type
;
476 for (i
= 0, value
= attr
->values
;
481 value
->string
.charset
= ((int)type
& IPP_TAG_COPY
) ? (char *)charset
:
482 charset
? _cupsStrAlloc(charset
) : NULL
;
484 value
->string
.charset
= attr
->values
[0].string
.charset
;
489 * Force language to be English for the POSIX locale...
492 if (type
== IPP_TAG_LANGUAGE
&& !strcasecmp(values
[i
], "C"))
493 value
->string
.text
= ((int)type
& IPP_TAG_COPY
) ? "en" :
496 value
->string
.text
= ((int)type
& IPP_TAG_COPY
) ? (char *)values
[i
] :
497 _cupsStrAlloc(values
[i
]);
506 * 'ippAddRange()' - Add a range of values to an IPP message.
509 ipp_attribute_t
* /* O - New attribute */
510 ippAddRange(ipp_t
*ipp
, /* I - IPP message */
511 ipp_tag_t group
, /* I - IPP group */
512 const char *name
, /* I - Name of attribute */
513 int lower
, /* I - Lower value */
514 int upper
) /* I - Upper value */
516 ipp_attribute_t
*attr
; /* New attribute */
519 if (ipp
== NULL
|| name
== NULL
)
522 if ((attr
= _ippAddAttr(ipp
, 1)) == NULL
)
525 attr
->name
= _cupsStrAlloc(name
);
526 attr
->group_tag
= group
;
527 attr
->value_tag
= IPP_TAG_RANGE
;
528 attr
->values
[0].range
.lower
= lower
;
529 attr
->values
[0].range
.upper
= upper
;
536 * 'ippAddRanges()' - Add ranges of values to an IPP message.
539 ipp_attribute_t
* /* O - New attribute */
540 ippAddRanges(ipp_t
*ipp
, /* I - IPP message */
541 ipp_tag_t group
, /* I - IPP group */
542 const char *name
, /* I - Name of attribute */
543 int num_values
, /* I - Number of values */
544 const int *lower
, /* I - Lower values */
545 const int *upper
) /* I - Upper values */
547 int i
; /* Looping var */
548 ipp_attribute_t
*attr
; /* New attribute */
549 ipp_value_t
*value
; /* Current value */
552 if (ipp
== NULL
|| name
== NULL
|| num_values
< 1)
555 if ((attr
= _ippAddAttr(ipp
, num_values
)) == NULL
)
558 attr
->name
= _cupsStrAlloc(name
);
559 attr
->group_tag
= group
;
560 attr
->value_tag
= IPP_TAG_RANGE
;
562 if (lower
!= NULL
&& upper
!= NULL
)
563 for (i
= 0, value
= attr
->values
;
567 value
->range
.lower
= lower
[i
];
568 value
->range
.upper
= upper
[i
];
576 * 'ippAddResolution()' - Add a resolution value to an IPP message.
579 ipp_attribute_t
* /* O - New attribute */
580 ippAddResolution(ipp_t
*ipp
, /* I - IPP message */
581 ipp_tag_t group
, /* I - IPP group */
582 const char *name
, /* I - Name of attribute */
583 ipp_res_t units
, /* I - Units for resolution */
584 int xres
, /* I - X resolution */
585 int yres
) /* I - Y resolution */
587 ipp_attribute_t
*attr
; /* New attribute */
590 if (ipp
== NULL
|| name
== NULL
)
593 if ((attr
= _ippAddAttr(ipp
, 1)) == NULL
)
596 attr
->name
= _cupsStrAlloc(name
);
597 attr
->group_tag
= group
;
598 attr
->value_tag
= IPP_TAG_RESOLUTION
;
599 attr
->values
[0].resolution
.xres
= xres
;
600 attr
->values
[0].resolution
.yres
= yres
;
601 attr
->values
[0].resolution
.units
= units
;
608 * 'ippAddResolutions()' - Add resolution values to an IPP message.
611 ipp_attribute_t
* /* O - New attribute */
612 ippAddResolutions(ipp_t
*ipp
, /* I - IPP message */
613 ipp_tag_t group
, /* I - IPP group */
614 const char *name
, /* I - Name of attribute */
615 int num_values
,/* I - Number of values */
616 ipp_res_t units
, /* I - Units for resolution */
617 const int *xres
, /* I - X resolutions */
618 const int *yres
) /* I - Y resolutions */
620 int i
; /* Looping var */
621 ipp_attribute_t
*attr
; /* New attribute */
622 ipp_value_t
*value
; /* Current value */
625 if (ipp
== NULL
|| name
== NULL
|| num_values
< 1)
628 if ((attr
= _ippAddAttr(ipp
, num_values
)) == NULL
)
631 attr
->name
= _cupsStrAlloc(name
);
632 attr
->group_tag
= group
;
633 attr
->value_tag
= IPP_TAG_RESOLUTION
;
635 if (xres
!= NULL
&& yres
!= NULL
)
636 for (i
= 0, value
= attr
->values
;
640 value
->resolution
.xres
= xres
[i
];
641 value
->resolution
.yres
= yres
[i
];
642 value
->resolution
.units
= units
;
650 * 'ippAddSeparator()' - Add a group separator to an IPP message.
653 ipp_attribute_t
* /* O - New attribute */
654 ippAddSeparator(ipp_t
*ipp
) /* I - IPP message */
656 ipp_attribute_t
*attr
; /* New attribute */
659 DEBUG_printf(("ippAddSeparator(%p)\n", ipp
));
664 if ((attr
= _ippAddAttr(ipp
, 0)) == NULL
)
667 attr
->group_tag
= IPP_TAG_ZERO
;
668 attr
->value_tag
= IPP_TAG_ZERO
;
675 * 'ippDateToTime()' - Convert from RFC 1903 Date/Time format to UNIX time
679 time_t /* O - UNIX time value */
680 ippDateToTime(const ipp_uchar_t
*date
) /* I - RFC 1903 date info */
682 struct tm unixdate
; /* UNIX date/time info */
683 time_t t
; /* Computed time */
686 memset(&unixdate
, 0, sizeof(unixdate
));
689 * RFC-1903 date/time format is:
691 * Byte(s) Description
692 * ------- -----------
693 * 0-1 Year (0 to 65535)
697 * 5 Minutes (0 to 59)
698 * 6 Seconds (0 to 60, 60 = "leap second")
699 * 7 Deciseconds (0 to 9)
701 * 9 UTC hours (0 to 11)
702 * 10 UTC minutes (0 to 59)
705 unixdate
.tm_year
= ((date
[0] << 8) | date
[1]) - 1900;
706 unixdate
.tm_mon
= date
[2] - 1;
707 unixdate
.tm_mday
= date
[3];
708 unixdate
.tm_hour
= date
[4];
709 unixdate
.tm_min
= date
[5];
710 unixdate
.tm_sec
= date
[6];
712 t
= mktime(&unixdate
);
715 t
+= date
[9] * 3600 + date
[10] * 60;
717 t
-= date
[9] * 3600 + date
[10] * 60;
724 * 'ippDelete()' - Delete an IPP message.
728 ippDelete(ipp_t
*ipp
) /* I - IPP message */
730 ipp_attribute_t
*attr
, /* Current attribute */
731 *next
; /* Next attribute */
734 DEBUG_printf(("ippDelete(): %p\n", ipp
));
739 for (attr
= ipp
->attrs
; attr
!= NULL
; attr
= next
)
750 * 'ippDeleteAttribute()' - Delete a single attribute in an IPP message.
752 * @since CUPS 1.1.19@
757 ipp_t
*ipp
, /* I - IPP message */
758 ipp_attribute_t
*attr
) /* I - Attribute to delete */
760 ipp_attribute_t
*current
, /* Current attribute */
761 *prev
; /* Previous attribute */
765 * Find the attribute in the list...
768 for (current
= ipp
->attrs
, prev
= NULL
;
769 current
!= NULL
&& current
!= attr
;
770 prev
= current
, current
= current
->next
);
775 * Found it, remove the attribute from the list...
779 prev
->next
= current
->next
;
781 ipp
->attrs
= current
->next
;
783 if (current
== ipp
->last
)
787 * Free memory used by the attribute...
790 _ippFreeAttr(current
);
796 * 'ippFindAttribute()' - Find a named attribute in a request...
799 ipp_attribute_t
* /* O - Matching attribute */
800 ippFindAttribute(ipp_t
*ipp
, /* I - IPP message */
801 const char *name
, /* I - Name of attribute */
802 ipp_tag_t type
) /* I - Type of attribute */
804 DEBUG_printf(("ippFindAttribute(%p, \'%s\')\n", ipp
, name
));
806 if (ipp
== NULL
|| name
== NULL
)
810 * Reset the current pointer...
816 * Search for the attribute...
819 return (ippFindNextAttribute(ipp
, name
, type
));
824 * 'ippFindNextAttribute()' - Find the next named attribute in a request...
827 ipp_attribute_t
* /* O - Matching attribute */
828 ippFindNextAttribute(ipp_t
*ipp
, /* I - IPP message */
829 const char *name
, /* I - Name of attribute */
830 ipp_tag_t type
) /* I - Type of attribute */
832 ipp_attribute_t
*attr
; /* Current atttribute */
833 ipp_tag_t value_tag
; /* Value tag */
836 DEBUG_printf(("ippFindNextAttribute(%p, \'%s\')\n", ipp
, name
));
838 if (ipp
== NULL
|| name
== NULL
)
843 ipp
->prev
= ipp
->current
;
844 attr
= ipp
->current
->next
;
852 for (; attr
!= NULL
; ipp
->prev
= attr
, attr
= attr
->next
)
854 DEBUG_printf(("ippFindAttribute: attr = %p, name = \'%s\'\n", attr
,
857 value_tag
= (ipp_tag_t
)(attr
->value_tag
& IPP_TAG_MASK
);
859 if (attr
->name
!= NULL
&& strcasecmp(attr
->name
, name
) == 0 &&
860 (value_tag
== type
|| type
== IPP_TAG_ZERO
||
861 (value_tag
== IPP_TAG_TEXTLANG
&& type
== IPP_TAG_TEXT
) ||
862 (value_tag
== IPP_TAG_NAMELANG
&& type
== IPP_TAG_NAME
)))
878 * 'ippLength()' - Compute the length of an IPP message.
881 size_t /* O - Size of IPP message */
882 ippLength(ipp_t
*ipp
) /* I - IPP message */
884 return (ipp_length(ipp
, 0));
889 * 'ippNew()' - Allocate a new IPP message.
892 ipp_t
* /* O - New IPP message */
895 ipp_t
*temp
; /* New IPP message */
898 DEBUG_puts("ippNew()");
900 if ((temp
= (ipp_t
*)calloc(1, sizeof(ipp_t
))) != NULL
)
903 * Default to IPP 1.1...
906 temp
->request
.any
.version
[0] = 1;
907 temp
->request
.any
.version
[1] = 1;
910 DEBUG_printf(("ippNew: %p\n", temp
));
917 * 'ippNewRequest()' - Allocate a new IPP request message.
919 * The new request message is initialized with the attributes-charset and
920 * attributes-natural-language attributes added. The
921 * attributes-natural-language value is derived from the current locale.
926 ipp_t
* /* O - IPP request message */
927 ippNewRequest(ipp_op_t op
) /* I - Operation code */
929 ipp_t
*request
; /* IPP request message */
930 cups_lang_t
*language
; /* Current language localization */
934 * Create a new IPP message...
937 if ((request
= ippNew()) == NULL
)
941 * Set the operation and request ID...
944 request
->request
.op
.operation_id
= op
;
945 request
->request
.op
.request_id
= 1;
948 * Use UTF-8 as the character set...
951 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_CHARSET
,
952 "attributes-charset", NULL
, "utf-8");
955 * Get the language from the current locale...
958 language
= cupsLangDefault();
960 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_LANGUAGE
,
961 "attributes-natural-language", NULL
, language
->language
);
964 * Return the new request...
972 * 'ippRead()' - Read data for an IPP message from a HTTP connection.
975 ipp_state_t
/* O - Current state */
976 ippRead(http_t
*http
, /* I - HTTP connection */
977 ipp_t
*ipp
) /* I - IPP data */
979 DEBUG_printf(("ippRead(http=%p, ipp=%p), data_remaining=" CUPS_LLFMT
"\n",
980 http
, ipp
, CUPS_LLCAST (http
? http
->data_remaining
: -1)));
985 DEBUG_printf(("http->state = %d\n", http
->state
));
987 return (ippReadIO(http
, (ipp_iocb_t
)ipp_read_http
,
988 http
->blocking
|| http
->used
!= 0, NULL
, ipp
));
993 * 'ippReadFile()' - Read data for an IPP message from a file.
995 * @since CUPS 1.1.19@
998 ipp_state_t
/* O - Current state */
999 ippReadFile(int fd
, /* I - HTTP data */
1000 ipp_t
*ipp
) /* I - IPP data */
1002 DEBUG_printf(("ippReadFile(%d, %p)\n", fd
, ipp
));
1004 return (ippReadIO(&fd
, (ipp_iocb_t
)ipp_read_file
, 1, NULL
, ipp
));
1009 * 'ippReadIO()' - Read data for an IPP message.
1014 ipp_state_t
/* O - Current state */
1015 ippReadIO(void *src
, /* I - Data source */
1016 ipp_iocb_t cb
, /* I - Read callback function */
1017 int blocking
, /* I - Use blocking IO? */
1018 ipp_t
*parent
, /* I - Parent request, if any */
1019 ipp_t
*ipp
) /* I - IPP data */
1021 int n
; /* Length of data */
1022 unsigned char buffer
[IPP_MAX_LENGTH
],
1024 string
[IPP_MAX_NAME
],
1025 /* Small string buffer */
1026 *bufptr
; /* Pointer into buffer */
1027 ipp_attribute_t
*attr
; /* Current attribute */
1028 ipp_tag_t tag
; /* Current tag */
1029 ipp_tag_t value_tag
; /* Current value tag */
1030 ipp_value_t
*value
; /* Current value */
1033 DEBUG_printf(("ippReadIO(%p, %p, %d, %p, %p)\n", src
, cb
, blocking
,
1035 DEBUG_printf(("ippReadIO: ipp->state=%d\n", ipp
->state
));
1037 if (src
== NULL
|| ipp
== NULL
)
1043 ipp
->state
++; /* Avoid common problem... */
1049 * Get the request header...
1052 if ((n
= (*cb
)(src
, buffer
, 8)) < 8)
1054 DEBUG_printf(("ippReadIO: Unable to read header (%d bytes read)!\n", n
));
1059 * Verify the major version number...
1064 DEBUG_printf(("ippReadIO: version number (%d.%d) is bad.\n", buffer
[0],
1070 * Then copy the request header over...
1073 ipp
->request
.any
.version
[0] = buffer
[0];
1074 ipp
->request
.any
.version
[1] = buffer
[1];
1075 ipp
->request
.any
.op_status
= (buffer
[2] << 8) | buffer
[3];
1076 ipp
->request
.any
.request_id
= (((((buffer
[4] << 8) | buffer
[5]) << 8) |
1077 buffer
[6]) << 8) | buffer
[7];
1079 DEBUG_printf(("ippReadIO: version=%d.%d\n", buffer
[0], buffer
[1]));
1080 DEBUG_printf(("ippReadIO: op_status=%04x\n",
1081 ipp
->request
.any
.op_status
));
1082 DEBUG_printf(("ippReadIO: request_id=%d\n",
1083 ipp
->request
.any
.request_id
));
1086 ipp
->state
= IPP_ATTRIBUTE
;
1087 ipp
->current
= NULL
;
1088 ipp
->curtag
= IPP_TAG_ZERO
;
1089 ipp
->prev
= ipp
->last
;
1092 * If blocking is disabled, stop here...
1098 case IPP_ATTRIBUTE
:
1101 if ((*cb
)(src
, buffer
, 1) < 1)
1104 DEBUG_printf(("ippReadIO: ipp->current=%p, ipp->prev=%p\n",
1105 ipp
->current
, ipp
->prev
));
1108 * Read this attribute...
1111 tag
= (ipp_tag_t
)buffer
[0];
1113 if (tag
== IPP_TAG_END
)
1116 * No more attributes left...
1119 DEBUG_puts("ippReadIO: IPP_TAG_END!");
1121 ipp
->state
= IPP_DATA
;
1124 else if (tag
< IPP_TAG_UNSUPPORTED_VALUE
)
1127 * Group tag... Set the current group and continue...
1130 if (ipp
->curtag
== tag
)
1131 ipp
->prev
= ippAddSeparator(ipp
);
1132 else if (ipp
->current
)
1133 ipp
->prev
= ipp
->current
;
1136 ipp
->current
= NULL
;
1137 DEBUG_printf(("ippReadIO: group tag = %x, ipp->prev=%p\n", tag
,
1142 DEBUG_printf(("ippReadIO: value tag = %x\n", tag
));
1148 if ((*cb
)(src
, buffer
, 2) < 2)
1150 DEBUG_puts("ippReadIO: unable to read name length!");
1154 n
= (buffer
[0] << 8) | buffer
[1];
1156 if (n
> (sizeof(buffer
) - 1))
1158 DEBUG_printf(("ippReadIO: bad name length %d!\n", n
));
1162 DEBUG_printf(("ippReadIO: name length = %d\n", n
));
1164 if (n
== 0 && tag
!= IPP_TAG_MEMBERNAME
&&
1165 tag
!= IPP_TAG_END_COLLECTION
)
1168 * More values for current attribute...
1171 if (ipp
->current
== NULL
)
1174 attr
= ipp
->current
;
1175 value_tag
= (ipp_tag_t
)(attr
->value_tag
& IPP_TAG_MASK
);
1178 * Make sure we aren't adding a new value of a different
1182 if (value_tag
== IPP_TAG_ZERO
)
1185 * Setting the value of a collection member...
1188 attr
->value_tag
= tag
;
1190 else if (value_tag
== IPP_TAG_STRING
||
1191 (value_tag
>= IPP_TAG_TEXTLANG
&&
1192 value_tag
<= IPP_TAG_MIMETYPE
))
1195 * String values can sometimes come across in different
1196 * forms; accept sets of differing values...
1199 if (tag
!= IPP_TAG_STRING
&&
1200 (tag
< IPP_TAG_TEXTLANG
|| tag
> IPP_TAG_MIMETYPE
))
1203 else if (value_tag
!= tag
)
1207 * Finally, reallocate the attribute array as needed...
1210 if (attr
->num_values
== 1 ||
1211 (attr
->num_values
> 0 &&
1212 (attr
->num_values
& (IPP_MAX_VALUES
- 1)) == 0))
1214 ipp_attribute_t
*temp
; /* Pointer to new buffer */
1217 DEBUG_printf(("ippReadIO: reallocating for up to %d values...\n",
1218 attr
->num_values
+ IPP_MAX_VALUES
));
1221 * Reallocate memory...
1224 if ((temp
= realloc(attr
, sizeof(ipp_attribute_t
) +
1225 (attr
->num_values
+ IPP_MAX_VALUES
- 1) *
1226 sizeof(ipp_value_t
))) == NULL
)
1232 * Reset pointers in the list...
1236 ipp
->prev
->next
= temp
;
1240 attr
= ipp
->current
= ipp
->last
= temp
;
1244 else if (tag
== IPP_TAG_MEMBERNAME
)
1247 * Name must be length 0!
1252 DEBUG_puts("ippReadIO: member name not empty!");
1257 ipp
->prev
= ipp
->current
;
1259 attr
= ipp
->current
= _ippAddAttr(ipp
, 1);
1261 DEBUG_printf(("ippReadIO: membername, ipp->current=%p, ipp->prev=%p\n",
1262 ipp
->current
, ipp
->prev
));
1264 attr
->group_tag
= ipp
->curtag
;
1265 attr
->value_tag
= IPP_TAG_ZERO
;
1266 attr
->num_values
= 0;
1268 else if (tag
!= IPP_TAG_END_COLLECTION
)
1271 * New attribute; read the name and add it...
1274 if ((*cb
)(src
, buffer
, n
) < n
)
1276 DEBUG_puts("ippReadIO: unable to read name!");
1283 ipp
->prev
= ipp
->current
;
1285 if ((attr
= ipp
->current
= _ippAddAttr(ipp
, 1)) == NULL
)
1287 DEBUG_puts("ippReadIO: unable to allocate attribute!");
1291 DEBUG_printf(("ippReadIO: name=\'%s\', ipp->current=%p, ipp->prev=%p\n",
1292 buffer
, ipp
->current
, ipp
->prev
));
1294 attr
->group_tag
= ipp
->curtag
;
1295 attr
->value_tag
= tag
;
1296 attr
->name
= _cupsStrAlloc((char *)buffer
);
1297 attr
->num_values
= 0;
1302 if (tag
!= IPP_TAG_END_COLLECTION
)
1303 value
= attr
->values
+ attr
->num_values
;
1307 if ((*cb
)(src
, buffer
, 2) < 2)
1309 DEBUG_puts("ippReadIO: unable to read value length!");
1313 n
= (buffer
[0] << 8) | buffer
[1];
1314 DEBUG_printf(("ippReadIO: value length = %d\n", n
));
1318 case IPP_TAG_INTEGER
:
1322 DEBUG_printf(("ippReadIO: bad value length %d!\n", n
));
1326 if ((*cb
)(src
, buffer
, 4) < 4)
1328 DEBUG_puts("ippReadIO: Unable to read integer value!");
1332 n
= (((((buffer
[0] << 8) | buffer
[1]) << 8) | buffer
[2]) << 8) |
1337 case IPP_TAG_BOOLEAN
:
1340 DEBUG_printf(("ippReadIO: bad value length %d!\n", n
));
1344 if ((*cb
)(src
, buffer
, 1) < 1)
1346 DEBUG_puts("ippReadIO: Unable to read boolean value!");
1350 value
->boolean
= buffer
[0];
1354 case IPP_TAG_KEYWORD
:
1355 case IPP_TAG_STRING
:
1357 case IPP_TAG_URISCHEME
:
1358 case IPP_TAG_CHARSET
:
1359 case IPP_TAG_LANGUAGE
:
1360 case IPP_TAG_MIMETYPE
:
1361 if (n
>= sizeof(buffer
))
1363 DEBUG_printf(("ippReadIO: bad value length %d!\n", n
));
1367 if ((*cb
)(src
, buffer
, n
) < n
)
1369 DEBUG_puts("ippReadIO: unable to read name!");
1374 value
->string
.text
= _cupsStrAlloc((char *)buffer
);
1375 DEBUG_printf(("ippReadIO: value = \'%s\'\n",
1376 value
->string
.text
));
1381 DEBUG_printf(("ippReadIO: bad value length %d!\n", n
));
1385 if ((*cb
)(src
, value
->date
, 11) < 11)
1387 DEBUG_puts("ippReadIO: Unable to date integer value!");
1391 case IPP_TAG_RESOLUTION
:
1394 DEBUG_printf(("ippReadIO: bad value length %d!\n", n
));
1398 if ((*cb
)(src
, buffer
, 9) < 9)
1400 DEBUG_puts("ippReadIO: Unable to read resolution value!");
1404 value
->resolution
.xres
=
1405 (((((buffer
[0] << 8) | buffer
[1]) << 8) | buffer
[2]) << 8) |
1407 value
->resolution
.yres
=
1408 (((((buffer
[4] << 8) | buffer
[5]) << 8) | buffer
[6]) << 8) |
1410 value
->resolution
.units
=
1411 (ipp_res_t
)buffer
[8];
1413 case IPP_TAG_RANGE
:
1416 DEBUG_printf(("ippReadIO: bad value length %d!\n", n
));
1420 if ((*cb
)(src
, buffer
, 8) < 8)
1422 DEBUG_puts("ippReadIO: Unable to read range value!");
1426 value
->range
.lower
=
1427 (((((buffer
[0] << 8) | buffer
[1]) << 8) | buffer
[2]) << 8) |
1429 value
->range
.upper
=
1430 (((((buffer
[4] << 8) | buffer
[5]) << 8) | buffer
[6]) << 8) |
1433 case IPP_TAG_TEXTLANG
:
1434 case IPP_TAG_NAMELANG
:
1435 if (n
>= sizeof(buffer
) || n
< 4)
1437 DEBUG_printf(("ippReadIO: bad value length %d!\n", n
));
1441 if ((*cb
)(src
, buffer
, n
) < n
)
1443 DEBUG_puts("ippReadIO: Unable to read string w/language value!");
1450 * text-with-language and name-with-language are composite
1459 n
= (bufptr
[0] << 8) | bufptr
[1];
1461 if ((bufptr
+ 2 + n
) >= (buffer
+ sizeof(buffer
)) ||
1462 n
>= sizeof(string
))
1464 DEBUG_printf(("ippReadIO: bad value length %d!\n", n
));
1468 memcpy(string
, bufptr
+ 2, n
);
1471 value
->string
.charset
= _cupsStrAlloc((char *)string
);
1474 n
= (bufptr
[0] << 8) | bufptr
[1];
1476 if ((bufptr
+ 2 + n
) >= (buffer
+ sizeof(buffer
)))
1478 DEBUG_printf(("ippReadIO: bad value length %d!\n", n
));
1482 bufptr
[2 + n
] = '\0';
1483 value
->string
.text
= _cupsStrAlloc((char *)bufptr
+ 2);
1486 case IPP_TAG_BEGIN_COLLECTION
:
1488 * Oh, boy, here comes a collection value, so read it...
1491 value
->collection
= ippNew();
1495 DEBUG_puts("ippReadIO: begCollection tag with value length > 0!");
1499 if (ippReadIO(src
, cb
, 1, ipp
, value
->collection
) == IPP_ERROR
)
1501 DEBUG_puts("ippReadIO: Unable to read collection value!");
1506 case IPP_TAG_END_COLLECTION
:
1509 DEBUG_puts("ippReadIO: endCollection tag with value length > 0!");
1513 DEBUG_puts("ippReadIO: endCollection tag...");
1515 return (ipp
->state
= IPP_DATA
);
1517 case IPP_TAG_MEMBERNAME
:
1519 * The value the name of the member in the collection, which
1520 * we need to carry over...
1523 if (n
>= sizeof(buffer
))
1525 DEBUG_printf(("ippReadIO: bad value length %d!\n", n
));
1529 if ((*cb
)(src
, buffer
, n
) < n
)
1531 DEBUG_puts("ippReadIO: Unable to read member name value!");
1536 attr
->name
= _cupsStrAlloc((char *)buffer
);
1539 * Since collection members are encoded differently than
1540 * regular attributes, make sure we don't start with an
1544 attr
->num_values
--;
1546 DEBUG_printf(("ippReadIO: member name = \"%s\"\n", attr
->name
));
1549 default : /* Other unsupported values */
1550 if (n
> IPP_MAX_LENGTH
)
1552 DEBUG_printf(("ippReadIO: bad value length %d!\n", n
));
1556 value
->unknown
.length
= n
;
1559 if ((value
->unknown
.data
= malloc(n
)) == NULL
)
1561 DEBUG_puts("ippReadIO: Unable to allocate value");
1565 if ((*cb
)(src
, value
->unknown
.data
, n
) < n
)
1567 DEBUG_puts("ippReadIO: Unable to read unsupported value!");
1572 value
->unknown
.data
= NULL
;
1576 attr
->num_values
++;
1579 * If blocking is disabled, stop here...
1591 break; /* anti-compiler-warning-code */
1594 DEBUG_printf(("ippReadIO: returning ipp->state=%d!\n", ipp
->state
));
1596 return (ipp
->state
);
1601 * 'ippTimeToDate()' - Convert from UNIX time to RFC 1903 format.
1604 const ipp_uchar_t
* /* O - RFC-1903 date/time data */
1605 ippTimeToDate(time_t t
) /* I - UNIX time value */
1607 struct tm
*unixdate
; /* UNIX unixdate/time info */
1608 ipp_uchar_t
*date
= _cupsGlobals()->ipp_date
;
1609 /* RFC-1903 date/time data */
1613 * RFC-1903 date/time format is:
1615 * Byte(s) Description
1616 * ------- -----------
1617 * 0-1 Year (0 to 65535)
1621 * 5 Minutes (0 to 59)
1622 * 6 Seconds (0 to 60, 60 = "leap second")
1623 * 7 Deciseconds (0 to 9)
1625 * 9 UTC hours (0 to 11)
1626 * 10 UTC minutes (0 to 59)
1629 unixdate
= gmtime(&t
);
1630 unixdate
->tm_year
+= 1900;
1632 date
[0] = unixdate
->tm_year
>> 8;
1633 date
[1] = unixdate
->tm_year
;
1634 date
[2] = unixdate
->tm_mon
+ 1;
1635 date
[3] = unixdate
->tm_mday
;
1636 date
[4] = unixdate
->tm_hour
;
1637 date
[5] = unixdate
->tm_min
;
1638 date
[6] = unixdate
->tm_sec
;
1649 * 'ippWrite()' - Write data for an IPP message to a HTTP connection.
1652 ipp_state_t
/* O - Current state */
1653 ippWrite(http_t
*http
, /* I - HTTP connection */
1654 ipp_t
*ipp
) /* I - IPP data */
1656 DEBUG_printf(("ippWrite(%p, %p)\n", http
, ipp
));
1661 return (ippWriteIO(http
, (ipp_iocb_t
)httpWrite2
,
1662 http
->blocking
, NULL
, ipp
));
1667 * 'ippWriteFile()' - Write data for an IPP message to a file.
1669 * @since CUPS 1.1.19@
1672 ipp_state_t
/* O - Current state */
1673 ippWriteFile(int fd
, /* I - HTTP data */
1674 ipp_t
*ipp
) /* I - IPP data */
1676 DEBUG_printf(("ippWriteFile(%d, %p)\n", fd
, ipp
));
1678 ipp
->state
= IPP_IDLE
;
1680 return (ippWriteIO(&fd
, (ipp_iocb_t
)ipp_write_file
, 1, NULL
, ipp
));
1685 * 'ippWriteIO()' - Write data for an IPP message.
1690 ipp_state_t
/* O - Current state */
1691 ippWriteIO(void *dst
, /* I - Destination */
1692 ipp_iocb_t cb
, /* I - Write callback function */
1693 int blocking
, /* I - Use blocking IO? */
1694 ipp_t
*parent
, /* I - Parent IPP message */
1695 ipp_t
*ipp
) /* I - IPP data */
1697 int i
; /* Looping var */
1698 int n
; /* Length of data */
1699 unsigned char buffer
[IPP_MAX_LENGTH
+ 2],
1700 /* Data buffer + length bytes */
1701 *bufptr
; /* Pointer into buffer */
1702 ipp_attribute_t
*attr
; /* Current attribute */
1703 ipp_value_t
*value
; /* Current value */
1706 DEBUG_printf(("ippWriteIO(%p, %p, %d, %p, %p)\n", dst
, cb
, blocking
,
1709 if (dst
== NULL
|| ipp
== NULL
)
1715 ipp
->state
++; /* Avoid common problem... */
1721 * Send the request header:
1724 * Operation/Status Code = 2 bytes
1725 * Request ID = 4 bytes
1731 *bufptr
++ = ipp
->request
.any
.version
[0];
1732 *bufptr
++ = ipp
->request
.any
.version
[1];
1733 *bufptr
++ = ipp
->request
.any
.op_status
>> 8;
1734 *bufptr
++ = ipp
->request
.any
.op_status
;
1735 *bufptr
++ = ipp
->request
.any
.request_id
>> 24;
1736 *bufptr
++ = ipp
->request
.any
.request_id
>> 16;
1737 *bufptr
++ = ipp
->request
.any
.request_id
>> 8;
1738 *bufptr
++ = ipp
->request
.any
.request_id
;
1740 if ((*cb
)(dst
, buffer
, (int)(bufptr
- buffer
)) < 0)
1742 DEBUG_puts("ippWriteIO: Could not write IPP header...");
1748 * Reset the state engine to point to the first attribute
1749 * in the request/response, with no current group.
1752 ipp
->state
= IPP_ATTRIBUTE
;
1753 ipp
->current
= ipp
->attrs
;
1754 ipp
->curtag
= IPP_TAG_ZERO
;
1756 DEBUG_printf(("ippWriteIO: version=%d.%d\n", buffer
[0], buffer
[1]));
1757 DEBUG_printf(("ippWriteIO: op_status=%04x\n", ipp
->request
.any
.op_status
));
1758 DEBUG_printf(("ippWriteIO: request_id=%d\n", ipp
->request
.any
.request_id
));
1761 * If blocking is disabled, stop here...
1767 case IPP_ATTRIBUTE
:
1768 while (ipp
->current
!= NULL
)
1771 * Write this attribute...
1775 attr
= ipp
->current
;
1777 ipp
->current
= ipp
->current
->next
;
1779 if (ipp
->curtag
!= attr
->group_tag
&& parent
== NULL
)
1782 * Send a group tag byte...
1785 ipp
->curtag
= attr
->group_tag
;
1787 if (attr
->group_tag
== IPP_TAG_ZERO
)
1790 DEBUG_printf(("ippWriteIO: wrote group tag = %x\n", attr
->group_tag
));
1791 *bufptr
++ = attr
->group_tag
;
1793 else if (attr
->group_tag
== IPP_TAG_ZERO
)
1797 * Write the attribute tag and name. The current implementation
1798 * does not support the extension value tags above 0x7f, so all
1799 * value tags are 1 byte.
1801 * The attribute name length does not include the trailing nul
1802 * character in the source string.
1804 * Collection values (parent != NULL) are written differently...
1810 * Get the length of the attribute name, and make sure it won't
1811 * overflow the buffer...
1814 if ((n
= (int)strlen(attr
->name
)) > (sizeof(buffer
) - 4))
1818 * Write the value tag, name length, and name string...
1821 DEBUG_printf(("ippWriteIO: writing value tag = %x\n", attr
->value_tag
));
1822 DEBUG_printf(("ippWriteIO: writing name = %d, \'%s\'\n", n
, attr
->name
));
1824 *bufptr
++ = attr
->value_tag
;
1827 memcpy(bufptr
, attr
->name
, n
);
1833 * Get the length of the attribute name, and make sure it won't
1834 * overflow the buffer...
1837 if ((n
= (int)strlen(attr
->name
)) > (sizeof(buffer
) - 7))
1841 * Write the member name tag, name length, name string, value tag,
1842 * and empty name for the collection member attribute...
1845 DEBUG_printf(("ippWriteIO: writing value tag = %x\n",
1846 IPP_TAG_MEMBERNAME
));
1847 DEBUG_printf(("ippWriteIO: writing name = %d, \'%s\'\n", n
, attr
->name
));
1848 DEBUG_printf(("ippWriteIO: writing value tag = %x\n", attr
->value_tag
));
1849 DEBUG_puts("ippWriteIO: writing name = 0, \'\'\n");
1851 *bufptr
++ = IPP_TAG_MEMBERNAME
;
1856 memcpy(bufptr
, attr
->name
, n
);
1859 *bufptr
++ = attr
->value_tag
;
1865 * Now write the attribute value(s)...
1868 switch (attr
->value_tag
& ~IPP_TAG_COPY
)
1870 case IPP_TAG_INTEGER
:
1872 for (i
= 0, value
= attr
->values
;
1873 i
< attr
->num_values
;
1876 if ((sizeof(buffer
) - (bufptr
- buffer
)) < 9)
1878 if ((*cb
)(dst
, buffer
, (int)(bufptr
- buffer
)) < 0)
1880 DEBUG_puts("ippWriteIO: Could not write IPP attribute...");
1890 * Arrays and sets are done by sending additional
1891 * values with a zero-length name...
1894 *bufptr
++ = attr
->value_tag
;
1900 * Integers and enumerations are both 4-byte signed
1901 * (twos-complement) values.
1903 * Put the 2-byte length and 4-byte value into the buffer...
1908 *bufptr
++ = value
->integer
>> 24;
1909 *bufptr
++ = value
->integer
>> 16;
1910 *bufptr
++ = value
->integer
>> 8;
1911 *bufptr
++ = value
->integer
;
1915 case IPP_TAG_BOOLEAN
:
1916 for (i
= 0, value
= attr
->values
;
1917 i
< attr
->num_values
;
1920 if ((sizeof(buffer
) - (bufptr
- buffer
)) < 6)
1922 if ((*cb
)(dst
, buffer
, (int)(bufptr
- buffer
)) < 0)
1924 DEBUG_puts("ippWriteIO: Could not write IPP attribute...");
1934 * Arrays and sets are done by sending additional
1935 * values with a zero-length name...
1938 *bufptr
++ = attr
->value_tag
;
1944 * Boolean values are 1-byte; 0 = false, 1 = true.
1946 * Put the 2-byte length and 1-byte value into the buffer...
1951 *bufptr
++ = value
->boolean
;
1957 case IPP_TAG_KEYWORD
:
1958 case IPP_TAG_STRING
:
1960 case IPP_TAG_URISCHEME
:
1961 case IPP_TAG_CHARSET
:
1962 case IPP_TAG_LANGUAGE
:
1963 case IPP_TAG_MIMETYPE
:
1964 for (i
= 0, value
= attr
->values
;
1965 i
< attr
->num_values
;
1971 * Arrays and sets are done by sending additional
1972 * values with a zero-length name...
1975 DEBUG_printf(("ippWriteIO: writing value tag = %x\n",
1977 DEBUG_printf(("ippWriteIO: writing name = 0, \'\'\n"));
1979 if ((sizeof(buffer
) - (bufptr
- buffer
)) < 3)
1981 if ((*cb
)(dst
, buffer
, (int)(bufptr
- buffer
)) < 0)
1983 DEBUG_puts("ippWriteIO: Could not write IPP attribute...");
1990 *bufptr
++ = attr
->value_tag
;
1995 if (value
->string
.text
!= NULL
)
1996 n
= (int)strlen(value
->string
.text
);
2000 if (n
> (sizeof(buffer
) - 2))
2003 DEBUG_printf(("ippWriteIO: writing string = %d, \'%s\'\n", n
,
2004 value
->string
.text
));
2006 if ((int)(sizeof(buffer
) - (bufptr
- buffer
)) < (n
+ 2))
2008 if ((*cb
)(dst
, buffer
, (int)(bufptr
- buffer
)) < 0)
2010 DEBUG_puts("ippWriteIO: Could not write IPP attribute...");
2018 * All simple strings consist of the 2-byte length and
2019 * character data without the trailing nul normally found
2020 * in C strings. Also, strings cannot be longer than IPP_MAX_LENGTH
2021 * bytes since the 2-byte length is a signed (twos-complement)
2024 * Put the 2-byte length and string characters in the buffer.
2032 memcpy(bufptr
, value
->string
.text
, n
);
2039 for (i
= 0, value
= attr
->values
;
2040 i
< attr
->num_values
;
2043 if ((sizeof(buffer
) - (bufptr
- buffer
)) < 16)
2045 if ((*cb
)(dst
, buffer
, (int)(bufptr
- buffer
)) < 0)
2047 DEBUG_puts("ippWriteIO: Could not write IPP attribute...");
2057 * Arrays and sets are done by sending additional
2058 * values with a zero-length name...
2061 *bufptr
++ = attr
->value_tag
;
2067 * Date values consist of a 2-byte length and an
2068 * 11-byte date/time structure defined by RFC 1903.
2070 * Put the 2-byte length and 11-byte date/time
2071 * structure in the buffer.
2076 memcpy(bufptr
, value
->date
, 11);
2081 case IPP_TAG_RESOLUTION
:
2082 for (i
= 0, value
= attr
->values
;
2083 i
< attr
->num_values
;
2086 if ((sizeof(buffer
) - (bufptr
- buffer
)) < 14)
2088 if ((*cb
)(dst
, buffer
, (int)(bufptr
- buffer
)) < 0)
2090 DEBUG_puts("ippWriteIO: Could not write IPP attribute...");
2100 * Arrays and sets are done by sending additional
2101 * values with a zero-length name...
2104 *bufptr
++ = attr
->value_tag
;
2110 * Resolution values consist of a 2-byte length,
2111 * 4-byte horizontal resolution value, 4-byte vertical
2112 * resolution value, and a 1-byte units value.
2114 * Put the 2-byte length and resolution value data
2120 *bufptr
++ = value
->resolution
.xres
>> 24;
2121 *bufptr
++ = value
->resolution
.xres
>> 16;
2122 *bufptr
++ = value
->resolution
.xres
>> 8;
2123 *bufptr
++ = value
->resolution
.xres
;
2124 *bufptr
++ = value
->resolution
.yres
>> 24;
2125 *bufptr
++ = value
->resolution
.yres
>> 16;
2126 *bufptr
++ = value
->resolution
.yres
>> 8;
2127 *bufptr
++ = value
->resolution
.yres
;
2128 *bufptr
++ = value
->resolution
.units
;
2132 case IPP_TAG_RANGE
:
2133 for (i
= 0, value
= attr
->values
;
2134 i
< attr
->num_values
;
2137 if ((sizeof(buffer
) - (bufptr
- buffer
)) < 13)
2139 if ((*cb
)(dst
, buffer
, (int)(bufptr
- buffer
)) < 0)
2141 DEBUG_puts("ippWriteIO: Could not write IPP attribute...");
2151 * Arrays and sets are done by sending additional
2152 * values with a zero-length name...
2155 *bufptr
++ = attr
->value_tag
;
2161 * Range values consist of a 2-byte length,
2162 * 4-byte lower value, and 4-byte upper value.
2164 * Put the 2-byte length and range value data
2170 *bufptr
++ = value
->range
.lower
>> 24;
2171 *bufptr
++ = value
->range
.lower
>> 16;
2172 *bufptr
++ = value
->range
.lower
>> 8;
2173 *bufptr
++ = value
->range
.lower
;
2174 *bufptr
++ = value
->range
.upper
>> 24;
2175 *bufptr
++ = value
->range
.upper
>> 16;
2176 *bufptr
++ = value
->range
.upper
>> 8;
2177 *bufptr
++ = value
->range
.upper
;
2181 case IPP_TAG_TEXTLANG
:
2182 case IPP_TAG_NAMELANG
:
2183 for (i
= 0, value
= attr
->values
;
2184 i
< attr
->num_values
;
2190 * Arrays and sets are done by sending additional
2191 * values with a zero-length name...
2194 if ((sizeof(buffer
) - (bufptr
- buffer
)) < 3)
2196 if ((*cb
)(dst
, buffer
, (int)(bufptr
- buffer
)) < 0)
2198 DEBUG_puts("ippWriteIO: Could not write IPP attribute...");
2205 *bufptr
++ = attr
->value_tag
;
2211 * textWithLanguage and nameWithLanguage values consist
2212 * of a 2-byte length for both strings and their
2213 * individual lengths, a 2-byte length for the
2214 * character string, the character string without the
2215 * trailing nul, a 2-byte length for the character
2216 * set string, and the character set string without
2222 if (value
->string
.charset
!= NULL
)
2223 n
+= (int)strlen(value
->string
.charset
);
2225 if (value
->string
.text
!= NULL
)
2226 n
+= (int)strlen(value
->string
.text
);
2228 if (n
> (sizeof(buffer
) - 2))
2231 if ((int)(sizeof(buffer
) - (bufptr
- buffer
)) < (n
+ 2))
2233 if ((*cb
)(dst
, buffer
, (int)(bufptr
- buffer
)) < 0)
2235 DEBUG_puts("ippWriteIO: Could not write IPP attribute...");
2242 /* Length of entire value */
2246 /* Length of charset */
2247 if (value
->string
.charset
!= NULL
)
2248 n
= (int)strlen(value
->string
.charset
);
2258 memcpy(bufptr
, value
->string
.charset
, n
);
2262 /* Length of text */
2263 if (value
->string
.text
!= NULL
)
2264 n
= (int)strlen(value
->string
.text
);
2274 memcpy(bufptr
, value
->string
.text
, n
);
2280 case IPP_TAG_BEGIN_COLLECTION
:
2281 for (i
= 0, value
= attr
->values
;
2282 i
< attr
->num_values
;
2286 * Collections are written with the begin-collection
2287 * tag first with a value of 0 length, followed by the
2288 * attributes in the collection, then the end-collection
2292 if ((sizeof(buffer
) - (bufptr
- buffer
)) < 5)
2294 if ((*cb
)(dst
, buffer
, (int)(bufptr
- buffer
)) < 0)
2296 DEBUG_puts("ippWriteIO: Could not write IPP attribute...");
2306 * Arrays and sets are done by sending additional
2307 * values with a zero-length name...
2310 *bufptr
++ = attr
->value_tag
;
2316 * Write a data length of 0 and flush the buffer...
2322 if ((*cb
)(dst
, buffer
, (int)(bufptr
- buffer
)) < 0)
2324 DEBUG_puts("ippWriteIO: Could not write IPP attribute...");
2331 * Then write the collection attribute...
2334 value
->collection
->state
= IPP_IDLE
;
2336 if (ippWriteIO(dst
, cb
, 1, ipp
, value
->collection
) == IPP_ERROR
)
2342 for (i
= 0, value
= attr
->values
;
2343 i
< attr
->num_values
;
2349 * Arrays and sets are done by sending additional
2350 * values with a zero-length name...
2353 if ((sizeof(buffer
) - (bufptr
- buffer
)) < 3)
2355 if ((*cb
)(dst
, buffer
, (int)(bufptr
- buffer
)) < 0)
2357 DEBUG_puts("ippWriteIO: Could not write IPP attribute...");
2364 *bufptr
++ = attr
->value_tag
;
2370 * An unknown value might some new value that a
2371 * vendor has come up with. It consists of a
2372 * 2-byte length and the bytes in the unknown
2376 n
= value
->unknown
.length
;
2378 if (n
> (sizeof(buffer
) - 2))
2381 if ((int)(sizeof(buffer
) - (bufptr
- buffer
)) < (n
+ 2))
2383 if ((*cb
)(dst
, buffer
, (int)(bufptr
- buffer
)) < 0)
2385 DEBUG_puts("ippWriteIO: Could not write IPP attribute...");
2392 /* Length of unknown value */
2399 memcpy(bufptr
, value
->unknown
.data
, n
);
2407 * Write the data out...
2410 if ((*cb
)(dst
, buffer
, (int)(bufptr
- buffer
)) < 0)
2412 DEBUG_puts("ippWriteIO: Could not write IPP attribute...");
2416 DEBUG_printf(("ippWriteIO: wrote %d bytes\n", bufptr
- buffer
));
2419 * If blocking is disabled, stop here...
2426 if (ipp
->current
== NULL
)
2429 * Done with all of the attributes; add the end-of-attributes
2430 * tag or end-collection attribute...
2435 buffer
[0] = IPP_TAG_END
;
2440 buffer
[0] = IPP_TAG_END_COLLECTION
;
2441 buffer
[1] = 0; /* empty name */
2443 buffer
[3] = 0; /* empty value */
2448 if ((*cb
)(dst
, buffer
, n
) < 0)
2450 DEBUG_puts("ippWriteIO: Could not write IPP end-tag...");
2454 ipp
->state
= IPP_DATA
;
2462 break; /* anti-compiler-warning-code */
2465 return (ipp
->state
);
2470 * '_ippAddAttr()' - Add a new attribute to the request.
2473 ipp_attribute_t
* /* O - New attribute */
2474 _ippAddAttr(ipp_t
*ipp
, /* I - IPP message */
2475 int num_values
) /* I - Number of values */
2477 ipp_attribute_t
*attr
; /* New attribute */
2480 DEBUG_printf(("_ippAddAttr(%p, %d)\n", ipp
, num_values
));
2482 if (ipp
== NULL
|| num_values
< 0)
2485 attr
= calloc(sizeof(ipp_attribute_t
) +
2486 (num_values
- 1) * sizeof(ipp_value_t
), 1);
2490 attr
->num_values
= num_values
;
2492 if (ipp
->last
== NULL
)
2495 ipp
->last
->next
= attr
;
2500 DEBUG_printf(("_ippAddAttr(): %p\n", attr
));
2507 * '_ippFreeAttr()' - Free an attribute.
2511 _ippFreeAttr(ipp_attribute_t
*attr
) /* I - Attribute to free */
2513 int i
; /* Looping var */
2514 ipp_value_t
*value
; /* Current value */
2517 DEBUG_printf(("_ippFreeAttr(): %p\n", attr
));
2519 switch (attr
->value_tag
)
2523 case IPP_TAG_KEYWORD
:
2524 case IPP_TAG_STRING
:
2526 case IPP_TAG_URISCHEME
:
2527 case IPP_TAG_CHARSET
:
2528 case IPP_TAG_LANGUAGE
:
2529 case IPP_TAG_MIMETYPE
:
2530 for (i
= 0, value
= attr
->values
;
2531 i
< attr
->num_values
;
2533 _cupsStrFree(value
->string
.text
);
2536 case IPP_TAG_TEXTLANG
:
2537 case IPP_TAG_NAMELANG
:
2538 for (i
= 0, value
= attr
->values
;
2539 i
< attr
->num_values
;
2542 if (value
->string
.charset
&& i
== 0)
2543 _cupsStrFree(value
->string
.charset
);
2544 _cupsStrFree(value
->string
.text
);
2548 case IPP_TAG_INTEGER
:
2550 case IPP_TAG_BOOLEAN
:
2552 case IPP_TAG_RESOLUTION
:
2553 case IPP_TAG_RANGE
:
2556 case IPP_TAG_BEGIN_COLLECTION
:
2557 for (i
= 0, value
= attr
->values
;
2558 i
< attr
->num_values
;
2560 ippDelete(value
->collection
);
2564 if (!((int)attr
->value_tag
& IPP_TAG_COPY
))
2566 for (i
= 0, value
= attr
->values
;
2567 i
< attr
->num_values
;
2569 if (value
->unknown
.data
)
2570 free(value
->unknown
.data
);
2576 _cupsStrFree(attr
->name
);
2583 * 'ipp_length()' - Compute the length of an IPP message or collection value.
2586 static size_t /* O - Size of IPP message */
2587 ipp_length(ipp_t
*ipp
, /* I - IPP message or collection */
2588 int collection
) /* I - 1 if a collection, 0 otherwise */
2590 int i
; /* Looping var */
2591 int bytes
; /* Number of bytes */
2592 ipp_attribute_t
*attr
; /* Current attribute */
2593 ipp_tag_t group
; /* Current group */
2594 ipp_value_t
*value
; /* Current value */
2601 * Start with 8 bytes for the IPP message header...
2604 bytes
= collection
? 0 : 8;
2607 * Then add the lengths of each attribute...
2610 group
= IPP_TAG_ZERO
;
2612 for (attr
= ipp
->attrs
; attr
!= NULL
; attr
= attr
->next
)
2614 if (attr
->group_tag
!= group
&& !collection
)
2616 group
= attr
->group_tag
;
2617 if (group
== IPP_TAG_ZERO
)
2620 bytes
++; /* Group tag */
2626 DEBUG_printf(("attr->name = %s, attr->num_values = %d, bytes = %d\n",
2627 attr
->name
, attr
->num_values
, bytes
));
2629 bytes
+= (int)strlen(attr
->name
); /* Name */
2630 bytes
+= attr
->num_values
; /* Value tag for each value */
2631 bytes
+= 2 * attr
->num_values
; /* Name lengths */
2632 bytes
+= 2 * attr
->num_values
; /* Value lengths */
2635 bytes
+= 5; /* Add membername overhead */
2637 switch (attr
->value_tag
& ~IPP_TAG_COPY
)
2639 case IPP_TAG_INTEGER
:
2641 bytes
+= 4 * attr
->num_values
;
2644 case IPP_TAG_BOOLEAN
:
2645 bytes
+= attr
->num_values
;
2650 case IPP_TAG_KEYWORD
:
2651 case IPP_TAG_STRING
:
2653 case IPP_TAG_URISCHEME
:
2654 case IPP_TAG_CHARSET
:
2655 case IPP_TAG_LANGUAGE
:
2656 case IPP_TAG_MIMETYPE
:
2657 for (i
= 0, value
= attr
->values
;
2658 i
< attr
->num_values
;
2660 if (value
->string
.text
!= NULL
)
2661 bytes
+= (int)strlen(value
->string
.text
);
2665 bytes
+= 11 * attr
->num_values
;
2668 case IPP_TAG_RESOLUTION
:
2669 bytes
+= 9 * attr
->num_values
;
2672 case IPP_TAG_RANGE
:
2673 bytes
+= 8 * attr
->num_values
;
2676 case IPP_TAG_TEXTLANG
:
2677 case IPP_TAG_NAMELANG
:
2678 bytes
+= 4 * attr
->num_values
;/* Charset + text length */
2680 for (i
= 0, value
= attr
->values
;
2681 i
< attr
->num_values
;
2684 if (value
->string
.charset
!= NULL
)
2685 bytes
+= (int)strlen(value
->string
.charset
);
2687 if (value
->string
.text
!= NULL
)
2688 bytes
+= (int)strlen(value
->string
.text
);
2692 case IPP_TAG_BEGIN_COLLECTION
:
2693 for (i
= 0, value
= attr
->values
;
2694 i
< attr
->num_values
;
2696 bytes
+= (int)ipp_length(value
->collection
, 1);
2700 for (i
= 0, value
= attr
->values
;
2701 i
< attr
->num_values
;
2703 bytes
+= value
->unknown
.length
;
2709 * Finally, add 1 byte for the "end of attributes" tag or 5 bytes
2710 * for the "end of collection" tag and return...
2718 DEBUG_printf(("bytes = %d\n", bytes
));
2725 * 'ipp_read_http()' - Semi-blocking read on a HTTP connection...
2728 static ssize_t
/* O - Number of bytes read */
2729 ipp_read_http(http_t
*http
, /* I - Client connection */
2730 ipp_uchar_t
*buffer
, /* O - Buffer for data */
2731 size_t length
) /* I - Total length */
2733 int tbytes
, /* Total bytes read */
2734 bytes
; /* Bytes read this pass */
2735 char len
[32]; /* Length string */
2738 DEBUG_printf(("ipp_read_http(http=%p, buffer=%p, length=%d)\n",
2739 http
, buffer
, length
));
2742 * Loop until all bytes are read...
2745 for (tbytes
= 0, bytes
= 0; tbytes
< (int)length
; tbytes
+= bytes
, buffer
+= bytes
)
2747 DEBUG_printf(("tbytes = %d, http->state = %d\n", tbytes
, http
->state
));
2749 if (http
->state
== HTTP_WAITING
)
2752 if (http
->used
> 0 && http
->data_encoding
== HTTP_ENCODE_LENGTH
)
2755 * Do "fast read" from HTTP buffer directly...
2758 if (http
->used
> (int)(length
- tbytes
))
2759 bytes
= (int)(length
- tbytes
);
2764 buffer
[0] = http
->buffer
[0];
2766 memcpy(buffer
, http
->buffer
, bytes
);
2768 http
->used
-= bytes
;
2769 http
->data_remaining
-= bytes
;
2771 if (http
->data_remaining
<= INT_MAX
)
2772 http
->_data_remaining
= (int)http
->data_remaining
;
2774 http
->_data_remaining
= INT_MAX
;
2777 memmove(http
->buffer
, http
->buffer
+ bytes
, http
->used
);
2779 if (http
->data_remaining
== 0)
2781 if (http
->data_encoding
== HTTP_ENCODE_CHUNKED
)
2784 * Get the trailing CR LF after the chunk...
2787 if (!httpGets(len
, sizeof(len
), http
))
2791 if (http
->data_encoding
!= HTTP_ENCODE_CHUNKED
)
2793 if (http
->state
== HTTP_POST_RECV
)
2796 http
->state
= HTTP_WAITING
;
2803 * Wait a maximum of 1 second for data...
2806 if (!http
->blocking
)
2809 * Wait up to 10 seconds for more data on non-blocking sockets...
2812 if (!httpWait(http
, 10000))
2823 if ((bytes
= httpRead2(http
, (char *)buffer
, length
- tbytes
)) <= 0)
2829 * Return the number of bytes read...
2832 if (tbytes
== 0 && bytes
< 0)
2835 DEBUG_printf(("returning %d bytes...\n", tbytes
));
2842 * 'ipp_read_file()' - Read IPP data from a file.
2845 static ssize_t
/* O - Number of bytes read */
2846 ipp_read_file(int *fd
, /* I - File descriptor */
2847 ipp_uchar_t
*buffer
, /* O - Read buffer */
2848 size_t length
) /* I - Number of bytes to read */
2851 return ((ssize_t
)read(*fd
, buffer
, (unsigned)length
));
2853 return (read(*fd
, buffer
, length
));
2859 * 'ipp_write_file()' - Write IPP data to a file.
2862 static ssize_t
/* O - Number of bytes written */
2863 ipp_write_file(int *fd
, /* I - File descriptor */
2864 ipp_uchar_t
*buffer
, /* I - Data to write */
2865 size_t length
) /* I - Number of bytes to write */
2868 return ((ssize_t
)write(*fd
, buffer
, (unsigned)length
));
2870 return (write(*fd
, buffer
, length
));
2877 * The following symbol definitions are provided only for KDE
2878 * compatibility during the CUPS 1.2 testing period and will be
2879 * removed in a future release of CUPS. These are PRIVATE APIs
2880 * from CUPS 1.1.x that the KDE developers chose to use...
2883 ipp_attribute_t
* /* O - New attribute */
2884 _ipp_add_attr(ipp_t
*ipp
, /* I - IPP message */
2885 int num_values
) /* I - Number of values */
2887 return (_ippAddAttr(ipp
, num_values
));
2891 _ipp_free_attr(ipp_attribute_t
*attr
) /* I - Attribute to free */
2895 #endif /* __linux */
2899 * End of "$Id: ipp.c 6649 2007-07-11 21:46:42Z mike $".