1 /*
2 * Raster error handling for CUPS.
3 *
4 * Copyright © 2020-2024 by OpenPrinting.
5 * Copyright © 2007-2018 by Apple Inc.
6 * Copyright © 2007 by Easy Software Products.
7 *
8 * Licensed under Apache License v2.0. See the file "LICENSE" for more
9 * information.
10 */
11
12 /*
13 * Include necessary headers...
14 */
15
16 #include "cups-private.h"
17 #include "raster-private.h"
18 #include "debug-internal.h"
19
20
21 /*
22 * '_cupsRasterAddError()' - Add an error message to the error buffer.
23 */
24
25 void
_cupsRasterAddError(const char * f,...)26 _cupsRasterAddError(const char *f, /* I - Printf-style error message */
27 ...) /* I - Additional arguments as needed */
28 {
29 _cups_globals_t *cg = _cupsGlobals();
30 /* Thread globals */
31 _cups_raster_error_t *buf = &cg->raster_error;
32 /* Error buffer */
33 va_list ap; /* Pointer to additional arguments */
34 char s[2048]; /* Message string */
35 ssize_t bytes; /* Bytes in message string */
36
37
38 DEBUG_printf(("_cupsRasterAddError(f=\"%s\", ...)", f));
39
40 va_start(ap, f);
41 bytes = vsnprintf(s, sizeof(s), f, ap);
42 va_end(ap);
43
44 if (bytes <= 0)
45 return;
46
47 DEBUG_printf(("1_cupsRasterAddError: %s", s));
48
49 bytes ++;
50
51 if ((size_t)bytes >= sizeof(s))
52 return;
53
54 if (bytes > (ssize_t)(buf->end - buf->current))
55 {
56 /*
57 * Allocate more memory...
58 */
59
60 char *temp; /* New buffer */
61 size_t size; /* Size of buffer */
62
63
64 size = (size_t)(buf->end - buf->start + 2 * bytes + 1024);
65
66 if (buf->start)
67 temp = realloc(buf->start, size);
68 else
69 temp = malloc(size);
70
71 if (!temp)
72 return;
73
74 /*
75 * Update pointers...
76 */
77
78 buf->end = temp + size;
79 buf->current = temp + (buf->current - buf->start);
80 buf->start = temp;
81 }
82
83 /*
84 * Append the message to the end of the current string...
85 */
86
87 memcpy(buf->current, s, (size_t)bytes);
88 buf->current += bytes - 1;
89 }
90
91
92 /*
93 * '_cupsRasterClearError()' - Clear the error buffer.
94 */
95
96 void
_cupsRasterClearError(void)97 _cupsRasterClearError(void)
98 {
99 _cups_globals_t *cg = _cupsGlobals();
100 /* Thread globals */
101 _cups_raster_error_t *buf = &cg->raster_error;
102 /* Error buffer */
103
104
105 buf->current = buf->start;
106
107 if (buf->start)
108 *(buf->start) = '\0';
109 }
110
111
112 /*
113 * '_cupsRasterErrorString()' - Return the last error from a raster function.
114 *
115 * If there are no recent errors, NULL is returned.
116 *
117 * @since CUPS 1.3/macOS 10.5@
118 */
119
120 const char * /* O - Last error */
_cupsRasterErrorString(void)121 _cupsRasterErrorString(void)
122 {
123 _cups_globals_t *cg = _cupsGlobals();
124 /* Thread globals */
125 _cups_raster_error_t *buf = &cg->raster_error;
126 /* Error buffer */
127
128
129 if (buf->current == buf->start)
130 return (NULL);
131 else
132 return (buf->start);
133 }
134