Commit b36f62a326e10f2b1beb9f3791a7b607ea3f994a

Authored by Jay Berkenbilt
1 parent bcb10021

add qpdf_read_memory to C API

git-svn-id: svn+q:///qpdf/trunk@1044 71b93d88-0707-0410-a8cf-f5a4172ac649
include/qpdf/qpdf-c.h
... ... @@ -183,7 +183,21 @@ extern "C" {
183 183 QPDF_ERROR_CODE qpdf_read(qpdf_data qpdf, char const* filename,
184 184 char const* password);
185 185  
186   - /* Read functions below must be called after qpdf_read. */
  186 + /* Calling qpdf_read_memory causes processMemoryFile to be called
  187 + * in the C++ API. Otherwise, it behaves in the same way as
  188 + * qpdf_read. The description argument will be used in place of
  189 + * the file name in any error or warning messages generated by the
  190 + * library.
  191 + */
  192 + QPDF_DLL
  193 + QPDF_ERROR_CODE qpdf_read_memory(qpdf_data qpdf,
  194 + char const* description,
  195 + char const* buffer,
  196 + unsigned long size,
  197 + char const* password);
  198 +
  199 + /* Read functions below must be called after qpdf_read or
  200 + * qpdf_read_memory. */
187 201  
188 202 /* Return the version of the PDF file. */
189 203 QPDF_DLL
... ...
libqpdf/qpdf-c.cc
... ... @@ -28,7 +28,9 @@ struct _qpdf_data
28 28 std::string tmp_string;
29 29  
30 30 // Parameters for functions we call
31   - char const* filename;
  31 + char const* filename; // or description
  32 + char const* buffer;
  33 + unsigned long size;
32 34 char const* password;
33 35 };
34 36  
... ... @@ -50,6 +52,13 @@ static void call_read(qpdf_data qpdf)
50 52 qpdf->qpdf->processFile(qpdf->filename, qpdf->password);
51 53 }
52 54  
  55 +// must set qpdf->filename, qpdf->buffer, qpdf->size, and qpdf->password
  56 +static void call_read_memory(qpdf_data qpdf)
  57 +{
  58 + qpdf->qpdf->processMemoryFile(qpdf->filename, qpdf->buffer,
  59 + qpdf->size, qpdf->password);
  60 +}
  61 +
53 62 // must set qpdf->filename
54 63 static void call_init_write(qpdf_data qpdf)
55 64 {
... ... @@ -244,6 +253,22 @@ QPDF_ERROR_CODE qpdf_read(qpdf_data qpdf, char const* filename,
244 253 return status;
245 254 }
246 255  
  256 +QPDF_ERROR_CODE qpdf_read_memory(qpdf_data qpdf,
  257 + char const* description,
  258 + char const* buffer,
  259 + unsigned long size,
  260 + char const* password)
  261 +{
  262 + QPDF_ERROR_CODE status = QPDF_SUCCESS;
  263 + qpdf->filename = description;
  264 + qpdf->buffer = buffer;
  265 + qpdf->size = size;
  266 + qpdf->password = password;
  267 + status = trap_errors(qpdf, &call_read_memory);
  268 + QTC::TC("qpdf", "qpdf-c called qpdf_read_memory", status);
  269 + return status;
  270 +}
  271 +
247 272 char const* qpdf_get_pdf_version(qpdf_data qpdf)
248 273 {
249 274 QTC::TC("qpdf", "qpdf-c called qpdf_get_pdf_version");
... ...
manual/qpdf-manual.xml
... ... @@ -5,8 +5,8 @@
5 5 <!ENTITY mdash "&#x2014;">
6 6 <!ENTITY ndash "&#x2013;">
7 7 <!ENTITY nbsp "&#xA0;">
8   -<!ENTITY swversion "2.2.1">
9   -<!ENTITY lastreleased "October 1, 2010">
  8 +<!ENTITY swversion "2.2.2">
  9 +<!ENTITY lastreleased "October 4, 2010">
10 10 ]>
11 11 <book>
12 12 <bookinfo>
... ... @@ -2075,6 +2075,21 @@ print &quot;\n&quot;;
2075 2075 </para>
2076 2076 <variablelist>
2077 2077 <varlistentry>
  2078 + <term>2.2.2: October 4, 2010</term>
  2079 + <listitem>
  2080 + <itemizedlist>
  2081 + <listitem>
  2082 + <para>
  2083 + Add new function <function>qpdf_read_memory</function>
  2084 + to the C API to call
  2085 + <function>QPDF::processMemoryFile</function>. This was an
  2086 + omission in qpdf 2.2.1.
  2087 + </para>
  2088 + </listitem>
  2089 + </itemizedlist>
  2090 + </listitem>
  2091 + </varlistentry>
  2092 + <varlistentry>
2078 2093 <term>2.2.1: October 1, 2010</term>
2079 2094 <listitem>
2080 2095 <itemizedlist>
... ...
qpdf/qpdf-ctest.c
... ... @@ -3,7 +3,9 @@
3 3 #include <assert.h>
4 4 #include <stdlib.h>
5 5 #include <string.h>
  6 +#include <errno.h>
6 7  
  8 +static char* whoami = 0;
7 9 static qpdf_data qpdf = 0;
8 10  
9 11 static void report_errors()
... ... @@ -42,6 +44,57 @@ static void report_errors()
42 44 }
43 45 }
44 46  
  47 +static void read_file_into_memory(char const* filename,
  48 + char** buf, unsigned long* size)
  49 +{
  50 + char* buf_p = 0;
  51 + FILE* f = NULL;
  52 + size_t bytes_read = 0;
  53 + size_t len = 0;
  54 +
  55 + f = fopen(filename, "rb");
  56 + if (f == NULL)
  57 + {
  58 + fprintf(stderr, "%s: unable to open %s: %s\n",
  59 + whoami, filename, strerror(errno));
  60 + exit(2);
  61 + }
  62 + fseek(f, 0, SEEK_END);
  63 + *size = (unsigned long) ftell(f);
  64 + fseek(f, 0, SEEK_SET);
  65 + *buf = malloc(*size);
  66 + if (*buf == NULL)
  67 + {
  68 + fprintf(stderr, "%s: unable to allocate %lu bytes\n",
  69 + whoami, *size);
  70 + exit(2);
  71 + }
  72 + buf_p = *buf;
  73 + bytes_read = 0;
  74 + len = 0;
  75 + while ((len = fread(buf_p + bytes_read, 1, *size - bytes_read, f)) > 0)
  76 + {
  77 + bytes_read += len;
  78 + }
  79 + if (bytes_read != *size)
  80 + {
  81 + if (ferror(f))
  82 + {
  83 + fprintf(stderr, "%s: failure reading file %s into memory:",
  84 + whoami, filename);
  85 + }
  86 + else
  87 + {
  88 + fprintf(stderr, "%s: premature EOF reading file %s:",
  89 + whoami, filename);
  90 + }
  91 + fprintf(stderr, " read %lu, wanted %lu\n",
  92 + (unsigned long) bytes_read, (unsigned long) size);
  93 + exit(2);
  94 + }
  95 + fclose(f);
  96 +}
  97 +
45 98 static void test01(char const* infile,
46 99 char const* password,
47 100 char const* outfile,
... ... @@ -135,7 +188,10 @@ static void test06(char const* infile,
135 188 char const* outfile,
136 189 char const* outfile2)
137 190 {
138   - qpdf_read(qpdf, infile, password);
  191 + char* buf = NULL;
  192 + unsigned long size = 0;
  193 + read_file_into_memory(infile, &buf, &size);
  194 + qpdf_read_memory(qpdf, infile, buf, size, password);
139 195 qpdf_init_write(qpdf, outfile);
140 196 qpdf_set_static_ID(qpdf, QPDF_TRUE);
141 197 qpdf_set_object_stream_mode(qpdf, qpdf_o_generate);
... ... @@ -271,7 +327,6 @@ static void test15(char const* infile,
271 327  
272 328 int main(int argc, char* argv[])
273 329 {
274   - char* whoami = 0;
275 330 char* p = 0;
276 331 int n = 0;
277 332 char const* infile = 0;
... ...
qpdf/qpdf.testcov
... ... @@ -186,3 +186,4 @@ QPDFObjectHandle append page contents 0
186 186 QPDF_Stream getRawStreamData 0
187 187 QPDF_Stream getStreamData 0
188 188 QPDF_Stream expand filter abbreviation 0
  189 +qpdf-c called qpdf_read_memory 0
... ...