]> git.ipfire.org Git - thirdparty/cups.git/blame - cups/ipp.c
Block invalid group tags when parsing IPP messages.
[thirdparty/cups.git] / cups / ipp.c
CommitLineData
ef416fc2 1/*
7e86f2f6
MS
2 * Internet Printing Protocol functions for CUPS.
3 *
4cadd620
MS
4 * Copyright © 2007-2018 by Apple Inc.
5 * Copyright © 1997-2007 by Easy Software Products, all rights reserved.
7e86f2f6 6 *
4cadd620
MS
7 * Licensed under Apache License v2.0. See the file "LICENSE" for more
8 * information.
ef416fc2 9 */
10
11/*
12 * Include necessary headers...
13 */
14
71e16022 15#include "cups-private.h"
c1420c87 16#include <regex.h>
ef416fc2 17#ifdef WIN32
18# include <io.h>
19#endif /* WIN32 */
20
21
22/*
23 * Local functions...
24 */
25
82cc1f9a
MS
26static 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);
29static void ipp_free_values(ipp_attribute_t *attr, int element,
30 int count);
31static char *ipp_get_code(const char *locale, char *buffer,
32 size_t bufsize)
33 __attribute__((nonnull(1,2)));
34static char *ipp_lang_code(const char *locale, char *buffer,
35 size_t bufsize)
36 __attribute__((nonnull(1,2)));
ef416fc2 37static size_t ipp_length(ipp_t *ipp, int collection);
a4d04587 38static ssize_t ipp_read_http(http_t *http, ipp_uchar_t *buffer,
39 size_t length);
40static ssize_t ipp_read_file(int *fd, ipp_uchar_t *buffer,
41 size_t length);
c1420c87
MS
42static void ipp_set_error(ipp_status_t status, const char *format,
43 ...);
82cc1f9a
MS
44static _ipp_value_t *ipp_set_value(ipp_t *ipp, ipp_attribute_t **attr,
45 int element);
a4d04587 46static ssize_t ipp_write_file(int *fd, ipp_uchar_t *buffer,
47 size_t length);
ef416fc2 48
49
dcb445bc
MS
50/*
51 * '_cupsBufferGet()' - Get a read/write buffer.
52 */
53
54char * /* 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
86void
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
ef416fc2 101/*
102 * 'ippAddBoolean()' - Add a boolean attribute to an IPP message.
a2326b5b 103 *
a469f8a5
MS
104 * The @code ipp@ parameter refers to an IPP message previously created using
105 * the @link ippNew@, @link ippNewRequest@, or @link ippNewResponse@ functions.
a2326b5b
MS
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@).
ef416fc2 112 */
113
114ipp_attribute_t * /* O - New attribute */
115ippAddBoolean(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
807315e6 123 DEBUG_printf(("ippAddBoolean(ipp=%p, group=%02x(%s), name=\"%s\", value=%d)", (void *)ipp, group, ippTagString(group), name, value));
ef416fc2 124
a2326b5b
MS
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)
ef416fc2 131 return (NULL);
132
a2326b5b
MS
133 /*
134 * Create the attribute...
135 */
136
137 if ((attr = ipp_add_attr(ipp, name, group, IPP_TAG_BOOLEAN, 1)) == NULL)
ef416fc2 138 return (NULL);
139
ef416fc2 140 attr->values[0].boolean = value;
141
142 return (attr);
143}
144
145
146/*
147 * 'ippAddBooleans()' - Add an array of boolean values.
a2326b5b 148 *
a469f8a5
MS
149 * The @code ipp@ parameter refers to an IPP message previously created using
150 * the @link ippNew@, @link ippNewRequest@, or @link ippNewResponse@ functions.
a2326b5b
MS
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@).
ef416fc2 157 */
158
159ipp_attribute_t * /* O - New attribute */
160ippAddBooleans(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 */
a2326b5b 168 _ipp_value_t *value; /* Current value */
ef416fc2 169
170
807315e6 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));
ef416fc2 172
a2326b5b
MS
173 /*
174 * Range check input...
175 */
ef416fc2 176
a2326b5b
MS
177 if (!ipp || !name || group < IPP_TAG_ZERO ||
178 group == IPP_TAG_END || group >= IPP_TAG_UNSUPPORTED_VALUE ||
179 num_values < 1)
ef416fc2 180 return (NULL);
181
a2326b5b
MS
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);
ef416fc2 188
a2326b5b
MS
189 if (values)
190 {
191 for (i = num_values, value = attr->values;
192 i > 0;
193 i --, value ++)
194 value->boolean = *values++;
195 }
ef416fc2 196
197 return (attr);
198}
199
200
201/*
202 * 'ippAddCollection()' - Add a collection value.
203 *
a469f8a5
MS
204 * The @code ipp@ parameter refers to an IPP message previously created using
205 * the @link ippNew@, @link ippNewRequest@, or @link ippNewResponse@ functions.
a2326b5b
MS
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 *
8072030b 213 * @since CUPS 1.1.19/macOS 10.3@
ef416fc2 214 */
215
216ipp_attribute_t * /* O - New attribute */
217ippAddCollection(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
807315e6 225 DEBUG_printf(("ippAddCollection(ipp=%p, group=%02x(%s), name=\"%s\", value=%p)", (void *)ipp, group, ippTagString(group), name, (void *)value));
ef416fc2 226
a2326b5b
MS
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)
ef416fc2 233 return (NULL);
234
a2326b5b
MS
235 /*
236 * Create the attribute...
237 */
238
239 if ((attr = ipp_add_attr(ipp, name, group, IPP_TAG_BEGIN_COLLECTION, 1)) == NULL)
ef416fc2 240 return (NULL);
241
ef416fc2 242 attr->values[0].collection = value;
243
9c80ffa2
MS
244 if (value)
245 value->use ++;
aaf19ab0 246
ef416fc2 247 return (attr);
248}
249
250
251/*
252 * 'ippAddCollections()' - Add an array of collection values.
253 *
a469f8a5
MS
254 * The @code ipp@ parameter refers to an IPP message previously created using
255 * the @link ippNew@, @link ippNewRequest@, or @link ippNewResponse@ functions.
a2326b5b
MS
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 *
8072030b 263 * @since CUPS 1.1.19/macOS 10.3@
ef416fc2 264 */
265
266ipp_attribute_t * /* O - New attribute */
267ippAddCollections(
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 */
a2326b5b 276 _ipp_value_t *value; /* Current value */
ef416fc2 277
278
807315e6 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));
ef416fc2 280
a2326b5b
MS
281 /*
282 * Range check input...
283 */
ef416fc2 284
a2326b5b
MS
285 if (!ipp || !name || group < IPP_TAG_ZERO ||
286 group == IPP_TAG_END || group >= IPP_TAG_UNSUPPORTED_VALUE ||
287 num_values < 1)
ef416fc2 288 return (NULL);
289
a2326b5b
MS
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);
ef416fc2 297
a2326b5b 298 if (values)
aaf19ab0 299 {
a2326b5b
MS
300 for (i = num_values, value = attr->values;
301 i > 0;
302 i --, value ++)
aaf19ab0 303 {
a2326b5b 304 value->collection = (ipp_t *)*values++;
aaf19ab0
MS
305 value->collection->use ++;
306 }
307 }
ef416fc2 308
309 return (attr);
310}
311
312
313/*
65bebeac 314 * 'ippAddDate()' - Add a dateTime attribute to an IPP message.
a2326b5b 315 *
a469f8a5
MS
316 * The @code ipp@ parameter refers to an IPP message previously created using
317 * the @link ippNew@, @link ippNewRequest@, or @link ippNewResponse@ functions.
a2326b5b
MS
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@).
ef416fc2 324 */
325
326ipp_attribute_t * /* O - New attribute */
327ippAddDate(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
807315e6 335 DEBUG_printf(("ippAddDate(ipp=%p, group=%02x(%s), name=\"%s\", value=%p)", (void *)ipp, group, ippTagString(group), name, (void *)value));
ef416fc2 336
a2326b5b
MS
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)
ef416fc2 343 return (NULL);
344
a2326b5b
MS
345 /*
346 * Create the attribute...
347 */
348
349 if ((attr = ipp_add_attr(ipp, name, group, IPP_TAG_DATE, 1)) == NULL)
ef416fc2 350 return (NULL);
351
ef416fc2 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.
a2326b5b 360 *
a469f8a5
MS
361 * The @code ipp@ parameter refers to an IPP message previously created using
362 * the @link ippNew@, @link ippNewRequest@, or @link ippNewResponse@ functions.
a2326b5b
MS
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@).
ef416fc2 372 */
373
374ipp_attribute_t * /* O - New attribute */
375ippAddInteger(ipp_t *ipp, /* I - IPP message */
376 ipp_tag_t group, /* I - IPP group */
a2326b5b 377 ipp_tag_t value_tag, /* I - Type of attribute */
ef416fc2 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
807315e6 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));
ef416fc2 385
cb7f98ee 386 value_tag &= IPP_TAG_CUPS_MASK;
a2326b5b
MS
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)
ef416fc2 408 return (NULL);
a2326b5b 409#endif /* 0 */
ef416fc2 410
a2326b5b
MS
411 /*
412 * Create the attribute...
413 */
414
415 if ((attr = ipp_add_attr(ipp, name, group, value_tag, 1)) == NULL)
ef416fc2 416 return (NULL);
417
ef416fc2 418 attr->values[0].integer = value;
419
420 return (attr);
421}
422
423
424/*
425 * 'ippAddIntegers()' - Add an array of integer values.
a2326b5b 426 *
a469f8a5
MS
427 * The @code ipp@ parameter refers to an IPP message previously created using
428 * the @link ippNew@, @link ippNewRequest@, or @link ippNewResponse@ functions.
a2326b5b
MS
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@).
ef416fc2 438 */
439
440ipp_attribute_t * /* O - New attribute */
441ippAddIntegers(ipp_t *ipp, /* I - IPP message */
442 ipp_tag_t group, /* I - IPP group */
a2326b5b 443 ipp_tag_t value_tag, /* I - Type of attribute */
ef416fc2 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 */
a2326b5b 450 _ipp_value_t *value; /* Current value */
ef416fc2 451
452
807315e6 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));
1ff0402e 454
cb7f98ee 455 value_tag &= IPP_TAG_CUPS_MASK;
a2326b5b
MS
456
457 /*
458 * Range check input...
459 */
ef416fc2 460
a2326b5b
MS
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)
ef416fc2 471 return (NULL);
a2326b5b 472#endif /* 0 */
ef416fc2 473
a2326b5b
MS
474 /*
475 * Create the attribute...
476 */
477
478 if ((attr = ipp_add_attr(ipp, name, group, value_tag, num_values)) == NULL)
479 return (NULL);
ef416fc2 480
a2326b5b
MS
481 if (values)
482 {
483 for (i = num_values, value = attr->values;
484 i > 0;
485 i --, value ++)
486 value->integer = *values++;
487 }
ef416fc2 488
489 return (attr);
490}
491
492
493/*
494 * 'ippAddOctetString()' - Add an octetString value to an IPP message.
495 *
a469f8a5
MS
496 * The @code ipp@ parameter refers to an IPP message previously created using
497 * the @link ippNew@, @link ippNewRequest@, or @link ippNewResponse@ functions.
a2326b5b
MS
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 *
8072030b 505 * @since CUPS 1.2/macOS 10.5@
ef416fc2 506 */
507
508ipp_attribute_t * /* O - New attribute */
509ippAddOctetString(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
a2326b5b 518 if (!ipp || !name || group < IPP_TAG_ZERO ||
5a9febac 519 group == IPP_TAG_END || group >= IPP_TAG_UNSUPPORTED_VALUE ||
a469f8a5 520 datalen < 0 || datalen > IPP_MAX_LENGTH)
ef416fc2 521 return (NULL);
522
a2326b5b 523 if ((attr = ipp_add_attr(ipp, name, group, IPP_TAG_STRING, 1)) == NULL)
ef416fc2 524 return (NULL);
525
526 /*
527 * Initialize the attribute data...
528 */
529
ef416fc2 530 attr->values[0].unknown.length = datalen;
531
532 if (data)
533 {
7e86f2f6 534 if ((attr->values[0].unknown.data = malloc((size_t)datalen)) == NULL)
91c84a35
MS
535 {
536 ippDeleteAttribute(ipp, attr);
537 return (NULL);
538 }
539
07623986 540 memcpy(attr->values[0].unknown.data, data, (size_t)datalen);
ef416fc2 541 }
542
543 /*
544 * Return the new attribute...
545 */
546
547 return (attr);
548}
549
550
551/*
a2326b5b
MS
552 * 'ippAddOutOfBand()' - Add an out-of-band value to an IPP message.
553 *
a469f8a5
MS
554 * The @code ipp@ parameter refers to an IPP message previously created using
555 * the @link ippNew@, @link ippNewRequest@, or @link ippNewResponse@ functions.
a2326b5b
MS
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 *
8072030b 569 * @since CUPS 1.6/macOS 10.8@
ef416fc2 570 */
571
a2326b5b
MS
572ipp_attribute_t * /* O - New attribute */
573ippAddOutOfBand(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 */
ef416fc2 577{
807315e6 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));
ef416fc2 579
cb7f98ee 580 value_tag &= IPP_TAG_CUPS_MASK;
ef416fc2 581
4400e98d 582 /*
a2326b5b 583 * Range check input...
4400e98d 584 */
585
a2326b5b
MS
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))
ef416fc2 595 return (NULL);
596
597 /*
a2326b5b 598 * Create the attribute...
ef416fc2 599 */
600
a2326b5b 601 return (ipp_add_attr(ipp, name, group, value_tag, 1));
ef416fc2 602}
603
604
605/*
606 * 'ippAddRange()' - Add a range of values to an IPP message.
a2326b5b 607 *
a469f8a5
MS
608 * The @code ipp@ parameter refers to an IPP message previously created using
609 * the @link ippNew@, @link ippNewRequest@, or @link ippNewResponse@ functions.
a2326b5b
MS
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.
ef416fc2 618 */
619
620ipp_attribute_t * /* O - New attribute */
621ippAddRange(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
807315e6 630 DEBUG_printf(("ippAddRange(ipp=%p, group=%02x(%s), name=\"%s\", lower=%d, upper=%d)", (void *)ipp, group, ippTagString(group), name, lower, upper));
1ff0402e 631
a2326b5b
MS
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)
ef416fc2 638 return (NULL);
639
a2326b5b
MS
640 /*
641 * Create the attribute...
642 */
643
644 if ((attr = ipp_add_attr(ipp, name, group, IPP_TAG_RANGE, 1)) == NULL)
ef416fc2 645 return (NULL);
646
ef416fc2 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.
a2326b5b 656 *
a469f8a5
MS
657 * The @code ipp@ parameter refers to an IPP message previously created using
658 * the @link ippNew@, @link ippNewRequest@, or @link ippNewResponse@ functions.
a2326b5b
MS
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@).
ef416fc2 665 */
666
667ipp_attribute_t * /* O - New attribute */
668ippAddRanges(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 */
a2326b5b 677 _ipp_value_t *value; /* Current value */
ef416fc2 678
679
807315e6 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));
1ff0402e 681
a2326b5b
MS
682 /*
683 * Range check input...
684 */
ef416fc2 685
a2326b5b
MS
686 if (!ipp || !name || group < IPP_TAG_ZERO ||
687 group == IPP_TAG_END || group >= IPP_TAG_UNSUPPORTED_VALUE ||
688 num_values < 1)
ef416fc2 689 return (NULL);
690
a2326b5b
MS
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);
ef416fc2 697
a2326b5b
MS
698 if (lower && upper)
699 {
700 for (i = num_values, value = attr->values;
701 i > 0;
702 i --, value ++)
ef416fc2 703 {
a2326b5b
MS
704 value->range.lower = *lower++;
705 value->range.upper = *upper++;
ef416fc2 706 }
a2326b5b 707 }
ef416fc2 708
709 return (attr);
710}
711
712
713/*
714 * 'ippAddResolution()' - Add a resolution value to an IPP message.
a2326b5b 715 *
a469f8a5
MS
716 * The @code ipp@ parameter refers to an IPP message previously created using
717 * the @link ippNew@, @link ippNewRequest@, or @link ippNewResponse@ functions.
a2326b5b
MS
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@).
ef416fc2 724 */
725
726ipp_attribute_t * /* O - New attribute */
727ippAddResolution(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
807315e6 737 DEBUG_printf(("ippAddResolution(ipp=%p, group=%02x(%s), name=\"%s\", units=%d, xres=%d, yres=%d)", (void *)ipp, group,
1ff0402e
MS
738 ippTagString(group), name, units, xres, yres));
739
a2326b5b
MS
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)
ef416fc2 748 return (NULL);
749
a2326b5b
MS
750 /*
751 * Create the attribute...
752 */
753
754 if ((attr = ipp_add_attr(ipp, name, group, IPP_TAG_RESOLUTION, 1)) == NULL)
ef416fc2 755 return (NULL);
756
ef416fc2 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.
a2326b5b 767 *
a469f8a5
MS
768 * The @code ipp@ parameter refers to an IPP message previously created using
769 * the @link ippNew@, @link ippNewRequest@, or @link ippNewResponse@ functions.
a2326b5b
MS
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@).
ef416fc2 776 */
777
778ipp_attribute_t * /* O - New attribute */
779ippAddResolutions(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 */
a2326b5b 789 _ipp_value_t *value; /* Current value */
ef416fc2 790
791
807315e6 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));
1ff0402e 793
a2326b5b
MS
794 /*
795 * Range check input...
796 */
ef416fc2 797
a2326b5b
MS
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)
ef416fc2 802 return (NULL);
803
a2326b5b
MS
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);
ef416fc2 810
a2326b5b
MS
811 if (xres && yres)
812 {
813 for (i = num_values, value = attr->values;
814 i > 0;
815 i --, value ++)
ef416fc2 816 {
a2326b5b
MS
817 value->resolution.xres = *xres++;
818 value->resolution.yres = *yres++;
ef416fc2 819 value->resolution.units = units;
820 }
a2326b5b 821 }
ef416fc2 822
823 return (attr);
824}
825
826
827/*
828 * 'ippAddSeparator()' - Add a group separator to an IPP message.
a2326b5b 829 *
a469f8a5
MS
830 * The @code ipp@ parameter refers to an IPP message previously created using
831 * the @link ippNew@, @link ippNewRequest@, or @link ippNewResponse@ functions.
ef416fc2 832 */
833
834ipp_attribute_t * /* O - New attribute */
835ippAddSeparator(ipp_t *ipp) /* I - IPP message */
836{
807315e6 837 DEBUG_printf(("ippAddSeparator(ipp=%p)", (void *)ipp));
ef416fc2 838
a2326b5b
MS
839 /*
840 * Range check input...
841 */
ef416fc2 842
a2326b5b 843 if (!ipp)
ef416fc2 844 return (NULL);
845
a2326b5b
MS
846 /*
847 * Create the attribute...
848 */
ef416fc2 849
a2326b5b 850 return (ipp_add_attr(ipp, NULL, IPP_TAG_ZERO, IPP_TAG_ZERO, 0));
ef416fc2 851}
852
853
854/*
a2326b5b
MS
855 * 'ippAddString()' - Add a language-encoded string to an IPP message.
856 *
a469f8a5
MS
857 * The @code ipp@ parameter refers to an IPP message previously created using
858 * the @link ippNew@, @link ippNewRequest@, or @link ippNewResponse@ functions.
a2326b5b
MS
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.
ef416fc2 875 */
876
a2326b5b
MS
877ipp_attribute_t * /* O - New attribute */
878ippAddString(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 */
ef416fc2 884{
a2326b5b
MS
885 ipp_tag_t temp_tag; /* Temporary value tag (masked) */
886 ipp_attribute_t *attr; /* New attribute */
5a9febac
MS
887 char code[IPP_MAX_LANGUAGE];
888 /* Charset/language code buffer */
ef416fc2 889
1ff0402e 890
807315e6 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));
ef416fc2 892
893 /*
a2326b5b 894 * Range check input...
ef416fc2 895 */
896
cb7f98ee 897 temp_tag = (ipp_tag_t)((int)value_tag & IPP_TAG_CUPS_MASK);
ef416fc2 898
a2326b5b
MS
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);
ef416fc2 905
a2326b5b
MS
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 */
ef416fc2 914
a2326b5b
MS
915 /*
916 * See if we need to map charset, language, or locale values...
917 */
ef416fc2 918
cb7f98ee 919 if (language && ((int)value_tag & IPP_TAG_CUPS_CONST) &&
a2326b5b
MS
920 strcmp(language, ipp_lang_code(language, code, sizeof(code))))
921 value_tag = temp_tag; /* Don't do a fast copy */
cb7f98ee 922 else if (value && value_tag == (ipp_tag_t)(IPP_TAG_CHARSET | IPP_TAG_CUPS_CONST) &&
a2326b5b
MS
923 strcmp(value, ipp_get_code(value, code, sizeof(code))))
924 value_tag = temp_tag; /* Don't do a fast copy */
cb7f98ee 925 else if (value && value_tag == (ipp_tag_t)(IPP_TAG_LANGUAGE | IPP_TAG_CUPS_CONST) &&
a2326b5b
MS
926 strcmp(value, ipp_lang_code(value, code, sizeof(code))))
927 value_tag = temp_tag; /* Don't do a fast copy */
ef416fc2 928
a2326b5b
MS
929 /*
930 * Create the attribute...
931 */
ef416fc2 932
a2326b5b
MS
933 if ((attr = ipp_add_attr(ipp, name, group, value_tag, 1)) == NULL)
934 return (NULL);
ef416fc2 935
a2326b5b
MS
936 /*
937 * Initialize the attribute data...
938 */
aaf19ab0 939
cb7f98ee 940 if ((int)value_tag & IPP_TAG_CUPS_CONST)
ef416fc2 941 {
a2326b5b
MS
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
82cc1f9a
MS
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 }
ef416fc2 962 }
963
a2326b5b 964 return (attr);
ef416fc2 965}
966
967
a469f8a5
MS
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 *
8072030b 997 * @since CUPS 1.7/macOS 10.9@
a469f8a5
MS
998 */
999
1000ipp_attribute_t * /* O - New attribute */
1001ippAddStringf(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 *
8072030b 1050 * @since CUPS 1.7/macOS 10.9@
a469f8a5
MS
1051 */
1052
1053ipp_attribute_t * /* O - New attribute */
1054ippAddStringfv(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 ||
56cd8959 1076 !format)
a469f8a5
MS
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
7e86f2f6 1098 bytes = (ssize_t)strlen(s);
a469f8a5
MS
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
ef416fc2 1183/*
a2326b5b 1184 * 'ippAddStrings()' - Add language-encoded strings to an IPP message.
ef416fc2 1185 *
a469f8a5
MS
1186 * The @code ipp@ parameter refers to an IPP message previously created using
1187 * the @link ippNew@, @link ippNewRequest@, or @link ippNewResponse@ functions.
a2326b5b
MS
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.
ef416fc2 1204 */
1205
a2326b5b
MS
1206ipp_attribute_t * /* O - New attribute */
1207ippAddStrings(
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 */
ef416fc2 1215{
a2326b5b
MS
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 */
ef416fc2 1221
1222
807315e6 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));
1ff0402e 1224
ef416fc2 1225 /*
a2326b5b 1226 * Range check input...
ef416fc2 1227 */
1228
cb7f98ee 1229 temp_tag = (ipp_tag_t)((int)value_tag & IPP_TAG_CUPS_MASK);
ef416fc2 1230
a2326b5b
MS
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);
ef416fc2 1238
a2326b5b
MS
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 */
ef416fc2 1248
a2326b5b
MS
1249 /*
1250 * See if we need to map charset, language, or locale values...
1251 */
ef416fc2 1252
cb7f98ee 1253 if (language && ((int)value_tag & IPP_TAG_CUPS_CONST) &&
a2326b5b
MS
1254 strcmp(language, ipp_lang_code(language, code, sizeof(code))))
1255 value_tag = temp_tag; /* Don't do a fast copy */
cb7f98ee 1256 else if (values && value_tag == (ipp_tag_t)(IPP_TAG_CHARSET | IPP_TAG_CUPS_CONST))
a2326b5b
MS
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 }
cb7f98ee 1265 else if (values && value_tag == (ipp_tag_t)(IPP_TAG_LANGUAGE | IPP_TAG_CUPS_CONST))
a2326b5b
MS
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 }
ef416fc2 1273 }
ef416fc2 1274
a2326b5b
MS
1275 /*
1276 * Create the attribute...
1277 */
ef416fc2 1278
a2326b5b 1279 if ((attr = ipp_add_attr(ipp, name, group, value_tag, num_values)) == NULL)
ef416fc2 1280 return (NULL);
1281
1282 /*
a2326b5b 1283 * Initialize the attribute data...
ef416fc2 1284 */
1285
a2326b5b
MS
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 {
cb7f98ee 1294 if ((int)value_tag & IPP_TAG_CUPS_CONST)
a2326b5b
MS
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 }
ef416fc2 1303
a2326b5b
MS
1304 if (values)
1305 {
cb7f98ee 1306 if ((int)value_tag & IPP_TAG_CUPS_CONST)
a2326b5b
MS
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 }
ef416fc2 1316
a2326b5b 1317 return (attr);
ef416fc2 1318}
1319
1320
a469f8a5
MS
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 *
8072030b 1329 * @since CUPS 1.7/macOS 10.9@
a469f8a5
MS
1330 */
1331
1332int /* O - 1 on a match, 0 on no match */
1333ippContainsInteger(
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,
65bebeac 1378 * naturalLanguage, mimeMediaType, name, text, uri, or uriScheme value.
a469f8a5 1379 *
8072030b 1380 * @since CUPS 1.7/macOS 10.9@
a469f8a5
MS
1381 */
1382
1383int /* O - 1 on a match, 0 on no match */
1384ippContainsString(
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
807315e6 1392 DEBUG_printf(("ippContainsString(attr=%p, value=\"%s\")", (void *)attr, value));
a469f8a5
MS
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 :
a268a6c9
MS
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
a469f8a5
MS
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 :
a469f8a5
MS
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
a268a6c9 1445 if (!_cups_strcasecmp(value, avalue->string.text))
a469f8a5
MS
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
ef416fc2 1462/*
a2326b5b
MS
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 *
8072030b 1470 * @since CUPS 1.6/macOS 10.8@
ef416fc2 1471 */
1472
a2326b5b
MS
1473
1474ipp_attribute_t * /* O - New attribute */
1475ippCopyAttribute(
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 */
ef416fc2 1479{
a2326b5b
MS
1480 int i; /* Looping var */
1481 ipp_attribute_t *dstattr; /* Destination attribute */
1482 _ipp_value_t *srcval, /* Source value */
1483 *dstval; /* Destination value */
ef416fc2 1484
1485
807315e6 1486 DEBUG_printf(("ippCopyAttribute(dst=%p, srcattr=%p, quickcopy=%d)", (void *)dst, (void *)srcattr, quickcopy));
ef416fc2 1487
a2326b5b
MS
1488 /*
1489 * Range check input...
1490 */
1491
1492 if (!dst || !srcattr)
ef416fc2 1493 return (NULL);
1494
a2326b5b
MS
1495 /*
1496 * Copy it...
1497 */
ef416fc2 1498
cb7f98ee 1499 quickcopy = quickcopy ? IPP_TAG_CUPS_CONST : 0;
a2326b5b 1500
cb7f98ee 1501 switch (srcattr->value_tag & ~IPP_TAG_CUPS_CONST)
ef416fc2 1502 {
a2326b5b
MS
1503 case IPP_TAG_ZERO :
1504 dstattr = ippAddSeparator(dst);
1505 break;
ef416fc2 1506
10f9350b
MS
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
a2326b5b
MS
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;
ef416fc2 1529
a2326b5b
MS
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;
ef416fc2 1541
a2326b5b
MS
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;
ef416fc2 1555
a2326b5b
MS
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 }
cb7f98ee 1564 else if (srcattr->value_tag & IPP_TAG_CUPS_CONST)
a2326b5b
MS
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;
ef416fc2 1581
a2326b5b
MS
1582 case IPP_TAG_DATE :
1583 if (srcattr->num_values != 1)
1584 return (NULL);
ef416fc2 1585
a2326b5b
MS
1586 dstattr = ippAddDate(dst, srcattr->group_tag, srcattr->name,
1587 srcattr->values[0].date);
1588 break;
ef416fc2 1589
a2326b5b
MS
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;
ef416fc2 1606
a2326b5b
MS
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;
ef416fc2 1621
a2326b5b
MS
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;
ef416fc2 1629
a2326b5b
MS
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 }
cb7f98ee 1641 else if (srcattr->value_tag & IPP_TAG_CUPS_CONST)
a2326b5b
MS
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;
ef416fc2 1652
a2326b5b
MS
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;
ef416fc2 1667
a2326b5b
MS
1668 dstval->string.text = _cupsStrRetain(srcval->string.text);
1669 }
1670 }
1671 break;
ef416fc2 1672
a2326b5b
MS
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;
ef416fc2 1687
a2326b5b
MS
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;
ef416fc2 1701
a2326b5b
MS
1702 if (dstval->unknown.length > 0)
1703 {
7e86f2f6 1704 if ((dstval->unknown.data = malloc((size_t)dstval->unknown.length)) == NULL)
a2326b5b
MS
1705 dstval->unknown.length = 0;
1706 else
07623986 1707 memcpy(dstval->unknown.data, srcval->unknown.data, (size_t)dstval->unknown.length);
a2326b5b
MS
1708 }
1709 }
1710 break; /* anti-compiler-warning-code */
ef416fc2 1711 }
1712
a2326b5b 1713 return (dstattr);
ef416fc2 1714}
1715
1716
1717/*
a2326b5b 1718 * 'ippCopyAttributes()' - Copy attributes from one IPP message to another.
ef416fc2 1719 *
58fce51f 1720 * Zero or more attributes are copied from the source IPP message, @code src@, to the
a2326b5b
MS
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.
ef416fc2 1724 *
a2326b5b
MS
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 *
8072030b 1730 * @since CUPS 1.6/macOS 10.8@
ef416fc2 1731 */
1732
a2326b5b
MS
1733int /* O - 1 on success, 0 on error */
1734ippCopyAttributes(
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 */
ef416fc2 1740{
a2326b5b 1741 ipp_attribute_t *srcattr; /* Source attribute */
ef416fc2 1742
1743
807315e6 1744 DEBUG_printf(("ippCopyAttributes(dst=%p, src=%p, quickcopy=%d, cb=%p, context=%p)", (void *)dst, (void *)src, quickcopy, (void *)cb, context));
1ff0402e 1745
ef416fc2 1746 /*
a2326b5b 1747 * Range check input...
ef416fc2 1748 */
1749
a2326b5b
MS
1750 if (!dst || !src)
1751 return (0);
ef416fc2 1752
1753 /*
a2326b5b 1754 * Loop through source attributes and copy as needed...
ef416fc2 1755 */
1756
a2326b5b
MS
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);
ef416fc2 1761
a2326b5b
MS
1762 return (1);
1763}
ef416fc2 1764
ef416fc2 1765
a2326b5b 1766/*
65bebeac
MS
1767 * 'ippDateToTime()' - Convert from RFC 2579 Date/Time format to time in
1768 * seconds.
a2326b5b 1769 */
ef416fc2 1770
a2326b5b 1771time_t /* O - UNIX time value */
65bebeac 1772ippDateToTime(const ipp_uchar_t *date) /* I - RFC 2579 date info */
a2326b5b
MS
1773{
1774 struct tm unixdate; /* UNIX date/time info */
1775 time_t t; /* Computed time */
ef416fc2 1776
a2326b5b
MS
1777
1778 if (!date)
1779 return (0);
1780
1781 memset(&unixdate, 0, sizeof(unixdate));
ef416fc2 1782
1783 /*
65bebeac 1784 * RFC-2579 date/time format is:
a2326b5b
MS
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)
ef416fc2 1798 */
1799
a2326b5b
MS
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);
ef416fc2 1815}
1816
1817
1818/*
a2326b5b 1819 * 'ippDelete()' - Delete an IPP message.
ef416fc2 1820 */
1821
a2326b5b
MS
1822void
1823ippDelete(ipp_t *ipp) /* I - IPP message */
ef416fc2 1824{
a2326b5b
MS
1825 ipp_attribute_t *attr, /* Current attribute */
1826 *next; /* Next attribute */
ef416fc2 1827
ef416fc2 1828
807315e6 1829 DEBUG_printf(("ippDelete(ipp=%p)", (void *)ipp));
ef416fc2 1830
a2326b5b
MS
1831 if (!ipp)
1832 return;
1833
1834 ipp->use --;
1835 if (ipp->use > 0)
b908d72c
MS
1836 {
1837 DEBUG_printf(("4debug_retain: %p IPP message (use=%d)", (void *)ipp, ipp->use));
a2326b5b 1838 return;
b908d72c
MS
1839 }
1840
1841 DEBUG_printf(("4debug_free: %p IPP message", (void *)ipp));
ef416fc2 1842
a2326b5b
MS
1843 for (attr = ipp->attrs; attr != NULL; attr = next)
1844 {
1845 next = attr->next;
ef416fc2 1846
b908d72c
MS
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
a2326b5b 1849 ipp_free_values(attr, 0, attr->num_values);
ef416fc2 1850
a2326b5b
MS
1851 if (attr->name)
1852 _cupsStrFree(attr->name);
ef416fc2 1853
a2326b5b
MS
1854 free(attr);
1855 }
1856
1857 free(ipp);
ef416fc2 1858}
1859
1860
1861/*
a2326b5b 1862 * 'ippDeleteAttribute()' - Delete a single attribute in an IPP message.
ef416fc2 1863 *
8072030b 1864 * @since CUPS 1.1.19/macOS 10.3@
ef416fc2 1865 */
1866
a2326b5b
MS
1867void
1868ippDeleteAttribute(
1869 ipp_t *ipp, /* I - IPP message */
1870 ipp_attribute_t *attr) /* I - Attribute to delete */
ef416fc2 1871{
a2326b5b
MS
1872 ipp_attribute_t *current, /* Current attribute */
1873 *prev; /* Previous attribute */
ef416fc2 1874
1875
807315e6 1876 DEBUG_printf(("ippDeleteAttribute(ipp=%p, attr=%p(%s))", (void *)ipp, (void *)attr, attr ? attr->name : "(null)"));
ef416fc2 1877
a2326b5b
MS
1878 /*
1879 * Range check input...
1880 */
ef416fc2 1881
a2326b5b
MS
1882 if (!attr)
1883 return;
1f6f3dbc 1884
b908d72c
MS
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
a2326b5b
MS
1887 /*
1888 * Find the attribute in the list...
1889 */
1890
1891 if (ipp)
ef416fc2 1892 {
a2326b5b
MS
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 */
ef416fc2 1901
a2326b5b
MS
1902 if (prev)
1903 prev->next = current->next;
1904 else
1905 ipp->attrs = current->next;
ef416fc2 1906
a2326b5b
MS
1907 if (current == ipp->last)
1908 ipp->last = prev;
ef416fc2 1909
a2326b5b
MS
1910 break;
1911 }
ef416fc2 1912
a2326b5b
MS
1913 if (!current)
1914 return;
1915 }
ef416fc2 1916
a2326b5b
MS
1917 /*
1918 * Free memory used by the attribute...
1919 */
ef416fc2 1920
a2326b5b 1921 ipp_free_values(attr, 0, attr->num_values);
ef416fc2 1922
a2326b5b
MS
1923 if (attr->name)
1924 _cupsStrFree(attr->name);
ef416fc2 1925
a2326b5b
MS
1926 free(attr);
1927}
ef416fc2 1928
b86bc4cf 1929
a2326b5b
MS
1930/*
1931 * 'ippDeleteValues()' - Delete values in an attribute.
1932 *
9c80ffa2
MS
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.
a2326b5b
MS
1937 *
1938 * Deleting all values in an attribute deletes the attribute.
1939 *
8072030b 1940 * @since CUPS 1.6/macOS 10.8@
a2326b5b 1941 */
ef416fc2 1942
9c80ffa2 1943int /* O - 1 on success, 0 on failure */
a2326b5b 1944ippDeleteValues(
9c80ffa2
MS
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 */
a2326b5b
MS
1949{
1950 /*
1951 * Range check input...
1952 */
ef416fc2 1953
9c80ffa2
MS
1954 if (!ipp || !attr || !*attr ||
1955 element < 0 || element >= (*attr)->num_values || count <= 0 ||
1956 (element + count) >= (*attr)->num_values)
a2326b5b 1957 return (0);
ef416fc2 1958
a2326b5b
MS
1959 /*
1960 * If we are deleting all values, just delete the attribute entirely.
1961 */
ef416fc2 1962
9c80ffa2 1963 if (count == (*attr)->num_values)
a2326b5b 1964 {
9c80ffa2
MS
1965 ippDeleteAttribute(ipp, *attr);
1966 *attr = NULL;
a2326b5b
MS
1967 return (1);
1968 }
ef416fc2 1969
a2326b5b
MS
1970 /*
1971 * Otherwise free the values in question and return.
1972 */
ef416fc2 1973
9c80ffa2 1974 ipp_free_values(*attr, element, count);
a2326b5b
MS
1975
1976 return (1);
1977}
1978
1979
1980/*
9c80ffa2 1981 * 'ippFindAttribute()' - Find a named attribute in a request.
f2a31e21
MS
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".
a2326b5b
MS
1986 */
1987
1988ipp_attribute_t * /* O - Matching attribute */
1989ippFindAttribute(ipp_t *ipp, /* I - IPP message */
1990 const char *name, /* I - Name of attribute */
1991 ipp_tag_t type) /* I - Type of attribute */
1992{
807315e6 1993 DEBUG_printf(("2ippFindAttribute(ipp=%p, name=\"%s\", type=%02x(%s))", (void *)ipp, name, type, ippTagString(type)));
a2326b5b
MS
1994
1995 if (!ipp || !name)
1996 return (NULL);
1997
1998 /*
1999 * Reset the current pointer...
2000 */
2001
2002 ipp->current = NULL;
f2a31e21 2003 ipp->atend = 0;
a2326b5b
MS
2004
2005 /*
2006 * Search for the attribute...
2007 */
2008
2009 return (ippFindNextAttribute(ipp, name, type));
2010}
2011
2012
2013/*
9c80ffa2 2014 * 'ippFindNextAttribute()' - Find the next named attribute in a request.
f2a31e21
MS
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".
a2326b5b
MS
2019 */
2020
2021ipp_attribute_t * /* O - Matching attribute */
2022ippFindNextAttribute(ipp_t *ipp, /* I - IPP message */
2023 const char *name, /* I - Name of attribute */
2024 ipp_tag_t type) /* I - Type of attribute */
2025{
f2a31e21
MS
2026 ipp_attribute_t *attr, /* Current atttribute */
2027 *childattr; /* Child attribute */
a2326b5b 2028 ipp_tag_t value_tag; /* Value tag */
f2a31e21 2029 char parent[1024], /* Parent attribute name */
770f94bc 2030 *child = NULL; /* Child attribute name */
a2326b5b
MS
2031
2032
807315e6 2033 DEBUG_printf(("2ippFindNextAttribute(ipp=%p, name=\"%s\", type=%02x(%s))", (void *)ipp, name, type, ippTagString(type)));
a2326b5b
MS
2034
2035 if (!ipp || !name)
2036 return (NULL);
2037
f2a31e21
MS
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)
a2326b5b
MS
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 {
807315e6 2104 DEBUG_printf(("4ippFindAttribute: attr=%p, name=\"%s\"", (void *)attr, attr->name));
a2326b5b 2105
cb7f98ee 2106 value_tag = (ipp_tag_t)(attr->value_tag & IPP_TAG_CUPS_MASK);
a2326b5b
MS
2107
2108 if (attr->name != NULL && _cups_strcasecmp(attr->name, name) == 0 &&
f2a31e21 2109 (value_tag == type || type == IPP_TAG_ZERO || name == parent ||
a2326b5b
MS
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
f2a31e21
MS
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);
a2326b5b
MS
2130 }
2131 }
2132
2133 ipp->current = NULL;
2134 ipp->prev = NULL;
f2a31e21 2135 ipp->atend = 1;
a2326b5b
MS
2136
2137 return (NULL);
2138}
2139
2140
2141/*
2142 * 'ippFirstAttribute()' - Return the first attribute in the message.
2143 *
8072030b 2144 * @since CUPS 1.6/macOS 10.8@
a2326b5b
MS
2145 */
2146
2147ipp_attribute_t * /* O - First attribute or @code NULL@ if none */
2148ippFirstAttribute(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
65bebeac 2169 * @code ippGetCount(attr)@ - 1.
a2326b5b 2170 *
8072030b 2171 * @since CUPS 1.6/macOS 10.8@
a2326b5b
MS
2172 */
2173
c52d341f 2174int /* O - Boolean value or 0 on error */
a2326b5b
MS
2175ippGetBoolean(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)
c52d341f 2184 return (0);
a2326b5b
MS
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
65bebeac 2198 * @code ippGetCount(attr)@ - 1.
a2326b5b 2199 *
8072030b 2200 * @since CUPS 1.6/macOS 10.8@
a2326b5b
MS
2201 */
2202
2203ipp_t * /* O - Collection value or @code NULL@ on error */
2204ippGetCollection(
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 *
8072030b 2227 * @since CUPS 1.6/macOS 10.8@
a2326b5b
MS
2228 */
2229
c52d341f 2230int /* O - Number of values or 0 on error */
a2326b5b
MS
2231ippGetCount(ipp_attribute_t *attr) /* I - IPP attribute */
2232{
2233 /*
2234 * Range check input...
2235 */
2236
2237 if (!attr)
c52d341f 2238 return (0);
a2326b5b
MS
2239
2240 /*
2241 * Return the number of values...
2242 */
2243
2244 return (attr->num_values);
2245}
2246
2247
9c80ffa2 2248/*
65bebeac 2249 * 'ippGetDate()' - Get a dateTime value for an attribute.
9c80ffa2
MS
2250 *
2251 * The @code element@ parameter specifies which value to get from 0 to
65bebeac 2252 * @code ippGetCount(attr)@ - 1.
9c80ffa2 2253 *
8072030b 2254 * @since CUPS 1.6/macOS 10.8@
9c80ffa2
MS
2255 */
2256
65bebeac 2257const ipp_uchar_t * /* O - dateTime value or @code NULL@ */
9c80ffa2
MS
2258ippGetDate(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
a2326b5b
MS
2277/*
2278 * 'ippGetGroupTag()' - Get the group associated with an attribute.
2279 *
8072030b 2280 * @since CUPS 1.6/macOS 10.8@
a2326b5b
MS
2281 */
2282
2283ipp_tag_t /* O - Group tag or @code IPP_TAG_ZERO@ on error */
2284ippGetGroupTag(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
65bebeac 2305 * @code ippGetCount(attr)@ - 1.
a2326b5b 2306 *
8072030b 2307 * @since CUPS 1.6/macOS 10.8@
a2326b5b
MS
2308 */
2309
c52d341f 2310int /* O - Value or 0 on error */
a2326b5b
MS
2311ippGetInteger(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)
c52d341f 2320 return (0);
a2326b5b
MS
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 *
8072030b 2333 * @since CUPS 1.6/macOS 10.8@
a2326b5b
MS
2334 */
2335
2336const char * /* O - Attribute name or @code NULL@ for separators */
2337ippGetName(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
6961465f
MS
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
65bebeac 2358 * @code ippGetCount(attr)@ - 1.
6961465f 2359 *
8072030b 2360 * @since CUPS 1.7/macOS 10.9@
6961465f
MS
2361 */
2362
2363void * /* O - Pointer to octetString data */
2364ippGetOctetString(
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
a2326b5b
MS
2393/*
2394 * 'ippGetOperation()' - Get the operation ID in an IPP message.
2395 *
8072030b 2396 * @since CUPS 1.6/macOS 10.8@
a2326b5b
MS
2397 */
2398
c52d341f 2399ipp_op_t /* O - Operation ID or 0 on error */
a2326b5b
MS
2400ippGetOperation(ipp_t *ipp) /* I - IPP request message */
2401{
2402 /*
2403 * Range check input...
2404 */
2405
2406 if (!ipp)
c52d341f 2407 return ((ipp_op_t)0);
a2326b5b
MS
2408
2409 /*
2410 * Return the value...
2411 */
2412
2413 return (ipp->request.op.operation_id);
2414}
2415
2416
9c80ffa2
MS
2417/*
2418 * 'ippGetRange()' - Get a rangeOfInteger value from an attribute.
2419 *
2420 * The @code element@ parameter specifies which value to get from 0 to
65bebeac 2421 * @code ippGetCount(attr)@ - 1.
9c80ffa2 2422 *
8072030b 2423 * @since CUPS 1.6/macOS 10.8@
9c80ffa2
MS
2424 */
2425
c52d341f 2426int /* O - Lower value of range or 0 */
9c80ffa2
MS
2427ippGetRange(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)
c52d341f 2439 *uppervalue = 0;
9c80ffa2 2440
c52d341f 2441 return (0);
9c80ffa2
MS
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
a2326b5b
MS
2455/*
2456 * 'ippGetRequestId()' - Get the request ID from an IPP message.
2457 *
8072030b 2458 * @since CUPS 1.6/macOS 10.8@
a2326b5b
MS
2459 */
2460
c52d341f 2461int /* O - Request ID or 0 on error */
a2326b5b
MS
2462ippGetRequestId(ipp_t *ipp) /* I - IPP message */
2463{
2464 /*
2465 * Range check input...
2466 */
2467
2468 if (!ipp)
c52d341f 2469 return (0);
a2326b5b
MS
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
65bebeac 2483 * @code ippGetCount(attr)@ - 1.
a2326b5b 2484 *
8072030b 2485 * @since CUPS 1.6/macOS 10.8@
a2326b5b
MS
2486 */
2487
c52d341f 2488int /* O - Horizontal/cross feed resolution or 0 */
a2326b5b
MS
2489ippGetResolution(
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)
c52d341f
MS
2501 {
2502 if (yres)
2503 *yres = 0;
2504
2505 if (units)
2506 *units = (ipp_res_t)0;
2507
2508 return (0);
2509 }
a2326b5b
MS
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
9c80ffa2
MS
2525/*
2526 * 'ippGetState()' - Get the IPP message state.
2527 *
8072030b 2528 * @since CUPS 1.6/macOS 10.8@
9c80ffa2
MS
2529 */
2530
2531ipp_state_t /* O - IPP message state value */
2532ippGetState(ipp_t *ipp) /* I - IPP message */
2533{
2534 /*
2535 * Range check input...
2536 */
2537
2538 if (!ipp)
cb7f98ee 2539 return (IPP_STATE_IDLE);
9c80ffa2
MS
2540
2541 /*
2542 * Return the value...
2543 */
2544
2545 return (ipp->state);
2546}
2547
2548
a2326b5b
MS
2549/*
2550 * 'ippGetStatusCode()' - Get the status code from an IPP response or event message.
2551 *
8072030b 2552 * @since CUPS 1.6/macOS 10.8@
a2326b5b
MS
2553 */
2554
2555ipp_status_t /* O - Status code in IPP message */
2556ippGetStatusCode(ipp_t *ipp) /* I - IPP response or event message */
2557{
2558 /*
2559 * Range check input...
2560 */
2561
2562 if (!ipp)
cb7f98ee 2563 return (IPP_STATUS_ERROR_INTERNAL);
a2326b5b
MS
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
65bebeac 2577 * @code ippGetCount(attr)@ - 1.
a2326b5b 2578 *
8072030b 2579 * @since CUPS 1.6/macOS 10.8@
a2326b5b
MS
2580 */
2581
2582const char *
2583ippGetString(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{
45eb1e5e
MS
2587 ipp_tag_t tag; /* Value tag */
2588
2589
a2326b5b
MS
2590 /*
2591 * Range check input...
2592 */
2593
45eb1e5e
MS
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)))
a2326b5b
MS
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 *
8072030b 2613 * @since CUPS 1.6/macOS 10.8@
a2326b5b
MS
2614 */
2615
2616ipp_tag_t /* O - Value tag or @code IPP_TAG_ZERO@ on error */
2617ippGetValueTag(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
cb7f98ee 2630 return (attr->value_tag & IPP_TAG_CUPS_MASK);
a2326b5b
MS
2631}
2632
2633
2634/*
2635 * 'ippGetVersion()' - Get the major and minor version number from an IPP message.
2636 *
8072030b 2637 * @since CUPS 1.6/macOS 10.8@
a2326b5b
MS
2638 */
2639
c52d341f 2640int /* O - Major version number or 0 on error */
a2326b5b 2641ippGetVersion(ipp_t *ipp, /* I - IPP message */
65bebeac 2642 int *minor) /* O - Minor version number or @code NULL@ for don't care */
a2326b5b
MS
2643{
2644 /*
2645 * Range check input...
2646 */
2647
2648 if (!ipp)
2649 {
2650 if (minor)
c52d341f 2651 *minor = 0;
a2326b5b 2652
c52d341f 2653 return (0);
a2326b5b
MS
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
2671size_t /* O - Size of IPP message */
2672ippLength(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 *
8072030b 2681 * @since CUPS 1.6/macOS 10.8@
a2326b5b
MS
2682 */
2683
2684ipp_attribute_t * /* O - Next attribute or @code NULL@ if none */
2685ippNextAttribute(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
2706ipp_t * /* O - New IPP message */
2707ippNew(void)
2708{
0cb67df3
MS
2709 ipp_t *temp; /* New IPP message */
2710 _cups_globals_t *cg = _cupsGlobals();
2711 /* Global data */
a2326b5b
MS
2712
2713
2714 DEBUG_puts("ippNew()");
2715
2716 if ((temp = (ipp_t *)calloc(1, sizeof(ipp_t))) != NULL)
2717 {
2718 /*
0cb67df3 2719 * Set default version - usually 2.0...
a2326b5b
MS
2720 */
2721
b908d72c
MS
2722 DEBUG_printf(("4debug_alloc: %p IPP message", (void *)temp));
2723
567f49cb
MS
2724 if (cg->server_version == 0)
2725 _cupsSetDefaults();
2726
7e86f2f6
MS
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);
a2326b5b
MS
2729 temp->use = 1;
2730 }
2731
807315e6 2732 DEBUG_printf(("1ippNew: Returning %p", (void *)temp));
a2326b5b
MS
2733
2734 return (temp);
2735}
2736
2737
2738/*
2739 * 'ippNewRequest()' - Allocate a new IPP request message.
2740 *
65bebeac
MS
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.
a2326b5b 2744 *
8072030b 2745 * @since CUPS 1.2/macOS 10.5@
a2326b5b
MS
2746 */
2747
2748ipp_t * /* O - IPP request message */
2749ippNewRequest(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
a469f8a5
MS
2802/*
2803 * 'ippNewResponse()' - Allocate a new IPP response message.
2804 *
65bebeac
MS
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,
a469f8a5
MS
2810 * respectively.
2811 *
8072030b 2812 * @since CUPS 1.7/macOS 10.9@
a469f8a5
MS
2813 */
2814
2815ipp_t * /* O - IPP response message */
2816ippNewResponse(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
a2326b5b
MS
2910/*
2911 * 'ippRead()' - Read data for an IPP message from a HTTP connection.
2912 */
2913
2914ipp_state_t /* O - Current state */
2915ippRead(http_t *http, /* I - HTTP connection */
2916 ipp_t *ipp) /* I - IPP data */
2917{
807315e6 2918 DEBUG_printf(("ippRead(http=%p, ipp=%p), data_remaining=" CUPS_LLFMT, (void *)http, (void *)ipp, CUPS_LLCAST (http ? http->data_remaining : -1)));
a2326b5b
MS
2919
2920 if (!http)
cb7f98ee 2921 return (IPP_STATE_ERROR);
a2326b5b 2922
807315e6 2923 DEBUG_printf(("2ippRead: http->state=%d, http->used=%d", http->state, http->used));
a2326b5b
MS
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 *
8072030b 2933 * @since CUPS 1.1.19/macOS 10.3@
a2326b5b
MS
2934 */
2935
2936ipp_state_t /* O - Current state */
2937ippReadFile(int fd, /* I - HTTP data */
2938 ipp_t *ipp) /* I - IPP data */
2939{
807315e6 2940 DEBUG_printf(("ippReadFile(fd=%d, ipp=%p)", fd, (void *)ipp));
a2326b5b
MS
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 *
8072030b 2949 * @since CUPS 1.2/macOS 10.5@
a2326b5b
MS
2950 */
2951
2952ipp_state_t /* O - Current state */
2953ippReadIO(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 */
5a9febac 2961 string[IPP_MAX_TEXT],
a2326b5b
MS
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
807315e6 2970 DEBUG_printf(("ippReadIO(src=%p, cb=%p, blocking=%d, parent=%p, ipp=%p)", (void *)src, (void *)cb, blocking, (void *)parent, (void *)ipp));
cb7f98ee 2971 DEBUG_printf(("2ippReadIO: ipp->state=%d", ipp ? ipp->state : IPP_STATE_ERROR));
a2326b5b
MS
2972
2973 if (!src || !ipp)
cb7f98ee 2974 return (IPP_STATE_ERROR);
a2326b5b 2975
dcb445bc 2976 if ((buffer = (unsigned char *)_cupsBufferGet(IPP_BUF_SIZE)) == NULL)
a2326b5b
MS
2977 {
2978 DEBUG_puts("1ippReadIO: Unable to get read buffer.");
cb7f98ee 2979 return (IPP_STATE_ERROR);
a2326b5b
MS
2980 }
2981
2982 switch (ipp->state)
2983 {
cb7f98ee 2984 case IPP_STATE_IDLE :
a2326b5b
MS
2985 ipp->state ++; /* Avoid common problem... */
2986
cb7f98ee 2987 case IPP_STATE_HEADER :
a2326b5b
MS
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.");
dcb445bc 2997 _cupsBufferRelease((char *)buffer);
cb7f98ee 2998 return (IPP_STATE_ERROR);
a2326b5b
MS
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
cb7f98ee 3018 ipp->state = IPP_STATE_ATTRIBUTE;
a2326b5b
MS
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
cb7f98ee 3030 case IPP_STATE_ATTRIBUTE :
a2326b5b
MS
3031 for (;;)
3032 {
3033 if ((*cb)(src, buffer, 1) < 1)
3034 {
3035 DEBUG_puts("1ippReadIO: Callback returned EOF/error");
dcb445bc 3036 _cupsBufferRelease((char *)buffer);
cb7f98ee 3037 return (IPP_STATE_ERROR);
a2326b5b
MS
3038 }
3039
807315e6 3040 DEBUG_printf(("2ippReadIO: ipp->current=%p, ipp->prev=%p", (void *)ipp->current, (void *)ipp->prev));
a2326b5b
MS
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");
dcb445bc 3056 _cupsBufferRelease((char *)buffer);
cb7f98ee 3057 return (IPP_STATE_ERROR);
a2326b5b
MS
3058 }
3059
3060 tag = (ipp_tag_t)((((((buffer[0] << 8) | buffer[1]) << 8) |
3061 buffer[2]) << 8) | buffer[3]);
3062
cb7f98ee 3063 if (tag & IPP_TAG_CUPS_CONST)
a2326b5b
MS
3064 {
3065 /*
3066 * Fail if the high bit is set in the tag...
3067 */
3068
cb7f98ee 3069 _cupsSetError(IPP_STATUS_ERROR_INTERNAL, _("IPP extension tag larger than 0x7FFFFFFF."), 1);
a29fd7dd 3070 DEBUG_printf(("1ippReadIO: bad tag 0x%x.", tag));
dcb445bc 3071 _cupsBufferRelease((char *)buffer);
cb7f98ee 3072 return (IPP_STATE_ERROR);
a2326b5b
MS
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
cb7f98ee 3084 ipp->state = IPP_STATE_DATA;
a2326b5b
MS
3085 break;
3086 }
fd32084d
MS
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 }
a2326b5b
MS
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;
ef416fc2 3104
3105 ipp->curtag = tag;
3106 ipp->current = NULL;
807315e6 3107 DEBUG_printf(("2ippReadIO: group tag=%x(%s), ipp->prev=%p", tag, ippTagString(tag), (void *)ipp->prev));
ef416fc2 3108 continue;
3109 }
3110
a2326b5b
MS
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.");
dcb445bc 3121 _cupsBufferRelease((char *)buffer);
cb7f98ee 3122 return (IPP_STATE_ERROR);
a2326b5b
MS
3123 }
3124
3125 n = (buffer[0] << 8) | buffer[1];
3126
3127 if (n >= IPP_BUF_SIZE)
3128 {
cb7f98ee 3129 _cupsSetError(IPP_STATUS_ERROR_INTERNAL, _("IPP name larger than 32767 bytes."), 1);
a2326b5b 3130 DEBUG_printf(("1ippReadIO: bad name length %d.", n));
dcb445bc 3131 _cupsBufferRelease((char *)buffer);
cb7f98ee 3132 return (IPP_STATE_ERROR);
a2326b5b
MS
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 {
cb7f98ee 3146 _cupsSetError(IPP_STATUS_ERROR_INTERNAL, _("IPP attribute has no name."), 1);
a2326b5b 3147 DEBUG_puts("1ippReadIO: Attribute without name and no current.");
dcb445bc 3148 _cupsBufferRelease((char *)buffer);
cb7f98ee 3149 return (IPP_STATE_ERROR);
a2326b5b
MS
3150 }
3151
3152 attr = ipp->current;
cb7f98ee 3153 value_tag = (ipp_tag_t)(attr->value_tag & IPP_TAG_CUPS_MASK);
a2326b5b
MS
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 {
cb7f98ee 3182 _cupsSetError(IPP_STATUS_ERROR_INTERNAL,
a2326b5b
MS
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)));
dcb445bc 3188 _cupsBufferRelease((char *)buffer);
cb7f98ee 3189 return (IPP_STATE_ERROR);
a2326b5b
MS
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 {
cb7f98ee 3209 _cupsSetError(IPP_STATUS_ERROR_INTERNAL,
a2326b5b
MS
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)));
dcb445bc 3215 _cupsBufferRelease((char *)buffer);
cb7f98ee 3216 return (IPP_STATE_ERROR);
a2326b5b
MS
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 {
cb7f98ee 3232 _cupsSetError(IPP_STATUS_ERROR_INTERNAL,
a2326b5b
MS
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)));
dcb445bc 3238 _cupsBufferRelease((char *)buffer);
cb7f98ee 3239 return (IPP_STATE_ERROR);
a2326b5b
MS
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 {
dcb445bc 3248 _cupsBufferRelease((char *)buffer);
cb7f98ee 3249 return (IPP_STATE_ERROR);
a2326b5b
MS
3250 }
3251 }
3252 else if (tag == IPP_TAG_MEMBERNAME)
3253 {
3254 /*
3255 * Name must be length 0!
3256 */
3257
3258 if (n)
3259 {
cb7f98ee 3260 _cupsSetError(IPP_STATUS_ERROR_INTERNAL, _("IPP member name is not empty."), 1);
a2326b5b 3261 DEBUG_puts("1ippReadIO: member name not empty.");
dcb445bc 3262 _cupsBufferRelease((char *)buffer);
cb7f98ee 3263 return (IPP_STATE_ERROR);
a2326b5b
MS
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);
0fa6c7fa
MS
3270 if (!attr)
3271 {
cb7f98ee 3272 _cupsSetHTTPError(HTTP_STATUS_ERROR);
0fa6c7fa
MS
3273 DEBUG_puts("1ippReadIO: unable to allocate attribute.");
3274 _cupsBufferRelease((char *)buffer);
cb7f98ee 3275 return (IPP_STATE_ERROR);
0fa6c7fa 3276 }
a2326b5b 3277
807315e6 3278 DEBUG_printf(("2ippReadIO: membername, ipp->current=%p, ipp->prev=%p", (void *)ipp->current, (void *)ipp->prev));
a2326b5b 3279
9c80ffa2 3280 value = attr->values;
a2326b5b
MS
3281 }
3282 else if (tag != IPP_TAG_END_COLLECTION)
3283 {
3284 /*
3285 * New attribute; read the name and add it...
3286 */
3287
7e86f2f6 3288 if ((*cb)(src, buffer, (size_t)n) < n)
a2326b5b
MS
3289 {
3290 DEBUG_puts("1ippReadIO: unable to read name.");
dcb445bc 3291 _cupsBufferRelease((char *)buffer);
cb7f98ee 3292 return (IPP_STATE_ERROR);
a2326b5b
MS
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 {
cb7f98ee 3303 _cupsSetHTTPError(HTTP_STATUS_ERROR);
a2326b5b 3304 DEBUG_puts("1ippReadIO: unable to allocate attribute.");
dcb445bc 3305 _cupsBufferRelease((char *)buffer);
cb7f98ee 3306 return (IPP_STATE_ERROR);
a2326b5b
MS
3307 }
3308
807315e6 3309 DEBUG_printf(("2ippReadIO: name=\"%s\", ipp->current=%p, ipp->prev=%p", buffer, (void *)ipp->current, (void *)ipp->prev));
a2326b5b 3310
9c80ffa2 3311 value = attr->values;
a2326b5b
MS
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.");
dcb445bc 3322 _cupsBufferRelease((char *)buffer);
cb7f98ee 3323 return (IPP_STATE_ERROR);
a2326b5b
MS
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 {
cb7f98ee 3331 _cupsSetError(IPP_STATUS_ERROR_INTERNAL,
a2326b5b
MS
3332 _("IPP value larger than 32767 bytes."), 1);
3333 DEBUG_printf(("1ippReadIO: bad value length %d.", n));
dcb445bc 3334 _cupsBufferRelease((char *)buffer);
cb7f98ee 3335 return (IPP_STATE_ERROR);
a2326b5b
MS
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)
cb7f98ee 3345 _cupsSetError(IPP_STATUS_ERROR_INTERNAL,
a2326b5b
MS
3346 _("IPP integer value not 4 bytes."), 1);
3347 else
cb7f98ee 3348 _cupsSetError(IPP_STATUS_ERROR_INTERNAL,
a2326b5b 3349 _("IPP enum value not 4 bytes."), 1);
a29fd7dd 3350 DEBUG_printf(("1ippReadIO: bad integer value length %d.", n));
dcb445bc 3351 _cupsBufferRelease((char *)buffer);
cb7f98ee 3352 return (IPP_STATE_ERROR);
a2326b5b
MS
3353 }
3354
3355 if ((*cb)(src, buffer, 4) < 4)
3356 {
3357 DEBUG_puts("1ippReadIO: Unable to read integer value.");
dcb445bc 3358 _cupsBufferRelease((char *)buffer);
cb7f98ee 3359 return (IPP_STATE_ERROR);
a2326b5b
MS
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 {
cb7f98ee 3374 _cupsSetError(IPP_STATUS_ERROR_INTERNAL, _("IPP boolean value not 1 byte."),
a2326b5b 3375 1);
a29fd7dd 3376 DEBUG_printf(("1ippReadIO: bad boolean value length %d.", n));
dcb445bc 3377 _cupsBufferRelease((char *)buffer);
cb7f98ee 3378 return (IPP_STATE_ERROR);
a2326b5b
MS
3379 }
3380
3381 if ((*cb)(src, buffer, 1) < 1)
3382 {
3383 DEBUG_puts("1ippReadIO: Unable to read boolean value.");
dcb445bc 3384 _cupsBufferRelease((char *)buffer);
cb7f98ee 3385 return (IPP_STATE_ERROR);
a2326b5b
MS
3386 }
3387
7e86f2f6 3388 value->boolean = (char)buffer[0];
a2326b5b
MS
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 :
a29fd7dd
MS
3417 if (n > 0)
3418 {
7e86f2f6 3419 if ((*cb)(src, buffer, (size_t)n) < n)
a29fd7dd
MS
3420 {
3421 DEBUG_puts("1ippReadIO: unable to read string value.");
3422 _cupsBufferRelease((char *)buffer);
cb7f98ee 3423 return (IPP_STATE_ERROR);
a29fd7dd 3424 }
a2326b5b
MS
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 {
cb7f98ee 3435 _cupsSetError(IPP_STATUS_ERROR_INTERNAL, _("IPP date value not 11 bytes."), 1);
a29fd7dd 3436 DEBUG_printf(("1ippReadIO: bad date value length %d.", n));
dcb445bc 3437 _cupsBufferRelease((char *)buffer);
cb7f98ee 3438 return (IPP_STATE_ERROR);
a2326b5b
MS
3439 }
3440
3441 if ((*cb)(src, value->date, 11) < 11)
3442 {
3443 DEBUG_puts("1ippReadIO: Unable to read date value.");
dcb445bc 3444 _cupsBufferRelease((char *)buffer);
cb7f98ee 3445 return (IPP_STATE_ERROR);
a2326b5b
MS
3446 }
3447 break;
3448
3449 case IPP_TAG_RESOLUTION :
3450 if (n != 9)
3451 {
cb7f98ee 3452 _cupsSetError(IPP_STATUS_ERROR_INTERNAL,
a2326b5b 3453 _("IPP resolution value not 9 bytes."), 1);
a29fd7dd 3454 DEBUG_printf(("1ippReadIO: bad resolution value length %d.", n));
dcb445bc 3455 _cupsBufferRelease((char *)buffer);
cb7f98ee 3456 return (IPP_STATE_ERROR);
a2326b5b
MS
3457 }
3458
3459 if ((*cb)(src, buffer, 9) < 9)
3460 {
3461 DEBUG_puts("1ippReadIO: Unable to read resolution value.");
dcb445bc 3462 _cupsBufferRelease((char *)buffer);
cb7f98ee 3463 return (IPP_STATE_ERROR);
a2326b5b
MS
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 {
cb7f98ee 3479 _cupsSetError(IPP_STATUS_ERROR_INTERNAL,
a2326b5b 3480 _("IPP rangeOfInteger value not 8 bytes."), 1);
a29fd7dd
MS
3481 DEBUG_printf(("1ippReadIO: bad rangeOfInteger value length "
3482 "%d.", n));
dcb445bc 3483 _cupsBufferRelease((char *)buffer);
cb7f98ee 3484 return (IPP_STATE_ERROR);
a2326b5b
MS
3485 }
3486
3487 if ((*cb)(src, buffer, 8) < 8)
3488 {
3489 DEBUG_puts("1ippReadIO: Unable to read range value.");
dcb445bc 3490 _cupsBufferRelease((char *)buffer);
cb7f98ee 3491 return (IPP_STATE_ERROR);
a2326b5b
MS
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)
cb7f98ee 3507 _cupsSetError(IPP_STATUS_ERROR_INTERNAL,
a2326b5b
MS
3508 _("IPP textWithLanguage value less than "
3509 "minimum 4 bytes."), 1);
3510 else
cb7f98ee 3511 _cupsSetError(IPP_STATUS_ERROR_INTERNAL,
a2326b5b
MS
3512 _("IPP nameWithLanguage value less than "
3513 "minimum 4 bytes."), 1);
a29fd7dd
MS
3514 DEBUG_printf(("1ippReadIO: bad stringWithLanguage value "
3515 "length %d.", n));
dcb445bc 3516 _cupsBufferRelease((char *)buffer);
cb7f98ee 3517 return (IPP_STATE_ERROR);
a2326b5b
MS
3518 }
3519
7e86f2f6 3520 if ((*cb)(src, buffer, (size_t)n) < n)
a2326b5b
MS
3521 {
3522 DEBUG_puts("1ippReadIO: Unable to read string w/language "
3523 "value.");
dcb445bc 3524 _cupsBufferRelease((char *)buffer);
cb7f98ee 3525 return (IPP_STATE_ERROR);
a2326b5b
MS
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
7e86f2f6 3542 if ((bufptr + 2 + n) >= (buffer + IPP_BUF_SIZE) || n >= (int)sizeof(string))
a2326b5b 3543 {
cb7f98ee 3544 _cupsSetError(IPP_STATUS_ERROR_INTERNAL,
a2326b5b 3545 _("IPP language length overflows value."), 1);
a29fd7dd
MS
3546 DEBUG_printf(("1ippReadIO: bad language value length %d.",
3547 n));
dcb445bc 3548 _cupsBufferRelease((char *)buffer);
cb7f98ee 3549 return (IPP_STATE_ERROR);
a2326b5b 3550 }
5a9febac
MS
3551 else if (n >= IPP_MAX_LANGUAGE)
3552 {
cb7f98ee 3553 _cupsSetError(IPP_STATUS_ERROR_INTERNAL,
5a9febac
MS
3554 _("IPP language length too large."), 1);
3555 DEBUG_printf(("1ippReadIO: bad language value length %d.",
3556 n));
3557 _cupsBufferRelease((char *)buffer);
cb7f98ee 3558 return (IPP_STATE_ERROR);
5a9febac 3559 }
a2326b5b 3560
07623986 3561 memcpy(string, bufptr + 2, (size_t)n);
a2326b5b
MS
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 {
cb7f98ee 3571 _cupsSetError(IPP_STATUS_ERROR_INTERNAL,
a2326b5b 3572 _("IPP string length overflows value."), 1);
a29fd7dd 3573 DEBUG_printf(("1ippReadIO: bad string value length %d.", n));
dcb445bc 3574 _cupsBufferRelease((char *)buffer);
cb7f98ee 3575 return (IPP_STATE_ERROR);
a2326b5b
MS
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 {
cb7f98ee 3591 _cupsSetError(IPP_STATUS_ERROR_INTERNAL,
a2326b5b
MS
3592 _("IPP begCollection value not 0 bytes."), 1);
3593 DEBUG_puts("1ippReadIO: begCollection tag with value length "
3594 "> 0.");
dcb445bc 3595 _cupsBufferRelease((char *)buffer);
cb7f98ee 3596 return (IPP_STATE_ERROR);
a2326b5b
MS
3597 }
3598
cb7f98ee 3599 if (ippReadIO(src, cb, 1, ipp, value->collection) == IPP_STATE_ERROR)
a2326b5b
MS
3600 {
3601 DEBUG_puts("1ippReadIO: Unable to read collection value.");
dcb445bc 3602 _cupsBufferRelease((char *)buffer);
cb7f98ee 3603 return (IPP_STATE_ERROR);
a2326b5b
MS
3604 }
3605 break;
3606
3607 case IPP_TAG_END_COLLECTION :
dcb445bc 3608 _cupsBufferRelease((char *)buffer);
a2326b5b
MS
3609
3610 if (n > 0)
3611 {
cb7f98ee 3612 _cupsSetError(IPP_STATUS_ERROR_INTERNAL,
a2326b5b
MS
3613 _("IPP endCollection value not 0 bytes."), 1);
3614 DEBUG_puts("1ippReadIO: endCollection tag with value length "
3615 "> 0.");
cb7f98ee 3616 return (IPP_STATE_ERROR);
a2326b5b
MS
3617 }
3618
3619 DEBUG_puts("1ippReadIO: endCollection tag...");
cb7f98ee 3620 return (ipp->state = IPP_STATE_DATA);
a2326b5b
MS
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
5a9febac
MS
3628 if (!attr)
3629 {
cb7f98ee 3630 _cupsSetError(IPP_STATUS_ERROR_INTERNAL,
5a9febac
MS
3631 _("IPP memberName with no attribute."), 1);
3632 DEBUG_puts("1ippReadIO: Member name without attribute.");
3633 _cupsBufferRelease((char *)buffer);
cb7f98ee 3634 return (IPP_STATE_ERROR);
5a9febac
MS
3635 }
3636 else if (n == 0)
a29fd7dd 3637 {
cb7f98ee 3638 _cupsSetError(IPP_STATUS_ERROR_INTERNAL,
a29fd7dd
MS
3639 _("IPP memberName value is empty."), 1);
3640 DEBUG_puts("1ippReadIO: Empty member name value.");
3641 _cupsBufferRelease((char *)buffer);
cb7f98ee 3642 return (IPP_STATE_ERROR);
a29fd7dd 3643 }
7e86f2f6 3644 else if ((*cb)(src, buffer, (size_t)n) < n)
a2326b5b
MS
3645 {
3646 DEBUG_puts("1ippReadIO: Unable to read member name value.");
dcb445bc 3647 _cupsBufferRelease((char *)buffer);
cb7f98ee 3648 return (IPP_STATE_ERROR);
a2326b5b
MS
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 */
a469f8a5 3666 if (tag == IPP_TAG_STRING && n > IPP_MAX_LENGTH)
5a9febac 3667 {
cb7f98ee 3668 _cupsSetError(IPP_STATUS_ERROR_INTERNAL,
5a9febac
MS
3669 _("IPP octetString length too large."), 1);
3670 DEBUG_printf(("1ippReadIO: bad octetString value length %d.",
3671 n));
3672 _cupsBufferRelease((char *)buffer);
cb7f98ee 3673 return (IPP_STATE_ERROR);
5a9febac
MS
3674 }
3675
a2326b5b 3676 value->unknown.length = n;
5a9febac 3677
a2326b5b
MS
3678 if (n > 0)
3679 {
7e86f2f6 3680 if ((value->unknown.data = malloc((size_t)n)) == NULL)
a2326b5b 3681 {
cb7f98ee 3682 _cupsSetHTTPError(HTTP_STATUS_ERROR);
a2326b5b 3683 DEBUG_puts("1ippReadIO: Unable to allocate value");
dcb445bc 3684 _cupsBufferRelease((char *)buffer);
cb7f98ee 3685 return (IPP_STATE_ERROR);
a2326b5b
MS
3686 }
3687
7e86f2f6 3688 if ((*cb)(src, value->unknown.data, (size_t)n) < n)
a2326b5b
MS
3689 {
3690 DEBUG_puts("1ippReadIO: Unable to read unsupported value.");
dcb445bc 3691 _cupsBufferRelease((char *)buffer);
cb7f98ee 3692 return (IPP_STATE_ERROR);
a2326b5b
MS
3693 }
3694 }
3695 else
3696 value->unknown.data = NULL;
3697 break;
3698 }
3699
a2326b5b
MS
3700 /*
3701 * If blocking is disabled, stop here...
ef416fc2 3702 */
3703
a2326b5b
MS
3704 if (!blocking)
3705 break;
3706 }
3707 break;
ef416fc2 3708
cb7f98ee 3709 case IPP_STATE_DATA :
a2326b5b 3710 break;
ef416fc2 3711
a2326b5b
MS
3712 default :
3713 break; /* anti-compiler-warning-code */
3714 }
ef416fc2 3715
a2326b5b 3716 DEBUG_printf(("1ippReadIO: returning ipp->state=%d.", ipp->state));
dcb445bc 3717 _cupsBufferRelease((char *)buffer);
ef416fc2 3718
a2326b5b
MS
3719 return (ipp->state);
3720}
ef416fc2 3721
ef416fc2 3722
a2326b5b
MS
3723/*
3724 * 'ippSetBoolean()' - Set a boolean value in an attribute.
3725 *
a469f8a5
MS
3726 * The @code ipp@ parameter refers to an IPP message previously created using
3727 * the @link ippNew@, @link ippNewRequest@, or @link ippNewResponse@ functions.
a2326b5b
MS
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
65bebeac 3732 * @code ippGetCount(attr)@.
a2326b5b 3733 *
8072030b 3734 * @since CUPS 1.6/macOS 10.8@
a2326b5b 3735 */
ef416fc2 3736
a2326b5b 3737int /* O - 1 on success, 0 on failure */
6961465f 3738ippSetBoolean(ipp_t *ipp, /* I - IPP message */
a2326b5b
MS
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 */
ef416fc2 3744
ef416fc2 3745
a2326b5b
MS
3746 /*
3747 * Range check input...
3748 */
ef416fc2 3749
a2326b5b
MS
3750 if (!ipp || !attr || !*attr || (*attr)->value_tag != IPP_TAG_BOOLEAN ||
3751 element < 0 || element > (*attr)->num_values)
3752 return (0);
83e08001 3753
a2326b5b
MS
3754 /*
3755 * Set the value and return...
3756 */
83e08001 3757
a2326b5b 3758 if ((value = ipp_set_value(ipp, attr, element)) != NULL)
7e86f2f6 3759 value->boolean = (char)boolvalue;
83e08001 3760
a2326b5b
MS
3761 return (value != NULL);
3762}
83e08001 3763
83e08001 3764
a2326b5b
MS
3765/*
3766 * 'ippSetCollection()' - Set a collection value in an attribute.
3767 *
a469f8a5
MS
3768 * The @code ipp@ parameter refers to an IPP message previously created using
3769 * the @link ippNew@, @link ippNewRequest@, or @link ippNewResponse@ functions.
a2326b5b
MS
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
65bebeac 3774 * @code ippGetCount(attr)@.
a2326b5b 3775 *
8072030b 3776 * @since CUPS 1.6/macOS 10.8@
a2326b5b 3777 */
83e08001 3778
a2326b5b
MS
3779int /* O - 1 on success, 0 on failure */
3780ippSetCollection(
6961465f 3781 ipp_t *ipp, /* I - IPP message */
a2326b5b
MS
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
9c80ffa2 3814/*
65bebeac 3815 * 'ippSetDate()' - Set a dateTime value in an attribute.
9c80ffa2 3816 *
a469f8a5
MS
3817 * The @code ipp@ parameter refers to an IPP message previously created using
3818 * the @link ippNew@, @link ippNewRequest@, or @link ippNewResponse@ functions.
9c80ffa2
MS
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
65bebeac 3823 * @code ippGetCount(attr)@.
9c80ffa2 3824 *
8072030b 3825 * @since CUPS 1.6/macOS 10.8@
9c80ffa2
MS
3826 */
3827
3828int /* O - 1 on success, 0 on failure */
6961465f 3829ippSetDate(ipp_t *ipp, /* I - IPP message */
9c80ffa2
MS
3830 ipp_attribute_t **attr, /* IO - IPP attribute */
3831 int element, /* I - Value number (0-based) */
65bebeac 3832 const ipp_uchar_t *datevalue)/* I - dateTime value */
9c80ffa2
MS
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
a2326b5b
MS
3856/*
3857 * 'ippSetGroupTag()' - Set the group tag of an attribute.
3858 *
a469f8a5
MS
3859 * The @code ipp@ parameter refers to an IPP message previously created using
3860 * the @link ippNew@, @link ippNewRequest@, or @link ippNewResponse@ functions.
a2326b5b
MS
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 *
8072030b 3870 * @since CUPS 1.6/macOS 10.8@
a2326b5b
MS
3871 */
3872
3873int /* O - 1 on success, 0 on failure */
3874ippSetGroupTag(
6961465f 3875 ipp_t *ipp, /* I - IPP message */
a2326b5b
MS
3876 ipp_attribute_t **attr, /* IO - Attribute */
3877 ipp_tag_t group_tag) /* I - Group tag */
3878{
3879 /*
65bebeac 3880 * Range check input - group tag must be 0x01 to 0x0F, per RFC 8011...
a2326b5b
MS
3881 */
3882
a469f8a5
MS
3883 if (!ipp || !attr || !*attr ||
3884 group_tag < IPP_TAG_ZERO || group_tag == IPP_TAG_END ||
a2326b5b
MS
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 *
a469f8a5
MS
3901 * The @code ipp@ parameter refers to an IPP message previously created using
3902 * the @link ippNew@, @link ippNewRequest@, or @link ippNewResponse@ functions.
a2326b5b
MS
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
65bebeac 3907 * @code ippGetCount(attr)@.
a2326b5b 3908 *
8072030b 3909 * @since CUPS 1.6/macOS 10.8@
a2326b5b
MS
3910 */
3911
3912int /* O - 1 on success, 0 on failure */
6961465f 3913ippSetInteger(ipp_t *ipp, /* I - IPP message */
a2326b5b
MS
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 *
a469f8a5
MS
3944 * The @code ipp@ parameter refers to an IPP message previously created using
3945 * the @link ippNew@, @link ippNewRequest@, or @link ippNewResponse@ functions.
a2326b5b
MS
3946 *
3947 * The @code attr@ parameter may be modified as a result of setting the value.
3948 *
8072030b 3949 * @since CUPS 1.6/macOS 10.8@
a2326b5b
MS
3950 */
3951
3952int /* O - 1 on success, 0 on failure */
6961465f 3953ippSetName(ipp_t *ipp, /* I - IPP message */
a2326b5b
MS
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);
ef416fc2 3966
a2326b5b
MS
3967 /*
3968 * Set the value and return...
3969 */
ef416fc2 3970
a2326b5b
MS
3971 if ((temp = _cupsStrAlloc(name)) != NULL)
3972 {
3973 if ((*attr)->name)
3974 _cupsStrFree((*attr)->name);
ef416fc2 3975
a2326b5b
MS
3976 (*attr)->name = temp;
3977 }
ef416fc2 3978
a2326b5b
MS
3979 return (temp != NULL);
3980}
ef416fc2 3981
ef416fc2 3982
6961465f
MS
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
65bebeac 3992 * @code ippGetCount(attr)@.
6961465f 3993 *
8072030b 3994 * @since CUPS 1.7/macOS 10.9@
6961465f
MS
3995 */
3996
3997int /* O - 1 on success, 0 on failure */
3998ippSetOctetString(
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
7e86f2f6 4054 if ((temp = malloc((size_t)datalen)) != NULL)
6961465f 4055 {
07623986 4056 memcpy(temp, data, (size_t)datalen);
6961465f
MS
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
a2326b5b
MS
4071/*
4072 * 'ippSetOperation()' - Set the operation ID in an IPP request message.
4073 *
a469f8a5
MS
4074 * The @code ipp@ parameter refers to an IPP message previously created using
4075 * the @link ippNew@, @link ippNewRequest@, or @link ippNewResponse@ functions.
a2326b5b 4076 *
8072030b 4077 * @since CUPS 1.6/macOS 10.8@
a2326b5b 4078 */
ef416fc2 4079
a2326b5b
MS
4080int /* O - 1 on success, 0 on failure */
4081ippSetOperation(ipp_t *ipp, /* I - IPP request message */
4082 ipp_op_t op) /* I - Operation ID */
4083{
4084 /*
4085 * Range check input...
4086 */
ef416fc2 4087
a2326b5b
MS
4088 if (!ipp)
4089 return (0);
ef416fc2 4090
a2326b5b
MS
4091 /*
4092 * Set the operation and return...
4093 */
ef416fc2 4094
a2326b5b 4095 ipp->request.op.operation_id = op;
ef416fc2 4096
a2326b5b
MS
4097 return (1);
4098}
ef416fc2 4099
ef416fc2 4100
a2326b5b
MS
4101/*
4102 * 'ippSetRange()' - Set a rangeOfInteger value in an attribute.
4103 *
a469f8a5
MS
4104 * The @code ipp@ parameter refers to an IPP message previously created using
4105 * the @link ippNew@, @link ippNewRequest@, or @link ippNewResponse@ functions.
a2326b5b
MS
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
65bebeac 4110 * @code ippGetCount(attr)@.
a2326b5b 4111 *
8072030b 4112 * @since CUPS 1.6/macOS 10.8@
a2326b5b 4113 */
ef416fc2 4114
a2326b5b 4115int /* O - 1 on success, 0 on failure */
6961465f 4116ippSetRange(ipp_t *ipp, /* I - IPP message */
a2326b5b
MS
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 */
ef416fc2 4123
ef416fc2 4124
a2326b5b
MS
4125 /*
4126 * Range check input...
4127 */
ef416fc2 4128
a2326b5b
MS
4129 if (!ipp || !attr || !*attr || (*attr)->value_tag != IPP_TAG_RANGE ||
4130 element < 0 || element > (*attr)->num_values || lowervalue > uppervalue)
4131 return (0);
ef416fc2 4132
a2326b5b
MS
4133 /*
4134 * Set the value and return...
4135 */
ef416fc2 4136
a2326b5b
MS
4137 if ((value = ipp_set_value(ipp, attr, element)) != NULL)
4138 {
4139 value->range.lower = lowervalue;
4140 value->range.upper = uppervalue;
4141 }
ef416fc2 4142
a2326b5b
MS
4143 return (value != NULL);
4144}
ef416fc2 4145
ef416fc2 4146
a2326b5b
MS
4147/*
4148 * 'ippSetRequestId()' - Set the request ID in an IPP message.
4149 *
a469f8a5
MS
4150 * The @code ipp@ parameter refers to an IPP message previously created using
4151 * the @link ippNew@, @link ippNewRequest@, or @link ippNewResponse@ functions.
a2326b5b
MS
4152 *
4153 * The @code request_id@ parameter must be greater than 0.
4154 *
8072030b 4155 * @since CUPS 1.6/macOS 10.8@
a2326b5b 4156 */
ef416fc2 4157
a2326b5b
MS
4158int /* O - 1 on success, 0 on failure */
4159ippSetRequestId(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 */
83e08001 4167
a2326b5b
MS
4168 if (!ipp)
4169 return (0);
a41f09e2 4170
a2326b5b
MS
4171 /*
4172 * Set the request ID and return...
4173 */
ef416fc2 4174
a2326b5b 4175 ipp->request.any.request_id = request_id;
ef416fc2 4176
a2326b5b
MS
4177 return (1);
4178}
5a738aea 4179
a41f09e2 4180
a2326b5b
MS
4181/*
4182 * 'ippSetResolution()' - Set a resolution value in an attribute.
4183 *
a469f8a5
MS
4184 * The @code ipp@ parameter refers to an IPP message previously created using
4185 * the @link ippNew@, @link ippNewRequest@, or @link ippNewResponse@ functions.
a2326b5b
MS
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
65bebeac 4190 * @code ippGetCount(attr)@.
a2326b5b 4191 *
8072030b 4192 * @since CUPS 1.6/macOS 10.8@
a2326b5b 4193 */
ef416fc2 4194
a2326b5b
MS
4195int /* O - 1 on success, 0 on failure */
4196ippSetResolution(
6961465f 4197 ipp_t *ipp, /* I - IPP message */
a2326b5b
MS
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 */
5a738aea 4205
536bc2c6 4206
a2326b5b
MS
4207 /*
4208 * Range check input...
4209 */
1ff0402e 4210
a2326b5b
MS
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);
1ff0402e 4215
a2326b5b
MS
4216 /*
4217 * Set the value and return...
4218 */
ef416fc2 4219
a2326b5b
MS
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 }
5a738aea 4226
a2326b5b
MS
4227 return (value != NULL);
4228}
a41f09e2 4229
5a738aea 4230
9c80ffa2
MS
4231/*
4232 * 'ippSetState()' - Set the current state of the IPP message.
4233 *
8072030b 4234 * @since CUPS 1.6/macOS 10.8@
9c80ffa2
MS
4235 */
4236
4237int /* O - 1 on success, 0 on failure */
4238ippSetState(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
a2326b5b
MS
4259/*
4260 * 'ippSetStatusCode()' - Set the status code in an IPP response or event message.
4261 *
a469f8a5
MS
4262 * The @code ipp@ parameter refers to an IPP message previously created using
4263 * the @link ippNew@, @link ippNewRequest@, or @link ippNewResponse@ functions.
a2326b5b 4264 *
8072030b 4265 * @since CUPS 1.6/macOS 10.8@
a2326b5b 4266 */
a41f09e2 4267
a2326b5b
MS
4268int /* O - 1 on success, 0 on failure */
4269ippSetStatusCode(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);
ef416fc2 4278
a2326b5b
MS
4279 /*
4280 * Set the status code and return...
4281 */
5a738aea 4282
a2326b5b 4283 ipp->request.status.status_code = status;
a41f09e2 4284
a2326b5b 4285 return (1);
a2326b5b 4286}
5a738aea 4287
ef416fc2 4288
a2326b5b
MS
4289/*
4290 * 'ippSetString()' - Set a string value in an attribute.
4291 *
a469f8a5
MS
4292 * The @code ipp@ parameter refers to an IPP message previously created using
4293 * the @link ippNew@, @link ippNewRequest@, or @link ippNewResponse@ functions.
a2326b5b
MS
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
65bebeac 4298 * @code ippGetCount(attr)@.
a2326b5b 4299 *
8072030b 4300 * @since CUPS 1.6/macOS 10.8@
a2326b5b 4301 */
ef416fc2 4302
a2326b5b 4303int /* O - 1 on success, 0 on failure */
6961465f 4304ippSetString(ipp_t *ipp, /* I - IPP message */
a2326b5b
MS
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 */
e22f464e 4311 ipp_tag_t value_tag; /* Value tag */
ef416fc2 4312
ef416fc2 4313
a2326b5b
MS
4314 /*
4315 * Range check input...
4316 */
ef416fc2 4317
e22f464e
MS
4318 if (attr && *attr)
4319 value_tag = (*attr)->value_tag & IPP_TAG_CUPS_MASK;
4320 else
4321 value_tag = IPP_TAG_ZERO;
4322
3e7fe0ca 4323 if (!ipp || !attr || !*attr ||
e22f464e
MS
4324 (value_tag < IPP_TAG_TEXT && value_tag != IPP_TAG_TEXTLANG &&
4325 value_tag != IPP_TAG_NAMELANG) || value_tag > IPP_TAG_MIMETYPE ||
4326 !strvalue)
a2326b5b 4327 return (0);
a41f09e2 4328
a2326b5b
MS
4329 /*
4330 * Set the value and return...
4331 */
ef416fc2 4332
a2326b5b
MS
4333 if ((value = ipp_set_value(ipp, attr, element)) != NULL)
4334 {
4335 if (element > 0)
4336 value->string.language = (*attr)->values[0].string.language;
ef416fc2 4337
cb7f98ee 4338 if ((int)((*attr)->value_tag) & IPP_TAG_CUPS_CONST)
a2326b5b
MS
4339 value->string.text = (char *)strvalue;
4340 else if ((temp = _cupsStrAlloc(strvalue)) != NULL)
4341 {
4342 if (value->string.text)
4343 _cupsStrFree(value->string.text);
ef416fc2 4344
a2326b5b
MS
4345 value->string.text = temp;
4346 }
4347 else
4348 return (0);
4349 }
a41f09e2 4350
a2326b5b
MS
4351 return (value != NULL);
4352}
ef416fc2 4353
ef416fc2 4354
a469f8a5
MS
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
65bebeac 4364 * @code ippGetCount(attr)@.
a469f8a5
MS
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 *
8072030b 4371 * @since CUPS 1.7/macOS 10.9@
a469f8a5
MS
4372 */
4373
4374int /* O - 1 on success, 0 on failure */
6961465f 4375ippSetStringf(ipp_t *ipp, /* I - IPP message */
a469f8a5
MS
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
65bebeac 4402 * @code ippGetCount(attr)@.
a469f8a5
MS
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 *
8072030b 4409 * @since CUPS 1.7/macOS 10.9@
a469f8a5
MS
4410 */
4411
4412int /* O - 1 on success, 0 on failure */
6961465f 4413ippSetStringfv(ipp_t *ipp, /* I - IPP message */
a469f8a5
MS
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 ||
56cd8959 4438 !format)
a469f8a5
MS
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
7e86f2f6 4456 bytes = (ssize_t)strlen(s);
a469f8a5
MS
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
a2326b5b
MS
4541/*
4542 * 'ippSetValueTag()' - Set the value tag of an attribute.
4543 *
a469f8a5
MS
4544 * The @code ipp@ parameter refers to an IPP message previously created using
4545 * the @link ippNew@, @link ippNewRequest@, or @link ippNewResponse@ functions.
a2326b5b
MS
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 *
8072030b 4561 * @since CUPS 1.6/macOS 10.8@
a2326b5b 4562 */
ef416fc2 4563
a2326b5b
MS
4564int /* O - 1 on success, 0 on failure */
4565ippSetValueTag(
6961465f 4566 ipp_t *ipp, /* I - IPP message */
a2326b5b
MS
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 */
ef416fc2 4576
ef416fc2 4577
a2326b5b
MS
4578 /*
4579 * Range check input...
4580 */
1f6f3dbc 4581
a469f8a5 4582 if (!ipp || !attr || !*attr)
a2326b5b 4583 return (0);
ef416fc2 4584
a2326b5b
MS
4585 /*
4586 * If there is no change, return immediately...
4587 */
ef416fc2 4588
a2326b5b
MS
4589 if (value_tag == (*attr)->value_tag)
4590 return (1);
ef416fc2 4591
a2326b5b
MS
4592 /*
4593 * Otherwise implement changes as needed...
4594 */
ef416fc2 4595
cb7f98ee 4596 temp_tag = (ipp_tag_t)((int)((*attr)->value_tag) & IPP_TAG_CUPS_MASK);
4400e98d 4597
a2326b5b
MS
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 */
ef416fc2 4610
a2326b5b
MS
4611 if ((*attr)->num_values > 0)
4612 ipp_free_values(*attr, 0, (*attr)->num_values);
ef416fc2 4613
a2326b5b
MS
4614 /*
4615 * Set out-of-band value...
4616 */
ef416fc2 4617
a2326b5b
MS
4618 (*attr)->value_tag = value_tag;
4619 break;
91c84a35 4620
a2326b5b
MS
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 }
ef416fc2 4632
a2326b5b
MS
4633 (*attr)->value_tag = IPP_TAG_RANGE;
4634 break;
ef416fc2 4635
a2326b5b
MS
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);
ef416fc2 4641
cb7f98ee 4642 (*attr)->value_tag = (ipp_tag_t)(IPP_TAG_NAME | ((*attr)->value_tag & IPP_TAG_CUPS_CONST));
ef416fc2 4643 break;
4644
a2326b5b
MS
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
cb7f98ee 4683 if ((int)(*attr)->value_tag & IPP_TAG_CUPS_CONST)
a2326b5b
MS
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;
ef416fc2 4696 break;
4697
a2326b5b
MS
4698 case IPP_TAG_KEYWORD :
4699 if (temp_tag == IPP_TAG_NAME || temp_tag == IPP_TAG_NAMELANG)
4700 break; /* Silently "allow" name -> keyword */
4701
ef416fc2 4702 default :
a2326b5b 4703 return (0);
ef416fc2 4704 }
4705
a2326b5b
MS
4706 return (1);
4707}
4708
4709
4710/*
4711 * 'ippSetVersion()' - Set the version number in an IPP message.
4712 *
a469f8a5
MS
4713 * The @code ipp@ parameter refers to an IPP message previously created using
4714 * the @link ippNew@, @link ippNewRequest@, or @link ippNewResponse@ functions.
a2326b5b
MS
4715 *
4716 * The valid version numbers are currently 1.0, 1.1, 2.0, 2.1, and 2.2.
4717 *
8072030b 4718 * @since CUPS 1.6/macOS 10.8@
a2326b5b
MS
4719 */
4720
4721int /* O - 1 on success, 0 on failure */
4722ippSetVersion(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
7e86f2f6
MS
4737 ipp->request.any.version[0] = (ipp_uchar_t)major;
4738 ipp->request.any.version[1] = (ipp_uchar_t)minor;
89d46774 4739
a2326b5b 4740 return (1);
ef416fc2 4741}
4742
4743
4744/*
65bebeac 4745 * 'ippTimeToDate()' - Convert from time in seconds to RFC 2579 format.
ef416fc2 4746 */
4747
65bebeac
MS
4748const ipp_uchar_t * /* O - RFC-2579 date/time data */
4749ippTimeToDate(time_t t) /* I - Time in seconds */
ef416fc2 4750{
4751 struct tm *unixdate; /* UNIX unixdate/time info */
4752 ipp_uchar_t *date = _cupsGlobals()->ipp_date;
65bebeac 4753 /* RFC-2579 date/time data */
ef416fc2 4754
4755
4756 /*
65bebeac 4757 * RFC-2579 date/time format is:
ef416fc2 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
7e86f2f6
MS
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;
ef416fc2 4783 date[7] = 0;
4784 date[8] = '+';
4785 date[9] = 0;
4786 date[10] = 0;
4787
4788 return (date);
4789}
4790
4791
c1420c87
MS
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
65bebeac 4797 * failure, @link cupsLastErrorString@ is set to a human-readable message.
c1420c87 4798 *
8072030b 4799 * @since CUPS 1.7/macOS 10.9@
c1420c87
MS
4800 */
4801
4802int /* O - 1 if valid, 0 otherwise */
4803ippValidateAttribute(
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 */
c1420c87
MS
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 {
2da2477d 4836 ipp_set_error(IPP_STATUS_ERROR_BAD_REQUEST, _("\"%s\": Bad attribute name - invalid character (RFC 8011 section 5.1.4)."), attr->name);
c1420c87
MS
4837 return (0);
4838 }
4839
4840 if ((ptr - attr->name) > 255)
4841 {
2da2477d 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));
c1420c87
MS
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 {
2da2477d 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);
c1420c87
MS
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 {
2da2477d 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);
c1420c87
MS
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 {
2da2477d 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);
c1420c87
MS
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 {
2da2477d 4892 ipp_set_error(IPP_STATUS_ERROR_BAD_REQUEST, _("\"%s\": Bad dateTime month %u (RFC 8011 section 5.1.15)."), attr->name, date[2]);
c1420c87
MS
4893 return (0);
4894 }
4895
4896 if (date[3] < 1 || date[3] > 31)
4897 {
2da2477d 4898 ipp_set_error(IPP_STATUS_ERROR_BAD_REQUEST, _("\"%s\": Bad dateTime day %u (RFC 8011 section 5.1.15)."), attr->name, date[3]);
c1420c87
MS
4899 return (0);
4900 }
4901
4902 if (date[4] > 23)
4903 {
2da2477d 4904 ipp_set_error(IPP_STATUS_ERROR_BAD_REQUEST, _("\"%s\": Bad dateTime hours %u (RFC 8011 section 5.1.15)."), attr->name, date[4]);
c1420c87
MS
4905 return (0);
4906 }
4907
4908 if (date[5] > 59)
4909 {
2da2477d 4910 ipp_set_error(IPP_STATUS_ERROR_BAD_REQUEST, _("\"%s\": Bad dateTime minutes %u (RFC 8011 section 5.1.15)."), attr->name, date[5]);
c1420c87
MS
4911 return (0);
4912 }
4913
4914 if (date[6] > 60)
4915 {
2da2477d 4916 ipp_set_error(IPP_STATUS_ERROR_BAD_REQUEST, _("\"%s\": Bad dateTime seconds %u (RFC 8011 section 5.1.15)."), attr->name, date[6]);
c1420c87
MS
4917 return (0);
4918 }
4919
4920 if (date[7] > 9)
4921 {
2da2477d 4922 ipp_set_error(IPP_STATUS_ERROR_BAD_REQUEST, _("\"%s\": Bad dateTime deciseconds %u (RFC 8011 section 5.1.15)."), attr->name, date[7]);
c1420c87
MS
4923 return (0);
4924 }
4925
4926 if (date[8] != '-' && date[8] != '+')
4927 {
2da2477d 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]);
c1420c87
MS
4929 return (0);
4930 }
4931
4932 if (date[9] > 11)
4933 {
2da2477d 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]);
c1420c87
MS
4935 return (0);
4936 }
4937
4938 if (date[10] > 59)
4939 {
2da2477d 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]);
c1420c87
MS
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 {
2da2477d 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");
c1420c87
MS
4952 return (0);
4953 }
4954
4955 if (attr->values[i].resolution.yres <= 0)
4956 {
2da2477d 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");
c1420c87
MS
4958 return (0);
4959 }
4960
2da2477d 4961 if (attr->values[i].resolution.units != IPP_RES_PER_INCH && attr->values[i].resolution.units != IPP_RES_PER_CM)
c1420c87 4962 {
2da2477d 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");
c1420c87
MS
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 {
2da2477d 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);
c1420c87
MS
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;
4cadd620
MS
5028 else if ((*ptr < ' ' && *ptr != '\n' && *ptr != '\r' && *ptr != '\t') || *ptr == 0x7f)
5029 break;
c1420c87
MS
5030 }
5031
4cadd620
MS
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)
c1420c87 5038 {
2da2477d 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);
c1420c87
MS
5040 return (0);
5041 }
5042
5043 if ((ptr - attr->values[i].string.text) > (IPP_MAX_TEXT - 1))
5044 {
2da2477d 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));
c1420c87
MS
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;
4cadd620
MS
5086 else if (*ptr < ' ' || *ptr == 0x7f)
5087 break;
c1420c87
MS
5088 }
5089
4cadd620
MS
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)
c1420c87 5096 {
2da2477d 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);
c1420c87
MS
5098 return (0);
5099 }
5100
5101 if ((ptr - attr->values[i].string.text) > (IPP_MAX_NAME - 1))
5102 {
2da2477d 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));
c1420c87
MS
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 ++)
2da2477d 5113 {
c1420c87
MS
5114 if (!isalnum(*ptr & 255) && *ptr != '-' && *ptr != '.' &&
5115 *ptr != '_')
5116 break;
2da2477d 5117 }
c1420c87
MS
5118
5119 if (*ptr || ptr == attr->values[i].string.text)
5120 {
2da2477d 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);
c1420c87
MS
5122 return (0);
5123 }
5124
5125 if ((ptr - attr->values[i].string.text) > (IPP_MAX_KEYWORD - 1))
5126 {
2da2477d 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));
c1420c87
MS
5128 return (0);
5129 }
5130 }
5131 break;
5132
5133 case IPP_TAG_URI :
5134 for (i = 0; i < attr->num_values; i ++)
5135 {
2da2477d 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));
c1420c87 5137
cb7f98ee 5138 if (uri_status < HTTP_URI_STATUS_OK)
c1420c87 5139 {
2da2477d 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));
c1420c87
MS
5141 return (0);
5142 }
5143
5144 if (strlen(attr->values[i].string.text) > (IPP_MAX_URI - 1))
5145 {
2da2477d 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));
c1420c87
MS
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 ++)
2da2477d 5158 {
c1420c87
MS
5159 if (!islower(*ptr & 255) && !isdigit(*ptr & 255) &&
5160 *ptr != '+' && *ptr != '-' && *ptr != '.')
5161 break;
2da2477d 5162 }
c1420c87
MS
5163 }
5164
5165 if (*ptr || ptr == attr->values[i].string.text)
5166 {
2da2477d 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);
c1420c87
MS
5168 return (0);
5169 }
5170
5171 if ((ptr - attr->values[i].string.text) > (IPP_MAX_URISCHEME - 1))
5172 {
2da2477d 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));
c1420c87
MS
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 ++)
2da2477d 5183 {
c1420c87
MS
5184 if (!isprint(*ptr & 255) || isupper(*ptr & 255) ||
5185 isspace(*ptr & 255))
5186 break;
2da2477d 5187 }
c1420c87
MS
5188
5189 if (*ptr || ptr == attr->values[i].string.text)
5190 {
2da2477d 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);
c1420c87
MS
5192 return (0);
5193 }
5194
5195 if ((ptr - attr->values[i].string.text) > (IPP_MAX_CHARSET - 1))
5196 {
2da2477d 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));
c1420c87
MS
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));
2da2477d 5229 ipp_set_error(IPP_STATUS_ERROR_INTERNAL, _("Unable to compile naturalLanguage regular expression: %s."), temp);
c1420c87
MS
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 {
2da2477d 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);
c1420c87
MS
5238 regfree(&re);
5239 return (0);
5240 }
5241
5242 if (strlen(attr->values[i].string.text) > (IPP_MAX_LANGUAGE - 1))
5243 {
2da2477d 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));
c1420c87
MS
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));
2da2477d 5274 ipp_set_error(IPP_STATUS_ERROR_BAD_REQUEST, _("Unable to compile mimeMediaType regular expression: %s."), temp);
c1420c87
MS
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 {
2da2477d 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);
c1420c87
MS
5283 regfree(&re);
5284 return (0);
5285 }
5286
5287 if (strlen(attr->values[i].string.text) > (IPP_MAX_MIMETYPE - 1))
5288 {
2da2477d 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));
c1420c87
MS
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
65bebeac
MS
5310 * attribute. Like @link ippValidateAttribute@, @link cupsLastErrorString@ is
5311 * set to a human-readable message on failure.
c1420c87 5312 *
8072030b 5313 * @since CUPS 1.7/macOS 10.9@
c1420c87
MS
5314 */
5315
5316int /* O - 1 if valid, 0 otherwise */
5317ippValidateAttributes(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
ef416fc2 5333/*
5334 * 'ippWrite()' - Write data for an IPP message to a HTTP connection.
5335 */
5336
5337ipp_state_t /* O - Current state */
5338ippWrite(http_t *http, /* I - HTTP connection */
5339 ipp_t *ipp) /* I - IPP data */
5340{
807315e6 5341 DEBUG_printf(("ippWrite(http=%p, ipp=%p)", (void *)http, (void *)ipp));
ef416fc2 5342
1ff0402e 5343 if (!http)
cb7f98ee 5344 return (IPP_STATE_ERROR);
ef416fc2 5345
e07d4801 5346 return (ippWriteIO(http, (ipp_iocb_t)httpWrite2, http->blocking, NULL, ipp));
ef416fc2 5347}
5348
5349
5350/*
5351 * 'ippWriteFile()' - Write data for an IPP message to a file.
5352 *
8072030b 5353 * @since CUPS 1.1.19/macOS 10.3@
ef416fc2 5354 */
5355
5356ipp_state_t /* O - Current state */
5357ippWriteFile(int fd, /* I - HTTP data */
5358 ipp_t *ipp) /* I - IPP data */
5359{
807315e6 5360 DEBUG_printf(("ippWriteFile(fd=%d, ipp=%p)", fd, (void *)ipp));
ef416fc2 5361
cb7f98ee 5362 ipp->state = IPP_STATE_IDLE;
ef416fc2 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 *
8072030b 5371 * @since CUPS 1.2/macOS 10.5@
ef416fc2 5372 */
5373
5374ipp_state_t /* O - Current state */
5375ippWriteIO(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 */
1f6f3dbc 5383 unsigned char *buffer, /* Data buffer */
ef416fc2 5384 *bufptr; /* Pointer into buffer */
5385 ipp_attribute_t *attr; /* Current attribute */
a2326b5b 5386 _ipp_value_t *value; /* Current value */
ef416fc2 5387
5388
807315e6 5389 DEBUG_printf(("ippWriteIO(dst=%p, cb=%p, blocking=%d, parent=%p, ipp=%p)", (void *)dst, (void *)cb, blocking, (void *)parent, (void *)ipp));
ef416fc2 5390
1ff0402e 5391 if (!dst || !ipp)
cb7f98ee 5392 return (IPP_STATE_ERROR);
ef416fc2 5393
dcb445bc 5394 if ((buffer = (unsigned char *)_cupsBufferGet(IPP_BUF_SIZE)) == NULL)
1f6f3dbc 5395 {
e07d4801 5396 DEBUG_puts("1ippWriteIO: Unable to get write buffer");
cb7f98ee 5397 return (IPP_STATE_ERROR);
1f6f3dbc
MS
5398 }
5399
ef416fc2 5400 switch (ipp->state)
5401 {
cb7f98ee 5402 case IPP_STATE_IDLE :
ef416fc2 5403 ipp->state ++; /* Avoid common problem... */
5404
cb7f98ee 5405 case IPP_STATE_HEADER :
ef416fc2 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];
7e86f2f6
MS
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;
ef416fc2 5427
ba55dc12
MS
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
7e86f2f6 5434 if ((*cb)(dst, buffer, (size_t)(bufptr - buffer)) < 0)
ef416fc2 5435 {
e07d4801 5436 DEBUG_puts("1ippWriteIO: Could not write IPP header...");
dcb445bc 5437 _cupsBufferRelease((char *)buffer);
cb7f98ee 5438 return (IPP_STATE_ERROR);
ef416fc2 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
cb7f98ee 5447 ipp->state = IPP_STATE_ATTRIBUTE;
ef416fc2 5448 ipp->current = ipp->attrs;
5449 ipp->curtag = IPP_TAG_ZERO;
5450
807315e6 5451 DEBUG_printf(("1ippWriteIO: ipp->current=%p", (void *)ipp->current));
ef416fc2 5452
5453 /*
5454 * If blocking is disabled, stop here...
5455 */
5456
5457 if (!blocking)
5458 break;
5459
cb7f98ee 5460 case IPP_STATE_ATTRIBUTE :
ef416fc2 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
ba55dc12 5472 if (!parent)
ef416fc2 5473 {
ba55dc12
MS
5474 if (ipp->curtag != attr->group_tag)
5475 {
5476 /*
5477 * Send a group tag byte...
5478 */
ef416fc2 5479
ba55dc12 5480 ipp->curtag = attr->group_tag;
ef416fc2 5481
ba55dc12
MS
5482 if (attr->group_tag == IPP_TAG_ZERO)
5483 continue;
ef416fc2 5484
ba55dc12
MS
5485 DEBUG_printf(("2ippWriteIO: wrote group tag=%x(%s)",
5486 attr->group_tag, ippTagString(attr->group_tag)));
7e86f2f6 5487 *bufptr++ = (ipp_uchar_t)attr->group_tag;
ba55dc12
MS
5488 }
5489 else if (attr->group_tag == IPP_TAG_ZERO)
5490 continue;
ef416fc2 5491 }
ba55dc12
MS
5492
5493 DEBUG_printf(("1ippWriteIO: %s (%s%s)", attr->name,
5494 attr->num_values > 1 ? "1setOf " : "",
5495 ippTagString(attr->value_tag)));
ef416fc2 5496
5497 /*
a2326b5b 5498 * Write the attribute tag and name.
ef416fc2 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
a2326b5b 5513 if ((n = (int)strlen(attr->name)) > (IPP_BUF_SIZE - 8))
1f6f3dbc 5514 {
e07d4801 5515 DEBUG_printf(("1ippWriteIO: Attribute name too long (%d)", n));
dcb445bc 5516 _cupsBufferRelease((char *)buffer);
cb7f98ee 5517 return (IPP_STATE_ERROR);
1f6f3dbc 5518 }
ef416fc2 5519
5520 /*
5521 * Write the value tag, name length, and name string...
5522 */
5523
e07d4801 5524 DEBUG_printf(("2ippWriteIO: writing value tag=%x(%s)",
1ff0402e 5525 attr->value_tag, ippTagString(attr->value_tag)));
e07d4801 5526 DEBUG_printf(("2ippWriteIO: writing name=%d,\"%s\"", n,
1ff0402e 5527 attr->name));
ef416fc2 5528
a2326b5b
MS
5529 if (attr->value_tag > 0xff)
5530 {
5531 *bufptr++ = IPP_TAG_EXTENSION;
7e86f2f6
MS
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;
a2326b5b
MS
5536 }
5537 else
7e86f2f6 5538 *bufptr++ = (ipp_uchar_t)attr->value_tag;
a2326b5b 5539
7e86f2f6
MS
5540 *bufptr++ = (ipp_uchar_t)(n >> 8);
5541 *bufptr++ = (ipp_uchar_t)n;
07623986 5542 memcpy(bufptr, attr->name, (size_t)n);
ef416fc2 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
a2326b5b 5552 if ((n = (int)strlen(attr->name)) > (IPP_BUF_SIZE - 12))
1f6f3dbc 5553 {
e07d4801 5554 DEBUG_printf(("1ippWriteIO: Attribute name too long (%d)", n));
dcb445bc 5555 _cupsBufferRelease((char *)buffer);
cb7f98ee 5556 return (IPP_STATE_ERROR);
1f6f3dbc 5557 }
ef416fc2 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
e07d4801 5564 DEBUG_printf(("2ippWriteIO: writing value tag=%x(memberName)",
ef416fc2 5565 IPP_TAG_MEMBERNAME));
e07d4801 5566 DEBUG_printf(("2ippWriteIO: writing name=%d,\"%s\"", n,
1ff0402e 5567 attr->name));
e07d4801 5568 DEBUG_printf(("2ippWriteIO: writing value tag=%x(%s)",
1ff0402e 5569 attr->value_tag, ippTagString(attr->value_tag)));
e07d4801 5570 DEBUG_puts("2ippWriteIO: writing name=0,\"\"");
ef416fc2 5571
5572 *bufptr++ = IPP_TAG_MEMBERNAME;
5573 *bufptr++ = 0;
5574 *bufptr++ = 0;
7e86f2f6
MS
5575 *bufptr++ = (ipp_uchar_t)(n >> 8);
5576 *bufptr++ = (ipp_uchar_t)n;
07623986 5577 memcpy(bufptr, attr->name, (size_t)n);
ef416fc2 5578 bufptr += n;
5579
a2326b5b
MS
5580 if (attr->value_tag > 0xff)
5581 {
5582 *bufptr++ = IPP_TAG_EXTENSION;
7e86f2f6
MS
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;
a2326b5b
MS
5587 }
5588 else
7e86f2f6 5589 *bufptr++ = (ipp_uchar_t)attr->value_tag;
a2326b5b 5590
ef416fc2 5591 *bufptr++ = 0;
5592 *bufptr++ = 0;
5593 }
5594
5595 /*
5596 * Now write the attribute value(s)...
5597 */
5598
cb7f98ee 5599 switch (attr->value_tag & ~IPP_TAG_CUPS_CONST)
ef416fc2 5600 {
a2326b5b
MS
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
ef416fc2 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 {
1f6f3dbc 5618 if ((IPP_BUF_SIZE - (bufptr - buffer)) < 9)
ef416fc2 5619 {
7e86f2f6 5620 if ((*cb)(dst, buffer, (size_t)(bufptr - buffer)) < 0)
ef416fc2 5621 {
e07d4801 5622 DEBUG_puts("1ippWriteIO: Could not write IPP "
1ff0402e 5623 "attribute...");
dcb445bc 5624 _cupsBufferRelease((char *)buffer);
cb7f98ee 5625 return (IPP_STATE_ERROR);
ef416fc2 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
7e86f2f6 5638 *bufptr++ = (ipp_uchar_t)attr->value_tag;
ef416fc2 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;
7e86f2f6
MS
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;
ef416fc2 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 {
1f6f3dbc 5664 if ((IPP_BUF_SIZE - (bufptr - buffer)) < 6)
ef416fc2 5665 {
7e86f2f6 5666 if ((*cb)(dst, buffer, (size_t)(bufptr - buffer)) < 0)
ef416fc2 5667 {
e07d4801 5668 DEBUG_puts("1ippWriteIO: Could not write IPP "
1ff0402e 5669 "attribute...");
dcb445bc 5670 _cupsBufferRelease((char *)buffer);
cb7f98ee 5671 return (IPP_STATE_ERROR);
ef416fc2 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
7e86f2f6 5684 *bufptr++ = (ipp_uchar_t)attr->value_tag;
ef416fc2 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;
7e86f2f6 5697 *bufptr++ = (ipp_uchar_t)value->boolean;
ef416fc2 5698 }
5699 break;
5700
5701 case IPP_TAG_TEXT :
5702 case IPP_TAG_NAME :
5703 case IPP_TAG_KEYWORD :
ef416fc2 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
e07d4801 5720 DEBUG_printf(("2ippWriteIO: writing value tag=%x(%s)",
1ff0402e
MS
5721 attr->value_tag,
5722 ippTagString(attr->value_tag)));
e07d4801 5723 DEBUG_printf(("2ippWriteIO: writing name=0,\"\""));
ef416fc2 5724
1f6f3dbc 5725 if ((IPP_BUF_SIZE - (bufptr - buffer)) < 3)
ef416fc2 5726 {
7e86f2f6 5727 if ((*cb)(dst, buffer, (size_t)(bufptr - buffer)) < 0)
ef416fc2 5728 {
e07d4801 5729 DEBUG_puts("1ippWriteIO: Could not write IPP "
1ff0402e 5730 "attribute...");
dcb445bc 5731 _cupsBufferRelease((char *)buffer);
cb7f98ee 5732 return (IPP_STATE_ERROR);
ef416fc2 5733 }
5734
5735 bufptr = buffer;
5736 }
5737
7e86f2f6 5738 *bufptr++ = (ipp_uchar_t)attr->value_tag;
ef416fc2 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
1f6f3dbc
MS
5748 if (n > (IPP_BUF_SIZE - 2))
5749 {
e07d4801 5750 DEBUG_printf(("1ippWriteIO: String too long (%d)", n));
dcb445bc 5751 _cupsBufferRelease((char *)buffer);
cb7f98ee 5752 return (IPP_STATE_ERROR);
1f6f3dbc 5753 }
ef416fc2 5754
e07d4801 5755 DEBUG_printf(("2ippWriteIO: writing string=%d,\"%s\"", n,
ef416fc2 5756 value->string.text));
5757
1f6f3dbc 5758 if ((int)(IPP_BUF_SIZE - (bufptr - buffer)) < (n + 2))
ef416fc2 5759 {
7e86f2f6 5760 if ((*cb)(dst, buffer, (size_t)(bufptr - buffer)) < 0)
ef416fc2 5761 {
e07d4801 5762 DEBUG_puts("1ippWriteIO: Could not write IPP "
1ff0402e 5763 "attribute...");
dcb445bc 5764 _cupsBufferRelease((char *)buffer);
cb7f98ee 5765 return (IPP_STATE_ERROR);
ef416fc2 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
a41f09e2 5774 * in C strings. Also, strings cannot be longer than IPP_MAX_LENGTH
ef416fc2 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
7e86f2f6
MS
5781 *bufptr++ = (ipp_uchar_t)(n >> 8);
5782 *bufptr++ = (ipp_uchar_t)n;
ef416fc2 5783
5784 if (n > 0)
5785 {
07623986 5786 memcpy(bufptr, value->string.text, (size_t)n);
ef416fc2 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 {
1f6f3dbc 5797 if ((IPP_BUF_SIZE - (bufptr - buffer)) < 16)
ef416fc2 5798 {
7e86f2f6 5799 if ((*cb)(dst, buffer, (size_t)(bufptr - buffer)) < 0)
ef416fc2 5800 {
e07d4801 5801 DEBUG_puts("1ippWriteIO: Could not write IPP "
1ff0402e 5802 "attribute...");
dcb445bc 5803 _cupsBufferRelease((char *)buffer);
cb7f98ee 5804 return (IPP_STATE_ERROR);
ef416fc2 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
7e86f2f6 5817 *bufptr++ = (ipp_uchar_t)attr->value_tag;
ef416fc2 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 {
1f6f3dbc 5842 if ((IPP_BUF_SIZE - (bufptr - buffer)) < 14)
ef416fc2 5843 {
7e86f2f6 5844 if ((*cb)(dst, buffer, (size_t)(bufptr - buffer)) < 0)
ef416fc2 5845 {
e07d4801 5846 DEBUG_puts("1ippWriteIO: Could not write IPP "
1ff0402e 5847 "attribute...");
dcb445bc 5848 _cupsBufferRelease((char *)buffer);
cb7f98ee 5849 return (IPP_STATE_ERROR);
ef416fc2 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
7e86f2f6 5862 *bufptr++ = (ipp_uchar_t)attr->value_tag;
ef416fc2 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;
7e86f2f6
MS
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;
ef416fc2 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 {
1f6f3dbc 5895 if ((IPP_BUF_SIZE - (bufptr - buffer)) < 13)
ef416fc2 5896 {
7e86f2f6 5897 if ((*cb)(dst, buffer, (size_t)(bufptr - buffer)) < 0)
ef416fc2 5898 {
e07d4801 5899 DEBUG_puts("1ippWriteIO: Could not write IPP "
1ff0402e 5900 "attribute...");
dcb445bc 5901 _cupsBufferRelease((char *)buffer);
cb7f98ee 5902 return (IPP_STATE_ERROR);
ef416fc2 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
7e86f2f6 5915 *bufptr++ = (ipp_uchar_t)attr->value_tag;
ef416fc2 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;
7e86f2f6
MS
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;
ef416fc2 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
1f6f3dbc 5954 if ((IPP_BUF_SIZE - (bufptr - buffer)) < 3)
ef416fc2 5955 {
7e86f2f6 5956 if ((*cb)(dst, buffer, (size_t)(bufptr - buffer)) < 0)
ef416fc2 5957 {
e07d4801 5958 DEBUG_puts("1ippWriteIO: Could not write IPP "
1ff0402e 5959 "attribute...");
dcb445bc 5960 _cupsBufferRelease((char *)buffer);
cb7f98ee 5961 return (IPP_STATE_ERROR);
ef416fc2 5962 }
5963
5964 bufptr = buffer;
5965 }
5966
7e86f2f6 5967 *bufptr++ = (ipp_uchar_t)attr->value_tag;
ef416fc2 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
a2326b5b
MS
5984 if (value->string.language != NULL)
5985 n += (int)strlen(value->string.language);
ef416fc2 5986
5987 if (value->string.text != NULL)
b86bc4cf 5988 n += (int)strlen(value->string.text);
ef416fc2 5989
1f6f3dbc
MS
5990 if (n > (IPP_BUF_SIZE - 2))
5991 {
e07d4801
MS
5992 DEBUG_printf(("1ippWriteIO: text/nameWithLanguage value "
5993 "too long (%d)", n));
dcb445bc 5994 _cupsBufferRelease((char *)buffer);
cb7f98ee 5995 return (IPP_STATE_ERROR);
1f6f3dbc 5996 }
ef416fc2 5997
1f6f3dbc 5998 if ((int)(IPP_BUF_SIZE - (bufptr - buffer)) < (n + 2))
ef416fc2 5999 {
7e86f2f6 6000 if ((*cb)(dst, buffer, (size_t)(bufptr - buffer)) < 0)
ef416fc2 6001 {
e07d4801 6002 DEBUG_puts("1ippWriteIO: Could not write IPP "
1ff0402e 6003 "attribute...");
dcb445bc 6004 _cupsBufferRelease((char *)buffer);
cb7f98ee 6005 return (IPP_STATE_ERROR);
ef416fc2 6006 }
6007
6008 bufptr = buffer;
6009 }
6010
6011 /* Length of entire value */
7e86f2f6
MS
6012 *bufptr++ = (ipp_uchar_t)(n >> 8);
6013 *bufptr++ = (ipp_uchar_t)n;
ef416fc2 6014
a2326b5b
MS
6015 /* Length of language */
6016 if (value->string.language != NULL)
6017 n = (int)strlen(value->string.language);
ef416fc2 6018 else
6019 n = 0;
6020
7e86f2f6
MS
6021 *bufptr++ = (ipp_uchar_t)(n >> 8);
6022 *bufptr++ = (ipp_uchar_t)n;
ef416fc2 6023
a2326b5b 6024 /* Language */
ef416fc2 6025 if (n > 0)
6026 {
07623986 6027 memcpy(bufptr, value->string.language, (size_t)n);
ef416fc2 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
7e86f2f6
MS
6037 *bufptr++ = (ipp_uchar_t)(n >> 8);
6038 *bufptr++ = (ipp_uchar_t)n;
ef416fc2 6039
6040 /* Text */
6041 if (n > 0)
6042 {
07623986 6043 memcpy(bufptr, value->string.text, (size_t)n);
ef416fc2 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
1f6f3dbc 6061 if ((IPP_BUF_SIZE - (bufptr - buffer)) < 5)
ef416fc2 6062 {
7e86f2f6 6063 if ((*cb)(dst, buffer, (size_t)(bufptr - buffer)) < 0)
ef416fc2 6064 {
e07d4801 6065 DEBUG_puts("1ippWriteIO: Could not write IPP "
1ff0402e 6066 "attribute...");
dcb445bc 6067 _cupsBufferRelease((char *)buffer);
cb7f98ee 6068 return (IPP_STATE_ERROR);
ef416fc2 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
7e86f2f6 6081 *bufptr++ = (ipp_uchar_t)attr->value_tag;
ef416fc2 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
7e86f2f6 6093 if ((*cb)(dst, buffer, (size_t)(bufptr - buffer)) < 0)
ef416fc2 6094 {
e07d4801 6095 DEBUG_puts("1ippWriteIO: Could not write IPP "
1ff0402e 6096 "attribute...");
dcb445bc 6097 _cupsBufferRelease((char *)buffer);
cb7f98ee 6098 return (IPP_STATE_ERROR);
ef416fc2 6099 }
6100
6101 bufptr = buffer;
6102
6103 /*
6104 * Then write the collection attribute...
6105 */
6106
cb7f98ee 6107 value->collection->state = IPP_STATE_IDLE;
ef416fc2 6108
1f6f3dbc 6109 if (ippWriteIO(dst, cb, 1, ipp,
cb7f98ee 6110 value->collection) == IPP_STATE_ERROR)
1f6f3dbc 6111 {
e07d4801 6112 DEBUG_puts("1ippWriteIO: Unable to write collection value");
dcb445bc 6113 _cupsBufferRelease((char *)buffer);
cb7f98ee 6114 return (IPP_STATE_ERROR);
1f6f3dbc 6115 }
ef416fc2 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
1f6f3dbc 6131 if ((IPP_BUF_SIZE - (bufptr - buffer)) < 3)
ef416fc2 6132 {
7e86f2f6 6133 if ((*cb)(dst, buffer, (size_t)(bufptr - buffer)) < 0)
ef416fc2 6134 {
e07d4801 6135 DEBUG_puts("1ippWriteIO: Could not write IPP "
1ff0402e 6136 "attribute...");
dcb445bc 6137 _cupsBufferRelease((char *)buffer);
cb7f98ee 6138 return (IPP_STATE_ERROR);
ef416fc2 6139 }
6140
6141 bufptr = buffer;
6142 }
6143
7e86f2f6 6144 *bufptr++ = (ipp_uchar_t)attr->value_tag;
ef416fc2 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
1f6f3dbc
MS
6158 if (n > (IPP_BUF_SIZE - 2))
6159 {
e07d4801 6160 DEBUG_printf(("1ippWriteIO: Data length too long (%d)",
1f6f3dbc 6161 n));
dcb445bc 6162 _cupsBufferRelease((char *)buffer);
cb7f98ee 6163 return (IPP_STATE_ERROR);
1f6f3dbc 6164 }
ef416fc2 6165
1f6f3dbc 6166 if ((int)(IPP_BUF_SIZE - (bufptr - buffer)) < (n + 2))
ef416fc2 6167 {
7e86f2f6 6168 if ((*cb)(dst, buffer, (size_t)(bufptr - buffer)) < 0)
ef416fc2 6169 {
e07d4801 6170 DEBUG_puts("1ippWriteIO: Could not write IPP "
1ff0402e 6171 "attribute...");
dcb445bc 6172 _cupsBufferRelease((char *)buffer);
cb7f98ee 6173 return (IPP_STATE_ERROR);
ef416fc2 6174 }
6175
6176 bufptr = buffer;
6177 }
6178
6179 /* Length of unknown value */
7e86f2f6
MS
6180 *bufptr++ = (ipp_uchar_t)(n >> 8);
6181 *bufptr++ = (ipp_uchar_t)n;
ef416fc2 6182
6183 /* Value */
6184 if (n > 0)
6185 {
07623986 6186 memcpy(bufptr, value->unknown.data, (size_t)n);
ef416fc2 6187 bufptr += n;
6188 }
6189 }
6190 break;
6191 }
6192
6193 /*
6194 * Write the data out...
6195 */
6196
ba55dc12 6197 if (bufptr > buffer)
ef416fc2 6198 {
7e86f2f6 6199 if ((*cb)(dst, buffer, (size_t)(bufptr - buffer)) < 0)
ba55dc12
MS
6200 {
6201 DEBUG_puts("1ippWriteIO: Could not write IPP attribute...");
dcb445bc 6202 _cupsBufferRelease((char *)buffer);
cb7f98ee 6203 return (IPP_STATE_ERROR);
ba55dc12 6204 }
ef416fc2 6205
ba55dc12
MS
6206 DEBUG_printf(("2ippWriteIO: wrote %d bytes",
6207 (int)(bufptr - buffer)));
6208 }
ef416fc2 6209
6210 /*
21f36711
MS
6211 * If blocking is disabled and we aren't at the end of the attribute
6212 * list, stop here...
ef416fc2 6213 */
6214
21f36711 6215 if (!blocking && ipp->current)
ef416fc2 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
7e86f2f6 6241 if ((*cb)(dst, buffer, (size_t)n) < 0)
ef416fc2 6242 {
e07d4801 6243 DEBUG_puts("1ippWriteIO: Could not write IPP end-tag...");
dcb445bc 6244 _cupsBufferRelease((char *)buffer);
cb7f98ee 6245 return (IPP_STATE_ERROR);
ef416fc2 6246 }
6247
cb7f98ee 6248 ipp->state = IPP_STATE_DATA;
ef416fc2 6249 }
6250 break;
6251
cb7f98ee 6252 case IPP_STATE_DATA :
ef416fc2 6253 break;
6254
6255 default :
6256 break; /* anti-compiler-warning-code */
6257 }
6258
dcb445bc 6259 _cupsBufferRelease((char *)buffer);
1f6f3dbc 6260
ef416fc2 6261 return (ipp->state);
6262}
6263
6264
6265/*
a2326b5b 6266 * 'ipp_add_attr()' - Add a new attribute to the message.
ef416fc2 6267 */
6268
a2326b5b
MS
6269static ipp_attribute_t * /* O - New attribute */
6270ipp_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 */
ef416fc2 6275{
a2326b5b 6276 int alloc_values; /* Number of values to allocate */
ef416fc2 6277 ipp_attribute_t *attr; /* New attribute */
6278
6279
807315e6 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));
a2326b5b
MS
6281
6282 /*
6283 * Range check input...
6284 */
ef416fc2 6285
1ff0402e 6286 if (!ipp || num_values < 0)
ef416fc2 6287 return (NULL);
6288
a2326b5b
MS
6289 /*
6290 * Allocate memory, rounding the allocation up as needed...
6291 */
ef416fc2 6292
a2326b5b 6293 if (num_values <= 1)
9c80ffa2 6294 alloc_values = 1;
a2326b5b
MS
6295 else
6296 alloc_values = (num_values + IPP_MAX_VALUES - 1) & ~(IPP_MAX_VALUES - 1);
ef416fc2 6297
a2326b5b 6298 attr = calloc(sizeof(ipp_attribute_t) +
7e86f2f6 6299 (size_t)(alloc_values - 1) * sizeof(_ipp_value_t), 1);
ef416fc2 6300
a2326b5b 6301 if (attr)
ef416fc2 6302 {
a2326b5b
MS
6303 /*
6304 * Initialize attribute...
6305 */
ef416fc2 6306
b908d72c
MS
6307 DEBUG_printf(("4debug_alloc: %p %s %s%s (%d values)", (void *)attr, name, num_values > 1 ? "1setOf " : "", ippTagString(value_tag), num_values));
6308
a2326b5b
MS
6309 if (name)
6310 attr->name = _cupsStrAlloc(name);
ef416fc2 6311
a2326b5b
MS
6312 attr->group_tag = group_tag;
6313 attr->value_tag = value_tag;
6314 attr->num_values = num_values;
4400e98d 6315
a2326b5b
MS
6316 /*
6317 * Add it to the end of the linked list...
6318 */
4400e98d 6319
a2326b5b
MS
6320 if (ipp->last)
6321 ipp->last->next = attr;
6322 else
6323 ipp->attrs = attr;
5a738aea 6324
a2326b5b
MS
6325 ipp->prev = ipp->last;
6326 ipp->last = ipp->current = attr;
ef416fc2 6327 }
6328
807315e6 6329 DEBUG_printf(("5ipp_add_attr: Returning %p", (void *)attr));
ef416fc2 6330
a2326b5b 6331 return (attr);
ef416fc2 6332}
6333
6334
a2326b5b
MS
6335/*
6336 * 'ipp_free_values()' - Free attribute values.
6337 */
6338
6339static void
6340ipp_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
807315e6 6348 DEBUG_printf(("4ipp_free_values(attr=%p, element=%d, count=%d)", (void *)attr, element, count));
a2326b5b 6349
cb7f98ee 6350 if (!(attr->value_tag & IPP_TAG_CUPS_CONST))
a2326b5b
MS
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 :
5a9febac
MS
6360 if (element == 0 && count == attr->num_values &&
6361 attr->values[0].string.language)
6362 {
a2326b5b 6363 _cupsStrFree(attr->values[0].string.language);
5a9febac
MS
6364 attr->values[0].string.language = NULL;
6365 }
0fa6c7fa 6366 /* Fall through to other string values */
a2326b5b
MS
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 ++)
5a9febac 6380 {
a2326b5b 6381 _cupsStrFree(value->string.text);
5a9febac
MS
6382 value->string.text = NULL;
6383 }
a2326b5b
MS
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 ++)
5a9febac 6404 {
a2326b5b 6405 ippDelete(value->collection);
5a9febac
MS
6406 value->collection = NULL;
6407 }
a2326b5b
MS
6408 break;
6409
6410 case IPP_TAG_STRING :
6411 default :
6412 for (i = count, value = attr->values + element;
6413 i > 0;
6414 i --, value ++)
5a9febac 6415 {
a2326b5b 6416 if (value->unknown.data)
5a9febac 6417 {
a2326b5b 6418 free(value->unknown.data);
5a9febac
MS
6419 value->unknown.data = NULL;
6420 }
6421 }
a2326b5b
MS
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,
7e86f2f6 6432 (size_t)(attr->num_values - count - element) * sizeof(_ipp_value_t));
a2326b5b
MS
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
6445static char * /* O - Language code string */
6446ipp_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
7e86f2f6 6464 *bufptr++ = (char)_cups_tolower(*value);
a2326b5b
MS
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
6483static char * /* O - Language code string */
6484ipp_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
ef416fc2 6502/*
6503 * 'ipp_length()' - Compute the length of an IPP message or collection value.
6504 */
6505
6506static size_t /* O - Size of IPP message */
6507ipp_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 */
a2326b5b 6511 size_t bytes; /* Number of bytes */
ef416fc2 6512 ipp_attribute_t *attr; /* Current attribute */
6513 ipp_tag_t group; /* Current group */
a2326b5b
MS
6514 _ipp_value_t *value; /* Current value */
6515
ef416fc2 6516
807315e6 6517 DEBUG_printf(("3ipp_length(ipp=%p, collection=%d)", (void *)ipp, collection));
ef416fc2 6518
a2326b5b
MS
6519 if (!ipp)
6520 {
6521 DEBUG_puts("4ipp_length: Returning 0 bytes");
ef416fc2 6522 return (0);
a2326b5b 6523 }
ef416fc2 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
a2326b5b
MS
6551 DEBUG_printf(("5ipp_length: attr->name=\"%s\", attr->num_values=%d, "
6552 "bytes=" CUPS_LLFMT, attr->name, attr->num_values, CUPS_LLCAST bytes));
ef416fc2 6553
426184cb 6554 if ((attr->value_tag & ~IPP_TAG_CUPS_CONST) < IPP_TAG_EXTENSION)
7e86f2f6 6555 bytes += (size_t)attr->num_values;/* Value tag for each value */
a2326b5b 6556 else
7e86f2f6
MS
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 */
ef416fc2 6564
6565 if (collection)
6566 bytes += 5; /* Add membername overhead */
6567
cb7f98ee 6568 switch (attr->value_tag & ~IPP_TAG_CUPS_CONST)
ef416fc2 6569 {
a2326b5b
MS
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
ef416fc2 6579 case IPP_TAG_INTEGER :
6580 case IPP_TAG_ENUM :
7e86f2f6 6581 bytes += (size_t)(4 * attr->num_values);
ef416fc2 6582 break;
6583
6584 case IPP_TAG_BOOLEAN :
7e86f2f6 6585 bytes += (size_t)attr->num_values;
ef416fc2 6586 break;
6587
6588 case IPP_TAG_TEXT :
6589 case IPP_TAG_NAME :
6590 case IPP_TAG_KEYWORD :
ef416fc2 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 ++)
a2326b5b
MS
6599 if (value->string.text)
6600 bytes += strlen(value->string.text);
ef416fc2 6601 break;
6602
6603 case IPP_TAG_DATE :
7e86f2f6 6604 bytes += (size_t)(11 * attr->num_values);
ef416fc2 6605 break;
6606
6607 case IPP_TAG_RESOLUTION :
7e86f2f6 6608 bytes += (size_t)(9 * attr->num_values);
ef416fc2 6609 break;
6610
6611 case IPP_TAG_RANGE :
7e86f2f6 6612 bytes += (size_t)(8 * attr->num_values);
ef416fc2 6613 break;
6614
6615 case IPP_TAG_TEXTLANG :
6616 case IPP_TAG_NAMELANG :
7e86f2f6
MS
6617 bytes += (size_t)(4 * attr->num_values);
6618 /* Charset + text length */
ef416fc2 6619
6620 for (i = 0, value = attr->values;
6621 i < attr->num_values;
6622 i ++, value ++)
6623 {
a2326b5b
MS
6624 if (value->string.language)
6625 bytes += strlen(value->string.language);
ef416fc2 6626
a2326b5b
MS
6627 if (value->string.text)
6628 bytes += strlen(value->string.text);
ef416fc2 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 ++)
a2326b5b 6636 bytes += ipp_length(value->collection, 1);
ef416fc2 6637 break;
6638
6639 default :
6640 for (i = 0, value = attr->values;
6641 i < attr->num_values;
6642 i ++, value ++)
7e86f2f6 6643 bytes += (size_t)value->unknown.length;
ef416fc2 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
a2326b5b 6658 DEBUG_printf(("4ipp_length: Returning " CUPS_LLFMT " bytes", CUPS_LLCAST bytes));
ef416fc2 6659
6660 return (bytes);
6661}
6662
6663
6664/*
6665 * 'ipp_read_http()' - Semi-blocking read on a HTTP connection...
6666 */
6667
a4d04587 6668static ssize_t /* O - Number of bytes read */
ef416fc2 6669ipp_read_http(http_t *http, /* I - Client connection */
6670 ipp_uchar_t *buffer, /* O - Buffer for data */
a4d04587 6671 size_t length) /* I - Total length */
ef416fc2 6672{
7e86f2f6
MS
6673 ssize_t tbytes, /* Total bytes read */
6674 bytes; /* Bytes read this pass */
aaf19ab0 6675
ef416fc2 6676
807315e6 6677 DEBUG_printf(("7ipp_read_http(http=%p, buffer=%p, length=%d)", (void *)http, (void *)buffer, (int)length));
ef416fc2 6678
6679 /*
6680 * Loop until all bytes are read...
6681 */
6682
ae71f5de
MS
6683 for (tbytes = 0, bytes = 0;
6684 tbytes < (int)length;
6685 tbytes += bytes, buffer += bytes)
ef416fc2 6686 {
7e86f2f6 6687 DEBUG_printf(("9ipp_read_http: tbytes=" CUPS_LLFMT ", http->state=%d", CUPS_LLCAST tbytes, http->state));
ef416fc2 6688
cb7f98ee 6689 if (http->state == HTTP_STATE_WAITING)
ef416fc2 6690 break;
6691
a29fd7dd 6692 if (http->used == 0 && !http->blocking)
ef416fc2 6693 {
6694 /*
a29fd7dd 6695 * Wait up to 10 seconds for more data on non-blocking sockets...
ef416fc2 6696 */
6697
a29fd7dd 6698 if (!httpWait(http, 10000))
ef416fc2 6699 {
6700 /*
a29fd7dd 6701 * Signal no data...
ef416fc2 6702 */
6703
a29fd7dd
MS
6704 bytes = -1;
6705 break;
ef416fc2 6706 }
a29fd7dd 6707 }
ba7900a5
MS
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 }
ef416fc2 6724
7e86f2f6 6725 if ((bytes = httpRead2(http, (char *)buffer, length - (size_t)tbytes)) < 0)
a29fd7dd 6726 {
d1c13e16 6727#ifdef WIN32
a29fd7dd 6728 break;
d1c13e16 6729#else
a29fd7dd
MS
6730 if (errno != EAGAIN && errno != EINTR)
6731 break;
d1c13e16 6732
a29fd7dd 6733 bytes = 0;
d1c13e16 6734#endif /* WIN32 */
ef416fc2 6735 }
a29fd7dd
MS
6736 else if (bytes == 0)
6737 break;
ef416fc2 6738 }
6739
6740 /*
6741 * Return the number of bytes read...
6742 */
6743
6744 if (tbytes == 0 && bytes < 0)
6745 tbytes = -1;
6746
7e86f2f6 6747 DEBUG_printf(("8ipp_read_http: Returning " CUPS_LLFMT " bytes", CUPS_LLCAST tbytes));
ef416fc2 6748
6749 return (tbytes);
6750}
6751
6752
6753/*
6754 * 'ipp_read_file()' - Read IPP data from a file.
6755 */
6756
a4d04587 6757static ssize_t /* O - Number of bytes read */
ef416fc2 6758ipp_read_file(int *fd, /* I - File descriptor */
6759 ipp_uchar_t *buffer, /* O - Read buffer */
a4d04587 6760 size_t length) /* I - Number of bytes to read */
ef416fc2 6761{
b86bc4cf 6762#ifdef WIN32
6763 return ((ssize_t)read(*fd, buffer, (unsigned)length));
6764#else
ef416fc2 6765 return (read(*fd, buffer, length));
b86bc4cf 6766#endif /* WIN32 */
ef416fc2 6767}
6768
6769
c1420c87
MS
6770/*
6771 * 'ipp_set_error()' - Set a formatted, localized error string.
6772 */
6773
6774static void
6775ipp_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
a2326b5b 6793/*
9c80ffa2
MS
6794 * 'ipp_set_value()' - Get the value element from an attribute, expanding it as
6795 * needed.
a2326b5b
MS
6796 */
6797
6798static _ipp_value_t * /* O - IPP value element or NULL on error */
9c80ffa2 6799ipp_set_value(ipp_t *ipp, /* IO - IPP message */
a2326b5b
MS
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)
9c80ffa2 6816 alloc_values = 1;
a2326b5b 6817 else
9c80ffa2
MS
6818 alloc_values = (temp->num_values + IPP_MAX_VALUES - 1) &
6819 ~(IPP_MAX_VALUES - 1);
a2326b5b
MS
6820
6821 if (element < alloc_values)
9c80ffa2
MS
6822 {
6823 if (element >= temp->num_values)
6824 temp->num_values = element + 1;
6825
a2326b5b 6826 return (temp->values + element);
9c80ffa2 6827 }
a2326b5b
MS
6828
6829 /*
9c80ffa2
MS
6830 * Otherwise re-allocate the attribute - we allocate in groups of IPP_MAX_VALUE
6831 * values when num_values > 1.
a2326b5b
MS
6832 */
6833
6834 if (alloc_values < IPP_MAX_VALUES)
6835 alloc_values = IPP_MAX_VALUES;
6836 else
6837 alloc_values += IPP_MAX_VALUES;
6838
9c80ffa2
MS
6839 DEBUG_printf(("4ipp_set_value: Reallocating for up to %d values.",
6840 alloc_values));
a2326b5b
MS
6841
6842 /*
6843 * Reallocate memory...
6844 */
6845
7e86f2f6 6846 if ((temp = realloc(temp, sizeof(ipp_attribute_t) + (size_t)(alloc_values - 1) * sizeof(_ipp_value_t))) == NULL)
a2326b5b 6847 {
cb7f98ee 6848 _cupsSetHTTPError(HTTP_STATUS_ERROR);
a2326b5b
MS
6849 DEBUG_puts("4ipp_set_value: Unable to resize attribute.");
6850 return (NULL);
6851 }
6852
6853 /*
6854 * Zero the new memory...
6855 */
6856
7e86f2f6 6857 memset(temp->values + temp->num_values, 0, (size_t)(alloc_values - temp->num_values) * sizeof(_ipp_value_t));
a2326b5b
MS
6858
6859 if (temp != *attr)
6860 {
6861 /*
6862 * Reset pointers in the list...
6863 */
6864
c0a47c11 6865 DEBUG_printf(("4debug_free: %p %s", (void *)*attr, temp->name));
b908d72c
MS
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
a2326b5b
MS
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;
cb7f98ee 6893 _cupsSetError(IPP_STATUS_ERROR_INTERNAL,
9c80ffa2 6894 _("IPP attribute is not a member of the message."), 1);
a2326b5b
MS
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
9c80ffa2
MS
6918 if (element >= temp->num_values)
6919 temp->num_values = element + 1;
6920
a2326b5b
MS
6921 return (temp->values + element);
6922}
6923
6924
ef416fc2 6925/*
6926 * 'ipp_write_file()' - Write IPP data to a file.
6927 */
6928
a4d04587 6929static ssize_t /* O - Number of bytes written */
ef416fc2 6930ipp_write_file(int *fd, /* I - File descriptor */
6931 ipp_uchar_t *buffer, /* I - Data to write */
a4d04587 6932 size_t length) /* I - Number of bytes to write */
ef416fc2 6933{
b86bc4cf 6934#ifdef WIN32
6935 return ((ssize_t)write(*fd, buffer, (unsigned)length));
6936#else
ef416fc2 6937 return (write(*fd, buffer, length));
b86bc4cf 6938#endif /* WIN32 */
ef416fc2 6939}