]> git.ipfire.org Git - thirdparty/cups.git/blob - cups/encode.c
Merge changes from CUPS 1.4svn-r8162.
[thirdparty/cups.git] / cups / encode.c
1 /*
2 * "$Id: encode.c 7696 2008-06-26 00:54:42Z mike $"
3 *
4 * Option encoding routines for the Common UNIX Printing System (CUPS).
5 *
6 * Copyright 2007-2008 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 * Contents:
18 *
19 * cupsEncodeOptions() - Encode printer options into IPP attributes.
20 * cupsEncodeOptions2() - Encode printer options into IPP attributes for
21 * a group.
22 * _ippFindOption() - Find the attribute information for an option.
23 * compare_ipp_options() - Compare two IPP options.
24 */
25
26 /*
27 * Include necessary headers...
28 */
29
30 #include "cups.h"
31 #include "ipp-private.h"
32 #include <stdlib.h>
33 #include <ctype.h>
34 #include "string.h"
35 #include "debug.h"
36
37
38 /*
39 * Local list of option names and the value tags they should use...
40 *
41 * **** THIS LIST MUST BE SORTED ****
42 */
43
44 static const _ipp_option_t ipp_options[] =
45 {
46 { 1, "auth-info", IPP_TAG_TEXT, IPP_TAG_JOB },
47 { 1, "auth-info-required", IPP_TAG_KEYWORD, IPP_TAG_PRINTER },
48 { 0, "blackplot", IPP_TAG_BOOLEAN, IPP_TAG_JOB },
49 { 0, "blackplot-default", IPP_TAG_BOOLEAN, IPP_TAG_PRINTER },
50 { 0, "brightness", IPP_TAG_INTEGER, IPP_TAG_JOB },
51 { 0, "brightness-default", IPP_TAG_INTEGER, IPP_TAG_PRINTER },
52 { 0, "columns", IPP_TAG_INTEGER, IPP_TAG_JOB },
53 { 0, "columns-default", IPP_TAG_INTEGER, IPP_TAG_PRINTER },
54 { 0, "compression", IPP_TAG_KEYWORD, IPP_TAG_OPERATION },
55 { 0, "copies", IPP_TAG_INTEGER, IPP_TAG_JOB },
56 { 0, "copies-default", IPP_TAG_INTEGER, IPP_TAG_PRINTER },
57 { 0, "document-format", IPP_TAG_MIMETYPE, IPP_TAG_OPERATION },
58 { 0, "document-format-default", IPP_TAG_MIMETYPE, IPP_TAG_PRINTER },
59 { 1, "exclude-schemes", IPP_TAG_NAME, IPP_TAG_OPERATION },
60 { 1, "finishings", IPP_TAG_ENUM, IPP_TAG_JOB },
61 { 1, "finishings-default", IPP_TAG_ENUM, IPP_TAG_PRINTER },
62 { 0, "fit-to-page", IPP_TAG_BOOLEAN, IPP_TAG_JOB },
63 { 0, "fit-to-page-default", IPP_TAG_BOOLEAN, IPP_TAG_PRINTER },
64 { 0, "fitplot", IPP_TAG_BOOLEAN, IPP_TAG_JOB },
65 { 0, "fitplot-default", IPP_TAG_BOOLEAN, IPP_TAG_PRINTER },
66 { 0, "gamma", IPP_TAG_INTEGER, IPP_TAG_JOB },
67 { 0, "gamma-default", IPP_TAG_INTEGER, IPP_TAG_PRINTER },
68 { 0, "hue", IPP_TAG_INTEGER, IPP_TAG_JOB },
69 { 0, "hue-default", IPP_TAG_INTEGER, IPP_TAG_PRINTER },
70 { 1, "include-schemes", IPP_TAG_NAME, IPP_TAG_OPERATION },
71 { 0, "job-impressions", IPP_TAG_INTEGER, IPP_TAG_JOB },
72 { 0, "job-k-limit", IPP_TAG_INTEGER, IPP_TAG_JOB },
73 { 0, "job-page-limit", IPP_TAG_INTEGER, IPP_TAG_JOB },
74 { 0, "job-priority", IPP_TAG_INTEGER, IPP_TAG_JOB },
75 { 0, "job-quota-period", IPP_TAG_INTEGER, IPP_TAG_JOB },
76 { 1, "job-sheets", IPP_TAG_NAME, IPP_TAG_JOB },
77 { 1, "job-sheets-default", IPP_TAG_NAME, IPP_TAG_PRINTER },
78 { 0, "job-uuid", IPP_TAG_URI, IPP_TAG_JOB },
79 { 0, "landscape", IPP_TAG_BOOLEAN, IPP_TAG_JOB },
80 { 1, "media", IPP_TAG_KEYWORD, IPP_TAG_JOB },
81 { 0, "mirror", IPP_TAG_BOOLEAN, IPP_TAG_JOB },
82 { 0, "mirror-default", IPP_TAG_BOOLEAN, IPP_TAG_PRINTER },
83 { 0, "natural-scaling", IPP_TAG_INTEGER, IPP_TAG_JOB },
84 { 0, "natural-scaling-default", IPP_TAG_INTEGER, IPP_TAG_PRINTER },
85 { 0, "notify-charset", IPP_TAG_CHARSET, IPP_TAG_SUBSCRIPTION },
86 { 1, "notify-events", IPP_TAG_KEYWORD, IPP_TAG_SUBSCRIPTION },
87 { 1, "notify-events-default", IPP_TAG_KEYWORD, IPP_TAG_PRINTER },
88 { 0, "notify-lease-duration", IPP_TAG_INTEGER, IPP_TAG_SUBSCRIPTION },
89 { 0, "notify-lease-duration-default", IPP_TAG_INTEGER, IPP_TAG_PRINTER },
90 { 0, "notify-natural-language", IPP_TAG_LANGUAGE, IPP_TAG_SUBSCRIPTION },
91 { 0, "notify-pull-method", IPP_TAG_KEYWORD, IPP_TAG_SUBSCRIPTION },
92 { 0, "notify-recipient-uri", IPP_TAG_URI, IPP_TAG_SUBSCRIPTION },
93 { 0, "notify-time-interval", IPP_TAG_INTEGER, IPP_TAG_SUBSCRIPTION },
94 { 0, "notify-user-data", IPP_TAG_STRING, IPP_TAG_SUBSCRIPTION },
95 { 0, "number-up", IPP_TAG_INTEGER, IPP_TAG_JOB },
96 { 0, "number-up-default", IPP_TAG_INTEGER, IPP_TAG_PRINTER },
97 { 0, "orientation-requested", IPP_TAG_ENUM, IPP_TAG_JOB },
98 { 0, "orientation-requested-default", IPP_TAG_ENUM, IPP_TAG_PRINTER },
99 { 0, "page-bottom", IPP_TAG_INTEGER, IPP_TAG_JOB },
100 { 0, "page-bottom-default", IPP_TAG_INTEGER, IPP_TAG_PRINTER },
101 { 0, "page-left", IPP_TAG_INTEGER, IPP_TAG_JOB },
102 { 0, "page-left-default", IPP_TAG_INTEGER, IPP_TAG_PRINTER },
103 { 1, "page-ranges", IPP_TAG_RANGE, IPP_TAG_JOB },
104 { 1, "page-ranges-default", IPP_TAG_RANGE, IPP_TAG_PRINTER },
105 { 0, "page-right", IPP_TAG_INTEGER, IPP_TAG_JOB },
106 { 0, "page-right-default", IPP_TAG_INTEGER, IPP_TAG_PRINTER },
107 { 0, "page-top", IPP_TAG_INTEGER, IPP_TAG_JOB },
108 { 0, "page-top-default", IPP_TAG_INTEGER, IPP_TAG_PRINTER },
109 { 0, "penwidth", IPP_TAG_INTEGER, IPP_TAG_JOB },
110 { 0, "penwidth-default", IPP_TAG_INTEGER, IPP_TAG_PRINTER },
111 { 0, "port-monitor", IPP_TAG_NAME, IPP_TAG_PRINTER },
112 { 0, "ppi", IPP_TAG_INTEGER, IPP_TAG_JOB },
113 { 0, "ppi-default", IPP_TAG_INTEGER, IPP_TAG_PRINTER },
114 { 0, "prettyprint", IPP_TAG_BOOLEAN, IPP_TAG_JOB },
115 { 0, "prettyprint-default", IPP_TAG_BOOLEAN, IPP_TAG_PRINTER },
116 { 0, "print-quality", IPP_TAG_ENUM, IPP_TAG_JOB },
117 { 0, "print-quality-default", IPP_TAG_ENUM, IPP_TAG_PRINTER },
118 { 0, "printer-error-policy", IPP_TAG_NAME, IPP_TAG_PRINTER },
119 { 0, "printer-info", IPP_TAG_TEXT, IPP_TAG_PRINTER },
120 { 0, "printer-is-accepting-jobs", IPP_TAG_BOOLEAN, IPP_TAG_PRINTER },
121 { 0, "printer-is-shared", IPP_TAG_BOOLEAN, IPP_TAG_PRINTER },
122 { 0, "printer-location", IPP_TAG_TEXT, IPP_TAG_PRINTER },
123 { 0, "printer-make-and-model", IPP_TAG_TEXT, IPP_TAG_PRINTER },
124 { 0, "printer-more-info", IPP_TAG_URI, IPP_TAG_PRINTER },
125 { 0, "printer-op-policy", IPP_TAG_NAME, IPP_TAG_PRINTER },
126 { 0, "printer-resolution", IPP_TAG_RESOLUTION, IPP_TAG_JOB },
127 { 0, "printer-state", IPP_TAG_ENUM, IPP_TAG_PRINTER },
128 { 0, "printer-state-change-time", IPP_TAG_INTEGER, IPP_TAG_PRINTER },
129 { 1, "printer-state-reasons", IPP_TAG_KEYWORD, IPP_TAG_PRINTER },
130 { 0, "printer-type", IPP_TAG_ENUM, IPP_TAG_PRINTER },
131 { 0, "printer-uri", IPP_TAG_URI, IPP_TAG_OPERATION },
132 { 0, "queued-job-count", IPP_TAG_INTEGER, IPP_TAG_PRINTER },
133 { 0, "raw", IPP_TAG_MIMETYPE, IPP_TAG_OPERATION },
134 { 1, "requested-attributes", IPP_TAG_NAME, IPP_TAG_OPERATION },
135 { 1, "requesting-user-name-allowed", IPP_TAG_NAME, IPP_TAG_PRINTER },
136 { 1, "requesting-user-name-denied", IPP_TAG_NAME, IPP_TAG_PRINTER },
137 { 0, "resolution", IPP_TAG_RESOLUTION, IPP_TAG_JOB },
138 { 0, "resolution-default", IPP_TAG_RESOLUTION, IPP_TAG_PRINTER },
139 { 0, "saturation", IPP_TAG_INTEGER, IPP_TAG_JOB },
140 { 0, "saturation-default", IPP_TAG_INTEGER, IPP_TAG_PRINTER },
141 { 0, "scaling", IPP_TAG_INTEGER, IPP_TAG_JOB },
142 { 0, "scaling-default", IPP_TAG_INTEGER, IPP_TAG_PRINTER },
143 { 0, "sides", IPP_TAG_KEYWORD, IPP_TAG_JOB },
144 { 0, "sides-default", IPP_TAG_KEYWORD, IPP_TAG_PRINTER },
145 { 0, "wrap", IPP_TAG_BOOLEAN, IPP_TAG_JOB },
146 { 0, "wrap-default", IPP_TAG_BOOLEAN, IPP_TAG_PRINTER }
147 };
148
149
150 /*
151 * Local functions...
152 */
153
154 static int compare_ipp_options(_ipp_option_t *a, _ipp_option_t *b);
155
156
157 /*
158 * 'cupsEncodeOptions()' - Encode printer options into IPP attributes.
159 *
160 * This function adds operation, job, and then subscription attributes,
161 * in that order. Use the cupsEncodeOptions2() function to add attributes
162 * for a single group.
163 */
164
165 void
166 cupsEncodeOptions(ipp_t *ipp, /* I - Request to add to */
167 int num_options, /* I - Number of options */
168 cups_option_t *options) /* I - Options */
169 {
170 DEBUG_printf(("cupsEncodeOptions(%p, %d, %p)\n", ipp, num_options, options));
171
172 /*
173 * Add the options in the proper groups & order...
174 */
175
176 cupsEncodeOptions2(ipp, num_options, options, IPP_TAG_OPERATION);
177 cupsEncodeOptions2(ipp, num_options, options, IPP_TAG_JOB);
178 cupsEncodeOptions2(ipp, num_options, options, IPP_TAG_SUBSCRIPTION);
179 }
180
181
182 /*
183 * 'cupsEncodeOptions2()' - Encode printer options into IPP attributes for a group.
184 *
185 * This function only adds attributes for a single group. Call this
186 * function multiple times for each group, or use cupsEncodeOptions()
187 * to add the standard groups.
188 *
189 * @since CUPS 1.2@
190 */
191
192 void
193 cupsEncodeOptions2(
194 ipp_t *ipp, /* I - Request to add to */
195 int num_options, /* I - Number of options */
196 cups_option_t *options, /* I - Options */
197 ipp_tag_t group_tag) /* I - Group to encode */
198 {
199 int i, j; /* Looping vars */
200 int count; /* Number of values */
201 char *s, /* Pointer into option value */
202 *val, /* Pointer to option value */
203 *copy, /* Copy of option value */
204 *sep, /* Option separator */
205 quote; /* Quote character */
206 ipp_attribute_t *attr; /* IPP attribute */
207 ipp_tag_t value_tag; /* IPP value tag */
208 cups_option_t *option; /* Current option */
209
210
211 DEBUG_printf(("cupsEncodeOptions2(ipp=%p, num_options=%d, options=%p, "
212 "group_tag=%x)\n", ipp, num_options, options, group_tag));
213
214 /*
215 * Range check input...
216 */
217
218 if (!ipp || num_options < 1 || !options)
219 return;
220
221 /*
222 * Do special handling for the document-format/raw options...
223 */
224
225 if (group_tag == IPP_TAG_OPERATION)
226 {
227 /*
228 * Handle the document format stuff first...
229 */
230
231 if ((val = (char *)cupsGetOption("document-format", num_options, options)) != NULL)
232 ippAddString(ipp, IPP_TAG_OPERATION, IPP_TAG_MIMETYPE, "document-format",
233 NULL, val);
234 else if (cupsGetOption("raw", num_options, options))
235 ippAddString(ipp, IPP_TAG_OPERATION, IPP_TAG_MIMETYPE, "document-format",
236 NULL, "application/vnd.cups-raw");
237 else
238 ippAddString(ipp, IPP_TAG_OPERATION, IPP_TAG_MIMETYPE, "document-format",
239 NULL, "application/octet-stream");
240 }
241
242 /*
243 * Then loop through the options...
244 */
245
246 for (i = num_options, option = options; i > 0; i --, option ++)
247 {
248 _ipp_option_t *match; /* Matching attribute */
249
250
251 /*
252 * Skip document format options that are handled above...
253 */
254
255 if (!strcasecmp(option->name, "raw") ||
256 !strcasecmp(option->name, "document-format") ||
257 !option->name[0])
258 continue;
259
260 /*
261 * Figure out the proper value and group tags for this option...
262 */
263
264 if ((match = _ippFindOption(option->name)) != NULL)
265 {
266 if (match->group_tag != group_tag)
267 continue;
268
269 value_tag = match->value_tag;
270 }
271 else
272 {
273 int namelen; /* Length of name */
274
275
276 namelen = (int)strlen(option->name);
277
278 if (namelen < 9 || strcmp(option->name + namelen - 8, "-default"))
279 {
280 if (group_tag != IPP_TAG_JOB)
281 continue;
282 }
283 else if (group_tag != IPP_TAG_PRINTER)
284 continue;
285
286 if (!strcasecmp(option->value, "true") ||
287 !strcasecmp(option->value, "false"))
288 value_tag = IPP_TAG_BOOLEAN;
289 else
290 value_tag = IPP_TAG_NAME;
291 }
292
293 /*
294 * Count the number of values...
295 */
296
297 if (match && match->multivalue)
298 {
299 for (count = 1, sep = option->value, quote = 0; *sep; sep ++)
300 {
301 if (*sep == quote)
302 quote = 0;
303 else if (!quote && (*sep == '\'' || *sep == '\"'))
304 {
305 /*
306 * Skip quoted option value...
307 */
308
309 quote = *sep++;
310 }
311 else if (*sep == ',' && !quote)
312 count ++;
313 else if (*sep == '\\' && sep[1])
314 sep ++;
315 }
316 }
317 else
318 count = 1;
319
320 DEBUG_printf(("cupsEncodeOptions2: option = \'%s\', count = %d\n",
321 option->name, count));
322
323 /*
324 * Allocate memory for the attribute values...
325 */
326
327 if ((attr = _ippAddAttr(ipp, count)) == NULL)
328 {
329 /*
330 * Ran out of memory!
331 */
332
333 DEBUG_puts("cupsEncodeOptions2: Ran out of memory for attributes!");
334 return;
335 }
336
337 /*
338 * Now figure out what type of value we have...
339 */
340
341 attr->group_tag = group_tag;
342 attr->value_tag = value_tag;
343
344 /*
345 * Copy the name over...
346 */
347
348 if ((attr->name = _cupsStrAlloc(option->name)) == NULL)
349 {
350 /*
351 * Ran out of memory!
352 */
353
354 DEBUG_puts("cupsEncodeOptions2: Ran out of memory for name!");
355 return;
356 }
357
358 if (count > 1)
359 {
360 /*
361 * Make a copy of the value we can fiddle with...
362 */
363
364 if ((copy = strdup(option->value)) == NULL)
365 {
366 /*
367 * Ran out of memory!
368 */
369
370 DEBUG_puts("cupsEncodeOptions2: Ran out of memory for value copy!");
371 return;
372 }
373
374 val = copy;
375 }
376 else
377 {
378 /*
379 * Since we have a single value, use the value directly...
380 */
381
382 val = option->value;
383 copy = NULL;
384 }
385
386 /*
387 * Scan the value string for values...
388 */
389
390 for (j = 0, sep = val; j < count; val = sep, j ++)
391 {
392 /*
393 * Find the end of this value and mark it if needed...
394 */
395
396 if (count > 1)
397 {
398 for (quote = 0; *sep; sep ++)
399 {
400 if (*sep == quote)
401 {
402 /*
403 * Finish quoted value...
404 */
405
406 quote = 0;
407 }
408 else if (!quote && (*sep == '\'' || *sep == '\"'))
409 {
410 /*
411 * Handle quoted option value...
412 */
413
414 quote = *sep;
415 }
416 else if (*sep == ',' && count > 1)
417 break;
418 else if (*sep == '\\' && sep[1])
419 {
420 /*
421 * Skip quoted character...
422 */
423
424 sep ++;
425 }
426 }
427
428 if (*sep == ',')
429 *sep++ = '\0';
430 }
431
432 /*
433 * Copy the option value(s) over as needed by the type...
434 */
435
436 switch (attr->value_tag)
437 {
438 case IPP_TAG_INTEGER :
439 case IPP_TAG_ENUM :
440 /*
441 * Integer/enumeration value...
442 */
443
444 attr->values[j].integer = strtol(val, &s, 10);
445
446 DEBUG_printf(("cupsEncodeOptions2: Added integer option value %d...\n",
447 attr->values[j].integer));
448 break;
449
450 case IPP_TAG_BOOLEAN :
451 if (!strcasecmp(val, "true") ||
452 !strcasecmp(val, "on") ||
453 !strcasecmp(val, "yes"))
454 {
455 /*
456 * Boolean value - true...
457 */
458
459 attr->values[j].boolean = 1;
460
461 DEBUG_puts("cupsEncodeOptions2: Added boolean true value...");
462 }
463 else
464 {
465 /*
466 * Boolean value - false...
467 */
468
469 attr->values[j].boolean = 0;
470
471 DEBUG_puts("cupsEncodeOptions2: Added boolean false value...");
472 }
473 break;
474
475 case IPP_TAG_RANGE :
476 /*
477 * Range...
478 */
479
480 if (*val == '-')
481 {
482 attr->values[j].range.lower = 1;
483 s = val;
484 }
485 else
486 attr->values[j].range.lower = strtol(val, &s, 10);
487
488 if (*s == '-')
489 {
490 if (s[1])
491 attr->values[j].range.upper = strtol(s + 1, NULL, 10);
492 else
493 attr->values[j].range.upper = 2147483647;
494 }
495 else
496 attr->values[j].range.upper = attr->values[j].range.lower;
497
498 DEBUG_printf(("cupsEncodeOptions2: Added range option value %d-%d...\n",
499 attr->values[j].range.lower,
500 attr->values[j].range.upper));
501 break;
502
503 case IPP_TAG_RESOLUTION :
504 /*
505 * Resolution...
506 */
507
508 attr->values[j].resolution.xres = strtol(val, &s, 10);
509
510 if (*s == 'x')
511 attr->values[j].resolution.yres = strtol(s + 1, &s, 10);
512 else
513 attr->values[j].resolution.yres = attr->values[j].resolution.xres;
514
515 if (!strcasecmp(s, "dpc"))
516 attr->values[j].resolution.units = IPP_RES_PER_CM;
517 else
518 attr->values[j].resolution.units = IPP_RES_PER_INCH;
519
520 DEBUG_printf(("cupsEncodeOptions2: Added resolution option value %s...\n",
521 val));
522 break;
523
524 case IPP_TAG_STRING :
525 /*
526 * octet-string
527 */
528
529 attr->values[j].unknown.length = (int)strlen(val);
530 attr->values[j].unknown.data = strdup(val);
531
532 DEBUG_printf(("cupsEncodeOptions2: Added octet-string value "
533 "\"%s\"...\n", (char *)attr->values[j].unknown.data));
534 break;
535
536 default :
537 if ((attr->values[j].string.text = _cupsStrAlloc(val)) == NULL)
538 {
539 /*
540 * Ran out of memory!
541 */
542
543 DEBUG_puts("cupsEncodeOptions2: Ran out of memory for string!");
544 return;
545 }
546
547 DEBUG_printf(("cupsEncodeOptions2: Added string value \"%s\"...\n",
548 val));
549 break;
550 }
551 }
552
553 if (copy)
554 free(copy);
555 }
556 }
557
558
559 /*
560 * '_ippFindOption()' - Find the attribute information for an option.
561 */
562
563 _ipp_option_t * /* O - Attribute information */
564 _ippFindOption(const char *name) /* I - Option/attribute name */
565 {
566 _ipp_option_t key; /* Search key */
567
568
569 /*
570 * Lookup the proper value and group tags for this option...
571 */
572
573 key.name = name;
574
575 return ((_ipp_option_t *)bsearch(&key, ipp_options,
576 sizeof(ipp_options) / sizeof(ipp_options[0]),
577 sizeof(ipp_options[0]),
578 (int (*)(const void *, const void *))
579 compare_ipp_options));
580 }
581
582
583 /*
584 * 'compare_ipp_options()' - Compare two IPP options.
585 */
586
587 static int /* O - Result of comparison */
588 compare_ipp_options(_ipp_option_t *a, /* I - First option */
589 _ipp_option_t *b) /* I - Second option */
590 {
591 return (strcmp(a->name, b->name));
592 }
593
594
595 /*
596 * End of "$Id: encode.c 7696 2008-06-26 00:54:42Z mike $".
597 */