summaryrefslogtreecommitdiff
path: root/examples/ppdx.c
blob: 0a8c1f6ec1d4cf466f9b193d6b312aff81fcb52f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
/*
 * Example code for encoding and decoding large amounts of data in a PPD file.
 * This would typically be used in a driver to save configuration/state
 * information that could be used by an application.
 *
 * Copyright 2012 by Apple Inc.
 *
 * Licensed under Apache License v2.0.  See the file "LICENSE" for more information.
 */

/*
 * Include necessary headers...
 */

#include "ppdx.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <zlib.h>			/* For compression of the data */


/*
 * Constants...
 */

#define PPDX_MAX_VALUE	(PPD_MAX_LINE - PPD_MAX_NAME - 4)
					/* Max value length with delimiters + nul */
#define PPDX_MAX_CHUNK	(PPDX_MAX_VALUE * 3 / 4)
					/* Max length of each chunk when Base64-encoded */


/*
 * 'ppdxReadData()' - Read encoded data from a ppd_file_t *.
 *
 * Reads chunked data in the PPD file "ppd" using the prefix "name".  Returns
 * an allocated pointer to the data (which is nul-terminated for convenience)
 * along with the length of the data in the variable pointed to by "datasize",
 * which can be NULL to indicate the caller doesn't need the length.
 *
 * Returns NULL if no data is present in the PPD with the prefix.
 */

void *					/* O - Data or NULL */
ppdxReadData(ppd_file_t *ppd,		/* I - PPD file */
             const char *name,		/* I - Keyword prefix */
             size_t     *datasize)	/* O - Size of data or NULL for don't care */
{
  char		keyword[PPD_MAX_NAME],	/* Keyword name */
		decoded[PPDX_MAX_CHUNK + 1];
					/* Decoded string */
  unsigned	chunk = 0;		/* Current chunk number */
  int		len;			/* Length of current chunk */
  ppd_attr_t	*attr;			/* Keyword/value from PPD file */
  Bytef		*data;			/* Pointer to data */
  size_t	alloc_size;		/* Allocated size of data buffer */
  z_stream	decomp;			/* Decompressor stream */
  int		error;			/* Error/status from inflate() */


 /*
  * Range check input...
  */

  if (datasize)
    *datasize = 0;

  if (!ppd || !name)
    return (NULL);

 /*
  * First see if there are any instances of the named keyword in the PPD...
  */

  snprintf(keyword, sizeof(keyword), "%s%04x", name, chunk);
  if ((attr = ppdFindAttr(ppd, keyword, NULL)) == NULL)
    return (NULL);

 /*
  * Allocate some memory and start decoding...
  */

  data       = malloc(257);
  alloc_size = 256;

  memset(&decomp, 0, sizeof(decomp));
  decomp.next_out  = data;
  decomp.avail_out = 256;

  inflateInit(&decomp);

  do
  {
   /*
    * Grab the data from the current attribute and decode it...
    */

    len = sizeof(decoded);
    if (!httpDecode64_2(decoded, &len, attr->value) || len == 0)
      break;

//    printf("chunk %04x has length %d\n", chunk, len);

   /*
    * Decompress this chunk...
    */

    decomp.next_in  = decoded;
    decomp.avail_in = len;

    do
    {
      Bytef	*temp;			/* Temporary pointer */
      size_t	temp_size;		/* Temporary allocation size */

//      printf("Before inflate: avail_in=%d, avail_out=%d\n", decomp.avail_in,
//             decomp.avail_out);

      if ((error = inflate(&decomp, Z_NO_FLUSH)) < Z_OK)
      {
        fprintf(stderr, "ERROR: inflate returned %d (%s)\n", error, decomp.msg);
        break;
      }

//      printf("After inflate: avail_in=%d, avail_out=%d, error=%d\n",
//             decomp.avail_in, decomp.avail_out, error);

      if (decomp.avail_out == 0)
      {
	if (alloc_size < 2048)
	  temp_size = alloc_size * 2;
	else if (alloc_size < PPDX_MAX_DATA)
	  temp_size = alloc_size + 2048;
	else
	  break;

	if ((temp = realloc(data, temp_size + 1)) == NULL)
	{
	  free(data);
	  return (NULL);
	}

	decomp.next_out  = temp + (decomp.next_out - data);
	decomp.avail_out = temp_size - alloc_size;
	data             = temp;
	alloc_size       = temp_size;
      }
    }
    while (decomp.avail_in > 0);

    chunk ++;
    snprintf(keyword, sizeof(keyword), "%s%04x", name, chunk);
  }
  while ((attr = ppdFindAttr(ppd, keyword, NULL)) != NULL);

  inflateEnd(&decomp);

 /*
  * Nul-terminate the data (usually a string)...
  */

  *(decomp.next_out) = '\0';

  if (datasize)
    *datasize = decomp.next_out - data;

  return (data);
}


/*
 * 'ppdxWriteData()' - Writes encoded data to stderr using PPD: messages.
 *
 * Writes chunked data to the PPD file using PPD: messages sent to stderr for
 * cupsd.  "name" must be a valid PPD keyword string whose length is less than
 * 37 characters to allow for chunk numbering.  "data" provides a pointer to the
 * data to be written, and "datasize" provides the length.
 */

extern void
ppdxWriteData(const char *name,		/* I - Base name of keyword */
              const void *data,		/* I - Data to write */
              size_t     datasize)	/* I - Number of bytes in data */
{
  char		buffer[PPDX_MAX_CHUNK],	/* Chunk buffer */
		encoded[PPDX_MAX_VALUE + 1],
					/* Encoded data */
		pair[PPD_MAX_LINE],	/* name=value pair */
		line[PPDX_MAX_STATUS],	/* Line buffer */
		*lineptr,		/* Current position in line buffer */
		*lineend;		/* End of line buffer */
  unsigned	chunk = 0;		/* Current chunk number */
  int		len;			/* Length of current chunk */
  z_stream	comp;			/* Compressor stream */
  int		error;			/* Error/status from deflate() */


 /*
  * Range check input...
  */

  if (!name || (!data && datasize > 0) || datasize > PPDX_MAX_DATA)
    return;

  strlcpy(line, "PPD:", sizeof(line));
  lineptr = line + 4;
  lineend = line + sizeof(line) - 2;

  if (datasize > 0)
  {
   /*
    * Compress and encode output...
    */

    memset(&comp, 0, sizeof(comp));
    comp.next_in   = (Bytef *)data;
    comp.avail_in  = datasize;

    deflateInit(&comp, 9);

    do
    {
     /*
      * Compress a chunk...
      */

      comp.next_out  = buffer;
      comp.avail_out = sizeof(buffer);

      if ((error = deflate(&comp, Z_FINISH)) < Z_OK)
      {
        fprintf(stderr, "ERROR: deflate returned %d (%s)\n", error, comp.msg);
        break;
      }

     /*
      * Write a chunk...
      */

      len = sizeof(buffer) - comp.avail_out;
      httpEncode64_2(encoded, sizeof(encoded), buffer, len);

      len = (int)snprintf(pair, sizeof(pair), " %s%04x=%s", name, chunk,
			  encoded);
#ifdef DEBUG
      fprintf(stderr, "DEBUG: *%s%04x: \"%s\"\n", name, chunk, encoded);
#endif /* DEBUG */

      if ((lineptr + len) >= lineend)
      {
	*lineptr++ = '\n';
	*lineptr   = '\0';

	fputs(line, stderr);
	lineptr = line + 4;
      }

      strlcpy(lineptr, pair, lineend - lineptr);
      lineptr += len;

     /*
      * Setup for the next one...
      */

      chunk ++;
    }
    while (comp.avail_out == 0);
  }

  deflateEnd(&comp);

 /*
  * Write a trailing empty chunk to signal EOD...
  */

  len = (int)snprintf(pair, sizeof(pair), " %s%04x=\"\"", name, chunk);
#ifdef DEBUG
  fprintf(stderr, "DEBUG: *%s%04x: \"\"\n", name, chunk);
#endif /* DEBUG */

  if ((lineptr + len) >= lineend)
  {
    *lineptr++ = '\n';
    *lineptr   = '\0';

    fputs(line, stderr);
    lineptr = line + 4;
  }

  strlcpy(lineptr, pair, lineend - lineptr);
  lineptr += len;

  *lineptr++ = '\n';
  *lineptr   = '\0';

  fputs(line, stderr);
}