2 * "$Id: ipp-support.c 9371 2010-11-17 06:21:32Z mike $"
4 * Internet Printing Protocol support functions for CUPS.
6 * Copyright 2007-2011 by Apple Inc.
7 * Copyright 1997-2007 by Easy Software Products, all rights reserved.
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/".
15 * This file is subject to the Apple OS-Developed Software exception.
19 * _ippAttrString() - Convert the attribute's value to a string.
20 * ippErrorString() - Return a name for the given status code.
21 * ippErrorValue() - Return a status code for the given name.
22 * ippOpString() - Return a name for the given operation id.
23 * ippOpValue() - Return an operation id for the given name.
24 * ippPort() - Return the default IPP port number.
25 * ippSetPort() - Set the default port number.
26 * ippTagString() - Return the tag name corresponding to a tag value.
27 * ippTagValue() - Return the tag value corresponding to a tag name.
28 * ipp_col_string() - Convert a collection to a string.
32 * Include necessary headers...
35 #include "cups-private.h"
42 static const char * const ipp_status_oks
[] = /* "OK" status codes */
45 "successful-ok-ignored-or-substituted-attributes",
46 "successful-ok-conflicting-attributes",
47 "successful-ok-ignored-subscriptions",
48 "successful-ok-ignored-notifications",
49 "successful-ok-too-many-events",
50 "successful-ok-but-cancel-subscription",
51 "successful-ok-events-complete"
53 * const ipp_status_400s
[] = /* Client errors */
55 "client-error-bad-request",
56 "client-error-forbidden",
57 "client-error-not-authenticated",
58 "client-error-not-authorized",
59 "client-error-not-possible",
60 "client-error-timeout",
61 "client-error-not-found",
63 "client-error-request-entity-too-large",
64 "client-error-request-value-too-long",
65 "client-error-document-format-not-supported",
66 "client-error-attributes-or-values-not-supported",
67 "client-error-uri-scheme-not-supported",
68 "client-error-charset-not-supported",
69 "client-error-conflicting-attributes",
70 "client-error-compression-not-supported",
71 "client-error-compression-error",
72 "client-error-document-format-error",
73 "client-error-document-access-error",
74 "client-error-attributes-not-settable",
75 "client-error-ignored-all-subscriptions",
76 "client-error-too-many-subscriptions",
77 "client-error-ignored-all-notifications",
78 "client-error-print-support-file-not-found"
80 * const ipp_status_500s
[] = /* Server errors */
82 "server-error-internal-error",
83 "server-error-operation-not-supported",
84 "server-error-service-unavailable",
85 "server-error-version-not-supported",
86 "server-error-device-error",
87 "server-error-temporary-error",
88 "server-error-not-accepting-jobs",
90 "server-error-job-canceled",
91 "server-error-multiple-document-jobs-not-supported",
92 "server-error-printer-is-deactivated"
94 * const ipp_status_1000s
[] = /* CUPS internal */
96 "cups-authorization-canceled",
98 "cups-upgrade-required"
100 static char * const ipp_std_ops
[] =
102 /* 0x0000 - 0x000f */
112 "Get-Job-Attributes",
114 "Get-Printer-Attributes",
120 /* 0x0010 - 0x001f */
124 "Set-Printer-Attributes",
125 "Set-Job-Attributes",
126 "Get-Printer-Supported-Values",
127 "Create-Printer-Subscription",
128 "Create-Job-Subscription",
129 "Get-Subscription-Attributes",
131 "Renew-Subscription",
132 "Cancel-Subscription",
134 "Send-Notifications",
138 /* 0x0020 - 0x002f */
140 "Get-Printer-Support-Files",
143 "Pause-Printer-After-Current-Job",
145 "Release-Held-New-Jobs",
146 "Deactivate-Printer",
152 "Cancel-Current-Job",
153 "Suspend-Current-Job",
156 /* 0x0030 - 0x003b */
158 "Schedule-Job-After",
161 "Get-Document-Attributes",
164 "Set-Document-Attributes",
171 * const ipp_cups_ops
[] =
175 "CUPS-Add-Modify-Printer",
176 "CUPS-Delete-Printer",
178 "CUPS-Add-Modify-Class",
186 "CUPS-Authenticate-Job",
189 * const ipp_cups_ops2
[] =
193 * const ipp_tag_names
[] =
194 { /* Value/group tag names */
196 "operation-attributes-tag",
198 "job-attributes-tag", /* 0x02 */
199 "end-of-attributes-tag",
201 "printer-attributes-tag",
203 "unsupported-attributes-tag",
205 "subscription-attributes-tag",
207 "event-notification-attributes-tag",
210 "document-attributes-tag",
218 "unsupported", /* 0x10 */
219 "default", /* 0x11 */
220 "unknown", /* 0x12 */
221 "no-value", /* 0x13 */
223 "not-settable", /* 0x15 */
224 "delete-attribute", /* 0x16 */
225 "admin-define", /* 0x17 */
235 "integer", /* 0x21 */
236 "boolean", /* 0x22 */
250 "octetString", /* 0x30 */
251 "dateTime", /* 0x31 */
252 "resolution", /* 0x32 */
253 "rangeOfInteger", /* 0x33 */
254 "collection", /* 0x34 */
255 "textWithLanguage", /* 0x35 */
256 "nameWithLanguage", /* 0x36 */
257 "endCollection", /* 0x37 */
267 "textWithoutLanguage",/* 0x41 */
268 "nameWithoutLanguage",/* 0x42 */
270 "keyword", /* 0x44 */
272 "uriScheme", /* 0x46 */
273 "charset", /* 0x47 */
274 "naturalLanguage", /* 0x48 */
275 "mimeMediaType", /* 0x49 */
276 "memberAttrName" /* 0x4a */
278 static const char * const job_states
[] =
279 { /* job-state enums */
283 "processing-stopped",
288 static const char * const printer_states
[] =
289 { /* printer-state enums */
300 static size_t ipp_col_string(ipp_t
*col
, char *buffer
, size_t bufsize
);
304 * '_ippAttrString()' - Convert the attribute's value to a string.
306 * Returns the number of bytes that would be written, not including the
307 * trailing nul. The buffer pointer can be NULL to get the required length,
308 * just like (v)snprintf.
311 size_t /* O - Number of bytes less nul */
312 _ippAttrString(ipp_attribute_t
*attr
, /* I - Attribute */
313 char *buffer
, /* I - String buffer or NULL */
314 size_t bufsize
) /* I - Size of string buffer */
316 int i
; /* Looping var */
317 char *bufptr
, /* Pointer into buffer */
318 *bufend
, /* End of buffer */
319 temp
[256]; /* Temporary string */
320 const char *ptr
; /* Pointer into string */
321 ipp_value_t
*val
; /* Current value */
324 if (!attr
|| !attr
->name
)
334 bufend
= buffer
+ bufsize
- 1;
338 for (i
= attr
->num_values
, val
= attr
->values
; i
> 0; i
--, val
++)
340 if (val
> attr
->values
)
342 if (buffer
&& bufptr
< bufend
)
348 switch (attr
->value_tag
& ~IPP_TAG_COPY
)
351 if (!strcmp(attr
->name
, "printer-state") &&
352 val
->integer
>= IPP_PRINTER_IDLE
&&
353 val
->integer
<= IPP_PRINTER_STOPPED
)
355 ptr
= printer_states
[val
->integer
- IPP_PRINTER_IDLE
];
357 if (buffer
&& bufptr
< bufend
)
358 strlcpy(bufptr
, ptr
, bufend
- bufptr
+ 1);
360 bufptr
+= strlen(ptr
);
363 else if (!strcmp(attr
->name
, "job-state") &&
364 val
->integer
>= IPP_JOB_PENDING
&&
365 val
->integer
<= IPP_JOB_COMPLETED
)
367 ptr
= job_states
[val
->integer
- IPP_JOB_PENDING
];
369 if (buffer
&& bufptr
< bufend
)
370 strlcpy(bufptr
, ptr
, bufend
- bufptr
+ 1);
372 bufptr
+= strlen(ptr
);
375 else if (!strcmp(attr
->name
, "operations-supported"))
377 ptr
= ippOpString(val
->integer
);
379 if (buffer
&& bufptr
< bufend
)
380 strlcpy(bufptr
, ptr
, bufend
- bufptr
+ 1);
382 bufptr
+= strlen(ptr
);
386 case IPP_TAG_INTEGER
:
387 if (buffer
&& bufptr
< bufend
)
388 bufptr
+= snprintf(bufptr
, bufend
- bufptr
+ 1, "%d", val
->integer
);
390 bufptr
+= snprintf(temp
, sizeof(temp
), "%d", val
->integer
);
393 case IPP_TAG_BOOLEAN
:
394 if (buffer
&& bufptr
< bufend
)
395 strlcpy(bufptr
, val
->boolean
? "true" : "false",
396 bufend
- bufptr
+ 1);
398 bufptr
+= val
->boolean
? 4 : 5;
402 if (buffer
&& bufptr
< bufend
)
403 bufptr
+= snprintf(bufptr
, bufend
- bufptr
+ 1, "%d-%d",
404 val
->range
.lower
, val
->range
.upper
);
406 bufptr
+= snprintf(temp
, sizeof(temp
), "%d-%d", val
->range
.lower
,
410 case IPP_TAG_RESOLUTION
:
411 if (buffer
&& bufptr
< bufend
)
412 bufptr
+= snprintf(bufptr
, bufend
- bufptr
+ 1, "%dx%d%s",
413 val
->resolution
.xres
, val
->resolution
.yres
,
414 val
->resolution
.units
== IPP_RES_PER_INCH
?
417 bufptr
+= snprintf(temp
, sizeof(temp
), "%dx%d%s",
418 val
->resolution
.xres
, val
->resolution
.yres
,
419 val
->resolution
.units
== IPP_RES_PER_INCH
?
425 unsigned year
; /* Year */
427 year
= (val
->date
[0] << 8) + val
->date
[1];
429 if (val
->date
[9] == 0 && val
->date
[10] == 0)
430 snprintf(temp
, sizeof(temp
), "%04u-%02u-%02uT%02u:%02u:%02uZ",
431 year
, val
->date
[2], val
->date
[3], val
->date
[4],
432 val
->date
[5], val
->date
[6]);
434 snprintf(temp
, sizeof(temp
),
435 "%04u-%02u-%02uT%02u:%02u:%02u%c%02u%02u",
436 year
, val
->date
[2], val
->date
[3], val
->date
[4],
437 val
->date
[5], val
->date
[6], val
->date
[8], val
->date
[9],
440 if (buffer
&& bufptr
< bufend
)
441 strlcpy(bufptr
, temp
, bufend
- bufptr
+ 1);
443 bufptr
+= strlen(temp
);
449 case IPP_TAG_KEYWORD
:
450 case IPP_TAG_CHARSET
:
452 case IPP_TAG_URISCHEME
:
453 case IPP_TAG_MIMETYPE
:
454 case IPP_TAG_LANGUAGE
:
455 case IPP_TAG_TEXTLANG
:
456 case IPP_TAG_NAMELANG
:
457 if (!val
->string
.text
)
460 for (ptr
= val
->string
.text
; *ptr
; ptr
++)
462 if (*ptr
== '\\' || *ptr
== '\"')
464 if (buffer
&& bufptr
< bufend
)
469 if (buffer
&& bufptr
< bufend
)
475 case IPP_TAG_BEGIN_COLLECTION
:
476 if (buffer
&& bufptr
< bufend
)
477 bufptr
+= ipp_col_string(val
->collection
, bufptr
,
478 bufend
- bufptr
+ 1);
480 bufptr
+= ipp_col_string(val
->collection
, NULL
, 0);
483 case IPP_TAG_STRING
:
484 for (ptr
= val
->string
.text
; *ptr
; ptr
++)
486 if (*ptr
== '\\' || _cups_isspace(*ptr
))
488 if (buffer
&& bufptr
< bufend
)
492 if (buffer
&& bufptr
< bufend
)
496 else if (!isprint(*ptr
& 255))
498 if (buffer
&& bufptr
< bufend
)
499 bufptr
+= snprintf(bufptr
, bufend
- bufptr
+ 1, "\\%03o",
502 bufptr
+= snprintf(temp
, sizeof(temp
), "\\%03o",
507 if (buffer
&& bufptr
< bufend
)
515 ptr
= ippTagString(attr
->value_tag
);
516 if (buffer
&& bufptr
< bufend
)
517 strlcpy(bufptr
, ptr
, bufend
- bufptr
+ 1);
518 bufptr
+= strlen(ptr
);
523 if (buffer
&& bufptr
< bufend
)
528 return (bufptr
- buffer
);
533 * 'ippErrorString()' - Return a name for the given status code.
536 const char * /* O - Text string */
537 ippErrorString(ipp_status_t error
) /* I - Error status */
539 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals */
543 * See if the error code is a known value...
546 if (error
>= IPP_OK
&& error
<= IPP_OK_EVENTS_COMPLETE
)
547 return (ipp_status_oks
[error
]);
548 else if (error
== IPP_REDIRECTION_OTHER_SITE
)
549 return ("redirection-other-site");
550 else if (error
== CUPS_SEE_OTHER
)
551 return ("cups-see-other");
552 else if (error
>= IPP_BAD_REQUEST
&& error
<= IPP_PRINT_SUPPORT_FILE_NOT_FOUND
)
553 return (ipp_status_400s
[error
- IPP_BAD_REQUEST
]);
554 else if (error
>= IPP_INTERNAL_ERROR
&& error
<= IPP_PRINTER_IS_DEACTIVATED
)
555 return (ipp_status_500s
[error
- IPP_INTERNAL_ERROR
]);
556 else if (error
>= IPP_AUTHENTICATION_CANCELED
&& error
<= IPP_UPGRADE_REQUIRED
)
557 return (ipp_status_1000s
[error
- IPP_AUTHENTICATION_CANCELED
]);
560 * No, build an "0xxxxx" error string...
563 sprintf(cg
->ipp_unknown
, "0x%04x", error
);
565 return (cg
->ipp_unknown
);
570 * 'ippErrorValue()' - Return a status code for the given name.
572 * @since CUPS 1.2/Mac OS X 10.5@
575 ipp_status_t
/* O - IPP status code */
576 ippErrorValue(const char *name
) /* I - Name */
581 for (i
= 0; i
< (sizeof(ipp_status_oks
) / sizeof(ipp_status_oks
[0])); i
++)
582 if (!_cups_strcasecmp(name
, ipp_status_oks
[i
]))
583 return ((ipp_status_t
)i
);
585 if (!_cups_strcasecmp(name
, "redirection-other-site"))
586 return (IPP_REDIRECTION_OTHER_SITE
);
588 if (!_cups_strcasecmp(name
, "cups-see-other"))
589 return (CUPS_SEE_OTHER
);
591 for (i
= 0; i
< (sizeof(ipp_status_400s
) / sizeof(ipp_status_400s
[0])); i
++)
592 if (!_cups_strcasecmp(name
, ipp_status_400s
[i
]))
593 return ((ipp_status_t
)(i
+ 0x400));
595 for (i
= 0; i
< (sizeof(ipp_status_500s
) / sizeof(ipp_status_500s
[0])); i
++)
596 if (!_cups_strcasecmp(name
, ipp_status_500s
[i
]))
597 return ((ipp_status_t
)(i
+ 0x500));
599 for (i
= 0; i
< (sizeof(ipp_status_1000s
) / sizeof(ipp_status_1000s
[0])); i
++)
600 if (!_cups_strcasecmp(name
, ipp_status_1000s
[i
]))
601 return ((ipp_status_t
)(i
+ 0x1000));
603 return ((ipp_status_t
)-1);
608 * 'ippOpString()' - Return a name for the given operation id.
610 * @since CUPS 1.2/Mac OS X 10.5@
613 const char * /* O - Name */
614 ippOpString(ipp_op_t op
) /* I - Operation ID */
616 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals */
620 * See if the operation ID is a known value...
623 if (op
>= IPP_PRINT_JOB
&& op
<= IPP_CLOSE_JOB
)
624 return (ipp_std_ops
[op
]);
625 else if (op
== IPP_PRIVATE
)
626 return ("windows-ext");
627 else if (op
>= CUPS_GET_DEFAULT
&& op
<= CUPS_GET_PPD
)
628 return (ipp_cups_ops
[op
- CUPS_GET_DEFAULT
]);
629 else if (op
== CUPS_GET_DOCUMENT
)
630 return (ipp_cups_ops2
[0]);
633 * No, build an "0xxxxx" operation string...
636 sprintf(cg
->ipp_unknown
, "0x%04x", op
);
638 return (cg
->ipp_unknown
);
643 * 'ippOpValue()' - Return an operation id for the given name.
645 * @since CUPS 1.2/Mac OS X 10.5@
648 ipp_op_t
/* O - Operation ID */
649 ippOpValue(const char *name
) /* I - Textual name */
654 if (!strncmp(name
, "0x", 2))
655 return ((ipp_op_t
)strtol(name
+ 2, NULL
, 16));
657 for (i
= 0; i
< (sizeof(ipp_std_ops
) / sizeof(ipp_std_ops
[0])); i
++)
658 if (!_cups_strcasecmp(name
, ipp_std_ops
[i
]))
659 return ((ipp_op_t
)i
);
661 if (!_cups_strcasecmp(name
, "windows-ext"))
662 return (IPP_PRIVATE
);
664 for (i
= 0; i
< (sizeof(ipp_cups_ops
) / sizeof(ipp_cups_ops
[0])); i
++)
665 if (!_cups_strcasecmp(name
, ipp_cups_ops
[i
]))
666 return ((ipp_op_t
)(i
+ 0x4001));
668 for (i
= 0; i
< (sizeof(ipp_cups_ops2
) / sizeof(ipp_cups_ops2
[0])); i
++)
669 if (!_cups_strcasecmp(name
, ipp_cups_ops2
[i
]))
670 return ((ipp_op_t
)(i
+ 0x4027));
672 if (!_cups_strcasecmp(name
, "CUPS-Add-Class"))
673 return (CUPS_ADD_MODIFY_CLASS
);
675 if (!_cups_strcasecmp(name
, "CUPS-Add-Printer"))
676 return (CUPS_ADD_MODIFY_PRINTER
);
678 return ((ipp_op_t
)-1);
683 * 'ippPort()' - Return the default IPP port number.
686 int /* O - Port number */
689 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals */
692 DEBUG_puts("ippPort()");
697 DEBUG_printf(("1ippPort: Returning %d...", cg
->ipp_port
));
699 return (cg
->ipp_port
);
704 * 'ippSetPort()' - Set the default port number.
708 ippSetPort(int p
) /* I - Port number to use */
710 DEBUG_printf(("ippSetPort(p=%d)", p
));
712 _cupsGlobals()->ipp_port
= p
;
717 * 'ippTagString()' - Return the tag name corresponding to a tag value.
719 * The returned names are defined in RFC 2911 and 3382.
721 * @since CUPS 1.4/Mac OS X 10.6@
724 const char * /* O - Tag name */
725 ippTagString(ipp_tag_t tag
) /* I - Tag value */
729 if (tag
< (ipp_tag_t
)(sizeof(ipp_tag_names
) / sizeof(ipp_tag_names
[0])))
730 return (ipp_tag_names
[tag
]);
737 * 'ippTagValue()' - Return the tag value corresponding to a tag name.
739 * The tag names are defined in RFC 2911 and 3382.
741 * @since CUPS 1.4/Mac OS X 10.6@
744 ipp_tag_t
/* O - Tag value */
745 ippTagValue(const char *name
) /* I - Tag name */
747 int i
; /* Looping var */
750 for (i
= 0; i
< (sizeof(ipp_tag_names
) / sizeof(ipp_tag_names
[0])); i
++)
751 if (!_cups_strcasecmp(name
, ipp_tag_names
[i
]))
752 return ((ipp_tag_t
)i
);
754 if (!_cups_strcasecmp(name
, "operation"))
755 return (IPP_TAG_OPERATION
);
756 else if (!_cups_strcasecmp(name
, "job"))
757 return (IPP_TAG_JOB
);
758 else if (!_cups_strcasecmp(name
, "printer"))
759 return (IPP_TAG_PRINTER
);
760 else if (!_cups_strcasecmp(name
, "unsupported"))
761 return (IPP_TAG_UNSUPPORTED_GROUP
);
762 else if (!_cups_strcasecmp(name
, "subscription"))
763 return (IPP_TAG_SUBSCRIPTION
);
764 else if (!_cups_strcasecmp(name
, "event"))
765 return (IPP_TAG_EVENT_NOTIFICATION
);
766 else if (!_cups_strcasecmp(name
, "language"))
767 return (IPP_TAG_LANGUAGE
);
768 else if (!_cups_strcasecmp(name
, "mimetype"))
769 return (IPP_TAG_MIMETYPE
);
770 else if (!_cups_strcasecmp(name
, "name"))
771 return (IPP_TAG_NAME
);
772 else if (!_cups_strcasecmp(name
, "text"))
773 return (IPP_TAG_TEXT
);
774 else if (!_cups_strcasecmp(name
, "begCollection"))
775 return (IPP_TAG_BEGIN_COLLECTION
);
777 return (IPP_TAG_ZERO
);
782 * 'ipp_col_string()' - Convert a collection to a string.
785 static size_t /* O - Number of bytes */
786 ipp_col_string(ipp_t
*col
, /* I - Collection attribute */
787 char *buffer
, /* I - Buffer or NULL */
788 size_t bufsize
) /* I - Size of buffer */
790 char *bufptr
, /* Position in buffer */
791 *bufend
, /* End of buffer */
792 temp
[256]; /* Temporary string */
793 ipp_attribute_t
*attr
; /* Current member attribute */
797 bufend
= buffer
+ bufsize
- 1;
799 if (buffer
&& bufptr
< bufend
)
803 for (attr
= col
->attrs
; attr
; attr
= attr
->next
)
808 if (buffer
&& bufptr
< bufend
)
809 bufptr
+= snprintf(bufptr
, bufend
- bufptr
+ 1, "%s=", attr
->name
);
811 bufptr
+= strlen(attr
->name
) + 1;
813 if (buffer
&& bufptr
< bufend
)
814 bufptr
+= _ippAttrString(attr
, bufptr
, bufend
- bufptr
+ 1);
816 bufptr
+= _ippAttrString(attr
, temp
, sizeof(temp
));
819 if (buffer
&& bufptr
< bufend
)
823 return (bufptr
- buffer
);
828 * End of "$Id: ipp-support.c 9371 2010-11-17 06:21:32Z mike $".