1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
|
/****************************************************************************
**
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the QtDBus module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 3 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL3 included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 3 requirements
** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 2.0 or (at your option) the GNU General
** Public license version 3 or any later version approved by the KDE Free
** Qt Foundation. The licenses are as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-2.0.html and
** https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/
#include "qdbusargument_p.h"
#include "qdbusconnection.h"
#include "qdbusmetatype_p.h"
#include "qdbusutil_p.h"
#ifndef QT_NO_DBUS
QT_BEGIN_NAMESPACE
static void qIterAppend(DBusMessageIter *it, QByteArray *ba, int type, const void *arg)
{
if (ba)
*ba += char(type);
else
q_dbus_message_iter_append_basic(it, type, arg);
}
QDBusMarshaller::~QDBusMarshaller()
{
close();
}
inline QString QDBusMarshaller::currentSignature()
{
if (message)
return QString::fromUtf8(q_dbus_message_get_signature(message));
return QString();
}
inline void QDBusMarshaller::append(uchar arg)
{
if (!skipSignature)
qIterAppend(&iterator, ba, DBUS_TYPE_BYTE, &arg);
}
inline void QDBusMarshaller::append(bool arg)
{
dbus_bool_t cast = arg;
if (!skipSignature)
qIterAppend(&iterator, ba, DBUS_TYPE_BOOLEAN, &cast);
}
inline void QDBusMarshaller::append(short arg)
{
if (!skipSignature)
qIterAppend(&iterator, ba, DBUS_TYPE_INT16, &arg);
}
inline void QDBusMarshaller::append(ushort arg)
{
if (!skipSignature)
qIterAppend(&iterator, ba, DBUS_TYPE_UINT16, &arg);
}
inline void QDBusMarshaller::append(int arg)
{
if (!skipSignature)
qIterAppend(&iterator, ba, DBUS_TYPE_INT32, &arg);
}
inline void QDBusMarshaller::append(uint arg)
{
if (!skipSignature)
qIterAppend(&iterator, ba, DBUS_TYPE_UINT32, &arg);
}
inline void QDBusMarshaller::append(qlonglong arg)
{
if (!skipSignature)
qIterAppend(&iterator, ba, DBUS_TYPE_INT64, &arg);
}
inline void QDBusMarshaller::append(qulonglong arg)
{
if (!skipSignature)
qIterAppend(&iterator, ba, DBUS_TYPE_UINT64, &arg);
}
inline void QDBusMarshaller::append(double arg)
{
if (!skipSignature)
qIterAppend(&iterator, ba, DBUS_TYPE_DOUBLE, &arg);
}
void QDBusMarshaller::append(const QString &arg)
{
QByteArray data = arg.toUtf8();
const char *cdata = data.constData();
if (!skipSignature)
qIterAppend(&iterator, ba, DBUS_TYPE_STRING, &cdata);
}
inline void QDBusMarshaller::append(const QDBusObjectPath &arg)
{
QByteArray data = arg.path().toUtf8();
if (!ba && data.isEmpty()) {
error(QLatin1String("Invalid object path passed in arguments"));
} else {
const char *cdata = data.constData();
if (!skipSignature)
qIterAppend(&iterator, ba, DBUS_TYPE_OBJECT_PATH, &cdata);
}
}
inline void QDBusMarshaller::append(const QDBusSignature &arg)
{
QByteArray data = arg.signature().toUtf8();
if (!ba && data.isEmpty()) {
error(QLatin1String("Invalid signature passed in arguments"));
} else {
const char *cdata = data.constData();
if (!skipSignature)
qIterAppend(&iterator, ba, DBUS_TYPE_SIGNATURE, &cdata);
}
}
inline void QDBusMarshaller::append(const QDBusUnixFileDescriptor &arg)
{
int fd = arg.fileDescriptor();
if (!ba && fd == -1) {
error(QLatin1String("Invalid file descriptor passed in arguments"));
} else {
if (!skipSignature)
qIterAppend(&iterator, ba, DBUS_TYPE_UNIX_FD, &fd);
}
}
inline void QDBusMarshaller::append(const QByteArray &arg)
{
if (ba) {
if (!skipSignature)
*ba += DBUS_TYPE_ARRAY_AS_STRING DBUS_TYPE_BYTE_AS_STRING;
return;
}
const char* cdata = arg.constData();
DBusMessageIter subiterator;
q_dbus_message_iter_open_container(&iterator, DBUS_TYPE_ARRAY, DBUS_TYPE_BYTE_AS_STRING,
&subiterator);
q_dbus_message_iter_append_fixed_array(&subiterator, DBUS_TYPE_BYTE, &cdata, arg.length());
q_dbus_message_iter_close_container(&iterator, &subiterator);
}
inline bool QDBusMarshaller::append(const QDBusVariant &arg)
{
if (ba) {
if (!skipSignature)
*ba += DBUS_TYPE_VARIANT_AS_STRING;
return true;
}
const QVariant &value = arg.variant();
int id = value.userType();
if (id == QVariant::Invalid) {
qWarning("QDBusMarshaller: cannot add a null QDBusVariant");
error(QLatin1String("Variant containing QVariant::Invalid passed in arguments"));
return false;
}
QByteArray tmpSignature;
const char *signature = 0;
if (id == QDBusMetaTypeId::argument()) {
// take the signature from the QDBusArgument object we're marshalling
tmpSignature =
qvariant_cast<QDBusArgument>(value).currentSignature().toLatin1();
signature = tmpSignature.constData();
} else {
// take the signatuer from the metatype we're marshalling
signature = QDBusMetaType::typeToSignature(id);
}
if (!signature) {
qWarning("QDBusMarshaller: type `%s' (%d) is not registered with D-BUS. "
"Use qDBusRegisterMetaType to register it",
QMetaType::typeName(id), id);
error(QString::fromLatin1("Unregistered type %1 passed in arguments")
.arg(QLatin1String(QMetaType::typeName(id))));
return false;
}
QDBusMarshaller sub(capabilities);
open(sub, DBUS_TYPE_VARIANT, signature);
bool isOk = sub.appendVariantInternal(value);
// don't call sub.close(): it auto-closes
return isOk;
}
inline void QDBusMarshaller::append(const QStringList &arg)
{
if (ba) {
if (!skipSignature)
*ba += DBUS_TYPE_ARRAY_AS_STRING DBUS_TYPE_STRING_AS_STRING;
return;
}
QDBusMarshaller sub(capabilities);
open(sub, DBUS_TYPE_ARRAY, DBUS_TYPE_STRING_AS_STRING);
QStringList::ConstIterator it = arg.constBegin();
QStringList::ConstIterator end = arg.constEnd();
for ( ; it != end; ++it)
sub.append(*it);
// don't call sub.close(): it auto-closes
}
inline QDBusMarshaller *QDBusMarshaller::beginStructure()
{
return beginCommon(DBUS_TYPE_STRUCT, 0);
}
inline QDBusMarshaller *QDBusMarshaller::beginArray(int id)
{
const char *signature = QDBusMetaType::typeToSignature( QVariant::Type(id) );
if (!signature) {
qWarning("QDBusMarshaller: type `%s' (%d) is not registered with D-BUS. "
"Use qDBusRegisterMetaType to register it",
QMetaType::typeName(id), id);
error(QString::fromLatin1("Unregistered type %1 passed in arguments")
.arg(QLatin1String(QMetaType::typeName(id))));
return this;
}
return beginCommon(DBUS_TYPE_ARRAY, signature);
}
inline QDBusMarshaller *QDBusMarshaller::beginMap(int kid, int vid)
{
const char *ksignature = QDBusMetaType::typeToSignature( QVariant::Type(kid) );
if (!ksignature) {
qWarning("QDBusMarshaller: type `%s' (%d) is not registered with D-BUS. "
"Use qDBusRegisterMetaType to register it",
QMetaType::typeName(kid), kid);
error(QString::fromLatin1("Unregistered type %1 passed in arguments")
.arg(QLatin1String(QMetaType::typeName(kid))));
return this;
}
if (ksignature[1] != 0 || !QDBusUtil::isValidBasicType(*ksignature)) {
qWarning("QDBusMarshaller: type '%s' (%d) cannot be used as the key type in a D-BUS map.",
QMetaType::typeName(kid), kid);
error(QString::fromLatin1("Type %1 passed in arguments cannot be used as a key in a map")
.arg(QLatin1String(QMetaType::typeName(kid))));
return this;
}
const char *vsignature = QDBusMetaType::typeToSignature( QVariant::Type(vid) );
if (!vsignature) {
const char *typeName = QMetaType::typeName(vid);
qWarning("QDBusMarshaller: type `%s' (%d) is not registered with D-BUS. "
"Use qDBusRegisterMetaType to register it",
typeName, vid);
error(QString::fromLatin1("Unregistered type %1 passed in arguments")
.arg(QLatin1String(typeName)));
return this;
}
QByteArray signature;
signature = DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING;
signature += ksignature;
signature += vsignature;
signature += DBUS_DICT_ENTRY_END_CHAR_AS_STRING;
return beginCommon(DBUS_TYPE_ARRAY, signature);
}
inline QDBusMarshaller *QDBusMarshaller::beginMapEntry()
{
return beginCommon(DBUS_TYPE_DICT_ENTRY, 0);
}
void QDBusMarshaller::open(QDBusMarshaller &sub, int code, const char *signature)
{
sub.parent = this;
sub.ba = ba;
sub.ok = true;
sub.capabilities = capabilities;
sub.skipSignature = skipSignature;
if (ba) {
if (!skipSignature) {
switch (code) {
case DBUS_TYPE_ARRAY:
*ba += char(code);
*ba += signature;
// fall through
case DBUS_TYPE_DICT_ENTRY:
sub.closeCode = 0;
sub.skipSignature = true;
break;
case DBUS_TYPE_STRUCT:
*ba += DBUS_STRUCT_BEGIN_CHAR;
sub.closeCode = DBUS_STRUCT_END_CHAR;
break;
}
}
} else {
q_dbus_message_iter_open_container(&iterator, code, signature, &sub.iterator);
}
}
QDBusMarshaller *QDBusMarshaller::beginCommon(int code, const char *signature)
{
QDBusMarshaller *d = new QDBusMarshaller(capabilities);
open(*d, code, signature);
return d;
}
inline QDBusMarshaller *QDBusMarshaller::endStructure()
{ return endCommon(); }
inline QDBusMarshaller *QDBusMarshaller::endArray()
{ return endCommon(); }
inline QDBusMarshaller *QDBusMarshaller::endMap()
{ return endCommon(); }
inline QDBusMarshaller *QDBusMarshaller::endMapEntry()
{ return endCommon(); }
QDBusMarshaller *QDBusMarshaller::endCommon()
{
QDBusMarshaller *retval = parent;
delete this;
return retval;
}
void QDBusMarshaller::close()
{
if (ba) {
if (!skipSignature && closeCode)
*ba += closeCode;
} else if (parent) {
q_dbus_message_iter_close_container(&parent->iterator, &iterator);
}
}
void QDBusMarshaller::error(const QString &msg)
{
ok = false;
if (parent)
parent->error(msg);
else
errorString = msg;
}
bool QDBusMarshaller::appendVariantInternal(const QVariant &arg)
{
int id = arg.userType();
if (id == QVariant::Invalid) {
qWarning("QDBusMarshaller: cannot add an invalid QVariant");
error(QLatin1String("Variant containing QVariant::Invalid passed in arguments"));
return false;
}
// intercept QDBusArgument parameters here
if (id == QDBusMetaTypeId::argument()) {
QDBusArgument dbusargument = qvariant_cast<QDBusArgument>(arg);
QDBusArgumentPrivate *d = QDBusArgumentPrivate::d(dbusargument);
if (!d->message)
return false; // can't append this one...
QDBusDemarshaller demarshaller(capabilities);
demarshaller.message = q_dbus_message_ref(d->message);
if (d->direction == Demarshalling) {
// it's demarshalling; just copy
demarshaller.iterator = static_cast<QDBusDemarshaller *>(d)->iterator;
} else {
// it's marshalling; start over
if (!q_dbus_message_iter_init(demarshaller.message, &demarshaller.iterator))
return false; // error!
}
return appendCrossMarshalling(&demarshaller);
}
const char *signature = QDBusMetaType::typeToSignature( QVariant::Type(id) );
if (!signature) {
qWarning("QDBusMarshaller: type `%s' (%d) is not registered with D-BUS. "
"Use qDBusRegisterMetaType to register it",
QMetaType::typeName(id), id);
error(QString::fromLatin1("Unregistered type %1 passed in arguments")
.arg(QLatin1String(QMetaType::typeName(id))));
return false;
}
switch (*signature) {
#ifdef __OPTIMIZE__
case DBUS_TYPE_BYTE:
case DBUS_TYPE_INT16:
case DBUS_TYPE_UINT16:
case DBUS_TYPE_INT32:
case DBUS_TYPE_UINT32:
case DBUS_TYPE_INT64:
case DBUS_TYPE_UINT64:
case DBUS_TYPE_DOUBLE:
qIterAppend(&iterator, ba, *signature, arg.constData());
return true;
case DBUS_TYPE_BOOLEAN:
append( arg.toBool() );
return true;
#else
case DBUS_TYPE_BYTE:
append( qvariant_cast<uchar>(arg) );
return true;
case DBUS_TYPE_BOOLEAN:
append( arg.toBool() );
return true;
case DBUS_TYPE_INT16:
append( qvariant_cast<short>(arg) );
return true;
case DBUS_TYPE_UINT16:
append( qvariant_cast<ushort>(arg) );
return true;
case DBUS_TYPE_INT32:
append( static_cast<dbus_int32_t>(arg.toInt()) );
return true;
case DBUS_TYPE_UINT32:
append( static_cast<dbus_uint32_t>(arg.toUInt()) );
return true;
case DBUS_TYPE_INT64:
append( arg.toLongLong() );
return true;
case DBUS_TYPE_UINT64:
append( arg.toULongLong() );
return true;
case DBUS_TYPE_DOUBLE:
append( arg.toDouble() );
return true;
#endif
case DBUS_TYPE_STRING:
append( arg.toString() );
return true;
case DBUS_TYPE_OBJECT_PATH:
append( qvariant_cast<QDBusObjectPath>(arg) );
return true;
case DBUS_TYPE_SIGNATURE:
append( qvariant_cast<QDBusSignature>(arg) );
return true;
// compound types:
case DBUS_TYPE_VARIANT:
// nested QVariant
return append( qvariant_cast<QDBusVariant>(arg) );
case DBUS_TYPE_ARRAY:
// could be many things
// find out what kind of array it is
switch (arg.type()) {
case QVariant::StringList:
append( arg.toStringList() );
return true;
case QVariant::ByteArray:
append( arg.toByteArray() );
return true;
default:
; // fall through
}
// fall through
case DBUS_TYPE_STRUCT:
case DBUS_STRUCT_BEGIN_CHAR:
return appendRegisteredType( arg );
case DBUS_TYPE_DICT_ENTRY:
case DBUS_DICT_ENTRY_BEGIN_CHAR:
qFatal("QDBusMarshaller::appendVariantInternal got a DICT_ENTRY!");
return false;
case DBUS_TYPE_UNIX_FD:
if (capabilities & QDBusConnection::UnixFileDescriptorPassing || ba) {
append(qvariant_cast<QDBusUnixFileDescriptor>(arg));
return true;
}
// fall through
default:
qWarning("QDBusMarshaller::appendVariantInternal: Found unknown D-BUS type '%s'",
signature);
return false;
}
return true;
}
bool QDBusMarshaller::appendRegisteredType(const QVariant &arg)
{
ref.ref(); // reference up
QDBusArgument self(QDBusArgumentPrivate::create(this));
return QDBusMetaType::marshall(self, arg.userType(), arg.constData());
}
bool QDBusMarshaller::appendCrossMarshalling(QDBusDemarshaller *demarshaller)
{
int code = q_dbus_message_iter_get_arg_type(&demarshaller->iterator);
if (QDBusUtil::isValidBasicType(code)) {
// easy: just append
// do exactly like the D-BUS docs suggest
// (see apidocs for q_dbus_message_iter_get_basic)
qlonglong value;
q_dbus_message_iter_get_basic(&demarshaller->iterator, &value);
q_dbus_message_iter_next(&demarshaller->iterator);
q_dbus_message_iter_append_basic(&iterator, code, &value);
return true;
}
if (code == DBUS_TYPE_ARRAY) {
int element = q_dbus_message_iter_get_element_type(&demarshaller->iterator);
if (QDBusUtil::isValidFixedType(element) && element != DBUS_TYPE_UNIX_FD) {
// another optimization: fixed size arrays
// code is exactly like QDBusDemarshaller::toByteArray
DBusMessageIter sub;
q_dbus_message_iter_recurse(&demarshaller->iterator, &sub);
q_dbus_message_iter_next(&demarshaller->iterator);
int len;
void* data;
q_dbus_message_iter_get_fixed_array(&sub,&data,&len);
char signature[2] = { char(element), 0 };
q_dbus_message_iter_open_container(&iterator, DBUS_TYPE_ARRAY, signature, &sub);
q_dbus_message_iter_append_fixed_array(&sub, element, &data, len);
q_dbus_message_iter_close_container(&iterator, &sub);
return true;
}
}
// We have to recurse
QDBusDemarshaller *drecursed = demarshaller->beginCommon();
QDBusMarshaller mrecursed(capabilities); // create on the stack makes it autoclose
QByteArray subSignature;
const char *sig = 0;
if (code == DBUS_TYPE_VARIANT || code == DBUS_TYPE_ARRAY) {
subSignature = drecursed->currentSignature().toLatin1();
if (!subSignature.isEmpty())
sig = subSignature.constData();
}
open(mrecursed, code, sig);
while (!drecursed->atEnd()) {
if (!mrecursed.appendCrossMarshalling(drecursed)) {
delete drecursed;
return false;
}
}
delete drecursed;
return true;
}
QT_END_NAMESPACE
#endif // QT_NO_DBUS
|