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