summaryrefslogtreecommitdiff
path: root/Build/source/texk/web2c/luatexdir/luapplib/ppstream.c
blob: 0e4be7b8e285ee1de4e0b83a249973775c83c6f0 (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
297
298
299
300
301
302
303
304
305
306
307
308
309
310

#include "ppfilter.h"
#include "pplib.h"

ppstream * ppstream_create (ppdoc *pdf, ppdict *dict, size_t offset)
{
	ppstream *stream;
	stream = (ppstream *)ppheap_take(&pdf->heap, sizeof(ppstream));
	stream->dict = dict;
	stream->offset = offset;
	//if (!ppdict_rget_uint(dict, "Length", &stream->length)) // may be indirect pointing PPNONE at this moment
	//  stream->length = 0;
	stream->length = 0;
	stream->input = &pdf->input;
	stream->I = NULL;
	stream->cryptkey = NULL;
	stream->flags = 0;
	return stream;
}

/* codecs */

enum {
  PPSTREAM_UNKNOWN = -1,
  PPSTREAM_BASE16 = 0,
  PPSTREAM_BASE85,
  PPSTREAM_RUNLENGTH,
  PPSTREAM_FLATE,
  PPSTREAM_LZW,
  PPSTREAM_CCITT,
  PPSTREAM_DCT,
  PPSTREAM_JBIG2,
  PPSTREAM_JPX,
  PPSTREAM_CRYPT
};

static int ppstream_codec_type (ppname name)
{ // one of those places where some hash wuld be nice..
  switch (name[0])
  {
    case 'A':
      if (ppname_is(name, "ASCIIHexDecode")) return PPSTREAM_BASE16;
      if (ppname_is(name, "ASCII85Decode")) return PPSTREAM_BASE85;
      break;
    case 'R':
      if (ppname_is(name, "RunLengthDecode")) return PPSTREAM_RUNLENGTH;
      break;
    case 'F':
      if (ppname_is(name, "FlateDecode")) return PPSTREAM_FLATE;
      break;
    case 'L':
      if (ppname_is(name, "LZWDecode")) return PPSTREAM_LZW;
      break;
    case 'D':
      if (ppname_is(name, "DCTDecode")) return PPSTREAM_DCT;
      break;
    case 'C':
      if (ppname_is(name, "CCITTFaxDecode")) return PPSTREAM_CCITT;
      if (ppname_is(name, "Crypt")) return PPSTREAM_CRYPT;
      break;
    case 'J':
      if (ppname_is(name, "JPXDecode")) return PPSTREAM_JPX;
      if (ppname_is(name, "JBIG2Decode")) return PPSTREAM_JBIG2;
      break;
  }
  return PPSTREAM_UNKNOWN;
}

static iof * ppstream_predictor (ppdict *params, iof *N)
{
  ppint predictor, rowsamples, components, samplebits;

  if (!ppdict_get_int(params, "Predictor", &predictor) || predictor <= 1)
    return N;
  if (!ppdict_get_int(params, "Columns", &rowsamples) || rowsamples == 0) // sanity, filter probably expects >0
    rowsamples = 1;;
  if (!ppdict_get_int(params, "Colors", &components) || components == 0) // ditto
    components = 1;
  if (!ppdict_get_int(params, "BitsPerComponent", &samplebits) || samplebits == 0)
    samplebits = 8;
  return iof_filter_predictor_decoder(N, (int)predictor, (int)rowsamples, (int)components, (int)samplebits);
}

static iof * ppstream_decoder (ppstream *stream, int codectype, ppdict *params, iof *N)
{
  int flags;
  iof *F, *P;
  ppint earlychange;
  ppstring cryptkey;

  switch (codectype)
  {
    case PPSTREAM_BASE16:
      return iof_filter_base16_decoder(N);
    case PPSTREAM_BASE85:
      return iof_filter_base85_decoder(N);
    case PPSTREAM_RUNLENGTH:
      return iof_filter_runlength_decoder(N);
    case PPSTREAM_FLATE:
      if ((F = iof_filter_flate_decoder(N)) != NULL)
      {
        if (params != NULL)
        {
          if ((P = ppstream_predictor(params, F)) != NULL)
            return P;
          iof_close(F);
          break;
        }
        return F;
      }
      break;
    case PPSTREAM_LZW:
      flags = LZW_DECODER_DEFAULTS;
      if (params != NULL && ppdict_get_int(params, "EarlyChange", &earlychange) && earlychange == 0) // integer, not boolean
        flags &= ~LZW_EARLY_INDEX;
      if ((F = iof_filter_lzw_decoder(N, flags)) != NULL)
      {
        if (params != NULL)
        {
          if ((P = ppstream_predictor(params, F)) != NULL)
            return P;
          iof_close(F);
          break;
        }
        return F;
      }
      break;
    case PPSTREAM_CRYPT:
      if ((cryptkey = stream->cryptkey) == NULL)
        return N; // /Identity crypt
      if (stream->flags & PPSTREAM_ENCRYPTED_AES)
        return iof_filter_aes_decoder(N, cryptkey, ppstring_size(cryptkey));
      if (stream->flags & PPSTREAM_ENCRYPTED_RC4)
        return iof_filter_rc4_decoder(N, cryptkey, ppstring_size(cryptkey));
      return NULL; // if neither AES or RC4 but cryptkey present, something went wrong; see ppstream_info()
    case PPSTREAM_CCITT:
    case PPSTREAM_DCT:
    case PPSTREAM_JBIG2:
    case PPSTREAM_JPX:
    case PPSTREAM_UNKNOWN:
      break;
  }
  return NULL;
}

#define ppstream_image(type) (type == PPSTREAM_DCT || type == PPSTREAM_JBIG2 || PPSTREAM_JPX)

#define ppstream_source(stream) iof_filter_stream_coreader((iof_file *)((stream)->input), (size_t)((stream)->offset), (size_t)((stream)->length))
#define ppstream_auxsource(filename) iof_filter_file_reader(filename)

static ppname ppstream_filter_name (ppobj *filterobj, size_t index)
{
  if (filterobj->type == PPNAME)
    return index == 0 ? filterobj->name : NULL;
  if (filterobj->type == PPARRAY)
    return pparray_get_name(filterobj->array, index);
  return NULL;
}

static ppdict * ppstream_filter_params (ppobj *paramsobj, size_t index)
{
  if (paramsobj->type == PPDICT)
    return index == 0 ? paramsobj->dict : NULL;
  if (paramsobj->type == PPARRAY)
    return pparray_rget_dict(paramsobj->array, index);
  return NULL;
}

static const char * ppstream_aux_filename (ppobj *filespec)
{ // mockup, here we should decode the string
  if (filespec->type == PPSTRING)
  {
    return (const char *)(filespec->string);
  }
  // else might be a dict - todo
  return NULL;
}

iof * ppstream_read (ppstream *stream, int decode, int all)
{
  ppdict *dict;
  iof *I, *F;
  int codectype, external, owncrypt;
  ppobj *filterobj, *paramsobj, *filespecobj;
  ppname filter;
  ppdict *params;
  size_t index;
  const char *filename;

  if (ppstream_iof(stream) != NULL)
    return NULL; // usage error

  dict = stream->dict;
  if ((filespecobj = ppdict_rget_obj(dict, "F")) != NULL)
  {
    filename = ppstream_aux_filename(filespecobj);
    I = filename != NULL ? ppstream_auxsource(filename) : NULL;
    external = 1;
  }
  else
  {
    I = ppstream_source(stream);
    external = 0;
  }
  if (I == NULL)
    return NULL;
  /* If the stream is encrypted, decipher is the first to be applied */
  owncrypt = (stream->flags & PPSTREAM_ENCRYPTED_OWN) != 0;
  if (!owncrypt)
  {
    if (stream->cryptkey != NULL)
    { /* implied global crypt */
      if ((F = ppstream_decoder(stream, PPSTREAM_CRYPT, NULL, I)) == NULL)
        goto stream_error;
      I = F;
    } /* otherwise no crypt at all or /Identity */
  }
  if (decode || owncrypt)
  {
    filterobj = ppdict_rget_obj(dict, external ? "FFilter" : "Filter");
    if (filterobj != NULL)
    {
      paramsobj = ppdict_rget_obj(dict, external ? "FDecodeParms" : "DecodeParms");
      for (index = 0, filter = ppstream_filter_name(filterobj, 0); filter != NULL; filter = ppstream_filter_name(filterobj, ++index))
      {
        params = paramsobj != NULL ? ppstream_filter_params(paramsobj, index) : NULL;
        codectype = ppstream_codec_type(filter);
        if ((F = ppstream_decoder(stream, codectype, params, I)) != NULL)
        {
          I = F;
          if (owncrypt && !decode && codectype == PPSTREAM_CRYPT)
            break; // /Crypt filter should always be first, so in practise we return decrypted but compressed
          continue;
        }
        if (!ppstream_image(codectype)) // something unexpected
          goto stream_error;
        else // just treat image data (jpeg/jbig) as the target data
          break;
      }
    }
  }
  if (all)
    iof_load(I);
  else
    iof_input(I);
  stream->I = I;
  return I;
stream_error:
  iof_close(I);
  return NULL;
}

uint8_t * ppstream_first (ppstream *stream, size_t *size, int decode)
{
  iof *I;
  if ((I = ppstream_read(stream, decode, 0)) != NULL)
  {
    *size = (size_t)iof_left(I);
    return I->pos;
  }
  *size = 0;
  return NULL;
}

uint8_t * ppstream_next (ppstream *stream, size_t *size)
{
  iof *I;
  if ((I = ppstream_iof(stream)) != NULL)
  {
    I->pos = I->end;
    if ((*size = iof_input(I)) > 0)
      return I->pos;
  }
  *size = 0;
  return NULL;
}

uint8_t * ppstream_all (ppstream *stream, size_t *size, int decode)
{
  iof *I;
  if ((I = ppstream_read(stream, decode, 1)) != NULL)
  {
    *size = (size_t)iof_left(I);
    return I->pos;
  }
  *size = 0;
  return NULL;
}

void ppstream_done (ppstream *stream)
{
  iof *I;
  if ((I = ppstream_iof(stream)) != NULL)
  {
    iof_close(I);
    stream->I = NULL;
  }
}

/* */

void ppstream_init_buffers (void)
{
	iof_filters_init();
}

void ppstream_free_buffers (void)
{
	iof_filters_free();
}