]> 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 5545 2006-05-18 21:00:56Z 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 { "finishings", IPP_TAG_ENUM, IPP_TAG_JOB },
71 { "finishings-default", IPP_TAG_ENUM, IPP_TAG_PRINTER },
72 { "fitplot", IPP_TAG_BOOLEAN, IPP_TAG_JOB },
73 { "fitplot-default", IPP_TAG_BOOLEAN, IPP_TAG_PRINTER },
74 { "gamma", IPP_TAG_INTEGER, IPP_TAG_JOB },
75 { "gamma-default", IPP_TAG_INTEGER, IPP_TAG_PRINTER },
76 { "hue", IPP_TAG_INTEGER, IPP_TAG_JOB },
77 { "hue-default", IPP_TAG_INTEGER, IPP_TAG_PRINTER },
78 { "job-k-limit", IPP_TAG_INTEGER, IPP_TAG_JOB },
79 { "job-page-limit", IPP_TAG_INTEGER, IPP_TAG_JOB },
80 { "job-priority", IPP_TAG_INTEGER, IPP_TAG_JOB },
81 { "job-quota-period", IPP_TAG_INTEGER, IPP_TAG_JOB },
82 { "job-uuid", IPP_TAG_URI, IPP_TAG_JOB },
83 { "landscape", IPP_TAG_BOOLEAN, IPP_TAG_JOB },
84 { "media", IPP_TAG_KEYWORD, IPP_TAG_JOB },
85 { "mirror", IPP_TAG_BOOLEAN, IPP_TAG_JOB },
86 { "mirror-default", IPP_TAG_BOOLEAN, IPP_TAG_PRINTER },
87 { "natural-scaling", IPP_TAG_INTEGER, IPP_TAG_JOB },
88 { "natural-scaling-default", IPP_TAG_INTEGER, IPP_TAG_PRINTER },
89 { "notify-charset", IPP_TAG_CHARSET, IPP_TAG_SUBSCRIPTION },
90 { "notify-events", IPP_TAG_KEYWORD, IPP_TAG_SUBSCRIPTION },
91 { "notify-lease-time", IPP_TAG_INTEGER, IPP_TAG_SUBSCRIPTION },
92 { "notify-natural-language", IPP_TAG_LANGUAGE, IPP_TAG_SUBSCRIPTION },
93 { "notify-pull-method", IPP_TAG_KEYWORD, IPP_TAG_SUBSCRIPTION },
94 { "notify-recipient", IPP_TAG_URI, IPP_TAG_SUBSCRIPTION },
95 { "notify-time-interval", IPP_TAG_INTEGER, IPP_TAG_SUBSCRIPTION },
96 { "notify-user-data", IPP_TAG_STRING, IPP_TAG_SUBSCRIPTION },
97 { "number-up", IPP_TAG_INTEGER, IPP_TAG_JOB },
98 { "number-up-default", IPP_TAG_INTEGER, IPP_TAG_PRINTER },
99 { "orientation-requested", IPP_TAG_ENUM, IPP_TAG_JOB },
100 { "orientation-requested-default", IPP_TAG_ENUM, IPP_TAG_PRINTER },
101 { "page-bottom", IPP_TAG_INTEGER, IPP_TAG_JOB },
102 { "page-bottom-default", IPP_TAG_INTEGER, IPP_TAG_PRINTER },
103 { "page-left", IPP_TAG_INTEGER, IPP_TAG_JOB },
104 { "page-left-default", IPP_TAG_INTEGER, IPP_TAG_PRINTER },
105 { "page-ranges", IPP_TAG_RANGE, IPP_TAG_JOB },
106 { "page-ranges-default", IPP_TAG_RANGE, IPP_TAG_PRINTER },
107 { "page-right", IPP_TAG_INTEGER, IPP_TAG_JOB },
108 { "page-right-default", IPP_TAG_INTEGER, IPP_TAG_PRINTER },
109 { "page-top", IPP_TAG_INTEGER, IPP_TAG_JOB },
110 { "page-top-default", IPP_TAG_INTEGER, IPP_TAG_PRINTER },
111 { "penwidth", IPP_TAG_INTEGER, IPP_TAG_JOB },
112 { "penwidth-default", IPP_TAG_INTEGER, IPP_TAG_PRINTER },
113 { "ppi", IPP_TAG_INTEGER, IPP_TAG_JOB },
114 { "ppi-default", IPP_TAG_INTEGER, IPP_TAG_PRINTER },
115 { "prettyprint", IPP_TAG_BOOLEAN, IPP_TAG_JOB },
116 { "prettyprint-default", IPP_TAG_BOOLEAN, IPP_TAG_PRINTER },
117 { "printer-error-policy", IPP_TAG_NAME, IPP_TAG_PRINTER },
118 { "printer-info", IPP_TAG_TEXT, IPP_TAG_PRINTER },
119 { "printer-is-accepting-jobs",IPP_TAG_BOOLEAN, IPP_TAG_PRINTER },
120 { "printer-is-shared", IPP_TAG_BOOLEAN, IPP_TAG_PRINTER },
121 { "printer-make-and-model", IPP_TAG_TEXT, IPP_TAG_PRINTER },
122 { "printer-more-info", IPP_TAG_URI, IPP_TAG_PRINTER },
123 { "printer-op-policy", IPP_TAG_NAME, IPP_TAG_PRINTER },
124 { "printer-resolution", IPP_TAG_RESOLUTION, IPP_TAG_JOB },
125 { "printer-state", IPP_TAG_ENUM, IPP_TAG_PRINTER },
126 { "printer-state-change-time",IPP_TAG_INTEGER, IPP_TAG_PRINTER },
127 { "printer-state-reasons", IPP_TAG_KEYWORD, IPP_TAG_PRINTER },
128 { "printer-type", IPP_TAG_ENUM, IPP_TAG_PRINTER },
129 { "printer-uri", IPP_TAG_URI, IPP_TAG_OPERATION },
130 { "print-quality", IPP_TAG_ENUM, IPP_TAG_JOB },
131 { "print-quality-default", IPP_TAG_ENUM, IPP_TAG_PRINTER },
132 { "queued-job-count", IPP_TAG_INTEGER, IPP_TAG_PRINTER },
133 { "raw", IPP_TAG_MIMETYPE, IPP_TAG_OPERATION },
134 { "resolution", IPP_TAG_RESOLUTION, IPP_TAG_JOB },
135 { "resolution-default", IPP_TAG_RESOLUTION, IPP_TAG_PRINTER },
136 { "requesting-user-name-allowed", IPP_TAG_NAME, IPP_TAG_PRINTER },
137 { "requesting-user-name-denied", IPP_TAG_NAME, 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 key, /* Search key */
247 *match; /* Matching attribute */
248
249
250 /*
251 * Skip document format options that are handled above...
252 */
253
254 if (!strcasecmp(option->name, "raw") ||
255 !strcasecmp(option->name, "document-format") ||
256 !option->name[0])
257 continue;
258
259 /*
260 * Figure out the proper value and group tags for this option...
261 */
262
263 key.name = option->name;
264 match = (_ipp_option_t *)bsearch(&key, ipp_options,
265 sizeof(ipp_options) /
266 sizeof(ipp_options[0]),
267 sizeof(ipp_options[0]),
268 (int (*)(const void *,
269 const void *))
270 compare_ipp_options);
271
272 if (match)
273 {
274 if (match->group_tag != group_tag)
275 continue;
276
277 value_tag = match->value_tag;
278 }
279 else
280 {
281 int namelen; /* Length of name */
282
283
284 namelen = strlen(option->name);
285
286 if (namelen < 9 || strcmp(option->name + namelen - 8, "-default"))
287 {
288 if (group_tag != IPP_TAG_JOB)
289 continue;
290 }
291 else if (group_tag != IPP_TAG_PRINTER)
292 continue;
293
294 if (!strcasecmp(option->value, "true") ||
295 !strcasecmp(option->value, "false"))
296 value_tag = IPP_TAG_BOOLEAN;
297 else
298 value_tag = IPP_TAG_NAME;
299 }
300
301 /*
302 * Count the number of values...
303 */
304
305 for (count = 1, sep = option->value; *sep; sep ++)
306 {
307 if (*sep == '\'')
308 {
309 /*
310 * Skip quoted option value...
311 */
312
313 sep ++;
314
315 while (*sep && *sep != '\'')
316 sep ++;
317
318 if (!*sep)
319 sep --;
320 }
321 else if (*sep == '\"')
322 {
323 /*
324 * Skip quoted option value...
325 */
326
327 sep ++;
328
329 while (*sep && *sep != '\"')
330 sep ++;
331
332 if (!*sep)
333 sep --;
334 }
335 else if (*sep == ',')
336 count ++;
337 else if (*sep == '\\' && sep[1])
338 sep ++;
339 }
340
341 DEBUG_printf(("cupsEncodeOptions2: option = \'%s\', count = %d\n",
342 option->name, count));
343
344 /*
345 * Allocate memory for the attribute values...
346 */
347
348 if ((attr = _ippAddAttr(ipp, count)) == NULL)
349 {
350 /*
351 * Ran out of memory!
352 */
353
354 DEBUG_puts("cupsEncodeOptions2: Ran out of memory for attributes!");
355 return;
356 }
357
358 /*
359 * Now figure out what type of value we have...
360 */
361
362 attr->group_tag = group_tag;
363 attr->value_tag = value_tag;
364
365 /*
366 * Copy the name over...
367 */
368
369 if ((attr->name = _cupsStrAlloc(option->name)) == NULL)
370 {
371 /*
372 * Ran out of memory!
373 */
374
375 DEBUG_puts("cupsEncodeOptions2: Ran out of memory for name!");
376 return;
377 }
378
379 if (count > 1)
380 {
381 /*
382 * Make a copy of the value we can fiddle with...
383 */
384
385 if ((copy = strdup(option->value)) == NULL)
386 {
387 /*
388 * Ran out of memory!
389 */
390
391 DEBUG_puts("cupsEncodeOptions2: Ran out of memory for value copy!");
392 return;
393 }
394
395 val = copy;
396 }
397 else
398 {
399 /*
400 * Since we have a single value, use the value directly...
401 */
402
403 val = option->value;
404 copy = NULL;
405 }
406
407 /*
408 * Scan the value string for values...
409 */
410
411 for (j = 0; j < count; val = sep, j ++)
412 {
413 /*
414 * Find the end of this value and mark it if needed...
415 */
416
417 if ((sep = strchr(val, ',')) != NULL)
418 *sep++ = '\0';
419 else
420 sep = val + strlen(val);
421
422 /*
423 * Copy the option value(s) over as needed by the type...
424 */
425
426 switch (attr->value_tag)
427 {
428 case IPP_TAG_INTEGER :
429 case IPP_TAG_ENUM :
430 /*
431 * Integer/enumeration value...
432 */
433
434 attr->values[j].integer = strtol(val, &s, 0);
435
436 DEBUG_printf(("cupsEncodeOptions2: Added integer option value %d...\n",
437 attr->values[j].integer));
438 break;
439
440 case IPP_TAG_BOOLEAN :
441 if (!strcasecmp(val, "true") ||
442 !strcasecmp(val, "on") ||
443 !strcasecmp(val, "yes"))
444 {
445 /*
446 * Boolean value - true...
447 */
448
449 attr->values[j].boolean = 1;
450
451 DEBUG_puts("cupsEncodeOptions2: Added boolean true value...");
452 }
453 else
454 {
455 /*
456 * Boolean value - false...
457 */
458
459 attr->values[j].boolean = 0;
460
461 DEBUG_puts("cupsEncodeOptions2: Added boolean false value...");
462 }
463 break;
464
465 case IPP_TAG_RANGE :
466 /*
467 * Range...
468 */
469
470 if (*val == '-')
471 {
472 attr->values[j].range.lower = 1;
473 s = val;
474 }
475 else
476 attr->values[j].range.lower = strtol(val, &s, 0);
477
478 if (*s == '-')
479 {
480 if (s[1])
481 attr->values[j].range.upper = strtol(s + 1, NULL, 0);
482 else
483 attr->values[j].range.upper = 2147483647;
484 }
485 else
486 attr->values[j].range.upper = attr->values[j].range.lower;
487
488 DEBUG_printf(("cupsEncodeOptions2: Added range option value %d-%d...\n",
489 attr->values[j].range.lower,
490 attr->values[j].range.upper));
491 break;
492
493 case IPP_TAG_RESOLUTION :
494 /*
495 * Resolution...
496 */
497
498 attr->values[j].resolution.xres = strtol(val, &s, 0);
499
500 if (*s == 'x')
501 attr->values[j].resolution.yres = strtol(s + 1, &s, 0);
502 else
503 attr->values[j].resolution.yres = attr->values[j].resolution.xres;
504
505 if (!strcasecmp(s, "dpc"))
506 attr->values[j].resolution.units = IPP_RES_PER_CM;
507 else
508 attr->values[j].resolution.units = IPP_RES_PER_INCH;
509
510 DEBUG_printf(("cupsEncodeOptions2: Added resolution option value %s...\n",
511 val));
512 break;
513
514 case IPP_TAG_STRING :
515 /*
516 * octet-string
517 */
518
519 attr->values[j].unknown.length = strlen(val);
520 attr->values[j].unknown.data = _cupsStrAlloc(val);
521
522 DEBUG_printf(("cupsEncodeOptions2: Added octet-string value \"%s\"...\n",
523 attr->values[j].unknown.data));
524 break;
525
526 default :
527 if ((attr->values[j].string.text = _cupsStrAlloc(val)) == NULL)
528 {
529 /*
530 * Ran out of memory!
531 */
532
533 DEBUG_puts("cupsEncodeOptions2: Ran out of memory for string!");
534 return;
535 }
536
537 DEBUG_printf(("cupsEncodeOptions2: Added string value \"%s\"...\n",
538 val));
539 break;
540 }
541 }
542
543 if (copy)
544 free(copy);
545 }
546 }
547
548
549 /*
550 * 'compare_ipp_options()' - Compare two IPP options.
551 */
552
553 static int /* O - Result of comparison */
554 compare_ipp_options(_ipp_option_t *a, /* I - First option */
555 _ipp_option_t *b) /* I - Second option */
556 {
557 return (strcmp(a->name, b->name));
558 }
559
560
561 /*
562 * End of "$Id: encode.c 5545 2006-05-18 21:00:56Z mike $".
563 */