]> 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/*
f7deaa1a 2 * "$Id: encode.c 6267 2007-02-11 23:24:22Z 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{
55 { "blackplot", IPP_TAG_BOOLEAN, IPP_TAG_JOB },
b423cd4c 56 { "blackplot-default", IPP_TAG_BOOLEAN, IPP_TAG_PRINTER },
ef416fc2 57 { "brightness", IPP_TAG_INTEGER, IPP_TAG_JOB },
b423cd4c 58 { "brightness-default", IPP_TAG_INTEGER, IPP_TAG_PRINTER },
ef416fc2 59 { "columns", IPP_TAG_INTEGER, IPP_TAG_JOB },
b423cd4c 60 { "columns-default", IPP_TAG_INTEGER, IPP_TAG_PRINTER },
ef416fc2 61 { "copies", IPP_TAG_INTEGER, IPP_TAG_JOB },
b423cd4c 62 { "copies-default", IPP_TAG_INTEGER, IPP_TAG_PRINTER },
ef416fc2 63 { "document-format", IPP_TAG_MIMETYPE, IPP_TAG_OPERATION },
f7deaa1a 64 { "document-format-default", IPP_TAG_MIMETYPE, IPP_TAG_PRINTER },
ef416fc2 65 { "finishings", IPP_TAG_ENUM, IPP_TAG_JOB },
b423cd4c 66 { "finishings-default", IPP_TAG_ENUM, IPP_TAG_PRINTER },
ef416fc2 67 { "fitplot", IPP_TAG_BOOLEAN, IPP_TAG_JOB },
b423cd4c 68 { "fitplot-default", IPP_TAG_BOOLEAN, IPP_TAG_PRINTER },
ef416fc2 69 { "gamma", IPP_TAG_INTEGER, IPP_TAG_JOB },
b423cd4c 70 { "gamma-default", IPP_TAG_INTEGER, IPP_TAG_PRINTER },
ef416fc2 71 { "hue", IPP_TAG_INTEGER, IPP_TAG_JOB },
b423cd4c 72 { "hue-default", IPP_TAG_INTEGER, IPP_TAG_PRINTER },
ef416fc2 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 },
bd7854cb 77 { "job-uuid", IPP_TAG_URI, IPP_TAG_JOB },
ef416fc2 78 { "landscape", IPP_TAG_BOOLEAN, IPP_TAG_JOB },
79 { "media", IPP_TAG_KEYWORD, IPP_TAG_JOB },
80 { "mirror", IPP_TAG_BOOLEAN, IPP_TAG_JOB },
b423cd4c 81 { "mirror-default", IPP_TAG_BOOLEAN, IPP_TAG_PRINTER },
ef416fc2 82 { "natural-scaling", IPP_TAG_INTEGER, IPP_TAG_JOB },
b423cd4c 83 { "natural-scaling-default", IPP_TAG_INTEGER, IPP_TAG_PRINTER },
ef416fc2 84 { "notify-charset", IPP_TAG_CHARSET, IPP_TAG_SUBSCRIPTION },
85 { "notify-events", IPP_TAG_KEYWORD, IPP_TAG_SUBSCRIPTION },
f7deaa1a 86 { "notify-events-default", IPP_TAG_KEYWORD, IPP_TAG_PRINTER },
b86bc4cf 87 { "notify-lease-duration", IPP_TAG_INTEGER, IPP_TAG_SUBSCRIPTION },
f7deaa1a 88 { "notify-lease-duration-default", IPP_TAG_INTEGER, IPP_TAG_PRINTER },
ef416fc2 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 },
b423cd4c 95 { "number-up-default", IPP_TAG_INTEGER, IPP_TAG_PRINTER },
ef416fc2 96 { "orientation-requested", IPP_TAG_ENUM, IPP_TAG_JOB },
b423cd4c 97 { "orientation-requested-default", IPP_TAG_ENUM, IPP_TAG_PRINTER },
ef416fc2 98 { "page-bottom", IPP_TAG_INTEGER, IPP_TAG_JOB },
b423cd4c 99 { "page-bottom-default", IPP_TAG_INTEGER, IPP_TAG_PRINTER },
ef416fc2 100 { "page-left", IPP_TAG_INTEGER, IPP_TAG_JOB },
b423cd4c 101 { "page-left-default", IPP_TAG_INTEGER, IPP_TAG_PRINTER },
ef416fc2 102 { "page-ranges", IPP_TAG_RANGE, IPP_TAG_JOB },
b423cd4c 103 { "page-ranges-default", IPP_TAG_RANGE, IPP_TAG_PRINTER },
ef416fc2 104 { "page-right", IPP_TAG_INTEGER, IPP_TAG_JOB },
b423cd4c 105 { "page-right-default", IPP_TAG_INTEGER, IPP_TAG_PRINTER },
ef416fc2 106 { "page-top", IPP_TAG_INTEGER, IPP_TAG_JOB },
b423cd4c 107 { "page-top-default", IPP_TAG_INTEGER, IPP_TAG_PRINTER },
ef416fc2 108 { "penwidth", IPP_TAG_INTEGER, IPP_TAG_JOB },
b423cd4c 109 { "penwidth-default", IPP_TAG_INTEGER, IPP_TAG_PRINTER },
ef416fc2 110 { "ppi", IPP_TAG_INTEGER, IPP_TAG_JOB },
b423cd4c 111 { "ppi-default", IPP_TAG_INTEGER, IPP_TAG_PRINTER },
ef416fc2 112 { "prettyprint", IPP_TAG_BOOLEAN, IPP_TAG_JOB },
b423cd4c 113 { "prettyprint-default", IPP_TAG_BOOLEAN, IPP_TAG_PRINTER },
8ca02f3c 114 { "print-quality", IPP_TAG_ENUM, IPP_TAG_JOB },
115 { "print-quality-default", IPP_TAG_ENUM, IPP_TAG_PRINTER },
80ca4592 116 { "printer-error-policy", IPP_TAG_NAME, IPP_TAG_PRINTER },
fa73b229 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 },
80ca4592 122 { "printer-op-policy", IPP_TAG_NAME, IPP_TAG_PRINTER },
ef416fc2 123 { "printer-resolution", IPP_TAG_RESOLUTION, IPP_TAG_JOB },
fa73b229 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 },
ef416fc2 128 { "printer-uri", IPP_TAG_URI, IPP_TAG_OPERATION },
fa73b229 129 { "queued-job-count", IPP_TAG_INTEGER, IPP_TAG_PRINTER },
ef416fc2 130 { "raw", IPP_TAG_MIMETYPE, IPP_TAG_OPERATION },
f301802f 131 { "requesting-user-name-allowed", IPP_TAG_NAME, IPP_TAG_PRINTER },
132 { "requesting-user-name-denied", IPP_TAG_NAME, IPP_TAG_PRINTER },
8ca02f3c 133 { "resolution", IPP_TAG_RESOLUTION, IPP_TAG_JOB },
134 { "resolution-default", IPP_TAG_RESOLUTION, IPP_TAG_PRINTER },
ef416fc2 135 { "saturation", IPP_TAG_INTEGER, IPP_TAG_JOB },
b423cd4c 136 { "saturation-default", IPP_TAG_INTEGER, IPP_TAG_PRINTER },
ef416fc2 137 { "scaling", IPP_TAG_INTEGER, IPP_TAG_JOB },
b423cd4c 138 { "scaling-default", IPP_TAG_INTEGER, IPP_TAG_PRINTER },
ef416fc2 139 { "sides", IPP_TAG_KEYWORD, IPP_TAG_JOB },
b423cd4c 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 }
ef416fc2 143};
144
145
fa73b229 146/*
147 * Local functions...
148 */
149
150static int compare_ipp_options(_ipp_option_t *a, _ipp_option_t *b);
151
152
ef416fc2 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
161void
162cupsEncodeOptions(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
188void
189cupsEncodeOptions2(
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 */
b423cd4c 203 cups_option_t *option; /* Current option */
ef416fc2 204
205
b423cd4c 206 DEBUG_printf(("cupsEncodeOptions2(ipp=%p, num_options=%d, options=%p, "
207 "group_tag=%x)\n", ipp, num_options, options, group_tag));
ef416fc2 208
209 /*
210 * Range check input...
211 */
212
b423cd4c 213 if (!ipp || num_options < 1 || !options)
ef416fc2 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
b423cd4c 241 for (i = num_options, option = options; i > 0; i --, option ++)
ef416fc2 242 {
8ca02f3c 243 _ipp_option_t *match; /* Matching attribute */
fa73b229 244
245
ef416fc2 246 /*
247 * Skip document format options that are handled above...
248 */
249
b423cd4c 250 if (!strcasecmp(option->name, "raw") ||
251 !strcasecmp(option->name, "document-format") ||
252 !option->name[0])
ef416fc2 253 continue;
254
255 /*
256 * Figure out the proper value and group tags for this option...
257 */
258
8ca02f3c 259 if ((match = _ippFindOption(option->name)) != NULL)
ef416fc2 260 {
fa73b229 261 if (match->group_tag != group_tag)
ef416fc2 262 continue;
263
fa73b229 264 value_tag = match->value_tag;
ef416fc2 265 }
ef416fc2 266 else
b423cd4c 267 {
268 int namelen; /* Length of name */
269
270
b86bc4cf 271 namelen = (int)strlen(option->name);
b423cd4c 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 }
ef416fc2 287
288 /*
289 * Count the number of values...
290 */
291
b423cd4c 292 for (count = 1, sep = option->value; *sep; sep ++)
ef416fc2 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",
b423cd4c 329 option->name, count));
ef416fc2 330
331 /*
332 * Allocate memory for the attribute values...
333 */
334
757d2cad 335 if ((attr = _ippAddAttr(ipp, count)) == NULL)
ef416fc2 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
e53920b9 356 if ((attr->name = _cupsStrAlloc(option->name)) == NULL)
ef416fc2 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
b423cd4c 372 if ((copy = strdup(option->value)) == NULL)
ef416fc2 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
b423cd4c 390 val = option->value;
ef416fc2 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
b423cd4c 492 if (!strcasecmp(s, "dpc"))
ef416fc2 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
b86bc4cf 506 attr->values[j].unknown.length = (int)strlen(val);
e53920b9 507 attr->values[j].unknown.data = _cupsStrAlloc(val);
ef416fc2 508
509 DEBUG_printf(("cupsEncodeOptions2: Added octet-string value \"%s\"...\n",
510 attr->values[j].unknown.data));
511 break;
512
513 default :
e53920b9 514 if ((attr->values[j].string.text = _cupsStrAlloc(val)) == NULL)
ef416fc2 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 }
e53920b9 529
530 if (copy)
531 free(copy);
ef416fc2 532 }
533}
534
535
8ca02f3c 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
ef416fc2 560/*
fa73b229 561 * 'compare_ipp_options()' - Compare two IPP options.
562 */
563
564static int /* O - Result of comparison */
565compare_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/*
f7deaa1a 573 * End of "$Id: encode.c 6267 2007-02-11 23:24:22Z mike $".
ef416fc2 574 */