]> git.ipfire.org Git - thirdparty/cups.git/blob - cups/encode.c
Merge changes from 1.4svn-r7067.
[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 { "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 { "port-monitor", IPP_TAG_NAME, IPP_TAG_PRINTER },
104 { "ppi", IPP_TAG_INTEGER, IPP_TAG_JOB },
105 { "ppi-default", IPP_TAG_INTEGER, IPP_TAG_PRINTER },
106 { "prettyprint", IPP_TAG_BOOLEAN, IPP_TAG_JOB },
107 { "prettyprint-default", IPP_TAG_BOOLEAN, IPP_TAG_PRINTER },
108 { "print-quality", IPP_TAG_ENUM, IPP_TAG_JOB },
109 { "print-quality-default", IPP_TAG_ENUM, IPP_TAG_PRINTER },
110 { "printer-error-policy", IPP_TAG_NAME, IPP_TAG_PRINTER },
111 { "printer-info", IPP_TAG_TEXT, IPP_TAG_PRINTER },
112 { "printer-is-accepting-jobs",IPP_TAG_BOOLEAN, IPP_TAG_PRINTER },
113 { "printer-is-shared", IPP_TAG_BOOLEAN, IPP_TAG_PRINTER },
114 { "printer-location", IPP_TAG_TEXT, IPP_TAG_PRINTER },
115 { "printer-make-and-model", IPP_TAG_TEXT, IPP_TAG_PRINTER },
116 { "printer-more-info", IPP_TAG_URI, IPP_TAG_PRINTER },
117 { "printer-op-policy", IPP_TAG_NAME, IPP_TAG_PRINTER },
118 { "printer-resolution", IPP_TAG_RESOLUTION, IPP_TAG_JOB },
119 { "printer-state", IPP_TAG_ENUM, IPP_TAG_PRINTER },
120 { "printer-state-change-time",IPP_TAG_INTEGER, IPP_TAG_PRINTER },
121 { "printer-state-reasons", IPP_TAG_KEYWORD, IPP_TAG_PRINTER },
122 { "printer-type", IPP_TAG_ENUM, IPP_TAG_PRINTER },
123 { "printer-uri", IPP_TAG_URI, IPP_TAG_OPERATION },
124 { "queued-job-count", IPP_TAG_INTEGER, IPP_TAG_PRINTER },
125 { "raw", IPP_TAG_MIMETYPE, IPP_TAG_OPERATION },
126 { "requesting-user-name-allowed", IPP_TAG_NAME, IPP_TAG_PRINTER },
127 { "requesting-user-name-denied", IPP_TAG_NAME, IPP_TAG_PRINTER },
128 { "resolution", IPP_TAG_RESOLUTION, IPP_TAG_JOB },
129 { "resolution-default", IPP_TAG_RESOLUTION, IPP_TAG_PRINTER },
130 { "saturation", IPP_TAG_INTEGER, IPP_TAG_JOB },
131 { "saturation-default", IPP_TAG_INTEGER, IPP_TAG_PRINTER },
132 { "scaling", IPP_TAG_INTEGER, IPP_TAG_JOB },
133 { "scaling-default", IPP_TAG_INTEGER, IPP_TAG_PRINTER },
134 { "sides", IPP_TAG_KEYWORD, IPP_TAG_JOB },
135 { "sides-default", IPP_TAG_KEYWORD, IPP_TAG_PRINTER },
136 { "wrap", IPP_TAG_BOOLEAN, IPP_TAG_JOB },
137 { "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 ipp_attribute_t *attr; /* IPP attribute */
197 ipp_tag_t value_tag; /* IPP value tag */
198 cups_option_t *option; /* Current option */
199
200
201 DEBUG_printf(("cupsEncodeOptions2(ipp=%p, num_options=%d, options=%p, "
202 "group_tag=%x)\n", ipp, num_options, options, group_tag));
203
204 /*
205 * Range check input...
206 */
207
208 if (!ipp || num_options < 1 || !options)
209 return;
210
211 /*
212 * Do special handling for the document-format/raw options...
213 */
214
215 if (group_tag == IPP_TAG_OPERATION)
216 {
217 /*
218 * Handle the document format stuff first...
219 */
220
221 if ((val = (char *)cupsGetOption("document-format", num_options, options)) != NULL)
222 ippAddString(ipp, IPP_TAG_OPERATION, IPP_TAG_MIMETYPE, "document-format",
223 NULL, val);
224 else if (cupsGetOption("raw", num_options, options))
225 ippAddString(ipp, IPP_TAG_OPERATION, IPP_TAG_MIMETYPE, "document-format",
226 NULL, "application/vnd.cups-raw");
227 else
228 ippAddString(ipp, IPP_TAG_OPERATION, IPP_TAG_MIMETYPE, "document-format",
229 NULL, "application/octet-stream");
230 }
231
232 /*
233 * Then loop through the options...
234 */
235
236 for (i = num_options, option = options; i > 0; i --, option ++)
237 {
238 _ipp_option_t *match; /* Matching attribute */
239
240
241 /*
242 * Skip document format options that are handled above...
243 */
244
245 if (!strcasecmp(option->name, "raw") ||
246 !strcasecmp(option->name, "document-format") ||
247 !option->name[0])
248 continue;
249
250 /*
251 * Figure out the proper value and group tags for this option...
252 */
253
254 if ((match = _ippFindOption(option->name)) != NULL)
255 {
256 if (match->group_tag != group_tag)
257 continue;
258
259 value_tag = match->value_tag;
260 }
261 else
262 {
263 int namelen; /* Length of name */
264
265
266 namelen = (int)strlen(option->name);
267
268 if (namelen < 9 || strcmp(option->name + namelen - 8, "-default"))
269 {
270 if (group_tag != IPP_TAG_JOB)
271 continue;
272 }
273 else if (group_tag != IPP_TAG_PRINTER)
274 continue;
275
276 if (!strcasecmp(option->value, "true") ||
277 !strcasecmp(option->value, "false"))
278 value_tag = IPP_TAG_BOOLEAN;
279 else
280 value_tag = IPP_TAG_NAME;
281 }
282
283 /*
284 * Count the number of values...
285 */
286
287 for (count = 1, sep = option->value; *sep; sep ++)
288 {
289 if (*sep == '\'')
290 {
291 /*
292 * Skip quoted option value...
293 */
294
295 sep ++;
296
297 while (*sep && *sep != '\'')
298 sep ++;
299
300 if (!*sep)
301 sep --;
302 }
303 else if (*sep == '\"')
304 {
305 /*
306 * Skip quoted option value...
307 */
308
309 sep ++;
310
311 while (*sep && *sep != '\"')
312 sep ++;
313
314 if (!*sep)
315 sep --;
316 }
317 else if (*sep == ',')
318 count ++;
319 else if (*sep == '\\' && sep[1])
320 sep ++;
321 }
322
323 DEBUG_printf(("cupsEncodeOptions2: option = \'%s\', count = %d\n",
324 option->name, count));
325
326 /*
327 * Allocate memory for the attribute values...
328 */
329
330 if ((attr = _ippAddAttr(ipp, count)) == NULL)
331 {
332 /*
333 * Ran out of memory!
334 */
335
336 DEBUG_puts("cupsEncodeOptions2: Ran out of memory for attributes!");
337 return;
338 }
339
340 /*
341 * Now figure out what type of value we have...
342 */
343
344 attr->group_tag = group_tag;
345 attr->value_tag = value_tag;
346
347 /*
348 * Copy the name over...
349 */
350
351 if ((attr->name = _cupsStrAlloc(option->name)) == NULL)
352 {
353 /*
354 * Ran out of memory!
355 */
356
357 DEBUG_puts("cupsEncodeOptions2: Ran out of memory for name!");
358 return;
359 }
360
361 if (count > 1)
362 {
363 /*
364 * Make a copy of the value we can fiddle with...
365 */
366
367 if ((copy = strdup(option->value)) == NULL)
368 {
369 /*
370 * Ran out of memory!
371 */
372
373 DEBUG_puts("cupsEncodeOptions2: Ran out of memory for value copy!");
374 return;
375 }
376
377 val = copy;
378 }
379 else
380 {
381 /*
382 * Since we have a single value, use the value directly...
383 */
384
385 val = option->value;
386 copy = NULL;
387 }
388
389 /*
390 * Scan the value string for values...
391 */
392
393 for (j = 0; j < count; val = sep, j ++)
394 {
395 /*
396 * Find the end of this value and mark it if needed...
397 */
398
399 if ((sep = strchr(val, ',')) != NULL)
400 *sep++ = '\0';
401 else
402 sep = val + strlen(val);
403
404 /*
405 * Copy the option value(s) over as needed by the type...
406 */
407
408 switch (attr->value_tag)
409 {
410 case IPP_TAG_INTEGER :
411 case IPP_TAG_ENUM :
412 /*
413 * Integer/enumeration value...
414 */
415
416 attr->values[j].integer = strtol(val, &s, 0);
417
418 DEBUG_printf(("cupsEncodeOptions2: Added integer option value %d...\n",
419 attr->values[j].integer));
420 break;
421
422 case IPP_TAG_BOOLEAN :
423 if (!strcasecmp(val, "true") ||
424 !strcasecmp(val, "on") ||
425 !strcasecmp(val, "yes"))
426 {
427 /*
428 * Boolean value - true...
429 */
430
431 attr->values[j].boolean = 1;
432
433 DEBUG_puts("cupsEncodeOptions2: Added boolean true value...");
434 }
435 else
436 {
437 /*
438 * Boolean value - false...
439 */
440
441 attr->values[j].boolean = 0;
442
443 DEBUG_puts("cupsEncodeOptions2: Added boolean false value...");
444 }
445 break;
446
447 case IPP_TAG_RANGE :
448 /*
449 * Range...
450 */
451
452 if (*val == '-')
453 {
454 attr->values[j].range.lower = 1;
455 s = val;
456 }
457 else
458 attr->values[j].range.lower = strtol(val, &s, 0);
459
460 if (*s == '-')
461 {
462 if (s[1])
463 attr->values[j].range.upper = strtol(s + 1, NULL, 0);
464 else
465 attr->values[j].range.upper = 2147483647;
466 }
467 else
468 attr->values[j].range.upper = attr->values[j].range.lower;
469
470 DEBUG_printf(("cupsEncodeOptions2: Added range option value %d-%d...\n",
471 attr->values[j].range.lower,
472 attr->values[j].range.upper));
473 break;
474
475 case IPP_TAG_RESOLUTION :
476 /*
477 * Resolution...
478 */
479
480 attr->values[j].resolution.xres = strtol(val, &s, 0);
481
482 if (*s == 'x')
483 attr->values[j].resolution.yres = strtol(s + 1, &s, 0);
484 else
485 attr->values[j].resolution.yres = attr->values[j].resolution.xres;
486
487 if (!strcasecmp(s, "dpc"))
488 attr->values[j].resolution.units = IPP_RES_PER_CM;
489 else
490 attr->values[j].resolution.units = IPP_RES_PER_INCH;
491
492 DEBUG_printf(("cupsEncodeOptions2: Added resolution option value %s...\n",
493 val));
494 break;
495
496 case IPP_TAG_STRING :
497 /*
498 * octet-string
499 */
500
501 attr->values[j].unknown.length = (int)strlen(val);
502 attr->values[j].unknown.data = _cupsStrAlloc(val);
503
504 DEBUG_printf(("cupsEncodeOptions2: Added octet-string value \"%s\"...\n",
505 attr->values[j].unknown.data));
506 break;
507
508 default :
509 if ((attr->values[j].string.text = _cupsStrAlloc(val)) == NULL)
510 {
511 /*
512 * Ran out of memory!
513 */
514
515 DEBUG_puts("cupsEncodeOptions2: Ran out of memory for string!");
516 return;
517 }
518
519 DEBUG_printf(("cupsEncodeOptions2: Added string value \"%s\"...\n",
520 val));
521 break;
522 }
523 }
524
525 if (copy)
526 free(copy);
527 }
528 }
529
530
531 /*
532 * '_ippFindOption()' - Find the attribute information for an option.
533 */
534
535 _ipp_option_t * /* O - Attribute information */
536 _ippFindOption(const char *name) /* I - Option/attribute name */
537 {
538 _ipp_option_t key; /* Search key */
539
540
541 /*
542 * Lookup the proper value and group tags for this option...
543 */
544
545 key.name = name;
546
547 return ((_ipp_option_t *)bsearch(&key, ipp_options,
548 sizeof(ipp_options) / sizeof(ipp_options[0]),
549 sizeof(ipp_options[0]),
550 (int (*)(const void *, const void *))
551 compare_ipp_options));
552 }
553
554
555 /*
556 * 'compare_ipp_options()' - Compare two IPP options.
557 */
558
559 static int /* O - Result of comparison */
560 compare_ipp_options(_ipp_option_t *a, /* I - First option */
561 _ipp_option_t *b) /* I - Second option */
562 {
563 return (strcmp(a->name, b->name));
564 }
565
566
567 /*
568 * End of "$Id: encode.c 6649 2007-07-11 21:46:42Z mike $".
569 */