9#include "private/qtools_p.h"
14#include "private/qnumeric_p.h"
15#include "private/qsimd_p.h"
19#include <qdatastream.h>
22#include "private/qstdweb_p.h"
24#include <QtCore/private/qtclasshelper_p.h>
29#include <qxpfunctional.h>
38#include <QtCore/q26numeric.h>
42# if !defined(QT_BOOTSTRAPPED) && (defined(QT_NO_CAST_FROM_ASCII) || defined(QT_NO_CAST_FROM_BYTEARRAY))
44# error "This file cannot be compiled with QT_NO_CAST_{TO,FROM}_ASCII, "
45 "otherwise some QByteArray functions will not get exported."
51Q_CONSTINIT
const char QByteArray::_empty =
'\0';
56 return c >=
'a' && c <=
'z' ? c & ~0x20 : c;
61 return c >=
'A' && c <=
'Z' ? c | 0x20 : c;
65
66
69
70
71
72
73
74
75
76
77const void *
qmemrchr(
const void *s,
int needle, size_t size)
noexcept
80 return memrchr(s, needle, size);
82 auto b =
static_cast<
const uchar *>(s);
83 const uchar *n = b + size;
85 if (*n == uchar(needle))
93
94
95
96
97
98
99
100
101
102
108 char *dst =
new char[strlen(src) + 1];
109 return qstrcpy(dst, src);
113
114
115
116
117
118
119
120
121
122
123
124
131 const size_t len = strlen(src);
134 if (len >= 0 && strcpy_s(dst, len+1, src) == 0)
138 return strcpy(dst, src);
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
163char *
qstrncpy(
char *dst,
const char *src, size_t len)
165 if (dst && len > 0) {
168 std::strncat(dst, src, len - 1);
170 return src ? dst :
nullptr;
174
175
176
177
178
179
180
181
182
185
186
187
188
189
190
191
192
193
194
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212int qstrcmp(
const char *str1,
const char *str2)
214 return (str1 && str2) ? strcmp(str1, str2)
215 : (str1 ? 1 : (str2 ? -1 : 0));
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
259 const uchar *s1 =
reinterpret_cast<
const uchar *>(str1);
260 const uchar *s2 =
reinterpret_cast<
const uchar *>(str2);
266 enum { Incomplete = 256 };
268 auto innerCompare = [=, &offset](qptrdiff max,
bool unlimited) {
271 uchar c = s1[offset];
272 if (
int res = QtMiscUtils::caseCompareAscii(c, s2[offset]))
277 }
while (unlimited || offset < max);
278 return int(Incomplete);
281#if defined(__SSE4_1__) && !(defined(__SANITIZE_ADDRESS__) || __has_feature(address_sanitizer))
282 enum { PageSize = 4096, PageMask = PageSize - 1 };
283 const __m128i zero = _mm_setzero_si128();
288 quintptr u1 = quintptr(s1 + offset);
289 quintptr u2 = quintptr(s2 + offset);
290 size_t n = PageSize - ((u1 | u2) & PageMask);
292 qptrdiff maxoffset = offset + n;
293 for ( ; offset + 16 <= maxoffset; offset +=
sizeof(__m128i)) {
295 __m128i a = _mm_loadu_si128(
reinterpret_cast<
const __m128i *>(s1 + offset));
296 __m128i b = _mm_loadu_si128(
reinterpret_cast<
const __m128i *>(s2 + offset));
299 __m128i cmp = _mm_cmpeq_epi8(a, b);
302 cmp = _mm_min_epu8(cmp, a);
303 cmp = _mm_cmpeq_epi8(cmp, zero);
306 uint mask = _mm_movemask_epi8(cmp);
309 uint start = qCountTrailingZeroBits(mask);
310 uint end =
sizeof(mask) * 8 - qCountLeadingZeroBits(mask);
311 Q_ASSERT(end >= start);
319 int res = innerCompare(n,
false);
320 if (res != Incomplete)
325 return innerCompare(-1,
true);
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
349
350
351
352
353
354
355
356
359
360
361int QtPrivate::compareMemory(QByteArrayView lhs, QByteArrayView rhs)
363 if (!lhs.isNull() && !rhs.isNull()) {
364 int ret = memcmp(lhs.data(), rhs.data(), qMin(lhs.size(), rhs.size()));
371 return lhs.size() == rhs.size() ? 0 : lhs.size() > rhs.size() ? 1 : -1;
375
376
379 return QUtf8::isValidUtf8(s).isValidUtf8;
384static void createCRC16Table()
388 unsigned short crc_tbl[16];
389 unsigned int v0, v1, v2, v3;
390 for (i = 0; i < 16; i++) {
397#define SET_BIT(x, b, v) (x) |= (v) << (b)
412 printf(
"static const quint16 crc_tbl[16] = {\n");
413 for (
int i = 0; i < 16; i +=4)
414 printf(
" 0x%04x, 0x%04x, 0x%04x, 0x%04x,\n", crc_tbl[i], crc_tbl[i+1], crc_tbl[i+2], crc_tbl[i+3]);
420 0x0000, 0x1081, 0x2102, 0x3183,
421 0x4204, 0x5285, 0x6306, 0x7387,
422 0x8408, 0x9489, 0xa50a, 0xb58b,
423 0xc60c, 0xd68d, 0xe70e, 0xf78f
427
428
429
430
431
432
433
434
435
436
437
438
441 quint16 crc = 0x0000;
443 case Qt::ChecksumIso3309:
446 case Qt::ChecksumItuV41:
451 const uchar *p =
reinterpret_cast<
const uchar *>(data.data());
452 qsizetype len = data.size();
455 crc = ((crc >> 4) & 0x0fff) ^ crc_tbl[((crc ^ c) & 15)];
457 crc = ((crc >> 4) & 0x0fff) ^ crc_tbl[((crc ^ c) & 15)];
460 case Qt::ChecksumIso3309:
463 case Qt::ChecksumItuV41:
471
472
473
474
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
497
498
499
500
501
502
503
504
506#ifndef QT_NO_COMPRESS
507using CompressSizeHint_t = quint32;
518 Q_UNREACHABLE_RETURN(
nullptr);
522static QByteArray zlibError(ZLibOp op,
const char *what)
524 qWarning(
"%s: %s", zlibOpAsString(op), what);
529static QByteArray dataIsNull(ZLibOp op)
531 return zlibError(op,
"Data is null");
535static QByteArray lengthIsNegative(ZLibOp op)
537 return zlibError(op,
"Input length is negative");
541static QByteArray tooMuchData(ZLibOp op)
543 return zlibError(op,
"Not enough memory");
547static QByteArray invalidCompressedData()
549 return zlibError(ZLibOp::Decompression,
"Input data is corrupted");
553static QByteArray unexpectedZlibError(ZLibOp op,
int err,
const char *msg)
555 qWarning(
"%s unexpected zlib error: %s (%d)",
567 if (out.data() ==
nullptr)
568 return tooMuchData(op);
569 qsizetype capacity = out.allocatedCapacity();
571 const auto initalSize = out.size;
574 zs.next_in =
reinterpret_cast<uchar *>(
const_cast<
char *>(input.data()));
575 if (
const int err = init(&zs); err != Z_OK)
576 return unexpectedZlibError(op, err, zs.msg);
577 const auto sg = qScopeGuard([&] { deinit(&zs); });
579 using ZlibChunkSize_t =
decltype(zs.avail_in);
580 static_assert(!std::is_signed_v<ZlibChunkSize_t>);
581 static_assert(std::is_same_v<ZlibChunkSize_t,
decltype(zs.avail_out)>);
582 constexpr auto MaxChunkSize =
std::numeric_limits<ZlibChunkSize_t>::max();
584 constexpr auto MaxStatisticsSize =
std::numeric_limits<
decltype(zs.total_out)>::max();
586 size_t inputLeft = size_t(input.size());
590 Q_ASSERT(out.freeSpaceAtBegin() == 0);
591 Q_ASSERT(capacity == out.allocatedCapacity());
593 if (zs.avail_out == 0) {
594 Q_ASSERT(size_t(out.size) - initalSize > MaxStatisticsSize ||
595 size_t(out.size) - initalSize == zs.total_out);
596 Q_ASSERT(out.size <= capacity);
598 qsizetype avail_out = capacity - out.size;
599 if (avail_out == 0) {
600 out.reallocateAndGrow(QArrayData::GrowsAtEnd, 1);
601 if (out.data() ==
nullptr)
602 return tooMuchData(op);
603 capacity = out.allocatedCapacity();
604 avail_out = capacity - out.size;
606 zs.next_out =
reinterpret_cast<uchar *>(out.data()) + out.size;
607 zs.avail_out = size_t(avail_out) > size_t(MaxChunkSize) ? MaxChunkSize
608 : ZlibChunkSize_t(avail_out);
609 out.size += zs.avail_out;
611 Q_ASSERT(zs.avail_out > 0);
614 if (zs.avail_in == 0) {
616 zs.avail_in = inputLeft > MaxChunkSize ? MaxChunkSize : ZlibChunkSize_t(inputLeft);
617 inputLeft -= zs.avail_in;
620 res = processChunk(&zs, inputLeft);
621 }
while (res == Z_OK);
625 out.size -= zs.avail_out;
626 Q_ASSERT(size_t(out.size) - initalSize > MaxStatisticsSize ||
627 size_t(out.size) - initalSize == zs.total_out);
628 Q_ASSERT(out.size <= out.allocatedCapacity());
629 out.data()[out.size] =
'\0';
630 return QByteArray(
std::move(out));
633 return tooMuchData(op);
638 return invalidCompressedData();
641 return unexpectedZlibError(op, res, zs.msg);
647 constexpr qsizetype HeaderSize =
sizeof(CompressSizeHint_t);
649 return QByteArray(HeaderSize,
'\0');
657 if (compressionLevel < -1 || compressionLevel > 9)
658 compressionLevel = -1;
660 QArrayDataPointer out = [&] {
661 constexpr qsizetype SingleAllocLimit = 256 * 1024;
665 qsizetype capacity = HeaderSize;
666 if (nbytes < SingleAllocLimit) {
668 capacity += compressBound(uLong(nbytes));
669 return QArrayDataPointer<
char>(capacity);
674 constexpr qsizetype MaxCompressionFactor = 1024;
677 capacity +=
std::max(qsizetype(compressBound(uLong(SingleAllocLimit))),
678 nbytes / MaxCompressionFactor);
679 return QArrayDataPointer<
char>(capacity, 0, QArrayData::Grow);
682 if (out.data() ==
nullptr)
685 qToBigEndian(q26::saturating_cast<CompressSizeHint_t>(nbytes), out.data());
686 out.size = HeaderSize;
688 return xxflate(ZLibOp::Compression, std::move(out), {data, nbytes},
689 [=] (z_stream *zs) {
return deflateInit(zs, compressionLevel); },
690 [] (z_stream *zs, size_t inputLeft) {
691 return deflate(zs, inputLeft ? Z_NO_FLUSH : Z_FINISH);
693 [] (z_stream *zs) { deflateEnd(zs); });
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
729#ifndef QT_NO_COMPRESS
731
732
733
734
735
736
745 constexpr qsizetype HeaderSize =
sizeof(CompressSizeHint_t);
746 if (nbytes < HeaderSize)
747 return invalidCompressedData();
749 auto expectedSize = qFromBigEndian<CompressSizeHint_t>(data);
750 if (nbytes == HeaderSize) {
751 if (expectedSize != 0)
752 return invalidCompressedData();
756 constexpr auto MaxDecompressedSize = size_t(QByteArray::maxSize());
762 constexpr size_t MaxCompressionRatio = 1032;
763 constexpr size_t Slack = 1024;
764 size_t maxPlausibleSize;
765 if (qMulOverflow(size_t(nbytes - HeaderSize), MaxCompressionRatio, &maxPlausibleSize)
766 || qAddOverflow(maxPlausibleSize, Slack, &maxPlausibleSize)) {
767 maxPlausibleSize = MaxDecompressedSize;
769 expectedSize = CompressSizeHint_t(
std::min(size_t(expectedSize), maxPlausibleSize));
771 if constexpr (MaxDecompressedSize < std::numeric_limits<CompressSizeHint_t>::max()) {
772 if (expectedSize > MaxDecompressedSize)
778 qsizetype capacity =
std::max(qsizetype(expectedSize),
781 QArrayDataPointer<
char> d(capacity);
782 return xxflate(ZLibOp::Decompression, std::move(d), {data + HeaderSize, nbytes - HeaderSize},
783 [] (z_stream *zs) {
return inflateInit(zs); },
784 [] (z_stream *zs, size_t) {
return inflate(zs, Z_NO_FLUSH); },
785 [] (z_stream *zs) { inflateEnd(zs); });
790
791
792
793
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
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1086
1087
1088
1091
1092
1093
1094
1095
1096
1097
1098
1099
1102
1103
1104
1105
1106
1107
1108
1109
1112
1113
1114
1115
1116
1117
1118
1119
1122
1123
1124
1127
1128
1129
1130
1131
1132
1133
1134
1135
1138
1139
1140
1141
1142
1143
1144
1145
1148
1149
1150
1151
1152
1153
1154
1155
1156
1159
1160
1161
1164
1165
1166
1167
1168
1169
1170
1171
1172
1175
1176
1177
1178
1179
1180
1181
1182
1183
1186
1187
1188
1191
1192
1193
1194
1195
1196
1197
1198
1199
1202
1203
1204
1205
1208
1209
1210
1211
1212
1215
1216
1217
1218
1219
1222
1223
1224
1225
1226
1229
1230
1231
1232
1235
1236
1237
1238
1239
1242
1243
1244
1245
1246
1249
1250
1251
1252
1253
1256
1257
1258
1259
1260
1263
1264
1265
1266
1267
1268
1269QByteArray::iterator
QByteArray::erase(QByteArray::const_iterator first, QByteArray::const_iterator last)
1271 const auto start =
std::distance(cbegin(), first);
1272 const auto len =
std::distance(first, last);
1274 return begin() + start;
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1306
1307
1308
1309
1310
1311
1312
1315
1316
1317
1318
1319
1322
1323
1326
1327
1328
1329
1332
1333
1334
1335
1336
1337
1338
1345 d = DataPointer::fromRawData(&_empty, 0);
1353
1354
1355
1356
1357
1358
1361
1362
1363
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1384
1385
1386
1387
1388
1389
1390
1393
1394
1395
1396
1397
1398
1399
1400
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1450
1451
1452
1453
1454
1455
1456
1457
1458
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1488
1489
1490
1491
1492
1493
1494
1495
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1533
1534
1535
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1556
1557
1558
1561
1562
1563
1566
1567
1568
1571
1572
1573
1574
1575
1576
1577
1578
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1594
1595
1596
1597
1598
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1661
1662
1663
1664
1665
1666
1667
1670
1671
1672
1673
1674
1675
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1741
1742
1743
1744
1745
1746
1749
1750
1751
1752
1753
1754
1757
1758
1759
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1777
1778
1779
1780
1781
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1803 size = qstrlen(data);
1805 d = DataPointer::fromRawData(&_empty, 0);
1807 d = DataPointer(size, size);
1808 Q_CHECK_PTR(d.data());
1809 memcpy(d.data(), data, size);
1810 d.data()[size] =
'\0';
1816
1817
1818
1819
1824 d = DataPointer::fromRawData(&_empty, 0);
1826 d = DataPointer(size, size);
1827 Q_CHECK_PTR(d.data());
1828 memset(d.data(), ch, size);
1829 d.data()[size] =
'\0';
1834
1835
1836
1837
1838
1839
1840
1845 d = DataPointer::fromRawData(&_empty, 0);
1847 d = DataPointer(size, size);
1848 Q_CHECK_PTR(d.data());
1849 d.data()[size] =
'\0';
1854
1855
1856
1857
1858
1859
1860
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1882 const auto capacityAtEnd = capacity() - d.freeSpaceAtBegin();
1883 if (d.needsDetach() || size > capacityAtEnd)
1884 reallocData(size, QArrayData::Grow);
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1909 const auto old = d.size;
1912 memset(d.data() + old, c, d.size - old);
1916
1917
1918
1919
1920
1921
1922
1923
1924
1931
1932
1933
1934
1935
1936
1937
1941 memset(begin(), ch, size());
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1960 resizeForOverwrite(size);
1964void QByteArray::reallocData(qsizetype alloc, QArrayData::AllocationOption option)
1967 d = DataPointer::fromRawData(&_empty, 0);
1973 const bool cannotUseReallocate = d.freeSpaceAtBegin() > 0;
1975 if (d.needsDetach() || cannotUseReallocate) {
1976 DataPointer dd(alloc, qMin(alloc, d.size), option);
1977 Q_CHECK_PTR(dd.data());
1979 ::memcpy(dd.data(), d.data(), dd.size);
1980 dd.data()[dd.size] = 0;
1983 d->reallocate(alloc, option);
1992 if (d.needsDetach()) {
1993 DataPointer dd(DataPointer::allocateGrow(d, n, QArrayData::GrowsAtEnd));
1994 Q_CHECK_PTR(dd.data());
1995 dd->copyAppend(d.data(), d.data() + d.size);
1996 dd.data()[dd.size] = 0;
1999 d->reallocate(d.constAllocatedCapacity() + n, QArrayData::Grow);
2005 resize(qMax(i + 1, size()));
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2030
2031
2032
2033
2034
2035
2036
2037
2038
2050 return std::move(*
this);
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2072
2073
2074
2075
2076
2079 if (size() == 0 && ba.size() > d.constAllocatedCapacity() && ba.d.isMutable())
2080 return (*
this = ba);
2081 return prepend(QByteArrayView(ba));
2085
2086
2087
2088
2089
2092
2093
2094
2095
2096
2097
2098
2101
2102
2103
2104
2105
2106
2109
2110
2111
2112
2113
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2143 if (Q_UNLIKELY(!ba.d.isMutable()))
2147 }
else if (ba.size()) {
2148 append(QByteArrayView(ba));
2155
2156
2157
2158
2159
2162
2163
2164
2165
2166
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2185
2186
2187
2188
2189
2190
2191
2192
2193
2196
2197
2198
2199
2203 d.detachAndGrow(QArrayData::GrowsAtEnd, 1,
nullptr,
nullptr);
2204 d->copyAppend(1, ch);
2205 d.data()[d.size] =
'\0';
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2260 const auto len = v.size();
2262 if (len <= capacity() && isDetached()) {
2263 const auto offset = d.freeSpaceAtBegin();
2265 d.setBegin(d.begin() - offset);
2267 std::memcpy(d.begin(), v.data(), len);
2269 d.data()[d.size] =
'\0';
2271 *
this = v.toByteArray();
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2298 const char *str = data.data();
2299 qsizetype size = data.size();
2300 if (i < 0 || size <= 0)
2309 DataPointer detached{};
2310 d.detachAndGrow(Data::GrowsAtEnd, (i - d.size) + size, &str, &detached);
2311 Q_CHECK_PTR(d.data());
2312 d->copyAppend(i - d.size,
' ');
2313 d->copyAppend(str, str + size);
2314 d.data()[d.size] =
'\0';
2318 if (!d.needsDetach() && QtPrivate::q_points_into_range(str, d)) {
2319 QVarLengthArray a(str, str + size);
2320 return insert(i, a);
2323 d->insert(i, str, size);
2324 d.data()[d.size] =
'\0';
2329
2330
2331
2332
2333
2334
2335
2336
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2351
2352
2353
2354
2355
2356
2357
2358
2359
2362
2363
2364
2365
2366
2367
2368
2371
2372
2373
2374
2375
2376
2377
2378
2379
2383 if (i < 0 || count <= 0)
2388 d.detachAndGrow(Data::GrowsAtEnd, (i - d.size) + count,
nullptr,
nullptr);
2389 Q_CHECK_PTR(d.data());
2390 d->copyAppend(i - d.size,
' ');
2391 d->copyAppend(count, ch);
2392 d.data()[d.size] =
'\0';
2396 d->insert(i, count, ch);
2397 d.data()[d.size] =
'\0';
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2421 if (len <= 0 || pos < 0 || size_t(pos) >= size_t(size()))
2423 if (pos + len > d.size)
2426 QStringAlgorithms<QByteArray>::remove_helper(*
this, pos, len);
2431
2432
2433
2434
2435
2436
2437
2438
2439
2442
2443
2444
2445
2446
2447
2448
2449
2450
2452
2453
2454
2455
2456
2457
2458
2459
2460
2463
2464
2465
2466
2467
2468
2469
2470
2473
2474
2475
2476
2477
2478
2479
2480
2484 if (size_t(pos) > size_t(
this->size()))
2486 if (len >
this->size() - pos)
2487 len =
this->size() - pos;
2492 return insert(pos, after);
2494 if (after.isEmpty())
2495 return remove(pos, len);
2497 using A = QStringAlgorithms<QByteArray>;
2498 const qsizetype newlen = A::newSize(*
this, len, after, {pos});
2499 if (data_ptr().needsDetach() || A::needsReallocate(*
this, newlen)) {
2500 A::replace_into_copy(*
this, len, after, {pos}, newlen);
2505 char *
const begin = data_ptr().data();
2506 char *
const before = begin + pos;
2507 const char *beforeEnd = before + len;
2508 if (len >= after.size()) {
2509 memmove(before , after.cbegin(), after.size());
2511 if (len > after.size()) {
2512 memmove(before + after.size(), beforeEnd, d.size - (beforeEnd - begin));
2513 A::setSize(*
this, newlen);
2516 char *oldEnd = begin + d.size;
2517 const qsizetype adjust = newlen - d.size;
2518 A::setSize(*
this, newlen);
2520 QByteArrayView tail{beforeEnd, oldEnd};
2521 QByteArrayView prefix = after;
2522 QByteArrayView suffix;
2523 if (QtPrivate::q_points_into_range(after.cend() - 1, tail)) {
2524 if (QtPrivate::q_points_into_range(after.cbegin(), tail)) {
2527 suffix = QByteArrayView{after.cbegin(), after.cend()};
2529 prefix = QByteArrayView{after.cbegin(), beforeEnd};
2530 suffix = QByteArrayView{beforeEnd, after.cend()};
2533 memmove(before + after.size(), tail.cbegin(), tail.size());
2534 if (!prefix.isEmpty())
2535 memmove(before, prefix.cbegin(), prefix.size());
2536 if (!suffix.isEmpty())
2537 memcpy(before + prefix.size(), suffix.cbegin() + adjust, suffix.size());
2543
2544
2545
2546
2547
2548
2549
2550
2553
2554
2555
2556
2557
2558
2559
2560
2563
2564
2565
2566
2567
2568
2569
2570
2571
2575 const char *b = before.data();
2576 qsizetype bsize = before.size();
2577 const char *a = after.data();
2578 qsizetype asize = after.size();
2584 if (b == a && bsize == asize)
2587 if (asize == 0 && bsize == 0)
2590 if (bsize == 1 && asize == 1)
2591 return replace(*b, *a);
2594 std::string pinnedReplacement;
2595 if (QtPrivate::q_points_into_range(a, d)) {
2596 pinnedReplacement.assign(a, a + asize);
2597 after = pinnedReplacement;
2605 QVarLengthArray<qsizetype> indices;
2606 qsizetype index = 0;
2607 while ((index = matcher.indexIn(*
this, index)) != -1) {
2608 indices.push_back(index);
2615 QStringAlgorithms<QByteArray>::replace_helper(*
this, bsize, after, indices);
2620
2621
2622
2623
2624
2625
2628
2629
2630
2631
2635 if (before != after) {
2636 if (
const auto pos = indexOf(before); pos >= 0) {
2637 if (d.needsDetach()) {
2639 auto dst = tmp.d.data();
2640 dst =
std::copy(d.data(), d.data() + pos, dst);
2642 std::replace_copy(d.data() + pos + 1, d.end(), dst, before, after);
2646 d.data()[pos] = after;
2647 std::replace(d.data() + pos + 1, d.end(), before, after);
2655
2656
2657
2658
2659
2664 qsizetype start = 0;
2666 while ((end = indexOf(sep, start)) != -1) {
2667 list.append(mid(start, end - start));
2670 list.append(mid(start));
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2696 const qsizetype resultSize = [&] {
2698 if (mul_overflow(r, size(), &r))
2704 result.reserve(resultSize);
2705 if (result.capacity() != resultSize)
2708 memcpy(result.d.data(), data(), size());
2710 qsizetype sizeSoFar = size();
2711 char *end = result.d.data() + sizeSoFar;
2713 const qsizetype halfResultSize = resultSize >> 1;
2714 while (sizeSoFar <= halfResultSize) {
2715 memcpy(end, result.d.data(), sizeSoFar);
2719 memcpy(end, result.d.data(), resultSize - sizeSoFar);
2720 result.d.data()[resultSize] =
'\0';
2721 result.d.size = resultSize;
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2753 qsizetype ol, qsizetype from)
2755 auto delta = l - ol;
2758 if (from < 0 || from > delta)
2763 const char *end = haystack;
2765 const qregisteruint ol_minus_1 = ol - 1;
2766 const char *n = needle + ol_minus_1;
2767 const char *h = haystack + ol_minus_1;
2768 qregisteruint hashNeedle = 0, hashHaystack = 0;
2770 for (idx = 0; idx < ol; ++idx) {
2771 hashNeedle = ((hashNeedle<<1) + *(n-idx));
2772 hashHaystack = ((hashHaystack<<1) + *(h-idx));
2774 hashHaystack -= *haystack;
2775 while (haystack >= end) {
2776 hashHaystack += *haystack;
2777 if (hashHaystack == hashNeedle && memcmp(needle, haystack, ol) == 0)
2778 return haystack - end;
2780 if (ol_minus_1 <
sizeof(ol_minus_1) * CHAR_BIT)
2781 hashHaystack -= qregisteruint(*(haystack + ol)) << ol_minus_1;
2787qsizetype
QtPrivate::lastIndexOf(QByteArrayView haystack, qsizetype from, QByteArrayView needle)
noexcept
2789 if (haystack.isEmpty()) {
2790 if (needle.isEmpty() && from == 0)
2794 const auto ol = needle.size();
2796 return QtPrivate::lastIndexOf(haystack, from, needle.front());
2798 return lastIndexOfHelper(haystack.data(), haystack.size(), needle.data(), ol, from);
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2857 for (
char ch : haystack) {
2864qsizetype
QtPrivate::count(QByteArrayView haystack, QByteArrayView needle)
noexcept
2866 if (needle.size() == 0)
2867 return haystack.size() + 1;
2869 if (needle.size() == 1)
2870 return countCharHelper(haystack, needle[0]);
2874 if (haystack.size() > 500 && needle.size() > 5) {
2876 while ((i = matcher.indexIn(haystack, i + 1)) != -1)
2879 while ((i = haystack.indexOf(needle, i + 1)) != -1)
2886
2887
2888
2889
2890
2891
2892
2895
2896
2897
2898
2899
2900
2904 return countCharHelper(*
this, ch);
2907#if QT_DEPRECATED_SINCE(6
, 4
)
2909
2910
2911
2912
2913
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2928bool QtPrivate::startsWith(QByteArrayView haystack, QByteArrayView needle)
noexcept
2930 if (haystack.size() < needle.size())
2932 if (haystack.data() == needle.data() || needle.size() == 0)
2934 return memcmp(haystack.data(), needle.data(), needle.size()) == 0;
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2950
2951
2952
2953
2954
2955
2957bool QtPrivate::endsWith(QByteArrayView haystack, QByteArrayView needle)
noexcept
2959 if (haystack.size() < needle.size())
2961 if (haystack.end() == needle.end() || needle.size() == 0)
2963 return memcmp(haystack.end() - needle.size(), needle.data(), needle.size()) == 0;
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2980
2981
2982
2983
2984
2985
2988
2989
2992 return c >=
'A' && c <=
'Z';
2996
2997
3000 return c >=
'a' && c <=
'z';
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3036
3037
3038
3039
3040
3041
3042
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3101 switch (QContainerImplHelper::mid(size(), &p, &l)) {
3102 case QContainerImplHelper::Null:
3104 case QContainerImplHelper::Empty:
3106 return QByteArray(DataPointer::fromRawData(&_empty, 0));
3108 case QContainerImplHelper::Full:
3110 case QContainerImplHelper::Subset:
3111 return sliced(p, l);
3121 switch (QContainerImplHelper::mid(size(), &p, &l)) {
3122 case QContainerImplHelper::Null:
3124 case QContainerImplHelper::Empty:
3127 case QContainerImplHelper::Full:
3128 return std::move(*
this);
3129 case QContainerImplHelper::Subset:
3130 return std::move(*
this).sliced(p, l);
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3184 return fromRawData(&_empty, 0);
3185 DataPointer d =
std::move(a.d).sliced(pos, n);
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3261 const char *orig_begin = input.constBegin();
3262 const char *firstBad = orig_begin;
3263 const char *e = input.constEnd();
3264 for ( ; firstBad != e ; ++firstBad) {
3265 uchar ch = uchar(*firstBad);
3266 uchar converted = lookup(ch);
3267 if (ch != converted)
3272 return q_choose_copy_move(input, rvalue);
3275 QByteArray s = q_choose_copy_move(input, rvalue);
3276 char *b = s.begin();
3277 char *p = b + (firstBad - orig_begin);
3279 for ( ; p != e; ++p)
3280 *p =
char(lookup(uchar(*p)));
3286 return toCase(a,
nullptr, asciiLower);
3291 return toCase(a, &a, asciiLower);
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3308 return toCase(a,
nullptr, asciiUpper);
3313 return toCase(a, &a, asciiUpper);
3317
3318
3319
3320
3321
3328#if !defined(QT_NO_DATASTREAM)
3331
3332
3333
3334
3335
3336
3340 if (ba.isNull() && out.version() >= 6) {
3341 QDataStream::writeQSizeType(out, -1);
3344 return out.writeBytes(ba.constData(), ba.size());
3348
3349
3350
3351
3352
3353
3359 qint64 size = QDataStream::readQSizeType(in);
3360 if (in.status() != QDataStream::Ok)
3363 qsizetype len = size;
3364 if (size != len || size < -1) {
3366 in.setStatus(QDataStream::SizeLimitExceeded);
3374 constexpr qsizetype Step = 1024 * 1024;
3375 qsizetype allocated = 0;
3378 qsizetype blockSize = qMin(Step, len - allocated);
3379 ba.resize(allocated + blockSize);
3380 if (in.readRawData(ba.data() + allocated, blockSize) != blockSize) {
3382 in.setStatus(QDataStream::ReadPastEnd);
3385 allocated += blockSize;
3386 }
while (allocated < len);
3393
3394
3395
3396
3397
3398
3399
3402
3403
3404
3405
3406
3407
3408
3411
3412
3413
3414
3415
3416
3417
3420
3421
3422
3423
3424
3425
3426
3429
3430
3431
3432
3433
3434
3435
3438
3439
3440
3441
3442
3443
3444
3447
3448
3449
3450
3451
3452
3453
3456
3457
3458
3459
3460
3461
3462
3465
3466
3467
3468
3469
3470
3471
3474
3475
3476
3477
3478
3479
3480
3483
3484
3485
3486
3487
3488
3489
3492
3493
3494
3495
3496
3497
3498
3501
3502
3503
3504
3505
3506
3507
3510
3511
3512
3513
3514
3515
3516
3519
3520
3521
3522
3523
3524
3525
3528
3529
3530
3531
3532
3533
3534
3537
3538
3539
3540
3541
3542
3543
3546
3547
3548
3549
3550
3551
3552
3555
3556
3557
3558
3559
3560
3561
3564
3565
3566
3567
3568
3569
3570
3573
3574
3575
3576
3577
3578
3579
3582
3583
3584
3585
3586
3587
3588
3591
3592
3593
3594
3595
3596
3597
3600
3601
3602
3603
3604
3605
3606
3607
3608
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3629 return QStringAlgorithms<
const QByteArray>::simplified_helper(a);
3634 return QStringAlgorithms<QByteArray>::simplified_helper(a);
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3658 return QStringAlgorithms<
const QByteArray>::trimmed_helper(a);
3663 return QStringAlgorithms<QByteArray>::trimmed_helper(a);
3666QByteArrayView
QtPrivate::trimmed(QByteArrayView view)
noexcept
3668 const auto [start, stop] = QStringAlgorithms<QByteArrayView>::trimmed_helper_positions(view);
3669 return QByteArrayView(start, stop);
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3692 return QStringAlgorithms<QByteArray>::aligned(*
this, width, fill, Qt::AlignLeft, truncate);
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3715 return QStringAlgorithms<QByteArray>::aligned(*
this, width, fill, Qt::AlignRight, truncate);
3718auto QtPrivate::toSignedInteger(QByteArrayView data,
int base) -> ParsedNumber<qlonglong>
3720#if defined(QT_CHECK_RANGE)
3721 if (base != 0 && (base < 2 || base > 36)) {
3722 qWarning(
"QByteArray::toIntegral: Invalid base %d", base);
3729 const QSimpleParsedNumber r = QLocaleData::bytearrayToLongLong(data, base);
3731 return ParsedNumber(r.result);
3735auto QtPrivate::toUnsignedInteger(QByteArrayView data,
int base) -> ParsedNumber<qulonglong>
3737#if defined(QT_CHECK_RANGE)
3738 if (base != 0 && (base < 2 || base > 36)) {
3739 qWarning(
"QByteArray::toIntegral: Invalid base %d", base);
3746 const QSimpleParsedNumber r = QLocaleData::bytearrayToUnsLongLong(data, base);
3748 return ParsedNumber(r.result);
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3779 return QtPrivate::toIntegral<qlonglong>(qToByteArrayViewIgnoringNull(*
this), ok, base);
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3809 return QtPrivate::toIntegral<qulonglong>(qToByteArrayViewIgnoringNull(*
this), ok, base);
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3841 return QtPrivate::toIntegral<
int>(qToByteArrayViewIgnoringNull(*
this), ok, base);
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3871 return QtPrivate::toIntegral<uint>(qToByteArrayViewIgnoringNull(*
this), ok, base);
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3904 return QtPrivate::toIntegral<
long>(qToByteArrayViewIgnoringNull(*
this), ok, base);
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3935 return QtPrivate::toIntegral<ulong>(qToByteArrayViewIgnoringNull(*
this), ok, base);
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3965 return QtPrivate::toIntegral<
short>(qToByteArrayViewIgnoringNull(*
this), ok, base);
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3995 return QtPrivate::toIntegral<ushort>(qToByteArrayViewIgnoringNull(*
this), ok, base);
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4025 return QByteArrayView(*
this).toDouble(ok);
4028auto QtPrivate::toDouble(QByteArrayView a)
noexcept -> ParsedNumber<
double>
4031 auto r = qt_asciiToDouble(a.data(), a.size());
4033 return ParsedNumber{r.result};
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4068auto QtPrivate::toFloat(QByteArrayView a)
noexcept -> ParsedNumber<
float>
4070 if (
const auto r = toDouble(a)) {
4074 return ParsedNumber(f);
4079template <
typename S>
4083 constexpr auto fail = (
std::numeric_limits<S>::max)();
4084 if (add_overflow(n, S{2}, &n))
4087 if (mul_overflow(n, S{4}, &n))
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4105 constexpr char alphabet_base64[] =
"ABCDEFGH" "IJKLMNOP" "QRSTUVWX" "YZabcdef"
4106 "ghijklmn" "opqrstuv" "wxyz0123" "456789+/";
4107 constexpr char alphabet_base64url[] =
"ABCDEFGH" "IJKLMNOP" "QRSTUVWX" "YZabcdef"
4108 "ghijklmn" "opqrstuv" "wxyz0123" "456789-_";
4109 const char *
const alphabet = options & Base64UrlEncoding ? alphabet_base64url : alphabet_base64;
4110 constexpr char padchar =
'=';
4111 qsizetype padlen = 0;
4113 const qsizetype sz = size();
4115 QByteArray tmp(b64_to_encoded_size_saturated(sz), Qt::Uninitialized);
4118 char *out = tmp.data();
4122 chunk |=
int(uchar(data()[i++])) << 16;
4126 chunk |=
int(uchar(data()[i++])) << 8;
4130 chunk |=
int(uchar(data()[i++]));
4133 int j = (chunk & 0x00fc0000) >> 18;
4134 int k = (chunk & 0x0003f000) >> 12;
4135 int l = (chunk & 0x00000fc0) >> 6;
4136 int m = (chunk & 0x0000003f);
4137 *out++ = alphabet[j];
4138 *out++ = alphabet[k];
4141 if ((options & OmitTrailingEquals) == 0)
4144 *out++ = alphabet[l];
4147 if ((options & OmitTrailingEquals) == 0)
4150 *out++ = alphabet[m];
4153 Q_ASSERT((options & OmitTrailingEquals) || (out == tmp.size() + tmp.data()));
4154 if (options & OmitTrailingEquals)
4155 tmp.truncate(out - tmp.data());
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4180
4181
4182
4183
4184
4187
4188
4189
4190
4191
4194
4195
4196
4197
4198
4201
4202
4203
4204
4205
4208
4209
4210
4211
4212
4215
4216
4217
4218
4221 constexpr int buffsize = 66;
4222 char buff[buffsize];
4227 p = qulltoa2(buff + buffsize, qulonglong(-(1 + n)) + 1, base);
4230 p = qulltoa2(buff + buffsize, qulonglong(n), base);
4233 return assign(QByteArrayView{p, buff + buffsize});
4237
4238
4239
4240
4244 constexpr int buffsize = 66;
4245 char buff[buffsize];
4246 char *p = qulltoa2(buff + buffsize, n, base);
4248 return assign(QByteArrayView{p, buff + buffsize});
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4265 return *
this = QByteArray::number(n, format, precision);
4269
4270
4271
4272
4273
4274
4275
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4301
4302
4303
4304
4313
4314
4315
4316
4325
4326
4327
4328
4337
4338
4339
4340
4349
4350
4351
4352
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4376 switch (QtMiscUtils::toAsciiLower(format)) {
4387#if defined(QT_CHECK_RANGE)
4388 qWarning(
"QByteArray::setNum: Invalid format char '%c'", format);
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4451 *
this = fromRawData(data, size);
4456struct fromBase64_helper_result {
4457 qsizetype decodedLength;
4458 QByteArray::Base64DecodingStatus status;
4461fromBase64_helper_result fromBase64_helper(
const char *input, qsizetype inputSize,
4463 QByteArray::Base64Options options)
4465 fromBase64_helper_result result{ 0, QByteArray::Base64DecodingStatus::Ok };
4467 unsigned int buf = 0;
4470 qsizetype offset = 0;
4471 for (qsizetype i = 0; i < inputSize; ++i) {
4475 if (ch >=
'A' && ch <=
'Z') {
4477 }
else if (ch >=
'a' && ch <=
'z') {
4479 }
else if (ch >=
'0' && ch <=
'9') {
4481 }
else if (ch ==
'+' && (options & QByteArray::Base64UrlEncoding) == 0) {
4483 }
else if (ch ==
'-' && (options & QByteArray::Base64UrlEncoding) != 0) {
4485 }
else if (ch ==
'/' && (options & QByteArray::Base64UrlEncoding) == 0) {
4487 }
else if (ch ==
'_' && (options & QByteArray::Base64UrlEncoding) != 0) {
4490 if (options & QByteArray::AbortOnBase64DecodingErrors) {
4494 if ((inputSize % 4) != 0) {
4495 result.status = QByteArray::Base64DecodingStatus::IllegalInputLength;
4497 }
else if ((i == inputSize - 1) ||
4498 (i == inputSize - 2 && input[++i] ==
'=')) {
4501 result.status = QByteArray::Base64DecodingStatus::IllegalPadding;
4505 result.status = QByteArray::Base64DecodingStatus::IllegalCharacter;
4514 buf = (buf << 6) | d;
4518 Q_ASSERT(offset < i);
4519 output[offset++] = buf >> nbits;
4520 buf &= (1 << nbits) - 1;
4525 result.decodedLength = offset;
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4561 if (base64.isDetached()) {
4562 const auto base64result = fromBase64_helper(base64.data(),
4566 base64.truncate(base64result.decodedLength);
4567 return {
std::move(base64), base64result.status };
4570 return fromBase64Encoding(base64, options);
4573template <
typename S>
4579 return n - (n / 4 +
bool(n % 4));
4584 const auto base64Size = base64.size();
4585 QByteArray result(b64_to_decoded_size(base64Size), Qt::Uninitialized);
4586 const auto base64result = fromBase64_helper(base64.data(),
4588 const_cast<
char *>(result.constData()),
4590 result.truncate(base64result.decodedLength);
4591 return {
std::move(result), base64result.status };
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4620 if (
auto result = fromBase64Encoding(base64, options))
4621 return std::move(result.decoded);
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4638 QByteArray res((hexEncoded.size() + 1)/ 2, Qt::Uninitialized);
4639 uchar *result = (uchar *)res.data() + res.size();
4641 bool odd_digit =
true;
4642 for (qsizetype i = hexEncoded.size() - 1; i >= 0; --i) {
4643 uchar ch = uchar(hexEncoded.at(i));
4644 int tmp = QtMiscUtils::fromHex(ch);
4652 *result |= tmp << 4;
4657 res.remove(0, result - (
const uchar *)res.constData());
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4680 const auto length = [&] {
4681 using S = QByteArray::size_type;
4682 constexpr auto fail = (std::numeric_limits<S>::max)();
4685 if (mul_overflow(r, S{3}, &r))
4689 if (mul_overflow(r, S{2}, &r))
4695 char *hexData = hex.data();
4696 const uchar *data = (
const uchar *)
this->data();
4697 for (qsizetype i = 0, o = 0; i < size(); ++i) {
4698 hexData[o++] = QtMiscUtils::toHexLower(data[i] >> 4);
4699 hexData[o++] = QtMiscUtils::toHexLower(data[i] & 0xf);
4701 if ((separator) && (o < length))
4702 hexData[o++] = separator;
4709 char *data = buffer.begin();
4710 const char *inputPtr = src.begin();
4713 const qsizetype len = src.size();
4715 char c = inputPtr[i];
4716 if (c == percent && i + 2 < len) {
4717 int a = QtMiscUtils::fromHex(uchar(inputPtr[++i]));
4718 int b = QtMiscUtils::fromHex(uchar(inputPtr[++i]));
4720 *data = uchar(a << 4) | uchar(b);
4728 return data - buffer.begin();
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4752
4753
4754
4755
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4773 if (input.isEmpty())
4776 QByteArray out{input.size(), Qt::Uninitialized};
4777 qsizetype len = q_fromPercentEncoding(input, percent, out);
4783
4784
4785
4788 if (input.d.needsDetach())
4789 return fromPercentEncoding(input, percent);
4791 if (input.isEmpty())
4792 return std::move(input);
4794 qsizetype len = q_fromPercentEncoding(input, percent, input);
4795 input.truncate(len);
4796 return std::move(input);
4800
4801
4802
4803
4804
4805
4808 return QByteArray(s.data(), qsizetype(s.size()));
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4825 return std::string(data(), size_t(size()));
4829
4830
4831
4832
4833
4834
4835
4836
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4870 const auto contains = [](
const QByteArray &view,
char c) {
4872 return view.size() > 0 && memchr(view.data(), c, view.size()) !=
nullptr;
4876 char *output =
nullptr;
4877 qsizetype length = 0;
4879 for (
unsigned char c : *
this) {
4880 if (
char(c) != percent
4881 && ((c >= 0x61 && c <= 0x7A)
4882 || (c >= 0x41 && c <= 0x5A)
4883 || (c >= 0x30 && c <= 0x39)
4888 || contains(exclude, c))
4889 && !contains(include, c)) {
4896 size_type n = size();
4897 if (mul_overflow(n, size_type{3}, &n))
4898 n = (std::numeric_limits<size_type>::max)();
4900 output = result.data();
4902 output[length++] = percent;
4903 output[length++] = QtMiscUtils::toHexUpper((c & 0xf0) >> 4);
4904 output[length++] = QtMiscUtils::toHexUpper(c & 0xf);
4908 result.truncate(length);
4913#if defined(Q_OS_WASM) || defined(Q_QDOC)
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4938QByteArray QByteArray::fromEcmaUint8Array(emscripten::val uint8array)
4940 return qstdweb::Uint8Array(uint8array).copyToQByteArray();
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960emscripten::val QByteArray::toEcmaUint8Array()
4962 return qstdweb::Uint8Array::copyFrom(*
this).val();
4968
4969
4972
4973
4976
4977
4978
4979
4980
4983
4984
4985
4986
4987
4990
4991
4992
4993
4994
4995
4998
4999
5000
5001
5002
5003
5006
5007
5010
5011
5014
5015
5018
5019
5022
5023
5026
5027
5030
5031
5034
5035
5036
5039
5040
5041
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5063#if QT_DEPRECATED_SINCE(6
, 8
)
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5139
5140
5141
5142
5145
5146
5147
5148
5149
5152
5153
5154
5155
5156
5157
5160
5161
5162
5163
5164
5165
5166
5169
5170
5171
5172
5173
5174
5175
5176
5179
5180
5181
5182
5183
5186
5187
5188size_t
qHash(
const QByteArray::FromBase64Result &key, size_t seed)
noexcept
5190 return qHashMulti(seed, key.decoded,
static_cast<
int>(key.decodingStatus));
5194
5195
5196
5197
5198
5199
5200
5201
5204
5205
5206
5207
5208
5209
5210
5211
5212
QDataStream & operator>>(QDataStream &in, QByteArray &ba)
Reads a byte array into ba from the stream in and returns a reference to the stream.
quint16 qChecksum(QByteArrayView data, Qt::ChecksumType standard)
static constexpr bool isLowerCaseAscii(char c)
static const quint16 crc_tbl[16]
QByteArray qCompress(const uchar *data, qsizetype nbytes, int compressionLevel)
static Q_DECL_COLD_FUNCTION const char * zlibOpAsString(ZLibOp op)
static QByteArray toCase(const QByteArray &input, QByteArray *rvalue, uchar(*lookup)(uchar))
static qsizetype q_fromPercentEncoding(QByteArrayView src, char percent, QSpan< char > buffer)
static qsizetype lastIndexOfHelper(const char *haystack, qsizetype l, const char *needle, qsizetype ol, qsizetype from)
static constexpr bool isUpperCaseAscii(char c)
static QByteArray xxflate(ZLibOp op, QArrayDataPointer< char > out, QByteArrayView input, qxp::function_ref< int(z_stream *) const > init, qxp::function_ref< int(z_stream *, size_t) const > processChunk, qxp::function_ref< void(z_stream *) const > deinit)
static constexpr uchar asciiLower(uchar c)
static qsizetype countCharHelper(QByteArrayView haystack, char needle) noexcept
static S b64_to_decoded_size(S n)
static S b64_to_encoded_size_saturated(S n)
static constexpr uchar asciiUpper(uchar c)
Q_CORE_EXPORT char * qstrncpy(char *dst, const char *src, size_t len)
Q_CORE_EXPORT int qstricmp(const char *, const char *)
Q_CORE_EXPORT char * qstrdup(const char *)
Q_CORE_EXPORT char * qstrcpy(char *dst, const char *src)
Q_DECL_PURE_FUNCTION Q_CORE_EXPORT const void * qmemrchr(const void *s, int needle, size_t n) noexcept
Q_CORE_EXPORT int qstrcmp(const char *str1, const char *str2)
constexpr size_t qHash(const QSize &s, size_t seed=0) noexcept
static float convertDoubleToFloat(double d, bool *ok)