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 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 attr
->values
[0].unknown
.data
= malloc(datalen
);
365 memcpy(attr
->values
[0].unknown
.data
, data
, datalen
);
369 * Return the new attribute...
377 * 'ippAddString()' - Add a language-encoded string to an IPP message.
380 ipp_attribute_t
* /* O - New attribute */
381 ippAddString(ipp_t
*ipp
, /* I - IPP message */
382 ipp_tag_t group
, /* I - IPP group */
383 ipp_tag_t type
, /* I - Type of attribute */
384 const char *name
, /* I - Name of attribute */
385 const char *charset
, /* I - Character set */
386 const char *value
) /* I - Value */
388 ipp_attribute_t
*attr
; /* New attribute */
389 char buffer
[1024], /* Language/charset value buffer */
390 *bufptr
; /* Pointer into buffer */
393 if (ipp
== NULL
|| name
== NULL
)
396 if ((attr
= _ippAddAttr(ipp
, 1)) == NULL
)
400 * Force value to be English for the POSIX locale...
403 if (type
== IPP_TAG_LANGUAGE
&& !strcasecmp(value
, "C"))
407 * Convert language values to lowercase and change _ to - as needed...
410 if ((type
== IPP_TAG_LANGUAGE
|| type
== IPP_TAG_CHARSET
) && value
)
412 strlcpy(buffer
, value
, sizeof(buffer
));
415 for (bufptr
= buffer
; *bufptr
; bufptr
++)
419 *bufptr
= tolower(*bufptr
& 255);
423 * Initialize the attribute data...
426 attr
->name
= _cupsStrAlloc(name
);
427 attr
->group_tag
= group
;
428 attr
->value_tag
= type
;
429 attr
->values
[0].string
.charset
= ((int)type
& IPP_TAG_COPY
) ? (char *)charset
:
430 charset
? _cupsStrAlloc(charset
) : NULL
;
431 attr
->values
[0].string
.text
= ((int)type
& IPP_TAG_COPY
) ? (char *)value
:
432 value
? _cupsStrAlloc(value
) : NULL
;
439 * 'ippAddStrings()' - Add language-encoded strings to an IPP message.
442 ipp_attribute_t
* /* O - New attribute */
444 ipp_t
*ipp
, /* I - IPP message */
445 ipp_tag_t group
, /* I - IPP group */
446 ipp_tag_t type
, /* I - Type of attribute */
447 const char *name
, /* I - Name of attribute */
448 int num_values
, /* I - Number of values */
449 const char *charset
, /* I - Character set */
450 const char * const *values
) /* I - Values */
452 int i
; /* Looping var */
453 ipp_attribute_t
*attr
; /* New attribute */
454 ipp_value_t
*value
; /* Current value */
457 if (ipp
== NULL
|| name
== NULL
|| num_values
< 1)
460 if ((attr
= _ippAddAttr(ipp
, num_values
)) == NULL
)
464 * Initialize the attribute data...
467 attr
->name
= _cupsStrAlloc(name
);
468 attr
->group_tag
= group
;
469 attr
->value_tag
= type
;
471 for (i
= 0, value
= attr
->values
;
476 value
->string
.charset
= ((int)type
& IPP_TAG_COPY
) ? (char *)charset
:
477 charset
? _cupsStrAlloc(charset
) : NULL
;
479 value
->string
.charset
= attr
->values
[0].string
.charset
;
484 * Force language to be English for the POSIX locale...
487 if (type
== IPP_TAG_LANGUAGE
&& !strcasecmp(values
[i
], "C"))
488 value
->string
.text
= ((int)type
& IPP_TAG_COPY
) ? "en" :
491 value
->string
.text
= ((int)type
& IPP_TAG_COPY
) ? (char *)values
[i
] :
492 _cupsStrAlloc(values
[i
]);
501 * 'ippAddRange()' - Add a range of values to an IPP message.
504 ipp_attribute_t
* /* O - New attribute */
505 ippAddRange(ipp_t
*ipp
, /* I - IPP message */
506 ipp_tag_t group
, /* I - IPP group */
507 const char *name
, /* I - Name of attribute */
508 int lower
, /* I - Lower value */
509 int upper
) /* I - Upper value */
511 ipp_attribute_t
*attr
; /* New attribute */
514 if (ipp
== NULL
|| name
== NULL
)
517 if ((attr
= _ippAddAttr(ipp
, 1)) == NULL
)
520 attr
->name
= _cupsStrAlloc(name
);
521 attr
->group_tag
= group
;
522 attr
->value_tag
= IPP_TAG_RANGE
;
523 attr
->values
[0].range
.lower
= lower
;
524 attr
->values
[0].range
.upper
= upper
;
531 * 'ippAddRanges()' - Add ranges of values to an IPP message.
534 ipp_attribute_t
* /* O - New attribute */
535 ippAddRanges(ipp_t
*ipp
, /* I - IPP message */
536 ipp_tag_t group
, /* I - IPP group */
537 const char *name
, /* I - Name of attribute */
538 int num_values
, /* I - Number of values */
539 const int *lower
, /* I - Lower values */
540 const int *upper
) /* I - Upper values */
542 int i
; /* Looping var */
543 ipp_attribute_t
*attr
; /* New attribute */
544 ipp_value_t
*value
; /* Current value */
547 if (ipp
== NULL
|| name
== NULL
|| num_values
< 1)
550 if ((attr
= _ippAddAttr(ipp
, num_values
)) == NULL
)
553 attr
->name
= _cupsStrAlloc(name
);
554 attr
->group_tag
= group
;
555 attr
->value_tag
= IPP_TAG_RANGE
;
557 if (lower
!= NULL
&& upper
!= NULL
)
558 for (i
= 0, value
= attr
->values
;
562 value
->range
.lower
= lower
[i
];
563 value
->range
.upper
= upper
[i
];
571 * 'ippAddResolution()' - Add a resolution value to an IPP message.
574 ipp_attribute_t
* /* O - New attribute */
575 ippAddResolution(ipp_t
*ipp
, /* I - IPP message */
576 ipp_tag_t group
, /* I - IPP group */
577 const char *name
, /* I - Name of attribute */
578 ipp_res_t units
, /* I - Units for resolution */
579 int xres
, /* I - X resolution */
580 int yres
) /* I - Y resolution */
582 ipp_attribute_t
*attr
; /* New attribute */
585 if (ipp
== NULL
|| name
== NULL
)
588 if ((attr
= _ippAddAttr(ipp
, 1)) == NULL
)
591 attr
->name
= _cupsStrAlloc(name
);
592 attr
->group_tag
= group
;
593 attr
->value_tag
= IPP_TAG_RESOLUTION
;
594 attr
->values
[0].resolution
.xres
= xres
;
595 attr
->values
[0].resolution
.yres
= yres
;
596 attr
->values
[0].resolution
.units
= units
;
603 * 'ippAddResolutions()' - Add resolution values to an IPP message.
606 ipp_attribute_t
* /* O - New attribute */
607 ippAddResolutions(ipp_t
*ipp
, /* I - IPP message */
608 ipp_tag_t group
, /* I - IPP group */
609 const char *name
, /* I - Name of attribute */
610 int num_values
,/* I - Number of values */
611 ipp_res_t units
, /* I - Units for resolution */
612 const int *xres
, /* I - X resolutions */
613 const int *yres
) /* I - Y resolutions */
615 int i
; /* Looping var */
616 ipp_attribute_t
*attr
; /* New attribute */
617 ipp_value_t
*value
; /* Current value */
620 if (ipp
== NULL
|| name
== NULL
|| num_values
< 1)
623 if ((attr
= _ippAddAttr(ipp
, num_values
)) == NULL
)
626 attr
->name
= _cupsStrAlloc(name
);
627 attr
->group_tag
= group
;
628 attr
->value_tag
= IPP_TAG_RESOLUTION
;
630 if (xres
!= NULL
&& yres
!= NULL
)
631 for (i
= 0, value
= attr
->values
;
635 value
->resolution
.xres
= xres
[i
];
636 value
->resolution
.yres
= yres
[i
];
637 value
->resolution
.units
= units
;
645 * 'ippAddSeparator()' - Add a group separator to an IPP message.
648 ipp_attribute_t
* /* O - New attribute */
649 ippAddSeparator(ipp_t
*ipp
) /* I - IPP message */
651 ipp_attribute_t
*attr
; /* New attribute */
654 DEBUG_printf(("ippAddSeparator(%p)\n", ipp
));
659 if ((attr
= _ippAddAttr(ipp
, 0)) == NULL
)
662 attr
->group_tag
= IPP_TAG_ZERO
;
663 attr
->value_tag
= IPP_TAG_ZERO
;
670 * 'ippDateToTime()' - Convert from RFC 1903 Date/Time format to UNIX time
674 time_t /* O - UNIX time value */
675 ippDateToTime(const ipp_uchar_t
*date
) /* I - RFC 1903 date info */
677 struct tm unixdate
; /* UNIX date/time info */
678 time_t t
; /* Computed time */
681 memset(&unixdate
, 0, sizeof(unixdate
));
684 * RFC-1903 date/time format is:
686 * Byte(s) Description
687 * ------- -----------
688 * 0-1 Year (0 to 65535)
692 * 5 Minutes (0 to 59)
693 * 6 Seconds (0 to 60, 60 = "leap second")
694 * 7 Deciseconds (0 to 9)
696 * 9 UTC hours (0 to 11)
697 * 10 UTC minutes (0 to 59)
700 unixdate
.tm_year
= ((date
[0] << 8) | date
[1]) - 1900;
701 unixdate
.tm_mon
= date
[2] - 1;
702 unixdate
.tm_mday
= date
[3];
703 unixdate
.tm_hour
= date
[4];
704 unixdate
.tm_min
= date
[5];
705 unixdate
.tm_sec
= date
[6];
707 t
= mktime(&unixdate
);
710 t
+= date
[9] * 3600 + date
[10] * 60;
712 t
-= date
[9] * 3600 + date
[10] * 60;
719 * 'ippDelete()' - Delete an IPP message.
723 ippDelete(ipp_t
*ipp
) /* I - IPP message */
725 ipp_attribute_t
*attr
, /* Current attribute */
726 *next
; /* Next attribute */
729 DEBUG_printf(("ippDelete(): %p\n", ipp
));
734 for (attr
= ipp
->attrs
; attr
!= NULL
; attr
= next
)
745 * 'ippDeleteAttribute()' - Delete a single attribute in an IPP message.
747 * @since CUPS 1.1.19@
752 ipp_t
*ipp
, /* I - IPP message */
753 ipp_attribute_t
*attr
) /* I - Attribute to delete */
755 ipp_attribute_t
*current
, /* Current attribute */
756 *prev
; /* Previous attribute */
760 * Find the attribute in the list...
763 for (current
= ipp
->attrs
, prev
= NULL
;
764 current
!= NULL
&& current
!= attr
;
765 prev
= current
, current
= current
->next
);
770 * Found it, remove the attribute from the list...
774 prev
->next
= current
->next
;
776 ipp
->attrs
= current
->next
;
778 if (current
== ipp
->last
)
782 * Free memory used by the attribute...
785 _ippFreeAttr(current
);
791 * 'ippFindAttribute()' - Find a named attribute in a request...
794 ipp_attribute_t
* /* O - Matching attribute */
795 ippFindAttribute(ipp_t
*ipp
, /* I - IPP message */
796 const char *name
, /* I - Name of attribute */
797 ipp_tag_t type
) /* I - Type of attribute */
799 DEBUG_printf(("ippFindAttribute(%p, \'%s\')\n", ipp
, name
));
801 if (ipp
== NULL
|| name
== NULL
)
805 * Reset the current pointer...
811 * Search for the attribute...
814 return (ippFindNextAttribute(ipp
, name
, type
));
819 * 'ippFindNextAttribute()' - Find the next named attribute in a request...
822 ipp_attribute_t
* /* O - Matching attribute */
823 ippFindNextAttribute(ipp_t
*ipp
, /* I - IPP message */
824 const char *name
, /* I - Name of attribute */
825 ipp_tag_t type
) /* I - Type of attribute */
827 ipp_attribute_t
*attr
; /* Current atttribute */
828 ipp_tag_t value_tag
; /* Value tag */
831 DEBUG_printf(("ippFindNextAttribute(%p, \'%s\')\n", ipp
, name
));
833 if (ipp
== NULL
|| name
== NULL
)
838 ipp
->prev
= ipp
->current
;
839 attr
= ipp
->current
->next
;
847 for (; attr
!= NULL
; ipp
->prev
= attr
, attr
= attr
->next
)
849 DEBUG_printf(("ippFindAttribute: attr = %p, name = \'%s\'\n", attr
,
852 value_tag
= (ipp_tag_t
)(attr
->value_tag
& IPP_TAG_MASK
);
854 if (attr
->name
!= NULL
&& strcasecmp(attr
->name
, name
) == 0 &&
855 (value_tag
== type
|| type
== IPP_TAG_ZERO
||
856 (value_tag
== IPP_TAG_TEXTLANG
&& type
== IPP_TAG_TEXT
) ||
857 (value_tag
== IPP_TAG_NAMELANG
&& type
== IPP_TAG_NAME
)))
873 * 'ippLength()' - Compute the length of an IPP message.
876 size_t /* O - Size of IPP message */
877 ippLength(ipp_t
*ipp
) /* I - IPP message */
879 return (ipp_length(ipp
, 0));
884 * 'ippNew()' - Allocate a new IPP message.
887 ipp_t
* /* O - New IPP message */
890 ipp_t
*temp
; /* New IPP message */
893 DEBUG_puts("ippNew()");
895 if ((temp
= (ipp_t
*)calloc(1, sizeof(ipp_t
))) != NULL
)
898 * Default to IPP 1.1...
901 temp
->request
.any
.version
[0] = 1;
902 temp
->request
.any
.version
[1] = 1;
905 DEBUG_printf(("ippNew: %p\n", temp
));
912 * 'ippNewRequest()' - Allocate a new IPP request message.
914 * The new request message is initialized with the attributes-charset and
915 * attributes-natural-language attributes added. The
916 * attributes-natural-language value is derived from the current locale.
921 ipp_t
* /* O - IPP request message */
922 ippNewRequest(ipp_op_t op
) /* I - Operation code */
924 ipp_t
*request
; /* IPP request message */
925 cups_lang_t
*language
; /* Current language localization */
929 * Create a new IPP message...
932 if ((request
= ippNew()) == NULL
)
936 * Set the operation and request ID...
939 request
->request
.op
.operation_id
= op
;
940 request
->request
.op
.request_id
= 1;
943 * Use UTF-8 as the character set...
946 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_CHARSET
,
947 "attributes-charset", NULL
, "utf-8");
950 * Get the language from the current locale...
953 language
= cupsLangDefault();
955 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_LANGUAGE
,
956 "attributes-natural-language", NULL
, language
->language
);
959 * Return the new request...
967 * 'ippRead()' - Read data for an IPP message from a HTTP connection.
970 ipp_state_t
/* O - Current state */
971 ippRead(http_t
*http
, /* I - HTTP connection */
972 ipp_t
*ipp
) /* I - IPP data */
974 DEBUG_printf(("ippRead(http=%p, ipp=%p), data_remaining=" CUPS_LLFMT
"\n",
975 http
, ipp
, CUPS_LLCAST (http
? http
->data_remaining
: -1)));
980 DEBUG_printf(("http->state = %d\n", http
->state
));
982 return (ippReadIO(http
, (ipp_iocb_t
)ipp_read_http
,
983 http
->blocking
|| http
->used
!= 0, NULL
, ipp
));
988 * 'ippReadFile()' - Read data for an IPP message from a file.
990 * @since CUPS 1.1.19@
993 ipp_state_t
/* O - Current state */
994 ippReadFile(int fd
, /* I - HTTP data */
995 ipp_t
*ipp
) /* I - IPP data */
997 DEBUG_printf(("ippReadFile(%d, %p)\n", fd
, ipp
));
999 return (ippReadIO(&fd
, (ipp_iocb_t
)ipp_read_file
, 1, NULL
, ipp
));
1004 * 'ippReadIO()' - Read data for an IPP message.
1009 ipp_state_t
/* O - Current state */
1010 ippReadIO(void *src
, /* I - Data source */
1011 ipp_iocb_t cb
, /* I - Read callback function */
1012 int blocking
, /* I - Use blocking IO? */
1013 ipp_t
*parent
, /* I - Parent request, if any */
1014 ipp_t
*ipp
) /* I - IPP data */
1016 int n
; /* Length of data */
1017 unsigned char buffer
[32768], /* Data buffer */
1018 string
[255], /* Small string buffer */
1019 *bufptr
; /* Pointer into buffer */
1020 ipp_attribute_t
*attr
; /* Current attribute */
1021 ipp_tag_t tag
; /* Current tag */
1022 ipp_tag_t value_tag
; /* Current value tag */
1023 ipp_value_t
*value
; /* Current value */
1026 DEBUG_printf(("ippReadIO(%p, %p, %d, %p, %p)\n", src
, cb
, blocking
,
1028 DEBUG_printf(("ippReadIO: ipp->state=%d\n", ipp
->state
));
1030 if (src
== NULL
|| ipp
== NULL
)
1036 ipp
->state
++; /* Avoid common problem... */
1042 * Get the request header...
1045 if ((n
= (*cb
)(src
, buffer
, 8)) < 8)
1047 DEBUG_printf(("ippReadIO: Unable to read header (%d bytes read)!\n", n
));
1052 * Verify the major version number...
1057 DEBUG_printf(("ippReadIO: version number (%d.%d) is bad.\n", buffer
[0],
1063 * Then copy the request header over...
1066 ipp
->request
.any
.version
[0] = buffer
[0];
1067 ipp
->request
.any
.version
[1] = buffer
[1];
1068 ipp
->request
.any
.op_status
= (buffer
[2] << 8) | buffer
[3];
1069 ipp
->request
.any
.request_id
= (((((buffer
[4] << 8) | buffer
[5]) << 8) |
1070 buffer
[6]) << 8) | buffer
[7];
1072 DEBUG_printf(("ippReadIO: version=%d.%d\n", buffer
[0], buffer
[1]));
1073 DEBUG_printf(("ippReadIO: op_status=%04x\n",
1074 ipp
->request
.any
.op_status
));
1075 DEBUG_printf(("ippReadIO: request_id=%d\n",
1076 ipp
->request
.any
.request_id
));
1079 ipp
->state
= IPP_ATTRIBUTE
;
1080 ipp
->current
= NULL
;
1081 ipp
->curtag
= IPP_TAG_ZERO
;
1082 ipp
->prev
= ipp
->last
;
1085 * If blocking is disabled, stop here...
1091 case IPP_ATTRIBUTE
:
1094 if ((*cb
)(src
, buffer
, 1) < 1)
1097 DEBUG_printf(("ippReadIO: ipp->current=%p, ipp->prev=%p\n",
1098 ipp
->current
, ipp
->prev
));
1101 * Read this attribute...
1104 tag
= (ipp_tag_t
)buffer
[0];
1106 if (tag
== IPP_TAG_END
)
1109 * No more attributes left...
1112 DEBUG_puts("ippReadIO: IPP_TAG_END!");
1114 ipp
->state
= IPP_DATA
;
1117 else if (tag
< IPP_TAG_UNSUPPORTED_VALUE
)
1120 * Group tag... Set the current group and continue...
1123 if (ipp
->curtag
== tag
)
1124 ipp
->prev
= ippAddSeparator(ipp
);
1125 else if (ipp
->current
)
1126 ipp
->prev
= ipp
->current
;
1129 ipp
->current
= NULL
;
1130 DEBUG_printf(("ippReadIO: group tag = %x, ipp->prev=%p\n", tag
,
1135 DEBUG_printf(("ippReadIO: value tag = %x\n", tag
));
1141 if ((*cb
)(src
, buffer
, 2) < 2)
1143 DEBUG_puts("ippReadIO: unable to read name length!");
1147 n
= (buffer
[0] << 8) | buffer
[1];
1149 if (n
> (sizeof(buffer
) - 1))
1151 DEBUG_printf(("ippReadIO: bad name length %d!\n", n
));
1155 DEBUG_printf(("ippReadIO: name length = %d\n", n
));
1157 if (n
== 0 && tag
!= IPP_TAG_MEMBERNAME
&&
1158 tag
!= IPP_TAG_END_COLLECTION
)
1161 * More values for current attribute...
1164 if (ipp
->current
== NULL
)
1167 attr
= ipp
->current
;
1168 value_tag
= (ipp_tag_t
)(attr
->value_tag
& IPP_TAG_MASK
);
1171 * Make sure we aren't adding a new value of a different
1175 if (value_tag
== IPP_TAG_ZERO
)
1178 * Setting the value of a collection member...
1181 attr
->value_tag
= tag
;
1183 else if (value_tag
== IPP_TAG_STRING
||
1184 (value_tag
>= IPP_TAG_TEXTLANG
&&
1185 value_tag
<= IPP_TAG_MIMETYPE
))
1188 * String values can sometimes come across in different
1189 * forms; accept sets of differing values...
1192 if (tag
!= IPP_TAG_STRING
&&
1193 (tag
< IPP_TAG_TEXTLANG
|| tag
> IPP_TAG_MIMETYPE
))
1196 else if (value_tag
!= tag
)
1200 * Finally, reallocate the attribute array as needed...
1203 if (attr
->num_values
== 1 ||
1204 (attr
->num_values
> 0 &&
1205 (attr
->num_values
& (IPP_MAX_VALUES
- 1)) == 0))
1207 ipp_attribute_t
*temp
; /* Pointer to new buffer */
1210 DEBUG_printf(("ippReadIO: reallocating for up to %d values...\n",
1211 attr
->num_values
+ IPP_MAX_VALUES
));
1214 * Reallocate memory...
1217 if ((temp
= realloc(attr
, sizeof(ipp_attribute_t
) +
1218 (attr
->num_values
+ IPP_MAX_VALUES
- 1) *
1219 sizeof(ipp_value_t
))) == NULL
)
1225 * Reset pointers in the list...
1229 ipp
->prev
->next
= temp
;
1233 attr
= ipp
->current
= ipp
->last
= temp
;
1237 else if (tag
== IPP_TAG_MEMBERNAME
)
1240 * Name must be length 0!
1245 DEBUG_puts("ippReadIO: member name not empty!");
1250 ipp
->prev
= ipp
->current
;
1252 attr
= ipp
->current
= _ippAddAttr(ipp
, 1);
1254 DEBUG_printf(("ippReadIO: membername, ipp->current=%p, ipp->prev=%p\n",
1255 ipp
->current
, ipp
->prev
));
1257 attr
->group_tag
= ipp
->curtag
;
1258 attr
->value_tag
= IPP_TAG_ZERO
;
1259 attr
->num_values
= 0;
1261 else if (tag
!= IPP_TAG_END_COLLECTION
)
1264 * New attribute; read the name and add it...
1267 if ((*cb
)(src
, buffer
, n
) < n
)
1269 DEBUG_puts("ippReadIO: unable to read name!");
1276 ipp
->prev
= ipp
->current
;
1278 attr
= ipp
->current
= _ippAddAttr(ipp
, 1);
1280 DEBUG_printf(("ippReadIO: name=\'%s\', ipp->current=%p, ipp->prev=%p\n",
1281 buffer
, ipp
->current
, ipp
->prev
));
1283 attr
->group_tag
= ipp
->curtag
;
1284 attr
->value_tag
= tag
;
1285 attr
->name
= _cupsStrAlloc((char *)buffer
);
1286 attr
->num_values
= 0;
1291 if (tag
!= IPP_TAG_END_COLLECTION
)
1292 value
= attr
->values
+ attr
->num_values
;
1296 if ((*cb
)(src
, buffer
, 2) < 2)
1298 DEBUG_puts("ippReadIO: unable to read value length!");
1302 n
= (buffer
[0] << 8) | buffer
[1];
1303 DEBUG_printf(("ippReadIO: value length = %d\n", n
));
1307 case IPP_TAG_INTEGER
:
1309 if ((*cb
)(src
, buffer
, 4) < 4)
1311 DEBUG_puts("ippReadIO: Unable to read integer value!");
1315 n
= (((((buffer
[0] << 8) | buffer
[1]) << 8) | buffer
[2]) << 8) |
1320 case IPP_TAG_BOOLEAN
:
1321 if ((*cb
)(src
, buffer
, 1) < 1)
1323 DEBUG_puts("ippReadIO: Unable to read boolean value!");
1327 value
->boolean
= buffer
[0];
1331 case IPP_TAG_KEYWORD
:
1332 case IPP_TAG_STRING
:
1334 case IPP_TAG_URISCHEME
:
1335 case IPP_TAG_CHARSET
:
1336 case IPP_TAG_LANGUAGE
:
1337 case IPP_TAG_MIMETYPE
:
1338 if ((*cb
)(src
, buffer
, n
) < n
)
1340 DEBUG_puts("ippReadIO: unable to read name!");
1345 value
->string
.text
= _cupsStrAlloc((char *)buffer
);
1346 DEBUG_printf(("ippReadIO: value = \'%s\'\n",
1347 value
->string
.text
));
1350 if ((*cb
)(src
, value
->date
, 11) < 11)
1352 DEBUG_puts("ippReadIO: Unable to date integer value!");
1356 case IPP_TAG_RESOLUTION
:
1357 if ((*cb
)(src
, buffer
, 9) < 9)
1359 DEBUG_puts("ippReadIO: Unable to read resolution value!");
1363 value
->resolution
.xres
=
1364 (((((buffer
[0] << 8) | buffer
[1]) << 8) | buffer
[2]) << 8) |
1366 value
->resolution
.yres
=
1367 (((((buffer
[4] << 8) | buffer
[5]) << 8) | buffer
[6]) << 8) |
1369 value
->resolution
.units
=
1370 (ipp_res_t
)buffer
[8];
1372 case IPP_TAG_RANGE
:
1373 if ((*cb
)(src
, buffer
, 8) < 8)
1375 DEBUG_puts("ippReadIO: Unable to read range value!");
1379 value
->range
.lower
=
1380 (((((buffer
[0] << 8) | buffer
[1]) << 8) | buffer
[2]) << 8) |
1382 value
->range
.upper
=
1383 (((((buffer
[4] << 8) | buffer
[5]) << 8) | buffer
[6]) << 8) |
1386 case IPP_TAG_TEXTLANG
:
1387 case IPP_TAG_NAMELANG
:
1388 if (n
> sizeof(buffer
) || n
< 4)
1390 DEBUG_printf(("ippReadIO: bad value length %d!\n", n
));
1394 if ((*cb
)(src
, buffer
, n
) < n
)
1396 DEBUG_puts("ippReadIO: Unable to read string w/language value!");
1403 * text-with-language and name-with-language are composite
1412 n
= (bufptr
[0] << 8) | bufptr
[1];
1414 if (n
>= sizeof(string
))
1416 memcpy(string
, bufptr
+ 2, sizeof(string
) - 1);
1417 string
[sizeof(string
) - 1] = '\0';
1421 memcpy(string
, bufptr
+ 2, n
);
1425 value
->string
.charset
= _cupsStrAlloc((char *)string
);
1428 n
= (bufptr
[0] << 8) | bufptr
[1];
1430 bufptr
[2 + n
] = '\0';
1431 value
->string
.text
= _cupsStrAlloc((char *)bufptr
+ 2);
1434 case IPP_TAG_BEGIN_COLLECTION
:
1436 * Oh, boy, here comes a collection value, so read it...
1439 value
->collection
= ippNew();
1443 DEBUG_puts("ippReadIO: begCollection tag with value length > 0!");
1447 if (ippReadIO(src
, cb
, 1, ipp
, value
->collection
) == IPP_ERROR
)
1449 DEBUG_puts("ippReadIO: Unable to read collection value!");
1454 case IPP_TAG_END_COLLECTION
:
1457 DEBUG_puts("ippReadIO: endCollection tag with value length > 0!");
1461 DEBUG_puts("ippReadIO: endCollection tag...");
1463 return (ipp
->state
= IPP_DATA
);
1465 case IPP_TAG_MEMBERNAME
:
1467 * The value the name of the member in the collection, which
1468 * we need to carry over...
1471 if ((*cb
)(src
, buffer
, n
) < n
)
1473 DEBUG_puts("ippReadIO: Unable to read member name value!");
1478 attr
->name
= _cupsStrAlloc((char *)buffer
);
1481 * Since collection members are encoded differently than
1482 * regular attributes, make sure we don't start with an
1486 attr
->num_values
--;
1488 DEBUG_printf(("ippReadIO: member name = \"%s\"\n", attr
->name
));
1491 default : /* Other unsupported values */
1492 value
->unknown
.length
= n
;
1495 value
->unknown
.data
= malloc(n
);
1496 if ((*cb
)(src
, value
->unknown
.data
, n
) < n
)
1498 DEBUG_puts("ippReadIO: Unable to read unsupported value!");
1503 value
->unknown
.data
= NULL
;
1507 attr
->num_values
++;
1510 * If blocking is disabled, stop here...
1522 break; /* anti-compiler-warning-code */
1525 DEBUG_printf(("ippReadIO: returning ipp->state=%d!\n", ipp
->state
));
1527 return (ipp
->state
);
1532 * 'ippTimeToDate()' - Convert from UNIX time to RFC 1903 format.
1535 const ipp_uchar_t
* /* O - RFC-1903 date/time data */
1536 ippTimeToDate(time_t t
) /* I - UNIX time value */
1538 struct tm
*unixdate
; /* UNIX unixdate/time info */
1539 ipp_uchar_t
*date
= _cupsGlobals()->ipp_date
;
1540 /* RFC-1903 date/time data */
1544 * RFC-1903 date/time format is:
1546 * Byte(s) Description
1547 * ------- -----------
1548 * 0-1 Year (0 to 65535)
1552 * 5 Minutes (0 to 59)
1553 * 6 Seconds (0 to 60, 60 = "leap second")
1554 * 7 Deciseconds (0 to 9)
1556 * 9 UTC hours (0 to 11)
1557 * 10 UTC minutes (0 to 59)
1560 unixdate
= gmtime(&t
);
1561 unixdate
->tm_year
+= 1900;
1563 date
[0] = unixdate
->tm_year
>> 8;
1564 date
[1] = unixdate
->tm_year
;
1565 date
[2] = unixdate
->tm_mon
+ 1;
1566 date
[3] = unixdate
->tm_mday
;
1567 date
[4] = unixdate
->tm_hour
;
1568 date
[5] = unixdate
->tm_min
;
1569 date
[6] = unixdate
->tm_sec
;
1580 * 'ippWrite()' - Write data for an IPP message to a HTTP connection.
1583 ipp_state_t
/* O - Current state */
1584 ippWrite(http_t
*http
, /* I - HTTP connection */
1585 ipp_t
*ipp
) /* I - IPP data */
1587 DEBUG_printf(("ippWrite(%p, %p)\n", http
, ipp
));
1592 return (ippWriteIO(http
, (ipp_iocb_t
)httpWrite2
,
1593 http
->blocking
, NULL
, ipp
));
1598 * 'ippWriteFile()' - Write data for an IPP message to a file.
1600 * @since CUPS 1.1.19@
1603 ipp_state_t
/* O - Current state */
1604 ippWriteFile(int fd
, /* I - HTTP data */
1605 ipp_t
*ipp
) /* I - IPP data */
1607 DEBUG_printf(("ippWriteFile(%d, %p)\n", fd
, ipp
));
1609 ipp
->state
= IPP_IDLE
;
1611 return (ippWriteIO(&fd
, (ipp_iocb_t
)ipp_write_file
, 1, NULL
, ipp
));
1616 * 'ippWriteIO()' - Write data for an IPP message.
1621 ipp_state_t
/* O - Current state */
1622 ippWriteIO(void *dst
, /* I - Destination */
1623 ipp_iocb_t cb
, /* I - Write callback function */
1624 int blocking
, /* I - Use blocking IO? */
1625 ipp_t
*parent
, /* I - Parent IPP message */
1626 ipp_t
*ipp
) /* I - IPP data */
1628 int i
; /* Looping var */
1629 int n
; /* Length of data */
1630 unsigned char buffer
[32768], /* Data buffer */
1631 *bufptr
; /* Pointer into buffer */
1632 ipp_attribute_t
*attr
; /* Current attribute */
1633 ipp_value_t
*value
; /* Current value */
1636 DEBUG_printf(("ippWriteIO(%p, %p, %d, %p, %p)\n", dst
, cb
, blocking
,
1639 if (dst
== NULL
|| ipp
== NULL
)
1645 ipp
->state
++; /* Avoid common problem... */
1651 * Send the request header:
1654 * Operation/Status Code = 2 bytes
1655 * Request ID = 4 bytes
1661 *bufptr
++ = ipp
->request
.any
.version
[0];
1662 *bufptr
++ = ipp
->request
.any
.version
[1];
1663 *bufptr
++ = ipp
->request
.any
.op_status
>> 8;
1664 *bufptr
++ = ipp
->request
.any
.op_status
;
1665 *bufptr
++ = ipp
->request
.any
.request_id
>> 24;
1666 *bufptr
++ = ipp
->request
.any
.request_id
>> 16;
1667 *bufptr
++ = ipp
->request
.any
.request_id
>> 8;
1668 *bufptr
++ = ipp
->request
.any
.request_id
;
1670 if ((*cb
)(dst
, buffer
, (int)(bufptr
- buffer
)) < 0)
1672 DEBUG_puts("ippWriteIO: Could not write IPP header...");
1678 * Reset the state engine to point to the first attribute
1679 * in the request/response, with no current group.
1682 ipp
->state
= IPP_ATTRIBUTE
;
1683 ipp
->current
= ipp
->attrs
;
1684 ipp
->curtag
= IPP_TAG_ZERO
;
1686 DEBUG_printf(("ippWriteIO: version=%d.%d\n", buffer
[0], buffer
[1]));
1687 DEBUG_printf(("ippWriteIO: op_status=%04x\n", ipp
->request
.any
.op_status
));
1688 DEBUG_printf(("ippWriteIO: request_id=%d\n", ipp
->request
.any
.request_id
));
1691 * If blocking is disabled, stop here...
1697 case IPP_ATTRIBUTE
:
1698 while (ipp
->current
!= NULL
)
1701 * Write this attribute...
1705 attr
= ipp
->current
;
1707 ipp
->current
= ipp
->current
->next
;
1709 if (ipp
->curtag
!= attr
->group_tag
&& parent
== NULL
)
1712 * Send a group tag byte...
1715 ipp
->curtag
= attr
->group_tag
;
1717 if (attr
->group_tag
== IPP_TAG_ZERO
)
1720 DEBUG_printf(("ippWriteIO: wrote group tag = %x\n", attr
->group_tag
));
1721 *bufptr
++ = attr
->group_tag
;
1723 else if (attr
->group_tag
== IPP_TAG_ZERO
)
1727 * Write the attribute tag and name. The current implementation
1728 * does not support the extension value tags above 0x7f, so all
1729 * value tags are 1 byte.
1731 * The attribute name length does not include the trailing nul
1732 * character in the source string.
1734 * Collection values (parent != NULL) are written differently...
1740 * Get the length of the attribute name, and make sure it won't
1741 * overflow the buffer...
1744 if ((n
= (int)strlen(attr
->name
)) > (sizeof(buffer
) - 4))
1748 * Write the value tag, name length, and name string...
1751 DEBUG_printf(("ippWriteIO: writing value tag = %x\n", attr
->value_tag
));
1752 DEBUG_printf(("ippWriteIO: writing name = %d, \'%s\'\n", n
, attr
->name
));
1754 *bufptr
++ = attr
->value_tag
;
1757 memcpy(bufptr
, attr
->name
, n
);
1763 * Get the length of the attribute name, and make sure it won't
1764 * overflow the buffer...
1767 if ((n
= (int)strlen(attr
->name
)) > (sizeof(buffer
) - 7))
1771 * Write the member name tag, name length, name string, value tag,
1772 * and empty name for the collection member attribute...
1775 DEBUG_printf(("ippWriteIO: writing value tag = %x\n",
1776 IPP_TAG_MEMBERNAME
));
1777 DEBUG_printf(("ippWriteIO: writing name = %d, \'%s\'\n", n
, attr
->name
));
1778 DEBUG_printf(("ippWriteIO: writing value tag = %x\n", attr
->value_tag
));
1779 DEBUG_puts("ippWriteIO: writing name = 0, \'\'\n");
1781 *bufptr
++ = IPP_TAG_MEMBERNAME
;
1786 memcpy(bufptr
, attr
->name
, n
);
1789 *bufptr
++ = attr
->value_tag
;
1795 * Now write the attribute value(s)...
1798 switch (attr
->value_tag
& ~IPP_TAG_COPY
)
1800 case IPP_TAG_INTEGER
:
1802 for (i
= 0, value
= attr
->values
;
1803 i
< attr
->num_values
;
1806 if ((sizeof(buffer
) - (bufptr
- buffer
)) < 9)
1808 if ((*cb
)(dst
, buffer
, (int)(bufptr
- buffer
)) < 0)
1810 DEBUG_puts("ippWriteIO: Could not write IPP attribute...");
1820 * Arrays and sets are done by sending additional
1821 * values with a zero-length name...
1824 *bufptr
++ = attr
->value_tag
;
1830 * Integers and enumerations are both 4-byte signed
1831 * (twos-complement) values.
1833 * Put the 2-byte length and 4-byte value into the buffer...
1838 *bufptr
++ = value
->integer
>> 24;
1839 *bufptr
++ = value
->integer
>> 16;
1840 *bufptr
++ = value
->integer
>> 8;
1841 *bufptr
++ = value
->integer
;
1845 case IPP_TAG_BOOLEAN
:
1846 for (i
= 0, value
= attr
->values
;
1847 i
< attr
->num_values
;
1850 if ((sizeof(buffer
) - (bufptr
- buffer
)) < 6)
1852 if ((*cb
)(dst
, buffer
, (int)(bufptr
- buffer
)) < 0)
1854 DEBUG_puts("ippWriteIO: Could not write IPP attribute...");
1864 * Arrays and sets are done by sending additional
1865 * values with a zero-length name...
1868 *bufptr
++ = attr
->value_tag
;
1874 * Boolean values are 1-byte; 0 = false, 1 = true.
1876 * Put the 2-byte length and 1-byte value into the buffer...
1881 *bufptr
++ = value
->boolean
;
1887 case IPP_TAG_KEYWORD
:
1888 case IPP_TAG_STRING
:
1890 case IPP_TAG_URISCHEME
:
1891 case IPP_TAG_CHARSET
:
1892 case IPP_TAG_LANGUAGE
:
1893 case IPP_TAG_MIMETYPE
:
1894 for (i
= 0, value
= attr
->values
;
1895 i
< attr
->num_values
;
1901 * Arrays and sets are done by sending additional
1902 * values with a zero-length name...
1905 DEBUG_printf(("ippWriteIO: writing value tag = %x\n",
1907 DEBUG_printf(("ippWriteIO: writing name = 0, \'\'\n"));
1909 if ((sizeof(buffer
) - (bufptr
- buffer
)) < 3)
1911 if ((*cb
)(dst
, buffer
, (int)(bufptr
- buffer
)) < 0)
1913 DEBUG_puts("ippWriteIO: Could not write IPP attribute...");
1920 *bufptr
++ = attr
->value_tag
;
1925 if (value
->string
.text
!= NULL
)
1926 n
= (int)strlen(value
->string
.text
);
1930 if (n
> (sizeof(buffer
) - 2))
1933 DEBUG_printf(("ippWriteIO: writing string = %d, \'%s\'\n", n
,
1934 value
->string
.text
));
1936 if ((int)(sizeof(buffer
) - (bufptr
- buffer
)) < (n
+ 2))
1938 if ((*cb
)(dst
, buffer
, (int)(bufptr
- buffer
)) < 0)
1940 DEBUG_puts("ippWriteIO: Could not write IPP attribute...");
1948 * All simple strings consist of the 2-byte length and
1949 * character data without the trailing nul normally found
1950 * in C strings. Also, strings cannot be longer than 32767
1951 * bytes since the 2-byte length is a signed (twos-complement)
1954 * Put the 2-byte length and string characters in the buffer.
1962 memcpy(bufptr
, value
->string
.text
, n
);
1969 for (i
= 0, value
= attr
->values
;
1970 i
< attr
->num_values
;
1973 if ((sizeof(buffer
) - (bufptr
- buffer
)) < 16)
1975 if ((*cb
)(dst
, buffer
, (int)(bufptr
- buffer
)) < 0)
1977 DEBUG_puts("ippWriteIO: Could not write IPP attribute...");
1987 * Arrays and sets are done by sending additional
1988 * values with a zero-length name...
1991 *bufptr
++ = attr
->value_tag
;
1997 * Date values consist of a 2-byte length and an
1998 * 11-byte date/time structure defined by RFC 1903.
2000 * Put the 2-byte length and 11-byte date/time
2001 * structure in the buffer.
2006 memcpy(bufptr
, value
->date
, 11);
2011 case IPP_TAG_RESOLUTION
:
2012 for (i
= 0, value
= attr
->values
;
2013 i
< attr
->num_values
;
2016 if ((sizeof(buffer
) - (bufptr
- buffer
)) < 14)
2018 if ((*cb
)(dst
, buffer
, (int)(bufptr
- buffer
)) < 0)
2020 DEBUG_puts("ippWriteIO: Could not write IPP attribute...");
2030 * Arrays and sets are done by sending additional
2031 * values with a zero-length name...
2034 *bufptr
++ = attr
->value_tag
;
2040 * Resolution values consist of a 2-byte length,
2041 * 4-byte horizontal resolution value, 4-byte vertical
2042 * resolution value, and a 1-byte units value.
2044 * Put the 2-byte length and resolution value data
2050 *bufptr
++ = value
->resolution
.xres
>> 24;
2051 *bufptr
++ = value
->resolution
.xres
>> 16;
2052 *bufptr
++ = value
->resolution
.xres
>> 8;
2053 *bufptr
++ = value
->resolution
.xres
;
2054 *bufptr
++ = value
->resolution
.yres
>> 24;
2055 *bufptr
++ = value
->resolution
.yres
>> 16;
2056 *bufptr
++ = value
->resolution
.yres
>> 8;
2057 *bufptr
++ = value
->resolution
.yres
;
2058 *bufptr
++ = value
->resolution
.units
;
2062 case IPP_TAG_RANGE
:
2063 for (i
= 0, value
= attr
->values
;
2064 i
< attr
->num_values
;
2067 if ((sizeof(buffer
) - (bufptr
- buffer
)) < 13)
2069 if ((*cb
)(dst
, buffer
, (int)(bufptr
- buffer
)) < 0)
2071 DEBUG_puts("ippWriteIO: Could not write IPP attribute...");
2081 * Arrays and sets are done by sending additional
2082 * values with a zero-length name...
2085 *bufptr
++ = attr
->value_tag
;
2091 * Range values consist of a 2-byte length,
2092 * 4-byte lower value, and 4-byte upper value.
2094 * Put the 2-byte length and range value data
2100 *bufptr
++ = value
->range
.lower
>> 24;
2101 *bufptr
++ = value
->range
.lower
>> 16;
2102 *bufptr
++ = value
->range
.lower
>> 8;
2103 *bufptr
++ = value
->range
.lower
;
2104 *bufptr
++ = value
->range
.upper
>> 24;
2105 *bufptr
++ = value
->range
.upper
>> 16;
2106 *bufptr
++ = value
->range
.upper
>> 8;
2107 *bufptr
++ = value
->range
.upper
;
2111 case IPP_TAG_TEXTLANG
:
2112 case IPP_TAG_NAMELANG
:
2113 for (i
= 0, value
= attr
->values
;
2114 i
< attr
->num_values
;
2120 * Arrays and sets are done by sending additional
2121 * values with a zero-length name...
2124 if ((sizeof(buffer
) - (bufptr
- buffer
)) < 3)
2126 if ((*cb
)(dst
, buffer
, (int)(bufptr
- buffer
)) < 0)
2128 DEBUG_puts("ippWriteIO: Could not write IPP attribute...");
2135 *bufptr
++ = attr
->value_tag
;
2141 * textWithLanguage and nameWithLanguage values consist
2142 * of a 2-byte length for both strings and their
2143 * individual lengths, a 2-byte length for the
2144 * character string, the character string without the
2145 * trailing nul, a 2-byte length for the character
2146 * set string, and the character set string without
2152 if (value
->string
.charset
!= NULL
)
2153 n
+= (int)strlen(value
->string
.charset
);
2155 if (value
->string
.text
!= NULL
)
2156 n
+= (int)strlen(value
->string
.text
);
2158 if (n
> (sizeof(buffer
) - 2))
2161 if ((int)(sizeof(buffer
) - (bufptr
- buffer
)) < (n
+ 2))
2163 if ((*cb
)(dst
, buffer
, (int)(bufptr
- buffer
)) < 0)
2165 DEBUG_puts("ippWriteIO: Could not write IPP attribute...");
2172 /* Length of entire value */
2176 /* Length of charset */
2177 if (value
->string
.charset
!= NULL
)
2178 n
= (int)strlen(value
->string
.charset
);
2188 memcpy(bufptr
, value
->string
.charset
, n
);
2192 /* Length of text */
2193 if (value
->string
.text
!= NULL
)
2194 n
= (int)strlen(value
->string
.text
);
2204 memcpy(bufptr
, value
->string
.text
, n
);
2210 case IPP_TAG_BEGIN_COLLECTION
:
2211 for (i
= 0, value
= attr
->values
;
2212 i
< attr
->num_values
;
2216 * Collections are written with the begin-collection
2217 * tag first with a value of 0 length, followed by the
2218 * attributes in the collection, then the end-collection
2222 if ((sizeof(buffer
) - (bufptr
- buffer
)) < 5)
2224 if ((*cb
)(dst
, buffer
, (int)(bufptr
- buffer
)) < 0)
2226 DEBUG_puts("ippWriteIO: Could not write IPP attribute...");
2236 * Arrays and sets are done by sending additional
2237 * values with a zero-length name...
2240 *bufptr
++ = attr
->value_tag
;
2246 * Write a data length of 0 and flush the buffer...
2252 if ((*cb
)(dst
, buffer
, (int)(bufptr
- buffer
)) < 0)
2254 DEBUG_puts("ippWriteIO: Could not write IPP attribute...");
2261 * Then write the collection attribute...
2264 value
->collection
->state
= IPP_IDLE
;
2266 if (ippWriteIO(dst
, cb
, 1, ipp
, value
->collection
) == IPP_ERROR
)
2272 for (i
= 0, value
= attr
->values
;
2273 i
< attr
->num_values
;
2279 * Arrays and sets are done by sending additional
2280 * values with a zero-length name...
2283 if ((sizeof(buffer
) - (bufptr
- buffer
)) < 3)
2285 if ((*cb
)(dst
, buffer
, (int)(bufptr
- buffer
)) < 0)
2287 DEBUG_puts("ippWriteIO: Could not write IPP attribute...");
2294 *bufptr
++ = attr
->value_tag
;
2300 * An unknown value might some new value that a
2301 * vendor has come up with. It consists of a
2302 * 2-byte length and the bytes in the unknown
2306 n
= value
->unknown
.length
;
2308 if (n
> (sizeof(buffer
) - 2))
2311 if ((int)(sizeof(buffer
) - (bufptr
- buffer
)) < (n
+ 2))
2313 if ((*cb
)(dst
, buffer
, (int)(bufptr
- buffer
)) < 0)
2315 DEBUG_puts("ippWriteIO: Could not write IPP attribute...");
2322 /* Length of unknown value */
2329 memcpy(bufptr
, value
->unknown
.data
, n
);
2337 * Write the data out...
2340 if ((*cb
)(dst
, buffer
, (int)(bufptr
- buffer
)) < 0)
2342 DEBUG_puts("ippWriteIO: Could not write IPP attribute...");
2346 DEBUG_printf(("ippWriteIO: wrote %d bytes\n", bufptr
- buffer
));
2349 * If blocking is disabled, stop here...
2356 if (ipp
->current
== NULL
)
2359 * Done with all of the attributes; add the end-of-attributes
2360 * tag or end-collection attribute...
2365 buffer
[0] = IPP_TAG_END
;
2370 buffer
[0] = IPP_TAG_END_COLLECTION
;
2371 buffer
[1] = 0; /* empty name */
2373 buffer
[3] = 0; /* empty value */
2378 if ((*cb
)(dst
, buffer
, n
) < 0)
2380 DEBUG_puts("ippWriteIO: Could not write IPP end-tag...");
2384 ipp
->state
= IPP_DATA
;
2392 break; /* anti-compiler-warning-code */
2395 return (ipp
->state
);
2400 * '_ippAddAttr()' - Add a new attribute to the request.
2403 ipp_attribute_t
* /* O - New attribute */
2404 _ippAddAttr(ipp_t
*ipp
, /* I - IPP message */
2405 int num_values
) /* I - Number of values */
2407 ipp_attribute_t
*attr
; /* New attribute */
2410 DEBUG_printf(("_ippAddAttr(%p, %d)\n", ipp
, num_values
));
2412 if (ipp
== NULL
|| num_values
< 0)
2415 attr
= calloc(sizeof(ipp_attribute_t
) +
2416 (num_values
- 1) * sizeof(ipp_value_t
), 1);
2420 attr
->num_values
= num_values
;
2422 if (ipp
->last
== NULL
)
2425 ipp
->last
->next
= attr
;
2430 DEBUG_printf(("_ippAddAttr(): %p\n", attr
));
2437 * '_ippFreeAttr()' - Free an attribute.
2441 _ippFreeAttr(ipp_attribute_t
*attr
) /* I - Attribute to free */
2443 int i
; /* Looping var */
2444 ipp_value_t
*value
; /* Current value */
2447 DEBUG_printf(("_ippFreeAttr(): %p\n", attr
));
2449 switch (attr
->value_tag
)
2453 case IPP_TAG_KEYWORD
:
2454 case IPP_TAG_STRING
:
2456 case IPP_TAG_URISCHEME
:
2457 case IPP_TAG_CHARSET
:
2458 case IPP_TAG_LANGUAGE
:
2459 case IPP_TAG_MIMETYPE
:
2460 for (i
= 0, value
= attr
->values
;
2461 i
< attr
->num_values
;
2463 _cupsStrFree(value
->string
.text
);
2466 case IPP_TAG_TEXTLANG
:
2467 case IPP_TAG_NAMELANG
:
2468 for (i
= 0, value
= attr
->values
;
2469 i
< attr
->num_values
;
2472 if (value
->string
.charset
&& i
== 0)
2473 _cupsStrFree(value
->string
.charset
);
2474 _cupsStrFree(value
->string
.text
);
2478 case IPP_TAG_INTEGER
:
2480 case IPP_TAG_BOOLEAN
:
2482 case IPP_TAG_RESOLUTION
:
2483 case IPP_TAG_RANGE
:
2486 case IPP_TAG_BEGIN_COLLECTION
:
2487 for (i
= 0, value
= attr
->values
;
2488 i
< attr
->num_values
;
2490 ippDelete(value
->collection
);
2494 if (!((int)attr
->value_tag
& IPP_TAG_COPY
))
2496 for (i
= 0, value
= attr
->values
;
2497 i
< attr
->num_values
;
2499 if (value
->unknown
.data
)
2500 free(value
->unknown
.data
);
2506 _cupsStrFree(attr
->name
);
2513 * 'ipp_length()' - Compute the length of an IPP message or collection value.
2516 static size_t /* O - Size of IPP message */
2517 ipp_length(ipp_t
*ipp
, /* I - IPP message or collection */
2518 int collection
) /* I - 1 if a collection, 0 otherwise */
2520 int i
; /* Looping var */
2521 int bytes
; /* Number of bytes */
2522 ipp_attribute_t
*attr
; /* Current attribute */
2523 ipp_tag_t group
; /* Current group */
2524 ipp_value_t
*value
; /* Current value */
2531 * Start with 8 bytes for the IPP message header...
2534 bytes
= collection
? 0 : 8;
2537 * Then add the lengths of each attribute...
2540 group
= IPP_TAG_ZERO
;
2542 for (attr
= ipp
->attrs
; attr
!= NULL
; attr
= attr
->next
)
2544 if (attr
->group_tag
!= group
&& !collection
)
2546 group
= attr
->group_tag
;
2547 if (group
== IPP_TAG_ZERO
)
2550 bytes
++; /* Group tag */
2556 DEBUG_printf(("attr->name = %s, attr->num_values = %d, bytes = %d\n",
2557 attr
->name
, attr
->num_values
, bytes
));
2559 bytes
+= (int)strlen(attr
->name
); /* Name */
2560 bytes
+= attr
->num_values
; /* Value tag for each value */
2561 bytes
+= 2 * attr
->num_values
; /* Name lengths */
2562 bytes
+= 2 * attr
->num_values
; /* Value lengths */
2565 bytes
+= 5; /* Add membername overhead */
2567 switch (attr
->value_tag
& ~IPP_TAG_COPY
)
2569 case IPP_TAG_INTEGER
:
2571 bytes
+= 4 * attr
->num_values
;
2574 case IPP_TAG_BOOLEAN
:
2575 bytes
+= attr
->num_values
;
2580 case IPP_TAG_KEYWORD
:
2581 case IPP_TAG_STRING
:
2583 case IPP_TAG_URISCHEME
:
2584 case IPP_TAG_CHARSET
:
2585 case IPP_TAG_LANGUAGE
:
2586 case IPP_TAG_MIMETYPE
:
2587 for (i
= 0, value
= attr
->values
;
2588 i
< attr
->num_values
;
2590 if (value
->string
.text
!= NULL
)
2591 bytes
+= (int)strlen(value
->string
.text
);
2595 bytes
+= 11 * attr
->num_values
;
2598 case IPP_TAG_RESOLUTION
:
2599 bytes
+= 9 * attr
->num_values
;
2602 case IPP_TAG_RANGE
:
2603 bytes
+= 8 * attr
->num_values
;
2606 case IPP_TAG_TEXTLANG
:
2607 case IPP_TAG_NAMELANG
:
2608 bytes
+= 4 * attr
->num_values
;/* Charset + text length */
2610 for (i
= 0, value
= attr
->values
;
2611 i
< attr
->num_values
;
2614 if (value
->string
.charset
!= NULL
)
2615 bytes
+= (int)strlen(value
->string
.charset
);
2617 if (value
->string
.text
!= NULL
)
2618 bytes
+= (int)strlen(value
->string
.text
);
2622 case IPP_TAG_BEGIN_COLLECTION
:
2623 for (i
= 0, value
= attr
->values
;
2624 i
< attr
->num_values
;
2626 bytes
+= (int)ipp_length(value
->collection
, 1);
2630 for (i
= 0, value
= attr
->values
;
2631 i
< attr
->num_values
;
2633 bytes
+= value
->unknown
.length
;
2639 * Finally, add 1 byte for the "end of attributes" tag or 5 bytes
2640 * for the "end of collection" tag and return...
2648 DEBUG_printf(("bytes = %d\n", bytes
));
2655 * 'ipp_read_http()' - Semi-blocking read on a HTTP connection...
2658 static ssize_t
/* O - Number of bytes read */
2659 ipp_read_http(http_t
*http
, /* I - Client connection */
2660 ipp_uchar_t
*buffer
, /* O - Buffer for data */
2661 size_t length
) /* I - Total length */
2663 int tbytes
, /* Total bytes read */
2664 bytes
; /* Bytes read this pass */
2665 char len
[32]; /* Length string */
2668 DEBUG_printf(("ipp_read_http(http=%p, buffer=%p, length=%d)\n",
2669 http
, buffer
, length
));
2672 * Loop until all bytes are read...
2675 for (tbytes
= 0, bytes
= 0; tbytes
< (int)length
; tbytes
+= bytes
, buffer
+= bytes
)
2677 DEBUG_printf(("tbytes = %d, http->state = %d\n", tbytes
, http
->state
));
2679 if (http
->state
== HTTP_WAITING
)
2682 if (http
->used
> 0 && http
->data_encoding
== HTTP_ENCODE_LENGTH
)
2685 * Do "fast read" from HTTP buffer directly...
2688 if (http
->used
> (int)(length
- tbytes
))
2689 bytes
= (int)(length
- tbytes
);
2694 buffer
[0] = http
->buffer
[0];
2696 memcpy(buffer
, http
->buffer
, bytes
);
2698 http
->used
-= bytes
;
2699 http
->data_remaining
-= bytes
;
2701 if (http
->data_remaining
<= INT_MAX
)
2702 http
->_data_remaining
= (int)http
->data_remaining
;
2704 http
->_data_remaining
= INT_MAX
;
2707 memmove(http
->buffer
, http
->buffer
+ bytes
, http
->used
);
2709 if (http
->data_remaining
== 0)
2711 if (http
->data_encoding
== HTTP_ENCODE_CHUNKED
)
2714 * Get the trailing CR LF after the chunk...
2717 if (!httpGets(len
, sizeof(len
), http
))
2721 if (http
->data_encoding
!= HTTP_ENCODE_CHUNKED
)
2723 if (http
->state
== HTTP_POST_RECV
)
2726 http
->state
= HTTP_WAITING
;
2733 * Wait a maximum of 1 second for data...
2736 if (!http
->blocking
)
2739 * Wait up to 10 seconds for more data on non-blocking sockets...
2742 if (!httpWait(http
, 10000))
2753 if ((bytes
= httpRead2(http
, (char *)buffer
, length
- tbytes
)) <= 0)
2759 * Return the number of bytes read...
2762 if (tbytes
== 0 && bytes
< 0)
2765 DEBUG_printf(("returning %d bytes...\n", tbytes
));
2772 * 'ipp_read_file()' - Read IPP data from a file.
2775 static ssize_t
/* O - Number of bytes read */
2776 ipp_read_file(int *fd
, /* I - File descriptor */
2777 ipp_uchar_t
*buffer
, /* O - Read buffer */
2778 size_t length
) /* I - Number of bytes to read */
2781 return ((ssize_t
)read(*fd
, buffer
, (unsigned)length
));
2783 return (read(*fd
, buffer
, length
));
2789 * 'ipp_write_file()' - Write IPP data to a file.
2792 static ssize_t
/* O - Number of bytes written */
2793 ipp_write_file(int *fd
, /* I - File descriptor */
2794 ipp_uchar_t
*buffer
, /* I - Data to write */
2795 size_t length
) /* I - Number of bytes to write */
2798 return ((ssize_t
)write(*fd
, buffer
, (unsigned)length
));
2800 return (write(*fd
, buffer
, length
));
2807 * The following symbol definitions are provided only for KDE
2808 * compatibility during the CUPS 1.2 testing period and will be
2809 * removed in a future release of CUPS. These are PRIVATE APIs
2810 * from CUPS 1.1.x that the KDE developers chose to use...
2813 ipp_attribute_t
* /* O - New attribute */
2814 _ipp_add_attr(ipp_t
*ipp
, /* I - IPP message */
2815 int num_values
) /* I - Number of values */
2817 return (_ippAddAttr(ipp
, num_values
));
2821 _ipp_free_attr(ipp_attribute_t
*attr
) /* I - Attribute to free */
2825 #endif /* __linux */
2829 * End of "$Id: ipp.c 6649 2007-07-11 21:46:42Z mike $".