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