2 * "$Id: ipp-support.c 7847 2008-08-19 04:22:14Z mike $"
4 * Internet Printing Protocol support functions for CUPS.
6 * Copyright 2007-2010 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 static char * const ipp_std_ops
[] =
106 "Get-Job-Attributes",
108 "Get-Printer-Attributes",
114 /* 0x0010 - 0x001f */
118 "Set-Printer-Attributes",
119 "Set-Job-Attributes",
120 "Get-Printer-Supported-Values",
121 "Create-Printer-Subscription",
122 "Create-Job-Subscription",
123 "Get-Subscription-Attributes",
125 "Renew-Subscription",
126 "Cancel-Subscription",
128 "Send-Notifications",
132 /* 0x0020 - 0x002f */
134 "Get-Printer-Support-Files",
137 "Pause-Printer-After-Current-Job",
139 "Release-Held-New-Jobs",
140 "Deactivate-Printer",
146 "Cancel-Current-Job",
147 "Suspend-Current-Job",
150 /* 0x0030 - 0x003b */
152 "Schedule-Job-After",
155 "Get-Document-Attributes",
158 "Set-Document-Attributes",
164 * const ipp_cups_ops
[] =
168 "CUPS-Add-Modify-Printer",
169 "CUPS-Delete-Printer",
171 "CUPS-Add-Modify-Class",
179 "CUPS-Authenticate-Job",
182 * const ipp_cups_ops2
[] =
186 * const ipp_tag_names
[] =
187 { /* Value/group tag names */
189 "operation-attributes-tag",
191 "job-attributes-tag", /* 0x02 */
192 "end-of-attributes-tag",
194 "printer-attributes-tag",
196 "unsupported-attributes-tag",
198 "subscription-attributes-tag",
200 "event-notification-attributes-tag",
202 "unknown-08", /* 0x08 */
203 "unknown-09", /* 0x09 */
204 "unknown-0a", /* 0x0a */
205 "unknown-0b", /* 0x0b */
206 "unknown-0c", /* 0x0c */
207 "unknown-0d", /* 0x0d */
208 "unknown-0e", /* 0x0e */
209 "unknown-0f", /* 0x0f */
210 "unsupported", /* 0x10 */
211 "default", /* 0x11 */
212 "unknown", /* 0x12 */
213 "no-value", /* 0x13 */
214 "unknown-14", /* 0x14 */
215 "not-settable", /* 0x15 */
216 "delete-attribute", /* 0x16 */
217 "admin-define", /* 0x17 */
218 "unknown-18", /* 0x18 */
219 "unknown-19", /* 0x19 */
220 "unknown-1a", /* 0x1a */
221 "unknown-1b", /* 0x1b */
222 "unknown-1c", /* 0x1c */
223 "unknown-1d", /* 0x1d */
224 "unknown-1e", /* 0x1e */
225 "unknown-1f", /* 0x1f */
226 "unknown-20", /* 0x20 */
227 "integer", /* 0x21 */
228 "boolean", /* 0x22 */
230 "unknown-24", /* 0x24 */
231 "unknown-25", /* 0x25 */
232 "unknown-26", /* 0x26 */
233 "unknown-27", /* 0x27 */
234 "unknown-28", /* 0x28 */
235 "unknown-29", /* 0x29 */
236 "unknown-2a", /* 0x2a */
237 "unknown-2b", /* 0x2b */
238 "unknown-2c", /* 0x2c */
239 "unknown-2d", /* 0x2d */
240 "unknown-2e", /* 0x2e */
241 "unknown-2f", /* 0x2f */
242 "octetString", /* 0x30 */
243 "dateTime", /* 0x31 */
244 "resolution", /* 0x32 */
245 "rangeOfInteger", /* 0x33 */
246 "collection", /* 0x34 */
247 "textWithLanguage", /* 0x35 */
248 "nameWithLanguage", /* 0x36 */
249 "endCollection", /* 0x37 */
250 "unknown-38", /* 0x38 */
251 "unknown-39", /* 0x39 */
252 "unknown-3a", /* 0x3a */
253 "unknown-3b", /* 0x3b */
254 "unknown-3c", /* 0x3c */
255 "unknown-3d", /* 0x3d */
256 "unknown-3e", /* 0x3e */
257 "unknown-3f", /* 0x3f */
258 "unknown-40", /* 0x40 */
259 "textWithoutLanguage",/* 0x41 */
260 "nameWithoutLanguage",/* 0x42 */
261 "unknown-43", /* 0x43 */
262 "keyword", /* 0x44 */
264 "uriScheme", /* 0x46 */
265 "charset", /* 0x47 */
266 "naturalLanguage", /* 0x48 */
267 "mimeMediaType", /* 0x49 */
268 "memberAttrName" /* 0x4a */
270 static const char * const job_states
[] =
271 { /* job-state enums */
275 "processing-stopped",
280 static const char * const printer_states
[] =
281 { /* printer-state enums */
292 static size_t ipp_col_string(ipp_t
*col
, char *buffer
, size_t bufsize
);
296 * '_ippAttrString()' - Convert the attribute's value to a string.
298 * Returns the number of bytes that would be written, not including the
299 * trailing nul. The buffer pointer can be NULL to get the required length,
300 * just like (v)snprintf.
303 size_t /* O - Number of bytes less nul */
304 _ippAttrString(ipp_attribute_t
*attr
, /* I - Attribute */
305 char *buffer
, /* I - String buffer or NULL */
306 size_t bufsize
) /* I - Size of string buffer */
308 int i
; /* Looping var */
309 char *bufptr
, /* Pointer into buffer */
310 *bufend
, /* End of buffer */
311 temp
[256]; /* Temporary string */
312 const char *ptr
; /* Pointer into string */
313 ipp_value_t
*val
; /* Current value */
316 if (!attr
|| !attr
->name
)
326 bufend
= buffer
+ bufsize
- 1;
330 for (i
= attr
->num_values
, val
= attr
->values
; i
> 0; i
--, val
++)
332 if (val
> attr
->values
)
340 switch (attr
->value_tag
)
343 if (!strcmp(attr
->name
, "printer-state") &&
344 val
->integer
>= IPP_PRINTER_IDLE
&&
345 val
->integer
<= IPP_PRINTER_STOPPED
)
347 ptr
= printer_states
[val
->integer
- IPP_PRINTER_IDLE
];
350 strlcpy(bufptr
, ptr
, bufend
- bufptr
+ 1);
352 bufptr
+= strlen(ptr
);
355 else if (!strcmp(attr
->name
, "job-state") &&
356 val
->integer
>= IPP_JOB_PENDING
&&
357 val
->integer
<= IPP_JOB_COMPLETED
)
359 ptr
= job_states
[val
->integer
- IPP_JOB_PENDING
];
362 strlcpy(bufptr
, ptr
, bufend
- bufptr
+ 1);
364 bufptr
+= strlen(ptr
);
368 case IPP_TAG_INTEGER
:
370 bufptr
+= snprintf(bufptr
, bufend
- bufptr
+ 1, "%d", val
->integer
);
372 bufptr
+= snprintf(temp
, sizeof(temp
), "%d", val
->integer
);
375 case IPP_TAG_BOOLEAN
:
377 strlcpy(bufptr
, val
->boolean
? "true" : "false",
378 bufend
- bufptr
+ 1);
380 bufptr
+= val
->boolean
? 4 : 5;
385 bufptr
+= snprintf(bufptr
, bufend
- bufptr
+ 1, "%d-%d",
386 val
->range
.lower
, val
->range
.upper
);
388 bufptr
+= snprintf(temp
, sizeof(temp
), "%d-%d", val
->range
.lower
,
392 case IPP_TAG_RESOLUTION
:
394 bufptr
+= snprintf(bufptr
, bufend
- bufptr
+ 1, "%dx%d%s",
395 val
->resolution
.xres
, val
->resolution
.yres
,
396 val
->resolution
.units
== IPP_RES_PER_INCH
?
399 bufptr
+= snprintf(temp
, sizeof(temp
), "%dx%d%s",
400 val
->resolution
.xres
, val
->resolution
.yres
,
401 val
->resolution
.units
== IPP_RES_PER_INCH
?
407 unsigned year
; /* Year */
409 year
= (val
->date
[0] << 8) + val
->date
[1];
411 if (val
->date
[9] == 0 && val
->date
[10] == 0)
412 snprintf(temp
, sizeof(temp
), "%04u-%02u-%02uT%02u:%02u:%02uZ",
413 year
, val
->date
[2], val
->date
[3], val
->date
[4],
414 val
->date
[5], val
->date
[6]);
416 snprintf(temp
, sizeof(temp
),
417 "%04u-%02u-%02uT%02u:%02u:%02u%c%02u%02u",
418 year
, val
->date
[2], val
->date
[3], val
->date
[4],
419 val
->date
[5], val
->date
[6], val
->date
[8], val
->date
[9],
423 strlcpy(bufptr
, temp
, bufend
- bufptr
+ 1);
425 bufptr
+= strlen(temp
);
431 case IPP_TAG_KEYWORD
:
432 case IPP_TAG_CHARSET
:
434 case IPP_TAG_MIMETYPE
:
435 case IPP_TAG_LANGUAGE
:
436 case IPP_TAG_TEXTLANG
:
437 case IPP_TAG_NAMELANG
:
438 for (ptr
= val
->string
.text
; *ptr
; ptr
++)
440 if (*ptr
== '\\' || *ptr
== '\"')
453 case IPP_TAG_BEGIN_COLLECTION
:
455 bufptr
+= ipp_col_string(val
->collection
, bufptr
,
456 bufend
- bufptr
+ 1);
458 bufptr
+= ipp_col_string(val
->collection
, NULL
, 0);
461 case IPP_TAG_STRING
:
462 for (ptr
= val
->string
.text
; *ptr
; ptr
++)
464 if (*ptr
== '\\' || isspace(*ptr
& 255))
474 else if (!isprint(*ptr
& 255))
477 bufptr
+= snprintf(bufptr
, bufend
- bufptr
+ 1, "\\%03o",
480 bufptr
+= snprintf(temp
, sizeof(temp
), "\\%03o",
493 ptr
= ippTagString(attr
->value_tag
);
495 strlcpy(bufptr
, ptr
, bufend
- bufptr
+ 1);
496 bufptr
+= strlen(ptr
);
506 return (bufptr
- buffer
);
511 * 'ippErrorString()' - Return a name for the given status code.
514 const char * /* O - Text string */
515 ippErrorString(ipp_status_t error
) /* I - Error status */
517 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals */
521 * See if the error code is a known value...
524 if (error
>= IPP_OK
&& error
<= IPP_OK_EVENTS_COMPLETE
)
525 return (ipp_status_oks
[error
]);
526 else if (error
== IPP_REDIRECTION_OTHER_SITE
)
527 return ("redirection-other-site");
528 else if (error
== CUPS_SEE_OTHER
)
529 return ("cups-see-other");
530 else if (error
>= IPP_BAD_REQUEST
&& error
<= IPP_PRINT_SUPPORT_FILE_NOT_FOUND
)
531 return (ipp_status_400s
[error
- IPP_BAD_REQUEST
]);
532 else if (error
>= IPP_INTERNAL_ERROR
&& error
<= IPP_PRINTER_IS_DEACTIVATED
)
533 return (ipp_status_500s
[error
- IPP_INTERNAL_ERROR
]);
536 * No, build an "unknown-xxxx" error string...
539 sprintf(cg
->ipp_unknown
, "unknown-%04x", error
);
541 return (cg
->ipp_unknown
);
546 * 'ippErrorValue()' - Return a status code for the given name.
548 * @since CUPS 1.2/Mac OS X 10.5@
551 ipp_status_t
/* O - IPP status code */
552 ippErrorValue(const char *name
) /* I - Name */
557 for (i
= 0; i
< (sizeof(ipp_status_oks
) / sizeof(ipp_status_oks
[0])); i
++)
558 if (!strcasecmp(name
, ipp_status_oks
[i
]))
559 return ((ipp_status_t
)i
);
561 if (!strcasecmp(name
, "redirection-other-site"))
562 return (IPP_REDIRECTION_OTHER_SITE
);
564 if (!strcasecmp(name
, "cups-see-other"))
565 return (CUPS_SEE_OTHER
);
567 for (i
= 0; i
< (sizeof(ipp_status_400s
) / sizeof(ipp_status_400s
[0])); i
++)
568 if (!strcasecmp(name
, ipp_status_400s
[i
]))
569 return ((ipp_status_t
)(i
+ 0x400));
571 for (i
= 0; i
< (sizeof(ipp_status_500s
) / sizeof(ipp_status_500s
[0])); i
++)
572 if (!strcasecmp(name
, ipp_status_500s
[i
]))
573 return ((ipp_status_t
)(i
+ 0x500));
575 return ((ipp_status_t
)-1);
580 * 'ippOpString()' - Return a name for the given operation id.
582 * @since CUPS 1.2/Mac OS X 10.5@
585 const char * /* O - Name */
586 ippOpString(ipp_op_t op
) /* I - Operation ID */
588 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals */
592 * See if the operation ID is a known value...
595 if (op
>= IPP_PRINT_JOB
&& op
<= IPP_CLOSE_JOB
)
596 return (ipp_std_ops
[op
]);
597 else if (op
== IPP_PRIVATE
)
598 return ("windows-ext");
599 else if (op
>= CUPS_GET_DEFAULT
&& op
<= CUPS_GET_PPD
)
600 return (ipp_cups_ops
[op
- CUPS_GET_DEFAULT
]);
601 else if (op
== CUPS_GET_DOCUMENT
)
602 return (ipp_cups_ops2
[0]);
605 * No, build an "unknown-xxxx" operation string...
608 sprintf(cg
->ipp_unknown
, "unknown-%04x", op
);
610 return (cg
->ipp_unknown
);
615 * 'ippOpValue()' - Return an operation id for the given name.
617 * @since CUPS 1.2/Mac OS X 10.5@
620 ipp_op_t
/* O - Operation ID */
621 ippOpValue(const char *name
) /* I - Textual name */
626 for (i
= 0; i
< (sizeof(ipp_std_ops
) / sizeof(ipp_std_ops
[0])); i
++)
627 if (!strcasecmp(name
, ipp_std_ops
[i
]))
628 return ((ipp_op_t
)i
);
630 if (!strcasecmp(name
, "windows-ext"))
631 return (IPP_PRIVATE
);
633 for (i
= 0; i
< (sizeof(ipp_cups_ops
) / sizeof(ipp_cups_ops
[0])); i
++)
634 if (!strcasecmp(name
, ipp_cups_ops
[i
]))
635 return ((ipp_op_t
)(i
+ 0x4001));
637 for (i
= 0; i
< (sizeof(ipp_cups_ops2
) / sizeof(ipp_cups_ops2
[0])); i
++)
638 if (!strcasecmp(name
, ipp_cups_ops2
[i
]))
639 return ((ipp_op_t
)(i
+ 0x4027));
641 if (!strcasecmp(name
, "CUPS-Add-Class"))
642 return (CUPS_ADD_MODIFY_CLASS
);
644 if (!strcasecmp(name
, "CUPS-Add-Printer"))
645 return (CUPS_ADD_MODIFY_PRINTER
);
647 return ((ipp_op_t
)-1);
652 * 'ippPort()' - Return the default IPP port number.
655 int /* O - Port number */
658 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals */
661 DEBUG_puts("ippPort()");
666 DEBUG_printf(("1ippPort: Returning %d...", cg
->ipp_port
));
668 return (cg
->ipp_port
);
673 * 'ippSetPort()' - Set the default port number.
677 ippSetPort(int p
) /* I - Port number to use */
679 DEBUG_printf(("ippSetPort(p=%d)", p
));
681 _cupsGlobals()->ipp_port
= p
;
686 * 'ippTagString()' - Return the tag name corresponding to a tag value.
688 * The returned names are defined in RFC 2911 and 3382.
690 * @since CUPS 1.4/Mac OS X 10.6@
693 const char * /* O - Tag name */
694 ippTagString(ipp_tag_t tag
) /* I - Tag value */
698 if (tag
< (ipp_tag_t
)(sizeof(ipp_tag_names
) / sizeof(ipp_tag_names
[0])))
699 return (ipp_tag_names
[tag
]);
706 * 'ippTagValue()' - Return the tag value corresponding to a tag name.
708 * The tag names are defined in RFC 2911 and 3382.
710 * @since CUPS 1.4/Mac OS X 10.6@
713 ipp_tag_t
/* O - Tag value */
714 ippTagValue(const char *name
) /* I - Tag name */
716 int i
; /* Looping var */
719 for (i
= 0; i
< (sizeof(ipp_tag_names
) / sizeof(ipp_tag_names
[0])); i
++)
720 if (!strcasecmp(name
, ipp_tag_names
[i
]))
721 return ((ipp_tag_t
)i
);
723 if (!strcasecmp(name
, "operation"))
724 return (IPP_TAG_OPERATION
);
725 else if (!strcasecmp(name
, "job"))
726 return (IPP_TAG_JOB
);
727 else if (!strcasecmp(name
, "printer"))
728 return (IPP_TAG_PRINTER
);
729 else if (!strcasecmp(name
, "unsupported"))
730 return (IPP_TAG_UNSUPPORTED_GROUP
);
731 else if (!strcasecmp(name
, "subscription"))
732 return (IPP_TAG_SUBSCRIPTION
);
733 else if (!strcasecmp(name
, "event"))
734 return (IPP_TAG_EVENT_NOTIFICATION
);
735 else if (!strcasecmp(name
, "language"))
736 return (IPP_TAG_LANGUAGE
);
737 else if (!strcasecmp(name
, "mimetype"))
738 return (IPP_TAG_MIMETYPE
);
739 else if (!strcasecmp(name
, "name"))
740 return (IPP_TAG_NAME
);
741 else if (!strcasecmp(name
, "text"))
742 return (IPP_TAG_TEXT
);
743 else if (!strcasecmp(name
, "begCollection"))
744 return (IPP_TAG_BEGIN_COLLECTION
);
746 return (IPP_TAG_ZERO
);
751 * End of "$Id: ipp-support.c 7847 2008-08-19 04:22:14Z mike $".