4 * Internet Printing Protocol support functions for the Common UNIX
5 * Printing System (CUPS).
7 * Copyright 1997-2006 by Easy Software Products, all rights reserved.
9 * These coded instructions, statements, and computer programs are the
10 * property of Easy Software Products and are protected by Federal
11 * copyright law. Distribution and use rights are outlined in the file
12 * "LICENSE.txt" which should have been included with this file. If this
13 * file is missing or damaged please contact Easy Software Products
16 * Attn: CUPS Licensing Information
17 * Easy Software Products
18 * 44141 Airport View Drive, Suite 204
19 * Hollywood, Maryland 20636 USA
21 * Voice: (301) 373-9600
22 * EMail: cups-info@cups.org
23 * WWW: http://www.cups.org
25 * This file is subject to the Apple OS-Developed Software exception.
29 * ippAddBoolean() - Add a boolean attribute to an IPP message.
30 * ippAddBooleans() - Add an array of boolean values.
31 * ippAddDate() - Add a date attribute to an IPP message.
32 * ippAddInteger() - Add a integer attribute to an IPP message.
33 * ippAddIntegers() - Add an array of integer values.
34 * ippAddOctetString() - Add an octetString value to an IPP message.
35 * ippAddString() - Add a language-encoded string to an IPP message.
36 * ippAddStrings() - Add language-encoded strings to an IPP message.
37 * ippAddRange() - Add a range of values to an IPP message.
38 * ippAddRanges() - Add ranges of values to an IPP message.
39 * ippAddResolution() - Add a resolution value to an IPP message.
40 * ippAddResolutions() - Add resolution values to an IPP message.
41 * ippAddSeparator() - Add a group separator to an IPP message.
42 * ippDateToTime() - Convert from RFC 1903 Date/Time format to
43 * UNIX time in seconds.
44 * ippDelete() - Delete an IPP message.
45 * ippDeleteAttribute() - Delete a single attribute in an IPP message.
46 * ippFindAttribute() - Find a named attribute in a request...
47 * ippFindNextAttribute() - Find the next named attribute in a request...
48 * ippLength() - Compute the length of an IPP message.
49 * ippNew() - Allocate a new IPP message.
50 * ippNewRequest() - Allocate a new IPP message.
51 * ippRead() - Read data for an IPP message from a HTTP
53 * ippReadFile() - Read data for an IPP message from a file.
54 * ippReadIO() - Read data for an IPP message.
55 * ippTimeToDate() - Convert from UNIX time to RFC 1903 format.
56 * ippWrite() - Write data for an IPP message to a HTTP
58 * ippWriteFile() - Write data for an IPP message to a file.
59 * ippWriteIO() - Write data for an IPP message.
60 * _ippAddAttr() - Add a new attribute to the request.
61 * _ippFreeAttr() - Free an attribute.
62 * ipp_length() - Compute the length of an IPP message or
64 * ipp_read_http() - Semi-blocking read on a HTTP connection...
65 * ipp_read_file() - Read IPP data from a file.
66 * ipp_write_file() - Write IPP data to a file.
70 * Include necessary headers...
73 #include "http-private.h"
87 static size_t ipp_length(ipp_t
*ipp
, int collection
);
88 static ssize_t
ipp_read_http(http_t
*http
, ipp_uchar_t
*buffer
,
90 static ssize_t
ipp_read_file(int *fd
, ipp_uchar_t
*buffer
,
92 static ssize_t
ipp_write_file(int *fd
, ipp_uchar_t
*buffer
,
97 * 'ippAddBoolean()' - Add a boolean attribute to an IPP message.
100 ipp_attribute_t
* /* O - New attribute */
101 ippAddBoolean(ipp_t
*ipp
, /* I - IPP message */
102 ipp_tag_t group
, /* I - IPP group */
103 const char *name
, /* I - Name of attribute */
104 char value
) /* I - Value of attribute */
106 ipp_attribute_t
*attr
; /* New attribute */
109 DEBUG_printf(("ippAddBoolean(%p, %02x, \'%s\', %d)\n", ipp
, group
, name
, value
));
111 if (ipp
== NULL
|| name
== NULL
)
114 if ((attr
= _ippAddAttr(ipp
, 1)) == NULL
)
117 attr
->name
= _cupsStrAlloc(name
);
118 attr
->group_tag
= group
;
119 attr
->value_tag
= IPP_TAG_BOOLEAN
;
120 attr
->values
[0].boolean
= value
;
127 * 'ippAddBooleans()' - Add an array of boolean values.
130 ipp_attribute_t
* /* O - New attribute */
131 ippAddBooleans(ipp_t
*ipp
, /* I - IPP message */
132 ipp_tag_t group
, /* I - IPP group */
133 const char *name
, /* I - Name of attribute */
134 int num_values
, /* I - Number of values */
135 const char *values
) /* I - Values */
137 int i
; /* Looping var */
138 ipp_attribute_t
*attr
; /* New attribute */
139 ipp_value_t
*value
; /* Current value */
142 DEBUG_printf(("ippAddBooleans(%p, %02x, \'%s\', %d, %p)\n", ipp
,
143 group
, name
, num_values
, values
));
145 if (ipp
== NULL
|| name
== NULL
|| num_values
< 1)
148 if ((attr
= _ippAddAttr(ipp
, num_values
)) == NULL
)
151 attr
->name
= _cupsStrAlloc(name
);
152 attr
->group_tag
= group
;
153 attr
->value_tag
= IPP_TAG_BOOLEAN
;
156 for (i
= 0, value
= attr
->values
;
159 value
->boolean
= values
[i
];
166 * 'ippAddCollection()' - Add a collection value.
168 * @since CUPS 1.1.19@
171 ipp_attribute_t
* /* O - New attribute */
172 ippAddCollection(ipp_t
*ipp
, /* I - IPP message */
173 ipp_tag_t group
, /* I - IPP group */
174 const char *name
, /* I - Name of attribute */
175 ipp_t
*value
) /* I - Value */
177 ipp_attribute_t
*attr
; /* New attribute */
180 DEBUG_printf(("ippAddCollection(%p, %02x, \'%s\', %p)\n", ipp
, group
, name
,
183 if (ipp
== NULL
|| name
== NULL
)
186 if ((attr
= _ippAddAttr(ipp
, 1)) == NULL
)
189 attr
->name
= _cupsStrAlloc(name
);
190 attr
->group_tag
= group
;
191 attr
->value_tag
= IPP_TAG_BEGIN_COLLECTION
;
192 attr
->values
[0].collection
= value
;
199 * 'ippAddCollections()' - Add an array of collection values.
201 * @since CUPS 1.1.19@
204 ipp_attribute_t
* /* O - New attribute */
206 ipp_t
*ipp
, /* I - IPP message */
207 ipp_tag_t group
, /* I - IPP group */
208 const char *name
, /* I - Name of attribute */
209 int num_values
, /* I - Number of values */
210 const ipp_t
**values
) /* I - Values */
212 int i
; /* Looping var */
213 ipp_attribute_t
*attr
; /* New attribute */
214 ipp_value_t
*value
; /* Current value */
217 DEBUG_printf(("ippAddCollections(%p, %02x, \'%s\', %d, %p)\n", ipp
,
218 group
, name
, num_values
, values
));
220 if (ipp
== NULL
|| name
== NULL
|| num_values
< 1)
223 if ((attr
= _ippAddAttr(ipp
, num_values
)) == NULL
)
226 attr
->name
= _cupsStrAlloc(name
);
227 attr
->group_tag
= group
;
228 attr
->value_tag
= IPP_TAG_BEGIN_COLLECTION
;
231 for (i
= 0, value
= attr
->values
;
234 value
->collection
= (ipp_t
*)values
[i
];
241 * 'ippAddDate()' - Add a date attribute to an IPP message.
244 ipp_attribute_t
* /* O - New attribute */
245 ippAddDate(ipp_t
*ipp
, /* I - IPP message */
246 ipp_tag_t group
, /* I - IPP group */
247 const char *name
, /* I - Name of attribute */
248 const ipp_uchar_t
*value
) /* I - Value */
250 ipp_attribute_t
*attr
; /* New attribute */
253 DEBUG_printf(("ippAddDate(%p, %02x, \'%s\', %p)\n", ipp
, group
, name
,
256 if (ipp
== NULL
|| name
== NULL
|| value
== NULL
)
259 if ((attr
= _ippAddAttr(ipp
, 1)) == NULL
)
262 attr
->name
= _cupsStrAlloc(name
);
263 attr
->group_tag
= group
;
264 attr
->value_tag
= IPP_TAG_DATE
;
265 memcpy(attr
->values
[0].date
, value
, 11);
272 * 'ippAddInteger()' - Add a integer attribute to an IPP message.
275 ipp_attribute_t
* /* O - New attribute */
276 ippAddInteger(ipp_t
*ipp
, /* I - IPP message */
277 ipp_tag_t group
, /* I - IPP group */
278 ipp_tag_t type
, /* I - Type of attribute */
279 const char *name
, /* I - Name of attribute */
280 int value
) /* I - Value of attribute */
282 ipp_attribute_t
*attr
; /* New attribute */
285 DEBUG_printf(("ippAddInteger(%p, %d, \'%s\', %d)\n", ipp
, group
, name
,
288 if (ipp
== NULL
|| name
== NULL
)
291 if ((attr
= _ippAddAttr(ipp
, 1)) == NULL
)
294 attr
->name
= _cupsStrAlloc(name
);
295 attr
->group_tag
= group
;
296 attr
->value_tag
= type
;
297 attr
->values
[0].integer
= value
;
304 * 'ippAddIntegers()' - Add an array of integer values.
307 ipp_attribute_t
* /* O - New attribute */
308 ippAddIntegers(ipp_t
*ipp
, /* I - IPP message */
309 ipp_tag_t group
, /* I - IPP group */
310 ipp_tag_t type
, /* I - Type of attribute */
311 const char *name
, /* I - Name of attribute */
312 int num_values
, /* I - Number of values */
313 const int *values
) /* I - Values */
315 int i
; /* Looping var */
316 ipp_attribute_t
*attr
; /* New attribute */
317 ipp_value_t
*value
; /* Current value */
320 if (ipp
== NULL
|| name
== NULL
|| num_values
< 1)
323 if ((attr
= _ippAddAttr(ipp
, num_values
)) == NULL
)
326 attr
->name
= _cupsStrAlloc(name
);
327 attr
->group_tag
= group
;
328 attr
->value_tag
= type
;
331 for (i
= 0, value
= attr
->values
;
334 value
->integer
= values
[i
];
341 * 'ippAddOctetString()' - Add an octetString value to an IPP message.
346 ipp_attribute_t
* /* O - New attribute */
347 ippAddOctetString(ipp_t
*ipp
, /* I - IPP message */
348 ipp_tag_t group
, /* I - IPP group */
349 const char *name
, /* I - Name of attribute */
350 const void *data
, /* I - octetString data */
351 int datalen
) /* I - Length of data in bytes */
353 ipp_attribute_t
*attr
; /* New attribute */
356 if (ipp
== NULL
|| name
== NULL
)
359 if ((attr
= _ippAddAttr(ipp
, 1)) == NULL
)
363 * Initialize the attribute data...
366 attr
->name
= _cupsStrAlloc(name
);
367 attr
->group_tag
= group
;
368 attr
->value_tag
= IPP_TAG_STRING
;
369 attr
->values
[0].unknown
.length
= datalen
;
373 attr
->values
[0].unknown
.data
= malloc(datalen
);
374 memcpy(attr
->values
[0].unknown
.data
, data
, datalen
);
378 * Return the new attribute...
386 * 'ippAddString()' - Add a language-encoded string to an IPP message.
389 ipp_attribute_t
* /* O - New attribute */
390 ippAddString(ipp_t
*ipp
, /* I - IPP message */
391 ipp_tag_t group
, /* I - IPP group */
392 ipp_tag_t type
, /* I - Type of attribute */
393 const char *name
, /* I - Name of attribute */
394 const char *charset
, /* I - Character set */
395 const char *value
) /* I - Value */
397 ipp_attribute_t
*attr
; /* New attribute */
398 char buffer
[1024], /* Language/charset value buffer */
399 *bufptr
; /* Pointer into buffer */
402 if (ipp
== NULL
|| name
== NULL
)
405 if ((attr
= _ippAddAttr(ipp
, 1)) == NULL
)
409 * Force value to be English for the POSIX locale...
412 if (type
== IPP_TAG_LANGUAGE
&& !strcasecmp(value
, "C"))
416 * Convert language values to lowercase and change _ to - as needed...
419 if ((type
== IPP_TAG_LANGUAGE
|| type
== IPP_TAG_CHARSET
) && value
)
421 strlcpy(buffer
, value
, sizeof(buffer
));
424 for (bufptr
= buffer
; *bufptr
; bufptr
++)
428 *bufptr
= tolower(*bufptr
& 255);
432 * Initialize the attribute data...
435 attr
->name
= _cupsStrAlloc(name
);
436 attr
->group_tag
= group
;
437 attr
->value_tag
= type
;
438 attr
->values
[0].string
.charset
= ((int)type
& IPP_TAG_COPY
) ? (char *)charset
:
439 charset
? _cupsStrAlloc(charset
) : NULL
;
440 attr
->values
[0].string
.text
= ((int)type
& IPP_TAG_COPY
) ? (char *)value
:
441 value
? _cupsStrAlloc(value
) : NULL
;
448 * 'ippAddStrings()' - Add language-encoded strings to an IPP message.
451 ipp_attribute_t
* /* O - New attribute */
453 ipp_t
*ipp
, /* I - IPP message */
454 ipp_tag_t group
, /* I - IPP group */
455 ipp_tag_t type
, /* I - Type of attribute */
456 const char *name
, /* I - Name of attribute */
457 int num_values
, /* I - Number of values */
458 const char *charset
, /* I - Character set */
459 const char * const *values
) /* I - Values */
461 int i
; /* Looping var */
462 ipp_attribute_t
*attr
; /* New attribute */
463 ipp_value_t
*value
; /* Current value */
466 if (ipp
== NULL
|| name
== NULL
|| num_values
< 1)
469 if ((attr
= _ippAddAttr(ipp
, num_values
)) == NULL
)
473 * Initialize the attribute data...
476 attr
->name
= _cupsStrAlloc(name
);
477 attr
->group_tag
= group
;
478 attr
->value_tag
= type
;
480 for (i
= 0, value
= attr
->values
;
485 value
->string
.charset
= ((int)type
& IPP_TAG_COPY
) ? (char *)charset
:
486 charset
? _cupsStrAlloc(charset
) : NULL
;
488 value
->string
.charset
= attr
->values
[0].string
.charset
;
493 * Force language to be English for the POSIX locale...
496 if (type
== IPP_TAG_LANGUAGE
&& !strcasecmp(values
[i
], "C"))
497 value
->string
.text
= ((int)type
& IPP_TAG_COPY
) ? "en" :
500 value
->string
.text
= ((int)type
& IPP_TAG_COPY
) ? (char *)values
[i
] :
501 _cupsStrAlloc(values
[i
]);
510 * 'ippAddRange()' - Add a range of values to an IPP message.
513 ipp_attribute_t
* /* O - New attribute */
514 ippAddRange(ipp_t
*ipp
, /* I - IPP message */
515 ipp_tag_t group
, /* I - IPP group */
516 const char *name
, /* I - Name of attribute */
517 int lower
, /* I - Lower value */
518 int upper
) /* I - Upper value */
520 ipp_attribute_t
*attr
; /* New attribute */
523 if (ipp
== NULL
|| name
== NULL
)
526 if ((attr
= _ippAddAttr(ipp
, 1)) == NULL
)
529 attr
->name
= _cupsStrAlloc(name
);
530 attr
->group_tag
= group
;
531 attr
->value_tag
= IPP_TAG_RANGE
;
532 attr
->values
[0].range
.lower
= lower
;
533 attr
->values
[0].range
.upper
= upper
;
540 * 'ippAddRanges()' - Add ranges of values to an IPP message.
543 ipp_attribute_t
* /* O - New attribute */
544 ippAddRanges(ipp_t
*ipp
, /* I - IPP message */
545 ipp_tag_t group
, /* I - IPP group */
546 const char *name
, /* I - Name of attribute */
547 int num_values
, /* I - Number of values */
548 const int *lower
, /* I - Lower values */
549 const int *upper
) /* I - Upper values */
551 int i
; /* Looping var */
552 ipp_attribute_t
*attr
; /* New attribute */
553 ipp_value_t
*value
; /* Current value */
556 if (ipp
== NULL
|| name
== NULL
|| num_values
< 1)
559 if ((attr
= _ippAddAttr(ipp
, num_values
)) == NULL
)
562 attr
->name
= _cupsStrAlloc(name
);
563 attr
->group_tag
= group
;
564 attr
->value_tag
= IPP_TAG_RANGE
;
566 if (lower
!= NULL
&& upper
!= NULL
)
567 for (i
= 0, value
= attr
->values
;
571 value
->range
.lower
= lower
[i
];
572 value
->range
.upper
= upper
[i
];
580 * 'ippAddResolution()' - Add a resolution value to an IPP message.
583 ipp_attribute_t
* /* O - New attribute */
584 ippAddResolution(ipp_t
*ipp
, /* I - IPP message */
585 ipp_tag_t group
, /* I - IPP group */
586 const char *name
, /* I - Name of attribute */
587 ipp_res_t units
, /* I - Units for resolution */
588 int xres
, /* I - X resolution */
589 int yres
) /* I - Y resolution */
591 ipp_attribute_t
*attr
; /* New attribute */
594 if (ipp
== NULL
|| name
== NULL
)
597 if ((attr
= _ippAddAttr(ipp
, 1)) == NULL
)
600 attr
->name
= _cupsStrAlloc(name
);
601 attr
->group_tag
= group
;
602 attr
->value_tag
= IPP_TAG_RESOLUTION
;
603 attr
->values
[0].resolution
.xres
= xres
;
604 attr
->values
[0].resolution
.yres
= yres
;
605 attr
->values
[0].resolution
.units
= units
;
612 * 'ippAddResolutions()' - Add resolution values to an IPP message.
615 ipp_attribute_t
* /* O - New attribute */
616 ippAddResolutions(ipp_t
*ipp
, /* I - IPP message */
617 ipp_tag_t group
, /* I - IPP group */
618 const char *name
, /* I - Name of attribute */
619 int num_values
,/* I - Number of values */
620 ipp_res_t units
, /* I - Units for resolution */
621 const int *xres
, /* I - X resolutions */
622 const int *yres
) /* I - Y resolutions */
624 int i
; /* Looping var */
625 ipp_attribute_t
*attr
; /* New attribute */
626 ipp_value_t
*value
; /* Current value */
629 if (ipp
== NULL
|| name
== NULL
|| num_values
< 1)
632 if ((attr
= _ippAddAttr(ipp
, num_values
)) == NULL
)
635 attr
->name
= _cupsStrAlloc(name
);
636 attr
->group_tag
= group
;
637 attr
->value_tag
= IPP_TAG_RESOLUTION
;
639 if (xres
!= NULL
&& yres
!= NULL
)
640 for (i
= 0, value
= attr
->values
;
644 value
->resolution
.xres
= xres
[i
];
645 value
->resolution
.yres
= yres
[i
];
646 value
->resolution
.units
= units
;
654 * 'ippAddSeparator()' - Add a group separator to an IPP message.
657 ipp_attribute_t
* /* O - New attribute */
658 ippAddSeparator(ipp_t
*ipp
) /* I - IPP message */
660 ipp_attribute_t
*attr
; /* New attribute */
663 DEBUG_printf(("ippAddSeparator(%p)\n", ipp
));
668 if ((attr
= _ippAddAttr(ipp
, 0)) == NULL
)
671 attr
->group_tag
= IPP_TAG_ZERO
;
672 attr
->value_tag
= IPP_TAG_ZERO
;
679 * 'ippDateToTime()' - Convert from RFC 1903 Date/Time format to UNIX time
683 time_t /* O - UNIX time value */
684 ippDateToTime(const ipp_uchar_t
*date
) /* I - RFC 1903 date info */
686 struct tm unixdate
; /* UNIX date/time info */
687 time_t t
; /* Computed time */
690 memset(&unixdate
, 0, sizeof(unixdate
));
693 * RFC-1903 date/time format is:
695 * Byte(s) Description
696 * ------- -----------
697 * 0-1 Year (0 to 65535)
701 * 5 Minutes (0 to 59)
702 * 6 Seconds (0 to 60, 60 = "leap second")
703 * 7 Deciseconds (0 to 9)
705 * 9 UTC hours (0 to 11)
706 * 10 UTC minutes (0 to 59)
709 unixdate
.tm_year
= ((date
[0] << 8) | date
[1]) - 1900;
710 unixdate
.tm_mon
= date
[2] - 1;
711 unixdate
.tm_mday
= date
[3];
712 unixdate
.tm_hour
= date
[4];
713 unixdate
.tm_min
= date
[5];
714 unixdate
.tm_sec
= date
[6];
716 t
= mktime(&unixdate
);
719 t
+= date
[9] * 3600 + date
[10] * 60;
721 t
-= date
[9] * 3600 + date
[10] * 60;
728 * 'ippDelete()' - Delete an IPP message.
732 ippDelete(ipp_t
*ipp
) /* I - IPP message */
734 ipp_attribute_t
*attr
, /* Current attribute */
735 *next
; /* Next attribute */
738 DEBUG_printf(("ippDelete(): %p\n", ipp
));
743 for (attr
= ipp
->attrs
; attr
!= NULL
; attr
= next
)
754 * 'ippDeleteAttribute()' - Delete a single attribute in an IPP message.
756 * @since CUPS 1.1.19@
761 ipp_t
*ipp
, /* I - IPP message */
762 ipp_attribute_t
*attr
) /* I - Attribute to delete */
764 ipp_attribute_t
*current
, /* Current attribute */
765 *prev
; /* Previous attribute */
769 * Find the attribute in the list...
772 for (current
= ipp
->attrs
, prev
= NULL
;
773 current
!= NULL
&& current
!= attr
;
774 prev
= current
, current
= current
->next
);
779 * Found it, remove the attribute from the list...
783 prev
->next
= current
->next
;
785 ipp
->attrs
= current
->next
;
787 if (current
== ipp
->last
)
791 * Free memory used by the attribute...
794 _ippFreeAttr(current
);
800 * 'ippFindAttribute()' - Find a named attribute in a request...
803 ipp_attribute_t
* /* O - Matching attribute */
804 ippFindAttribute(ipp_t
*ipp
, /* I - IPP message */
805 const char *name
, /* I - Name of attribute */
806 ipp_tag_t type
) /* I - Type of attribute */
808 DEBUG_printf(("ippFindAttribute(%p, \'%s\')\n", ipp
, name
));
810 if (ipp
== NULL
|| name
== NULL
)
814 * Reset the current pointer...
820 * Search for the attribute...
823 return (ippFindNextAttribute(ipp
, name
, type
));
828 * 'ippFindNextAttribute()' - Find the next named attribute in a request...
831 ipp_attribute_t
* /* O - Matching attribute */
832 ippFindNextAttribute(ipp_t
*ipp
, /* I - IPP message */
833 const char *name
, /* I - Name of attribute */
834 ipp_tag_t type
) /* I - Type of attribute */
836 ipp_attribute_t
*attr
; /* Current atttribute */
837 ipp_tag_t value_tag
; /* Value tag */
840 DEBUG_printf(("ippFindNextAttribute(%p, \'%s\')\n", ipp
, name
));
842 if (ipp
== NULL
|| name
== NULL
)
847 ipp
->prev
= ipp
->current
;
848 attr
= ipp
->current
->next
;
856 for (; attr
!= NULL
; ipp
->prev
= attr
, attr
= attr
->next
)
858 DEBUG_printf(("ippFindAttribute: attr = %p, name = \'%s\'\n", attr
,
861 value_tag
= (ipp_tag_t
)(attr
->value_tag
& IPP_TAG_MASK
);
863 if (attr
->name
!= NULL
&& strcasecmp(attr
->name
, name
) == 0 &&
864 (value_tag
== type
|| type
== IPP_TAG_ZERO
||
865 (value_tag
== IPP_TAG_TEXTLANG
&& type
== IPP_TAG_TEXT
) ||
866 (value_tag
== IPP_TAG_NAMELANG
&& type
== IPP_TAG_NAME
)))
882 * 'ippLength()' - Compute the length of an IPP message.
885 size_t /* O - Size of IPP message */
886 ippLength(ipp_t
*ipp
) /* I - IPP message */
888 return (ipp_length(ipp
, 0));
893 * 'ippNew()' - Allocate a new IPP message.
896 ipp_t
* /* O - New IPP message */
899 ipp_t
*temp
; /* New IPP message */
902 DEBUG_puts("ippNew()");
904 if ((temp
= (ipp_t
*)calloc(1, sizeof(ipp_t
))) != NULL
)
907 * Default to IPP 1.1...
910 temp
->request
.any
.version
[0] = 1;
911 temp
->request
.any
.version
[1] = 1;
914 DEBUG_printf(("ippNew: %p\n", temp
));
921 * 'ippNewRequest()' - Allocate a new IPP request message.
923 * The new request message is initialized with the attributes-charset and
924 * attributes-natural-language attributes added. The
925 * attributes-natural-language value is derived from the current locale.
930 ipp_t
* /* O - IPP request message */
931 ippNewRequest(ipp_op_t op
) /* I - Operation code */
933 ipp_t
*request
; /* IPP request message */
934 cups_lang_t
*language
; /* Current language localization */
938 * Create a new IPP message...
941 if ((request
= ippNew()) == NULL
)
945 * Set the operation and request ID...
948 request
->request
.op
.operation_id
= op
;
949 request
->request
.op
.request_id
= 1;
952 * Use UTF-8 as the character set...
955 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_CHARSET
,
956 "attributes-charset", NULL
, "utf-8");
959 * Get the language from the current locale...
962 language
= cupsLangDefault();
964 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_LANGUAGE
,
965 "attributes-natural-language", NULL
, language
->language
);
968 * Return the new request...
976 * 'ippRead()' - Read data for an IPP message from a HTTP connection.
979 ipp_state_t
/* O - Current state */
980 ippRead(http_t
*http
, /* I - HTTP connection */
981 ipp_t
*ipp
) /* I - IPP data */
983 DEBUG_printf(("ippRead(http=%p, ipp=%p), data_remaining=" CUPS_LLFMT
"\n",
984 http
, ipp
, CUPS_LLCAST (http
? http
->data_remaining
: -1)));
989 DEBUG_printf(("http->state = %d\n", http
->state
));
991 return (ippReadIO(http
, (ipp_iocb_t
)ipp_read_http
,
992 http
->blocking
|| http
->used
!= 0, NULL
, ipp
));
997 * 'ippReadFile()' - Read data for an IPP message from a file.
999 * @since CUPS 1.1.19@
1002 ipp_state_t
/* O - Current state */
1003 ippReadFile(int fd
, /* I - HTTP data */
1004 ipp_t
*ipp
) /* I - IPP data */
1006 DEBUG_printf(("ippReadFile(%d, %p)\n", fd
, ipp
));
1008 return (ippReadIO(&fd
, (ipp_iocb_t
)ipp_read_file
, 1, NULL
, ipp
));
1013 * 'ippReadIO()' - Read data for an IPP message.
1018 ipp_state_t
/* O - Current state */
1019 ippReadIO(void *src
, /* I - Data source */
1020 ipp_iocb_t cb
, /* I - Read callback function */
1021 int blocking
, /* I - Use blocking IO? */
1022 ipp_t
*parent
, /* I - Parent request, if any */
1023 ipp_t
*ipp
) /* I - IPP data */
1025 int n
; /* Length of data */
1026 unsigned char buffer
[32768], /* Data buffer */
1027 string
[255], /* Small string buffer */
1028 *bufptr
; /* Pointer into buffer */
1029 ipp_attribute_t
*attr
; /* Current attribute */
1030 ipp_tag_t tag
; /* Current tag */
1031 ipp_tag_t value_tag
; /* Current value tag */
1032 ipp_value_t
*value
; /* Current value */
1035 DEBUG_printf(("ippReadIO(%p, %p, %d, %p, %p)\n", src
, cb
, blocking
,
1037 DEBUG_printf(("ippReadIO: ipp->state=%d\n", ipp
->state
));
1039 if (src
== NULL
|| ipp
== NULL
)
1045 ipp
->state
++; /* Avoid common problem... */
1051 * Get the request header...
1054 if ((n
= (*cb
)(src
, buffer
, 8)) < 8)
1056 DEBUG_printf(("ippReadIO: Unable to read header (%d bytes read)!\n", n
));
1057 return (n
== 0 ? IPP_IDLE
: IPP_ERROR
);
1061 * Verify the major version number...
1066 DEBUG_printf(("ippReadIO: version number (%d.%d) is bad.\n", buffer
[0],
1072 * Then copy the request header over...
1075 ipp
->request
.any
.version
[0] = buffer
[0];
1076 ipp
->request
.any
.version
[1] = buffer
[1];
1077 ipp
->request
.any
.op_status
= (buffer
[2] << 8) | buffer
[3];
1078 ipp
->request
.any
.request_id
= (((((buffer
[4] << 8) | buffer
[5]) << 8) |
1079 buffer
[6]) << 8) | buffer
[7];
1081 DEBUG_printf(("ippReadIO: version=%d.%d\n", buffer
[0], buffer
[1]));
1082 DEBUG_printf(("ippReadIO: op_status=%04x\n",
1083 ipp
->request
.any
.op_status
));
1084 DEBUG_printf(("ippReadIO: request_id=%d\n",
1085 ipp
->request
.any
.request_id
));
1088 ipp
->state
= IPP_ATTRIBUTE
;
1089 ipp
->current
= NULL
;
1090 ipp
->curtag
= IPP_TAG_ZERO
;
1091 ipp
->prev
= ipp
->last
;
1094 * If blocking is disabled, stop here...
1100 case IPP_ATTRIBUTE
:
1101 while ((*cb
)(src
, buffer
, 1) > 0)
1103 DEBUG_printf(("ippReadIO: ipp->current=%p, ipp->prev=%p\n",
1104 ipp
->current
, ipp
->prev
));
1107 * Read this attribute...
1110 tag
= (ipp_tag_t
)buffer
[0];
1112 if (tag
== IPP_TAG_END
)
1115 * No more attributes left...
1118 DEBUG_puts("ippReadIO: IPP_TAG_END!");
1120 ipp
->state
= IPP_DATA
;
1123 else if (tag
< IPP_TAG_UNSUPPORTED_VALUE
)
1126 * Group tag... Set the current group and continue...
1129 if (ipp
->curtag
== tag
)
1130 ipp
->prev
= ippAddSeparator(ipp
);
1131 else if (ipp
->current
)
1132 ipp
->prev
= ipp
->current
;
1135 ipp
->current
= NULL
;
1136 DEBUG_printf(("ippReadIO: group tag = %x, ipp->prev=%p\n", tag
,
1141 DEBUG_printf(("ippReadIO: value tag = %x\n", tag
));
1147 if ((*cb
)(src
, buffer
, 2) < 2)
1149 DEBUG_puts("ippReadIO: unable to read name length!");
1153 n
= (buffer
[0] << 8) | buffer
[1];
1155 if (n
> (sizeof(buffer
) - 1))
1157 DEBUG_printf(("ippReadIO: bad name length %d!\n", n
));
1161 DEBUG_printf(("ippReadIO: name length = %d\n", n
));
1163 if (n
== 0 && tag
!= IPP_TAG_MEMBERNAME
&&
1164 tag
!= IPP_TAG_END_COLLECTION
)
1167 * More values for current attribute...
1170 if (ipp
->current
== NULL
)
1173 attr
= ipp
->current
;
1174 value_tag
= (ipp_tag_t
)(attr
->value_tag
& IPP_TAG_MASK
);
1177 * Make sure we aren't adding a new value of a different
1181 if (value_tag
== IPP_TAG_ZERO
)
1184 * Setting the value of a collection member...
1187 attr
->value_tag
= tag
;
1189 else if (value_tag
== IPP_TAG_STRING
||
1190 (value_tag
>= IPP_TAG_TEXTLANG
&&
1191 value_tag
<= IPP_TAG_MIMETYPE
))
1194 * String values can sometimes come across in different
1195 * forms; accept sets of differing values...
1198 if (tag
!= IPP_TAG_STRING
&&
1199 (tag
< IPP_TAG_TEXTLANG
|| tag
> IPP_TAG_MIMETYPE
))
1202 else if (value_tag
!= tag
)
1206 * Finally, reallocate the attribute array as needed...
1209 if (attr
->num_values
== 1 ||
1210 (attr
->num_values
> 0 &&
1211 (attr
->num_values
& (IPP_MAX_VALUES
- 1)) == 0))
1213 ipp_attribute_t
*temp
; /* Pointer to new buffer */
1216 DEBUG_printf(("ippReadIO: reallocating for up to %d values...\n",
1217 attr
->num_values
+ IPP_MAX_VALUES
));
1220 * Reallocate memory...
1223 if ((temp
= realloc(attr
, sizeof(ipp_attribute_t
) +
1224 (attr
->num_values
+ IPP_MAX_VALUES
- 1) *
1225 sizeof(ipp_value_t
))) == NULL
)
1231 * Reset pointers in the list...
1235 ipp
->prev
->next
= temp
;
1239 attr
= ipp
->current
= ipp
->last
= temp
;
1243 else if (tag
== IPP_TAG_MEMBERNAME
)
1246 * Name must be length 0!
1251 DEBUG_puts("ippReadIO: member name not empty!");
1256 ipp
->prev
= ipp
->current
;
1258 attr
= ipp
->current
= _ippAddAttr(ipp
, 1);
1260 DEBUG_printf(("ippReadIO: membername, ipp->current=%p, ipp->prev=%p\n",
1261 ipp
->current
, ipp
->prev
));
1263 attr
->group_tag
= ipp
->curtag
;
1264 attr
->value_tag
= IPP_TAG_ZERO
;
1265 attr
->num_values
= 0;
1267 else if (tag
!= IPP_TAG_END_COLLECTION
)
1270 * New attribute; read the name and add it...
1273 if ((*cb
)(src
, buffer
, n
) < n
)
1275 DEBUG_puts("ippReadIO: unable to read name!");
1282 ipp
->prev
= ipp
->current
;
1284 attr
= ipp
->current
= _ippAddAttr(ipp
, 1);
1286 DEBUG_printf(("ippReadIO: name=\'%s\', ipp->current=%p, ipp->prev=%p\n",
1287 buffer
, ipp
->current
, ipp
->prev
));
1289 attr
->group_tag
= ipp
->curtag
;
1290 attr
->value_tag
= tag
;
1291 attr
->name
= _cupsStrAlloc((char *)buffer
);
1292 attr
->num_values
= 0;
1297 if (tag
!= IPP_TAG_END_COLLECTION
)
1298 value
= attr
->values
+ attr
->num_values
;
1302 if ((*cb
)(src
, buffer
, 2) < 2)
1304 DEBUG_puts("ippReadIO: unable to read value length!");
1308 n
= (buffer
[0] << 8) | buffer
[1];
1309 DEBUG_printf(("ippReadIO: value length = %d\n", n
));
1313 case IPP_TAG_INTEGER
:
1315 if ((*cb
)(src
, buffer
, 4) < 4)
1317 DEBUG_puts("ippReadIO: Unable to read integer value!");
1321 n
= (((((buffer
[0] << 8) | buffer
[1]) << 8) | buffer
[2]) << 8) |
1326 case IPP_TAG_BOOLEAN
:
1327 if ((*cb
)(src
, buffer
, 1) < 1)
1329 DEBUG_puts("ippReadIO: Unable to read boolean value!");
1333 value
->boolean
= buffer
[0];
1337 case IPP_TAG_KEYWORD
:
1338 case IPP_TAG_STRING
:
1340 case IPP_TAG_URISCHEME
:
1341 case IPP_TAG_CHARSET
:
1342 case IPP_TAG_LANGUAGE
:
1343 case IPP_TAG_MIMETYPE
:
1344 if ((*cb
)(src
, buffer
, n
) < n
)
1346 DEBUG_puts("ippReadIO: unable to read name!");
1351 value
->string
.text
= _cupsStrAlloc((char *)buffer
);
1352 DEBUG_printf(("ippReadIO: value = \'%s\'\n",
1353 value
->string
.text
));
1356 if ((*cb
)(src
, value
->date
, 11) < 11)
1358 DEBUG_puts("ippReadIO: Unable to date integer value!");
1362 case IPP_TAG_RESOLUTION
:
1363 if ((*cb
)(src
, buffer
, 9) < 9)
1365 DEBUG_puts("ippReadIO: Unable to read resolution value!");
1369 value
->resolution
.xres
=
1370 (((((buffer
[0] << 8) | buffer
[1]) << 8) | buffer
[2]) << 8) |
1372 value
->resolution
.yres
=
1373 (((((buffer
[4] << 8) | buffer
[5]) << 8) | buffer
[6]) << 8) |
1375 value
->resolution
.units
=
1376 (ipp_res_t
)buffer
[8];
1378 case IPP_TAG_RANGE
:
1379 if ((*cb
)(src
, buffer
, 8) < 8)
1381 DEBUG_puts("ippReadIO: Unable to read range value!");
1385 value
->range
.lower
=
1386 (((((buffer
[0] << 8) | buffer
[1]) << 8) | buffer
[2]) << 8) |
1388 value
->range
.upper
=
1389 (((((buffer
[4] << 8) | buffer
[5]) << 8) | buffer
[6]) << 8) |
1392 case IPP_TAG_TEXTLANG
:
1393 case IPP_TAG_NAMELANG
:
1394 if (n
> sizeof(buffer
) || n
< 4)
1396 DEBUG_printf(("ippReadIO: bad value length %d!\n", n
));
1400 if ((*cb
)(src
, buffer
, n
) < n
)
1402 DEBUG_puts("ippReadIO: Unable to read string w/language value!");
1409 * text-with-language and name-with-language are composite
1418 n
= (bufptr
[0] << 8) | bufptr
[1];
1420 if (n
>= sizeof(string
))
1422 memcpy(string
, bufptr
+ 2, sizeof(string
) - 1);
1423 string
[sizeof(string
) - 1] = '\0';
1427 memcpy(string
, bufptr
+ 2, n
);
1431 value
->string
.charset
= _cupsStrAlloc((char *)string
);
1434 n
= (bufptr
[0] << 8) | bufptr
[1];
1436 bufptr
[2 + n
] = '\0';
1437 value
->string
.text
= _cupsStrAlloc((char *)bufptr
+ 2);
1440 case IPP_TAG_BEGIN_COLLECTION
:
1442 * Oh, boy, here comes a collection value, so read it...
1445 value
->collection
= ippNew();
1449 DEBUG_puts("ippReadIO: begCollection tag with value length > 0!");
1453 if (ippReadIO(src
, cb
, 1, ipp
, value
->collection
) == IPP_ERROR
)
1455 DEBUG_puts("ippReadIO: Unable to read collection value!");
1460 case IPP_TAG_END_COLLECTION
:
1463 DEBUG_puts("ippReadIO: endCollection tag with value length > 0!");
1467 DEBUG_puts("ippReadIO: endCollection tag...");
1469 return (ipp
->state
= IPP_DATA
);
1471 case IPP_TAG_MEMBERNAME
:
1473 * The value the name of the member in the collection, which
1474 * we need to carry over...
1477 if ((*cb
)(src
, buffer
, n
) < n
)
1479 DEBUG_puts("ippReadIO: Unable to read member name value!");
1484 attr
->name
= _cupsStrAlloc((char *)buffer
);
1487 * Since collection members are encoded differently than
1488 * regular attributes, make sure we don't start with an
1492 attr
->num_values
--;
1494 DEBUG_printf(("ippReadIO: member name = \"%s\"\n", attr
->name
));
1497 default : /* Other unsupported values */
1498 value
->unknown
.length
= n
;
1501 value
->unknown
.data
= malloc(n
);
1502 if ((*cb
)(src
, value
->unknown
.data
, n
) < n
)
1504 DEBUG_puts("ippReadIO: Unable to read unsupported value!");
1509 value
->unknown
.data
= NULL
;
1513 attr
->num_values
++;
1516 * If blocking is disabled, stop here...
1528 break; /* anti-compiler-warning-code */
1531 DEBUG_printf(("ippReadIO: returning ipp->state=%d!\n", ipp
->state
));
1533 return (ipp
->state
);
1538 * 'ippTimeToDate()' - Convert from UNIX time to RFC 1903 format.
1541 const ipp_uchar_t
* /* O - RFC-1903 date/time data */
1542 ippTimeToDate(time_t t
) /* I - UNIX time value */
1544 struct tm
*unixdate
; /* UNIX unixdate/time info */
1545 ipp_uchar_t
*date
= _cupsGlobals()->ipp_date
;
1546 /* RFC-1903 date/time data */
1550 * RFC-1903 date/time format is:
1552 * Byte(s) Description
1553 * ------- -----------
1554 * 0-1 Year (0 to 65535)
1558 * 5 Minutes (0 to 59)
1559 * 6 Seconds (0 to 60, 60 = "leap second")
1560 * 7 Deciseconds (0 to 9)
1562 * 9 UTC hours (0 to 11)
1563 * 10 UTC minutes (0 to 59)
1566 unixdate
= gmtime(&t
);
1567 unixdate
->tm_year
+= 1900;
1569 date
[0] = unixdate
->tm_year
>> 8;
1570 date
[1] = unixdate
->tm_year
;
1571 date
[2] = unixdate
->tm_mon
+ 1;
1572 date
[3] = unixdate
->tm_mday
;
1573 date
[4] = unixdate
->tm_hour
;
1574 date
[5] = unixdate
->tm_min
;
1575 date
[6] = unixdate
->tm_sec
;
1586 * 'ippWrite()' - Write data for an IPP message to a HTTP connection.
1589 ipp_state_t
/* O - Current state */
1590 ippWrite(http_t
*http
, /* I - HTTP connection */
1591 ipp_t
*ipp
) /* I - IPP data */
1593 DEBUG_printf(("ippWrite(%p, %p)\n", http
, ipp
));
1598 return (ippWriteIO(http
, (ipp_iocb_t
)httpWrite2
,
1599 http
->blocking
, NULL
, ipp
));
1604 * 'ippWriteFile()' - Write data for an IPP message to a file.
1606 * @since CUPS 1.1.19@
1609 ipp_state_t
/* O - Current state */
1610 ippWriteFile(int fd
, /* I - HTTP data */
1611 ipp_t
*ipp
) /* I - IPP data */
1613 DEBUG_printf(("ippWriteFile(%d, %p)\n", fd
, ipp
));
1615 ipp
->state
= IPP_IDLE
;
1617 return (ippWriteIO(&fd
, (ipp_iocb_t
)ipp_write_file
, 1, NULL
, ipp
));
1622 * 'ippWriteIO()' - Write data for an IPP message.
1627 ipp_state_t
/* O - Current state */
1628 ippWriteIO(void *dst
, /* I - Destination */
1629 ipp_iocb_t cb
, /* I - Write callback function */
1630 int blocking
, /* I - Use blocking IO? */
1631 ipp_t
*parent
, /* I - Parent IPP message */
1632 ipp_t
*ipp
) /* I - IPP data */
1634 int i
; /* Looping var */
1635 int n
; /* Length of data */
1636 unsigned char buffer
[32768], /* Data buffer */
1637 *bufptr
; /* Pointer into buffer */
1638 ipp_attribute_t
*attr
; /* Current attribute */
1639 ipp_value_t
*value
; /* Current value */
1642 DEBUG_printf(("ippWriteIO(%p, %p, %d, %p, %p)\n", dst
, cb
, blocking
,
1645 if (dst
== NULL
|| ipp
== NULL
)
1651 ipp
->state
++; /* Avoid common problem... */
1657 * Send the request header:
1660 * Operation/Status Code = 2 bytes
1661 * Request ID = 4 bytes
1667 *bufptr
++ = ipp
->request
.any
.version
[0];
1668 *bufptr
++ = ipp
->request
.any
.version
[1];
1669 *bufptr
++ = ipp
->request
.any
.op_status
>> 8;
1670 *bufptr
++ = ipp
->request
.any
.op_status
;
1671 *bufptr
++ = ipp
->request
.any
.request_id
>> 24;
1672 *bufptr
++ = ipp
->request
.any
.request_id
>> 16;
1673 *bufptr
++ = ipp
->request
.any
.request_id
>> 8;
1674 *bufptr
++ = ipp
->request
.any
.request_id
;
1676 if ((*cb
)(dst
, buffer
, (int)(bufptr
- buffer
)) < 0)
1678 DEBUG_puts("ippWriteIO: Could not write IPP header...");
1684 * Reset the state engine to point to the first attribute
1685 * in the request/response, with no current group.
1688 ipp
->state
= IPP_ATTRIBUTE
;
1689 ipp
->current
= ipp
->attrs
;
1690 ipp
->curtag
= IPP_TAG_ZERO
;
1692 DEBUG_printf(("ippWriteIO: version=%d.%d\n", buffer
[0], buffer
[1]));
1693 DEBUG_printf(("ippWriteIO: op_status=%04x\n", ipp
->request
.any
.op_status
));
1694 DEBUG_printf(("ippWriteIO: request_id=%d\n", ipp
->request
.any
.request_id
));
1697 * If blocking is disabled, stop here...
1703 case IPP_ATTRIBUTE
:
1704 while (ipp
->current
!= NULL
)
1707 * Write this attribute...
1711 attr
= ipp
->current
;
1713 ipp
->current
= ipp
->current
->next
;
1715 if (ipp
->curtag
!= attr
->group_tag
&& parent
== NULL
)
1718 * Send a group tag byte...
1721 ipp
->curtag
= attr
->group_tag
;
1723 if (attr
->group_tag
== IPP_TAG_ZERO
)
1726 DEBUG_printf(("ippWriteIO: wrote group tag = %x\n", attr
->group_tag
));
1727 *bufptr
++ = attr
->group_tag
;
1729 else if (attr
->group_tag
== IPP_TAG_ZERO
)
1733 * Write the attribute tag and name. The current implementation
1734 * does not support the extension value tags above 0x7f, so all
1735 * value tags are 1 byte.
1737 * The attribute name length does not include the trailing nul
1738 * character in the source string.
1740 * Collection values (parent != NULL) are written differently...
1746 * Get the length of the attribute name, and make sure it won't
1747 * overflow the buffer...
1750 if ((n
= (int)strlen(attr
->name
)) > (sizeof(buffer
) - 4))
1754 * Write the value tag, name length, and name string...
1757 DEBUG_printf(("ippWriteIO: writing value tag = %x\n", attr
->value_tag
));
1758 DEBUG_printf(("ippWriteIO: writing name = %d, \'%s\'\n", n
, attr
->name
));
1760 *bufptr
++ = attr
->value_tag
;
1763 memcpy(bufptr
, attr
->name
, n
);
1769 * Get the length of the attribute name, and make sure it won't
1770 * overflow the buffer...
1773 if ((n
= (int)strlen(attr
->name
)) > (sizeof(buffer
) - 7))
1777 * Write the member name tag, name length, name string, value tag,
1778 * and empty name for the collection member attribute...
1781 DEBUG_printf(("ippWriteIO: writing value tag = %x\n",
1782 IPP_TAG_MEMBERNAME
));
1783 DEBUG_printf(("ippWriteIO: writing name = %d, \'%s\'\n", n
, attr
->name
));
1784 DEBUG_printf(("ippWriteIO: writing value tag = %x\n", attr
->value_tag
));
1785 DEBUG_puts("ippWriteIO: writing name = 0, \'\'\n");
1787 *bufptr
++ = IPP_TAG_MEMBERNAME
;
1792 memcpy(bufptr
, attr
->name
, n
);
1795 *bufptr
++ = attr
->value_tag
;
1801 * Now write the attribute value(s)...
1804 switch (attr
->value_tag
& ~IPP_TAG_COPY
)
1806 case IPP_TAG_INTEGER
:
1808 for (i
= 0, value
= attr
->values
;
1809 i
< attr
->num_values
;
1812 if ((sizeof(buffer
) - (bufptr
- buffer
)) < 9)
1814 if ((*cb
)(dst
, buffer
, (int)(bufptr
- buffer
)) < 0)
1816 DEBUG_puts("ippWriteIO: Could not write IPP attribute...");
1826 * Arrays and sets are done by sending additional
1827 * values with a zero-length name...
1830 *bufptr
++ = attr
->value_tag
;
1836 * Integers and enumerations are both 4-byte signed
1837 * (twos-complement) values.
1839 * Put the 2-byte length and 4-byte value into the buffer...
1844 *bufptr
++ = value
->integer
>> 24;
1845 *bufptr
++ = value
->integer
>> 16;
1846 *bufptr
++ = value
->integer
>> 8;
1847 *bufptr
++ = value
->integer
;
1851 case IPP_TAG_BOOLEAN
:
1852 for (i
= 0, value
= attr
->values
;
1853 i
< attr
->num_values
;
1856 if ((sizeof(buffer
) - (bufptr
- buffer
)) < 6)
1858 if ((*cb
)(dst
, buffer
, (int)(bufptr
- buffer
)) < 0)
1860 DEBUG_puts("ippWriteIO: Could not write IPP attribute...");
1870 * Arrays and sets are done by sending additional
1871 * values with a zero-length name...
1874 *bufptr
++ = attr
->value_tag
;
1880 * Boolean values are 1-byte; 0 = false, 1 = true.
1882 * Put the 2-byte length and 1-byte value into the buffer...
1887 *bufptr
++ = value
->boolean
;
1893 case IPP_TAG_KEYWORD
:
1894 case IPP_TAG_STRING
:
1896 case IPP_TAG_URISCHEME
:
1897 case IPP_TAG_CHARSET
:
1898 case IPP_TAG_LANGUAGE
:
1899 case IPP_TAG_MIMETYPE
:
1900 for (i
= 0, value
= attr
->values
;
1901 i
< attr
->num_values
;
1907 * Arrays and sets are done by sending additional
1908 * values with a zero-length name...
1911 DEBUG_printf(("ippWriteIO: writing value tag = %x\n",
1913 DEBUG_printf(("ippWriteIO: writing name = 0, \'\'\n"));
1915 if ((sizeof(buffer
) - (bufptr
- buffer
)) < 3)
1917 if ((*cb
)(dst
, buffer
, (int)(bufptr
- buffer
)) < 0)
1919 DEBUG_puts("ippWriteIO: Could not write IPP attribute...");
1926 *bufptr
++ = attr
->value_tag
;
1931 if (value
->string
.text
!= NULL
)
1932 n
= (int)strlen(value
->string
.text
);
1936 if (n
> (sizeof(buffer
) - 2))
1939 DEBUG_printf(("ippWriteIO: writing string = %d, \'%s\'\n", n
,
1940 value
->string
.text
));
1942 if ((int)(sizeof(buffer
) - (bufptr
- buffer
)) < (n
+ 2))
1944 if ((*cb
)(dst
, buffer
, (int)(bufptr
- buffer
)) < 0)
1946 DEBUG_puts("ippWriteIO: Could not write IPP attribute...");
1954 * All simple strings consist of the 2-byte length and
1955 * character data without the trailing nul normally found
1956 * in C strings. Also, strings cannot be longer than 32767
1957 * bytes since the 2-byte length is a signed (twos-complement)
1960 * Put the 2-byte length and string characters in the buffer.
1968 memcpy(bufptr
, value
->string
.text
, n
);
1975 for (i
= 0, value
= attr
->values
;
1976 i
< attr
->num_values
;
1979 if ((sizeof(buffer
) - (bufptr
- buffer
)) < 16)
1981 if ((*cb
)(dst
, buffer
, (int)(bufptr
- buffer
)) < 0)
1983 DEBUG_puts("ippWriteIO: Could not write IPP attribute...");
1993 * Arrays and sets are done by sending additional
1994 * values with a zero-length name...
1997 *bufptr
++ = attr
->value_tag
;
2003 * Date values consist of a 2-byte length and an
2004 * 11-byte date/time structure defined by RFC 1903.
2006 * Put the 2-byte length and 11-byte date/time
2007 * structure in the buffer.
2012 memcpy(bufptr
, value
->date
, 11);
2017 case IPP_TAG_RESOLUTION
:
2018 for (i
= 0, value
= attr
->values
;
2019 i
< attr
->num_values
;
2022 if ((sizeof(buffer
) - (bufptr
- buffer
)) < 14)
2024 if ((*cb
)(dst
, buffer
, (int)(bufptr
- buffer
)) < 0)
2026 DEBUG_puts("ippWriteIO: Could not write IPP attribute...");
2036 * Arrays and sets are done by sending additional
2037 * values with a zero-length name...
2040 *bufptr
++ = attr
->value_tag
;
2046 * Resolution values consist of a 2-byte length,
2047 * 4-byte horizontal resolution value, 4-byte vertical
2048 * resolution value, and a 1-byte units value.
2050 * Put the 2-byte length and resolution value data
2056 *bufptr
++ = value
->resolution
.xres
>> 24;
2057 *bufptr
++ = value
->resolution
.xres
>> 16;
2058 *bufptr
++ = value
->resolution
.xres
>> 8;
2059 *bufptr
++ = value
->resolution
.xres
;
2060 *bufptr
++ = value
->resolution
.yres
>> 24;
2061 *bufptr
++ = value
->resolution
.yres
>> 16;
2062 *bufptr
++ = value
->resolution
.yres
>> 8;
2063 *bufptr
++ = value
->resolution
.yres
;
2064 *bufptr
++ = value
->resolution
.units
;
2068 case IPP_TAG_RANGE
:
2069 for (i
= 0, value
= attr
->values
;
2070 i
< attr
->num_values
;
2073 if ((sizeof(buffer
) - (bufptr
- buffer
)) < 13)
2075 if ((*cb
)(dst
, buffer
, (int)(bufptr
- buffer
)) < 0)
2077 DEBUG_puts("ippWriteIO: Could not write IPP attribute...");
2087 * Arrays and sets are done by sending additional
2088 * values with a zero-length name...
2091 *bufptr
++ = attr
->value_tag
;
2097 * Range values consist of a 2-byte length,
2098 * 4-byte lower value, and 4-byte upper value.
2100 * Put the 2-byte length and range value data
2106 *bufptr
++ = value
->range
.lower
>> 24;
2107 *bufptr
++ = value
->range
.lower
>> 16;
2108 *bufptr
++ = value
->range
.lower
>> 8;
2109 *bufptr
++ = value
->range
.lower
;
2110 *bufptr
++ = value
->range
.upper
>> 24;
2111 *bufptr
++ = value
->range
.upper
>> 16;
2112 *bufptr
++ = value
->range
.upper
>> 8;
2113 *bufptr
++ = value
->range
.upper
;
2117 case IPP_TAG_TEXTLANG
:
2118 case IPP_TAG_NAMELANG
:
2119 for (i
= 0, value
= attr
->values
;
2120 i
< attr
->num_values
;
2126 * Arrays and sets are done by sending additional
2127 * values with a zero-length name...
2130 if ((sizeof(buffer
) - (bufptr
- buffer
)) < 3)
2132 if ((*cb
)(dst
, buffer
, (int)(bufptr
- buffer
)) < 0)
2134 DEBUG_puts("ippWriteIO: Could not write IPP attribute...");
2141 *bufptr
++ = attr
->value_tag
;
2147 * textWithLanguage and nameWithLanguage values consist
2148 * of a 2-byte length for both strings and their
2149 * individual lengths, a 2-byte length for the
2150 * character string, the character string without the
2151 * trailing nul, a 2-byte length for the character
2152 * set string, and the character set string without
2158 if (value
->string
.charset
!= NULL
)
2159 n
+= strlen(value
->string
.charset
);
2161 if (value
->string
.text
!= NULL
)
2162 n
+= strlen(value
->string
.text
);
2164 if (n
> (sizeof(buffer
) - 2))
2167 if ((int)(sizeof(buffer
) - (bufptr
- buffer
)) < (n
+ 2))
2169 if ((*cb
)(dst
, buffer
, (int)(bufptr
- buffer
)) < 0)
2171 DEBUG_puts("ippWriteIO: Could not write IPP attribute...");
2178 /* Length of entire value */
2182 /* Length of charset */
2183 if (value
->string
.charset
!= NULL
)
2184 n
= (int)strlen(value
->string
.charset
);
2194 memcpy(bufptr
, value
->string
.charset
, n
);
2198 /* Length of text */
2199 if (value
->string
.text
!= NULL
)
2200 n
= (int)strlen(value
->string
.text
);
2210 memcpy(bufptr
, value
->string
.text
, n
);
2216 case IPP_TAG_BEGIN_COLLECTION
:
2217 for (i
= 0, value
= attr
->values
;
2218 i
< attr
->num_values
;
2222 * Collections are written with the begin-collection
2223 * tag first with a value of 0 length, followed by the
2224 * attributes in the collection, then the end-collection
2228 if ((sizeof(buffer
) - (bufptr
- buffer
)) < 5)
2230 if ((*cb
)(dst
, buffer
, (int)(bufptr
- buffer
)) < 0)
2232 DEBUG_puts("ippWriteIO: Could not write IPP attribute...");
2242 * Arrays and sets are done by sending additional
2243 * values with a zero-length name...
2246 *bufptr
++ = attr
->value_tag
;
2252 * Write a data length of 0 and flush the buffer...
2258 if ((*cb
)(dst
, buffer
, (int)(bufptr
- buffer
)) < 0)
2260 DEBUG_puts("ippWriteIO: Could not write IPP attribute...");
2267 * Then write the collection attribute...
2270 value
->collection
->state
= IPP_IDLE
;
2272 if (ippWriteIO(dst
, cb
, 1, ipp
, value
->collection
) == IPP_ERROR
)
2278 for (i
= 0, value
= attr
->values
;
2279 i
< attr
->num_values
;
2285 * Arrays and sets are done by sending additional
2286 * values with a zero-length name...
2289 if ((sizeof(buffer
) - (bufptr
- buffer
)) < 3)
2291 if ((*cb
)(dst
, buffer
, (int)(bufptr
- buffer
)) < 0)
2293 DEBUG_puts("ippWriteIO: Could not write IPP attribute...");
2300 *bufptr
++ = attr
->value_tag
;
2306 * An unknown value might some new value that a
2307 * vendor has come up with. It consists of a
2308 * 2-byte length and the bytes in the unknown
2312 n
= value
->unknown
.length
;
2314 if (n
> (sizeof(buffer
) - 2))
2317 if ((int)(sizeof(buffer
) - (bufptr
- buffer
)) < (n
+ 2))
2319 if ((*cb
)(dst
, buffer
, (int)(bufptr
- buffer
)) < 0)
2321 DEBUG_puts("ippWriteIO: Could not write IPP attribute...");
2328 /* Length of unknown value */
2335 memcpy(bufptr
, value
->unknown
.data
, n
);
2343 * Write the data out...
2346 if ((*cb
)(dst
, buffer
, (int)(bufptr
- buffer
)) < 0)
2348 DEBUG_puts("ippWriteIO: Could not write IPP attribute...");
2352 DEBUG_printf(("ippWriteIO: wrote %d bytes\n", bufptr
- buffer
));
2355 * If blocking is disabled, stop here...
2362 if (ipp
->current
== NULL
)
2365 * Done with all of the attributes; add the end-of-attributes
2366 * tag or end-collection attribute...
2371 buffer
[0] = IPP_TAG_END
;
2376 buffer
[0] = IPP_TAG_END_COLLECTION
;
2377 buffer
[1] = 0; /* empty name */
2379 buffer
[3] = 0; /* empty value */
2384 if ((*cb
)(dst
, buffer
, n
) < 0)
2386 DEBUG_puts("ippWriteIO: Could not write IPP end-tag...");
2390 ipp
->state
= IPP_DATA
;
2398 break; /* anti-compiler-warning-code */
2401 return (ipp
->state
);
2406 * '_ippAddAttr()' - Add a new attribute to the request.
2409 ipp_attribute_t
* /* O - New attribute */
2410 _ippAddAttr(ipp_t
*ipp
, /* I - IPP message */
2411 int num_values
) /* I - Number of values */
2413 ipp_attribute_t
*attr
; /* New attribute */
2416 DEBUG_printf(("_ippAddAttr(%p, %d)\n", ipp
, num_values
));
2418 if (ipp
== NULL
|| num_values
< 0)
2421 attr
= calloc(sizeof(ipp_attribute_t
) +
2422 (num_values
- 1) * sizeof(ipp_value_t
), 1);
2426 attr
->num_values
= num_values
;
2428 if (ipp
->last
== NULL
)
2431 ipp
->last
->next
= attr
;
2436 DEBUG_printf(("_ippAddAttr(): %p\n", attr
));
2443 * '_ippFreeAttr()' - Free an attribute.
2447 _ippFreeAttr(ipp_attribute_t
*attr
) /* I - Attribute to free */
2449 int i
; /* Looping var */
2450 ipp_value_t
*value
; /* Current value */
2453 DEBUG_printf(("_ippFreeAttr(): %p\n", attr
));
2455 switch (attr
->value_tag
)
2459 case IPP_TAG_KEYWORD
:
2460 case IPP_TAG_STRING
:
2462 case IPP_TAG_URISCHEME
:
2463 case IPP_TAG_CHARSET
:
2464 case IPP_TAG_LANGUAGE
:
2465 case IPP_TAG_MIMETYPE
:
2466 for (i
= 0, value
= attr
->values
;
2467 i
< attr
->num_values
;
2469 _cupsStrFree(value
->string
.text
);
2472 case IPP_TAG_TEXTLANG
:
2473 case IPP_TAG_NAMELANG
:
2474 for (i
= 0, value
= attr
->values
;
2475 i
< attr
->num_values
;
2478 if (value
->string
.charset
&& i
== 0)
2479 _cupsStrFree(value
->string
.charset
);
2480 _cupsStrFree(value
->string
.text
);
2484 case IPP_TAG_INTEGER
:
2486 case IPP_TAG_BOOLEAN
:
2488 case IPP_TAG_RESOLUTION
:
2489 case IPP_TAG_RANGE
:
2492 case IPP_TAG_BEGIN_COLLECTION
:
2493 for (i
= 0, value
= attr
->values
;
2494 i
< attr
->num_values
;
2496 ippDelete(value
->collection
);
2500 if (!((int)attr
->value_tag
& IPP_TAG_COPY
))
2502 for (i
= 0, value
= attr
->values
;
2503 i
< attr
->num_values
;
2505 if (value
->unknown
.data
)
2506 free(value
->unknown
.data
);
2512 _cupsStrFree(attr
->name
);
2519 * 'ipp_length()' - Compute the length of an IPP message or collection value.
2522 static size_t /* O - Size of IPP message */
2523 ipp_length(ipp_t
*ipp
, /* I - IPP message or collection */
2524 int collection
) /* I - 1 if a collection, 0 otherwise */
2526 int i
; /* Looping var */
2527 int bytes
; /* Number of bytes */
2528 ipp_attribute_t
*attr
; /* Current attribute */
2529 ipp_tag_t group
; /* Current group */
2530 ipp_value_t
*value
; /* Current value */
2537 * Start with 8 bytes for the IPP message header...
2540 bytes
= collection
? 0 : 8;
2543 * Then add the lengths of each attribute...
2546 group
= IPP_TAG_ZERO
;
2548 for (attr
= ipp
->attrs
; attr
!= NULL
; attr
= attr
->next
)
2550 if (attr
->group_tag
!= group
&& !collection
)
2552 group
= attr
->group_tag
;
2553 if (group
== IPP_TAG_ZERO
)
2556 bytes
++; /* Group tag */
2562 DEBUG_printf(("attr->name = %s, attr->num_values = %d, bytes = %d\n",
2563 attr
->name
, attr
->num_values
, bytes
));
2565 bytes
+= strlen(attr
->name
); /* Name */
2566 bytes
+= attr
->num_values
; /* Value tag for each value */
2567 bytes
+= 2 * attr
->num_values
; /* Name lengths */
2568 bytes
+= 2 * attr
->num_values
; /* Value lengths */
2571 bytes
+= 5; /* Add membername overhead */
2573 switch (attr
->value_tag
& ~IPP_TAG_COPY
)
2575 case IPP_TAG_INTEGER
:
2577 bytes
+= 4 * attr
->num_values
;
2580 case IPP_TAG_BOOLEAN
:
2581 bytes
+= attr
->num_values
;
2586 case IPP_TAG_KEYWORD
:
2587 case IPP_TAG_STRING
:
2589 case IPP_TAG_URISCHEME
:
2590 case IPP_TAG_CHARSET
:
2591 case IPP_TAG_LANGUAGE
:
2592 case IPP_TAG_MIMETYPE
:
2593 for (i
= 0, value
= attr
->values
;
2594 i
< attr
->num_values
;
2596 if (value
->string
.text
!= NULL
)
2597 bytes
+= strlen(value
->string
.text
);
2601 bytes
+= 11 * attr
->num_values
;
2604 case IPP_TAG_RESOLUTION
:
2605 bytes
+= 9 * attr
->num_values
;
2608 case IPP_TAG_RANGE
:
2609 bytes
+= 8 * attr
->num_values
;
2612 case IPP_TAG_TEXTLANG
:
2613 case IPP_TAG_NAMELANG
:
2614 bytes
+= 4 * attr
->num_values
;/* Charset + text length */
2616 for (i
= 0, value
= attr
->values
;
2617 i
< attr
->num_values
;
2620 if (value
->string
.charset
!= NULL
)
2621 bytes
+= strlen(value
->string
.charset
);
2623 if (value
->string
.text
!= NULL
)
2624 bytes
+= strlen(value
->string
.text
);
2628 case IPP_TAG_BEGIN_COLLECTION
:
2629 for (i
= 0, value
= attr
->values
;
2630 i
< attr
->num_values
;
2632 bytes
+= ipp_length(value
->collection
, 1);
2636 for (i
= 0, value
= attr
->values
;
2637 i
< attr
->num_values
;
2639 bytes
+= value
->unknown
.length
;
2645 * Finally, add 1 byte for the "end of attributes" tag or 5 bytes
2646 * for the "end of collection" tag and return...
2654 DEBUG_printf(("bytes = %d\n", bytes
));
2661 * 'ipp_read_http()' - Semi-blocking read on a HTTP connection...
2664 static ssize_t
/* O - Number of bytes read */
2665 ipp_read_http(http_t
*http
, /* I - Client connection */
2666 ipp_uchar_t
*buffer
, /* O - Buffer for data */
2667 size_t length
) /* I - Total length */
2669 int tbytes
, /* Total bytes read */
2670 bytes
; /* Bytes read this pass */
2671 char len
[32]; /* Length string */
2674 DEBUG_printf(("ipp_read_http(http=%p, buffer=%p, length=%d)\n",
2675 http
, buffer
, length
));
2678 * Loop until all bytes are read...
2681 for (tbytes
= 0, bytes
= 0; tbytes
< length
; tbytes
+= bytes
, buffer
+= bytes
)
2683 DEBUG_printf(("tbytes = %d, http->state = %d\n", tbytes
, http
->state
));
2685 if (http
->state
== HTTP_WAITING
)
2688 if (http
->used
> 0 && http
->data_encoding
== HTTP_ENCODE_LENGTH
)
2691 * Do "fast read" from HTTP buffer directly...
2694 if (http
->used
> (length
- tbytes
))
2695 bytes
= length
- tbytes
;
2700 buffer
[0] = http
->buffer
[0];
2702 memcpy(buffer
, http
->buffer
, bytes
);
2704 http
->used
-= bytes
;
2705 http
->data_remaining
-= bytes
;
2707 if (http
->data_remaining
<= INT_MAX
)
2708 http
->_data_remaining
= (int)http
->data_remaining
;
2710 http
->_data_remaining
= INT_MAX
;
2713 memmove(http
->buffer
, http
->buffer
+ bytes
, http
->used
);
2715 if (http
->data_remaining
== 0)
2717 if (http
->data_encoding
== HTTP_ENCODE_CHUNKED
)
2720 * Get the trailing CR LF after the chunk...
2723 if (!httpGets(len
, sizeof(len
), http
))
2727 if (http
->data_encoding
!= HTTP_ENCODE_CHUNKED
)
2729 if (http
->state
== HTTP_POST_RECV
)
2732 http
->state
= HTTP_WAITING
;
2739 * Wait a maximum of 1 second for data...
2742 if (!http
->blocking
)
2745 * Wait up to 10 seconds for more data on non-blocking sockets...
2748 if (!httpWait(http
, 10000))
2759 if ((bytes
= httpRead2(http
, (char *)buffer
, length
- tbytes
)) <= 0)
2765 * Return the number of bytes read...
2768 if (tbytes
== 0 && bytes
< 0)
2771 DEBUG_printf(("returning %d bytes...\n", tbytes
));
2778 * 'ipp_read_file()' - Read IPP data from a file.
2781 static ssize_t
/* O - Number of bytes read */
2782 ipp_read_file(int *fd
, /* I - File descriptor */
2783 ipp_uchar_t
*buffer
, /* O - Read buffer */
2784 size_t length
) /* I - Number of bytes to read */
2786 return (read(*fd
, buffer
, length
));
2791 * 'ipp_write_file()' - Write IPP data to a file.
2794 static ssize_t
/* O - Number of bytes written */
2795 ipp_write_file(int *fd
, /* I - File descriptor */
2796 ipp_uchar_t
*buffer
, /* I - Data to write */
2797 size_t length
) /* I - Number of bytes to write */
2799 return (write(*fd
, buffer
, length
));
2805 * The following symbol definitions are provided only for KDE
2806 * compatibility during the CUPS 1.2 testing period and will be
2807 * removed in a future release of CUPS. These are PRIVATE APIs
2808 * from CUPS 1.1.x that the KDE developers chose to use...
2811 ipp_attribute_t
* /* O - New attribute */
2812 _ipp_add_attr(ipp_t
*ipp
, /* I - IPP message */
2813 int num_values
) /* I - Number of values */
2815 return (_ippAddAttr(ipp
, num_values
));
2819 _ipp_free_attr(ipp_attribute_t
*attr
) /* I - Attribute to free */
2823 #endif /* __linux */