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 * _ipp_add_attr() - Add a new attribute to the request.
61 * _ipp_free_attr() - 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
= _ipp_add_attr(ipp
, 1)) == NULL
)
117 attr
->name
= _cups_sp_alloc(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
= _ipp_add_attr(ipp
, num_values
)) == NULL
)
151 attr
->name
= _cups_sp_alloc(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
= _ipp_add_attr(ipp
, 1)) == NULL
)
189 attr
->name
= _cups_sp_alloc(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
= _ipp_add_attr(ipp
, num_values
)) == NULL
)
226 attr
->name
= _cups_sp_alloc(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
= _ipp_add_attr(ipp
, 1)) == NULL
)
262 attr
->name
= _cups_sp_alloc(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
= _ipp_add_attr(ipp
, 1)) == NULL
)
294 attr
->name
= _cups_sp_alloc(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
= _ipp_add_attr(ipp
, num_values
)) == NULL
)
326 attr
->name
= _cups_sp_alloc(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
= _ipp_add_attr(ipp
, 1)) == NULL
)
363 * Initialize the attribute data...
366 attr
->name
= _cups_sp_alloc(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
= _ipp_add_attr(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
= _cups_sp_alloc(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
? _cups_sp_alloc(charset
) : NULL
;
440 attr
->values
[0].string
.text
= ((int)type
& IPP_TAG_COPY
) ? (char *)value
:
441 value
? _cups_sp_alloc(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
= _ipp_add_attr(ipp
, num_values
)) == NULL
)
473 * Initialize the attribute data...
476 attr
->name
= _cups_sp_alloc(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
? _cups_sp_alloc(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" :
498 _cups_sp_alloc("en");
500 value
->string
.text
= ((int)type
& IPP_TAG_COPY
) ? (char *)values
[i
] :
501 _cups_sp_alloc(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
= _ipp_add_attr(ipp
, 1)) == NULL
)
529 attr
->name
= _cups_sp_alloc(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
= _ipp_add_attr(ipp
, num_values
)) == NULL
)
562 attr
->name
= _cups_sp_alloc(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
= _ipp_add_attr(ipp
, 1)) == NULL
)
600 attr
->name
= _cups_sp_alloc(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
= _ipp_add_attr(ipp
, num_values
)) == NULL
)
635 attr
->name
= _cups_sp_alloc(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
= _ipp_add_attr(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
)
746 _ipp_free_attr(attr
);
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 _ipp_free_attr(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
,
1038 if (src
== NULL
|| ipp
== NULL
)
1044 ipp
->state
++; /* Avoid common problem... */
1050 * Get the request header...
1053 if ((n
= (*cb
)(src
, buffer
, 8)) < 8)
1055 DEBUG_printf(("ippReadIO: Unable to read header (%d bytes read)!\n", n
));
1056 return (n
== 0 ? IPP_IDLE
: IPP_ERROR
);
1060 * Verify the major version number...
1065 DEBUG_printf(("ippReadIO: version number (%d.%d) is bad.\n", buffer
[0],
1071 * Then copy the request header over...
1074 ipp
->request
.any
.version
[0] = buffer
[0];
1075 ipp
->request
.any
.version
[1] = buffer
[1];
1076 ipp
->request
.any
.op_status
= (buffer
[2] << 8) | buffer
[3];
1077 ipp
->request
.any
.request_id
= (((((buffer
[4] << 8) | buffer
[5]) << 8) |
1078 buffer
[6]) << 8) | buffer
[7];
1080 DEBUG_printf(("ippReadIO: version=%d.%d\n", buffer
[0], buffer
[1]));
1081 DEBUG_printf(("ippReadIO: op_status=%04x\n",
1082 ipp
->request
.any
.op_status
));
1083 DEBUG_printf(("ippReadIO: request_id=%d\n",
1084 ipp
->request
.any
.request_id
));
1087 ipp
->state
= IPP_ATTRIBUTE
;
1088 ipp
->current
= NULL
;
1089 ipp
->curtag
= IPP_TAG_ZERO
;
1090 ipp
->prev
= ipp
->last
;
1093 * If blocking is disabled, stop here...
1099 case IPP_ATTRIBUTE
:
1100 while ((*cb
)(src
, buffer
, 1) > 0)
1102 DEBUG_printf(("ippReadIO: ipp->current=%p, ipp->prev=%p\n",
1103 ipp
->current
, ipp
->prev
));
1106 * Read this attribute...
1109 tag
= (ipp_tag_t
)buffer
[0];
1111 if (tag
== IPP_TAG_END
)
1114 * No more attributes left...
1117 DEBUG_puts("ippReadIO: IPP_TAG_END!");
1119 ipp
->state
= IPP_DATA
;
1122 else if (tag
< IPP_TAG_UNSUPPORTED_VALUE
)
1125 * Group tag... Set the current group and continue...
1128 if (ipp
->curtag
== tag
)
1129 ipp
->prev
= ippAddSeparator(ipp
);
1130 else if (ipp
->current
)
1131 ipp
->prev
= ipp
->current
;
1134 ipp
->current
= NULL
;
1135 DEBUG_printf(("ippReadIO: group tag = %x, ipp->prev=%p\n", tag
,
1140 DEBUG_printf(("ippReadIO: value tag = %x\n", tag
));
1146 if ((*cb
)(src
, buffer
, 2) < 2)
1148 DEBUG_puts("ippReadIO: unable to read name length!");
1152 n
= (buffer
[0] << 8) | buffer
[1];
1154 if (n
> (sizeof(buffer
) - 1))
1156 DEBUG_printf(("ippReadIO: bad name length %d!\n", n
));
1160 DEBUG_printf(("ippReadIO: name length = %d\n", n
));
1162 if (n
== 0 && tag
!= IPP_TAG_MEMBERNAME
&&
1163 tag
!= IPP_TAG_END_COLLECTION
)
1166 * More values for current attribute...
1169 if (ipp
->current
== NULL
)
1172 attr
= ipp
->current
;
1173 value_tag
= (ipp_tag_t
)(attr
->value_tag
& IPP_TAG_MASK
);
1176 * Make sure we aren't adding a new value of a different
1180 if (value_tag
== IPP_TAG_ZERO
)
1183 * Setting the value of a collection member...
1186 attr
->value_tag
= tag
;
1188 else if (value_tag
== IPP_TAG_STRING
||
1189 (value_tag
>= IPP_TAG_TEXTLANG
&&
1190 value_tag
<= IPP_TAG_MIMETYPE
))
1193 * String values can sometimes come across in different
1194 * forms; accept sets of differing values...
1197 if (tag
!= IPP_TAG_STRING
&&
1198 (tag
< IPP_TAG_TEXTLANG
|| tag
> IPP_TAG_MIMETYPE
))
1201 else if (value_tag
!= tag
)
1205 * Finally, reallocate the attribute array as needed...
1208 if (attr
->num_values
== 1 ||
1209 (attr
->num_values
> 0 &&
1210 (attr
->num_values
& (IPP_MAX_VALUES
- 1)) == 0))
1212 ipp_attribute_t
*temp
; /* Pointer to new buffer */
1215 DEBUG_printf(("ippReadIO: reallocating for up to %d values...\n",
1216 attr
->num_values
+ IPP_MAX_VALUES
));
1219 * Reallocate memory...
1222 if ((temp
= realloc(attr
, sizeof(ipp_attribute_t
) +
1223 (attr
->num_values
+ IPP_MAX_VALUES
- 1) *
1224 sizeof(ipp_value_t
))) == NULL
)
1230 * Reset pointers in the list...
1234 ipp
->prev
->next
= temp
;
1238 attr
= ipp
->current
= ipp
->last
= temp
;
1242 else if (tag
== IPP_TAG_MEMBERNAME
)
1245 * Name must be length 0!
1250 DEBUG_puts("ippReadIO: member name not empty!");
1255 ipp
->prev
= ipp
->current
;
1257 attr
= ipp
->current
= _ipp_add_attr(ipp
, 1);
1259 DEBUG_printf(("ippReadIO: membername, ipp->current=%p, ipp->prev=%p\n",
1260 ipp
->current
, ipp
->prev
));
1262 attr
->group_tag
= ipp
->curtag
;
1263 attr
->value_tag
= IPP_TAG_ZERO
;
1264 attr
->num_values
= 0;
1266 else if (tag
!= IPP_TAG_END_COLLECTION
)
1269 * New attribute; read the name and add it...
1272 if ((*cb
)(src
, buffer
, n
) < n
)
1274 DEBUG_puts("ippReadIO: unable to read name!");
1281 ipp
->prev
= ipp
->current
;
1283 attr
= ipp
->current
= _ipp_add_attr(ipp
, 1);
1285 DEBUG_printf(("ippReadIO: name=\'%s\', ipp->current=%p, ipp->prev=%p\n",
1286 buffer
, ipp
->current
, ipp
->prev
));
1288 attr
->group_tag
= ipp
->curtag
;
1289 attr
->value_tag
= tag
;
1290 attr
->name
= _cups_sp_alloc((char *)buffer
);
1291 attr
->num_values
= 0;
1296 if (tag
!= IPP_TAG_END_COLLECTION
)
1297 value
= attr
->values
+ attr
->num_values
;
1301 if ((*cb
)(src
, buffer
, 2) < 2)
1303 DEBUG_puts("ippReadIO: unable to read value length!");
1307 n
= (buffer
[0] << 8) | buffer
[1];
1308 DEBUG_printf(("ippReadIO: value length = %d\n", n
));
1312 case IPP_TAG_INTEGER
:
1314 if ((*cb
)(src
, buffer
, 4) < 4)
1316 DEBUG_puts("ippReadIO: Unable to read integer value!");
1320 n
= (((((buffer
[0] << 8) | buffer
[1]) << 8) | buffer
[2]) << 8) |
1325 case IPP_TAG_BOOLEAN
:
1326 if ((*cb
)(src
, buffer
, 1) < 1)
1328 DEBUG_puts("ippReadIO: Unable to read boolean value!");
1332 value
->boolean
= buffer
[0];
1336 case IPP_TAG_KEYWORD
:
1337 case IPP_TAG_STRING
:
1339 case IPP_TAG_URISCHEME
:
1340 case IPP_TAG_CHARSET
:
1341 case IPP_TAG_LANGUAGE
:
1342 case IPP_TAG_MIMETYPE
:
1343 if ((*cb
)(src
, buffer
, n
) < n
)
1345 DEBUG_puts("ippReadIO: unable to read name!");
1350 value
->string
.text
= _cups_sp_alloc((char *)buffer
);
1351 DEBUG_printf(("ippReadIO: value = \'%s\'\n",
1352 value
->string
.text
));
1355 if ((*cb
)(src
, value
->date
, 11) < 11)
1357 DEBUG_puts("ippReadIO: Unable to date integer value!");
1361 case IPP_TAG_RESOLUTION
:
1362 if ((*cb
)(src
, buffer
, 9) < 9)
1364 DEBUG_puts("ippReadIO: Unable to read resolution value!");
1368 value
->resolution
.xres
=
1369 (((((buffer
[0] << 8) | buffer
[1]) << 8) | buffer
[2]) << 8) |
1371 value
->resolution
.yres
=
1372 (((((buffer
[4] << 8) | buffer
[5]) << 8) | buffer
[6]) << 8) |
1374 value
->resolution
.units
=
1375 (ipp_res_t
)buffer
[8];
1377 case IPP_TAG_RANGE
:
1378 if ((*cb
)(src
, buffer
, 8) < 8)
1380 DEBUG_puts("ippReadIO: Unable to read range value!");
1384 value
->range
.lower
=
1385 (((((buffer
[0] << 8) | buffer
[1]) << 8) | buffer
[2]) << 8) |
1387 value
->range
.upper
=
1388 (((((buffer
[4] << 8) | buffer
[5]) << 8) | buffer
[6]) << 8) |
1391 case IPP_TAG_TEXTLANG
:
1392 case IPP_TAG_NAMELANG
:
1393 if (n
> sizeof(buffer
) || n
< 4)
1395 DEBUG_printf(("ippReadIO: bad value length %d!\n", n
));
1399 if ((*cb
)(src
, buffer
, n
) < n
)
1401 DEBUG_puts("ippReadIO: Unable to read string w/language value!");
1408 * text-with-language and name-with-language are composite
1417 n
= (bufptr
[0] << 8) | bufptr
[1];
1419 if (n
>= sizeof(string
))
1421 memcpy(string
, bufptr
+ 2, sizeof(string
) - 1);
1422 string
[sizeof(string
) - 1] = '\0';
1426 memcpy(string
, bufptr
+ 2, n
);
1430 value
->string
.charset
= _cups_sp_alloc((char *)string
);
1433 n
= (bufptr
[0] << 8) | bufptr
[1];
1435 bufptr
[2 + n
] = '\0';
1436 value
->string
.text
= _cups_sp_alloc((char *)bufptr
+ 2);
1439 case IPP_TAG_BEGIN_COLLECTION
:
1441 * Oh, boy, here comes a collection value, so read it...
1444 value
->collection
= ippNew();
1448 DEBUG_puts("ippReadIO: begCollection tag with value length > 0!");
1452 if (ippReadIO(src
, cb
, 1, ipp
, value
->collection
) == IPP_ERROR
)
1454 DEBUG_puts("ippReadIO: Unable to read collection value!");
1459 case IPP_TAG_END_COLLECTION
:
1462 DEBUG_puts("ippReadIO: endCollection tag with value length > 0!");
1466 DEBUG_puts("ippReadIO: endCollection tag...");
1468 return (ipp
->state
= IPP_DATA
);
1470 case IPP_TAG_MEMBERNAME
:
1472 * The value the name of the member in the collection, which
1473 * we need to carry over...
1476 if ((*cb
)(src
, buffer
, n
) < n
)
1478 DEBUG_puts("ippReadIO: Unable to read member name value!");
1483 attr
->name
= _cups_sp_alloc((char *)buffer
);
1486 * Since collection members are encoded differently than
1487 * regular attributes, make sure we don't start with an
1491 attr
->num_values
--;
1493 DEBUG_printf(("ippReadIO: member name = \"%s\"\n", attr
->name
));
1496 default : /* Other unsupported values */
1497 value
->unknown
.length
= n
;
1500 value
->unknown
.data
= malloc(n
);
1501 if ((*cb
)(src
, value
->unknown
.data
, n
) < n
)
1503 DEBUG_puts("ippReadIO: Unable to read unsupported value!");
1508 value
->unknown
.data
= NULL
;
1512 attr
->num_values
++;
1515 * If blocking is disabled, stop here...
1527 break; /* anti-compiler-warning-code */
1530 return (ipp
->state
);
1535 * 'ippTimeToDate()' - Convert from UNIX time to RFC 1903 format.
1538 const ipp_uchar_t
* /* O - RFC-1903 date/time data */
1539 ippTimeToDate(time_t t
) /* I - UNIX time value */
1541 struct tm
*unixdate
; /* UNIX unixdate/time info */
1542 ipp_uchar_t
*date
= _cupsGlobals()->ipp_date
;
1543 /* RFC-1903 date/time data */
1547 * RFC-1903 date/time format is:
1549 * Byte(s) Description
1550 * ------- -----------
1551 * 0-1 Year (0 to 65535)
1555 * 5 Minutes (0 to 59)
1556 * 6 Seconds (0 to 60, 60 = "leap second")
1557 * 7 Deciseconds (0 to 9)
1559 * 9 UTC hours (0 to 11)
1560 * 10 UTC minutes (0 to 59)
1563 unixdate
= gmtime(&t
);
1564 unixdate
->tm_year
+= 1900;
1566 date
[0] = unixdate
->tm_year
>> 8;
1567 date
[1] = unixdate
->tm_year
;
1568 date
[2] = unixdate
->tm_mon
+ 1;
1569 date
[3] = unixdate
->tm_mday
;
1570 date
[4] = unixdate
->tm_hour
;
1571 date
[5] = unixdate
->tm_min
;
1572 date
[6] = unixdate
->tm_sec
;
1583 * 'ippWrite()' - Write data for an IPP message to a HTTP connection.
1586 ipp_state_t
/* O - Current state */
1587 ippWrite(http_t
*http
, /* I - HTTP connection */
1588 ipp_t
*ipp
) /* I - IPP data */
1590 DEBUG_printf(("ippWrite(%p, %p)\n", http
, ipp
));
1595 return (ippWriteIO(http
, (ipp_iocb_t
)httpWrite2
,
1596 http
->blocking
, NULL
, ipp
));
1601 * 'ippWriteFile()' - Write data for an IPP message to a file.
1603 * @since CUPS 1.1.19@
1606 ipp_state_t
/* O - Current state */
1607 ippWriteFile(int fd
, /* I - HTTP data */
1608 ipp_t
*ipp
) /* I - IPP data */
1610 DEBUG_printf(("ippWriteFile(%d, %p)\n", fd
, ipp
));
1612 ipp
->state
= IPP_IDLE
;
1614 return (ippWriteIO(&fd
, (ipp_iocb_t
)ipp_write_file
, 1, NULL
, ipp
));
1619 * 'ippWriteIO()' - Write data for an IPP message.
1624 ipp_state_t
/* O - Current state */
1625 ippWriteIO(void *dst
, /* I - Destination */
1626 ipp_iocb_t cb
, /* I - Write callback function */
1627 int blocking
, /* I - Use blocking IO? */
1628 ipp_t
*parent
, /* I - Parent IPP message */
1629 ipp_t
*ipp
) /* I - IPP data */
1631 int i
; /* Looping var */
1632 int n
; /* Length of data */
1633 unsigned char buffer
[32768], /* Data buffer */
1634 *bufptr
; /* Pointer into buffer */
1635 ipp_attribute_t
*attr
; /* Current attribute */
1636 ipp_value_t
*value
; /* Current value */
1639 DEBUG_printf(("ippWriteIO(%p, %p, %d, %p, %p)\n", dst
, cb
, blocking
,
1642 if (dst
== NULL
|| ipp
== NULL
)
1648 ipp
->state
++; /* Avoid common problem... */
1654 * Send the request header:
1657 * Operation/Status Code = 2 bytes
1658 * Request ID = 4 bytes
1664 *bufptr
++ = ipp
->request
.any
.version
[0];
1665 *bufptr
++ = ipp
->request
.any
.version
[1];
1666 *bufptr
++ = ipp
->request
.any
.op_status
>> 8;
1667 *bufptr
++ = ipp
->request
.any
.op_status
;
1668 *bufptr
++ = ipp
->request
.any
.request_id
>> 24;
1669 *bufptr
++ = ipp
->request
.any
.request_id
>> 16;
1670 *bufptr
++ = ipp
->request
.any
.request_id
>> 8;
1671 *bufptr
++ = ipp
->request
.any
.request_id
;
1673 if ((*cb
)(dst
, buffer
, (int)(bufptr
- buffer
)) < 0)
1675 DEBUG_puts("ippWrite: Could not write IPP header...");
1681 * Reset the state engine to point to the first attribute
1682 * in the request/response, with no current group.
1685 ipp
->state
= IPP_ATTRIBUTE
;
1686 ipp
->current
= ipp
->attrs
;
1687 ipp
->curtag
= IPP_TAG_ZERO
;
1689 DEBUG_printf(("ippWrite: version=%d.%d\n", buffer
[0], buffer
[1]));
1690 DEBUG_printf(("ippWrite: op_status=%04x\n", ipp
->request
.any
.op_status
));
1691 DEBUG_printf(("ippWrite: request_id=%d\n", ipp
->request
.any
.request_id
));
1694 * If blocking is disabled, stop here...
1700 case IPP_ATTRIBUTE
:
1701 while (ipp
->current
!= NULL
)
1704 * Write this attribute...
1708 attr
= ipp
->current
;
1710 ipp
->current
= ipp
->current
->next
;
1712 if (ipp
->curtag
!= attr
->group_tag
&& parent
== NULL
)
1715 * Send a group tag byte...
1718 ipp
->curtag
= attr
->group_tag
;
1720 if (attr
->group_tag
== IPP_TAG_ZERO
)
1723 DEBUG_printf(("ippWrite: wrote group tag = %x\n", attr
->group_tag
));
1724 *bufptr
++ = attr
->group_tag
;
1726 else if (attr
->group_tag
== IPP_TAG_ZERO
)
1730 * Write the attribute tag and name. The current implementation
1731 * does not support the extension value tags above 0x7f, so all
1732 * value tags are 1 byte.
1734 * The attribute name length does not include the trailing nul
1735 * character in the source string.
1737 * Collection values (parent != NULL) are written differently...
1743 * Get the length of the attribute name, and make sure it won't
1744 * overflow the buffer...
1747 if ((n
= (int)strlen(attr
->name
)) > (sizeof(buffer
) - 4))
1751 * Write the value tag, name length, and name string...
1754 DEBUG_printf(("ippWrite: writing value tag = %x\n", attr
->value_tag
));
1755 DEBUG_printf(("ippWrite: writing name = %d, \'%s\'\n", n
, attr
->name
));
1757 *bufptr
++ = attr
->value_tag
;
1760 memcpy(bufptr
, attr
->name
, n
);
1766 * Get the length of the attribute name, and make sure it won't
1767 * overflow the buffer...
1770 if ((n
= (int)strlen(attr
->name
)) > (sizeof(buffer
) - 7))
1774 * Write the member name tag, name length, name string, value tag,
1775 * and empty name for the collection member attribute...
1778 DEBUG_printf(("ippWrite: writing value tag = %x\n",
1779 IPP_TAG_MEMBERNAME
));
1780 DEBUG_printf(("ippWrite: writing name = %d, \'%s\'\n", n
, attr
->name
));
1781 DEBUG_printf(("ippWrite: writing value tag = %x\n", attr
->value_tag
));
1782 DEBUG_puts("ippWrite: writing name = 0, \'\'\n");
1784 *bufptr
++ = IPP_TAG_MEMBERNAME
;
1789 memcpy(bufptr
, attr
->name
, n
);
1792 *bufptr
++ = attr
->value_tag
;
1798 * Now write the attribute value(s)...
1801 switch (attr
->value_tag
& ~IPP_TAG_COPY
)
1803 case IPP_TAG_INTEGER
:
1805 for (i
= 0, value
= attr
->values
;
1806 i
< attr
->num_values
;
1809 if ((sizeof(buffer
) - (bufptr
- buffer
)) < 9)
1811 if ((*cb
)(dst
, buffer
, (int)(bufptr
- buffer
)) < 0)
1813 DEBUG_puts("ippWrite: Could not write IPP attribute...");
1823 * Arrays and sets are done by sending additional
1824 * values with a zero-length name...
1827 *bufptr
++ = attr
->value_tag
;
1833 * Integers and enumerations are both 4-byte signed
1834 * (twos-complement) values.
1836 * Put the 2-byte length and 4-byte value into the buffer...
1841 *bufptr
++ = value
->integer
>> 24;
1842 *bufptr
++ = value
->integer
>> 16;
1843 *bufptr
++ = value
->integer
>> 8;
1844 *bufptr
++ = value
->integer
;
1848 case IPP_TAG_BOOLEAN
:
1849 for (i
= 0, value
= attr
->values
;
1850 i
< attr
->num_values
;
1853 if ((sizeof(buffer
) - (bufptr
- buffer
)) < 6)
1855 if ((*cb
)(dst
, buffer
, (int)(bufptr
- buffer
)) < 0)
1857 DEBUG_puts("ippWrite: Could not write IPP attribute...");
1867 * Arrays and sets are done by sending additional
1868 * values with a zero-length name...
1871 *bufptr
++ = attr
->value_tag
;
1877 * Boolean values are 1-byte; 0 = false, 1 = true.
1879 * Put the 2-byte length and 1-byte value into the buffer...
1884 *bufptr
++ = value
->boolean
;
1890 case IPP_TAG_KEYWORD
:
1891 case IPP_TAG_STRING
:
1893 case IPP_TAG_URISCHEME
:
1894 case IPP_TAG_CHARSET
:
1895 case IPP_TAG_LANGUAGE
:
1896 case IPP_TAG_MIMETYPE
:
1897 for (i
= 0, value
= attr
->values
;
1898 i
< attr
->num_values
;
1904 * Arrays and sets are done by sending additional
1905 * values with a zero-length name...
1908 DEBUG_printf(("ippWrite: writing value tag = %x\n",
1910 DEBUG_printf(("ippWrite: writing name = 0, \'\'\n"));
1912 if ((sizeof(buffer
) - (bufptr
- buffer
)) < 3)
1914 if ((*cb
)(dst
, buffer
, (int)(bufptr
- buffer
)) < 0)
1916 DEBUG_puts("ippWrite: Could not write IPP attribute...");
1923 *bufptr
++ = attr
->value_tag
;
1928 if (value
->string
.text
!= NULL
)
1929 n
= (int)strlen(value
->string
.text
);
1933 if (n
> (sizeof(buffer
) - 2))
1936 DEBUG_printf(("ippWrite: writing string = %d, \'%s\'\n", n
,
1937 value
->string
.text
));
1939 if ((int)(sizeof(buffer
) - (bufptr
- buffer
)) < (n
+ 2))
1941 if ((*cb
)(dst
, buffer
, (int)(bufptr
- buffer
)) < 0)
1943 DEBUG_puts("ippWrite: Could not write IPP attribute...");
1951 * All simple strings consist of the 2-byte length and
1952 * character data without the trailing nul normally found
1953 * in C strings. Also, strings cannot be longer than 32767
1954 * bytes since the 2-byte length is a signed (twos-complement)
1957 * Put the 2-byte length and string characters in the buffer.
1965 memcpy(bufptr
, value
->string
.text
, n
);
1972 for (i
= 0, value
= attr
->values
;
1973 i
< attr
->num_values
;
1976 if ((sizeof(buffer
) - (bufptr
- buffer
)) < 16)
1978 if ((*cb
)(dst
, buffer
, (int)(bufptr
- buffer
)) < 0)
1980 DEBUG_puts("ippWrite: Could not write IPP attribute...");
1990 * Arrays and sets are done by sending additional
1991 * values with a zero-length name...
1994 *bufptr
++ = attr
->value_tag
;
2000 * Date values consist of a 2-byte length and an
2001 * 11-byte date/time structure defined by RFC 1903.
2003 * Put the 2-byte length and 11-byte date/time
2004 * structure in the buffer.
2009 memcpy(bufptr
, value
->date
, 11);
2014 case IPP_TAG_RESOLUTION
:
2015 for (i
= 0, value
= attr
->values
;
2016 i
< attr
->num_values
;
2019 if ((sizeof(buffer
) - (bufptr
- buffer
)) < 14)
2021 if ((*cb
)(dst
, buffer
, (int)(bufptr
- buffer
)) < 0)
2023 DEBUG_puts("ippWrite: Could not write IPP attribute...");
2033 * Arrays and sets are done by sending additional
2034 * values with a zero-length name...
2037 *bufptr
++ = attr
->value_tag
;
2043 * Resolution values consist of a 2-byte length,
2044 * 4-byte horizontal resolution value, 4-byte vertical
2045 * resolution value, and a 1-byte units value.
2047 * Put the 2-byte length and resolution value data
2053 *bufptr
++ = value
->resolution
.xres
>> 24;
2054 *bufptr
++ = value
->resolution
.xres
>> 16;
2055 *bufptr
++ = value
->resolution
.xres
>> 8;
2056 *bufptr
++ = value
->resolution
.xres
;
2057 *bufptr
++ = value
->resolution
.yres
>> 24;
2058 *bufptr
++ = value
->resolution
.yres
>> 16;
2059 *bufptr
++ = value
->resolution
.yres
>> 8;
2060 *bufptr
++ = value
->resolution
.yres
;
2061 *bufptr
++ = value
->resolution
.units
;
2065 case IPP_TAG_RANGE
:
2066 for (i
= 0, value
= attr
->values
;
2067 i
< attr
->num_values
;
2070 if ((sizeof(buffer
) - (bufptr
- buffer
)) < 13)
2072 if ((*cb
)(dst
, buffer
, (int)(bufptr
- buffer
)) < 0)
2074 DEBUG_puts("ippWrite: Could not write IPP attribute...");
2084 * Arrays and sets are done by sending additional
2085 * values with a zero-length name...
2088 *bufptr
++ = attr
->value_tag
;
2094 * Range values consist of a 2-byte length,
2095 * 4-byte lower value, and 4-byte upper value.
2097 * Put the 2-byte length and range value data
2103 *bufptr
++ = value
->range
.lower
>> 24;
2104 *bufptr
++ = value
->range
.lower
>> 16;
2105 *bufptr
++ = value
->range
.lower
>> 8;
2106 *bufptr
++ = value
->range
.lower
;
2107 *bufptr
++ = value
->range
.upper
>> 24;
2108 *bufptr
++ = value
->range
.upper
>> 16;
2109 *bufptr
++ = value
->range
.upper
>> 8;
2110 *bufptr
++ = value
->range
.upper
;
2114 case IPP_TAG_TEXTLANG
:
2115 case IPP_TAG_NAMELANG
:
2116 for (i
= 0, value
= attr
->values
;
2117 i
< attr
->num_values
;
2123 * Arrays and sets are done by sending additional
2124 * values with a zero-length name...
2127 if ((sizeof(buffer
) - (bufptr
- buffer
)) < 3)
2129 if ((*cb
)(dst
, buffer
, (int)(bufptr
- buffer
)) < 0)
2131 DEBUG_puts("ippWrite: Could not write IPP attribute...");
2138 *bufptr
++ = attr
->value_tag
;
2144 * textWithLanguage and nameWithLanguage values consist
2145 * of a 2-byte length for both strings and their
2146 * individual lengths, a 2-byte length for the
2147 * character string, the character string without the
2148 * trailing nul, a 2-byte length for the character
2149 * set string, and the character set string without
2155 if (value
->string
.charset
!= NULL
)
2156 n
+= strlen(value
->string
.charset
);
2158 if (value
->string
.text
!= NULL
)
2159 n
+= strlen(value
->string
.text
);
2161 if (n
> (sizeof(buffer
) - 2))
2164 if ((int)(sizeof(buffer
) - (bufptr
- buffer
)) < (n
+ 2))
2166 if ((*cb
)(dst
, buffer
, (int)(bufptr
- buffer
)) < 0)
2168 DEBUG_puts("ippWrite: Could not write IPP attribute...");
2175 /* Length of entire value */
2179 /* Length of charset */
2180 if (value
->string
.charset
!= NULL
)
2181 n
= (int)strlen(value
->string
.charset
);
2191 memcpy(bufptr
, value
->string
.charset
, n
);
2195 /* Length of text */
2196 if (value
->string
.text
!= NULL
)
2197 n
= (int)strlen(value
->string
.text
);
2207 memcpy(bufptr
, value
->string
.text
, n
);
2213 case IPP_TAG_BEGIN_COLLECTION
:
2214 for (i
= 0, value
= attr
->values
;
2215 i
< attr
->num_values
;
2219 * Collections are written with the begin-collection
2220 * tag first with a value of 0 length, followed by the
2221 * attributes in the collection, then the end-collection
2225 if ((sizeof(buffer
) - (bufptr
- buffer
)) < 5)
2227 if ((*cb
)(dst
, buffer
, (int)(bufptr
- buffer
)) < 0)
2229 DEBUG_puts("ippWrite: Could not write IPP attribute...");
2239 * Arrays and sets are done by sending additional
2240 * values with a zero-length name...
2243 *bufptr
++ = attr
->value_tag
;
2249 * Write a data length of 0 and flush the buffer...
2255 if ((*cb
)(dst
, buffer
, (int)(bufptr
- buffer
)) < 0)
2257 DEBUG_puts("ippWrite: Could not write IPP attribute...");
2264 * Then write the collection attribute...
2267 value
->collection
->state
= IPP_IDLE
;
2269 if (ippWriteIO(dst
, cb
, 1, ipp
, value
->collection
) == IPP_ERROR
)
2275 for (i
= 0, value
= attr
->values
;
2276 i
< attr
->num_values
;
2282 * Arrays and sets are done by sending additional
2283 * values with a zero-length name...
2286 if ((sizeof(buffer
) - (bufptr
- buffer
)) < 3)
2288 if ((*cb
)(dst
, buffer
, (int)(bufptr
- buffer
)) < 0)
2290 DEBUG_puts("ippWrite: Could not write IPP attribute...");
2297 *bufptr
++ = attr
->value_tag
;
2303 * An unknown value might some new value that a
2304 * vendor has come up with. It consists of a
2305 * 2-byte length and the bytes in the unknown
2309 n
= value
->unknown
.length
;
2311 if (n
> (sizeof(buffer
) - 2))
2314 if ((int)(sizeof(buffer
) - (bufptr
- buffer
)) < (n
+ 2))
2316 if ((*cb
)(dst
, buffer
, (int)(bufptr
- buffer
)) < 0)
2318 DEBUG_puts("ippWrite: Could not write IPP attribute...");
2325 /* Length of unknown value */
2332 memcpy(bufptr
, value
->unknown
.data
, n
);
2340 * Write the data out...
2343 if ((*cb
)(dst
, buffer
, (int)(bufptr
- buffer
)) < 0)
2345 DEBUG_puts("ippWrite: Could not write IPP attribute...");
2349 DEBUG_printf(("ippWrite: wrote %d bytes\n", bufptr
- buffer
));
2352 * If blocking is disabled, stop here...
2359 if (ipp
->current
== NULL
)
2362 * Done with all of the attributes; add the end-of-attributes
2363 * tag or end-collection attribute...
2368 buffer
[0] = IPP_TAG_END
;
2373 buffer
[0] = IPP_TAG_END_COLLECTION
;
2374 buffer
[1] = 0; /* empty name */
2376 buffer
[3] = 0; /* empty value */
2381 if ((*cb
)(dst
, buffer
, n
) < 0)
2383 DEBUG_puts("ippWrite: Could not write IPP end-tag...");
2387 ipp
->state
= IPP_DATA
;
2395 break; /* anti-compiler-warning-code */
2398 return (ipp
->state
);
2403 * '_ipp_add_attr()' - Add a new attribute to the request.
2406 ipp_attribute_t
* /* O - New attribute */
2407 _ipp_add_attr(ipp_t
*ipp
, /* I - IPP message */
2408 int num_values
) /* I - Number of values */
2410 ipp_attribute_t
*attr
; /* New attribute */
2413 DEBUG_printf(("_ipp_add_attr(%p, %d)\n", ipp
, num_values
));
2415 if (ipp
== NULL
|| num_values
< 0)
2418 attr
= calloc(sizeof(ipp_attribute_t
) +
2419 (num_values
- 1) * sizeof(ipp_value_t
), 1);
2423 attr
->num_values
= num_values
;
2425 if (ipp
->last
== NULL
)
2428 ipp
->last
->next
= attr
;
2433 DEBUG_printf(("_ipp_add_attr(): %p\n", attr
));
2440 * '_ipp_free_attr()' - Free an attribute.
2444 _ipp_free_attr(ipp_attribute_t
*attr
) /* I - Attribute to free */
2446 int i
; /* Looping var */
2447 ipp_value_t
*value
; /* Current value */
2450 DEBUG_printf(("_ipp_free_attr(): %p\n", attr
));
2452 switch (attr
->value_tag
)
2456 case IPP_TAG_KEYWORD
:
2457 case IPP_TAG_STRING
:
2459 case IPP_TAG_URISCHEME
:
2460 case IPP_TAG_CHARSET
:
2461 case IPP_TAG_LANGUAGE
:
2462 case IPP_TAG_MIMETYPE
:
2463 for (i
= 0, value
= attr
->values
;
2464 i
< attr
->num_values
;
2466 _cups_sp_free(value
->string
.text
);
2469 case IPP_TAG_TEXTLANG
:
2470 case IPP_TAG_NAMELANG
:
2471 for (i
= 0, value
= attr
->values
;
2472 i
< attr
->num_values
;
2475 if (value
->string
.charset
&& i
== 0)
2476 _cups_sp_free(value
->string
.charset
);
2477 _cups_sp_free(value
->string
.text
);
2481 case IPP_TAG_INTEGER
:
2483 case IPP_TAG_BOOLEAN
:
2485 case IPP_TAG_RESOLUTION
:
2486 case IPP_TAG_RANGE
:
2489 case IPP_TAG_BEGIN_COLLECTION
:
2490 for (i
= 0, value
= attr
->values
;
2491 i
< attr
->num_values
;
2493 ippDelete(value
->collection
);
2497 if (!((int)attr
->value_tag
& IPP_TAG_COPY
))
2499 for (i
= 0, value
= attr
->values
;
2500 i
< attr
->num_values
;
2502 if (value
->unknown
.data
)
2503 free(value
->unknown
.data
);
2509 _cups_sp_free(attr
->name
);
2516 * 'ipp_length()' - Compute the length of an IPP message or collection value.
2519 static size_t /* O - Size of IPP message */
2520 ipp_length(ipp_t
*ipp
, /* I - IPP message or collection */
2521 int collection
) /* I - 1 if a collection, 0 otherwise */
2523 int i
; /* Looping var */
2524 int bytes
; /* Number of bytes */
2525 ipp_attribute_t
*attr
; /* Current attribute */
2526 ipp_tag_t group
; /* Current group */
2527 ipp_value_t
*value
; /* Current value */
2534 * Start with 8 bytes for the IPP message header...
2537 bytes
= collection
? 0 : 8;
2540 * Then add the lengths of each attribute...
2543 group
= IPP_TAG_ZERO
;
2545 for (attr
= ipp
->attrs
; attr
!= NULL
; attr
= attr
->next
)
2547 if (attr
->group_tag
!= group
&& !collection
)
2549 group
= attr
->group_tag
;
2550 if (group
== IPP_TAG_ZERO
)
2553 bytes
++; /* Group tag */
2559 DEBUG_printf(("attr->name = %s, attr->num_values = %d, bytes = %d\n",
2560 attr
->name
, attr
->num_values
, bytes
));
2562 bytes
+= strlen(attr
->name
); /* Name */
2563 bytes
+= attr
->num_values
; /* Value tag for each value */
2564 bytes
+= 2 * attr
->num_values
; /* Name lengths */
2565 bytes
+= 2 * attr
->num_values
; /* Value lengths */
2568 bytes
+= 5; /* Add membername overhead */
2570 switch (attr
->value_tag
& ~IPP_TAG_COPY
)
2572 case IPP_TAG_INTEGER
:
2574 bytes
+= 4 * attr
->num_values
;
2577 case IPP_TAG_BOOLEAN
:
2578 bytes
+= attr
->num_values
;
2583 case IPP_TAG_KEYWORD
:
2584 case IPP_TAG_STRING
:
2586 case IPP_TAG_URISCHEME
:
2587 case IPP_TAG_CHARSET
:
2588 case IPP_TAG_LANGUAGE
:
2589 case IPP_TAG_MIMETYPE
:
2590 for (i
= 0, value
= attr
->values
;
2591 i
< attr
->num_values
;
2593 if (value
->string
.text
!= NULL
)
2594 bytes
+= strlen(value
->string
.text
);
2598 bytes
+= 11 * attr
->num_values
;
2601 case IPP_TAG_RESOLUTION
:
2602 bytes
+= 9 * attr
->num_values
;
2605 case IPP_TAG_RANGE
:
2606 bytes
+= 8 * attr
->num_values
;
2609 case IPP_TAG_TEXTLANG
:
2610 case IPP_TAG_NAMELANG
:
2611 bytes
+= 4 * attr
->num_values
;/* Charset + text length */
2613 for (i
= 0, value
= attr
->values
;
2614 i
< attr
->num_values
;
2617 if (value
->string
.charset
!= NULL
)
2618 bytes
+= strlen(value
->string
.charset
);
2620 if (value
->string
.text
!= NULL
)
2621 bytes
+= strlen(value
->string
.text
);
2625 case IPP_TAG_BEGIN_COLLECTION
:
2626 for (i
= 0, value
= attr
->values
;
2627 i
< attr
->num_values
;
2629 bytes
+= ipp_length(value
->collection
, 1);
2633 for (i
= 0, value
= attr
->values
;
2634 i
< attr
->num_values
;
2636 bytes
+= value
->unknown
.length
;
2642 * Finally, add 1 byte for the "end of attributes" tag or 5 bytes
2643 * for the "end of collection" tag and return...
2651 DEBUG_printf(("bytes = %d\n", bytes
));
2658 * 'ipp_read_http()' - Semi-blocking read on a HTTP connection...
2661 static ssize_t
/* O - Number of bytes read */
2662 ipp_read_http(http_t
*http
, /* I - Client connection */
2663 ipp_uchar_t
*buffer
, /* O - Buffer for data */
2664 size_t length
) /* I - Total length */
2666 int tbytes
, /* Total bytes read */
2667 bytes
; /* Bytes read this pass */
2668 char len
[32]; /* Length string */
2671 DEBUG_printf(("ipp_read_http(http=%p, buffer=%p, length=%d)\n",
2672 http
, buffer
, length
));
2675 * Loop until all bytes are read...
2678 for (tbytes
= 0, bytes
= 0; tbytes
< length
; tbytes
+= bytes
, buffer
+= bytes
)
2680 DEBUG_printf(("tbytes = %d, http->state = %d\n", tbytes
, http
->state
));
2682 if (http
->state
== HTTP_WAITING
)
2685 if (http
->used
> 0 && http
->data_encoding
== HTTP_ENCODE_LENGTH
)
2688 * Do "fast read" from HTTP buffer directly...
2691 if (http
->used
> (length
- tbytes
))
2692 bytes
= length
- tbytes
;
2697 buffer
[0] = http
->buffer
[0];
2699 memcpy(buffer
, http
->buffer
, bytes
);
2701 http
->used
-= bytes
;
2702 http
->data_remaining
-= bytes
;
2704 if (http
->data_remaining
<= INT_MAX
)
2705 http
->_data_remaining
= (int)http
->data_remaining
;
2707 http
->_data_remaining
= INT_MAX
;
2710 memmove(http
->buffer
, http
->buffer
+ bytes
, http
->used
);
2712 if (http
->data_remaining
== 0)
2714 if (http
->data_encoding
== HTTP_ENCODE_CHUNKED
)
2717 * Get the trailing CR LF after the chunk...
2720 if (!httpGets(len
, sizeof(len
), http
))
2724 if (http
->data_encoding
!= HTTP_ENCODE_CHUNKED
)
2726 if (http
->state
== HTTP_POST_RECV
)
2729 http
->state
= HTTP_WAITING
;
2736 * Wait a maximum of 1 second for data...
2739 if (!http
->blocking
)
2742 * Wait up to 1 second for more data on non-blocking sockets...
2745 if (!httpWait(http
, 1000))
2756 if ((bytes
= httpRead2(http
, (char *)buffer
, length
- tbytes
)) <= 0)
2762 * Return the number of bytes read...
2765 if (tbytes
== 0 && bytes
< 0)
2768 DEBUG_printf(("returning %d bytes...\n", tbytes
));
2775 * 'ipp_read_file()' - Read IPP data from a file.
2778 static ssize_t
/* O - Number of bytes read */
2779 ipp_read_file(int *fd
, /* I - File descriptor */
2780 ipp_uchar_t
*buffer
, /* O - Read buffer */
2781 size_t length
) /* I - Number of bytes to read */
2783 return (read(*fd
, buffer
, length
));
2788 * 'ipp_write_file()' - Write IPP data to a file.
2791 static ssize_t
/* O - Number of bytes written */
2792 ipp_write_file(int *fd
, /* I - File descriptor */
2793 ipp_uchar_t
*buffer
, /* I - Data to write */
2794 size_t length
) /* I - Number of bytes to write */
2796 return (write(*fd
, buffer
, length
));