]>
git.ipfire.org Git - thirdparty/cups.git/blob - examples/ppdx.c
ec93d63accab4abfb630151089e5ba7fb1172711
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 * These coded instructions, statements, and computer programs are the
9 * property of Apple Inc. and are protected by Federal copyright
10 * law. Distribution and use rights are outlined in the file "LICENSE.txt"
11 * which should have been included with this file. If this file is
12 * file is missing or damaged, see the license at "http://www.cups.org/".
14 * This file is subject to the Apple OS-Developed Software exception.
18 * Include necessary headers...
25 #include <zlib.h> /* For compression of the data */
32 #define PPDX_MAX_VALUE (PPD_MAX_LINE - PPD_MAX_NAME - 4)
33 /* Max value length with delimiters + nul */
34 #define PPDX_MAX_CHUNK (PPDX_MAX_VALUE * 3 / 4)
35 /* Max length of each chunk when Base64-encoded */
39 * 'ppdxReadData()' - Read encoded data from a ppd_file_t *.
41 * Reads chunked data in the PPD file "ppd" using the prefix "name". Returns
42 * an allocated pointer to the data (which is nul-terminated for convenience)
43 * along with the length of the data in the variable pointed to by "datasize",
44 * which can be NULL to indicate the caller doesn't need the length.
46 * Returns NULL if no data is present in the PPD with the prefix.
49 void * /* O - Data or NULL */
50 ppdxReadData(ppd_file_t
*ppd
, /* I - PPD file */
51 const char *name
, /* I - Keyword prefix */
52 size_t *datasize
) /* O - Size of data or NULL for don't care */
54 char keyword
[PPD_MAX_NAME
], /* Keyword name */
55 decoded
[PPDX_MAX_CHUNK
+ 1];
57 unsigned chunk
= 0; /* Current chunk number */
58 int len
; /* Length of current chunk */
59 ppd_attr_t
*attr
; /* Keyword/value from PPD file */
60 Bytef
*data
; /* Pointer to data */
61 size_t alloc_size
; /* Allocated size of data buffer */
62 z_stream decomp
; /* Decompressor stream */
63 int error
; /* Error/status from inflate() */
67 * Range check input...
77 * First see if there are any instances of the named keyword in the PPD...
80 snprintf(keyword
, sizeof(keyword
), "%s%04x", name
, chunk
);
81 if ((attr
= ppdFindAttr(ppd
, keyword
, NULL
)) == NULL
)
85 * Allocate some memory and start decoding...
91 memset(&decomp
, 0, sizeof(decomp
));
92 decomp
.next_out
= data
;
93 decomp
.avail_out
= 256;
100 * Grab the data from the current attribute and decode it...
103 len
= sizeof(decoded
);
104 if (!httpDecode64_2(decoded
, &len
, attr
->value
) || len
== 0)
107 // printf("chunk %04x has length %d\n", chunk, len);
110 * Decompress this chunk...
113 decomp
.next_in
= decoded
;
114 decomp
.avail_in
= len
;
118 Bytef
*temp
; /* Temporary pointer */
119 size_t temp_size
; /* Temporary allocation size */
121 // printf("Before inflate: avail_in=%d, avail_out=%d\n", decomp.avail_in,
122 // decomp.avail_out);
124 if ((error
= inflate(&decomp
, Z_NO_FLUSH
)) < Z_OK
)
126 fprintf(stderr
, "ERROR: inflate returned %d (%s)\n", error
, decomp
.msg
);
130 // printf("After inflate: avail_in=%d, avail_out=%d, error=%d\n",
131 // decomp.avail_in, decomp.avail_out, error);
133 if (decomp
.avail_out
== 0)
135 if (alloc_size
< 2048)
136 temp_size
= alloc_size
* 2;
137 else if (alloc_size
< PPDX_MAX_DATA
)
138 temp_size
= alloc_size
+ 2048;
142 if ((temp
= realloc(data
, temp_size
+ 1)) == NULL
)
148 decomp
.next_out
= temp
+ (decomp
.next_out
- data
);
149 decomp
.avail_out
= temp_size
- alloc_size
;
151 alloc_size
= temp_size
;
154 while (decomp
.avail_in
> 0);
157 snprintf(keyword
, sizeof(keyword
), "%s%04x", name
, chunk
);
159 while ((attr
= ppdFindAttr(ppd
, keyword
, NULL
)) != NULL
);
164 * Nul-terminate the data (usually a string)...
167 *(decomp
.next_out
) = '\0';
170 *datasize
= decomp
.next_out
- data
;
177 * 'ppdxWriteData()' - Writes encoded data to stderr using PPD: messages.
179 * Writes chunked data to the PPD file using PPD: messages sent to stderr for
180 * cupsd. "name" must be a valid PPD keyword string whose length is less than
181 * 37 characters to allow for chunk numbering. "data" provides a pointer to the
182 * data to be written, and "datasize" provides the length.
186 ppdxWriteData(const char *name
, /* I - Base name of keyword */
187 const void *data
, /* I - Data to write */
188 size_t datasize
) /* I - Number of bytes in data */
190 char buffer
[PPDX_MAX_CHUNK
], /* Chunk buffer */
191 encoded
[PPDX_MAX_VALUE
+ 1],
193 pair
[PPD_MAX_LINE
], /* name=value pair */
194 line
[PPDX_MAX_STATUS
], /* Line buffer */
195 *lineptr
, /* Current position in line buffer */
196 *lineend
; /* End of line buffer */
197 unsigned chunk
= 0; /* Current chunk number */
198 int len
; /* Length of current chunk */
199 z_stream comp
; /* Compressor stream */
200 int error
; /* Error/status from deflate() */
204 * Range check input...
207 if (!name
|| (!data
&& datasize
> 0) || datasize
> PPDX_MAX_DATA
)
210 strlcpy(line
, "PPD:", sizeof(line
));
212 lineend
= line
+ sizeof(line
) - 2;
217 * Compress and encode output...
220 memset(&comp
, 0, sizeof(comp
));
221 comp
.next_in
= (Bytef
*)data
;
222 comp
.avail_in
= datasize
;
224 deflateInit(&comp
, 9);
229 * Compress a chunk...
232 comp
.next_out
= buffer
;
233 comp
.avail_out
= sizeof(buffer
);
235 if ((error
= deflate(&comp
, Z_FINISH
)) < Z_OK
)
237 fprintf(stderr
, "ERROR: deflate returned %d (%s)\n", error
, comp
.msg
);
245 len
= sizeof(buffer
) - comp
.avail_out
;
246 httpEncode64_2(encoded
, sizeof(encoded
), buffer
, len
);
248 len
= (int)snprintf(pair
, sizeof(pair
), " %s%04x=%s", name
, chunk
,
251 fprintf(stderr
, "DEBUG: *%s%04x: \"%s\"\n", name
, chunk
, encoded
);
254 if ((lineptr
+ len
) >= lineend
)
263 strlcpy(lineptr
, pair
, lineend
- lineptr
);
267 * Setup for the next one...
272 while (comp
.avail_out
== 0);
278 * Write a trailing empty chunk to signal EOD...
281 len
= (int)snprintf(pair
, sizeof(pair
), " %s%04x=\"\"", name
, chunk
);
283 fprintf(stderr
, "DEBUG: *%s%04x: \"\"\n", name
, chunk
);
286 if ((lineptr
+ len
) >= lineend
)
295 strlcpy(lineptr
, pair
, lineend
- lineptr
);