Commit 920e929864aecf1e9e16c96565ac28a9121e278e
1 parent
9589fad1
Reimplement QPDF_Stream::getStreamJSON in terms of writeStreamJSON
writeStreamJSON is a temporary implementation minimally adapted from getStreamJSON.
Showing
2 changed files
with
44 additions
and
12 deletions
libqpdf/QPDF_Stream.cc
| ... | ... | @@ -191,21 +191,43 @@ QPDF_Stream::getStreamJSON( |
| 191 | 191 | Pipeline* p, |
| 192 | 192 | std::string const& data_filename) |
| 193 | 193 | { |
| 194 | + Pl_Buffer pb{"streamjson"}; | |
| 195 | + JSON::Writer jw{&pb, 0}; | |
| 196 | + decode_level = | |
| 197 | + writeStreamJSON(json_version, jw, json_data, decode_level, p, data_filename, true); | |
| 198 | + pb.finish(); | |
| 199 | + auto result = JSON::parse(pb.getString()); | |
| 200 | + if (json_data == qpdf_sj_inline) { | |
| 201 | + result.addDictionaryMember("data", JSON::makeBlob(StreamBlobProvider(this, decode_level))); | |
| 202 | + } | |
| 203 | + return result; | |
| 204 | +} | |
| 205 | + | |
| 206 | +qpdf_stream_decode_level_e | |
| 207 | +QPDF_Stream::writeStreamJSON( | |
| 208 | + int json_version, | |
| 209 | + JSON::Writer& jw, | |
| 210 | + qpdf_json_stream_data_e json_data, | |
| 211 | + qpdf_stream_decode_level_e decode_level, | |
| 212 | + Pipeline* p, | |
| 213 | + std::string const& data_filename, | |
| 214 | + bool no_data_key) | |
| 215 | +{ | |
| 194 | 216 | switch (json_data) { |
| 195 | 217 | case qpdf_sj_none: |
| 196 | 218 | case qpdf_sj_inline: |
| 197 | 219 | if (p != nullptr) { |
| 198 | - throw std::logic_error("QPDF_Stream::getStreamJSON: pipeline should only be supplied " | |
| 220 | + throw std::logic_error("QPDF_Stream::writeStreamJSON: pipeline should only be supplied " | |
| 199 | 221 | "when json_data is file"); |
| 200 | 222 | } |
| 201 | 223 | break; |
| 202 | 224 | case qpdf_sj_file: |
| 203 | 225 | if (p == nullptr) { |
| 204 | 226 | throw std::logic_error( |
| 205 | - "QPDF_Stream::getStreamJSON: pipeline must be supplied when json_data is file"); | |
| 227 | + "QPDF_Stream::writeStreamJSON: pipeline must be supplied when json_data is file"); | |
| 206 | 228 | } |
| 207 | 229 | if (data_filename.empty()) { |
| 208 | - throw std::logic_error("QPDF_Stream::getStreamJSON: data_filename must be supplied " | |
| 230 | + throw std::logic_error("QPDF_Stream::writeStreamJSON: data_filename must be supplied " | |
| 209 | 231 | "when json_data is file"); |
| 210 | 232 | } |
| 211 | 233 | break; |
| ... | ... | @@ -234,12 +256,13 @@ QPDF_Stream::getStreamJSON( |
| 234 | 256 | decode_level = qpdf_dl_none; |
| 235 | 257 | buf_pl.getString(); // reset buf_pl |
| 236 | 258 | } else { |
| 237 | - if (json_data == qpdf_sj_file) { | |
| 238 | - buf_pl_ready = true; | |
| 239 | - } | |
| 259 | + buf_pl_ready = true; | |
| 240 | 260 | break; |
| 241 | 261 | } |
| 242 | 262 | } |
| 263 | + if (!buf_pl_ready) { | |
| 264 | + throw std::logic_error("QPDF_Stream: failed to get stream data"); | |
| 265 | + } | |
| 243 | 266 | // We can use unsafeShallowCopy because we are only touching top-level keys. |
| 244 | 267 | dict = this->stream_dict.unsafeShallowCopy(); |
| 245 | 268 | dict.removeKey("/Length"); |
| ... | ... | @@ -249,19 +272,20 @@ QPDF_Stream::getStreamJSON( |
| 249 | 272 | } |
| 250 | 273 | if (json_data == qpdf_sj_file) { |
| 251 | 274 | result.addDictionaryMember("datafile", JSON::makeString(data_filename)); |
| 252 | - if (!buf_pl_ready) { | |
| 253 | - throw std::logic_error("QPDF_Stream: failed to get stream data in json file mode"); | |
| 254 | - } | |
| 275 | + | |
| 255 | 276 | p->writeString(buf_pl.getString()); |
| 256 | 277 | } else if (json_data == qpdf_sj_inline) { |
| 257 | - result.addDictionaryMember( | |
| 258 | - "data", JSON::makeBlob(StreamBlobProvider(this, decode_level))); | |
| 278 | + if (!no_data_key) { | |
| 279 | + result.addDictionaryMember( | |
| 280 | + "data", JSON::makeBlob(StreamBlobProvider(this, decode_level))); | |
| 281 | + } | |
| 259 | 282 | } else { |
| 260 | 283 | throw std::logic_error("QPDF_Stream: unexpected value of json_data"); |
| 261 | 284 | } |
| 262 | 285 | } |
| 263 | 286 | result.addDictionaryMember("dict", dict.getJSON(json_version)); |
| 264 | - return result; | |
| 287 | + jw << std::move(result); | |
| 288 | + return decode_level; | |
| 265 | 289 | } |
| 266 | 290 | |
| 267 | 291 | void | ... | ... |
libqpdf/qpdf/QPDF_Stream.hh
| ... | ... | @@ -64,6 +64,14 @@ class QPDF_Stream: public QPDFValue |
| 64 | 64 | qpdf_stream_decode_level_e decode_level, |
| 65 | 65 | Pipeline* p, |
| 66 | 66 | std::string const& data_filename); |
| 67 | + qpdf_stream_decode_level_e writeStreamJSON( | |
| 68 | + int json_version, | |
| 69 | + JSON::Writer& jw, | |
| 70 | + qpdf_json_stream_data_e json_data, | |
| 71 | + qpdf_stream_decode_level_e decode_level, | |
| 72 | + Pipeline* p, | |
| 73 | + std::string const& data_filename, | |
| 74 | + bool no_data_key = false); | |
| 67 | 75 | |
| 68 | 76 | void replaceDict(QPDFObjectHandle const& new_dict); |
| 69 | 77 | ... | ... |