summaryrefslogtreecommitdiff
path: root/Build/source/libs/pplib/pplib-src/src/pptest3.c
blob: 815ed51b6b666153effceb2f9a58367cf4b3b60c (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

#include <stdio.h>
//#include "ppapi.h"
#include "pplib.h"
#include "assert.h"

static int usage (const char *argv0)
{
  printf("pplib " pplib_version ", " pplib_author "\n");
  printf("usage: %s file1.pdf file2.pdf ...\n", argv0);
  return 0;
}

static void print_result_filter (ppstream *stream, int decode)
{
  ppstream_filter info;
  size_t i;

  ppstream_filter_info(stream, &info, decode);
  printf("  when %s: /Filter [", decode ? "uncompressed" : "compressed");
  for (i = 0; i < info.count; ++i)
    printf(" /%s", ppstream_filter_name[info.filters[i]]);
  printf(" ]");
  if (info.params != NULL)
  {
    printf(" /DecodeParms [");
    for (i = 0; i < info.count; ++i)
      printf(" %s", info.params[i] != NULL ? "<<...>>" : "null");
    printf(" ]");
  }
  printf("\n");
}

static void print_stream_info (ppref *ref, ppstream *stream)
{
  size_t length;
  printf("object %lu %lu R\n", (unsigned long)ref->number, (unsigned long)ref->version);
  if (stream->flags & PPSTREAM_FILTER)
    printf("  filtered ");
  else
    printf("  plain ");
  if (stream->flags & PPSTREAM_IMAGE)
    printf("image ");
  else
    printf("stream ");
  if (stream->flags & PPSTREAM_ENCRYPTED)
    printf("encrypted ");
  if (stream->flags & PPSTREAM_NOT_SUPPORTED)
    printf("invalid ");
  if (!ppdict_rget_uint(stream->dict, "Length", &length))
    length = 0;
  assert(stream->length == length);
  printf("length %lu (/Length %lu)\n", (unsigned long)stream->length, (unsigned long)length);
  print_result_filter(stream, 0);
  print_result_filter(stream, 1);
}

static void check_stream_chunks (ppstream *stream)
{
  size_t sum, size;
  uint8_t *data;
  const int decode[2] = {0, 1};
  int d;

  for (d = 0; d < 2; ++d)
  {
    for (sum = 0, data = ppstream_first(stream, &size, decode[d]); data != NULL; data = ppstream_next(stream, &size))
      sum += size;
    ppstream_done(stream);
    ppstream_all(stream, &size, decode[d]);
    ppstream_done(stream);
    assert(sum == size);
    printf("  %s chunks size [%lu]\n", (decode[d] ? "decoded" : "raw"), (unsigned long)size);
  }
}

#define USE_BUFFERS_POOL 1

int main (int argc, const char **argv)
{
  const char *filepath;
  int a;
  ppdoc *pdf;
  ppxref *xref;
  ppxsec *xsec;
  size_t xi;
  ppuint refnum;
  ppref *ref;

  if (argc < 2)
    return usage(argv[0]);
  if (USE_BUFFERS_POOL)
    ppstream_init_buffers();
  for (a = 1; a < argc; ++a)
  {
    filepath = argv[a];
    printf("loading %s... ", filepath);
    pdf = ppdoc_load(filepath);
    if (pdf == NULL)
    {
      printf("failed\n");
      continue;
    }
    printf("done.\n");
    for (xref = ppdoc_xref(pdf); xref != NULL; xref = ppxref_prev(xref))
    {
      for (xi = 0, xsec = xref->sects; xi < xref->size; ++xi, ++xsec)
      {
        for (refnum = xsec->first, ref = xsec->refs; refnum <= xsec->last; ++refnum, ++ref)
        {
          if (ref->object.type != PPSTREAM)
            continue;
          print_stream_info(ref, ref->object.stream);
          check_stream_chunks(ref->object.stream);
        }
      }
    }
    ppdoc_free(pdf);
  }
  if (USE_BUFFERS_POOL)
    ppstream_free_buffers();
  return 0;
}