| 1 |
|
| 2 |
#include <stdio.h> |
| 3 |
#include <assert.h> |
| 4 |
#include "ppapi.h" |
| 5 |
|
| 6 |
/* |
| 7 |
static const char * get_file_name (const char *path) |
| 8 |
{ |
| 9 |
const char *fn, *p; |
| 10 |
for (fn = p = path; *p != '\0'; ++p) |
| 11 |
if (*p == '\\' || *p == '/') |
| 12 |
fn = p + 1; |
| 13 |
return fn; |
| 14 |
} |
| 15 |
*/ |
| 16 |
|
| 17 |
static void box_info (ppdict *pagedict, FILE *fh) |
| 18 |
{ |
| 19 |
const char *boxes[] = {"MediaBox", "CropBox", "BleedBox", "TrimBox", "ArtBox"}; |
| 20 |
pprect rect; |
| 21 |
size_t i; |
| 22 |
for (i = 0; i < sizeof(boxes) / sizeof(const char *); ++i) |
| 23 |
if (ppdict_get_box(pagedict, boxes[i], &rect)) |
| 24 |
fprintf(fh, "%%%% %s [%f %f %f %f]\n", boxes[i], rect.lx, rect.ly, rect.rx, rect.ry); |
| 25 |
} |
| 26 |
|
| 27 |
static int usage (const char *argv0) |
| 28 |
{ |
| 29 |
printf("pplib " pplib_version ", " pplib_author "\n"); |
| 30 |
printf("usage: %s file1.pdf file2.pdf ...\n", argv0); |
| 31 |
printf(" %s file.pdf -u userpassword\n", argv0); |
| 32 |
printf(" %s file.pdf -o ownerpassword\n", argv0); |
| 33 |
printf(" %s file.pdf -p bothpasswords\n", argv0); |
| 34 |
return 0; |
| 35 |
} |
| 36 |
|
| 37 |
static void log_callback (const char *message, void *alien) |
| 38 |
{ |
| 39 |
fprintf((FILE *)alien, "\nooops: %s\n", message); |
| 40 |
} |
| 41 |
|
| 42 |
static const char * get_next_argument (const char *opt, int *a, int argc, const char **argv) |
| 43 |
{ |
| 44 |
const char *next; |
| 45 |
if ((*a) + 2 < argc) |
| 46 |
{ |
| 47 |
next = argv[*a + 1]; |
| 48 |
if (strcmp(next, opt) == 0) |
| 49 |
{ |
| 50 |
*a += 2; |
| 51 |
return argv[*a]; |
| 52 |
} |
| 53 |
} |
| 54 |
return NULL; |
| 55 |
} |
| 56 |
|
| 57 |
int main (int argc, const char **argv) |
| 58 |
{ |
| 59 |
const char *filepath, *password; |
| 60 |
int a; |
| 61 |
ppdoc *pdf; |
| 62 |
ppcrypt_status cryptstatus; |
| 63 |
ppref *pageref; |
| 64 |
ppdict *pagedict; |
| 65 |
int pageno; |
| 66 |
char outname[1024]; |
| 67 |
FILE *fh; |
| 68 |
ppstream *stream; |
| 69 |
uint8_t *data; |
| 70 |
size_t size; |
| 71 |
ppcontext *context; |
| 72 |
ppobj *obj; |
| 73 |
ppname *op; |
| 74 |
size_t operators; |
| 75 |
|
| 76 |
if (argc < 2) |
| 77 |
return usage(argv[0]); |
| 78 |
ppstream_init_buffers(); |
| 79 |
pplog_callback(log_callback, stderr); |
| 80 |
context = ppcontext_new(); |
| 81 |
for (a = 1; a < argc; ++a) |
| 82 |
{ |
| 83 |
/* load */ |
| 84 |
filepath = argv[a]; |
| 85 |
printf("loading %s... ", filepath); |
| 86 |
pdf = ppdoc_load(filepath); |
| 87 |
if (pdf == NULL) |
| 88 |
{ |
| 89 |
printf("failed\n"); |
| 90 |
continue; |
| 91 |
} |
| 92 |
printf("done\n"); |
| 93 |
|
| 94 |
/* decrypt */ |
| 95 |
if ((password = get_next_argument("-u", &a, argc, argv)) != NULL) |
| 96 |
cryptstatus = ppdoc_crypt_pass(pdf, password, strlen(password), NULL, 0); |
| 97 |
else if ((password = get_next_argument("-o", &a, argc, argv)) != NULL) |
| 98 |
cryptstatus = ppdoc_crypt_pass(pdf, NULL, 0, password, strlen(password)); |
| 99 |
else if ((password = get_next_argument("-p", &a, argc, argv)) != NULL) |
| 100 |
cryptstatus = ppdoc_crypt_pass(pdf, password, strlen(password), password, strlen(password)); |
| 101 |
else |
| 102 |
cryptstatus = ppdoc_crypt_status(pdf); |
| 103 |
switch (cryptstatus) |
| 104 |
{ |
| 105 |
case PPCRYPT_NONE: |
| 106 |
break; |
| 107 |
case PPCRYPT_DONE: |
| 108 |
printf("opened with password '%s'\n", password != NULL ? password : ""); |
| 109 |
break; |
| 110 |
case PPCRYPT_PASS: |
| 111 |
printf("invalid password\n"); |
| 112 |
ppdoc_free(pdf); |
| 113 |
continue; |
| 114 |
case PPCRYPT_FAIL: |
| 115 |
printf("invalid encryption\n"); |
| 116 |
ppdoc_free(pdf); |
| 117 |
continue; |
| 118 |
} |
| 119 |
|
| 120 |
/* process */ |
| 121 |
sprintf(outname, "%s.out", filepath); |
| 122 |
fh = fopen(outname, "wb"); |
| 123 |
if (fh == NULL) |
| 124 |
{ |
| 125 |
printf("can't open %s for writing\n", outname); |
| 126 |
continue; |
| 127 |
} |
| 128 |
for (pageref = ppdoc_first_page(pdf), pageno = 1; |
| 129 |
pageref != NULL; |
| 130 |
pageref = ppdoc_next_page(pdf), ++pageno) |
| 131 |
{ |
| 132 |
pagedict = pageref->object.dict; |
| 133 |
/* decompress contents data */ |
| 134 |
fprintf(fh, "%%%% PAGE %d\n", pageno); |
| 135 |
box_info(pagedict, fh); |
| 136 |
for (stream = ppcontents_first(pagedict); |
| 137 |
stream != NULL; |
| 138 |
stream = ppcontents_next(pagedict, stream)) |
| 139 |
{ |
| 140 |
for (data = ppstream_first(stream, &size, 1); |
| 141 |
data != NULL; |
| 142 |
data = ppstream_next(stream, &size)) |
| 143 |
fwrite(data, size, 1, fh); |
| 144 |
ppstream_done(stream); |
| 145 |
} |
| 146 |
/* now parse contents */ |
| 147 |
for (stream = ppcontents_first(pagedict); |
| 148 |
stream != NULL; |
| 149 |
stream = ppcontents_next(pagedict, stream)) |
| 150 |
{ |
| 151 |
operators = 0; |
| 152 |
for (obj = ppcontents_first_op(context, stream, &size, &op); |
| 153 |
obj != NULL; |
| 154 |
obj = ppcontents_next_op(context, stream, &size, &op)) |
| 155 |
++operators; |
| 156 |
fprintf(fh, "%%%% OPERATORS count %lu\n", (unsigned long)operators); |
| 157 |
ppstream_done(stream); |
| 158 |
//obj = ppcontents_parse(context, stream, &size); |
| 159 |
//fprintf(fh, "%%%% items count %lu\n", (unsigned long)size); |
| 160 |
fprintf(fh, "\n"); |
| 161 |
} |
| 162 |
ppcontext_done(context); |
| 163 |
} |
| 164 |
fclose(fh); |
| 165 |
ppdoc_free(pdf); |
| 166 |
} |
| 167 |
ppcontext_free(context); |
| 168 |
ppstream_free_buffers(); |
| 169 |
return 0; |
| 170 |
} |