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