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