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