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