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