Commit 920e929864aecf1e9e16c96565ac28a9121e278e

Authored by m-holger
1 parent 9589fad1

Reimplement QPDF_Stream::getStreamJSON in terms of writeStreamJSON

writeStreamJSON is a temporary implementation minimally adapted from
getStreamJSON.
libqpdf/QPDF_Stream.cc
@@ -191,21 +191,43 @@ QPDF_Stream::getStreamJSON( @@ -191,21 +191,43 @@ QPDF_Stream::getStreamJSON(
191 Pipeline* p, 191 Pipeline* p,
192 std::string const& data_filename) 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 switch (json_data) { 216 switch (json_data) {
195 case qpdf_sj_none: 217 case qpdf_sj_none:
196 case qpdf_sj_inline: 218 case qpdf_sj_inline:
197 if (p != nullptr) { 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 "when json_data is file"); 221 "when json_data is file");
200 } 222 }
201 break; 223 break;
202 case qpdf_sj_file: 224 case qpdf_sj_file:
203 if (p == nullptr) { 225 if (p == nullptr) {
204 throw std::logic_error( 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 if (data_filename.empty()) { 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 "when json_data is file"); 231 "when json_data is file");
210 } 232 }
211 break; 233 break;
@@ -234,12 +256,13 @@ QPDF_Stream::getStreamJSON( @@ -234,12 +256,13 @@ QPDF_Stream::getStreamJSON(
234 decode_level = qpdf_dl_none; 256 decode_level = qpdf_dl_none;
235 buf_pl.getString(); // reset buf_pl 257 buf_pl.getString(); // reset buf_pl
236 } else { 258 } else {
237 - if (json_data == qpdf_sj_file) {  
238 - buf_pl_ready = true;  
239 - } 259 + buf_pl_ready = true;
240 break; 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 // We can use unsafeShallowCopy because we are only touching top-level keys. 266 // We can use unsafeShallowCopy because we are only touching top-level keys.
244 dict = this->stream_dict.unsafeShallowCopy(); 267 dict = this->stream_dict.unsafeShallowCopy();
245 dict.removeKey("/Length"); 268 dict.removeKey("/Length");
@@ -249,19 +272,20 @@ QPDF_Stream::getStreamJSON( @@ -249,19 +272,20 @@ QPDF_Stream::getStreamJSON(
249 } 272 }
250 if (json_data == qpdf_sj_file) { 273 if (json_data == qpdf_sj_file) {
251 result.addDictionaryMember("datafile", JSON::makeString(data_filename)); 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 p->writeString(buf_pl.getString()); 276 p->writeString(buf_pl.getString());
256 } else if (json_data == qpdf_sj_inline) { 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 } else { 282 } else {
260 throw std::logic_error("QPDF_Stream: unexpected value of json_data"); 283 throw std::logic_error("QPDF_Stream: unexpected value of json_data");
261 } 284 }
262 } 285 }
263 result.addDictionaryMember("dict", dict.getJSON(json_version)); 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 void 291 void
libqpdf/qpdf/QPDF_Stream.hh
@@ -64,6 +64,14 @@ class QPDF_Stream: public QPDFValue @@ -64,6 +64,14 @@ class QPDF_Stream: public QPDFValue
64 qpdf_stream_decode_level_e decode_level, 64 qpdf_stream_decode_level_e decode_level,
65 Pipeline* p, 65 Pipeline* p,
66 std::string const& data_filename); 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 void replaceDict(QPDFObjectHandle const& new_dict); 76 void replaceDict(QPDFObjectHandle const& new_dict);
69 77