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