Coverage Report

Created: 2026-12-01 07:13

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libcups/cups/ipp.c
Line
Count
Source
1
//
2
// Internet Printing Protocol functions for CUPS.
3
//
4
// Copyright © 2021-2026 by OpenPrinting.
5
// Copyright © 2007-2021 by Apple Inc.
6
// Copyright © 1997-2007 by Easy Software Products, all rights reserved.
7
//
8
// Licensed under Apache License v2.0.  See the file "LICENSE" for more
9
// information.
10
//
11
12
#include "cups-private.h"
13
#include <regex.h>
14
#ifdef _WIN32
15
#  include <io.h>
16
#endif // _WIN32
17
18
19
//
20
// Local globals...
21
//
22
23
static cups_mutex_t regex_mutex = CUPS_MUTEX_INITIALIZER;
24
          // Mutex for regular expressions
25
static bool   regex_init = false;
26
          // Have regular expressions been initialized?
27
static regex_t    lang_regex; // Regular expression for naturalLanguage
28
static regex_t    mime_regex; // Regular expression for mimeMediaType
29
30
31
//
32
// Local functions...
33
//
34
35
static ipp_attribute_t  *ipp_add_attr(ipp_t *ipp, const char *name, ipp_tag_t group_tag, ipp_tag_t value_tag, size_t num_values);
36
static void   ipp_free_values(ipp_attribute_t *attr, size_t element, size_t count);
37
static char   *ipp_get_code(const char *locale, char *buffer, size_t bufsize) _CUPS_NONNULL(1,2);
38
static bool   ipp_is_valid_keyword(const char *name, const char *keyword);
39
static bool   ipp_is_valid_language(const char *name, const char *lang);
40
static bool   ipp_is_valid_mimetype(const char *name, const char *type);
41
static bool   ipp_is_valid_string(const char *name, const char *s, bool is_text);
42
static char   *ipp_lang_code(const char *locale, char *buffer, size_t bufsize) _CUPS_NONNULL(1,2);
43
static size_t   ipp_length(ipp_t *ipp, int collection);
44
static ssize_t    ipp_read_file(int *fd, ipp_uchar_t *buffer, size_t length);
45
static ssize_t    ipp_read_http(http_t *http, ipp_uchar_t *buffer, size_t length);
46
static ipp_state_t  ipp_read_io(void *src, ipp_io_cb_t cb, bool blocking, ipp_t *parent, ipp_t *ipp, int depth);
47
static bool   ipp_regex_begin(void);
48
static void   ipp_regex_end(void);
49
static void   ipp_set_error(ipp_status_t status, const char *format, ...);
50
static _ipp_value_t *ipp_set_value(ipp_t *ipp, ipp_attribute_t **attr, size_t element);
51
static ssize_t    ipp_write_file(int *fd, ipp_uchar_t *buffer, size_t length);
52
53
54
//
55
// '_cupsBufferGet()' - Get a read/write buffer.
56
//
57
58
char *          // O - Buffer
59
_cupsBufferGet(size_t size)   // I - Size required
60
309k
{
61
309k
  _cups_buffer_t  *buffer;  // Current buffer
62
309k
  _cups_globals_t *cg = _cupsGlobals();
63
          // Global data
64
65
66
611k
  for (buffer = cg->cups_buffers; buffer; buffer = buffer->next)
67
611k
    if (!buffer->used && buffer->size >= size)
68
309k
      break;
69
70
309k
  if (!buffer)
71
22
  {
72
22
    if ((buffer = malloc(sizeof(_cups_buffer_t) + size - 1)) == NULL)
73
0
      return (NULL);
74
75
22
    buffer->next     = cg->cups_buffers;
76
22
    buffer->size     = size;
77
22
    cg->cups_buffers = buffer;
78
22
  }
79
80
309k
  buffer->used = 1;
81
82
309k
  return (buffer->d);
83
309k
}
84
85
86
//
87
// '_cupsBufferRelease()' - Release a read/write buffer.
88
//
89
90
void
91
_cupsBufferRelease(char *b)   // I - Buffer to release
92
309k
{
93
309k
  _cups_buffer_t  *buffer;  // Buffer
94
95
96
  // Mark this buffer as unused...
97
309k
  buffer       = (_cups_buffer_t *)(b - offsetof(_cups_buffer_t, d));
98
309k
  buffer->used = 0;
99
309k
}
100
101
102
//
103
// 'ippAddBoolean()' - Add a boolean attribute to an IPP message.
104
//
105
// The "ipp" argument refers to an IPP message previously created using
106
// the @link ippNew@, @link ippNewRequest@, or  @link ippNewResponse@ functions.
107
//
108
// The "group" argument specifies the IPP attribute group tag: none
109
// (`IPP_TAG_ZERO`, for member attributes), document (`IPP_TAG_DOCUMENT`),
110
// event notification (`IPP_TAG_EVENT_NOTIFICATION`), operation
111
// (`IPP_TAG_OPERATION`), printer (`IPP_TAG_PRINTER`), subscription
112
// (`IPP_TAG_SUBSCRIPTION`), or unsupported (`IPP_TAG_UNSUPPORTED_GROUP`).
113
//
114
115
ipp_attribute_t *     // O - New attribute
116
ippAddBoolean(ipp_t      *ipp,    // I - IPP message
117
              ipp_tag_t  group,   // I - IPP group
118
              const char *name,   // I - Name of attribute
119
              bool       value)   // I - Value of attribute
120
0
{
121
0
  ipp_attribute_t *attr;    // New attribute
122
123
124
0
  DEBUG_printf("ippAddBoolean(ipp=%p, group=%02x(%s), name=\"%s\", value=%d)", (void *)ipp, group, ippTagString(group), name, value);
125
126
  // Range check input...
127
0
  if (!ipp || !name || group < IPP_TAG_ZERO || group == IPP_TAG_END || group >= IPP_TAG_UNSUPPORTED_VALUE)
128
0
    return (NULL);
129
130
  // Create the attribute...
131
0
  if ((attr = ipp_add_attr(ipp, name, group, IPP_TAG_BOOLEAN, 1)) == NULL)
132
0
    return (NULL);
133
134
0
  attr->values[0].boolean = value;
135
136
0
  return (attr);
137
0
}
138
139
140
//
141
// 'ippAddBooleans()' - Add an array of boolean values.
142
//
143
// The "ipp" argument refers to an IPP message previously created using
144
// the @link ippNew@, @link ippNewRequest@, or  @link ippNewResponse@ functions.
145
//
146
// The "group" argument specifies the IPP attribute group tag: none
147
// (`IPP_TAG_ZERO`, for member attributes), document (`IPP_TAG_DOCUMENT`),
148
// event notification (`IPP_TAG_EVENT_NOTIFICATION`), operation
149
// (`IPP_TAG_OPERATION`), printer (`IPP_TAG_PRINTER`), subscription
150
// (`IPP_TAG_SUBSCRIPTION`), or unsupported (`IPP_TAG_UNSUPPORTED_GROUP`).
151
//
152
153
ipp_attribute_t *     // O - New attribute
154
ippAddBooleans(ipp_t      *ipp,   // I - IPP message
155
               ipp_tag_t  group,  // I - IPP group
156
         const char *name,  // I - Name of attribute
157
         size_t     num_values, // I - Number of values
158
         const bool *values)  // I - Values
159
0
{
160
0
  size_t    i;    // Looping var
161
0
  ipp_attribute_t *attr;    // New attribute
162
0
  _ipp_value_t    *value;   // Current value
163
164
165
0
  DEBUG_printf("ippAddBooleans(ipp=%p, group=%02x(%s), name=\"%s\", num_values=%u, values=%p)", (void *)ipp, group, ippTagString(group), name, (unsigned)num_values, (void *)values);
166
167
  // Range check input...
168
0
  if (!ipp || !name || group < IPP_TAG_ZERO || group == IPP_TAG_END || group >= IPP_TAG_UNSUPPORTED_VALUE || num_values < 1)
169
0
    return (NULL);
170
171
  // Create the attribute...
172
0
  if ((attr = ipp_add_attr(ipp, name, group, IPP_TAG_BOOLEAN, num_values)) == NULL)
173
0
    return (NULL);
174
175
0
  if (values)
176
0
  {
177
0
    for (i = num_values, value = attr->values; i > 0; i --, value ++)
178
0
      value->boolean = *values++;
179
0
  }
180
181
0
  return (attr);
182
0
}
183
184
185
//
186
// 'ippAddCollection()' - Add a collection value.
187
//
188
// The "ipp" argument refers to an IPP message previously created using
189
// the @link ippNew@, @link ippNewRequest@, or @link ippNewResponse@ functions.
190
//
191
// The "group" argument specifies the IPP attribute group tag: none
192
// (`IPP_TAG_ZERO`, for member attributes), document (`IPP_TAG_DOCUMENT`),
193
// event notification (`IPP_TAG_EVENT_NOTIFICATION`), operation
194
// (`IPP_TAG_OPERATION`), printer (`IPP_TAG_PRINTER`), subscription
195
// (`IPP_TAG_SUBSCRIPTION`), or unsupported (`IPP_TAG_UNSUPPORTED_GROUP`).
196
//
197
// The "name" argument specifies the name of the attribute, while the "value"
198
// argument provides an IPP message containing the collection member attributes.
199
//
200
// > **Note:** You must call the @link ippDelete@ function on the "value"
201
// > argument to make sure the memory used by the value is released.
202
//
203
204
ipp_attribute_t *     // O - New attribute
205
ippAddCollection(ipp_t      *ipp, // I - IPP message
206
                 ipp_tag_t  group,  // I - IPP group
207
     const char *name,  // I - Name of attribute
208
     ipp_t      *value) // I - Value
209
0
{
210
0
  ipp_attribute_t *attr;    // New attribute
211
212
213
0
  DEBUG_printf("ippAddCollection(ipp=%p, group=%02x(%s), name=\"%s\", value=%p)", (void *)ipp, group, ippTagString(group), name, (void *)value);
214
215
  // Range check input...
216
0
  if (!ipp || !name || group < IPP_TAG_ZERO || group == IPP_TAG_END || group >= IPP_TAG_UNSUPPORTED_VALUE)
217
0
    return (NULL);
218
219
  // Create the attribute...
220
0
  if ((attr = ipp_add_attr(ipp, name, group, IPP_TAG_BEGIN_COLLECTION, 1)) == NULL)
221
0
    return (NULL);
222
223
0
  attr->values[0].collection = value;
224
225
0
  if (value)
226
0
    value->use ++;
227
228
0
  return (attr);
229
0
}
230
231
232
//
233
// 'ippAddCollections()' - Add an array of collection values.
234
//
235
// The "ipp" argument refers to an IPP message previously created using
236
// the @link ippNew@, @link ippNewRequest@, or  @link ippNewResponse@ functions.
237
//
238
// The "group" argument specifies the IPP attribute group tag: none
239
// (`IPP_TAG_ZERO`, for member attributes), document (`IPP_TAG_DOCUMENT`),
240
// event notification (`IPP_TAG_EVENT_NOTIFICATION`), operation
241
// (`IPP_TAG_OPERATION`), printer (`IPP_TAG_PRINTER`), subscription
242
// (`IPP_TAG_SUBSCRIPTION`), or unsupported (`IPP_TAG_UNSUPPORTED_GROUP`).
243
//
244
// The "name" argument specifies the name of the attribute, while the
245
// "num_values" and "values" arguments provide IPP messages containing the
246
// collection member attributes for each value.
247
//
248
// > **Note:** You must call the @link ippDelete@ function on each of the
249
// > "values" arguments to make sure the memory used by the value is released.
250
//
251
252
ipp_attribute_t *     // O - New attribute
253
ippAddCollections(
254
    ipp_t       *ipp,     // I - IPP message
255
    ipp_tag_t   group,      // I - IPP group
256
    const char  *name,      // I - Name of attribute
257
    size_t      num_values,   // I - Number of values
258
    const ipp_t **values)   // I - Values
259
0
{
260
0
  size_t    i;    // Looping var
261
0
  ipp_attribute_t *attr;    // New attribute
262
0
  _ipp_value_t    *value;   // Current value
263
264
265
0
  DEBUG_printf("ippAddCollections(ipp=%p, group=%02x(%s), name=\"%s\", num_values=%u, values=%p)", (void *)ipp, group, ippTagString(group), name, (unsigned)num_values, (void *)values);
266
267
  // Range check input...
268
0
  if (!ipp || !name || group < IPP_TAG_ZERO || group == IPP_TAG_END || group >= IPP_TAG_UNSUPPORTED_VALUE || num_values < 1)
269
0
    return (NULL);
270
271
  // Create the attribute...
272
0
  if ((attr = ipp_add_attr(ipp, name, group, IPP_TAG_BEGIN_COLLECTION, num_values)) == NULL)
273
0
    return (NULL);
274
275
0
  if (values)
276
0
  {
277
0
    for (i = num_values, value = attr->values; i > 0; i --, value ++)
278
0
    {
279
0
      value->collection = (ipp_t *)*values++;
280
0
      value->collection->use ++;
281
0
    }
282
0
  }
283
284
0
  return (attr);
285
0
}
286
287
288
//
289
// 'ippAddCredentialsString()' - Add a credentials string attribute to an IPP message.
290
//
291
// This function adds a 1setOf text attribute to an IPP message corresponding to
292
// the specified credentials string.
293
//
294
// The "ipp" argument refers to an IPP message previously created using
295
// the @link ippNew@, @link ippNewRequest@, or  @link ippNewResponse@ functions.
296
//
297
// The "group" argument specifies the IPP attribute group tag: none
298
// (`IPP_TAG_ZERO`, for member attributes), document (`IPP_TAG_DOCUMENT`),
299
// event notification (`IPP_TAG_EVENT_NOTIFICATION`), operation
300
// (`IPP_TAG_OPERATION`), printer (`IPP_TAG_PRINTER`), subscription
301
// (`IPP_TAG_SUBSCRIPTION`), or unsupported (`IPP_TAG_UNSUPPORTED_GROUP`).
302
//
303
304
ipp_attribute_t *     // O - New attribute
305
ippAddCredentialsString(
306
    ipp_t      *ipp,      // I - IPP message
307
    ipp_tag_t  group,     // I - IPP group
308
    const char *name,     // I - Attribute name
309
    const char *credentials)    // I - Credentials string
310
0
{
311
0
  ipp_attribute_t *attr;    // New attribute
312
0
  char      *cvalue,  // Copied value
313
0
      *cstart,  // Start of value
314
0
      *cptr;    // Pointer into copied value
315
0
  size_t    i,    // Looping var
316
0
      num_values; // Number of values
317
318
319
  // Range check input...
320
0
  if (!ipp || !name || !credentials)
321
0
    return (NULL);
322
323
  // Copy the value string and figure out the number of values...
324
0
  if ((cvalue = strdup(credentials)) == NULL)
325
0
    return (NULL);
326
327
0
  for (num_values = 0, cptr = cvalue; cptr;)
328
0
  {
329
    // Find the next delimiter
330
0
    if (*cptr && *cptr != '\r' && *cptr != '\n')
331
0
      num_values ++;
332
333
0
    cstart = cptr;
334
0
    if ((cptr = (char *)strchr(cstart, '\r')) != NULL)
335
0
    {
336
      // Skip CR or CR LF
337
0
      if (cptr[1] == '\n')
338
0
        cptr += 2;
339
0
      else
340
0
        cptr ++;
341
0
    }
342
0
    else if ((cptr = (char *)strchr(cstart, '\n')) != NULL)
343
0
    {
344
      // Skip LF
345
0
      cptr ++;
346
0
    }
347
0
  }
348
349
  // Create the empty attribute and copy the values...
350
0
  if ((attr = ippAddStrings(ipp, group, IPP_TAG_TEXT, name, num_values, NULL, NULL)) != NULL)
351
0
  {
352
0
    for (i = 0, cptr = cvalue; cptr && i < num_values;)
353
0
    {
354
0
      cstart = cptr;
355
0
      if ((cptr = (char *)strchr(cptr, '\r')) != NULL)
356
0
      {
357
        // Terminate on CR
358
0
        *cptr++ = '\0';
359
0
        if (*cptr == '\n')
360
0
          cptr ++;     // Skip LF
361
0
      }
362
0
      else if ((cptr = (char *)strchr(cstart, '\n')) != NULL)
363
0
      {
364
        // Terminate on LF
365
0
        *cptr++ = '\0';
366
0
      }
367
368
0
      if (*cstart)
369
0
        attr->values[i++].string.text = _cupsStrAlloc(cstart);
370
0
    }
371
0
  }
372
373
  // Free the copied string and return...
374
0
  free(cvalue);
375
376
0
  return (attr);
377
0
}
378
379
380
//
381
// 'ippAddDate()' - Add a dateTime attribute to an IPP message.
382
//
383
// The "ipp" argument refers to an IPP message previously created using
384
// the @link ippNew@, @link ippNewRequest@, or  @link ippNewResponse@ functions.
385
//
386
// The "group" argument specifies the IPP attribute group tag: none
387
// (`IPP_TAG_ZERO`, for member attributes), document (`IPP_TAG_DOCUMENT`),
388
// event notification (`IPP_TAG_EVENT_NOTIFICATION`), operation
389
// (`IPP_TAG_OPERATION`), printer (`IPP_TAG_PRINTER`), subscription
390
// (`IPP_TAG_SUBSCRIPTION`), or unsupported (`IPP_TAG_UNSUPPORTED_GROUP`).
391
//
392
393
ipp_attribute_t *     // O - New attribute
394
ippAddDate(ipp_t             *ipp,  // I - IPP message
395
           ipp_tag_t         group, // I - IPP group
396
     const char        *name, // I - Name of attribute
397
     const ipp_uchar_t *value)  // I - Value
398
0
{
399
0
  ipp_attribute_t *attr;    // New attribute
400
401
402
0
  DEBUG_printf("ippAddDate(ipp=%p, group=%02x(%s), name=\"%s\", value=%p)", (void *)ipp, group, ippTagString(group), name, (void *)value);
403
404
  // Range check input...
405
0
  if (!ipp || !name || !value || group < IPP_TAG_ZERO || group == IPP_TAG_END || group >= IPP_TAG_UNSUPPORTED_VALUE)
406
0
    return (NULL);
407
408
  // Create the attribute...
409
0
  if ((attr = ipp_add_attr(ipp, name, group, IPP_TAG_DATE, 1)) == NULL)
410
0
    return (NULL);
411
412
0
  memcpy(attr->values[0].date, value, 11);
413
414
0
  return (attr);
415
0
}
416
417
418
//
419
// 'ippAddInteger()' - Add a integer attribute to an IPP message.
420
//
421
// This function adds an integer or enum attribute to an IPP message.
422
//
423
// The "ipp" argument refers to an IPP message previously created using
424
// the @link ippNew@, @link ippNewRequest@, or  @link ippNewResponse@ functions.
425
//
426
// The "group" argument specifies the IPP attribute group tag: none
427
// (`IPP_TAG_ZERO`, for member attributes), document (`IPP_TAG_DOCUMENT`),
428
// event notification (`IPP_TAG_EVENT_NOTIFICATION`), operation
429
// (`IPP_TAG_OPERATION`), printer (`IPP_TAG_PRINTER`), subscription
430
// (`IPP_TAG_SUBSCRIPTION`), or unsupported (`IPP_TAG_UNSUPPORTED_GROUP`).
431
//
432
// Supported values include enum (`IPP_TAG_ENUM`) and integer
433
// (`IPP_TAG_INTEGER`).
434
//
435
436
ipp_attribute_t *     // O - New attribute
437
ippAddInteger(ipp_t      *ipp,    // I - IPP message
438
              ipp_tag_t  group,   // I - IPP group
439
        ipp_tag_t  value_tag, // I - Type of attribute
440
              const char *name,   // I - Name of attribute
441
              int        value)   // I - Value of attribute
442
0
{
443
0
  ipp_attribute_t *attr;    // New attribute
444
445
446
0
  DEBUG_printf("ippAddInteger(ipp=%p, group=%02x(%s), type=%02x(%s), name=\"%s\", value=%d)", (void *)ipp, group, ippTagString(group), value_tag, ippTagString(value_tag), name, value);
447
448
0
  value_tag &= IPP_TAG_CUPS_MASK;
449
450
  // Special-case for legacy usage: map out-of-band attributes to new ippAddOutOfBand function...
451
0
  if (value_tag >= IPP_TAG_UNSUPPORTED_VALUE && value_tag <= IPP_TAG_ADMINDEFINE)
452
0
    return (ippAddOutOfBand(ipp, group, value_tag, name));
453
454
  // Range check input...
455
0
  if (!ipp || !name || group < IPP_TAG_ZERO || group == IPP_TAG_END || group >= IPP_TAG_UNSUPPORTED_VALUE || (value_tag != IPP_TAG_INTEGER && value_tag != IPP_TAG_ENUM))
456
0
    return (NULL);
457
458
  // Create the attribute...
459
0
  if ((attr = ipp_add_attr(ipp, name, group, value_tag, 1)) == NULL)
460
0
    return (NULL);
461
462
0
  attr->values[0].integer = value;
463
464
0
  return (attr);
465
0
}
466
467
468
//
469
// 'ippAddIntegers()' - Add an array of integer values.
470
//
471
// The "ipp" argument refers to an IPP message previously created using
472
// the @link ippNew@, @link ippNewRequest@, or  @link ippNewResponse@ functions.
473
//
474
// The "group" argument specifies the IPP attribute group tag: none
475
// (`IPP_TAG_ZERO`, for member attributes), document (`IPP_TAG_DOCUMENT`),
476
// event notification (`IPP_TAG_EVENT_NOTIFICATION`), operation
477
// (`IPP_TAG_OPERATION`), printer (`IPP_TAG_PRINTER`), subscription
478
// (`IPP_TAG_SUBSCRIPTION`), or unsupported (`IPP_TAG_UNSUPPORTED_GROUP`).
479
//
480
// Supported values include enum (`IPP_TAG_ENUM`) and integer
481
// (`IPP_TAG_INTEGER`).
482
//
483
484
ipp_attribute_t *     // O - New attribute
485
ippAddIntegers(ipp_t      *ipp,   // I - IPP message
486
               ipp_tag_t  group,  // I - IPP group
487
         ipp_tag_t  value_tag,  // I - Type of attribute
488
         const char *name,  // I - Name of attribute
489
         size_t     num_values, // I - Number of values
490
         const int  *values)  // I - Values
491
0
{
492
0
  size_t    i;    // Looping var
493
0
  ipp_attribute_t *attr;    // New attribute
494
0
  _ipp_value_t    *value;   // Current value
495
496
497
0
  DEBUG_printf("ippAddIntegers(ipp=%p, group=%02x(%s), type=%02x(%s), name=\"%s\", num_values=%u, values=%p)", (void *)ipp, group, ippTagString(group), value_tag, ippTagString(value_tag), name, (unsigned)num_values, (void *)values);
498
499
0
  value_tag &= IPP_TAG_CUPS_MASK;
500
501
  // Range check input...
502
0
  if (!ipp || !name || group < IPP_TAG_ZERO || group == IPP_TAG_END || group >= IPP_TAG_UNSUPPORTED_VALUE || (value_tag != IPP_TAG_INTEGER && value_tag != IPP_TAG_ENUM) || num_values < 1)
503
0
    return (NULL);
504
505
  // Create the attribute...
506
0
  if ((attr = ipp_add_attr(ipp, name, group, value_tag, num_values)) == NULL)
507
0
    return (NULL);
508
509
0
  if (values)
510
0
  {
511
0
    for (i = num_values, value = attr->values; i > 0; i --, value ++)
512
0
      value->integer = *values++;
513
0
  }
514
515
0
  return (attr);
516
0
}
517
518
519
//
520
// 'ippAddOctetString()' - Add an octetString value to an IPP message.
521
//
522
// The "ipp" argument refers to an IPP message previously created using
523
// the @link ippNew@, @link ippNewRequest@, or  @link ippNewResponse@ functions.
524
//
525
// The "group" argument specifies the IPP attribute group tag: none
526
// (`IPP_TAG_ZERO`, for member attributes), document (`IPP_TAG_DOCUMENT`),
527
// event notification (`IPP_TAG_EVENT_NOTIFICATION`), operation
528
// (`IPP_TAG_OPERATION`), printer (`IPP_TAG_PRINTER`), subscription
529
// (`IPP_TAG_SUBSCRIPTION`), or unsupported (`IPP_TAG_UNSUPPORTED_GROUP`).
530
//
531
532
ipp_attribute_t *     // O - New attribute
533
ippAddOctetString(ipp_t      *ipp,  // I - IPP message
534
                  ipp_tag_t  group, // I - IPP group
535
                  const char *name, // I - Name of attribute
536
                  const void *data, // I - octetString data
537
      size_t     datalen) // I - Length of data in bytes
538
0
{
539
0
  ipp_attribute_t *attr;    // New attribute
540
541
542
  // Range check input...
543
0
  if (!ipp || !name || group < IPP_TAG_ZERO || group == IPP_TAG_END || group >= IPP_TAG_UNSUPPORTED_VALUE || datalen > IPP_MAX_LENGTH)
544
0
    return (NULL);
545
546
0
  if ((attr = ipp_add_attr(ipp, name, group, IPP_TAG_STRING, 1)) == NULL)
547
0
    return (NULL);
548
549
  // Initialize the attribute data...
550
0
  attr->values[0].unknown.length = datalen;
551
552
0
  if (data)
553
0
  {
554
0
    if ((attr->values[0].unknown.data = malloc((size_t)datalen)) == NULL)
555
0
    {
556
0
      ippDeleteAttribute(ipp, attr);
557
0
      return (NULL);
558
0
    }
559
560
0
    memcpy(attr->values[0].unknown.data, data, (size_t)datalen);
561
0
  }
562
563
  // Return the new attribute...
564
0
  return (attr);
565
0
}
566
567
568
//
569
// 'ippAddOutOfBand()' - Add an out-of-band value to an IPP message.
570
//
571
// The "ipp" argument refers to an IPP message previously created using
572
// the @link ippNew@, @link ippNewRequest@, or  @link ippNewResponse@ functions.
573
//
574
// The "group" argument specifies the IPP attribute group tag: none
575
// (`IPP_TAG_ZERO`, for member attributes), document (`IPP_TAG_DOCUMENT`),
576
// event notification (`IPP_TAG_EVENT_NOTIFICATION`), operation
577
// (`IPP_TAG_OPERATION`), printer (`IPP_TAG_PRINTER`), subscription
578
// (`IPP_TAG_SUBSCRIPTION`), or unsupported (`IPP_TAG_UNSUPPORTED_GROUP`).
579
//
580
// Supported out-of-band values include unsupported-value
581
// (`IPP_TAG_UNSUPPORTED_VALUE`), default (`IPP_TAG_DEFAULT`), unknown
582
// (`IPP_TAG_UNKNOWN`), no-value (`IPP_TAG_NOVALUE`), not-settable
583
// (`IPP_TAG_NOTSETTABLE`), delete-attribute (`IPP_TAG_DELETEATTR`), and
584
// admin-define (`IPP_TAG_ADMINDEFINE`).
585
//
586
587
ipp_attribute_t *     // O - New attribute
588
ippAddOutOfBand(ipp_t      *ipp,  // I - IPP message
589
                ipp_tag_t  group, // I - IPP group
590
                ipp_tag_t  value_tag, // I - Type of attribute
591
    const char *name) // I - Name of attribute
592
0
{
593
0
  DEBUG_printf("ippAddOutOfBand(ipp=%p, group=%02x(%s), value_tag=%02x(%s), name=\"%s\")", (void *)ipp, group, ippTagString(group), value_tag, ippTagString(value_tag), name);
594
595
0
  value_tag &= IPP_TAG_CUPS_MASK;
596
597
  // Range check input...
598
0
  if (!ipp || !name || group < IPP_TAG_ZERO || group == IPP_TAG_END || group >= IPP_TAG_UNSUPPORTED_VALUE || (value_tag != IPP_TAG_UNSUPPORTED_VALUE && value_tag != IPP_TAG_DEFAULT && value_tag != IPP_TAG_UNKNOWN && value_tag != IPP_TAG_NOVALUE && value_tag != IPP_TAG_NOTSETTABLE && value_tag != IPP_TAG_DELETEATTR && value_tag != IPP_TAG_ADMINDEFINE))
599
0
    return (NULL);
600
601
  // Create the attribute...
602
0
  return (ipp_add_attr(ipp, name, group, value_tag, 1));
603
0
}
604
605
606
//
607
// 'ippAddRange()' - Add a range of values to an IPP message.
608
//
609
// The "ipp" argument refers to an IPP message previously created using
610
// the @link ippNew@, @link ippNewRequest@, or  @link ippNewResponse@ functions.
611
//
612
// The "group" argument specifies the IPP attribute group tag: none
613
// (`IPP_TAG_ZERO`, for member attributes), document (`IPP_TAG_DOCUMENT`),
614
// event notification (`IPP_TAG_EVENT_NOTIFICATION`), operation
615
// (`IPP_TAG_OPERATION`), printer (`IPP_TAG_PRINTER`), subscription
616
// (`IPP_TAG_SUBSCRIPTION`), or unsupported (`IPP_TAG_UNSUPPORTED_GROUP`).
617
//
618
// The "lower" argument must be less than or equal to the `upper" argument.
619
//
620
621
ipp_attribute_t *     // O - New attribute
622
ippAddRange(ipp_t      *ipp,    // I - IPP message
623
            ipp_tag_t  group,   // I - IPP group
624
      const char *name,   // I - Name of attribute
625
      int        lower,   // I - Lower value
626
      int        upper)   // I - Upper value
627
0
{
628
0
  ipp_attribute_t *attr;    // New attribute
629
630
631
0
  DEBUG_printf("ippAddRange(ipp=%p, group=%02x(%s), name=\"%s\", lower=%d, upper=%d)", (void *)ipp, group, ippTagString(group), name, lower, upper);
632
633
  // Range check input...
634
0
  if (!ipp || !name || group < IPP_TAG_ZERO || group == IPP_TAG_END || group >= IPP_TAG_UNSUPPORTED_VALUE)
635
0
    return (NULL);
636
637
  // Create the attribute...
638
0
  if ((attr = ipp_add_attr(ipp, name, group, IPP_TAG_RANGE, 1)) == NULL)
639
0
    return (NULL);
640
641
0
  attr->values[0].range.lower = lower;
642
0
  attr->values[0].range.upper = upper;
643
644
0
  return (attr);
645
0
}
646
647
648
//
649
// 'ippAddRanges()' - Add ranges of values to an IPP message.
650
//
651
// The "ipp" argument refers to an IPP message previously created using
652
// the @link ippNew@, @link ippNewRequest@, or  @link ippNewResponse@ functions.
653
//
654
// The "group" argument specifies the IPP attribute group tag: none
655
// (`IPP_TAG_ZERO`, for member attributes), document (`IPP_TAG_DOCUMENT`),
656
// event notification (`IPP_TAG_EVENT_NOTIFICATION`), operation
657
// (`IPP_TAG_OPERATION`), printer (`IPP_TAG_PRINTER`), subscription
658
// (`IPP_TAG_SUBSCRIPTION`), or unsupported (`IPP_TAG_UNSUPPORTED_GROUP`).
659
//
660
661
ipp_attribute_t *     // O - New attribute
662
ippAddRanges(ipp_t      *ipp,   // I - IPP message
663
             ipp_tag_t  group,    // I - IPP group
664
       const char *name,    // I - Name of attribute
665
       size_t     num_values, // I - Number of values
666
       const int  *lower,   // I - Lower values
667
       const int  *upper)   // I - Upper values
668
0
{
669
0
  size_t    i;    // Looping var
670
0
  ipp_attribute_t *attr;    // New attribute
671
0
  _ipp_value_t    *value;   // Current value
672
673
674
0
  DEBUG_printf("ippAddRanges(ipp=%p, group=%02x(%s), name=\"%s\", num_values=%u, lower=%p, upper=%p)", (void *)ipp, group, ippTagString(group), name, (unsigned)num_values, (void *)lower, (void *)upper);
675
676
  // Range check input...
677
0
  if (!ipp || !name || group < IPP_TAG_ZERO || group == IPP_TAG_END || group >= IPP_TAG_UNSUPPORTED_VALUE || num_values < 1)
678
0
    return (NULL);
679
680
  // Create the attribute...
681
0
  if ((attr = ipp_add_attr(ipp, name, group, IPP_TAG_RANGE, num_values)) == NULL)
682
0
    return (NULL);
683
684
0
  if (lower && upper)
685
0
  {
686
0
    for (i = num_values, value = attr->values; i > 0; i --, value ++)
687
0
    {
688
0
      value->range.lower = *lower++;
689
0
      value->range.upper = *upper++;
690
0
    }
691
0
  }
692
693
0
  return (attr);
694
0
}
695
696
697
//
698
// 'ippAddResolution()' - Add a resolution value to an IPP message.
699
//
700
// The "ipp" argument refers to an IPP message previously created using
701
// the @link ippNew@, @link ippNewRequest@, or  @link ippNewResponse@ functions.
702
//
703
// The "group" argument specifies the IPP attribute group tag: none
704
// (`IPP_TAG_ZERO`, for member attributes), document (`IPP_TAG_DOCUMENT`),
705
// event notification (`IPP_TAG_EVENT_NOTIFICATION`), operation
706
// (`IPP_TAG_OPERATION`), printer (`IPP_TAG_PRINTER`), subscription
707
// (`IPP_TAG_SUBSCRIPTION`), or unsupported (`IPP_TAG_UNSUPPORTED_GROUP`).
708
//
709
710
ipp_attribute_t *     // O - New attribute
711
ippAddResolution(ipp_t      *ipp, // I - IPP message
712
           ipp_tag_t  group,  // I - IPP group
713
     const char *name,  // I - Name of attribute
714
     ipp_res_t  units,  // I - Units for resolution
715
     int        xres, // I - X resolution
716
     int        yres) // I - Y resolution
717
0
{
718
0
  ipp_attribute_t *attr;    // New attribute
719
720
721
0
  DEBUG_printf("ippAddResolution(ipp=%p, group=%02x(%s), name=\"%s\", units=%d, xres=%d, yres=%d)", (void *)ipp, group, ippTagString(group), name, units, xres, yres);
722
723
  // Range check input...
724
0
  if (!ipp || !name || group < IPP_TAG_ZERO || group == IPP_TAG_END || group >= IPP_TAG_UNSUPPORTED_VALUE || units < IPP_RES_PER_INCH || units > IPP_RES_PER_CM || xres < 0 || yres < 0)
725
0
    return (NULL);
726
727
  // Create the attribute...
728
0
  if ((attr = ipp_add_attr(ipp, name, group, IPP_TAG_RESOLUTION, 1)) == NULL)
729
0
    return (NULL);
730
731
0
  attr->values[0].resolution.xres  = xres;
732
0
  attr->values[0].resolution.yres  = yres;
733
0
  attr->values[0].resolution.units = units;
734
735
0
  return (attr);
736
0
}
737
738
739
//
740
// 'ippAddResolutions()' - Add resolution values to an IPP message.
741
//
742
// The "ipp" argument refers to an IPP message previously created using
743
// the @link ippNew@, @link ippNewRequest@, or  @link ippNewResponse@ functions.
744
//
745
// The "group" argument specifies the IPP attribute group tag: none
746
// (`IPP_TAG_ZERO`, for member attributes), document (`IPP_TAG_DOCUMENT`),
747
// event notification (`IPP_TAG_EVENT_NOTIFICATION`), operation
748
// (`IPP_TAG_OPERATION`), printer (`IPP_TAG_PRINTER`), subscription
749
// (`IPP_TAG_SUBSCRIPTION`), or unsupported (`IPP_TAG_UNSUPPORTED_GROUP`).
750
//
751
752
ipp_attribute_t *     // O - New attribute
753
ippAddResolutions(ipp_t      *ipp,  // I - IPP message
754
            ipp_tag_t  group, // I - IPP group
755
      const char *name, // I - Name of attribute
756
      size_t     num_values,// I - Number of values
757
      ipp_res_t  units, // I - Units for resolution
758
      const int  *xres, // I - X resolutions
759
      const int  *yres) // I - Y resolutions
760
0
{
761
0
  size_t    i;    // Looping var
762
0
  ipp_attribute_t *attr;    // New attribute
763
0
  _ipp_value_t    *value;   // Current value
764
765
766
0
  DEBUG_printf("ippAddResolutions(ipp=%p, group=%02x(%s), name=\"%s\", num_value=%u, units=%d, xres=%p, yres=%p)", (void *)ipp, group, ippTagString(group), name, (unsigned)num_values, units, (void *)xres, (void *)yres);
767
768
  // Range check input...
769
0
  if (!ipp || !name || group < IPP_TAG_ZERO || group == IPP_TAG_END || group >= IPP_TAG_UNSUPPORTED_VALUE || num_values < 1 || units < IPP_RES_PER_INCH || units > IPP_RES_PER_CM)
770
0
    return (NULL);
771
772
  // Create the attribute...
773
0
  if ((attr = ipp_add_attr(ipp, name, group, IPP_TAG_RESOLUTION, num_values)) == NULL)
774
0
    return (NULL);
775
776
0
  if (xres && yres)
777
0
  {
778
0
    for (i = num_values, value = attr->values; i > 0; i --, value ++)
779
0
    {
780
0
      value->resolution.xres  = *xres++;
781
0
      value->resolution.yres  = *yres++;
782
0
      value->resolution.units = units;
783
0
    }
784
0
  }
785
786
0
  return (attr);
787
0
}
788
789
790
//
791
// 'ippAddSeparator()' - Add a group separator to an IPP message.
792
//
793
// The "ipp" argument refers to an IPP message previously created using
794
// the @link ippNew@, @link ippNewRequest@, or  @link ippNewResponse@ functions.
795
//
796
797
ipp_attribute_t *     // O - New attribute
798
ippAddSeparator(ipp_t *ipp)   // I - IPP message
799
990k
{
800
990k
  DEBUG_printf("ippAddSeparator(ipp=%p)", (void *)ipp);
801
802
  // Range check input...
803
990k
  if (!ipp)
804
0
    return (NULL);
805
806
  // Create the attribute...
807
990k
  return (ipp_add_attr(ipp, NULL, IPP_TAG_ZERO, IPP_TAG_ZERO, 0));
808
990k
}
809
810
811
//
812
// 'ippAddString()' - Add a language-encoded string to an IPP message.
813
//
814
// The "ipp" argument refers to an IPP message previously created using
815
// the @link ippNew@, @link ippNewRequest@, or  @link ippNewResponse@ functions.
816
//
817
// The "group" argument specifies the IPP attribute group tag: none
818
// (`IPP_TAG_ZERO`, for member attributes), document (`IPP_TAG_DOCUMENT`),
819
// event notification (`IPP_TAG_EVENT_NOTIFICATION`), operation
820
// (`IPP_TAG_OPERATION`), printer (`IPP_TAG_PRINTER`), subscription
821
// (`IPP_TAG_SUBSCRIPTION`), or unsupported (`IPP_TAG_UNSUPPORTED_GROUP`).
822
//
823
// Supported string values include charset (`IPP_TAG_CHARSET`), keyword
824
// (`IPP_TAG_KEYWORD`), language (`IPP_TAG_LANGUAGE`), mimeMediaType
825
// (`IPP_TAG_MIMETYPE`), name (`IPP_TAG_NAME`), nameWithLanguage
826
// (@code IPP_TAG_NAMELANG), text (`IPP_TAG_TEXT`), textWithLanguage
827
// (`IPP_TAG_TEXTLANG`), uri (`IPP_TAG_URI`), and uriScheme
828
// (`IPP_TAG_URISCHEME`).
829
//
830
// The "language" argument must be non-`NULL` for nameWithLanguage and
831
// textWithLanguage string values and must be `NULL` for all other string values.
832
//
833
834
ipp_attribute_t *     // O - New attribute
835
ippAddString(ipp_t      *ipp,   // I - IPP message
836
             ipp_tag_t  group,    // I - IPP group
837
       ipp_tag_t  value_tag,  // I - Type of attribute
838
             const char *name,    // I - Name of attribute
839
             const char *language,  // I - Language code
840
             const char *value)   // I - Value
841
5.64k
{
842
5.64k
  ipp_tag_t   temp_tag; // Temporary value tag (masked)
843
5.64k
  ipp_attribute_t *attr;    // New attribute
844
5.64k
  char      code[IPP_MAX_LANGUAGE];
845
          // Charset/language code buffer
846
847
848
5.64k
  DEBUG_printf("ippAddString(ipp=%p, group=%02x(%s), value_tag=%02x(%s), name=\"%s\", language=\"%s\", value=\"%s\")", (void *)ipp, group, ippTagString(group), value_tag, ippTagString(value_tag), name, language, value);
849
850
  // Range check input...
851
5.64k
  temp_tag = (ipp_tag_t)((int)value_tag & IPP_TAG_CUPS_MASK);
852
853
#if 0
854
  if (!ipp || !name || group < IPP_TAG_ZERO ||
855
      group == IPP_TAG_END || group >= IPP_TAG_UNSUPPORTED_VALUE ||
856
      (temp_tag < IPP_TAG_TEXT && temp_tag != IPP_TAG_TEXTLANG &&
857
       temp_tag != IPP_TAG_NAMELANG) || temp_tag > IPP_TAG_MIMETYPE)
858
    return (NULL);
859
860
  if ((temp_tag == IPP_TAG_TEXTLANG || temp_tag == IPP_TAG_NAMELANG)
861
          != (language != NULL))
862
    return (NULL);
863
#else
864
5.64k
  if (!ipp || !name || group < IPP_TAG_ZERO || group == IPP_TAG_END || group >= IPP_TAG_UNSUPPORTED_VALUE)
865
0
    return (NULL);
866
5.64k
#endif // 0
867
868
  // See if we need to map charset, language, or locale values...
869
5.64k
  if (language && ((int)value_tag & IPP_TAG_CUPS_CONST) && strcmp(language, ipp_lang_code(language, code, sizeof(code))))
870
0
    value_tag = temp_tag;   // Don't do a fast copy
871
5.64k
  else if (value && value_tag == (ipp_tag_t)(IPP_TAG_CHARSET | IPP_TAG_CUPS_CONST) && strcmp(value, ipp_get_code(value, code, sizeof(code))))
872
0
    value_tag = temp_tag;   // Don't do a fast copy
873
5.64k
  else if (value && value_tag == (ipp_tag_t)(IPP_TAG_LANGUAGE | IPP_TAG_CUPS_CONST) && strcmp(value, ipp_lang_code(value, code, sizeof(code))))
874
2.82k
    value_tag = temp_tag;   // Don't do a fast copy
875
876
  // Create the attribute...
877
5.64k
  if ((attr = ipp_add_attr(ipp, name, group, value_tag, 1)) == NULL)
878
0
    return (NULL);
879
880
  // Initialize the attribute data...
881
5.64k
  if ((int)value_tag & IPP_TAG_CUPS_CONST)
882
2.82k
  {
883
2.82k
    attr->values[0].string.language = (char *)language;
884
2.82k
    attr->values[0].string.text     = (char *)value;
885
2.82k
  }
886
2.82k
  else
887
2.82k
  {
888
2.82k
    if (language)
889
0
      attr->values[0].string.language = _cupsStrAlloc(ipp_lang_code(language, code, sizeof(code)));
890
891
2.82k
    if (value)
892
2.82k
    {
893
2.82k
      if (value_tag == IPP_TAG_CHARSET)
894
0
  attr->values[0].string.text = _cupsStrAlloc(ipp_get_code(value, code, sizeof(code)));
895
2.82k
      else if (value_tag == IPP_TAG_LANGUAGE)
896
2.82k
  attr->values[0].string.text = _cupsStrAlloc(ipp_lang_code(value, code, sizeof(code)));
897
0
      else
898
0
  attr->values[0].string.text = _cupsStrAlloc(value);
899
2.82k
    }
900
2.82k
  }
901
902
5.64k
  return (attr);
903
5.64k
}
904
905
906
//
907
// 'ippAddStringf()' - Add a formatted string to an IPP message.
908
//
909
// The "ipp" argument refers to an IPP message previously created using
910
// the @link ippNew@, @link ippNewRequest@, or  @link ippNewResponse@ functions.
911
//
912
// The "group" argument specifies the IPP attribute group tag: none
913
// (`IPP_TAG_ZERO`, for member attributes), document
914
// (`IPP_TAG_DOCUMENT`), event notification
915
// (`IPP_TAG_EVENT_NOTIFICATION`), operation (`IPP_TAG_OPERATION`),
916
// printer (`IPP_TAG_PRINTER`), subscription (`IPP_TAG_SUBSCRIPTION`),
917
// or unsupported (`IPP_TAG_UNSUPPORTED_GROUP`).
918
//
919
// Supported string values include charset (`IPP_TAG_CHARSET`), keyword
920
// (`IPP_TAG_KEYWORD`), language (`IPP_TAG_LANGUAGE`), mimeMediaType
921
// (`IPP_TAG_MIMETYPE`), name (`IPP_TAG_NAME`), nameWithLanguage
922
// (@code IPP_TAG_NAMELANG), text (`IPP_TAG_TEXT`), textWithLanguage
923
// (`IPP_TAG_TEXTLANG`), uri (`IPP_TAG_URI`), and uriScheme
924
// (`IPP_TAG_URISCHEME`).
925
//
926
// The "language" argument must be non-`NULL` for nameWithLanguage
927
// and textWithLanguage string values and must be `NULL` for all other
928
// string values.
929
//
930
// The "format" argument uses formatting characters compatible with the
931
// printf family of standard functions.  Additional arguments follow it as
932
// needed.  The formatted string is truncated as needed to the maximum length of
933
// the corresponding value type.
934
//
935
936
ipp_attribute_t *     // O - New attribute
937
ippAddStringf(ipp_t      *ipp,    // I - IPP message
938
              ipp_tag_t  group,   // I - IPP group
939
        ipp_tag_t  value_tag, // I - Type of attribute
940
        const char *name,   // I - Name of attribute
941
        const char *language, // I - Language code (`NULL` for default)
942
        const char *format, // I - Printf-style format string
943
        ...)      // I - Additional arguments as needed
944
0
{
945
0
  ipp_attribute_t *attr;    // New attribute
946
0
  va_list   ap;   // Argument pointer
947
948
949
0
  va_start(ap, format);
950
0
  attr = ippAddStringfv(ipp, group, value_tag, name, language, format, ap);
951
0
  va_end(ap);
952
953
0
  return (attr);
954
0
}
955
956
957
//
958
// 'ippAddStringfv()' - Add a formatted string to an IPP message.
959
//
960
// The "ipp" argument refers to an IPP message previously created using
961
// the @link ippNew@, @link ippNewRequest@, or  @link ippNewResponse@ functions.
962
//
963
// The "group" argument specifies the IPP attribute group tag: none
964
// (`IPP_TAG_ZERO`, for member attributes), document
965
// (`IPP_TAG_DOCUMENT`), event notification
966
// (`IPP_TAG_EVENT_NOTIFICATION`), operation (`IPP_TAG_OPERATION`),
967
// printer (`IPP_TAG_PRINTER`), subscription (`IPP_TAG_SUBSCRIPTION`),
968
// or unsupported (`IPP_TAG_UNSUPPORTED_GROUP`).
969
//
970
// Supported string values include charset (`IPP_TAG_CHARSET`), keyword
971
// (`IPP_TAG_KEYWORD`), language (`IPP_TAG_LANGUAGE`), mimeMediaType
972
// (`IPP_TAG_MIMETYPE`), name (`IPP_TAG_NAME`), nameWithLanguage
973
// (@code IPP_TAG_NAMELANG), text (`IPP_TAG_TEXT`), textWithLanguage
974
// (`IPP_TAG_TEXTLANG`), uri (`IPP_TAG_URI`), and uriScheme
975
// (`IPP_TAG_URISCHEME`).
976
//
977
// The "language" argument must be non-`NULL` for nameWithLanguage
978
// and textWithLanguage string values and must be `NULL` for all other
979
// string values.
980
//
981
// The "format" argument uses formatting characters compatible with the
982
// printf family of standard functions.  Additional arguments are passed in the
983
// stdarg pointer "ap".  The formatted string is truncated as needed to the
984
// maximum length of the corresponding value type.
985
//
986
987
ipp_attribute_t *     // O - New attribute
988
ippAddStringfv(ipp_t      *ipp,   // I - IPP message
989
               ipp_tag_t  group,  // I - IPP group
990
         ipp_tag_t  value_tag,  // I - Type of attribute
991
         const char *name,  // I - Name of attribute
992
         const char *language,  // I - Language code (`NULL` for default)
993
         const char *format,  // I - Printf-style format string
994
         va_list    ap)   // I - Additional arguments
995
0
{
996
0
  char    buffer[IPP_MAX_TEXT + 4];
997
          // Formatted text string
998
0
  ssize_t bytes,      // Length of formatted value
999
0
    max_bytes;    // Maximum number of bytes for value
1000
1001
1002
  // Range check input...
1003
0
  if (!ipp || !name || group < IPP_TAG_ZERO || group == IPP_TAG_END || group >= IPP_TAG_UNSUPPORTED_VALUE || (value_tag < IPP_TAG_TEXT && value_tag != IPP_TAG_TEXTLANG && value_tag != IPP_TAG_NAMELANG) || value_tag > IPP_TAG_MIMETYPE || !format)
1004
0
    return (NULL);
1005
1006
0
  if ((value_tag == IPP_TAG_TEXTLANG || value_tag == IPP_TAG_NAMELANG) != (language != NULL))
1007
0
    return (NULL);
1008
1009
  // Format the string...
1010
0
  if (!strcmp(format, "%s"))
1011
0
  {
1012
    // Optimize the simple case...
1013
0
    const char *s = va_arg(ap, char *);
1014
1015
0
    if (!s)
1016
0
      s = "(null)";
1017
1018
0
    bytes = (ssize_t)strlen(s);
1019
0
    cupsCopyString(buffer, s, sizeof(buffer));
1020
0
  }
1021
0
  else
1022
0
  {
1023
    // Do a full formatting of the message...
1024
0
    if ((bytes = vsnprintf(buffer, sizeof(buffer), format, ap)) < 0)
1025
0
      return (NULL);
1026
0
  }
1027
1028
  // Limit the length of the string...
1029
0
  switch (value_tag)
1030
0
  {
1031
0
    default :
1032
0
    case IPP_TAG_TEXT :
1033
0
    case IPP_TAG_TEXTLANG :
1034
0
        max_bytes = IPP_MAX_TEXT;
1035
0
        break;
1036
1037
0
    case IPP_TAG_NAME :
1038
0
    case IPP_TAG_NAMELANG :
1039
0
        max_bytes = IPP_MAX_NAME;
1040
0
        break;
1041
1042
0
    case IPP_TAG_CHARSET :
1043
0
        max_bytes = IPP_MAX_CHARSET;
1044
0
        break;
1045
1046
0
    case IPP_TAG_KEYWORD :
1047
0
        max_bytes = IPP_MAX_KEYWORD;
1048
0
        break;
1049
1050
0
    case IPP_TAG_LANGUAGE :
1051
0
        max_bytes = IPP_MAX_LANGUAGE;
1052
0
        break;
1053
1054
0
    case IPP_TAG_MIMETYPE :
1055
0
        max_bytes = IPP_MAX_MIMETYPE;
1056
0
        break;
1057
1058
0
    case IPP_TAG_URI :
1059
0
        max_bytes = IPP_MAX_URI;
1060
0
        break;
1061
1062
0
    case IPP_TAG_URISCHEME :
1063
0
        max_bytes = IPP_MAX_URISCHEME;
1064
0
        break;
1065
0
  }
1066
1067
0
  if (bytes >= max_bytes)
1068
0
  {
1069
    // Safely truncate the UTF-8 string...
1070
0
    char  *bufmax,    // Buffer at max_bytes
1071
0
    *bufptr;    // Pointer into buffer
1072
1073
0
    bufptr = buffer + strlen(buffer) - 1;
1074
0
    bufmax = buffer + max_bytes - 1;
1075
1076
0
    while (bufptr > bufmax)
1077
0
    {
1078
0
      if (*bufptr & 0x80)
1079
0
      {
1080
0
        while ((*bufptr & 0xc0) == 0x80 && bufptr > buffer)
1081
0
          bufptr --;
1082
0
      }
1083
1084
0
      if (bufptr > buffer)
1085
0
        bufptr --;
1086
0
    }
1087
1088
0
    *bufptr = '\0';
1089
0
  }
1090
1091
  // Add the formatted string and return...
1092
0
  return (ippAddString(ipp, group, value_tag, name, language, buffer));
1093
0
}
1094
1095
1096
//
1097
// 'ippAddStrings()' - Add language-encoded strings to an IPP message.
1098
//
1099
// The "ipp" argument refers to an IPP message previously created using
1100
// the @link ippNew@, @link ippNewRequest@, or  @link ippNewResponse@ functions.
1101
//
1102
// The "group" argument specifies the IPP attribute group tag: none
1103
// (`IPP_TAG_ZERO`, for member attributes), document (`IPP_TAG_DOCUMENT`),
1104
// event notification (`IPP_TAG_EVENT_NOTIFICATION`), operation
1105
// (`IPP_TAG_OPERATION`), printer (`IPP_TAG_PRINTER`), subscription
1106
// (`IPP_TAG_SUBSCRIPTION`), or unsupported (`IPP_TAG_UNSUPPORTED_GROUP`).
1107
//
1108
// Supported string values include charset (`IPP_TAG_CHARSET`), keyword
1109
// (`IPP_TAG_KEYWORD`), language (`IPP_TAG_LANGUAGE`), mimeMediaType
1110
// (`IPP_TAG_MIMETYPE`), name (`IPP_TAG_NAME`), nameWithLanguage
1111
// (@code IPP_TAG_NAMELANG), text (`IPP_TAG_TEXT`), textWithLanguage
1112
// (`IPP_TAG_TEXTLANG`), uri (`IPP_TAG_URI`), and uriScheme
1113
// (`IPP_TAG_URISCHEME`).
1114
//
1115
// The "language" argument must be non-`NULL` for nameWithLanguage and
1116
// textWithLanguage string values and must be `NULL` for all other string values.
1117
//
1118
1119
ipp_attribute_t *     // O - New attribute
1120
ippAddStrings(
1121
    ipp_t              *ipp,    // I - IPP message
1122
    ipp_tag_t          group,   // I - IPP group
1123
    ipp_tag_t          value_tag, // I - Type of attribute
1124
    const char         *name,   // I - Name of attribute
1125
    size_t             num_values,  // I - Number of values
1126
    const char         *language, // I - Language code (`NULL` for default)
1127
    const char * const *values)   // I - Values
1128
0
{
1129
0
  size_t    i;    // Looping var
1130
0
  ipp_tag_t   temp_tag; // Temporary value tag (masked)
1131
0
  ipp_attribute_t *attr;    // New attribute
1132
0
  _ipp_value_t    *value;   // Current value
1133
0
  char      code[32]; // Language/charset value buffer
1134
1135
1136
0
  DEBUG_printf("ippAddStrings(ipp=%p, group=%02x(%s), value_tag=%02x(%s), name=\"%s\", num_values=%u, language=\"%s\", values=%p)", (void *)ipp, group, ippTagString(group), value_tag, ippTagString(value_tag), name, (unsigned)num_values, language, (void *)values);
1137
1138
  // Range check input...
1139
0
  temp_tag = (ipp_tag_t)((int)value_tag & IPP_TAG_CUPS_MASK);
1140
1141
#if 0
1142
  if (!ipp || !name || group < IPP_TAG_ZERO || group == IPP_TAG_END || group >= IPP_TAG_UNSUPPORTED_VALUE || (temp_tag < IPP_TAG_TEXT && temp_tag != IPP_TAG_TEXTLANG && temp_tag != IPP_TAG_NAMELANG) || temp_tag > IPP_TAG_MIMETYPE || num_values < 1)
1143
    return (NULL);
1144
1145
  if ((temp_tag == IPP_TAG_TEXTLANG || temp_tag == IPP_TAG_NAMELANG) != (language != NULL))
1146
    return (NULL);
1147
#else
1148
0
  if (!ipp || !name || group < IPP_TAG_ZERO || group == IPP_TAG_END || group >= IPP_TAG_UNSUPPORTED_VALUE || num_values < 1)
1149
0
    return (NULL);
1150
0
#endif // 0
1151
1152
  // See if we need to map charset, language, or locale values...
1153
0
  if (language && ((int)value_tag & IPP_TAG_CUPS_CONST) && strcmp(language, ipp_lang_code(language, code, sizeof(code))))
1154
0
  {
1155
0
    value_tag = temp_tag;   // Don't do a fast copy
1156
0
  }
1157
0
  else if (values && value_tag == (ipp_tag_t)(IPP_TAG_CHARSET | IPP_TAG_CUPS_CONST))
1158
0
  {
1159
0
    for (i = 0; i < num_values; i ++)
1160
0
    {
1161
0
      if (strcmp(values[i], ipp_get_code(values[i], code, sizeof(code))))
1162
0
      {
1163
0
  value_tag = temp_tag;   // Don't do a fast copy
1164
0
        break;
1165
0
      }
1166
0
    }
1167
0
  }
1168
0
  else if (values && value_tag == (ipp_tag_t)(IPP_TAG_LANGUAGE | IPP_TAG_CUPS_CONST))
1169
0
  {
1170
0
    for (i = 0; i < num_values; i ++)
1171
0
    {
1172
0
      if (strcmp(values[i], ipp_lang_code(values[i], code, sizeof(code))))
1173
0
      {
1174
0
  value_tag = temp_tag;   // Don't do a fast copy
1175
0
        break;
1176
0
      }
1177
0
    }
1178
0
  }
1179
1180
  // Create the attribute...
1181
0
  if ((attr = ipp_add_attr(ipp, name, group, value_tag, num_values)) == NULL)
1182
0
    return (NULL);
1183
1184
  // Initialize the attribute data...
1185
0
  for (i = num_values, value = attr->values; i > 0; i --, value ++)
1186
0
  {
1187
0
    if (language)
1188
0
    {
1189
0
      if ((int)value_tag & IPP_TAG_CUPS_CONST)
1190
0
  value->string.language = (char *)language;
1191
0
      else
1192
0
  value->string.language = _cupsStrAlloc(ipp_lang_code(language, code, sizeof(code)));
1193
0
    }
1194
1195
0
    if (values)
1196
0
    {
1197
0
      if ((int)value_tag & IPP_TAG_CUPS_CONST)
1198
0
        value->string.text = (char *)*values++;
1199
0
      else if (value_tag == IPP_TAG_CHARSET)
1200
0
  value->string.text = _cupsStrAlloc(ipp_get_code(*values++, code, sizeof(code)));
1201
0
      else if (value_tag == IPP_TAG_LANGUAGE)
1202
0
  value->string.text = _cupsStrAlloc(ipp_lang_code(*values++, code, sizeof(code)));
1203
0
      else
1204
0
  value->string.text = _cupsStrAlloc(*values++);
1205
0
    }
1206
0
  }
1207
1208
0
  return (attr);
1209
0
}
1210
1211
1212
//
1213
// 'ippContainsInteger()' - Determine whether an attribute contains the
1214
//                          specified value or is within the list of ranges.
1215
//
1216
// This function returns `true` when the attribute contains either a matching
1217
// integer or enum value, or the value falls within one of the rangeOfInteger
1218
// values for the attribute.
1219
//
1220
1221
bool          // O - `true` on a match, `false` on no match
1222
ippContainsInteger(
1223
    ipp_attribute_t *attr,    // I - Attribute
1224
    int             value)    // I - Integer/enum value
1225
0
{
1226
0
  size_t  i;      // Looping var
1227
0
  _ipp_value_t  *avalue;    // Current attribute value
1228
1229
1230
  // Range check input...
1231
0
  if (!attr)
1232
0
    return (false);
1233
1234
0
  if (attr->value_tag != IPP_TAG_INTEGER && attr->value_tag != IPP_TAG_ENUM && attr->value_tag != IPP_TAG_RANGE)
1235
0
    return (false);
1236
1237
  // Compare...
1238
0
  if (attr->value_tag == IPP_TAG_RANGE)
1239
0
  {
1240
    // Check ranges...
1241
0
    for (i = attr->num_values, avalue = attr->values; i > 0; i --, avalue ++)
1242
0
    {
1243
0
      if (value >= avalue->range.lower && value <= avalue->range.upper)
1244
0
        return (true);
1245
0
    }
1246
0
  }
1247
0
  else
1248
0
  {
1249
    // Check discrete values...
1250
0
    for (i = attr->num_values, avalue = attr->values; i > 0; i --, avalue ++)
1251
0
    {
1252
0
      if (value == avalue->integer)
1253
0
        return (true);
1254
0
    }
1255
0
  }
1256
1257
0
  return (false);
1258
0
}
1259
1260
1261
//
1262
// 'ippContainsString()' - Determine whether an attribute contains the
1263
//                         specified string value.
1264
//
1265
// This function returns `true` when the attribute contains a matching charset,
1266
// keyword, naturalLanguage, mimeMediaType, name, text, uri, or uriScheme value.
1267
//
1268
1269
bool          // O - `true` on a match, `false` on no match
1270
ippContainsString(
1271
    ipp_attribute_t *attr,    // I - Attribute
1272
    const char      *value)   // I - String value
1273
0
{
1274
0
  size_t  i;      // Looping var
1275
0
  _ipp_value_t  *avalue;    // Current attribute value
1276
1277
1278
0
  DEBUG_printf("ippContainsString(attr=%p, value=\"%s\")", (void *)attr, value);
1279
1280
  // Range check input...
1281
0
  if (!attr || !value)
1282
0
  {
1283
0
    DEBUG_puts("1ippContainsString: Returning false (bad input)");
1284
0
    return (false);
1285
0
  }
1286
1287
  // Compare...
1288
0
  DEBUG_printf("1ippContainsString: attr %s, %s%s with %u values.", attr->name, (attr->value_tag & IPP_TAG_CUPS_CONST) ? "(const)" : "", ippTagString(attr->value_tag), (unsigned)attr->num_values);
1289
1290
0
  switch (attr->value_tag & IPP_TAG_CUPS_MASK)
1291
0
  {
1292
0
    case IPP_TAG_CHARSET :
1293
0
    case IPP_TAG_KEYWORD :
1294
0
    case IPP_TAG_LANGUAGE :
1295
0
    case IPP_TAG_URI :
1296
0
    case IPP_TAG_URISCHEME :
1297
0
  for (i = attr->num_values, avalue = attr->values; i > 0; i --, avalue ++)
1298
0
  {
1299
0
    DEBUG_printf("1ippContainsString: value[%u]=\"%s\"", (unsigned)(attr->num_values - i), avalue->string.text);
1300
1301
0
    if (avalue->string.text && !strcmp(value, avalue->string.text))
1302
0
    {
1303
0
      DEBUG_puts("1ippContainsString: Returning true (match)");
1304
0
      return (true);
1305
0
    }
1306
0
        }
1307
0
        break;
1308
1309
0
    case IPP_TAG_MIMETYPE :
1310
0
    case IPP_TAG_NAME :
1311
0
    case IPP_TAG_NAMELANG :
1312
0
    case IPP_TAG_TEXT :
1313
0
    case IPP_TAG_TEXTLANG :
1314
0
  for (i = attr->num_values, avalue = attr->values; i > 0; i --, avalue ++)
1315
0
  {
1316
0
    DEBUG_printf("1ippContainsString: value[%u]=\"%s\"", (unsigned)(attr->num_values - i), avalue->string.text);
1317
1318
0
    if (avalue->string.text && !_cups_strcasecmp(value, avalue->string.text))
1319
0
    {
1320
0
      DEBUG_puts("1ippContainsString: Returning true (match)");
1321
0
      return (true);
1322
0
    }
1323
0
        }
1324
0
        break;
1325
1326
0
    default :
1327
0
        break;
1328
0
  }
1329
1330
0
  DEBUG_puts("1ippContainsString: Returning false (no match)");
1331
1332
0
  return (false);
1333
0
}
1334
1335
1336
//
1337
// 'ippCopyAttribute()' - Copy an attribute.
1338
//
1339
// This function copies an attribute to another IPP message.  When "quickcopy"
1340
// is `true`, a shallow reference copy of the attribute is created - this should
1341
// only be done as long as the original source IPP message will not be freed for
1342
// the life of the destination.
1343
//
1344
1345
1346
ipp_attribute_t *     // O - New attribute
1347
ippCopyAttribute(
1348
    ipp_t           *dst,   // I - Destination IPP message
1349
    ipp_attribute_t *srcattr,   // I - Attribute to copy
1350
    bool            quickcopy)    // I - `true` for a referenced copy, `false` for a new copy
1351
0
{
1352
0
  size_t    i;    // Looping var
1353
0
  ipp_tag_t   srctag,   // Source value tag
1354
0
      dstcopy;  // Copy bit for quick copies
1355
0
  ipp_attribute_t *dstattr; // Destination attribute
1356
0
  _ipp_value_t    *srcval,  // Source value
1357
0
      *dstval;  // Destination value
1358
1359
1360
0
  DEBUG_printf("ippCopyAttribute(dst=%p, srcattr=%p, quickcopy=%s)", (void *)dst, (void *)srcattr, quickcopy ? "true" : "false");
1361
1362
  // Range check input...
1363
0
  if (!dst || !srcattr)
1364
0
    return (NULL);
1365
1366
  // Copy it...
1367
0
  dstcopy = (quickcopy && (srcattr->value_tag & IPP_TAG_CUPS_CONST)) ? IPP_TAG_CUPS_CONST : 0;
1368
0
  srctag  = srcattr->value_tag & IPP_TAG_CUPS_MASK;
1369
1370
0
  switch (srctag)
1371
0
  {
1372
0
    case IPP_TAG_ZERO :
1373
0
        dstattr = ippAddSeparator(dst);
1374
0
  break;
1375
1376
0
    case IPP_TAG_UNSUPPORTED_VALUE :
1377
0
    case IPP_TAG_DEFAULT :
1378
0
    case IPP_TAG_UNKNOWN :
1379
0
    case IPP_TAG_NOVALUE :
1380
0
    case IPP_TAG_NOTSETTABLE :
1381
0
    case IPP_TAG_DELETEATTR :
1382
0
    case IPP_TAG_ADMINDEFINE :
1383
0
        dstattr = ippAddOutOfBand(dst, srcattr->group_tag, srctag, srcattr->name);
1384
0
        break;
1385
1386
0
    case IPP_TAG_INTEGER :
1387
0
    case IPP_TAG_ENUM :
1388
0
    case IPP_TAG_BOOLEAN :
1389
0
    case IPP_TAG_DATE :
1390
0
    case IPP_TAG_RESOLUTION :
1391
0
    case IPP_TAG_RANGE :
1392
0
        if ((dstattr = ipp_add_attr(dst, srcattr->name, srcattr->group_tag, srctag, srcattr->num_values)) != NULL)
1393
0
    memcpy(dstattr->values, srcattr->values, (size_t)srcattr->num_values * sizeof(_ipp_value_t));
1394
0
        break;
1395
1396
0
    case IPP_TAG_TEXT :
1397
0
    case IPP_TAG_NAME :
1398
0
    case IPP_TAG_RESERVED_STRING :
1399
0
    case IPP_TAG_KEYWORD :
1400
0
    case IPP_TAG_URI :
1401
0
    case IPP_TAG_URISCHEME :
1402
0
    case IPP_TAG_CHARSET :
1403
0
    case IPP_TAG_LANGUAGE :
1404
0
    case IPP_TAG_MIMETYPE :
1405
0
        if ((dstattr = ippAddStrings(dst, srcattr->group_tag, (ipp_tag_t)(srctag | dstcopy), srcattr->name, srcattr->num_values, NULL, NULL)) == NULL)
1406
0
          break;
1407
1408
0
        if (dstcopy)
1409
0
  {
1410
    // Can safely quick-copy these string values...
1411
0
    memcpy(dstattr->values, srcattr->values, (size_t)srcattr->num_values * sizeof(_ipp_value_t));
1412
0
        }
1413
0
  else
1414
0
  {
1415
    // Otherwise do a normal reference counted copy...
1416
0
    for (i = srcattr->num_values, srcval = srcattr->values, dstval = dstattr->values; i > 0; i --, srcval ++, dstval ++)
1417
0
      dstval->string.text = _cupsStrAlloc(srcval->string.text);
1418
0
  }
1419
0
        break;
1420
1421
0
    case IPP_TAG_TEXTLANG :
1422
0
    case IPP_TAG_NAMELANG :
1423
0
        if ((dstattr = ippAddStrings(dst, srcattr->group_tag, (ipp_tag_t)(srctag | dstcopy), srcattr->name, srcattr->num_values, NULL, NULL)) == NULL)
1424
0
          break;
1425
1426
0
        if (dstcopy)
1427
0
  {
1428
    // Can safely quick-copy these string values...
1429
0
    memcpy(dstattr->values, srcattr->values, (size_t)srcattr->num_values * sizeof(_ipp_value_t));
1430
0
        }
1431
0
  else
1432
0
  {
1433
    // Otherwise do a normal reference counted copy...
1434
0
    for (i = srcattr->num_values, srcval = srcattr->values, dstval = dstattr->values; i > 0; i --, srcval ++, dstval ++)
1435
0
    {
1436
0
            dstval->string.language = _cupsStrAlloc(srcval->string.language);
1437
0
      dstval->string.text     = _cupsStrAlloc(srcval->string.text);
1438
0
          }
1439
0
        }
1440
0
        break;
1441
1442
0
    case IPP_TAG_BEGIN_COLLECTION :
1443
0
        if (quickcopy)
1444
0
        {
1445
0
    for (i = srcattr->num_values, srcval = srcattr->values, dstattr = NULL; i > 0; i --, srcval ++)
1446
0
    {
1447
0
      if (srcval->collection)
1448
0
      {
1449
0
        if (dstattr)
1450
0
    ippSetCollection(dst, &dstattr, ippGetCount(dstattr), srcval->collection);
1451
0
        else
1452
0
    dstattr = ippAddCollection(dst, srcattr->group_tag, srcattr->name, srcval->collection);
1453
0
      }
1454
0
    }
1455
0
  }
1456
0
  else
1457
0
  {
1458
0
    for (i = srcattr->num_values, srcval = srcattr->values, dstattr = NULL; i > 0; i --, srcval ++)
1459
0
    {
1460
0
      if (srcval->collection)
1461
0
      {
1462
0
        ipp_t *col = ippNew();  // Copy of collection
1463
1464
0
              if (!col)
1465
0
              {
1466
0
                ippDeleteAttribute(dst, dstattr);
1467
0
                return (NULL);
1468
0
              }
1469
1470
0
        ippCopyAttributes(col, srcval->collection, false, /*cb*/NULL, /*cb_data*/NULL);
1471
1472
0
        if (dstattr)
1473
0
    ippSetCollection(dst, &dstattr, ippGetCount(dstattr), col);
1474
0
        else
1475
0
    dstattr = ippAddCollection(dst, srcattr->group_tag, srcattr->name, col);
1476
1477
0
              col->use --;
1478
0
      }
1479
0
    }
1480
0
  }
1481
0
        break;
1482
1483
0
    case IPP_TAG_STRING :
1484
0
    default :
1485
0
        if ((dstattr = ipp_add_attr(dst, srcattr->name, srcattr->group_tag, srctag, srcattr->num_values)) == NULL)
1486
0
          break;
1487
1488
0
        for (i = srcattr->num_values, srcval = srcattr->values, dstval = dstattr->values; i > 0; i --, srcval ++, dstval ++)
1489
0
  {
1490
0
    dstval->unknown.length = srcval->unknown.length;
1491
1492
0
    if (dstval->unknown.length > 0)
1493
0
    {
1494
0
      if ((dstval->unknown.data = malloc((size_t)dstval->unknown.length)) == NULL)
1495
0
        dstval->unknown.length = 0;
1496
0
      else
1497
0
        memcpy(dstval->unknown.data, srcval->unknown.data, (size_t)dstval->unknown.length);
1498
0
    }
1499
0
  }
1500
0
        break; // anti-compiler-warning-code
1501
0
  }
1502
1503
0
  return (dstattr);
1504
0
}
1505
1506
1507
//
1508
// 'ippCopyAttributes()' - Copy attributes from one IPP message to another.
1509
//
1510
// This function copies zero or more attributes from the source to the
1511
// destination IPP message.  When "quickcopy" is `true`, a shallow reference
1512
// copy of the attribute is created - this should only be done as long as the
1513
// original source IPP message will not be freed for the life of the
1514
// destination.
1515
//
1516
// The "cb" and "cb_data" arguments provide a generic way to "filter" the
1517
// attributes that are copied - the function must return `true` to copy the
1518
// attribute or `false` to skip it.  The function may also choose to do a
1519
// partial copy of the source attribute itself and return `false` to tell this
1520
// function to skip it.
1521
//
1522
1523
bool          // O - `true` on success, `false` on error
1524
ippCopyAttributes(
1525
    ipp_t         *dst,     // I - Destination IPP message
1526
    ipp_t         *src,     // I - Source IPP message
1527
    bool          quickcopy,    // I - `true` for a referenced copy, `false` for a new copy
1528
    ipp_copy_cb_t cb,     // I - Copy callback or `NULL` for none
1529
    void          *cb_data)   // I - Callback data pointer
1530
0
{
1531
0
  ipp_attribute_t *srcattr; // Source attribute
1532
1533
1534
0
  DEBUG_printf("ippCopyAttributes(dst=%p, src=%p, quickcopy=%s, cb=%p, cb_data=%p)", (void *)dst, (void *)src, quickcopy ? "true" : "false", (void *)cb, cb_data);
1535
1536
  // Range check input...
1537
0
  if (!dst || !src)
1538
0
    return (false);
1539
1540
  // Loop through source attributes and copy as needed...
1541
0
  for (srcattr = src->attrs; srcattr; srcattr = srcattr->next)
1542
0
  {
1543
0
    if (!cb || (*cb)(cb_data, dst, srcattr))
1544
0
    {
1545
0
      if (!ippCopyAttribute(dst, srcattr, quickcopy))
1546
0
        return (false);
1547
0
    }
1548
0
  }
1549
1550
0
  return (true);
1551
0
}
1552
1553
1554
//
1555
// 'ippCopyCredentialsString()' - Copy a credentials value from an IPP attribute.
1556
//
1557
// This function concatenates the 1setOf text credential values of an attribute,
1558
// separated by newlines.  The returned string must be freed using the `free`
1559
// function.
1560
//
1561
1562
char *          // O - Combined string or `NULL` on error
1563
ippCopyCredentialsString(
1564
    ipp_attribute_t *attr)    // I - Attribute
1565
0
{
1566
0
  char    *s = NULL,   // Combined string
1567
0
    *ptr;     // Pointer into string
1568
0
  size_t  i,      // Looping var
1569
0
    slen;     // Length of combined string
1570
1571
1572
0
  if (attr && ippGetValueTag(attr) == IPP_TAG_TEXT)
1573
0
  {
1574
    // Loop through string values and add up the total length...
1575
0
    for (i = 0, slen = 0; i < attr->num_values; i ++)
1576
0
    {
1577
0
      if (attr->values[i].string.text)
1578
0
        slen += strlen(attr->values[i].string.text) + 1;
1579
0
    }
1580
1581
0
    if (slen > 0)
1582
0
    {
1583
      // Allocate memory...
1584
0
      if ((s = malloc(slen + 1)) != NULL)
1585
0
      {
1586
0
  for (i = 0, ptr = s; i < attr->num_values; i ++)
1587
0
  {
1588
0
    if (attr->values[i].string.text)
1589
0
    {
1590
0
      size_t len = strlen(attr->values[i].string.text);
1591
            // Length of string
1592
1593
0
      memcpy(ptr, attr->values[i].string.text, len);
1594
0
      ptr += len;
1595
0
      *ptr++ = '\n';
1596
0
    }
1597
0
  }
1598
1599
0
  *ptr = '\0';
1600
0
      }
1601
0
    }
1602
0
  }
1603
1604
0
  return (s);
1605
0
}
1606
1607
1608
//
1609
// 'ippDateToTime()' - Convert from RFC 2579 Date/Time format to time in
1610
//                     seconds.
1611
//
1612
1613
time_t          // O - UNIX time value
1614
ippDateToTime(const ipp_uchar_t *date)  // I - RFC 2579 date info
1615
0
{
1616
0
  struct tm unixdate;   // UNIX date/time info
1617
0
  time_t  t;      // Computed time
1618
1619
1620
0
  if (!date)
1621
0
    return (0);
1622
1623
0
  memset(&unixdate, 0, sizeof(unixdate));
1624
1625
  // RFC-2579 date/time format is:
1626
  //
1627
  //    Byte(s)  Description
1628
  //    -------  -----------
1629
  //    0-1      Year (0 to 65535)
1630
  //    2        Month (1 to 12)
1631
  //    3        Day (1 to 31)
1632
  //    4        Hours (0 to 23)
1633
  //    5        Minutes (0 to 59)
1634
  //    6        Seconds (0 to 60, 60 = "leap second")
1635
  //    7        Deciseconds (0 to 9)
1636
  //    8        +/- UTC
1637
  //    9        UTC hours (0 to 14)
1638
  //    10       UTC minutes (0 to 59)
1639
0
  unixdate.tm_year = ((date[0] << 8) | date[1]) - 1900;
1640
0
  unixdate.tm_mon  = date[2] - 1;
1641
0
  unixdate.tm_mday = date[3];
1642
0
  unixdate.tm_hour = date[4];
1643
0
  unixdate.tm_min  = date[5];
1644
0
  unixdate.tm_sec  = date[6];
1645
1646
#if _WIN32
1647
  if ((t = _mkgmtime(&unixdate)) < 0)
1648
    return (0);
1649
1650
#elif defined(HAVE_TIMEGM)
1651
0
  if ((t = timegm(&unixdate)) < 0)
1652
0
    return (0);
1653
1654
#else
1655
  if ((t = mktime(&unixdate)) < 0)
1656
    return (0);
1657
1658
#  if defined(HAVE_TM_GMTOFF)
1659
  // Adjust the time value using the "tm_gmtoff" and "tm_isdst" members.  As
1660
  // noted by M-HT on Github, this DST hack will fail in timezones where the
1661
  // DST offset is not one hour, such as Australia/Lord_Howe.  Fortunately,
1662
  // this is unusual and most systems support the "timegm" function...
1663
  t += unixdate.tm_gmtoff - 3600 * unixdate.tm_isdst;
1664
#  else
1665
  // Adjust the time value using the even more legacy "timezone" variable,
1666
  // which also reflects any DST offset...
1667
  t += timezone;
1668
#  endif // HAVE_TM_GMTOFF
1669
#endif // _WIN32
1670
1671
  // Subtract the UTC timezone offset to get the actual time_t value...
1672
0
  if (date[8] == '-')
1673
0
    t += date[9] * 3600 + date[10] * 60;// "t - -offset" is "t + offset"
1674
0
  else
1675
0
    t -= date[9] * 3600 + date[10] * 60;// "t - offset"
1676
1677
0
  return (t);
1678
0
}
1679
1680
1681
//
1682
// 'ippDelete()' - Delete an IPP message.
1683
//
1684
1685
void
1686
ippDelete(ipp_t *ipp)     // I - IPP message
1687
194k
{
1688
194k
  ipp_attribute_t *attr,    // Current attribute
1689
194k
      *next;    // Next attribute
1690
1691
1692
194k
  DEBUG_printf("ippDelete(ipp=%p)", (void *)ipp);
1693
1694
194k
  if (!ipp)
1695
186
    return;
1696
1697
193k
  ipp->use --;
1698
193k
  if (ipp->use > 0)
1699
0
  {
1700
0
    DEBUG_printf("4debug_retain: %p IPP message (use=%u)", (void *)ipp, (unsigned)ipp->use);
1701
0
    return;
1702
0
  }
1703
1704
193k
  DEBUG_printf("4debug_free: %p IPP message", (void *)ipp);
1705
1706
1.44M
  for (attr = ipp->attrs; attr != NULL; attr = next)
1707
1.25M
  {
1708
1.25M
    next = attr->next;
1709
1710
1.25M
    DEBUG_printf("4debug_free: %p %s %s%s (%u values)", (void *)attr, attr->name, attr->num_values > 1 ? "1setOf " : "", ippTagString(attr->value_tag), (unsigned)attr->num_values);
1711
1712
1.25M
    ipp_free_values(attr, 0, attr->num_values);
1713
1714
1.25M
    if (attr->name)
1715
265k
      _cupsStrFree(attr->name);
1716
1717
1.25M
    free(attr);
1718
1.25M
  }
1719
1720
193k
  _cupsStrFree(ipp->def_language);
1721
1722
193k
  free(ipp);
1723
193k
}
1724
1725
1726
//
1727
// 'ippDeleteAttribute()' - Delete a single attribute in an IPP message.
1728
//
1729
1730
void
1731
ippDeleteAttribute(
1732
    ipp_t           *ipp,   // I - IPP message
1733
    ipp_attribute_t *attr)    // I - Attribute to delete
1734
1.98k
{
1735
1.98k
  ipp_attribute_t *current, // Current attribute
1736
1.98k
      *prev;    // Previous attribute
1737
1738
1739
1.98k
  DEBUG_printf("ippDeleteAttribute(ipp=%p, attr=%p(%s))", (void *)ipp, (void *)attr, attr ? attr->name : "(null)");
1740
1741
  // Range check input...
1742
1.98k
  if (!attr)
1743
0
    return;
1744
1745
1.98k
  DEBUG_printf("4debug_free: %p %s %s%s (%u values)", (void *)attr, attr->name, attr->num_values > 1 ? "1setOf " : "", ippTagString(attr->value_tag), (unsigned)attr->num_values);
1746
1747
  // Find the attribute in the list...
1748
1.98k
  if (ipp)
1749
1.98k
  {
1750
89.3k
    for (current = ipp->attrs, prev = NULL; current; prev = current, current = current->next)
1751
89.3k
    {
1752
89.3k
      if (current == attr)
1753
1.98k
      {
1754
  // Found it, remove the attribute from the list...
1755
1.98k
  if (prev)
1756
984
    prev->next = current->next;
1757
997
  else
1758
997
    ipp->attrs = current->next;
1759
1760
1.98k
  if (current == ipp->last)
1761
1.98k
    ipp->last = prev;
1762
1763
1.98k
        break;
1764
1.98k
      }
1765
89.3k
    }
1766
1767
1.98k
    if (!current)
1768
0
      return;
1769
1.98k
  }
1770
1771
  // Free memory used by the attribute...
1772
1.98k
  ipp_free_values(attr, 0, attr->num_values);
1773
1774
1.98k
  if (attr->name)
1775
1.91k
    _cupsStrFree(attr->name);
1776
1777
1.98k
  free(attr);
1778
1.98k
}
1779
1780
1781
//
1782
// 'ippDeleteValues()' - Delete values in an attribute.
1783
//
1784
// This function deletes one or more values in an attribute.  The "element"
1785
// argument specifies the first value to delete, starting at `0`.  It must be
1786
// less than the number of values returned by @link ippGetCount@.
1787
//
1788
// The "attr" argument may be modified as a result of setting the value,
1789
// which will set the variable to `NULL`.
1790
//
1791
// Deleting all values in an attribute deletes the attribute.
1792
//
1793
1794
bool          // O  - `true` on success, `false` on failure
1795
ippDeleteValues(
1796
    ipp_t           *ipp,   // I  - IPP message
1797
    ipp_attribute_t **attr,   // IO - Attribute
1798
    size_t          element,    // I  - Index of first value to delete (`0`-based)
1799
    size_t          count)    // I  - Number of values to delete
1800
0
{
1801
  // Range check input...
1802
0
  if (!ipp || !attr || !*attr || element >= (*attr)->num_values || !count || (element + count) >= (*attr)->num_values)
1803
0
    return (false);
1804
1805
  // If we are deleting all values, just delete the attribute entirely.
1806
0
  if (count == (*attr)->num_values)
1807
0
  {
1808
0
    ippDeleteAttribute(ipp, *attr);
1809
0
    *attr = NULL;
1810
0
    return (true);
1811
0
  }
1812
1813
  // Otherwise free the values in question and return.
1814
0
  ipp_free_values(*attr, element, count);
1815
1816
0
  return (true);
1817
0
}
1818
1819
1820
//
1821
// 'ippFindAttribute()' - Find a named attribute in an IPP message.
1822
//
1823
// This function finds the first occurrence of a named attribute in an IPP
1824
// message.  The attribute name can contain a hierarchical list of attribute and
1825
// member names separated by slashes, for example "media-col/media-size".
1826
//
1827
1828
ipp_attribute_t *     // O - Matching attribute
1829
ippFindAttribute(ipp_t      *ipp, // I - IPP message
1830
                 const char *name,  // I - Name of attribute
1831
     ipp_tag_t  type) // I - Type of attribute
1832
0
{
1833
0
  DEBUG_printf("2ippFindAttribute(ipp=%p, name=\"%s\", type=%02x(%s))", (void *)ipp, name, type, ippTagString(type));
1834
1835
  // Range check input...
1836
0
  if (!ipp || !name)
1837
0
    return (NULL);
1838
1839
  // Reset the current attribute pointer...
1840
0
  ipp->find->attr  = NULL;
1841
0
  ipp->find->idx   = 0;
1842
0
  ipp->find->atend = false;
1843
1844
  // Search for the attribute...
1845
0
  return (ippFindNextAttribute(ipp, name, type));
1846
0
}
1847
1848
1849
//
1850
// 'ippFindNextAttribute()' - Find the next named attribute in an IPP message.
1851
//
1852
// This function finds the next named attribute in an IPP message.  The
1853
// attribute name can contain a hierarchical list of attribute and member names
1854
// separated by slashes, for example "media-col/media-size".
1855
//
1856
1857
ipp_attribute_t *     // O - Matching attribute
1858
ippFindNextAttribute(ipp_t      *ipp, // I - IPP message
1859
                     const char *name,  // I - Name of attribute
1860
         ipp_tag_t  type) // I - Type of attribute
1861
0
{
1862
0
  ipp_attribute_t *attr,    // Current attribute
1863
0
      *childattr; // Child attribute
1864
0
  ipp_tag_t   value_tag;  // Value tag
1865
0
  char      parent[1024], // Parent attribute name
1866
0
      *child = NULL; // Child attribute name
1867
1868
1869
0
  DEBUG_printf("2ippFindNextAttribute(ipp=%p, name=\"%s\", type=%02x(%s))", (void *)ipp, name, type, ippTagString(type));
1870
1871
  // Range check input...
1872
0
  if (!ipp || !name)
1873
0
    return (NULL);
1874
1875
0
  DEBUG_printf("3ippFindNextAttribute: atend=%s", ipp->find->atend ? "true" : "false");
1876
1877
0
  if (ipp->find->atend)
1878
0
    return (NULL);
1879
1880
0
  if (strchr(name, '/'))
1881
0
  {
1882
    // Search for child attribute...
1883
0
    cupsCopyString(parent, name, sizeof(parent));
1884
0
    if ((child = (char *)strchr(parent, '/')) == NULL)
1885
0
    {
1886
0
      DEBUG_puts("3ippFindNextAttribute: Attribute name too long.");
1887
0
      return (NULL);
1888
0
    }
1889
1890
0
    *child++ = '\0';
1891
1892
0
    if (ipp->find->attr && ipp->find->attr->name && ipp->find->attr->value_tag == IPP_TAG_BEGIN_COLLECTION && !strcmp(parent, ipp->find->attr->name))
1893
0
    {
1894
0
      while (ipp->find->idx < ipp->find->attr->num_values)
1895
0
      {
1896
0
        if ((childattr = ippFindNextAttribute(ipp->find->attr->values[ipp->find->idx].collection, child, type)) != NULL)
1897
0
          return (childattr);
1898
1899
0
        ipp->find->idx ++;
1900
0
        if (ipp->find->idx < ipp->find->attr->num_values && ipp->find->attr->values[ipp->find->idx].collection)
1901
0
          ipp->find->attr->values[ipp->find->idx].collection->find->attr = NULL;
1902
0
      }
1903
1904
0
      ipp->find->attr = ipp->find->attr->next;
1905
0
      ipp->find->idx  = 0;
1906
1907
0
      if (!ipp->find->attr)
1908
0
      {
1909
0
        ipp->find->atend = true;
1910
0
        return (NULL);
1911
0
      }
1912
0
    }
1913
1914
0
    if (!ipp->find->attr)
1915
0
    {
1916
0
      ipp->find->attr = ipp->attrs;
1917
0
      ipp->find->idx  = 0;
1918
0
    }
1919
1920
0
    name = parent;
1921
0
    attr = ipp->find->attr;
1922
0
  }
1923
0
  else if (ipp->find->attr)
1924
0
  {
1925
0
    attr = ipp->find->attr->next;
1926
0
  }
1927
0
  else
1928
0
  {
1929
0
    attr = ipp->attrs;
1930
0
  }
1931
1932
0
  for (; attr != NULL; ipp->prev = attr, attr = attr->next)
1933
0
  {
1934
0
    DEBUG_printf("4ippFindAttribute: attr=%p, name=\"%s\"", (void *)attr, attr->name);
1935
1936
0
    value_tag = (ipp_tag_t)(attr->value_tag & IPP_TAG_CUPS_MASK);
1937
1938
0
    if (attr->name != NULL && strcmp(attr->name, name) == 0 && (value_tag == type || type == IPP_TAG_ZERO || name == parent || (value_tag == IPP_TAG_TEXTLANG && type == IPP_TAG_TEXT) || (value_tag == IPP_TAG_NAMELANG && type == IPP_TAG_NAME)))
1939
0
    {
1940
0
      ipp->find->attr = attr;
1941
1942
0
      if (name == parent && attr->value_tag == IPP_TAG_BEGIN_COLLECTION)
1943
0
      {
1944
0
        size_t i;     // Looping var
1945
1946
0
        for (i = 0; i < attr->num_values; i ++)
1947
0
        {
1948
0
    if ((childattr = ippFindAttribute(attr->values[i].collection, child, type)) != NULL)
1949
0
    {
1950
0
      attr->values[0].collection->find->idx = i;
1951
0
      return (childattr);
1952
0
    }
1953
0
        }
1954
0
      }
1955
0
      else
1956
0
      {
1957
0
        return (attr);
1958
0
      }
1959
0
    }
1960
0
  }
1961
1962
  // If we get this far, we didn't find it...
1963
0
  ipp->find->attr  = NULL;
1964
0
  ipp->find->atend = true;
1965
1966
0
  return (NULL);
1967
0
}
1968
1969
1970
//
1971
// 'ippGetBoolean()' - Get a boolean value for an attribute.
1972
//
1973
// The "element" argument specifies which value to get from `0` to
1974
// `ippGetCount(attr) - 1`.
1975
//
1976
1977
bool          // O - Boolean value or `false` on error
1978
ippGetBoolean(ipp_attribute_t *attr,  // I - IPP attribute
1979
              size_t          element)  // I - Value number (`0`-based)
1980
0
{
1981
  // Range check input...
1982
0
  if (!attr || attr->value_tag != IPP_TAG_BOOLEAN || element >= attr->num_values)
1983
0
    return (false);
1984
1985
  // Return the value...
1986
0
  return (attr->values[element].boolean);
1987
0
}
1988
1989
1990
//
1991
// 'ippGetCollection()' - Get a collection value for an attribute.
1992
//
1993
// The "element" argument specifies which value to get from `0` to
1994
// `ippGetCount(attr) - 1`.
1995
//
1996
1997
ipp_t *         // O - Collection value or `NULL` on error
1998
ippGetCollection(
1999
    ipp_attribute_t *attr,    // I - IPP attribute
2000
    size_t          element)    // I - Value number (`0`-based)
2001
0
{
2002
  // Range check input...
2003
0
  if (!attr || attr->value_tag != IPP_TAG_BEGIN_COLLECTION || element >= attr->num_values)
2004
0
    return (NULL);
2005
2006
  // Return the value...
2007
0
  return (attr->values[element].collection);
2008
0
}
2009
2010
2011
//
2012
// 'ippGetCount()' - Get the number of values in an attribute.
2013
//
2014
2015
size_t          // O - Number of values or 0 on error
2016
ippGetCount(ipp_attribute_t *attr)  // I - IPP attribute
2017
0
{
2018
  // Range check input...
2019
0
  if (!attr)
2020
0
    return (0);
2021
2022
  // Return the number of values...
2023
0
  return (attr->num_values);
2024
0
}
2025
2026
2027
//
2028
// 'ippGetDate()' - Get a dateTime value for an attribute.
2029
//
2030
// The "element" argument specifies which value to get from `0` to
2031
// `ippGetCount(attr) - 1`.
2032
//
2033
2034
const ipp_uchar_t *     // O - dateTime value or `NULL`
2035
ippGetDate(ipp_attribute_t *attr, // I - IPP attribute
2036
           size_t          element) // I - Value number (`0`-based)
2037
0
{
2038
  // Range check input...
2039
0
  if (!attr || attr->value_tag != IPP_TAG_DATE || element >= attr->num_values)
2040
0
    return (NULL);
2041
2042
  // Return the value...
2043
0
  return (attr->values[element].date);
2044
0
}
2045
2046
2047
//
2048
// 'ippGetFirstAttribute()' - Return the first attribute in the message.
2049
//
2050
2051
ipp_attribute_t *     // O - First attribute or `NULL` if none
2052
ippGetFirstAttribute(ipp_t *ipp)  // I - IPP message
2053
1.77k
{
2054
  // Range check input...
2055
1.77k
  if (!ipp)
2056
0
    return (NULL);
2057
2058
1.77k
  if (!ipp->find)
2059
0
    ipp->find = ipp->fstack;
2060
2061
1.77k
  ipp->find->attr  = ipp->attrs;
2062
1.77k
  ipp->find->idx   = 0;
2063
1.77k
  ipp->find->atend = ipp->find->attr == NULL;
2064
2065
  // Return the first attribute...
2066
1.77k
  return (ipp->find->attr);
2067
1.77k
}
2068
2069
2070
//
2071
// 'ippGetGroupTag()' - Get the group associated with an attribute.
2072
//
2073
2074
ipp_tag_t       // O - Group tag or `IPP_TAG_ZERO` on error
2075
ippGetGroupTag(ipp_attribute_t *attr) // I - IPP attribute
2076
0
{
2077
  // Range check input...
2078
0
  if (!attr)
2079
0
    return (IPP_TAG_ZERO);
2080
2081
  // Return the group...
2082
0
  return (attr->group_tag);
2083
0
}
2084
2085
2086
//
2087
// 'ippGetInteger()' - Get the integer/enum value for an attribute.
2088
//
2089
// The "element" argument specifies which value to get from `0` to
2090
// `ippGetCount(attr) - 1`.
2091
//
2092
2093
int         // O - Value or `0` on error
2094
ippGetInteger(ipp_attribute_t *attr,  // I - IPP attribute
2095
              size_t          element)  // I - Value number (`0`-based)
2096
0
{
2097
  // Range check input...
2098
0
  if (!attr || (attr->value_tag != IPP_TAG_INTEGER && attr->value_tag != IPP_TAG_ENUM) || element >= attr->num_values)
2099
0
    return (0);
2100
2101
  // Return the value...
2102
0
  return (attr->values[element].integer);
2103
0
}
2104
2105
2106
//
2107
// 'ippGetLength()' - Compute the length of an IPP message.
2108
//
2109
2110
size_t          // O - Size of IPP message
2111
ippGetLength(ipp_t *ipp)    // I - IPP message
2112
0
{
2113
0
  return (ipp_length(ipp, 0));
2114
0
}
2115
2116
2117
//
2118
// 'ippGetName()' - Get the attribute name.
2119
//
2120
2121
const char *        // O - Attribute name or `NULL` for separators
2122
ippGetName(ipp_attribute_t *attr) // I - IPP attribute
2123
0
{
2124
  // Range check input...
2125
0
  if (!attr)
2126
0
    return (NULL);
2127
2128
  // Return the name...
2129
0
  return (attr->name);
2130
0
}
2131
2132
2133
//
2134
// 'ippGetNextAttribute()' - Return the next attribute in the message.
2135
//
2136
2137
ipp_attribute_t *     // O - Next attribute or `NULL` if none
2138
ippGetNextAttribute(ipp_t *ipp)   // I - IPP message
2139
90.6k
{
2140
  // Range check input...
2141
90.6k
  if (!ipp || !ipp->find || !ipp->find->attr)
2142
0
    return (NULL);
2143
2144
90.6k
  ipp->find->attr  = ipp->find->attr->next;
2145
90.6k
  ipp->find->atend = ipp->find->attr == NULL;
2146
2147
  // Return the next attribute...
2148
90.6k
  return (ipp->find->attr);
2149
90.6k
}
2150
2151
2152
//
2153
// 'ippGetOctetString()' - Get an octetString value from an IPP attribute.
2154
//
2155
// The "element" argument specifies which value to get from '0' to
2156
// `ippGetCount(attr) - 1`.
2157
//
2158
2159
void *          // O - Pointer to octetString data
2160
ippGetOctetString(
2161
    ipp_attribute_t *attr,    // I - IPP attribute
2162
    size_t          element,    // I - Value number (`0`-based)
2163
    size_t          *datalen)   // O - Length of octetString data
2164
0
{
2165
  // Range check input...
2166
0
  if (!attr || (attr->value_tag != IPP_TAG_STRING && attr->value_tag != IPP_TAG_EXTENSION) || element >= attr->num_values)
2167
0
  {
2168
0
    if (datalen)
2169
0
      *datalen = 0;
2170
2171
0
    return (NULL);
2172
0
  }
2173
2174
  // Return the values...
2175
0
  if (datalen)
2176
0
    *datalen = attr->values[element].unknown.length;
2177
2178
0
  return (attr->values[element].unknown.data);
2179
0
}
2180
2181
2182
//
2183
// 'ippGetOperation()' - Get the operation ID in an IPP message.
2184
//
2185
2186
ipp_op_t        // O - Operation ID or 0 on error
2187
ippGetOperation(ipp_t *ipp)   // I - IPP request message
2188
0
{
2189
  // Range check input...
2190
0
  if (!ipp)
2191
0
    return ((ipp_op_t)0);
2192
2193
  // Return the value...
2194
0
  return ((ipp_op_t)ipp->request.op_status);
2195
0
}
2196
2197
2198
//
2199
// 'ippGetRange()' - Get a rangeOfInteger value from an attribute.
2200
//
2201
// The "element" argument specifies which value to get from `0` to
2202
// `ippGetCount(attr) - 1`.
2203
//
2204
2205
int         // O - Lower value of range or 0
2206
ippGetRange(ipp_attribute_t *attr,  // I - IPP attribute
2207
      size_t          element,  // I - Value number (`0`-based)
2208
      int             *uppervalue)// O - Upper value of range
2209
0
{
2210
  // Range check input...
2211
0
  if (!attr || attr->value_tag != IPP_TAG_RANGE || element >= attr->num_values)
2212
0
  {
2213
0
    if (uppervalue)
2214
0
      *uppervalue = 0;
2215
2216
0
    return (0);
2217
0
  }
2218
2219
  // Return the values...
2220
0
  if (uppervalue)
2221
0
    *uppervalue = attr->values[element].range.upper;
2222
2223
0
  return (attr->values[element].range.lower);
2224
0
}
2225
2226
2227
//
2228
// 'ippGetRequestId()' - Get the request ID from an IPP message.
2229
//
2230
2231
int         // O - Request ID or 0 on error
2232
ippGetRequestId(ipp_t *ipp)   // I - IPP message
2233
0
{
2234
  // Range check input...
2235
0
  if (!ipp)
2236
0
    return (0);
2237
2238
  // Return the request ID...
2239
0
  return (ipp->request.request_id);
2240
0
}
2241
2242
2243
//
2244
// 'ippGetResolution()' - Get a resolution value for an attribute.
2245
//
2246
// The "element" argument specifies which value to get from `0` to
2247
// `ippGetCount(attr) - 1`.
2248
//
2249
2250
int         // O - Horizontal/cross feed resolution or 0
2251
ippGetResolution(
2252
    ipp_attribute_t *attr,    // I - IPP attribute
2253
    size_t          element,    // I - Value number (`0`-based)
2254
    int             *yres,    // O - Vertical/feed resolution
2255
    ipp_res_t       *units)   // O - Units for resolution
2256
0
{
2257
  // Range check input...
2258
0
  if (!attr || attr->value_tag != IPP_TAG_RESOLUTION || element >= attr->num_values)
2259
0
  {
2260
0
    if (yres)
2261
0
      *yres = 0;
2262
2263
0
    if (units)
2264
0
      *units = (ipp_res_t)0;
2265
2266
0
    return (0);
2267
0
  }
2268
2269
  // Return the value...
2270
0
  if (yres)
2271
0
    *yres = attr->values[element].resolution.yres;
2272
2273
0
  if (units)
2274
0
    *units = attr->values[element].resolution.units;
2275
2276
0
  return (attr->values[element].resolution.xres);
2277
0
}
2278
2279
2280
//
2281
// 'ippGetState()' - Get the IPP message state.
2282
//
2283
2284
ipp_state_t       // O - IPP message state value
2285
ippGetState(ipp_t *ipp)     // I - IPP message
2286
0
{
2287
  // Range check input...
2288
0
  if (!ipp)
2289
0
    return (IPP_STATE_IDLE);
2290
2291
  // Return the value...
2292
0
  return (ipp->state);
2293
0
}
2294
2295
2296
//
2297
// 'ippGetStatusCode()' - Get the status code from an IPP response or event message.
2298
//
2299
2300
ipp_status_t        // O - Status code in IPP message
2301
ippGetStatusCode(ipp_t *ipp)    // I - IPP response or event message
2302
0
{
2303
  // Range check input...
2304
0
  if (!ipp)
2305
0
    return (IPP_STATUS_ERROR_INTERNAL);
2306
2307
  // Return the value...
2308
0
  return ((ipp_status_t)ipp->request.op_status);
2309
0
}
2310
2311
2312
//
2313
// 'ippGetString()' - Get the string and optionally the language code for an attribute.
2314
//
2315
// The "element" argument specifies which value to get from `0` to
2316
// `ippGetCount(attr) - 1`.
2317
//
2318
2319
const char *
2320
ippGetString(ipp_attribute_t *attr, // I - IPP attribute
2321
             size_t          element, // I - Value number (`0`-based)
2322
       const char      **language)// O - Language code (`NULL` for don't care)
2323
0
{
2324
0
  ipp_tag_t tag;      // Value tag
2325
2326
2327
  // Range check input...
2328
0
  tag = ippGetValueTag(attr);
2329
2330
0
  if (!attr || element >= attr->num_values || (tag != IPP_TAG_TEXTLANG && tag != IPP_TAG_NAMELANG && (tag < IPP_TAG_TEXT || tag > IPP_TAG_MIMETYPE)))
2331
0
    return (NULL);
2332
2333
  // Return the value...
2334
0
  if (language)
2335
0
    *language = attr->values[element].string.language;
2336
2337
0
  return (attr->values[element].string.text);
2338
0
}
2339
2340
2341
//
2342
// 'ippGetValueTag()' - Get the value tag for an attribute.
2343
//
2344
2345
ipp_tag_t       // O - Value tag or `IPP_TAG_ZERO` on error
2346
ippGetValueTag(ipp_attribute_t *attr) // I - IPP attribute
2347
0
{
2348
  // Range check input...
2349
0
  if (!attr)
2350
0
    return (IPP_TAG_ZERO);
2351
2352
  // Return the value...
2353
0
  return (attr->value_tag & IPP_TAG_CUPS_MASK);
2354
0
}
2355
2356
2357
//
2358
// 'ippGetVersion()' - Get the major and minor version number from an IPP message.
2359
//
2360
2361
int         // O - Major version number or 0 on error
2362
ippGetVersion(ipp_t *ipp,   // I - IPP message
2363
              int   *minor)   // O - Minor version number or `NULL` for don't care
2364
0
{
2365
  // Range check input...
2366
0
  if (!ipp)
2367
0
  {
2368
0
    if (minor)
2369
0
      *minor = 0;
2370
2371
0
    return (0);
2372
0
  }
2373
2374
  // Return the value...
2375
0
  if (minor)
2376
0
    *minor = ipp->request.version[1];
2377
2378
0
  return (ipp->request.version[0]);
2379
0
}
2380
2381
2382
//
2383
// 'ippNew()' - Allocate a new IPP message.
2384
//
2385
2386
ipp_t *         // O - New IPP message
2387
ippNew(void)
2388
193k
{
2389
193k
  ipp_t     *temp;    // New IPP message
2390
193k
  _cups_globals_t *cg = _cupsGlobals();
2391
          // Global data
2392
2393
2394
193k
  DEBUG_puts("ippNew()");
2395
2396
193k
  if ((temp = (ipp_t *)calloc(1, sizeof(ipp_t))) != NULL)
2397
193k
  {
2398
    // Set default version - usually 2.0...
2399
193k
    DEBUG_printf("4debug_alloc: %p IPP message", (void *)temp);
2400
2401
193k
    if (!cg->client_conf_loaded)
2402
2
      _cupsSetDefaults();
2403
2404
193k
    temp->request.version[0] = (ipp_uchar_t)(cg->server_version / 10);
2405
193k
    temp->request.version[1] = (ipp_uchar_t)(cg->server_version % 10);
2406
193k
    temp->use                = 1;
2407
193k
    temp->find               = temp->fstack;
2408
193k
  }
2409
2410
193k
  DEBUG_printf("1ippNew: Returning %p", (void *)temp);
2411
2412
193k
  return (temp);
2413
193k
}
2414
2415
2416
//
2417
//  'ippNewRequest()' - Allocate a new IPP request message.
2418
//
2419
// The new request message is initialized with the "attributes-charset" and
2420
// "attributes-natural-language" attributes added. The
2421
// "attributes-natural-language" value is derived from the current locale.
2422
//
2423
2424
ipp_t *         // O - IPP request message
2425
ippNewRequest(ipp_op_t op)    // I - Operation code
2426
2.82k
{
2427
2.82k
  ipp_t   *request;   // IPP request message
2428
2.82k
  cups_lang_t *language;    // Current language localization
2429
2.82k
  static int  request_id = 0;   // Current request ID
2430
2.82k
  static cups_mutex_t request_mutex = CUPS_MUTEX_INITIALIZER;
2431
          // Mutex for request ID
2432
2433
2434
2.82k
  DEBUG_printf("ippNewRequest(op=%02x(%s))", op, ippOpString(op));
2435
2436
  // Create a new IPP message...
2437
2.82k
  if ((request = ippNew()) == NULL)
2438
0
    return (NULL);
2439
2440
  // Set the operation and request ID...
2441
2.82k
  cupsMutexLock(&request_mutex);
2442
2443
2.82k
  request->request.op_status  = (short)op;
2444
2.82k
  request->request.request_id = ++request_id;
2445
2446
2.82k
  cupsMutexUnlock(&request_mutex);
2447
2448
  // Use UTF-8 as the character set...
2449
2.82k
  ippAddString(request, IPP_TAG_OPERATION, IPP_CONST_TAG(IPP_TAG_CHARSET), "attributes-charset", NULL, "utf-8");
2450
2451
  // Get the language from the current locale...
2452
2.82k
  language = cupsLangDefault();
2453
2454
2.82k
  ippAddString(request, IPP_TAG_OPERATION, IPP_CONST_TAG(IPP_TAG_LANGUAGE), "attributes-natural-language", NULL, cupsLangGetName(language));
2455
2456
  // Return the new request...
2457
2.82k
  return (request);
2458
2.82k
}
2459
2460
2461
//
2462
// 'ippNewResponse()' - Allocate a new IPP response message.
2463
//
2464
// The new response message is initialized with the same "version-number",
2465
// "request-id", "attributes-charset", and "attributes-natural-language" as the
2466
// provided request message.  If the "attributes-charset" or
2467
// "attributes-natural-language" attributes are missing from the request,
2468
// 'utf-8' and a value derived from the current locale are substituted,
2469
// respectively.
2470
//
2471
2472
ipp_t *         // O - IPP response message
2473
ippNewResponse(ipp_t *request)    // I - IPP request message
2474
0
{
2475
0
  ipp_t     *response;  // IPP response message
2476
0
  ipp_attribute_t *attr;    // Current attribute
2477
2478
2479
  // Range check input...
2480
0
  if (!request)
2481
0
    return (NULL);
2482
2483
  // Create a new IPP message...
2484
0
  if ((response = ippNew()) == NULL)
2485
0
    return (NULL);
2486
2487
  // Copy the request values over to the response...
2488
0
  response->request.version[0] = request->request.version[0];
2489
0
  response->request.version[1] = request->request.version[1];
2490
0
  response->request.request_id = request->request.request_id;
2491
2492
  // The first attribute MUST be attributes-charset...
2493
0
  attr = request->attrs;
2494
2495
0
  if (attr && attr->name && !strcmp(attr->name, "attributes-charset") && attr->group_tag == IPP_TAG_OPERATION && attr->value_tag == IPP_TAG_CHARSET && attr->num_values == 1)
2496
0
  {
2497
    // Copy charset from request...
2498
0
    ippAddString(response, IPP_TAG_OPERATION, IPP_TAG_CHARSET, "attributes-charset", NULL, attr->values[0].string.text);
2499
0
  }
2500
0
  else
2501
0
  {
2502
    // Use "utf-8" as the default...
2503
0
    ippAddString(response, IPP_TAG_OPERATION, IPP_CONST_TAG(IPP_TAG_CHARSET), "attributes-charset", NULL, "utf-8");
2504
0
  }
2505
2506
  // Then attributes-natural-language...
2507
0
  if (attr)
2508
0
    attr = attr->next;
2509
2510
0
  if (attr && attr->name && !strcmp(attr->name, "attributes-natural-language") && attr->group_tag == IPP_TAG_OPERATION && attr->value_tag == IPP_TAG_LANGUAGE && attr->num_values == 1)
2511
0
  {
2512
    // Copy language from request...
2513
0
    ippAddString(response, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE, "attributes-natural-language", NULL, attr->values[0].string.text);
2514
0
  }
2515
0
  else
2516
0
  {
2517
    // Use the language from the current locale...
2518
0
    cups_lang_t *language = cupsLangDefault();
2519
          // Current locale
2520
2521
0
    ippAddString(response, IPP_TAG_OPERATION, IPP_CONST_TAG(IPP_TAG_LANGUAGE), "attributes-natural-language", NULL, cupsLangGetName(language));
2522
0
  }
2523
2524
0
  return (response);
2525
0
}
2526
2527
2528
//
2529
// 'ippRead()' - Read data for an IPP message from a HTTP connection.
2530
//
2531
2532
ipp_state_t       // O - Current state
2533
ippRead(http_t *http,     // I - HTTP connection
2534
        ipp_t  *ipp)      // I - IPP data
2535
0
{
2536
0
  DEBUG_printf("ippRead(http=%p, ipp=%p), data_remaining=" CUPS_LLFMT, (void *)http, (void *)ipp, CUPS_LLCAST (http ? http->data_remaining : -1));
2537
2538
0
  if (!http || !ipp)
2539
0
    return (IPP_STATE_ERROR);
2540
2541
0
  DEBUG_printf("2ippRead: http->state=%d, http->used=%d", http->state, http->used);
2542
2543
0
  return (ipp_read_io(http, (ipp_io_cb_t)ipp_read_http, http->blocking, /*parent*/NULL, ipp, /*depth*/0));
2544
0
}
2545
2546
2547
//
2548
// 'ippReadFile()' - Read data for an IPP message from a file.
2549
//
2550
2551
ipp_state_t       // O - Current state
2552
ippReadFile(int   fd,     // I - HTTP data
2553
            ipp_t *ipp)     // I - IPP data
2554
0
{
2555
0
  DEBUG_printf("ippReadFile(fd=%d, ipp=%p)", fd, (void *)ipp);
2556
2557
0
  if (!ipp)
2558
0
    return (IPP_STATE_ERROR);
2559
2560
0
  return (ipp_read_io(&fd, (ipp_io_cb_t)ipp_read_file, /*blocking*/true, /*parent*/NULL, ipp, /*depth*/0));
2561
0
}
2562
2563
2564
//
2565
// 'ippReadIO()' - Read data for an IPP message.
2566
//
2567
2568
ipp_state_t       // O - Current state
2569
ippReadIO(void        *src,   // I - Data source
2570
          ipp_io_cb_t cb,   // I - Read callback function
2571
    bool        blocking,   // I - Use blocking IO?
2572
    ipp_t       *parent,    // I - Parent request, if any
2573
          ipp_t       *ipp)   // I - IPP data
2574
4.74k
{
2575
4.74k
  DEBUG_printf("ippReadIO(src=%p, cb=%p, blocking=%d, parent=%p, ipp=%p)", (void *)src, (void *)cb, blocking, (void *)parent, (void *)ipp);
2576
2577
4.74k
  if (!src || !ipp)
2578
0
    return (IPP_STATE_ERROR);
2579
2580
4.74k
  return (ipp_read_io(src, cb, blocking, parent, ipp, /*depth*/0));
2581
4.74k
}
2582
2583
2584
//
2585
// 'ippRestore()' - Restore a previously saved find position.
2586
//
2587
2588
void
2589
ippRestore(ipp_t *ipp)      // I - IPP message
2590
0
{
2591
0
  if (ipp)
2592
0
  {
2593
0
    if (ipp->find > ipp->fstack)
2594
0
      ipp->find --;
2595
0
    else
2596
0
      ipp->find = NULL;
2597
0
  }
2598
0
}
2599
2600
2601
//
2602
// 'ippSave()' - Save the current find position.
2603
//
2604
2605
void
2606
ippSave(ipp_t *ipp)     // I - IPP message
2607
0
{
2608
0
  if (ipp && ipp->find < (ipp->fstack + _IPP_MAX_FIND))
2609
0
  {
2610
0
    ipp->find[1] = ipp->find[0];
2611
0
    ipp->find ++;
2612
0
  }
2613
0
}
2614
2615
2616
//
2617
// 'ippSetBoolean()' - Set a boolean value in an attribute.
2618
//
2619
// The "ipp" argument refers to an IPP message previously created using
2620
// the @link ippNew@, @link ippNewRequest@, or  @link ippNewResponse@ functions.
2621
//
2622
// The "attr" argument may be modified as a result of setting the value.
2623
//
2624
// The "element" argument specifies which value to set from `0` to
2625
// `ippGetCount(attr)`.
2626
//
2627
2628
bool          // O  - `true` on success, `false` on failure
2629
ippSetBoolean(ipp_t           *ipp, // I  - IPP message
2630
              ipp_attribute_t **attr, // IO - IPP attribute
2631
              size_t          element,  // I  - Value number (`0`-based)
2632
              bool            boolvalue)// I  - Boolean value
2633
0
{
2634
0
  _ipp_value_t  *value;     // Current value
2635
2636
2637
  // Range check input...
2638
0
  if (!ipp || !attr || !*attr || (*attr)->value_tag != IPP_TAG_BOOLEAN || element > (*attr)->num_values)
2639
0
    return (false);
2640
2641
  // Set the value and return...
2642
0
  if ((value = ipp_set_value(ipp, attr, element)) != NULL)
2643
0
    value->boolean = boolvalue;
2644
2645
0
  return (value != NULL);
2646
0
}
2647
2648
2649
//
2650
// 'ippSetCollection()' - Set a collection value in an attribute.
2651
//
2652
// The "ipp" argument refers to an IPP message previously created using
2653
// the @link ippNew@, @link ippNewRequest@, or  @link ippNewResponse@ functions.
2654
//
2655
// The "attr" argument may be modified as a result of setting the value.
2656
//
2657
// The "element" argument specifies which value to set from `0` to
2658
// `ippGetCount(attr)`.
2659
//
2660
2661
bool          // O  - `true` on success, `false` on failure
2662
ippSetCollection(
2663
    ipp_t           *ipp,   // I  - IPP message
2664
    ipp_attribute_t **attr,   // IO - IPP attribute
2665
    size_t          element,    // I  - Value number (`0`-based)
2666
    ipp_t           *colvalue)    // I  - Collection value
2667
0
{
2668
0
  _ipp_value_t  *value;     // Current value
2669
2670
2671
  // Range check input...
2672
0
  if (!ipp || !attr || !*attr || (*attr)->value_tag != IPP_TAG_BEGIN_COLLECTION || element > (*attr)->num_values || !colvalue)
2673
0
    return (false);
2674
2675
  // Set the value and return...
2676
0
  if ((value = ipp_set_value(ipp, attr, element)) != NULL)
2677
0
  {
2678
0
    if (value->collection)
2679
0
      ippDelete(value->collection);
2680
2681
0
    value->collection = colvalue;
2682
0
    colvalue->use ++;
2683
0
  }
2684
2685
0
  return (value != NULL);
2686
0
}
2687
2688
2689
//
2690
// 'ippSetDate()' - Set a dateTime value in an attribute.
2691
//
2692
// The "ipp" argument refers to an IPP message previously created using
2693
// the @link ippNew@, @link ippNewRequest@, or  @link ippNewResponse@ functions.
2694
//
2695
// The "attr" argument may be modified as a result of setting the value.
2696
//
2697
// The "element" argument specifies which value to set from `0` to
2698
// `ippGetCount(attr)`.
2699
//
2700
2701
bool          // O  - `true` on success, `false` on failure
2702
ippSetDate(ipp_t             *ipp,  // I  - IPP message
2703
           ipp_attribute_t   **attr,  // IO - IPP attribute
2704
           size_t            element, // I  - Value number (`0`-based)
2705
           const ipp_uchar_t *datevalue)// I  - dateTime value
2706
0
{
2707
0
  _ipp_value_t  *value;     // Current value
2708
2709
2710
  // Range check input...
2711
0
  if (!ipp || !attr || !*attr || ((*attr)->value_tag != IPP_TAG_DATE && (*attr)->value_tag != IPP_TAG_NOVALUE && (*attr)->value_tag != IPP_TAG_UNKNOWN) || element > (*attr)->num_values || !datevalue)
2712
0
    return (false);
2713
2714
  // Set the value and return...
2715
0
  if ((value = ipp_set_value(ipp, attr, element)) != NULL)
2716
0
  {
2717
0
    (*attr)->value_tag = IPP_TAG_DATE;
2718
0
    memcpy(value->date, datevalue, sizeof(value->date));
2719
0
  }
2720
2721
0
  return (value != NULL);
2722
0
}
2723
2724
2725
//
2726
// 'ippSetGroupTag()' - Set the group tag of an attribute.
2727
//
2728
// The "ipp" argument refers to an IPP message previously created using
2729
// the @link ippNew@, @link ippNewRequest@, or  @link ippNewResponse@ functions.
2730
//
2731
// The "attr" argument may be modified as a result of setting the value.
2732
//
2733
// The "group" argument specifies the IPP attribute group tag: none
2734
// (`IPP_TAG_ZERO`, for member attributes), document (`IPP_TAG_DOCUMENT`),
2735
// event notification (`IPP_TAG_EVENT_NOTIFICATION`), operation
2736
// (`IPP_TAG_OPERATION`), printer (`IPP_TAG_PRINTER`), subscription
2737
// (`IPP_TAG_SUBSCRIPTION`), or unsupported (`IPP_TAG_UNSUPPORTED_GROUP`).
2738
//
2739
2740
bool          // O  - `true` on success, `false` on failure
2741
ippSetGroupTag(
2742
    ipp_t           *ipp,   // I  - IPP message
2743
    ipp_attribute_t **attr,   // IO - Attribute
2744
    ipp_tag_t       group_tag)    // I  - Group tag
2745
0
{
2746
  // Range check input - group tag must be 0x01 to 0x0F, per RFC 8011...
2747
0
  if (!ipp || !attr || !*attr || group_tag < IPP_TAG_ZERO || group_tag == IPP_TAG_END || group_tag >= IPP_TAG_UNSUPPORTED_VALUE)
2748
0
    return (false);
2749
2750
  // Set the group tag and return...
2751
0
  (*attr)->group_tag = group_tag;
2752
2753
0
  return (true);
2754
0
}
2755
2756
2757
//
2758
// 'ippSetInteger()' - Set an integer or enum value in an attribute.
2759
//
2760
// The "ipp" argument refers to an IPP message previously created using
2761
// the @link ippNew@, @link ippNewRequest@, or  @link ippNewResponse@ functions.
2762
//
2763
// The "attr" argument may be modified as a result of setting the value.
2764
//
2765
// The "element" argument specifies which value to set from `0` to
2766
// `ippGetCount(attr)`.
2767
//
2768
2769
bool          // O  - `true` on success, `false` on failure
2770
ippSetInteger(ipp_t           *ipp, // I  - IPP message
2771
              ipp_attribute_t **attr, // IO - IPP attribute
2772
              size_t          element,  // I  - Value number (`0`-based)
2773
              int             intvalue) // I  - Integer/enum value
2774
0
{
2775
0
  _ipp_value_t  *value;     // Current value
2776
2777
2778
  // Range check input...
2779
0
  if (!ipp || !attr || !*attr || ((*attr)->value_tag != IPP_TAG_INTEGER && (*attr)->value_tag != IPP_TAG_ENUM && (*attr)->value_tag != IPP_TAG_NOVALUE && (*attr)->value_tag != IPP_TAG_UNKNOWN) || element > (*attr)->num_values)
2780
0
    return (false);
2781
2782
  // Set the value and return...
2783
0
  if ((value = ipp_set_value(ipp, attr, element)) != NULL)
2784
0
  {
2785
0
    if ((*attr)->value_tag != IPP_TAG_ENUM)
2786
0
      (*attr)->value_tag = IPP_TAG_INTEGER;
2787
2788
0
    value->integer = intvalue;
2789
0
  }
2790
2791
0
  return (value != NULL);
2792
0
}
2793
2794
2795
//
2796
// 'ippSetName()' - Set the name of an attribute.
2797
//
2798
// The "ipp" argument refers to an IPP message previously created using
2799
// the @link ippNew@, @link ippNewRequest@, or  @link ippNewResponse@ functions.
2800
//
2801
// The "attr" argument may be modified as a result of setting the value.
2802
//
2803
2804
bool          // O  - `true` on success, `false` on failure
2805
ippSetName(ipp_t           *ipp,  // I  - IPP message
2806
     ipp_attribute_t **attr,  // IO - IPP attribute
2807
     const char      *name) // I  - Attribute name
2808
0
{
2809
0
  char  *temp;        // Temporary name value
2810
2811
2812
  // Range check input...
2813
0
  if (!ipp || !attr || !*attr || !name)
2814
0
    return (false);
2815
2816
  // Set the value and return...
2817
0
  if ((temp = _cupsStrAlloc(name)) != NULL)
2818
0
  {
2819
0
    if ((*attr)->name)
2820
0
      _cupsStrFree((*attr)->name);
2821
2822
0
    (*attr)->name = temp;
2823
0
  }
2824
2825
0
  return (temp != NULL);
2826
0
}
2827
2828
2829
//
2830
// 'ippSetOctetString()' - Set an octetString value in an IPP attribute.
2831
//
2832
// The "ipp" argument refers to an IPP message previously created using
2833
// the @link ippNew@, @link ippNewRequest@, or  @link ippNewResponse@ functions.
2834
//
2835
// The "attr" argument may be modified as a result of setting the value.
2836
//
2837
// The "element" argument specifies which value to set from `0` to
2838
// `ippGetCount(attr)`.
2839
//
2840
2841
bool          // O  - `true` on success, `false` on failure
2842
ippSetOctetString(
2843
    ipp_t           *ipp,   // I  - IPP message
2844
    ipp_attribute_t **attr,   // IO - IPP attribute
2845
    size_t          element,    // I  - Value number (`0`-based)
2846
    const void      *data,    // I  - Pointer to octetString data
2847
    size_t          datalen)    // I  - Length of octetString data
2848
0
{
2849
0
  _ipp_value_t  *value;     // Current value
2850
2851
2852
  // Range check input...
2853
0
  if (!ipp || !attr || !*attr || ((*attr)->value_tag != IPP_TAG_STRING && (*attr)->value_tag != IPP_TAG_NOVALUE && (*attr)->value_tag != IPP_TAG_UNKNOWN && (*attr)->value_tag != IPP_TAG_EXTENSION) || element > (*attr)->num_values || datalen > IPP_MAX_LENGTH)
2854
0
    return (false);
2855
2856
  // Set the value and return...
2857
0
  if ((value = ipp_set_value(ipp, attr, element)) != NULL)
2858
0
  {
2859
0
    if ((int)((*attr)->value_tag) & IPP_TAG_CUPS_CONST)
2860
0
    {
2861
      // Just copy the pointer...
2862
0
      value->unknown.data   = (void *)data;
2863
0
      value->unknown.length = datalen;
2864
0
    }
2865
0
    else
2866
0
    {
2867
      // Copy the data...
2868
0
      (*attr)->value_tag = IPP_TAG_STRING;
2869
2870
0
      if (value->unknown.data)
2871
0
      {
2872
        // Free previous data...
2873
0
  free(value->unknown.data);
2874
2875
0
  value->unknown.data   = NULL;
2876
0
        value->unknown.length = 0;
2877
0
      }
2878
2879
0
      if (datalen > 0)
2880
0
      {
2881
0
  void  *temp;      // Temporary data pointer
2882
2883
0
  if ((temp = malloc((size_t)datalen)) != NULL)
2884
0
  {
2885
0
    memcpy(temp, data, (size_t)datalen);
2886
2887
0
    value->unknown.data   = temp;
2888
0
    value->unknown.length = datalen;
2889
0
  }
2890
0
  else
2891
0
  {
2892
0
    return (false);
2893
0
  }
2894
0
      }
2895
0
    }
2896
0
  }
2897
2898
0
  return (value != NULL);
2899
0
}
2900
2901
2902
//
2903
// 'ippSetOperation()' - Set the operation ID in an IPP request message.
2904
//
2905
// The "ipp" argument refers to an IPP message previously created using
2906
// the @link ippNew@, @link ippNewRequest@, or  @link ippNewResponse@ functions.
2907
//
2908
2909
bool          // O  - `true` on success, `false` on failure
2910
ippSetOperation(ipp_t    *ipp,    // I - IPP request message
2911
                ipp_op_t op)    // I - Operation ID
2912
0
{
2913
  // Range check input...
2914
0
  if (!ipp)
2915
0
    return (false);
2916
2917
  // Set the operation and return...
2918
0
  ipp->request.op_status = (short)op;
2919
2920
0
  return (true);
2921
0
}
2922
2923
2924
//
2925
// 'ippSetRange()' - Set a rangeOfInteger value in an attribute.
2926
//
2927
// The "ipp" argument refers to an IPP message previously created using
2928
// the @link ippNew@, @link ippNewRequest@, or  @link ippNewResponse@ functions.
2929
//
2930
// The "attr" argument may be modified as a result of setting the value.
2931
//
2932
// The "element" argument specifies which value to set from `0` to
2933
// `ippGetCount(attr)`.
2934
//
2935
2936
bool          // O  - `true` on success, `false` on failure
2937
ippSetRange(ipp_t           *ipp, // I  - IPP message
2938
            ipp_attribute_t **attr, // IO - IPP attribute
2939
            size_t          element,  // I  - Value number (`0`-based)
2940
      int             lowervalue, // I  - Lower bound for range
2941
      int             uppervalue) // I  - Upper bound for range
2942
0
{
2943
0
  _ipp_value_t  *value;     // Current value
2944
2945
2946
  // Range check input...
2947
0
  if (!ipp || !attr || !*attr || ((*attr)->value_tag != IPP_TAG_RANGE && (*attr)->value_tag != IPP_TAG_NOVALUE && (*attr)->value_tag != IPP_TAG_UNKNOWN) || element > (*attr)->num_values || lowervalue > uppervalue)
2948
0
    return (false);
2949
2950
  // Set the value and return...
2951
0
  if ((value = ipp_set_value(ipp, attr, element)) != NULL)
2952
0
  {
2953
0
    (*attr)->value_tag = IPP_TAG_RANGE;
2954
0
    value->range.lower = lowervalue;
2955
0
    value->range.upper = uppervalue;
2956
0
  }
2957
2958
0
  return (value != NULL);
2959
0
}
2960
2961
2962
//
2963
// 'ippSetRequestId()' - Set the request ID in an IPP message.
2964
//
2965
// The "ipp" argument refers to an IPP message previously created using
2966
// the @link ippNew@, @link ippNewRequest@, or  @link ippNewResponse@ functions.
2967
//
2968
// The "request_id" argument must be greater than 0.
2969
//
2970
2971
bool          // O  - `true` on success, `false` on failure
2972
ippSetRequestId(ipp_t *ipp,   // I - IPP message
2973
                int   request_id) // I - Request ID
2974
0
{
2975
  // Range check input; not checking request_id values since ipptool wants to
2976
  // send invalid values for conformance testing and a bad request_id does not
2977
  // affect the encoding of a message...
2978
0
  if (!ipp)
2979
0
    return (false);
2980
2981
  // Set the request ID and return...
2982
0
  ipp->request.request_id = request_id;
2983
2984
0
  return (true);
2985
0
}
2986
2987
2988
//
2989
// 'ippSetResolution()' - Set a resolution value in an attribute.
2990
//
2991
// The "ipp" argument refers to an IPP message previously created using
2992
// the @link ippNew@, @link ippNewRequest@, or  @link ippNewResponse@ functions.
2993
//
2994
// The "attr" argument may be modified as a result of setting the value.
2995
//
2996
// The "element" argument specifies which value to set from `0` to
2997
// `ippGetCount(attr)`.
2998
//
2999
3000
bool          // O  - `true` on success, `false` on failure
3001
ippSetResolution(
3002
    ipp_t           *ipp,   // I  - IPP message
3003
    ipp_attribute_t **attr,   // IO - IPP attribute
3004
    size_t          element,    // I  - Value number (`0`-based)
3005
    ipp_res_t       unitsvalue,   // I  - Resolution units
3006
    int             xresvalue,    // I  - Horizontal/cross feed resolution
3007
    int             yresvalue)    // I  - Vertical/feed resolution
3008
0
{
3009
0
  _ipp_value_t  *value;     // Current value
3010
3011
3012
  // Range check input...
3013
0
  if (!ipp || !attr || !*attr || ((*attr)->value_tag != IPP_TAG_RESOLUTION && (*attr)->value_tag != IPP_TAG_NOVALUE && (*attr)->value_tag != IPP_TAG_UNKNOWN) || element > (*attr)->num_values || xresvalue <= 0 || yresvalue <= 0 || unitsvalue < IPP_RES_PER_INCH || unitsvalue > IPP_RES_PER_CM)
3014
0
    return (false);
3015
3016
  // Set the value and return...
3017
0
  if ((value = ipp_set_value(ipp, attr, element)) != NULL)
3018
0
  {
3019
0
    (*attr)->value_tag      = IPP_TAG_RESOLUTION;
3020
0
    value->resolution.units = unitsvalue;
3021
0
    value->resolution.xres  = xresvalue;
3022
0
    value->resolution.yres  = yresvalue;
3023
0
  }
3024
3025
0
  return (value != NULL);
3026
0
}
3027
3028
3029
//
3030
// 'ippSetState()' - Set the current state of the IPP message.
3031
//
3032
3033
bool          // O  - `true` on success, `false` on failure
3034
ippSetState(ipp_t       *ipp,   // I - IPP message
3035
            ipp_state_t state)    // I - IPP state value
3036
3.72k
{
3037
  // Range check input...
3038
3.72k
  if (!ipp)
3039
0
    return (false);
3040
3041
  // Set the state and return...
3042
3.72k
  ipp->state   = state;
3043
3.72k
  ipp->current = NULL;
3044
3045
3.72k
  return (true);
3046
3.72k
}
3047
3048
3049
//
3050
// 'ippSetStatusCode()' - Set the status code in an IPP response or event message.
3051
//
3052
// The "ipp" argument refers to an IPP message previously created using
3053
// the @link ippNew@, @link ippNewRequest@, or  @link ippNewResponse@ functions.
3054
//
3055
3056
bool          // O  - `true` on success, `false` on failure
3057
ippSetStatusCode(ipp_t        *ipp, // I - IPP response or event message
3058
                 ipp_status_t status) // I - Status code
3059
0
{
3060
  // Range check input...
3061
0
  if (!ipp)
3062
0
    return (false);
3063
3064
  // Set the status code and return...
3065
0
  ipp->request.op_status = (short)status;
3066
3067
0
  return (true);
3068
0
}
3069
3070
3071
//
3072
// 'ippSetString()' - Set a string value in an attribute.
3073
//
3074
// The "ipp" argument refers to an IPP message previously created using
3075
// the @link ippNew@, @link ippNewRequest@, or  @link ippNewResponse@ functions.
3076
//
3077
// The "attr" argument may be modified as a result of setting the value.
3078
//
3079
// The "element" argument specifies which value to set from `0` to
3080
// `ippGetCount(attr)`.
3081
//
3082
3083
bool          // O  - `true` on success, `false` on failure
3084
ippSetString(ipp_t           *ipp,  // I  - IPP message
3085
             ipp_attribute_t **attr,  // IO - IPP attribute
3086
             size_t          element, // I  - Value number (`0`-based)
3087
       const char      *strvalue) // I  - String value
3088
0
{
3089
0
  char    *temp;      // Temporary string
3090
0
  _ipp_value_t  *value;     // Current value
3091
0
  ipp_tag_t value_tag;    // Value tag
3092
3093
3094
  // Range check input...
3095
0
  if (attr && *attr)
3096
0
    value_tag = (*attr)->value_tag & IPP_TAG_CUPS_MASK;
3097
0
  else
3098
0
    value_tag = IPP_TAG_ZERO;
3099
3100
0
  if (!ipp || !attr || !*attr || (value_tag < IPP_TAG_TEXT && value_tag != IPP_TAG_TEXTLANG && value_tag != IPP_TAG_NAMELANG && value_tag != IPP_TAG_NOVALUE && value_tag != IPP_TAG_UNKNOWN) || value_tag > IPP_TAG_MIMETYPE || element > (*attr)->num_values || !strvalue)
3101
0
    return (false);
3102
3103
  // Set the value and return...
3104
0
  if ((value = ipp_set_value(ipp, attr, element)) != NULL)
3105
0
  {
3106
0
    if (value_tag == IPP_TAG_NOVALUE || value_tag == IPP_TAG_UNKNOWN)
3107
0
      (*attr)->value_tag = IPP_TAG_KEYWORD;
3108
3109
0
    if ((int)((*attr)->value_tag) & IPP_TAG_CUPS_CONST)
3110
0
    {
3111
0
      if (element > 0 && !value->string.language && (*attr)->values[0].string.language)
3112
0
  value->string.language = (*attr)->values[0].string.language;
3113
3114
0
      value->string.text = (char *)strvalue;
3115
0
    }
3116
0
    else if ((temp = _cupsStrAlloc(strvalue)) != NULL)
3117
0
    {
3118
0
      if (element > 0 && !value->string.language && (*attr)->values[0].string.language)
3119
0
  value->string.language = _cupsStrAlloc((*attr)->values[0].string.language);
3120
3121
0
      if (value->string.text)
3122
0
        _cupsStrFree(value->string.text);
3123
3124
0
      value->string.text = temp;
3125
0
    }
3126
0
    else
3127
0
    {
3128
0
      return (false);
3129
0
    }
3130
0
  }
3131
3132
0
  return (value != NULL);
3133
0
}
3134
3135
3136
//
3137
// 'ippSetStringf()' - Set a formatted string value of an attribute.
3138
//
3139
// The "ipp" argument refers to an IPP message previously created using
3140
// the @link ippNew@, @link ippNewRequest@, or  @link ippNewResponse@ functions.
3141
//
3142
// The "attr" argument may be modified as a result of setting the value.
3143
//
3144
// The "element" argument specifies which value to set from `0` to
3145
// `ippGetCount(attr)`.
3146
//
3147
// The "format" argument uses formatting characters compatible with the
3148
// printf family of standard functions.  Additional arguments follow it as
3149
// needed.  The formatted string is truncated as needed to the maximum length of
3150
// the corresponding value type.
3151
//
3152
3153
bool          // O  - `true` on success, `false` on failure
3154
ippSetStringf(ipp_t           *ipp, // I  - IPP message
3155
              ipp_attribute_t **attr, // IO - IPP attribute
3156
              size_t          element,  // I  - Value number (`0`-based)
3157
        const char      *format,  // I  - Printf-style format string
3158
        ...)      // I  - Additional arguments as needed
3159
0
{
3160
0
  int   ret;      // Return value
3161
0
  va_list ap;     // Pointer to additional arguments
3162
3163
3164
0
  va_start(ap, format);
3165
0
  ret = ippSetStringfv(ipp, attr, element, format, ap);
3166
0
  va_end(ap);
3167
3168
0
  return (ret);
3169
0
}
3170
3171
3172
//
3173
// 'ippSetStringfv()' - Set a formatted string value of an attribute.
3174
//
3175
// The "ipp" argument refers to an IPP message previously created using
3176
// the @link ippNew@, @link ippNewRequest@, or  @link ippNewResponse@ functions.
3177
//
3178
// The "attr" argument may be modified as a result of setting the value.
3179
//
3180
// The "element" argument specifies which value to set from `0` to
3181
// `ippGetCount(attr)`.
3182
//
3183
// The "format" argument uses formatting characters compatible with the
3184
// printf family of standard functions.  Additional arguments follow it as
3185
// needed.  The formatted string is truncated as needed to the maximum length of
3186
// the corresponding value type.
3187
//
3188
3189
bool          // O  - `true` on success, `false` on failure
3190
ippSetStringfv(ipp_t           *ipp,  // I  - IPP message
3191
               ipp_attribute_t **attr,  // IO - IPP attribute
3192
               size_t          element, // I  - Value number (`0`-based)
3193
         const char      *format, // I  - Printf-style format string
3194
         va_list         ap)  // I  - Pointer to additional arguments
3195
0
{
3196
0
  ipp_tag_t value_tag;    // Value tag
3197
0
  char    buffer[IPP_MAX_TEXT + 4];
3198
          // Formatted text string
3199
0
  ssize_t bytes,      // Length of formatted value
3200
0
    max_bytes;    // Maximum number of bytes for value
3201
3202
3203
  // Range check input...
3204
0
  if (attr && *attr)
3205
0
    value_tag = (*attr)->value_tag & IPP_TAG_CUPS_MASK;
3206
0
  else
3207
0
    value_tag = IPP_TAG_ZERO;
3208
3209
0
  if (!ipp || !attr || !*attr || (value_tag < IPP_TAG_TEXT && value_tag != IPP_TAG_TEXTLANG && value_tag != IPP_TAG_NAMELANG && value_tag != IPP_TAG_NOVALUE && value_tag != IPP_TAG_UNKNOWN) || value_tag > IPP_TAG_MIMETYPE || !format)
3210
0
    return (false);
3211
3212
  // Format the string...
3213
0
  if (!strcmp(format, "%s"))
3214
0
  {
3215
    // Optimize the simple case...
3216
0
    const char *s = va_arg(ap, char *);
3217
3218
0
    if (!s)
3219
0
      s = "(null)";
3220
3221
0
    bytes = (ssize_t)strlen(s);
3222
0
    cupsCopyString(buffer, s, sizeof(buffer));
3223
0
  }
3224
0
  else
3225
0
  {
3226
    // Do a full formatting of the message...
3227
0
    if ((bytes = vsnprintf(buffer, sizeof(buffer), format, ap)) < 0)
3228
0
      return (0);
3229
0
  }
3230
3231
  // Limit the length of the string...
3232
0
  switch (value_tag)
3233
0
  {
3234
0
    default :
3235
0
    case IPP_TAG_TEXT :
3236
0
    case IPP_TAG_TEXTLANG :
3237
0
        max_bytes = IPP_MAX_TEXT;
3238
0
        break;
3239
3240
0
    case IPP_TAG_NAME :
3241
0
    case IPP_TAG_NAMELANG :
3242
0
        max_bytes = IPP_MAX_NAME;
3243
0
        break;
3244
3245
0
    case IPP_TAG_CHARSET :
3246
0
        max_bytes = IPP_MAX_CHARSET;
3247
0
        break;
3248
3249
0
    case IPP_TAG_NOVALUE :
3250
0
    case IPP_TAG_UNKNOWN :
3251
0
    case IPP_TAG_KEYWORD :
3252
0
        max_bytes = IPP_MAX_KEYWORD;
3253
0
        break;
3254
3255
0
    case IPP_TAG_LANGUAGE :
3256
0
        max_bytes = IPP_MAX_LANGUAGE;
3257
0
        break;
3258
3259
0
    case IPP_TAG_MIMETYPE :
3260
0
        max_bytes = IPP_MAX_MIMETYPE;
3261
0
        break;
3262
3263
0
    case IPP_TAG_URI :
3264
0
        max_bytes = IPP_MAX_URI;
3265
0
        break;
3266
3267
0
    case IPP_TAG_URISCHEME :
3268
0
        max_bytes = IPP_MAX_URISCHEME;
3269
0
        break;
3270
0
  }
3271
3272
0
  if (bytes >= max_bytes)
3273
0
  {
3274
    // Safely truncate the UTF-8 string...
3275
0
    char  *bufmax,    // Buffer at max_bytes
3276
0
    *bufptr;    // Pointer into buffer
3277
3278
0
    bufptr = buffer + strlen(buffer) - 1;
3279
0
    bufmax = buffer + max_bytes - 1;
3280
3281
0
    while (bufptr > bufmax)
3282
0
    {
3283
0
      if (*bufptr & 0x80)
3284
0
      {
3285
0
        while ((*bufptr & 0xc0) == 0x80 && bufptr > buffer)
3286
0
          bufptr --;
3287
0
      }
3288
3289
0
      if (bufptr > buffer)
3290
0
        bufptr --;
3291
0
    }
3292
3293
0
    *bufptr = '\0';
3294
0
  }
3295
3296
  // Set the formatted string and return...
3297
0
  if ((*attr)->value_tag & IPP_TAG_CUPS_CONST)
3298
0
    return (ippSetString(ipp, attr, element, _cupsStrAlloc(buffer)));
3299
0
  else
3300
0
    return (ippSetString(ipp, attr, element, buffer));
3301
0
}
3302
3303
3304
//
3305
// 'ippSetValueTag()' - Set the value tag of an attribute.
3306
//
3307
// The "ipp" argument refers to an IPP message previously created using
3308
// the @link ippNew@, @link ippNewRequest@, or  @link ippNewResponse@ functions.
3309
//
3310
// The "attr" argument may be modified as a result of setting the value.
3311
//
3312
// Integer (`IPP_TAG_INTEGER`) values can be promoted to rangeOfInteger
3313
// (`IPP_TAG_RANGE`) values, keyword (`IPP_TAG_KEYWORD`) values can be promoted
3314
// to name (`IPP_TAG_NAME`) or nameWithLanguage (`IPP_TAG_NAMELANG`) values,
3315
// name (`IPP_TAG_NAME`) values can be promoted to nameWithLanguage
3316
// (`IPP_TAG_NAMELANG`) values, text (`IPP_TAG_TEXT`) values can be promoted to
3317
// textWithLanguage (`IPP_TAG_TEXTLANG`) values, and all values can be demoted
3318
// to the various out-of-band value tags such as no-value (`IPP_TAG_NOVALUE`).
3319
// All other changes will be rejected.
3320
//
3321
// Promoting a string attribute to nameWithLanguage or textWithLanguage adds the
3322
// language code in the "attributes-natural-language" attribute or, if not
3323
// present, the language code for the current locale.
3324
//
3325
3326
bool          // O  - `true` on success, `false` on failure
3327
ippSetValueTag(
3328
    ipp_t          *ipp,    // I  - IPP message
3329
    ipp_attribute_t **attr,   // IO - IPP attribute
3330
    ipp_tag_t       value_tag)    // I  - Value tag
3331
716k
{
3332
716k
  size_t  i;      // Looping var
3333
716k
  _ipp_value_t  *value;     // Current value
3334
716k
  int   integer;    // Current integer value
3335
716k
  cups_lang_t *language;    // Current language
3336
716k
  char    code[32];   // Language code
3337
716k
  ipp_tag_t temp_tag;   // Temporary value tag
3338
3339
3340
716k
  DEBUG_printf("ippSetValueTag(ipp=%p, attr=%p(%p), value_tag=0x%x(%s))", ipp, attr, attr ? *attr : NULL, value_tag, ippTagString(value_tag));
3341
3342
  // Range check input...
3343
716k
  if (!ipp || !attr || !*attr)
3344
0
    return (false);
3345
3346
  // If there is no change, return immediately...
3347
716k
  temp_tag = (ipp_tag_t)((int)((*attr)->value_tag) & IPP_TAG_CUPS_MASK);
3348
3349
716k
  if (value_tag == temp_tag)
3350
0
  {
3351
0
    DEBUG_puts("2ippSetValueTag: No change.");
3352
0
    return (true);
3353
0
  }
3354
3355
  // Otherwise implement changes as needed...
3356
716k
  switch (value_tag)
3357
716k
  {
3358
0
    case IPP_TAG_UNSUPPORTED_VALUE :
3359
0
    case IPP_TAG_DEFAULT :
3360
0
    case IPP_TAG_UNKNOWN :
3361
1.95k
    case IPP_TAG_NOVALUE :
3362
1.95k
    case IPP_TAG_NOTSETTABLE :
3363
1.95k
    case IPP_TAG_DELETEATTR :
3364
1.95k
    case IPP_TAG_ADMINDEFINE :
3365
        // Free any existing values...
3366
1.95k
        if ((*attr)->num_values > 0)
3367
1.95k
          ipp_free_values(*attr, 0, (*attr)->num_values);
3368
3369
        // Set out-of-band value...
3370
1.95k
        (*attr)->value_tag = value_tag;
3371
1.95k
        break;
3372
3373
394
    case IPP_TAG_RANGE :
3374
394
        if (temp_tag != IPP_TAG_INTEGER)
3375
0
        {
3376
0
          DEBUG_printf("2ippSetValueTag: Unable to convert %s to rangeOfInteger.", ippTagString(temp_tag));
3377
0
          return (false);
3378
0
        }
3379
3380
16.9k
        for (i = (*attr)->num_values, value = (*attr)->values; i > 0; i --, value ++)
3381
16.5k
        {
3382
16.5k
          integer            = value->integer;
3383
16.5k
          value->range.lower = value->range.upper = integer;
3384
16.5k
          DEBUG_printf("2ippSetValueTag: values[%u].lower=%d, .upper=%d", (unsigned)((*attr)->num_values - i), value->range.lower, value->range.upper);
3385
16.5k
        }
3386
3387
394
        (*attr)->value_tag = IPP_TAG_RANGE;
3388
394
        break;
3389
3390
1.10k
    case IPP_TAG_NAME :
3391
1.10k
        if (temp_tag != IPP_TAG_KEYWORD)
3392
5
        {
3393
5
          DEBUG_printf("2ippSetValueTag: Unable to convert %s to name.", ippTagString(temp_tag));
3394
5
          return (false);
3395
5
        }
3396
3397
1.10k
        (*attr)->value_tag = (ipp_tag_t)(IPP_TAG_NAME | ((*attr)->value_tag & IPP_TAG_CUPS_CONST));
3398
1.10k
        break;
3399
3400
3.57k
    case IPP_TAG_NAMELANG :
3401
4.30k
    case IPP_TAG_TEXTLANG :
3402
4.30k
        if (value_tag == IPP_TAG_NAMELANG && (temp_tag != IPP_TAG_NAME && temp_tag != IPP_TAG_KEYWORD))
3403
4
        {
3404
4
          DEBUG_printf("2ippSetValueTag: Unable to convert %s to nameWithLanguage.", ippTagString(temp_tag));
3405
4
          return (false);
3406
4
        }
3407
3408
4.30k
        if (value_tag == IPP_TAG_TEXTLANG && temp_tag != IPP_TAG_TEXT)
3409
3
        {
3410
3
          DEBUG_printf("2ippSetValueTag: Unable to convert %s to textWithLanguage.", ippTagString(temp_tag));
3411
3
          return (false);
3412
3
        }
3413
3414
4.29k
        if (ipp->attrs && ipp->attrs->next && ipp->attrs->next->name && !strcmp(ipp->attrs->next->name, "attributes-natural-language") && (ipp->attrs->next->value_tag & IPP_TAG_CUPS_MASK) == IPP_TAG_LANGUAGE)
3415
519
        {
3416
          // Use the language code from the IPP message...
3417
519
    (*attr)->values[0].string.language = _cupsStrAlloc(ipp->attrs->next->values[0].string.text);
3418
519
        }
3419
3.77k
        else
3420
3.77k
        {
3421
          // Otherwise, use the language code corresponding to the locale...
3422
3.77k
    language = cupsLangDefault();
3423
3.77k
    (*attr)->values[0].string.language = _cupsStrAlloc(ipp_lang_code(cupsLangGetName(language), code, sizeof(code)));
3424
3.77k
        }
3425
3426
4.29k
  DEBUG_printf("2ippSetValueTag: values[0].string.language=\"%s\"(%p)", (*attr)->values[0].string.language, (*attr)->values[0].string.language);
3427
3428
148k
        for (i = (*attr)->num_values - 1, value = (*attr)->values + 1; i > 0; i --, value ++)
3429
143k
        {
3430
143k
          value->string.language = _cupsStrAlloc((*attr)->values[0].string.language);
3431
143k
          DEBUG_printf("2ippSetValueTag: values[%u].string.language=\"%s\"(%s)", (unsigned)((*attr)->num_values - i), value->string.language, value->string.language);
3432
143k
        }
3433
3434
4.29k
        if ((int)(*attr)->value_tag & IPP_TAG_CUPS_CONST)
3435
0
        {
3436
          // Make copies of all values...
3437
0
    for (i = (*attr)->num_values, value = (*attr)->values; i > 0; i --, value ++)
3438
0
    {
3439
0
      value->string.text = _cupsStrAlloc(value->string.text);
3440
0
      DEBUG_printf("2ippSetValueTag: values[%u].string.text=\"%s\"(%s)", (unsigned)((*attr)->num_values - i), value->string.text, value->string.text);
3441
0
    }
3442
0
        }
3443
3444
4.29k
        (*attr)->value_tag = value_tag;
3445
4.29k
        break;
3446
3447
708k
    case IPP_TAG_KEYWORD :
3448
708k
        if (temp_tag == IPP_TAG_NAME || temp_tag == IPP_TAG_NAMELANG)
3449
708k
        {
3450
708k
          DEBUG_printf("2ippSetValueTag: Ignoring change of %s to keyword.", ippTagString(temp_tag));
3451
708k
          break;      // Silently "allow" name -> keyword
3452
708k
        }
3453
3454
5
        DEBUG_printf("2ippSetValueTag: Unable to convert %s to keyword.", ippTagString(temp_tag));
3455
5
        return (false);
3456
3457
0
    case IPP_TAG_EXTENSION :
3458
0
        if (temp_tag == IPP_TAG_STRING && value_tag == IPP_TAG_EXTENSION)
3459
0
        {
3460
          // Allow octetString -> extension
3461
0
          (*attr)->value_tag = value_tag;
3462
0
          DEBUG_puts("2ippSetValueTag: Changing octetString to extension.");
3463
0
          break;
3464
0
        }
3465
3466
5
    default :
3467
5
        DEBUG_printf("2ippSetValueTag: Unable to convert %s to %s.", ippTagString(temp_tag), ippTagString(value_tag));
3468
5
        return (false);
3469
716k
  }
3470
3471
716k
  DEBUG_puts("2ippSetValueTag: Successful.");
3472
716k
  return (true);
3473
716k
}
3474
3475
3476
//
3477
// 'ippSetVersion()' - Set the version number in an IPP message.
3478
//
3479
// The "ipp" argument refers to an IPP message previously created using
3480
// the @link ippNew@, @link ippNewRequest@, or  @link ippNewResponse@ functions.
3481
//
3482
// The valid version numbers are currently 1.0, 1.1, 2.0, 2.1, and 2.2.
3483
//
3484
3485
bool          // O - `true` on success, `false` on failure
3486
ippSetVersion(ipp_t *ipp,   // I - IPP message
3487
              int   major,    // I - Major version number (major.minor)
3488
              int   minor)    // I - Minor version number (major.minor)
3489
0
{
3490
  // Range check input...
3491
0
  if (!ipp || major < 0 || minor < 0)
3492
0
    return (false);
3493
3494
  // Set the version number...
3495
0
  ipp->request.version[0] = (ipp_uchar_t)major;
3496
0
  ipp->request.version[1] = (ipp_uchar_t)minor;
3497
3498
0
  return (true);
3499
0
}
3500
3501
3502
//
3503
// 'ippTimeToDate()' - Convert from time in seconds to RFC 2579 format.
3504
//
3505
3506
const ipp_uchar_t *     // O - RFC-2579 date/time data
3507
ippTimeToDate(time_t t)     // I - Time in seconds
3508
0
{
3509
0
  struct tm unixdate;   // UNIX unixdate/time info
3510
0
  ipp_uchar_t *date = _cupsGlobals()->ipp_date;
3511
          // RFC-2579 date/time data
3512
3513
3514
  // RFC-2579 date/time format is:
3515
  //
3516
  //   Byte(s)  Description
3517
  //   -------  -----------
3518
  //   0-1      Year (0 to 65535)
3519
  //   2        Month (1 to 12)
3520
  //   3        Day (1 to 31)
3521
  //   4        Hours (0 to 23)
3522
  //   5        Minutes (0 to 59)
3523
  //   6        Seconds (0 to 60, 60 = "leap second")
3524
  //   7        Deciseconds (0 to 9)
3525
  //   8        +/- UTC
3526
  //   9        UTC hours (0 to 11)
3527
  //   10       UTC minutes (0 to 59)
3528
#if _WIN32
3529
  if (gmtime_s(&unixdate, &t))
3530
#else
3531
0
  if (!gmtime_r(&t, &unixdate))
3532
0
#endif // _WIN32
3533
0
  {
3534
0
    memset(&unixdate, 0, sizeof(unixdate));
3535
0
    unixdate.tm_mday = 1;
3536
0
  }
3537
0
  unixdate.tm_year += 1900;
3538
3539
0
  date[0]  = (ipp_uchar_t)(unixdate.tm_year >> 8);
3540
0
  date[1]  = (ipp_uchar_t)(unixdate.tm_year);
3541
0
  date[2]  = (ipp_uchar_t)(unixdate.tm_mon + 1);
3542
0
  date[3]  = (ipp_uchar_t)unixdate.tm_mday;
3543
0
  date[4]  = (ipp_uchar_t)unixdate.tm_hour;
3544
0
  date[5]  = (ipp_uchar_t)unixdate.tm_min;
3545
0
  date[6]  = (ipp_uchar_t)unixdate.tm_sec;
3546
0
  date[7]  = 0;
3547
0
  date[8]  = '+';
3548
0
  date[9]  = 0;
3549
0
  date[10] = 0;
3550
3551
0
  return (date);
3552
0
}
3553
3554
3555
//
3556
// 'ippValidateAttribute()' - Validate the contents of an attribute.
3557
//
3558
// This function validates the contents of an attribute based on the name and
3559
// value tag.  `true` is returned if the attribute is valid, `false` otherwise.
3560
// On failure, @link cupsGetErrorString@ is set to a human-readable message.
3561
//
3562
3563
bool          // O - `true` if valid, `false` otherwise
3564
ippValidateAttribute(
3565
    ipp_attribute_t *attr)    // I - Attribute
3566
110k
{
3567
110k
  ipp_tag_t value_tag;    // Masked value tag
3568
110k
  _ipp_value_t  *value;     // Current value
3569
110k
  size_t  i;      // Looping var
3570
110k
  char    scheme[64],   // Scheme from URI
3571
110k
    userpass[256],    // Username/password from URI
3572
110k
    hostname[256],    // Hostname from URI
3573
110k
    resource[1024];   // Resource from URI
3574
110k
  int   port,     // Port number from URI
3575
110k
    uri_status;   // URI separation status
3576
110k
  const char  *ptr;     // Pointer into string
3577
110k
  ipp_attribute_t *colattr;   // Collection attribute
3578
110k
  ipp_uchar_t *date;      // Current date value
3579
3580
3581
  // Skip separators.
3582
110k
  if (!attr->name)
3583
48.8k
    return (true);
3584
3585
  // Validate the attribute name.
3586
61.2k
  if (!ipp_is_valid_keyword(/*name*/NULL, attr->name))
3587
0
    return (false);
3588
3589
61.2k
  value_tag = attr->value_tag & IPP_TAG_CUPS_MASK;
3590
3591
61.2k
  switch (value_tag)
3592
61.2k
  {
3593
984
    case IPP_TAG_INTEGER :
3594
984
        break;
3595
3596
1.19k
    case IPP_TAG_BOOLEAN :
3597
722k
        for (i = attr->num_values, value = attr->values; i > 0; i --, value ++)
3598
721k
  {
3599
721k
    if (value->boolean != 0 && value->boolean != 1)
3600
0
    {
3601
0
      ipp_set_error(IPP_STATUS_ERROR_BAD_REQUEST, _("\"%s\": Bad boolean value %d (RFC 8011 section 5.1.21)."), attr->name, value->boolean);
3602
0
      return (false);
3603
0
    }
3604
721k
  }
3605
1.19k
        break;
3606
3607
1.19k
    case IPP_TAG_ENUM :
3608
3.19k
        for (i = attr->num_values, value = attr->values; i > 0; i --, value ++)
3609
2.61k
  {
3610
2.61k
    if (value->integer < 1)
3611
222
    {
3612
222
      ipp_set_error(IPP_STATUS_ERROR_BAD_REQUEST, _("\"%s\": Bad enum value %d - out of range (RFC 8011 section 5.1.5)."), attr->name, value->integer);
3613
222
            return (false);
3614
222
    }
3615
2.61k
  }
3616
580
        break;
3617
3618
1.24k
    case IPP_TAG_STRING :
3619
264k
        for (i = attr->num_values, value = attr->values; i > 0; i --, value ++)
3620
263k
  {
3621
263k
    if (value->unknown.length > IPP_MAX_OCTETSTRING)
3622
427
    {
3623
427
      ipp_set_error(IPP_STATUS_ERROR_BAD_REQUEST, _("\"%s\": Bad octetString value - bad length %d (RFC 8011 section 5.1.20)."), attr->name, value->unknown.length);
3624
427
      return (false);
3625
427
    }
3626
263k
  }
3627
819
        break;
3628
3629
2.91k
    case IPP_TAG_DATE :
3630
3.43k
        for (i = attr->num_values, value = attr->values; i > 0; i --, value ++)
3631
3.27k
  {
3632
3.27k
    date = value->date;
3633
3634
3.27k
          if (date[2] < 1 || date[2] > 12)
3635
681
    {
3636
681
      ipp_set_error(IPP_STATUS_ERROR_BAD_REQUEST, _("\"%s\": Bad dateTime month %u (RFC 8011 section 5.1.15)."), attr->name, date[2]);
3637
681
      return (false);
3638
681
    }
3639
3640
2.59k
          if (date[3] < 1 || date[3] > 31)
3641
690
    {
3642
690
      ipp_set_error(IPP_STATUS_ERROR_BAD_REQUEST, _("\"%s\": Bad dateTime day %u (RFC 8011 section 5.1.15)."), attr->name, date[3]);
3643
690
      return (false);
3644
690
    }
3645
3646
1.90k
          if (date[4] > 23)
3647
234
    {
3648
234
      ipp_set_error(IPP_STATUS_ERROR_BAD_REQUEST, _("\"%s\": Bad dateTime hours %u (RFC 8011 section 5.1.15)."), attr->name, date[4]);
3649
234
      return (false);
3650
234
    }
3651
3652
1.67k
          if (date[5] > 59)
3653
216
    {
3654
216
      ipp_set_error(IPP_STATUS_ERROR_BAD_REQUEST, _("\"%s\": Bad dateTime minutes %u (RFC 8011 section 5.1.15)."), attr->name, date[5]);
3655
216
      return (false);
3656
216
    }
3657
3658
1.45k
          if (date[6] > 60)
3659
162
    {
3660
162
      ipp_set_error(IPP_STATUS_ERROR_BAD_REQUEST, _("\"%s\": Bad dateTime seconds %u (RFC 8011 section 5.1.15)."), attr->name, date[6]);
3661
162
      return (false);
3662
162
    }
3663
3664
1.29k
          if (date[7] > 9)
3665
200
    {
3666
200
      ipp_set_error(IPP_STATUS_ERROR_BAD_REQUEST, _("\"%s\": Bad dateTime deciseconds %u (RFC 8011 section 5.1.15)."), attr->name, date[7]);
3667
200
      return (false);
3668
200
    }
3669
3670
1.09k
          if (date[8] != '-' && date[8] != '+')
3671
236
    {
3672
236
      ipp_set_error(IPP_STATUS_ERROR_BAD_REQUEST, _("\"%s\": Bad dateTime UTC sign '%c' (RFC 8011 section 5.1.15)."), attr->name, date[8]);
3673
236
      return (false);
3674
236
    }
3675
3676
857
          if (date[9] > 14)
3677
163
    {
3678
      // Kiribata has a UTC+14 time zone, RFC 2579 calls for UTC+13 support, errata filed...
3679
163
      ipp_set_error(IPP_STATUS_ERROR_BAD_REQUEST, _("\"%s\": Bad dateTime UTC hours %u (RFC 8011 section 5.1.15)."), attr->name, date[9]);
3680
163
      return (false);
3681
163
    }
3682
3683
694
          if (date[10] > 59)
3684
170
    {
3685
170
      ipp_set_error(IPP_STATUS_ERROR_BAD_REQUEST, _("\"%s\": Bad dateTime UTC minutes %u (RFC 8011 section 5.1.15)."), attr->name, date[10]);
3686
170
      return (false);
3687
170
    }
3688
694
  }
3689
158
        break;
3690
3691
2.64k
    case IPP_TAG_RESOLUTION :
3692
92.9k
        for (i = attr->num_values, value = attr->values; i > 0; i --, value ++)
3693
91.6k
  {
3694
91.6k
    if (value->resolution.xres <= 0)
3695
413
    {
3696
413
      ipp_set_error(IPP_STATUS_ERROR_BAD_REQUEST, _("\"%s\": Bad resolution value %dx%d%s - cross feed resolution must be positive (RFC 8011 section 5.1.16)."), attr->name, value->resolution.xres, value->resolution.yres, value->resolution.units == IPP_RES_PER_INCH ? "dpi" : value->resolution.units == IPP_RES_PER_CM ? "dpcm" : "unknown");
3697
413
      return (false);
3698
413
    }
3699
3700
91.2k
    if (value->resolution.yres <= 0)
3701
473
    {
3702
473
      ipp_set_error(IPP_STATUS_ERROR_BAD_REQUEST, _("\"%s\": Bad resolution value %dx%d%s - feed resolution must be positive (RFC 8011 section 5.1.16)."), attr->name, value->resolution.xres, value->resolution.yres, value->resolution.units == IPP_RES_PER_INCH ? "dpi" : value->resolution.units == IPP_RES_PER_CM ? "dpcm" : "unknown");
3703
473
            return (false);
3704
473
    }
3705
3706
90.7k
    if (value->resolution.units != IPP_RES_PER_INCH && value->resolution.units != IPP_RES_PER_CM)
3707
445
    {
3708
445
      ipp_set_error(IPP_STATUS_ERROR_BAD_REQUEST, _("\"%s\": Bad resolution value %dx%d%s - bad units value (RFC 8011 section 5.1.16)."), attr->name, value->resolution.xres, value->resolution.yres, value->resolution.units == IPP_RES_PER_INCH ? "dpi" : value->resolution.units == IPP_RES_PER_CM ? "dpcm" : "unknown");
3709
445
      return (false);
3710
445
    }
3711
90.7k
  }
3712
1.31k
        break;
3713
3714
1.86k
    case IPP_TAG_RANGE :
3715
467k
        for (i = attr->num_values, value = attr->values; i > 0; i --, value ++)
3716
466k
  {
3717
466k
    if (value->range.lower > value->range.upper)
3718
194
    {
3719
194
      ipp_set_error(IPP_STATUS_ERROR_BAD_REQUEST, _("\"%s\": Bad rangeOfInteger value %d-%d - lower greater than upper (RFC 8011 section 5.1.14)."), attr->name, value->range.lower, value->range.upper);
3720
194
      return (false);
3721
194
    }
3722
466k
  }
3723
1.67k
        break;
3724
3725
2.31k
    case IPP_TAG_BEGIN_COLLECTION :
3726
309k
        for (i = attr->num_values, value = attr->values; i > 0; i --, value ++)
3727
307k
  {
3728
307k
    for (colattr = value->collection->attrs;
3729
309k
         colattr;
3730
307k
         colattr = colattr->next)
3731
1.97k
    {
3732
1.97k
      if (!ippValidateAttribute(colattr))
3733
396
        return (false);
3734
1.97k
    }
3735
307k
  }
3736
1.91k
        break;
3737
3738
3.24k
    case IPP_TAG_TEXT :
3739
4.84k
    case IPP_TAG_TEXTLANG :
3740
4.84k
        if (value_tag == IPP_TAG_TEXTLANG)
3741
1.59k
        {
3742
1.59k
          if (!ipp_regex_begin())
3743
0
            return (false);
3744
1.59k
        }
3745
3746
11.5k
        for (i = attr->num_values, value = attr->values; i > 0; i --, value ++)
3747
10.2k
  {
3748
10.2k
    if (!value->string.text)
3749
0
    {
3750
0
      ipp_set_error(IPP_STATUS_ERROR_INTERNAL, _("\"%s\": NULL value."), attr->name);
3751
0
      return (0);
3752
0
    }
3753
3754
10.2k
          if (value_tag == IPP_TAG_TEXTLANG && !value->string.language)
3755
0
          {
3756
0
      ipp_set_error(IPP_STATUS_ERROR_INTERNAL, _("\"%s\": NULL language."), attr->name);
3757
0
      return (0);
3758
0
          }
3759
3760
10.2k
    if (value_tag == IPP_TAG_TEXTLANG && !ipp_is_valid_language(attr->name, value->string.language))
3761
1.28k
    {
3762
1.28k
      ipp_regex_end();
3763
1.28k
      return (false);
3764
1.28k
    }
3765
3766
8.98k
    if (!ipp_is_valid_string(attr->name, value->string.text, /*is_text*/true))
3767
2.23k
    {
3768
2.23k
      if (value_tag == IPP_TAG_TEXTLANG)
3769
178
        ipp_regex_end();
3770
3771
2.23k
      return (false);
3772
2.23k
    }
3773
8.98k
  }
3774
3775
1.32k
  if (value_tag == IPP_TAG_TEXTLANG)
3776
134
    ipp_regex_end();
3777
1.32k
        break;
3778
3779
2.53k
    case IPP_TAG_NAME :
3780
4.28k
    case IPP_TAG_NAMELANG :
3781
4.28k
        if (value_tag == IPP_TAG_NAMELANG)
3782
1.75k
        {
3783
1.75k
          if (!ipp_regex_begin())
3784
0
            return (false);
3785
1.75k
        }
3786
3787
20.6k
        for (i = attr->num_values, value = attr->values; i > 0; i --, value ++)
3788
18.9k
  {
3789
18.9k
    if (!value->string.text)
3790
0
    {
3791
0
      ipp_set_error(IPP_STATUS_ERROR_INTERNAL, _("\"%s\": NULL value."), attr->name);
3792
0
      return (0);
3793
0
    }
3794
3795
18.9k
          if (value_tag == IPP_TAG_NAMELANG && !value->string.language)
3796
0
          {
3797
0
      ipp_set_error(IPP_STATUS_ERROR_INTERNAL, _("\"%s\": NULL language."), attr->name);
3798
0
      return (0);
3799
0
          }
3800
3801
18.9k
    if (value_tag == IPP_TAG_NAMELANG && !ipp_is_valid_language(attr->name, value->string.language))
3802
553
    {
3803
553
      ipp_regex_end();
3804
553
      return (false);
3805
553
    }
3806
3807
18.3k
    if (!ipp_is_valid_string(attr->name, value->string.text, /*is_text*/false))
3808
2.00k
    {
3809
2.00k
      if (value_tag == IPP_TAG_NAMELANG)
3810
625
        ipp_regex_end();
3811
3812
2.00k
      return (false);
3813
2.00k
    }
3814
18.3k
  }
3815
3816
1.72k
  if (value_tag == IPP_TAG_NAMELANG)
3817
576
    ipp_regex_end();
3818
1.72k
        break;
3819
3820
2.18k
    case IPP_TAG_KEYWORD :
3821
2.57k
        for (i = attr->num_values, value = attr->values; i > 0; i --, value ++)
3822
2.39k
  {
3823
2.39k
    if (!value->string.text)
3824
0
    {
3825
0
      ipp_set_error(IPP_STATUS_ERROR_INTERNAL, _("\"%s\": NULL value."), attr->name);
3826
0
      return (0);
3827
0
    }
3828
3829
2.39k
    if (!ipp_is_valid_keyword(attr->name, value->string.text))
3830
2.00k
      return (false);
3831
2.39k
  }
3832
178
        break;
3833
3834
16.1k
    case IPP_TAG_URI :
3835
22.1k
        for (i = attr->num_values, value = attr->values; i > 0; i --, value ++)
3836
16.4k
  {
3837
16.4k
    if (!value->string.text)
3838
0
    {
3839
0
      ipp_set_error(IPP_STATUS_ERROR_INTERNAL, _("\"%s\": NULL value."), attr->name);
3840
0
      return (0);
3841
0
    }
3842
3843
16.4k
    uri_status = httpSeparateURI(HTTP_URI_CODING_ALL, value->string.text, scheme, sizeof(scheme), userpass, sizeof(userpass), hostname, sizeof(hostname), &port, resource, sizeof(resource));
3844
3845
16.4k
    if (uri_status < HTTP_URI_STATUS_OK)
3846
10.4k
    {
3847
10.4k
      ipp_set_error(IPP_STATUS_ERROR_BAD_REQUEST, _("\"%s\": Bad URI value \"%s\" - %s (RFC 8011 section 5.1.6)."), attr->name, value->string.text, httpURIStatusString(uri_status));
3848
10.4k
      return (false);
3849
10.4k
    }
3850
3851
6.04k
    if (strlen(value->string.text) > (IPP_MAX_URI - 1))
3852
99
    {
3853
99
      ipp_set_error(IPP_STATUS_ERROR_BAD_REQUEST, _("\"%s\": Bad URI value \"%s\" - bad length %d (RFC 8011 section 5.1.6)."), attr->name, value->string.text, (int)strlen(value->string.text));
3854
99
      return (false);
3855
99
    }
3856
6.04k
  }
3857
5.65k
        break;
3858
3859
5.65k
    case IPP_TAG_URISCHEME :
3860
3.28k
        for (i = attr->num_values, value = attr->values; i > 0; i --, value ++)
3861
2.75k
  {
3862
2.75k
    if (!value->string.text)
3863
0
    {
3864
0
      ipp_set_error(IPP_STATUS_ERROR_INTERNAL, _("\"%s\": NULL value."), attr->name);
3865
0
      return (0);
3866
0
    }
3867
3868
2.75k
    ptr = value->string.text;
3869
2.75k
    if (_cups_islower(*ptr))
3870
1.57k
    {
3871
15.5k
      for (ptr ++; *ptr; ptr ++)
3872
14.6k
      {
3873
14.6k
        if (!_cups_islower(*ptr) && !_cups_isdigit(*ptr) && *ptr != '+' && *ptr != '-' && *ptr != '.')
3874
684
                break;
3875
14.6k
      }
3876
1.57k
    }
3877
3878
2.75k
    if (*ptr || ptr == value->string.text)
3879
1.86k
    {
3880
1.86k
      ipp_set_error(IPP_STATUS_ERROR_BAD_REQUEST, _("\"%s\": Bad uriScheme value \"%s\" - bad characters (RFC 8011 section 5.1.7)."), attr->name, value->string.text);
3881
1.86k
      return (false);
3882
1.86k
    }
3883
3884
886
    if ((ptr - value->string.text) > (IPP_MAX_URISCHEME - 1))
3885
184
    {
3886
184
      ipp_set_error(IPP_STATUS_ERROR_BAD_REQUEST, _("\"%s\": Bad uriScheme value \"%s\" - bad length %d (RFC 8011 section 5.1.7)."), attr->name, value->string.text, (int)(ptr - value->string.text));
3887
184
      return (false);
3888
184
    }
3889
886
  }
3890
530
        break;
3891
3892
2.56k
    case IPP_TAG_CHARSET :
3893
3.02k
        for (i = attr->num_values, value = attr->values; i > 0; i --, value ++)
3894
2.82k
  {
3895
2.82k
    if (!value->string.text)
3896
0
    {
3897
0
      ipp_set_error(IPP_STATUS_ERROR_INTERNAL, _("\"%s\": NULL value."), attr->name);
3898
0
      return (0);
3899
0
    }
3900
3901
17.2k
    for (ptr = value->string.text; *ptr; ptr ++)
3902
15.6k
    {
3903
15.6k
      if (!_cups_isprint(*ptr) || _cups_isupper(*ptr) || _cups_isspace(*ptr))
3904
1.26k
        break;
3905
15.6k
    }
3906
3907
2.82k
    if (*ptr || ptr == value->string.text)
3908
2.17k
    {
3909
2.17k
      ipp_set_error(IPP_STATUS_ERROR_BAD_REQUEST, _("\"%s\": Bad charset value \"%s\" - bad characters (RFC 8011 section 5.1.8)."), attr->name, value->string.text);
3910
2.17k
      return (false);
3911
2.17k
    }
3912
3913
644
    if ((ptr - value->string.text) > (IPP_MAX_CHARSET - 1))
3914
185
    {
3915
185
      ipp_set_error(IPP_STATUS_ERROR_BAD_REQUEST, _("\"%s\": Bad charset value \"%s\" - bad length %d (RFC 8011 section 5.1.8)."), attr->name, value->string.text, (int)(ptr - value->string.text));
3916
185
      return (false);
3917
185
    }
3918
644
  }
3919
203
        break;
3920
3921
1.53k
    case IPP_TAG_LANGUAGE :
3922
1.53k
        if (!ipp_regex_begin())
3923
0
          return (false);
3924
3925
1.90k
        for (i = attr->num_values, value = attr->values; i > 0; i --, value ++)
3926
1.76k
  {
3927
1.76k
    if (!value->string.text)
3928
0
    {
3929
0
      ipp_set_error(IPP_STATUS_ERROR_INTERNAL, _("\"%s\": NULL value."), attr->name);
3930
0
      return (0);
3931
0
    }
3932
3933
1.76k
    if (!ipp_is_valid_language(attr->name, value->string.text))
3934
1.40k
    {
3935
1.40k
      ipp_regex_end();
3936
1.40k
      return (false);
3937
1.40k
    }
3938
1.76k
  }
3939
3940
134
  ipp_regex_end();
3941
134
        break;
3942
3943
1.01k
    case IPP_TAG_MIMETYPE :
3944
1.01k
        if (!ipp_regex_begin())
3945
0
          return (false);
3946
3947
1.28k
        for (i = attr->num_values, value = attr->values; i > 0; i --, value ++)
3948
1.14k
  {
3949
1.14k
    if (!value->string.text)
3950
0
    {
3951
0
      ipp_set_error(IPP_STATUS_ERROR_INTERNAL, _("\"%s\": NULL value."), attr->name);
3952
0
      return (0);
3953
0
    }
3954
3955
1.14k
    if (!ipp_is_valid_mimetype(attr->name, value->string.text))
3956
872
    {
3957
872
      ipp_regex_end();
3958
872
      return (false);
3959
872
    }
3960
1.14k
  }
3961
3962
144
  ipp_regex_end();
3963
144
        break;
3964
3965
12.1k
    default :
3966
12.1k
        break;
3967
61.2k
  }
3968
3969
30.6k
  return (true);
3970
61.2k
}
3971
3972
3973
//
3974
// 'ippValidateAttributes()' - Validate all attributes in an IPP message.
3975
//
3976
// This function validates the contents of the IPP message, including each
3977
// attribute.  Like @link ippValidateAttribute@, @link cupsGetErrorString@ is
3978
// set to a human-readable message on failure.
3979
//
3980
3981
bool          // O - `true` if valid, `false` otherwise
3982
ippValidateAttributes(ipp_t *ipp) // I - IPP message
3983
1.77k
{
3984
1.77k
  ipp_attribute_t *attr;    // Current attribute
3985
3986
3987
1.77k
  if (!ipp)
3988
0
    return (true);
3989
3990
18.0k
  for (attr = ipp->attrs; attr; attr = attr->next)
3991
17.4k
  {
3992
17.4k
    if (!ippValidateAttribute(attr))
3993
1.18k
      return (false);
3994
17.4k
  }
3995
3996
585
  return (true);
3997
1.77k
}
3998
3999
4000
//
4001
// 'ippWrite()' - Write data for an IPP message to a HTTP connection.
4002
//
4003
4004
ipp_state_t       // O - Current state
4005
ippWrite(http_t *http,      // I - HTTP connection
4006
         ipp_t  *ipp)     // I - IPP data
4007
0
{
4008
0
  DEBUG_printf("ippWrite(http=%p, ipp=%p)", (void *)http, (void *)ipp);
4009
4010
0
  if (!http)
4011
0
    return (IPP_STATE_ERROR);
4012
4013
0
  return (ippWriteIO(http, (ipp_io_cb_t)httpWrite, http->blocking, NULL, ipp));
4014
0
}
4015
4016
4017
//
4018
// 'ippWriteFile()' - Write data for an IPP message to a file.
4019
//
4020
4021
ipp_state_t       // O - Current state
4022
ippWriteFile(int   fd,      // I - HTTP data
4023
             ipp_t *ipp)    // I - IPP data
4024
0
{
4025
0
  DEBUG_printf("ippWriteFile(fd=%d, ipp=%p)", fd, (void *)ipp);
4026
4027
0
  ipp->state = IPP_STATE_IDLE;
4028
4029
0
  return (ippWriteIO(&fd, (ipp_io_cb_t)ipp_write_file, true, NULL, ipp));
4030
0
}
4031
4032
4033
//
4034
// 'ippWriteIO()' - Write data for an IPP message.
4035
//
4036
4037
ipp_state_t       // O - Current state
4038
ippWriteIO(void        *dst,    // I - Destination
4039
           ipp_io_cb_t cb,    // I - Write callback function
4040
     bool        blocking,  // I - Use blocking IO?
4041
     ipp_t       *parent,   // I - Parent IPP message
4042
           ipp_t       *ipp)    // I - IPP data
4043
118k
{
4044
118k
  size_t    i;    // Looping var
4045
118k
  int     n;    // Length of data
4046
118k
  unsigned char   *buffer,  // Data buffer
4047
118k
      *bufptr;  // Pointer into buffer
4048
118k
  ipp_attribute_t *attr;    // Current attribute
4049
118k
  _ipp_value_t    *value;   // Current value
4050
4051
4052
118k
  DEBUG_printf("ippWriteIO(dst=%p, cb=%p, blocking=%d, parent=%p, ipp=%p)", (void *)dst, (void *)cb, blocking, (void *)parent, (void *)ipp);
4053
4054
118k
  if (!dst || !ipp)
4055
0
    return (IPP_STATE_ERROR);
4056
4057
118k
  if ((buffer = (unsigned char *)_cupsBufferGet(IPP_BUF_SIZE)) == NULL)
4058
0
  {
4059
0
    DEBUG_puts("1ippWriteIO: Unable to get write buffer");
4060
0
    return (IPP_STATE_ERROR);
4061
0
  }
4062
4063
118k
  switch (ipp->state)
4064
118k
  {
4065
118k
    case IPP_STATE_IDLE :
4066
118k
        ipp->state ++; // Avoid common problem...
4067
4068
118k
    case IPP_STATE_HEADER :
4069
118k
        if (parent == NULL)
4070
6.54k
  {
4071
    // Send the request header:
4072
    //
4073
    //                 Version = 2 bytes
4074
    //   Operation/Status Code = 2 bytes
4075
    //              Request ID = 4 bytes
4076
    //                   Total = 8 bytes
4077
6.54k
          bufptr = buffer;
4078
4079
6.54k
    *bufptr++ = ipp->request.version[0];
4080
6.54k
    *bufptr++ = ipp->request.version[1];
4081
6.54k
    *bufptr++ = (ipp_uchar_t)(ipp->request.op_status >> 8);
4082
6.54k
    *bufptr++ = (ipp_uchar_t)ipp->request.op_status;
4083
6.54k
    *bufptr++ = (ipp_uchar_t)(ipp->request.request_id >> 24);
4084
6.54k
    *bufptr++ = (ipp_uchar_t)(ipp->request.request_id >> 16);
4085
6.54k
    *bufptr++ = (ipp_uchar_t)(ipp->request.request_id >> 8);
4086
6.54k
    *bufptr++ = (ipp_uchar_t)ipp->request.request_id;
4087
4088
6.54k
    DEBUG_printf("2ippWriteIO: version=%d.%d", buffer[0], buffer[1]);
4089
6.54k
    DEBUG_printf("2ippWriteIO: op_status=%04x", ipp->request.op_status);
4090
6.54k
    DEBUG_printf("2ippWriteIO: request_id=%d", ipp->request.request_id);
4091
4092
6.54k
          if ((*cb)(dst, buffer, (size_t)(bufptr - buffer)) < 0)
4093
0
    {
4094
0
      DEBUG_puts("1ippWriteIO: Could not write IPP header...");
4095
0
      _cupsBufferRelease((char *)buffer);
4096
0
      return (IPP_STATE_ERROR);
4097
0
    }
4098
6.54k
  }
4099
4100
  // Reset the state engine to point to the first attribute
4101
  // in the request/response, with no current group.
4102
118k
        ipp->state   = IPP_STATE_ATTRIBUTE;
4103
118k
  ipp->current = ipp->attrs;
4104
118k
  ipp->curtag  = IPP_TAG_ZERO;
4105
4106
118k
  DEBUG_printf("1ippWriteIO: ipp->current=%p", (void *)ipp->current);
4107
4108
        // If blocking is disabled, stop here...
4109
118k
        if (!blocking)
4110
0
    break;
4111
4112
118k
    case IPP_STATE_ATTRIBUTE :
4113
1.33M
        while (ipp->current != NULL)
4114
1.21M
  {
4115
    // Write this attribute...
4116
1.21M
    bufptr = buffer;
4117
1.21M
    attr   = ipp->current;
4118
4119
1.21M
    ipp->current = ipp->current->next;
4120
4121
1.21M
          if (!parent)
4122
1.21M
    {
4123
1.21M
      if (ipp->curtag != attr->group_tag)
4124
391k
      {
4125
        // Send a group tag byte...
4126
391k
        ipp->curtag = attr->group_tag;
4127
4128
391k
        if (attr->group_tag == IPP_TAG_ZERO)
4129
193k
    continue;
4130
4131
198k
        DEBUG_printf("2ippWriteIO: wrote group tag=%x(%s)", attr->group_tag, ippTagString(attr->group_tag));
4132
198k
        *bufptr++ = (ipp_uchar_t)attr->group_tag;
4133
198k
      }
4134
826k
      else if (attr->group_tag == IPP_TAG_ZERO)
4135
788k
      {
4136
788k
        continue;
4137
788k
      }
4138
1.21M
    }
4139
4140
236k
    DEBUG_printf("1ippWriteIO: %s (%s%s)", attr->name, attr->num_values > 1 ? "1setOf " : "", ippTagString(attr->value_tag));
4141
4142
    // Write the attribute tag and name.
4143
    //
4144
    // The attribute name length does not include the trailing nul
4145
    // character in the source string.
4146
    //
4147
    // Collection values (parent != NULL) are written differently...
4148
236k
          if (parent == NULL)
4149
235k
    {
4150
      // Get the length of the attribute name, and make sure it won't overflow the buffer...
4151
235k
            if ((n = (int)strlen(attr->name)) > (IPP_BUF_SIZE - 8))
4152
0
      {
4153
0
        DEBUG_printf("1ippWriteIO: Attribute name too long (%d)", n);
4154
0
        _cupsBufferRelease((char *)buffer);
4155
0
        return (IPP_STATE_ERROR);
4156
0
      }
4157
4158
      // Write the value tag, name length, and name string...
4159
235k
      DEBUG_printf("2ippWriteIO: writing value tag=%x(%s)", attr->value_tag, ippTagString(attr->value_tag));
4160
235k
            DEBUG_printf("2ippWriteIO: writing name=%d,\"%s\"", n, attr->name);
4161
4162
235k
      *bufptr++ = (ipp_uchar_t)attr->value_tag;
4163
235k
      *bufptr++ = (ipp_uchar_t)(n >> 8);
4164
235k
      *bufptr++ = (ipp_uchar_t)n;
4165
235k
      memcpy(bufptr, attr->name, (size_t)n);
4166
235k
      bufptr += n;
4167
235k
          }
4168
1.40k
    else
4169
1.40k
    {
4170
      // Get the length of the attribute name, and make sure it won't overflow the buffer...
4171
1.40k
            if ((n = (int)strlen(attr->name)) > (IPP_BUF_SIZE - 12))
4172
0
      {
4173
0
        DEBUG_printf("1ippWriteIO: Attribute name too long (%d)", n);
4174
0
        _cupsBufferRelease((char *)buffer);
4175
0
        return (IPP_STATE_ERROR);
4176
0
      }
4177
4178
      // Write the member name tag, name length, name string, value tag,
4179
      // and empty name for the collection member attribute...
4180
1.40k
            DEBUG_printf("2ippWriteIO: writing value tag=%x(memberName)", IPP_TAG_MEMBERNAME);
4181
1.40k
            DEBUG_printf("2ippWriteIO: writing name=%d,\"%s\"", n, attr->name);
4182
1.40k
            DEBUG_printf("2ippWriteIO: writing value tag=%x(%s)", attr->value_tag, ippTagString(attr->value_tag));
4183
1.40k
            DEBUG_puts("2ippWriteIO: writing name=0,\"\"");
4184
4185
1.40k
            *bufptr++ = IPP_TAG_MEMBERNAME;
4186
1.40k
      *bufptr++ = 0;
4187
1.40k
      *bufptr++ = 0;
4188
1.40k
      *bufptr++ = (ipp_uchar_t)(n >> 8);
4189
1.40k
      *bufptr++ = (ipp_uchar_t)n;
4190
1.40k
      memcpy(bufptr, attr->name, (size_t)n);
4191
1.40k
      bufptr += n;
4192
4193
1.40k
      *bufptr++ = (ipp_uchar_t)attr->value_tag;
4194
1.40k
            *bufptr++ = 0;
4195
1.40k
            *bufptr++ = 0;
4196
1.40k
    }
4197
4198
    // Now write the attribute value(s)...
4199
236k
    switch (attr->value_tag & ~IPP_TAG_CUPS_CONST)
4200
236k
    {
4201
560
      case IPP_TAG_UNSUPPORTED_VALUE :
4202
1.74k
      case IPP_TAG_DEFAULT :
4203
2.55k
      case IPP_TAG_UNKNOWN :
4204
4.88k
      case IPP_TAG_NOVALUE :
4205
5.37k
      case IPP_TAG_NOTSETTABLE :
4206
6.73k
      case IPP_TAG_DELETEATTR :
4207
7.69k
      case IPP_TAG_ADMINDEFINE :
4208
7.69k
    *bufptr++ = 0;
4209
7.69k
    *bufptr++ = 0;
4210
7.69k
          break;
4211
4212
534
      case IPP_TAG_INTEGER :
4213
1.10k
      case IPP_TAG_ENUM :
4214
226k
          for (i = 0, value = attr->values; i < attr->num_values; i ++, value ++)
4215
225k
    {
4216
225k
                  if ((IPP_BUF_SIZE - (bufptr - buffer)) < 9)
4217
41
      {
4218
41
                    if ((*cb)(dst, buffer, (size_t)(bufptr - buffer)) < 0)
4219
1
              {
4220
1
          DEBUG_puts("1ippWriteIO: Could not write IPP attribute...");
4221
1
          _cupsBufferRelease((char *)buffer);
4222
1
                return (IPP_STATE_ERROR);
4223
1
              }
4224
4225
40
        bufptr = buffer;
4226
40
      }
4227
4228
225k
      if (i)
4229
224k
      {
4230
        // Arrays and sets are done by sending additional values with a zero-length name...
4231
224k
                    *bufptr++ = (ipp_uchar_t)attr->value_tag;
4232
224k
        *bufptr++ = 0;
4233
224k
        *bufptr++ = 0;
4234
224k
      }
4235
4236
            // Integers and enumerations are both 4-byte signed
4237
      // (twos-complement) values.
4238
      //
4239
      // Put the 2-byte length and 4-byte value into the buffer...
4240
225k
            *bufptr++ = 0;
4241
225k
      *bufptr++ = 4;
4242
225k
      *bufptr++ = (ipp_uchar_t)(value->integer >> 24);
4243
225k
      *bufptr++ = (ipp_uchar_t)(value->integer >> 16);
4244
225k
      *bufptr++ = (ipp_uchar_t)(value->integer >> 8);
4245
225k
      *bufptr++ = (ipp_uchar_t)value->integer;
4246
225k
    }
4247
1.10k
    break;
4248
4249
1.10k
      case IPP_TAG_BOOLEAN :
4250
349k
          for (i = 0, value = attr->values; i < attr->num_values; i ++, value ++)
4251
348k
    {
4252
348k
                  if ((IPP_BUF_SIZE - (bufptr - buffer)) < 6)
4253
49
      {
4254
49
                    if ((*cb)(dst, buffer, (size_t)(bufptr - buffer)) < 0)
4255
0
              {
4256
0
          DEBUG_puts("1ippWriteIO: Could not write IPP attribute...");
4257
0
          _cupsBufferRelease((char *)buffer);
4258
0
                return (IPP_STATE_ERROR);
4259
0
              }
4260
4261
49
        bufptr = buffer;
4262
49
      }
4263
4264
348k
      if (i)
4265
348k
      {
4266
        // Arrays and sets are done by sending additional values with a zero-length name...
4267
348k
                    *bufptr++ = (ipp_uchar_t)attr->value_tag;
4268
348k
        *bufptr++ = 0;
4269
348k
        *bufptr++ = 0;
4270
348k
      }
4271
4272
      // Boolean values are 1-byte; 0 = false, 1 = true.
4273
      //
4274
      // Put the 2-byte length and 1-byte value into the buffer...
4275
348k
            *bufptr++ = 0;
4276
348k
      *bufptr++ = 1;
4277
348k
      *bufptr++ = (ipp_uchar_t)value->boolean;
4278
348k
    }
4279
608
    break;
4280
4281
2.11k
      case IPP_TAG_TEXT :
4282
5.77k
      case IPP_TAG_NAME :
4283
7.42k
      case IPP_TAG_KEYWORD :
4284
189k
      case IPP_TAG_URI :
4285
192k
      case IPP_TAG_URISCHEME :
4286
197k
      case IPP_TAG_CHARSET :
4287
201k
      case IPP_TAG_LANGUAGE :
4288
202k
      case IPP_TAG_MIMETYPE :
4289
1.19M
          for (i = 0, value = attr->values; i < attr->num_values; i ++, value ++)
4290
994k
    {
4291
994k
      if (i)
4292
791k
      {
4293
        // Arrays and sets are done by sending additional values with a zero-length name...
4294
791k
              DEBUG_printf("2ippWriteIO: writing value tag=%x(%s)", attr->value_tag, ippTagString(attr->value_tag));
4295
791k
              DEBUG_printf("2ippWriteIO: writing name=0,\"\"");
4296
4297
791k
                    if ((IPP_BUF_SIZE - (bufptr - buffer)) < 3)
4298
105
        {
4299
105
                      if ((*cb)(dst, buffer, (size_t)(bufptr - buffer)) < 0)
4300
1
                {
4301
1
      DEBUG_puts("1ippWriteIO: Could not write IPP attribute...");
4302
1
      _cupsBufferRelease((char *)buffer);
4303
1
            return (IPP_STATE_ERROR);
4304
1
                }
4305
4306
104
          bufptr = buffer;
4307
104
        }
4308
4309
791k
                    *bufptr++ = (ipp_uchar_t)attr->value_tag;
4310
791k
        *bufptr++ = 0;
4311
791k
        *bufptr++ = 0;
4312
791k
      }
4313
4314
994k
                  if (value->string.text != NULL)
4315
994k
                    n = (int)strlen(value->string.text);
4316
0
      else
4317
0
        n = 0;
4318
4319
994k
                  if (n > (IPP_BUF_SIZE - 2))
4320
0
      {
4321
0
        DEBUG_printf("1ippWriteIO: String too long (%d)", n);
4322
0
        _cupsBufferRelease((char *)buffer);
4323
0
        return (IPP_STATE_ERROR);
4324
0
      }
4325
4326
994k
                  DEBUG_printf("2ippWriteIO: writing string=%d,\"%s\"", n, value->string.text);
4327
4328
994k
                  if ((int)(IPP_BUF_SIZE - (bufptr - buffer)) < (n + 2))
4329
115
      {
4330
115
                    if ((*cb)(dst, buffer, (size_t)(bufptr - buffer)) < 0)
4331
1
              {
4332
1
          DEBUG_puts("1ippWriteIO: Could not write IPP attribute...");
4333
1
          _cupsBufferRelease((char *)buffer);
4334
1
                return (IPP_STATE_ERROR);
4335
1
              }
4336
4337
114
        bufptr = buffer;
4338
114
      }
4339
4340
      // All simple strings consist of the 2-byte length and
4341
      // character data without the trailing nul normally found
4342
      // in C strings.  Also, strings cannot be longer than IPP_MAX_LENGTH
4343
      // bytes since the 2-byte length is a signed (twos-complement)
4344
      // value.
4345
      //
4346
      // Put the 2-byte length and string characters in the buffer.
4347
994k
            *bufptr++ = (ipp_uchar_t)(n >> 8);
4348
994k
      *bufptr++ = (ipp_uchar_t)n;
4349
4350
994k
      if (n > 0)
4351
813k
      {
4352
813k
        memcpy(bufptr, value->string.text, (size_t)n);
4353
813k
        bufptr += n;
4354
813k
      }
4355
994k
    }
4356
202k
    break;
4357
4358
202k
      case IPP_TAG_DATE :
4359
322k
          for (i = 0, value = attr->values; i < attr->num_values; i ++, value ++)
4360
320k
    {
4361
320k
                  if ((IPP_BUF_SIZE - (bufptr - buffer)) < 16)
4362
142
      {
4363
142
                    if ((*cb)(dst, buffer, (size_t)(bufptr - buffer)) < 0)
4364
0
              {
4365
0
          DEBUG_puts("1ippWriteIO: Could not write IPP attribute...");
4366
0
          _cupsBufferRelease((char *)buffer);
4367
0
                return (IPP_STATE_ERROR);
4368
0
              }
4369
4370
142
        bufptr = buffer;
4371
142
      }
4372
4373
320k
      if (i)
4374
318k
      {
4375
        // Arrays and sets are done by sending additional values with a zero-length name...
4376
318k
                    *bufptr++ = (ipp_uchar_t)attr->value_tag;
4377
318k
        *bufptr++ = 0;
4378
318k
        *bufptr++ = 0;
4379
318k
      }
4380
4381
      // Date values consist of a 2-byte length and an
4382
      // 11-byte date/time structure defined by RFC 1903.
4383
      //
4384
      // Put the 2-byte length and 11-byte date/time
4385
      // structure in the buffer.
4386
320k
            *bufptr++ = 0;
4387
320k
      *bufptr++ = 11;
4388
320k
      memcpy(bufptr, value->date, 11);
4389
320k
      bufptr += 11;
4390
320k
    }
4391
1.78k
    break;
4392
4393
1.96k
      case IPP_TAG_RESOLUTION :
4394
538k
          for (i = 0, value = attr->values; i < attr->num_values; i ++, value ++)
4395
536k
    {
4396
536k
                  if ((IPP_BUF_SIZE - (bufptr - buffer)) < 14)
4397
204
      {
4398
204
                    if ((*cb)(dst, buffer, (size_t)(bufptr - buffer)) < 0)
4399
0
              {
4400
0
          DEBUG_puts("1ippWriteIO: Could not write IPP attribute...");
4401
0
          _cupsBufferRelease((char *)buffer);
4402
0
          return (IPP_STATE_ERROR);
4403
0
              }
4404
4405
204
        bufptr = buffer;
4406
204
      }
4407
4408
536k
      if (i)
4409
534k
      {
4410
        // Arrays and sets are done by sending additional values with a zero-length name...
4411
534k
                    *bufptr++ = (ipp_uchar_t)attr->value_tag;
4412
534k
        *bufptr++ = 0;
4413
534k
        *bufptr++ = 0;
4414
534k
      }
4415
4416
      // Resolution values consist of a 2-byte length,
4417
      // 4-byte horizontal resolution value, 4-byte vertical
4418
      // resolution value, and a 1-byte units value.
4419
      //
4420
      // Put the 2-byte length and resolution value data
4421
      // into the buffer.
4422
536k
            *bufptr++ = 0;
4423
536k
      *bufptr++ = 9;
4424
536k
      *bufptr++ = (ipp_uchar_t)(value->resolution.xres >> 24);
4425
536k
      *bufptr++ = (ipp_uchar_t)(value->resolution.xres >> 16);
4426
536k
      *bufptr++ = (ipp_uchar_t)(value->resolution.xres >> 8);
4427
536k
      *bufptr++ = (ipp_uchar_t)value->resolution.xres;
4428
536k
      *bufptr++ = (ipp_uchar_t)(value->resolution.yres >> 24);
4429
536k
      *bufptr++ = (ipp_uchar_t)(value->resolution.yres >> 16);
4430
536k
      *bufptr++ = (ipp_uchar_t)(value->resolution.yres >> 8);
4431
536k
      *bufptr++ = (ipp_uchar_t)value->resolution.yres;
4432
536k
      *bufptr++ = (ipp_uchar_t)value->resolution.units;
4433
536k
    }
4434
1.96k
    break;
4435
4436
1.96k
      case IPP_TAG_RANGE :
4437
556k
          for (i = 0, value = attr->values; i < attr->num_values; i ++, value ++)
4438
555k
    {
4439
555k
                  if ((IPP_BUF_SIZE - (bufptr - buffer)) < 13)
4440
191
      {
4441
191
                    if ((*cb)(dst, buffer, (size_t)(bufptr - buffer)) < 0)
4442
1
              {
4443
1
          DEBUG_puts("1ippWriteIO: Could not write IPP attribute...");
4444
1
          _cupsBufferRelease((char *)buffer);
4445
1
                return (IPP_STATE_ERROR);
4446
1
              }
4447
4448
190
        bufptr = buffer;
4449
190
      }
4450
4451
555k
      if (i)
4452
554k
      {
4453
        // Arrays and sets are done by sending additional values with a zero-length name...
4454
554k
                    *bufptr++ = (ipp_uchar_t)attr->value_tag;
4455
554k
        *bufptr++ = 0;
4456
554k
        *bufptr++ = 0;
4457
554k
      }
4458
4459
      // Range values consist of a 2-byte length,
4460
      // 4-byte lower value, and 4-byte upper value.
4461
      //
4462
      // Put the 2-byte length and range value data
4463
      // into the buffer.
4464
555k
            *bufptr++ = 0;
4465
555k
      *bufptr++ = 8;
4466
555k
      *bufptr++ = (ipp_uchar_t)(value->range.lower >> 24);
4467
555k
      *bufptr++ = (ipp_uchar_t)(value->range.lower >> 16);
4468
555k
      *bufptr++ = (ipp_uchar_t)(value->range.lower >> 8);
4469
555k
      *bufptr++ = (ipp_uchar_t)value->range.lower;
4470
555k
      *bufptr++ = (ipp_uchar_t)(value->range.upper >> 24);
4471
555k
      *bufptr++ = (ipp_uchar_t)(value->range.upper >> 16);
4472
555k
      *bufptr++ = (ipp_uchar_t)(value->range.upper >> 8);
4473
555k
      *bufptr++ = (ipp_uchar_t)value->range.upper;
4474
555k
    }
4475
1.32k
    break;
4476
4477
1.32k
      case IPP_TAG_TEXTLANG :
4478
4.53k
      case IPP_TAG_NAMELANG :
4479
1.08M
          for (i = 0, value = attr->values; i < attr->num_values; i ++, value ++)
4480
1.08M
    {
4481
1.08M
      if (i)
4482
1.07M
      {
4483
        // Arrays and sets are done by sending additional values with a zero-length name...
4484
1.07M
                    if ((IPP_BUF_SIZE - (bufptr - buffer)) < 3)
4485
169
        {
4486
169
                      if ((*cb)(dst, buffer, (size_t)(bufptr - buffer)) < 0)
4487
4
                {
4488
4
      DEBUG_puts("1ippWriteIO: Could not write IPP attribute...");
4489
4
      _cupsBufferRelease((char *)buffer);
4490
4
            return (IPP_STATE_ERROR);
4491
4
                }
4492
4493
165
          bufptr = buffer;
4494
165
        }
4495
4496
1.07M
                    *bufptr++ = (ipp_uchar_t)attr->value_tag;
4497
1.07M
        *bufptr++ = 0;
4498
1.07M
        *bufptr++ = 0;
4499
1.07M
      }
4500
4501
      // textWithLanguage and nameWithLanguage values consist
4502
      // of a 2-byte length for both strings and their
4503
      // individual lengths, a 2-byte length for the
4504
      // character string, the character string without the
4505
      // trailing nul, a 2-byte length for the character
4506
      // set string, and the character set string without
4507
      // the trailing nul.
4508
1.08M
                  n = 4;
4509
4510
1.08M
      if (value->string.language != NULL)
4511
1.08M
                    n += (int)strlen(value->string.language);
4512
4513
1.08M
      if (value->string.text != NULL)
4514
1.08M
                    n += (int)strlen(value->string.text);
4515
4516
1.08M
                  if (n > (IPP_BUF_SIZE - 2))
4517
2
      {
4518
2
        DEBUG_printf("1ippWriteIO: text/nameWithLanguage value too long (%d)", n);
4519
2
        _cupsBufferRelease((char *)buffer);
4520
2
        return (IPP_STATE_ERROR);
4521
2
                  }
4522
4523
1.08M
                  if ((int)(IPP_BUF_SIZE - (bufptr - buffer)) < (n + 2))
4524
308
      {
4525
308
                    if ((*cb)(dst, buffer, (size_t)(bufptr - buffer)) < 0)
4526
1
              {
4527
1
          DEBUG_puts("1ippWriteIO: Could not write IPP attribute...");
4528
1
          _cupsBufferRelease((char *)buffer);
4529
1
                return (IPP_STATE_ERROR);
4530
1
              }
4531
4532
307
        bufptr = buffer;
4533
307
      }
4534
4535
      // Length of entire value
4536
1.08M
            *bufptr++ = (ipp_uchar_t)(n >> 8);
4537
1.08M
      *bufptr++ = (ipp_uchar_t)n;
4538
4539
      // Length of language
4540
1.08M
      if (value->string.language != NULL)
4541
1.08M
        n = (int)strlen(value->string.language);
4542
602
      else
4543
602
        n = 0;
4544
4545
1.08M
            *bufptr++ = (ipp_uchar_t)(n >> 8);
4546
1.08M
      *bufptr++ = (ipp_uchar_t)n;
4547
4548
      // Language
4549
1.08M
      if (n > 0)
4550
1.07M
      {
4551
1.07M
        memcpy(bufptr, value->string.language, (size_t)n);
4552
1.07M
        bufptr += n;
4553
1.07M
      }
4554
4555
      // Length of text
4556
1.08M
                  if (value->string.text != NULL)
4557
1.08M
        n = (int)strlen(value->string.text);
4558
602
      else
4559
602
        n = 0;
4560
4561
1.08M
            *bufptr++ = (ipp_uchar_t)(n >> 8);
4562
1.08M
      *bufptr++ = (ipp_uchar_t)n;
4563
4564
      // Text
4565
1.08M
      if (n > 0)
4566
443k
      {
4567
443k
        memcpy(bufptr, value->string.text, (size_t)n);
4568
443k
        bufptr += n;
4569
443k
      }
4570
1.08M
    }
4571
4.52k
    break;
4572
4573
8.42k
            case IPP_TAG_BEGIN_COLLECTION :
4574
120k
          for (i = 0, value = attr->values; i < attr->num_values; i ++, value ++)
4575
112k
    {
4576
      // Collections are written with the begin-collection
4577
      // tag first with a value of 0 length, followed by the
4578
      // attributes in the collection, then the end-collection
4579
      // value...
4580
112k
                  if ((IPP_BUF_SIZE - (bufptr - buffer)) < 5)
4581
0
      {
4582
0
                    if ((*cb)(dst, buffer, (size_t)(bufptr - buffer)) < 0)
4583
0
              {
4584
0
          DEBUG_puts("1ippWriteIO: Could not write IPP attribute...");
4585
0
          _cupsBufferRelease((char *)buffer);
4586
0
                return (IPP_STATE_ERROR);
4587
0
              }
4588
4589
0
        bufptr = buffer;
4590
0
      }
4591
4592
112k
      if (i)
4593
103k
      {
4594
        // Arrays and sets are done by sending additional values with a zero-length name...
4595
103k
                    *bufptr++ = (ipp_uchar_t)attr->value_tag;
4596
103k
        *bufptr++ = 0;
4597
103k
        *bufptr++ = 0;
4598
103k
      }
4599
4600
      // Write a data length of 0 and flush the buffer...
4601
112k
            *bufptr++ = 0;
4602
112k
      *bufptr++ = 0;
4603
4604
112k
                  if ((*cb)(dst, buffer, (size_t)(bufptr - buffer)) < 0)
4605
1
            {
4606
1
        DEBUG_puts("1ippWriteIO: Could not write IPP attribute...");
4607
1
        _cupsBufferRelease((char *)buffer);
4608
1
              return (IPP_STATE_ERROR);
4609
1
            }
4610
4611
112k
      bufptr = buffer;
4612
4613
      // Then write the collection attribute...
4614
112k
                  value->collection->state = IPP_STATE_IDLE;
4615
4616
112k
      if (ippWriteIO(dst, cb, 1, ipp, value->collection) == IPP_STATE_ERROR)
4617
1
      {
4618
1
        DEBUG_puts("1ippWriteIO: Unable to write collection value");
4619
1
        _cupsBufferRelease((char *)buffer);
4620
1
        return (IPP_STATE_ERROR);
4621
1
      }
4622
112k
    }
4623
8.42k
    break;
4624
4625
8.42k
            default :
4626
655k
          for (i = 0, value = attr->values; i < attr->num_values; i ++, value ++)
4627
648k
    {
4628
648k
      if (i)
4629
643k
      {
4630
        // Arrays and sets are done by sending additional values with a zero-length name...
4631
643k
                    if ((IPP_BUF_SIZE - (bufptr - buffer)) < 3)
4632
122
        {
4633
122
                      if ((*cb)(dst, buffer, (size_t)(bufptr - buffer)) < 0)
4634
0
                {
4635
0
      DEBUG_puts("1ippWriteIO: Could not write IPP attribute...");
4636
0
      _cupsBufferRelease((char *)buffer);
4637
0
            return (IPP_STATE_ERROR);
4638
0
                }
4639
4640
122
          bufptr = buffer;
4641
122
        }
4642
4643
643k
                    *bufptr++ = (ipp_uchar_t)attr->value_tag;
4644
643k
        *bufptr++ = 0;
4645
643k
        *bufptr++ = 0;
4646
643k
      }
4647
4648
      // An unknown value might be some new syntax that a vendor has
4649
      // come up with. It consists of a 2-byte length and the bytes
4650
      // in the unknown value buffer.
4651
648k
                  n = value->unknown.length;
4652
4653
648k
                  if (n > (IPP_BUF_SIZE - 2))
4654
2
      {
4655
2
        DEBUG_printf("1ippWriteIO: Data length too long (%d)", n);
4656
2
        _cupsBufferRelease((char *)buffer);
4657
2
        return (IPP_STATE_ERROR);
4658
2
      }
4659
4660
648k
                  if ((int)(IPP_BUF_SIZE - (bufptr - buffer)) < (n + 2))
4661
208
      {
4662
208
                    if ((*cb)(dst, buffer, (size_t)(bufptr - buffer)) < 0)
4663
0
              {
4664
0
          DEBUG_puts("1ippWriteIO: Could not write IPP attribute...");
4665
0
          _cupsBufferRelease((char *)buffer);
4666
0
                return (IPP_STATE_ERROR);
4667
0
              }
4668
4669
208
        bufptr = buffer;
4670
208
      }
4671
4672
      // Length of unknown value
4673
648k
            *bufptr++ = (ipp_uchar_t)(n >> 8);
4674
648k
      *bufptr++ = (ipp_uchar_t)n;
4675
4676
      // Value
4677
648k
      if (n > 0)
4678
479k
      {
4679
479k
        memcpy(bufptr, value->unknown.data, (size_t)n);
4680
479k
        bufptr += n;
4681
479k
      }
4682
648k
    }
4683
6.73k
    break;
4684
236k
    }
4685
4686
    // Write the data out...
4687
236k
    if (bufptr > buffer)
4688
228k
    {
4689
228k
      if ((*cb)(dst, buffer, (size_t)(bufptr - buffer)) < 0)
4690
11
      {
4691
11
        DEBUG_puts("1ippWriteIO: Could not write IPP attribute...");
4692
11
        _cupsBufferRelease((char *)buffer);
4693
11
        return (IPP_STATE_ERROR);
4694
11
      }
4695
4696
228k
      DEBUG_printf("2ippWriteIO: wrote %d bytes", (int)(bufptr - buffer));
4697
228k
    }
4698
4699
          // If blocking is disabled and we aren't at the end of the attribute
4700
          // list, stop here...
4701
236k
          if (!blocking && ipp->current)
4702
0
      break;
4703
236k
  }
4704
4705
118k
  if (ipp->current == NULL)
4706
118k
  {
4707
    // Done with all of the attributes; add the end-of-attributes
4708
    // tag or end-collection attribute...
4709
118k
          if (parent == NULL)
4710
6.52k
    {
4711
6.52k
            buffer[0] = IPP_TAG_END;
4712
6.52k
      n         = 1;
4713
6.52k
    }
4714
112k
    else
4715
112k
    {
4716
112k
            buffer[0] = IPP_TAG_END_COLLECTION;
4717
112k
      buffer[1] = 0; // empty name
4718
112k
      buffer[2] = 0;
4719
112k
      buffer[3] = 0; // empty value
4720
112k
      buffer[4] = 0;
4721
112k
      n         = 5;
4722
112k
    }
4723
4724
118k
    if ((*cb)(dst, buffer, (size_t)n) < 0)
4725
3
    {
4726
3
      DEBUG_puts("1ippWriteIO: Could not write IPP end-tag...");
4727
3
      _cupsBufferRelease((char *)buffer);
4728
3
      return (IPP_STATE_ERROR);
4729
3
    }
4730
4731
118k
    ipp->state = IPP_STATE_DATA;
4732
118k
  }
4733
118k
        break;
4734
4735
118k
    case IPP_STATE_DATA :
4736
0
        break;
4737
4738
0
    default :
4739
0
        break; // anti-compiler-warning-code
4740
118k
  }
4741
4742
118k
  _cupsBufferRelease((char *)buffer);
4743
4744
118k
  return (ipp->state);
4745
118k
}
4746
4747
4748
//
4749
// 'ipp_add_attr()' - Add a new attribute to the message.
4750
//
4751
4752
static ipp_attribute_t *    // O - New attribute
4753
ipp_add_attr(ipp_t      *ipp,   // I - IPP message
4754
             const char *name,    // I - Attribute name or NULL
4755
             ipp_tag_t  group_tag,  // I - Group tag or IPP_TAG_ZERO
4756
             ipp_tag_t  value_tag,  // I - Value tag or IPP_TAG_ZERO
4757
             size_t     num_values) // I - Number of values
4758
1.25M
{
4759
1.25M
  size_t    alloc_values; // Number of values to allocate
4760
1.25M
  ipp_attribute_t *attr;    // New attribute
4761
4762
4763
1.25M
  DEBUG_printf("4ipp_add_attr(ipp=%p, name=\"%s\", group_tag=0x%x, value_tag=0x%x, num_values=%u)", (void *)ipp, name, group_tag, value_tag, (unsigned)num_values);
4764
4765
  // Range check input...
4766
1.25M
  if (!ipp || (name && !ipp_is_valid_keyword(/*name*/NULL, name)))
4767
0
    return (NULL);
4768
4769
  // Allocate memory, rounding the allocation up as needed...
4770
1.25M
  if (num_values <= 1)
4771
1.25M
    alloc_values = 1;
4772
0
  else
4773
0
    alloc_values = (num_values + IPP_MAX_VALUES - 1) & (size_t)~(IPP_MAX_VALUES - 1);
4774
4775
1.25M
  attr = calloc(1, sizeof(ipp_attribute_t) + (size_t)(alloc_values - 1) * sizeof(_ipp_value_t));
4776
4777
1.25M
  if (attr)
4778
1.25M
  {
4779
    // Initialize attribute...
4780
1.25M
    DEBUG_printf("4debug_alloc: %p %s %s%s (%u values)", (void *)attr, name, num_values > 1 ? "1setOf " : "", ippTagString(value_tag), (unsigned)num_values);
4781
4782
1.25M
    if (name)
4783
262k
      attr->name = _cupsStrAlloc(name);
4784
4785
1.25M
    attr->group_tag  = group_tag;
4786
1.25M
    attr->value_tag  = value_tag;
4787
1.25M
    attr->num_values = num_values;
4788
4789
    // Add it to the end of the linked list...
4790
1.25M
    if (ipp->last)
4791
1.24M
      ipp->last->next = attr;
4792
8.81k
    else
4793
8.81k
      ipp->attrs = attr;
4794
4795
1.25M
    ipp->prev = ipp->last;
4796
1.25M
    ipp->last = ipp->current = attr;
4797
1.25M
  }
4798
4799
1.25M
  DEBUG_printf("5ipp_add_attr: Returning %p", (void *)attr);
4800
4801
1.25M
  return (attr);
4802
1.25M
}
4803
4804
4805
//
4806
// 'ipp_free_values()' - Free attribute values.
4807
//
4808
4809
static void
4810
ipp_free_values(ipp_attribute_t *attr,  // I - Attribute to free values from
4811
                size_t          element,// I - First value to free
4812
                size_t          count)  // I - Number of values to free
4813
1.25M
{
4814
1.25M
  size_t  i;      // Looping var
4815
1.25M
  _ipp_value_t  *value;     // Current value
4816
4817
4818
1.25M
  DEBUG_printf("4ipp_free_values(attr=%p, element=%u, count=%u)", (void *)attr, (unsigned)element, (unsigned)count);
4819
4820
1.25M
  if (!(attr->value_tag & IPP_TAG_CUPS_CONST))
4821
1.25M
  {
4822
    // Free values as needed...
4823
1.25M
    switch (attr->value_tag)
4824
1.25M
    {
4825
2.32k
      case IPP_TAG_TEXTLANG :
4826
7.75k
      case IPP_TAG_NAMELANG :
4827
1.84M
    for (i = count, value = attr->values + element; i > 0; i --, value ++)
4828
1.84M
    {
4829
1.84M
      _cupsStrFree(value->string.language);
4830
1.84M
      _cupsStrFree(value->string.text);
4831
4832
1.84M
      value->string.language = NULL;
4833
1.84M
      value->string.text     = NULL;
4834
1.84M
    }
4835
7.75k
    break;
4836
4837
4.10k
      case IPP_TAG_TEXT :
4838
9.38k
      case IPP_TAG_NAME :
4839
10.7k
      case IPP_TAG_RESERVED_STRING :
4840
13.6k
      case IPP_TAG_KEYWORD :
4841
203k
      case IPP_TAG_URI :
4842
207k
      case IPP_TAG_URISCHEME :
4843
211k
      case IPP_TAG_CHARSET :
4844
216k
      case IPP_TAG_LANGUAGE :
4845
218k
      case IPP_TAG_MIMETYPE :
4846
1.65M
    for (i = count, value = attr->values + element; i > 0; i --, value ++)
4847
1.43M
    {
4848
1.43M
      _cupsStrFree(value->string.text);
4849
1.43M
      value->string.text = NULL;
4850
1.43M
    }
4851
218k
    break;
4852
4853
896
      case IPP_TAG_UNSUPPORTED_VALUE :
4854
2.55k
      case IPP_TAG_DEFAULT :
4855
3.81k
      case IPP_TAG_UNKNOWN :
4856
6.78k
      case IPP_TAG_NOVALUE :
4857
7.52k
      case IPP_TAG_NOTSETTABLE :
4858
9.23k
      case IPP_TAG_DELETEATTR :
4859
10.4k
      case IPP_TAG_ADMINDEFINE :
4860
11.2k
      case IPP_TAG_INTEGER :
4861
12.2k
      case IPP_TAG_ENUM :
4862
13.1k
      case IPP_TAG_BOOLEAN :
4863
16.3k
      case IPP_TAG_DATE :
4864
19.6k
      case IPP_TAG_RESOLUTION :
4865
21.7k
      case IPP_TAG_RANGE :
4866
21.7k
    break;
4867
4868
9.67k
      case IPP_TAG_BEGIN_COLLECTION :
4869
195k
    for (i = count, value = attr->values + element; i > 0; i --, value ++)
4870
186k
    {
4871
186k
      ippDelete(value->collection);
4872
186k
      value->collection = NULL;
4873
186k
    }
4874
9.67k
    break;
4875
4876
1.31k
      case IPP_TAG_STRING :
4877
999k
      default :
4878
1.90M
    for (i = count, value = attr->values + element; i > 0; i --, value ++)
4879
910k
    {
4880
910k
      if (value->unknown.data)
4881
527k
      {
4882
527k
        free(value->unknown.data);
4883
527k
        value->unknown.data = NULL;
4884
527k
      }
4885
910k
    }
4886
999k
    break;
4887
1.25M
    }
4888
1.25M
  }
4889
4890
  // If we are not freeing values from the end, move the remaining values up...
4891
1.25M
  if ((element + count) < attr->num_values)
4892
0
    memmove(attr->values + element, attr->values + element + count, (size_t)(attr->num_values - count - element) * sizeof(_ipp_value_t));
4893
4894
1.25M
  attr->num_values -= count;
4895
1.25M
}
4896
4897
4898
//
4899
// 'ipp_get_code()' - Convert a C locale/charset name into an IPP language/charset code.
4900
//
4901
// This typically converts strings of the form "ll_CC", "ll-REGION", and "CHARSET_NUMBER"
4902
// to "ll-cc", "ll-region", and "charset-number", respectively.
4903
//
4904
4905
static char *       // O - Language code string
4906
ipp_get_code(const char *value,   // I - Locale/charset string
4907
             char       *buffer,  // I - String buffer
4908
             size_t     bufsize)  // I - Size of string buffer
4909
12.2k
{
4910
12.2k
  char  *bufptr,      // Pointer into buffer
4911
12.2k
  *bufend;      // End of buffer
4912
4913
4914
  // Convert values to lowercase and change _ to - as needed...
4915
73.4k
  for (bufptr = buffer, bufend = buffer + bufsize - 1; *value && bufptr < bufend; value ++)
4916
61.2k
  {
4917
61.2k
    if (*value == '_')
4918
9.42k
      *bufptr++ = '-';
4919
51.8k
    else
4920
51.8k
      *bufptr++ = (char)_cups_tolower(*value);
4921
61.2k
  }
4922
12.2k
  *bufptr = '\0';
4923
4924
  // Return the converted string...
4925
12.2k
  return (buffer);
4926
12.2k
}
4927
4928
4929
//
4930
// 'ipp_is_valid_keyword()' - Determine whether a keyword string is valid.
4931
//
4932
4933
static bool       // O - `true` if valid, `false` if bad
4934
ipp_is_valid_keyword(
4935
    const char *name,     // I - Attribute name
4936
    const char *keyword)    // I - Keyword string
4937
588k
{
4938
588k
  const char  *ptr;     // Pointer into keyword
4939
4940
4941
1.52M
  for (ptr = keyword; *ptr; ptr ++)
4942
941k
  {
4943
941k
    if (!_cups_isalnum(*ptr) && *ptr != '-' && *ptr != '.' && *ptr != '_')
4944
1.20k
      break;
4945
941k
  }
4946
4947
588k
  if (*ptr || ptr == keyword)
4948
2.11k
  {
4949
2.11k
    if (name)
4950
1.99k
      ipp_set_error(IPP_STATUS_ERROR_BAD_REQUEST, _("\"%s\": Bad keyword value \"%s\" - invalid character (RFC 8011 section 5.1.4)."), name, keyword);
4951
121
    else
4952
121
      ipp_set_error(IPP_STATUS_ERROR_BAD_REQUEST, _("\"%s\": Bad attribute name - invalid character (RFC 8011 section 5.1.4)."), keyword);
4953
4954
2.11k
    return (false);
4955
2.11k
  }
4956
585k
  else if ((ptr - keyword) > 255)
4957
27
  {
4958
27
    if (name)
4959
10
      ipp_set_error(IPP_STATUS_ERROR_BAD_REQUEST, _("\"%s\": Bad keyword value \"%s\" - bad length %d (RFC 8011 section 5.1.4)."), name, keyword, (int)(ptr - keyword));
4960
17
    else
4961
17
      ipp_set_error(IPP_STATUS_ERROR_BAD_REQUEST, _("\"%s\": Bad attribute name - bad length %d (RFC 8011 section 5.1.4)."), keyword, (int)(ptr - keyword));
4962
4963
27
    return (false);
4964
27
  }
4965
4966
585k
  return (true);
4967
588k
}
4968
4969
4970
//
4971
// 'ipp_is_valid_language()' - Determine whether a language string is valid.
4972
//
4973
4974
static bool       // O - `true` if valid, `false` if bad
4975
ipp_is_valid_language(const char *name, // I - Attribute name
4976
                      const char *lang) // I - Language string
4977
7.27k
{
4978
7.27k
  if (regexec(&lang_regex, lang, 0, NULL, 0))
4979
3.05k
  {
4980
3.05k
    ipp_set_error(IPP_STATUS_ERROR_BAD_REQUEST, _("\"%s\": Bad naturalLanguage value \"%s\" - bad characters (RFC 8011 section 5.1.9)."), name, lang);
4981
3.05k
    return (false);
4982
3.05k
  }
4983
4984
4.22k
  if (strlen(lang) > (IPP_MAX_LANGUAGE - 1))
4985
184
  {
4986
184
    ipp_set_error(IPP_STATUS_ERROR_BAD_REQUEST, _("\"%s\": Bad naturalLanguage value \"%s\" - bad length %d (RFC 8011 section 5.1.9)."), name, lang, (int)strlen(lang));
4987
184
    return (false);
4988
184
  }
4989
4990
4.04k
  return (true);
4991
4.22k
}
4992
4993
4994
//
4995
// 'ipp_is_valid_mimetype()' - Determine whether a MIME media type string is valid.
4996
//
4997
4998
static bool       // O - `true` if valid, `false` if bad
4999
ipp_is_valid_mimetype(const char *name, // I - Attribute name
5000
                      const char *type) // I - mimeMediaType string
5001
1.14k
{
5002
1.14k
  if (regexec(&mime_regex, type, 0, NULL, 0))
5003
872
  {
5004
872
    ipp_set_error(IPP_STATUS_ERROR_BAD_REQUEST, _("\"%s\": Bad mimeMediaType value \"%s\" - bad characters (RFC 8011 section 5.1.10)."), name, type);
5005
872
    return (false);
5006
872
  }
5007
5008
272
  if (strlen(type) > (IPP_MAX_MIMETYPE - 1))
5009
0
  {
5010
0
    ipp_set_error(IPP_STATUS_ERROR_BAD_REQUEST, _("\"%s\": Bad mimeMediaType value \"%s\" - bad length %d (RFC 8011 section 5.1.10)."), name, type, (int)strlen(type));
5011
0
    return (false);
5012
0
  }
5013
5014
272
  return (true);
5015
272
}
5016
5017
5018
//
5019
// 'ipp_is_valid_string()' - Determine whether a name or text value is valid.
5020
//
5021
// Name and text values use the Net-Unicode format defined in RFC 5198: UTF-8
5022
// with no overlong sequences, surrogates, or characters above U+10FFFF, no C0
5023
// or C1 control characters, and no leading byte order mark.  Text values may
5024
// also contain HT and CR LF/LF line endings.
5025
//
5026
5027
static bool       // O - `true` if valid, `false` otherwise
5028
ipp_is_valid_string(const char *name, // I - Attribute name
5029
        const char *s,  // I - String value
5030
        bool       is_text) // I - `true` for text, `false` for name
5031
27.3k
{
5032
27.3k
  bool      first = true; // First character?
5033
27.3k
  const unsigned char *ptr;   // Pointer into string
5034
27.3k
  int     ch,   // Current Unicode character
5035
27.3k
      minch;    // Minimum character for sequence length
5036
27.3k
  size_t    len;    // Length of string
5037
5038
5039
  // Loop through the string to validate each character.
5040
261k
  for (ptr = (const unsigned char *)s; *ptr; ptr ++, first = false)
5041
238k
  {
5042
    // Decode the next UTF-8 sequence...
5043
238k
    if (*ptr < 0x80)
5044
234k
    {
5045
234k
      ch    = *ptr;
5046
234k
      minch = 0;
5047
234k
    }
5048
3.75k
    else if ((ptr[0] & 0xe0) == 0xc0 && (ptr[1] & 0xc0) == 0x80)
5049
528
    {
5050
528
      ch    = ((ptr[0] & 0x1f) << 6) | (ptr[1] & 0x3f);
5051
528
      minch = 0x80;
5052
528
      ptr ++;
5053
528
    }
5054
3.22k
    else if ((ptr[0] & 0xf0) == 0xe0 && (ptr[1] & 0xc0) == 0x80 && (ptr[2] & 0xc0) == 0x80)
5055
1.12k
    {
5056
1.12k
      ch    = ((ptr[0] & 0x0f) << 12) | ((ptr[1] & 0x3f) << 6) | (ptr[2] & 0x3f);
5057
1.12k
      minch = 0x800;
5058
1.12k
      ptr   += 2;
5059
1.12k
    }
5060
2.10k
    else if ((ptr[0] & 0xf8) == 0xf0 && (ptr[1] & 0xc0) == 0x80 && (ptr[2] & 0xc0) == 0x80 && (ptr[3] & 0xc0) == 0x80)
5061
444
    {
5062
444
      ch    = ((ptr[0] & 0x07) << 18) | ((ptr[1] & 0x3f) << 12) | ((ptr[2] & 0x3f) << 6) | (ptr[3] & 0x3f);
5063
444
      minch = 0x10000;
5064
444
      ptr   += 3;
5065
444
    }
5066
1.65k
    else if (is_text)
5067
978
    {
5068
978
      ipp_set_error(IPP_STATUS_ERROR_BAD_REQUEST, _("\"%s\": Bad text value \"%s\" - bad UTF-8 sequence (RFC 8011 section 5.1.2)."), name, s);
5069
978
      return (false);
5070
978
    }
5071
679
    else
5072
679
    {
5073
679
      ipp_set_error(IPP_STATUS_ERROR_BAD_REQUEST, _("\"%s\": Bad name value \"%s\" - bad UTF-8 sequence (RFC 8011 section 5.1.3)."), name, s);
5074
679
      return (false);
5075
679
    }
5076
5077
    // Then check the character value...
5078
236k
    if (ch < minch)
5079
136
    {
5080
136
      ipp_set_error(IPP_STATUS_ERROR_BAD_REQUEST, _("\"%s\": Bad %sd value \"%s\" - overlong UTF-8 sequence (RFC 3629 section 3)."), name, is_text ? "text" : "name", s);
5081
136
      return (false);
5082
136
    }
5083
236k
    else if (ch >= 0xd800 && ch <= 0xdfff)
5084
128
    {
5085
128
      ipp_set_error(IPP_STATUS_ERROR_BAD_REQUEST, _("\"%s\": Bad %s value \"%s\" - UTF-16 surrogate U+%04X (RFC 3629 section 3)."), name, is_text ? "text" : "name", s, ch);
5086
128
      return (false);
5087
128
    }
5088
236k
    else if (ch > 0x10ffff)
5089
176
    {
5090
176
      ipp_set_error(IPP_STATUS_ERROR_BAD_REQUEST, _("\"%s\": Bad %s value \"%s\" - character U+%04X out of range (RFC 3629 section 3)."), name, is_text ? "text" : "name", s, ch);
5091
176
      return (false);
5092
176
    }
5093
236k
    else if (ch == 0xfeff && first)
5094
161
    {
5095
161
      ipp_set_error(IPP_STATUS_ERROR_BAD_REQUEST, _("\"%s\": Bad %s value \"%s\" - leading byte order mark (RFC 5198)."), name, is_text ? "text" : "name", s);
5096
161
      return (false);
5097
161
    }
5098
236k
    else if (is_text && ch == '\r' && ptr[1] != '\n')
5099
202
    {
5100
202
      ipp_set_error(IPP_STATUS_ERROR_BAD_REQUEST, _("\"%s\": Bad text value \"%s\" - CR without LF (RFC 5198 section 2)."), name, s);
5101
202
      return (false);
5102
202
    }
5103
236k
    else if ((ch < ' ' && (!is_text || (ch != '\t' && ch != '\n' && ch != '\r'))) || (ch >= 0x7f && ch <= 0x9f))
5104
1.66k
    {
5105
1.66k
      if (is_text)
5106
634
  ipp_set_error(IPP_STATUS_ERROR_BAD_REQUEST, _("\"%s\": Bad text value \"%s\" - bad control character (PWG 5100.14 section 8.3)."), name, s);
5107
1.02k
      else
5108
1.02k
  ipp_set_error(IPP_STATUS_ERROR_BAD_REQUEST, _("\"%s\": Bad name value \"%s\" - bad control character (PWG 5100.14 section 8.1)."), name, s);
5109
1.66k
      return (false);
5110
1.66k
    }
5111
236k
  }
5112
5113
  // Check the length...
5114
23.2k
  len = (size_t)(ptr - (const unsigned char *)s);
5115
5116
23.2k
  if (is_text && len > (IPP_MAX_TEXT - 1))
5117
76
  {
5118
76
    ipp_set_error(IPP_STATUS_ERROR_BAD_REQUEST, _("\"%s\": Bad text value \"%s\" - bad length %d (RFC 8011 section 5.1.2)."), name, s, (int)len);
5119
76
    return (false);
5120
76
  }
5121
23.1k
  else if (!is_text && len > (IPP_MAX_NAME - 1))
5122
38
  {
5123
38
    ipp_set_error(IPP_STATUS_ERROR_BAD_REQUEST, _("\"%s\": Bad name value \"%s\" - bad length %d (RFC 8011 section 5.1.3)."), name, s, (int)len);
5124
38
    return (false);
5125
38
  }
5126
5127
23.1k
  return (true);
5128
23.2k
}
5129
5130
5131
//
5132
// 'ipp_lang_code()' - Convert a C locale name into an IPP language code.
5133
//
5134
// This typically converts strings of the form "ll_CC" and "ll-REGION" to "ll-cc" and
5135
// "ll-region", respectively.  It also converts the "C" (POSIX) locale to "en".
5136
//
5137
5138
static char *       // O - Language code string
5139
ipp_lang_code(const char *locale, // I - Locale string
5140
              char       *buffer, // I - String buffer
5141
              size_t     bufsize) // I - Size of string buffer
5142
9.42k
{
5143
  // Map POSIX ("C") locale to generic English, otherwise convert the locale string as-is.
5144
9.42k
  if (!_cups_strcasecmp(locale, "c"))
5145
0
  {
5146
0
    cupsCopyString(buffer, "en", bufsize);
5147
0
    return (buffer);
5148
0
  }
5149
9.42k
  else
5150
9.42k
  {
5151
9.42k
    return (ipp_get_code(locale, buffer, bufsize));
5152
9.42k
  }
5153
9.42k
}
5154
5155
5156
//
5157
// 'ipp_length()' - Compute the length of an IPP message or collection value.
5158
//
5159
5160
static size_t       // O - Size of IPP message
5161
ipp_length(ipp_t *ipp,      // I - IPP message or collection
5162
           int   collection)    // I - 1 if a collection, 0 otherwise
5163
0
{
5164
0
  size_t    i;    // Looping var
5165
0
  size_t    bytes;    // Number of bytes
5166
0
  ipp_attribute_t *attr;    // Current attribute
5167
0
  ipp_tag_t   group;    // Current group
5168
0
  _ipp_value_t    *value;   // Current value
5169
5170
5171
0
  DEBUG_printf("3ipp_length(ipp=%p, collection=%d)", (void *)ipp, collection);
5172
5173
0
  if (!ipp)
5174
0
  {
5175
0
    DEBUG_puts("4ipp_length: Returning 0 bytes");
5176
0
    return (0);
5177
0
  }
5178
5179
  // Start with 8 bytes for the IPP message header...
5180
0
  bytes = collection ? 0 : 8;
5181
5182
  // Then add the lengths of each attribute...
5183
0
  group = IPP_TAG_ZERO;
5184
5185
0
  for (attr = ipp->attrs; attr != NULL; attr = attr->next)
5186
0
  {
5187
0
    if (attr->group_tag != group && !collection)
5188
0
    {
5189
0
      group = attr->group_tag;
5190
0
      if (group == IPP_TAG_ZERO)
5191
0
  continue;
5192
5193
0
      bytes ++; // Group tag
5194
0
    }
5195
5196
0
    if (!attr->name)
5197
0
      continue;
5198
5199
0
    DEBUG_printf("5ipp_length: attr->name=\"%s\", attr->num_values=%u, bytes=" CUPS_LLFMT, attr->name, (unsigned)attr->num_values, CUPS_LLCAST bytes);
5200
5201
0
    bytes += (size_t)attr->num_values;  // Value tag for each value
5202
0
    bytes += (size_t)(2 * attr->num_values);
5203
          // Name lengths
5204
0
    bytes += strlen(attr->name);  // Name
5205
0
    bytes += (size_t)(2 * attr->num_values);
5206
          // Value lengths
5207
5208
0
    if (collection)
5209
0
      bytes += 5;     // Add membername overhead
5210
5211
0
    switch (attr->value_tag & ~IPP_TAG_CUPS_CONST)
5212
0
    {
5213
0
      case IPP_TAG_UNSUPPORTED_VALUE :
5214
0
      case IPP_TAG_DEFAULT :
5215
0
      case IPP_TAG_UNKNOWN :
5216
0
      case IPP_TAG_NOVALUE :
5217
0
      case IPP_TAG_NOTSETTABLE :
5218
0
      case IPP_TAG_DELETEATTR :
5219
0
      case IPP_TAG_ADMINDEFINE :
5220
0
          break;
5221
5222
0
      case IPP_TAG_INTEGER :
5223
0
      case IPP_TAG_ENUM :
5224
0
          bytes += (size_t)(4 * attr->num_values);
5225
0
    break;
5226
5227
0
      case IPP_TAG_BOOLEAN :
5228
0
          bytes += (size_t)attr->num_values;
5229
0
    break;
5230
5231
0
      case IPP_TAG_TEXT :
5232
0
      case IPP_TAG_NAME :
5233
0
      case IPP_TAG_KEYWORD :
5234
0
      case IPP_TAG_URI :
5235
0
      case IPP_TAG_URISCHEME :
5236
0
      case IPP_TAG_CHARSET :
5237
0
      case IPP_TAG_LANGUAGE :
5238
0
      case IPP_TAG_MIMETYPE :
5239
0
    for (i = 0, value = attr->values; i < attr->num_values; i ++, value ++)
5240
0
    {
5241
0
      if (value->string.text)
5242
0
        bytes += strlen(value->string.text);
5243
0
    }
5244
0
    break;
5245
5246
0
      case IPP_TAG_DATE :
5247
0
          bytes += (size_t)(11 * attr->num_values);
5248
0
    break;
5249
5250
0
      case IPP_TAG_RESOLUTION :
5251
0
          bytes += (size_t)(9 * attr->num_values);
5252
0
    break;
5253
5254
0
      case IPP_TAG_RANGE :
5255
0
          bytes += (size_t)(8 * attr->num_values);
5256
0
    break;
5257
5258
0
      case IPP_TAG_TEXTLANG :
5259
0
      case IPP_TAG_NAMELANG :
5260
0
          bytes += (size_t)(4 * attr->num_values);
5261
          // Charset + text length
5262
5263
0
    for (i = 0, value = attr->values; i < attr->num_values; i ++, value ++)
5264
0
    {
5265
0
      if (value->string.language)
5266
0
        bytes += strlen(value->string.language);
5267
5268
0
      if (value->string.text)
5269
0
        bytes += strlen(value->string.text);
5270
0
    }
5271
0
    break;
5272
5273
0
      case IPP_TAG_BEGIN_COLLECTION :
5274
0
    for (i = 0, value = attr->values; i < attr->num_values; i ++, value ++)
5275
0
            bytes += ipp_length(value->collection, 1);
5276
0
    break;
5277
5278
0
      default :
5279
0
    for (i = 0, value = attr->values; i < attr->num_values; i ++, value ++)
5280
0
            bytes += (size_t)value->unknown.length;
5281
0
    break;
5282
0
    }
5283
0
  }
5284
5285
  // Finally, add 1 byte for the "end of attributes" tag or 5 bytes for the "end of collection" tag and return...
5286
0
  if (collection)
5287
0
    bytes += 5;
5288
0
  else
5289
0
    bytes ++;
5290
5291
0
  DEBUG_printf("4ipp_length: Returning " CUPS_LLFMT " bytes", CUPS_LLCAST bytes);
5292
5293
0
  return (bytes);
5294
0
}
5295
5296
5297
//
5298
// 'ipp_read_file()' - Read IPP data from a file.
5299
//
5300
5301
static ssize_t        // O - Number of bytes read
5302
ipp_read_file(int         *fd,    // I - File descriptor
5303
              ipp_uchar_t *buffer,  // O - Read buffer
5304
        size_t      length) // I - Number of bytes to read
5305
0
{
5306
#ifdef _WIN32
5307
  return ((ssize_t)read(*fd, buffer, (unsigned)length));
5308
#else
5309
0
  return (read(*fd, buffer, length));
5310
0
#endif // _WIN32
5311
0
}
5312
5313
5314
//
5315
// 'ipp_read_http()' - Semi-blocking read on a HTTP connection...
5316
//
5317
5318
static ssize_t        // O - Number of bytes read
5319
ipp_read_http(http_t      *http,  // I - Client connection
5320
              ipp_uchar_t *buffer,  // O - Buffer for data
5321
        size_t      length) // I - Total length
5322
0
{
5323
0
  ssize_t tbytes,     // Total bytes read
5324
0
    bytes;      // Bytes read this pass
5325
5326
5327
0
  DEBUG_printf("7ipp_read_http(http=%p, buffer=%p, length=%d)", (void *)http, (void *)buffer, (int)length);
5328
5329
  // Loop until all bytes are read...
5330
0
  for (tbytes = 0, bytes = 0; tbytes < (int)length; tbytes += bytes, buffer += bytes)
5331
0
  {
5332
0
    DEBUG_printf("9ipp_read_http: tbytes=" CUPS_LLFMT ", http->state=%d", CUPS_LLCAST tbytes, http->state);
5333
5334
0
    if (http->state == HTTP_STATE_WAITING)
5335
0
      break;
5336
5337
0
    if (http->used == 0 && !http->blocking)
5338
0
    {
5339
      // Wait up to 10 seconds for more data on non-blocking sockets...
5340
0
      if (!httpWait(http, 10000))
5341
0
      {
5342
  // Signal no data...
5343
0
  bytes = -1;
5344
0
  break;
5345
0
      }
5346
0
    }
5347
0
    else if (http->used == 0 && http->timeout_value > 0)
5348
0
    {
5349
      // Wait up to timeout seconds for more data on blocking sockets...
5350
0
      if (!httpWait(http, (int)(1000 * http->timeout_value)))
5351
0
      {
5352
  // Signal no data...
5353
0
  bytes = -1;
5354
0
  break;
5355
0
      }
5356
0
    }
5357
5358
0
    if ((bytes = httpRead(http, (char *)buffer, length - (size_t)tbytes)) < 0)
5359
0
    {
5360
#ifdef _WIN32
5361
      break;
5362
#else
5363
0
      if (errno != EAGAIN && errno != EINTR)
5364
0
  break;
5365
5366
0
      bytes = 0;
5367
0
#endif // _WIN32
5368
0
    }
5369
0
    else if (bytes == 0)
5370
0
    {
5371
0
      break;
5372
0
    }
5373
0
  }
5374
5375
  // Return the number of bytes read...
5376
0
  if (tbytes == 0 && bytes < 0)
5377
0
    tbytes = -1;
5378
5379
0
  DEBUG_printf("8ipp_read_http: Returning " CUPS_LLFMT " bytes", CUPS_LLCAST tbytes);
5380
5381
0
  return (tbytes);
5382
0
}
5383
5384
5385
//
5386
// 'ipp_read_io()' - Read data for an IPP message.
5387
//
5388
5389
static ipp_state_t      // O - Current state
5390
ipp_read_io(void        *src,   // I - Data source
5391
            ipp_io_cb_t cb,   // I - Read callback function
5392
      bool        blocking, // I - Use blocking IO?
5393
      ipp_t       *parent,  // I - Parent request, if any
5394
            ipp_t       *ipp,   // I - IPP data
5395
            int         depth)    // I - Depth of collection
5396
190k
{
5397
190k
  int     n;    // Length of data
5398
190k
  unsigned char   *buffer,  // Data buffer
5399
190k
      string[IPP_MAX_TEXT],
5400
          // Small string buffer
5401
190k
      *bufptr,  // Pointer into buffer
5402
190k
      *bufend;  // End of buffer
5403
190k
  ipp_attribute_t *attr = NULL; // Current attribute
5404
190k
  ipp_tag_t   tag;    // Current tag
5405
190k
  ipp_tag_t   value_tag;  // Current value tag
5406
190k
  _ipp_value_t    *value;   // Current value
5407
5408
5409
190k
  DEBUG_printf("ipp_read_io(src=%p, cb=%p, blocking=%d, parent=%p, ipp=%p, depth=%d)", (void *)src, (void *)cb, blocking, (void *)parent, (void *)ipp, depth);
5410
190k
  DEBUG_printf("2ipp_read_io: ipp->state=%d", ipp->state);
5411
5412
190k
  if (depth > _IPP_MAX_DEPTH)
5413
1
  {
5414
1
    _cupsSetError(IPP_STATUS_ERROR_INTERNAL, _("IPP message nested too deeply."), true);
5415
1
    return (IPP_STATE_ERROR);
5416
1
  }
5417
5418
190k
  if ((buffer = (unsigned char *)_cupsBufferGet(IPP_BUF_SIZE)) == NULL)
5419
0
  {
5420
0
    DEBUG_puts("1ipp_read_io: Unable to get read buffer.");
5421
0
    return (IPP_STATE_ERROR);
5422
0
  }
5423
5424
190k
  if (!ipp->def_language)
5425
190k
  {
5426
190k
    if (parent && parent->def_language)
5427
186k
      ipp->def_language = _cupsStrAlloc(parent->def_language);
5428
4.74k
    else
5429
4.74k
      ipp->def_language = _cupsStrAlloc("en-us");
5430
190k
  }
5431
5432
190k
  switch (ipp->state)
5433
190k
  {
5434
190k
    case IPP_STATE_IDLE :
5435
190k
        ipp->state ++; // Avoid common problem...
5436
5437
190k
    case IPP_STATE_HEADER :
5438
190k
        if (parent == NULL)
5439
4.74k
  {
5440
          // Get the request header...
5441
4.74k
          if ((*cb)(src, buffer, 8) < 8)
5442
189
    {
5443
189
      DEBUG_puts("1ipp_read_io: Unable to read header.");
5444
189
      goto rollback;
5445
189
    }
5446
5447
          // Then copy the request header over...
5448
4.55k
          ipp->request.version[0]  = buffer[0];
5449
4.55k
          ipp->request.version[1]  = buffer[1];
5450
4.55k
          ipp->request.op_status   = (short)((buffer[2] << 8) | buffer[3]);
5451
4.55k
          ipp->request.request_id  = (buffer[4] << 24) | (buffer[5] << 16) | (buffer[6] << 8) | buffer[7];
5452
5453
4.55k
          DEBUG_printf("2ipp_read_io: version=%d.%d", buffer[0], buffer[1]);
5454
4.55k
    DEBUG_printf("2ipp_read_io: op_status=%04x", ipp->request.op_status);
5455
4.55k
    DEBUG_printf("2ipp_read_io: request_id=%d", ipp->request.request_id);
5456
4.55k
        }
5457
5458
190k
        ipp->state   = IPP_STATE_ATTRIBUTE;
5459
190k
  ipp->current = NULL;
5460
190k
  ipp->curtag  = IPP_TAG_ZERO;
5461
190k
  ipp->prev    = ipp->last;
5462
5463
        // If blocking is disabled, stop here...
5464
190k
        if (!blocking)
5465
0
    break;
5466
5467
190k
    case IPP_STATE_ATTRIBUTE :
5468
190k
        for (;;)
5469
8.38M
  {
5470
8.38M
    if ((*cb)(src, buffer, 1) < 1)
5471
530
    {
5472
530
      DEBUG_puts("1ipp_read_io: Callback returned EOF/error");
5473
530
      goto rollback;
5474
530
    }
5475
5476
8.37M
    DEBUG_printf("2ipp_read_io: ipp->current=%p, ipp->prev=%p", (void *)ipp->current, (void *)ipp->prev);
5477
5478
    // Read this attribute...
5479
8.37M
          tag = (ipp_tag_t)buffer[0];
5480
5481
8.37M
    if (tag == IPP_TAG_END)
5482
136k
    {
5483
      // No more attributes left...
5484
136k
            DEBUG_puts("2ipp_read_io: IPP_TAG_END.");
5485
5486
136k
      ipp->state = IPP_STATE_DATA;
5487
136k
      break;
5488
136k
    }
5489
8.24M
    else if (tag == IPP_TAG_ZERO || (tag == IPP_TAG_OPERATION && ipp->curtag != IPP_TAG_ZERO))
5490
115
    {
5491
115
      _cupsSetError(IPP_STATUS_ERROR_INTERNAL, _("Invalid group tag."), true);
5492
115
      DEBUG_printf("1ipp_read_io: bad tag 0x%02x.", tag);
5493
115
      goto rollback;
5494
115
    }
5495
8.24M
          else if (tag < IPP_TAG_UNSUPPORTED_VALUE)
5496
1.29M
    {
5497
      // Group tag...  Set the current group and continue...
5498
1.29M
            if (parent)
5499
3
            {
5500
3
        _cupsSetError(IPP_STATUS_ERROR_INTERNAL, _("Invalid group tag."), true);
5501
3
        DEBUG_printf("1ipp_read_io: bad tag 0x%02x.", tag);
5502
3
        goto rollback;
5503
3
            }
5504
1.29M
            else if (ipp->curtag == tag)
5505
990k
            {
5506
990k
        ipp->prev = ippAddSeparator(ipp);
5507
990k
            }
5508
306k
            else if (ipp->current)
5509
113k
      {
5510
113k
        ipp->prev = ipp->current;
5511
113k
      }
5512
5513
1.29M
      ipp->curtag  = tag;
5514
1.29M
      ipp->current = NULL;
5515
1.29M
      attr         = NULL;
5516
1.29M
      DEBUG_printf("2ipp_read_io: group tag=%x(%s), ipp->prev=%p", tag, ippTagString(tag), (void *)ipp->prev);
5517
1.29M
      continue;
5518
1.29M
    }
5519
5520
6.94M
          DEBUG_printf("2ipp_read_io: value tag=%x(%s)", tag, ippTagString(tag));
5521
5522
    // Get the name...
5523
6.94M
          if ((*cb)(src, buffer, 2) < 2)
5524
121
    {
5525
121
      DEBUG_puts("1ipp_read_io: unable to read name length.");
5526
121
      goto rollback;
5527
121
    }
5528
5529
6.94M
          n = (buffer[0] << 8) | buffer[1];
5530
5531
6.94M
    if (n >= IPP_BUF_SIZE)
5532
47
    {
5533
47
      _cupsSetError(IPP_STATUS_ERROR_INTERNAL, _("IPP name larger than 32767 bytes."), true);
5534
47
      DEBUG_printf("1ipp_read_io: bad name length %d.", n);
5535
47
      goto rollback;
5536
47
    }
5537
5538
6.94M
          DEBUG_printf("2ipp_read_io: name length=%d", n);
5539
5540
6.94M
          if (n && parent)
5541
4
          {
5542
4
            _cupsSetError(IPP_STATUS_ERROR_INTERNAL, _("Invalid named IPP attribute in collection."), true);
5543
4
            DEBUG_puts("1ipp_read_io: bad attribute name in collection.");
5544
4
      goto rollback;
5545
4
          }
5546
6.94M
          else if (n == 0 && tag != IPP_TAG_MEMBERNAME && tag != IPP_TAG_END_COLLECTION)
5547
6.63M
    {
5548
      // More values for current attribute...
5549
6.63M
            if (ipp->current == NULL)
5550
164
      {
5551
164
        _cupsSetError(IPP_STATUS_ERROR_INTERNAL, _("IPP attribute has no name."), true);
5552
164
        DEBUG_puts("1ipp_read_io: Attribute without name and no current.");
5553
164
        goto rollback;
5554
164
      }
5555
5556
6.63M
            attr      = ipp->current;
5557
6.63M
      value_tag = (ipp_tag_t)(attr->value_tag & IPP_TAG_CUPS_MASK);
5558
5559
      // Make sure we aren't adding a new value of a different type...
5560
6.63M
      if (value_tag == IPP_TAG_ZERO)
5561
1.66k
      {
5562
        // Setting the value of a collection member...
5563
1.66k
        attr->value_tag = tag;
5564
1.66k
      }
5565
6.63M
      else if (value_tag == IPP_TAG_TEXTLANG || value_tag == IPP_TAG_NAMELANG || (value_tag >= IPP_TAG_TEXT && value_tag <= IPP_TAG_MIMETYPE))
5566
3.05M
            {
5567
        // String values can sometimes come across in different forms; accept sets of differing values...
5568
3.05M
        if (tag != IPP_TAG_TEXTLANG && tag != IPP_TAG_NAMELANG && (tag < IPP_TAG_TEXT || tag > IPP_TAG_MIMETYPE) && tag != IPP_TAG_NOVALUE)
5569
24
        {
5570
24
    _cupsSetError(IPP_STATUS_ERROR_INTERNAL, _("IPP 1setOf attribute with incompatible value tags."), true);
5571
24
    DEBUG_printf("1ipp_read_io: 1setOf value tag %x(%s) != %x(%s)", value_tag, ippTagString(value_tag), tag, ippTagString(tag));
5572
24
    goto rollback;
5573
24
        }
5574
5575
3.05M
              if (value_tag != tag && value_tag != IPP_TAG_TEXTLANG && value_tag != IPP_TAG_NAMELANG)
5576
716k
              {
5577
716k
                DEBUG_printf("1ipp_read_io: Converting %s attribute from %s to %s.", attr->name, ippTagString(value_tag), ippTagString(tag));
5578
716k
    if (!ippSetValueTag(ipp, &attr, tag))
5579
22
      goto rollback;
5580
716k
        }
5581
3.05M
            }
5582
3.57M
      else if (value_tag == IPP_TAG_INTEGER || value_tag == IPP_TAG_RANGE)
5583
1.01M
            {
5584
        // Integer and rangeOfInteger values can sometimes be mixed; accept sets of differing values...
5585
1.01M
        if (tag != IPP_TAG_INTEGER && tag != IPP_TAG_RANGE)
5586
13
        {
5587
13
    _cupsSetError(IPP_STATUS_ERROR_INTERNAL, _("IPP 1setOf attribute with incompatible value tags."), true);
5588
13
    DEBUG_printf("1ipp_read_io: 1setOf value tag %x(%s) != %x(%s)", value_tag, ippTagString(value_tag), tag, ippTagString(tag));
5589
13
    goto rollback;
5590
13
        }
5591
5592
1.01M
              if (value_tag == IPP_TAG_INTEGER && tag == IPP_TAG_RANGE)
5593
394
              {
5594
                // Convert integer values to rangeOfInteger values...
5595
394
    DEBUG_printf("1ipp_read_io: Converting %s attribute to rangeOfInteger.", attr->name);
5596
394
                ippSetValueTag(ipp, &attr, IPP_TAG_RANGE);
5597
394
              }
5598
1.01M
            }
5599
2.56M
      else if (value_tag != tag)
5600
41
      {
5601
41
        _cupsSetError(IPP_STATUS_ERROR_INTERNAL, _("IPP 1setOf attribute with incompatible value tags."), true);
5602
41
        DEBUG_printf("1ipp_read_io: value tag %x(%s) != %x(%s)", value_tag, ippTagString(value_tag), tag, ippTagString(tag));
5603
41
        goto rollback;
5604
41
            }
5605
5606
      // Finally, reallocate the attribute array as needed...
5607
6.63M
      if ((value = ipp_set_value(ipp, &attr, attr->num_values)) == NULL)
5608
0
        goto rollback;
5609
6.63M
    }
5610
313k
    else if (tag == IPP_TAG_MEMBERNAME)
5611
4.38k
    {
5612
      // Name must be length 0!
5613
4.38k
      if (n)
5614
1
      {
5615
1
        _cupsSetError(IPP_STATUS_ERROR_INTERNAL, _("IPP member name is not empty."), true);
5616
1
        DEBUG_puts("1ipp_read_io: member name not empty.");
5617
1
        goto rollback;
5618
1
      }
5619
4.38k
      else if (!parent)
5620
1
      {
5621
1
        _cupsSetError(IPP_STATUS_ERROR_INTERNAL, _("IPP member attribute outside of collection."), true);
5622
1
        DEBUG_puts("1ipp_read_io: member attribute outside of collection.");
5623
1
        goto rollback;
5624
1
      }
5625
5626
4.38k
            if (ipp->current)
5627
2.67k
        ipp->prev = ipp->current;
5628
5629
4.38k
      attr = ipp->current = ipp_add_attr(ipp, NULL, ipp->curtag, IPP_TAG_ZERO, 1);
5630
4.38k
      if (!attr)
5631
0
      {
5632
0
        _cupsSetError(IPP_STATUS_ERROR_INTERNAL, strerror(errno), false);
5633
0
        DEBUG_puts("1ipp_read_io: unable to allocate attribute.");
5634
0
        goto rollback;
5635
0
      }
5636
5637
4.38k
      DEBUG_printf("2ipp_read_io: membername, ipp->current=%p, ipp->prev=%p", (void *)ipp->current, (void *)ipp->prev);
5638
5639
4.38k
      value = attr->values;
5640
4.38k
    }
5641
308k
    else if (tag != IPP_TAG_END_COLLECTION)
5642
257k
    {
5643
      // New attribute; read the name and add it...
5644
257k
      if ((*cb)(src, buffer, (size_t)n) < n)
5645
331
      {
5646
331
        _cupsSetError(IPP_STATUS_ERROR_INTERNAL, _("Unable to read IPP attribute name."), true);
5647
331
        DEBUG_puts("1ipp_read_io: unable to read name.");
5648
331
        goto rollback;
5649
331
      }
5650
5651
257k
      buffer[n] = '\0';
5652
5653
257k
      if (!ipp_is_valid_keyword(/*name*/NULL, (char *)buffer))
5654
134
        goto rollback;
5655
5656
257k
            if (ipp->current)
5657
48.2k
        ipp->prev = ipp->current;
5658
5659
257k
      if ((attr = ipp->current = ipp_add_attr(ipp, (char *)buffer, ipp->curtag, tag, 1)) == NULL)
5660
0
      {
5661
0
        _cupsSetError(IPP_STATUS_ERROR_INTERNAL, strerror(errno), false);
5662
0
        DEBUG_puts("1ipp_read_io: unable to allocate attribute.");
5663
0
        goto rollback;
5664
0
      }
5665
5666
257k
      DEBUG_printf("2ipp_read_io: name=\"%s\", ipp->current=%p, ipp->prev=%p", buffer, (void *)ipp->current, (void *)ipp->prev);
5667
5668
257k
      value = attr->values;
5669
257k
    }
5670
51.1k
    else
5671
51.1k
    {
5672
51.1k
      attr  = NULL;
5673
51.1k
      value = NULL;
5674
51.1k
    }
5675
5676
6.94M
    if ((*cb)(src, buffer, 2) < 2)
5677
433
    {
5678
433
      DEBUG_puts("1ipp_read_io: unable to read value length.");
5679
433
      goto rollback;
5680
433
    }
5681
5682
6.94M
    n = (buffer[0] << 8) | buffer[1];
5683
6.94M
          DEBUG_printf("2ipp_read_io: value length=%d", n);
5684
5685
6.94M
    if (n >= IPP_BUF_SIZE)
5686
6
    {
5687
6
      _cupsSetError(IPP_STATUS_ERROR_INTERNAL,
5688
6
        _("IPP value larger than 32767 bytes."), true);
5689
6
      DEBUG_printf("1ipp_read_io: bad value length %d.", n);
5690
6
      goto rollback;
5691
6
    }
5692
5693
6.94M
    switch (tag)
5694
6.94M
    {
5695
524k
      case IPP_TAG_INTEGER :
5696
526k
      case IPP_TAG_ENUM :
5697
526k
    if (n != 4)
5698
17
    {
5699
17
      if (tag == IPP_TAG_INTEGER)
5700
9
        _cupsSetError(IPP_STATUS_ERROR_INTERNAL, _("IPP integer value not 4 bytes."), true);
5701
8
      else
5702
8
        _cupsSetError(IPP_STATUS_ERROR_INTERNAL, _("IPP enum value not 4 bytes."), true);
5703
17
      DEBUG_printf("1ipp_read_io: bad integer value length %d.", n);
5704
17
      goto rollback;
5705
17
    }
5706
5707
526k
          if ((*cb)(src, buffer, 4) < 4)
5708
10
    {
5709
10
            DEBUG_puts("1ipp_read_io: Unable to read integer value.");
5710
10
      goto rollback;
5711
10
    }
5712
5713
526k
    n = (buffer[0] << 24) | (buffer[1] << 16) | (buffer[2] << 8) | buffer[3];
5714
5715
526k
                if (attr->value_tag == IPP_TAG_RANGE)
5716
170k
                  value->range.lower = value->range.upper = n;
5717
355k
                else
5718
355k
      value->integer = n;
5719
526k
          break;
5720
5721
529k
      case IPP_TAG_BOOLEAN :
5722
529k
    if (n != 1)
5723
13
    {
5724
13
      _cupsSetError(IPP_STATUS_ERROR_INTERNAL, _("IPP boolean value not 1 byte."),
5725
13
                    1);
5726
13
      DEBUG_printf("1ipp_read_io: bad boolean value length %d.", n);
5727
13
      goto rollback;
5728
13
    }
5729
5730
529k
          if ((*cb)(src, buffer, 1) < 1)
5731
2
    {
5732
2
            DEBUG_puts("1ipp_read_io: Unable to read boolean value.");
5733
2
      goto rollback;
5734
2
    }
5735
5736
529k
                value->boolean = (char)buffer[0];
5737
529k
          break;
5738
5739
1.43k
      case IPP_TAG_UNSUPPORTED_VALUE :
5740
3.51k
      case IPP_TAG_DEFAULT :
5741
4.77k
      case IPP_TAG_UNKNOWN :
5742
8.98k
      case IPP_TAG_NOVALUE :
5743
9.72k
      case IPP_TAG_NOTSETTABLE :
5744
11.4k
      case IPP_TAG_DELETEATTR :
5745
12.6k
      case IPP_TAG_ADMINDEFINE :
5746
          // These value types are not supposed to have values, however
5747
    // some vendors (Brother) do not implement IPP correctly and so
5748
    // we need to discard them...
5749
12.6k
    if (n > 0)
5750
4.41k
    {
5751
4.41k
      DEBUG_printf("1ipp_read_io: Invalid out-of-band value of length %d, discarding.", n);
5752
4.41k
      if ((*cb)(src, buffer, (size_t)n) < n)
5753
56
      {
5754
56
        DEBUG_puts("1ipp_read_io: Unable to read out-of-band value.");
5755
56
        goto rollback;
5756
56
      }
5757
4.41k
    }
5758
12.5k
    break;
5759
5760
12.5k
      case IPP_TAG_TEXT :
5761
1.13M
      case IPP_TAG_NAME :
5762
1.13M
      case IPP_TAG_RESERVED_STRING :
5763
2.55M
      case IPP_TAG_KEYWORD :
5764
2.89M
      case IPP_TAG_URI :
5765
2.90M
      case IPP_TAG_URISCHEME :
5766
2.91M
      case IPP_TAG_CHARSET :
5767
2.91M
      case IPP_TAG_LANGUAGE :
5768
2.92M
      case IPP_TAG_MIMETYPE :
5769
2.92M
          if (n > 0)
5770
1.87M
          {
5771
1.87M
      if ((*cb)(src, buffer, (size_t)n) < n)
5772
155
      {
5773
155
        DEBUG_puts("1ipp_read_io: unable to read string value.");
5774
155
        goto rollback;
5775
155
      }
5776
1.87M
    }
5777
5778
2.92M
    buffer[n] = '\0';
5779
2.92M
    value->string.text = _cupsStrAlloc((char *)buffer);
5780
2.92M
    DEBUG_printf("2ipp_read_io: value=\"%s\"(%p)", value->string.text, value->string.text);
5781
5782
2.92M
    if (tag == IPP_TAG_LANGUAGE && !strcmp(attr->name, "attributes-natural-language"))
5783
405
    {
5784
405
      _cupsStrFree(ipp->def_language);
5785
405
      ipp->def_language = _cupsStrAlloc(value->string.text);
5786
405
    }
5787
5788
2.92M
    value_tag = (ipp_tag_t)(attr->value_tag & IPP_TAG_CUPS_MASK);
5789
5790
2.92M
    if (value_tag == IPP_TAG_TEXTLANG || value_tag == IPP_TAG_NAMELANG)
5791
1.33M
    {
5792
      // name/text values use the default language while
5793
      // keyword/uri/uriScheme/charset/naturalLanguage/mimeMediaType
5794
      // values are US English per RFC 8011...
5795
1.33M
      if (tag == IPP_TAG_NAME || tag == IPP_TAG_TEXT)
5796
635k
        value->string.language = _cupsStrAlloc(ipp->def_language);
5797
703k
      else
5798
703k
        value->string.language = _cupsStrAlloc("en-us");
5799
1.33M
    }
5800
2.92M
          break;
5801
5802
322k
      case IPP_TAG_DATE :
5803
322k
    if (n != 11)
5804
16
    {
5805
16
      _cupsSetError(IPP_STATUS_ERROR_INTERNAL, _("IPP date value not 11 bytes."), true);
5806
16
      DEBUG_printf("1ipp_read_io: bad date value length %d.", n);
5807
16
      goto rollback;
5808
16
    }
5809
5810
322k
          if ((*cb)(src, value->date, 11) < 11)
5811
15
    {
5812
15
            DEBUG_puts("1ipp_read_io: Unable to read date value.");
5813
15
      goto rollback;
5814
15
    }
5815
322k
          break;
5816
5817
632k
      case IPP_TAG_RESOLUTION :
5818
632k
    if (n != 9)
5819
4
    {
5820
4
      _cupsSetError(IPP_STATUS_ERROR_INTERNAL, _("IPP resolution value not 9 bytes."), true);
5821
4
      DEBUG_printf("1ipp_read_io: bad resolution value length %d.", n);
5822
4
      goto rollback;
5823
4
    }
5824
5825
632k
          if ((*cb)(src, buffer, 9) < 9)
5826
17
    {
5827
17
            DEBUG_puts("1ipp_read_io: Unable to read resolution value.");
5828
17
      goto rollback;
5829
17
    }
5830
5831
632k
                value->resolution.xres  = (buffer[0] << 24) | (buffer[1] << 16) | (buffer[2] << 8) | buffer[3];
5832
632k
                value->resolution.yres  = (buffer[4] << 24) | (buffer[5] << 16) | (buffer[6] << 8) | buffer[7];
5833
632k
                value->resolution.units =
5834
632k
        (ipp_res_t)buffer[8];
5835
632k
          break;
5836
5837
495k
      case IPP_TAG_RANGE :
5838
495k
    if (n != 8)
5839
30
    {
5840
30
      _cupsSetError(IPP_STATUS_ERROR_INTERNAL, _("IPP rangeOfInteger value not 8 bytes."), true);
5841
30
      DEBUG_printf("1ipp_read_io: bad rangeOfInteger value length %d.", n);
5842
30
      goto rollback;
5843
30
    }
5844
5845
495k
          if ((*cb)(src, buffer, 8) < 8)
5846
15
    {
5847
15
            DEBUG_puts("1ipp_read_io: Unable to read range value.");
5848
15
      goto rollback;
5849
15
    }
5850
5851
495k
                value->range.lower = (buffer[0] << 24) | (buffer[1] << 16) | (buffer[2] << 8) | buffer[3];
5852
495k
                value->range.upper = (buffer[4] << 24) | (buffer[5] << 16) | (buffer[6] << 8) | buffer[7];
5853
495k
          break;
5854
5855
271k
      case IPP_TAG_TEXTLANG :
5856
352k
      case IPP_TAG_NAMELANG :
5857
352k
          if (n < 4)
5858
14
    {
5859
14
      if (tag == IPP_TAG_TEXTLANG)
5860
4
        _cupsSetError(IPP_STATUS_ERROR_INTERNAL, _("IPP textWithLanguage value less than minimum 4 bytes."), true);
5861
10
      else
5862
10
        _cupsSetError(IPP_STATUS_ERROR_INTERNAL, _("IPP nameWithLanguage value less than minimum 4 bytes."), true);
5863
14
      DEBUG_printf("1ipp_read_io: bad stringWithLanguage value length %d.", n);
5864
14
      goto rollback;
5865
14
    }
5866
5867
352k
          if ((*cb)(src, buffer, (size_t)n) < n)
5868
84
    {
5869
84
            DEBUG_puts("1ipp_read_io: Unable to read string w/language value.");
5870
84
      goto rollback;
5871
84
    }
5872
5873
352k
                bufptr = buffer;
5874
352k
                bufend = buffer + n;
5875
5876
          // textWithLanguage and nameWithLanguage are composite values:
5877
    //
5878
    //   language-length
5879
    //   language
5880
    //   text-length
5881
    //   text
5882
352k
    n = (bufptr[0] << 8) | bufptr[1];
5883
5884
352k
    if ((bufptr + 2 + n + 2) > bufend || n >= (int)sizeof(string))
5885
38
    {
5886
38
      _cupsSetError(IPP_STATUS_ERROR_INTERNAL, _("IPP language length overflows value."), true);
5887
38
      DEBUG_printf("1ipp_read_io: bad language value length %d.", n);
5888
38
      goto rollback;
5889
38
    }
5890
352k
    else if (n >= IPP_MAX_LANGUAGE)
5891
9
    {
5892
9
      _cupsSetError(IPP_STATUS_ERROR_INTERNAL, _("IPP language length too large."), true);
5893
9
      DEBUG_printf("1ipp_read_io: bad language value length %d.", n);
5894
9
      goto rollback;
5895
9
    }
5896
5897
352k
    memcpy(string, bufptr + 2, (size_t)n);
5898
352k
    string[n] = '\0';
5899
5900
352k
    value->string.language = _cupsStrAlloc((char *)string);
5901
5902
352k
                bufptr += 2 + n;
5903
352k
    n = (bufptr[0] << 8) | bufptr[1];
5904
5905
352k
    if ((bufptr + 2 + n) > bufend)
5906
33
    {
5907
33
      _cupsSetError(IPP_STATUS_ERROR_INTERNAL, _("IPP string length overflows value."), true);
5908
33
      DEBUG_printf("1ipp_read_io: bad string value length %d.", n);
5909
33
      goto rollback;
5910
33
    }
5911
5912
351k
    bufptr[2 + n] = '\0';
5913
351k
                value->string.text = _cupsStrAlloc((char *)bufptr + 2);
5914
351k
          break;
5915
5916
186k
            case IPP_TAG_BEGIN_COLLECTION :
5917
          // Oh boy, here comes a collection value, so read it...
5918
186k
                value->collection = ippNew();
5919
5920
186k
                if (n > 0)
5921
20
    {
5922
20
      _cupsSetError(IPP_STATUS_ERROR_INTERNAL, _("IPP begCollection value not 0 bytes."), true);
5923
20
            DEBUG_puts("1ipp_read_io: begCollection tag with value length > 0.");
5924
20
      goto rollback;
5925
20
    }
5926
5927
186k
    if (ipp_read_io(src, cb, 1, ipp, value->collection, depth + 1) == IPP_STATE_ERROR)
5928
247
    {
5929
247
            DEBUG_puts("1ipp_read_io: Unable to read collection value.");
5930
247
      goto rollback;
5931
247
    }
5932
185k
                break;
5933
5934
185k
            case IPP_TAG_END_COLLECTION :
5935
51.1k
                if (n > 0)
5936
26
    {
5937
26
      _cupsSetError(IPP_STATUS_ERROR_INTERNAL, _("IPP endCollection value not 0 bytes."), true);
5938
26
            DEBUG_puts("1ipp_read_io: endCollection tag with value length > 0.");
5939
26
      goto rollback;
5940
26
    }
5941
5942
51.1k
    _cupsBufferRelease((char *)buffer);
5943
5944
51.1k
          DEBUG_puts("1ipp_read_io: endCollection tag...");
5945
51.1k
    return (ipp->state = IPP_STATE_DATA);
5946
5947
4.37k
            case IPP_TAG_MEMBERNAME :
5948
          // The value is the name of the member in the collection, which
5949
    // we need to carry over...
5950
4.37k
    if (n == 0)
5951
1
    {
5952
1
      _cupsSetError(IPP_STATUS_ERROR_INTERNAL, _("IPP memberName value is empty."), true);
5953
1
            DEBUG_puts("1ipp_read_io: Empty member name value.");
5954
1
      goto rollback;
5955
1
    }
5956
4.37k
    else if ((*cb)(src, buffer, (size_t)n) < n)
5957
44
    {
5958
44
            DEBUG_puts("1ipp_read_io: Unable to read member name value.");
5959
44
      goto rollback;
5960
44
    }
5961
5962
4.32k
    buffer[n] = '\0';
5963
5964
4.32k
    if (!ipp_is_valid_keyword(/*name*/NULL, (char *)buffer))
5965
4
      goto rollback;
5966
5967
4.32k
    attr->name = _cupsStrAlloc((char *)buffer);
5968
5969
          // Since collection members are encoded differently than
5970
    // regular attributes, make sure we don't start with an
5971
    // empty value...
5972
4.32k
                attr->num_values --;
5973
5974
4.32k
    DEBUG_printf("2ipp_read_io: member name=\"%s\"", attr->name);
5975
4.32k
    break;
5976
5977
445k
            case IPP_TAG_STRING :
5978
910k
            default : // Other unsupported values
5979
910k
                if (tag == IPP_TAG_STRING && n > IPP_MAX_LENGTH)
5980
1
    {
5981
1
      _cupsSetError(IPP_STATUS_ERROR_INTERNAL, _("IPP octetString length too large."), true);
5982
1
      DEBUG_printf("1ipp_read_io: bad octetString value length %d.", n);
5983
1
      goto rollback;
5984
1
    }
5985
5986
910k
                value->unknown.length = (size_t)n;
5987
5988
910k
          if (n > 0)
5989
527k
    {
5990
527k
      if ((value->unknown.data = malloc((size_t)n)) == NULL)
5991
0
      {
5992
0
        _cupsSetError(IPP_STATUS_ERROR_INTERNAL, strerror(errno), false);
5993
0
        DEBUG_puts("1ipp_read_io: Unable to allocate value");
5994
0
        goto rollback;
5995
0
      }
5996
5997
527k
            if ((*cb)(src, value->unknown.data, (size_t)n) < n)
5998
128
      {
5999
128
              DEBUG_puts("1ipp_read_io: Unable to read unsupported value.");
6000
128
        goto rollback;
6001
128
      }
6002
527k
    }
6003
382k
    else
6004
382k
    {
6005
382k
      value->unknown.data = NULL;
6006
382k
    }
6007
910k
          break;
6008
6.94M
    }
6009
6010
          // If blocking is disabled, stop here...
6011
6.89M
          if (!blocking)
6012
0
      break;
6013
6.89M
  }
6014
136k
        break;
6015
6016
136k
    case IPP_STATE_DATA :
6017
0
        break;
6018
6019
0
    default :
6020
0
        break; // anti-compiler-warning-code
6021
190k
  }
6022
6023
136k
  DEBUG_printf("1ipp_read_io: returning ipp->state=%d.", ipp->state);
6024
136k
  _cupsBufferRelease((char *)buffer);
6025
6026
136k
  return (ipp->state);
6027
6028
  // If we get here, there was an error that required us to roll back the last
6029
  // attribute read in order to keep the IPP message valid...
6030
3.17k
  rollback:
6031
6032
3.17k
  DEBUG_puts("1ipp_read_io: <<rollback>>");
6033
6034
3.17k
  _cupsBufferRelease((char *)buffer);
6035
6036
3.17k
  if (attr)
6037
1.98k
    ippDeleteAttribute(ipp, attr);
6038
6039
3.17k
  return (IPP_STATE_ERROR);
6040
190k
}
6041
6042
6043
//
6044
// 'ipp_regex_begin()' - Start using validation regular expressions.
6045
//
6046
6047
static bool       // O - `true` on success, `false` on failure
6048
ipp_regex_begin(void)
6049
5.89k
{
6050
5.89k
  int   r;      // regcomp() error code
6051
6052
6053
  // Take a lock...
6054
5.89k
  cupsMutexLock(&regex_mutex);
6055
6056
  // If the regular expressions have not been initialized, do so now...
6057
5.89k
  if (!regex_init)
6058
1
  {
6059
1
    regex_init = true;
6060
6061
    // The following regular expression is derived from the ABNF for
6062
    // language tags in RFC 4646.  All I can say is that this is the
6063
    // easiest way to check the values...
6064
1
    if ((r = regcomp(&lang_regex,
6065
1
         "^("
6066
1
         "(([a-z]{2,3}(-[a-z][a-z][a-z]){0,3})|[a-z]{4,8})"
6067
              // language
6068
1
         "(-[a-z][a-z][a-z][a-z]){0,1}" // script
6069
1
         "(-([a-z][a-z]|[0-9][0-9][0-9])){0,1}"
6070
              // region
6071
1
         "(-([a-z]{5,8}|[0-9][0-9][0-9]))*" // variant
6072
1
         "(-[a-wy-z](-[a-z0-9]{2,8})+)*"  // extension
6073
1
         "(-x(-[a-z0-9]{1,8})+)*"   // privateuse
6074
1
         "|"
6075
1
         "x(-[a-z0-9]{1,8})+"   // privateuse
6076
1
         "|"
6077
1
         "[a-z]{1,3}(-[a-z][0-9]{2,8}){1,2}"// grandfathered
6078
1
         ")$",
6079
1
         REG_NOSUB | REG_EXTENDED)) != 0)
6080
0
    {
6081
0
      char  temp[256];    // Temporary error string
6082
6083
0
      regerror(r, &lang_regex, temp, sizeof(temp));
6084
0
      ipp_set_error(IPP_STATUS_ERROR_INTERNAL, _("Unable to compile naturalLanguage regular expression: %s."), temp);
6085
0
      cupsMutexUnlock(&regex_mutex);
6086
0
      return (false);
6087
0
    }
6088
6089
6090
    // The following regular expression is derived from the ABNF for
6091
    // MIME media types in RFC 2045 and 4288.  All I can say is that this is
6092
    // the easiest way to check the values...
6093
1
    if ((r = regcomp(&mime_regex,
6094
1
         "^"
6095
1
         "[-a-zA-Z0-9!#$&.+^_]{1,127}"  // type-name
6096
1
         "/"
6097
1
         "[-a-zA-Z0-9!#$&.+^_]{1,127}"  // subtype-name
6098
1
         "(;[-a-zA-Z0-9!#$&.+^_]{1,127}=" // argument=
6099
1
         "([-a-zA-Z0-9!#$&.+^_]{1,127}|\"[^\"]*\"))*"
6100
              // value
6101
1
         "$",
6102
1
         REG_NOSUB | REG_EXTENDED)) != 0)
6103
0
    {
6104
0
      char  temp[256];    // Temporary error string
6105
6106
0
      regerror(r, &mime_regex, temp, sizeof(temp));
6107
0
      ipp_set_error(IPP_STATUS_ERROR_BAD_REQUEST, _("Unable to compile mimeMediaType regular expression: %s."), temp);
6108
0
      cupsMutexUnlock(&regex_mutex);
6109
0
      return (false);
6110
0
    }
6111
1
  }
6112
6113
5.89k
  return (true);
6114
5.89k
}
6115
6116
6117
//
6118
// 'ipp_regex_end()' - Finish using validation regular expressions.
6119
//
6120
6121
static void
6122
ipp_regex_end(void)
6123
5.89k
{
6124
5.89k
  cupsMutexUnlock(&regex_mutex);
6125
5.89k
}
6126
6127
6128
//
6129
// 'ipp_set_error()' - Set a formatted, localized error string.
6130
//
6131
6132
static void
6133
ipp_set_error(ipp_status_t status,  // I - Status code
6134
              const char   *format, // I - Printf-style error string
6135
        ...)      // I - Additional arguments as needed
6136
30.3k
{
6137
30.3k
  va_list ap;     // Pointer to additional args
6138
30.3k
  char    buffer[2048];   // Message buffer
6139
30.3k
  cups_lang_t *lang = cupsLangDefault();
6140
          // Current language
6141
6142
6143
30.3k
  va_start(ap, format);
6144
30.3k
  vsnprintf(buffer, sizeof(buffer), cupsLangGetString(lang, format), ap);
6145
30.3k
  va_end(ap);
6146
6147
30.3k
  _cupsSetError(status, buffer, 0);
6148
30.3k
}
6149
6150
6151
//
6152
// 'ipp_set_value()' - Get the value element from an attribute, expanding it as
6153
//                     needed.
6154
//
6155
6156
static _ipp_value_t *     // O  - IPP value element or NULL on error
6157
ipp_set_value(ipp_t           *ipp, // IO - IPP message
6158
              ipp_attribute_t **attr, // IO - IPP attribute
6159
              size_t          element)  // I  - Value number (`0`-based)
6160
6.63M
{
6161
6.63M
  ipp_attribute_t *temp,    // New attribute pointer
6162
6.63M
      *current, // Current attribute in list
6163
6.63M
      *prev;    // Previous attribute in list
6164
6.63M
  size_t    alloc_values; // Allocated values
6165
6166
6167
  // If we are setting an existing value element, return it...
6168
6.63M
  temp = *attr;
6169
6170
6.63M
  if (temp->num_values <= 1)
6171
14.1k
    alloc_values = 1;
6172
6.61M
  else
6173
6.61M
    alloc_values = (temp->num_values + IPP_MAX_VALUES - 1) & (size_t)~(IPP_MAX_VALUES - 1);
6174
6175
6.63M
  if (element < alloc_values)
6176
5.79M
  {
6177
5.79M
    if (element >= temp->num_values)
6178
5.79M
      temp->num_values = element + 1;
6179
6180
5.79M
    return (temp->values + element);
6181
5.79M
  }
6182
6183
  // Otherwise re-allocate the attribute - we allocate in groups of
6184
  // IPP_MAX_VALUE values when num_values > 1.
6185
836k
  if (alloc_values < IPP_MAX_VALUES)
6186
10.5k
    alloc_values = IPP_MAX_VALUES;
6187
825k
  else
6188
825k
    alloc_values += IPP_MAX_VALUES;
6189
6190
836k
  DEBUG_printf("4ipp_set_value: Reallocating for up to %u values.", (unsigned)alloc_values);
6191
6192
  // Reallocate memory...
6193
836k
  if ((temp = realloc(temp, sizeof(ipp_attribute_t) + (size_t)(alloc_values - 1) * sizeof(_ipp_value_t))) == NULL)
6194
0
  {
6195
0
    _cupsSetError(IPP_STATUS_ERROR_INTERNAL, strerror(errno), false);
6196
0
    DEBUG_puts("4ipp_set_value: Unable to resize attribute.");
6197
0
    return (NULL);
6198
0
  }
6199
6200
  // Zero the new memory...
6201
836k
  memset(temp->values + temp->num_values, 0, (size_t)(alloc_values - temp->num_values) * sizeof(_ipp_value_t));
6202
6203
836k
  if (temp != *attr)
6204
24.5k
  {
6205
    // Reset pointers in the list...
6206
24.5k
#ifndef __clang_analyzer__
6207
24.5k
    DEBUG_printf("4debug_free: %p %s", (void *)*attr, temp->name);
6208
24.5k
#endif // !__clang_analyzer__
6209
24.5k
    DEBUG_printf("4debug_alloc: %p %s %s%s (%u)", (void *)temp, temp->name, temp->num_values > 1 ? "1setOf " : "", ippTagString(temp->value_tag), (unsigned)temp->num_values);
6210
6211
24.5k
    if (ipp->current == *attr && ipp->prev && ipp->prev->next == *attr)
6212
13.4k
    {
6213
      // Use current "previous" pointer...
6214
13.4k
      prev = ipp->prev;
6215
13.4k
    }
6216
11.0k
    else
6217
11.0k
    {
6218
      // Find this attribute in the linked list...
6219
11.0k
      for (prev = NULL, current = ipp->attrs; current && current != *attr; prev = current, current = current->next)
6220
0
        ;        // Loop until we find the attribute
6221
6222
11.0k
      if (!current)
6223
0
      {
6224
  // This is a serious error!
6225
0
  *attr = temp;
6226
0
  _cupsSetError(IPP_STATUS_ERROR_INTERNAL, _("IPP attribute is not a member of the message."), true);
6227
0
  DEBUG_puts("4ipp_set_value: Unable to find attribute in message.");
6228
0
  return (NULL);
6229
0
      }
6230
11.0k
    }
6231
6232
24.5k
    if (prev)
6233
13.4k
      prev->next = temp;
6234
11.0k
    else
6235
11.0k
      ipp->attrs = temp;
6236
6237
24.5k
    ipp->current = temp;
6238
24.5k
    ipp->prev    = prev;
6239
6240
24.5k
    if (ipp->last == *attr)
6241
24.5k
      ipp->last = temp;
6242
6243
24.5k
    *attr = temp;
6244
24.5k
  }
6245
6246
  // Return the value element...
6247
836k
  if (element >= temp->num_values)
6248
836k
    temp->num_values = element + 1;
6249
6250
836k
  return (temp->values + element);
6251
836k
}
6252
6253
6254
//
6255
// 'ipp_write_file()' - Write IPP data to a file.
6256
//
6257
6258
static ssize_t        // O - Number of bytes written
6259
ipp_write_file(int         *fd,   // I - File descriptor
6260
               ipp_uchar_t *buffer, // I - Data to write
6261
               size_t      length)  // I - Number of bytes to write
6262
0
{
6263
#ifdef _WIN32
6264
  return ((ssize_t)write(*fd, buffer, (unsigned)length));
6265
#else
6266
0
  return (write(*fd, buffer, length));
6267
0
#endif // _WIN32
6268
0
}