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