7#define CBOR_NO_PARSER_API
8#include <private/qcborcommon_p.h>
10#include <private/qnumeric_p.h>
11#include <private/qstringconverter_p.h>
15#include <qvarlengtharray.h>
20#define CBOR_ENCODER_WRITER_CONTROL 1
21#define CBOR_ENCODER_WRITE_FUNCTION qt_cbor_encoder_write_callback
22#define CBOR_ENCODER_NO_CHECK_USER
25QT_WARNING_DISABLE_MSVC(4334)
27#include <cborencoder.c>
33[[maybe_unused]]
static CborError cbor_encoder_close_container_checked(CborEncoder*,
const CborEncoder*)
35 Q_UNREACHABLE_RETURN(CborErrorInternalError);
40 Q_UNREACHABLE_RETURN(CborErrorInternalError);
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
197 cbor_encoder_init_writer(&encoder, qt_cbor_encoder_write_callback,
this);
206 template <
typename... Args>
void executeAppend(CborError (*f)(CborEncoder *, Args...), Args... args)
208 f(&encoder, std::forward<Args>(args)...);
211 void createContainer(CborError (*f)(CborEncoder *, CborEncoder *, size_t), quint64 len = IndefiniteLength)
213 static_assert(size_t(IndefiniteLength) == CborIndefiniteLength);
214 if (
sizeof(len) !=
sizeof(size_t) && len != IndefiniteLength) {
215 if (Q_UNLIKELY(len >= CborIndefiniteLength)) {
217 qWarning(
"QCborStreamWriter: container of size %llu is too big for a 32-bit build; "
218 "will use indeterminate length instead", len);
219 len = CborIndefiniteLength;
223 containerStack.push(encoder);
224 f(&containerStack.top(), &encoder, len);
229 if (containerStack.isEmpty()) {
230 qWarning(
"QCborStreamWriter: closing map or array that wasn't open");
234 CborEncoder container = containerStack.pop();
235 CborError err = cbor_encoder_close_container(&container, &encoder);
238 if (Q_UNLIKELY(err)) {
239 if (err == CborErrorTooFewItems)
240 qWarning(
"QCborStreamWriter: not enough items added to array or map");
241 else if (err == CborErrorTooManyItems)
242 qWarning(
"QCborStreamWriter: too many items added to array or map");
255 qint64 written = that
->device->write(
static_cast<
const char *>(data), len);
256 return (written == qsizetype(len) ? CborNoError : CborErrorIO);
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276QCborStreamWriter::QCborStreamWriter(QIODevice *device)
277 : d(
new QCborStreamWriterPrivate(device))
281#ifndef QT_BOOTSTRAPPED
283
284
285
286
287
288
289
290
291
292
293
294QCborStreamWriter::QCborStreamWriter(QByteArray *data)
295 : d(
new QCborStreamWriterPrivate(
new QBuffer(data)))
297 d->deleteDevice =
true;
298 d->device->open(QIODevice::WriteOnly | QIODevice::Unbuffered);
303
304
305
306
307
308
309QCborStreamWriter::~QCborStreamWriter()
314
315
316
317
318
319void QCborStreamWriter::setDevice(QIODevice *device)
324 d->deleteDevice =
false;
328
329
330
331
332
333
334
335
336
337QIODevice *QCborStreamWriter::device()
const
343
344
345
346
347
348
349
350
351
352
353void QCborStreamWriter::append(quint64 u)
355 d->executeAppend(cbor_encode_uint, uint64_t(u));
359
360
361
362
363
364
365
366
367
368
369
370void QCborStreamWriter::append(qint64 i)
372 d->executeAppend(cbor_encode_int, int64_t(i));
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394void QCborStreamWriter::append(QCborNegativeInteger n)
396 d->executeAppend(cbor_encode_negative_int, uint64_t(n));
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
424
425
426
427
428
429
430
431
432
433
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455void QCborStreamWriter::append(QLatin1StringView str)
459 if (QtPrivate::isAscii(str)) {
461 appendTextString(str.latin1(), str.size());
464 QVarLengthArray<
char> utf8(str.size() * 2);
465 const qsizetype written = QUtf8::convertFromLatin1(utf8.data(), str) - utf8.data();
466 appendTextString(utf8.data(), written);
471
472
473
474
475
476
477
478
479
480
481
482
483void QCborStreamWriter::append(QStringView str)
485 QByteArray utf8 = str.toUtf8();
486 appendTextString(utf8.constData(), utf8.size());
490
491
492
493
494
495
496
497
498
499
500
501
502void QCborStreamWriter::append(QCborTag tag)
504 d->executeAppend(cbor_encode_tag, CborTag(tag));
508
509
510
511
512
513
514
515
516
517
518
519
520
521
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538void QCborStreamWriter::append(QCborSimpleType st)
540 d->executeAppend(cbor_encode_simple_value, uint8_t(st));
543#ifndef QT_BOOTSTRAPPED
545
546
547
548
549
550
551
552
553
554
555
556void QCborStreamWriter::append(qfloat16 f)
558 d->executeAppend(cbor_encode_half_float,
static_cast<
const void *>(&f));
563
564
565
566
567
568
569
570
571
572
573
574void QCborStreamWriter::append(
float f)
576 d->executeAppend(cbor_encode_float, f);
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599void QCborStreamWriter::append(
double d)
601 this->d->executeAppend(cbor_encode_double, d);
605
606
607
608
609
610
611
612void QCborStreamWriter::appendByteString(
const char *data, qsizetype len)
614 d->executeAppend(cbor_encode_byte_string,
reinterpret_cast<
const uint8_t *>(data), size_t(len));
618
619
620
621
622
623
624
625
626
627
628void QCborStreamWriter::appendTextString(
const char *utf8, qsizetype len)
630 d->executeAppend(cbor_encode_text_string, utf8, size_t(len));
634
635
636
637
638
639
640
641
642
643
644
645
646
647
650
651
652
653
654
655
656
657
658
659
660
661
664
665
666
667
668
669
670
671
672
673
676
677
678
679
680
681
682
683
684
687
688
689
690
691
692
693
694
695
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714void QCborStreamWriter::startArray()
716 d->createContainer(cbor_encoder_create_array);
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746void QCborStreamWriter::startArray(quint64 count)
748 d->createContainer(cbor_encoder_create_array, count);
752
753
754
755
756
757
758
759
760
761
762
763
764
765bool QCborStreamWriter::endArray()
767 return d->closeContainer();
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788void QCborStreamWriter::startMap()
790 d->createContainer(cbor_encoder_create_map);
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820void QCborStreamWriter::startMap(quint64 count)
822 d->createContainer(cbor_encoder_create_map, count);
826
827
828
829
830
831
832
833
834
835
836
837
838
839bool QCborStreamWriter::endMap()
841 return d->closeContainer();
846#undef CBOR_ENCODER_WRITER_CONTROL
847#undef CBOR_ENCODER_WRITE_FUNCTION
848#undef CBOR_ENCODER_NO_CHECK_USER
static constexpr quint64 IndefiniteLength
~QCborStreamWriterPrivate()
QStack< CborEncoder > containerStack
void executeAppend(CborError(*f)(CborEncoder *, Args...), Args... args)
QCborStreamWriterPrivate(QIODevice *device)
void createContainer(CborError(*f)(CborEncoder *, CborEncoder *, size_t), quint64 len=IndefiniteLength)
static QT_BEGIN_NAMESPACE CborError qt_cbor_encoder_write_callback(void *token, const void *data, size_t len, CborEncoderAppendType)
static CborError cbor_encode_float_as_half_float(CborEncoder *, float)
Q_DECLARE_TYPEINFO(QObjectPrivate::ConnectionList, Q_RELOCATABLE_TYPE)