aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorm-holger <m-holger@kubitscheck.org>2024-02-15 13:33:31 +0100
committerm-holger <m-holger@kubitscheck.org>2024-02-16 12:37:50 +0100
commitb15d0bf6e1305982450879b498a116d3387b705b (patch)
tree5fdd29f63031fc56698c33cfff24fc220ebfec3d
parent920e929864aecf1e9e16c96565ac28a9121e278e (diff)
downloadqpdf-b15d0bf6e1305982450879b498a116d3387b705b.tar.zst
Add new method QPDF_Stream::writeStreamJSON
(Replacing the temporary implementation from the last commit.)
-rw-r--r--libqpdf/QPDF_Stream.cc109
-rw-r--r--libqpdf/qpdf/JSON_writer.hh12
2 files changed, 70 insertions, 51 deletions
diff --git a/libqpdf/QPDF_Stream.cc b/libqpdf/QPDF_Stream.cc
index d7087819..6cfcdd46 100644
--- a/libqpdf/QPDF_Stream.cc
+++ b/libqpdf/QPDF_Stream.cc
@@ -178,9 +178,9 @@ QPDF_Stream::unparse()
}
void
-QPDF_Stream::writeJSON(int json_version, JSON::Writer& p)
+QPDF_Stream::writeJSON(int json_version, JSON::Writer& jw)
{
- stream_dict.writeJSON(json_version, p);
+ stream_dict.writeJSON(json_version, jw);
}
JSON
@@ -233,58 +233,65 @@ QPDF_Stream::writeStreamJSON(
break;
}
- auto dict = this->stream_dict;
- JSON result = JSON::makeDictionary();
- if (json_data != qpdf_sj_none) {
- Pl_Discard discard;
- Pl_Buffer buf_pl{"stream data"};
- // buf_pl contains valid data and is ready for retrieval of the data.
- bool buf_pl_ready = false;
- bool filtered = false;
- bool filter = (decode_level != qpdf_dl_none);
- for (int attempt = 1; attempt <= 2; ++attempt) {
- Pipeline* data_pipeline = &discard;
- if (json_data == qpdf_sj_file) {
- // We need to capture the data to write
- data_pipeline = &buf_pl;
- }
- bool succeeded =
- pipeStreamData(data_pipeline, &filtered, 0, decode_level, false, (attempt == 1));
- if (!succeeded || (filter && !filtered)) {
- // Try again
- filter = false;
- decode_level = qpdf_dl_none;
- buf_pl.getString(); // reset buf_pl
- } else {
- buf_pl_ready = true;
- break;
- }
- }
- if (!buf_pl_ready) {
- throw std::logic_error("QPDF_Stream: failed to get stream data");
- }
- // We can use unsafeShallowCopy because we are only touching top-level keys.
- dict = this->stream_dict.unsafeShallowCopy();
- dict.removeKey("/Length");
- if (filter && filtered) {
- dict.removeKey("/Filter");
- dict.removeKey("/DecodeParms");
- }
- if (json_data == qpdf_sj_file) {
- result.addDictionaryMember("datafile", JSON::makeString(data_filename));
-
- p->writeString(buf_pl.getString());
- } else if (json_data == qpdf_sj_inline) {
- if (!no_data_key) {
- result.addDictionaryMember(
- "data", JSON::makeBlob(StreamBlobProvider(this, decode_level)));
- }
+ jw.writeStart('{');
+
+ if (json_data == qpdf_sj_none) {
+ jw.writeNext();
+ jw << R"("dict": )";
+ stream_dict.writeJSON(json_version, jw);
+ jw.writeEnd('}');
+ return decode_level;
+ }
+
+ Pl_Discard discard;
+ Pl_Buffer buf_pl{"stream data"};
+ Pipeline* data_pipeline = &buf_pl;
+ if (no_data_key && json_data == qpdf_sj_inline) {
+ data_pipeline = &discard;
+ }
+ // pipeStreamData produced valid data.
+ bool buf_pl_ready = false;
+ bool filtered = false;
+ bool filter = (decode_level != qpdf_dl_none);
+ for (int attempt = 1; attempt <= 2; ++attempt) {
+ bool succeeded =
+ pipeStreamData(data_pipeline, &filtered, 0, decode_level, false, (attempt == 1));
+ if (!succeeded || (filter && !filtered)) {
+ // Try again
+ filter = false;
+ decode_level = qpdf_dl_none;
+ buf_pl.getString(); // reset buf_pl
} else {
- throw std::logic_error("QPDF_Stream: unexpected value of json_data");
+ buf_pl_ready = true;
+ break;
+ }
+ }
+ if (!buf_pl_ready) {
+ throw std::logic_error("QPDF_Stream: failed to get stream data");
+ }
+ // We can use unsafeShallowCopy because we are only touching top-level keys.
+ auto dict = stream_dict.unsafeShallowCopy();
+ dict.removeKey("/Length");
+ if (filter && filtered) {
+ dict.removeKey("/Filter");
+ dict.removeKey("/DecodeParms");
+ }
+ if (json_data == qpdf_sj_file) {
+ jw.writeNext() << R"("datafile": ")" << JSON::Writer::encode_string(data_filename) << "\"";
+ p->writeString(buf_pl.getString());
+ } else if (json_data == qpdf_sj_inline) {
+ if (!no_data_key) {
+ jw.writeNext() << R"("data": ")";
+ jw.writeBase64(buf_pl.getString()) << "\"";
}
+ } else {
+ throw std::logic_error("QPDF_Stream::writeStreamJSON : unexpected value of json_data");
}
- result.addDictionaryMember("dict", dict.getJSON(json_version));
- jw << std::move(result);
+
+ jw.writeNext() << R"("dict": )";
+ dict.writeJSON(json_version, jw);
+ jw.writeEnd('}');
+
return decode_level;
}
diff --git a/libqpdf/qpdf/JSON_writer.hh b/libqpdf/qpdf/JSON_writer.hh
index 6d870b88..3f770c58 100644
--- a/libqpdf/qpdf/JSON_writer.hh
+++ b/libqpdf/qpdf/JSON_writer.hh
@@ -3,6 +3,8 @@
#include <qpdf/JSON.hh>
#include <qpdf/Pipeline.hh>
+#include <qpdf/Pl_Base64.hh>
+#include <qpdf/Pl_Concatenate.hh>
#include <string_view>
@@ -28,6 +30,16 @@ class JSON::Writer
}
Writer&
+ writeBase64(std::string_view sv)
+ {
+ Pl_Concatenate cat{"writer concat", p};
+ Pl_Base64 base{"writer base64", &cat, Pl_Base64::a_encode};
+ base.write(reinterpret_cast<unsigned char const*>(sv.data()), sv.size());
+ base.finish();
+ return *this;
+ }
+
+ Writer&
writeNext()
{
auto n = indent;