From c58ed1e67d0c0bd056bb5fd42272b0cb46a5a007 Mon Sep 17 00:00:00 2001 From: Anton Thomasson Date: Sat, 13 Jun 2020 20:12:29 +0200 Subject: [PATCH] Add support for encoding collections --- src/ippmsg.cpp | 30 ++++++++++++++++++++++++++++-- src/ippmsg.h | 2 +- 2 files changed, 29 insertions(+), 3 deletions(-) diff --git a/src/ippmsg.cpp b/src/ippmsg.cpp index d54ef26..f48d6bd 100644 --- a/src/ippmsg.cpp +++ b/src/ippmsg.cpp @@ -187,7 +187,7 @@ QJsonValue IppMsg::collect_attributes(QJsonArray& attrs) tmpobj = attrs.takeAt(0).toObject(); if(tmpobj["tag"] == BeginCollection) { - resObj[key] = collect_attributes(attrs); + resObj[key] = QJsonObject {{"tag", BeginCollection}, {"value", collect_attributes(attrs)}}; } else { // This should be general data attributes @@ -333,11 +333,17 @@ QByteArray IppMsg::encode(Operation op) return QByteArray((char*)(ipp.raw()), ipp.size()); } -void IppMsg::encode_attr(Bytestream& msg, quint8 tag, QString name, QJsonValueRef value) +void IppMsg::encode_attr(Bytestream& msg, quint8 tag, QString name, QJsonValue value, bool inCollection) { + if(inCollection) + { + msg << (quint8)MemberName << (quint16)0 << (quint16)name.length() << name.toStdString(); + name = ""; + } msg << tag << quint16(name.length()) << name.toStdString(); + switch (tag) { case OpAttrs: case JobAttrs: @@ -377,6 +383,26 @@ void IppMsg::encode_attr(Bytestream& msg, quint8 tag, QString name, QJsonValueRe msg << (quint16)8 << low << high; break; } + case BeginCollection: + { + msg << (quint16)0; // length of value + if(value.isObject()) + { + QJsonObject collection = value.toObject(); + foreach(QString key, collection.keys()) + { + encode_attr(msg, collection[key].toObject()["tag"].toInt(), key, + collection[key].toObject()["value"], true); + } + } + else + { + // TODO add support for 1-setOf in collections + Q_ASSERT("FIXME-array"); + } + msg << (quint8)EndCollection << (quint16)0 << (quint16)0; + break; + } case OctetStringUnknown: case TextWithLanguage: case NameWithLanguage: diff --git a/src/ippmsg.h b/src/ippmsg.h index 431f43a..c93de29 100644 --- a/src/ippmsg.h +++ b/src/ippmsg.h @@ -85,7 +85,7 @@ private: QJsonArray get_unnamed_attributes(Bytestream& data); QJsonValue collect_attributes(QJsonArray& attrs); QString consume_attribute(QJsonObject& attrs, Bytestream& data); - void encode_attr(Bytestream& msg, quint8 tag, QString name, QJsonValueRef value); + void encode_attr(Bytestream& msg, quint8 tag, QString name, QJsonValue value, bool subCollection=false); quint8 _majVsn; quint8 _minVsn;