]> git.ipfire.org Git - thirdparty/cups.git/blame - cups/encode.c
Load cups into easysw/current.
[thirdparty/cups.git] / cups / encode.c
CommitLineData
ef416fc2 1/*
f42414bf 2 * "$Id: encode.c 6386 2007-03-23 19:03:01Z mike $"
ef416fc2 3 *
4 * Option encoding routines for the Common UNIX Printing System (CUPS).
5 *
b86bc4cf 6 * Copyright 1997-2007 by Easy Software Products.
ef416fc2 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 *
fa73b229 28 * cupsEncodeOptions() - Encode printer options into IPP attributes.
29 * cupsEncodeOptions2() - Encode printer options into IPP attributes for
30 * a group.
8ca02f3c 31 * _ippFindOption() - Find the attribute information for an option.
fa73b229 32 * compare_ipp_options() - Compare two IPP options.
ef416fc2 33 */
34
35/*
36 * Include necessary headers...
37 */
38
39#include "cups.h"
fa73b229 40#include "ipp-private.h"
ef416fc2 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...
fa73b229 49 *
50 * **** THIS LIST MUST BE SORTED ****
ef416fc2 51 */
52
ef416fc2 53static const _ipp_option_t ipp_options[] =
54{
f42414bf 55 { "auth-info", IPP_TAG_TEXT, IPP_TAG_JOB },
56 { "auth-info-required", IPP_TAG_KEYWORD, IPP_TAG_PRINTER },
ef416fc2 57 { "blackplot", IPP_TAG_BOOLEAN, IPP_TAG_JOB },
b423cd4c 58 { "blackplot-default", IPP_TAG_BOOLEAN, IPP_TAG_PRINTER },
ef416fc2 59 { "brightness", IPP_TAG_INTEGER, IPP_TAG_JOB },
b423cd4c 60 { "brightness-default", IPP_TAG_INTEGER, IPP_TAG_PRINTER },
ef416fc2 61 { "columns", IPP_TAG_INTEGER, IPP_TAG_JOB },
b423cd4c 62 { "columns-default", IPP_TAG_INTEGER, IPP_TAG_PRINTER },
ef416fc2 63 { "copies", IPP_TAG_INTEGER, IPP_TAG_JOB },
b423cd4c 64 { "copies-default", IPP_TAG_INTEGER, IPP_TAG_PRINTER },
ef416fc2 65 { "document-format", IPP_TAG_MIMETYPE, IPP_TAG_OPERATION },
f7deaa1a 66 { "document-format-default", IPP_TAG_MIMETYPE, IPP_TAG_PRINTER },
ef416fc2 67 { "finishings", IPP_TAG_ENUM, IPP_TAG_JOB },
b423cd4c 68 { "finishings-default", IPP_TAG_ENUM, IPP_TAG_PRINTER },
ef416fc2 69 { "fitplot", IPP_TAG_BOOLEAN, IPP_TAG_JOB },
b423cd4c 70 { "fitplot-default", IPP_TAG_BOOLEAN, IPP_TAG_PRINTER },
ef416fc2 71 { "gamma", IPP_TAG_INTEGER, IPP_TAG_JOB },
b423cd4c 72 { "gamma-default", IPP_TAG_INTEGER, IPP_TAG_PRINTER },
ef416fc2 73 { "hue", IPP_TAG_INTEGER, IPP_TAG_JOB },
b423cd4c 74 { "hue-default", IPP_TAG_INTEGER, IPP_TAG_PRINTER },
ef416fc2 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 },
bd7854cb 79 { "job-uuid", IPP_TAG_URI, IPP_TAG_JOB },
ef416fc2 80 { "landscape", IPP_TAG_BOOLEAN, IPP_TAG_JOB },
81 { "media", IPP_TAG_KEYWORD, IPP_TAG_JOB },
82 { "mirror", IPP_TAG_BOOLEAN, IPP_TAG_JOB },
b423cd4c 83 { "mirror-default", IPP_TAG_BOOLEAN, IPP_TAG_PRINTER },
ef416fc2 84 { "natural-scaling", IPP_TAG_INTEGER, IPP_TAG_JOB },
b423cd4c 85 { "natural-scaling-default", IPP_TAG_INTEGER, IPP_TAG_PRINTER },
ef416fc2 86 { "notify-charset", IPP_TAG_CHARSET, IPP_TAG_SUBSCRIPTION },
87 { "notify-events", IPP_TAG_KEYWORD, IPP_TAG_SUBSCRIPTION },
f7deaa1a 88 { "notify-events-default", IPP_TAG_KEYWORD, IPP_TAG_PRINTER },
b86bc4cf 89 { "notify-lease-duration", IPP_TAG_INTEGER, IPP_TAG_SUBSCRIPTION },
f7deaa1a 90 { "notify-lease-duration-default", IPP_TAG_INTEGER, IPP_TAG_PRINTER },
ef416fc2 91 { "notify-natural-language", IPP_TAG_LANGUAGE, IPP_TAG_SUBSCRIPTION },
92 { "notify-pull-method", IPP_TAG_KEYWORD, IPP_TAG_SUBSCRIPTION },
7594b224 93 { "notify-recipient-uri", IPP_TAG_URI, IPP_TAG_SUBSCRIPTION },
ef416fc2 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 },
b423cd4c 97 { "number-up-default", IPP_TAG_INTEGER, IPP_TAG_PRINTER },
ef416fc2 98 { "orientation-requested", IPP_TAG_ENUM, IPP_TAG_JOB },
b423cd4c 99 { "orientation-requested-default", IPP_TAG_ENUM, IPP_TAG_PRINTER },
ef416fc2 100 { "page-bottom", IPP_TAG_INTEGER, IPP_TAG_JOB },
b423cd4c 101 { "page-bottom-default", IPP_TAG_INTEGER, IPP_TAG_PRINTER },
ef416fc2 102 { "page-left", IPP_TAG_INTEGER, IPP_TAG_JOB },
b423cd4c 103 { "page-left-default", IPP_TAG_INTEGER, IPP_TAG_PRINTER },
ef416fc2 104 { "page-ranges", IPP_TAG_RANGE, IPP_TAG_JOB },
b423cd4c 105 { "page-ranges-default", IPP_TAG_RANGE, IPP_TAG_PRINTER },
ef416fc2 106 { "page-right", IPP_TAG_INTEGER, IPP_TAG_JOB },
b423cd4c 107 { "page-right-default", IPP_TAG_INTEGER, IPP_TAG_PRINTER },
ef416fc2 108 { "page-top", IPP_TAG_INTEGER, IPP_TAG_JOB },
b423cd4c 109 { "page-top-default", IPP_TAG_INTEGER, IPP_TAG_PRINTER },
ef416fc2 110 { "penwidth", IPP_TAG_INTEGER, IPP_TAG_JOB },
b423cd4c 111 { "penwidth-default", IPP_TAG_INTEGER, IPP_TAG_PRINTER },
ef416fc2 112 { "ppi", IPP_TAG_INTEGER, IPP_TAG_JOB },
b423cd4c 113 { "ppi-default", IPP_TAG_INTEGER, IPP_TAG_PRINTER },
ef416fc2 114 { "prettyprint", IPP_TAG_BOOLEAN, IPP_TAG_JOB },
b423cd4c 115 { "prettyprint-default", IPP_TAG_BOOLEAN, IPP_TAG_PRINTER },
8ca02f3c 116 { "print-quality", IPP_TAG_ENUM, IPP_TAG_JOB },
117 { "print-quality-default", IPP_TAG_ENUM, IPP_TAG_PRINTER },
80ca4592 118 { "printer-error-policy", IPP_TAG_NAME, IPP_TAG_PRINTER },
fa73b229 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 },
f42414bf 122 { "printer-location", IPP_TAG_TEXT, IPP_TAG_PRINTER },
fa73b229 123 { "printer-make-and-model", IPP_TAG_TEXT, IPP_TAG_PRINTER },
124 { "printer-more-info", IPP_TAG_URI, IPP_TAG_PRINTER },
80ca4592 125 { "printer-op-policy", IPP_TAG_NAME, IPP_TAG_PRINTER },
ef416fc2 126 { "printer-resolution", IPP_TAG_RESOLUTION, IPP_TAG_JOB },
fa73b229 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 },
ef416fc2 131 { "printer-uri", IPP_TAG_URI, IPP_TAG_OPERATION },
fa73b229 132 { "queued-job-count", IPP_TAG_INTEGER, IPP_TAG_PRINTER },
ef416fc2 133 { "raw", IPP_TAG_MIMETYPE, IPP_TAG_OPERATION },
f301802f 134 { "requesting-user-name-allowed", IPP_TAG_NAME, IPP_TAG_PRINTER },
135 { "requesting-user-name-denied", IPP_TAG_NAME, IPP_TAG_PRINTER },
8ca02f3c 136 { "resolution", IPP_TAG_RESOLUTION, IPP_TAG_JOB },
137 { "resolution-default", IPP_TAG_RESOLUTION, IPP_TAG_PRINTER },
ef416fc2 138 { "saturation", IPP_TAG_INTEGER, IPP_TAG_JOB },
b423cd4c 139 { "saturation-default", IPP_TAG_INTEGER, IPP_TAG_PRINTER },
ef416fc2 140 { "scaling", IPP_TAG_INTEGER, IPP_TAG_JOB },
b423cd4c 141 { "scaling-default", IPP_TAG_INTEGER, IPP_TAG_PRINTER },
ef416fc2 142 { "sides", IPP_TAG_KEYWORD, IPP_TAG_JOB },
b423cd4c 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 }
ef416fc2 146};
147
148
fa73b229 149/*
150 * Local functions...
151 */
152
153static int compare_ipp_options(_ipp_option_t *a, _ipp_option_t *b);
154
155
ef416fc2 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
164void
165cupsEncodeOptions(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
191void
192cupsEncodeOptions2(
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 */
b423cd4c 206 cups_option_t *option; /* Current option */
ef416fc2 207
208
b423cd4c 209 DEBUG_printf(("cupsEncodeOptions2(ipp=%p, num_options=%d, options=%p, "
210 "group_tag=%x)\n", ipp, num_options, options, group_tag));
ef416fc2 211
212 /*
213 * Range check input...
214 */
215
b423cd4c 216 if (!ipp || num_options < 1 || !options)
ef416fc2 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
b423cd4c 244 for (i = num_options, option = options; i > 0; i --, option ++)
ef416fc2 245 {
8ca02f3c 246 _ipp_option_t *match; /* Matching attribute */
fa73b229 247
248
ef416fc2 249 /*
250 * Skip document format options that are handled above...
251 */
252
b423cd4c 253 if (!strcasecmp(option->name, "raw") ||
254 !strcasecmp(option->name, "document-format") ||
255 !option->name[0])
ef416fc2 256 continue;
257
258 /*
259 * Figure out the proper value and group tags for this option...
260 */
261
8ca02f3c 262 if ((match = _ippFindOption(option->name)) != NULL)
ef416fc2 263 {
fa73b229 264 if (match->group_tag != group_tag)
ef416fc2 265 continue;
266
fa73b229 267 value_tag = match->value_tag;
ef416fc2 268 }
ef416fc2 269 else
b423cd4c 270 {
271 int namelen; /* Length of name */
272
273
b86bc4cf 274 namelen = (int)strlen(option->name);
b423cd4c 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 }
ef416fc2 290
291 /*
292 * Count the number of values...
293 */
294
b423cd4c 295 for (count = 1, sep = option->value; *sep; sep ++)
ef416fc2 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",
b423cd4c 332 option->name, count));
ef416fc2 333
334 /*
335 * Allocate memory for the attribute values...
336 */
337
757d2cad 338 if ((attr = _ippAddAttr(ipp, count)) == NULL)
ef416fc2 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
e53920b9 359 if ((attr->name = _cupsStrAlloc(option->name)) == NULL)
ef416fc2 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
b423cd4c 375 if ((copy = strdup(option->value)) == NULL)
ef416fc2 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
b423cd4c 393 val = option->value;
ef416fc2 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
b423cd4c 495 if (!strcasecmp(s, "dpc"))
ef416fc2 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
b86bc4cf 509 attr->values[j].unknown.length = (int)strlen(val);
e53920b9 510 attr->values[j].unknown.data = _cupsStrAlloc(val);
ef416fc2 511
512 DEBUG_printf(("cupsEncodeOptions2: Added octet-string value \"%s\"...\n",
513 attr->values[j].unknown.data));
514 break;
515
516 default :
e53920b9 517 if ((attr->values[j].string.text = _cupsStrAlloc(val)) == NULL)
ef416fc2 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 }
e53920b9 532
533 if (copy)
534 free(copy);
ef416fc2 535 }
536}
537
538
8ca02f3c 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
ef416fc2 563/*
fa73b229 564 * 'compare_ipp_options()' - Compare two IPP options.
565 */
566
567static int /* O - Result of comparison */
568compare_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/*
f42414bf 576 * End of "$Id: encode.c 6386 2007-03-23 19:03:01Z mike $".
ef416fc2 577 */