/src/libcups/cups/string.c
Line | Count | Source |
1 | | // |
2 | | // String functions for CUPS. |
3 | | // |
4 | | // Copyright © 2022-2026 by OpenPrinting. |
5 | | // Copyright © 2007-2019 by Apple Inc. |
6 | | // Copyright © 1997-2007 by Easy Software Products. |
7 | | // |
8 | | // Licensed under Apache License v2.0. See the file "LICENSE" for more |
9 | | // information. |
10 | | // |
11 | | |
12 | | #define _CUPS_STRING_C_ |
13 | | #include "cups-private.h" |
14 | | #include <stddef.h> |
15 | | #include <limits.h> |
16 | | |
17 | | |
18 | | // |
19 | | // Local globals... |
20 | | // |
21 | | |
22 | | static cups_mutex_t sp_mutex = CUPS_MUTEX_INITIALIZER; |
23 | | // Mutex to control access to pool |
24 | | static cups_array_t *stringpool = NULL; |
25 | | // Global string pool |
26 | | |
27 | | |
28 | | // |
29 | | // Local functions... |
30 | | // |
31 | | |
32 | | static int compare_sp_items(_cups_sp_item_t *a, _cups_sp_item_t *b); |
33 | | static void validate_end(char *s, char *end); |
34 | | |
35 | | |
36 | | // |
37 | | // 'cupsConcatString()' - Safely concatenate two UTF-8 strings. |
38 | | // |
39 | | |
40 | | size_t // O - Length of string |
41 | | cupsConcatString(char *dst, // O - Destination string |
42 | | const char *src, // I - Source string |
43 | | size_t dstsize) // I - Size of destination string buffer |
44 | 2.51k | { |
45 | 2.51k | size_t srclen; // Length of source string |
46 | 2.51k | size_t dstlen; // Length of destination string |
47 | | |
48 | | |
49 | | // Range check input... |
50 | 2.51k | if (!dst || !src || dstsize == 0) |
51 | 0 | return (0); |
52 | | |
53 | | // Figure out how much room is left... |
54 | 2.51k | dstlen = strlen(dst); |
55 | | |
56 | 2.51k | if (dstsize < (dstlen + 1)) |
57 | 0 | return (dstlen); // No room, return immediately... |
58 | | |
59 | 2.51k | dstsize -= dstlen + 1; |
60 | | |
61 | | // Figure out how much room is needed... |
62 | 2.51k | srclen = strlen(src); |
63 | | |
64 | | // Copy the appropriate amount... |
65 | 2.51k | if (srclen <= dstsize) |
66 | 2.51k | { |
67 | | // String fits, just copy over... |
68 | 2.51k | memmove(dst + dstlen, src, srclen); |
69 | 2.51k | dst[dstlen + srclen] = '\0'; |
70 | 2.51k | } |
71 | 0 | else |
72 | 0 | { |
73 | | // String too big, copy what we can and clean up the end... |
74 | 0 | memmove(dst + dstlen, src, dstsize); |
75 | 0 | dst[dstlen + dstsize] = '\0'; |
76 | |
|
77 | 0 | validate_end(dst, dst + dstlen + dstsize); |
78 | 0 | } |
79 | | |
80 | 2.51k | return (dstlen + srclen); |
81 | 2.51k | } |
82 | | |
83 | | |
84 | | // |
85 | | // 'cupsCopyString()' - Safely copy a UTF-8 string. |
86 | | // |
87 | | |
88 | | size_t // O - Length of string |
89 | | cupsCopyString(char *dst, // O - Destination string |
90 | | const char *src, // I - Source string |
91 | | size_t dstsize) // I - Size of destination string buffer |
92 | 30.5k | { |
93 | 30.5k | size_t srclen; // Length of source string |
94 | | |
95 | | |
96 | | // Range check input... |
97 | 30.5k | if (!dst || !src || dstsize == 0) |
98 | 0 | { |
99 | 0 | if (dst) |
100 | 0 | *dst = '\0'; |
101 | 0 | return (0); |
102 | 0 | } |
103 | | |
104 | | // Figure out how much room is needed... |
105 | 30.5k | dstsize --; |
106 | | |
107 | 30.5k | srclen = strlen(src); |
108 | | |
109 | | // Copy the appropriate amount... |
110 | 30.5k | if (srclen <= dstsize) |
111 | 28.7k | { |
112 | | // Source string will fit... |
113 | 28.7k | memmove(dst, src, srclen); |
114 | 28.7k | dst[srclen] = '\0'; |
115 | 28.7k | } |
116 | 1.86k | else |
117 | 1.86k | { |
118 | | // Source string too big, copy what we can and clean up the end... |
119 | 1.86k | memmove(dst, src, dstsize); |
120 | 1.86k | dst[dstsize] = '\0'; |
121 | | |
122 | 1.86k | validate_end(dst, dst + dstsize); |
123 | 1.86k | } |
124 | | |
125 | 30.5k | return (srclen); |
126 | 30.5k | } |
127 | | |
128 | | |
129 | | // |
130 | | // 'cupsFormatString()' - Format a UTF-8 string into a fixed size buffer. |
131 | | // |
132 | | // This function formats a UTF-8 string into a fixed size buffer, escaping |
133 | | // special/control characters as needed so they can be safely displayed or |
134 | | // logged. |
135 | | // |
136 | | |
137 | | ssize_t // O - Number of bytes formatted |
138 | | cupsFormatString( |
139 | | char *buffer, // O - Output buffer |
140 | | size_t bufsize, // O - Size of output buffer |
141 | | const char *format, // I - `printf`-style format string |
142 | | ...) // I - Additional arguments |
143 | 0 | { |
144 | 0 | va_list ap; // Pointer to additional arguments |
145 | 0 | ssize_t ret; // Return value |
146 | | |
147 | | |
148 | | // Range check input... |
149 | 0 | if (!buffer || bufsize < 2 || !format) |
150 | 0 | return (-1); |
151 | | |
152 | | // Format the string... |
153 | 0 | va_start(ap, format); |
154 | 0 | ret = cupsFormatStringv(buffer, bufsize, format, ap); |
155 | 0 | va_end(ap); |
156 | | |
157 | | // Return the number of bytes that could have been written... |
158 | 0 | return (ret); |
159 | 0 | } |
160 | | |
161 | | |
162 | | // |
163 | | // 'cupsFormatStringv()' - Format a UTF-8 string into a fixed size buffer (`va_list` version). |
164 | | // |
165 | | // This function formats a UTF-8 string into a fixed size buffer using a |
166 | | // variable argument pointer, escaping special/control characters as needed so |
167 | | // they can be safely displayed or logged. |
168 | | // |
169 | | |
170 | | ssize_t // O - Number of bytes formatted |
171 | | cupsFormatStringv( |
172 | | char *buffer, // O - Output buffer |
173 | | size_t bufsize, // O - Size of output buffer |
174 | | const char *format, // I - printf-style format string |
175 | | va_list ap) // I - Pointer to additional arguments |
176 | 0 | { |
177 | 0 | char *bufptr, // Pointer to position in buffer |
178 | 0 | *bufend, // Pointer to end of buffer |
179 | 0 | size, // Size character (h, l, L) |
180 | 0 | type; // Format type character |
181 | 0 | int width, // Width of field |
182 | 0 | prec; // Number of characters of precision |
183 | 0 | char tformat[100], // Temporary format string for snprintf() |
184 | 0 | *tptr, // Pointer into temporary format |
185 | 0 | temp[1024]; // Buffer for formatted numbers |
186 | 0 | char *s; // Pointer to string |
187 | 0 | ssize_t bytes; // Total number of bytes needed |
188 | | |
189 | | |
190 | | // Range check input... |
191 | 0 | if (!buffer || bufsize < 2 || !format) |
192 | 0 | return (-1); |
193 | | |
194 | | // Loop through the format string, formatting as needed... |
195 | 0 | bufptr = buffer; |
196 | 0 | bufend = buffer + bufsize - 1; |
197 | 0 | bytes = 0; |
198 | |
|
199 | 0 | while (*format) |
200 | 0 | { |
201 | 0 | if (*format == '%') |
202 | 0 | { |
203 | | // Format character... |
204 | 0 | tptr = tformat; |
205 | 0 | *tptr++ = *format++; |
206 | |
|
207 | 0 | if (*format == '%') |
208 | 0 | { |
209 | 0 | if (bufptr < bufend) |
210 | 0 | *bufptr++ = *format; |
211 | 0 | bytes ++; |
212 | 0 | format ++; |
213 | 0 | continue; |
214 | 0 | } |
215 | 0 | else if (strchr(" -+#\'", *format)) |
216 | 0 | { |
217 | 0 | *tptr++ = *format++; |
218 | 0 | } |
219 | | |
220 | 0 | if (*format == '*') |
221 | 0 | { |
222 | | // Get width from argument... |
223 | 0 | format ++; |
224 | 0 | width = va_arg(ap, int); |
225 | |
|
226 | 0 | snprintf(tptr, sizeof(tformat) - (size_t)(tptr - tformat), "%d", width); |
227 | 0 | tptr += strlen(tptr); |
228 | 0 | } |
229 | 0 | else |
230 | 0 | { |
231 | 0 | width = 0; |
232 | |
|
233 | 0 | while (_cups_isdigit(*format)) |
234 | 0 | { |
235 | 0 | if (tptr < (tformat + sizeof(tformat) - 1)) |
236 | 0 | *tptr++ = *format; |
237 | |
|
238 | 0 | width = width * 10 + *format++ - '0'; |
239 | 0 | } |
240 | 0 | } |
241 | |
|
242 | 0 | if (*format == '.') |
243 | 0 | { |
244 | 0 | if (tptr < (tformat + sizeof(tformat) - 1)) |
245 | 0 | *tptr++ = *format; |
246 | |
|
247 | 0 | format ++; |
248 | |
|
249 | 0 | if (*format == '*') |
250 | 0 | { |
251 | | // Get precision from argument... |
252 | 0 | format ++; |
253 | 0 | prec = va_arg(ap, int); |
254 | |
|
255 | 0 | snprintf(tptr, sizeof(tformat) - (size_t)(tptr - tformat), "%d", prec); |
256 | 0 | tptr += strlen(tptr); |
257 | 0 | } |
258 | 0 | else |
259 | 0 | { |
260 | 0 | prec = 0; |
261 | |
|
262 | 0 | while (_cups_isdigit(*format)) |
263 | 0 | { |
264 | 0 | if (tptr < (tformat + sizeof(tformat) - 1)) |
265 | 0 | *tptr++ = *format; |
266 | |
|
267 | 0 | prec = prec * 10 + *format++ - '0'; |
268 | 0 | } |
269 | 0 | } |
270 | 0 | } |
271 | |
|
272 | 0 | if (*format == 'l' && format[1] == 'l') |
273 | 0 | { |
274 | 0 | size = 'L'; |
275 | |
|
276 | 0 | if (tptr < (tformat + sizeof(tformat) - 2)) |
277 | 0 | { |
278 | 0 | *tptr++ = 'l'; |
279 | 0 | *tptr++ = 'l'; |
280 | 0 | } |
281 | |
|
282 | 0 | format += 2; |
283 | 0 | } |
284 | 0 | else if (*format == 'h' || *format == 'l' || *format == 'L') |
285 | 0 | { |
286 | 0 | if (tptr < (tformat + sizeof(tformat) - 1)) |
287 | 0 | *tptr++ = *format; |
288 | |
|
289 | 0 | size = *format++; |
290 | 0 | } |
291 | 0 | else |
292 | 0 | { |
293 | 0 | size = 0; |
294 | 0 | } |
295 | |
|
296 | 0 | if (!*format) |
297 | 0 | break; |
298 | | |
299 | 0 | if (tptr < (tformat + sizeof(tformat) - 1)) |
300 | 0 | *tptr++ = *format; |
301 | |
|
302 | 0 | type = *format++; |
303 | 0 | *tptr = '\0'; |
304 | |
|
305 | 0 | switch (type) |
306 | 0 | { |
307 | 0 | case 'E' : // Floating point formats |
308 | 0 | case 'G' : |
309 | 0 | case 'e' : |
310 | 0 | case 'f' : |
311 | 0 | case 'g' : |
312 | 0 | if ((size_t)(width + 2) > sizeof(temp)) |
313 | 0 | break; |
314 | | |
315 | 0 | snprintf(temp, sizeof(temp), tformat, va_arg(ap, double)); |
316 | |
|
317 | 0 | bytes += (int)strlen(temp); |
318 | |
|
319 | 0 | if (bufptr < bufend) |
320 | 0 | { |
321 | 0 | cupsCopyString(bufptr, temp, (size_t)(bufend - bufptr)); |
322 | 0 | bufptr += strlen(bufptr); |
323 | 0 | } |
324 | 0 | break; |
325 | | |
326 | 0 | case 'B' : // Integer formats |
327 | 0 | case 'X' : |
328 | 0 | case 'b' : |
329 | 0 | case 'd' : |
330 | 0 | case 'i' : |
331 | 0 | case 'o' : |
332 | 0 | case 'u' : |
333 | 0 | case 'x' : |
334 | 0 | if ((size_t)(width + 2) > sizeof(temp)) |
335 | 0 | break; |
336 | | |
337 | 0 | # ifdef HAVE_LONG_LONG |
338 | 0 | if (size == 'L') |
339 | 0 | snprintf(temp, sizeof(temp), tformat, va_arg(ap, long long)); |
340 | 0 | else |
341 | 0 | # endif // HAVE_LONG_LONG |
342 | 0 | if (size == 'l') |
343 | 0 | snprintf(temp, sizeof(temp), tformat, va_arg(ap, long)); |
344 | 0 | else |
345 | 0 | snprintf(temp, sizeof(temp), tformat, va_arg(ap, int)); |
346 | |
|
347 | 0 | bytes += (int)strlen(temp); |
348 | |
|
349 | 0 | if (bufptr < bufend) |
350 | 0 | { |
351 | 0 | cupsCopyString(bufptr, temp, (size_t)(bufend - bufptr)); |
352 | 0 | bufptr += strlen(bufptr); |
353 | 0 | } |
354 | 0 | break; |
355 | | |
356 | 0 | case 'p' : // Pointer value |
357 | 0 | if ((size_t)(width + 2) > sizeof(temp)) |
358 | 0 | break; |
359 | | |
360 | 0 | snprintf(temp, sizeof(temp), tformat, va_arg(ap, void *)); |
361 | |
|
362 | 0 | bytes += (int)strlen(temp); |
363 | |
|
364 | 0 | if (bufptr < bufend) |
365 | 0 | { |
366 | 0 | cupsCopyString(bufptr, temp, (size_t)(bufend - bufptr)); |
367 | 0 | bufptr += strlen(bufptr); |
368 | 0 | } |
369 | 0 | break; |
370 | | |
371 | 0 | case 'c' : // Character or character array |
372 | 0 | bytes += width; |
373 | |
|
374 | 0 | if (bufptr < bufend) |
375 | 0 | { |
376 | 0 | if (width <= 1) |
377 | 0 | { |
378 | 0 | *bufptr++ = (char)va_arg(ap, int); |
379 | 0 | } |
380 | 0 | else |
381 | 0 | { |
382 | 0 | if ((bufptr + width) > bufend) |
383 | 0 | width = (int)(bufend - bufptr); |
384 | |
|
385 | 0 | memcpy(bufptr, va_arg(ap, char *), (size_t)width); |
386 | 0 | bufptr += width; |
387 | 0 | } |
388 | 0 | } |
389 | 0 | else if (width <= 1) |
390 | 0 | { |
391 | | // Consume character from parameters... |
392 | 0 | va_arg(ap, int); |
393 | 0 | } |
394 | 0 | else |
395 | 0 | { |
396 | | // Consume character pointer from parameters... |
397 | 0 | va_arg(ap, char *); |
398 | 0 | } |
399 | 0 | break; |
400 | | |
401 | 0 | case 's' : // String |
402 | 0 | if ((s = va_arg(ap, char *)) == NULL) |
403 | 0 | s = "(null)"; |
404 | | |
405 | | // Copy the C string, replacing control chars and \ with C character escapes... |
406 | 0 | for (; *s && bufptr < bufend; s ++) |
407 | 0 | { |
408 | 0 | if (*s == '\n') |
409 | 0 | { |
410 | 0 | *bufptr++ = '\\'; |
411 | 0 | if (bufptr < bufend) |
412 | 0 | *bufptr++ = 'n'; |
413 | 0 | bytes += 2; |
414 | 0 | } |
415 | 0 | else if (*s == '\r') |
416 | 0 | { |
417 | 0 | *bufptr++ = '\\'; |
418 | 0 | if (bufptr < bufend) |
419 | 0 | *bufptr++ = 'r'; |
420 | 0 | bytes += 2; |
421 | 0 | } |
422 | 0 | else if (*s == '\t') |
423 | 0 | { |
424 | 0 | *bufptr++ = '\\'; |
425 | 0 | if (bufptr < bufend) |
426 | 0 | *bufptr++ = 't'; |
427 | 0 | bytes += 2; |
428 | 0 | } |
429 | 0 | else if (*s == '\\') |
430 | 0 | { |
431 | 0 | *bufptr++ = '\\'; |
432 | 0 | if (bufptr < bufend) |
433 | 0 | *bufptr++ = '\\'; |
434 | 0 | bytes += 2; |
435 | 0 | } |
436 | 0 | else if (*s == '\'') |
437 | 0 | { |
438 | 0 | *bufptr++ = '\\'; |
439 | 0 | if (bufptr < bufend) |
440 | 0 | *bufptr++ = '\''; |
441 | 0 | bytes += 2; |
442 | 0 | } |
443 | 0 | else if (*s == '\"') |
444 | 0 | { |
445 | 0 | *bufptr++ = '\\'; |
446 | 0 | if (bufptr < bufend) |
447 | 0 | *bufptr++ = '\"'; |
448 | 0 | bytes += 2; |
449 | 0 | } |
450 | 0 | else if (_cups_iscntrl(*s)) |
451 | 0 | { |
452 | 0 | *bufptr++ = '\\'; |
453 | 0 | if (bufptr < bufend) |
454 | 0 | *bufptr++ = '0'; |
455 | 0 | if (bufptr < bufend) |
456 | 0 | *bufptr++ = '0' + *s / 8; |
457 | 0 | if (bufptr < bufend) |
458 | 0 | *bufptr++ = '0' + (*s & 7); |
459 | 0 | bytes += 4; |
460 | 0 | } |
461 | 0 | else |
462 | 0 | { |
463 | 0 | *bufptr++ = *s; |
464 | 0 | bytes ++; |
465 | 0 | } |
466 | 0 | } |
467 | |
|
468 | 0 | if (bufptr >= bufend) |
469 | 0 | bytes += 2 * strlen(s); |
470 | 0 | break; |
471 | | |
472 | 0 | case 'n' : // Output number of chars so far |
473 | 0 | *(va_arg(ap, int *)) = (int)bytes; |
474 | 0 | break; |
475 | 0 | } |
476 | 0 | } |
477 | 0 | else |
478 | 0 | { |
479 | | // Literal character... |
480 | 0 | bytes ++; |
481 | |
|
482 | 0 | if (bufptr < bufend) |
483 | 0 | *bufptr++ = *format++; |
484 | 0 | else |
485 | 0 | format ++; |
486 | 0 | } |
487 | 0 | } |
488 | | |
489 | | // Nul-terminate the string and return the number of characters needed. |
490 | 0 | if (bufptr < bufend) |
491 | 0 | { |
492 | | // Everything fit in the buffer... |
493 | 0 | *bufptr = '\0'; |
494 | 0 | } |
495 | 0 | else |
496 | 0 | { |
497 | | // Make sure the last characters are valid UTF-8... |
498 | 0 | *bufend = '\0'; |
499 | |
|
500 | 0 | validate_end(buffer, bufend); |
501 | 0 | } |
502 | |
|
503 | 0 | return (bytes); |
504 | 0 | } |
505 | | |
506 | | |
507 | | // |
508 | | // '_cupsStrAlloc()' - Allocate/reference a string. |
509 | | // |
510 | | |
511 | | char * // O - String pointer |
512 | | _cupsStrAlloc(const char *s) // I - String |
513 | 5.90k | { |
514 | 5.90k | size_t slen; // Length of string |
515 | 5.90k | _cups_sp_item_t *item, // String pool item |
516 | 5.90k | *key; // Search key |
517 | | |
518 | | |
519 | | // Range check input... |
520 | 5.90k | if (!s) |
521 | 0 | return (NULL); |
522 | | |
523 | | // Get the string pool... |
524 | 5.90k | cupsMutexLock(&sp_mutex); |
525 | | |
526 | 5.90k | if (!stringpool) |
527 | 1 | stringpool = cupsArrayNew((cups_array_cb_t)compare_sp_items, NULL, NULL, 0, NULL, NULL); |
528 | | |
529 | 5.90k | if (!stringpool) |
530 | 0 | { |
531 | 0 | cupsMutexUnlock(&sp_mutex); |
532 | |
|
533 | 0 | return (NULL); |
534 | 0 | } |
535 | | |
536 | | // See if the string is already in the pool... |
537 | 5.90k | key = (_cups_sp_item_t *)(s - offsetof(_cups_sp_item_t, str)); |
538 | | |
539 | 5.90k | if ((item = (_cups_sp_item_t *)cupsArrayFind(stringpool, key)) != NULL) |
540 | 1.71k | { |
541 | | // Found it, return the cached string... |
542 | 1.71k | item->ref_count ++; |
543 | | |
544 | | #ifdef DEBUG_GUARDS |
545 | | DEBUG_printf("5_cupsStrAlloc: Using string %p(%s) for \"%s\", guard=%08x, ref_count=%d", item, item->str, s, item->guard, item->ref_count); |
546 | | |
547 | | if (item->guard != _CUPS_STR_GUARD) |
548 | | abort(); |
549 | | #endif // DEBUG_GUARDS |
550 | | |
551 | 1.71k | cupsMutexUnlock(&sp_mutex); |
552 | | |
553 | 1.71k | return (item->str); |
554 | 1.71k | } |
555 | | |
556 | | // Not found, so allocate a new one... |
557 | 4.19k | slen = strlen(s); |
558 | 4.19k | item = (_cups_sp_item_t *)calloc(1, sizeof(_cups_sp_item_t) + slen); |
559 | 4.19k | if (!item) |
560 | 0 | { |
561 | 0 | cupsMutexUnlock(&sp_mutex); |
562 | |
|
563 | 0 | return (NULL); |
564 | 0 | } |
565 | | |
566 | 4.19k | item->ref_count = 1; |
567 | 4.19k | memcpy(item->str, s, slen + 1); |
568 | | |
569 | | #ifdef DEBUG_GUARDS |
570 | | item->guard = _CUPS_STR_GUARD; |
571 | | |
572 | | DEBUG_printf("5_cupsStrAlloc: Created string %p(%s) for \"%s\", guard=%08x, ref_count=%d", item, item->str, s, item->guard, item->ref_count); |
573 | | #endif // DEBUG_GUARDS |
574 | | |
575 | | // Add the string to the pool and return it... |
576 | 4.19k | cupsArrayAdd(stringpool, item); |
577 | | |
578 | 4.19k | cupsMutexUnlock(&sp_mutex); |
579 | | |
580 | 4.19k | return (item->str); |
581 | 4.19k | } |
582 | | |
583 | | |
584 | | // |
585 | | // '_cupsStrFlush()' - Flush the string pool. |
586 | | // |
587 | | |
588 | | void |
589 | | _cupsStrFlush(void) |
590 | 0 | { |
591 | 0 | _cups_sp_item_t *item; // Current item |
592 | | |
593 | |
|
594 | 0 | DEBUG_printf("4_cupsStrFlush: %u strings in array", (unsigned)cupsArrayGetCount(stringpool)); |
595 | |
|
596 | 0 | cupsMutexLock(&sp_mutex); |
597 | |
|
598 | 0 | for (item = (_cups_sp_item_t *)cupsArrayGetFirst(stringpool); item; item = (_cups_sp_item_t *)cupsArrayGetNext(stringpool)) |
599 | 0 | free(item); |
600 | |
|
601 | 0 | cupsArrayDelete(stringpool); |
602 | 0 | stringpool = NULL; |
603 | |
|
604 | 0 | cupsMutexUnlock(&sp_mutex); |
605 | 0 | } |
606 | | |
607 | | |
608 | | // |
609 | | // '_cupsStrFormatd()' - Format a floating-point number. |
610 | | // |
611 | | |
612 | | char * // O - Pointer to end of string |
613 | | _cupsStrFormatd(char *buf, // I - String |
614 | | char *bufend, // I - End of string buffer |
615 | | double number, // I - Number to format |
616 | | struct lconv *loc) // I - Locale data |
617 | 0 | { |
618 | 0 | char *bufptr, // Pointer into buffer |
619 | 0 | temp[1024], // Temporary string |
620 | 0 | *tempdec, // Pointer to decimal point |
621 | 0 | *tempptr; // Pointer into temporary string |
622 | 0 | const char *dec; // Decimal point |
623 | 0 | int declen; // Length of decimal point |
624 | | |
625 | | |
626 | | // Format the number using the "%.12f" format and then eliminate unnecessary trailing 0's. |
627 | 0 | snprintf(temp, sizeof(temp), "%.12f", number); |
628 | 0 | tempptr = temp + strlen(temp) - 1; |
629 | 0 | while (tempptr > temp && *tempptr == '0') |
630 | 0 | *tempptr-- = '\0'; |
631 | | |
632 | | // Next, find the decimal point... |
633 | 0 | if (loc && loc->decimal_point) |
634 | 0 | { |
635 | 0 | dec = loc->decimal_point; |
636 | 0 | declen = (int)strlen(dec); |
637 | 0 | } |
638 | 0 | else |
639 | 0 | { |
640 | 0 | dec = "."; |
641 | 0 | declen = 1; |
642 | 0 | } |
643 | |
|
644 | 0 | if (declen == 1) |
645 | 0 | tempdec = (char *)strchr(temp, *dec); |
646 | 0 | else |
647 | 0 | tempdec = (char *)strstr(temp, dec); |
648 | | |
649 | | // Copy everything up to the decimal point... |
650 | 0 | if (tempdec) |
651 | 0 | { |
652 | 0 | tempptr = temp; |
653 | 0 | bufptr = buf; |
654 | 0 | while (tempptr < tempdec && bufptr < bufend) |
655 | 0 | { |
656 | 0 | *bufptr++ = *tempptr++; |
657 | 0 | } |
658 | |
|
659 | 0 | tempptr += declen; |
660 | |
|
661 | 0 | if (*tempptr && bufptr < bufend) |
662 | 0 | { |
663 | 0 | *bufptr++ = '.'; |
664 | |
|
665 | 0 | while (*tempptr && bufptr < bufend) |
666 | 0 | *bufptr++ = *tempptr++; |
667 | 0 | } |
668 | |
|
669 | 0 | *bufptr = '\0'; |
670 | 0 | } |
671 | 0 | else |
672 | 0 | { |
673 | 0 | cupsCopyString(buf, temp, (size_t)(bufend - buf + 1)); |
674 | 0 | bufptr = buf + strlen(buf); |
675 | 0 | } |
676 | |
|
677 | 0 | return (bufptr); |
678 | 0 | } |
679 | | |
680 | | |
681 | | // |
682 | | // '_cupsStrFree()' - Free/dereference a string. |
683 | | // |
684 | | |
685 | | void |
686 | | _cupsStrFree(const char *s) // I - String to free |
687 | 866 | { |
688 | 866 | _cups_sp_item_t *item, // String pool item |
689 | 866 | *key; // Search key |
690 | | |
691 | | |
692 | | // Range check input... |
693 | 866 | if (!s) |
694 | 0 | return; |
695 | | |
696 | | // See if the string is already in the pool... |
697 | 866 | cupsMutexLock(&sp_mutex); |
698 | | |
699 | 866 | key = (_cups_sp_item_t *)(s - offsetof(_cups_sp_item_t, str)); |
700 | | |
701 | 866 | if ((item = (_cups_sp_item_t *)cupsArrayFind(stringpool, key)) != NULL && item == key) |
702 | 866 | { |
703 | | // Found it, dereference... |
704 | | #ifdef DEBUG_GUARDS |
705 | | if (key->guard != _CUPS_STR_GUARD) |
706 | | { |
707 | | DEBUG_printf("5_cupsStrFree: Freeing string %p(%s), guard=%08x, ref_count=%d", key, key->str, key->guard, key->ref_count); |
708 | | abort(); |
709 | | } |
710 | | #endif // DEBUG_GUARDS |
711 | | |
712 | 866 | item->ref_count --; |
713 | | |
714 | 866 | if (!item->ref_count) |
715 | 4 | { |
716 | | // Remove and free... |
717 | 4 | cupsArrayRemove(stringpool, item); |
718 | | |
719 | 4 | free(item); |
720 | 4 | } |
721 | 866 | } |
722 | | |
723 | 866 | cupsMutexUnlock(&sp_mutex); |
724 | 866 | } |
725 | | |
726 | | |
727 | | // |
728 | | // '_cupsStrRetain()' - Increment the reference count of a string. |
729 | | // |
730 | | // Note: This function does not verify that the passed pointer is in the |
731 | | // string pool, so any calls to it MUST know they are passing in a |
732 | | // good pointer. |
733 | | // |
734 | | |
735 | | char * // O - Pointer to string |
736 | | _cupsStrRetain(const char *s) // I - String to retain |
737 | 0 | { |
738 | 0 | _cups_sp_item_t *item; // Pointer to string pool item |
739 | | |
740 | |
|
741 | 0 | if (s) |
742 | 0 | { |
743 | 0 | item = (_cups_sp_item_t *)(s - offsetof(_cups_sp_item_t, str)); |
744 | |
|
745 | 0 | cupsMutexLock(&sp_mutex); |
746 | |
|
747 | | #ifdef DEBUG_GUARDS |
748 | | if (item->guard != _CUPS_STR_GUARD) |
749 | | { |
750 | | DEBUG_printf("5_cupsStrRetain: Retaining string %p(%s), guard=%08x, ref_count=%d", item, s, item->guard, item->ref_count); |
751 | | abort(); |
752 | | } |
753 | | #endif // DEBUG_GUARDS |
754 | |
|
755 | 0 | item->ref_count ++; |
756 | |
|
757 | 0 | cupsMutexUnlock(&sp_mutex); |
758 | 0 | } |
759 | |
|
760 | 0 | return ((char *)s); |
761 | 0 | } |
762 | | |
763 | | |
764 | | // |
765 | | // '_cupsStrScand()' - Scan a string for a floating-point number. |
766 | | // |
767 | | // This function handles the locale-specific BS so that a decimal |
768 | | // point is always the period (".")... |
769 | | // |
770 | | |
771 | | double // O - Number |
772 | | _cupsStrScand(const char *buf, // I - Pointer to number |
773 | | char **bufptr, // O - New pointer or NULL on error |
774 | | struct lconv *loc) // I - Locale data |
775 | 3.45k | { |
776 | 3.45k | char temp[1024], // Temporary buffer |
777 | 3.45k | *tempptr; // Pointer into temporary buffer |
778 | | |
779 | | |
780 | | // Range check input... |
781 | 3.45k | if (!buf) |
782 | 0 | return (0.0); |
783 | | |
784 | | // Skip leading whitespace... |
785 | 4.92k | while (_cups_isspace(*buf)) |
786 | 1.47k | buf ++; |
787 | | |
788 | | // Copy leading sign, numbers, period, and then numbers... |
789 | 3.45k | tempptr = temp; |
790 | 3.45k | if (*buf == '-' || *buf == '+') |
791 | 840 | *tempptr++ = *buf++; |
792 | | |
793 | 4.98k | while (_cups_isdigit(*buf)) |
794 | 1.53k | { |
795 | 1.53k | if (tempptr < (temp + sizeof(temp) - 1)) |
796 | 1.53k | { |
797 | 1.53k | *tempptr++ = *buf++; |
798 | 1.53k | } |
799 | 0 | else |
800 | 0 | { |
801 | 0 | if (bufptr) |
802 | 0 | *bufptr = NULL; |
803 | |
|
804 | 0 | return (0.0); |
805 | 0 | } |
806 | 1.53k | } |
807 | | |
808 | 3.45k | if (*buf == '.') |
809 | 1.09k | { |
810 | | // Read fractional portion of number... |
811 | 1.09k | buf ++; |
812 | | |
813 | 1.09k | if (loc && loc->decimal_point) |
814 | 1.09k | { |
815 | 1.09k | cupsCopyString(tempptr, loc->decimal_point, sizeof(temp) - (size_t)(tempptr - temp)); |
816 | 1.09k | tempptr += strlen(tempptr); |
817 | 1.09k | } |
818 | 0 | else if (tempptr < (temp + sizeof(temp) - 1)) |
819 | 0 | { |
820 | 0 | *tempptr++ = '.'; |
821 | 0 | } |
822 | 0 | else |
823 | 0 | { |
824 | 0 | if (bufptr) |
825 | 0 | *bufptr = NULL; |
826 | |
|
827 | 0 | return (0.0); |
828 | 0 | } |
829 | | |
830 | 1.78k | while (_cups_isdigit(*buf)) |
831 | 692 | { |
832 | 692 | if (tempptr < (temp + sizeof(temp) - 1)) |
833 | 692 | { |
834 | 692 | *tempptr++ = *buf++; |
835 | 692 | } |
836 | 0 | else |
837 | 0 | { |
838 | 0 | if (bufptr) |
839 | 0 | *bufptr = NULL; |
840 | |
|
841 | 0 | return (0.0); |
842 | 0 | } |
843 | 692 | } |
844 | 1.09k | } |
845 | | |
846 | 3.45k | if (*buf == 'e' || *buf == 'E') |
847 | 1.37k | { |
848 | | // Read exponent... |
849 | 1.37k | if (tempptr < (temp + sizeof(temp) - 1)) |
850 | 1.37k | { |
851 | 1.37k | *tempptr++ = *buf++; |
852 | 1.37k | } |
853 | 0 | else |
854 | 0 | { |
855 | 0 | if (bufptr) |
856 | 0 | *bufptr = NULL; |
857 | |
|
858 | 0 | return (0.0); |
859 | 0 | } |
860 | | |
861 | 1.37k | if (*buf == '+' || *buf == '-') |
862 | 500 | { |
863 | 500 | if (tempptr < (temp + sizeof(temp) - 1)) |
864 | 500 | { |
865 | 500 | *tempptr++ = *buf++; |
866 | 500 | } |
867 | 0 | else |
868 | 0 | { |
869 | 0 | if (bufptr) |
870 | 0 | *bufptr = NULL; |
871 | |
|
872 | 0 | return (0.0); |
873 | 0 | } |
874 | 500 | } |
875 | | |
876 | 1.96k | while (_cups_isdigit(*buf)) |
877 | 594 | { |
878 | 594 | if (tempptr < (temp + sizeof(temp) - 1)) |
879 | 594 | { |
880 | 594 | *tempptr++ = *buf++; |
881 | 594 | } |
882 | 0 | else |
883 | 0 | { |
884 | 0 | if (bufptr) |
885 | 0 | *bufptr = NULL; |
886 | |
|
887 | 0 | return (0.0); |
888 | 0 | } |
889 | 594 | } |
890 | 1.37k | } |
891 | | |
892 | | // Nul-terminate the temporary string and return the value... |
893 | 3.45k | if (bufptr) |
894 | 0 | *bufptr = (char *)buf; |
895 | | |
896 | 3.45k | *tempptr = '\0'; |
897 | | |
898 | 3.45k | return (strtod(temp, NULL)); |
899 | 3.45k | } |
900 | | |
901 | | |
902 | | // |
903 | | // '_cupsStrStatistics()' - Return allocation statistics for string pool. |
904 | | // |
905 | | |
906 | | size_t // O - Number of strings |
907 | | _cupsStrStatistics(size_t *alloc_bytes, // O - Allocated bytes |
908 | | size_t *total_bytes) // O - Total string bytes |
909 | 0 | { |
910 | 0 | size_t count, // Number of strings |
911 | 0 | abytes, // Allocated string bytes |
912 | 0 | tbytes, // Total string bytes |
913 | 0 | len; // Length of string |
914 | 0 | _cups_sp_item_t *item; // Current item |
915 | | |
916 | | |
917 | | // Loop through strings in pool, counting everything up... |
918 | 0 | cupsMutexLock(&sp_mutex); |
919 | |
|
920 | 0 | for (count = 0, abytes = 0, tbytes = 0, item = (_cups_sp_item_t *)cupsArrayGetFirst(stringpool); item; item = (_cups_sp_item_t *)cupsArrayGetNext(stringpool)) |
921 | 0 | { |
922 | | // Count allocated memory, using a 64-bit aligned buffer as a basis. |
923 | 0 | count += item->ref_count; |
924 | 0 | len = (strlen(item->str) + 8) & (size_t)~7; |
925 | 0 | abytes += sizeof(_cups_sp_item_t) + len; |
926 | 0 | tbytes += item->ref_count * len; |
927 | 0 | } |
928 | |
|
929 | 0 | cupsMutexUnlock(&sp_mutex); |
930 | | |
931 | | // Return values... |
932 | 0 | if (alloc_bytes) |
933 | 0 | *alloc_bytes = abytes; |
934 | |
|
935 | 0 | if (total_bytes) |
936 | 0 | *total_bytes = tbytes; |
937 | |
|
938 | 0 | return (count); |
939 | 0 | } |
940 | | |
941 | | |
942 | | // |
943 | | // '_cups_strcpy()' - Copy a string allowing for overlapping strings. |
944 | | // |
945 | | |
946 | | void |
947 | | _cups_strcpy(char *dst, // I - Destination string |
948 | | const char *src) // I - Source string |
949 | 0 | { |
950 | 0 | while (*src) |
951 | 0 | *dst++ = *src++; |
952 | |
|
953 | 0 | *dst = '\0'; |
954 | 0 | } |
955 | | |
956 | | |
957 | | // |
958 | | // '_cups_strcasecmp()' - Do a case-insensitive comparison. |
959 | | // |
960 | | |
961 | | int // O - Result of comparison (-1, 0, or 1) |
962 | | _cups_strcasecmp(const char *s, // I - First string |
963 | | const char *t) // I - Second string |
964 | 1.04M | { |
965 | 1.59M | while (*s != '\0' && *t != '\0') |
966 | 1.44M | { |
967 | 1.44M | if (_cups_tolower(*s) < _cups_tolower(*t)) |
968 | 101k | return (-1); |
969 | 1.34M | else if (_cups_tolower(*s) > _cups_tolower(*t)) |
970 | 796k | return (1); |
971 | | |
972 | 552k | s ++; |
973 | 552k | t ++; |
974 | 552k | } |
975 | | |
976 | 146k | if (*s == '\0' && *t == '\0') |
977 | 40.4k | return (0); |
978 | 106k | else if (*s != '\0') |
979 | 1.85k | return (1); |
980 | 104k | else |
981 | 104k | return (-1); |
982 | 146k | } |
983 | | |
984 | | // |
985 | | // '_cups_strncasecmp()' - Do a case-insensitive comparison on up to N chars. |
986 | | // |
987 | | |
988 | | int // O - Result of comparison (-1, 0, or 1) |
989 | | _cups_strncasecmp(const char *s, // I - First string |
990 | | const char *t, // I - Second string |
991 | | size_t n) // I - Maximum number of characters to compare |
992 | 4.85k | { |
993 | 16.3k | while (*s != '\0' && *t != '\0' && n > 0) |
994 | 14.3k | { |
995 | 14.3k | if (_cups_tolower(*s) < _cups_tolower(*t)) |
996 | 121 | return (-1); |
997 | 14.2k | else if (_cups_tolower(*s) > _cups_tolower(*t)) |
998 | 2.82k | return (1); |
999 | | |
1000 | 11.4k | s ++; |
1001 | 11.4k | t ++; |
1002 | 11.4k | n --; |
1003 | 11.4k | } |
1004 | | |
1005 | 1.90k | if (n == 0) |
1006 | 1.90k | return (0); |
1007 | 0 | else if (*s == '\0' && *t == '\0') |
1008 | 0 | return (0); |
1009 | 0 | else if (*s != '\0') |
1010 | 0 | return (1); |
1011 | 0 | else |
1012 | 0 | return (-1); |
1013 | 1.90k | } |
1014 | | |
1015 | | |
1016 | | // |
1017 | | // 'compare_sp_items()' - Compare two string pool items... |
1018 | | // |
1019 | | |
1020 | | static int // O - Result of comparison |
1021 | | compare_sp_items(_cups_sp_item_t *a, // I - First item |
1022 | | _cups_sp_item_t *b) // I - Second item |
1023 | 109k | { |
1024 | 109k | return (strcmp(a->str, b->str)); |
1025 | 109k | } |
1026 | | |
1027 | | |
1028 | | // |
1029 | | // 'validate_end()' - Validate the last UTF-8 character in a buffer. |
1030 | | // |
1031 | | |
1032 | | static void |
1033 | | validate_end(char *s, // I - Pointer to start of string |
1034 | | char *end) // I - Pointer to end of string |
1035 | 1.86k | { |
1036 | 1.86k | char *ptr = end - 1; // Pointer into string |
1037 | | |
1038 | | |
1039 | 1.86k | if (ptr > s && *ptr & 0x80) |
1040 | 1.64k | { |
1041 | 15.8k | while ((*ptr & 0xc0) == 0x80 && ptr > s) |
1042 | 14.1k | ptr --; |
1043 | | |
1044 | 1.64k | if ((*ptr & 0xe0) == 0xc0) |
1045 | 405 | { |
1046 | | // Verify 2-byte UTF-8 sequence... |
1047 | 405 | if ((end - ptr) != 2) |
1048 | 211 | *ptr = '\0'; |
1049 | 405 | } |
1050 | 1.23k | else if ((*ptr & 0xf0) == 0xe0) |
1051 | 423 | { |
1052 | | // Verify 3-byte UTF-8 sequence... |
1053 | 423 | if ((end - ptr) != 3) |
1054 | 229 | *ptr = '\0'; |
1055 | 423 | } |
1056 | 815 | else if ((*ptr & 0xf8) == 0xf0) |
1057 | 396 | { |
1058 | | // Verify 4-byte UTF-8 sequence... |
1059 | 396 | if ((end - ptr) != 4) |
1060 | 202 | *ptr = '\0'; |
1061 | 396 | } |
1062 | 419 | else if (*ptr & 0x80) |
1063 | 217 | { |
1064 | | // Invalid sequence at end... |
1065 | 217 | *ptr = '\0'; |
1066 | 217 | } |
1067 | 1.64k | } |
1068 | 1.86k | } |