Coverage Report

Created: 2025-11-09 06:35

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/cups/cups/options.c
Line
Count
Source
1
/*
2
 * Option routines for CUPS.
3
 *
4
 * Copyright © 2020-2025 by OpenPrinting.
5
 * Copyright © 2007-2017 by Apple Inc.
6
 * Copyright © 1997-2007 by Easy Software Products.
7
 *
8
 * Licensed under Apache License v2.0.  See the file "LICENSE" for more information.
9
 */
10
11
#include "cups-private.h"
12
#include "debug-internal.h"
13
14
15
/*
16
 * Local functions...
17
 */
18
19
static int  cups_compare_options(cups_option_t *a, cups_option_t *b);
20
static int  cups_find_option(const char *name, int num_options,
21
                           cups_option_t *option, int prev, int *rdiff);
22
23
24
/*
25
 * 'cupsAddIntegerOption()' - Add an integer option to an option array.
26
 *
27
 * New option arrays can be initialized simply by passing 0 for the
28
 * "num_options" parameter.
29
 *
30
 * @since CUPS 2.2.4@
31
 */
32
33
int         /* O  - Number of options */
34
cupsAddIntegerOption(
35
    const char    *name,    /* I  - Name of option */
36
    int           value,    /* I  - Value of option */
37
    int           num_options,    /* I  - Number of options */
38
    cups_option_t **options)    /* IO - Pointer to options */
39
0
{
40
0
  char  strvalue[32];     /* String value */
41
42
43
0
  snprintf(strvalue, sizeof(strvalue), "%d", value);
44
45
0
  return (cupsAddOption(name, strvalue, num_options, options));
46
0
}
47
48
49
/*
50
 * 'cupsAddOption()' - Add an option to an option array.
51
 *
52
 * New option arrays can be initialized simply by passing 0 for the
53
 * "num_options" parameter.
54
 */
55
56
int         /* O  - Number of options */
57
cupsAddOption(const char    *name,  /* I  - Name of option */
58
              const char    *value, /* I  - Value of option */
59
        int           num_options,/* I  - Number of options */
60
              cups_option_t **options)  /* IO - Pointer to options */
61
0
{
62
0
  cups_option_t *temp;      /* Pointer to new option */
63
0
  int   insert,     /* Insertion point */
64
0
    diff;     /* Result of search */
65
66
67
0
  DEBUG_printf("2cupsAddOption(name=\"%s\", value=\"%s\", num_options=%d, options=%p)", name, value, num_options, (void *)options);
68
69
0
  if (!name || !name[0] || !value || !options || num_options < 0)
70
0
  {
71
0
    DEBUG_printf("3cupsAddOption: Returning %d", num_options);
72
0
    return (num_options);
73
0
  }
74
75
0
  if (!_cups_strcasecmp(name, "cupsPrintQuality"))
76
0
    num_options = cupsRemoveOption("print-quality", num_options, options);
77
0
  else if (!_cups_strcasecmp(name, "print-quality"))
78
0
    num_options = cupsRemoveOption("cupsPrintQuality", num_options, options);
79
80
 /*
81
  * Look for an existing option with the same name...
82
  */
83
84
0
  if (num_options == 0)
85
0
  {
86
0
    insert = 0;
87
0
    diff   = 1;
88
0
  }
89
0
  else
90
0
  {
91
0
    insert = cups_find_option(name, num_options, *options, num_options - 1,
92
0
                              &diff);
93
94
0
    if (diff > 0)
95
0
      insert ++;
96
0
  }
97
98
0
  if (diff)
99
0
  {
100
   /*
101
    * No matching option name...
102
    */
103
104
0
    DEBUG_printf("4cupsAddOption: New option inserted at index %d...", insert);
105
106
0
    if (num_options == 0)
107
0
      temp = (cups_option_t *)malloc(sizeof(cups_option_t));
108
0
    else
109
0
      temp = (cups_option_t *)realloc(*options, sizeof(cups_option_t) * (size_t)(num_options + 1));
110
111
0
    if (!temp)
112
0
    {
113
0
      DEBUG_puts("3cupsAddOption: Unable to expand option array, returning 0");
114
0
      return (0);
115
0
    }
116
117
0
    *options = temp;
118
119
0
    if (insert < num_options)
120
0
    {
121
0
      DEBUG_printf("4cupsAddOption: Shifting %d options...", (int)(num_options - insert));
122
0
      memmove(temp + insert + 1, temp + insert, (size_t)(num_options - insert) * sizeof(cups_option_t));
123
0
    }
124
125
0
    temp        += insert;
126
0
    temp->name  = _cupsStrAlloc(name);
127
0
    num_options ++;
128
0
  }
129
0
  else
130
0
  {
131
   /*
132
    * Match found; free the old value...
133
    */
134
135
0
    DEBUG_printf("4cupsAddOption: Option already exists at index %d...", insert);
136
137
0
    temp = *options + insert;
138
0
    _cupsStrFree(temp->value);
139
0
  }
140
141
0
  temp->value = _cupsStrAlloc(value);
142
143
0
  DEBUG_printf("3cupsAddOption: Returning %d", num_options);
144
145
0
  return (num_options);
146
0
}
147
148
149
/*
150
 * 'cupsFreeOptions()' - Free all memory used by options.
151
 */
152
153
void
154
cupsFreeOptions(
155
    int           num_options,    /* I - Number of options */
156
    cups_option_t *options)   /* I - Pointer to options */
157
0
{
158
0
  int i;        /* Looping var */
159
160
161
0
  DEBUG_printf("cupsFreeOptions(num_options=%d, options=%p)", num_options, (void *)options);
162
163
0
  if (num_options <= 0 || !options)
164
0
    return;
165
166
0
  for (i = 0; i < num_options; i ++)
167
0
  {
168
0
    _cupsStrFree(options[i].name);
169
0
    _cupsStrFree(options[i].value);
170
0
  }
171
172
0
  free(options);
173
0
}
174
175
176
/*
177
 * 'cupsGetIntegerOption()' - Get an integer option value.
178
 *
179
 * INT_MIN is returned when the option does not exist, is not an integer, or
180
 * exceeds the range of values for the "int" type.
181
 *
182
 * @since CUPS 2.2.4@
183
 */
184
185
int         /* O - Option value or @code INT_MIN@ */
186
cupsGetIntegerOption(
187
    const char    *name,    /* I - Name of option */
188
    int           num_options,    /* I - Number of options */
189
    cups_option_t *options)   /* I - Options */
190
0
{
191
0
  const char  *value = cupsGetOption(name, num_options, options);
192
          /* String value of option */
193
0
  char    *ptr;     /* Pointer into string value */
194
0
  long    intvalue;   /* Integer value */
195
196
197
0
  if (!value || !*value)
198
0
    return (INT_MIN);
199
200
0
  intvalue = strtol(value, &ptr, 10);
201
0
  if (intvalue < INT_MIN || intvalue > INT_MAX || *ptr)
202
0
    return (INT_MIN);
203
204
0
  return ((int)intvalue);
205
0
}
206
207
208
/*
209
 * 'cupsGetOption()' - Get an option value.
210
 */
211
212
const char *        /* O - Option value or @code NULL@ */
213
cupsGetOption(const char    *name,  /* I - Name of option */
214
              int           num_options,/* I - Number of options */
215
              cups_option_t *options) /* I - Options */
216
0
{
217
0
  int diff,       /* Result of comparison */
218
0
  match;        /* Matching index */
219
220
221
0
  DEBUG_printf("2cupsGetOption(name=\"%s\", num_options=%d, options=%p)", name, num_options, (void *)options);
222
223
0
  if (!name || num_options <= 0 || !options)
224
0
  {
225
0
    DEBUG_puts("3cupsGetOption: Returning NULL");
226
0
    return (NULL);
227
0
  }
228
229
0
  match = cups_find_option(name, num_options, options, -1, &diff);
230
231
0
  if (!diff)
232
0
  {
233
0
    DEBUG_printf("3cupsGetOption: Returning \"%s\"", options[match].value);
234
0
    return (options[match].value);
235
0
  }
236
237
0
  DEBUG_puts("3cupsGetOption: Returning NULL");
238
0
  return (NULL);
239
0
}
240
241
242
/*
243
 * 'cupsParseOptions()' - Parse options from a command-line argument.
244
 *
245
 * This function converts space-delimited name/value pairs according
246
 * to the PAPI text option ABNF specification. Collection values
247
 * ("name={a=... b=... c=...}") are stored with the curley brackets
248
 * intact - use @code cupsParseOptions@ on the value to extract the
249
 * collection attributes.
250
 */
251
252
int         /* O - Number of options found */
253
cupsParseOptions(
254
    const char    *arg,     /* I - Argument to parse */
255
    int           num_options,    /* I - Number of options */
256
    cups_option_t **options)    /* O - Options found */
257
0
{
258
0
  return (cupsParseOptions2(arg, /*end*/NULL, num_options, options));
259
0
}
260
261
262
//
263
// 'cupsParseOptions2()' - Parse options from a command-line argument.
264
//
265
// This function converts space-delimited name/value pairs according
266
// to the PAPI text option ABNF specification. Collection values
267
// ("name={a=... b=... c=...}") are stored with the curley brackets
268
// intact - use @code cupsParseOptions@ on the value to extract the
269
// collection attributes.
270
//
271
// The "end" argument, if not `NULL`, receives a pointer to the end of the
272
// options.
273
//
274
// @since CUPS 2.5@
275
//
276
277
int         // O - Number of options found
278
cupsParseOptions2(
279
    const char    *arg,     // I - Argument to parse
280
    const char    **end,    // O - Pointer to end of options or `NULL` for "don't care"
281
    int           num_options,    // I - Number of options
282
    cups_option_t **options)    // O - Options found
283
0
{
284
0
  char  *copyarg,     // Copy of input string
285
0
  *ptr,       // Pointer into string
286
0
  *name,        // Pointer to name
287
0
  *value,       // Pointer to value
288
0
  sep,        // Separator character
289
0
  quote;        // Quote character
290
291
292
  // Range check input...
293
0
  if (end)
294
0
    *end = NULL;
295
296
0
  if (!arg)
297
0
    return (num_options);
298
299
0
  if (!options)
300
0
    return (0);
301
302
  // Make a copy of the argument string and then divide it up...
303
0
  if ((copyarg = strdup(arg)) == NULL)
304
0
  {
305
0
    DEBUG_puts("1cupsParseOptions2: Unable to copy arg string");
306
0
    return (num_options);
307
0
  }
308
309
0
  if (*copyarg == '{')
310
0
    ptr = copyarg + 1;
311
0
  else
312
0
    ptr = copyarg;
313
314
  // Skip leading spaces...
315
0
  while (_cups_isspace(*ptr))
316
0
    ptr ++;
317
318
  // Loop through the string...
319
0
  while (*ptr != '\0')
320
0
  {
321
    // Get the name up to a SPACE, =, or end-of-string...
322
0
    name = ptr;
323
0
    while (!strchr("\f\n\r\t\v =", *ptr) && *ptr)
324
0
      ptr ++;
325
326
    // Avoid an empty name...
327
0
    if (ptr == name)
328
0
      break;
329
330
    // End after the closing brace...
331
0
    if (*ptr == '}' && *copyarg == '{')
332
0
    {
333
0
      *ptr++ = '\0';
334
0
      break;
335
0
    }
336
337
    // Skip trailing spaces...
338
0
    while (_cups_isspace(*ptr))
339
0
      *ptr++ = '\0';
340
341
0
    if ((sep = *ptr) == '=')
342
0
      *ptr++ = '\0';
343
344
0
    if (sep != '=')
345
0
    {
346
      // Boolean option...
347
0
      if (!_cups_strncasecmp(name, "no", 2))
348
0
        num_options = cupsAddOption(name + 2, "false", num_options, options);
349
0
      else
350
0
        num_options = cupsAddOption(name, "true", num_options, options);
351
352
0
      continue;
353
0
    }
354
355
    // Remove = and parse the value...
356
0
    value = ptr;
357
358
0
    while (*ptr && !_cups_isspace(*ptr))
359
0
    {
360
0
      if (*ptr == ',')
361
0
      {
362
0
        ptr ++;
363
0
      }
364
0
      else if (*ptr == '\'' || *ptr == '\"')
365
0
      {
366
        // Quoted string constant...
367
0
  quote = *ptr;
368
0
  _cups_strcpy(ptr, ptr + 1);
369
370
0
  while (*ptr != quote && *ptr)
371
0
  {
372
0
    if (*ptr == '\\' && ptr[1])
373
0
      _cups_strcpy(ptr, ptr + 1);
374
375
0
    ptr ++;
376
0
  }
377
378
0
  if (*ptr)
379
0
    _cups_strcpy(ptr, ptr + 1);
380
0
      }
381
0
      else if (*ptr == '{')
382
0
      {
383
        // Collection value...
384
0
  int depth;      // Nesting depth for braces
385
386
0
  for (depth = 0; *ptr; ptr ++)
387
0
  {
388
0
    if (*ptr == '{')
389
0
    {
390
0
      depth ++;
391
0
    }
392
0
    else if (*ptr == '}')
393
0
    {
394
0
      depth --;
395
0
      if (!depth)
396
0
      {
397
0
        ptr ++;
398
0
        break;
399
0
      }
400
0
    }
401
0
    else if (*ptr == '\\' && ptr[1])
402
0
    {
403
0
      _cups_strcpy(ptr, ptr + 1);
404
0
    }
405
0
  }
406
0
      }
407
0
      else
408
0
      {
409
        // Normal space-delimited string...
410
0
  while (*ptr && !_cups_isspace(*ptr))
411
0
  {
412
0
    if (*ptr == '}' && *copyarg == '{')
413
0
    {
414
0
      *ptr++ = '\0';
415
0
      break;
416
0
    }
417
418
0
    if (*ptr == '\\' && ptr[1])
419
0
      _cups_strcpy(ptr, ptr + 1);
420
421
0
    ptr ++;
422
0
  }
423
0
      }
424
0
    }
425
426
0
    if (*ptr != '\0')
427
0
      *ptr++ = '\0';
428
429
    // Skip trailing whitespace...
430
0
    while (_cups_isspace(*ptr))
431
0
      ptr ++;
432
433
    // Add the string value...
434
0
    num_options = cupsAddOption(name, value, num_options, options);
435
0
  }
436
437
  // Save the progress in the input string...
438
0
  if (end)
439
0
    *end = arg + (ptr - copyarg);
440
441
  // Free the copy of the argument we made and return the number of options found.
442
0
  free(copyarg);
443
444
0
  return (num_options);
445
0
}
446
447
448
/*
449
 * 'cupsRemoveOption()' - Remove an option from an option array.
450
 *
451
 * @since CUPS 1.2@
452
 */
453
454
int         /* O  - New number of options */
455
cupsRemoveOption(
456
    const char    *name,    /* I  - Option name */
457
    int           num_options,    /* I  - Current number of options */
458
    cups_option_t **options)    /* IO - Options */
459
0
{
460
0
  int   i;      /* Looping var */
461
0
  cups_option_t *option;    /* Current option */
462
463
464
0
  DEBUG_printf("2cupsRemoveOption(name=\"%s\", num_options=%d, options=%p)", name, num_options, (void *)options);
465
466
 /*
467
  * Range check input...
468
  */
469
470
0
  if (!name || num_options < 1 || !options)
471
0
  {
472
0
    DEBUG_printf("3cupsRemoveOption: Returning %d", num_options);
473
0
    return (num_options);
474
0
  }
475
476
 /*
477
  * Loop for the option...
478
  */
479
480
0
  for (i = num_options, option = *options; i > 0; i --, option ++)
481
0
    if (!_cups_strcasecmp(name, option->name))
482
0
      break;
483
484
0
  if (i)
485
0
  {
486
   /*
487
    * Remove this option from the array...
488
    */
489
490
0
    DEBUG_puts("4cupsRemoveOption: Found option, removing it...");
491
492
0
    num_options --;
493
0
    i --;
494
495
0
    _cupsStrFree(option->name);
496
0
    _cupsStrFree(option->value);
497
498
0
    if (i > 0)
499
0
      memmove(option, option + 1, (size_t)i * sizeof(cups_option_t));
500
0
  }
501
502
 /*
503
  * Return the new number of options...
504
  */
505
506
0
  DEBUG_printf("3cupsRemoveOption: Returning %d", num_options);
507
0
  return (num_options);
508
0
}
509
510
511
/*
512
 * '_cupsGet1284Values()' - Get 1284 device ID keys and values.
513
 *
514
 * The returned dictionary is a CUPS option array that can be queried with
515
 * cupsGetOption and freed with cupsFreeOptions.
516
 */
517
518
int         /* O - Number of key/value pairs */
519
_cupsGet1284Values(
520
    const char *device_id,    /* I - IEEE-1284 device ID string */
521
    cups_option_t **values)   /* O - Array of key/value pairs */
522
0
{
523
0
  int   num_values;   /* Number of values */
524
0
  char    key[256],   /* Key string */
525
0
    value[256],   /* Value string */
526
0
    *ptr;     /* Pointer into key/value */
527
528
529
 /*
530
  * Range check input...
531
  */
532
533
0
  if (values)
534
0
    *values = NULL;
535
536
0
  if (!device_id || !values)
537
0
    return (0);
538
539
 /*
540
  * Parse the 1284 device ID value into keys and values.  The format is
541
  * repeating sequences of:
542
  *
543
  *   [whitespace]key:value[whitespace];
544
  */
545
546
0
  num_values = 0;
547
0
  while (*device_id)
548
0
  {
549
0
    while (_cups_isspace(*device_id))
550
0
      device_id ++;
551
552
0
    if (!*device_id)
553
0
      break;
554
555
0
    for (ptr = key; *device_id && *device_id != ':'; device_id ++)
556
0
      if (ptr < (key + sizeof(key) - 1))
557
0
        *ptr++ = *device_id;
558
559
0
    if (!*device_id)
560
0
      break;
561
562
0
    while (ptr > key && _cups_isspace(ptr[-1]))
563
0
      ptr --;
564
565
0
    *ptr = '\0';
566
0
    device_id ++;
567
568
0
    while (_cups_isspace(*device_id))
569
0
      device_id ++;
570
571
0
    if (!*device_id)
572
0
      break;
573
574
0
    for (ptr = value; *device_id && *device_id != ';'; device_id ++)
575
0
      if (ptr < (value + sizeof(value) - 1))
576
0
        *ptr++ = *device_id;
577
578
0
    while (ptr > value && _cups_isspace(ptr[-1]))
579
0
      ptr --;
580
581
0
    *ptr = '\0';
582
0
    num_values = cupsAddOption(key, value, num_values, values);
583
584
0
    if (!*device_id)
585
0
      break;
586
0
    device_id ++;
587
0
  }
588
589
0
  return (num_values);
590
0
}
591
592
593
/*
594
 * 'cups_compare_options()' - Compare two options.
595
 */
596
597
static int        /* O - Result of comparison */
598
cups_compare_options(cups_option_t *a,  /* I - First option */
599
         cups_option_t *b)  /* I - Second option */
600
0
{
601
0
  return (_cups_strcasecmp(a->name, b->name));
602
0
}
603
604
605
/*
606
 * 'cups_find_option()' - Find an option using a binary search.
607
 */
608
609
static int        /* O - Index of match */
610
cups_find_option(
611
    const char    *name,    /* I - Option name */
612
    int           num_options,    /* I - Number of options */
613
    cups_option_t *options,   /* I - Options */
614
    int           prev,     /* I - Previous index */
615
    int           *rdiff)   /* O - Difference of match */
616
0
{
617
0
  int   left,     /* Low mark for binary search */
618
0
    right,      /* High mark for binary search */
619
0
    current,    /* Current index */
620
0
    diff;     /* Result of comparison */
621
0
  cups_option_t key;      /* Search key */
622
623
624
0
  DEBUG_printf("7cups_find_option(name=\"%s\", num_options=%d, options=%p, prev=%d, rdiff=%p)", name, num_options, (void *)options, prev, (void *)rdiff);
625
626
#ifdef DEBUG
627
  for (left = 0; left < num_options; left ++)
628
    DEBUG_printf("9cups_find_option: options[%d].name=\"%s\", .value=\"%s\"", left, options[left].name, options[left].value);
629
#endif /* DEBUG */
630
631
0
  key.name = (char *)name;
632
633
0
  if (prev >= 0)
634
0
  {
635
   /*
636
    * Start search on either side of previous...
637
    */
638
639
0
    if ((diff = cups_compare_options(&key, options + prev)) == 0 ||
640
0
        (diff < 0 && prev == 0) ||
641
0
  (diff > 0 && prev == (num_options - 1)))
642
0
    {
643
0
      *rdiff = diff;
644
0
      return (prev);
645
0
    }
646
0
    else if (diff < 0)
647
0
    {
648
     /*
649
      * Start with previous on right side...
650
      */
651
652
0
      left  = 0;
653
0
      right = prev;
654
0
    }
655
0
    else
656
0
    {
657
     /*
658
      * Start with previous on left side...
659
      */
660
661
0
      left  = prev;
662
0
      right = num_options - 1;
663
0
    }
664
0
  }
665
0
  else
666
0
  {
667
   /*
668
    * Start search in the middle...
669
    */
670
671
0
    left  = 0;
672
0
    right = num_options - 1;
673
0
  }
674
675
0
  do
676
0
  {
677
0
    current = (left + right) / 2;
678
0
    diff    = cups_compare_options(&key, options + current);
679
680
0
    if (diff == 0)
681
0
      break;
682
0
    else if (diff < 0)
683
0
      right = current;
684
0
    else
685
0
      left = current;
686
0
  }
687
0
  while ((right - left) > 1);
688
689
0
  if (diff != 0)
690
0
  {
691
   /*
692
    * Check the last 1 or 2 elements...
693
    */
694
695
0
    if ((diff = cups_compare_options(&key, options + left)) <= 0)
696
0
      current = left;
697
0
    else
698
0
    {
699
0
      diff    = cups_compare_options(&key, options + right);
700
0
      current = right;
701
0
    }
702
0
  }
703
704
 /*
705
  * Return the closest destination and the difference...
706
  */
707
708
0
  *rdiff = diff;
709
710
0
  return (current);
711
0
}