]>
git.ipfire.org Git - thirdparty/cups.git/blob - examples/ppdx.c
2 * Example code for encoding and decoding large amounts of data in a PPD file.
3 * This would typically be used in a driver to save configuration/state
4 * information that could be used by an application.
6 * Copyright 2012 by Apple Inc.
8 * Licensed under Apache License v2.0. See the file "LICENSE" for more information.
12 * Include necessary headers...
19 #include <zlib.h> /* For compression of the data */
26 #define PPDX_MAX_VALUE (PPD_MAX_LINE - PPD_MAX_NAME - 4)
27 /* Max value length with delimiters + nul */
28 #define PPDX_MAX_CHUNK (PPDX_MAX_VALUE * 3 / 4)
29 /* Max length of each chunk when Base64-encoded */
33 * 'ppdxReadData()' - Read encoded data from a ppd_file_t *.
35 * Reads chunked data in the PPD file "ppd" using the prefix "name". Returns
36 * an allocated pointer to the data (which is nul-terminated for convenience)
37 * along with the length of the data in the variable pointed to by "datasize",
38 * which can be NULL to indicate the caller doesn't need the length.
40 * Returns NULL if no data is present in the PPD with the prefix.
43 void * /* O - Data or NULL */
44 ppdxReadData(ppd_file_t
*ppd
, /* I - PPD file */
45 const char *name
, /* I - Keyword prefix */
46 size_t *datasize
) /* O - Size of data or NULL for don't care */
48 char keyword
[PPD_MAX_NAME
], /* Keyword name */
49 decoded
[PPDX_MAX_CHUNK
+ 1];
51 unsigned chunk
= 0; /* Current chunk number */
52 int len
; /* Length of current chunk */
53 ppd_attr_t
*attr
; /* Keyword/value from PPD file */
54 Bytef
*data
; /* Pointer to data */
55 size_t alloc_size
; /* Allocated size of data buffer */
56 z_stream decomp
; /* Decompressor stream */
57 int error
; /* Error/status from inflate() */
61 * Range check input...
71 * First see if there are any instances of the named keyword in the PPD...
74 snprintf(keyword
, sizeof(keyword
), "%s%04x", name
, chunk
);
75 if ((attr
= ppdFindAttr(ppd
, keyword
, NULL
)) == NULL
)
79 * Allocate some memory and start decoding...
85 memset(&decomp
, 0, sizeof(decomp
));
86 decomp
.next_out
= data
;
87 decomp
.avail_out
= 256;
94 * Grab the data from the current attribute and decode it...
97 len
= sizeof(decoded
);
98 if (!httpDecode64_2(decoded
, &len
, attr
->value
) || len
== 0)
101 // printf("chunk %04x has length %d\n", chunk, len);
104 * Decompress this chunk...
107 decomp
.next_in
= decoded
;
108 decomp
.avail_in
= len
;
112 Bytef
*temp
; /* Temporary pointer */
113 size_t temp_size
; /* Temporary allocation size */
115 // printf("Before inflate: avail_in=%d, avail_out=%d\n", decomp.avail_in,
116 // decomp.avail_out);
118 if ((error
= inflate(&decomp
, Z_NO_FLUSH
)) < Z_OK
)
120 fprintf(stderr
, "ERROR: inflate returned %d (%s)\n", error
, decomp
.msg
);
124 // printf("After inflate: avail_in=%d, avail_out=%d, error=%d\n",
125 // decomp.avail_in, decomp.avail_out, error);
127 if (decomp
.avail_out
== 0)
129 if (alloc_size
< 2048)
130 temp_size
= alloc_size
* 2;
131 else if (alloc_size
< PPDX_MAX_DATA
)
132 temp_size
= alloc_size
+ 2048;
136 if ((temp
= realloc(data
, temp_size
+ 1)) == NULL
)
142 decomp
.next_out
= temp
+ (decomp
.next_out
- data
);
143 decomp
.avail_out
= temp_size
- alloc_size
;
145 alloc_size
= temp_size
;
148 while (decomp
.avail_in
> 0);
151 snprintf(keyword
, sizeof(keyword
), "%s%04x", name
, chunk
);
153 while ((attr
= ppdFindAttr(ppd
, keyword
, NULL
)) != NULL
);
158 * Nul-terminate the data (usually a string)...
161 *(decomp
.next_out
) = '\0';
164 *datasize
= decomp
.next_out
- data
;
171 * 'ppdxWriteData()' - Writes encoded data to stderr using PPD: messages.
173 * Writes chunked data to the PPD file using PPD: messages sent to stderr for
174 * cupsd. "name" must be a valid PPD keyword string whose length is less than
175 * 37 characters to allow for chunk numbering. "data" provides a pointer to the
176 * data to be written, and "datasize" provides the length.
180 ppdxWriteData(const char *name
, /* I - Base name of keyword */
181 const void *data
, /* I - Data to write */
182 size_t datasize
) /* I - Number of bytes in data */
184 char buffer
[PPDX_MAX_CHUNK
], /* Chunk buffer */
185 encoded
[PPDX_MAX_VALUE
+ 1],
187 pair
[PPD_MAX_LINE
], /* name=value pair */
188 line
[PPDX_MAX_STATUS
], /* Line buffer */
189 *lineptr
, /* Current position in line buffer */
190 *lineend
; /* End of line buffer */
191 unsigned chunk
= 0; /* Current chunk number */
192 int len
; /* Length of current chunk */
193 z_stream comp
; /* Compressor stream */
194 int error
; /* Error/status from deflate() */
198 * Range check input...
201 if (!name
|| (!data
&& datasize
> 0) || datasize
> PPDX_MAX_DATA
)
204 strlcpy(line
, "PPD:", sizeof(line
));
206 lineend
= line
+ sizeof(line
) - 2;
211 * Compress and encode output...
214 memset(&comp
, 0, sizeof(comp
));
215 comp
.next_in
= (Bytef
*)data
;
216 comp
.avail_in
= datasize
;
218 deflateInit(&comp
, 9);
223 * Compress a chunk...
226 comp
.next_out
= buffer
;
227 comp
.avail_out
= sizeof(buffer
);
229 if ((error
= deflate(&comp
, Z_FINISH
)) < Z_OK
)
231 fprintf(stderr
, "ERROR: deflate returned %d (%s)\n", error
, comp
.msg
);
239 len
= sizeof(buffer
) - comp
.avail_out
;
240 httpEncode64_2(encoded
, sizeof(encoded
), buffer
, len
);
242 len
= (int)snprintf(pair
, sizeof(pair
), " %s%04x=%s", name
, chunk
,
245 fprintf(stderr
, "DEBUG: *%s%04x: \"%s\"\n", name
, chunk
, encoded
);
248 if ((lineptr
+ len
) >= lineend
)
257 strlcpy(lineptr
, pair
, lineend
- lineptr
);
261 * Setup for the next one...
266 while (comp
.avail_out
== 0);
272 * Write a trailing empty chunk to signal EOD...
275 len
= (int)snprintf(pair
, sizeof(pair
), " %s%04x=\"\"", name
, chunk
);
277 fprintf(stderr
, "DEBUG: *%s%04x: \"\"\n", name
, chunk
);
280 if ((lineptr
+ len
) >= lineend
)
289 strlcpy(lineptr
, pair
, lineend
- lineptr
);