]> git.ipfire.org Git - thirdparty/cups.git/blob - cups/ipp.c
Block invalid group tags when parsing IPP messages.
[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_ZERO || (tag == IPP_TAG_OPERATION && ipp->curtag != IPP_TAG_ZERO))
3088 {
3089 _cupsSetError(IPP_STATUS_ERROR_INTERNAL, _("Invalid group tag."), 1);
3090 DEBUG_printf(("1ippReadIO: bad tag 0x%02x.", tag));
3091 _cupsBufferRelease((char *)buffer);
3092 return (IPP_STATE_ERROR);
3093 }
3094 else if (tag < IPP_TAG_UNSUPPORTED_VALUE)
3095 {
3096 /*
3097 * Group tag... Set the current group and continue...
3098 */
3099
3100 if (ipp->curtag == tag)
3101 ipp->prev = ippAddSeparator(ipp);
3102 else if (ipp->current)
3103 ipp->prev = ipp->current;
3104
3105 ipp->curtag = tag;
3106 ipp->current = NULL;
3107 DEBUG_printf(("2ippReadIO: group tag=%x(%s), ipp->prev=%p", tag, ippTagString(tag), (void *)ipp->prev));
3108 continue;
3109 }
3110
3111 DEBUG_printf(("2ippReadIO: value tag=%x(%s)", tag,
3112 ippTagString(tag)));
3113
3114 /*
3115 * Get the name...
3116 */
3117
3118 if ((*cb)(src, buffer, 2) < 2)
3119 {
3120 DEBUG_puts("1ippReadIO: unable to read name length.");
3121 _cupsBufferRelease((char *)buffer);
3122 return (IPP_STATE_ERROR);
3123 }
3124
3125 n = (buffer[0] << 8) | buffer[1];
3126
3127 if (n >= IPP_BUF_SIZE)
3128 {
3129 _cupsSetError(IPP_STATUS_ERROR_INTERNAL, _("IPP name larger than 32767 bytes."), 1);
3130 DEBUG_printf(("1ippReadIO: bad name length %d.", n));
3131 _cupsBufferRelease((char *)buffer);
3132 return (IPP_STATE_ERROR);
3133 }
3134
3135 DEBUG_printf(("2ippReadIO: name length=%d", n));
3136
3137 if (n == 0 && tag != IPP_TAG_MEMBERNAME &&
3138 tag != IPP_TAG_END_COLLECTION)
3139 {
3140 /*
3141 * More values for current attribute...
3142 */
3143
3144 if (ipp->current == NULL)
3145 {
3146 _cupsSetError(IPP_STATUS_ERROR_INTERNAL, _("IPP attribute has no name."), 1);
3147 DEBUG_puts("1ippReadIO: Attribute without name and no current.");
3148 _cupsBufferRelease((char *)buffer);
3149 return (IPP_STATE_ERROR);
3150 }
3151
3152 attr = ipp->current;
3153 value_tag = (ipp_tag_t)(attr->value_tag & IPP_TAG_CUPS_MASK);
3154
3155 /*
3156 * Make sure we aren't adding a new value of a different
3157 * type...
3158 */
3159
3160 if (value_tag == IPP_TAG_ZERO)
3161 {
3162 /*
3163 * Setting the value of a collection member...
3164 */
3165
3166 attr->value_tag = tag;
3167 }
3168 else if (value_tag == IPP_TAG_TEXTLANG ||
3169 value_tag == IPP_TAG_NAMELANG ||
3170 (value_tag >= IPP_TAG_TEXT &&
3171 value_tag <= IPP_TAG_MIMETYPE))
3172 {
3173 /*
3174 * String values can sometimes come across in different
3175 * forms; accept sets of differing values...
3176 */
3177
3178 if (tag != IPP_TAG_TEXTLANG && tag != IPP_TAG_NAMELANG &&
3179 (tag < IPP_TAG_TEXT || tag > IPP_TAG_MIMETYPE) &&
3180 tag != IPP_TAG_NOVALUE)
3181 {
3182 _cupsSetError(IPP_STATUS_ERROR_INTERNAL,
3183 _("IPP 1setOf attribute with incompatible value "
3184 "tags."), 1);
3185 DEBUG_printf(("1ippReadIO: 1setOf value tag %x(%s) != %x(%s)",
3186 value_tag, ippTagString(value_tag), tag,
3187 ippTagString(tag)));
3188 _cupsBufferRelease((char *)buffer);
3189 return (IPP_STATE_ERROR);
3190 }
3191
3192 if (value_tag != tag)
3193 {
3194 DEBUG_printf(("1ippReadIO: Converting %s attribute from %s to %s.",
3195 attr->name, ippTagString(value_tag), ippTagString(tag)));
3196 ippSetValueTag(ipp, &attr, tag);
3197 }
3198 }
3199 else if (value_tag == IPP_TAG_INTEGER ||
3200 value_tag == IPP_TAG_RANGE)
3201 {
3202 /*
3203 * Integer and rangeOfInteger values can sometimes be mixed; accept
3204 * sets of differing values...
3205 */
3206
3207 if (tag != IPP_TAG_INTEGER && tag != IPP_TAG_RANGE)
3208 {
3209 _cupsSetError(IPP_STATUS_ERROR_INTERNAL,
3210 _("IPP 1setOf attribute with incompatible value "
3211 "tags."), 1);
3212 DEBUG_printf(("1ippReadIO: 1setOf value tag %x(%s) != %x(%s)",
3213 value_tag, ippTagString(value_tag), tag,
3214 ippTagString(tag)));
3215 _cupsBufferRelease((char *)buffer);
3216 return (IPP_STATE_ERROR);
3217 }
3218
3219 if (value_tag == IPP_TAG_INTEGER && tag == IPP_TAG_RANGE)
3220 {
3221 /*
3222 * Convert integer values to rangeOfInteger values...
3223 */
3224
3225 DEBUG_printf(("1ippReadIO: Converting %s attribute to "
3226 "rangeOfInteger.", attr->name));
3227 ippSetValueTag(ipp, &attr, IPP_TAG_RANGE);
3228 }
3229 }
3230 else if (value_tag != tag)
3231 {
3232 _cupsSetError(IPP_STATUS_ERROR_INTERNAL,
3233 _("IPP 1setOf attribute with incompatible value "
3234 "tags."), 1);
3235 DEBUG_printf(("1ippReadIO: value tag %x(%s) != %x(%s)",
3236 value_tag, ippTagString(value_tag), tag,
3237 ippTagString(tag)));
3238 _cupsBufferRelease((char *)buffer);
3239 return (IPP_STATE_ERROR);
3240 }
3241
3242 /*
3243 * Finally, reallocate the attribute array as needed...
3244 */
3245
3246 if ((value = ipp_set_value(ipp, &attr, attr->num_values)) == NULL)
3247 {
3248 _cupsBufferRelease((char *)buffer);
3249 return (IPP_STATE_ERROR);
3250 }
3251 }
3252 else if (tag == IPP_TAG_MEMBERNAME)
3253 {
3254 /*
3255 * Name must be length 0!
3256 */
3257
3258 if (n)
3259 {
3260 _cupsSetError(IPP_STATUS_ERROR_INTERNAL, _("IPP member name is not empty."), 1);
3261 DEBUG_puts("1ippReadIO: member name not empty.");
3262 _cupsBufferRelease((char *)buffer);
3263 return (IPP_STATE_ERROR);
3264 }
3265
3266 if (ipp->current)
3267 ipp->prev = ipp->current;
3268
3269 attr = ipp->current = ipp_add_attr(ipp, NULL, ipp->curtag, IPP_TAG_ZERO, 1);
3270 if (!attr)
3271 {
3272 _cupsSetHTTPError(HTTP_STATUS_ERROR);
3273 DEBUG_puts("1ippReadIO: unable to allocate attribute.");
3274 _cupsBufferRelease((char *)buffer);
3275 return (IPP_STATE_ERROR);
3276 }
3277
3278 DEBUG_printf(("2ippReadIO: membername, ipp->current=%p, ipp->prev=%p", (void *)ipp->current, (void *)ipp->prev));
3279
3280 value = attr->values;
3281 }
3282 else if (tag != IPP_TAG_END_COLLECTION)
3283 {
3284 /*
3285 * New attribute; read the name and add it...
3286 */
3287
3288 if ((*cb)(src, buffer, (size_t)n) < n)
3289 {
3290 DEBUG_puts("1ippReadIO: unable to read name.");
3291 _cupsBufferRelease((char *)buffer);
3292 return (IPP_STATE_ERROR);
3293 }
3294
3295 buffer[n] = '\0';
3296
3297 if (ipp->current)
3298 ipp->prev = ipp->current;
3299
3300 if ((attr = ipp->current = ipp_add_attr(ipp, (char *)buffer, ipp->curtag, tag,
3301 1)) == NULL)
3302 {
3303 _cupsSetHTTPError(HTTP_STATUS_ERROR);
3304 DEBUG_puts("1ippReadIO: unable to allocate attribute.");
3305 _cupsBufferRelease((char *)buffer);
3306 return (IPP_STATE_ERROR);
3307 }
3308
3309 DEBUG_printf(("2ippReadIO: name=\"%s\", ipp->current=%p, ipp->prev=%p", buffer, (void *)ipp->current, (void *)ipp->prev));
3310
3311 value = attr->values;
3312 }
3313 else
3314 {
3315 attr = NULL;
3316 value = NULL;
3317 }
3318
3319 if ((*cb)(src, buffer, 2) < 2)
3320 {
3321 DEBUG_puts("1ippReadIO: unable to read value length.");
3322 _cupsBufferRelease((char *)buffer);
3323 return (IPP_STATE_ERROR);
3324 }
3325
3326 n = (buffer[0] << 8) | buffer[1];
3327 DEBUG_printf(("2ippReadIO: value length=%d", n));
3328
3329 if (n >= IPP_BUF_SIZE)
3330 {
3331 _cupsSetError(IPP_STATUS_ERROR_INTERNAL,
3332 _("IPP value larger than 32767 bytes."), 1);
3333 DEBUG_printf(("1ippReadIO: bad value length %d.", n));
3334 _cupsBufferRelease((char *)buffer);
3335 return (IPP_STATE_ERROR);
3336 }
3337
3338 switch (tag)
3339 {
3340 case IPP_TAG_INTEGER :
3341 case IPP_TAG_ENUM :
3342 if (n != 4)
3343 {
3344 if (tag == IPP_TAG_INTEGER)
3345 _cupsSetError(IPP_STATUS_ERROR_INTERNAL,
3346 _("IPP integer value not 4 bytes."), 1);
3347 else
3348 _cupsSetError(IPP_STATUS_ERROR_INTERNAL,
3349 _("IPP enum value not 4 bytes."), 1);
3350 DEBUG_printf(("1ippReadIO: bad integer value length %d.", n));
3351 _cupsBufferRelease((char *)buffer);
3352 return (IPP_STATE_ERROR);
3353 }
3354
3355 if ((*cb)(src, buffer, 4) < 4)
3356 {
3357 DEBUG_puts("1ippReadIO: Unable to read integer value.");
3358 _cupsBufferRelease((char *)buffer);
3359 return (IPP_STATE_ERROR);
3360 }
3361
3362 n = (((((buffer[0] << 8) | buffer[1]) << 8) | buffer[2]) << 8) |
3363 buffer[3];
3364
3365 if (attr->value_tag == IPP_TAG_RANGE)
3366 value->range.lower = value->range.upper = n;
3367 else
3368 value->integer = n;
3369 break;
3370
3371 case IPP_TAG_BOOLEAN :
3372 if (n != 1)
3373 {
3374 _cupsSetError(IPP_STATUS_ERROR_INTERNAL, _("IPP boolean value not 1 byte."),
3375 1);
3376 DEBUG_printf(("1ippReadIO: bad boolean value length %d.", n));
3377 _cupsBufferRelease((char *)buffer);
3378 return (IPP_STATE_ERROR);
3379 }
3380
3381 if ((*cb)(src, buffer, 1) < 1)
3382 {
3383 DEBUG_puts("1ippReadIO: Unable to read boolean value.");
3384 _cupsBufferRelease((char *)buffer);
3385 return (IPP_STATE_ERROR);
3386 }
3387
3388 value->boolean = (char)buffer[0];
3389 break;
3390
3391 case IPP_TAG_NOVALUE :
3392 case IPP_TAG_NOTSETTABLE :
3393 case IPP_TAG_DELETEATTR :
3394 case IPP_TAG_ADMINDEFINE :
3395 /*
3396 * These value types are not supposed to have values, however
3397 * some vendors (Brother) do not implement IPP correctly and so
3398 * we need to map non-empty values to text...
3399 */
3400
3401 if (attr->value_tag == tag)
3402 {
3403 if (n == 0)
3404 break;
3405
3406 attr->value_tag = IPP_TAG_TEXT;
3407 }
3408
3409 case IPP_TAG_TEXT :
3410 case IPP_TAG_NAME :
3411 case IPP_TAG_KEYWORD :
3412 case IPP_TAG_URI :
3413 case IPP_TAG_URISCHEME :
3414 case IPP_TAG_CHARSET :
3415 case IPP_TAG_LANGUAGE :
3416 case IPP_TAG_MIMETYPE :
3417 if (n > 0)
3418 {
3419 if ((*cb)(src, buffer, (size_t)n) < n)
3420 {
3421 DEBUG_puts("1ippReadIO: unable to read string value.");
3422 _cupsBufferRelease((char *)buffer);
3423 return (IPP_STATE_ERROR);
3424 }
3425 }
3426
3427 buffer[n] = '\0';
3428 value->string.text = _cupsStrAlloc((char *)buffer);
3429 DEBUG_printf(("2ippReadIO: value=\"%s\"", value->string.text));
3430 break;
3431
3432 case IPP_TAG_DATE :
3433 if (n != 11)
3434 {
3435 _cupsSetError(IPP_STATUS_ERROR_INTERNAL, _("IPP date value not 11 bytes."), 1);
3436 DEBUG_printf(("1ippReadIO: bad date value length %d.", n));
3437 _cupsBufferRelease((char *)buffer);
3438 return (IPP_STATE_ERROR);
3439 }
3440
3441 if ((*cb)(src, value->date, 11) < 11)
3442 {
3443 DEBUG_puts("1ippReadIO: Unable to read date value.");
3444 _cupsBufferRelease((char *)buffer);
3445 return (IPP_STATE_ERROR);
3446 }
3447 break;
3448
3449 case IPP_TAG_RESOLUTION :
3450 if (n != 9)
3451 {
3452 _cupsSetError(IPP_STATUS_ERROR_INTERNAL,
3453 _("IPP resolution value not 9 bytes."), 1);
3454 DEBUG_printf(("1ippReadIO: bad resolution value length %d.", n));
3455 _cupsBufferRelease((char *)buffer);
3456 return (IPP_STATE_ERROR);
3457 }
3458
3459 if ((*cb)(src, buffer, 9) < 9)
3460 {
3461 DEBUG_puts("1ippReadIO: Unable to read resolution value.");
3462 _cupsBufferRelease((char *)buffer);
3463 return (IPP_STATE_ERROR);
3464 }
3465
3466 value->resolution.xres =
3467 (((((buffer[0] << 8) | buffer[1]) << 8) | buffer[2]) << 8) |
3468 buffer[3];
3469 value->resolution.yres =
3470 (((((buffer[4] << 8) | buffer[5]) << 8) | buffer[6]) << 8) |
3471 buffer[7];
3472 value->resolution.units =
3473 (ipp_res_t)buffer[8];
3474 break;
3475
3476 case IPP_TAG_RANGE :
3477 if (n != 8)
3478 {
3479 _cupsSetError(IPP_STATUS_ERROR_INTERNAL,
3480 _("IPP rangeOfInteger value not 8 bytes."), 1);
3481 DEBUG_printf(("1ippReadIO: bad rangeOfInteger value length "
3482 "%d.", n));
3483 _cupsBufferRelease((char *)buffer);
3484 return (IPP_STATE_ERROR);
3485 }
3486
3487 if ((*cb)(src, buffer, 8) < 8)
3488 {
3489 DEBUG_puts("1ippReadIO: Unable to read range value.");
3490 _cupsBufferRelease((char *)buffer);
3491 return (IPP_STATE_ERROR);
3492 }
3493
3494 value->range.lower =
3495 (((((buffer[0] << 8) | buffer[1]) << 8) | buffer[2]) << 8) |
3496 buffer[3];
3497 value->range.upper =
3498 (((((buffer[4] << 8) | buffer[5]) << 8) | buffer[6]) << 8) |
3499 buffer[7];
3500 break;
3501
3502 case IPP_TAG_TEXTLANG :
3503 case IPP_TAG_NAMELANG :
3504 if (n < 4)
3505 {
3506 if (tag == IPP_TAG_TEXTLANG)
3507 _cupsSetError(IPP_STATUS_ERROR_INTERNAL,
3508 _("IPP textWithLanguage value less than "
3509 "minimum 4 bytes."), 1);
3510 else
3511 _cupsSetError(IPP_STATUS_ERROR_INTERNAL,
3512 _("IPP nameWithLanguage value less than "
3513 "minimum 4 bytes."), 1);
3514 DEBUG_printf(("1ippReadIO: bad stringWithLanguage value "
3515 "length %d.", n));
3516 _cupsBufferRelease((char *)buffer);
3517 return (IPP_STATE_ERROR);
3518 }
3519
3520 if ((*cb)(src, buffer, (size_t)n) < n)
3521 {
3522 DEBUG_puts("1ippReadIO: Unable to read string w/language "
3523 "value.");
3524 _cupsBufferRelease((char *)buffer);
3525 return (IPP_STATE_ERROR);
3526 }
3527
3528 bufptr = buffer;
3529
3530 /*
3531 * text-with-language and name-with-language are composite
3532 * values:
3533 *
3534 * language-length
3535 * language
3536 * text-length
3537 * text
3538 */
3539
3540 n = (bufptr[0] << 8) | bufptr[1];
3541
3542 if ((bufptr + 2 + n) >= (buffer + IPP_BUF_SIZE) || n >= (int)sizeof(string))
3543 {
3544 _cupsSetError(IPP_STATUS_ERROR_INTERNAL,
3545 _("IPP language length overflows value."), 1);
3546 DEBUG_printf(("1ippReadIO: bad language value length %d.",
3547 n));
3548 _cupsBufferRelease((char *)buffer);
3549 return (IPP_STATE_ERROR);
3550 }
3551 else if (n >= IPP_MAX_LANGUAGE)
3552 {
3553 _cupsSetError(IPP_STATUS_ERROR_INTERNAL,
3554 _("IPP language length too large."), 1);
3555 DEBUG_printf(("1ippReadIO: bad language value length %d.",
3556 n));
3557 _cupsBufferRelease((char *)buffer);
3558 return (IPP_STATE_ERROR);
3559 }
3560
3561 memcpy(string, bufptr + 2, (size_t)n);
3562 string[n] = '\0';
3563
3564 value->string.language = _cupsStrAlloc((char *)string);
3565
3566 bufptr += 2 + n;
3567 n = (bufptr[0] << 8) | bufptr[1];
3568
3569 if ((bufptr + 2 + n) >= (buffer + IPP_BUF_SIZE))
3570 {
3571 _cupsSetError(IPP_STATUS_ERROR_INTERNAL,
3572 _("IPP string length overflows value."), 1);
3573 DEBUG_printf(("1ippReadIO: bad string value length %d.", n));
3574 _cupsBufferRelease((char *)buffer);
3575 return (IPP_STATE_ERROR);
3576 }
3577
3578 bufptr[2 + n] = '\0';
3579 value->string.text = _cupsStrAlloc((char *)bufptr + 2);
3580 break;
3581
3582 case IPP_TAG_BEGIN_COLLECTION :
3583 /*
3584 * Oh, boy, here comes a collection value, so read it...
3585 */
3586
3587 value->collection = ippNew();
3588
3589 if (n > 0)
3590 {
3591 _cupsSetError(IPP_STATUS_ERROR_INTERNAL,
3592 _("IPP begCollection value not 0 bytes."), 1);
3593 DEBUG_puts("1ippReadIO: begCollection tag with value length "
3594 "> 0.");
3595 _cupsBufferRelease((char *)buffer);
3596 return (IPP_STATE_ERROR);
3597 }
3598
3599 if (ippReadIO(src, cb, 1, ipp, value->collection) == IPP_STATE_ERROR)
3600 {
3601 DEBUG_puts("1ippReadIO: Unable to read collection value.");
3602 _cupsBufferRelease((char *)buffer);
3603 return (IPP_STATE_ERROR);
3604 }
3605 break;
3606
3607 case IPP_TAG_END_COLLECTION :
3608 _cupsBufferRelease((char *)buffer);
3609
3610 if (n > 0)
3611 {
3612 _cupsSetError(IPP_STATUS_ERROR_INTERNAL,
3613 _("IPP endCollection value not 0 bytes."), 1);
3614 DEBUG_puts("1ippReadIO: endCollection tag with value length "
3615 "> 0.");
3616 return (IPP_STATE_ERROR);
3617 }
3618
3619 DEBUG_puts("1ippReadIO: endCollection tag...");
3620 return (ipp->state = IPP_STATE_DATA);
3621
3622 case IPP_TAG_MEMBERNAME :
3623 /*
3624 * The value the name of the member in the collection, which
3625 * we need to carry over...
3626 */
3627
3628 if (!attr)
3629 {
3630 _cupsSetError(IPP_STATUS_ERROR_INTERNAL,
3631 _("IPP memberName with no attribute."), 1);
3632 DEBUG_puts("1ippReadIO: Member name without attribute.");
3633 _cupsBufferRelease((char *)buffer);
3634 return (IPP_STATE_ERROR);
3635 }
3636 else if (n == 0)
3637 {
3638 _cupsSetError(IPP_STATUS_ERROR_INTERNAL,
3639 _("IPP memberName value is empty."), 1);
3640 DEBUG_puts("1ippReadIO: Empty member name value.");
3641 _cupsBufferRelease((char *)buffer);
3642 return (IPP_STATE_ERROR);
3643 }
3644 else if ((*cb)(src, buffer, (size_t)n) < n)
3645 {
3646 DEBUG_puts("1ippReadIO: Unable to read member name value.");
3647 _cupsBufferRelease((char *)buffer);
3648 return (IPP_STATE_ERROR);
3649 }
3650
3651 buffer[n] = '\0';
3652 attr->name = _cupsStrAlloc((char *)buffer);
3653
3654 /*
3655 * Since collection members are encoded differently than
3656 * regular attributes, make sure we don't start with an
3657 * empty value...
3658 */
3659
3660 attr->num_values --;
3661
3662 DEBUG_printf(("2ippReadIO: member name=\"%s\"", attr->name));
3663 break;
3664
3665 default : /* Other unsupported values */
3666 if (tag == IPP_TAG_STRING && n > IPP_MAX_LENGTH)
3667 {
3668 _cupsSetError(IPP_STATUS_ERROR_INTERNAL,
3669 _("IPP octetString length too large."), 1);
3670 DEBUG_printf(("1ippReadIO: bad octetString value length %d.",
3671 n));
3672 _cupsBufferRelease((char *)buffer);
3673 return (IPP_STATE_ERROR);
3674 }
3675
3676 value->unknown.length = n;
3677
3678 if (n > 0)
3679 {
3680 if ((value->unknown.data = malloc((size_t)n)) == NULL)
3681 {
3682 _cupsSetHTTPError(HTTP_STATUS_ERROR);
3683 DEBUG_puts("1ippReadIO: Unable to allocate value");
3684 _cupsBufferRelease((char *)buffer);
3685 return (IPP_STATE_ERROR);
3686 }
3687
3688 if ((*cb)(src, value->unknown.data, (size_t)n) < n)
3689 {
3690 DEBUG_puts("1ippReadIO: Unable to read unsupported value.");
3691 _cupsBufferRelease((char *)buffer);
3692 return (IPP_STATE_ERROR);
3693 }
3694 }
3695 else
3696 value->unknown.data = NULL;
3697 break;
3698 }
3699
3700 /*
3701 * If blocking is disabled, stop here...
3702 */
3703
3704 if (!blocking)
3705 break;
3706 }
3707 break;
3708
3709 case IPP_STATE_DATA :
3710 break;
3711
3712 default :
3713 break; /* anti-compiler-warning-code */
3714 }
3715
3716 DEBUG_printf(("1ippReadIO: returning ipp->state=%d.", ipp->state));
3717 _cupsBufferRelease((char *)buffer);
3718
3719 return (ipp->state);
3720 }
3721
3722
3723 /*
3724 * 'ippSetBoolean()' - Set a boolean value in an attribute.
3725 *
3726 * The @code ipp@ parameter refers to an IPP message previously created using
3727 * the @link ippNew@, @link ippNewRequest@, or @link ippNewResponse@ functions.
3728 *
3729 * The @code attr@ parameter may be modified as a result of setting the value.
3730 *
3731 * The @code element@ parameter specifies which value to set from 0 to
3732 * @code ippGetCount(attr)@.
3733 *
3734 * @since CUPS 1.6/macOS 10.8@
3735 */
3736
3737 int /* O - 1 on success, 0 on failure */
3738 ippSetBoolean(ipp_t *ipp, /* I - IPP message */
3739 ipp_attribute_t **attr, /* IO - IPP attribute */
3740 int element, /* I - Value number (0-based) */
3741 int boolvalue)/* I - Boolean value */
3742 {
3743 _ipp_value_t *value; /* Current value */
3744
3745
3746 /*
3747 * Range check input...
3748 */
3749
3750 if (!ipp || !attr || !*attr || (*attr)->value_tag != IPP_TAG_BOOLEAN ||
3751 element < 0 || element > (*attr)->num_values)
3752 return (0);
3753
3754 /*
3755 * Set the value and return...
3756 */
3757
3758 if ((value = ipp_set_value(ipp, attr, element)) != NULL)
3759 value->boolean = (char)boolvalue;
3760
3761 return (value != NULL);
3762 }
3763
3764
3765 /*
3766 * 'ippSetCollection()' - Set a collection value in an attribute.
3767 *
3768 * The @code ipp@ parameter refers to an IPP message previously created using
3769 * the @link ippNew@, @link ippNewRequest@, or @link ippNewResponse@ functions.
3770 *
3771 * The @code attr@ parameter may be modified as a result of setting the value.
3772 *
3773 * The @code element@ parameter specifies which value to set from 0 to
3774 * @code ippGetCount(attr)@.
3775 *
3776 * @since CUPS 1.6/macOS 10.8@
3777 */
3778
3779 int /* O - 1 on success, 0 on failure */
3780 ippSetCollection(
3781 ipp_t *ipp, /* I - IPP message */
3782 ipp_attribute_t **attr, /* IO - IPP attribute */
3783 int element, /* I - Value number (0-based) */
3784 ipp_t *colvalue) /* I - Collection value */
3785 {
3786 _ipp_value_t *value; /* Current value */
3787
3788
3789 /*
3790 * Range check input...
3791 */
3792
3793 if (!ipp || !attr || !*attr || (*attr)->value_tag != IPP_TAG_BEGIN_COLLECTION ||
3794 element < 0 || element > (*attr)->num_values || !colvalue)
3795 return (0);
3796
3797 /*
3798 * Set the value and return...
3799 */
3800
3801 if ((value = ipp_set_value(ipp, attr, element)) != NULL)
3802 {
3803 if (value->collection)
3804 ippDelete(value->collection);
3805
3806 value->collection = colvalue;
3807 colvalue->use ++;
3808 }
3809
3810 return (value != NULL);
3811 }
3812
3813
3814 /*
3815 * 'ippSetDate()' - Set a dateTime value in an attribute.
3816 *
3817 * The @code ipp@ parameter refers to an IPP message previously created using
3818 * the @link ippNew@, @link ippNewRequest@, or @link ippNewResponse@ functions.
3819 *
3820 * The @code attr@ parameter may be modified as a result of setting the value.
3821 *
3822 * The @code element@ parameter specifies which value to set from 0 to
3823 * @code ippGetCount(attr)@.
3824 *
3825 * @since CUPS 1.6/macOS 10.8@
3826 */
3827
3828 int /* O - 1 on success, 0 on failure */
3829 ippSetDate(ipp_t *ipp, /* I - IPP message */
3830 ipp_attribute_t **attr, /* IO - IPP attribute */
3831 int element, /* I - Value number (0-based) */
3832 const ipp_uchar_t *datevalue)/* I - dateTime value */
3833 {
3834 _ipp_value_t *value; /* Current value */
3835
3836
3837 /*
3838 * Range check input...
3839 */
3840
3841 if (!ipp || !attr || !*attr || (*attr)->value_tag != IPP_TAG_DATE ||
3842 element < 0 || element > (*attr)->num_values || !datevalue)
3843 return (0);
3844
3845 /*
3846 * Set the value and return...
3847 */
3848
3849 if ((value = ipp_set_value(ipp, attr, element)) != NULL)
3850 memcpy(value->date, datevalue, sizeof(value->date));
3851
3852 return (value != NULL);
3853 }
3854
3855
3856 /*
3857 * 'ippSetGroupTag()' - Set the group tag of an attribute.
3858 *
3859 * The @code ipp@ parameter refers to an IPP message previously created using
3860 * the @link ippNew@, @link ippNewRequest@, or @link ippNewResponse@ functions.
3861 *
3862 * The @code attr@ parameter may be modified as a result of setting the value.
3863 *
3864 * The @code group@ parameter specifies the IPP attribute group tag: none
3865 * (@code IPP_TAG_ZERO@, for member attributes), document (@code IPP_TAG_DOCUMENT@),
3866 * event notification (@code IPP_TAG_EVENT_NOTIFICATION@), operation
3867 * (@code IPP_TAG_OPERATION@), printer (@code IPP_TAG_PRINTER@), subscription
3868 * (@code IPP_TAG_SUBSCRIPTION@), or unsupported (@code IPP_TAG_UNSUPPORTED_GROUP@).
3869 *
3870 * @since CUPS 1.6/macOS 10.8@
3871 */
3872
3873 int /* O - 1 on success, 0 on failure */
3874 ippSetGroupTag(
3875 ipp_t *ipp, /* I - IPP message */
3876 ipp_attribute_t **attr, /* IO - Attribute */
3877 ipp_tag_t group_tag) /* I - Group tag */
3878 {
3879 /*
3880 * Range check input - group tag must be 0x01 to 0x0F, per RFC 8011...
3881 */
3882
3883 if (!ipp || !attr || !*attr ||
3884 group_tag < IPP_TAG_ZERO || group_tag == IPP_TAG_END ||
3885 group_tag >= IPP_TAG_UNSUPPORTED_VALUE)
3886 return (0);
3887
3888 /*
3889 * Set the group tag and return...
3890 */
3891
3892 (*attr)->group_tag = group_tag;
3893
3894 return (1);
3895 }
3896
3897
3898 /*
3899 * 'ippSetInteger()' - Set an integer or enum value in an attribute.
3900 *
3901 * The @code ipp@ parameter refers to an IPP message previously created using
3902 * the @link ippNew@, @link ippNewRequest@, or @link ippNewResponse@ functions.
3903 *
3904 * The @code attr@ parameter may be modified as a result of setting the value.
3905 *
3906 * The @code element@ parameter specifies which value to set from 0 to
3907 * @code ippGetCount(attr)@.
3908 *
3909 * @since CUPS 1.6/macOS 10.8@
3910 */
3911
3912 int /* O - 1 on success, 0 on failure */
3913 ippSetInteger(ipp_t *ipp, /* I - IPP message */
3914 ipp_attribute_t **attr, /* IO - IPP attribute */
3915 int element, /* I - Value number (0-based) */
3916 int intvalue) /* I - Integer/enum value */
3917 {
3918 _ipp_value_t *value; /* Current value */
3919
3920
3921 /*
3922 * Range check input...
3923 */
3924
3925 if (!ipp || !attr || !*attr ||
3926 ((*attr)->value_tag != IPP_TAG_INTEGER && (*attr)->value_tag != IPP_TAG_ENUM) ||
3927 element < 0 || element > (*attr)->num_values)
3928 return (0);
3929
3930 /*
3931 * Set the value and return...
3932 */
3933
3934 if ((value = ipp_set_value(ipp, attr, element)) != NULL)
3935 value->integer = intvalue;
3936
3937 return (value != NULL);
3938 }
3939
3940
3941 /*
3942 * 'ippSetName()' - Set the name of an attribute.
3943 *
3944 * The @code ipp@ parameter refers to an IPP message previously created using
3945 * the @link ippNew@, @link ippNewRequest@, or @link ippNewResponse@ functions.
3946 *
3947 * The @code attr@ parameter may be modified as a result of setting the value.
3948 *
3949 * @since CUPS 1.6/macOS 10.8@
3950 */
3951
3952 int /* O - 1 on success, 0 on failure */
3953 ippSetName(ipp_t *ipp, /* I - IPP message */
3954 ipp_attribute_t **attr, /* IO - IPP attribute */
3955 const char *name) /* I - Attribute name */
3956 {
3957 char *temp; /* Temporary name value */
3958
3959
3960 /*
3961 * Range check input...
3962 */
3963
3964 if (!ipp || !attr || !*attr)
3965 return (0);
3966
3967 /*
3968 * Set the value and return...
3969 */
3970
3971 if ((temp = _cupsStrAlloc(name)) != NULL)
3972 {
3973 if ((*attr)->name)
3974 _cupsStrFree((*attr)->name);
3975
3976 (*attr)->name = temp;
3977 }
3978
3979 return (temp != NULL);
3980 }
3981
3982
3983 /*
3984 * 'ippSetOctetString()' - Set an octetString value in an IPP attribute.
3985 *
3986 * The @code ipp@ parameter refers to an IPP message previously created using
3987 * the @link ippNew@, @link ippNewRequest@, or @link ippNewResponse@ functions.
3988 *
3989 * The @code attr@ parameter may be modified as a result of setting the value.
3990 *
3991 * The @code element@ parameter specifies which value to set from 0 to
3992 * @code ippGetCount(attr)@.
3993 *
3994 * @since CUPS 1.7/macOS 10.9@
3995 */
3996
3997 int /* O - 1 on success, 0 on failure */
3998 ippSetOctetString(
3999 ipp_t *ipp, /* I - IPP message */
4000 ipp_attribute_t **attr, /* IO - IPP attribute */
4001 int element, /* I - Value number (0-based) */
4002 const void *data, /* I - Pointer to octetString data */
4003 int datalen) /* I - Length of octetString data */
4004 {
4005 _ipp_value_t *value; /* Current value */
4006
4007
4008 /*
4009 * Range check input...
4010 */
4011
4012 if (!ipp || !attr || !*attr || (*attr)->value_tag != IPP_TAG_STRING ||
4013 element < 0 || element > (*attr)->num_values ||
4014 datalen < 0 || datalen > IPP_MAX_LENGTH)
4015 return (0);
4016
4017 /*
4018 * Set the value and return...
4019 */
4020
4021 if ((value = ipp_set_value(ipp, attr, element)) != NULL)
4022 {
4023 if ((int)((*attr)->value_tag) & IPP_TAG_CUPS_CONST)
4024 {
4025 /*
4026 * Just copy the pointer...
4027 */
4028
4029 value->unknown.data = (void *)data;
4030 value->unknown.length = datalen;
4031 }
4032 else
4033 {
4034 /*
4035 * Copy the data...
4036 */
4037
4038 if (value->unknown.data)
4039 {
4040 /*
4041 * Free previous data...
4042 */
4043
4044 free(value->unknown.data);
4045
4046 value->unknown.data = NULL;
4047 value->unknown.length = 0;
4048 }
4049
4050 if (datalen > 0)
4051 {
4052 void *temp; /* Temporary data pointer */
4053
4054 if ((temp = malloc((size_t)datalen)) != NULL)
4055 {
4056 memcpy(temp, data, (size_t)datalen);
4057
4058 value->unknown.data = temp;
4059 value->unknown.length = datalen;
4060 }
4061 else
4062 return (0);
4063 }
4064 }
4065 }
4066
4067 return (value != NULL);
4068 }
4069
4070
4071 /*
4072 * 'ippSetOperation()' - Set the operation ID in an IPP request message.
4073 *
4074 * The @code ipp@ parameter refers to an IPP message previously created using
4075 * the @link ippNew@, @link ippNewRequest@, or @link ippNewResponse@ functions.
4076 *
4077 * @since CUPS 1.6/macOS 10.8@
4078 */
4079
4080 int /* O - 1 on success, 0 on failure */
4081 ippSetOperation(ipp_t *ipp, /* I - IPP request message */
4082 ipp_op_t op) /* I - Operation ID */
4083 {
4084 /*
4085 * Range check input...
4086 */
4087
4088 if (!ipp)
4089 return (0);
4090
4091 /*
4092 * Set the operation and return...
4093 */
4094
4095 ipp->request.op.operation_id = op;
4096
4097 return (1);
4098 }
4099
4100
4101 /*
4102 * 'ippSetRange()' - Set a rangeOfInteger value in an attribute.
4103 *
4104 * The @code ipp@ parameter refers to an IPP message previously created using
4105 * the @link ippNew@, @link ippNewRequest@, or @link ippNewResponse@ functions.
4106 *
4107 * The @code attr@ parameter may be modified as a result of setting the value.
4108 *
4109 * The @code element@ parameter specifies which value to set from 0 to
4110 * @code ippGetCount(attr)@.
4111 *
4112 * @since CUPS 1.6/macOS 10.8@
4113 */
4114
4115 int /* O - 1 on success, 0 on failure */
4116 ippSetRange(ipp_t *ipp, /* I - IPP message */
4117 ipp_attribute_t **attr, /* IO - IPP attribute */
4118 int element, /* I - Value number (0-based) */
4119 int lowervalue, /* I - Lower bound for range */
4120 int uppervalue) /* I - Upper bound for range */
4121 {
4122 _ipp_value_t *value; /* Current value */
4123
4124
4125 /*
4126 * Range check input...
4127 */
4128
4129 if (!ipp || !attr || !*attr || (*attr)->value_tag != IPP_TAG_RANGE ||
4130 element < 0 || element > (*attr)->num_values || lowervalue > uppervalue)
4131 return (0);
4132
4133 /*
4134 * Set the value and return...
4135 */
4136
4137 if ((value = ipp_set_value(ipp, attr, element)) != NULL)
4138 {
4139 value->range.lower = lowervalue;
4140 value->range.upper = uppervalue;
4141 }
4142
4143 return (value != NULL);
4144 }
4145
4146
4147 /*
4148 * 'ippSetRequestId()' - Set the request ID in an IPP message.
4149 *
4150 * The @code ipp@ parameter refers to an IPP message previously created using
4151 * the @link ippNew@, @link ippNewRequest@, or @link ippNewResponse@ functions.
4152 *
4153 * The @code request_id@ parameter must be greater than 0.
4154 *
4155 * @since CUPS 1.6/macOS 10.8@
4156 */
4157
4158 int /* O - 1 on success, 0 on failure */
4159 ippSetRequestId(ipp_t *ipp, /* I - IPP message */
4160 int request_id) /* I - Request ID */
4161 {
4162 /*
4163 * Range check input; not checking request_id values since ipptool wants to send
4164 * invalid values for conformance testing and a bad request_id does not affect the
4165 * encoding of a message...
4166 */
4167
4168 if (!ipp)
4169 return (0);
4170
4171 /*
4172 * Set the request ID and return...
4173 */
4174
4175 ipp->request.any.request_id = request_id;
4176
4177 return (1);
4178 }
4179
4180
4181 /*
4182 * 'ippSetResolution()' - Set a resolution value in an attribute.
4183 *
4184 * The @code ipp@ parameter refers to an IPP message previously created using
4185 * the @link ippNew@, @link ippNewRequest@, or @link ippNewResponse@ functions.
4186 *
4187 * The @code attr@ parameter may be modified as a result of setting the value.
4188 *
4189 * The @code element@ parameter specifies which value to set from 0 to
4190 * @code ippGetCount(attr)@.
4191 *
4192 * @since CUPS 1.6/macOS 10.8@
4193 */
4194
4195 int /* O - 1 on success, 0 on failure */
4196 ippSetResolution(
4197 ipp_t *ipp, /* I - IPP message */
4198 ipp_attribute_t **attr, /* IO - IPP attribute */
4199 int element, /* I - Value number (0-based) */
4200 ipp_res_t unitsvalue, /* I - Resolution units */
4201 int xresvalue, /* I - Horizontal/cross feed resolution */
4202 int yresvalue) /* I - Vertical/feed resolution */
4203 {
4204 _ipp_value_t *value; /* Current value */
4205
4206
4207 /*
4208 * Range check input...
4209 */
4210
4211 if (!ipp || !attr || !*attr || (*attr)->value_tag != IPP_TAG_RESOLUTION ||
4212 element < 0 || element > (*attr)->num_values || xresvalue <= 0 || yresvalue <= 0 ||
4213 unitsvalue < IPP_RES_PER_INCH || unitsvalue > IPP_RES_PER_CM)
4214 return (0);
4215
4216 /*
4217 * Set the value and return...
4218 */
4219
4220 if ((value = ipp_set_value(ipp, attr, element)) != NULL)
4221 {
4222 value->resolution.units = unitsvalue;
4223 value->resolution.xres = xresvalue;
4224 value->resolution.yres = yresvalue;
4225 }
4226
4227 return (value != NULL);
4228 }
4229
4230
4231 /*
4232 * 'ippSetState()' - Set the current state of the IPP message.
4233 *
4234 * @since CUPS 1.6/macOS 10.8@
4235 */
4236
4237 int /* O - 1 on success, 0 on failure */
4238 ippSetState(ipp_t *ipp, /* I - IPP message */
4239 ipp_state_t state) /* I - IPP state value */
4240 {
4241 /*
4242 * Range check input...
4243 */
4244
4245 if (!ipp)
4246 return (0);
4247
4248 /*
4249 * Set the state and return...
4250 */
4251
4252 ipp->state = state;
4253 ipp->current = NULL;
4254
4255 return (1);
4256 }
4257
4258
4259 /*
4260 * 'ippSetStatusCode()' - Set the status code in an IPP response or event message.
4261 *
4262 * The @code ipp@ parameter refers to an IPP message previously created using
4263 * the @link ippNew@, @link ippNewRequest@, or @link ippNewResponse@ functions.
4264 *
4265 * @since CUPS 1.6/macOS 10.8@
4266 */
4267
4268 int /* O - 1 on success, 0 on failure */
4269 ippSetStatusCode(ipp_t *ipp, /* I - IPP response or event message */
4270 ipp_status_t status) /* I - Status code */
4271 {
4272 /*
4273 * Range check input...
4274 */
4275
4276 if (!ipp)
4277 return (0);
4278
4279 /*
4280 * Set the status code and return...
4281 */
4282
4283 ipp->request.status.status_code = status;
4284
4285 return (1);
4286 }
4287
4288
4289 /*
4290 * 'ippSetString()' - Set a string value in an attribute.
4291 *
4292 * The @code ipp@ parameter refers to an IPP message previously created using
4293 * the @link ippNew@, @link ippNewRequest@, or @link ippNewResponse@ functions.
4294 *
4295 * The @code attr@ parameter may be modified as a result of setting the value.
4296 *
4297 * The @code element@ parameter specifies which value to set from 0 to
4298 * @code ippGetCount(attr)@.
4299 *
4300 * @since CUPS 1.6/macOS 10.8@
4301 */
4302
4303 int /* O - 1 on success, 0 on failure */
4304 ippSetString(ipp_t *ipp, /* I - IPP message */
4305 ipp_attribute_t **attr, /* IO - IPP attribute */
4306 int element, /* I - Value number (0-based) */
4307 const char *strvalue) /* I - String value */
4308 {
4309 char *temp; /* Temporary string */
4310 _ipp_value_t *value; /* Current value */
4311 ipp_tag_t value_tag; /* Value tag */
4312
4313
4314 /*
4315 * Range check input...
4316 */
4317
4318 if (attr && *attr)
4319 value_tag = (*attr)->value_tag & IPP_TAG_CUPS_MASK;
4320 else
4321 value_tag = IPP_TAG_ZERO;
4322
4323 if (!ipp || !attr || !*attr ||
4324 (value_tag < IPP_TAG_TEXT && value_tag != IPP_TAG_TEXTLANG &&
4325 value_tag != IPP_TAG_NAMELANG) || value_tag > IPP_TAG_MIMETYPE ||
4326 !strvalue)
4327 return (0);
4328
4329 /*
4330 * Set the value and return...
4331 */
4332
4333 if ((value = ipp_set_value(ipp, attr, element)) != NULL)
4334 {
4335 if (element > 0)
4336 value->string.language = (*attr)->values[0].string.language;
4337
4338 if ((int)((*attr)->value_tag) & IPP_TAG_CUPS_CONST)
4339 value->string.text = (char *)strvalue;
4340 else if ((temp = _cupsStrAlloc(strvalue)) != NULL)
4341 {
4342 if (value->string.text)
4343 _cupsStrFree(value->string.text);
4344
4345 value->string.text = temp;
4346 }
4347 else
4348 return (0);
4349 }
4350
4351 return (value != NULL);
4352 }
4353
4354
4355 /*
4356 * 'ippSetStringf()' - Set a formatted string value of an attribute.
4357 *
4358 * The @code ipp@ parameter refers to an IPP message previously created using
4359 * the @link ippNew@, @link ippNewRequest@, or @link ippNewResponse@ functions.
4360 *
4361 * The @code attr@ parameter may be modified as a result of setting the value.
4362 *
4363 * The @code element@ parameter specifies which value to set from 0 to
4364 * @code ippGetCount(attr)@.
4365 *
4366 * The @code format@ parameter uses formatting characters compatible with the
4367 * printf family of standard functions. Additional arguments follow it as
4368 * needed. The formatted string is truncated as needed to the maximum length of
4369 * the corresponding value type.
4370 *
4371 * @since CUPS 1.7/macOS 10.9@
4372 */
4373
4374 int /* O - 1 on success, 0 on failure */
4375 ippSetStringf(ipp_t *ipp, /* I - IPP message */
4376 ipp_attribute_t **attr, /* IO - IPP attribute */
4377 int element, /* I - Value number (0-based) */
4378 const char *format, /* I - Printf-style format string */
4379 ...) /* I - Additional arguments as needed */
4380 {
4381 int ret; /* Return value */
4382 va_list ap; /* Pointer to additional arguments */
4383
4384
4385 va_start(ap, format);
4386 ret = ippSetStringfv(ipp, attr, element, format, ap);
4387 va_end(ap);
4388
4389 return (ret);
4390 }
4391
4392
4393 /*
4394 * 'ippSetStringf()' - Set a formatted string value of an attribute.
4395 *
4396 * The @code ipp@ parameter refers to an IPP message previously created using
4397 * the @link ippNew@, @link ippNewRequest@, or @link ippNewResponse@ functions.
4398 *
4399 * The @code attr@ parameter may be modified as a result of setting the value.
4400 *
4401 * The @code element@ parameter specifies which value to set from 0 to
4402 * @code ippGetCount(attr)@.
4403 *
4404 * The @code format@ parameter uses formatting characters compatible with the
4405 * printf family of standard functions. Additional arguments follow it as
4406 * needed. The formatted string is truncated as needed to the maximum length of
4407 * the corresponding value type.
4408 *
4409 * @since CUPS 1.7/macOS 10.9@
4410 */
4411
4412 int /* O - 1 on success, 0 on failure */
4413 ippSetStringfv(ipp_t *ipp, /* I - IPP message */
4414 ipp_attribute_t **attr, /* IO - IPP attribute */
4415 int element, /* I - Value number (0-based) */
4416 const char *format, /* I - Printf-style format string */
4417 va_list ap) /* I - Pointer to additional arguments */
4418 {
4419 ipp_tag_t value_tag; /* Value tag */
4420 char buffer[IPP_MAX_TEXT + 4];
4421 /* Formatted text string */
4422 ssize_t bytes, /* Length of formatted value */
4423 max_bytes; /* Maximum number of bytes for value */
4424
4425
4426 /*
4427 * Range check input...
4428 */
4429
4430 if (attr && *attr)
4431 value_tag = (*attr)->value_tag & IPP_TAG_CUPS_MASK;
4432 else
4433 value_tag = IPP_TAG_ZERO;
4434
4435 if (!ipp || !attr || !*attr ||
4436 (value_tag < IPP_TAG_TEXT && value_tag != IPP_TAG_TEXTLANG &&
4437 value_tag != IPP_TAG_NAMELANG) || value_tag > IPP_TAG_MIMETYPE ||
4438 !format)
4439 return (0);
4440
4441 /*
4442 * Format the string...
4443 */
4444
4445 if (!strcmp(format, "%s"))
4446 {
4447 /*
4448 * Optimize the simple case...
4449 */
4450
4451 const char *s = va_arg(ap, char *);
4452
4453 if (!s)
4454 s = "(null)";
4455
4456 bytes = (ssize_t)strlen(s);
4457 strlcpy(buffer, s, sizeof(buffer));
4458 }
4459 else
4460 {
4461 /*
4462 * Do a full formatting of the message...
4463 */
4464
4465 if ((bytes = vsnprintf(buffer, sizeof(buffer), format, ap)) < 0)
4466 return (0);
4467 }
4468
4469 /*
4470 * Limit the length of the string...
4471 */
4472
4473 switch (value_tag)
4474 {
4475 default :
4476 case IPP_TAG_TEXT :
4477 case IPP_TAG_TEXTLANG :
4478 max_bytes = IPP_MAX_TEXT;
4479 break;
4480
4481 case IPP_TAG_NAME :
4482 case IPP_TAG_NAMELANG :
4483 max_bytes = IPP_MAX_NAME;
4484 break;
4485
4486 case IPP_TAG_CHARSET :
4487 max_bytes = IPP_MAX_CHARSET;
4488 break;
4489
4490 case IPP_TAG_KEYWORD :
4491 max_bytes = IPP_MAX_KEYWORD;
4492 break;
4493
4494 case IPP_TAG_LANGUAGE :
4495 max_bytes = IPP_MAX_LANGUAGE;
4496 break;
4497
4498 case IPP_TAG_MIMETYPE :
4499 max_bytes = IPP_MAX_MIMETYPE;
4500 break;
4501
4502 case IPP_TAG_URI :
4503 max_bytes = IPP_MAX_URI;
4504 break;
4505
4506 case IPP_TAG_URISCHEME :
4507 max_bytes = IPP_MAX_URISCHEME;
4508 break;
4509 }
4510
4511 if (bytes >= max_bytes)
4512 {
4513 char *bufmax, /* Buffer at max_bytes */
4514 *bufptr; /* Pointer into buffer */
4515
4516 bufptr = buffer + strlen(buffer) - 1;
4517 bufmax = buffer + max_bytes - 1;
4518
4519 while (bufptr > bufmax)
4520 {
4521 if (*bufptr & 0x80)
4522 {
4523 while ((*bufptr & 0xc0) == 0x80 && bufptr > buffer)
4524 bufptr --;
4525 }
4526
4527 bufptr --;
4528 }
4529
4530 *bufptr = '\0';
4531 }
4532
4533 /*
4534 * Set the formatted string and return...
4535 */
4536
4537 return (ippSetString(ipp, attr, element, buffer));
4538 }
4539
4540
4541 /*
4542 * 'ippSetValueTag()' - Set the value tag of an attribute.
4543 *
4544 * The @code ipp@ parameter refers to an IPP message previously created using
4545 * the @link ippNew@, @link ippNewRequest@, or @link ippNewResponse@ functions.
4546 *
4547 * The @code attr@ parameter may be modified as a result of setting the value.
4548 *
4549 * Integer (@code IPP_TAG_INTEGER@) values can be promoted to rangeOfInteger
4550 * (@code IPP_TAG_RANGE@) values, the various string tags can be promoted to name
4551 * (@code IPP_TAG_NAME@) or nameWithLanguage (@code IPP_TAG_NAMELANG@) values, text
4552 * (@code IPP_TAG_TEXT@) values can be promoted to textWithLanguage
4553 * (@code IPP_TAG_TEXTLANG@) values, and all values can be demoted to the various
4554 * out-of-band value tags such as no-value (@code IPP_TAG_NOVALUE@). All other changes
4555 * will be rejected.
4556 *
4557 * Promoting a string attribute to nameWithLanguage or textWithLanguage adds the language
4558 * code in the "attributes-natural-language" attribute or, if not present, the language
4559 * code for the current locale.
4560 *
4561 * @since CUPS 1.6/macOS 10.8@
4562 */
4563
4564 int /* O - 1 on success, 0 on failure */
4565 ippSetValueTag(
4566 ipp_t *ipp, /* I - IPP message */
4567 ipp_attribute_t **attr, /* IO - IPP attribute */
4568 ipp_tag_t value_tag) /* I - Value tag */
4569 {
4570 int i; /* Looping var */
4571 _ipp_value_t *value; /* Current value */
4572 int integer; /* Current integer value */
4573 cups_lang_t *language; /* Current language */
4574 char code[32]; /* Language code */
4575 ipp_tag_t temp_tag; /* Temporary value tag */
4576
4577
4578 /*
4579 * Range check input...
4580 */
4581
4582 if (!ipp || !attr || !*attr)
4583 return (0);
4584
4585 /*
4586 * If there is no change, return immediately...
4587 */
4588
4589 if (value_tag == (*attr)->value_tag)
4590 return (1);
4591
4592 /*
4593 * Otherwise implement changes as needed...
4594 */
4595
4596 temp_tag = (ipp_tag_t)((int)((*attr)->value_tag) & IPP_TAG_CUPS_MASK);
4597
4598 switch (value_tag)
4599 {
4600 case IPP_TAG_UNSUPPORTED_VALUE :
4601 case IPP_TAG_DEFAULT :
4602 case IPP_TAG_UNKNOWN :
4603 case IPP_TAG_NOVALUE :
4604 case IPP_TAG_NOTSETTABLE :
4605 case IPP_TAG_DELETEATTR :
4606 case IPP_TAG_ADMINDEFINE :
4607 /*
4608 * Free any existing values...
4609 */
4610
4611 if ((*attr)->num_values > 0)
4612 ipp_free_values(*attr, 0, (*attr)->num_values);
4613
4614 /*
4615 * Set out-of-band value...
4616 */
4617
4618 (*attr)->value_tag = value_tag;
4619 break;
4620
4621 case IPP_TAG_RANGE :
4622 if (temp_tag != IPP_TAG_INTEGER)
4623 return (0);
4624
4625 for (i = (*attr)->num_values, value = (*attr)->values;
4626 i > 0;
4627 i --, value ++)
4628 {
4629 integer = value->integer;
4630 value->range.lower = value->range.upper = integer;
4631 }
4632
4633 (*attr)->value_tag = IPP_TAG_RANGE;
4634 break;
4635
4636 case IPP_TAG_NAME :
4637 if (temp_tag != IPP_TAG_KEYWORD && temp_tag != IPP_TAG_URI &&
4638 temp_tag != IPP_TAG_URISCHEME && temp_tag != IPP_TAG_LANGUAGE &&
4639 temp_tag != IPP_TAG_MIMETYPE)
4640 return (0);
4641
4642 (*attr)->value_tag = (ipp_tag_t)(IPP_TAG_NAME | ((*attr)->value_tag & IPP_TAG_CUPS_CONST));
4643 break;
4644
4645 case IPP_TAG_NAMELANG :
4646 case IPP_TAG_TEXTLANG :
4647 if (value_tag == IPP_TAG_NAMELANG &&
4648 (temp_tag != IPP_TAG_NAME && temp_tag != IPP_TAG_KEYWORD &&
4649 temp_tag != IPP_TAG_URI && temp_tag != IPP_TAG_URISCHEME &&
4650 temp_tag != IPP_TAG_LANGUAGE && temp_tag != IPP_TAG_MIMETYPE))
4651 return (0);
4652
4653 if (value_tag == IPP_TAG_TEXTLANG && temp_tag != IPP_TAG_TEXT)
4654 return (0);
4655
4656 if (ipp->attrs && ipp->attrs->next && ipp->attrs->next->name &&
4657 !strcmp(ipp->attrs->next->name, "attributes-natural-language"))
4658 {
4659 /*
4660 * Use the language code from the IPP message...
4661 */
4662
4663 (*attr)->values[0].string.language =
4664 _cupsStrAlloc(ipp->attrs->next->values[0].string.text);
4665 }
4666 else
4667 {
4668 /*
4669 * Otherwise, use the language code corresponding to the locale...
4670 */
4671
4672 language = cupsLangDefault();
4673 (*attr)->values[0].string.language = _cupsStrAlloc(ipp_lang_code(language->language,
4674 code,
4675 sizeof(code)));
4676 }
4677
4678 for (i = (*attr)->num_values - 1, value = (*attr)->values + 1;
4679 i > 0;
4680 i --, value ++)
4681 value->string.language = (*attr)->values[0].string.language;
4682
4683 if ((int)(*attr)->value_tag & IPP_TAG_CUPS_CONST)
4684 {
4685 /*
4686 * Make copies of all values...
4687 */
4688
4689 for (i = (*attr)->num_values, value = (*attr)->values;
4690 i > 0;
4691 i --, value ++)
4692 value->string.text = _cupsStrAlloc(value->string.text);
4693 }
4694
4695 (*attr)->value_tag = IPP_TAG_NAMELANG;
4696 break;
4697
4698 case IPP_TAG_KEYWORD :
4699 if (temp_tag == IPP_TAG_NAME || temp_tag == IPP_TAG_NAMELANG)
4700 break; /* Silently "allow" name -> keyword */
4701
4702 default :
4703 return (0);
4704 }
4705
4706 return (1);
4707 }
4708
4709
4710 /*
4711 * 'ippSetVersion()' - Set the version number in an IPP message.
4712 *
4713 * The @code ipp@ parameter refers to an IPP message previously created using
4714 * the @link ippNew@, @link ippNewRequest@, or @link ippNewResponse@ functions.
4715 *
4716 * The valid version numbers are currently 1.0, 1.1, 2.0, 2.1, and 2.2.
4717 *
4718 * @since CUPS 1.6/macOS 10.8@
4719 */
4720
4721 int /* O - 1 on success, 0 on failure */
4722 ippSetVersion(ipp_t *ipp, /* I - IPP message */
4723 int major, /* I - Major version number (major.minor) */
4724 int minor) /* I - Minor version number (major.minor) */
4725 {
4726 /*
4727 * Range check input...
4728 */
4729
4730 if (!ipp || major < 0 || minor < 0)
4731 return (0);
4732
4733 /*
4734 * Set the version number...
4735 */
4736
4737 ipp->request.any.version[0] = (ipp_uchar_t)major;
4738 ipp->request.any.version[1] = (ipp_uchar_t)minor;
4739
4740 return (1);
4741 }
4742
4743
4744 /*
4745 * 'ippTimeToDate()' - Convert from time in seconds to RFC 2579 format.
4746 */
4747
4748 const ipp_uchar_t * /* O - RFC-2579 date/time data */
4749 ippTimeToDate(time_t t) /* I - Time in seconds */
4750 {
4751 struct tm *unixdate; /* UNIX unixdate/time info */
4752 ipp_uchar_t *date = _cupsGlobals()->ipp_date;
4753 /* RFC-2579 date/time data */
4754
4755
4756 /*
4757 * RFC-2579 date/time format is:
4758 *
4759 * Byte(s) Description
4760 * ------- -----------
4761 * 0-1 Year (0 to 65535)
4762 * 2 Month (1 to 12)
4763 * 3 Day (1 to 31)
4764 * 4 Hours (0 to 23)
4765 * 5 Minutes (0 to 59)
4766 * 6 Seconds (0 to 60, 60 = "leap second")
4767 * 7 Deciseconds (0 to 9)
4768 * 8 +/- UTC
4769 * 9 UTC hours (0 to 11)
4770 * 10 UTC minutes (0 to 59)
4771 */
4772
4773 unixdate = gmtime(&t);
4774 unixdate->tm_year += 1900;
4775
4776 date[0] = (ipp_uchar_t)(unixdate->tm_year >> 8);
4777 date[1] = (ipp_uchar_t)(unixdate->tm_year);
4778 date[2] = (ipp_uchar_t)(unixdate->tm_mon + 1);
4779 date[3] = (ipp_uchar_t)unixdate->tm_mday;
4780 date[4] = (ipp_uchar_t)unixdate->tm_hour;
4781 date[5] = (ipp_uchar_t)unixdate->tm_min;
4782 date[6] = (ipp_uchar_t)unixdate->tm_sec;
4783 date[7] = 0;
4784 date[8] = '+';
4785 date[9] = 0;
4786 date[10] = 0;
4787
4788 return (date);
4789 }
4790
4791
4792 /*
4793 * 'ippValidateAttribute()' - Validate the contents of an attribute.
4794 *
4795 * This function validates the contents of an attribute based on the name and
4796 * value tag. 1 is returned if the attribute is valid, 0 otherwise. On
4797 * failure, @link cupsLastErrorString@ is set to a human-readable message.
4798 *
4799 * @since CUPS 1.7/macOS 10.9@
4800 */
4801
4802 int /* O - 1 if valid, 0 otherwise */
4803 ippValidateAttribute(
4804 ipp_attribute_t *attr) /* I - Attribute */
4805 {
4806 int i; /* Looping var */
4807 char scheme[64], /* Scheme from URI */
4808 userpass[256], /* Username/password from URI */
4809 hostname[256], /* Hostname from URI */
4810 resource[1024]; /* Resource from URI */
4811 int port, /* Port number from URI */
4812 uri_status; /* URI separation status */
4813 const char *ptr; /* Pointer into string */
4814 ipp_attribute_t *colattr; /* Collection attribute */
4815 regex_t re; /* Regular expression */
4816 ipp_uchar_t *date; /* Current date value */
4817
4818
4819 /*
4820 * Skip separators.
4821 */
4822
4823 if (!attr->name)
4824 return (1);
4825
4826 /*
4827 * Validate the attribute name.
4828 */
4829
4830 for (ptr = attr->name; *ptr; ptr ++)
4831 if (!isalnum(*ptr & 255) && *ptr != '-' && *ptr != '.' && *ptr != '_')
4832 break;
4833
4834 if (*ptr || ptr == attr->name)
4835 {
4836 ipp_set_error(IPP_STATUS_ERROR_BAD_REQUEST, _("\"%s\": Bad attribute name - invalid character (RFC 8011 section 5.1.4)."), attr->name);
4837 return (0);
4838 }
4839
4840 if ((ptr - attr->name) > 255)
4841 {
4842 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));
4843 return (0);
4844 }
4845
4846 switch (attr->value_tag)
4847 {
4848 case IPP_TAG_INTEGER :
4849 break;
4850
4851 case IPP_TAG_BOOLEAN :
4852 for (i = 0; i < attr->num_values; i ++)
4853 {
4854 if (attr->values[i].boolean != 0 &&
4855 attr->values[i].boolean != 1)
4856 {
4857 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);
4858 return (0);
4859 }
4860 }
4861 break;
4862
4863 case IPP_TAG_ENUM :
4864 for (i = 0; i < attr->num_values; i ++)
4865 {
4866 if (attr->values[i].integer < 1)
4867 {
4868 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);
4869 return (0);
4870 }
4871 }
4872 break;
4873
4874 case IPP_TAG_STRING :
4875 for (i = 0; i < attr->num_values; i ++)
4876 {
4877 if (attr->values[i].unknown.length > IPP_MAX_OCTETSTRING)
4878 {
4879 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);
4880 return (0);
4881 }
4882 }
4883 break;
4884
4885 case IPP_TAG_DATE :
4886 for (i = 0; i < attr->num_values; i ++)
4887 {
4888 date = attr->values[i].date;
4889
4890 if (date[2] < 1 || date[2] > 12)
4891 {
4892 ipp_set_error(IPP_STATUS_ERROR_BAD_REQUEST, _("\"%s\": Bad dateTime month %u (RFC 8011 section 5.1.15)."), attr->name, date[2]);
4893 return (0);
4894 }
4895
4896 if (date[3] < 1 || date[3] > 31)
4897 {
4898 ipp_set_error(IPP_STATUS_ERROR_BAD_REQUEST, _("\"%s\": Bad dateTime day %u (RFC 8011 section 5.1.15)."), attr->name, date[3]);
4899 return (0);
4900 }
4901
4902 if (date[4] > 23)
4903 {
4904 ipp_set_error(IPP_STATUS_ERROR_BAD_REQUEST, _("\"%s\": Bad dateTime hours %u (RFC 8011 section 5.1.15)."), attr->name, date[4]);
4905 return (0);
4906 }
4907
4908 if (date[5] > 59)
4909 {
4910 ipp_set_error(IPP_STATUS_ERROR_BAD_REQUEST, _("\"%s\": Bad dateTime minutes %u (RFC 8011 section 5.1.15)."), attr->name, date[5]);
4911 return (0);
4912 }
4913
4914 if (date[6] > 60)
4915 {
4916 ipp_set_error(IPP_STATUS_ERROR_BAD_REQUEST, _("\"%s\": Bad dateTime seconds %u (RFC 8011 section 5.1.15)."), attr->name, date[6]);
4917 return (0);
4918 }
4919
4920 if (date[7] > 9)
4921 {
4922 ipp_set_error(IPP_STATUS_ERROR_BAD_REQUEST, _("\"%s\": Bad dateTime deciseconds %u (RFC 8011 section 5.1.15)."), attr->name, date[7]);
4923 return (0);
4924 }
4925
4926 if (date[8] != '-' && date[8] != '+')
4927 {
4928 ipp_set_error(IPP_STATUS_ERROR_BAD_REQUEST, _("\"%s\": Bad dateTime UTC sign '%c' (RFC 8011 section 5.1.15)."), attr->name, date[8]);
4929 return (0);
4930 }
4931
4932 if (date[9] > 11)
4933 {
4934 ipp_set_error(IPP_STATUS_ERROR_BAD_REQUEST, _("\"%s\": Bad dateTime UTC hours %u (RFC 8011 section 5.1.15)."), attr->name, date[9]);
4935 return (0);
4936 }
4937
4938 if (date[10] > 59)
4939 {
4940 ipp_set_error(IPP_STATUS_ERROR_BAD_REQUEST, _("\"%s\": Bad dateTime UTC minutes %u (RFC 8011 section 5.1.15)."), attr->name, date[10]);
4941 return (0);
4942 }
4943 }
4944 break;
4945
4946 case IPP_TAG_RESOLUTION :
4947 for (i = 0; i < attr->num_values; i ++)
4948 {
4949 if (attr->values[i].resolution.xres <= 0)
4950 {
4951 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");
4952 return (0);
4953 }
4954
4955 if (attr->values[i].resolution.yres <= 0)
4956 {
4957 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");
4958 return (0);
4959 }
4960
4961 if (attr->values[i].resolution.units != IPP_RES_PER_INCH && attr->values[i].resolution.units != IPP_RES_PER_CM)
4962 {
4963 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");
4964 return (0);
4965 }
4966 }
4967 break;
4968
4969 case IPP_TAG_RANGE :
4970 for (i = 0; i < attr->num_values; i ++)
4971 {
4972 if (attr->values[i].range.lower > attr->values[i].range.upper)
4973 {
4974 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);
4975 return (0);
4976 }
4977 }
4978 break;
4979
4980 case IPP_TAG_BEGIN_COLLECTION :
4981 for (i = 0; i < attr->num_values; i ++)
4982 {
4983 for (colattr = attr->values[i].collection->attrs;
4984 colattr;
4985 colattr = colattr->next)
4986 {
4987 if (!ippValidateAttribute(colattr))
4988 return (0);
4989 }
4990 }
4991 break;
4992
4993 case IPP_TAG_TEXT :
4994 case IPP_TAG_TEXTLANG :
4995 for (i = 0; i < attr->num_values; i ++)
4996 {
4997 for (ptr = attr->values[i].string.text; *ptr; ptr ++)
4998 {
4999 if ((*ptr & 0xe0) == 0xc0)
5000 {
5001 ptr ++;
5002 if ((*ptr & 0xc0) != 0x80)
5003 break;
5004 }
5005 else if ((*ptr & 0xf0) == 0xe0)
5006 {
5007 ptr ++;
5008 if ((*ptr & 0xc0) != 0x80)
5009 break;
5010 ptr ++;
5011 if ((*ptr & 0xc0) != 0x80)
5012 break;
5013 }
5014 else if ((*ptr & 0xf8) == 0xf0)
5015 {
5016 ptr ++;
5017 if ((*ptr & 0xc0) != 0x80)
5018 break;
5019 ptr ++;
5020 if ((*ptr & 0xc0) != 0x80)
5021 break;
5022 ptr ++;
5023 if ((*ptr & 0xc0) != 0x80)
5024 break;
5025 }
5026 else if (*ptr & 0x80)
5027 break;
5028 else if ((*ptr < ' ' && *ptr != '\n' && *ptr != '\r' && *ptr != '\t') || *ptr == 0x7f)
5029 break;
5030 }
5031
5032 if (*ptr < ' ' || *ptr == 0x7f)
5033 {
5034 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);
5035 return (0);
5036 }
5037 else if (*ptr)
5038 {
5039 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);
5040 return (0);
5041 }
5042
5043 if ((ptr - attr->values[i].string.text) > (IPP_MAX_TEXT - 1))
5044 {
5045 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));
5046 return (0);
5047 }
5048 }
5049 break;
5050
5051 case IPP_TAG_NAME :
5052 case IPP_TAG_NAMELANG :
5053 for (i = 0; i < attr->num_values; i ++)
5054 {
5055 for (ptr = attr->values[i].string.text; *ptr; ptr ++)
5056 {
5057 if ((*ptr & 0xe0) == 0xc0)
5058 {
5059 ptr ++;
5060 if ((*ptr & 0xc0) != 0x80)
5061 break;
5062 }
5063 else if ((*ptr & 0xf0) == 0xe0)
5064 {
5065 ptr ++;
5066 if ((*ptr & 0xc0) != 0x80)
5067 break;
5068 ptr ++;
5069 if ((*ptr & 0xc0) != 0x80)
5070 break;
5071 }
5072 else if ((*ptr & 0xf8) == 0xf0)
5073 {
5074 ptr ++;
5075 if ((*ptr & 0xc0) != 0x80)
5076 break;
5077 ptr ++;
5078 if ((*ptr & 0xc0) != 0x80)
5079 break;
5080 ptr ++;
5081 if ((*ptr & 0xc0) != 0x80)
5082 break;
5083 }
5084 else if (*ptr & 0x80)
5085 break;
5086 else if (*ptr < ' ' || *ptr == 0x7f)
5087 break;
5088 }
5089
5090 if (*ptr < ' ' || *ptr == 0x7f)
5091 {
5092 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);
5093 return (0);
5094 }
5095 else if (*ptr)
5096 {
5097 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);
5098 return (0);
5099 }
5100
5101 if ((ptr - attr->values[i].string.text) > (IPP_MAX_NAME - 1))
5102 {
5103 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));
5104 return (0);
5105 }
5106 }
5107 break;
5108
5109 case IPP_TAG_KEYWORD :
5110 for (i = 0; i < attr->num_values; i ++)
5111 {
5112 for (ptr = attr->values[i].string.text; *ptr; ptr ++)
5113 {
5114 if (!isalnum(*ptr & 255) && *ptr != '-' && *ptr != '.' &&
5115 *ptr != '_')
5116 break;
5117 }
5118
5119 if (*ptr || ptr == attr->values[i].string.text)
5120 {
5121 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);
5122 return (0);
5123 }
5124
5125 if ((ptr - attr->values[i].string.text) > (IPP_MAX_KEYWORD - 1))
5126 {
5127 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));
5128 return (0);
5129 }
5130 }
5131 break;
5132
5133 case IPP_TAG_URI :
5134 for (i = 0; i < attr->num_values; i ++)
5135 {
5136 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));
5137
5138 if (uri_status < HTTP_URI_STATUS_OK)
5139 {
5140 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));
5141 return (0);
5142 }
5143
5144 if (strlen(attr->values[i].string.text) > (IPP_MAX_URI - 1))
5145 {
5146 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));
5147 }
5148 }
5149 break;
5150
5151 case IPP_TAG_URISCHEME :
5152 for (i = 0; i < attr->num_values; i ++)
5153 {
5154 ptr = attr->values[i].string.text;
5155 if (islower(*ptr & 255))
5156 {
5157 for (ptr ++; *ptr; ptr ++)
5158 {
5159 if (!islower(*ptr & 255) && !isdigit(*ptr & 255) &&
5160 *ptr != '+' && *ptr != '-' && *ptr != '.')
5161 break;
5162 }
5163 }
5164
5165 if (*ptr || ptr == attr->values[i].string.text)
5166 {
5167 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);
5168 return (0);
5169 }
5170
5171 if ((ptr - attr->values[i].string.text) > (IPP_MAX_URISCHEME - 1))
5172 {
5173 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));
5174 return (0);
5175 }
5176 }
5177 break;
5178
5179 case IPP_TAG_CHARSET :
5180 for (i = 0; i < attr->num_values; i ++)
5181 {
5182 for (ptr = attr->values[i].string.text; *ptr; ptr ++)
5183 {
5184 if (!isprint(*ptr & 255) || isupper(*ptr & 255) ||
5185 isspace(*ptr & 255))
5186 break;
5187 }
5188
5189 if (*ptr || ptr == attr->values[i].string.text)
5190 {
5191 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);
5192 return (0);
5193 }
5194
5195 if ((ptr - attr->values[i].string.text) > (IPP_MAX_CHARSET - 1))
5196 {
5197 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));
5198 return (0);
5199 }
5200 }
5201 break;
5202
5203 case IPP_TAG_LANGUAGE :
5204 /*
5205 * The following regular expression is derived from the ABNF for
5206 * language tags in RFC 4646. All I can say is that this is the
5207 * easiest way to check the values...
5208 */
5209
5210 if ((i = regcomp(&re,
5211 "^("
5212 "(([a-z]{2,3}(-[a-z][a-z][a-z]){0,3})|[a-z]{4,8})"
5213 /* language */
5214 "(-[a-z][a-z][a-z][a-z]){0,1}" /* script */
5215 "(-([a-z][a-z]|[0-9][0-9][0-9])){0,1}" /* region */
5216 "(-([a-z]{5,8}|[0-9][0-9][0-9]))*" /* variant */
5217 "(-[a-wy-z](-[a-z0-9]{2,8})+)*" /* extension */
5218 "(-x(-[a-z0-9]{1,8})+)*" /* privateuse */
5219 "|"
5220 "x(-[a-z0-9]{1,8})+" /* privateuse */
5221 "|"
5222 "[a-z]{1,3}(-[a-z][0-9]{2,8}){1,2}" /* grandfathered */
5223 ")$",
5224 REG_NOSUB | REG_EXTENDED)) != 0)
5225 {
5226 char temp[256]; /* Temporary error string */
5227
5228 regerror(i, &re, temp, sizeof(temp));
5229 ipp_set_error(IPP_STATUS_ERROR_INTERNAL, _("Unable to compile naturalLanguage regular expression: %s."), temp);
5230 return (0);
5231 }
5232
5233 for (i = 0; i < attr->num_values; i ++)
5234 {
5235 if (regexec(&re, attr->values[i].string.text, 0, NULL, 0))
5236 {
5237 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);
5238 regfree(&re);
5239 return (0);
5240 }
5241
5242 if (strlen(attr->values[i].string.text) > (IPP_MAX_LANGUAGE - 1))
5243 {
5244 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));
5245 regfree(&re);
5246 return (0);
5247 }
5248 }
5249
5250 regfree(&re);
5251 break;
5252
5253 case IPP_TAG_MIMETYPE :
5254 /*
5255 * The following regular expression is derived from the ABNF for
5256 * MIME media types in RFC 2045 and 4288. All I can say is that this is
5257 * the easiest way to check the values...
5258 */
5259
5260 if ((i = regcomp(&re,
5261 "^"
5262 "[-a-zA-Z0-9!#$&.+^_]{1,127}" /* type-name */
5263 "/"
5264 "[-a-zA-Z0-9!#$&.+^_]{1,127}" /* subtype-name */
5265 "(;[-a-zA-Z0-9!#$&.+^_]{1,127}=" /* parameter= */
5266 "([-a-zA-Z0-9!#$&.+^_]{1,127}|\"[^\"]*\"))*"
5267 /* value */
5268 "$",
5269 REG_NOSUB | REG_EXTENDED)) != 0)
5270 {
5271 char temp[256]; /* Temporary error string */
5272
5273 regerror(i, &re, temp, sizeof(temp));
5274 ipp_set_error(IPP_STATUS_ERROR_BAD_REQUEST, _("Unable to compile mimeMediaType regular expression: %s."), temp);
5275 return (0);
5276 }
5277
5278 for (i = 0; i < attr->num_values; i ++)
5279 {
5280 if (regexec(&re, attr->values[i].string.text, 0, NULL, 0))
5281 {
5282 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);
5283 regfree(&re);
5284 return (0);
5285 }
5286
5287 if (strlen(attr->values[i].string.text) > (IPP_MAX_MIMETYPE - 1))
5288 {
5289 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));
5290 regfree(&re);
5291 return (0);
5292 }
5293 }
5294
5295 regfree(&re);
5296 break;
5297
5298 default :
5299 break;
5300 }
5301
5302 return (1);
5303 }
5304
5305
5306 /*
5307 * 'ippValidateAttributes()' - Validate all attributes in an IPP message.
5308 *
5309 * This function validates the contents of the IPP message, including each
5310 * attribute. Like @link ippValidateAttribute@, @link cupsLastErrorString@ is
5311 * set to a human-readable message on failure.
5312 *
5313 * @since CUPS 1.7/macOS 10.9@
5314 */
5315
5316 int /* O - 1 if valid, 0 otherwise */
5317 ippValidateAttributes(ipp_t *ipp) /* I - IPP message */
5318 {
5319 ipp_attribute_t *attr; /* Current attribute */
5320
5321
5322 if (!ipp)
5323 return (1);
5324
5325 for (attr = ipp->attrs; attr; attr = attr->next)
5326 if (!ippValidateAttribute(attr))
5327 return (0);
5328
5329 return (1);
5330 }
5331
5332
5333 /*
5334 * 'ippWrite()' - Write data for an IPP message to a HTTP connection.
5335 */
5336
5337 ipp_state_t /* O - Current state */
5338 ippWrite(http_t *http, /* I - HTTP connection */
5339 ipp_t *ipp) /* I - IPP data */
5340 {
5341 DEBUG_printf(("ippWrite(http=%p, ipp=%p)", (void *)http, (void *)ipp));
5342
5343 if (!http)
5344 return (IPP_STATE_ERROR);
5345
5346 return (ippWriteIO(http, (ipp_iocb_t)httpWrite2, http->blocking, NULL, ipp));
5347 }
5348
5349
5350 /*
5351 * 'ippWriteFile()' - Write data for an IPP message to a file.
5352 *
5353 * @since CUPS 1.1.19/macOS 10.3@
5354 */
5355
5356 ipp_state_t /* O - Current state */
5357 ippWriteFile(int fd, /* I - HTTP data */
5358 ipp_t *ipp) /* I - IPP data */
5359 {
5360 DEBUG_printf(("ippWriteFile(fd=%d, ipp=%p)", fd, (void *)ipp));
5361
5362 ipp->state = IPP_STATE_IDLE;
5363
5364 return (ippWriteIO(&fd, (ipp_iocb_t)ipp_write_file, 1, NULL, ipp));
5365 }
5366
5367
5368 /*
5369 * 'ippWriteIO()' - Write data for an IPP message.
5370 *
5371 * @since CUPS 1.2/macOS 10.5@
5372 */
5373
5374 ipp_state_t /* O - Current state */
5375 ippWriteIO(void *dst, /* I - Destination */
5376 ipp_iocb_t cb, /* I - Write callback function */
5377 int blocking, /* I - Use blocking IO? */
5378 ipp_t *parent, /* I - Parent IPP message */
5379 ipp_t *ipp) /* I - IPP data */
5380 {
5381 int i; /* Looping var */
5382 int n; /* Length of data */
5383 unsigned char *buffer, /* Data buffer */
5384 *bufptr; /* Pointer into buffer */
5385 ipp_attribute_t *attr; /* Current attribute */
5386 _ipp_value_t *value; /* Current value */
5387
5388
5389 DEBUG_printf(("ippWriteIO(dst=%p, cb=%p, blocking=%d, parent=%p, ipp=%p)", (void *)dst, (void *)cb, blocking, (void *)parent, (void *)ipp));
5390
5391 if (!dst || !ipp)
5392 return (IPP_STATE_ERROR);
5393
5394 if ((buffer = (unsigned char *)_cupsBufferGet(IPP_BUF_SIZE)) == NULL)
5395 {
5396 DEBUG_puts("1ippWriteIO: Unable to get write buffer");
5397 return (IPP_STATE_ERROR);
5398 }
5399
5400 switch (ipp->state)
5401 {
5402 case IPP_STATE_IDLE :
5403 ipp->state ++; /* Avoid common problem... */
5404
5405 case IPP_STATE_HEADER :
5406 if (parent == NULL)
5407 {
5408 /*
5409 * Send the request header:
5410 *
5411 * Version = 2 bytes
5412 * Operation/Status Code = 2 bytes
5413 * Request ID = 4 bytes
5414 * Total = 8 bytes
5415 */
5416
5417 bufptr = buffer;
5418
5419 *bufptr++ = ipp->request.any.version[0];
5420 *bufptr++ = ipp->request.any.version[1];
5421 *bufptr++ = (ipp_uchar_t)(ipp->request.any.op_status >> 8);
5422 *bufptr++ = (ipp_uchar_t)ipp->request.any.op_status;
5423 *bufptr++ = (ipp_uchar_t)(ipp->request.any.request_id >> 24);
5424 *bufptr++ = (ipp_uchar_t)(ipp->request.any.request_id >> 16);
5425 *bufptr++ = (ipp_uchar_t)(ipp->request.any.request_id >> 8);
5426 *bufptr++ = (ipp_uchar_t)ipp->request.any.request_id;
5427
5428 DEBUG_printf(("2ippWriteIO: version=%d.%d", buffer[0], buffer[1]));
5429 DEBUG_printf(("2ippWriteIO: op_status=%04x",
5430 ipp->request.any.op_status));
5431 DEBUG_printf(("2ippWriteIO: request_id=%d",
5432 ipp->request.any.request_id));
5433
5434 if ((*cb)(dst, buffer, (size_t)(bufptr - buffer)) < 0)
5435 {
5436 DEBUG_puts("1ippWriteIO: Could not write IPP header...");
5437 _cupsBufferRelease((char *)buffer);
5438 return (IPP_STATE_ERROR);
5439 }
5440 }
5441
5442 /*
5443 * Reset the state engine to point to the first attribute
5444 * in the request/response, with no current group.
5445 */
5446
5447 ipp->state = IPP_STATE_ATTRIBUTE;
5448 ipp->current = ipp->attrs;
5449 ipp->curtag = IPP_TAG_ZERO;
5450
5451 DEBUG_printf(("1ippWriteIO: ipp->current=%p", (void *)ipp->current));
5452
5453 /*
5454 * If blocking is disabled, stop here...
5455 */
5456
5457 if (!blocking)
5458 break;
5459
5460 case IPP_STATE_ATTRIBUTE :
5461 while (ipp->current != NULL)
5462 {
5463 /*
5464 * Write this attribute...
5465 */
5466
5467 bufptr = buffer;
5468 attr = ipp->current;
5469
5470 ipp->current = ipp->current->next;
5471
5472 if (!parent)
5473 {
5474 if (ipp->curtag != attr->group_tag)
5475 {
5476 /*
5477 * Send a group tag byte...
5478 */
5479
5480 ipp->curtag = attr->group_tag;
5481
5482 if (attr->group_tag == IPP_TAG_ZERO)
5483 continue;
5484
5485 DEBUG_printf(("2ippWriteIO: wrote group tag=%x(%s)",
5486 attr->group_tag, ippTagString(attr->group_tag)));
5487 *bufptr++ = (ipp_uchar_t)attr->group_tag;
5488 }
5489 else if (attr->group_tag == IPP_TAG_ZERO)
5490 continue;
5491 }
5492
5493 DEBUG_printf(("1ippWriteIO: %s (%s%s)", attr->name,
5494 attr->num_values > 1 ? "1setOf " : "",
5495 ippTagString(attr->value_tag)));
5496
5497 /*
5498 * Write the attribute tag and name.
5499 *
5500 * The attribute name length does not include the trailing nul
5501 * character in the source string.
5502 *
5503 * Collection values (parent != NULL) are written differently...
5504 */
5505
5506 if (parent == NULL)
5507 {
5508 /*
5509 * Get the length of the attribute name, and make sure it won't
5510 * overflow the buffer...
5511 */
5512
5513 if ((n = (int)strlen(attr->name)) > (IPP_BUF_SIZE - 8))
5514 {
5515 DEBUG_printf(("1ippWriteIO: Attribute name too long (%d)", n));
5516 _cupsBufferRelease((char *)buffer);
5517 return (IPP_STATE_ERROR);
5518 }
5519
5520 /*
5521 * Write the value tag, name length, and name string...
5522 */
5523
5524 DEBUG_printf(("2ippWriteIO: writing value tag=%x(%s)",
5525 attr->value_tag, ippTagString(attr->value_tag)));
5526 DEBUG_printf(("2ippWriteIO: writing name=%d,\"%s\"", n,
5527 attr->name));
5528
5529 if (attr->value_tag > 0xff)
5530 {
5531 *bufptr++ = IPP_TAG_EXTENSION;
5532 *bufptr++ = (ipp_uchar_t)(attr->value_tag >> 24);
5533 *bufptr++ = (ipp_uchar_t)(attr->value_tag >> 16);
5534 *bufptr++ = (ipp_uchar_t)(attr->value_tag >> 8);
5535 *bufptr++ = (ipp_uchar_t)attr->value_tag;
5536 }
5537 else
5538 *bufptr++ = (ipp_uchar_t)attr->value_tag;
5539
5540 *bufptr++ = (ipp_uchar_t)(n >> 8);
5541 *bufptr++ = (ipp_uchar_t)n;
5542 memcpy(bufptr, attr->name, (size_t)n);
5543 bufptr += n;
5544 }
5545 else
5546 {
5547 /*
5548 * Get the length of the attribute name, and make sure it won't
5549 * overflow the buffer...
5550 */
5551
5552 if ((n = (int)strlen(attr->name)) > (IPP_BUF_SIZE - 12))
5553 {
5554 DEBUG_printf(("1ippWriteIO: Attribute name too long (%d)", n));
5555 _cupsBufferRelease((char *)buffer);
5556 return (IPP_STATE_ERROR);
5557 }
5558
5559 /*
5560 * Write the member name tag, name length, name string, value tag,
5561 * and empty name for the collection member attribute...
5562 */
5563
5564 DEBUG_printf(("2ippWriteIO: writing value tag=%x(memberName)",
5565 IPP_TAG_MEMBERNAME));
5566 DEBUG_printf(("2ippWriteIO: writing name=%d,\"%s\"", n,
5567 attr->name));
5568 DEBUG_printf(("2ippWriteIO: writing value tag=%x(%s)",
5569 attr->value_tag, ippTagString(attr->value_tag)));
5570 DEBUG_puts("2ippWriteIO: writing name=0,\"\"");
5571
5572 *bufptr++ = IPP_TAG_MEMBERNAME;
5573 *bufptr++ = 0;
5574 *bufptr++ = 0;
5575 *bufptr++ = (ipp_uchar_t)(n >> 8);
5576 *bufptr++ = (ipp_uchar_t)n;
5577 memcpy(bufptr, attr->name, (size_t)n);
5578 bufptr += n;
5579
5580 if (attr->value_tag > 0xff)
5581 {
5582 *bufptr++ = IPP_TAG_EXTENSION;
5583 *bufptr++ = (ipp_uchar_t)(attr->value_tag >> 24);
5584 *bufptr++ = (ipp_uchar_t)(attr->value_tag >> 16);
5585 *bufptr++ = (ipp_uchar_t)(attr->value_tag >> 8);
5586 *bufptr++ = (ipp_uchar_t)attr->value_tag;
5587 }
5588 else
5589 *bufptr++ = (ipp_uchar_t)attr->value_tag;
5590
5591 *bufptr++ = 0;
5592 *bufptr++ = 0;
5593 }
5594
5595 /*
5596 * Now write the attribute value(s)...
5597 */
5598
5599 switch (attr->value_tag & ~IPP_TAG_CUPS_CONST)
5600 {
5601 case IPP_TAG_UNSUPPORTED_VALUE :
5602 case IPP_TAG_DEFAULT :
5603 case IPP_TAG_UNKNOWN :
5604 case IPP_TAG_NOVALUE :
5605 case IPP_TAG_NOTSETTABLE :
5606 case IPP_TAG_DELETEATTR :
5607 case IPP_TAG_ADMINDEFINE :
5608 *bufptr++ = 0;
5609 *bufptr++ = 0;
5610 break;
5611
5612 case IPP_TAG_INTEGER :
5613 case IPP_TAG_ENUM :
5614 for (i = 0, value = attr->values;
5615 i < attr->num_values;
5616 i ++, value ++)
5617 {
5618 if ((IPP_BUF_SIZE - (bufptr - buffer)) < 9)
5619 {
5620 if ((*cb)(dst, buffer, (size_t)(bufptr - buffer)) < 0)
5621 {
5622 DEBUG_puts("1ippWriteIO: Could not write IPP "
5623 "attribute...");
5624 _cupsBufferRelease((char *)buffer);
5625 return (IPP_STATE_ERROR);
5626 }
5627
5628 bufptr = buffer;
5629 }
5630
5631 if (i)
5632 {
5633 /*
5634 * Arrays and sets are done by sending additional
5635 * values with a zero-length name...
5636 */
5637
5638 *bufptr++ = (ipp_uchar_t)attr->value_tag;
5639 *bufptr++ = 0;
5640 *bufptr++ = 0;
5641 }
5642
5643 /*
5644 * Integers and enumerations are both 4-byte signed
5645 * (twos-complement) values.
5646 *
5647 * Put the 2-byte length and 4-byte value into the buffer...
5648 */
5649
5650 *bufptr++ = 0;
5651 *bufptr++ = 4;
5652 *bufptr++ = (ipp_uchar_t)(value->integer >> 24);
5653 *bufptr++ = (ipp_uchar_t)(value->integer >> 16);
5654 *bufptr++ = (ipp_uchar_t)(value->integer >> 8);
5655 *bufptr++ = (ipp_uchar_t)value->integer;
5656 }
5657 break;
5658
5659 case IPP_TAG_BOOLEAN :
5660 for (i = 0, value = attr->values;
5661 i < attr->num_values;
5662 i ++, value ++)
5663 {
5664 if ((IPP_BUF_SIZE - (bufptr - buffer)) < 6)
5665 {
5666 if ((*cb)(dst, buffer, (size_t)(bufptr - buffer)) < 0)
5667 {
5668 DEBUG_puts("1ippWriteIO: Could not write IPP "
5669 "attribute...");
5670 _cupsBufferRelease((char *)buffer);
5671 return (IPP_STATE_ERROR);
5672 }
5673
5674 bufptr = buffer;
5675 }
5676
5677 if (i)
5678 {
5679 /*
5680 * Arrays and sets are done by sending additional
5681 * values with a zero-length name...
5682 */
5683
5684 *bufptr++ = (ipp_uchar_t)attr->value_tag;
5685 *bufptr++ = 0;
5686 *bufptr++ = 0;
5687 }
5688
5689 /*
5690 * Boolean values are 1-byte; 0 = false, 1 = true.
5691 *
5692 * Put the 2-byte length and 1-byte value into the buffer...
5693 */
5694
5695 *bufptr++ = 0;
5696 *bufptr++ = 1;
5697 *bufptr++ = (ipp_uchar_t)value->boolean;
5698 }
5699 break;
5700
5701 case IPP_TAG_TEXT :
5702 case IPP_TAG_NAME :
5703 case IPP_TAG_KEYWORD :
5704 case IPP_TAG_URI :
5705 case IPP_TAG_URISCHEME :
5706 case IPP_TAG_CHARSET :
5707 case IPP_TAG_LANGUAGE :
5708 case IPP_TAG_MIMETYPE :
5709 for (i = 0, value = attr->values;
5710 i < attr->num_values;
5711 i ++, value ++)
5712 {
5713 if (i)
5714 {
5715 /*
5716 * Arrays and sets are done by sending additional
5717 * values with a zero-length name...
5718 */
5719
5720 DEBUG_printf(("2ippWriteIO: writing value tag=%x(%s)",
5721 attr->value_tag,
5722 ippTagString(attr->value_tag)));
5723 DEBUG_printf(("2ippWriteIO: writing name=0,\"\""));
5724
5725 if ((IPP_BUF_SIZE - (bufptr - buffer)) < 3)
5726 {
5727 if ((*cb)(dst, buffer, (size_t)(bufptr - buffer)) < 0)
5728 {
5729 DEBUG_puts("1ippWriteIO: Could not write IPP "
5730 "attribute...");
5731 _cupsBufferRelease((char *)buffer);
5732 return (IPP_STATE_ERROR);
5733 }
5734
5735 bufptr = buffer;
5736 }
5737
5738 *bufptr++ = (ipp_uchar_t)attr->value_tag;
5739 *bufptr++ = 0;
5740 *bufptr++ = 0;
5741 }
5742
5743 if (value->string.text != NULL)
5744 n = (int)strlen(value->string.text);
5745 else
5746 n = 0;
5747
5748 if (n > (IPP_BUF_SIZE - 2))
5749 {
5750 DEBUG_printf(("1ippWriteIO: String too long (%d)", n));
5751 _cupsBufferRelease((char *)buffer);
5752 return (IPP_STATE_ERROR);
5753 }
5754
5755 DEBUG_printf(("2ippWriteIO: writing string=%d,\"%s\"", n,
5756 value->string.text));
5757
5758 if ((int)(IPP_BUF_SIZE - (bufptr - buffer)) < (n + 2))
5759 {
5760 if ((*cb)(dst, buffer, (size_t)(bufptr - buffer)) < 0)
5761 {
5762 DEBUG_puts("1ippWriteIO: Could not write IPP "
5763 "attribute...");
5764 _cupsBufferRelease((char *)buffer);
5765 return (IPP_STATE_ERROR);
5766 }
5767
5768 bufptr = buffer;
5769 }
5770
5771 /*
5772 * All simple strings consist of the 2-byte length and
5773 * character data without the trailing nul normally found
5774 * in C strings. Also, strings cannot be longer than IPP_MAX_LENGTH
5775 * bytes since the 2-byte length is a signed (twos-complement)
5776 * value.
5777 *
5778 * Put the 2-byte length and string characters in the buffer.
5779 */
5780
5781 *bufptr++ = (ipp_uchar_t)(n >> 8);
5782 *bufptr++ = (ipp_uchar_t)n;
5783
5784 if (n > 0)
5785 {
5786 memcpy(bufptr, value->string.text, (size_t)n);
5787 bufptr += n;
5788 }
5789 }
5790 break;
5791
5792 case IPP_TAG_DATE :
5793 for (i = 0, value = attr->values;
5794 i < attr->num_values;
5795 i ++, value ++)
5796 {
5797 if ((IPP_BUF_SIZE - (bufptr - buffer)) < 16)
5798 {
5799 if ((*cb)(dst, buffer, (size_t)(bufptr - buffer)) < 0)
5800 {
5801 DEBUG_puts("1ippWriteIO: Could not write IPP "
5802 "attribute...");
5803 _cupsBufferRelease((char *)buffer);
5804 return (IPP_STATE_ERROR);
5805 }
5806
5807 bufptr = buffer;
5808 }
5809
5810 if (i)
5811 {
5812 /*
5813 * Arrays and sets are done by sending additional
5814 * values with a zero-length name...
5815 */
5816
5817 *bufptr++ = (ipp_uchar_t)attr->value_tag;
5818 *bufptr++ = 0;
5819 *bufptr++ = 0;
5820 }
5821
5822 /*
5823 * Date values consist of a 2-byte length and an
5824 * 11-byte date/time structure defined by RFC 1903.
5825 *
5826 * Put the 2-byte length and 11-byte date/time
5827 * structure in the buffer.
5828 */
5829
5830 *bufptr++ = 0;
5831 *bufptr++ = 11;
5832 memcpy(bufptr, value->date, 11);
5833 bufptr += 11;
5834 }
5835 break;
5836
5837 case IPP_TAG_RESOLUTION :
5838 for (i = 0, value = attr->values;
5839 i < attr->num_values;
5840 i ++, value ++)
5841 {
5842 if ((IPP_BUF_SIZE - (bufptr - buffer)) < 14)
5843 {
5844 if ((*cb)(dst, buffer, (size_t)(bufptr - buffer)) < 0)
5845 {
5846 DEBUG_puts("1ippWriteIO: Could not write IPP "
5847 "attribute...");
5848 _cupsBufferRelease((char *)buffer);
5849 return (IPP_STATE_ERROR);
5850 }
5851
5852 bufptr = buffer;
5853 }
5854
5855 if (i)
5856 {
5857 /*
5858 * Arrays and sets are done by sending additional
5859 * values with a zero-length name...
5860 */
5861
5862 *bufptr++ = (ipp_uchar_t)attr->value_tag;
5863 *bufptr++ = 0;
5864 *bufptr++ = 0;
5865 }
5866
5867 /*
5868 * Resolution values consist of a 2-byte length,
5869 * 4-byte horizontal resolution value, 4-byte vertical
5870 * resolution value, and a 1-byte units value.
5871 *
5872 * Put the 2-byte length and resolution value data
5873 * into the buffer.
5874 */
5875
5876 *bufptr++ = 0;
5877 *bufptr++ = 9;
5878 *bufptr++ = (ipp_uchar_t)(value->resolution.xres >> 24);
5879 *bufptr++ = (ipp_uchar_t)(value->resolution.xres >> 16);
5880 *bufptr++ = (ipp_uchar_t)(value->resolution.xres >> 8);
5881 *bufptr++ = (ipp_uchar_t)value->resolution.xres;
5882 *bufptr++ = (ipp_uchar_t)(value->resolution.yres >> 24);
5883 *bufptr++ = (ipp_uchar_t)(value->resolution.yres >> 16);
5884 *bufptr++ = (ipp_uchar_t)(value->resolution.yres >> 8);
5885 *bufptr++ = (ipp_uchar_t)value->resolution.yres;
5886 *bufptr++ = (ipp_uchar_t)value->resolution.units;
5887 }
5888 break;
5889
5890 case IPP_TAG_RANGE :
5891 for (i = 0, value = attr->values;
5892 i < attr->num_values;
5893 i ++, value ++)
5894 {
5895 if ((IPP_BUF_SIZE - (bufptr - buffer)) < 13)
5896 {
5897 if ((*cb)(dst, buffer, (size_t)(bufptr - buffer)) < 0)
5898 {
5899 DEBUG_puts("1ippWriteIO: Could not write IPP "
5900 "attribute...");
5901 _cupsBufferRelease((char *)buffer);
5902 return (IPP_STATE_ERROR);
5903 }
5904
5905 bufptr = buffer;
5906 }
5907
5908 if (i)
5909 {
5910 /*
5911 * Arrays and sets are done by sending additional
5912 * values with a zero-length name...
5913 */
5914
5915 *bufptr++ = (ipp_uchar_t)attr->value_tag;
5916 *bufptr++ = 0;
5917 *bufptr++ = 0;
5918 }
5919
5920 /*
5921 * Range values consist of a 2-byte length,
5922 * 4-byte lower value, and 4-byte upper value.
5923 *
5924 * Put the 2-byte length and range value data
5925 * into the buffer.
5926 */
5927
5928 *bufptr++ = 0;
5929 *bufptr++ = 8;
5930 *bufptr++ = (ipp_uchar_t)(value->range.lower >> 24);
5931 *bufptr++ = (ipp_uchar_t)(value->range.lower >> 16);
5932 *bufptr++ = (ipp_uchar_t)(value->range.lower >> 8);
5933 *bufptr++ = (ipp_uchar_t)value->range.lower;
5934 *bufptr++ = (ipp_uchar_t)(value->range.upper >> 24);
5935 *bufptr++ = (ipp_uchar_t)(value->range.upper >> 16);
5936 *bufptr++ = (ipp_uchar_t)(value->range.upper >> 8);
5937 *bufptr++ = (ipp_uchar_t)value->range.upper;
5938 }
5939 break;
5940
5941 case IPP_TAG_TEXTLANG :
5942 case IPP_TAG_NAMELANG :
5943 for (i = 0, value = attr->values;
5944 i < attr->num_values;
5945 i ++, value ++)
5946 {
5947 if (i)
5948 {
5949 /*
5950 * Arrays and sets are done by sending additional
5951 * values with a zero-length name...
5952 */
5953
5954 if ((IPP_BUF_SIZE - (bufptr - buffer)) < 3)
5955 {
5956 if ((*cb)(dst, buffer, (size_t)(bufptr - buffer)) < 0)
5957 {
5958 DEBUG_puts("1ippWriteIO: Could not write IPP "
5959 "attribute...");
5960 _cupsBufferRelease((char *)buffer);
5961 return (IPP_STATE_ERROR);
5962 }
5963
5964 bufptr = buffer;
5965 }
5966
5967 *bufptr++ = (ipp_uchar_t)attr->value_tag;
5968 *bufptr++ = 0;
5969 *bufptr++ = 0;
5970 }
5971
5972 /*
5973 * textWithLanguage and nameWithLanguage values consist
5974 * of a 2-byte length for both strings and their
5975 * individual lengths, a 2-byte length for the
5976 * character string, the character string without the
5977 * trailing nul, a 2-byte length for the character
5978 * set string, and the character set string without
5979 * the trailing nul.
5980 */
5981
5982 n = 4;
5983
5984 if (value->string.language != NULL)
5985 n += (int)strlen(value->string.language);
5986
5987 if (value->string.text != NULL)
5988 n += (int)strlen(value->string.text);
5989
5990 if (n > (IPP_BUF_SIZE - 2))
5991 {
5992 DEBUG_printf(("1ippWriteIO: text/nameWithLanguage value "
5993 "too long (%d)", n));
5994 _cupsBufferRelease((char *)buffer);
5995 return (IPP_STATE_ERROR);
5996 }
5997
5998 if ((int)(IPP_BUF_SIZE - (bufptr - buffer)) < (n + 2))
5999 {
6000 if ((*cb)(dst, buffer, (size_t)(bufptr - buffer)) < 0)
6001 {
6002 DEBUG_puts("1ippWriteIO: Could not write IPP "
6003 "attribute...");
6004 _cupsBufferRelease((char *)buffer);
6005 return (IPP_STATE_ERROR);
6006 }
6007
6008 bufptr = buffer;
6009 }
6010
6011 /* Length of entire value */
6012 *bufptr++ = (ipp_uchar_t)(n >> 8);
6013 *bufptr++ = (ipp_uchar_t)n;
6014
6015 /* Length of language */
6016 if (value->string.language != NULL)
6017 n = (int)strlen(value->string.language);
6018 else
6019 n = 0;
6020
6021 *bufptr++ = (ipp_uchar_t)(n >> 8);
6022 *bufptr++ = (ipp_uchar_t)n;
6023
6024 /* Language */
6025 if (n > 0)
6026 {
6027 memcpy(bufptr, value->string.language, (size_t)n);
6028 bufptr += n;
6029 }
6030
6031 /* Length of text */
6032 if (value->string.text != NULL)
6033 n = (int)strlen(value->string.text);
6034 else
6035 n = 0;
6036
6037 *bufptr++ = (ipp_uchar_t)(n >> 8);
6038 *bufptr++ = (ipp_uchar_t)n;
6039
6040 /* Text */
6041 if (n > 0)
6042 {
6043 memcpy(bufptr, value->string.text, (size_t)n);
6044 bufptr += n;
6045 }
6046 }
6047 break;
6048
6049 case IPP_TAG_BEGIN_COLLECTION :
6050 for (i = 0, value = attr->values;
6051 i < attr->num_values;
6052 i ++, value ++)
6053 {
6054 /*
6055 * Collections are written with the begin-collection
6056 * tag first with a value of 0 length, followed by the
6057 * attributes in the collection, then the end-collection
6058 * value...
6059 */
6060
6061 if ((IPP_BUF_SIZE - (bufptr - buffer)) < 5)
6062 {
6063 if ((*cb)(dst, buffer, (size_t)(bufptr - buffer)) < 0)
6064 {
6065 DEBUG_puts("1ippWriteIO: Could not write IPP "
6066 "attribute...");
6067 _cupsBufferRelease((char *)buffer);
6068 return (IPP_STATE_ERROR);
6069 }
6070
6071 bufptr = buffer;
6072 }
6073
6074 if (i)
6075 {
6076 /*
6077 * Arrays and sets are done by sending additional
6078 * values with a zero-length name...
6079 */
6080
6081 *bufptr++ = (ipp_uchar_t)attr->value_tag;
6082 *bufptr++ = 0;
6083 *bufptr++ = 0;
6084 }
6085
6086 /*
6087 * Write a data length of 0 and flush the buffer...
6088 */
6089
6090 *bufptr++ = 0;
6091 *bufptr++ = 0;
6092
6093 if ((*cb)(dst, buffer, (size_t)(bufptr - buffer)) < 0)
6094 {
6095 DEBUG_puts("1ippWriteIO: Could not write IPP "
6096 "attribute...");
6097 _cupsBufferRelease((char *)buffer);
6098 return (IPP_STATE_ERROR);
6099 }
6100
6101 bufptr = buffer;
6102
6103 /*
6104 * Then write the collection attribute...
6105 */
6106
6107 value->collection->state = IPP_STATE_IDLE;
6108
6109 if (ippWriteIO(dst, cb, 1, ipp,
6110 value->collection) == IPP_STATE_ERROR)
6111 {
6112 DEBUG_puts("1ippWriteIO: Unable to write collection value");
6113 _cupsBufferRelease((char *)buffer);
6114 return (IPP_STATE_ERROR);
6115 }
6116 }
6117 break;
6118
6119 default :
6120 for (i = 0, value = attr->values;
6121 i < attr->num_values;
6122 i ++, value ++)
6123 {
6124 if (i)
6125 {
6126 /*
6127 * Arrays and sets are done by sending additional
6128 * values with a zero-length name...
6129 */
6130
6131 if ((IPP_BUF_SIZE - (bufptr - buffer)) < 3)
6132 {
6133 if ((*cb)(dst, buffer, (size_t)(bufptr - buffer)) < 0)
6134 {
6135 DEBUG_puts("1ippWriteIO: Could not write IPP "
6136 "attribute...");
6137 _cupsBufferRelease((char *)buffer);
6138 return (IPP_STATE_ERROR);
6139 }
6140
6141 bufptr = buffer;
6142 }
6143
6144 *bufptr++ = (ipp_uchar_t)attr->value_tag;
6145 *bufptr++ = 0;
6146 *bufptr++ = 0;
6147 }
6148
6149 /*
6150 * An unknown value might some new value that a
6151 * vendor has come up with. It consists of a
6152 * 2-byte length and the bytes in the unknown
6153 * value buffer.
6154 */
6155
6156 n = value->unknown.length;
6157
6158 if (n > (IPP_BUF_SIZE - 2))
6159 {
6160 DEBUG_printf(("1ippWriteIO: Data length too long (%d)",
6161 n));
6162 _cupsBufferRelease((char *)buffer);
6163 return (IPP_STATE_ERROR);
6164 }
6165
6166 if ((int)(IPP_BUF_SIZE - (bufptr - buffer)) < (n + 2))
6167 {
6168 if ((*cb)(dst, buffer, (size_t)(bufptr - buffer)) < 0)
6169 {
6170 DEBUG_puts("1ippWriteIO: Could not write IPP "
6171 "attribute...");
6172 _cupsBufferRelease((char *)buffer);
6173 return (IPP_STATE_ERROR);
6174 }
6175
6176 bufptr = buffer;
6177 }
6178
6179 /* Length of unknown value */
6180 *bufptr++ = (ipp_uchar_t)(n >> 8);
6181 *bufptr++ = (ipp_uchar_t)n;
6182
6183 /* Value */
6184 if (n > 0)
6185 {
6186 memcpy(bufptr, value->unknown.data, (size_t)n);
6187 bufptr += n;
6188 }
6189 }
6190 break;
6191 }
6192
6193 /*
6194 * Write the data out...
6195 */
6196
6197 if (bufptr > buffer)
6198 {
6199 if ((*cb)(dst, buffer, (size_t)(bufptr - buffer)) < 0)
6200 {
6201 DEBUG_puts("1ippWriteIO: Could not write IPP attribute...");
6202 _cupsBufferRelease((char *)buffer);
6203 return (IPP_STATE_ERROR);
6204 }
6205
6206 DEBUG_printf(("2ippWriteIO: wrote %d bytes",
6207 (int)(bufptr - buffer)));
6208 }
6209
6210 /*
6211 * If blocking is disabled and we aren't at the end of the attribute
6212 * list, stop here...
6213 */
6214
6215 if (!blocking && ipp->current)
6216 break;
6217 }
6218
6219 if (ipp->current == NULL)
6220 {
6221 /*
6222 * Done with all of the attributes; add the end-of-attributes
6223 * tag or end-collection attribute...
6224 */
6225
6226 if (parent == NULL)
6227 {
6228 buffer[0] = IPP_TAG_END;
6229 n = 1;
6230 }
6231 else
6232 {
6233 buffer[0] = IPP_TAG_END_COLLECTION;
6234 buffer[1] = 0; /* empty name */
6235 buffer[2] = 0;
6236 buffer[3] = 0; /* empty value */
6237 buffer[4] = 0;
6238 n = 5;
6239 }
6240
6241 if ((*cb)(dst, buffer, (size_t)n) < 0)
6242 {
6243 DEBUG_puts("1ippWriteIO: Could not write IPP end-tag...");
6244 _cupsBufferRelease((char *)buffer);
6245 return (IPP_STATE_ERROR);
6246 }
6247
6248 ipp->state = IPP_STATE_DATA;
6249 }
6250 break;
6251
6252 case IPP_STATE_DATA :
6253 break;
6254
6255 default :
6256 break; /* anti-compiler-warning-code */
6257 }
6258
6259 _cupsBufferRelease((char *)buffer);
6260
6261 return (ipp->state);
6262 }
6263
6264
6265 /*
6266 * 'ipp_add_attr()' - Add a new attribute to the message.
6267 */
6268
6269 static ipp_attribute_t * /* O - New attribute */
6270 ipp_add_attr(ipp_t *ipp, /* I - IPP message */
6271 const char *name, /* I - Attribute name or NULL */
6272 ipp_tag_t group_tag, /* I - Group tag or IPP_TAG_ZERO */
6273 ipp_tag_t value_tag, /* I - Value tag or IPP_TAG_ZERO */
6274 int num_values) /* I - Number of values */
6275 {
6276 int alloc_values; /* Number of values to allocate */
6277 ipp_attribute_t *attr; /* New attribute */
6278
6279
6280 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));
6281
6282 /*
6283 * Range check input...
6284 */
6285
6286 if (!ipp || num_values < 0)
6287 return (NULL);
6288
6289 /*
6290 * Allocate memory, rounding the allocation up as needed...
6291 */
6292
6293 if (num_values <= 1)
6294 alloc_values = 1;
6295 else
6296 alloc_values = (num_values + IPP_MAX_VALUES - 1) & ~(IPP_MAX_VALUES - 1);
6297
6298 attr = calloc(sizeof(ipp_attribute_t) +
6299 (size_t)(alloc_values - 1) * sizeof(_ipp_value_t), 1);
6300
6301 if (attr)
6302 {
6303 /*
6304 * Initialize attribute...
6305 */
6306
6307 DEBUG_printf(("4debug_alloc: %p %s %s%s (%d values)", (void *)attr, name, num_values > 1 ? "1setOf " : "", ippTagString(value_tag), num_values));
6308
6309 if (name)
6310 attr->name = _cupsStrAlloc(name);
6311
6312 attr->group_tag = group_tag;
6313 attr->value_tag = value_tag;
6314 attr->num_values = num_values;
6315
6316 /*
6317 * Add it to the end of the linked list...
6318 */
6319
6320 if (ipp->last)
6321 ipp->last->next = attr;
6322 else
6323 ipp->attrs = attr;
6324
6325 ipp->prev = ipp->last;
6326 ipp->last = ipp->current = attr;
6327 }
6328
6329 DEBUG_printf(("5ipp_add_attr: Returning %p", (void *)attr));
6330
6331 return (attr);
6332 }
6333
6334
6335 /*
6336 * 'ipp_free_values()' - Free attribute values.
6337 */
6338
6339 static void
6340 ipp_free_values(ipp_attribute_t *attr, /* I - Attribute to free values from */
6341 int element,/* I - First value to free */
6342 int count) /* I - Number of values to free */
6343 {
6344 int i; /* Looping var */
6345 _ipp_value_t *value; /* Current value */
6346
6347
6348 DEBUG_printf(("4ipp_free_values(attr=%p, element=%d, count=%d)", (void *)attr, element, count));
6349
6350 if (!(attr->value_tag & IPP_TAG_CUPS_CONST))
6351 {
6352 /*
6353 * Free values as needed...
6354 */
6355
6356 switch (attr->value_tag)
6357 {
6358 case IPP_TAG_TEXTLANG :
6359 case IPP_TAG_NAMELANG :
6360 if (element == 0 && count == attr->num_values &&
6361 attr->values[0].string.language)
6362 {
6363 _cupsStrFree(attr->values[0].string.language);
6364 attr->values[0].string.language = NULL;
6365 }
6366 /* Fall through to other string values */
6367
6368 case IPP_TAG_TEXT :
6369 case IPP_TAG_NAME :
6370 case IPP_TAG_RESERVED_STRING :
6371 case IPP_TAG_KEYWORD :
6372 case IPP_TAG_URI :
6373 case IPP_TAG_URISCHEME :
6374 case IPP_TAG_CHARSET :
6375 case IPP_TAG_LANGUAGE :
6376 case IPP_TAG_MIMETYPE :
6377 for (i = count, value = attr->values + element;
6378 i > 0;
6379 i --, value ++)
6380 {
6381 _cupsStrFree(value->string.text);
6382 value->string.text = NULL;
6383 }
6384 break;
6385
6386 case IPP_TAG_DEFAULT :
6387 case IPP_TAG_UNKNOWN :
6388 case IPP_TAG_NOVALUE :
6389 case IPP_TAG_NOTSETTABLE :
6390 case IPP_TAG_DELETEATTR :
6391 case IPP_TAG_ADMINDEFINE :
6392 case IPP_TAG_INTEGER :
6393 case IPP_TAG_ENUM :
6394 case IPP_TAG_BOOLEAN :
6395 case IPP_TAG_DATE :
6396 case IPP_TAG_RESOLUTION :
6397 case IPP_TAG_RANGE :
6398 break;
6399
6400 case IPP_TAG_BEGIN_COLLECTION :
6401 for (i = count, value = attr->values + element;
6402 i > 0;
6403 i --, value ++)
6404 {
6405 ippDelete(value->collection);
6406 value->collection = NULL;
6407 }
6408 break;
6409
6410 case IPP_TAG_STRING :
6411 default :
6412 for (i = count, value = attr->values + element;
6413 i > 0;
6414 i --, value ++)
6415 {
6416 if (value->unknown.data)
6417 {
6418 free(value->unknown.data);
6419 value->unknown.data = NULL;
6420 }
6421 }
6422 break;
6423 }
6424 }
6425
6426 /*
6427 * If we are not freeing values from the end, move the remaining values up...
6428 */
6429
6430 if ((element + count) < attr->num_values)
6431 memmove(attr->values + element, attr->values + element + count,
6432 (size_t)(attr->num_values - count - element) * sizeof(_ipp_value_t));
6433
6434 attr->num_values -= count;
6435 }
6436
6437
6438 /*
6439 * 'ipp_get_code()' - Convert a C locale/charset name into an IPP language/charset code.
6440 *
6441 * This typically converts strings of the form "ll_CC", "ll-REGION", and "CHARSET_NUMBER"
6442 * to "ll-cc", "ll-region", and "charset-number", respectively.
6443 */
6444
6445 static char * /* O - Language code string */
6446 ipp_get_code(const char *value, /* I - Locale/charset string */
6447 char *buffer, /* I - String buffer */
6448 size_t bufsize) /* I - Size of string buffer */
6449 {
6450 char *bufptr, /* Pointer into buffer */
6451 *bufend; /* End of buffer */
6452
6453
6454 /*
6455 * Convert values to lowercase and change _ to - as needed...
6456 */
6457
6458 for (bufptr = buffer, bufend = buffer + bufsize - 1;
6459 *value && bufptr < bufend;
6460 value ++)
6461 if (*value == '_')
6462 *bufptr++ = '-';
6463 else
6464 *bufptr++ = (char)_cups_tolower(*value);
6465
6466 *bufptr = '\0';
6467
6468 /*
6469 * Return the converted string...
6470 */
6471
6472 return (buffer);
6473 }
6474
6475
6476 /*
6477 * 'ipp_lang_code()' - Convert a C locale name into an IPP language code.
6478 *
6479 * This typically converts strings of the form "ll_CC" and "ll-REGION" to "ll-cc" and
6480 * "ll-region", respectively. It also converts the "C" (POSIX) locale to "en".
6481 */
6482
6483 static char * /* O - Language code string */
6484 ipp_lang_code(const char *locale, /* I - Locale string */
6485 char *buffer, /* I - String buffer */
6486 size_t bufsize) /* I - Size of string buffer */
6487 {
6488 /*
6489 * Map POSIX ("C") locale to generic English, otherwise convert the locale string as-is.
6490 */
6491
6492 if (!_cups_strcasecmp(locale, "c"))
6493 {
6494 strlcpy(buffer, "en", bufsize);
6495 return (buffer);
6496 }
6497 else
6498 return (ipp_get_code(locale, buffer, bufsize));
6499 }
6500
6501
6502 /*
6503 * 'ipp_length()' - Compute the length of an IPP message or collection value.
6504 */
6505
6506 static size_t /* O - Size of IPP message */
6507 ipp_length(ipp_t *ipp, /* I - IPP message or collection */
6508 int collection) /* I - 1 if a collection, 0 otherwise */
6509 {
6510 int i; /* Looping var */
6511 size_t bytes; /* Number of bytes */
6512 ipp_attribute_t *attr; /* Current attribute */
6513 ipp_tag_t group; /* Current group */
6514 _ipp_value_t *value; /* Current value */
6515
6516
6517 DEBUG_printf(("3ipp_length(ipp=%p, collection=%d)", (void *)ipp, collection));
6518
6519 if (!ipp)
6520 {
6521 DEBUG_puts("4ipp_length: Returning 0 bytes");
6522 return (0);
6523 }
6524
6525 /*
6526 * Start with 8 bytes for the IPP message header...
6527 */
6528
6529 bytes = collection ? 0 : 8;
6530
6531 /*
6532 * Then add the lengths of each attribute...
6533 */
6534
6535 group = IPP_TAG_ZERO;
6536
6537 for (attr = ipp->attrs; attr != NULL; attr = attr->next)
6538 {
6539 if (attr->group_tag != group && !collection)
6540 {
6541 group = attr->group_tag;
6542 if (group == IPP_TAG_ZERO)
6543 continue;
6544
6545 bytes ++; /* Group tag */
6546 }
6547
6548 if (!attr->name)
6549 continue;
6550
6551 DEBUG_printf(("5ipp_length: attr->name=\"%s\", attr->num_values=%d, "
6552 "bytes=" CUPS_LLFMT, attr->name, attr->num_values, CUPS_LLCAST bytes));
6553
6554 if ((attr->value_tag & ~IPP_TAG_CUPS_CONST) < IPP_TAG_EXTENSION)
6555 bytes += (size_t)attr->num_values;/* Value tag for each value */
6556 else
6557 bytes += (size_t)(5 * attr->num_values);
6558 /* Value tag for each value */
6559 bytes += (size_t)(2 * attr->num_values);
6560 /* Name lengths */
6561 bytes += strlen(attr->name); /* Name */
6562 bytes += (size_t)(2 * attr->num_values);
6563 /* Value lengths */
6564
6565 if (collection)
6566 bytes += 5; /* Add membername overhead */
6567
6568 switch (attr->value_tag & ~IPP_TAG_CUPS_CONST)
6569 {
6570 case IPP_TAG_UNSUPPORTED_VALUE :
6571 case IPP_TAG_DEFAULT :
6572 case IPP_TAG_UNKNOWN :
6573 case IPP_TAG_NOVALUE :
6574 case IPP_TAG_NOTSETTABLE :
6575 case IPP_TAG_DELETEATTR :
6576 case IPP_TAG_ADMINDEFINE :
6577 break;
6578
6579 case IPP_TAG_INTEGER :
6580 case IPP_TAG_ENUM :
6581 bytes += (size_t)(4 * attr->num_values);
6582 break;
6583
6584 case IPP_TAG_BOOLEAN :
6585 bytes += (size_t)attr->num_values;
6586 break;
6587
6588 case IPP_TAG_TEXT :
6589 case IPP_TAG_NAME :
6590 case IPP_TAG_KEYWORD :
6591 case IPP_TAG_URI :
6592 case IPP_TAG_URISCHEME :
6593 case IPP_TAG_CHARSET :
6594 case IPP_TAG_LANGUAGE :
6595 case IPP_TAG_MIMETYPE :
6596 for (i = 0, value = attr->values;
6597 i < attr->num_values;
6598 i ++, value ++)
6599 if (value->string.text)
6600 bytes += strlen(value->string.text);
6601 break;
6602
6603 case IPP_TAG_DATE :
6604 bytes += (size_t)(11 * attr->num_values);
6605 break;
6606
6607 case IPP_TAG_RESOLUTION :
6608 bytes += (size_t)(9 * attr->num_values);
6609 break;
6610
6611 case IPP_TAG_RANGE :
6612 bytes += (size_t)(8 * attr->num_values);
6613 break;
6614
6615 case IPP_TAG_TEXTLANG :
6616 case IPP_TAG_NAMELANG :
6617 bytes += (size_t)(4 * attr->num_values);
6618 /* Charset + text length */
6619
6620 for (i = 0, value = attr->values;
6621 i < attr->num_values;
6622 i ++, value ++)
6623 {
6624 if (value->string.language)
6625 bytes += strlen(value->string.language);
6626
6627 if (value->string.text)
6628 bytes += strlen(value->string.text);
6629 }
6630 break;
6631
6632 case IPP_TAG_BEGIN_COLLECTION :
6633 for (i = 0, value = attr->values;
6634 i < attr->num_values;
6635 i ++, value ++)
6636 bytes += ipp_length(value->collection, 1);
6637 break;
6638
6639 default :
6640 for (i = 0, value = attr->values;
6641 i < attr->num_values;
6642 i ++, value ++)
6643 bytes += (size_t)value->unknown.length;
6644 break;
6645 }
6646 }
6647
6648 /*
6649 * Finally, add 1 byte for the "end of attributes" tag or 5 bytes
6650 * for the "end of collection" tag and return...
6651 */
6652
6653 if (collection)
6654 bytes += 5;
6655 else
6656 bytes ++;
6657
6658 DEBUG_printf(("4ipp_length: Returning " CUPS_LLFMT " bytes", CUPS_LLCAST bytes));
6659
6660 return (bytes);
6661 }
6662
6663
6664 /*
6665 * 'ipp_read_http()' - Semi-blocking read on a HTTP connection...
6666 */
6667
6668 static ssize_t /* O - Number of bytes read */
6669 ipp_read_http(http_t *http, /* I - Client connection */
6670 ipp_uchar_t *buffer, /* O - Buffer for data */
6671 size_t length) /* I - Total length */
6672 {
6673 ssize_t tbytes, /* Total bytes read */
6674 bytes; /* Bytes read this pass */
6675
6676
6677 DEBUG_printf(("7ipp_read_http(http=%p, buffer=%p, length=%d)", (void *)http, (void *)buffer, (int)length));
6678
6679 /*
6680 * Loop until all bytes are read...
6681 */
6682
6683 for (tbytes = 0, bytes = 0;
6684 tbytes < (int)length;
6685 tbytes += bytes, buffer += bytes)
6686 {
6687 DEBUG_printf(("9ipp_read_http: tbytes=" CUPS_LLFMT ", http->state=%d", CUPS_LLCAST tbytes, http->state));
6688
6689 if (http->state == HTTP_STATE_WAITING)
6690 break;
6691
6692 if (http->used == 0 && !http->blocking)
6693 {
6694 /*
6695 * Wait up to 10 seconds for more data on non-blocking sockets...
6696 */
6697
6698 if (!httpWait(http, 10000))
6699 {
6700 /*
6701 * Signal no data...
6702 */
6703
6704 bytes = -1;
6705 break;
6706 }
6707 }
6708 else if (http->used == 0 && http->timeout_value > 0)
6709 {
6710 /*
6711 * Wait up to timeout seconds for more data on blocking sockets...
6712 */
6713
6714 if (!httpWait(http, (int)(1000 * http->timeout_value)))
6715 {
6716 /*
6717 * Signal no data...
6718 */
6719
6720 bytes = -1;
6721 break;
6722 }
6723 }
6724
6725 if ((bytes = httpRead2(http, (char *)buffer, length - (size_t)tbytes)) < 0)
6726 {
6727 #ifdef WIN32
6728 break;
6729 #else
6730 if (errno != EAGAIN && errno != EINTR)
6731 break;
6732
6733 bytes = 0;
6734 #endif /* WIN32 */
6735 }
6736 else if (bytes == 0)
6737 break;
6738 }
6739
6740 /*
6741 * Return the number of bytes read...
6742 */
6743
6744 if (tbytes == 0 && bytes < 0)
6745 tbytes = -1;
6746
6747 DEBUG_printf(("8ipp_read_http: Returning " CUPS_LLFMT " bytes", CUPS_LLCAST tbytes));
6748
6749 return (tbytes);
6750 }
6751
6752
6753 /*
6754 * 'ipp_read_file()' - Read IPP data from a file.
6755 */
6756
6757 static ssize_t /* O - Number of bytes read */
6758 ipp_read_file(int *fd, /* I - File descriptor */
6759 ipp_uchar_t *buffer, /* O - Read buffer */
6760 size_t length) /* I - Number of bytes to read */
6761 {
6762 #ifdef WIN32
6763 return ((ssize_t)read(*fd, buffer, (unsigned)length));
6764 #else
6765 return (read(*fd, buffer, length));
6766 #endif /* WIN32 */
6767 }
6768
6769
6770 /*
6771 * 'ipp_set_error()' - Set a formatted, localized error string.
6772 */
6773
6774 static void
6775 ipp_set_error(ipp_status_t status, /* I - Status code */
6776 const char *format, /* I - Printf-style error string */
6777 ...) /* I - Additional arguments as needed */
6778 {
6779 va_list ap; /* Pointer to additional args */
6780 char buffer[2048]; /* Message buffer */
6781 cups_lang_t *lang = cupsLangDefault();
6782 /* Current language */
6783
6784
6785 va_start(ap, format);
6786 vsnprintf(buffer, sizeof(buffer), _cupsLangString(lang, format), ap);
6787 va_end(ap);
6788
6789 _cupsSetError(status, buffer, 0);
6790 }
6791
6792
6793 /*
6794 * 'ipp_set_value()' - Get the value element from an attribute, expanding it as
6795 * needed.
6796 */
6797
6798 static _ipp_value_t * /* O - IPP value element or NULL on error */
6799 ipp_set_value(ipp_t *ipp, /* IO - IPP message */
6800 ipp_attribute_t **attr, /* IO - IPP attribute */
6801 int element) /* I - Value number (0-based) */
6802 {
6803 ipp_attribute_t *temp, /* New attribute pointer */
6804 *current, /* Current attribute in list */
6805 *prev; /* Previous attribute in list */
6806 int alloc_values; /* Allocated values */
6807
6808
6809 /*
6810 * If we are setting an existing value element, return it...
6811 */
6812
6813 temp = *attr;
6814
6815 if (temp->num_values <= 1)
6816 alloc_values = 1;
6817 else
6818 alloc_values = (temp->num_values + IPP_MAX_VALUES - 1) &
6819 ~(IPP_MAX_VALUES - 1);
6820
6821 if (element < alloc_values)
6822 {
6823 if (element >= temp->num_values)
6824 temp->num_values = element + 1;
6825
6826 return (temp->values + element);
6827 }
6828
6829 /*
6830 * Otherwise re-allocate the attribute - we allocate in groups of IPP_MAX_VALUE
6831 * values when num_values > 1.
6832 */
6833
6834 if (alloc_values < IPP_MAX_VALUES)
6835 alloc_values = IPP_MAX_VALUES;
6836 else
6837 alloc_values += IPP_MAX_VALUES;
6838
6839 DEBUG_printf(("4ipp_set_value: Reallocating for up to %d values.",
6840 alloc_values));
6841
6842 /*
6843 * Reallocate memory...
6844 */
6845
6846 if ((temp = realloc(temp, sizeof(ipp_attribute_t) + (size_t)(alloc_values - 1) * sizeof(_ipp_value_t))) == NULL)
6847 {
6848 _cupsSetHTTPError(HTTP_STATUS_ERROR);
6849 DEBUG_puts("4ipp_set_value: Unable to resize attribute.");
6850 return (NULL);
6851 }
6852
6853 /*
6854 * Zero the new memory...
6855 */
6856
6857 memset(temp->values + temp->num_values, 0, (size_t)(alloc_values - temp->num_values) * sizeof(_ipp_value_t));
6858
6859 if (temp != *attr)
6860 {
6861 /*
6862 * Reset pointers in the list...
6863 */
6864
6865 DEBUG_printf(("4debug_free: %p %s", (void *)*attr, temp->name));
6866 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));
6867
6868 if (ipp->current == *attr && ipp->prev)
6869 {
6870 /*
6871 * Use current "previous" pointer...
6872 */
6873
6874 prev = ipp->prev;
6875 }
6876 else
6877 {
6878 /*
6879 * Find this attribute in the linked list...
6880 */
6881
6882 for (prev = NULL, current = ipp->attrs;
6883 current && current != *attr;
6884 prev = current, current = current->next);
6885
6886 if (!current)
6887 {
6888 /*
6889 * This is a serious error!
6890 */
6891
6892 *attr = temp;
6893 _cupsSetError(IPP_STATUS_ERROR_INTERNAL,
6894 _("IPP attribute is not a member of the message."), 1);
6895 DEBUG_puts("4ipp_set_value: Unable to find attribute in message.");
6896 return (NULL);
6897 }
6898 }
6899
6900 if (prev)
6901 prev->next = temp;
6902 else
6903 ipp->attrs = temp;
6904
6905 ipp->current = temp;
6906 ipp->prev = prev;
6907
6908 if (ipp->last == *attr)
6909 ipp->last = temp;
6910
6911 *attr = temp;
6912 }
6913
6914 /*
6915 * Return the value element...
6916 */
6917
6918 if (element >= temp->num_values)
6919 temp->num_values = element + 1;
6920
6921 return (temp->values + element);
6922 }
6923
6924
6925 /*
6926 * 'ipp_write_file()' - Write IPP data to a file.
6927 */
6928
6929 static ssize_t /* O - Number of bytes written */
6930 ipp_write_file(int *fd, /* I - File descriptor */
6931 ipp_uchar_t *buffer, /* I - Data to write */
6932 size_t length) /* I - Number of bytes to write */
6933 {
6934 #ifdef WIN32
6935 return ((ssize_t)write(*fd, buffer, (unsigned)length));
6936 #else
6937 return (write(*fd, buffer, length));
6938 #endif /* WIN32 */
6939 }