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