|
9 | 9 | # maintained and developed by Artifex Software, Inc. https://artifex.com. |
10 | 10 | # ------------------------------------------------------------------------ |
11 | 11 | */ |
| 12 | + |
| 13 | + |
12 | 14 | void remove_dest_range(fz_context *ctx, pdf_document *pdf, PyObject *numbers) |
13 | 15 | { |
14 | 16 | fz_try(ctx) { |
15 | | - int i, j, pno, len, pagecount = pdf_count_pages(ctx, pdf); |
| 17 | + int i, j, pno, len; |
| 18 | + int pagecount = pdf_count_pages(ctx, pdf); |
16 | 19 | PyObject *n1 = NULL; |
17 | 20 | pdf_obj *target, *annots, *pageref, *o, *action, *dest; |
| 21 | + |
18 | 22 | for (i = 0; i < pagecount; i++) { |
19 | | - n1 = PyLong_FromLong((long) i); |
20 | | - if (PySet_Contains(numbers, n1)) { |
| 23 | + |
| 24 | + /* Create page index Python int */ |
| 25 | + n1 = PyLong_FromLong(i); |
| 26 | + if (!n1) { |
| 27 | + fz_throw(ctx, FZ_ERROR_GENERIC, "Out of memory in PyLong_FromLong"); |
| 28 | + } |
| 29 | + |
| 30 | + int contains = PySet_Contains(numbers, n1); |
| 31 | + if (contains == -1) { |
| 32 | + Py_DECREF(n1); |
| 33 | + fz_throw(ctx, FZ_ERROR_GENERIC, "PySet_Contains failed"); |
| 34 | + } |
| 35 | + |
| 36 | + if (contains) { |
21 | 37 | Py_DECREF(n1); |
22 | 38 | continue; |
23 | 39 | } |
24 | 40 | Py_DECREF(n1); |
25 | 41 |
|
26 | 42 | pageref = pdf_lookup_page_obj(ctx, pdf, i); |
27 | 43 | annots = pdf_dict_get(ctx, pageref, PDF_NAME(Annots)); |
28 | | - if (!annots) continue; |
| 44 | + if (!annots) { |
| 45 | + continue; |
| 46 | + } |
| 47 | + |
29 | 48 | len = pdf_array_len(ctx, annots); |
30 | | - for (j = len - 1; j >= 0; j -= 1) { |
| 49 | + |
| 50 | + /* Iterate backwards to safely delete array entries */ |
| 51 | + for (j = len - 1; j >= 0; j--) { |
| 52 | + |
31 | 53 | o = pdf_array_get(ctx, annots, j); |
32 | | - if (!pdf_name_eq(ctx, pdf_dict_get(ctx, o, PDF_NAME(Subtype)), PDF_NAME(Link))) { |
| 54 | + |
| 55 | + if (!pdf_name_eq(ctx, |
| 56 | + pdf_dict_get(ctx, o, PDF_NAME(Subtype)), |
| 57 | + PDF_NAME(Link))) { |
33 | 58 | continue; |
34 | 59 | } |
| 60 | + |
35 | 61 | action = pdf_dict_get(ctx, o, PDF_NAME(A)); |
36 | | - dest = pdf_dict_get(ctx, o, PDF_NAME(Dest)); |
| 62 | + dest = pdf_dict_get(ctx, o, PDF_NAME(Dest)); |
| 63 | + |
37 | 64 | if (action) { |
38 | | - if (!pdf_name_eq(ctx, pdf_dict_get(ctx, action, |
39 | | - PDF_NAME(S)), PDF_NAME(GoTo))) |
| 65 | + if (!pdf_name_eq(ctx, |
| 66 | + pdf_dict_get(ctx, action, PDF_NAME(S)), |
| 67 | + PDF_NAME(GoTo))) { |
40 | 68 | continue; |
| 69 | + } |
41 | 70 | dest = pdf_dict_get(ctx, action, PDF_NAME(D)); |
42 | 71 | } |
| 72 | + |
43 | 73 | pno = -1; |
44 | 74 | if (pdf_is_array(ctx, dest)) { |
45 | 75 | target = pdf_array_get(ctx, dest, 0); |
46 | 76 | pno = pdf_lookup_page_number(ctx, pdf, target); |
| 77 | + |
| 78 | + } else if (pdf_is_string(ctx, dest)) { |
| 79 | + fz_location loc = fz_resolve_link(ctx, &pdf->super, |
| 80 | + pdf_to_text_string(ctx, dest), |
| 81 | + NULL, NULL); |
| 82 | + pno = loc.page; |
47 | 83 | } |
48 | | - else if (pdf_is_string(ctx, dest)) { |
49 | | - fz_location location = fz_resolve_link(ctx, &pdf->super, |
50 | | - pdf_to_text_string(ctx, dest), |
51 | | - NULL, NULL); |
52 | | - pno = location.page; |
| 84 | + |
| 85 | + if (pno < 0) { |
| 86 | + continue; /* could not resolve link target */ |
53 | 87 | } |
54 | | - if (pno < 0) { // page number lookup did not work |
55 | | - continue; |
| 88 | + |
| 89 | + n1 = PyLong_FromLong(pno); |
| 90 | + if (!n1) { |
| 91 | + fz_throw(ctx, FZ_ERROR_GENERIC, "Out of memory in PyLong_FromLong"); |
| 92 | + } |
| 93 | + |
| 94 | + contains = PySet_Contains(numbers, n1); |
| 95 | + if (contains == -1) { |
| 96 | + Py_DECREF(n1); |
| 97 | + fz_throw(ctx, FZ_ERROR_GENERIC, "PySet_Contains failed"); |
56 | 98 | } |
57 | | - n1 = PyLong_FromLong((long) pno); |
58 | | - if (PySet_Contains(numbers, n1)) { |
| 99 | + |
| 100 | + if (contains) { |
59 | 101 | pdf_array_delete(ctx, annots, j); |
60 | 102 | } |
| 103 | + |
61 | 104 | Py_DECREF(n1); |
62 | 105 | } |
63 | 106 | } |
|
0 commit comments