]> git.ipfire.org Git - thirdparty/cups.git/blob - cups/encode.c
Support many new attributes and fix some IPP conformance issues (STR #4630)
[thirdparty/cups.git] / cups / encode.c
1 /*
2 * "$Id$"
3 *
4 * Option encoding routines for CUPS.
5 *
6 * Copyright 2007-2015 by Apple Inc.
7 * Copyright 1997-2007 by Easy Software Products.
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
18 /*
19 * Include necessary headers...
20 */
21
22 #include "cups-private.h"
23
24
25 /*
26 * Local list of option names, the value tags they should use, and the list of
27 * supported operations...
28 *
29 * **** THIS LIST MUST BE SORTED BY ATTRIBUTE NAME ****
30 */
31
32 static const ipp_op_t ipp_job_creation[] =
33 {
34 IPP_OP_PRINT_JOB,
35 IPP_OP_PRINT_URI,
36 IPP_OP_VALIDATE_JOB,
37 IPP_OP_CREATE_JOB,
38 IPP_OP_HOLD_JOB,
39 IPP_OP_SET_JOB_ATTRIBUTES,
40 IPP_OP_CUPS_NONE
41 };
42
43 static const ipp_op_t ipp_doc_creation[] =
44 {
45 IPP_OP_PRINT_JOB,
46 IPP_OP_PRINT_URI,
47 IPP_OP_SEND_DOCUMENT,
48 IPP_OP_SEND_URI,
49 IPP_OP_SET_JOB_ATTRIBUTES,
50 IPP_OP_SET_DOCUMENT_ATTRIBUTES,
51 IPP_OP_CUPS_NONE
52 };
53
54 static const ipp_op_t ipp_sub_creation[] =
55 {
56 IPP_OP_PRINT_JOB,
57 IPP_OP_PRINT_URI,
58 IPP_OP_CREATE_JOB,
59 IPP_OP_CREATE_PRINTER_SUBSCRIPTIONS,
60 IPP_OP_CREATE_JOB_SUBSCRIPTIONS,
61 IPP_OP_CUPS_NONE
62 };
63
64 static const ipp_op_t ipp_all_print[] =
65 {
66 IPP_OP_PRINT_JOB,
67 IPP_OP_PRINT_URI,
68 IPP_OP_VALIDATE_JOB,
69 IPP_OP_CREATE_JOB,
70 IPP_OP_SEND_DOCUMENT,
71 IPP_OP_SEND_URI,
72 IPP_OP_CUPS_NONE
73 };
74
75 static const ipp_op_t ipp_set_printer[] =
76 {
77 IPP_OP_SET_PRINTER_ATTRIBUTES,
78 IPP_OP_CUPS_ADD_MODIFY_PRINTER,
79 IPP_OP_CUPS_ADD_MODIFY_CLASS,
80 IPP_OP_CUPS_NONE
81 };
82
83 static const ipp_op_t cups_schemes[] =
84 {
85 IPP_OP_CUPS_GET_DEVICES,
86 IPP_OP_CUPS_GET_PPDS,
87 IPP_OP_CUPS_NONE
88 };
89
90 static const ipp_op_t cups_get_ppds[] =
91 {
92 IPP_OP_CUPS_GET_PPDS,
93 IPP_OP_CUPS_NONE
94 };
95
96 static const ipp_op_t cups_ppd_name[] =
97 {
98 IPP_OP_CUPS_ADD_MODIFY_PRINTER,
99 IPP_OP_CUPS_GET_PPD,
100 IPP_OP_CUPS_NONE
101 };
102
103 static const _ipp_option_t ipp_options[] =
104 {
105 { 1, "auth-info", IPP_TAG_TEXT, IPP_TAG_JOB },
106 { 1, "auth-info-default", IPP_TAG_TEXT, IPP_TAG_PRINTER },
107 { 1, "auth-info-required", IPP_TAG_KEYWORD, IPP_TAG_PRINTER },
108 { 0, "blackplot", IPP_TAG_BOOLEAN, IPP_TAG_JOB },
109 { 0, "blackplot-default", IPP_TAG_BOOLEAN, IPP_TAG_PRINTER },
110 { 0, "brightness", IPP_TAG_INTEGER, IPP_TAG_JOB },
111 { 0, "brightness-default", IPP_TAG_INTEGER, IPP_TAG_PRINTER },
112 { 0, "columns", IPP_TAG_INTEGER, IPP_TAG_JOB },
113 { 0, "columns-default", IPP_TAG_INTEGER, IPP_TAG_PRINTER },
114 { 0, "compression", IPP_TAG_KEYWORD, IPP_TAG_OPERATION,
115 IPP_TAG_ZERO,
116 ipp_doc_creation },
117 { 0, "copies", IPP_TAG_INTEGER, IPP_TAG_JOB,
118 IPP_TAG_DOCUMENT },
119 { 0, "copies-default", IPP_TAG_INTEGER, IPP_TAG_PRINTER },
120 { 0, "device-uri", IPP_TAG_URI, IPP_TAG_PRINTER },
121 { 1, "document-copies", IPP_TAG_RANGE, IPP_TAG_JOB,
122 IPP_TAG_DOCUMENT,
123 ipp_doc_creation },
124 { 0, "document-format", IPP_TAG_MIMETYPE, IPP_TAG_OPERATION,
125 IPP_TAG_ZERO,
126 ipp_doc_creation },
127 { 0, "document-format-default", IPP_TAG_MIMETYPE, IPP_TAG_PRINTER },
128 { 1, "document-numbers", IPP_TAG_RANGE, IPP_TAG_JOB,
129 IPP_TAG_DOCUMENT,
130 ipp_all_print },
131 { 1, "exclude-schemes", IPP_TAG_NAME, IPP_TAG_OPERATION,
132 IPP_TAG_ZERO,
133 cups_schemes },
134 { 1, "finishings", IPP_TAG_ENUM, IPP_TAG_JOB,
135 IPP_TAG_DOCUMENT },
136 { 1, "finishings-default", IPP_TAG_ENUM, IPP_TAG_PRINTER },
137 { 0, "fit-to-page", IPP_TAG_BOOLEAN, IPP_TAG_JOB,
138 IPP_TAG_DOCUMENT },
139 { 0, "fit-to-page-default", IPP_TAG_BOOLEAN, IPP_TAG_PRINTER },
140 { 0, "fitplot", IPP_TAG_BOOLEAN, IPP_TAG_JOB },
141 { 0, "fitplot-default", IPP_TAG_BOOLEAN, IPP_TAG_PRINTER },
142 { 0, "gamma", IPP_TAG_INTEGER, IPP_TAG_JOB },
143 { 0, "gamma-default", IPP_TAG_INTEGER, IPP_TAG_PRINTER },
144 { 0, "hue", IPP_TAG_INTEGER, IPP_TAG_JOB },
145 { 0, "hue-default", IPP_TAG_INTEGER, IPP_TAG_PRINTER },
146 { 1, "include-schemes", IPP_TAG_NAME, IPP_TAG_OPERATION,
147 IPP_TAG_ZERO,
148 cups_schemes },
149 { 0, "job-account-id", IPP_TAG_NAME, IPP_TAG_JOB },
150 { 0, "job-account-id-default",IPP_TAG_NAME, IPP_TAG_PRINTER },
151 { 0, "job-accounting-user-id", IPP_TAG_NAME, IPP_TAG_JOB },
152 { 0, "job-accounting-user-id-default", IPP_TAG_NAME, IPP_TAG_PRINTER },
153 { 0, "job-authorization-uri", IPP_TAG_URI, IPP_TAG_OPERATION },
154 { 0, "job-cancel-after", IPP_TAG_INTEGER, IPP_TAG_JOB },
155 { 0, "job-cancel-after-default", IPP_TAG_INTEGER, IPP_TAG_PRINTER },
156 { 0, "job-hold-until", IPP_TAG_KEYWORD, IPP_TAG_JOB },
157 { 0, "job-id", IPP_TAG_INTEGER, IPP_TAG_ZERO }, /* never send as option */
158 { 0, "job-impressions", IPP_TAG_INTEGER, IPP_TAG_ZERO }, /* never send as option */
159 { 0, "job-impressions-completed", IPP_TAG_INTEGER, IPP_TAG_ZERO }, /* never send as option */
160 { 0, "job-k-limit", IPP_TAG_INTEGER, IPP_TAG_PRINTER },
161 { 0, "job-k-octets", IPP_TAG_INTEGER, IPP_TAG_ZERO }, /* never send as option */
162 { 0, "job-k-octets-completed",IPP_TAG_INTEGER, IPP_TAG_ZERO }, /* never send as option */
163 { 0, "job-media-sheets", IPP_TAG_INTEGER, IPP_TAG_ZERO }, /* never send as option */
164 { 0, "job-media-sheets-completed", IPP_TAG_INTEGER, IPP_TAG_ZERO }, /* never send as option */
165 { 0, "job-page-limit", IPP_TAG_INTEGER, IPP_TAG_PRINTER },
166 { 0, "job-password", IPP_TAG_STRING, IPP_TAG_OPERATION,
167 IPP_TAG_ZERO,
168 ipp_job_creation },
169 { 0, "job-password-encryption", IPP_TAG_KEYWORD, IPP_TAG_OPERATION,
170 IPP_TAG_ZERO,
171 ipp_job_creation },
172 { 0, "job-priority", IPP_TAG_INTEGER, IPP_TAG_JOB },
173 { 0, "job-quota-period", IPP_TAG_INTEGER, IPP_TAG_PRINTER },
174 { 1, "job-sheets", IPP_TAG_NAME, IPP_TAG_JOB },
175 { 1, "job-sheets-default", IPP_TAG_NAME, IPP_TAG_PRINTER },
176 { 0, "job-state", IPP_TAG_ENUM, IPP_TAG_ZERO }, /* never send as option */
177 { 0, "job-state-message", IPP_TAG_TEXT, IPP_TAG_ZERO }, /* never send as option */
178 { 0, "job-state-reasons", IPP_TAG_KEYWORD, IPP_TAG_ZERO }, /* never send as option */
179 { 0, "job-uuid", IPP_TAG_URI, IPP_TAG_JOB },
180 { 0, "landscape", IPP_TAG_BOOLEAN, IPP_TAG_JOB },
181 { 1, "marker-change-time", IPP_TAG_INTEGER, IPP_TAG_PRINTER },
182 { 1, "marker-colors", IPP_TAG_NAME, IPP_TAG_PRINTER },
183 { 1, "marker-high-levels", IPP_TAG_INTEGER, IPP_TAG_PRINTER },
184 { 1, "marker-levels", IPP_TAG_INTEGER, IPP_TAG_PRINTER },
185 { 1, "marker-low-levels", IPP_TAG_INTEGER, IPP_TAG_PRINTER },
186 { 0, "marker-message", IPP_TAG_TEXT, IPP_TAG_PRINTER },
187 { 1, "marker-names", IPP_TAG_NAME, IPP_TAG_PRINTER },
188 { 1, "marker-types", IPP_TAG_KEYWORD, IPP_TAG_PRINTER },
189 { 1, "media", IPP_TAG_KEYWORD, IPP_TAG_JOB,
190 IPP_TAG_DOCUMENT },
191 { 0, "media-col", IPP_TAG_BEGIN_COLLECTION, IPP_TAG_JOB,
192 IPP_TAG_DOCUMENT },
193 { 0, "media-col-default", IPP_TAG_BEGIN_COLLECTION, IPP_TAG_PRINTER },
194 { 0, "media-color", IPP_TAG_KEYWORD, IPP_TAG_JOB,
195 IPP_TAG_DOCUMENT },
196 { 1, "media-default", IPP_TAG_KEYWORD, IPP_TAG_PRINTER },
197 { 0, "media-key", IPP_TAG_KEYWORD, IPP_TAG_JOB,
198 IPP_TAG_DOCUMENT },
199 { 0, "media-size", IPP_TAG_BEGIN_COLLECTION, IPP_TAG_JOB,
200 IPP_TAG_DOCUMENT },
201 { 0, "media-type", IPP_TAG_KEYWORD, IPP_TAG_JOB,
202 IPP_TAG_DOCUMENT },
203 { 0, "mirror", IPP_TAG_BOOLEAN, IPP_TAG_JOB },
204 { 0, "mirror-default", IPP_TAG_BOOLEAN, IPP_TAG_PRINTER },
205 { 0, "natural-scaling", IPP_TAG_INTEGER, IPP_TAG_JOB },
206 { 0, "natural-scaling-default", IPP_TAG_INTEGER, IPP_TAG_PRINTER },
207 { 0, "notify-charset", IPP_TAG_CHARSET, IPP_TAG_SUBSCRIPTION },
208 { 1, "notify-events", IPP_TAG_KEYWORD, IPP_TAG_SUBSCRIPTION },
209 { 1, "notify-events-default", IPP_TAG_KEYWORD, IPP_TAG_PRINTER },
210 { 0, "notify-lease-duration", IPP_TAG_INTEGER, IPP_TAG_SUBSCRIPTION },
211 { 0, "notify-lease-duration-default", IPP_TAG_INTEGER, IPP_TAG_PRINTER },
212 { 0, "notify-natural-language", IPP_TAG_LANGUAGE, IPP_TAG_SUBSCRIPTION },
213 { 0, "notify-pull-method", IPP_TAG_KEYWORD, IPP_TAG_SUBSCRIPTION },
214 { 0, "notify-recipient-uri", IPP_TAG_URI, IPP_TAG_SUBSCRIPTION },
215 { 0, "notify-time-interval", IPP_TAG_INTEGER, IPP_TAG_SUBSCRIPTION },
216 { 0, "notify-user-data", IPP_TAG_STRING, IPP_TAG_SUBSCRIPTION },
217 { 0, "number-up", IPP_TAG_INTEGER, IPP_TAG_JOB,
218 IPP_TAG_DOCUMENT },
219 { 0, "number-up-default", IPP_TAG_INTEGER, IPP_TAG_PRINTER },
220 { 0, "orientation-requested", IPP_TAG_ENUM, IPP_TAG_JOB,
221 IPP_TAG_DOCUMENT },
222 { 0, "orientation-requested-default", IPP_TAG_ENUM, IPP_TAG_PRINTER },
223 { 1, "overrides", IPP_TAG_BEGIN_COLLECTION, IPP_TAG_JOB,
224 IPP_TAG_DOCUMENT },
225 { 0, "page-bottom", IPP_TAG_INTEGER, IPP_TAG_JOB },
226 { 0, "page-bottom-default", IPP_TAG_INTEGER, IPP_TAG_PRINTER },
227 { 0, "page-left", IPP_TAG_INTEGER, IPP_TAG_JOB },
228 { 0, "page-left-default", IPP_TAG_INTEGER, IPP_TAG_PRINTER },
229 { 1, "page-ranges", IPP_TAG_RANGE, IPP_TAG_JOB,
230 IPP_TAG_DOCUMENT },
231 { 1, "page-ranges-default", IPP_TAG_RANGE, IPP_TAG_PRINTER },
232 { 0, "page-right", IPP_TAG_INTEGER, IPP_TAG_JOB },
233 { 0, "page-right-default", IPP_TAG_INTEGER, IPP_TAG_PRINTER },
234 { 0, "page-top", IPP_TAG_INTEGER, IPP_TAG_JOB },
235 { 0, "page-top-default", IPP_TAG_INTEGER, IPP_TAG_PRINTER },
236 { 1, "pages", IPP_TAG_RANGE, IPP_TAG_JOB,
237 IPP_TAG_DOCUMENT },
238 { 0, "penwidth", IPP_TAG_INTEGER, IPP_TAG_JOB },
239 { 0, "penwidth-default", IPP_TAG_INTEGER, IPP_TAG_PRINTER },
240 { 0, "port-monitor", IPP_TAG_NAME, IPP_TAG_PRINTER },
241 { 0, "ppd-device-id", IPP_TAG_TEXT, IPP_TAG_OPERATION,
242 IPP_TAG_ZERO,
243 cups_get_ppds },
244 { 0, "ppd-make", IPP_TAG_TEXT, IPP_TAG_OPERATION,
245 IPP_TAG_ZERO,
246 cups_get_ppds },
247 { 0, "ppd-make-and-model", IPP_TAG_TEXT, IPP_TAG_OPERATION,
248 IPP_TAG_ZERO,
249 cups_get_ppds },
250 { 0, "ppd-model-number", IPP_TAG_INTEGER, IPP_TAG_OPERATION,
251 IPP_TAG_ZERO,
252 cups_get_ppds },
253 { 0, "ppd-name", IPP_TAG_NAME, IPP_TAG_OPERATION,
254 IPP_TAG_ZERO,
255 cups_ppd_name },
256 { 0, "ppd-natural-language", IPP_TAG_LANGUAGE, IPP_TAG_OPERATION,
257 IPP_TAG_ZERO,
258 cups_get_ppds },
259 { 0, "ppd-product", IPP_TAG_TEXT, IPP_TAG_OPERATION,
260 IPP_TAG_ZERO,
261 cups_get_ppds },
262 { 0, "ppd-psversion", IPP_TAG_TEXT, IPP_TAG_OPERATION,
263 IPP_TAG_ZERO,
264 cups_get_ppds },
265 { 0, "ppd-type", IPP_TAG_KEYWORD, IPP_TAG_OPERATION,
266 IPP_TAG_ZERO,
267 cups_get_ppds },
268 { 0, "ppi", IPP_TAG_INTEGER, IPP_TAG_JOB },
269 { 0, "ppi-default", IPP_TAG_INTEGER, IPP_TAG_PRINTER },
270 { 0, "prettyprint", IPP_TAG_BOOLEAN, IPP_TAG_JOB },
271 { 0, "prettyprint-default", IPP_TAG_BOOLEAN, IPP_TAG_PRINTER },
272 { 0, "print-quality", IPP_TAG_ENUM, IPP_TAG_JOB,
273 IPP_TAG_DOCUMENT },
274 { 0, "print-quality-default", IPP_TAG_ENUM, IPP_TAG_PRINTER },
275 { 1, "printer-commands", IPP_TAG_KEYWORD, IPP_TAG_PRINTER },
276 { 0, "printer-error-policy", IPP_TAG_NAME, IPP_TAG_PRINTER },
277 { 0, "printer-geo-location", IPP_TAG_URI, IPP_TAG_PRINTER },
278 { 0, "printer-info", IPP_TAG_TEXT, IPP_TAG_PRINTER },
279 { 0, "printer-is-accepting-jobs", IPP_TAG_BOOLEAN, IPP_TAG_PRINTER },
280 { 0, "printer-is-shared", IPP_TAG_BOOLEAN, IPP_TAG_PRINTER },
281 { 0, "printer-location", IPP_TAG_TEXT, IPP_TAG_PRINTER },
282 { 0, "printer-make-and-model", IPP_TAG_TEXT, IPP_TAG_PRINTER },
283 { 0, "printer-more-info", IPP_TAG_URI, IPP_TAG_PRINTER },
284 { 0, "printer-op-policy", IPP_TAG_NAME, IPP_TAG_PRINTER },
285 { 0, "printer-resolution", IPP_TAG_RESOLUTION, IPP_TAG_JOB,
286 IPP_TAG_DOCUMENT },
287 { 0, "printer-state", IPP_TAG_ENUM, IPP_TAG_PRINTER },
288 { 0, "printer-state-change-time", IPP_TAG_INTEGER, IPP_TAG_PRINTER },
289 { 1, "printer-state-reasons", IPP_TAG_KEYWORD, IPP_TAG_PRINTER },
290 { 0, "printer-type", IPP_TAG_ENUM, IPP_TAG_PRINTER },
291 { 0, "printer-uri", IPP_TAG_URI, IPP_TAG_OPERATION },
292 { 1, "printer-uri-supported", IPP_TAG_URI, IPP_TAG_PRINTER },
293 { 0, "queued-job-count", IPP_TAG_INTEGER, IPP_TAG_PRINTER },
294 { 0, "raw", IPP_TAG_MIMETYPE, IPP_TAG_OPERATION },
295 { 1, "requested-attributes", IPP_TAG_NAME, IPP_TAG_OPERATION },
296 { 1, "requesting-user-name-allowed", IPP_TAG_NAME, IPP_TAG_PRINTER },
297 { 1, "requesting-user-name-denied", IPP_TAG_NAME, IPP_TAG_PRINTER },
298 { 0, "resolution", IPP_TAG_RESOLUTION, IPP_TAG_JOB },
299 { 0, "resolution-default", IPP_TAG_RESOLUTION, IPP_TAG_PRINTER },
300 { 0, "saturation", IPP_TAG_INTEGER, IPP_TAG_JOB },
301 { 0, "saturation-default", IPP_TAG_INTEGER, IPP_TAG_PRINTER },
302 { 0, "scaling", IPP_TAG_INTEGER, IPP_TAG_JOB },
303 { 0, "scaling-default", IPP_TAG_INTEGER, IPP_TAG_PRINTER },
304 { 0, "sides", IPP_TAG_KEYWORD, IPP_TAG_JOB,
305 IPP_TAG_DOCUMENT },
306 { 0, "sides-default", IPP_TAG_KEYWORD, IPP_TAG_PRINTER },
307 { 0, "time-at-completed", IPP_TAG_INTEGER, IPP_TAG_ZERO }, /* never send as option */
308 { 0, "time-at-creation", IPP_TAG_INTEGER, IPP_TAG_ZERO }, /* never send as option */
309 { 0, "time-at-processing", IPP_TAG_INTEGER, IPP_TAG_ZERO }, /* never send as option */
310 { 0, "wrap", IPP_TAG_BOOLEAN, IPP_TAG_JOB },
311 { 0, "wrap-default", IPP_TAG_BOOLEAN, IPP_TAG_PRINTER },
312 { 0, "x-dimension", IPP_TAG_INTEGER, IPP_TAG_JOB,
313 IPP_TAG_DOCUMENT },
314 { 0, "y-dimension", IPP_TAG_INTEGER, IPP_TAG_JOB,
315 IPP_TAG_DOCUMENT }
316 };
317
318
319 /*
320 * Local functions...
321 */
322
323 static int compare_ipp_options(_ipp_option_t *a, _ipp_option_t *b);
324
325
326 /*
327 * 'cupsEncodeOptions()' - Encode printer options into IPP attributes.
328 *
329 * This function adds operation, job, and then subscription attributes,
330 * in that order. Use the cupsEncodeOptions2() function to add attributes
331 * for a single group.
332 */
333
334 void
335 cupsEncodeOptions(ipp_t *ipp, /* I - Request to add to */
336 int num_options, /* I - Number of options */
337 cups_option_t *options) /* I - Options */
338 {
339 DEBUG_printf(("cupsEncodeOptions(%p, %d, %p)", ipp, num_options, options));
340
341 /*
342 * Add the options in the proper groups & order...
343 */
344
345 cupsEncodeOptions2(ipp, num_options, options, IPP_TAG_OPERATION);
346 cupsEncodeOptions2(ipp, num_options, options, IPP_TAG_JOB);
347 cupsEncodeOptions2(ipp, num_options, options, IPP_TAG_SUBSCRIPTION);
348 }
349
350
351 /*
352 * 'cupsEncodeOptions2()' - Encode printer options into IPP attributes for a group.
353 *
354 * This function only adds attributes for a single group. Call this
355 * function multiple times for each group, or use cupsEncodeOptions()
356 * to add the standard groups.
357 *
358 * @since CUPS 1.2/OS X 10.5@
359 */
360
361 void
362 cupsEncodeOptions2(
363 ipp_t *ipp, /* I - Request to add to */
364 int num_options, /* I - Number of options */
365 cups_option_t *options, /* I - Options */
366 ipp_tag_t group_tag) /* I - Group to encode */
367 {
368 int i, j; /* Looping vars */
369 int count; /* Number of values */
370 char *s, /* Pointer into option value */
371 *val, /* Pointer to option value */
372 *copy, /* Copy of option value */
373 *sep, /* Option separator */
374 quote; /* Quote character */
375 ipp_attribute_t *attr; /* IPP attribute */
376 ipp_tag_t value_tag; /* IPP value tag */
377 cups_option_t *option; /* Current option */
378 ipp_t *collection; /* Collection value */
379 int num_cols; /* Number of collection values */
380 cups_option_t *cols; /* Collection values */
381 ipp_op_t op; /* Operation for this request */
382 const ipp_op_t *ops; /* List of allowed operations */
383
384
385 DEBUG_printf(("cupsEncodeOptions2(ipp=%p(%s), num_options=%d, options=%p, "
386 "group_tag=%x)", ipp,
387 ipp ? ippOpString(ippGetOperation(ipp)) : "", num_options,
388 options, group_tag));
389
390 /*
391 * Range check input...
392 */
393
394 if (!ipp || num_options < 1 || !options)
395 return;
396
397 /*
398 * Do special handling for the document-format/raw options...
399 */
400
401 op = ippGetOperation(ipp);
402
403 if (group_tag == IPP_TAG_OPERATION &&
404 (op == IPP_OP_PRINT_JOB || op == IPP_OP_PRINT_URI ||
405 op == IPP_OP_SEND_DOCUMENT || op == IPP_OP_SEND_URI))
406 {
407 /*
408 * Handle the document format stuff first...
409 */
410
411 if ((val = (char *)cupsGetOption("document-format", num_options,
412 options)) != NULL)
413 ippAddString(ipp, IPP_TAG_OPERATION, IPP_TAG_MIMETYPE, "document-format",
414 NULL, val);
415 else if (cupsGetOption("raw", num_options, options))
416 ippAddString(ipp, IPP_TAG_OPERATION, IPP_TAG_MIMETYPE, "document-format",
417 NULL, "application/vnd.cups-raw");
418 else
419 ippAddString(ipp, IPP_TAG_OPERATION, IPP_TAG_MIMETYPE, "document-format",
420 NULL, "application/octet-stream");
421 }
422
423 /*
424 * Then loop through the options...
425 */
426
427 for (i = num_options, option = options; i > 0; i --, option ++)
428 {
429 _ipp_option_t *match; /* Matching attribute */
430
431
432 /*
433 * Skip document format options that are handled above...
434 */
435
436 if (!_cups_strcasecmp(option->name, "raw") ||
437 !_cups_strcasecmp(option->name, "document-format") ||
438 !option->name[0])
439 continue;
440
441 /*
442 * Figure out the proper value and group tags for this option...
443 */
444
445 if ((match = _ippFindOption(option->name)) != NULL)
446 {
447 if (match->group_tag != group_tag && match->alt_group_tag != group_tag)
448 continue;
449
450 value_tag = match->value_tag;
451
452 if (match->operations)
453 ops = match->operations;
454 else if (group_tag == IPP_TAG_JOB)
455 ops = ipp_job_creation;
456 else if (group_tag == IPP_TAG_DOCUMENT)
457 ops = ipp_doc_creation;
458 else if (group_tag == IPP_TAG_SUBSCRIPTION)
459 ops = ipp_sub_creation;
460 else if (group_tag == IPP_TAG_PRINTER)
461 ops = ipp_set_printer;
462 else
463 {
464 DEBUG_printf(("2cupsEncodeOptions2: Skipping \"%s\".", option->name));
465 continue;
466 }
467 }
468 else
469 {
470 int namelen; /* Length of name */
471
472
473 namelen = (int)strlen(option->name);
474
475 if (namelen < 10 ||
476 (strcmp(option->name + namelen - 8, "-default") &&
477 strcmp(option->name + namelen - 10, "-supported")))
478 {
479 if (group_tag != IPP_TAG_JOB && group_tag != IPP_TAG_DOCUMENT)
480 {
481 DEBUG_printf(("2cupsEncodeOptions2: Skipping \"%s\".", option->name));
482 continue;
483 }
484 }
485 else if (group_tag != IPP_TAG_PRINTER)
486 {
487 DEBUG_printf(("2cupsEncodeOptions2: Skipping \"%s\".", option->name));
488 continue;
489 }
490
491 if (group_tag == IPP_TAG_JOB)
492 ops = ipp_job_creation;
493 else if (group_tag == IPP_TAG_DOCUMENT)
494 ops = ipp_doc_creation;
495 else
496 ops = ipp_set_printer;
497
498 if (!_cups_strcasecmp(option->value, "true") ||
499 !_cups_strcasecmp(option->value, "false"))
500 value_tag = IPP_TAG_BOOLEAN;
501 else
502 value_tag = IPP_TAG_NAME;
503 }
504
505 /*
506 * Verify that we send this attribute for this operation...
507 */
508
509 while (*ops != IPP_OP_CUPS_NONE)
510 if (op == *ops)
511 break;
512 else
513 ops ++;
514
515 if (*ops == IPP_OP_CUPS_NONE && op != IPP_OP_CUPS_NONE)
516 {
517 DEBUG_printf(("2cupsEncodeOptions2: Skipping \"%s\".", option->name));
518 continue;
519 }
520
521 /*
522 * Count the number of values...
523 */
524
525 if (match && match->multivalue)
526 {
527 for (count = 1, sep = option->value, quote = 0; *sep; sep ++)
528 {
529 if (*sep == quote)
530 quote = 0;
531 else if (!quote && (*sep == '\'' || *sep == '\"'))
532 {
533 /*
534 * Skip quoted option value...
535 */
536
537 quote = *sep++;
538 }
539 else if (*sep == ',' && !quote)
540 count ++;
541 else if (*sep == '\\' && sep[1])
542 sep ++;
543 }
544 }
545 else
546 count = 1;
547
548 DEBUG_printf(("2cupsEncodeOptions2: option=\"%s\", count=%d",
549 option->name, count));
550
551 /*
552 * Allocate memory for the attribute values...
553 */
554
555 if ((attr = ippAddStrings(ipp, group_tag, value_tag, option->name, count,
556 NULL, NULL)) == NULL)
557 {
558 /*
559 * Ran out of memory!
560 */
561
562 DEBUG_puts("1cupsEncodeOptions2: Ran out of memory for attributes!");
563 return;
564 }
565
566 if (count > 1)
567 {
568 /*
569 * Make a copy of the value we can fiddle with...
570 */
571
572 if ((copy = strdup(option->value)) == NULL)
573 {
574 /*
575 * Ran out of memory!
576 */
577
578 DEBUG_puts("1cupsEncodeOptions2: Ran out of memory for value copy!");
579 ippDeleteAttribute(ipp, attr);
580 return;
581 }
582
583 val = copy;
584 }
585 else
586 {
587 /*
588 * Since we have a single value, use the value directly...
589 */
590
591 val = option->value;
592 copy = NULL;
593 }
594
595 /*
596 * Scan the value string for values...
597 */
598
599 for (j = 0, sep = val; j < count; val = sep, j ++)
600 {
601 /*
602 * Find the end of this value and mark it if needed...
603 */
604
605 if (count > 1)
606 {
607 for (quote = 0; *sep; sep ++)
608 {
609 if (*sep == quote)
610 {
611 /*
612 * Finish quoted value...
613 */
614
615 quote = 0;
616 }
617 else if (!quote && (*sep == '\'' || *sep == '\"'))
618 {
619 /*
620 * Handle quoted option value...
621 */
622
623 quote = *sep;
624 }
625 else if (*sep == ',' && count > 1)
626 break;
627 else if (*sep == '\\' && sep[1])
628 {
629 /*
630 * Skip quoted character...
631 */
632
633 sep ++;
634 }
635 }
636
637 if (*sep == ',')
638 *sep++ = '\0';
639 }
640
641 /*
642 * Copy the option value(s) over as needed by the type...
643 */
644
645 switch (attr->value_tag)
646 {
647 case IPP_TAG_INTEGER :
648 case IPP_TAG_ENUM :
649 /*
650 * Integer/enumeration value...
651 */
652
653 attr->values[j].integer = (int)strtol(val, &s, 10);
654
655 DEBUG_printf(("2cupsEncodeOptions2: Added integer option value "
656 "%d...", attr->values[j].integer));
657 break;
658
659 case IPP_TAG_BOOLEAN :
660 if (!_cups_strcasecmp(val, "true") ||
661 !_cups_strcasecmp(val, "on") ||
662 !_cups_strcasecmp(val, "yes"))
663 {
664 /*
665 * Boolean value - true...
666 */
667
668 attr->values[j].boolean = 1;
669
670 DEBUG_puts("2cupsEncodeOptions2: Added boolean true value...");
671 }
672 else
673 {
674 /*
675 * Boolean value - false...
676 */
677
678 attr->values[j].boolean = 0;
679
680 DEBUG_puts("2cupsEncodeOptions2: Added boolean false value...");
681 }
682 break;
683
684 case IPP_TAG_RANGE :
685 /*
686 * Range...
687 */
688
689 if (*val == '-')
690 {
691 attr->values[j].range.lower = 1;
692 s = val;
693 }
694 else
695 attr->values[j].range.lower = (int)strtol(val, &s, 10);
696
697 if (*s == '-')
698 {
699 if (s[1])
700 attr->values[j].range.upper = (int)strtol(s + 1, NULL, 10);
701 else
702 attr->values[j].range.upper = 2147483647;
703 }
704 else
705 attr->values[j].range.upper = attr->values[j].range.lower;
706
707 DEBUG_printf(("2cupsEncodeOptions2: Added range option value "
708 "%d-%d...", attr->values[j].range.lower,
709 attr->values[j].range.upper));
710 break;
711
712 case IPP_TAG_RESOLUTION :
713 /*
714 * Resolution...
715 */
716
717 attr->values[j].resolution.xres = (int)strtol(val, &s, 10);
718
719 if (*s == 'x')
720 attr->values[j].resolution.yres = (int)strtol(s + 1, &s, 10);
721 else
722 attr->values[j].resolution.yres = attr->values[j].resolution.xres;
723
724 if (!_cups_strcasecmp(s, "dpc") ||
725 !_cups_strcasecmp(s, "dpcm"))
726 attr->values[j].resolution.units = IPP_RES_PER_CM;
727 else
728 attr->values[j].resolution.units = IPP_RES_PER_INCH;
729
730 DEBUG_printf(("2cupsEncodeOptions2: Added resolution option value "
731 "%s...", val));
732 break;
733
734 case IPP_TAG_STRING :
735 /*
736 * octet-string
737 */
738
739 attr->values[j].unknown.length = (int)strlen(val);
740 attr->values[j].unknown.data = strdup(val);
741
742 DEBUG_printf(("2cupsEncodeOptions2: Added octet-string value "
743 "\"%s\"...", (char *)attr->values[j].unknown.data));
744 break;
745
746 case IPP_TAG_BEGIN_COLLECTION :
747 /*
748 * Collection value
749 */
750
751 num_cols = cupsParseOptions(val, 0, &cols);
752 if ((collection = ippNew()) == NULL)
753 {
754 cupsFreeOptions(num_cols, cols);
755
756 if (copy)
757 free(copy);
758
759 ippDeleteAttribute(ipp, attr);
760 return;
761 }
762
763 attr->values[j].collection = collection;
764 cupsEncodeOptions2(collection, num_cols, cols, IPP_TAG_JOB);
765 cupsFreeOptions(num_cols, cols);
766 break;
767
768 default :
769 if ((attr->values[j].string.text = _cupsStrAlloc(val)) == NULL)
770 {
771 /*
772 * Ran out of memory!
773 */
774
775 DEBUG_puts("1cupsEncodeOptions2: Ran out of memory for string!");
776
777 if (copy)
778 free(copy);
779
780 ippDeleteAttribute(ipp, attr);
781 return;
782 }
783
784 DEBUG_printf(("2cupsEncodeOptions2: Added string value \"%s\"...",
785 val));
786 break;
787 }
788 }
789
790 if (copy)
791 free(copy);
792 }
793 }
794
795
796 #ifdef DEBUG
797 /*
798 * '_ippCheckOptions()' - Validate that the option array is sorted properly.
799 */
800
801 const char * /* O - First out-of-order option or NULL */
802 _ippCheckOptions(void)
803 {
804 int i; /* Looping var */
805
806
807 for (i = 0; i < (int)(sizeof(ipp_options) / sizeof(ipp_options[0]) - 1); i ++)
808 if (strcmp(ipp_options[i].name, ipp_options[i + 1].name) >= 0)
809 return (ipp_options[i + 1].name);
810
811 return (NULL);
812 }
813 #endif /* DEBUG */
814
815
816 /*
817 * '_ippFindOption()' - Find the attribute information for an option.
818 */
819
820 _ipp_option_t * /* O - Attribute information */
821 _ippFindOption(const char *name) /* I - Option/attribute name */
822 {
823 _ipp_option_t key; /* Search key */
824
825
826 /*
827 * Lookup the proper value and group tags for this option...
828 */
829
830 key.name = name;
831
832 return ((_ipp_option_t *)bsearch(&key, ipp_options,
833 sizeof(ipp_options) / sizeof(ipp_options[0]),
834 sizeof(ipp_options[0]),
835 (int (*)(const void *, const void *))
836 compare_ipp_options));
837 }
838
839
840 /*
841 * 'compare_ipp_options()' - Compare two IPP options.
842 */
843
844 static int /* O - Result of comparison */
845 compare_ipp_options(_ipp_option_t *a, /* I - First option */
846 _ipp_option_t *b) /* I - Second option */
847 {
848 return (strcmp(a->name, b->name));
849 }
850
851
852 /*
853 * End of "$Id$".
854 */