2 * "$Id: encode.c 6649 2007-07-11 21:46:42Z mike $"
4 * Option encoding routines for the Common UNIX Printing System (CUPS).
6 * Copyright 2007 by Apple Inc.
7 * Copyright 1997-2007 by Easy Software Products.
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 * cupsEncodeOptions() - Encode printer options into IPP attributes.
20 * cupsEncodeOptions2() - Encode printer options into IPP attributes for
22 * _ippFindOption() - Find the attribute information for an option.
23 * compare_ipp_options() - Compare two IPP options.
27 * Include necessary headers...
31 #include "ipp-private.h"
39 * Local list of option names and the value tags they should use...
41 * **** THIS LIST MUST BE SORTED ****
44 static const _ipp_option_t ipp_options
[] =
46 { "auth-info", IPP_TAG_TEXT
, IPP_TAG_JOB
},
47 { "auth-info-required", IPP_TAG_KEYWORD
, IPP_TAG_PRINTER
},
48 { "blackplot", IPP_TAG_BOOLEAN
, IPP_TAG_JOB
},
49 { "blackplot-default", IPP_TAG_BOOLEAN
, IPP_TAG_PRINTER
},
50 { "brightness", IPP_TAG_INTEGER
, IPP_TAG_JOB
},
51 { "brightness-default", IPP_TAG_INTEGER
, IPP_TAG_PRINTER
},
52 { "columns", IPP_TAG_INTEGER
, IPP_TAG_JOB
},
53 { "columns-default", IPP_TAG_INTEGER
, IPP_TAG_PRINTER
},
54 { "copies", IPP_TAG_INTEGER
, IPP_TAG_JOB
},
55 { "copies-default", IPP_TAG_INTEGER
, IPP_TAG_PRINTER
},
56 { "document-format", IPP_TAG_MIMETYPE
, IPP_TAG_OPERATION
},
57 { "document-format-default", IPP_TAG_MIMETYPE
, IPP_TAG_PRINTER
},
58 { "finishings", IPP_TAG_ENUM
, IPP_TAG_JOB
},
59 { "finishings-default", IPP_TAG_ENUM
, IPP_TAG_PRINTER
},
60 { "fitplot", IPP_TAG_BOOLEAN
, IPP_TAG_JOB
},
61 { "fitplot-default", IPP_TAG_BOOLEAN
, IPP_TAG_PRINTER
},
62 { "gamma", IPP_TAG_INTEGER
, IPP_TAG_JOB
},
63 { "gamma-default", IPP_TAG_INTEGER
, IPP_TAG_PRINTER
},
64 { "hue", IPP_TAG_INTEGER
, IPP_TAG_JOB
},
65 { "hue-default", IPP_TAG_INTEGER
, IPP_TAG_PRINTER
},
66 { "job-k-limit", IPP_TAG_INTEGER
, IPP_TAG_JOB
},
67 { "job-page-limit", IPP_TAG_INTEGER
, IPP_TAG_JOB
},
68 { "job-priority", IPP_TAG_INTEGER
, IPP_TAG_JOB
},
69 { "job-quota-period", IPP_TAG_INTEGER
, IPP_TAG_JOB
},
70 { "job-uuid", IPP_TAG_URI
, IPP_TAG_JOB
},
71 { "landscape", IPP_TAG_BOOLEAN
, IPP_TAG_JOB
},
72 { "media", IPP_TAG_KEYWORD
, IPP_TAG_JOB
},
73 { "mirror", IPP_TAG_BOOLEAN
, IPP_TAG_JOB
},
74 { "mirror-default", IPP_TAG_BOOLEAN
, IPP_TAG_PRINTER
},
75 { "natural-scaling", IPP_TAG_INTEGER
, IPP_TAG_JOB
},
76 { "natural-scaling-default", IPP_TAG_INTEGER
, IPP_TAG_PRINTER
},
77 { "notify-charset", IPP_TAG_CHARSET
, IPP_TAG_SUBSCRIPTION
},
78 { "notify-events", IPP_TAG_KEYWORD
, IPP_TAG_SUBSCRIPTION
},
79 { "notify-events-default", IPP_TAG_KEYWORD
, IPP_TAG_PRINTER
},
80 { "notify-lease-duration", IPP_TAG_INTEGER
, IPP_TAG_SUBSCRIPTION
},
81 { "notify-lease-duration-default", IPP_TAG_INTEGER
, IPP_TAG_PRINTER
},
82 { "notify-natural-language", IPP_TAG_LANGUAGE
, IPP_TAG_SUBSCRIPTION
},
83 { "notify-pull-method", IPP_TAG_KEYWORD
, IPP_TAG_SUBSCRIPTION
},
84 { "notify-recipient-uri", IPP_TAG_URI
, IPP_TAG_SUBSCRIPTION
},
85 { "notify-time-interval", IPP_TAG_INTEGER
, IPP_TAG_SUBSCRIPTION
},
86 { "notify-user-data", IPP_TAG_STRING
, IPP_TAG_SUBSCRIPTION
},
87 { "number-up", IPP_TAG_INTEGER
, IPP_TAG_JOB
},
88 { "number-up-default", IPP_TAG_INTEGER
, IPP_TAG_PRINTER
},
89 { "orientation-requested", IPP_TAG_ENUM
, IPP_TAG_JOB
},
90 { "orientation-requested-default", IPP_TAG_ENUM
, IPP_TAG_PRINTER
},
91 { "page-bottom", IPP_TAG_INTEGER
, IPP_TAG_JOB
},
92 { "page-bottom-default", IPP_TAG_INTEGER
, IPP_TAG_PRINTER
},
93 { "page-left", IPP_TAG_INTEGER
, IPP_TAG_JOB
},
94 { "page-left-default", IPP_TAG_INTEGER
, IPP_TAG_PRINTER
},
95 { "page-ranges", IPP_TAG_RANGE
, IPP_TAG_JOB
},
96 { "page-ranges-default", IPP_TAG_RANGE
, IPP_TAG_PRINTER
},
97 { "page-right", IPP_TAG_INTEGER
, IPP_TAG_JOB
},
98 { "page-right-default", IPP_TAG_INTEGER
, IPP_TAG_PRINTER
},
99 { "page-top", IPP_TAG_INTEGER
, IPP_TAG_JOB
},
100 { "page-top-default", IPP_TAG_INTEGER
, IPP_TAG_PRINTER
},
101 { "penwidth", IPP_TAG_INTEGER
, IPP_TAG_JOB
},
102 { "penwidth-default", IPP_TAG_INTEGER
, IPP_TAG_PRINTER
},
103 { "ppi", IPP_TAG_INTEGER
, IPP_TAG_JOB
},
104 { "ppi-default", IPP_TAG_INTEGER
, IPP_TAG_PRINTER
},
105 { "prettyprint", IPP_TAG_BOOLEAN
, IPP_TAG_JOB
},
106 { "prettyprint-default", IPP_TAG_BOOLEAN
, IPP_TAG_PRINTER
},
107 { "print-quality", IPP_TAG_ENUM
, IPP_TAG_JOB
},
108 { "print-quality-default", IPP_TAG_ENUM
, IPP_TAG_PRINTER
},
109 { "printer-error-policy", IPP_TAG_NAME
, IPP_TAG_PRINTER
},
110 { "printer-info", IPP_TAG_TEXT
, IPP_TAG_PRINTER
},
111 { "printer-is-accepting-jobs",IPP_TAG_BOOLEAN
, IPP_TAG_PRINTER
},
112 { "printer-is-shared", IPP_TAG_BOOLEAN
, IPP_TAG_PRINTER
},
113 { "printer-location", IPP_TAG_TEXT
, IPP_TAG_PRINTER
},
114 { "printer-make-and-model", IPP_TAG_TEXT
, IPP_TAG_PRINTER
},
115 { "printer-more-info", IPP_TAG_URI
, IPP_TAG_PRINTER
},
116 { "printer-op-policy", IPP_TAG_NAME
, IPP_TAG_PRINTER
},
117 { "printer-resolution", IPP_TAG_RESOLUTION
, IPP_TAG_JOB
},
118 { "printer-state", IPP_TAG_ENUM
, IPP_TAG_PRINTER
},
119 { "printer-state-change-time",IPP_TAG_INTEGER
, IPP_TAG_PRINTER
},
120 { "printer-state-reasons", IPP_TAG_KEYWORD
, IPP_TAG_PRINTER
},
121 { "printer-type", IPP_TAG_ENUM
, IPP_TAG_PRINTER
},
122 { "printer-uri", IPP_TAG_URI
, IPP_TAG_OPERATION
},
123 { "queued-job-count", IPP_TAG_INTEGER
, IPP_TAG_PRINTER
},
124 { "raw", IPP_TAG_MIMETYPE
, IPP_TAG_OPERATION
},
125 { "requesting-user-name-allowed", IPP_TAG_NAME
, IPP_TAG_PRINTER
},
126 { "requesting-user-name-denied", IPP_TAG_NAME
, IPP_TAG_PRINTER
},
127 { "resolution", IPP_TAG_RESOLUTION
, IPP_TAG_JOB
},
128 { "resolution-default", IPP_TAG_RESOLUTION
, IPP_TAG_PRINTER
},
129 { "saturation", IPP_TAG_INTEGER
, IPP_TAG_JOB
},
130 { "saturation-default", IPP_TAG_INTEGER
, IPP_TAG_PRINTER
},
131 { "scaling", IPP_TAG_INTEGER
, IPP_TAG_JOB
},
132 { "scaling-default", IPP_TAG_INTEGER
, IPP_TAG_PRINTER
},
133 { "sides", IPP_TAG_KEYWORD
, IPP_TAG_JOB
},
134 { "sides-default", IPP_TAG_KEYWORD
, IPP_TAG_PRINTER
},
135 { "wrap", IPP_TAG_BOOLEAN
, IPP_TAG_JOB
},
136 { "wrap-default", IPP_TAG_BOOLEAN
, IPP_TAG_PRINTER
}
144 static int compare_ipp_options(_ipp_option_t
*a
, _ipp_option_t
*b
);
148 * 'cupsEncodeOptions()' - Encode printer options into IPP attributes.
150 * This function adds operation, job, and then subscription attributes,
151 * in that order. Use the cupsEncodeOptions2() function to add attributes
152 * for a single group.
156 cupsEncodeOptions(ipp_t
*ipp
, /* I - Request to add to */
157 int num_options
, /* I - Number of options */
158 cups_option_t
*options
) /* I - Options */
160 DEBUG_printf(("cupsEncodeOptions(%p, %d, %p)\n", ipp
, num_options
, options
));
163 * Add the options in the proper groups & order...
166 cupsEncodeOptions2(ipp
, num_options
, options
, IPP_TAG_OPERATION
);
167 cupsEncodeOptions2(ipp
, num_options
, options
, IPP_TAG_JOB
);
168 cupsEncodeOptions2(ipp
, num_options
, options
, IPP_TAG_SUBSCRIPTION
);
173 * 'cupsEncodeOptions2()' - Encode printer options into IPP attributes for a group.
175 * This function only adds attributes for a single group. Call this
176 * function multiple times for each group, or use cupsEncodeOptions()
177 * to add the standard groups.
184 ipp_t
*ipp
, /* I - Request to add to */
185 int num_options
, /* I - Number of options */
186 cups_option_t
*options
, /* I - Options */
187 ipp_tag_t group_tag
) /* I - Group to encode */
189 int i
, j
; /* Looping vars */
190 int count
; /* Number of values */
191 char *s
, /* Pointer into option value */
192 *val
, /* Pointer to option value */
193 *copy
, /* Copy of option value */
194 *sep
; /* Option separator */
195 ipp_attribute_t
*attr
; /* IPP attribute */
196 ipp_tag_t value_tag
; /* IPP value tag */
197 cups_option_t
*option
; /* Current option */
200 DEBUG_printf(("cupsEncodeOptions2(ipp=%p, num_options=%d, options=%p, "
201 "group_tag=%x)\n", ipp
, num_options
, options
, group_tag
));
204 * Range check input...
207 if (!ipp
|| num_options
< 1 || !options
)
211 * Do special handling for the document-format/raw options...
214 if (group_tag
== IPP_TAG_OPERATION
)
217 * Handle the document format stuff first...
220 if ((val
= (char *)cupsGetOption("document-format", num_options
, options
)) != NULL
)
221 ippAddString(ipp
, IPP_TAG_OPERATION
, IPP_TAG_MIMETYPE
, "document-format",
223 else if (cupsGetOption("raw", num_options
, options
))
224 ippAddString(ipp
, IPP_TAG_OPERATION
, IPP_TAG_MIMETYPE
, "document-format",
225 NULL
, "application/vnd.cups-raw");
227 ippAddString(ipp
, IPP_TAG_OPERATION
, IPP_TAG_MIMETYPE
, "document-format",
228 NULL
, "application/octet-stream");
232 * Then loop through the options...
235 for (i
= num_options
, option
= options
; i
> 0; i
--, option
++)
237 _ipp_option_t
*match
; /* Matching attribute */
241 * Skip document format options that are handled above...
244 if (!strcasecmp(option
->name
, "raw") ||
245 !strcasecmp(option
->name
, "document-format") ||
250 * Figure out the proper value and group tags for this option...
253 if ((match
= _ippFindOption(option
->name
)) != NULL
)
255 if (match
->group_tag
!= group_tag
)
258 value_tag
= match
->value_tag
;
262 int namelen
; /* Length of name */
265 namelen
= (int)strlen(option
->name
);
267 if (namelen
< 9 || strcmp(option
->name
+ namelen
- 8, "-default"))
269 if (group_tag
!= IPP_TAG_JOB
)
272 else if (group_tag
!= IPP_TAG_PRINTER
)
275 if (!strcasecmp(option
->value
, "true") ||
276 !strcasecmp(option
->value
, "false"))
277 value_tag
= IPP_TAG_BOOLEAN
;
279 value_tag
= IPP_TAG_NAME
;
283 * Count the number of values...
286 for (count
= 1, sep
= option
->value
; *sep
; sep
++)
291 * Skip quoted option value...
296 while (*sep
&& *sep
!= '\'')
302 else if (*sep
== '\"')
305 * Skip quoted option value...
310 while (*sep
&& *sep
!= '\"')
316 else if (*sep
== ',')
318 else if (*sep
== '\\' && sep
[1])
322 DEBUG_printf(("cupsEncodeOptions2: option = \'%s\', count = %d\n",
323 option
->name
, count
));
326 * Allocate memory for the attribute values...
329 if ((attr
= _ippAddAttr(ipp
, count
)) == NULL
)
335 DEBUG_puts("cupsEncodeOptions2: Ran out of memory for attributes!");
340 * Now figure out what type of value we have...
343 attr
->group_tag
= group_tag
;
344 attr
->value_tag
= value_tag
;
347 * Copy the name over...
350 if ((attr
->name
= _cupsStrAlloc(option
->name
)) == NULL
)
356 DEBUG_puts("cupsEncodeOptions2: Ran out of memory for name!");
363 * Make a copy of the value we can fiddle with...
366 if ((copy
= strdup(option
->value
)) == NULL
)
372 DEBUG_puts("cupsEncodeOptions2: Ran out of memory for value copy!");
381 * Since we have a single value, use the value directly...
389 * Scan the value string for values...
392 for (j
= 0; j
< count
; val
= sep
, j
++)
395 * Find the end of this value and mark it if needed...
398 if ((sep
= strchr(val
, ',')) != NULL
)
401 sep
= val
+ strlen(val
);
404 * Copy the option value(s) over as needed by the type...
407 switch (attr
->value_tag
)
409 case IPP_TAG_INTEGER
:
412 * Integer/enumeration value...
415 attr
->values
[j
].integer
= strtol(val
, &s
, 0);
417 DEBUG_printf(("cupsEncodeOptions2: Added integer option value %d...\n",
418 attr
->values
[j
].integer
));
421 case IPP_TAG_BOOLEAN
:
422 if (!strcasecmp(val
, "true") ||
423 !strcasecmp(val
, "on") ||
424 !strcasecmp(val
, "yes"))
427 * Boolean value - true...
430 attr
->values
[j
].boolean
= 1;
432 DEBUG_puts("cupsEncodeOptions2: Added boolean true value...");
437 * Boolean value - false...
440 attr
->values
[j
].boolean
= 0;
442 DEBUG_puts("cupsEncodeOptions2: Added boolean false value...");
453 attr
->values
[j
].range
.lower
= 1;
457 attr
->values
[j
].range
.lower
= strtol(val
, &s
, 0);
462 attr
->values
[j
].range
.upper
= strtol(s
+ 1, NULL
, 0);
464 attr
->values
[j
].range
.upper
= 2147483647;
467 attr
->values
[j
].range
.upper
= attr
->values
[j
].range
.lower
;
469 DEBUG_printf(("cupsEncodeOptions2: Added range option value %d-%d...\n",
470 attr
->values
[j
].range
.lower
,
471 attr
->values
[j
].range
.upper
));
474 case IPP_TAG_RESOLUTION
:
479 attr
->values
[j
].resolution
.xres
= strtol(val
, &s
, 0);
482 attr
->values
[j
].resolution
.yres
= strtol(s
+ 1, &s
, 0);
484 attr
->values
[j
].resolution
.yres
= attr
->values
[j
].resolution
.xres
;
486 if (!strcasecmp(s
, "dpc"))
487 attr
->values
[j
].resolution
.units
= IPP_RES_PER_CM
;
489 attr
->values
[j
].resolution
.units
= IPP_RES_PER_INCH
;
491 DEBUG_printf(("cupsEncodeOptions2: Added resolution option value %s...\n",
495 case IPP_TAG_STRING
:
500 attr
->values
[j
].unknown
.length
= (int)strlen(val
);
501 attr
->values
[j
].unknown
.data
= _cupsStrAlloc(val
);
503 DEBUG_printf(("cupsEncodeOptions2: Added octet-string value \"%s\"...\n",
504 attr
->values
[j
].unknown
.data
));
508 if ((attr
->values
[j
].string
.text
= _cupsStrAlloc(val
)) == NULL
)
514 DEBUG_puts("cupsEncodeOptions2: Ran out of memory for string!");
518 DEBUG_printf(("cupsEncodeOptions2: Added string value \"%s\"...\n",
531 * '_ippFindOption()' - Find the attribute information for an option.
534 _ipp_option_t
* /* O - Attribute information */
535 _ippFindOption(const char *name
) /* I - Option/attribute name */
537 _ipp_option_t key
; /* Search key */
541 * Lookup the proper value and group tags for this option...
546 return ((_ipp_option_t
*)bsearch(&key
, ipp_options
,
547 sizeof(ipp_options
) / sizeof(ipp_options
[0]),
548 sizeof(ipp_options
[0]),
549 (int (*)(const void *, const void *))
550 compare_ipp_options
));
555 * 'compare_ipp_options()' - Compare two IPP options.
558 static int /* O - Result of comparison */
559 compare_ipp_options(_ipp_option_t
*a
, /* I - First option */
560 _ipp_option_t
*b
) /* I - Second option */
562 return (strcmp(a
->name
, b
->name
));
567 * End of "$Id: encode.c 6649 2007-07-11 21:46:42Z mike $".