Add placeholder password callback to support reading of encrypted PDF files in the future.

This commit is contained in:
Michael R Sweet 2021-09-27 08:37:14 -04:00
parent ba9d03ecac
commit 43239eaf8a
No known key found for this signature in database
GPG Key ID: BE67C75EC81F3244
5 changed files with 78 additions and 32 deletions

View File

@ -2016,8 +2016,8 @@ CropBox for pages in the PDF file - if \fBNULL\fR then a default "Universal" siz
of 8.27x11in (the intersection of US Letter and ISO A4) is used. of 8.27x11in (the intersection of US Letter and ISO A4) is used.
.PP .PP
The "error_cb" and "error_data" arguments specify an error handler callback The "error_cb" and "error_data" arguments specify an error handler callback
and its data pointer - if not specified the default error handler is used and its data pointer - if \fBNULL\fR the default error handler is used that
that writes error messages to \fBstderr\fR. writes error messages to \fBstderr\fR.
.SS pdfioFileCreateArrayObj .SS pdfioFileCreateArrayObj
Create a new object in a PDF file containing an array. Create a new object in a PDF file containing an array.
.PP .PP
@ -2196,8 +2196,8 @@ CropBox for pages in the PDF file - if \fBNULL\fR then a default "Universal" siz
of 8.27x11in (the intersection of US Letter and ISO A4) is used. of 8.27x11in (the intersection of US Letter and ISO A4) is used.
.PP .PP
The "error_cb" and "error_data" arguments specify an error handler callback The "error_cb" and "error_data" arguments specify an error handler callback
and its data pointer - if not specified the default error handler is used and its data pointer - if \fBNULL\fR the default error handler is used that
that writes error messages to \fBstderr\fR. writes error messages to \fBstderr\fR.
.PP .PP
.IP 5 .IP 5
\fINote\fR: Files created using this API are slightly larger than those \fINote\fR: Files created using this API are slightly larger than those
@ -2346,16 +2346,25 @@ Open a PDF file for reading.
.nf .nf
pdfio_file_t * pdfioFileOpen ( pdfio_file_t * pdfioFileOpen (
const char *filename, const char *filename,
pdfio_password_cb_t password_cb,
void *password_data,
pdfio_error_cb_t error_cb, pdfio_error_cb_t error_cb,
void *error_data void *error_data
); );
.fi .fi
.PP .PP
This function opens an existing PDF file. The "filename" argument specifies This function opens an existing PDF file. The "filename" argument specifies
the name of the PDF file to create. The "error_cb" and "error_data" the name of the PDF file to create.
arguments specify an error handler callback and its data pointer - if not .PP
specified the default error handler is used that writes error messages to The "password_cb" and "password_data" arguments specify a password callback
\fBstderr\fR. and its data pointer for PDF files that use one of the standard Adobe
"security" handlers. The callback returns a password string or \fBNULL\fR to
cancel the open. If \fBNULL\fR is specified for the callback function and the
PDF file requires a password, the open will always fail.
.PP
The "error_cb" and "error_data" arguments specify an error handler callback
and its data pointer - if \fBNULL\fR the default error handler is used that
writes error messages to \fBstderr\fR.
.SS pdfioFileSetAuthor .SS pdfioFileSetAuthor
Set the author for a PDF file. Set the author for a PDF file.
.PP .PP
@ -2778,6 +2787,12 @@ Output callback for pdfioFileCreateOutput
.nf .nf
typedef ssize_t(*)(void *ctx const void *data size_t datalen) pdfio_output_cb_t; typedef ssize_t(*)(void *ctx const void *data size_t datalen) pdfio_output_cb_t;
.fi .fi
.SS pdfio_password_cb_t
Password callback for pdfioFileOpen
.PP
.nf
typedef const char *(*)(void *data const char *filename) pdfio_password_cb_t;
.fi
.SS pdfio_rect_t .SS pdfio_rect_t
PDF rectangle PDF rectangle
.PP .PP

View File

@ -447,6 +447,7 @@ span.string {
<li><a href="#pdfio_matrix_t[3][2]">pdfio_matrix_t[3][2]</a></li> <li><a href="#pdfio_matrix_t[3][2]">pdfio_matrix_t[3][2]</a></li>
<li><a href="#pdfio_obj_t">pdfio_obj_t</a></li> <li><a href="#pdfio_obj_t">pdfio_obj_t</a></li>
<li><a href="#pdfio_output_cb_t">pdfio_output_cb_t</a></li> <li><a href="#pdfio_output_cb_t">pdfio_output_cb_t</a></li>
<li><a href="#pdfio_password_cb_t">pdfio_password_cb_t</a></li>
<li><a href="#pdfio_rect_t">pdfio_rect_t</a></li> <li><a href="#pdfio_rect_t">pdfio_rect_t</a></li>
<li><a href="#pdfio_stream_t">pdfio_stream_t</a></li> <li><a href="#pdfio_stream_t">pdfio_stream_t</a></li>
<li><a href="#pdfio_textrendering_t">pdfio_textrendering_t</a></li> <li><a href="#pdfio_textrendering_t">pdfio_textrendering_t</a></li>
@ -2464,8 +2465,8 @@ CropBox for pages in the PDF file - if <code>NULL</code> then a default &quot;Un
of 8.27x11in (the intersection of US Letter and ISO A4) is used.<br> of 8.27x11in (the intersection of US Letter and ISO A4) is used.<br>
<br> <br>
The &quot;error_cb&quot; and &quot;error_data&quot; arguments specify an error handler callback The &quot;error_cb&quot; and &quot;error_data&quot; arguments specify an error handler callback
and its data pointer - if not specified the default error handler is used and its data pointer - if <code>NULL</code> the default error handler is used that
that writes error messages to <code>stderr</code>.</p> writes error messages to <code>stderr</code>.</p>
<h3 class="function"><a id="pdfioFileCreateArrayObj">pdfioFileCreateArrayObj</a></h3> <h3 class="function"><a id="pdfioFileCreateArrayObj">pdfioFileCreateArrayObj</a></h3>
<p class="description">Create a new object in a PDF file containing an array.</p> <p class="description">Create a new object in a PDF file containing an array.</p>
<p class="code"> <p class="code">
@ -2687,8 +2688,8 @@ CropBox for pages in the PDF file - if <code>NULL</code> then a default &quot;Un
of 8.27x11in (the intersection of US Letter and ISO A4) is used.<br> of 8.27x11in (the intersection of US Letter and ISO A4) is used.<br>
<br> <br>
The &quot;error_cb&quot; and &quot;error_data&quot; arguments specify an error handler callback The &quot;error_cb&quot; and &quot;error_data&quot; arguments specify an error handler callback
and its data pointer - if not specified the default error handler is used and its data pointer - if <code>NULL</code> the default error handler is used that
that writes error messages to <code>stderr</code>.<br> writes error messages to <code>stderr</code>.<br>
<br> <br>
</p><blockquote> </p><blockquote>
<em>Note</em>: Files created using this API are slightly larger than those <em>Note</em>: Files created using this API are slightly larger than those
@ -2884,11 +2885,15 @@ const char *pdfioFileGetVersion(<a href="#pdfio_file_t">pdfio_file_t</a> *pdf);<
<h3 class="function"><a id="pdfioFileOpen">pdfioFileOpen</a></h3> <h3 class="function"><a id="pdfioFileOpen">pdfioFileOpen</a></h3>
<p class="description">Open a PDF file for reading.</p> <p class="description">Open a PDF file for reading.</p>
<p class="code"> <p class="code">
<a href="#pdfio_file_t">pdfio_file_t</a> *pdfioFileOpen(const char *filename, <a href="#pdfio_error_cb_t">pdfio_error_cb_t</a> error_cb, void *error_data);</p> <a href="#pdfio_file_t">pdfio_file_t</a> *pdfioFileOpen(const char *filename, <a href="#pdfio_password_cb_t">pdfio_password_cb_t</a> password_cb, void *password_data, <a href="#pdfio_error_cb_t">pdfio_error_cb_t</a> error_cb, void *error_data);</p>
<h4 class="parameters">Parameters</h4> <h4 class="parameters">Parameters</h4>
<table class="list"><tbody> <table class="list"><tbody>
<tr><th>filename</th> <tr><th>filename</th>
<td class="description">Filename</td></tr> <td class="description">Filename</td></tr>
<tr><th>password_cb</th>
<td class="description">Password callback or <code>NULL</code> for none</td></tr>
<tr><th>password_data</th>
<td class="description">Password callback data, if any</td></tr>
<tr><th>error_cb</th> <tr><th>error_cb</th>
<td class="description">Error callback or <code>NULL</code> for default</td></tr> <td class="description">Error callback or <code>NULL</code> for default</td></tr>
<tr><th>error_data</th> <tr><th>error_data</th>
@ -2898,10 +2903,17 @@ const char *pdfioFileGetVersion(<a href="#pdfio_file_t">pdfio_file_t</a> *pdf);<
<p class="description">PDF file</p> <p class="description">PDF file</p>
<h4 class="discussion">Discussion</h4> <h4 class="discussion">Discussion</h4>
<p class="discussion">This function opens an existing PDF file. The &quot;filename&quot; argument specifies <p class="discussion">This function opens an existing PDF file. The &quot;filename&quot; argument specifies
the name of the PDF file to create. The &quot;error_cb&quot; and &quot;error_data&quot; the name of the PDF file to create.<br>
arguments specify an error handler callback and its data pointer - if not <br>
specified the default error handler is used that writes error messages to The &quot;password_cb&quot; and &quot;password_data&quot; arguments specify a password callback
<code>stderr</code>.</p> and its data pointer for PDF files that use one of the standard Adobe
&quot;security&quot; handlers. The callback returns a password string or <code>NULL</code> to
cancel the open. If <code>NULL</code> is specified for the callback function and the
PDF file requires a password, the open will always fail.<br>
<br>
The &quot;error_cb&quot; and &quot;error_data&quot; arguments specify an error handler callback
and its data pointer - if <code>NULL</code> the default error handler is used that
writes error messages to <code>stderr</code>.</p>
<h3 class="function"><a id="pdfioFileSetAuthor">pdfioFileSetAuthor</a></h3> <h3 class="function"><a id="pdfioFileSetAuthor">pdfioFileSetAuthor</a></h3>
<p class="description">Set the author for a PDF file.</p> <p class="description">Set the author for a PDF file.</p>
<p class="code"> <p class="code">
@ -3425,6 +3437,11 @@ typedef struct _pdfio_obj_s pdfio_obj_t;
<p class="code"> <p class="code">
typedef ssize_t (*pdfio_output_cb_t)(void *ctx const void *data size_t datalen); typedef ssize_t (*pdfio_output_cb_t)(void *ctx const void *data size_t datalen);
</p> </p>
<h3 class="typedef"><a id="pdfio_password_cb_t">pdfio_password_cb_t</a></h3>
<p class="description">Password callback for pdfioFileOpen</p>
<p class="code">
typedef const char *(*pdfio_password_cb_t)(void *data const char *filename);
</p>
<h3 class="typedef"><a id="pdfio_rect_t">pdfio_rect_t</a></h3> <h3 class="typedef"><a id="pdfio_rect_t">pdfio_rect_t</a></h3>
<p class="description">PDF rectangle</p> <p class="description">PDF rectangle</p>
<p class="code"> <p class="code">

View File

@ -177,8 +177,8 @@ pdfioFileClose(pdfio_file_t *pdf) // I - PDF file
// of 8.27x11in (the intersection of US Letter and ISO A4) is used. // of 8.27x11in (the intersection of US Letter and ISO A4) is used.
// //
// The "error_cb" and "error_data" arguments specify an error handler callback // The "error_cb" and "error_data" arguments specify an error handler callback
// and its data pointer - if not specified the default error handler is used // and its data pointer - if `NULL` the default error handler is used that
// that writes error messages to `stderr`. // writes error messages to `stderr`.
// //
pdfio_file_t * // O - PDF file or `NULL` on error pdfio_file_t * // O - PDF file or `NULL` on error
@ -437,8 +437,8 @@ _pdfioFileCreateObj(
// of 8.27x11in (the intersection of US Letter and ISO A4) is used. // of 8.27x11in (the intersection of US Letter and ISO A4) is used.
// //
// The "error_cb" and "error_data" arguments specify an error handler callback // The "error_cb" and "error_data" arguments specify an error handler callback
// and its data pointer - if not specified the default error handler is used // and its data pointer - if `NULL` the default error handler is used that
// that writes error messages to `stderr`. // writes error messages to `stderr`.
// //
// > *Note*: Files created using this API are slightly larger than those // > *Note*: Files created using this API are slightly larger than those
// > created using the @link pdfioFileCreate@ function since stream lengths are // > created using the @link pdfioFileCreate@ function since stream lengths are
@ -863,17 +863,26 @@ pdfioFileGetVersion(
// 'pdfioFileOpen()' - Open a PDF file for reading. // 'pdfioFileOpen()' - Open a PDF file for reading.
// //
// This function opens an existing PDF file. The "filename" argument specifies // This function opens an existing PDF file. The "filename" argument specifies
// the name of the PDF file to create. The "error_cb" and "error_data" // the name of the PDF file to create.
// arguments specify an error handler callback and its data pointer - if not //
// specified the default error handler is used that writes error messages to // The "password_cb" and "password_data" arguments specify a password callback
// `stderr`. // and its data pointer for PDF files that use one of the standard Adobe
// "security" handlers. The callback returns a password string or `NULL` to
// cancel the open. If `NULL` is specified for the callback function and the
// PDF file requires a password, the open will always fail.
//
// The "error_cb" and "error_data" arguments specify an error handler callback
// and its data pointer - if `NULL` the default error handler is used that
// writes error messages to `stderr`.
// //
pdfio_file_t * // O - PDF file pdfio_file_t * // O - PDF file
pdfioFileOpen( pdfioFileOpen(
const char *filename, // I - Filename const char *filename, // I - Filename
pdfio_error_cb_t error_cb, // I - Error callback or `NULL` for default pdfio_password_cb_t password_cb, // I - Password callback or `NULL` for none
void *error_data) // I - Error callback data, if any void *password_data, // I - Password callback data, if any
pdfio_error_cb_t error_cb, // I - Error callback or `NULL` for default
void *error_data) // I - Error callback data, if any
{ {
pdfio_file_t *pdf; // PDF file pdfio_file_t *pdf; // PDF file
char line[1024], // Line from file char line[1024], // Line from file
@ -881,6 +890,9 @@ pdfioFileOpen(
off_t xref_offset; // Offset to xref table off_t xref_offset; // Offset to xref table
(void)password_cb;
(void)password_data;
// Range check input... // Range check input...
if (!filename) if (!filename)
return (NULL); return (NULL);

View File

@ -76,6 +76,8 @@ typedef enum pdfio_filter_e // Compression/decompression filters for streams
typedef struct _pdfio_obj_s pdfio_obj_t;// Numbered object in PDF file typedef struct _pdfio_obj_s pdfio_obj_t;// Numbered object in PDF file
typedef ssize_t (*pdfio_output_cb_t)(void *ctx, const void *data, size_t datalen); typedef ssize_t (*pdfio_output_cb_t)(void *ctx, const void *data, size_t datalen);
// Output callback for pdfioFileCreateOutput // Output callback for pdfioFileCreateOutput
typedef const char *(*pdfio_password_cb_t)(void *data, const char *filename);
// Password callback for pdfioFileOpen
typedef struct pdfio_rect_s // PDF rectangle typedef struct pdfio_rect_s // PDF rectangle
{ {
double x1; // Lower-left X coordinate double x1; // Lower-left X coordinate
@ -176,7 +178,7 @@ extern const char *pdfioFileGetProducer(pdfio_file_t *pdf) _PDFIO_PUBLIC;
extern const char *pdfioFileGetSubject(pdfio_file_t *pdf) _PDFIO_PUBLIC; extern const char *pdfioFileGetSubject(pdfio_file_t *pdf) _PDFIO_PUBLIC;
extern const char *pdfioFileGetTitle(pdfio_file_t *pdf) _PDFIO_PUBLIC; extern const char *pdfioFileGetTitle(pdfio_file_t *pdf) _PDFIO_PUBLIC;
extern const char *pdfioFileGetVersion(pdfio_file_t *pdf) _PDFIO_PUBLIC; extern const char *pdfioFileGetVersion(pdfio_file_t *pdf) _PDFIO_PUBLIC;
extern pdfio_file_t *pdfioFileOpen(const char *filename, pdfio_error_cb_t error_cb, void *error_data) _PDFIO_PUBLIC; extern pdfio_file_t *pdfioFileOpen(const char *filename, pdfio_password_cb_t password_cb, void *password_data, pdfio_error_cb_t error_cb, void *error_data) _PDFIO_PUBLIC;
extern void pdfioFileSetAuthor(pdfio_file_t *pdf, const char *value) _PDFIO_PUBLIC; extern void pdfioFileSetAuthor(pdfio_file_t *pdf, const char *value) _PDFIO_PUBLIC;
extern void pdfioFileSetCreationDate(pdfio_file_t *pdf, time_t value) _PDFIO_PUBLIC; extern void pdfioFileSetCreationDate(pdfio_file_t *pdf, time_t value) _PDFIO_PUBLIC;
extern void pdfioFileSetCreator(pdfio_file_t *pdf, const char *value) _PDFIO_PUBLIC; extern void pdfioFileSetCreator(pdfio_file_t *pdf, const char *value) _PDFIO_PUBLIC;

View File

@ -136,7 +136,7 @@ do_test_file(const char *filename, // I - PDF filename
fflush(stdout); fflush(stdout);
} }
if ((pdf = pdfioFileOpen(filename, (pdfio_error_cb_t)error_cb, &error)) != NULL) if ((pdf = pdfioFileOpen(filename, /*password_cb*/NULL, /*password_data*/NULL, (pdfio_error_cb_t)error_cb, &error)) != NULL)
{ {
if (objnum) if (objnum)
{ {
@ -715,7 +715,7 @@ do_unit_tests(void)
// First open the test PDF file... // First open the test PDF file...
fputs("pdfioFileOpen(\"testfiles/testpdfio.pdf\"): ", stdout); fputs("pdfioFileOpen(\"testfiles/testpdfio.pdf\"): ", stdout);
if ((inpdf = pdfioFileOpen("testfiles/testpdfio.pdf", (pdfio_error_cb_t)error_cb, &error)) != NULL) if ((inpdf = pdfioFileOpen("testfiles/testpdfio.pdf", /*password_cb*/NULL, /*password_data*/NULL, (pdfio_error_cb_t)error_cb, &error)) != NULL)
puts("PASS"); puts("PASS");
else else
return (1); return (1);
@ -898,7 +898,7 @@ read_unit_file(const char *filename, // I - File to read
// Open the new PDF file to read it... // Open the new PDF file to read it...
printf("pdfioFileOpen(\"%s\", ...): ", filename); printf("pdfioFileOpen(\"%s\", ...): ", filename);
if ((pdf = pdfioFileOpen(filename, (pdfio_error_cb_t)error_cb, &error)) != NULL) if ((pdf = pdfioFileOpen(filename, /*password_cb*/NULL, /*password_data*/NULL, (pdfio_error_cb_t)error_cb, &error)) != NULL)
puts("PASS"); puts("PASS");
else else
return (1); return (1);