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