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