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_TEXTLANG
&&
1191 value_tag
<= IPP_TAG_MIMETYPE
)
1194 * String values can sometimes come across in different
1195 * forms; accept sets of differing values...
1198 if (tag
< IPP_TAG_TEXTLANG
|| tag
> IPP_TAG_MIMETYPE
)
1201 else if (value_tag
!= tag
)
1205 * Finally, reallocate the attribute array as needed...
1208 if (attr
->num_values
== 1 ||
1209 (attr
->num_values
> 0 &&
1210 (attr
->num_values
& (IPP_MAX_VALUES
- 1)) == 0))
1212 ipp_attribute_t
*temp
; /* Pointer to new buffer */
1215 DEBUG_printf(("ippReadIO: reallocating for up to %d values...\n",
1216 attr
->num_values
+ IPP_MAX_VALUES
));
1219 * Reallocate memory...
1222 if ((temp
= realloc(attr
, sizeof(ipp_attribute_t
) +
1223 (attr
->num_values
+ IPP_MAX_VALUES
- 1) *
1224 sizeof(ipp_value_t
))) == NULL
)
1230 * Reset pointers in the list...
1234 ipp
->prev
->next
= temp
;
1238 attr
= ipp
->current
= ipp
->last
= temp
;
1242 else if (tag
== IPP_TAG_MEMBERNAME
)
1245 * Name must be length 0!
1250 DEBUG_puts("ippReadIO: member name not empty!");
1255 ipp
->prev
= ipp
->current
;
1257 attr
= ipp
->current
= _ippAddAttr(ipp
, 1);
1259 DEBUG_printf(("ippReadIO: membername, ipp->current=%p, ipp->prev=%p\n",
1260 ipp
->current
, ipp
->prev
));
1262 attr
->group_tag
= ipp
->curtag
;
1263 attr
->value_tag
= IPP_TAG_ZERO
;
1264 attr
->num_values
= 0;
1266 else if (tag
!= IPP_TAG_END_COLLECTION
)
1269 * New attribute; read the name and add it...
1272 if ((*cb
)(src
, buffer
, n
) < n
)
1274 DEBUG_puts("ippReadIO: unable to read name!");
1281 ipp
->prev
= ipp
->current
;
1283 if ((attr
= ipp
->current
= _ippAddAttr(ipp
, 1)) == NULL
)
1285 DEBUG_puts("ippReadIO: unable to allocate attribute!");
1289 DEBUG_printf(("ippReadIO: name=\'%s\', ipp->current=%p, ipp->prev=%p\n",
1290 buffer
, ipp
->current
, ipp
->prev
));
1292 attr
->group_tag
= ipp
->curtag
;
1293 attr
->value_tag
= tag
;
1294 attr
->name
= _cupsStrAlloc((char *)buffer
);
1295 attr
->num_values
= 0;
1300 if (tag
!= IPP_TAG_END_COLLECTION
)
1301 value
= attr
->values
+ attr
->num_values
;
1305 if ((*cb
)(src
, buffer
, 2) < 2)
1307 DEBUG_puts("ippReadIO: unable to read value length!");
1311 n
= (buffer
[0] << 8) | buffer
[1];
1312 DEBUG_printf(("ippReadIO: value length = %d\n", n
));
1316 case IPP_TAG_INTEGER
:
1320 DEBUG_printf(("ippReadIO: bad value length %d!\n", n
));
1324 if ((*cb
)(src
, buffer
, 4) < 4)
1326 DEBUG_puts("ippReadIO: Unable to read integer value!");
1330 n
= (((((buffer
[0] << 8) | buffer
[1]) << 8) | buffer
[2]) << 8) |
1336 case IPP_TAG_BOOLEAN
:
1339 DEBUG_printf(("ippReadIO: bad value length %d!\n", n
));
1343 if ((*cb
)(src
, buffer
, 1) < 1)
1345 DEBUG_puts("ippReadIO: Unable to read boolean value!");
1349 value
->boolean
= buffer
[0];
1354 case IPP_TAG_KEYWORD
:
1356 case IPP_TAG_URISCHEME
:
1357 case IPP_TAG_CHARSET
:
1358 case IPP_TAG_LANGUAGE
:
1359 case IPP_TAG_MIMETYPE
:
1360 if (n
>= sizeof(buffer
))
1362 DEBUG_printf(("ippReadIO: bad value length %d!\n", n
));
1366 if ((*cb
)(src
, buffer
, n
) < n
)
1368 DEBUG_puts("ippReadIO: unable to read name!");
1373 value
->string
.text
= _cupsStrAlloc((char *)buffer
);
1374 DEBUG_printf(("ippReadIO: value = \'%s\'\n",
1375 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!");
1392 case IPP_TAG_RESOLUTION
:
1395 DEBUG_printf(("ippReadIO: bad value length %d!\n", n
));
1399 if ((*cb
)(src
, buffer
, 9) < 9)
1401 DEBUG_puts("ippReadIO: Unable to read resolution value!");
1405 value
->resolution
.xres
=
1406 (((((buffer
[0] << 8) | buffer
[1]) << 8) | buffer
[2]) << 8) |
1408 value
->resolution
.yres
=
1409 (((((buffer
[4] << 8) | buffer
[5]) << 8) | buffer
[6]) << 8) |
1411 value
->resolution
.units
=
1412 (ipp_res_t
)buffer
[8];
1415 case IPP_TAG_RANGE
:
1418 DEBUG_printf(("ippReadIO: bad value length %d!\n", n
));
1422 if ((*cb
)(src
, buffer
, 8) < 8)
1424 DEBUG_puts("ippReadIO: Unable to read range value!");
1428 value
->range
.lower
=
1429 (((((buffer
[0] << 8) | buffer
[1]) << 8) | buffer
[2]) << 8) |
1431 value
->range
.upper
=
1432 (((((buffer
[4] << 8) | buffer
[5]) << 8) | buffer
[6]) << 8) |
1436 case IPP_TAG_TEXTLANG
:
1437 case IPP_TAG_NAMELANG
:
1438 if (n
>= sizeof(buffer
) || n
< 4)
1440 DEBUG_printf(("ippReadIO: bad value length %d!\n", n
));
1444 if ((*cb
)(src
, buffer
, n
) < n
)
1446 DEBUG_puts("ippReadIO: Unable to read string w/language value!");
1453 * text-with-language and name-with-language are composite
1462 n
= (bufptr
[0] << 8) | bufptr
[1];
1464 if ((bufptr
+ 2 + n
) >= (buffer
+ sizeof(buffer
)) ||
1465 n
>= sizeof(string
))
1467 DEBUG_printf(("ippReadIO: bad value length %d!\n", n
));
1471 memcpy(string
, bufptr
+ 2, n
);
1474 value
->string
.charset
= _cupsStrAlloc((char *)string
);
1477 n
= (bufptr
[0] << 8) | bufptr
[1];
1479 if ((bufptr
+ 2 + n
) >= (buffer
+ sizeof(buffer
)))
1481 DEBUG_printf(("ippReadIO: bad value length %d!\n", n
));
1485 bufptr
[2 + n
] = '\0';
1486 value
->string
.text
= _cupsStrAlloc((char *)bufptr
+ 2);
1489 case IPP_TAG_BEGIN_COLLECTION
:
1491 * Oh, boy, here comes a collection value, so read it...
1494 value
->collection
= ippNew();
1498 DEBUG_puts("ippReadIO: begCollection tag with value length > 0!");
1502 if (ippReadIO(src
, cb
, 1, ipp
, value
->collection
) == IPP_ERROR
)
1504 DEBUG_puts("ippReadIO: Unable to read collection value!");
1509 case IPP_TAG_END_COLLECTION
:
1512 DEBUG_puts("ippReadIO: endCollection tag with value length > 0!");
1516 DEBUG_puts("ippReadIO: endCollection tag...");
1518 return (ipp
->state
= IPP_DATA
);
1520 case IPP_TAG_MEMBERNAME
:
1522 * The value the name of the member in the collection, which
1523 * we need to carry over...
1526 if (n
>= sizeof(buffer
))
1528 DEBUG_printf(("ippReadIO: bad value length %d!\n", n
));
1532 if ((*cb
)(src
, buffer
, n
) < n
)
1534 DEBUG_puts("ippReadIO: Unable to read member name value!");
1539 attr
->name
= _cupsStrAlloc((char *)buffer
);
1542 * Since collection members are encoded differently than
1543 * regular attributes, make sure we don't start with an
1547 attr
->num_values
--;
1549 DEBUG_printf(("ippReadIO: member name = \"%s\"\n", attr
->name
));
1552 default : /* Other unsupported values */
1553 if (n
> IPP_MAX_LENGTH
)
1555 DEBUG_printf(("ippReadIO: bad value length %d!\n", n
));
1559 value
->unknown
.length
= n
;
1562 if ((value
->unknown
.data
= malloc(n
)) == NULL
)
1564 DEBUG_puts("ippReadIO: Unable to allocate value");
1568 if ((*cb
)(src
, value
->unknown
.data
, n
) < n
)
1570 DEBUG_puts("ippReadIO: Unable to read unsupported value!");
1575 value
->unknown
.data
= NULL
;
1579 attr
->num_values
++;
1582 * If blocking is disabled, stop here...
1594 break; /* anti-compiler-warning-code */
1597 DEBUG_printf(("ippReadIO: returning ipp->state=%d!\n", ipp
->state
));
1599 return (ipp
->state
);
1604 * 'ippTimeToDate()' - Convert from UNIX time to RFC 1903 format.
1607 const ipp_uchar_t
* /* O - RFC-1903 date/time data */
1608 ippTimeToDate(time_t t
) /* I - UNIX time value */
1610 struct tm
*unixdate
; /* UNIX unixdate/time info */
1611 ipp_uchar_t
*date
= _cupsGlobals()->ipp_date
;
1612 /* RFC-1903 date/time data */
1616 * RFC-1903 date/time format is:
1618 * Byte(s) Description
1619 * ------- -----------
1620 * 0-1 Year (0 to 65535)
1624 * 5 Minutes (0 to 59)
1625 * 6 Seconds (0 to 60, 60 = "leap second")
1626 * 7 Deciseconds (0 to 9)
1628 * 9 UTC hours (0 to 11)
1629 * 10 UTC minutes (0 to 59)
1632 unixdate
= gmtime(&t
);
1633 unixdate
->tm_year
+= 1900;
1635 date
[0] = unixdate
->tm_year
>> 8;
1636 date
[1] = unixdate
->tm_year
;
1637 date
[2] = unixdate
->tm_mon
+ 1;
1638 date
[3] = unixdate
->tm_mday
;
1639 date
[4] = unixdate
->tm_hour
;
1640 date
[5] = unixdate
->tm_min
;
1641 date
[6] = unixdate
->tm_sec
;
1652 * 'ippWrite()' - Write data for an IPP message to a HTTP connection.
1655 ipp_state_t
/* O - Current state */
1656 ippWrite(http_t
*http
, /* I - HTTP connection */
1657 ipp_t
*ipp
) /* I - IPP data */
1659 DEBUG_printf(("ippWrite(%p, %p)\n", http
, ipp
));
1664 return (ippWriteIO(http
, (ipp_iocb_t
)httpWrite2
,
1665 http
->blocking
, NULL
, ipp
));
1670 * 'ippWriteFile()' - Write data for an IPP message to a file.
1672 * @since CUPS 1.1.19@
1675 ipp_state_t
/* O - Current state */
1676 ippWriteFile(int fd
, /* I - HTTP data */
1677 ipp_t
*ipp
) /* I - IPP data */
1679 DEBUG_printf(("ippWriteFile(%d, %p)\n", fd
, ipp
));
1681 ipp
->state
= IPP_IDLE
;
1683 return (ippWriteIO(&fd
, (ipp_iocb_t
)ipp_write_file
, 1, NULL
, ipp
));
1688 * 'ippWriteIO()' - Write data for an IPP message.
1693 ipp_state_t
/* O - Current state */
1694 ippWriteIO(void *dst
, /* I - Destination */
1695 ipp_iocb_t cb
, /* I - Write callback function */
1696 int blocking
, /* I - Use blocking IO? */
1697 ipp_t
*parent
, /* I - Parent IPP message */
1698 ipp_t
*ipp
) /* I - IPP data */
1700 int i
; /* Looping var */
1701 int n
; /* Length of data */
1702 unsigned char buffer
[IPP_MAX_LENGTH
+ 2],
1703 /* Data buffer + length bytes */
1704 *bufptr
; /* Pointer into buffer */
1705 ipp_attribute_t
*attr
; /* Current attribute */
1706 ipp_value_t
*value
; /* Current value */
1709 DEBUG_printf(("ippWriteIO(%p, %p, %d, %p, %p)\n", dst
, cb
, blocking
,
1712 if (dst
== NULL
|| ipp
== NULL
)
1718 ipp
->state
++; /* Avoid common problem... */
1724 * Send the request header:
1727 * Operation/Status Code = 2 bytes
1728 * Request ID = 4 bytes
1734 *bufptr
++ = ipp
->request
.any
.version
[0];
1735 *bufptr
++ = ipp
->request
.any
.version
[1];
1736 *bufptr
++ = ipp
->request
.any
.op_status
>> 8;
1737 *bufptr
++ = ipp
->request
.any
.op_status
;
1738 *bufptr
++ = ipp
->request
.any
.request_id
>> 24;
1739 *bufptr
++ = ipp
->request
.any
.request_id
>> 16;
1740 *bufptr
++ = ipp
->request
.any
.request_id
>> 8;
1741 *bufptr
++ = ipp
->request
.any
.request_id
;
1743 if ((*cb
)(dst
, buffer
, (int)(bufptr
- buffer
)) < 0)
1745 DEBUG_puts("ippWriteIO: Could not write IPP header...");
1751 * Reset the state engine to point to the first attribute
1752 * in the request/response, with no current group.
1755 ipp
->state
= IPP_ATTRIBUTE
;
1756 ipp
->current
= ipp
->attrs
;
1757 ipp
->curtag
= IPP_TAG_ZERO
;
1759 DEBUG_printf(("ippWriteIO: version=%d.%d\n", buffer
[0], buffer
[1]));
1760 DEBUG_printf(("ippWriteIO: op_status=%04x\n", ipp
->request
.any
.op_status
));
1761 DEBUG_printf(("ippWriteIO: request_id=%d\n", ipp
->request
.any
.request_id
));
1764 * If blocking is disabled, stop here...
1770 case IPP_ATTRIBUTE
:
1771 while (ipp
->current
!= NULL
)
1774 * Write this attribute...
1778 attr
= ipp
->current
;
1780 ipp
->current
= ipp
->current
->next
;
1782 if (ipp
->curtag
!= attr
->group_tag
&& parent
== NULL
)
1785 * Send a group tag byte...
1788 ipp
->curtag
= attr
->group_tag
;
1790 if (attr
->group_tag
== IPP_TAG_ZERO
)
1793 DEBUG_printf(("ippWriteIO: wrote group tag = %x\n", attr
->group_tag
));
1794 *bufptr
++ = attr
->group_tag
;
1796 else if (attr
->group_tag
== IPP_TAG_ZERO
)
1800 * Write the attribute tag and name. The current implementation
1801 * does not support the extension value tags above 0x7f, so all
1802 * value tags are 1 byte.
1804 * The attribute name length does not include the trailing nul
1805 * character in the source string.
1807 * Collection values (parent != NULL) are written differently...
1813 * Get the length of the attribute name, and make sure it won't
1814 * overflow the buffer...
1817 if ((n
= (int)strlen(attr
->name
)) > (sizeof(buffer
) - 4))
1821 * Write the value tag, name length, and name string...
1824 DEBUG_printf(("ippWriteIO: writing value tag = %x\n", attr
->value_tag
));
1825 DEBUG_printf(("ippWriteIO: writing name = %d, \'%s\'\n", n
, attr
->name
));
1827 *bufptr
++ = attr
->value_tag
;
1830 memcpy(bufptr
, attr
->name
, n
);
1836 * Get the length of the attribute name, and make sure it won't
1837 * overflow the buffer...
1840 if ((n
= (int)strlen(attr
->name
)) > (sizeof(buffer
) - 7))
1844 * Write the member name tag, name length, name string, value tag,
1845 * and empty name for the collection member attribute...
1848 DEBUG_printf(("ippWriteIO: writing value tag = %x\n",
1849 IPP_TAG_MEMBERNAME
));
1850 DEBUG_printf(("ippWriteIO: writing name = %d, \'%s\'\n", n
, attr
->name
));
1851 DEBUG_printf(("ippWriteIO: writing value tag = %x\n", attr
->value_tag
));
1852 DEBUG_puts("ippWriteIO: writing name = 0, \'\'\n");
1854 *bufptr
++ = IPP_TAG_MEMBERNAME
;
1859 memcpy(bufptr
, attr
->name
, n
);
1862 *bufptr
++ = attr
->value_tag
;
1868 * Now write the attribute value(s)...
1871 switch (attr
->value_tag
& ~IPP_TAG_COPY
)
1873 case IPP_TAG_INTEGER
:
1875 for (i
= 0, value
= attr
->values
;
1876 i
< attr
->num_values
;
1879 if ((sizeof(buffer
) - (bufptr
- buffer
)) < 9)
1881 if ((*cb
)(dst
, buffer
, (int)(bufptr
- buffer
)) < 0)
1883 DEBUG_puts("ippWriteIO: Could not write IPP attribute...");
1893 * Arrays and sets are done by sending additional
1894 * values with a zero-length name...
1897 *bufptr
++ = attr
->value_tag
;
1903 * Integers and enumerations are both 4-byte signed
1904 * (twos-complement) values.
1906 * Put the 2-byte length and 4-byte value into the buffer...
1911 *bufptr
++ = value
->integer
>> 24;
1912 *bufptr
++ = value
->integer
>> 16;
1913 *bufptr
++ = value
->integer
>> 8;
1914 *bufptr
++ = value
->integer
;
1918 case IPP_TAG_BOOLEAN
:
1919 for (i
= 0, value
= attr
->values
;
1920 i
< attr
->num_values
;
1923 if ((sizeof(buffer
) - (bufptr
- buffer
)) < 6)
1925 if ((*cb
)(dst
, buffer
, (int)(bufptr
- buffer
)) < 0)
1927 DEBUG_puts("ippWriteIO: Could not write IPP attribute...");
1937 * Arrays and sets are done by sending additional
1938 * values with a zero-length name...
1941 *bufptr
++ = attr
->value_tag
;
1947 * Boolean values are 1-byte; 0 = false, 1 = true.
1949 * Put the 2-byte length and 1-byte value into the buffer...
1954 *bufptr
++ = value
->boolean
;
1960 case IPP_TAG_KEYWORD
:
1962 case IPP_TAG_URISCHEME
:
1963 case IPP_TAG_CHARSET
:
1964 case IPP_TAG_LANGUAGE
:
1965 case IPP_TAG_MIMETYPE
:
1966 for (i
= 0, value
= attr
->values
;
1967 i
< attr
->num_values
;
1973 * Arrays and sets are done by sending additional
1974 * values with a zero-length name...
1977 DEBUG_printf(("ippWriteIO: writing value tag = %x\n",
1979 DEBUG_printf(("ippWriteIO: writing name = 0, \'\'\n"));
1981 if ((sizeof(buffer
) - (bufptr
- buffer
)) < 3)
1983 if ((*cb
)(dst
, buffer
, (int)(bufptr
- buffer
)) < 0)
1985 DEBUG_puts("ippWriteIO: Could not write IPP attribute...");
1992 *bufptr
++ = attr
->value_tag
;
1997 if (value
->string
.text
!= NULL
)
1998 n
= (int)strlen(value
->string
.text
);
2002 if (n
> (sizeof(buffer
) - 2))
2005 DEBUG_printf(("ippWriteIO: writing string = %d, \'%s\'\n", n
,
2006 value
->string
.text
));
2008 if ((int)(sizeof(buffer
) - (bufptr
- buffer
)) < (n
+ 2))
2010 if ((*cb
)(dst
, buffer
, (int)(bufptr
- buffer
)) < 0)
2012 DEBUG_puts("ippWriteIO: Could not write IPP attribute...");
2020 * All simple strings consist of the 2-byte length and
2021 * character data without the trailing nul normally found
2022 * in C strings. Also, strings cannot be longer than IPP_MAX_LENGTH
2023 * bytes since the 2-byte length is a signed (twos-complement)
2026 * Put the 2-byte length and string characters in the buffer.
2034 memcpy(bufptr
, value
->string
.text
, n
);
2041 for (i
= 0, value
= attr
->values
;
2042 i
< attr
->num_values
;
2045 if ((sizeof(buffer
) - (bufptr
- buffer
)) < 16)
2047 if ((*cb
)(dst
, buffer
, (int)(bufptr
- buffer
)) < 0)
2049 DEBUG_puts("ippWriteIO: Could not write IPP attribute...");
2059 * Arrays and sets are done by sending additional
2060 * values with a zero-length name...
2063 *bufptr
++ = attr
->value_tag
;
2069 * Date values consist of a 2-byte length and an
2070 * 11-byte date/time structure defined by RFC 1903.
2072 * Put the 2-byte length and 11-byte date/time
2073 * structure in the buffer.
2078 memcpy(bufptr
, value
->date
, 11);
2083 case IPP_TAG_RESOLUTION
:
2084 for (i
= 0, value
= attr
->values
;
2085 i
< attr
->num_values
;
2088 if ((sizeof(buffer
) - (bufptr
- buffer
)) < 14)
2090 if ((*cb
)(dst
, buffer
, (int)(bufptr
- buffer
)) < 0)
2092 DEBUG_puts("ippWriteIO: Could not write IPP attribute...");
2102 * Arrays and sets are done by sending additional
2103 * values with a zero-length name...
2106 *bufptr
++ = attr
->value_tag
;
2112 * Resolution values consist of a 2-byte length,
2113 * 4-byte horizontal resolution value, 4-byte vertical
2114 * resolution value, and a 1-byte units value.
2116 * Put the 2-byte length and resolution value data
2122 *bufptr
++ = value
->resolution
.xres
>> 24;
2123 *bufptr
++ = value
->resolution
.xres
>> 16;
2124 *bufptr
++ = value
->resolution
.xres
>> 8;
2125 *bufptr
++ = value
->resolution
.xres
;
2126 *bufptr
++ = value
->resolution
.yres
>> 24;
2127 *bufptr
++ = value
->resolution
.yres
>> 16;
2128 *bufptr
++ = value
->resolution
.yres
>> 8;
2129 *bufptr
++ = value
->resolution
.yres
;
2130 *bufptr
++ = value
->resolution
.units
;
2134 case IPP_TAG_RANGE
:
2135 for (i
= 0, value
= attr
->values
;
2136 i
< attr
->num_values
;
2139 if ((sizeof(buffer
) - (bufptr
- buffer
)) < 13)
2141 if ((*cb
)(dst
, buffer
, (int)(bufptr
- buffer
)) < 0)
2143 DEBUG_puts("ippWriteIO: Could not write IPP attribute...");
2153 * Arrays and sets are done by sending additional
2154 * values with a zero-length name...
2157 *bufptr
++ = attr
->value_tag
;
2163 * Range values consist of a 2-byte length,
2164 * 4-byte lower value, and 4-byte upper value.
2166 * Put the 2-byte length and range value data
2172 *bufptr
++ = value
->range
.lower
>> 24;
2173 *bufptr
++ = value
->range
.lower
>> 16;
2174 *bufptr
++ = value
->range
.lower
>> 8;
2175 *bufptr
++ = value
->range
.lower
;
2176 *bufptr
++ = value
->range
.upper
>> 24;
2177 *bufptr
++ = value
->range
.upper
>> 16;
2178 *bufptr
++ = value
->range
.upper
>> 8;
2179 *bufptr
++ = value
->range
.upper
;
2183 case IPP_TAG_TEXTLANG
:
2184 case IPP_TAG_NAMELANG
:
2185 for (i
= 0, value
= attr
->values
;
2186 i
< attr
->num_values
;
2192 * Arrays and sets are done by sending additional
2193 * values with a zero-length name...
2196 if ((sizeof(buffer
) - (bufptr
- buffer
)) < 3)
2198 if ((*cb
)(dst
, buffer
, (int)(bufptr
- buffer
)) < 0)
2200 DEBUG_puts("ippWriteIO: Could not write IPP attribute...");
2207 *bufptr
++ = attr
->value_tag
;
2213 * textWithLanguage and nameWithLanguage values consist
2214 * of a 2-byte length for both strings and their
2215 * individual lengths, a 2-byte length for the
2216 * character string, the character string without the
2217 * trailing nul, a 2-byte length for the character
2218 * set string, and the character set string without
2224 if (value
->string
.charset
!= NULL
)
2225 n
+= (int)strlen(value
->string
.charset
);
2227 if (value
->string
.text
!= NULL
)
2228 n
+= (int)strlen(value
->string
.text
);
2230 if (n
> (sizeof(buffer
) - 2))
2233 if ((int)(sizeof(buffer
) - (bufptr
- buffer
)) < (n
+ 2))
2235 if ((*cb
)(dst
, buffer
, (int)(bufptr
- buffer
)) < 0)
2237 DEBUG_puts("ippWriteIO: Could not write IPP attribute...");
2244 /* Length of entire value */
2248 /* Length of charset */
2249 if (value
->string
.charset
!= NULL
)
2250 n
= (int)strlen(value
->string
.charset
);
2260 memcpy(bufptr
, value
->string
.charset
, n
);
2264 /* Length of text */
2265 if (value
->string
.text
!= NULL
)
2266 n
= (int)strlen(value
->string
.text
);
2276 memcpy(bufptr
, value
->string
.text
, n
);
2282 case IPP_TAG_BEGIN_COLLECTION
:
2283 for (i
= 0, value
= attr
->values
;
2284 i
< attr
->num_values
;
2288 * Collections are written with the begin-collection
2289 * tag first with a value of 0 length, followed by the
2290 * attributes in the collection, then the end-collection
2294 if ((sizeof(buffer
) - (bufptr
- buffer
)) < 5)
2296 if ((*cb
)(dst
, buffer
, (int)(bufptr
- buffer
)) < 0)
2298 DEBUG_puts("ippWriteIO: Could not write IPP attribute...");
2308 * Arrays and sets are done by sending additional
2309 * values with a zero-length name...
2312 *bufptr
++ = attr
->value_tag
;
2318 * Write a data length of 0 and flush the buffer...
2324 if ((*cb
)(dst
, buffer
, (int)(bufptr
- buffer
)) < 0)
2326 DEBUG_puts("ippWriteIO: Could not write IPP attribute...");
2333 * Then write the collection attribute...
2336 value
->collection
->state
= IPP_IDLE
;
2338 if (ippWriteIO(dst
, cb
, 1, ipp
, value
->collection
) == IPP_ERROR
)
2344 for (i
= 0, value
= attr
->values
;
2345 i
< attr
->num_values
;
2351 * Arrays and sets are done by sending additional
2352 * values with a zero-length name...
2355 if ((sizeof(buffer
) - (bufptr
- buffer
)) < 3)
2357 if ((*cb
)(dst
, buffer
, (int)(bufptr
- buffer
)) < 0)
2359 DEBUG_puts("ippWriteIO: Could not write IPP attribute...");
2366 *bufptr
++ = attr
->value_tag
;
2372 * An unknown value might some new value that a
2373 * vendor has come up with. It consists of a
2374 * 2-byte length and the bytes in the unknown
2378 n
= value
->unknown
.length
;
2380 if (n
> (sizeof(buffer
) - 2))
2383 if ((int)(sizeof(buffer
) - (bufptr
- buffer
)) < (n
+ 2))
2385 if ((*cb
)(dst
, buffer
, (int)(bufptr
- buffer
)) < 0)
2387 DEBUG_puts("ippWriteIO: Could not write IPP attribute...");
2394 /* Length of unknown value */
2401 memcpy(bufptr
, value
->unknown
.data
, n
);
2409 * Write the data out...
2412 if ((*cb
)(dst
, buffer
, (int)(bufptr
- buffer
)) < 0)
2414 DEBUG_puts("ippWriteIO: Could not write IPP attribute...");
2418 DEBUG_printf(("ippWriteIO: wrote %d bytes\n", bufptr
- buffer
));
2421 * If blocking is disabled, stop here...
2428 if (ipp
->current
== NULL
)
2431 * Done with all of the attributes; add the end-of-attributes
2432 * tag or end-collection attribute...
2437 buffer
[0] = IPP_TAG_END
;
2442 buffer
[0] = IPP_TAG_END_COLLECTION
;
2443 buffer
[1] = 0; /* empty name */
2445 buffer
[3] = 0; /* empty value */
2450 if ((*cb
)(dst
, buffer
, n
) < 0)
2452 DEBUG_puts("ippWriteIO: Could not write IPP end-tag...");
2456 ipp
->state
= IPP_DATA
;
2464 break; /* anti-compiler-warning-code */
2467 return (ipp
->state
);
2472 * '_ippAddAttr()' - Add a new attribute to the request.
2475 ipp_attribute_t
* /* O - New attribute */
2476 _ippAddAttr(ipp_t
*ipp
, /* I - IPP message */
2477 int num_values
) /* I - Number of values */
2479 ipp_attribute_t
*attr
; /* New attribute */
2482 DEBUG_printf(("_ippAddAttr(%p, %d)\n", ipp
, num_values
));
2484 if (ipp
== NULL
|| num_values
< 0)
2487 attr
= calloc(sizeof(ipp_attribute_t
) +
2488 (num_values
- 1) * sizeof(ipp_value_t
), 1);
2492 attr
->num_values
= num_values
;
2494 if (ipp
->last
== NULL
)
2497 ipp
->last
->next
= attr
;
2502 DEBUG_printf(("_ippAddAttr(): %p\n", attr
));
2509 * '_ippFreeAttr()' - Free an attribute.
2513 _ippFreeAttr(ipp_attribute_t
*attr
) /* I - Attribute to free */
2515 int i
; /* Looping var */
2516 ipp_value_t
*value
; /* Current value */
2519 DEBUG_printf(("_ippFreeAttr(): %p\n", attr
));
2521 switch (attr
->value_tag
)
2525 case IPP_TAG_KEYWORD
:
2527 case IPP_TAG_URISCHEME
:
2528 case IPP_TAG_CHARSET
:
2529 case IPP_TAG_LANGUAGE
:
2530 case IPP_TAG_MIMETYPE
:
2531 for (i
= 0, value
= attr
->values
;
2532 i
< attr
->num_values
;
2534 _cupsStrFree(value
->string
.text
);
2537 case IPP_TAG_TEXTLANG
:
2538 case IPP_TAG_NAMELANG
:
2539 for (i
= 0, value
= attr
->values
;
2540 i
< attr
->num_values
;
2543 if (value
->string
.charset
&& i
== 0)
2544 _cupsStrFree(value
->string
.charset
);
2545 _cupsStrFree(value
->string
.text
);
2549 case IPP_TAG_INTEGER
:
2551 case IPP_TAG_BOOLEAN
:
2553 case IPP_TAG_RESOLUTION
:
2554 case IPP_TAG_RANGE
:
2557 case IPP_TAG_BEGIN_COLLECTION
:
2558 for (i
= 0, value
= attr
->values
;
2559 i
< attr
->num_values
;
2561 ippDelete(value
->collection
);
2564 case IPP_TAG_STRING
:
2565 for (i
= 0, value
= attr
->values
;
2566 i
< attr
->num_values
;
2568 free(value
->unknown
.data
);
2572 if (!((int)attr
->value_tag
& IPP_TAG_COPY
))
2574 for (i
= 0, value
= attr
->values
;
2575 i
< attr
->num_values
;
2577 if (value
->unknown
.data
)
2578 free(value
->unknown
.data
);
2584 _cupsStrFree(attr
->name
);
2591 * 'ipp_length()' - Compute the length of an IPP message or collection value.
2594 static size_t /* O - Size of IPP message */
2595 ipp_length(ipp_t
*ipp
, /* I - IPP message or collection */
2596 int collection
) /* I - 1 if a collection, 0 otherwise */
2598 int i
; /* Looping var */
2599 int bytes
; /* Number of bytes */
2600 ipp_attribute_t
*attr
; /* Current attribute */
2601 ipp_tag_t group
; /* Current group */
2602 ipp_value_t
*value
; /* Current value */
2609 * Start with 8 bytes for the IPP message header...
2612 bytes
= collection
? 0 : 8;
2615 * Then add the lengths of each attribute...
2618 group
= IPP_TAG_ZERO
;
2620 for (attr
= ipp
->attrs
; attr
!= NULL
; attr
= attr
->next
)
2622 if (attr
->group_tag
!= group
&& !collection
)
2624 group
= attr
->group_tag
;
2625 if (group
== IPP_TAG_ZERO
)
2628 bytes
++; /* Group tag */
2634 DEBUG_printf(("attr->name = %s, attr->num_values = %d, bytes = %d\n",
2635 attr
->name
, attr
->num_values
, bytes
));
2637 bytes
+= (int)strlen(attr
->name
); /* Name */
2638 bytes
+= attr
->num_values
; /* Value tag for each value */
2639 bytes
+= 2 * attr
->num_values
; /* Name lengths */
2640 bytes
+= 2 * attr
->num_values
; /* Value lengths */
2643 bytes
+= 5; /* Add membername overhead */
2645 switch (attr
->value_tag
& ~IPP_TAG_COPY
)
2647 case IPP_TAG_INTEGER
:
2649 bytes
+= 4 * attr
->num_values
;
2652 case IPP_TAG_BOOLEAN
:
2653 bytes
+= attr
->num_values
;
2658 case IPP_TAG_KEYWORD
:
2660 case IPP_TAG_URISCHEME
:
2661 case IPP_TAG_CHARSET
:
2662 case IPP_TAG_LANGUAGE
:
2663 case IPP_TAG_MIMETYPE
:
2664 for (i
= 0, value
= attr
->values
;
2665 i
< attr
->num_values
;
2667 if (value
->string
.text
!= NULL
)
2668 bytes
+= (int)strlen(value
->string
.text
);
2672 bytes
+= 11 * attr
->num_values
;
2675 case IPP_TAG_RESOLUTION
:
2676 bytes
+= 9 * attr
->num_values
;
2679 case IPP_TAG_RANGE
:
2680 bytes
+= 8 * attr
->num_values
;
2683 case IPP_TAG_TEXTLANG
:
2684 case IPP_TAG_NAMELANG
:
2685 bytes
+= 4 * attr
->num_values
;/* Charset + text length */
2687 for (i
= 0, value
= attr
->values
;
2688 i
< attr
->num_values
;
2691 if (value
->string
.charset
!= NULL
)
2692 bytes
+= (int)strlen(value
->string
.charset
);
2694 if (value
->string
.text
!= NULL
)
2695 bytes
+= (int)strlen(value
->string
.text
);
2699 case IPP_TAG_BEGIN_COLLECTION
:
2700 for (i
= 0, value
= attr
->values
;
2701 i
< attr
->num_values
;
2703 bytes
+= (int)ipp_length(value
->collection
, 1);
2707 for (i
= 0, value
= attr
->values
;
2708 i
< attr
->num_values
;
2710 bytes
+= value
->unknown
.length
;
2716 * Finally, add 1 byte for the "end of attributes" tag or 5 bytes
2717 * for the "end of collection" tag and return...
2725 DEBUG_printf(("bytes = %d\n", bytes
));
2732 * 'ipp_read_http()' - Semi-blocking read on a HTTP connection...
2735 static ssize_t
/* O - Number of bytes read */
2736 ipp_read_http(http_t
*http
, /* I - Client connection */
2737 ipp_uchar_t
*buffer
, /* O - Buffer for data */
2738 size_t length
) /* I - Total length */
2740 int tbytes
, /* Total bytes read */
2741 bytes
; /* Bytes read this pass */
2742 char len
[32]; /* Length string */
2745 DEBUG_printf(("ipp_read_http(http=%p, buffer=%p, length=%d)\n",
2746 http
, buffer
, length
));
2749 * Loop until all bytes are read...
2752 for (tbytes
= 0, bytes
= 0; tbytes
< (int)length
; tbytes
+= bytes
, buffer
+= bytes
)
2754 DEBUG_printf(("tbytes = %d, http->state = %d\n", tbytes
, http
->state
));
2756 if (http
->state
== HTTP_WAITING
)
2759 if (http
->used
> 0 && http
->data_encoding
== HTTP_ENCODE_LENGTH
)
2762 * Do "fast read" from HTTP buffer directly...
2765 if (http
->used
> (int)(length
- tbytes
))
2766 bytes
= (int)(length
- tbytes
);
2771 buffer
[0] = http
->buffer
[0];
2773 memcpy(buffer
, http
->buffer
, bytes
);
2775 http
->used
-= bytes
;
2776 http
->data_remaining
-= bytes
;
2778 if (http
->data_remaining
<= INT_MAX
)
2779 http
->_data_remaining
= (int)http
->data_remaining
;
2781 http
->_data_remaining
= INT_MAX
;
2784 memmove(http
->buffer
, http
->buffer
+ bytes
, http
->used
);
2786 if (http
->data_remaining
== 0)
2788 if (http
->data_encoding
== HTTP_ENCODE_CHUNKED
)
2791 * Get the trailing CR LF after the chunk...
2794 if (!httpGets(len
, sizeof(len
), http
))
2798 if (http
->data_encoding
!= HTTP_ENCODE_CHUNKED
)
2800 if (http
->state
== HTTP_POST_RECV
)
2803 http
->state
= HTTP_WAITING
;
2810 * Wait a maximum of 1 second for data...
2813 if (!http
->blocking
)
2816 * Wait up to 10 seconds for more data on non-blocking sockets...
2819 if (!httpWait(http
, 10000))
2830 if ((bytes
= httpRead2(http
, (char *)buffer
, length
- tbytes
)) <= 0)
2836 * Return the number of bytes read...
2839 if (tbytes
== 0 && bytes
< 0)
2842 DEBUG_printf(("returning %d bytes...\n", tbytes
));
2849 * 'ipp_read_file()' - Read IPP data from a file.
2852 static ssize_t
/* O - Number of bytes read */
2853 ipp_read_file(int *fd
, /* I - File descriptor */
2854 ipp_uchar_t
*buffer
, /* O - Read buffer */
2855 size_t length
) /* I - Number of bytes to read */
2858 return ((ssize_t
)read(*fd
, buffer
, (unsigned)length
));
2860 return (read(*fd
, buffer
, length
));
2866 * 'ipp_write_file()' - Write IPP data to a file.
2869 static ssize_t
/* O - Number of bytes written */
2870 ipp_write_file(int *fd
, /* I - File descriptor */
2871 ipp_uchar_t
*buffer
, /* I - Data to write */
2872 size_t length
) /* I - Number of bytes to write */
2875 return ((ssize_t
)write(*fd
, buffer
, (unsigned)length
));
2877 return (write(*fd
, buffer
, length
));
2884 * The following symbol definitions are provided only for KDE
2885 * compatibility during the CUPS 1.2 testing period and will be
2886 * removed in a future release of CUPS. These are PRIVATE APIs
2887 * from CUPS 1.1.x that the KDE developers chose to use...
2890 ipp_attribute_t
* /* O - New attribute */
2891 _ipp_add_attr(ipp_t
*ipp
, /* I - IPP message */
2892 int num_values
) /* I - Number of values */
2894 return (_ippAddAttr(ipp
, num_values
));
2898 _ipp_free_attr(ipp_attribute_t
*attr
) /* I - Attribute to free */
2902 #endif /* __linux */
2906 * End of "$Id: ipp.c 6649 2007-07-11 21:46:42Z mike $".