pdfium-native 0.9.1 → 0.11.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -47,6 +47,54 @@ struct NamedDestinationInfo {
47
47
  float x = 0, y = 0, zoom = 0;
48
48
  };
49
49
 
50
+ // read page index, fit type and view parameters out of a resolved FPDF_DEST
51
+ inline void ReadDestination(FPDF_DOCUMENT doc, FPDF_DEST dest,
52
+ NamedDestinationInfo &info) {
53
+ info.pageIndex = FPDFDest_GetDestPageIndex(doc, dest);
54
+
55
+ unsigned long numParams = 0;
56
+ FS_FLOAT params[4] = {0, 0, 0, 0};
57
+ unsigned long view = FPDFDest_GetView(dest, &numParams, params);
58
+ info.view = DestinationViewString(view);
59
+ if (numParams > 4)
60
+ numParams = 4;
61
+ info.viewParams.assign(params, params + numParams);
62
+
63
+ FPDF_BOOL hasX = 0, hasY = 0, hasZoom = 0;
64
+ FS_FLOAT x = 0, y = 0, zoom = 0;
65
+ if (FPDFDest_GetLocationInPage(dest, &hasX, &hasY, &hasZoom, &x, &y, &zoom)) {
66
+ info.hasX = hasX != 0;
67
+ info.hasY = hasY != 0;
68
+ info.hasZoom = hasZoom != 0;
69
+ info.x = x;
70
+ info.y = y;
71
+ info.zoom = zoom;
72
+ }
73
+ }
74
+
75
+ inline Napi::Object DestinationToObject(Napi::Env env,
76
+ const NamedDestinationInfo &info) {
77
+ Napi::Object obj = Napi::Object::New(env);
78
+ SetU16(obj, "name", env, info.name);
79
+ if (info.pageIndex >= 0)
80
+ obj.Set("pageIndex", Napi::Number::New(env, info.pageIndex));
81
+ obj.Set("view", Napi::String::New(env, info.view));
82
+
83
+ Napi::Array params = Napi::Array::New(env, info.viewParams.size());
84
+ for (uint32_t j = 0; j < info.viewParams.size(); j++) {
85
+ params.Set(j, Napi::Number::New(env, info.viewParams[j]));
86
+ }
87
+ obj.Set("viewParams", params);
88
+
89
+ if (info.hasX)
90
+ obj.Set("destX", Napi::Number::New(env, info.x));
91
+ if (info.hasY)
92
+ obj.Set("destY", Napi::Number::New(env, info.y));
93
+ if (info.hasZoom)
94
+ obj.Set("destZoom", Napi::Number::New(env, info.zoom));
95
+ return obj;
96
+ }
97
+
50
98
  class GetNamedDestinationsWorker : public SafeAsyncWorker {
51
99
  public:
52
100
  GetNamedDestinationsWorker(Napi::Env env, FPDF_DOCUMENT doc,
@@ -75,6 +123,30 @@ protected:
75
123
  // FPDF_GetNamedDest uses an in/out byte length rather than the usual
76
124
  // two-pass protocol, and despite the header calling the buffer a
77
125
  // wchar_t* it is written as UTF-16LE on every platform.
126
+ // A null here is ambiguous and must NOT be reported as a failed entry
127
+ // the way the other counted listings do. FPDF_GetNamedDest has two
128
+ // lookup paths: indices below the /Names /Dests name-tree count go
129
+ // through the tree, which dereferences indirect objects, while indices
130
+ // at or above it fall through to the legacy catalog /Dests dictionary,
131
+ // which does not. FPDF_CountNamedDests counts every legacy key all the
132
+ // same, so a perfectly legal entry whose value is an indirect reference
133
+ // to a destination array is counted and then returns null -- PDF
134
+ // 32000-1 allows an indirect reference anywhere a direct object is, and
135
+ // PDFium resolves those same destinations correctly through its own
136
+ // name lookup. Surfacing them as nulls would flag healthy documents as
137
+ // damaged. The legacy boundary is the total count minus the name-tree
138
+ // count, and PDFium exposes no way to ask for the latter, so the two
139
+ // cases cannot be told apart here.
140
+ //
141
+ // Nor can such an entry be reported under its name: when the lookup
142
+ // fails, nameLen is left at 0 and nothing is written to the buffer, so
143
+ // the index API yields neither a destination nor a name. That is why
144
+ // this listing is the one place in the library that can come back
145
+ // shorter than its count. metadata.namedDestinationCount exposes the
146
+ // count so the gap is visible, and getNamedDestination(name) reaches
147
+ // these destinations through FPDF_GetNamedDestByName, which resolves
148
+ // them -- so a caller holding a name from an incoming link is not stuck
149
+ // with what this loop could not enumerate.
78
150
  long nameLen = 0;
79
151
  FPDF_DEST dest =
80
152
  FPDF_GetNamedDest(doc_, static_cast<int>(i), nullptr, &nameLen);
@@ -93,28 +165,7 @@ protected:
93
165
  }
94
166
  }
95
167
 
96
- info.pageIndex = FPDFDest_GetDestPageIndex(doc_, dest);
97
-
98
- unsigned long numParams = 0;
99
- FS_FLOAT params[4] = {0, 0, 0, 0};
100
- unsigned long view = FPDFDest_GetView(dest, &numParams, params);
101
- info.view = DestinationViewString(view);
102
- if (numParams > 4)
103
- numParams = 4;
104
- info.viewParams.assign(params, params + numParams);
105
-
106
- FPDF_BOOL hasX = 0, hasY = 0, hasZoom = 0;
107
- FS_FLOAT x = 0, y = 0, zoom = 0;
108
- if (FPDFDest_GetLocationInPage(dest, &hasX, &hasY, &hasZoom, &x, &y,
109
- &zoom)) {
110
- info.hasX = hasX != 0;
111
- info.hasY = hasY != 0;
112
- info.hasZoom = hasZoom != 0;
113
- info.x = x;
114
- info.y = y;
115
- info.zoom = zoom;
116
- }
117
-
168
+ ReadDestination(doc_, dest, info);
118
169
  destinations_.push_back(std::move(info));
119
170
  }
120
171
  }
@@ -123,27 +174,7 @@ protected:
123
174
  Napi::Env env = Env();
124
175
  Napi::Array arr = Napi::Array::New(env, destinations_.size());
125
176
  for (uint32_t i = 0; i < destinations_.size(); i++) {
126
- const auto &info = destinations_[i];
127
- Napi::Object obj = Napi::Object::New(env);
128
- SetU16(obj, "name", env, info.name);
129
- if (info.pageIndex >= 0)
130
- obj.Set("pageIndex", Napi::Number::New(env, info.pageIndex));
131
- obj.Set("view", Napi::String::New(env, info.view));
132
-
133
- Napi::Array params = Napi::Array::New(env, info.viewParams.size());
134
- for (uint32_t j = 0; j < info.viewParams.size(); j++) {
135
- params.Set(j, Napi::Number::New(env, info.viewParams[j]));
136
- }
137
- obj.Set("viewParams", params);
138
-
139
- if (info.hasX)
140
- obj.Set("destX", Napi::Number::New(env, info.x));
141
- if (info.hasY)
142
- obj.Set("destY", Napi::Number::New(env, info.y));
143
- if (info.hasZoom)
144
- obj.Set("destZoom", Napi::Number::New(env, info.zoom));
145
-
146
- arr.Set(i, obj);
177
+ arr.Set(i, DestinationToObject(env, destinations_[i]));
147
178
  }
148
179
  deferred_.Resolve(arr);
149
180
  }
@@ -158,3 +189,75 @@ private:
158
189
  std::shared_ptr<std::atomic<bool>> docAlive_;
159
190
  std::vector<NamedDestinationInfo> destinations_;
160
191
  };
192
+
193
+ // ---------------------------------------------------------------------------
194
+ // GetNamedDestinationWorker — async lookup of a single destination by name
195
+ // ---------------------------------------------------------------------------
196
+ // FPDF_GetNamedDestByName searches the /Names /Dests name tree and the legacy
197
+ // catalog /Dests dictionary alike, and dereferences an indirect value in
198
+ // either. It therefore resolves destinations the index-based listing above
199
+ // cannot reach, which is the whole reason this entry point exists: resolving
200
+ // the "#Chapter2" of an incoming link is the listing's stated use, and for a
201
+ // document whose legacy /Dests values are indirect the listing yields nothing
202
+ // to match against.
203
+ //
204
+ // The name is passed through as UTF-8. PDF name-tree keys are byte strings and
205
+ // PDFium compares them as such, so an ASCII name -- what producers emit in
206
+ // practice -- matches exactly, while a key stored in UTF-16BE or PDFDocEncoding
207
+ // will not be found by its decoded form. Resolves to null when no destination
208
+ // carries the name, which is an answer rather than an error.
209
+
210
+ class GetNamedDestinationWorker : public SafeAsyncWorker {
211
+ public:
212
+ GetNamedDestinationWorker(Napi::Env env, FPDF_DOCUMENT doc, std::string name,
213
+ std::u16string nameU16,
214
+ std::shared_ptr<std::atomic<bool>> docAlive)
215
+ : SafeAsyncWorker(env), deferred_(Napi::Promise::Deferred::New(env)),
216
+ doc_(doc), name_(std::move(name)), nameU16_(std::move(nameU16)),
217
+ docAlive_(std::move(docAlive)) {}
218
+
219
+ Napi::Promise Promise() { return deferred_.Promise(); }
220
+
221
+ protected:
222
+ void Execute() override {
223
+ std::lock_guard<std::mutex> lock(g_pdfium_mutex);
224
+ if (!docAlive_ || !docAlive_->load()) {
225
+ SetError("Document was destroyed");
226
+ return;
227
+ }
228
+
229
+ FPDF_DEST dest = FPDF_GetNamedDestByName(doc_, name_.c_str());
230
+ if (!dest)
231
+ return;
232
+
233
+ // echo the name back: the by-name path returns no name of its own, and an
234
+ // entry that matched carries exactly the one that was asked for. The
235
+ // caller's own UTF-16 string is used rather than widening the UTF-8 bytes,
236
+ // which would mangle anything outside ASCII.
237
+ info_.name = nameU16_;
238
+ ReadDestination(doc_, dest, info_);
239
+ found_ = true;
240
+ }
241
+
242
+ void OnOK() override {
243
+ Napi::Env env = Env();
244
+ if (!found_) {
245
+ deferred_.Resolve(env.Null());
246
+ return;
247
+ }
248
+ deferred_.Resolve(DestinationToObject(env, info_));
249
+ }
250
+
251
+ void OnError(const Napi::Error &err) override {
252
+ deferred_.Reject(err.Value());
253
+ }
254
+
255
+ private:
256
+ Napi::Promise::Deferred deferred_;
257
+ FPDF_DOCUMENT doc_;
258
+ std::string name_;
259
+ std::u16string nameU16_;
260
+ std::shared_ptr<std::atomic<bool>> docAlive_;
261
+ NamedDestinationInfo info_;
262
+ bool found_ = false;
263
+ };
package/src/document.h CHANGED
@@ -34,6 +34,8 @@ public:
34
34
  "getJavaScriptActions"),
35
35
  InstanceMethod<&PDFiumDocument::GetNamedDestinations>(
36
36
  "getNamedDestinations"),
37
+ InstanceMethod<&PDFiumDocument::GetNamedDestination>(
38
+ "getNamedDestination"),
37
39
  InstanceMethod<&PDFiumDocument::Destroy>("destroy"),
38
40
  });
39
41
  }
@@ -216,6 +218,31 @@ private:
216
218
  return promise;
217
219
  }
218
220
 
221
+ /**
222
+ * Resolves a single named destination by name (async). Resolves to null when
223
+ * no destination carries the name. Reaches destinations the index-based
224
+ * listing cannot enumerate — see the note in destinations_worker.h.
225
+ */
226
+ Napi::Value GetNamedDestination(const Napi::CallbackInfo &info) {
227
+ Napi::Env env = info.Env();
228
+ EnsureOpen(env);
229
+ if (env.IsExceptionPending())
230
+ return env.Null();
231
+
232
+ if (info.Length() < 1 || !info[0].IsString()) {
233
+ Napi::TypeError::New(env, "Expected destination name")
234
+ .ThrowAsJavaScriptException();
235
+ return env.Null();
236
+ }
237
+
238
+ Napi::String name = info[0].As<Napi::String>();
239
+ auto *worker = new GetNamedDestinationWorker(
240
+ env, doc_, name.Utf8Value(), name.Utf16Value(), docAlive_);
241
+ auto promise = worker->Promise();
242
+ worker->Queue();
243
+ return promise;
244
+ }
245
+
219
246
  /**
220
247
  * Closes the document and releases all resources.
221
248
  */
@@ -20,6 +20,17 @@ struct FormFieldOptionData {
20
20
  };
21
21
 
22
22
  struct FormFieldData {
23
+ // FPDFPage_GetAnnot returned NULL for an annotation on this page --
24
+ // documented as "NULL on failure". Because the subtype is only readable
25
+ // through that handle, there is no way to tell whether the unloadable
26
+ // annotation was a widget, so it cannot be filtered out the way a known
27
+ // non-widget is. It is serialized as a null entry: "a form field may be
28
+ // missing here" is the honest answer, and silently returning a short list
29
+ // let a damaged widget pass for a page that simply has fewer fields.
30
+ // This listing is a filtered subset, so there is no page index to carry on
31
+ // the sentinel; getAnnotations() on the same page reports the identical
32
+ // failure at its true index.
33
+ bool failed = false;
23
34
  std::string type;
24
35
  std::u16string name;
25
36
  std::u16string value;
@@ -59,10 +70,17 @@ protected:
59
70
  }
60
71
 
61
72
  int count = FPDFPage_GetAnnotCount(page_);
73
+ // an upper bound: widgets plus any annotation that fails to load
74
+ if (count > 0)
75
+ fields_.reserve(count);
62
76
  for (int i = 0; i < count; i++) {
63
77
  FPDF_ANNOTATION annot = FPDFPage_GetAnnot(page_, i);
64
- if (!annot)
78
+ if (!annot) {
79
+ FormFieldData missing;
80
+ missing.failed = true;
81
+ fields_.push_back(std::move(missing));
65
82
  continue;
83
+ }
66
84
 
67
85
  // only process widget annotations (form fields)
68
86
  if (FPDFAnnot_GetSubtype(annot) != FPDF_ANNOT_WIDGET) {
@@ -205,6 +223,10 @@ protected:
205
223
  Napi::Array arr = Napi::Array::New(env, fields_.size());
206
224
  for (uint32_t i = 0; i < fields_.size(); i++) {
207
225
  auto &d = fields_[i];
226
+ if (d.failed) {
227
+ arr.Set(i, env.Null());
228
+ continue;
229
+ }
208
230
  Napi::Object obj = Napi::Object::New(env);
209
231
  obj.Set("type", Napi::String::New(env, d.type));
210
232
  SetU16(obj, "name", env, d.name);
@@ -22,6 +22,12 @@
22
22
 
23
23
  struct JavaScriptActionInfo {
24
24
  int index = 0;
25
+ // FPDFDoc_GetJavaScriptAction returned NULL for this index -- documented as
26
+ // "NULL on failure", so the action is counted but could not be loaded.
27
+ // Serialized as a null entry rather than dropped: a document-open script
28
+ // that cannot be read is exactly what a caller triaging an untrusted PDF
29
+ // needs to know about.
30
+ bool failed = false;
25
31
  std::u16string name;
26
32
  std::u16string script;
27
33
  };
@@ -50,8 +56,15 @@ protected:
50
56
  actions_.reserve(count);
51
57
  for (int i = 0; i < count; i++) {
52
58
  FPDF_JAVASCRIPT_ACTION action = FPDFDoc_GetJavaScriptAction(doc_, i);
53
- if (!action)
59
+ if (!action) {
60
+ // The count promised this index, so a NULL handle is damage,
61
+ // not absence. Dropping it silently shortened the array.
62
+ JavaScriptActionInfo missing;
63
+ missing.index = i;
64
+ missing.failed = true;
65
+ actions_.push_back(std::move(missing));
54
66
  continue;
67
+ }
55
68
 
56
69
  JavaScriptActionInfo info;
57
70
  info.index = i;
@@ -84,6 +97,10 @@ protected:
84
97
  Napi::Array arr = Napi::Array::New(env, actions_.size());
85
98
  for (uint32_t i = 0; i < actions_.size(); i++) {
86
99
  const auto &info = actions_[i];
100
+ if (info.failed) {
101
+ arr.Set(i, env.Null());
102
+ continue;
103
+ }
87
104
  Napi::Object obj = Napi::Object::New(env);
88
105
  obj.Set("index", Napi::Number::New(env, info.index));
89
106
  SetU16(obj, "name", env, info.name);