]> git.ipfire.org Git - thirdparty/cups.git/blob - cups/ipp.c
Update Github repository to match macOS 11.2 CUPS sources (cups-494.1).
[thirdparty/cups.git] / cups / ipp.c
1 /*
2 * Internet Printing Protocol functions for CUPS.
3 *
4 * Copyright © 2007-2020 by Apple Inc.
5 * Copyright © 1997-2007 by Easy Software Products, all rights reserved.
6 *
7 * Licensed under Apache License v2.0. See the file "LICENSE" for more
8 * information.
9 */
10
11 /*
12 * Include necessary headers...
13 */
14
15 #include "cups-private.h"
16 #include "debug-internal.h"
17 #include <regex.h>
18 #ifdef _WIN32
19 # include <io.h>
20 #endif /* _WIN32 */
21
22
23 /*
24 * Local functions...
25 */
26
27 static ipp_attribute_t *ipp_add_attr(ipp_t *ipp, const char *name,
28 ipp_tag_t group_tag, ipp_tag_t value_tag,
29 int num_values);
30 static void ipp_free_values(ipp_attribute_t *attr, int element,
31 int count);
32 static char *ipp_get_code(const char *locale, char *buffer, size_t bufsize) _CUPS_NONNULL(1,2);
33 static char *ipp_lang_code(const char *locale, char *buffer, size_t bufsize) _CUPS_NONNULL(1,2);
34 static size_t ipp_length(ipp_t *ipp, int collection);
35 static ssize_t ipp_read_http(http_t *http, ipp_uchar_t *buffer,
36 size_t length);
37 static ssize_t ipp_read_file(int *fd, ipp_uchar_t *buffer,
38 size_t length);
39 static void ipp_set_error(ipp_status_t status, const char *format,
40 ...);
41 static _ipp_value_t *ipp_set_value(ipp_t *ipp, ipp_attribute_t **attr,
42 int element);
43 static ssize_t ipp_write_file(int *fd, ipp_uchar_t *buffer,
44 size_t length);
45
46
47 /*
48 * '_cupsBufferGet()' - Get a read/write buffer.
49 */
50
51 char * /* O - Buffer */
52 _cupsBufferGet(size_t size) /* I - Size required */
53 {
54 _cups_buffer_t *buffer; /* Current buffer */
55 _cups_globals_t *cg = _cupsGlobals();
56 /* Global data */
57
58
59 for (buffer = cg->cups_buffers; buffer; buffer = buffer->next)
60 if (!buffer->used && buffer->size >= size)
61 break;
62
63 if (!buffer)
64 {
65 if ((buffer = malloc(sizeof(_cups_buffer_t) + size - 1)) == NULL)
66 return (NULL);
67
68 buffer->next = cg->cups_buffers;
69 buffer->size = size;
70 cg->cups_buffers = buffer;
71 }
72
73 buffer->used = 1;
74
75 return (buffer->d);
76 }
77
78
79 /*
80 * '_cupsBufferRelease()' - Release a read/write buffer.
81 */
82
83 void
84 _cupsBufferRelease(char *b) /* I - Buffer to release */
85 {
86 _cups_buffer_t *buffer; /* Buffer */
87
88
89 /*
90 * Mark this buffer as unused...
91 */
92
93 buffer = (_cups_buffer_t *)(b - offsetof(_cups_buffer_t, d));
94 buffer->used = 0;
95 }
96
97
98 /*
99 * 'ippAddBoolean()' - Add a boolean attribute to an IPP message.
100 *
101 * The @code ipp@ parameter refers to an IPP message previously created using
102 * the @link ippNew@, @link ippNewRequest@, or @link ippNewResponse@ functions.
103 *
104 * The @code group@ parameter specifies the IPP attribute group tag: none
105 * (@code IPP_TAG_ZERO@, for member attributes), document (@code IPP_TAG_DOCUMENT@),
106 * event notification (@code IPP_TAG_EVENT_NOTIFICATION@), operation
107 * (@code IPP_TAG_OPERATION@), printer (@code IPP_TAG_PRINTER@), subscription
108 * (@code IPP_TAG_SUBSCRIPTION@), or unsupported (@code IPP_TAG_UNSUPPORTED_GROUP@).
109 */
110
111 ipp_attribute_t * /* O - New attribute */
112 ippAddBoolean(ipp_t *ipp, /* I - IPP message */
113 ipp_tag_t group, /* I - IPP group */
114 const char *name, /* I - Name of attribute */
115 char value) /* I - Value of attribute */
116 {
117 ipp_attribute_t *attr; /* New attribute */
118
119
120 DEBUG_printf(("ippAddBoolean(ipp=%p, group=%02x(%s), name=\"%s\", value=%d)", (void *)ipp, group, ippTagString(group), name, value));
121
122 /*
123 * Range check input...
124 */
125
126 if (!ipp || !name || group < IPP_TAG_ZERO ||
127 group == IPP_TAG_END || group >= IPP_TAG_UNSUPPORTED_VALUE)
128 return (NULL);
129
130 /*
131 * Create the attribute...
132 */
133
134 if ((attr = ipp_add_attr(ipp, name, group, IPP_TAG_BOOLEAN, 1)) == NULL)
135 return (NULL);
136
137 attr->values[0].boolean = value;
138
139 return (attr);
140 }
141
142
143 /*
144 * 'ippAddBooleans()' - Add an array of boolean values.
145 *
146 * The @code ipp@ parameter refers to an IPP message previously created using
147 * the @link ippNew@, @link ippNewRequest@, or @link ippNewResponse@ functions.
148 *
149 * The @code group@ parameter specifies the IPP attribute group tag: none
150 * (@code IPP_TAG_ZERO@, for member attributes), document (@code IPP_TAG_DOCUMENT@),
151 * event notification (@code IPP_TAG_EVENT_NOTIFICATION@), operation
152 * (@code IPP_TAG_OPERATION@), printer (@code IPP_TAG_PRINTER@), subscription
153 * (@code IPP_TAG_SUBSCRIPTION@), or unsupported (@code IPP_TAG_UNSUPPORTED_GROUP@).
154 */
155
156 ipp_attribute_t * /* O - New attribute */
157 ippAddBooleans(ipp_t *ipp, /* I - IPP message */
158 ipp_tag_t group, /* I - IPP group */
159 const char *name, /* I - Name of attribute */
160 int num_values, /* I - Number of values */
161 const char *values) /* I - Values */
162 {
163 int i; /* Looping var */
164 ipp_attribute_t *attr; /* New attribute */
165 _ipp_value_t *value; /* Current value */
166
167
168 DEBUG_printf(("ippAddBooleans(ipp=%p, group=%02x(%s), name=\"%s\", num_values=%d, values=%p)", (void *)ipp, group, ippTagString(group), name, num_values, (void *)values));
169
170 /*
171 * Range check input...
172 */
173
174 if (!ipp || !name || group < IPP_TAG_ZERO ||
175 group == IPP_TAG_END || group >= IPP_TAG_UNSUPPORTED_VALUE ||
176 num_values < 1)
177 return (NULL);
178
179 /*
180 * Create the attribute...
181 */
182
183 if ((attr = ipp_add_attr(ipp, name, group, IPP_TAG_BOOLEAN, num_values)) == NULL)
184 return (NULL);
185
186 if (values)
187 {
188 for (i = num_values, value = attr->values;
189 i > 0;
190 i --, value ++)
191 value->boolean = *values++;
192 }
193
194 return (attr);
195 }
196
197
198 /*
199 * 'ippAddCollection()' - Add a collection value.
200 *
201 * The @code ipp@ parameter refers to an IPP message previously created using
202 * the @link ippNew@, @link ippNewRequest@, or @link ippNewResponse@ functions.
203 *
204 * The @code group@ parameter specifies the IPP attribute group tag: none
205 * (@code IPP_TAG_ZERO@, for member attributes), document (@code IPP_TAG_DOCUMENT@),
206 * event notification (@code IPP_TAG_EVENT_NOTIFICATION@), operation
207 * (@code IPP_TAG_OPERATION@), printer (@code IPP_TAG_PRINTER@), subscription
208 * (@code IPP_TAG_SUBSCRIPTION@), or unsupported (@code IPP_TAG_UNSUPPORTED_GROUP@).
209 *
210 * @since CUPS 1.1.19/macOS 10.3@
211 */
212
213 ipp_attribute_t * /* O - New attribute */
214 ippAddCollection(ipp_t *ipp, /* I - IPP message */
215 ipp_tag_t group, /* I - IPP group */
216 const char *name, /* I - Name of attribute */
217 ipp_t *value) /* I - Value */
218 {
219 ipp_attribute_t *attr; /* New attribute */
220
221
222 DEBUG_printf(("ippAddCollection(ipp=%p, group=%02x(%s), name=\"%s\", value=%p)", (void *)ipp, group, ippTagString(group), name, (void *)value));
223
224 /*
225 * Range check input...
226 */
227
228 if (!ipp || !name || group < IPP_TAG_ZERO ||
229 group == IPP_TAG_END || group >= IPP_TAG_UNSUPPORTED_VALUE)
230 return (NULL);
231
232 /*
233 * Create the attribute...
234 */
235
236 if ((attr = ipp_add_attr(ipp, name, group, IPP_TAG_BEGIN_COLLECTION, 1)) == NULL)
237 return (NULL);
238
239 attr->values[0].collection = value;
240
241 if (value)
242 value->use ++;
243
244 return (attr);
245 }
246
247
248 /*
249 * 'ippAddCollections()' - Add an array of collection values.
250 *
251 * The @code ipp@ parameter refers to an IPP message previously created using
252 * the @link ippNew@, @link ippNewRequest@, or @link ippNewResponse@ functions.
253 *
254 * The @code group@ parameter specifies the IPP attribute group tag: none
255 * (@code IPP_TAG_ZERO@, for member attributes), document (@code IPP_TAG_DOCUMENT@),
256 * event notification (@code IPP_TAG_EVENT_NOTIFICATION@), operation
257 * (@code IPP_TAG_OPERATION@), printer (@code IPP_TAG_PRINTER@), subscription
258 * (@code IPP_TAG_SUBSCRIPTION@), or unsupported (@code IPP_TAG_UNSUPPORTED_GROUP@).
259 *
260 * @since CUPS 1.1.19/macOS 10.3@
261 */
262
263 ipp_attribute_t * /* O - New attribute */
264 ippAddCollections(
265 ipp_t *ipp, /* I - IPP message */
266 ipp_tag_t group, /* I - IPP group */
267 const char *name, /* I - Name of attribute */
268 int num_values, /* I - Number of values */
269 const ipp_t **values) /* I - Values */
270 {
271 int i; /* Looping var */
272 ipp_attribute_t *attr; /* New attribute */
273 _ipp_value_t *value; /* Current value */
274
275
276 DEBUG_printf(("ippAddCollections(ipp=%p, group=%02x(%s), name=\"%s\", num_values=%d, values=%p)", (void *)ipp, group, ippTagString(group), name, num_values, (void *)values));
277
278 /*
279 * Range check input...
280 */
281
282 if (!ipp || !name || group < IPP_TAG_ZERO ||
283 group == IPP_TAG_END || group >= IPP_TAG_UNSUPPORTED_VALUE ||
284 num_values < 1)
285 return (NULL);
286
287 /*
288 * Create the attribute...
289 */
290
291 if ((attr = ipp_add_attr(ipp, name, group, IPP_TAG_BEGIN_COLLECTION,
292 num_values)) == NULL)
293 return (NULL);
294
295 if (values)
296 {
297 for (i = num_values, value = attr->values;
298 i > 0;
299 i --, value ++)
300 {
301 value->collection = (ipp_t *)*values++;
302 value->collection->use ++;
303 }
304 }
305
306 return (attr);
307 }
308
309
310 /*
311 * 'ippAddDate()' - Add a dateTime attribute to an IPP message.
312 *
313 * The @code ipp@ parameter refers to an IPP message previously created using
314 * the @link ippNew@, @link ippNewRequest@, or @link ippNewResponse@ functions.
315 *
316 * The @code group@ parameter specifies the IPP attribute group tag: none
317 * (@code IPP_TAG_ZERO@, for member attributes), document (@code IPP_TAG_DOCUMENT@),
318 * event notification (@code IPP_TAG_EVENT_NOTIFICATION@), operation
319 * (@code IPP_TAG_OPERATION@), printer (@code IPP_TAG_PRINTER@), subscription
320 * (@code IPP_TAG_SUBSCRIPTION@), or unsupported (@code IPP_TAG_UNSUPPORTED_GROUP@).
321 */
322
323 ipp_attribute_t * /* O - New attribute */
324 ippAddDate(ipp_t *ipp, /* I - IPP message */
325 ipp_tag_t group, /* I - IPP group */
326 const char *name, /* I - Name of attribute */
327 const ipp_uchar_t *value) /* I - Value */
328 {
329 ipp_attribute_t *attr; /* New attribute */
330
331
332 DEBUG_printf(("ippAddDate(ipp=%p, group=%02x(%s), name=\"%s\", value=%p)", (void *)ipp, group, ippTagString(group), name, (void *)value));
333
334 /*
335 * Range check input...
336 */
337
338 if (!ipp || !name || !value || group < IPP_TAG_ZERO ||
339 group == IPP_TAG_END || group >= IPP_TAG_UNSUPPORTED_VALUE)
340 return (NULL);
341
342 /*
343 * Create the attribute...
344 */
345
346 if ((attr = ipp_add_attr(ipp, name, group, IPP_TAG_DATE, 1)) == NULL)
347 return (NULL);
348
349 memcpy(attr->values[0].date, value, 11);
350
351 return (attr);
352 }
353
354
355 /*
356 * 'ippAddInteger()' - Add a integer attribute to an IPP message.
357 *
358 * The @code ipp@ parameter refers to an IPP message previously created using
359 * the @link ippNew@, @link ippNewRequest@, or @link ippNewResponse@ functions.
360 *
361 * The @code group@ parameter specifies the IPP attribute group tag: none
362 * (@code IPP_TAG_ZERO@, for member attributes), document (@code IPP_TAG_DOCUMENT@),
363 * event notification (@code IPP_TAG_EVENT_NOTIFICATION@), operation
364 * (@code IPP_TAG_OPERATION@), printer (@code IPP_TAG_PRINTER@), subscription
365 * (@code IPP_TAG_SUBSCRIPTION@), or unsupported (@code IPP_TAG_UNSUPPORTED_GROUP@).
366 *
367 * Supported values include enum (@code IPP_TAG_ENUM@) and integer
368 * (@code IPP_TAG_INTEGER@).
369 */
370
371 ipp_attribute_t * /* O - New attribute */
372 ippAddInteger(ipp_t *ipp, /* I - IPP message */
373 ipp_tag_t group, /* I - IPP group */
374 ipp_tag_t value_tag, /* I - Type of attribute */
375 const char *name, /* I - Name of attribute */
376 int value) /* I - Value of attribute */
377 {
378 ipp_attribute_t *attr; /* New attribute */
379
380
381 DEBUG_printf(("ippAddInteger(ipp=%p, group=%02x(%s), type=%02x(%s), name=\"%s\", value=%d)", (void *)ipp, group, ippTagString(group), value_tag, ippTagString(value_tag), name, value));
382
383 value_tag &= IPP_TAG_CUPS_MASK;
384
385 /*
386 * Special-case for legacy usage: map out-of-band attributes to new ippAddOutOfBand
387 * function...
388 */
389
390 if (value_tag >= IPP_TAG_UNSUPPORTED_VALUE && value_tag <= IPP_TAG_ADMINDEFINE)
391 return (ippAddOutOfBand(ipp, group, value_tag, name));
392
393 /*
394 * Range check input...
395 */
396
397 #if 0
398 if (!ipp || !name || group < IPP_TAG_ZERO ||
399 group == IPP_TAG_END || group >= IPP_TAG_UNSUPPORTED_VALUE ||
400 (value_tag != IPP_TAG_INTEGER && value_tag != IPP_TAG_ENUM))
401 return (NULL);
402 #else
403 if (!ipp || !name || group < IPP_TAG_ZERO ||
404 group == IPP_TAG_END || group >= IPP_TAG_UNSUPPORTED_VALUE)
405 return (NULL);
406 #endif /* 0 */
407
408 /*
409 * Create the attribute...
410 */
411
412 if ((attr = ipp_add_attr(ipp, name, group, value_tag, 1)) == NULL)
413 return (NULL);
414
415 attr->values[0].integer = value;
416
417 return (attr);
418 }
419
420
421 /*
422 * 'ippAddIntegers()' - Add an array of integer values.
423 *
424 * The @code ipp@ parameter refers to an IPP message previously created using
425 * the @link ippNew@, @link ippNewRequest@, or @link ippNewResponse@ functions.
426 *
427 * The @code group@ parameter specifies the IPP attribute group tag: none
428 * (@code IPP_TAG_ZERO@, for member attributes), document (@code IPP_TAG_DOCUMENT@),
429 * event notification (@code IPP_TAG_EVENT_NOTIFICATION@), operation
430 * (@code IPP_TAG_OPERATION@), printer (@code IPP_TAG_PRINTER@), subscription
431 * (@code IPP_TAG_SUBSCRIPTION@), or unsupported (@code IPP_TAG_UNSUPPORTED_GROUP@).
432 *
433 * Supported values include enum (@code IPP_TAG_ENUM@) and integer
434 * (@code IPP_TAG_INTEGER@).
435 */
436
437 ipp_attribute_t * /* O - New attribute */
438 ippAddIntegers(ipp_t *ipp, /* I - IPP message */
439 ipp_tag_t group, /* I - IPP group */
440 ipp_tag_t value_tag, /* I - Type of attribute */
441 const char *name, /* I - Name of attribute */
442 int num_values, /* I - Number of values */
443 const int *values) /* I - Values */
444 {
445 int i; /* Looping var */
446 ipp_attribute_t *attr; /* New attribute */
447 _ipp_value_t *value; /* Current value */
448
449
450 DEBUG_printf(("ippAddIntegers(ipp=%p, group=%02x(%s), type=%02x(%s), name=\"%s\", num_values=%d, values=%p)", (void *)ipp, group, ippTagString(group), value_tag, ippTagString(value_tag), name, num_values, (void *)values));
451
452 value_tag &= IPP_TAG_CUPS_MASK;
453
454 /*
455 * Range check input...
456 */
457
458 #if 0
459 if (!ipp || !name || group < IPP_TAG_ZERO ||
460 group == IPP_TAG_END || group >= IPP_TAG_UNSUPPORTED_VALUE ||
461 (value_tag != IPP_TAG_INTEGER && value_tag != IPP_TAG_ENUM) ||
462 num_values < 1)
463 return (NULL);
464 #else
465 if (!ipp || !name || group < IPP_TAG_ZERO ||
466 group == IPP_TAG_END || group >= IPP_TAG_UNSUPPORTED_VALUE ||
467 num_values < 1)
468 return (NULL);
469 #endif /* 0 */
470
471 /*
472 * Create the attribute...
473 */
474
475 if ((attr = ipp_add_attr(ipp, name, group, value_tag, num_values)) == NULL)
476 return (NULL);
477
478 if (values)
479 {
480 for (i = num_values, value = attr->values;
481 i > 0;
482 i --, value ++)
483 value->integer = *values++;
484 }
485
486 return (attr);
487 }
488
489
490 /*
491 * 'ippAddOctetString()' - Add an octetString value to an IPP message.
492 *
493 * The @code ipp@ parameter refers to an IPP message previously created using
494 * the @link ippNew@, @link ippNewRequest@, or @link ippNewResponse@ functions.
495 *
496 * The @code group@ parameter specifies the IPP attribute group tag: none
497 * (@code IPP_TAG_ZERO@, for member attributes), document (@code IPP_TAG_DOCUMENT@),
498 * event notification (@code IPP_TAG_EVENT_NOTIFICATION@), operation
499 * (@code IPP_TAG_OPERATION@), printer (@code IPP_TAG_PRINTER@), subscription
500 * (@code IPP_TAG_SUBSCRIPTION@), or unsupported (@code IPP_TAG_UNSUPPORTED_GROUP@).
501 *
502 * @since CUPS 1.2/macOS 10.5@
503 */
504
505 ipp_attribute_t * /* O - New attribute */
506 ippAddOctetString(ipp_t *ipp, /* I - IPP message */
507 ipp_tag_t group, /* I - IPP group */
508 const char *name, /* I - Name of attribute */
509 const void *data, /* I - octetString data */
510 int datalen) /* I - Length of data in bytes */
511 {
512 ipp_attribute_t *attr; /* New attribute */
513
514
515 if (!ipp || !name || group < IPP_TAG_ZERO ||
516 group == IPP_TAG_END || group >= IPP_TAG_UNSUPPORTED_VALUE ||
517 datalen < 0 || datalen > IPP_MAX_LENGTH)
518 return (NULL);
519
520 if ((attr = ipp_add_attr(ipp, name, group, IPP_TAG_STRING, 1)) == NULL)
521 return (NULL);
522
523 /*
524 * Initialize the attribute data...
525 */
526
527 attr->values[0].unknown.length = datalen;
528
529 if (data)
530 {
531 if ((attr->values[0].unknown.data = malloc((size_t)datalen)) == NULL)
532 {
533 ippDeleteAttribute(ipp, attr);
534 return (NULL);
535 }
536
537 memcpy(attr->values[0].unknown.data, data, (size_t)datalen);
538 }
539
540 /*
541 * Return the new attribute...
542 */
543
544 return (attr);
545 }
546
547
548 /*
549 * 'ippAddOutOfBand()' - Add an out-of-band value to an IPP message.
550 *
551 * The @code ipp@ parameter refers to an IPP message previously created using
552 * the @link ippNew@, @link ippNewRequest@, or @link ippNewResponse@ functions.
553 *
554 * The @code group@ parameter specifies the IPP attribute group tag: none
555 * (@code IPP_TAG_ZERO@, for member attributes), document (@code IPP_TAG_DOCUMENT@),
556 * event notification (@code IPP_TAG_EVENT_NOTIFICATION@), operation
557 * (@code IPP_TAG_OPERATION@), printer (@code IPP_TAG_PRINTER@), subscription
558 * (@code IPP_TAG_SUBSCRIPTION@), or unsupported (@code IPP_TAG_UNSUPPORTED_GROUP@).
559 *
560 * Supported out-of-band values include unsupported-value
561 * (@code IPP_TAG_UNSUPPORTED_VALUE@), default (@code IPP_TAG_DEFAULT@), unknown
562 * (@code IPP_TAG_UNKNOWN@), no-value (@code IPP_TAG_NOVALUE@), not-settable
563 * (@code IPP_TAG_NOTSETTABLE@), delete-attribute (@code IPP_TAG_DELETEATTR@), and
564 * admin-define (@code IPP_TAG_ADMINDEFINE@).
565 *
566 * @since CUPS 1.6/macOS 10.8@
567 */
568
569 ipp_attribute_t * /* O - New attribute */
570 ippAddOutOfBand(ipp_t *ipp, /* I - IPP message */
571 ipp_tag_t group, /* I - IPP group */
572 ipp_tag_t value_tag, /* I - Type of attribute */
573 const char *name) /* I - Name of attribute */
574 {
575 DEBUG_printf(("ippAddOutOfBand(ipp=%p, group=%02x(%s), value_tag=%02x(%s), name=\"%s\")", (void *)ipp, group, ippTagString(group), value_tag, ippTagString(value_tag), name));
576
577 value_tag &= IPP_TAG_CUPS_MASK;
578
579 /*
580 * Range check input...
581 */
582
583 if (!ipp || !name || group < IPP_TAG_ZERO ||
584 group == IPP_TAG_END || group >= IPP_TAG_UNSUPPORTED_VALUE ||
585 (value_tag != IPP_TAG_UNSUPPORTED_VALUE &&
586 value_tag != IPP_TAG_DEFAULT &&
587 value_tag != IPP_TAG_UNKNOWN &&
588 value_tag != IPP_TAG_NOVALUE &&
589 value_tag != IPP_TAG_NOTSETTABLE &&
590 value_tag != IPP_TAG_DELETEATTR &&
591 value_tag != IPP_TAG_ADMINDEFINE))
592 return (NULL);
593
594 /*
595 * Create the attribute...
596 */
597
598 return (ipp_add_attr(ipp, name, group, value_tag, 1));
599 }
600
601
602 /*
603 * 'ippAddRange()' - Add a range of values to an IPP message.
604 *
605 * The @code ipp@ parameter refers to an IPP message previously created using
606 * the @link ippNew@, @link ippNewRequest@, or @link ippNewResponse@ functions.
607 *
608 * The @code group@ parameter specifies the IPP attribute group tag: none
609 * (@code IPP_TAG_ZERO@, for member attributes), document (@code IPP_TAG_DOCUMENT@),
610 * event notification (@code IPP_TAG_EVENT_NOTIFICATION@), operation
611 * (@code IPP_TAG_OPERATION@), printer (@code IPP_TAG_PRINTER@), subscription
612 * (@code IPP_TAG_SUBSCRIPTION@), or unsupported (@code IPP_TAG_UNSUPPORTED_GROUP@).
613 *
614 * The @code lower@ parameter must be less than or equal to the @code upper@ parameter.
615 */
616
617 ipp_attribute_t * /* O - New attribute */
618 ippAddRange(ipp_t *ipp, /* I - IPP message */
619 ipp_tag_t group, /* I - IPP group */
620 const char *name, /* I - Name of attribute */
621 int lower, /* I - Lower value */
622 int upper) /* I - Upper value */
623 {
624 ipp_attribute_t *attr; /* New attribute */
625
626
627 DEBUG_printf(("ippAddRange(ipp=%p, group=%02x(%s), name=\"%s\", lower=%d, upper=%d)", (void *)ipp, group, ippTagString(group), name, lower, upper));
628
629 /*
630 * Range check input...
631 */
632
633 if (!ipp || !name || group < IPP_TAG_ZERO ||
634 group == IPP_TAG_END || group >= IPP_TAG_UNSUPPORTED_VALUE)
635 return (NULL);
636
637 /*
638 * Create the attribute...
639 */
640
641 if ((attr = ipp_add_attr(ipp, name, group, IPP_TAG_RANGE, 1)) == NULL)
642 return (NULL);
643
644 attr->values[0].range.lower = lower;
645 attr->values[0].range.upper = upper;
646
647 return (attr);
648 }
649
650
651 /*
652 * 'ippAddRanges()' - Add ranges of values to an IPP message.
653 *
654 * The @code ipp@ parameter refers to an IPP message previously created using
655 * the @link ippNew@, @link ippNewRequest@, or @link ippNewResponse@ functions.
656 *
657 * The @code group@ parameter specifies the IPP attribute group tag: none
658 * (@code IPP_TAG_ZERO@, for member attributes), document (@code IPP_TAG_DOCUMENT@),
659 * event notification (@code IPP_TAG_EVENT_NOTIFICATION@), operation
660 * (@code IPP_TAG_OPERATION@), printer (@code IPP_TAG_PRINTER@), subscription
661 * (@code IPP_TAG_SUBSCRIPTION@), or unsupported (@code IPP_TAG_UNSUPPORTED_GROUP@).
662 */
663
664 ipp_attribute_t * /* O - New attribute */
665 ippAddRanges(ipp_t *ipp, /* I - IPP message */
666 ipp_tag_t group, /* I - IPP group */
667 const char *name, /* I - Name of attribute */
668 int num_values, /* I - Number of values */
669 const int *lower, /* I - Lower values */
670 const int *upper) /* I - Upper values */
671 {
672 int i; /* Looping var */
673 ipp_attribute_t *attr; /* New attribute */
674 _ipp_value_t *value; /* Current value */
675
676
677 DEBUG_printf(("ippAddRanges(ipp=%p, group=%02x(%s), name=\"%s\", num_values=%d, lower=%p, upper=%p)", (void *)ipp, group, ippTagString(group), name, num_values, (void *)lower, (void *)upper));
678
679 /*
680 * Range check input...
681 */
682
683 if (!ipp || !name || group < IPP_TAG_ZERO ||
684 group == IPP_TAG_END || group >= IPP_TAG_UNSUPPORTED_VALUE ||
685 num_values < 1)
686 return (NULL);
687
688 /*
689 * Create the attribute...
690 */
691
692 if ((attr = ipp_add_attr(ipp, name, group, IPP_TAG_RANGE, num_values)) == NULL)
693 return (NULL);
694
695 if (lower && upper)
696 {
697 for (i = num_values, value = attr->values;
698 i > 0;
699 i --, value ++)
700 {
701 value->range.lower = *lower++;
702 value->range.upper = *upper++;
703 }
704 }
705
706 return (attr);
707 }
708
709
710 /*
711 * 'ippAddResolution()' - Add a resolution value to an IPP message.
712 *
713 * The @code ipp@ parameter refers to an IPP message previously created using
714 * the @link ippNew@, @link ippNewRequest@, or @link ippNewResponse@ functions.
715 *
716 * The @code group@ parameter specifies the IPP attribute group tag: none
717 * (@code IPP_TAG_ZERO@, for member attributes), document (@code IPP_TAG_DOCUMENT@),
718 * event notification (@code IPP_TAG_EVENT_NOTIFICATION@), operation
719 * (@code IPP_TAG_OPERATION@), printer (@code IPP_TAG_PRINTER@), subscription
720 * (@code IPP_TAG_SUBSCRIPTION@), or unsupported (@code IPP_TAG_UNSUPPORTED_GROUP@).
721 */
722
723 ipp_attribute_t * /* O - New attribute */
724 ippAddResolution(ipp_t *ipp, /* I - IPP message */
725 ipp_tag_t group, /* I - IPP group */
726 const char *name, /* I - Name of attribute */
727 ipp_res_t units, /* I - Units for resolution */
728 int xres, /* I - X resolution */
729 int yres) /* I - Y resolution */
730 {
731 ipp_attribute_t *attr; /* New attribute */
732
733
734 DEBUG_printf(("ippAddResolution(ipp=%p, group=%02x(%s), name=\"%s\", units=%d, xres=%d, yres=%d)", (void *)ipp, group,
735 ippTagString(group), name, units, xres, yres));
736
737 /*
738 * Range check input...
739 */
740
741 if (!ipp || !name || group < IPP_TAG_ZERO ||
742 group == IPP_TAG_END || group >= IPP_TAG_UNSUPPORTED_VALUE ||
743 units < IPP_RES_PER_INCH || units > IPP_RES_PER_CM ||
744 xres < 0 || yres < 0)
745 return (NULL);
746
747 /*
748 * Create the attribute...
749 */
750
751 if ((attr = ipp_add_attr(ipp, name, group, IPP_TAG_RESOLUTION, 1)) == NULL)
752 return (NULL);
753
754 attr->values[0].resolution.xres = xres;
755 attr->values[0].resolution.yres = yres;
756 attr->values[0].resolution.units = units;
757
758 return (attr);
759 }
760
761
762 /*
763 * 'ippAddResolutions()' - Add resolution values to an IPP message.
764 *
765 * The @code ipp@ parameter refers to an IPP message previously created using
766 * the @link ippNew@, @link ippNewRequest@, or @link ippNewResponse@ functions.
767 *
768 * The @code group@ parameter specifies the IPP attribute group tag: none
769 * (@code IPP_TAG_ZERO@, for member attributes), document (@code IPP_TAG_DOCUMENT@),
770 * event notification (@code IPP_TAG_EVENT_NOTIFICATION@), operation
771 * (@code IPP_TAG_OPERATION@), printer (@code IPP_TAG_PRINTER@), subscription
772 * (@code IPP_TAG_SUBSCRIPTION@), or unsupported (@code IPP_TAG_UNSUPPORTED_GROUP@).
773 */
774
775 ipp_attribute_t * /* O - New attribute */
776 ippAddResolutions(ipp_t *ipp, /* I - IPP message */
777 ipp_tag_t group, /* I - IPP group */
778 const char *name, /* I - Name of attribute */
779 int num_values,/* I - Number of values */
780 ipp_res_t units, /* I - Units for resolution */
781 const int *xres, /* I - X resolutions */
782 const int *yres) /* I - Y resolutions */
783 {
784 int i; /* Looping var */
785 ipp_attribute_t *attr; /* New attribute */
786 _ipp_value_t *value; /* Current value */
787
788
789 DEBUG_printf(("ippAddResolutions(ipp=%p, group=%02x(%s), name=\"%s\", num_value=%d, units=%d, xres=%p, yres=%p)", (void *)ipp, group, ippTagString(group), name, num_values, units, (void *)xres, (void *)yres));
790
791 /*
792 * Range check input...
793 */
794
795 if (!ipp || !name || group < IPP_TAG_ZERO ||
796 group == IPP_TAG_END || group >= IPP_TAG_UNSUPPORTED_VALUE ||
797 num_values < 1 ||
798 units < IPP_RES_PER_INCH || units > IPP_RES_PER_CM)
799 return (NULL);
800
801 /*
802 * Create the attribute...
803 */
804
805 if ((attr = ipp_add_attr(ipp, name, group, IPP_TAG_RESOLUTION, num_values)) == NULL)
806 return (NULL);
807
808 if (xres && yres)
809 {
810 for (i = num_values, value = attr->values;
811 i > 0;
812 i --, value ++)
813 {
814 value->resolution.xres = *xres++;
815 value->resolution.yres = *yres++;
816 value->resolution.units = units;
817 }
818 }
819
820 return (attr);
821 }
822
823
824 /*
825 * 'ippAddSeparator()' - Add a group separator to an IPP message.
826 *
827 * The @code ipp@ parameter refers to an IPP message previously created using
828 * the @link ippNew@, @link ippNewRequest@, or @link ippNewResponse@ functions.
829 */
830
831 ipp_attribute_t * /* O - New attribute */
832 ippAddSeparator(ipp_t *ipp) /* I - IPP message */
833 {
834 DEBUG_printf(("ippAddSeparator(ipp=%p)", (void *)ipp));
835
836 /*
837 * Range check input...
838 */
839
840 if (!ipp)
841 return (NULL);
842
843 /*
844 * Create the attribute...
845 */
846
847 return (ipp_add_attr(ipp, NULL, IPP_TAG_ZERO, IPP_TAG_ZERO, 0));
848 }
849
850
851 /*
852 * 'ippAddString()' - Add a language-encoded string to an IPP message.
853 *
854 * The @code ipp@ parameter refers to an IPP message previously created using
855 * the @link ippNew@, @link ippNewRequest@, or @link ippNewResponse@ functions.
856 *
857 * The @code group@ parameter specifies the IPP attribute group tag: none
858 * (@code IPP_TAG_ZERO@, for member attributes), document (@code IPP_TAG_DOCUMENT@),
859 * event notification (@code IPP_TAG_EVENT_NOTIFICATION@), operation
860 * (@code IPP_TAG_OPERATION@), printer (@code IPP_TAG_PRINTER@), subscription
861 * (@code IPP_TAG_SUBSCRIPTION@), or unsupported (@code IPP_TAG_UNSUPPORTED_GROUP@).
862 *
863 * Supported string values include charset (@code IPP_TAG_CHARSET@), keyword
864 * (@code IPP_TAG_KEYWORD@), language (@code IPP_TAG_LANGUAGE@), mimeMediaType
865 * (@code IPP_TAG_MIMETYPE@), name (@code IPP_TAG_NAME@), nameWithLanguage
866 * (@code IPP_TAG_NAMELANG), text (@code IPP_TAG_TEXT@), textWithLanguage
867 * (@code IPP_TAG_TEXTLANG@), uri (@code IPP_TAG_URI@), and uriScheme
868 * (@code IPP_TAG_URISCHEME@).
869 *
870 * The @code language@ parameter must be non-@code NULL@ for nameWithLanguage and
871 * textWithLanguage string values and must be @code NULL@ for all other string values.
872 */
873
874 ipp_attribute_t * /* O - New attribute */
875 ippAddString(ipp_t *ipp, /* I - IPP message */
876 ipp_tag_t group, /* I - IPP group */
877 ipp_tag_t value_tag, /* I - Type of attribute */
878 const char *name, /* I - Name of attribute */
879 const char *language, /* I - Language code */
880 const char *value) /* I - Value */
881 {
882 ipp_tag_t temp_tag; /* Temporary value tag (masked) */
883 ipp_attribute_t *attr; /* New attribute */
884 char code[IPP_MAX_LANGUAGE];
885 /* Charset/language code buffer */
886
887
888 DEBUG_printf(("ippAddString(ipp=%p, group=%02x(%s), value_tag=%02x(%s), name=\"%s\", language=\"%s\", value=\"%s\")", (void *)ipp, group, ippTagString(group), value_tag, ippTagString(value_tag), name, language, value));
889
890 /*
891 * Range check input...
892 */
893
894 temp_tag = (ipp_tag_t)((int)value_tag & IPP_TAG_CUPS_MASK);
895
896 #if 0
897 if (!ipp || !name || group < IPP_TAG_ZERO ||
898 group == IPP_TAG_END || group >= IPP_TAG_UNSUPPORTED_VALUE ||
899 (temp_tag < IPP_TAG_TEXT && temp_tag != IPP_TAG_TEXTLANG &&
900 temp_tag != IPP_TAG_NAMELANG) || temp_tag > IPP_TAG_MIMETYPE)
901 return (NULL);
902
903 if ((temp_tag == IPP_TAG_TEXTLANG || temp_tag == IPP_TAG_NAMELANG)
904 != (language != NULL))
905 return (NULL);
906 #else
907 if (!ipp || !name || group < IPP_TAG_ZERO ||
908 group == IPP_TAG_END || group >= IPP_TAG_UNSUPPORTED_VALUE)
909 return (NULL);
910 #endif /* 0 */
911
912 /*
913 * See if we need to map charset, language, or locale values...
914 */
915
916 if (language && ((int)value_tag & IPP_TAG_CUPS_CONST) &&
917 strcmp(language, ipp_lang_code(language, code, sizeof(code))))
918 value_tag = temp_tag; /* Don't do a fast copy */
919 else if (value && value_tag == (ipp_tag_t)(IPP_TAG_CHARSET | IPP_TAG_CUPS_CONST) &&
920 strcmp(value, ipp_get_code(value, code, sizeof(code))))
921 value_tag = temp_tag; /* Don't do a fast copy */
922 else if (value && value_tag == (ipp_tag_t)(IPP_TAG_LANGUAGE | IPP_TAG_CUPS_CONST) &&
923 strcmp(value, ipp_lang_code(value, code, sizeof(code))))
924 value_tag = temp_tag; /* Don't do a fast copy */
925
926 /*
927 * Create the attribute...
928 */
929
930 if ((attr = ipp_add_attr(ipp, name, group, value_tag, 1)) == NULL)
931 return (NULL);
932
933 /*
934 * Initialize the attribute data...
935 */
936
937 if ((int)value_tag & IPP_TAG_CUPS_CONST)
938 {
939 attr->values[0].string.language = (char *)language;
940 attr->values[0].string.text = (char *)value;
941 }
942 else
943 {
944 if (language)
945 attr->values[0].string.language = _cupsStrAlloc(ipp_lang_code(language, code,
946 sizeof(code)));
947
948 if (value)
949 {
950 if (value_tag == IPP_TAG_CHARSET)
951 attr->values[0].string.text = _cupsStrAlloc(ipp_get_code(value, code,
952 sizeof(code)));
953 else if (value_tag == IPP_TAG_LANGUAGE)
954 attr->values[0].string.text = _cupsStrAlloc(ipp_lang_code(value, code,
955 sizeof(code)));
956 else
957 attr->values[0].string.text = _cupsStrAlloc(value);
958 }
959 }
960
961 return (attr);
962 }
963
964
965 /*
966 * 'ippAddStringf()' - Add a formatted string to an IPP message.
967 *
968 * The @code ipp@ parameter refers to an IPP message previously created using
969 * the @link ippNew@, @link ippNewRequest@, or @link ippNewResponse@ functions.
970 *
971 * The @code group@ parameter specifies the IPP attribute group tag: none
972 * (@code IPP_TAG_ZERO@, for member attributes), document
973 * (@code IPP_TAG_DOCUMENT@), event notification
974 * (@code IPP_TAG_EVENT_NOTIFICATION@), operation (@code IPP_TAG_OPERATION@),
975 * printer (@code IPP_TAG_PRINTER@), subscription (@code IPP_TAG_SUBSCRIPTION@),
976 * or unsupported (@code IPP_TAG_UNSUPPORTED_GROUP@).
977 *
978 * Supported string values include charset (@code IPP_TAG_CHARSET@), keyword
979 * (@code IPP_TAG_KEYWORD@), language (@code IPP_TAG_LANGUAGE@), mimeMediaType
980 * (@code IPP_TAG_MIMETYPE@), name (@code IPP_TAG_NAME@), nameWithLanguage
981 * (@code IPP_TAG_NAMELANG), text (@code IPP_TAG_TEXT@), textWithLanguage
982 * (@code IPP_TAG_TEXTLANG@), uri (@code IPP_TAG_URI@), and uriScheme
983 * (@code IPP_TAG_URISCHEME@).
984 *
985 * The @code language@ parameter must be non-@code NULL@ for nameWithLanguage
986 * and textWithLanguage string values and must be @code NULL@ for all other
987 * string values.
988 *
989 * The @code format@ parameter uses formatting characters compatible with the
990 * printf family of standard functions. Additional arguments follow it as
991 * needed. The formatted string is truncated as needed to the maximum length of
992 * the corresponding value type.
993 *
994 * @since CUPS 1.7/macOS 10.9@
995 */
996
997 ipp_attribute_t * /* O - New attribute */
998 ippAddStringf(ipp_t *ipp, /* I - IPP message */
999 ipp_tag_t group, /* I - IPP group */
1000 ipp_tag_t value_tag, /* I - Type of attribute */
1001 const char *name, /* I - Name of attribute */
1002 const char *language, /* I - Language code (@code NULL@ for default) */
1003 const char *format, /* I - Printf-style format string */
1004 ...) /* I - Additional arguments as needed */
1005 {
1006 ipp_attribute_t *attr; /* New attribute */
1007 va_list ap; /* Argument pointer */
1008
1009
1010 va_start(ap, format);
1011 attr = ippAddStringfv(ipp, group, value_tag, name, language, format, ap);
1012 va_end(ap);
1013
1014 return (attr);
1015 }
1016
1017
1018 /*
1019 * 'ippAddStringfv()' - Add a formatted string to an IPP message.
1020 *
1021 * The @code ipp@ parameter refers to an IPP message previously created using
1022 * the @link ippNew@, @link ippNewRequest@, or @link ippNewResponse@ functions.
1023 *
1024 * The @code group@ parameter specifies the IPP attribute group tag: none
1025 * (@code IPP_TAG_ZERO@, for member attributes), document
1026 * (@code IPP_TAG_DOCUMENT@), event notification
1027 * (@code IPP_TAG_EVENT_NOTIFICATION@), operation (@code IPP_TAG_OPERATION@),
1028 * printer (@code IPP_TAG_PRINTER@), subscription (@code IPP_TAG_SUBSCRIPTION@),
1029 * or unsupported (@code IPP_TAG_UNSUPPORTED_GROUP@).
1030 *
1031 * Supported string values include charset (@code IPP_TAG_CHARSET@), keyword
1032 * (@code IPP_TAG_KEYWORD@), language (@code IPP_TAG_LANGUAGE@), mimeMediaType
1033 * (@code IPP_TAG_MIMETYPE@), name (@code IPP_TAG_NAME@), nameWithLanguage
1034 * (@code IPP_TAG_NAMELANG), text (@code IPP_TAG_TEXT@), textWithLanguage
1035 * (@code IPP_TAG_TEXTLANG@), uri (@code IPP_TAG_URI@), and uriScheme
1036 * (@code IPP_TAG_URISCHEME@).
1037 *
1038 * The @code language@ parameter must be non-@code NULL@ for nameWithLanguage
1039 * and textWithLanguage string values and must be @code NULL@ for all other
1040 * string values.
1041 *
1042 * The @code format@ parameter uses formatting characters compatible with the
1043 * printf family of standard functions. Additional arguments are passed in the
1044 * stdarg pointer @code ap@. The formatted string is truncated as needed to the
1045 * maximum length of the corresponding value type.
1046 *
1047 * @since CUPS 1.7/macOS 10.9@
1048 */
1049
1050 ipp_attribute_t * /* O - New attribute */
1051 ippAddStringfv(ipp_t *ipp, /* I - IPP message */
1052 ipp_tag_t group, /* I - IPP group */
1053 ipp_tag_t value_tag, /* I - Type of attribute */
1054 const char *name, /* I - Name of attribute */
1055 const char *language, /* I - Language code (@code NULL@ for default) */
1056 const char *format, /* I - Printf-style format string */
1057 va_list ap) /* I - Additional arguments */
1058 {
1059 char buffer[IPP_MAX_TEXT + 4];
1060 /* Formatted text string */
1061 ssize_t bytes, /* Length of formatted value */
1062 max_bytes; /* Maximum number of bytes for value */
1063
1064
1065 /*
1066 * Range check input...
1067 */
1068
1069 if (!ipp || !name || group < IPP_TAG_ZERO ||
1070 group == IPP_TAG_END || group >= IPP_TAG_UNSUPPORTED_VALUE ||
1071 (value_tag < IPP_TAG_TEXT && value_tag != IPP_TAG_TEXTLANG &&
1072 value_tag != IPP_TAG_NAMELANG) || value_tag > IPP_TAG_MIMETYPE ||
1073 !format)
1074 return (NULL);
1075
1076 if ((value_tag == IPP_TAG_TEXTLANG || value_tag == IPP_TAG_NAMELANG)
1077 != (language != NULL))
1078 return (NULL);
1079
1080 /*
1081 * Format the string...
1082 */
1083
1084 if (!strcmp(format, "%s"))
1085 {
1086 /*
1087 * Optimize the simple case...
1088 */
1089
1090 const char *s = va_arg(ap, char *);
1091
1092 if (!s)
1093 s = "(null)";
1094
1095 bytes = (ssize_t)strlen(s);
1096 strlcpy(buffer, s, sizeof(buffer));
1097 }
1098 else
1099 {
1100 /*
1101 * Do a full formatting of the message...
1102 */
1103
1104 if ((bytes = vsnprintf(buffer, sizeof(buffer), format, ap)) < 0)
1105 return (NULL);
1106 }
1107
1108 /*
1109 * Limit the length of the string...
1110 */
1111
1112 switch (value_tag)
1113 {
1114 default :
1115 case IPP_TAG_TEXT :
1116 case IPP_TAG_TEXTLANG :
1117 max_bytes = IPP_MAX_TEXT;
1118 break;
1119
1120 case IPP_TAG_NAME :
1121 case IPP_TAG_NAMELANG :
1122 max_bytes = IPP_MAX_NAME;
1123 break;
1124
1125 case IPP_TAG_CHARSET :
1126 max_bytes = IPP_MAX_CHARSET;
1127 break;
1128
1129 case IPP_TAG_KEYWORD :
1130 max_bytes = IPP_MAX_KEYWORD;
1131 break;
1132
1133 case IPP_TAG_LANGUAGE :
1134 max_bytes = IPP_MAX_LANGUAGE;
1135 break;
1136
1137 case IPP_TAG_MIMETYPE :
1138 max_bytes = IPP_MAX_MIMETYPE;
1139 break;
1140
1141 case IPP_TAG_URI :
1142 max_bytes = IPP_MAX_URI;
1143 break;
1144
1145 case IPP_TAG_URISCHEME :
1146 max_bytes = IPP_MAX_URISCHEME;
1147 break;
1148 }
1149
1150 if (bytes >= max_bytes)
1151 {
1152 char *bufmax, /* Buffer at max_bytes */
1153 *bufptr; /* Pointer into buffer */
1154
1155 bufptr = buffer + strlen(buffer) - 1;
1156 bufmax = buffer + max_bytes - 1;
1157
1158 while (bufptr > bufmax)
1159 {
1160 if (*bufptr & 0x80)
1161 {
1162 while ((*bufptr & 0xc0) == 0x80 && bufptr > buffer)
1163 bufptr --;
1164 }
1165
1166 bufptr --;
1167 }
1168
1169 *bufptr = '\0';
1170 }
1171
1172 /*
1173 * Add the formatted string and return...
1174 */
1175
1176 return (ippAddString(ipp, group, value_tag, name, language, buffer));
1177 }
1178
1179
1180 /*
1181 * 'ippAddStrings()' - Add language-encoded strings to an IPP message.
1182 *
1183 * The @code ipp@ parameter refers to an IPP message previously created using
1184 * the @link ippNew@, @link ippNewRequest@, or @link ippNewResponse@ functions.
1185 *
1186 * The @code group@ parameter specifies the IPP attribute group tag: none
1187 * (@code IPP_TAG_ZERO@, for member attributes), document (@code IPP_TAG_DOCUMENT@),
1188 * event notification (@code IPP_TAG_EVENT_NOTIFICATION@), operation
1189 * (@code IPP_TAG_OPERATION@), printer (@code IPP_TAG_PRINTER@), subscription
1190 * (@code IPP_TAG_SUBSCRIPTION@), or unsupported (@code IPP_TAG_UNSUPPORTED_GROUP@).
1191 *
1192 * Supported string values include charset (@code IPP_TAG_CHARSET@), keyword
1193 * (@code IPP_TAG_KEYWORD@), language (@code IPP_TAG_LANGUAGE@), mimeMediaType
1194 * (@code IPP_TAG_MIMETYPE@), name (@code IPP_TAG_NAME@), nameWithLanguage
1195 * (@code IPP_TAG_NAMELANG), text (@code IPP_TAG_TEXT@), textWithLanguage
1196 * (@code IPP_TAG_TEXTLANG@), uri (@code IPP_TAG_URI@), and uriScheme
1197 * (@code IPP_TAG_URISCHEME@).
1198 *
1199 * The @code language@ parameter must be non-@code NULL@ for nameWithLanguage and
1200 * textWithLanguage string values and must be @code NULL@ for all other string values.
1201 */
1202
1203 ipp_attribute_t * /* O - New attribute */
1204 ippAddStrings(
1205 ipp_t *ipp, /* I - IPP message */
1206 ipp_tag_t group, /* I - IPP group */
1207 ipp_tag_t value_tag, /* I - Type of attribute */
1208 const char *name, /* I - Name of attribute */
1209 int num_values, /* I - Number of values */
1210 const char *language, /* I - Language code (@code NULL@ for default) */
1211 const char * const *values) /* I - Values */
1212 {
1213 int i; /* Looping var */
1214 ipp_tag_t temp_tag; /* Temporary value tag (masked) */
1215 ipp_attribute_t *attr; /* New attribute */
1216 _ipp_value_t *value; /* Current value */
1217 char code[32]; /* Language/charset value buffer */
1218
1219
1220 DEBUG_printf(("ippAddStrings(ipp=%p, group=%02x(%s), value_tag=%02x(%s), name=\"%s\", num_values=%d, language=\"%s\", values=%p)", (void *)ipp, group, ippTagString(group), value_tag, ippTagString(value_tag), name, num_values, language, (void *)values));
1221
1222 /*
1223 * Range check input...
1224 */
1225
1226 temp_tag = (ipp_tag_t)((int)value_tag & IPP_TAG_CUPS_MASK);
1227
1228 #if 0
1229 if (!ipp || !name || group < IPP_TAG_ZERO ||
1230 group == IPP_TAG_END || group >= IPP_TAG_UNSUPPORTED_VALUE ||
1231 (temp_tag < IPP_TAG_TEXT && temp_tag != IPP_TAG_TEXTLANG &&
1232 temp_tag != IPP_TAG_NAMELANG) || temp_tag > IPP_TAG_MIMETYPE ||
1233 num_values < 1)
1234 return (NULL);
1235
1236 if ((temp_tag == IPP_TAG_TEXTLANG || temp_tag == IPP_TAG_NAMELANG)
1237 != (language != NULL))
1238 return (NULL);
1239 #else
1240 if (!ipp || !name || group < IPP_TAG_ZERO ||
1241 group == IPP_TAG_END || group >= IPP_TAG_UNSUPPORTED_VALUE ||
1242 num_values < 1)
1243 return (NULL);
1244 #endif /* 0 */
1245
1246 /*
1247 * See if we need to map charset, language, or locale values...
1248 */
1249
1250 if (language && ((int)value_tag & IPP_TAG_CUPS_CONST) &&
1251 strcmp(language, ipp_lang_code(language, code, sizeof(code))))
1252 value_tag = temp_tag; /* Don't do a fast copy */
1253 else if (values && value_tag == (ipp_tag_t)(IPP_TAG_CHARSET | IPP_TAG_CUPS_CONST))
1254 {
1255 for (i = 0; i < num_values; i ++)
1256 if (strcmp(values[i], ipp_get_code(values[i], code, sizeof(code))))
1257 {
1258 value_tag = temp_tag; /* Don't do a fast copy */
1259 break;
1260 }
1261 }
1262 else if (values && value_tag == (ipp_tag_t)(IPP_TAG_LANGUAGE | IPP_TAG_CUPS_CONST))
1263 {
1264 for (i = 0; i < num_values; i ++)
1265 if (strcmp(values[i], ipp_lang_code(values[i], code, sizeof(code))))
1266 {
1267 value_tag = temp_tag; /* Don't do a fast copy */
1268 break;
1269 }
1270 }
1271
1272 /*
1273 * Create the attribute...
1274 */
1275
1276 if ((attr = ipp_add_attr(ipp, name, group, value_tag, num_values)) == NULL)
1277 return (NULL);
1278
1279 /*
1280 * Initialize the attribute data...
1281 */
1282
1283 for (i = num_values, value = attr->values;
1284 i > 0;
1285 i --, value ++)
1286 {
1287 if (language)
1288 {
1289 if (value == attr->values)
1290 {
1291 if ((int)value_tag & IPP_TAG_CUPS_CONST)
1292 value->string.language = (char *)language;
1293 else
1294 value->string.language = _cupsStrAlloc(ipp_lang_code(language, code,
1295 sizeof(code)));
1296 }
1297 else
1298 value->string.language = attr->values[0].string.language;
1299 }
1300
1301 if (values)
1302 {
1303 if ((int)value_tag & IPP_TAG_CUPS_CONST)
1304 value->string.text = (char *)*values++;
1305 else if (value_tag == IPP_TAG_CHARSET)
1306 value->string.text = _cupsStrAlloc(ipp_get_code(*values++, code, sizeof(code)));
1307 else if (value_tag == IPP_TAG_LANGUAGE)
1308 value->string.text = _cupsStrAlloc(ipp_lang_code(*values++, code, sizeof(code)));
1309 else
1310 value->string.text = _cupsStrAlloc(*values++);
1311 }
1312 }
1313
1314 return (attr);
1315 }
1316
1317
1318 /*
1319 * 'ippContainsInteger()' - Determine whether an attribute contains the
1320 * specified value or is within the list of ranges.
1321 *
1322 * Returns non-zero when the attribute contains either a matching integer or
1323 * enum value, or the value falls within one of the rangeOfInteger values for
1324 * the attribute.
1325 *
1326 * @since CUPS 1.7/macOS 10.9@
1327 */
1328
1329 int /* O - 1 on a match, 0 on no match */
1330 ippContainsInteger(
1331 ipp_attribute_t *attr, /* I - Attribute */
1332 int value) /* I - Integer/enum value */
1333 {
1334 int i; /* Looping var */
1335 _ipp_value_t *avalue; /* Current attribute value */
1336
1337
1338 /*
1339 * Range check input...
1340 */
1341
1342 if (!attr)
1343 return (0);
1344
1345 if (attr->value_tag != IPP_TAG_INTEGER && attr->value_tag != IPP_TAG_ENUM &&
1346 attr->value_tag != IPP_TAG_RANGE)
1347 return (0);
1348
1349 /*
1350 * Compare...
1351 */
1352
1353 if (attr->value_tag == IPP_TAG_RANGE)
1354 {
1355 for (i = attr->num_values, avalue = attr->values; i > 0; i --, avalue ++)
1356 if (value >= avalue->range.lower && value <= avalue->range.upper)
1357 return (1);
1358 }
1359 else
1360 {
1361 for (i = attr->num_values, avalue = attr->values; i > 0; i --, avalue ++)
1362 if (value == avalue->integer)
1363 return (1);
1364 }
1365
1366 return (0);
1367 }
1368
1369
1370 /*
1371 * 'ippContainsString()' - Determine whether an attribute contains the
1372 * specified string value.
1373 *
1374 * Returns non-zero when the attribute contains a matching charset, keyword,
1375 * naturalLanguage, mimeMediaType, name, text, uri, or uriScheme value.
1376 *
1377 * @since CUPS 1.7/macOS 10.9@
1378 */
1379
1380 int /* O - 1 on a match, 0 on no match */
1381 ippContainsString(
1382 ipp_attribute_t *attr, /* I - Attribute */
1383 const char *value) /* I - String value */
1384 {
1385 int i; /* Looping var */
1386 _ipp_value_t *avalue; /* Current attribute value */
1387
1388
1389 DEBUG_printf(("ippContainsString(attr=%p, value=\"%s\")", (void *)attr, value));
1390
1391 /*
1392 * Range check input...
1393 */
1394
1395 if (!attr || !value)
1396 {
1397 DEBUG_puts("1ippContainsString: Returning 0 (bad input)");
1398 return (0);
1399 }
1400
1401 /*
1402 * Compare...
1403 */
1404
1405 DEBUG_printf(("1ippContainsString: attr %s, %s with %d values.",
1406 attr->name, ippTagString(attr->value_tag),
1407 attr->num_values));
1408
1409 switch (attr->value_tag & IPP_TAG_CUPS_MASK)
1410 {
1411 case IPP_TAG_CHARSET :
1412 case IPP_TAG_KEYWORD :
1413 case IPP_TAG_LANGUAGE :
1414 case IPP_TAG_URI :
1415 case IPP_TAG_URISCHEME :
1416 for (i = attr->num_values, avalue = attr->values;
1417 i > 0;
1418 i --, avalue ++)
1419 {
1420 DEBUG_printf(("1ippContainsString: value[%d]=\"%s\"",
1421 attr->num_values - i, avalue->string.text));
1422
1423 if (!strcmp(value, avalue->string.text))
1424 {
1425 DEBUG_puts("1ippContainsString: Returning 1 (match)");
1426 return (1);
1427 }
1428 }
1429
1430 case IPP_TAG_MIMETYPE :
1431 case IPP_TAG_NAME :
1432 case IPP_TAG_NAMELANG :
1433 case IPP_TAG_TEXT :
1434 case IPP_TAG_TEXTLANG :
1435 for (i = attr->num_values, avalue = attr->values;
1436 i > 0;
1437 i --, avalue ++)
1438 {
1439 DEBUG_printf(("1ippContainsString: value[%d]=\"%s\"",
1440 attr->num_values - i, avalue->string.text));
1441
1442 if (!_cups_strcasecmp(value, avalue->string.text))
1443 {
1444 DEBUG_puts("1ippContainsString: Returning 1 (match)");
1445 return (1);
1446 }
1447 }
1448
1449 default :
1450 break;
1451 }
1452
1453 DEBUG_puts("1ippContainsString: Returning 0 (no match)");
1454
1455 return (0);
1456 }
1457
1458
1459 /*
1460 * 'ippCopyAttribute()' - Copy an attribute.
1461 *
1462 * The specified attribute, @code attr@, is copied to the destination IPP message.
1463 * When @code quickcopy@ is non-zero, a "shallow" reference copy of the attribute is
1464 * created - this should only be done as long as the original source IPP message will
1465 * not be freed for the life of the destination.
1466 *
1467 * @since CUPS 1.6/macOS 10.8@
1468 */
1469
1470
1471 ipp_attribute_t * /* O - New attribute */
1472 ippCopyAttribute(
1473 ipp_t *dst, /* I - Destination IPP message */
1474 ipp_attribute_t *srcattr, /* I - Attribute to copy */
1475 int quickcopy) /* I - 1 for a referenced copy, 0 for normal */
1476 {
1477 int i; /* Looping var */
1478 ipp_tag_t srctag; /* Source value tag */
1479 ipp_attribute_t *dstattr; /* Destination attribute */
1480 _ipp_value_t *srcval, /* Source value */
1481 *dstval; /* Destination value */
1482
1483
1484 DEBUG_printf(("ippCopyAttribute(dst=%p, srcattr=%p, quickcopy=%d)", (void *)dst, (void *)srcattr, quickcopy));
1485
1486 /*
1487 * Range check input...
1488 */
1489
1490 if (!dst || !srcattr)
1491 return (NULL);
1492
1493 /*
1494 * Copy it...
1495 */
1496
1497 quickcopy = (quickcopy && (srcattr->value_tag & IPP_TAG_CUPS_CONST)) ? IPP_TAG_CUPS_CONST : 0;
1498 srctag = srcattr->value_tag & IPP_TAG_CUPS_MASK;
1499
1500 switch (srctag)
1501 {
1502 case IPP_TAG_ZERO :
1503 dstattr = ippAddSeparator(dst);
1504 break;
1505
1506 case IPP_TAG_UNSUPPORTED_VALUE :
1507 case IPP_TAG_DEFAULT :
1508 case IPP_TAG_UNKNOWN :
1509 case IPP_TAG_NOVALUE :
1510 case IPP_TAG_NOTSETTABLE :
1511 case IPP_TAG_DELETEATTR :
1512 case IPP_TAG_ADMINDEFINE :
1513 dstattr = ippAddOutOfBand(dst, srcattr->group_tag, srctag, srcattr->name);
1514 break;
1515
1516 case IPP_TAG_INTEGER :
1517 case IPP_TAG_ENUM :
1518 case IPP_TAG_BOOLEAN :
1519 case IPP_TAG_DATE :
1520 case IPP_TAG_RESOLUTION :
1521 case IPP_TAG_RANGE :
1522 if ((dstattr = ipp_add_attr(dst, srcattr->name, srcattr->group_tag, srctag, srcattr->num_values)) != NULL)
1523 memcpy(dstattr->values, srcattr->values, (size_t)srcattr->num_values * sizeof(_ipp_value_t));
1524 break;
1525
1526 case IPP_TAG_TEXT :
1527 case IPP_TAG_NAME :
1528 case IPP_TAG_RESERVED_STRING :
1529 case IPP_TAG_KEYWORD :
1530 case IPP_TAG_URI :
1531 case IPP_TAG_URISCHEME :
1532 case IPP_TAG_CHARSET :
1533 case IPP_TAG_LANGUAGE :
1534 case IPP_TAG_MIMETYPE :
1535 if ((dstattr = ippAddStrings(dst, srcattr->group_tag, (ipp_tag_t)(srctag | quickcopy), srcattr->name, srcattr->num_values, NULL, NULL)) == NULL)
1536 break;
1537
1538 if (quickcopy)
1539 {
1540 /*
1541 * Can safely quick-copy these string values...
1542 */
1543
1544 memcpy(dstattr->values, srcattr->values, (size_t)srcattr->num_values * sizeof(_ipp_value_t));
1545 }
1546 else
1547 {
1548 /*
1549 * Otherwise do a normal reference counted copy...
1550 */
1551
1552 for (i = srcattr->num_values, srcval = srcattr->values, dstval = dstattr->values; i > 0; i --, srcval ++, dstval ++)
1553 dstval->string.text = _cupsStrAlloc(srcval->string.text);
1554 }
1555 break;
1556
1557 case IPP_TAG_TEXTLANG :
1558 case IPP_TAG_NAMELANG :
1559 if ((dstattr = ippAddStrings(dst, srcattr->group_tag, (ipp_tag_t)(srctag | quickcopy), srcattr->name, srcattr->num_values, NULL, NULL)) == NULL)
1560 break;
1561
1562 if (quickcopy)
1563 {
1564 /*
1565 * Can safely quick-copy these string values...
1566 */
1567
1568 memcpy(dstattr->values, srcattr->values, (size_t)srcattr->num_values * sizeof(_ipp_value_t));
1569 }
1570 else if (srcattr->value_tag & IPP_TAG_CUPS_CONST)
1571 {
1572 /*
1573 * Otherwise do a normal reference counted copy...
1574 */
1575
1576 for (i = srcattr->num_values, srcval = srcattr->values, dstval = dstattr->values; i > 0; i --, srcval ++, dstval ++)
1577 {
1578 if (srcval == srcattr->values)
1579 dstval->string.language = _cupsStrAlloc(srcval->string.language);
1580 else
1581 dstval->string.language = dstattr->values[0].string.language;
1582
1583 dstval->string.text = _cupsStrAlloc(srcval->string.text);
1584 }
1585 }
1586 break;
1587
1588 case IPP_TAG_BEGIN_COLLECTION :
1589 if ((dstattr = ippAddCollections(dst, srcattr->group_tag, srcattr->name, srcattr->num_values, NULL)) == NULL)
1590 break;
1591
1592 for (i = srcattr->num_values, srcval = srcattr->values, dstval = dstattr->values; i > 0; i --, srcval ++, dstval ++)
1593 {
1594 dstval->collection = srcval->collection;
1595 srcval->collection->use ++;
1596 }
1597 break;
1598
1599 case IPP_TAG_STRING :
1600 default :
1601 if ((dstattr = ipp_add_attr(dst, srcattr->name, srcattr->group_tag, srctag, srcattr->num_values)) == NULL)
1602 break;
1603
1604 for (i = srcattr->num_values, srcval = srcattr->values, dstval = dstattr->values; i > 0; i --, srcval ++, dstval ++)
1605 {
1606 dstval->unknown.length = srcval->unknown.length;
1607
1608 if (dstval->unknown.length > 0)
1609 {
1610 if ((dstval->unknown.data = malloc((size_t)dstval->unknown.length)) == NULL)
1611 dstval->unknown.length = 0;
1612 else
1613 memcpy(dstval->unknown.data, srcval->unknown.data, (size_t)dstval->unknown.length);
1614 }
1615 }
1616 break; /* anti-compiler-warning-code */
1617 }
1618
1619 return (dstattr);
1620 }
1621
1622
1623 /*
1624 * 'ippCopyAttributes()' - Copy attributes from one IPP message to another.
1625 *
1626 * Zero or more attributes are copied from the source IPP message, @code src@, to the
1627 * destination IPP message, @code dst@. When @code quickcopy@ is non-zero, a "shallow"
1628 * reference copy of the attribute is created - this should only be done as long as the
1629 * original source IPP message will not be freed for the life of the destination.
1630 *
1631 * The @code cb@ and @code context@ parameters provide a generic way to "filter" the
1632 * attributes that are copied - the function must return 1 to copy the attribute or
1633 * 0 to skip it. The function may also choose to do a partial copy of the source attribute
1634 * itself.
1635 *
1636 * @since CUPS 1.6/macOS 10.8@
1637 */
1638
1639 int /* O - 1 on success, 0 on error */
1640 ippCopyAttributes(
1641 ipp_t *dst, /* I - Destination IPP message */
1642 ipp_t *src, /* I - Source IPP message */
1643 int quickcopy, /* I - 1 for a referenced copy, 0 for normal */
1644 ipp_copycb_t cb, /* I - Copy callback or @code NULL@ for none */
1645 void *context) /* I - Context pointer */
1646 {
1647 ipp_attribute_t *srcattr; /* Source attribute */
1648
1649
1650 DEBUG_printf(("ippCopyAttributes(dst=%p, src=%p, quickcopy=%d, cb=%p, context=%p)", (void *)dst, (void *)src, quickcopy, (void *)cb, context));
1651
1652 /*
1653 * Range check input...
1654 */
1655
1656 if (!dst || !src)
1657 return (0);
1658
1659 /*
1660 * Loop through source attributes and copy as needed...
1661 */
1662
1663 for (srcattr = src->attrs; srcattr; srcattr = srcattr->next)
1664 if (!cb || (*cb)(context, dst, srcattr))
1665 if (!ippCopyAttribute(dst, srcattr, quickcopy))
1666 return (0);
1667
1668 return (1);
1669 }
1670
1671
1672 /*
1673 * 'ippDateToTime()' - Convert from RFC 2579 Date/Time format to time in
1674 * seconds.
1675 */
1676
1677 time_t /* O - UNIX time value */
1678 ippDateToTime(const ipp_uchar_t *date) /* I - RFC 2579 date info */
1679 {
1680 struct tm unixdate; /* UNIX date/time info */
1681 time_t t; /* Computed time */
1682
1683
1684 if (!date)
1685 return (0);
1686
1687 memset(&unixdate, 0, sizeof(unixdate));
1688
1689 /*
1690 * RFC-2579 date/time format is:
1691 *
1692 * Byte(s) Description
1693 * ------- -----------
1694 * 0-1 Year (0 to 65535)
1695 * 2 Month (1 to 12)
1696 * 3 Day (1 to 31)
1697 * 4 Hours (0 to 23)
1698 * 5 Minutes (0 to 59)
1699 * 6 Seconds (0 to 60, 60 = "leap second")
1700 * 7 Deciseconds (0 to 9)
1701 * 8 +/- UTC
1702 * 9 UTC hours (0 to 11)
1703 * 10 UTC minutes (0 to 59)
1704 */
1705
1706 unixdate.tm_year = ((date[0] << 8) | date[1]) - 1900;
1707 unixdate.tm_mon = date[2] - 1;
1708 unixdate.tm_mday = date[3];
1709 unixdate.tm_hour = date[4];
1710 unixdate.tm_min = date[5];
1711 unixdate.tm_sec = date[6];
1712
1713 t = mktime(&unixdate);
1714
1715 if (date[8] == '-')
1716 t += date[9] * 3600 + date[10] * 60;
1717 else
1718 t -= date[9] * 3600 + date[10] * 60;
1719
1720 return (t);
1721 }
1722
1723
1724 /*
1725 * 'ippDelete()' - Delete an IPP message.
1726 */
1727
1728 void
1729 ippDelete(ipp_t *ipp) /* I - IPP message */
1730 {
1731 ipp_attribute_t *attr, /* Current attribute */
1732 *next; /* Next attribute */
1733
1734
1735 DEBUG_printf(("ippDelete(ipp=%p)", (void *)ipp));
1736
1737 if (!ipp)
1738 return;
1739
1740 ipp->use --;
1741 if (ipp->use > 0)
1742 {
1743 DEBUG_printf(("4debug_retain: %p IPP message (use=%d)", (void *)ipp, ipp->use));
1744 return;
1745 }
1746
1747 DEBUG_printf(("4debug_free: %p IPP message", (void *)ipp));
1748
1749 for (attr = ipp->attrs; attr != NULL; attr = next)
1750 {
1751 next = attr->next;
1752
1753 DEBUG_printf(("4debug_free: %p %s %s%s (%d values)", (void *)attr, attr->name, attr->num_values > 1 ? "1setOf " : "", ippTagString(attr->value_tag), attr->num_values));
1754
1755 ipp_free_values(attr, 0, attr->num_values);
1756
1757 if (attr->name)
1758 _cupsStrFree(attr->name);
1759
1760 free(attr);
1761 }
1762
1763 free(ipp);
1764 }
1765
1766
1767 /*
1768 * 'ippDeleteAttribute()' - Delete a single attribute in an IPP message.
1769 *
1770 * @since CUPS 1.1.19/macOS 10.3@
1771 */
1772
1773 void
1774 ippDeleteAttribute(
1775 ipp_t *ipp, /* I - IPP message */
1776 ipp_attribute_t *attr) /* I - Attribute to delete */
1777 {
1778 ipp_attribute_t *current, /* Current attribute */
1779 *prev; /* Previous attribute */
1780
1781
1782 DEBUG_printf(("ippDeleteAttribute(ipp=%p, attr=%p(%s))", (void *)ipp, (void *)attr, attr ? attr->name : "(null)"));
1783
1784 /*
1785 * Range check input...
1786 */
1787
1788 if (!attr)
1789 return;
1790
1791 DEBUG_printf(("4debug_free: %p %s %s%s (%d values)", (void *)attr, attr->name, attr->num_values > 1 ? "1setOf " : "", ippTagString(attr->value_tag), attr->num_values));
1792
1793 /*
1794 * Find the attribute in the list...
1795 */
1796
1797 if (ipp)
1798 {
1799 for (current = ipp->attrs, prev = NULL;
1800 current;
1801 prev = current, current = current->next)
1802 if (current == attr)
1803 {
1804 /*
1805 * Found it, remove the attribute from the list...
1806 */
1807
1808 if (prev)
1809 prev->next = current->next;
1810 else
1811 ipp->attrs = current->next;
1812
1813 if (current == ipp->last)
1814 ipp->last = prev;
1815
1816 break;
1817 }
1818
1819 if (!current)
1820 return;
1821 }
1822
1823 /*
1824 * Free memory used by the attribute...
1825 */
1826
1827 ipp_free_values(attr, 0, attr->num_values);
1828
1829 if (attr->name)
1830 _cupsStrFree(attr->name);
1831
1832 free(attr);
1833 }
1834
1835
1836 /*
1837 * 'ippDeleteValues()' - Delete values in an attribute.
1838 *
1839 * The @code element@ parameter specifies the first value to delete, starting at
1840 * 0. It must be less than the number of values returned by @link ippGetCount@.
1841 *
1842 * The @code attr@ parameter may be modified as a result of setting the value.
1843 *
1844 * Deleting all values in an attribute deletes the attribute.
1845 *
1846 * @since CUPS 1.6/macOS 10.8@
1847 */
1848
1849 int /* O - 1 on success, 0 on failure */
1850 ippDeleteValues(
1851 ipp_t *ipp, /* I - IPP message */
1852 ipp_attribute_t **attr, /* IO - Attribute */
1853 int element, /* I - Index of first value to delete (0-based) */
1854 int count) /* I - Number of values to delete */
1855 {
1856 /*
1857 * Range check input...
1858 */
1859
1860 if (!ipp || !attr || !*attr ||
1861 element < 0 || element >= (*attr)->num_values || count <= 0 ||
1862 (element + count) >= (*attr)->num_values)
1863 return (0);
1864
1865 /*
1866 * If we are deleting all values, just delete the attribute entirely.
1867 */
1868
1869 if (count == (*attr)->num_values)
1870 {
1871 ippDeleteAttribute(ipp, *attr);
1872 *attr = NULL;
1873 return (1);
1874 }
1875
1876 /*
1877 * Otherwise free the values in question and return.
1878 */
1879
1880 ipp_free_values(*attr, element, count);
1881
1882 return (1);
1883 }
1884
1885
1886 /*
1887 * 'ippFindAttribute()' - Find a named attribute in a request.
1888 *
1889 * Starting with CUPS 2.0, the attribute name can contain a hierarchical list
1890 * of attribute and member names separated by slashes, for example
1891 * "media-col/media-size".
1892 */
1893
1894 ipp_attribute_t * /* O - Matching attribute */
1895 ippFindAttribute(ipp_t *ipp, /* I - IPP message */
1896 const char *name, /* I - Name of attribute */
1897 ipp_tag_t type) /* I - Type of attribute */
1898 {
1899 DEBUG_printf(("2ippFindAttribute(ipp=%p, name=\"%s\", type=%02x(%s))", (void *)ipp, name, type, ippTagString(type)));
1900
1901 if (!ipp || !name)
1902 return (NULL);
1903
1904 /*
1905 * Reset the current pointer...
1906 */
1907
1908 ipp->current = NULL;
1909 ipp->atend = 0;
1910
1911 /*
1912 * Search for the attribute...
1913 */
1914
1915 return (ippFindNextAttribute(ipp, name, type));
1916 }
1917
1918
1919 /*
1920 * 'ippFindNextAttribute()' - Find the next named attribute in a request.
1921 *
1922 * Starting with CUPS 2.0, the attribute name can contain a hierarchical list
1923 * of attribute and member names separated by slashes, for example
1924 * "media-col/media-size".
1925 */
1926
1927 ipp_attribute_t * /* O - Matching attribute */
1928 ippFindNextAttribute(ipp_t *ipp, /* I - IPP message */
1929 const char *name, /* I - Name of attribute */
1930 ipp_tag_t type) /* I - Type of attribute */
1931 {
1932 ipp_attribute_t *attr, /* Current atttribute */
1933 *childattr; /* Child attribute */
1934 ipp_tag_t value_tag; /* Value tag */
1935 char parent[1024], /* Parent attribute name */
1936 *child = NULL; /* Child attribute name */
1937
1938
1939 DEBUG_printf(("2ippFindNextAttribute(ipp=%p, name=\"%s\", type=%02x(%s))", (void *)ipp, name, type, ippTagString(type)));
1940
1941 if (!ipp || !name)
1942 return (NULL);
1943
1944 DEBUG_printf(("3ippFindNextAttribute: atend=%d", ipp->atend));
1945
1946 if (ipp->atend)
1947 return (NULL);
1948
1949 if (strchr(name, '/'))
1950 {
1951 /*
1952 * Search for child attribute...
1953 */
1954
1955 strlcpy(parent, name, sizeof(parent));
1956 if ((child = strchr(parent, '/')) == NULL)
1957 {
1958 DEBUG_puts("3ippFindNextAttribute: Attribute name too long.");
1959 return (NULL);
1960 }
1961
1962 *child++ = '\0';
1963
1964 if (ipp->current && ipp->current->name && ipp->current->value_tag == IPP_TAG_BEGIN_COLLECTION && !strcmp(parent, ipp->current->name))
1965 {
1966 while (ipp->curindex < ipp->current->num_values)
1967 {
1968 if ((childattr = ippFindNextAttribute(ipp->current->values[ipp->curindex].collection, child, type)) != NULL)
1969 return (childattr);
1970
1971 ipp->curindex ++;
1972 if (ipp->curindex < ipp->current->num_values && ipp->current->values[ipp->curindex].collection)
1973 ipp->current->values[ipp->curindex].collection->current = NULL;
1974 }
1975
1976 ipp->prev = ipp->current;
1977 ipp->current = ipp->current->next;
1978 ipp->curindex = 0;
1979
1980 if (!ipp->current)
1981 {
1982 ipp->atend = 1;
1983 return (NULL);
1984 }
1985 }
1986
1987 if (!ipp->current)
1988 {
1989 ipp->prev = NULL;
1990 ipp->current = ipp->attrs;
1991 ipp->curindex = 0;
1992 }
1993
1994 name = parent;
1995 attr = ipp->current;
1996 }
1997 else if (ipp->current)
1998 {
1999 ipp->prev = ipp->current;
2000 attr = ipp->current->next;
2001 }
2002 else
2003 {
2004 ipp->prev = NULL;
2005 attr = ipp->attrs;
2006 }
2007
2008 for (; attr != NULL; ipp->prev = attr, attr = attr->next)
2009 {
2010 DEBUG_printf(("4ippFindAttribute: attr=%p, name=\"%s\"", (void *)attr, attr->name));
2011
2012 value_tag = (ipp_tag_t)(attr->value_tag & IPP_TAG_CUPS_MASK);
2013
2014 if (attr->name != NULL && _cups_strcasecmp(attr->name, name) == 0 &&
2015 (value_tag == type || type == IPP_TAG_ZERO || name == parent ||
2016 (value_tag == IPP_TAG_TEXTLANG && type == IPP_TAG_TEXT) ||
2017 (value_tag == IPP_TAG_NAMELANG && type == IPP_TAG_NAME)))
2018 {
2019 ipp->current = attr;
2020
2021 if (name == parent && attr->value_tag == IPP_TAG_BEGIN_COLLECTION)
2022 {
2023 int i; /* Looping var */
2024
2025 for (i = 0; i < attr->num_values; i ++)
2026 {
2027 if ((childattr = ippFindAttribute(attr->values[i].collection, child, type)) != NULL)
2028 {
2029 attr->values[0].collection->curindex = i;
2030 return (childattr);
2031 }
2032 }
2033 }
2034 else
2035 return (attr);
2036 }
2037 }
2038
2039 ipp->current = NULL;
2040 ipp->prev = NULL;
2041 ipp->atend = 1;
2042
2043 return (NULL);
2044 }
2045
2046
2047 /*
2048 * 'ippFirstAttribute()' - Return the first attribute in the message.
2049 *
2050 * @since CUPS 1.6/macOS 10.8@
2051 */
2052
2053 ipp_attribute_t * /* O - First attribute or @code NULL@ if none */
2054 ippFirstAttribute(ipp_t *ipp) /* I - IPP message */
2055 {
2056 /*
2057 * Range check input...
2058 */
2059
2060 if (!ipp)
2061 return (NULL);
2062
2063 /*
2064 * Return the first attribute...
2065 */
2066
2067 return (ipp->current = ipp->attrs);
2068 }
2069
2070
2071 /*
2072 * 'ippGetBoolean()' - Get a boolean value for an attribute.
2073 *
2074 * The @code element@ parameter specifies which value to get from 0 to
2075 * @code ippGetCount(attr)@ - 1.
2076 *
2077 * @since CUPS 1.6/macOS 10.8@
2078 */
2079
2080 int /* O - Boolean value or 0 on error */
2081 ippGetBoolean(ipp_attribute_t *attr, /* I - IPP attribute */
2082 int element) /* I - Value number (0-based) */
2083 {
2084 /*
2085 * Range check input...
2086 */
2087
2088 if (!attr || attr->value_tag != IPP_TAG_BOOLEAN ||
2089 element < 0 || element >= attr->num_values)
2090 return (0);
2091
2092 /*
2093 * Return the value...
2094 */
2095
2096 return (attr->values[element].boolean);
2097 }
2098
2099
2100 /*
2101 * 'ippGetCollection()' - Get a collection value for an attribute.
2102 *
2103 * The @code element@ parameter specifies which value to get from 0 to
2104 * @code ippGetCount(attr)@ - 1.
2105 *
2106 * @since CUPS 1.6/macOS 10.8@
2107 */
2108
2109 ipp_t * /* O - Collection value or @code NULL@ on error */
2110 ippGetCollection(
2111 ipp_attribute_t *attr, /* I - IPP attribute */
2112 int element) /* I - Value number (0-based) */
2113 {
2114 /*
2115 * Range check input...
2116 */
2117
2118 if (!attr || attr->value_tag != IPP_TAG_BEGIN_COLLECTION ||
2119 element < 0 || element >= attr->num_values)
2120 return (NULL);
2121
2122 /*
2123 * Return the value...
2124 */
2125
2126 return (attr->values[element].collection);
2127 }
2128
2129
2130 /*
2131 * 'ippGetCount()' - Get the number of values in an attribute.
2132 *
2133 * @since CUPS 1.6/macOS 10.8@
2134 */
2135
2136 int /* O - Number of values or 0 on error */
2137 ippGetCount(ipp_attribute_t *attr) /* I - IPP attribute */
2138 {
2139 /*
2140 * Range check input...
2141 */
2142
2143 if (!attr)
2144 return (0);
2145
2146 /*
2147 * Return the number of values...
2148 */
2149
2150 return (attr->num_values);
2151 }
2152
2153
2154 /*
2155 * 'ippGetDate()' - Get a dateTime value for an attribute.
2156 *
2157 * The @code element@ parameter specifies which value to get from 0 to
2158 * @code ippGetCount(attr)@ - 1.
2159 *
2160 * @since CUPS 1.6/macOS 10.8@
2161 */
2162
2163 const ipp_uchar_t * /* O - dateTime value or @code NULL@ */
2164 ippGetDate(ipp_attribute_t *attr, /* I - IPP attribute */
2165 int element) /* I - Value number (0-based) */
2166 {
2167 /*
2168 * Range check input...
2169 */
2170
2171 if (!attr || attr->value_tag != IPP_TAG_DATE ||
2172 element < 0 || element >= attr->num_values)
2173 return (NULL);
2174
2175 /*
2176 * Return the value...
2177 */
2178
2179 return (attr->values[element].date);
2180 }
2181
2182
2183 /*
2184 * 'ippGetGroupTag()' - Get the group associated with an attribute.
2185 *
2186 * @since CUPS 1.6/macOS 10.8@
2187 */
2188
2189 ipp_tag_t /* O - Group tag or @code IPP_TAG_ZERO@ on error */
2190 ippGetGroupTag(ipp_attribute_t *attr) /* I - IPP attribute */
2191 {
2192 /*
2193 * Range check input...
2194 */
2195
2196 if (!attr)
2197 return (IPP_TAG_ZERO);
2198
2199 /*
2200 * Return the group...
2201 */
2202
2203 return (attr->group_tag);
2204 }
2205
2206
2207 /*
2208 * 'ippGetInteger()' - Get the integer/enum value for an attribute.
2209 *
2210 * The @code element@ parameter specifies which value to get from 0 to
2211 * @code ippGetCount(attr)@ - 1.
2212 *
2213 * @since CUPS 1.6/macOS 10.8@
2214 */
2215
2216 int /* O - Value or 0 on error */
2217 ippGetInteger(ipp_attribute_t *attr, /* I - IPP attribute */
2218 int element) /* I - Value number (0-based) */
2219 {
2220 /*
2221 * Range check input...
2222 */
2223
2224 if (!attr || (attr->value_tag != IPP_TAG_INTEGER && attr->value_tag != IPP_TAG_ENUM) ||
2225 element < 0 || element >= attr->num_values)
2226 return (0);
2227
2228 /*
2229 * Return the value...
2230 */
2231
2232 return (attr->values[element].integer);
2233 }
2234
2235
2236 /*
2237 * 'ippGetName()' - Get the attribute name.
2238 *
2239 * @since CUPS 1.6/macOS 10.8@
2240 */
2241
2242 const char * /* O - Attribute name or @code NULL@ for separators */
2243 ippGetName(ipp_attribute_t *attr) /* I - IPP attribute */
2244 {
2245 /*
2246 * Range check input...
2247 */
2248
2249 if (!attr)
2250 return (NULL);
2251
2252 /*
2253 * Return the name...
2254 */
2255
2256 return (attr->name);
2257 }
2258
2259
2260 /*
2261 * 'ippGetOctetString()' - Get an octetString value from an IPP attribute.
2262 *
2263 * The @code element@ parameter specifies which value to get from 0 to
2264 * @code ippGetCount(attr)@ - 1.
2265 *
2266 * @since CUPS 1.7/macOS 10.9@
2267 */
2268
2269 void * /* O - Pointer to octetString data */
2270 ippGetOctetString(
2271 ipp_attribute_t *attr, /* I - IPP attribute */
2272 int element, /* I - Value number (0-based) */
2273 int *datalen) /* O - Length of octetString data */
2274 {
2275 /*
2276 * Range check input...
2277 */
2278
2279 if (!attr || attr->value_tag != IPP_TAG_STRING ||
2280 element < 0 || element >= attr->num_values)
2281 {
2282 if (datalen)
2283 *datalen = 0;
2284
2285 return (NULL);
2286 }
2287
2288 /*
2289 * Return the values...
2290 */
2291
2292 if (datalen)
2293 *datalen = attr->values[element].unknown.length;
2294
2295 return (attr->values[element].unknown.data);
2296 }
2297
2298
2299 /*
2300 * 'ippGetOperation()' - Get the operation ID in an IPP message.
2301 *
2302 * @since CUPS 1.6/macOS 10.8@
2303 */
2304
2305 ipp_op_t /* O - Operation ID or 0 on error */
2306 ippGetOperation(ipp_t *ipp) /* I - IPP request message */
2307 {
2308 /*
2309 * Range check input...
2310 */
2311
2312 if (!ipp)
2313 return ((ipp_op_t)0);
2314
2315 /*
2316 * Return the value...
2317 */
2318
2319 return (ipp->request.op.operation_id);
2320 }
2321
2322
2323 /*
2324 * 'ippGetRange()' - Get a rangeOfInteger value from an attribute.
2325 *
2326 * The @code element@ parameter specifies which value to get from 0 to
2327 * @code ippGetCount(attr)@ - 1.
2328 *
2329 * @since CUPS 1.6/macOS 10.8@
2330 */
2331
2332 int /* O - Lower value of range or 0 */
2333 ippGetRange(ipp_attribute_t *attr, /* I - IPP attribute */
2334 int element, /* I - Value number (0-based) */
2335 int *uppervalue)/* O - Upper value of range */
2336 {
2337 /*
2338 * Range check input...
2339 */
2340
2341 if (!attr || attr->value_tag != IPP_TAG_RANGE ||
2342 element < 0 || element >= attr->num_values)
2343 {
2344 if (uppervalue)
2345 *uppervalue = 0;
2346
2347 return (0);
2348 }
2349
2350 /*
2351 * Return the values...
2352 */
2353
2354 if (uppervalue)
2355 *uppervalue = attr->values[element].range.upper;
2356
2357 return (attr->values[element].range.lower);
2358 }
2359
2360
2361 /*
2362 * 'ippGetRequestId()' - Get the request ID from an IPP message.
2363 *
2364 * @since CUPS 1.6/macOS 10.8@
2365 */
2366
2367 int /* O - Request ID or 0 on error */
2368 ippGetRequestId(ipp_t *ipp) /* I - IPP message */
2369 {
2370 /*
2371 * Range check input...
2372 */
2373
2374 if (!ipp)
2375 return (0);
2376
2377 /*
2378 * Return the request ID...
2379 */
2380
2381 return (ipp->request.any.request_id);
2382 }
2383
2384
2385 /*
2386 * 'ippGetResolution()' - Get a resolution value for an attribute.
2387 *
2388 * The @code element@ parameter specifies which value to get from 0 to
2389 * @code ippGetCount(attr)@ - 1.
2390 *
2391 * @since CUPS 1.6/macOS 10.8@
2392 */
2393
2394 int /* O - Horizontal/cross feed resolution or 0 */
2395 ippGetResolution(
2396 ipp_attribute_t *attr, /* I - IPP attribute */
2397 int element, /* I - Value number (0-based) */
2398 int *yres, /* O - Vertical/feed resolution */
2399 ipp_res_t *units) /* O - Units for resolution */
2400 {
2401 /*
2402 * Range check input...
2403 */
2404
2405 if (!attr || attr->value_tag != IPP_TAG_RESOLUTION ||
2406 element < 0 || element >= attr->num_values)
2407 {
2408 if (yres)
2409 *yres = 0;
2410
2411 if (units)
2412 *units = (ipp_res_t)0;
2413
2414 return (0);
2415 }
2416
2417 /*
2418 * Return the value...
2419 */
2420
2421 if (yres)
2422 *yres = attr->values[element].resolution.yres;
2423
2424 if (units)
2425 *units = attr->values[element].resolution.units;
2426
2427 return (attr->values[element].resolution.xres);
2428 }
2429
2430
2431 /*
2432 * 'ippGetState()' - Get the IPP message state.
2433 *
2434 * @since CUPS 1.6/macOS 10.8@
2435 */
2436
2437 ipp_state_t /* O - IPP message state value */
2438 ippGetState(ipp_t *ipp) /* I - IPP message */
2439 {
2440 /*
2441 * Range check input...
2442 */
2443
2444 if (!ipp)
2445 return (IPP_STATE_IDLE);
2446
2447 /*
2448 * Return the value...
2449 */
2450
2451 return (ipp->state);
2452 }
2453
2454
2455 /*
2456 * 'ippGetStatusCode()' - Get the status code from an IPP response or event message.
2457 *
2458 * @since CUPS 1.6/macOS 10.8@
2459 */
2460
2461 ipp_status_t /* O - Status code in IPP message */
2462 ippGetStatusCode(ipp_t *ipp) /* I - IPP response or event message */
2463 {
2464 /*
2465 * Range check input...
2466 */
2467
2468 if (!ipp)
2469 return (IPP_STATUS_ERROR_INTERNAL);
2470
2471 /*
2472 * Return the value...
2473 */
2474
2475 return (ipp->request.status.status_code);
2476 }
2477
2478
2479 /*
2480 * 'ippGetString()' - Get the string and optionally the language code for an attribute.
2481 *
2482 * The @code element@ parameter specifies which value to get from 0 to
2483 * @code ippGetCount(attr)@ - 1.
2484 *
2485 * @since CUPS 1.6/macOS 10.8@
2486 */
2487
2488 const char *
2489 ippGetString(ipp_attribute_t *attr, /* I - IPP attribute */
2490 int element, /* I - Value number (0-based) */
2491 const char **language)/* O - Language code (@code NULL@ for don't care) */
2492 {
2493 ipp_tag_t tag; /* Value tag */
2494
2495
2496 /*
2497 * Range check input...
2498 */
2499
2500 tag = ippGetValueTag(attr);
2501
2502 if (!attr || element < 0 || element >= attr->num_values || (tag != IPP_TAG_TEXTLANG && tag != IPP_TAG_NAMELANG && (tag < IPP_TAG_TEXT || tag > IPP_TAG_MIMETYPE)))
2503 return (NULL);
2504
2505 /*
2506 * Return the value...
2507 */
2508
2509 if (language)
2510 *language = attr->values[element].string.language;
2511
2512 return (attr->values[element].string.text);
2513 }
2514
2515
2516 /*
2517 * 'ippGetValueTag()' - Get the value tag for an attribute.
2518 *
2519 * @since CUPS 1.6/macOS 10.8@
2520 */
2521
2522 ipp_tag_t /* O - Value tag or @code IPP_TAG_ZERO@ on error */
2523 ippGetValueTag(ipp_attribute_t *attr) /* I - IPP attribute */
2524 {
2525 /*
2526 * Range check input...
2527 */
2528
2529 if (!attr)
2530 return (IPP_TAG_ZERO);
2531
2532 /*
2533 * Return the value...
2534 */
2535
2536 return (attr->value_tag & IPP_TAG_CUPS_MASK);
2537 }
2538
2539
2540 /*
2541 * 'ippGetVersion()' - Get the major and minor version number from an IPP message.
2542 *
2543 * @since CUPS 1.6/macOS 10.8@
2544 */
2545
2546 int /* O - Major version number or 0 on error */
2547 ippGetVersion(ipp_t *ipp, /* I - IPP message */
2548 int *minor) /* O - Minor version number or @code NULL@ for don't care */
2549 {
2550 /*
2551 * Range check input...
2552 */
2553
2554 if (!ipp)
2555 {
2556 if (minor)
2557 *minor = 0;
2558
2559 return (0);
2560 }
2561
2562 /*
2563 * Return the value...
2564 */
2565
2566 if (minor)
2567 *minor = ipp->request.any.version[1];
2568
2569 return (ipp->request.any.version[0]);
2570 }
2571
2572
2573 /*
2574 * 'ippLength()' - Compute the length of an IPP message.
2575 */
2576
2577 size_t /* O - Size of IPP message */
2578 ippLength(ipp_t *ipp) /* I - IPP message */
2579 {
2580 return (ipp_length(ipp, 0));
2581 }
2582
2583
2584 /*
2585 * 'ippNextAttribute()' - Return the next attribute in the message.
2586 *
2587 * @since CUPS 1.6/macOS 10.8@
2588 */
2589
2590 ipp_attribute_t * /* O - Next attribute or @code NULL@ if none */
2591 ippNextAttribute(ipp_t *ipp) /* I - IPP message */
2592 {
2593 /*
2594 * Range check input...
2595 */
2596
2597 if (!ipp || !ipp->current)
2598 return (NULL);
2599
2600 /*
2601 * Return the next attribute...
2602 */
2603
2604 return (ipp->current = ipp->current->next);
2605 }
2606
2607
2608 /*
2609 * 'ippNew()' - Allocate a new IPP message.
2610 */
2611
2612 ipp_t * /* O - New IPP message */
2613 ippNew(void)
2614 {
2615 ipp_t *temp; /* New IPP message */
2616 _cups_globals_t *cg = _cupsGlobals();
2617 /* Global data */
2618
2619
2620 DEBUG_puts("ippNew()");
2621
2622 if ((temp = (ipp_t *)calloc(1, sizeof(ipp_t))) != NULL)
2623 {
2624 /*
2625 * Set default version - usually 2.0...
2626 */
2627
2628 DEBUG_printf(("4debug_alloc: %p IPP message", (void *)temp));
2629
2630 if (cg->server_version == 0)
2631 _cupsSetDefaults();
2632
2633 temp->request.any.version[0] = (ipp_uchar_t)(cg->server_version / 10);
2634 temp->request.any.version[1] = (ipp_uchar_t)(cg->server_version % 10);
2635 temp->use = 1;
2636 }
2637
2638 DEBUG_printf(("1ippNew: Returning %p", (void *)temp));
2639
2640 return (temp);
2641 }
2642
2643
2644 /*
2645 * 'ippNewRequest()' - Allocate a new IPP request message.
2646 *
2647 * The new request message is initialized with the "attributes-charset" and
2648 * "attributes-natural-language" attributes added. The
2649 * "attributes-natural-language" value is derived from the current locale.
2650 *
2651 * @since CUPS 1.2/macOS 10.5@
2652 */
2653
2654 ipp_t * /* O - IPP request message */
2655 ippNewRequest(ipp_op_t op) /* I - Operation code */
2656 {
2657 ipp_t *request; /* IPP request message */
2658 cups_lang_t *language; /* Current language localization */
2659 static int request_id = 0; /* Current request ID */
2660 static _cups_mutex_t request_mutex = _CUPS_MUTEX_INITIALIZER;
2661 /* Mutex for request ID */
2662
2663
2664 DEBUG_printf(("ippNewRequest(op=%02x(%s))", op, ippOpString(op)));
2665
2666 /*
2667 * Create a new IPP message...
2668 */
2669
2670 if ((request = ippNew()) == NULL)
2671 return (NULL);
2672
2673 /*
2674 * Set the operation and request ID...
2675 */
2676
2677 _cupsMutexLock(&request_mutex);
2678
2679 request->request.op.operation_id = op;
2680 request->request.op.request_id = ++request_id;
2681
2682 _cupsMutexUnlock(&request_mutex);
2683
2684 /*
2685 * Use UTF-8 as the character set...
2686 */
2687
2688 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
2689 "attributes-charset", NULL, "utf-8");
2690
2691 /*
2692 * Get the language from the current locale...
2693 */
2694
2695 language = cupsLangDefault();
2696
2697 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
2698 "attributes-natural-language", NULL, language->language);
2699
2700 /*
2701 * Return the new request...
2702 */
2703
2704 return (request);
2705 }
2706
2707
2708 /*
2709 * 'ippNewResponse()' - Allocate a new IPP response message.
2710 *
2711 * The new response message is initialized with the same "version-number",
2712 * "request-id", "attributes-charset", and "attributes-natural-language" as the
2713 * provided request message. If the "attributes-charset" or
2714 * "attributes-natural-language" attributes are missing from the request,
2715 * 'utf-8' and a value derived from the current locale are substituted,
2716 * respectively.
2717 *
2718 * @since CUPS 1.7/macOS 10.9@
2719 */
2720
2721 ipp_t * /* O - IPP response message */
2722 ippNewResponse(ipp_t *request) /* I - IPP request message */
2723 {
2724 ipp_t *response; /* IPP response message */
2725 ipp_attribute_t *attr; /* Current attribute */
2726
2727
2728 /*
2729 * Range check input...
2730 */
2731
2732 if (!request)
2733 return (NULL);
2734
2735 /*
2736 * Create a new IPP message...
2737 */
2738
2739 if ((response = ippNew()) == NULL)
2740 return (NULL);
2741
2742 /*
2743 * Copy the request values over to the response...
2744 */
2745
2746 response->request.status.version[0] = request->request.op.version[0];
2747 response->request.status.version[1] = request->request.op.version[1];
2748 response->request.status.request_id = request->request.op.request_id;
2749
2750 /*
2751 * The first attribute MUST be attributes-charset...
2752 */
2753
2754 attr = request->attrs;
2755
2756 if (attr && attr->name && !strcmp(attr->name, "attributes-charset") &&
2757 attr->group_tag == IPP_TAG_OPERATION &&
2758 attr->value_tag == IPP_TAG_CHARSET &&
2759 attr->num_values == 1)
2760 {
2761 /*
2762 * Copy charset from request...
2763 */
2764
2765 ippAddString(response, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
2766 "attributes-charset", NULL, attr->values[0].string.text);
2767 }
2768 else
2769 {
2770 /*
2771 * Use "utf-8" as the default...
2772 */
2773
2774 ippAddString(response, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
2775 "attributes-charset", NULL, "utf-8");
2776 }
2777
2778 /*
2779 * Then attributes-natural-language...
2780 */
2781
2782 if (attr)
2783 attr = attr->next;
2784
2785 if (attr && attr->name &&
2786 !strcmp(attr->name, "attributes-natural-language") &&
2787 attr->group_tag == IPP_TAG_OPERATION &&
2788 attr->value_tag == IPP_TAG_LANGUAGE &&
2789 attr->num_values == 1)
2790 {
2791 /*
2792 * Copy language from request...
2793 */
2794
2795 ippAddString(response, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
2796 "attributes-natural-language", NULL,
2797 attr->values[0].string.text);
2798 }
2799 else
2800 {
2801 /*
2802 * Use the language from the current locale...
2803 */
2804
2805 cups_lang_t *language = cupsLangDefault();
2806 /* Current locale */
2807
2808 ippAddString(response, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
2809 "attributes-natural-language", NULL, language->language);
2810 }
2811
2812 return (response);
2813 }
2814
2815
2816 /*
2817 * 'ippRead()' - Read data for an IPP message from a HTTP connection.
2818 */
2819
2820 ipp_state_t /* O - Current state */
2821 ippRead(http_t *http, /* I - HTTP connection */
2822 ipp_t *ipp) /* I - IPP data */
2823 {
2824 DEBUG_printf(("ippRead(http=%p, ipp=%p), data_remaining=" CUPS_LLFMT, (void *)http, (void *)ipp, CUPS_LLCAST (http ? http->data_remaining : -1)));
2825
2826 if (!http)
2827 return (IPP_STATE_ERROR);
2828
2829 DEBUG_printf(("2ippRead: http->state=%d, http->used=%d", http->state, http->used));
2830
2831 return (ippReadIO(http, (ipp_iocb_t)ipp_read_http, http->blocking, NULL,
2832 ipp));
2833 }
2834
2835
2836 /*
2837 * 'ippReadFile()' - Read data for an IPP message from a file.
2838 *
2839 * @since CUPS 1.1.19/macOS 10.3@
2840 */
2841
2842 ipp_state_t /* O - Current state */
2843 ippReadFile(int fd, /* I - HTTP data */
2844 ipp_t *ipp) /* I - IPP data */
2845 {
2846 DEBUG_printf(("ippReadFile(fd=%d, ipp=%p)", fd, (void *)ipp));
2847
2848 return (ippReadIO(&fd, (ipp_iocb_t)ipp_read_file, 1, NULL, ipp));
2849 }
2850
2851
2852 /*
2853 * 'ippReadIO()' - Read data for an IPP message.
2854 *
2855 * @since CUPS 1.2/macOS 10.5@
2856 */
2857
2858 ipp_state_t /* O - Current state */
2859 ippReadIO(void *src, /* I - Data source */
2860 ipp_iocb_t cb, /* I - Read callback function */
2861 int blocking, /* I - Use blocking IO? */
2862 ipp_t *parent, /* I - Parent request, if any */
2863 ipp_t *ipp) /* I - IPP data */
2864 {
2865 int n; /* Length of data */
2866 unsigned char *buffer, /* Data buffer */
2867 string[IPP_MAX_TEXT],
2868 /* Small string buffer */
2869 *bufptr, /* Pointer into buffer */
2870 *bufptrEnd; /* Pointer after valid buffer range */
2871 ipp_attribute_t *attr; /* Current attribute */
2872 ipp_tag_t tag; /* Current tag */
2873 ipp_tag_t value_tag; /* Current value tag */
2874 _ipp_value_t *value; /* Current value */
2875
2876
2877 DEBUG_printf(("ippReadIO(src=%p, cb=%p, blocking=%d, parent=%p, ipp=%p)", (void *)src, (void *)cb, blocking, (void *)parent, (void *)ipp));
2878 DEBUG_printf(("2ippReadIO: ipp->state=%d", ipp ? ipp->state : IPP_STATE_ERROR));
2879
2880 if (!src || !ipp)
2881 return (IPP_STATE_ERROR);
2882
2883 if ((buffer = (unsigned char *)_cupsBufferGet(IPP_BUF_SIZE)) == NULL)
2884 {
2885 DEBUG_puts("1ippReadIO: Unable to get read buffer.");
2886 return (IPP_STATE_ERROR);
2887 }
2888
2889 switch (ipp->state)
2890 {
2891 case IPP_STATE_IDLE :
2892 ipp->state ++; /* Avoid common problem... */
2893
2894 case IPP_STATE_HEADER :
2895 if (parent == NULL)
2896 {
2897 /*
2898 * Get the request header...
2899 */
2900
2901 if ((*cb)(src, buffer, 8) < 8)
2902 {
2903 DEBUG_puts("1ippReadIO: Unable to read header.");
2904 _cupsBufferRelease((char *)buffer);
2905 return (IPP_STATE_ERROR);
2906 }
2907
2908 /*
2909 * Then copy the request header over...
2910 */
2911
2912 ipp->request.any.version[0] = buffer[0];
2913 ipp->request.any.version[1] = buffer[1];
2914 ipp->request.any.op_status = (buffer[2] << 8) | buffer[3];
2915 ipp->request.any.request_id = (((((buffer[4] << 8) | buffer[5]) << 8) |
2916 buffer[6]) << 8) | buffer[7];
2917
2918 DEBUG_printf(("2ippReadIO: version=%d.%d", buffer[0], buffer[1]));
2919 DEBUG_printf(("2ippReadIO: op_status=%04x",
2920 ipp->request.any.op_status));
2921 DEBUG_printf(("2ippReadIO: request_id=%d",
2922 ipp->request.any.request_id));
2923 }
2924
2925 ipp->state = IPP_STATE_ATTRIBUTE;
2926 ipp->current = NULL;
2927 ipp->curtag = IPP_TAG_ZERO;
2928 ipp->prev = ipp->last;
2929
2930 /*
2931 * If blocking is disabled, stop here...
2932 */
2933
2934 if (!blocking)
2935 break;
2936
2937 case IPP_STATE_ATTRIBUTE :
2938 for (;;)
2939 {
2940 if ((*cb)(src, buffer, 1) < 1)
2941 {
2942 DEBUG_puts("1ippReadIO: Callback returned EOF/error");
2943 _cupsBufferRelease((char *)buffer);
2944 return (IPP_STATE_ERROR);
2945 }
2946
2947 DEBUG_printf(("2ippReadIO: ipp->current=%p, ipp->prev=%p", (void *)ipp->current, (void *)ipp->prev));
2948
2949 /*
2950 * Read this attribute...
2951 */
2952
2953 tag = (ipp_tag_t)buffer[0];
2954 if (tag == IPP_TAG_EXTENSION)
2955 {
2956 /*
2957 * Read 32-bit "extension" tag...
2958 */
2959
2960 if ((*cb)(src, buffer, 4) < 4)
2961 {
2962 DEBUG_puts("1ippReadIO: Callback returned EOF/error");
2963 _cupsBufferRelease((char *)buffer);
2964 return (IPP_STATE_ERROR);
2965 }
2966
2967 tag = (ipp_tag_t)((((((buffer[0] << 8) | buffer[1]) << 8) |
2968 buffer[2]) << 8) | buffer[3]);
2969
2970 if (tag & IPP_TAG_CUPS_CONST)
2971 {
2972 /*
2973 * Fail if the high bit is set in the tag...
2974 */
2975
2976 _cupsSetError(IPP_STATUS_ERROR_INTERNAL, _("IPP extension tag larger than 0x7FFFFFFF."), 1);
2977 DEBUG_printf(("1ippReadIO: bad tag 0x%x.", tag));
2978 _cupsBufferRelease((char *)buffer);
2979 return (IPP_STATE_ERROR);
2980 }
2981 }
2982
2983 if (tag == IPP_TAG_END)
2984 {
2985 /*
2986 * No more attributes left...
2987 */
2988
2989 DEBUG_puts("2ippReadIO: IPP_TAG_END.");
2990
2991 ipp->state = IPP_STATE_DATA;
2992 break;
2993 }
2994 else if (tag == IPP_TAG_ZERO || (tag == IPP_TAG_OPERATION && ipp->curtag != IPP_TAG_ZERO))
2995 {
2996 _cupsSetError(IPP_STATUS_ERROR_INTERNAL, _("Invalid group tag."), 1);
2997 DEBUG_printf(("1ippReadIO: bad tag 0x%02x.", tag));
2998 _cupsBufferRelease((char *)buffer);
2999 return (IPP_STATE_ERROR);
3000 }
3001 else if (tag < IPP_TAG_UNSUPPORTED_VALUE)
3002 {
3003 /*
3004 * Group tag... Set the current group and continue...
3005 */
3006
3007 if (ipp->curtag == tag)
3008 ipp->prev = ippAddSeparator(ipp);
3009 else if (ipp->current)
3010 ipp->prev = ipp->current;
3011
3012 ipp->curtag = tag;
3013 ipp->current = NULL;
3014 DEBUG_printf(("2ippReadIO: group tag=%x(%s), ipp->prev=%p", tag, ippTagString(tag), (void *)ipp->prev));
3015 continue;
3016 }
3017
3018 DEBUG_printf(("2ippReadIO: value tag=%x(%s)", tag,
3019 ippTagString(tag)));
3020
3021 /*
3022 * Get the name...
3023 */
3024
3025 if ((*cb)(src, buffer, 2) < 2)
3026 {
3027 DEBUG_puts("1ippReadIO: unable to read name length.");
3028 _cupsBufferRelease((char *)buffer);
3029 return (IPP_STATE_ERROR);
3030 }
3031
3032 n = (buffer[0] << 8) | buffer[1];
3033
3034 if (n >= IPP_BUF_SIZE)
3035 {
3036 _cupsSetError(IPP_STATUS_ERROR_INTERNAL, _("IPP name larger than 32767 bytes."), 1);
3037 DEBUG_printf(("1ippReadIO: bad name length %d.", n));
3038 _cupsBufferRelease((char *)buffer);
3039 return (IPP_STATE_ERROR);
3040 }
3041
3042 DEBUG_printf(("2ippReadIO: name length=%d", n));
3043
3044 if (n && parent)
3045 {
3046 _cupsSetError(IPP_STATUS_ERROR_INTERNAL, _("Invalid named IPP attribute in collection."), 1);
3047 DEBUG_puts("1ippReadIO: bad attribute name in collection.");
3048 return (IPP_STATE_ERROR);
3049 }
3050 else if (n == 0 && tag != IPP_TAG_MEMBERNAME && tag != IPP_TAG_END_COLLECTION)
3051 {
3052 /*
3053 * More values for current attribute...
3054 */
3055
3056 if (ipp->current == NULL)
3057 {
3058 _cupsSetError(IPP_STATUS_ERROR_INTERNAL, _("IPP attribute has no name."), 1);
3059 DEBUG_puts("1ippReadIO: Attribute without name and no current.");
3060 _cupsBufferRelease((char *)buffer);
3061 return (IPP_STATE_ERROR);
3062 }
3063
3064 attr = ipp->current;
3065 value_tag = (ipp_tag_t)(attr->value_tag & IPP_TAG_CUPS_MASK);
3066
3067 /*
3068 * Make sure we aren't adding a new value of a different
3069 * type...
3070 */
3071
3072 if (value_tag == IPP_TAG_ZERO)
3073 {
3074 /*
3075 * Setting the value of a collection member...
3076 */
3077
3078 attr->value_tag = tag;
3079 }
3080 else if (value_tag == IPP_TAG_TEXTLANG ||
3081 value_tag == IPP_TAG_NAMELANG ||
3082 (value_tag >= IPP_TAG_TEXT &&
3083 value_tag <= IPP_TAG_MIMETYPE))
3084 {
3085 /*
3086 * String values can sometimes come across in different
3087 * forms; accept sets of differing values...
3088 */
3089
3090 if (tag != IPP_TAG_TEXTLANG && tag != IPP_TAG_NAMELANG &&
3091 (tag < IPP_TAG_TEXT || tag > IPP_TAG_MIMETYPE) &&
3092 tag != IPP_TAG_NOVALUE)
3093 {
3094 _cupsSetError(IPP_STATUS_ERROR_INTERNAL,
3095 _("IPP 1setOf attribute with incompatible value "
3096 "tags."), 1);
3097 DEBUG_printf(("1ippReadIO: 1setOf value tag %x(%s) != %x(%s)",
3098 value_tag, ippTagString(value_tag), tag,
3099 ippTagString(tag)));
3100 _cupsBufferRelease((char *)buffer);
3101 return (IPP_STATE_ERROR);
3102 }
3103
3104 if (value_tag != tag)
3105 {
3106 DEBUG_printf(("1ippReadIO: Converting %s attribute from %s to %s.",
3107 attr->name, ippTagString(value_tag), ippTagString(tag)));
3108 ippSetValueTag(ipp, &attr, tag);
3109 }
3110 }
3111 else if (value_tag == IPP_TAG_INTEGER ||
3112 value_tag == IPP_TAG_RANGE)
3113 {
3114 /*
3115 * Integer and rangeOfInteger values can sometimes be mixed; accept
3116 * sets of differing values...
3117 */
3118
3119 if (tag != IPP_TAG_INTEGER && tag != IPP_TAG_RANGE)
3120 {
3121 _cupsSetError(IPP_STATUS_ERROR_INTERNAL,
3122 _("IPP 1setOf attribute with incompatible value "
3123 "tags."), 1);
3124 DEBUG_printf(("1ippReadIO: 1setOf value tag %x(%s) != %x(%s)",
3125 value_tag, ippTagString(value_tag), tag,
3126 ippTagString(tag)));
3127 _cupsBufferRelease((char *)buffer);
3128 return (IPP_STATE_ERROR);
3129 }
3130
3131 if (value_tag == IPP_TAG_INTEGER && tag == IPP_TAG_RANGE)
3132 {
3133 /*
3134 * Convert integer values to rangeOfInteger values...
3135 */
3136
3137 DEBUG_printf(("1ippReadIO: Converting %s attribute to "
3138 "rangeOfInteger.", attr->name));
3139 ippSetValueTag(ipp, &attr, IPP_TAG_RANGE);
3140 }
3141 }
3142 else if (value_tag != tag)
3143 {
3144 _cupsSetError(IPP_STATUS_ERROR_INTERNAL,
3145 _("IPP 1setOf attribute with incompatible value "
3146 "tags."), 1);
3147 DEBUG_printf(("1ippReadIO: value tag %x(%s) != %x(%s)",
3148 value_tag, ippTagString(value_tag), tag,
3149 ippTagString(tag)));
3150 _cupsBufferRelease((char *)buffer);
3151 return (IPP_STATE_ERROR);
3152 }
3153
3154 /*
3155 * Finally, reallocate the attribute array as needed...
3156 */
3157
3158 if ((value = ipp_set_value(ipp, &attr, attr->num_values)) == NULL)
3159 {
3160 _cupsBufferRelease((char *)buffer);
3161 return (IPP_STATE_ERROR);
3162 }
3163 }
3164 else if (tag == IPP_TAG_MEMBERNAME)
3165 {
3166 /*
3167 * Name must be length 0!
3168 */
3169
3170 if (n)
3171 {
3172 _cupsSetError(IPP_STATUS_ERROR_INTERNAL, _("IPP member name is not empty."), 1);
3173 DEBUG_puts("1ippReadIO: member name not empty.");
3174 _cupsBufferRelease((char *)buffer);
3175 return (IPP_STATE_ERROR);
3176 }
3177
3178 if (ipp->current)
3179 ipp->prev = ipp->current;
3180
3181 attr = ipp->current = ipp_add_attr(ipp, NULL, ipp->curtag, IPP_TAG_ZERO, 1);
3182 if (!attr)
3183 {
3184 _cupsSetHTTPError(HTTP_STATUS_ERROR);
3185 DEBUG_puts("1ippReadIO: unable to allocate attribute.");
3186 _cupsBufferRelease((char *)buffer);
3187 return (IPP_STATE_ERROR);
3188 }
3189
3190 DEBUG_printf(("2ippReadIO: membername, ipp->current=%p, ipp->prev=%p", (void *)ipp->current, (void *)ipp->prev));
3191
3192 value = attr->values;
3193 }
3194 else if (tag != IPP_TAG_END_COLLECTION)
3195 {
3196 /*
3197 * New attribute; read the name and add it...
3198 */
3199
3200 if ((*cb)(src, buffer, (size_t)n) < n)
3201 {
3202 DEBUG_puts("1ippReadIO: unable to read name.");
3203 _cupsBufferRelease((char *)buffer);
3204 return (IPP_STATE_ERROR);
3205 }
3206
3207 buffer[n] = '\0';
3208
3209 if (ipp->current)
3210 ipp->prev = ipp->current;
3211
3212 if ((attr = ipp->current = ipp_add_attr(ipp, (char *)buffer, ipp->curtag, tag,
3213 1)) == NULL)
3214 {
3215 _cupsSetHTTPError(HTTP_STATUS_ERROR);
3216 DEBUG_puts("1ippReadIO: unable to allocate attribute.");
3217 _cupsBufferRelease((char *)buffer);
3218 return (IPP_STATE_ERROR);
3219 }
3220
3221 DEBUG_printf(("2ippReadIO: name=\"%s\", ipp->current=%p, ipp->prev=%p", buffer, (void *)ipp->current, (void *)ipp->prev));
3222
3223 value = attr->values;
3224 }
3225 else
3226 {
3227 attr = NULL;
3228 value = NULL;
3229 }
3230
3231 if ((*cb)(src, buffer, 2) < 2)
3232 {
3233 DEBUG_puts("1ippReadIO: unable to read value length.");
3234 _cupsBufferRelease((char *)buffer);
3235 return (IPP_STATE_ERROR);
3236 }
3237
3238 n = (buffer[0] << 8) | buffer[1];
3239 DEBUG_printf(("2ippReadIO: value length=%d", n));
3240
3241 if (n >= IPP_BUF_SIZE)
3242 {
3243 _cupsSetError(IPP_STATUS_ERROR_INTERNAL,
3244 _("IPP value larger than 32767 bytes."), 1);
3245 DEBUG_printf(("1ippReadIO: bad value length %d.", n));
3246 _cupsBufferRelease((char *)buffer);
3247 return (IPP_STATE_ERROR);
3248 }
3249
3250 switch (tag)
3251 {
3252 case IPP_TAG_INTEGER :
3253 case IPP_TAG_ENUM :
3254 if (n != 4)
3255 {
3256 if (tag == IPP_TAG_INTEGER)
3257 _cupsSetError(IPP_STATUS_ERROR_INTERNAL,
3258 _("IPP integer value not 4 bytes."), 1);
3259 else
3260 _cupsSetError(IPP_STATUS_ERROR_INTERNAL,
3261 _("IPP enum value not 4 bytes."), 1);
3262 DEBUG_printf(("1ippReadIO: bad integer value length %d.", n));
3263 _cupsBufferRelease((char *)buffer);
3264 return (IPP_STATE_ERROR);
3265 }
3266
3267 if ((*cb)(src, buffer, 4) < 4)
3268 {
3269 DEBUG_puts("1ippReadIO: Unable to read integer value.");
3270 _cupsBufferRelease((char *)buffer);
3271 return (IPP_STATE_ERROR);
3272 }
3273
3274 n = (((((buffer[0] << 8) | buffer[1]) << 8) | buffer[2]) << 8) |
3275 buffer[3];
3276
3277 if (attr->value_tag == IPP_TAG_RANGE)
3278 value->range.lower = value->range.upper = n;
3279 else
3280 value->integer = n;
3281 break;
3282
3283 case IPP_TAG_BOOLEAN :
3284 if (n != 1)
3285 {
3286 _cupsSetError(IPP_STATUS_ERROR_INTERNAL, _("IPP boolean value not 1 byte."),
3287 1);
3288 DEBUG_printf(("1ippReadIO: bad boolean value length %d.", n));
3289 _cupsBufferRelease((char *)buffer);
3290 return (IPP_STATE_ERROR);
3291 }
3292
3293 if ((*cb)(src, buffer, 1) < 1)
3294 {
3295 DEBUG_puts("1ippReadIO: Unable to read boolean value.");
3296 _cupsBufferRelease((char *)buffer);
3297 return (IPP_STATE_ERROR);
3298 }
3299
3300 value->boolean = (char)buffer[0];
3301 break;
3302
3303 case IPP_TAG_UNSUPPORTED_VALUE :
3304 case IPP_TAG_DEFAULT :
3305 case IPP_TAG_UNKNOWN :
3306 case IPP_TAG_NOVALUE :
3307 case IPP_TAG_NOTSETTABLE :
3308 case IPP_TAG_DELETEATTR :
3309 case IPP_TAG_ADMINDEFINE :
3310 /*
3311 * These value types are not supposed to have values, however
3312 * some vendors (Brother) do not implement IPP correctly and so
3313 * we need to map non-empty values to text...
3314 */
3315
3316 if (attr->value_tag == tag)
3317 {
3318 if (n == 0)
3319 break;
3320
3321 attr->value_tag = IPP_TAG_TEXT;
3322 }
3323
3324 case IPP_TAG_TEXT :
3325 case IPP_TAG_NAME :
3326 case IPP_TAG_RESERVED_STRING :
3327 case IPP_TAG_KEYWORD :
3328 case IPP_TAG_URI :
3329 case IPP_TAG_URISCHEME :
3330 case IPP_TAG_CHARSET :
3331 case IPP_TAG_LANGUAGE :
3332 case IPP_TAG_MIMETYPE :
3333 if (n > 0)
3334 {
3335 if ((*cb)(src, buffer, (size_t)n) < n)
3336 {
3337 DEBUG_puts("1ippReadIO: unable to read string value.");
3338 _cupsBufferRelease((char *)buffer);
3339 return (IPP_STATE_ERROR);
3340 }
3341 }
3342
3343 buffer[n] = '\0';
3344 value->string.text = _cupsStrAlloc((char *)buffer);
3345 DEBUG_printf(("2ippReadIO: value=\"%s\"", value->string.text));
3346 break;
3347
3348 case IPP_TAG_DATE :
3349 if (n != 11)
3350 {
3351 _cupsSetError(IPP_STATUS_ERROR_INTERNAL, _("IPP date value not 11 bytes."), 1);
3352 DEBUG_printf(("1ippReadIO: bad date value length %d.", n));
3353 _cupsBufferRelease((char *)buffer);
3354 return (IPP_STATE_ERROR);
3355 }
3356
3357 if ((*cb)(src, value->date, 11) < 11)
3358 {
3359 DEBUG_puts("1ippReadIO: Unable to read date value.");
3360 _cupsBufferRelease((char *)buffer);
3361 return (IPP_STATE_ERROR);
3362 }
3363 break;
3364
3365 case IPP_TAG_RESOLUTION :
3366 if (n != 9)
3367 {
3368 _cupsSetError(IPP_STATUS_ERROR_INTERNAL,
3369 _("IPP resolution value not 9 bytes."), 1);
3370 DEBUG_printf(("1ippReadIO: bad resolution value length %d.", n));
3371 _cupsBufferRelease((char *)buffer);
3372 return (IPP_STATE_ERROR);
3373 }
3374
3375 if ((*cb)(src, buffer, 9) < 9)
3376 {
3377 DEBUG_puts("1ippReadIO: Unable to read resolution value.");
3378 _cupsBufferRelease((char *)buffer);
3379 return (IPP_STATE_ERROR);
3380 }
3381
3382 value->resolution.xres =
3383 (((((buffer[0] << 8) | buffer[1]) << 8) | buffer[2]) << 8) |
3384 buffer[3];
3385 value->resolution.yres =
3386 (((((buffer[4] << 8) | buffer[5]) << 8) | buffer[6]) << 8) |
3387 buffer[7];
3388 value->resolution.units =
3389 (ipp_res_t)buffer[8];
3390 break;
3391
3392 case IPP_TAG_RANGE :
3393 if (n != 8)
3394 {
3395 _cupsSetError(IPP_STATUS_ERROR_INTERNAL,
3396 _("IPP rangeOfInteger value not 8 bytes."), 1);
3397 DEBUG_printf(("1ippReadIO: bad rangeOfInteger value length "
3398 "%d.", n));
3399 _cupsBufferRelease((char *)buffer);
3400 return (IPP_STATE_ERROR);
3401 }
3402
3403 if ((*cb)(src, buffer, 8) < 8)
3404 {
3405 DEBUG_puts("1ippReadIO: Unable to read range value.");
3406 _cupsBufferRelease((char *)buffer);
3407 return (IPP_STATE_ERROR);
3408 }
3409
3410 value->range.lower =
3411 (((((buffer[0] << 8) | buffer[1]) << 8) | buffer[2]) << 8) |
3412 buffer[3];
3413 value->range.upper =
3414 (((((buffer[4] << 8) | buffer[5]) << 8) | buffer[6]) << 8) |
3415 buffer[7];
3416 break;
3417
3418 case IPP_TAG_TEXTLANG :
3419 case IPP_TAG_NAMELANG :
3420 if (n < 4)
3421 {
3422 if (tag == IPP_TAG_TEXTLANG)
3423 _cupsSetError(IPP_STATUS_ERROR_INTERNAL,
3424 _("IPP textWithLanguage value less than "
3425 "minimum 4 bytes."), 1);
3426 else
3427 _cupsSetError(IPP_STATUS_ERROR_INTERNAL,
3428 _("IPP nameWithLanguage value less than "
3429 "minimum 4 bytes."), 1);
3430 DEBUG_printf(("1ippReadIO: bad stringWithLanguage value "
3431 "length %d.", n));
3432 _cupsBufferRelease((char *)buffer);
3433 return (IPP_STATE_ERROR);
3434 }
3435
3436 if ((*cb)(src, buffer, (size_t)n) < n)
3437 {
3438 DEBUG_puts("1ippReadIO: Unable to read string w/language "
3439 "value.");
3440 _cupsBufferRelease((char *)buffer);
3441 return (IPP_STATE_ERROR);
3442 }
3443
3444 bufptr = buffer;
3445 bufptrEnd = &buffer[n];
3446
3447
3448 /*
3449 * text-with-language and name-with-language are composite
3450 * values:
3451 *
3452 * language-length
3453 * language
3454 * text-length
3455 * text
3456 */
3457
3458 n = (bufptr[0] << 8) | bufptr[1];
3459
3460 if ((bufptr + 2 + n) > bufptrEnd || n >= (int)sizeof(string))
3461 {
3462 _cupsSetError(IPP_STATUS_ERROR_INTERNAL,
3463 _("IPP language length overflows value."), 1);
3464 DEBUG_printf(("1ippReadIO: bad language value length %d.",
3465 n));
3466 _cupsBufferRelease((char *)buffer);
3467 return (IPP_STATE_ERROR);
3468 }
3469 else if (n >= IPP_MAX_LANGUAGE)
3470 {
3471 _cupsSetError(IPP_STATUS_ERROR_INTERNAL,
3472 _("IPP language length too large."), 1);
3473 DEBUG_printf(("1ippReadIO: bad language value length %d.",
3474 n));
3475 _cupsBufferRelease((char *)buffer);
3476 return (IPP_STATE_ERROR);
3477 }
3478
3479 memcpy(string, bufptr + 2, (size_t)n);
3480 string[n] = '\0';
3481
3482 value->string.language = _cupsStrAlloc((char *)string);
3483
3484 bufptr += 2 + n;
3485 n = (bufptr[0] << 8) | bufptr[1];
3486
3487 if ((bufptr + 2 + n) > bufptrEnd)
3488 {
3489 _cupsSetError(IPP_STATUS_ERROR_INTERNAL,
3490 _("IPP string length overflows value."), 1);
3491 DEBUG_printf(("1ippReadIO: bad string value length %d.", n));
3492 _cupsBufferRelease((char *)buffer);
3493 return (IPP_STATE_ERROR);
3494 }
3495
3496 bufptr[2 + n] = '\0';
3497 value->string.text = _cupsStrAlloc((char *)bufptr + 2);
3498 break;
3499
3500 case IPP_TAG_BEGIN_COLLECTION :
3501 /*
3502 * Oh, boy, here comes a collection value, so read it...
3503 */
3504
3505 value->collection = ippNew();
3506
3507 if (n > 0)
3508 {
3509 _cupsSetError(IPP_STATUS_ERROR_INTERNAL,
3510 _("IPP begCollection value not 0 bytes."), 1);
3511 DEBUG_puts("1ippReadIO: begCollection tag with value length "
3512 "> 0.");
3513 _cupsBufferRelease((char *)buffer);
3514 return (IPP_STATE_ERROR);
3515 }
3516
3517 if (ippReadIO(src, cb, 1, ipp, value->collection) == IPP_STATE_ERROR)
3518 {
3519 DEBUG_puts("1ippReadIO: Unable to read collection value.");
3520 _cupsBufferRelease((char *)buffer);
3521 return (IPP_STATE_ERROR);
3522 }
3523 break;
3524
3525 case IPP_TAG_END_COLLECTION :
3526 _cupsBufferRelease((char *)buffer);
3527
3528 if (n > 0)
3529 {
3530 _cupsSetError(IPP_STATUS_ERROR_INTERNAL,
3531 _("IPP endCollection value not 0 bytes."), 1);
3532 DEBUG_puts("1ippReadIO: endCollection tag with value length "
3533 "> 0.");
3534 return (IPP_STATE_ERROR);
3535 }
3536
3537 DEBUG_puts("1ippReadIO: endCollection tag...");
3538 return (ipp->state = IPP_STATE_DATA);
3539
3540 case IPP_TAG_MEMBERNAME :
3541 /*
3542 * The value the name of the member in the collection, which
3543 * we need to carry over...
3544 */
3545
3546 if (!attr)
3547 {
3548 _cupsSetError(IPP_STATUS_ERROR_INTERNAL,
3549 _("IPP memberName with no attribute."), 1);
3550 DEBUG_puts("1ippReadIO: Member name without attribute.");
3551 _cupsBufferRelease((char *)buffer);
3552 return (IPP_STATE_ERROR);
3553 }
3554 else if (n == 0)
3555 {
3556 _cupsSetError(IPP_STATUS_ERROR_INTERNAL,
3557 _("IPP memberName value is empty."), 1);
3558 DEBUG_puts("1ippReadIO: Empty member name value.");
3559 _cupsBufferRelease((char *)buffer);
3560 return (IPP_STATE_ERROR);
3561 }
3562 else if ((*cb)(src, buffer, (size_t)n) < n)
3563 {
3564 DEBUG_puts("1ippReadIO: Unable to read member name value.");
3565 _cupsBufferRelease((char *)buffer);
3566 return (IPP_STATE_ERROR);
3567 }
3568
3569 buffer[n] = '\0';
3570 attr->name = _cupsStrAlloc((char *)buffer);
3571
3572 /*
3573 * Since collection members are encoded differently than
3574 * regular attributes, make sure we don't start with an
3575 * empty value...
3576 */
3577
3578 attr->num_values --;
3579
3580 DEBUG_printf(("2ippReadIO: member name=\"%s\"", attr->name));
3581 break;
3582
3583 case IPP_TAG_STRING :
3584 default : /* Other unsupported values */
3585 if (tag == IPP_TAG_STRING && n > IPP_MAX_LENGTH)
3586 {
3587 _cupsSetError(IPP_STATUS_ERROR_INTERNAL,
3588 _("IPP octetString length too large."), 1);
3589 DEBUG_printf(("1ippReadIO: bad octetString value length %d.",
3590 n));
3591 _cupsBufferRelease((char *)buffer);
3592 return (IPP_STATE_ERROR);
3593 }
3594
3595 value->unknown.length = n;
3596
3597 if (n > 0)
3598 {
3599 if ((value->unknown.data = malloc((size_t)n)) == NULL)
3600 {
3601 _cupsSetHTTPError(HTTP_STATUS_ERROR);
3602 DEBUG_puts("1ippReadIO: Unable to allocate value");
3603 _cupsBufferRelease((char *)buffer);
3604 return (IPP_STATE_ERROR);
3605 }
3606
3607 if ((*cb)(src, value->unknown.data, (size_t)n) < n)
3608 {
3609 DEBUG_puts("1ippReadIO: Unable to read unsupported value.");
3610 _cupsBufferRelease((char *)buffer);
3611 return (IPP_STATE_ERROR);
3612 }
3613 }
3614 else
3615 value->unknown.data = NULL;
3616 break;
3617 }
3618
3619 /*
3620 * If blocking is disabled, stop here...
3621 */
3622
3623 if (!blocking)
3624 break;
3625 }
3626 break;
3627
3628 case IPP_STATE_DATA :
3629 break;
3630
3631 default :
3632 break; /* anti-compiler-warning-code */
3633 }
3634
3635 DEBUG_printf(("1ippReadIO: returning ipp->state=%d.", ipp->state));
3636 _cupsBufferRelease((char *)buffer);
3637
3638 return (ipp->state);
3639 }
3640
3641
3642 /*
3643 * 'ippSetBoolean()' - Set a boolean value in an attribute.
3644 *
3645 * The @code ipp@ parameter refers to an IPP message previously created using
3646 * the @link ippNew@, @link ippNewRequest@, or @link ippNewResponse@ functions.
3647 *
3648 * The @code attr@ parameter may be modified as a result of setting the value.
3649 *
3650 * The @code element@ parameter specifies which value to set from 0 to
3651 * @code ippGetCount(attr)@.
3652 *
3653 * @since CUPS 1.6/macOS 10.8@
3654 */
3655
3656 int /* O - 1 on success, 0 on failure */
3657 ippSetBoolean(ipp_t *ipp, /* I - IPP message */
3658 ipp_attribute_t **attr, /* IO - IPP attribute */
3659 int element, /* I - Value number (0-based) */
3660 int boolvalue)/* I - Boolean value */
3661 {
3662 _ipp_value_t *value; /* Current value */
3663
3664
3665 /*
3666 * Range check input...
3667 */
3668
3669 if (!ipp || !attr || !*attr || (*attr)->value_tag != IPP_TAG_BOOLEAN ||
3670 element < 0 || element > (*attr)->num_values)
3671 return (0);
3672
3673 /*
3674 * Set the value and return...
3675 */
3676
3677 if ((value = ipp_set_value(ipp, attr, element)) != NULL)
3678 value->boolean = (char)boolvalue;
3679
3680 return (value != NULL);
3681 }
3682
3683
3684 /*
3685 * 'ippSetCollection()' - Set a collection value in an attribute.
3686 *
3687 * The @code ipp@ parameter refers to an IPP message previously created using
3688 * the @link ippNew@, @link ippNewRequest@, or @link ippNewResponse@ functions.
3689 *
3690 * The @code attr@ parameter may be modified as a result of setting the value.
3691 *
3692 * The @code element@ parameter specifies which value to set from 0 to
3693 * @code ippGetCount(attr)@.
3694 *
3695 * @since CUPS 1.6/macOS 10.8@
3696 */
3697
3698 int /* O - 1 on success, 0 on failure */
3699 ippSetCollection(
3700 ipp_t *ipp, /* I - IPP message */
3701 ipp_attribute_t **attr, /* IO - IPP attribute */
3702 int element, /* I - Value number (0-based) */
3703 ipp_t *colvalue) /* I - Collection value */
3704 {
3705 _ipp_value_t *value; /* Current value */
3706
3707
3708 /*
3709 * Range check input...
3710 */
3711
3712 if (!ipp || !attr || !*attr || (*attr)->value_tag != IPP_TAG_BEGIN_COLLECTION ||
3713 element < 0 || element > (*attr)->num_values || !colvalue)
3714 return (0);
3715
3716 /*
3717 * Set the value and return...
3718 */
3719
3720 if ((value = ipp_set_value(ipp, attr, element)) != NULL)
3721 {
3722 if (value->collection)
3723 ippDelete(value->collection);
3724
3725 value->collection = colvalue;
3726 colvalue->use ++;
3727 }
3728
3729 return (value != NULL);
3730 }
3731
3732
3733 /*
3734 * 'ippSetDate()' - Set a dateTime value in an attribute.
3735 *
3736 * The @code ipp@ parameter refers to an IPP message previously created using
3737 * the @link ippNew@, @link ippNewRequest@, or @link ippNewResponse@ functions.
3738 *
3739 * The @code attr@ parameter may be modified as a result of setting the value.
3740 *
3741 * The @code element@ parameter specifies which value to set from 0 to
3742 * @code ippGetCount(attr)@.
3743 *
3744 * @since CUPS 1.6/macOS 10.8@
3745 */
3746
3747 int /* O - 1 on success, 0 on failure */
3748 ippSetDate(ipp_t *ipp, /* I - IPP message */
3749 ipp_attribute_t **attr, /* IO - IPP attribute */
3750 int element, /* I - Value number (0-based) */
3751 const ipp_uchar_t *datevalue)/* I - dateTime value */
3752 {
3753 _ipp_value_t *value; /* Current value */
3754
3755
3756 /*
3757 * Range check input...
3758 */
3759
3760 if (!ipp || !attr || !*attr || ((*attr)->value_tag != IPP_TAG_DATE && (*attr)->value_tag != IPP_TAG_NOVALUE && (*attr)->value_tag != IPP_TAG_UNKNOWN) || element < 0 || element > (*attr)->num_values || !datevalue)
3761 return (0);
3762
3763 /*
3764 * Set the value and return...
3765 */
3766
3767 if ((value = ipp_set_value(ipp, attr, element)) != NULL)
3768 memcpy(value->date, datevalue, sizeof(value->date));
3769
3770 return (value != NULL);
3771 }
3772
3773
3774 /*
3775 * 'ippSetGroupTag()' - Set the group tag of an attribute.
3776 *
3777 * The @code ipp@ parameter refers to an IPP message previously created using
3778 * the @link ippNew@, @link ippNewRequest@, or @link ippNewResponse@ functions.
3779 *
3780 * The @code attr@ parameter may be modified as a result of setting the value.
3781 *
3782 * The @code group@ parameter specifies the IPP attribute group tag: none
3783 * (@code IPP_TAG_ZERO@, for member attributes), document (@code IPP_TAG_DOCUMENT@),
3784 * event notification (@code IPP_TAG_EVENT_NOTIFICATION@), operation
3785 * (@code IPP_TAG_OPERATION@), printer (@code IPP_TAG_PRINTER@), subscription
3786 * (@code IPP_TAG_SUBSCRIPTION@), or unsupported (@code IPP_TAG_UNSUPPORTED_GROUP@).
3787 *
3788 * @since CUPS 1.6/macOS 10.8@
3789 */
3790
3791 int /* O - 1 on success, 0 on failure */
3792 ippSetGroupTag(
3793 ipp_t *ipp, /* I - IPP message */
3794 ipp_attribute_t **attr, /* IO - Attribute */
3795 ipp_tag_t group_tag) /* I - Group tag */
3796 {
3797 /*
3798 * Range check input - group tag must be 0x01 to 0x0F, per RFC 8011...
3799 */
3800
3801 if (!ipp || !attr || !*attr ||
3802 group_tag < IPP_TAG_ZERO || group_tag == IPP_TAG_END ||
3803 group_tag >= IPP_TAG_UNSUPPORTED_VALUE)
3804 return (0);
3805
3806 /*
3807 * Set the group tag and return...
3808 */
3809
3810 (*attr)->group_tag = group_tag;
3811
3812 return (1);
3813 }
3814
3815
3816 /*
3817 * 'ippSetInteger()' - Set an integer or enum value in an attribute.
3818 *
3819 * The @code ipp@ parameter refers to an IPP message previously created using
3820 * the @link ippNew@, @link ippNewRequest@, or @link ippNewResponse@ functions.
3821 *
3822 * The @code attr@ parameter may be modified as a result of setting the value.
3823 *
3824 * The @code element@ parameter specifies which value to set from 0 to
3825 * @code ippGetCount(attr)@.
3826 *
3827 * @since CUPS 1.6/macOS 10.8@
3828 */
3829
3830 int /* O - 1 on success, 0 on failure */
3831 ippSetInteger(ipp_t *ipp, /* I - IPP message */
3832 ipp_attribute_t **attr, /* IO - IPP attribute */
3833 int element, /* I - Value number (0-based) */
3834 int intvalue) /* I - Integer/enum value */
3835 {
3836 _ipp_value_t *value; /* Current value */
3837
3838
3839 /*
3840 * Range check input...
3841 */
3842
3843 if (!ipp || !attr || !*attr || ((*attr)->value_tag != IPP_TAG_INTEGER && (*attr)->value_tag != IPP_TAG_ENUM && (*attr)->value_tag != IPP_TAG_NOVALUE && (*attr)->value_tag != IPP_TAG_UNKNOWN) || element < 0 || element > (*attr)->num_values)
3844 return (0);
3845
3846 /*
3847 * Set the value and return...
3848 */
3849
3850 if ((value = ipp_set_value(ipp, attr, element)) != NULL)
3851 {
3852 if ((*attr)->value_tag != IPP_TAG_ENUM)
3853 (*attr)->value_tag = IPP_TAG_INTEGER;
3854
3855 value->integer = intvalue;
3856 }
3857
3858 return (value != NULL);
3859 }
3860
3861
3862 /*
3863 * 'ippSetName()' - Set the name of an attribute.
3864 *
3865 * The @code ipp@ parameter refers to an IPP message previously created using
3866 * the @link ippNew@, @link ippNewRequest@, or @link ippNewResponse@ functions.
3867 *
3868 * The @code attr@ parameter may be modified as a result of setting the value.
3869 *
3870 * @since CUPS 1.6/macOS 10.8@
3871 */
3872
3873 int /* O - 1 on success, 0 on failure */
3874 ippSetName(ipp_t *ipp, /* I - IPP message */
3875 ipp_attribute_t **attr, /* IO - IPP attribute */
3876 const char *name) /* I - Attribute name */
3877 {
3878 char *temp; /* Temporary name value */
3879
3880
3881 /*
3882 * Range check input...
3883 */
3884
3885 if (!ipp || !attr || !*attr)
3886 return (0);
3887
3888 /*
3889 * Set the value and return...
3890 */
3891
3892 if ((temp = _cupsStrAlloc(name)) != NULL)
3893 {
3894 if ((*attr)->name)
3895 _cupsStrFree((*attr)->name);
3896
3897 (*attr)->name = temp;
3898 }
3899
3900 return (temp != NULL);
3901 }
3902
3903
3904 /*
3905 * 'ippSetOctetString()' - Set an octetString value in an IPP attribute.
3906 *
3907 * The @code ipp@ parameter refers to an IPP message previously created using
3908 * the @link ippNew@, @link ippNewRequest@, or @link ippNewResponse@ functions.
3909 *
3910 * The @code attr@ parameter may be modified as a result of setting the value.
3911 *
3912 * The @code element@ parameter specifies which value to set from 0 to
3913 * @code ippGetCount(attr)@.
3914 *
3915 * @since CUPS 1.7/macOS 10.9@
3916 */
3917
3918 int /* O - 1 on success, 0 on failure */
3919 ippSetOctetString(
3920 ipp_t *ipp, /* I - IPP message */
3921 ipp_attribute_t **attr, /* IO - IPP attribute */
3922 int element, /* I - Value number (0-based) */
3923 const void *data, /* I - Pointer to octetString data */
3924 int datalen) /* I - Length of octetString data */
3925 {
3926 _ipp_value_t *value; /* Current value */
3927
3928
3929 /*
3930 * Range check input...
3931 */
3932
3933 if (!ipp || !attr || !*attr || ((*attr)->value_tag != IPP_TAG_STRING && (*attr)->value_tag != IPP_TAG_NOVALUE && (*attr)->value_tag != IPP_TAG_UNKNOWN) || element < 0 || element > (*attr)->num_values || datalen < 0 || datalen > IPP_MAX_LENGTH)
3934 return (0);
3935
3936 /*
3937 * Set the value and return...
3938 */
3939
3940 if ((value = ipp_set_value(ipp, attr, element)) != NULL)
3941 {
3942 if ((int)((*attr)->value_tag) & IPP_TAG_CUPS_CONST)
3943 {
3944 /*
3945 * Just copy the pointer...
3946 */
3947
3948 value->unknown.data = (void *)data;
3949 value->unknown.length = datalen;
3950 }
3951 else
3952 {
3953 /*
3954 * Copy the data...
3955 */
3956
3957 (*attr)->value_tag = IPP_TAG_STRING;
3958
3959 if (value->unknown.data)
3960 {
3961 /*
3962 * Free previous data...
3963 */
3964
3965 free(value->unknown.data);
3966
3967 value->unknown.data = NULL;
3968 value->unknown.length = 0;
3969 }
3970
3971 if (datalen > 0)
3972 {
3973 void *temp; /* Temporary data pointer */
3974
3975 if ((temp = malloc((size_t)datalen)) != NULL)
3976 {
3977 memcpy(temp, data, (size_t)datalen);
3978
3979 value->unknown.data = temp;
3980 value->unknown.length = datalen;
3981 }
3982 else
3983 return (0);
3984 }
3985 }
3986 }
3987
3988 return (value != NULL);
3989 }
3990
3991
3992 /*
3993 * 'ippSetOperation()' - Set the operation ID in an IPP request message.
3994 *
3995 * The @code ipp@ parameter refers to an IPP message previously created using
3996 * the @link ippNew@, @link ippNewRequest@, or @link ippNewResponse@ functions.
3997 *
3998 * @since CUPS 1.6/macOS 10.8@
3999 */
4000
4001 int /* O - 1 on success, 0 on failure */
4002 ippSetOperation(ipp_t *ipp, /* I - IPP request message */
4003 ipp_op_t op) /* I - Operation ID */
4004 {
4005 /*
4006 * Range check input...
4007 */
4008
4009 if (!ipp)
4010 return (0);
4011
4012 /*
4013 * Set the operation and return...
4014 */
4015
4016 ipp->request.op.operation_id = op;
4017
4018 return (1);
4019 }
4020
4021
4022 /*
4023 * 'ippSetRange()' - Set a rangeOfInteger value in an attribute.
4024 *
4025 * The @code ipp@ parameter refers to an IPP message previously created using
4026 * the @link ippNew@, @link ippNewRequest@, or @link ippNewResponse@ functions.
4027 *
4028 * The @code attr@ parameter may be modified as a result of setting the value.
4029 *
4030 * The @code element@ parameter specifies which value to set from 0 to
4031 * @code ippGetCount(attr)@.
4032 *
4033 * @since CUPS 1.6/macOS 10.8@
4034 */
4035
4036 int /* O - 1 on success, 0 on failure */
4037 ippSetRange(ipp_t *ipp, /* I - IPP message */
4038 ipp_attribute_t **attr, /* IO - IPP attribute */
4039 int element, /* I - Value number (0-based) */
4040 int lowervalue, /* I - Lower bound for range */
4041 int uppervalue) /* I - Upper bound for range */
4042 {
4043 _ipp_value_t *value; /* Current value */
4044
4045
4046 /*
4047 * Range check input...
4048 */
4049
4050 if (!ipp || !attr || !*attr || ((*attr)->value_tag != IPP_TAG_RANGE && (*attr)->value_tag != IPP_TAG_NOVALUE && (*attr)->value_tag != IPP_TAG_UNKNOWN) || element < 0 || element > (*attr)->num_values || lowervalue > uppervalue)
4051 return (0);
4052
4053 /*
4054 * Set the value and return...
4055 */
4056
4057 if ((value = ipp_set_value(ipp, attr, element)) != NULL)
4058 {
4059 (*attr)->value_tag = IPP_TAG_RANGE;
4060 value->range.lower = lowervalue;
4061 value->range.upper = uppervalue;
4062 }
4063
4064 return (value != NULL);
4065 }
4066
4067
4068 /*
4069 * 'ippSetRequestId()' - Set the request ID in an IPP message.
4070 *
4071 * The @code ipp@ parameter refers to an IPP message previously created using
4072 * the @link ippNew@, @link ippNewRequest@, or @link ippNewResponse@ functions.
4073 *
4074 * The @code request_id@ parameter must be greater than 0.
4075 *
4076 * @since CUPS 1.6/macOS 10.8@
4077 */
4078
4079 int /* O - 1 on success, 0 on failure */
4080 ippSetRequestId(ipp_t *ipp, /* I - IPP message */
4081 int request_id) /* I - Request ID */
4082 {
4083 /*
4084 * Range check input; not checking request_id values since ipptool wants to send
4085 * invalid values for conformance testing and a bad request_id does not affect the
4086 * encoding of a message...
4087 */
4088
4089 if (!ipp)
4090 return (0);
4091
4092 /*
4093 * Set the request ID and return...
4094 */
4095
4096 ipp->request.any.request_id = request_id;
4097
4098 return (1);
4099 }
4100
4101
4102 /*
4103 * 'ippSetResolution()' - Set a resolution value in an attribute.
4104 *
4105 * The @code ipp@ parameter refers to an IPP message previously created using
4106 * the @link ippNew@, @link ippNewRequest@, or @link ippNewResponse@ functions.
4107 *
4108 * The @code attr@ parameter may be modified as a result of setting the value.
4109 *
4110 * The @code element@ parameter specifies which value to set from 0 to
4111 * @code ippGetCount(attr)@.
4112 *
4113 * @since CUPS 1.6/macOS 10.8@
4114 */
4115
4116 int /* O - 1 on success, 0 on failure */
4117 ippSetResolution(
4118 ipp_t *ipp, /* I - IPP message */
4119 ipp_attribute_t **attr, /* IO - IPP attribute */
4120 int element, /* I - Value number (0-based) */
4121 ipp_res_t unitsvalue, /* I - Resolution units */
4122 int xresvalue, /* I - Horizontal/cross feed resolution */
4123 int yresvalue) /* I - Vertical/feed resolution */
4124 {
4125 _ipp_value_t *value; /* Current value */
4126
4127
4128 /*
4129 * Range check input...
4130 */
4131
4132 if (!ipp || !attr || !*attr || ((*attr)->value_tag != IPP_TAG_RESOLUTION && (*attr)->value_tag != IPP_TAG_NOVALUE && (*attr)->value_tag != IPP_TAG_UNKNOWN) || element < 0 || element > (*attr)->num_values || xresvalue <= 0 || yresvalue <= 0 || unitsvalue < IPP_RES_PER_INCH || unitsvalue > IPP_RES_PER_CM)
4133 return (0);
4134
4135 /*
4136 * Set the value and return...
4137 */
4138
4139 if ((value = ipp_set_value(ipp, attr, element)) != NULL)
4140 {
4141 (*attr)->value_tag = IPP_TAG_RESOLUTION;
4142 value->resolution.units = unitsvalue;
4143 value->resolution.xres = xresvalue;
4144 value->resolution.yres = yresvalue;
4145 }
4146
4147 return (value != NULL);
4148 }
4149
4150
4151 /*
4152 * 'ippSetState()' - Set the current state of the IPP message.
4153 *
4154 * @since CUPS 1.6/macOS 10.8@
4155 */
4156
4157 int /* O - 1 on success, 0 on failure */
4158 ippSetState(ipp_t *ipp, /* I - IPP message */
4159 ipp_state_t state) /* I - IPP state value */
4160 {
4161 /*
4162 * Range check input...
4163 */
4164
4165 if (!ipp)
4166 return (0);
4167
4168 /*
4169 * Set the state and return...
4170 */
4171
4172 ipp->state = state;
4173 ipp->current = NULL;
4174
4175 return (1);
4176 }
4177
4178
4179 /*
4180 * 'ippSetStatusCode()' - Set the status code in an IPP response or event message.
4181 *
4182 * The @code ipp@ parameter refers to an IPP message previously created using
4183 * the @link ippNew@, @link ippNewRequest@, or @link ippNewResponse@ functions.
4184 *
4185 * @since CUPS 1.6/macOS 10.8@
4186 */
4187
4188 int /* O - 1 on success, 0 on failure */
4189 ippSetStatusCode(ipp_t *ipp, /* I - IPP response or event message */
4190 ipp_status_t status) /* I - Status code */
4191 {
4192 /*
4193 * Range check input...
4194 */
4195
4196 if (!ipp)
4197 return (0);
4198
4199 /*
4200 * Set the status code and return...
4201 */
4202
4203 ipp->request.status.status_code = status;
4204
4205 return (1);
4206 }
4207
4208
4209 /*
4210 * 'ippSetString()' - Set a string value in an attribute.
4211 *
4212 * The @code ipp@ parameter refers to an IPP message previously created using
4213 * the @link ippNew@, @link ippNewRequest@, or @link ippNewResponse@ functions.
4214 *
4215 * The @code attr@ parameter may be modified as a result of setting the value.
4216 *
4217 * The @code element@ parameter specifies which value to set from 0 to
4218 * @code ippGetCount(attr)@.
4219 *
4220 * @since CUPS 1.6/macOS 10.8@
4221 */
4222
4223 int /* O - 1 on success, 0 on failure */
4224 ippSetString(ipp_t *ipp, /* I - IPP message */
4225 ipp_attribute_t **attr, /* IO - IPP attribute */
4226 int element, /* I - Value number (0-based) */
4227 const char *strvalue) /* I - String value */
4228 {
4229 char *temp; /* Temporary string */
4230 _ipp_value_t *value; /* Current value */
4231 ipp_tag_t value_tag; /* Value tag */
4232
4233
4234 /*
4235 * Range check input...
4236 */
4237
4238 if (attr && *attr)
4239 value_tag = (*attr)->value_tag & IPP_TAG_CUPS_MASK;
4240 else
4241 value_tag = IPP_TAG_ZERO;
4242
4243 if (!ipp || !attr || !*attr || (value_tag < IPP_TAG_TEXT && value_tag != IPP_TAG_TEXTLANG && value_tag != IPP_TAG_NAMELANG && value_tag != IPP_TAG_NOVALUE && value_tag != IPP_TAG_UNKNOWN) || value_tag > IPP_TAG_MIMETYPE || element < 0 || element > (*attr)->num_values || !strvalue)
4244 return (0);
4245
4246 /*
4247 * Set the value and return...
4248 */
4249
4250 if ((value = ipp_set_value(ipp, attr, element)) != NULL)
4251 {
4252 if (value_tag == IPP_TAG_NOVALUE || value_tag == IPP_TAG_UNKNOWN)
4253 (*attr)->value_tag = IPP_TAG_KEYWORD;
4254
4255 if (element > 0)
4256 value->string.language = (*attr)->values[0].string.language;
4257
4258 if ((int)((*attr)->value_tag) & IPP_TAG_CUPS_CONST)
4259 value->string.text = (char *)strvalue;
4260 else if ((temp = _cupsStrAlloc(strvalue)) != NULL)
4261 {
4262 if (value->string.text)
4263 _cupsStrFree(value->string.text);
4264
4265 value->string.text = temp;
4266 }
4267 else
4268 return (0);
4269 }
4270
4271 return (value != NULL);
4272 }
4273
4274
4275 /*
4276 * 'ippSetStringf()' - Set a formatted string value of an attribute.
4277 *
4278 * The @code ipp@ parameter refers to an IPP message previously created using
4279 * the @link ippNew@, @link ippNewRequest@, or @link ippNewResponse@ functions.
4280 *
4281 * The @code attr@ parameter may be modified as a result of setting the value.
4282 *
4283 * The @code element@ parameter specifies which value to set from 0 to
4284 * @code ippGetCount(attr)@.
4285 *
4286 * The @code format@ parameter uses formatting characters compatible with the
4287 * printf family of standard functions. Additional arguments follow it as
4288 * needed. The formatted string is truncated as needed to the maximum length of
4289 * the corresponding value type.
4290 *
4291 * @since CUPS 1.7/macOS 10.9@
4292 */
4293
4294 int /* O - 1 on success, 0 on failure */
4295 ippSetStringf(ipp_t *ipp, /* I - IPP message */
4296 ipp_attribute_t **attr, /* IO - IPP attribute */
4297 int element, /* I - Value number (0-based) */
4298 const char *format, /* I - Printf-style format string */
4299 ...) /* I - Additional arguments as needed */
4300 {
4301 int ret; /* Return value */
4302 va_list ap; /* Pointer to additional arguments */
4303
4304
4305 va_start(ap, format);
4306 ret = ippSetStringfv(ipp, attr, element, format, ap);
4307 va_end(ap);
4308
4309 return (ret);
4310 }
4311
4312
4313 /*
4314 * 'ippSetStringf()' - Set a formatted string value of an attribute.
4315 *
4316 * The @code ipp@ parameter refers to an IPP message previously created using
4317 * the @link ippNew@, @link ippNewRequest@, or @link ippNewResponse@ functions.
4318 *
4319 * The @code attr@ parameter may be modified as a result of setting the value.
4320 *
4321 * The @code element@ parameter specifies which value to set from 0 to
4322 * @code ippGetCount(attr)@.
4323 *
4324 * The @code format@ parameter uses formatting characters compatible with the
4325 * printf family of standard functions. Additional arguments follow it as
4326 * needed. The formatted string is truncated as needed to the maximum length of
4327 * the corresponding value type.
4328 *
4329 * @since CUPS 1.7/macOS 10.9@
4330 */
4331
4332 int /* O - 1 on success, 0 on failure */
4333 ippSetStringfv(ipp_t *ipp, /* I - IPP message */
4334 ipp_attribute_t **attr, /* IO - IPP attribute */
4335 int element, /* I - Value number (0-based) */
4336 const char *format, /* I - Printf-style format string */
4337 va_list ap) /* I - Pointer to additional arguments */
4338 {
4339 ipp_tag_t value_tag; /* Value tag */
4340 char buffer[IPP_MAX_TEXT + 4];
4341 /* Formatted text string */
4342 ssize_t bytes, /* Length of formatted value */
4343 max_bytes; /* Maximum number of bytes for value */
4344
4345
4346 /*
4347 * Range check input...
4348 */
4349
4350 if (attr && *attr)
4351 value_tag = (*attr)->value_tag & IPP_TAG_CUPS_MASK;
4352 else
4353 value_tag = IPP_TAG_ZERO;
4354
4355 if (!ipp || !attr || !*attr || (value_tag < IPP_TAG_TEXT && value_tag != IPP_TAG_TEXTLANG && value_tag != IPP_TAG_NAMELANG && value_tag != IPP_TAG_NOVALUE && value_tag != IPP_TAG_UNKNOWN) || value_tag > IPP_TAG_MIMETYPE || !format)
4356 return (0);
4357
4358 /*
4359 * Format the string...
4360 */
4361
4362 if (!strcmp(format, "%s"))
4363 {
4364 /*
4365 * Optimize the simple case...
4366 */
4367
4368 const char *s = va_arg(ap, char *);
4369
4370 if (!s)
4371 s = "(null)";
4372
4373 bytes = (ssize_t)strlen(s);
4374 strlcpy(buffer, s, sizeof(buffer));
4375 }
4376 else
4377 {
4378 /*
4379 * Do a full formatting of the message...
4380 */
4381
4382 if ((bytes = vsnprintf(buffer, sizeof(buffer), format, ap)) < 0)
4383 return (0);
4384 }
4385
4386 /*
4387 * Limit the length of the string...
4388 */
4389
4390 switch (value_tag)
4391 {
4392 default :
4393 case IPP_TAG_TEXT :
4394 case IPP_TAG_TEXTLANG :
4395 max_bytes = IPP_MAX_TEXT;
4396 break;
4397
4398 case IPP_TAG_NAME :
4399 case IPP_TAG_NAMELANG :
4400 max_bytes = IPP_MAX_NAME;
4401 break;
4402
4403 case IPP_TAG_CHARSET :
4404 max_bytes = IPP_MAX_CHARSET;
4405 break;
4406
4407 case IPP_TAG_NOVALUE :
4408 case IPP_TAG_UNKNOWN :
4409 case IPP_TAG_KEYWORD :
4410 max_bytes = IPP_MAX_KEYWORD;
4411 break;
4412
4413 case IPP_TAG_LANGUAGE :
4414 max_bytes = IPP_MAX_LANGUAGE;
4415 break;
4416
4417 case IPP_TAG_MIMETYPE :
4418 max_bytes = IPP_MAX_MIMETYPE;
4419 break;
4420
4421 case IPP_TAG_URI :
4422 max_bytes = IPP_MAX_URI;
4423 break;
4424
4425 case IPP_TAG_URISCHEME :
4426 max_bytes = IPP_MAX_URISCHEME;
4427 break;
4428 }
4429
4430 if (bytes >= max_bytes)
4431 {
4432 char *bufmax, /* Buffer at max_bytes */
4433 *bufptr; /* Pointer into buffer */
4434
4435 bufptr = buffer + strlen(buffer) - 1;
4436 bufmax = buffer + max_bytes - 1;
4437
4438 while (bufptr > bufmax)
4439 {
4440 if (*bufptr & 0x80)
4441 {
4442 while ((*bufptr & 0xc0) == 0x80 && bufptr > buffer)
4443 bufptr --;
4444 }
4445
4446 bufptr --;
4447 }
4448
4449 *bufptr = '\0';
4450 }
4451
4452 /*
4453 * Set the formatted string and return...
4454 */
4455
4456 return (ippSetString(ipp, attr, element, buffer));
4457 }
4458
4459
4460 /*
4461 * 'ippSetValueTag()' - Set the value tag of an attribute.
4462 *
4463 * The @code ipp@ parameter refers to an IPP message previously created using
4464 * the @link ippNew@, @link ippNewRequest@, or @link ippNewResponse@ functions.
4465 *
4466 * The @code attr@ parameter may be modified as a result of setting the value.
4467 *
4468 * Integer (@code IPP_TAG_INTEGER@) values can be promoted to rangeOfInteger
4469 * (@code IPP_TAG_RANGE@) values, the various string tags can be promoted to name
4470 * (@code IPP_TAG_NAME@) or nameWithLanguage (@code IPP_TAG_NAMELANG@) values, text
4471 * (@code IPP_TAG_TEXT@) values can be promoted to textWithLanguage
4472 * (@code IPP_TAG_TEXTLANG@) values, and all values can be demoted to the various
4473 * out-of-band value tags such as no-value (@code IPP_TAG_NOVALUE@). All other changes
4474 * will be rejected.
4475 *
4476 * Promoting a string attribute to nameWithLanguage or textWithLanguage adds the language
4477 * code in the "attributes-natural-language" attribute or, if not present, the language
4478 * code for the current locale.
4479 *
4480 * @since CUPS 1.6/macOS 10.8@
4481 */
4482
4483 int /* O - 1 on success, 0 on failure */
4484 ippSetValueTag(
4485 ipp_t *ipp, /* I - IPP message */
4486 ipp_attribute_t **attr, /* IO - IPP attribute */
4487 ipp_tag_t value_tag) /* I - Value tag */
4488 {
4489 int i; /* Looping var */
4490 _ipp_value_t *value; /* Current value */
4491 int integer; /* Current integer value */
4492 cups_lang_t *language; /* Current language */
4493 char code[32]; /* Language code */
4494 ipp_tag_t temp_tag; /* Temporary value tag */
4495
4496
4497 /*
4498 * Range check input...
4499 */
4500
4501 if (!ipp || !attr || !*attr)
4502 return (0);
4503
4504 /*
4505 * If there is no change, return immediately...
4506 */
4507
4508 if (value_tag == (*attr)->value_tag)
4509 return (1);
4510
4511 /*
4512 * Otherwise implement changes as needed...
4513 */
4514
4515 temp_tag = (ipp_tag_t)((int)((*attr)->value_tag) & IPP_TAG_CUPS_MASK);
4516
4517 switch (value_tag)
4518 {
4519 case IPP_TAG_UNSUPPORTED_VALUE :
4520 case IPP_TAG_DEFAULT :
4521 case IPP_TAG_UNKNOWN :
4522 case IPP_TAG_NOVALUE :
4523 case IPP_TAG_NOTSETTABLE :
4524 case IPP_TAG_DELETEATTR :
4525 case IPP_TAG_ADMINDEFINE :
4526 /*
4527 * Free any existing values...
4528 */
4529
4530 if ((*attr)->num_values > 0)
4531 ipp_free_values(*attr, 0, (*attr)->num_values);
4532
4533 /*
4534 * Set out-of-band value...
4535 */
4536
4537 (*attr)->value_tag = value_tag;
4538 break;
4539
4540 case IPP_TAG_RANGE :
4541 if (temp_tag != IPP_TAG_INTEGER)
4542 return (0);
4543
4544 for (i = (*attr)->num_values, value = (*attr)->values;
4545 i > 0;
4546 i --, value ++)
4547 {
4548 integer = value->integer;
4549 value->range.lower = value->range.upper = integer;
4550 }
4551
4552 (*attr)->value_tag = IPP_TAG_RANGE;
4553 break;
4554
4555 case IPP_TAG_NAME :
4556 if (temp_tag != IPP_TAG_KEYWORD)
4557 return (0);
4558
4559 (*attr)->value_tag = (ipp_tag_t)(IPP_TAG_NAME | ((*attr)->value_tag & IPP_TAG_CUPS_CONST));
4560 break;
4561
4562 case IPP_TAG_NAMELANG :
4563 case IPP_TAG_TEXTLANG :
4564 if (value_tag == IPP_TAG_NAMELANG && (temp_tag != IPP_TAG_NAME && temp_tag != IPP_TAG_KEYWORD))
4565 return (0);
4566
4567 if (value_tag == IPP_TAG_TEXTLANG && temp_tag != IPP_TAG_TEXT)
4568 return (0);
4569
4570 if (ipp->attrs && ipp->attrs->next && ipp->attrs->next->name &&
4571 !strcmp(ipp->attrs->next->name, "attributes-natural-language") && (ipp->attrs->next->value_tag & IPP_TAG_CUPS_MASK) == IPP_TAG_LANGUAGE)
4572 {
4573 /*
4574 * Use the language code from the IPP message...
4575 */
4576
4577 (*attr)->values[0].string.language =
4578 _cupsStrAlloc(ipp->attrs->next->values[0].string.text);
4579 }
4580 else
4581 {
4582 /*
4583 * Otherwise, use the language code corresponding to the locale...
4584 */
4585
4586 language = cupsLangDefault();
4587 (*attr)->values[0].string.language = _cupsStrAlloc(ipp_lang_code(language->language,
4588 code,
4589 sizeof(code)));
4590 }
4591
4592 for (i = (*attr)->num_values - 1, value = (*attr)->values + 1;
4593 i > 0;
4594 i --, value ++)
4595 value->string.language = (*attr)->values[0].string.language;
4596
4597 if ((int)(*attr)->value_tag & IPP_TAG_CUPS_CONST)
4598 {
4599 /*
4600 * Make copies of all values...
4601 */
4602
4603 for (i = (*attr)->num_values, value = (*attr)->values;
4604 i > 0;
4605 i --, value ++)
4606 value->string.text = _cupsStrAlloc(value->string.text);
4607 }
4608
4609 (*attr)->value_tag = IPP_TAG_NAMELANG;
4610 break;
4611
4612 case IPP_TAG_KEYWORD :
4613 if (temp_tag == IPP_TAG_NAME || temp_tag == IPP_TAG_NAMELANG)
4614 break; /* Silently "allow" name -> keyword */
4615
4616 default :
4617 return (0);
4618 }
4619
4620 return (1);
4621 }
4622
4623
4624 /*
4625 * 'ippSetVersion()' - Set the version number in an IPP message.
4626 *
4627 * The @code ipp@ parameter refers to an IPP message previously created using
4628 * the @link ippNew@, @link ippNewRequest@, or @link ippNewResponse@ functions.
4629 *
4630 * The valid version numbers are currently 1.0, 1.1, 2.0, 2.1, and 2.2.
4631 *
4632 * @since CUPS 1.6/macOS 10.8@
4633 */
4634
4635 int /* O - 1 on success, 0 on failure */
4636 ippSetVersion(ipp_t *ipp, /* I - IPP message */
4637 int major, /* I - Major version number (major.minor) */
4638 int minor) /* I - Minor version number (major.minor) */
4639 {
4640 /*
4641 * Range check input...
4642 */
4643
4644 if (!ipp || major < 0 || minor < 0)
4645 return (0);
4646
4647 /*
4648 * Set the version number...
4649 */
4650
4651 ipp->request.any.version[0] = (ipp_uchar_t)major;
4652 ipp->request.any.version[1] = (ipp_uchar_t)minor;
4653
4654 return (1);
4655 }
4656
4657
4658 /*
4659 * 'ippTimeToDate()' - Convert from time in seconds to RFC 2579 format.
4660 */
4661
4662 const ipp_uchar_t * /* O - RFC-2579 date/time data */
4663 ippTimeToDate(time_t t) /* I - Time in seconds */
4664 {
4665 struct tm unixdate; /* UNIX unixdate/time info */
4666 ipp_uchar_t *date = _cupsGlobals()->ipp_date;
4667 /* RFC-2579 date/time data */
4668
4669
4670 /*
4671 * RFC-2579 date/time format is:
4672 *
4673 * Byte(s) Description
4674 * ------- -----------
4675 * 0-1 Year (0 to 65535)
4676 * 2 Month (1 to 12)
4677 * 3 Day (1 to 31)
4678 * 4 Hours (0 to 23)
4679 * 5 Minutes (0 to 59)
4680 * 6 Seconds (0 to 60, 60 = "leap second")
4681 * 7 Deciseconds (0 to 9)
4682 * 8 +/- UTC
4683 * 9 UTC hours (0 to 11)
4684 * 10 UTC minutes (0 to 59)
4685 */
4686
4687 gmtime_r(&t, &unixdate);
4688 unixdate.tm_year += 1900;
4689
4690 date[0] = (ipp_uchar_t)(unixdate.tm_year >> 8);
4691 date[1] = (ipp_uchar_t)(unixdate.tm_year);
4692 date[2] = (ipp_uchar_t)(unixdate.tm_mon + 1);
4693 date[3] = (ipp_uchar_t)unixdate.tm_mday;
4694 date[4] = (ipp_uchar_t)unixdate.tm_hour;
4695 date[5] = (ipp_uchar_t)unixdate.tm_min;
4696 date[6] = (ipp_uchar_t)unixdate.tm_sec;
4697 date[7] = 0;
4698 date[8] = '+';
4699 date[9] = 0;
4700 date[10] = 0;
4701
4702 return (date);
4703 }
4704
4705
4706 /*
4707 * 'ippValidateAttribute()' - Validate the contents of an attribute.
4708 *
4709 * This function validates the contents of an attribute based on the name and
4710 * value tag. 1 is returned if the attribute is valid, 0 otherwise. On
4711 * failure, @link cupsLastErrorString@ is set to a human-readable message.
4712 *
4713 * @since CUPS 1.7/macOS 10.9@
4714 */
4715
4716 int /* O - 1 if valid, 0 otherwise */
4717 ippValidateAttribute(
4718 ipp_attribute_t *attr) /* I - Attribute */
4719 {
4720 int i; /* Looping var */
4721 char scheme[64], /* Scheme from URI */
4722 userpass[256], /* Username/password from URI */
4723 hostname[256], /* Hostname from URI */
4724 resource[1024]; /* Resource from URI */
4725 int port, /* Port number from URI */
4726 uri_status; /* URI separation status */
4727 const char *ptr; /* Pointer into string */
4728 ipp_attribute_t *colattr; /* Collection attribute */
4729 regex_t re; /* Regular expression */
4730 ipp_uchar_t *date; /* Current date value */
4731
4732
4733 /*
4734 * Skip separators.
4735 */
4736
4737 if (!attr->name)
4738 return (1);
4739
4740 /*
4741 * Validate the attribute name.
4742 */
4743
4744 for (ptr = attr->name; *ptr; ptr ++)
4745 if (!isalnum(*ptr & 255) && *ptr != '-' && *ptr != '.' && *ptr != '_')
4746 break;
4747
4748 if (*ptr || ptr == attr->name)
4749 {
4750 ipp_set_error(IPP_STATUS_ERROR_BAD_REQUEST, _("\"%s\": Bad attribute name - invalid character (RFC 8011 section 5.1.4)."), attr->name);
4751 return (0);
4752 }
4753
4754 if ((ptr - attr->name) > 255)
4755 {
4756 ipp_set_error(IPP_STATUS_ERROR_BAD_REQUEST, _("\"%s\": Bad attribute name - bad length %d (RFC 8011 section 5.1.4)."), attr->name, (int)(ptr - attr->name));
4757 return (0);
4758 }
4759
4760 switch (attr->value_tag)
4761 {
4762 case IPP_TAG_INTEGER :
4763 break;
4764
4765 case IPP_TAG_BOOLEAN :
4766 for (i = 0; i < attr->num_values; i ++)
4767 {
4768 if (attr->values[i].boolean != 0 &&
4769 attr->values[i].boolean != 1)
4770 {
4771 ipp_set_error(IPP_STATUS_ERROR_BAD_REQUEST, _("\"%s\": Bad boolean value %d (RFC 8011 section 5.1.21)."), attr->name, attr->values[i].boolean);
4772 return (0);
4773 }
4774 }
4775 break;
4776
4777 case IPP_TAG_ENUM :
4778 for (i = 0; i < attr->num_values; i ++)
4779 {
4780 if (attr->values[i].integer < 1)
4781 {
4782 ipp_set_error(IPP_STATUS_ERROR_BAD_REQUEST, _("\"%s\": Bad enum value %d - out of range (RFC 8011 section 5.1.5)."), attr->name, attr->values[i].integer);
4783 return (0);
4784 }
4785 }
4786 break;
4787
4788 case IPP_TAG_STRING :
4789 for (i = 0; i < attr->num_values; i ++)
4790 {
4791 if (attr->values[i].unknown.length > IPP_MAX_OCTETSTRING)
4792 {
4793 ipp_set_error(IPP_STATUS_ERROR_BAD_REQUEST, _("\"%s\": Bad octetString value - bad length %d (RFC 8011 section 5.1.20)."), attr->name, attr->values[i].unknown.length);
4794 return (0);
4795 }
4796 }
4797 break;
4798
4799 case IPP_TAG_DATE :
4800 for (i = 0; i < attr->num_values; i ++)
4801 {
4802 date = attr->values[i].date;
4803
4804 if (date[2] < 1 || date[2] > 12)
4805 {
4806 ipp_set_error(IPP_STATUS_ERROR_BAD_REQUEST, _("\"%s\": Bad dateTime month %u (RFC 8011 section 5.1.15)."), attr->name, date[2]);
4807 return (0);
4808 }
4809
4810 if (date[3] < 1 || date[3] > 31)
4811 {
4812 ipp_set_error(IPP_STATUS_ERROR_BAD_REQUEST, _("\"%s\": Bad dateTime day %u (RFC 8011 section 5.1.15)."), attr->name, date[3]);
4813 return (0);
4814 }
4815
4816 if (date[4] > 23)
4817 {
4818 ipp_set_error(IPP_STATUS_ERROR_BAD_REQUEST, _("\"%s\": Bad dateTime hours %u (RFC 8011 section 5.1.15)."), attr->name, date[4]);
4819 return (0);
4820 }
4821
4822 if (date[5] > 59)
4823 {
4824 ipp_set_error(IPP_STATUS_ERROR_BAD_REQUEST, _("\"%s\": Bad dateTime minutes %u (RFC 8011 section 5.1.15)."), attr->name, date[5]);
4825 return (0);
4826 }
4827
4828 if (date[6] > 60)
4829 {
4830 ipp_set_error(IPP_STATUS_ERROR_BAD_REQUEST, _("\"%s\": Bad dateTime seconds %u (RFC 8011 section 5.1.15)."), attr->name, date[6]);
4831 return (0);
4832 }
4833
4834 if (date[7] > 9)
4835 {
4836 ipp_set_error(IPP_STATUS_ERROR_BAD_REQUEST, _("\"%s\": Bad dateTime deciseconds %u (RFC 8011 section 5.1.15)."), attr->name, date[7]);
4837 return (0);
4838 }
4839
4840 if (date[8] != '-' && date[8] != '+')
4841 {
4842 ipp_set_error(IPP_STATUS_ERROR_BAD_REQUEST, _("\"%s\": Bad dateTime UTC sign '%c' (RFC 8011 section 5.1.15)."), attr->name, date[8]);
4843 return (0);
4844 }
4845
4846 if (date[9] > 11)
4847 {
4848 ipp_set_error(IPP_STATUS_ERROR_BAD_REQUEST, _("\"%s\": Bad dateTime UTC hours %u (RFC 8011 section 5.1.15)."), attr->name, date[9]);
4849 return (0);
4850 }
4851
4852 if (date[10] > 59)
4853 {
4854 ipp_set_error(IPP_STATUS_ERROR_BAD_REQUEST, _("\"%s\": Bad dateTime UTC minutes %u (RFC 8011 section 5.1.15)."), attr->name, date[10]);
4855 return (0);
4856 }
4857 }
4858 break;
4859
4860 case IPP_TAG_RESOLUTION :
4861 for (i = 0; i < attr->num_values; i ++)
4862 {
4863 if (attr->values[i].resolution.xres <= 0)
4864 {
4865 ipp_set_error(IPP_STATUS_ERROR_BAD_REQUEST, _("\"%s\": Bad resolution value %dx%d%s - cross feed resolution must be positive (RFC 8011 section 5.1.16)."), attr->name, attr->values[i].resolution.xres, attr->values[i].resolution.yres, attr->values[i].resolution.units == IPP_RES_PER_INCH ? "dpi" : attr->values[i].resolution.units == IPP_RES_PER_CM ? "dpcm" : "unknown");
4866 return (0);
4867 }
4868
4869 if (attr->values[i].resolution.yres <= 0)
4870 {
4871 ipp_set_error(IPP_STATUS_ERROR_BAD_REQUEST, _("\"%s\": Bad resolution value %dx%d%s - feed resolution must be positive (RFC 8011 section 5.1.16)."), attr->name, attr->values[i].resolution.xres, attr->values[i].resolution.yres, attr->values[i].resolution.units == IPP_RES_PER_INCH ? "dpi" : attr->values[i].resolution.units == IPP_RES_PER_CM ? "dpcm" : "unknown");
4872 return (0);
4873 }
4874
4875 if (attr->values[i].resolution.units != IPP_RES_PER_INCH && attr->values[i].resolution.units != IPP_RES_PER_CM)
4876 {
4877 ipp_set_error(IPP_STATUS_ERROR_BAD_REQUEST, _("\"%s\": Bad resolution value %dx%d%s - bad units value (RFC 8011 section 5.1.16)."), attr->name, attr->values[i].resolution.xres, attr->values[i].resolution.yres, attr->values[i].resolution.units == IPP_RES_PER_INCH ? "dpi" : attr->values[i].resolution.units == IPP_RES_PER_CM ? "dpcm" : "unknown");
4878 return (0);
4879 }
4880 }
4881 break;
4882
4883 case IPP_TAG_RANGE :
4884 for (i = 0; i < attr->num_values; i ++)
4885 {
4886 if (attr->values[i].range.lower > attr->values[i].range.upper)
4887 {
4888 ipp_set_error(IPP_STATUS_ERROR_BAD_REQUEST, _("\"%s\": Bad rangeOfInteger value %d-%d - lower greater than upper (RFC 8011 section 5.1.14)."), attr->name, attr->values[i].range.lower, attr->values[i].range.upper);
4889 return (0);
4890 }
4891 }
4892 break;
4893
4894 case IPP_TAG_BEGIN_COLLECTION :
4895 for (i = 0; i < attr->num_values; i ++)
4896 {
4897 for (colattr = attr->values[i].collection->attrs;
4898 colattr;
4899 colattr = colattr->next)
4900 {
4901 if (!ippValidateAttribute(colattr))
4902 return (0);
4903 }
4904 }
4905 break;
4906
4907 case IPP_TAG_TEXT :
4908 case IPP_TAG_TEXTLANG :
4909 for (i = 0; i < attr->num_values; i ++)
4910 {
4911 for (ptr = attr->values[i].string.text; *ptr; ptr ++)
4912 {
4913 if ((*ptr & 0xe0) == 0xc0)
4914 {
4915 if ((ptr[1] & 0xc0) != 0x80)
4916 break;
4917
4918 ptr ++;
4919 }
4920 else if ((*ptr & 0xf0) == 0xe0)
4921 {
4922 if ((ptr[1] & 0xc0) != 0x80 || (ptr[2] & 0xc0) != 0x80)
4923 break;
4924
4925 ptr += 2;
4926 }
4927 else if ((*ptr & 0xf8) == 0xf0)
4928 {
4929 if ((ptr[1] & 0xc0) != 0x80 || (ptr[2] & 0xc0) != 0x80 || (ptr[3] & 0xc0) != 0x80)
4930 break;
4931
4932 ptr += 3;
4933 }
4934 else if (*ptr & 0x80)
4935 break;
4936 else if ((*ptr < ' ' && *ptr != '\n' && *ptr != '\r' && *ptr != '\t') || *ptr == 0x7f)
4937 break;
4938 }
4939
4940 if (*ptr)
4941 {
4942 if (*ptr < ' ' || *ptr == 0x7f)
4943 {
4944 ipp_set_error(IPP_STATUS_ERROR_BAD_REQUEST, _("\"%s\": Bad text value \"%s\" - bad control character (PWG 5100.14 section 8.3)."), attr->name, attr->values[i].string.text);
4945 return (0);
4946 }
4947 else
4948 {
4949 ipp_set_error(IPP_STATUS_ERROR_BAD_REQUEST, _("\"%s\": Bad text value \"%s\" - bad UTF-8 sequence (RFC 8011 section 5.1.2)."), attr->name, attr->values[i].string.text);
4950 return (0);
4951 }
4952 }
4953
4954 if ((ptr - attr->values[i].string.text) > (IPP_MAX_TEXT - 1))
4955 {
4956 ipp_set_error(IPP_STATUS_ERROR_BAD_REQUEST, _("\"%s\": Bad text value \"%s\" - bad length %d (RFC 8011 section 5.1.2)."), attr->name, attr->values[i].string.text, (int)(ptr - attr->values[i].string.text));
4957 return (0);
4958 }
4959 }
4960 break;
4961
4962 case IPP_TAG_NAME :
4963 case IPP_TAG_NAMELANG :
4964 for (i = 0; i < attr->num_values; i ++)
4965 {
4966 for (ptr = attr->values[i].string.text; *ptr; ptr ++)
4967 {
4968 if ((*ptr & 0xe0) == 0xc0)
4969 {
4970 if ((ptr[1] & 0xc0) != 0x80)
4971 break;
4972
4973 ptr ++;
4974 }
4975 else if ((*ptr & 0xf0) == 0xe0)
4976 {
4977 if ((ptr[1] & 0xc0) != 0x80 || (ptr[2] & 0xc0) != 0x80)
4978 break;
4979
4980 ptr += 2;
4981 }
4982 else if ((*ptr & 0xf8) == 0xf0)
4983 {
4984 if ((ptr[1] & 0xc0) != 0x80 || (ptr[2] & 0xc0) != 0x80 || (ptr[3] & 0xc0) != 0x80)
4985 break;
4986
4987 ptr += 3;
4988 }
4989 else if (*ptr & 0x80)
4990 break;
4991 else if (*ptr < ' ' || *ptr == 0x7f)
4992 break;
4993 }
4994
4995 if (*ptr)
4996 {
4997 if (*ptr < ' ' || *ptr == 0x7f)
4998 {
4999 ipp_set_error(IPP_STATUS_ERROR_BAD_REQUEST, _("\"%s\": Bad name value \"%s\" - bad control character (PWG 5100.14 section 8.1)."), attr->name, attr->values[i].string.text);
5000 return (0);
5001 }
5002 else
5003 {
5004 ipp_set_error(IPP_STATUS_ERROR_BAD_REQUEST, _("\"%s\": Bad name value \"%s\" - bad UTF-8 sequence (RFC 8011 section 5.1.3)."), attr->name, attr->values[i].string.text);
5005 return (0);
5006 }
5007 }
5008
5009 if ((ptr - attr->values[i].string.text) > (IPP_MAX_NAME - 1))
5010 {
5011 ipp_set_error(IPP_STATUS_ERROR_BAD_REQUEST, _("\"%s\": Bad name value \"%s\" - bad length %d (RFC 8011 section 5.1.3)."), attr->name, attr->values[i].string.text, (int)(ptr - attr->values[i].string.text));
5012 return (0);
5013 }
5014 }
5015 break;
5016
5017 case IPP_TAG_KEYWORD :
5018 for (i = 0; i < attr->num_values; i ++)
5019 {
5020 for (ptr = attr->values[i].string.text; *ptr; ptr ++)
5021 {
5022 if (!isalnum(*ptr & 255) && *ptr != '-' && *ptr != '.' &&
5023 *ptr != '_')
5024 break;
5025 }
5026
5027 if (*ptr || ptr == attr->values[i].string.text)
5028 {
5029 ipp_set_error(IPP_STATUS_ERROR_BAD_REQUEST, _("\"%s\": Bad keyword value \"%s\" - invalid character (RFC 8011 section 5.1.4)."), attr->name, attr->values[i].string.text);
5030 return (0);
5031 }
5032
5033 if ((ptr - attr->values[i].string.text) > (IPP_MAX_KEYWORD - 1))
5034 {
5035 ipp_set_error(IPP_STATUS_ERROR_BAD_REQUEST, _("\"%s\": Bad keyword value \"%s\" - bad length %d (RFC 8011 section 5.1.4)."), attr->name, attr->values[i].string.text, (int)(ptr - attr->values[i].string.text));
5036 return (0);
5037 }
5038 }
5039 break;
5040
5041 case IPP_TAG_URI :
5042 for (i = 0; i < attr->num_values; i ++)
5043 {
5044 uri_status = httpSeparateURI(HTTP_URI_CODING_ALL, attr->values[i].string.text, scheme, sizeof(scheme), userpass, sizeof(userpass), hostname, sizeof(hostname), &port, resource, sizeof(resource));
5045
5046 if (uri_status < HTTP_URI_STATUS_OK)
5047 {
5048 ipp_set_error(IPP_STATUS_ERROR_BAD_REQUEST, _("\"%s\": Bad URI value \"%s\" - %s (RFC 8011 section 5.1.6)."), attr->name, attr->values[i].string.text, httpURIStatusString(uri_status));
5049 return (0);
5050 }
5051
5052 if (strlen(attr->values[i].string.text) > (IPP_MAX_URI - 1))
5053 {
5054 ipp_set_error(IPP_STATUS_ERROR_BAD_REQUEST, _("\"%s\": Bad URI value \"%s\" - bad length %d (RFC 8011 section 5.1.6)."), attr->name, attr->values[i].string.text, (int)strlen(attr->values[i].string.text));
5055 }
5056 }
5057 break;
5058
5059 case IPP_TAG_URISCHEME :
5060 for (i = 0; i < attr->num_values; i ++)
5061 {
5062 ptr = attr->values[i].string.text;
5063 if (islower(*ptr & 255))
5064 {
5065 for (ptr ++; *ptr; ptr ++)
5066 {
5067 if (!islower(*ptr & 255) && !isdigit(*ptr & 255) &&
5068 *ptr != '+' && *ptr != '-' && *ptr != '.')
5069 break;
5070 }
5071 }
5072
5073 if (*ptr || ptr == attr->values[i].string.text)
5074 {
5075 ipp_set_error(IPP_STATUS_ERROR_BAD_REQUEST, _("\"%s\": Bad uriScheme value \"%s\" - bad characters (RFC 8011 section 5.1.7)."), attr->name, attr->values[i].string.text);
5076 return (0);
5077 }
5078
5079 if ((ptr - attr->values[i].string.text) > (IPP_MAX_URISCHEME - 1))
5080 {
5081 ipp_set_error(IPP_STATUS_ERROR_BAD_REQUEST, _("\"%s\": Bad uriScheme value \"%s\" - bad length %d (RFC 8011 section 5.1.7)."), attr->name, attr->values[i].string.text, (int)(ptr - attr->values[i].string.text));
5082 return (0);
5083 }
5084 }
5085 break;
5086
5087 case IPP_TAG_CHARSET :
5088 for (i = 0; i < attr->num_values; i ++)
5089 {
5090 for (ptr = attr->values[i].string.text; *ptr; ptr ++)
5091 {
5092 if (!isprint(*ptr & 255) || isupper(*ptr & 255) ||
5093 isspace(*ptr & 255))
5094 break;
5095 }
5096
5097 if (*ptr || ptr == attr->values[i].string.text)
5098 {
5099 ipp_set_error(IPP_STATUS_ERROR_BAD_REQUEST, _("\"%s\": Bad charset value \"%s\" - bad characters (RFC 8011 section 5.1.8)."), attr->name, attr->values[i].string.text);
5100 return (0);
5101 }
5102
5103 if ((ptr - attr->values[i].string.text) > (IPP_MAX_CHARSET - 1))
5104 {
5105 ipp_set_error(IPP_STATUS_ERROR_BAD_REQUEST, _("\"%s\": Bad charset value \"%s\" - bad length %d (RFC 8011 section 5.1.8)."), attr->name, attr->values[i].string.text, (int)(ptr - attr->values[i].string.text));
5106 return (0);
5107 }
5108 }
5109 break;
5110
5111 case IPP_TAG_LANGUAGE :
5112 /*
5113 * The following regular expression is derived from the ABNF for
5114 * language tags in RFC 4646. All I can say is that this is the
5115 * easiest way to check the values...
5116 */
5117
5118 if ((i = regcomp(&re,
5119 "^("
5120 "(([a-z]{2,3}(-[a-z][a-z][a-z]){0,3})|[a-z]{4,8})"
5121 /* language */
5122 "(-[a-z][a-z][a-z][a-z]){0,1}" /* script */
5123 "(-([a-z][a-z]|[0-9][0-9][0-9])){0,1}" /* region */
5124 "(-([a-z]{5,8}|[0-9][0-9][0-9]))*" /* variant */
5125 "(-[a-wy-z](-[a-z0-9]{2,8})+)*" /* extension */
5126 "(-x(-[a-z0-9]{1,8})+)*" /* privateuse */
5127 "|"
5128 "x(-[a-z0-9]{1,8})+" /* privateuse */
5129 "|"
5130 "[a-z]{1,3}(-[a-z][0-9]{2,8}){1,2}" /* grandfathered */
5131 ")$",
5132 REG_NOSUB | REG_EXTENDED)) != 0)
5133 {
5134 char temp[256]; /* Temporary error string */
5135
5136 regerror(i, &re, temp, sizeof(temp));
5137 ipp_set_error(IPP_STATUS_ERROR_INTERNAL, _("Unable to compile naturalLanguage regular expression: %s."), temp);
5138 return (0);
5139 }
5140
5141 for (i = 0; i < attr->num_values; i ++)
5142 {
5143 if (regexec(&re, attr->values[i].string.text, 0, NULL, 0))
5144 {
5145 ipp_set_error(IPP_STATUS_ERROR_BAD_REQUEST, _("\"%s\": Bad naturalLanguage value \"%s\" - bad characters (RFC 8011 section 5.1.9)."), attr->name, attr->values[i].string.text);
5146 regfree(&re);
5147 return (0);
5148 }
5149
5150 if (strlen(attr->values[i].string.text) > (IPP_MAX_LANGUAGE - 1))
5151 {
5152 ipp_set_error(IPP_STATUS_ERROR_BAD_REQUEST, _("\"%s\": Bad naturalLanguage value \"%s\" - bad length %d (RFC 8011 section 5.1.9)."), attr->name, attr->values[i].string.text, (int)strlen(attr->values[i].string.text));
5153 regfree(&re);
5154 return (0);
5155 }
5156 }
5157
5158 regfree(&re);
5159 break;
5160
5161 case IPP_TAG_MIMETYPE :
5162 /*
5163 * The following regular expression is derived from the ABNF for
5164 * MIME media types in RFC 2045 and 4288. All I can say is that this is
5165 * the easiest way to check the values...
5166 */
5167
5168 if ((i = regcomp(&re,
5169 "^"
5170 "[-a-zA-Z0-9!#$&.+^_]{1,127}" /* type-name */
5171 "/"
5172 "[-a-zA-Z0-9!#$&.+^_]{1,127}" /* subtype-name */
5173 "(;[-a-zA-Z0-9!#$&.+^_]{1,127}=" /* parameter= */
5174 "([-a-zA-Z0-9!#$&.+^_]{1,127}|\"[^\"]*\"))*"
5175 /* value */
5176 "$",
5177 REG_NOSUB | REG_EXTENDED)) != 0)
5178 {
5179 char temp[256]; /* Temporary error string */
5180
5181 regerror(i, &re, temp, sizeof(temp));
5182 ipp_set_error(IPP_STATUS_ERROR_BAD_REQUEST, _("Unable to compile mimeMediaType regular expression: %s."), temp);
5183 return (0);
5184 }
5185
5186 for (i = 0; i < attr->num_values; i ++)
5187 {
5188 if (regexec(&re, attr->values[i].string.text, 0, NULL, 0))
5189 {
5190 ipp_set_error(IPP_STATUS_ERROR_BAD_REQUEST, _("\"%s\": Bad mimeMediaType value \"%s\" - bad characters (RFC 8011 section 5.1.10)."), attr->name, attr->values[i].string.text);
5191 regfree(&re);
5192 return (0);
5193 }
5194
5195 if (strlen(attr->values[i].string.text) > (IPP_MAX_MIMETYPE - 1))
5196 {
5197 ipp_set_error(IPP_STATUS_ERROR_BAD_REQUEST, _("\"%s\": Bad mimeMediaType value \"%s\" - bad length %d (RFC 8011 section 5.1.10)."), attr->name, attr->values[i].string.text, (int)strlen(attr->values[i].string.text));
5198 regfree(&re);
5199 return (0);
5200 }
5201 }
5202
5203 regfree(&re);
5204 break;
5205
5206 default :
5207 break;
5208 }
5209
5210 return (1);
5211 }
5212
5213
5214 /*
5215 * 'ippValidateAttributes()' - Validate all attributes in an IPP message.
5216 *
5217 * This function validates the contents of the IPP message, including each
5218 * attribute. Like @link ippValidateAttribute@, @link cupsLastErrorString@ is
5219 * set to a human-readable message on failure.
5220 *
5221 * @since CUPS 1.7/macOS 10.9@
5222 */
5223
5224 int /* O - 1 if valid, 0 otherwise */
5225 ippValidateAttributes(ipp_t *ipp) /* I - IPP message */
5226 {
5227 ipp_attribute_t *attr; /* Current attribute */
5228
5229
5230 if (!ipp)
5231 return (1);
5232
5233 for (attr = ipp->attrs; attr; attr = attr->next)
5234 if (!ippValidateAttribute(attr))
5235 return (0);
5236
5237 return (1);
5238 }
5239
5240
5241 /*
5242 * 'ippWrite()' - Write data for an IPP message to a HTTP connection.
5243 */
5244
5245 ipp_state_t /* O - Current state */
5246 ippWrite(http_t *http, /* I - HTTP connection */
5247 ipp_t *ipp) /* I - IPP data */
5248 {
5249 DEBUG_printf(("ippWrite(http=%p, ipp=%p)", (void *)http, (void *)ipp));
5250
5251 if (!http)
5252 return (IPP_STATE_ERROR);
5253
5254 return (ippWriteIO(http, (ipp_iocb_t)httpWrite2, http->blocking, NULL, ipp));
5255 }
5256
5257
5258 /*
5259 * 'ippWriteFile()' - Write data for an IPP message to a file.
5260 *
5261 * @since CUPS 1.1.19/macOS 10.3@
5262 */
5263
5264 ipp_state_t /* O - Current state */
5265 ippWriteFile(int fd, /* I - HTTP data */
5266 ipp_t *ipp) /* I - IPP data */
5267 {
5268 DEBUG_printf(("ippWriteFile(fd=%d, ipp=%p)", fd, (void *)ipp));
5269
5270 ipp->state = IPP_STATE_IDLE;
5271
5272 return (ippWriteIO(&fd, (ipp_iocb_t)ipp_write_file, 1, NULL, ipp));
5273 }
5274
5275
5276 /*
5277 * 'ippWriteIO()' - Write data for an IPP message.
5278 *
5279 * @since CUPS 1.2/macOS 10.5@
5280 */
5281
5282 ipp_state_t /* O - Current state */
5283 ippWriteIO(void *dst, /* I - Destination */
5284 ipp_iocb_t cb, /* I - Write callback function */
5285 int blocking, /* I - Use blocking IO? */
5286 ipp_t *parent, /* I - Parent IPP message */
5287 ipp_t *ipp) /* I - IPP data */
5288 {
5289 int i; /* Looping var */
5290 int n; /* Length of data */
5291 unsigned char *buffer, /* Data buffer */
5292 *bufptr; /* Pointer into buffer */
5293 ipp_attribute_t *attr; /* Current attribute */
5294 _ipp_value_t *value; /* Current value */
5295
5296
5297 DEBUG_printf(("ippWriteIO(dst=%p, cb=%p, blocking=%d, parent=%p, ipp=%p)", (void *)dst, (void *)cb, blocking, (void *)parent, (void *)ipp));
5298
5299 if (!dst || !ipp)
5300 return (IPP_STATE_ERROR);
5301
5302 if ((buffer = (unsigned char *)_cupsBufferGet(IPP_BUF_SIZE)) == NULL)
5303 {
5304 DEBUG_puts("1ippWriteIO: Unable to get write buffer");
5305 return (IPP_STATE_ERROR);
5306 }
5307
5308 switch (ipp->state)
5309 {
5310 case IPP_STATE_IDLE :
5311 ipp->state ++; /* Avoid common problem... */
5312
5313 case IPP_STATE_HEADER :
5314 if (parent == NULL)
5315 {
5316 /*
5317 * Send the request header:
5318 *
5319 * Version = 2 bytes
5320 * Operation/Status Code = 2 bytes
5321 * Request ID = 4 bytes
5322 * Total = 8 bytes
5323 */
5324
5325 bufptr = buffer;
5326
5327 *bufptr++ = ipp->request.any.version[0];
5328 *bufptr++ = ipp->request.any.version[1];
5329 *bufptr++ = (ipp_uchar_t)(ipp->request.any.op_status >> 8);
5330 *bufptr++ = (ipp_uchar_t)ipp->request.any.op_status;
5331 *bufptr++ = (ipp_uchar_t)(ipp->request.any.request_id >> 24);
5332 *bufptr++ = (ipp_uchar_t)(ipp->request.any.request_id >> 16);
5333 *bufptr++ = (ipp_uchar_t)(ipp->request.any.request_id >> 8);
5334 *bufptr++ = (ipp_uchar_t)ipp->request.any.request_id;
5335
5336 DEBUG_printf(("2ippWriteIO: version=%d.%d", buffer[0], buffer[1]));
5337 DEBUG_printf(("2ippWriteIO: op_status=%04x",
5338 ipp->request.any.op_status));
5339 DEBUG_printf(("2ippWriteIO: request_id=%d",
5340 ipp->request.any.request_id));
5341
5342 if ((*cb)(dst, buffer, (size_t)(bufptr - buffer)) < 0)
5343 {
5344 DEBUG_puts("1ippWriteIO: Could not write IPP header...");
5345 _cupsBufferRelease((char *)buffer);
5346 return (IPP_STATE_ERROR);
5347 }
5348 }
5349
5350 /*
5351 * Reset the state engine to point to the first attribute
5352 * in the request/response, with no current group.
5353 */
5354
5355 ipp->state = IPP_STATE_ATTRIBUTE;
5356 ipp->current = ipp->attrs;
5357 ipp->curtag = IPP_TAG_ZERO;
5358
5359 DEBUG_printf(("1ippWriteIO: ipp->current=%p", (void *)ipp->current));
5360
5361 /*
5362 * If blocking is disabled, stop here...
5363 */
5364
5365 if (!blocking)
5366 break;
5367
5368 case IPP_STATE_ATTRIBUTE :
5369 while (ipp->current != NULL)
5370 {
5371 /*
5372 * Write this attribute...
5373 */
5374
5375 bufptr = buffer;
5376 attr = ipp->current;
5377
5378 ipp->current = ipp->current->next;
5379
5380 if (!parent)
5381 {
5382 if (ipp->curtag != attr->group_tag)
5383 {
5384 /*
5385 * Send a group tag byte...
5386 */
5387
5388 ipp->curtag = attr->group_tag;
5389
5390 if (attr->group_tag == IPP_TAG_ZERO)
5391 continue;
5392
5393 DEBUG_printf(("2ippWriteIO: wrote group tag=%x(%s)",
5394 attr->group_tag, ippTagString(attr->group_tag)));
5395 *bufptr++ = (ipp_uchar_t)attr->group_tag;
5396 }
5397 else if (attr->group_tag == IPP_TAG_ZERO)
5398 continue;
5399 }
5400
5401 DEBUG_printf(("1ippWriteIO: %s (%s%s)", attr->name,
5402 attr->num_values > 1 ? "1setOf " : "",
5403 ippTagString(attr->value_tag)));
5404
5405 /*
5406 * Write the attribute tag and name.
5407 *
5408 * The attribute name length does not include the trailing nul
5409 * character in the source string.
5410 *
5411 * Collection values (parent != NULL) are written differently...
5412 */
5413
5414 if (parent == NULL)
5415 {
5416 /*
5417 * Get the length of the attribute name, and make sure it won't
5418 * overflow the buffer...
5419 */
5420
5421 if ((n = (int)strlen(attr->name)) > (IPP_BUF_SIZE - 8))
5422 {
5423 DEBUG_printf(("1ippWriteIO: Attribute name too long (%d)", n));
5424 _cupsBufferRelease((char *)buffer);
5425 return (IPP_STATE_ERROR);
5426 }
5427
5428 /*
5429 * Write the value tag, name length, and name string...
5430 */
5431
5432 DEBUG_printf(("2ippWriteIO: writing value tag=%x(%s)",
5433 attr->value_tag, ippTagString(attr->value_tag)));
5434 DEBUG_printf(("2ippWriteIO: writing name=%d,\"%s\"", n,
5435 attr->name));
5436
5437 if (attr->value_tag > 0xff)
5438 {
5439 *bufptr++ = IPP_TAG_EXTENSION;
5440 *bufptr++ = (ipp_uchar_t)(attr->value_tag >> 24);
5441 *bufptr++ = (ipp_uchar_t)(attr->value_tag >> 16);
5442 *bufptr++ = (ipp_uchar_t)(attr->value_tag >> 8);
5443 *bufptr++ = (ipp_uchar_t)attr->value_tag;
5444 }
5445 else
5446 *bufptr++ = (ipp_uchar_t)attr->value_tag;
5447
5448 *bufptr++ = (ipp_uchar_t)(n >> 8);
5449 *bufptr++ = (ipp_uchar_t)n;
5450 memcpy(bufptr, attr->name, (size_t)n);
5451 bufptr += n;
5452 }
5453 else
5454 {
5455 /*
5456 * Get the length of the attribute name, and make sure it won't
5457 * overflow the buffer...
5458 */
5459
5460 if ((n = (int)strlen(attr->name)) > (IPP_BUF_SIZE - 12))
5461 {
5462 DEBUG_printf(("1ippWriteIO: Attribute name too long (%d)", n));
5463 _cupsBufferRelease((char *)buffer);
5464 return (IPP_STATE_ERROR);
5465 }
5466
5467 /*
5468 * Write the member name tag, name length, name string, value tag,
5469 * and empty name for the collection member attribute...
5470 */
5471
5472 DEBUG_printf(("2ippWriteIO: writing value tag=%x(memberName)",
5473 IPP_TAG_MEMBERNAME));
5474 DEBUG_printf(("2ippWriteIO: writing name=%d,\"%s\"", n,
5475 attr->name));
5476 DEBUG_printf(("2ippWriteIO: writing value tag=%x(%s)",
5477 attr->value_tag, ippTagString(attr->value_tag)));
5478 DEBUG_puts("2ippWriteIO: writing name=0,\"\"");
5479
5480 *bufptr++ = IPP_TAG_MEMBERNAME;
5481 *bufptr++ = 0;
5482 *bufptr++ = 0;
5483 *bufptr++ = (ipp_uchar_t)(n >> 8);
5484 *bufptr++ = (ipp_uchar_t)n;
5485 memcpy(bufptr, attr->name, (size_t)n);
5486 bufptr += n;
5487
5488 if (attr->value_tag > 0xff)
5489 {
5490 *bufptr++ = IPP_TAG_EXTENSION;
5491 *bufptr++ = (ipp_uchar_t)(attr->value_tag >> 24);
5492 *bufptr++ = (ipp_uchar_t)(attr->value_tag >> 16);
5493 *bufptr++ = (ipp_uchar_t)(attr->value_tag >> 8);
5494 *bufptr++ = (ipp_uchar_t)attr->value_tag;
5495 }
5496 else
5497 *bufptr++ = (ipp_uchar_t)attr->value_tag;
5498
5499 *bufptr++ = 0;
5500 *bufptr++ = 0;
5501 }
5502
5503 /*
5504 * Now write the attribute value(s)...
5505 */
5506
5507 switch (attr->value_tag & ~IPP_TAG_CUPS_CONST)
5508 {
5509 case IPP_TAG_UNSUPPORTED_VALUE :
5510 case IPP_TAG_DEFAULT :
5511 case IPP_TAG_UNKNOWN :
5512 case IPP_TAG_NOVALUE :
5513 case IPP_TAG_NOTSETTABLE :
5514 case IPP_TAG_DELETEATTR :
5515 case IPP_TAG_ADMINDEFINE :
5516 *bufptr++ = 0;
5517 *bufptr++ = 0;
5518 break;
5519
5520 case IPP_TAG_INTEGER :
5521 case IPP_TAG_ENUM :
5522 for (i = 0, value = attr->values;
5523 i < attr->num_values;
5524 i ++, value ++)
5525 {
5526 if ((IPP_BUF_SIZE - (bufptr - buffer)) < 9)
5527 {
5528 if ((*cb)(dst, buffer, (size_t)(bufptr - buffer)) < 0)
5529 {
5530 DEBUG_puts("1ippWriteIO: Could not write IPP "
5531 "attribute...");
5532 _cupsBufferRelease((char *)buffer);
5533 return (IPP_STATE_ERROR);
5534 }
5535
5536 bufptr = buffer;
5537 }
5538
5539 if (i)
5540 {
5541 /*
5542 * Arrays and sets are done by sending additional
5543 * values with a zero-length name...
5544 */
5545
5546 *bufptr++ = (ipp_uchar_t)attr->value_tag;
5547 *bufptr++ = 0;
5548 *bufptr++ = 0;
5549 }
5550
5551 /*
5552 * Integers and enumerations are both 4-byte signed
5553 * (twos-complement) values.
5554 *
5555 * Put the 2-byte length and 4-byte value into the buffer...
5556 */
5557
5558 *bufptr++ = 0;
5559 *bufptr++ = 4;
5560 *bufptr++ = (ipp_uchar_t)(value->integer >> 24);
5561 *bufptr++ = (ipp_uchar_t)(value->integer >> 16);
5562 *bufptr++ = (ipp_uchar_t)(value->integer >> 8);
5563 *bufptr++ = (ipp_uchar_t)value->integer;
5564 }
5565 break;
5566
5567 case IPP_TAG_BOOLEAN :
5568 for (i = 0, value = attr->values;
5569 i < attr->num_values;
5570 i ++, value ++)
5571 {
5572 if ((IPP_BUF_SIZE - (bufptr - buffer)) < 6)
5573 {
5574 if ((*cb)(dst, buffer, (size_t)(bufptr - buffer)) < 0)
5575 {
5576 DEBUG_puts("1ippWriteIO: Could not write IPP "
5577 "attribute...");
5578 _cupsBufferRelease((char *)buffer);
5579 return (IPP_STATE_ERROR);
5580 }
5581
5582 bufptr = buffer;
5583 }
5584
5585 if (i)
5586 {
5587 /*
5588 * Arrays and sets are done by sending additional
5589 * values with a zero-length name...
5590 */
5591
5592 *bufptr++ = (ipp_uchar_t)attr->value_tag;
5593 *bufptr++ = 0;
5594 *bufptr++ = 0;
5595 }
5596
5597 /*
5598 * Boolean values are 1-byte; 0 = false, 1 = true.
5599 *
5600 * Put the 2-byte length and 1-byte value into the buffer...
5601 */
5602
5603 *bufptr++ = 0;
5604 *bufptr++ = 1;
5605 *bufptr++ = (ipp_uchar_t)value->boolean;
5606 }
5607 break;
5608
5609 case IPP_TAG_TEXT :
5610 case IPP_TAG_NAME :
5611 case IPP_TAG_KEYWORD :
5612 case IPP_TAG_URI :
5613 case IPP_TAG_URISCHEME :
5614 case IPP_TAG_CHARSET :
5615 case IPP_TAG_LANGUAGE :
5616 case IPP_TAG_MIMETYPE :
5617 for (i = 0, value = attr->values;
5618 i < attr->num_values;
5619 i ++, value ++)
5620 {
5621 if (i)
5622 {
5623 /*
5624 * Arrays and sets are done by sending additional
5625 * values with a zero-length name...
5626 */
5627
5628 DEBUG_printf(("2ippWriteIO: writing value tag=%x(%s)",
5629 attr->value_tag,
5630 ippTagString(attr->value_tag)));
5631 DEBUG_printf(("2ippWriteIO: writing name=0,\"\""));
5632
5633 if ((IPP_BUF_SIZE - (bufptr - buffer)) < 3)
5634 {
5635 if ((*cb)(dst, buffer, (size_t)(bufptr - buffer)) < 0)
5636 {
5637 DEBUG_puts("1ippWriteIO: Could not write IPP "
5638 "attribute...");
5639 _cupsBufferRelease((char *)buffer);
5640 return (IPP_STATE_ERROR);
5641 }
5642
5643 bufptr = buffer;
5644 }
5645
5646 *bufptr++ = (ipp_uchar_t)attr->value_tag;
5647 *bufptr++ = 0;
5648 *bufptr++ = 0;
5649 }
5650
5651 if (value->string.text != NULL)
5652 n = (int)strlen(value->string.text);
5653 else
5654 n = 0;
5655
5656 if (n > (IPP_BUF_SIZE - 2))
5657 {
5658 DEBUG_printf(("1ippWriteIO: String too long (%d)", n));
5659 _cupsBufferRelease((char *)buffer);
5660 return (IPP_STATE_ERROR);
5661 }
5662
5663 DEBUG_printf(("2ippWriteIO: writing string=%d,\"%s\"", n,
5664 value->string.text));
5665
5666 if ((int)(IPP_BUF_SIZE - (bufptr - buffer)) < (n + 2))
5667 {
5668 if ((*cb)(dst, buffer, (size_t)(bufptr - buffer)) < 0)
5669 {
5670 DEBUG_puts("1ippWriteIO: Could not write IPP "
5671 "attribute...");
5672 _cupsBufferRelease((char *)buffer);
5673 return (IPP_STATE_ERROR);
5674 }
5675
5676 bufptr = buffer;
5677 }
5678
5679 /*
5680 * All simple strings consist of the 2-byte length and
5681 * character data without the trailing nul normally found
5682 * in C strings. Also, strings cannot be longer than IPP_MAX_LENGTH
5683 * bytes since the 2-byte length is a signed (twos-complement)
5684 * value.
5685 *
5686 * Put the 2-byte length and string characters in the buffer.
5687 */
5688
5689 *bufptr++ = (ipp_uchar_t)(n >> 8);
5690 *bufptr++ = (ipp_uchar_t)n;
5691
5692 if (n > 0)
5693 {
5694 memcpy(bufptr, value->string.text, (size_t)n);
5695 bufptr += n;
5696 }
5697 }
5698 break;
5699
5700 case IPP_TAG_DATE :
5701 for (i = 0, value = attr->values;
5702 i < attr->num_values;
5703 i ++, value ++)
5704 {
5705 if ((IPP_BUF_SIZE - (bufptr - buffer)) < 16)
5706 {
5707 if ((*cb)(dst, buffer, (size_t)(bufptr - buffer)) < 0)
5708 {
5709 DEBUG_puts("1ippWriteIO: Could not write IPP "
5710 "attribute...");
5711 _cupsBufferRelease((char *)buffer);
5712 return (IPP_STATE_ERROR);
5713 }
5714
5715 bufptr = buffer;
5716 }
5717
5718 if (i)
5719 {
5720 /*
5721 * Arrays and sets are done by sending additional
5722 * values with a zero-length name...
5723 */
5724
5725 *bufptr++ = (ipp_uchar_t)attr->value_tag;
5726 *bufptr++ = 0;
5727 *bufptr++ = 0;
5728 }
5729
5730 /*
5731 * Date values consist of a 2-byte length and an
5732 * 11-byte date/time structure defined by RFC 1903.
5733 *
5734 * Put the 2-byte length and 11-byte date/time
5735 * structure in the buffer.
5736 */
5737
5738 *bufptr++ = 0;
5739 *bufptr++ = 11;
5740 memcpy(bufptr, value->date, 11);
5741 bufptr += 11;
5742 }
5743 break;
5744
5745 case IPP_TAG_RESOLUTION :
5746 for (i = 0, value = attr->values;
5747 i < attr->num_values;
5748 i ++, value ++)
5749 {
5750 if ((IPP_BUF_SIZE - (bufptr - buffer)) < 14)
5751 {
5752 if ((*cb)(dst, buffer, (size_t)(bufptr - buffer)) < 0)
5753 {
5754 DEBUG_puts("1ippWriteIO: Could not write IPP "
5755 "attribute...");
5756 _cupsBufferRelease((char *)buffer);
5757 return (IPP_STATE_ERROR);
5758 }
5759
5760 bufptr = buffer;
5761 }
5762
5763 if (i)
5764 {
5765 /*
5766 * Arrays and sets are done by sending additional
5767 * values with a zero-length name...
5768 */
5769
5770 *bufptr++ = (ipp_uchar_t)attr->value_tag;
5771 *bufptr++ = 0;
5772 *bufptr++ = 0;
5773 }
5774
5775 /*
5776 * Resolution values consist of a 2-byte length,
5777 * 4-byte horizontal resolution value, 4-byte vertical
5778 * resolution value, and a 1-byte units value.
5779 *
5780 * Put the 2-byte length and resolution value data
5781 * into the buffer.
5782 */
5783
5784 *bufptr++ = 0;
5785 *bufptr++ = 9;
5786 *bufptr++ = (ipp_uchar_t)(value->resolution.xres >> 24);
5787 *bufptr++ = (ipp_uchar_t)(value->resolution.xres >> 16);
5788 *bufptr++ = (ipp_uchar_t)(value->resolution.xres >> 8);
5789 *bufptr++ = (ipp_uchar_t)value->resolution.xres;
5790 *bufptr++ = (ipp_uchar_t)(value->resolution.yres >> 24);
5791 *bufptr++ = (ipp_uchar_t)(value->resolution.yres >> 16);
5792 *bufptr++ = (ipp_uchar_t)(value->resolution.yres >> 8);
5793 *bufptr++ = (ipp_uchar_t)value->resolution.yres;
5794 *bufptr++ = (ipp_uchar_t)value->resolution.units;
5795 }
5796 break;
5797
5798 case IPP_TAG_RANGE :
5799 for (i = 0, value = attr->values;
5800 i < attr->num_values;
5801 i ++, value ++)
5802 {
5803 if ((IPP_BUF_SIZE - (bufptr - buffer)) < 13)
5804 {
5805 if ((*cb)(dst, buffer, (size_t)(bufptr - buffer)) < 0)
5806 {
5807 DEBUG_puts("1ippWriteIO: Could not write IPP "
5808 "attribute...");
5809 _cupsBufferRelease((char *)buffer);
5810 return (IPP_STATE_ERROR);
5811 }
5812
5813 bufptr = buffer;
5814 }
5815
5816 if (i)
5817 {
5818 /*
5819 * Arrays and sets are done by sending additional
5820 * values with a zero-length name...
5821 */
5822
5823 *bufptr++ = (ipp_uchar_t)attr->value_tag;
5824 *bufptr++ = 0;
5825 *bufptr++ = 0;
5826 }
5827
5828 /*
5829 * Range values consist of a 2-byte length,
5830 * 4-byte lower value, and 4-byte upper value.
5831 *
5832 * Put the 2-byte length and range value data
5833 * into the buffer.
5834 */
5835
5836 *bufptr++ = 0;
5837 *bufptr++ = 8;
5838 *bufptr++ = (ipp_uchar_t)(value->range.lower >> 24);
5839 *bufptr++ = (ipp_uchar_t)(value->range.lower >> 16);
5840 *bufptr++ = (ipp_uchar_t)(value->range.lower >> 8);
5841 *bufptr++ = (ipp_uchar_t)value->range.lower;
5842 *bufptr++ = (ipp_uchar_t)(value->range.upper >> 24);
5843 *bufptr++ = (ipp_uchar_t)(value->range.upper >> 16);
5844 *bufptr++ = (ipp_uchar_t)(value->range.upper >> 8);
5845 *bufptr++ = (ipp_uchar_t)value->range.upper;
5846 }
5847 break;
5848
5849 case IPP_TAG_TEXTLANG :
5850 case IPP_TAG_NAMELANG :
5851 for (i = 0, value = attr->values;
5852 i < attr->num_values;
5853 i ++, value ++)
5854 {
5855 if (i)
5856 {
5857 /*
5858 * Arrays and sets are done by sending additional
5859 * values with a zero-length name...
5860 */
5861
5862 if ((IPP_BUF_SIZE - (bufptr - buffer)) < 3)
5863 {
5864 if ((*cb)(dst, buffer, (size_t)(bufptr - buffer)) < 0)
5865 {
5866 DEBUG_puts("1ippWriteIO: Could not write IPP "
5867 "attribute...");
5868 _cupsBufferRelease((char *)buffer);
5869 return (IPP_STATE_ERROR);
5870 }
5871
5872 bufptr = buffer;
5873 }
5874
5875 *bufptr++ = (ipp_uchar_t)attr->value_tag;
5876 *bufptr++ = 0;
5877 *bufptr++ = 0;
5878 }
5879
5880 /*
5881 * textWithLanguage and nameWithLanguage values consist
5882 * of a 2-byte length for both strings and their
5883 * individual lengths, a 2-byte length for the
5884 * character string, the character string without the
5885 * trailing nul, a 2-byte length for the character
5886 * set string, and the character set string without
5887 * the trailing nul.
5888 */
5889
5890 n = 4;
5891
5892 if (value->string.language != NULL)
5893 n += (int)strlen(value->string.language);
5894
5895 if (value->string.text != NULL)
5896 n += (int)strlen(value->string.text);
5897
5898 if (n > (IPP_BUF_SIZE - 2))
5899 {
5900 DEBUG_printf(("1ippWriteIO: text/nameWithLanguage value "
5901 "too long (%d)", n));
5902 _cupsBufferRelease((char *)buffer);
5903 return (IPP_STATE_ERROR);
5904 }
5905
5906 if ((int)(IPP_BUF_SIZE - (bufptr - buffer)) < (n + 2))
5907 {
5908 if ((*cb)(dst, buffer, (size_t)(bufptr - buffer)) < 0)
5909 {
5910 DEBUG_puts("1ippWriteIO: Could not write IPP "
5911 "attribute...");
5912 _cupsBufferRelease((char *)buffer);
5913 return (IPP_STATE_ERROR);
5914 }
5915
5916 bufptr = buffer;
5917 }
5918
5919 /* Length of entire value */
5920 *bufptr++ = (ipp_uchar_t)(n >> 8);
5921 *bufptr++ = (ipp_uchar_t)n;
5922
5923 /* Length of language */
5924 if (value->string.language != NULL)
5925 n = (int)strlen(value->string.language);
5926 else
5927 n = 0;
5928
5929 *bufptr++ = (ipp_uchar_t)(n >> 8);
5930 *bufptr++ = (ipp_uchar_t)n;
5931
5932 /* Language */
5933 if (n > 0)
5934 {
5935 memcpy(bufptr, value->string.language, (size_t)n);
5936 bufptr += n;
5937 }
5938
5939 /* Length of text */
5940 if (value->string.text != NULL)
5941 n = (int)strlen(value->string.text);
5942 else
5943 n = 0;
5944
5945 *bufptr++ = (ipp_uchar_t)(n >> 8);
5946 *bufptr++ = (ipp_uchar_t)n;
5947
5948 /* Text */
5949 if (n > 0)
5950 {
5951 memcpy(bufptr, value->string.text, (size_t)n);
5952 bufptr += n;
5953 }
5954 }
5955 break;
5956
5957 case IPP_TAG_BEGIN_COLLECTION :
5958 for (i = 0, value = attr->values;
5959 i < attr->num_values;
5960 i ++, value ++)
5961 {
5962 /*
5963 * Collections are written with the begin-collection
5964 * tag first with a value of 0 length, followed by the
5965 * attributes in the collection, then the end-collection
5966 * value...
5967 */
5968
5969 if ((IPP_BUF_SIZE - (bufptr - buffer)) < 5)
5970 {
5971 if ((*cb)(dst, buffer, (size_t)(bufptr - buffer)) < 0)
5972 {
5973 DEBUG_puts("1ippWriteIO: Could not write IPP "
5974 "attribute...");
5975 _cupsBufferRelease((char *)buffer);
5976 return (IPP_STATE_ERROR);
5977 }
5978
5979 bufptr = buffer;
5980 }
5981
5982 if (i)
5983 {
5984 /*
5985 * Arrays and sets are done by sending additional
5986 * values with a zero-length name...
5987 */
5988
5989 *bufptr++ = (ipp_uchar_t)attr->value_tag;
5990 *bufptr++ = 0;
5991 *bufptr++ = 0;
5992 }
5993
5994 /*
5995 * Write a data length of 0 and flush the buffer...
5996 */
5997
5998 *bufptr++ = 0;
5999 *bufptr++ = 0;
6000
6001 if ((*cb)(dst, buffer, (size_t)(bufptr - buffer)) < 0)
6002 {
6003 DEBUG_puts("1ippWriteIO: Could not write IPP "
6004 "attribute...");
6005 _cupsBufferRelease((char *)buffer);
6006 return (IPP_STATE_ERROR);
6007 }
6008
6009 bufptr = buffer;
6010
6011 /*
6012 * Then write the collection attribute...
6013 */
6014
6015 value->collection->state = IPP_STATE_IDLE;
6016
6017 if (ippWriteIO(dst, cb, 1, ipp,
6018 value->collection) == IPP_STATE_ERROR)
6019 {
6020 DEBUG_puts("1ippWriteIO: Unable to write collection value");
6021 _cupsBufferRelease((char *)buffer);
6022 return (IPP_STATE_ERROR);
6023 }
6024 }
6025 break;
6026
6027 default :
6028 for (i = 0, value = attr->values;
6029 i < attr->num_values;
6030 i ++, value ++)
6031 {
6032 if (i)
6033 {
6034 /*
6035 * Arrays and sets are done by sending additional
6036 * values with a zero-length name...
6037 */
6038
6039 if ((IPP_BUF_SIZE - (bufptr - buffer)) < 3)
6040 {
6041 if ((*cb)(dst, buffer, (size_t)(bufptr - buffer)) < 0)
6042 {
6043 DEBUG_puts("1ippWriteIO: Could not write IPP "
6044 "attribute...");
6045 _cupsBufferRelease((char *)buffer);
6046 return (IPP_STATE_ERROR);
6047 }
6048
6049 bufptr = buffer;
6050 }
6051
6052 *bufptr++ = (ipp_uchar_t)attr->value_tag;
6053 *bufptr++ = 0;
6054 *bufptr++ = 0;
6055 }
6056
6057 /*
6058 * An unknown value might some new value that a
6059 * vendor has come up with. It consists of a
6060 * 2-byte length and the bytes in the unknown
6061 * value buffer.
6062 */
6063
6064 n = value->unknown.length;
6065
6066 if (n > (IPP_BUF_SIZE - 2))
6067 {
6068 DEBUG_printf(("1ippWriteIO: Data length too long (%d)",
6069 n));
6070 _cupsBufferRelease((char *)buffer);
6071 return (IPP_STATE_ERROR);
6072 }
6073
6074 if ((int)(IPP_BUF_SIZE - (bufptr - buffer)) < (n + 2))
6075 {
6076 if ((*cb)(dst, buffer, (size_t)(bufptr - buffer)) < 0)
6077 {
6078 DEBUG_puts("1ippWriteIO: Could not write IPP "
6079 "attribute...");
6080 _cupsBufferRelease((char *)buffer);
6081 return (IPP_STATE_ERROR);
6082 }
6083
6084 bufptr = buffer;
6085 }
6086
6087 /* Length of unknown value */
6088 *bufptr++ = (ipp_uchar_t)(n >> 8);
6089 *bufptr++ = (ipp_uchar_t)n;
6090
6091 /* Value */
6092 if (n > 0)
6093 {
6094 memcpy(bufptr, value->unknown.data, (size_t)n);
6095 bufptr += n;
6096 }
6097 }
6098 break;
6099 }
6100
6101 /*
6102 * Write the data out...
6103 */
6104
6105 if (bufptr > buffer)
6106 {
6107 if ((*cb)(dst, buffer, (size_t)(bufptr - buffer)) < 0)
6108 {
6109 DEBUG_puts("1ippWriteIO: Could not write IPP attribute...");
6110 _cupsBufferRelease((char *)buffer);
6111 return (IPP_STATE_ERROR);
6112 }
6113
6114 DEBUG_printf(("2ippWriteIO: wrote %d bytes",
6115 (int)(bufptr - buffer)));
6116 }
6117
6118 /*
6119 * If blocking is disabled and we aren't at the end of the attribute
6120 * list, stop here...
6121 */
6122
6123 if (!blocking && ipp->current)
6124 break;
6125 }
6126
6127 if (ipp->current == NULL)
6128 {
6129 /*
6130 * Done with all of the attributes; add the end-of-attributes
6131 * tag or end-collection attribute...
6132 */
6133
6134 if (parent == NULL)
6135 {
6136 buffer[0] = IPP_TAG_END;
6137 n = 1;
6138 }
6139 else
6140 {
6141 buffer[0] = IPP_TAG_END_COLLECTION;
6142 buffer[1] = 0; /* empty name */
6143 buffer[2] = 0;
6144 buffer[3] = 0; /* empty value */
6145 buffer[4] = 0;
6146 n = 5;
6147 }
6148
6149 if ((*cb)(dst, buffer, (size_t)n) < 0)
6150 {
6151 DEBUG_puts("1ippWriteIO: Could not write IPP end-tag...");
6152 _cupsBufferRelease((char *)buffer);
6153 return (IPP_STATE_ERROR);
6154 }
6155
6156 ipp->state = IPP_STATE_DATA;
6157 }
6158 break;
6159
6160 case IPP_STATE_DATA :
6161 break;
6162
6163 default :
6164 break; /* anti-compiler-warning-code */
6165 }
6166
6167 _cupsBufferRelease((char *)buffer);
6168
6169 return (ipp->state);
6170 }
6171
6172
6173 /*
6174 * 'ipp_add_attr()' - Add a new attribute to the message.
6175 */
6176
6177 static ipp_attribute_t * /* O - New attribute */
6178 ipp_add_attr(ipp_t *ipp, /* I - IPP message */
6179 const char *name, /* I - Attribute name or NULL */
6180 ipp_tag_t group_tag, /* I - Group tag or IPP_TAG_ZERO */
6181 ipp_tag_t value_tag, /* I - Value tag or IPP_TAG_ZERO */
6182 int num_values) /* I - Number of values */
6183 {
6184 int alloc_values; /* Number of values to allocate */
6185 ipp_attribute_t *attr; /* New attribute */
6186
6187
6188 DEBUG_printf(("4ipp_add_attr(ipp=%p, name=\"%s\", group_tag=0x%x, value_tag=0x%x, num_values=%d)", (void *)ipp, name, group_tag, value_tag, num_values));
6189
6190 /*
6191 * Range check input...
6192 */
6193
6194 if (!ipp || num_values < 0)
6195 return (NULL);
6196
6197 /*
6198 * Allocate memory, rounding the allocation up as needed...
6199 */
6200
6201 if (num_values <= 1)
6202 alloc_values = 1;
6203 else
6204 alloc_values = (num_values + IPP_MAX_VALUES - 1) & ~(IPP_MAX_VALUES - 1);
6205
6206 attr = calloc(sizeof(ipp_attribute_t) +
6207 (size_t)(alloc_values - 1) * sizeof(_ipp_value_t), 1);
6208
6209 if (attr)
6210 {
6211 /*
6212 * Initialize attribute...
6213 */
6214
6215 DEBUG_printf(("4debug_alloc: %p %s %s%s (%d values)", (void *)attr, name, num_values > 1 ? "1setOf " : "", ippTagString(value_tag), num_values));
6216
6217 if (name)
6218 attr->name = _cupsStrAlloc(name);
6219
6220 attr->group_tag = group_tag;
6221 attr->value_tag = value_tag;
6222 attr->num_values = num_values;
6223
6224 /*
6225 * Add it to the end of the linked list...
6226 */
6227
6228 if (ipp->last)
6229 ipp->last->next = attr;
6230 else
6231 ipp->attrs = attr;
6232
6233 ipp->prev = ipp->last;
6234 ipp->last = ipp->current = attr;
6235 }
6236
6237 DEBUG_printf(("5ipp_add_attr: Returning %p", (void *)attr));
6238
6239 return (attr);
6240 }
6241
6242
6243 /*
6244 * 'ipp_free_values()' - Free attribute values.
6245 */
6246
6247 static void
6248 ipp_free_values(ipp_attribute_t *attr, /* I - Attribute to free values from */
6249 int element,/* I - First value to free */
6250 int count) /* I - Number of values to free */
6251 {
6252 int i; /* Looping var */
6253 _ipp_value_t *value; /* Current value */
6254
6255
6256 DEBUG_printf(("4ipp_free_values(attr=%p, element=%d, count=%d)", (void *)attr, element, count));
6257
6258 if (!(attr->value_tag & IPP_TAG_CUPS_CONST))
6259 {
6260 /*
6261 * Free values as needed...
6262 */
6263
6264 switch (attr->value_tag)
6265 {
6266 case IPP_TAG_TEXTLANG :
6267 case IPP_TAG_NAMELANG :
6268 if (element == 0 && count == attr->num_values &&
6269 attr->values[0].string.language)
6270 {
6271 _cupsStrFree(attr->values[0].string.language);
6272 attr->values[0].string.language = NULL;
6273 }
6274 /* Fall through to other string values */
6275
6276 case IPP_TAG_TEXT :
6277 case IPP_TAG_NAME :
6278 case IPP_TAG_RESERVED_STRING :
6279 case IPP_TAG_KEYWORD :
6280 case IPP_TAG_URI :
6281 case IPP_TAG_URISCHEME :
6282 case IPP_TAG_CHARSET :
6283 case IPP_TAG_LANGUAGE :
6284 case IPP_TAG_MIMETYPE :
6285 for (i = count, value = attr->values + element;
6286 i > 0;
6287 i --, value ++)
6288 {
6289 _cupsStrFree(value->string.text);
6290 value->string.text = NULL;
6291 }
6292 break;
6293
6294 case IPP_TAG_UNSUPPORTED_VALUE :
6295 case IPP_TAG_DEFAULT :
6296 case IPP_TAG_UNKNOWN :
6297 case IPP_TAG_NOVALUE :
6298 case IPP_TAG_NOTSETTABLE :
6299 case IPP_TAG_DELETEATTR :
6300 case IPP_TAG_ADMINDEFINE :
6301 case IPP_TAG_INTEGER :
6302 case IPP_TAG_ENUM :
6303 case IPP_TAG_BOOLEAN :
6304 case IPP_TAG_DATE :
6305 case IPP_TAG_RESOLUTION :
6306 case IPP_TAG_RANGE :
6307 break;
6308
6309 case IPP_TAG_BEGIN_COLLECTION :
6310 for (i = count, value = attr->values + element;
6311 i > 0;
6312 i --, value ++)
6313 {
6314 ippDelete(value->collection);
6315 value->collection = NULL;
6316 }
6317 break;
6318
6319 case IPP_TAG_STRING :
6320 default :
6321 for (i = count, value = attr->values + element;
6322 i > 0;
6323 i --, value ++)
6324 {
6325 if (value->unknown.data)
6326 {
6327 free(value->unknown.data);
6328 value->unknown.data = NULL;
6329 }
6330 }
6331 break;
6332 }
6333 }
6334
6335 /*
6336 * If we are not freeing values from the end, move the remaining values up...
6337 */
6338
6339 if ((element + count) < attr->num_values)
6340 memmove(attr->values + element, attr->values + element + count,
6341 (size_t)(attr->num_values - count - element) * sizeof(_ipp_value_t));
6342
6343 attr->num_values -= count;
6344 }
6345
6346
6347 /*
6348 * 'ipp_get_code()' - Convert a C locale/charset name into an IPP language/charset code.
6349 *
6350 * This typically converts strings of the form "ll_CC", "ll-REGION", and "CHARSET_NUMBER"
6351 * to "ll-cc", "ll-region", and "charset-number", respectively.
6352 */
6353
6354 static char * /* O - Language code string */
6355 ipp_get_code(const char *value, /* I - Locale/charset string */
6356 char *buffer, /* I - String buffer */
6357 size_t bufsize) /* I - Size of string buffer */
6358 {
6359 char *bufptr, /* Pointer into buffer */
6360 *bufend; /* End of buffer */
6361
6362
6363 /*
6364 * Convert values to lowercase and change _ to - as needed...
6365 */
6366
6367 for (bufptr = buffer, bufend = buffer + bufsize - 1;
6368 *value && bufptr < bufend;
6369 value ++)
6370 if (*value == '_')
6371 *bufptr++ = '-';
6372 else
6373 *bufptr++ = (char)_cups_tolower(*value);
6374
6375 *bufptr = '\0';
6376
6377 /*
6378 * Return the converted string...
6379 */
6380
6381 return (buffer);
6382 }
6383
6384
6385 /*
6386 * 'ipp_lang_code()' - Convert a C locale name into an IPP language code.
6387 *
6388 * This typically converts strings of the form "ll_CC" and "ll-REGION" to "ll-cc" and
6389 * "ll-region", respectively. It also converts the "C" (POSIX) locale to "en".
6390 */
6391
6392 static char * /* O - Language code string */
6393 ipp_lang_code(const char *locale, /* I - Locale string */
6394 char *buffer, /* I - String buffer */
6395 size_t bufsize) /* I - Size of string buffer */
6396 {
6397 /*
6398 * Map POSIX ("C") locale to generic English, otherwise convert the locale string as-is.
6399 */
6400
6401 if (!_cups_strcasecmp(locale, "c"))
6402 {
6403 strlcpy(buffer, "en", bufsize);
6404 return (buffer);
6405 }
6406 else
6407 return (ipp_get_code(locale, buffer, bufsize));
6408 }
6409
6410
6411 /*
6412 * 'ipp_length()' - Compute the length of an IPP message or collection value.
6413 */
6414
6415 static size_t /* O - Size of IPP message */
6416 ipp_length(ipp_t *ipp, /* I - IPP message or collection */
6417 int collection) /* I - 1 if a collection, 0 otherwise */
6418 {
6419 int i; /* Looping var */
6420 size_t bytes; /* Number of bytes */
6421 ipp_attribute_t *attr; /* Current attribute */
6422 ipp_tag_t group; /* Current group */
6423 _ipp_value_t *value; /* Current value */
6424
6425
6426 DEBUG_printf(("3ipp_length(ipp=%p, collection=%d)", (void *)ipp, collection));
6427
6428 if (!ipp)
6429 {
6430 DEBUG_puts("4ipp_length: Returning 0 bytes");
6431 return (0);
6432 }
6433
6434 /*
6435 * Start with 8 bytes for the IPP message header...
6436 */
6437
6438 bytes = collection ? 0 : 8;
6439
6440 /*
6441 * Then add the lengths of each attribute...
6442 */
6443
6444 group = IPP_TAG_ZERO;
6445
6446 for (attr = ipp->attrs; attr != NULL; attr = attr->next)
6447 {
6448 if (attr->group_tag != group && !collection)
6449 {
6450 group = attr->group_tag;
6451 if (group == IPP_TAG_ZERO)
6452 continue;
6453
6454 bytes ++; /* Group tag */
6455 }
6456
6457 if (!attr->name)
6458 continue;
6459
6460 DEBUG_printf(("5ipp_length: attr->name=\"%s\", attr->num_values=%d, "
6461 "bytes=" CUPS_LLFMT, attr->name, attr->num_values, CUPS_LLCAST bytes));
6462
6463 if ((attr->value_tag & ~IPP_TAG_CUPS_CONST) < IPP_TAG_EXTENSION)
6464 bytes += (size_t)attr->num_values;/* Value tag for each value */
6465 else
6466 bytes += (size_t)(5 * attr->num_values);
6467 /* Value tag for each value */
6468 bytes += (size_t)(2 * attr->num_values);
6469 /* Name lengths */
6470 bytes += strlen(attr->name); /* Name */
6471 bytes += (size_t)(2 * attr->num_values);
6472 /* Value lengths */
6473
6474 if (collection)
6475 bytes += 5; /* Add membername overhead */
6476
6477 switch (attr->value_tag & ~IPP_TAG_CUPS_CONST)
6478 {
6479 case IPP_TAG_UNSUPPORTED_VALUE :
6480 case IPP_TAG_DEFAULT :
6481 case IPP_TAG_UNKNOWN :
6482 case IPP_TAG_NOVALUE :
6483 case IPP_TAG_NOTSETTABLE :
6484 case IPP_TAG_DELETEATTR :
6485 case IPP_TAG_ADMINDEFINE :
6486 break;
6487
6488 case IPP_TAG_INTEGER :
6489 case IPP_TAG_ENUM :
6490 bytes += (size_t)(4 * attr->num_values);
6491 break;
6492
6493 case IPP_TAG_BOOLEAN :
6494 bytes += (size_t)attr->num_values;
6495 break;
6496
6497 case IPP_TAG_TEXT :
6498 case IPP_TAG_NAME :
6499 case IPP_TAG_KEYWORD :
6500 case IPP_TAG_URI :
6501 case IPP_TAG_URISCHEME :
6502 case IPP_TAG_CHARSET :
6503 case IPP_TAG_LANGUAGE :
6504 case IPP_TAG_MIMETYPE :
6505 for (i = 0, value = attr->values;
6506 i < attr->num_values;
6507 i ++, value ++)
6508 if (value->string.text)
6509 bytes += strlen(value->string.text);
6510 break;
6511
6512 case IPP_TAG_DATE :
6513 bytes += (size_t)(11 * attr->num_values);
6514 break;
6515
6516 case IPP_TAG_RESOLUTION :
6517 bytes += (size_t)(9 * attr->num_values);
6518 break;
6519
6520 case IPP_TAG_RANGE :
6521 bytes += (size_t)(8 * attr->num_values);
6522 break;
6523
6524 case IPP_TAG_TEXTLANG :
6525 case IPP_TAG_NAMELANG :
6526 bytes += (size_t)(4 * attr->num_values);
6527 /* Charset + text length */
6528
6529 for (i = 0, value = attr->values;
6530 i < attr->num_values;
6531 i ++, value ++)
6532 {
6533 if (value->string.language)
6534 bytes += strlen(value->string.language);
6535
6536 if (value->string.text)
6537 bytes += strlen(value->string.text);
6538 }
6539 break;
6540
6541 case IPP_TAG_BEGIN_COLLECTION :
6542 for (i = 0, value = attr->values;
6543 i < attr->num_values;
6544 i ++, value ++)
6545 bytes += ipp_length(value->collection, 1);
6546 break;
6547
6548 default :
6549 for (i = 0, value = attr->values;
6550 i < attr->num_values;
6551 i ++, value ++)
6552 bytes += (size_t)value->unknown.length;
6553 break;
6554 }
6555 }
6556
6557 /*
6558 * Finally, add 1 byte for the "end of attributes" tag or 5 bytes
6559 * for the "end of collection" tag and return...
6560 */
6561
6562 if (collection)
6563 bytes += 5;
6564 else
6565 bytes ++;
6566
6567 DEBUG_printf(("4ipp_length: Returning " CUPS_LLFMT " bytes", CUPS_LLCAST bytes));
6568
6569 return (bytes);
6570 }
6571
6572
6573 /*
6574 * 'ipp_read_http()' - Semi-blocking read on a HTTP connection...
6575 */
6576
6577 static ssize_t /* O - Number of bytes read */
6578 ipp_read_http(http_t *http, /* I - Client connection */
6579 ipp_uchar_t *buffer, /* O - Buffer for data */
6580 size_t length) /* I - Total length */
6581 {
6582 ssize_t tbytes, /* Total bytes read */
6583 bytes; /* Bytes read this pass */
6584
6585
6586 DEBUG_printf(("7ipp_read_http(http=%p, buffer=%p, length=%d)", (void *)http, (void *)buffer, (int)length));
6587
6588 /*
6589 * Loop until all bytes are read...
6590 */
6591
6592 for (tbytes = 0, bytes = 0;
6593 tbytes < (int)length;
6594 tbytes += bytes, buffer += bytes)
6595 {
6596 DEBUG_printf(("9ipp_read_http: tbytes=" CUPS_LLFMT ", http->state=%d", CUPS_LLCAST tbytes, http->state));
6597
6598 if (http->state == HTTP_STATE_WAITING)
6599 break;
6600
6601 if (http->used == 0 && !http->blocking)
6602 {
6603 /*
6604 * Wait up to 10 seconds for more data on non-blocking sockets...
6605 */
6606
6607 if (!httpWait(http, 10000))
6608 {
6609 /*
6610 * Signal no data...
6611 */
6612
6613 bytes = -1;
6614 break;
6615 }
6616 }
6617 else if (http->used == 0 && http->timeout_value > 0)
6618 {
6619 /*
6620 * Wait up to timeout seconds for more data on blocking sockets...
6621 */
6622
6623 if (!httpWait(http, (int)(1000 * http->timeout_value)))
6624 {
6625 /*
6626 * Signal no data...
6627 */
6628
6629 bytes = -1;
6630 break;
6631 }
6632 }
6633
6634 if ((bytes = httpRead2(http, (char *)buffer, length - (size_t)tbytes)) < 0)
6635 {
6636 #ifdef _WIN32
6637 break;
6638 #else
6639 if (errno != EAGAIN && errno != EINTR)
6640 break;
6641
6642 bytes = 0;
6643 #endif /* _WIN32 */
6644 }
6645 else if (bytes == 0)
6646 break;
6647 }
6648
6649 /*
6650 * Return the number of bytes read...
6651 */
6652
6653 if (tbytes == 0 && bytes < 0)
6654 tbytes = -1;
6655
6656 DEBUG_printf(("8ipp_read_http: Returning " CUPS_LLFMT " bytes", CUPS_LLCAST tbytes));
6657
6658 return (tbytes);
6659 }
6660
6661
6662 /*
6663 * 'ipp_read_file()' - Read IPP data from a file.
6664 */
6665
6666 static ssize_t /* O - Number of bytes read */
6667 ipp_read_file(int *fd, /* I - File descriptor */
6668 ipp_uchar_t *buffer, /* O - Read buffer */
6669 size_t length) /* I - Number of bytes to read */
6670 {
6671 #ifdef _WIN32
6672 return ((ssize_t)read(*fd, buffer, (unsigned)length));
6673 #else
6674 return (read(*fd, buffer, length));
6675 #endif /* _WIN32 */
6676 }
6677
6678
6679 /*
6680 * 'ipp_set_error()' - Set a formatted, localized error string.
6681 */
6682
6683 static void
6684 ipp_set_error(ipp_status_t status, /* I - Status code */
6685 const char *format, /* I - Printf-style error string */
6686 ...) /* I - Additional arguments as needed */
6687 {
6688 va_list ap; /* Pointer to additional args */
6689 char buffer[2048]; /* Message buffer */
6690 cups_lang_t *lang = cupsLangDefault();
6691 /* Current language */
6692
6693
6694 va_start(ap, format);
6695 vsnprintf(buffer, sizeof(buffer), _cupsLangString(lang, format), ap);
6696 va_end(ap);
6697
6698 _cupsSetError(status, buffer, 0);
6699 }
6700
6701
6702 /*
6703 * 'ipp_set_value()' - Get the value element from an attribute, expanding it as
6704 * needed.
6705 */
6706
6707 static _ipp_value_t * /* O - IPP value element or NULL on error */
6708 ipp_set_value(ipp_t *ipp, /* IO - IPP message */
6709 ipp_attribute_t **attr, /* IO - IPP attribute */
6710 int element) /* I - Value number (0-based) */
6711 {
6712 ipp_attribute_t *temp, /* New attribute pointer */
6713 *current, /* Current attribute in list */
6714 *prev; /* Previous attribute in list */
6715 int alloc_values; /* Allocated values */
6716
6717
6718 /*
6719 * If we are setting an existing value element, return it...
6720 */
6721
6722 temp = *attr;
6723
6724 if (temp->num_values <= 1)
6725 alloc_values = 1;
6726 else
6727 alloc_values = (temp->num_values + IPP_MAX_VALUES - 1) &
6728 ~(IPP_MAX_VALUES - 1);
6729
6730 if (element < alloc_values)
6731 {
6732 if (element >= temp->num_values)
6733 temp->num_values = element + 1;
6734
6735 return (temp->values + element);
6736 }
6737
6738 /*
6739 * Otherwise re-allocate the attribute - we allocate in groups of IPP_MAX_VALUE
6740 * values when num_values > 1.
6741 */
6742
6743 if (alloc_values < IPP_MAX_VALUES)
6744 alloc_values = IPP_MAX_VALUES;
6745 else
6746 alloc_values += IPP_MAX_VALUES;
6747
6748 DEBUG_printf(("4ipp_set_value: Reallocating for up to %d values.",
6749 alloc_values));
6750
6751 /*
6752 * Reallocate memory...
6753 */
6754
6755 if ((temp = realloc(temp, sizeof(ipp_attribute_t) + (size_t)(alloc_values - 1) * sizeof(_ipp_value_t))) == NULL)
6756 {
6757 _cupsSetHTTPError(HTTP_STATUS_ERROR);
6758 DEBUG_puts("4ipp_set_value: Unable to resize attribute.");
6759 return (NULL);
6760 }
6761
6762 /*
6763 * Zero the new memory...
6764 */
6765
6766 memset(temp->values + temp->num_values, 0, (size_t)(alloc_values - temp->num_values) * sizeof(_ipp_value_t));
6767
6768 if (temp != *attr)
6769 {
6770 /*
6771 * Reset pointers in the list...
6772 */
6773
6774 #ifndef __clang_analyzer__
6775 DEBUG_printf(("4debug_free: %p %s", (void *)*attr, temp->name));
6776 #endif /* !__clang_analyzer__ */
6777 DEBUG_printf(("4debug_alloc: %p %s %s%s (%d)", (void *)temp, temp->name, temp->num_values > 1 ? "1setOf " : "", ippTagString(temp->value_tag), temp->num_values));
6778
6779 if (ipp->current == *attr && ipp->prev)
6780 {
6781 /*
6782 * Use current "previous" pointer...
6783 */
6784
6785 prev = ipp->prev;
6786 }
6787 else
6788 {
6789 /*
6790 * Find this attribute in the linked list...
6791 */
6792
6793 for (prev = NULL, current = ipp->attrs;
6794 current && current != *attr;
6795 prev = current, current = current->next);
6796
6797 if (!current)
6798 {
6799 /*
6800 * This is a serious error!
6801 */
6802
6803 *attr = temp;
6804 _cupsSetError(IPP_STATUS_ERROR_INTERNAL,
6805 _("IPP attribute is not a member of the message."), 1);
6806 DEBUG_puts("4ipp_set_value: Unable to find attribute in message.");
6807 return (NULL);
6808 }
6809 }
6810
6811 if (prev)
6812 prev->next = temp;
6813 else
6814 ipp->attrs = temp;
6815
6816 ipp->current = temp;
6817 ipp->prev = prev;
6818
6819 if (ipp->last == *attr)
6820 ipp->last = temp;
6821
6822 *attr = temp;
6823 }
6824
6825 /*
6826 * Return the value element...
6827 */
6828
6829 if (element >= temp->num_values)
6830 temp->num_values = element + 1;
6831
6832 return (temp->values + element);
6833 }
6834
6835
6836 /*
6837 * 'ipp_write_file()' - Write IPP data to a file.
6838 */
6839
6840 static ssize_t /* O - Number of bytes written */
6841 ipp_write_file(int *fd, /* I - File descriptor */
6842 ipp_uchar_t *buffer, /* I - Data to write */
6843 size_t length) /* I - Number of bytes to write */
6844 {
6845 #ifdef _WIN32
6846 return ((ssize_t)write(*fd, buffer, (unsigned)length));
6847 #else
6848 return (write(*fd, buffer, length));
6849 #endif /* _WIN32 */
6850 }