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