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>
37#include <QtCore/q26numeric.h>
41# if !defined(QT_BOOTSTRAPPED) && (defined(QT_NO_CAST_FROM_ASCII) || defined(QT_NO_CAST_FROM_BYTEARRAY))
43# error "This file cannot be compiled with QT_NO_CAST_{TO,FROM}_ASCII, "
44 "otherwise some QByteArray functions will not get exported."
50Q_CONSTINIT
const char QByteArray::_empty =
'\0';
55 return c >=
'a' && c <=
'z' ? c & ~0x20 : c;
60 return c >=
'A' && c <=
'Z' ? c | 0x20 : c;
64
65
68
69
70
71
72
73
74
75
76const void *
qmemrchr(
const void *s,
int needle, size_t size)
noexcept
79 return memrchr(s, needle, size);
81 auto b =
static_cast<
const uchar *>(s);
82 const uchar *n = b + size;
84 if (*n == uchar(needle))
92
93
94
95
96
97
98
99
100
101
107 char *dst =
new char[strlen(src) + 1];
108 return qstrcpy(dst, src);
112
113
114
115
116
117
118
119
120
121
122
123
130 const size_t len = strlen(src);
133 if (len >= 0 && strcpy_s(dst, len+1, src) == 0)
137 return strcpy(dst, src);
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
162char *
qstrncpy(
char *dst,
const char *src, size_t len)
164 if (dst && len > 0) {
167 std::strncat(dst, src, len - 1);
169 return src ? dst :
nullptr;
173
174
175
176
177
178
179
180
181
184
185
186
187
188
189
190
191
192
193
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211int qstrcmp(
const char *str1,
const char *str2)
213 return (str1 && str2) ? strcmp(str1, str2)
214 : (str1 ? 1 : (str2 ? -1 : 0));
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
258 const uchar *s1 =
reinterpret_cast<
const uchar *>(str1);
259 const uchar *s2 =
reinterpret_cast<
const uchar *>(str2);
265 enum { Incomplete = 256 };
267 auto innerCompare = [=, &offset](qptrdiff max,
bool unlimited) {
270 uchar c = s1[offset];
271 if (
int res = QtMiscUtils::caseCompareAscii(c, s2[offset]))
276 }
while (unlimited || offset < max);
277 return int(Incomplete);
280#if defined(__SSE4_1__) && !(defined(__SANITIZE_ADDRESS__) || __has_feature(address_sanitizer))
281 enum { PageSize = 4096, PageMask = PageSize - 1 };
282 const __m128i zero = _mm_setzero_si128();
287 quintptr u1 = quintptr(s1 + offset);
288 quintptr u2 = quintptr(s2 + offset);
289 size_t n = PageSize - ((u1 | u2) & PageMask);
291 qptrdiff maxoffset = offset + n;
292 for ( ; offset + 16 <= maxoffset; offset +=
sizeof(__m128i)) {
294 __m128i a = _mm_loadu_si128(
reinterpret_cast<
const __m128i *>(s1 + offset));
295 __m128i b = _mm_loadu_si128(
reinterpret_cast<
const __m128i *>(s2 + offset));
298 __m128i cmp = _mm_cmpeq_epi8(a, b);
301 cmp = _mm_min_epu8(cmp, a);
302 cmp = _mm_cmpeq_epi8(cmp, zero);
305 uint mask = _mm_movemask_epi8(cmp);
308 uint start = qCountTrailingZeroBits(mask);
309 uint end =
sizeof(mask) * 8 - qCountLeadingZeroBits(mask);
310 Q_ASSERT(end >= start);
318 int res = innerCompare(n,
false);
319 if (res != Incomplete)
324 return innerCompare(-1,
true);
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
348
349
350
351
352
353
354
355
358
359
360int QtPrivate::compareMemory(QByteArrayView lhs, QByteArrayView rhs)
362 if (!lhs.isNull() && !rhs.isNull()) {
363 int ret = memcmp(lhs.data(), rhs.data(), qMin(lhs.size(), rhs.size()));
370 return lhs.size() == rhs.size() ? 0 : lhs.size() > rhs.size() ? 1 : -1;
374
375
378 return QUtf8::isValidUtf8(s).isValidUtf8;
383static void createCRC16Table()
387 unsigned short crc_tbl[16];
388 unsigned int v0, v1, v2, v3;
389 for (i = 0; i < 16; i++) {
396#define SET_BIT(x, b, v) (x) |= (v) << (b)
411 printf(
"static const quint16 crc_tbl[16] = {\n");
412 for (
int i = 0; i < 16; i +=4)
413 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]);
419 0x0000, 0x1081, 0x2102, 0x3183,
420 0x4204, 0x5285, 0x6306, 0x7387,
421 0x8408, 0x9489, 0xa50a, 0xb58b,
422 0xc60c, 0xd68d, 0xe70e, 0xf78f
426
427
428
429
430
431
432
433
434
435
436
437
440 quint16 crc = 0x0000;
442 case Qt::ChecksumIso3309:
445 case Qt::ChecksumItuV41:
450 const uchar *p =
reinterpret_cast<
const uchar *>(data.data());
451 qsizetype len = data.size();
454 crc = ((crc >> 4) & 0x0fff) ^ crc_tbl[((crc ^ c) & 15)];
456 crc = ((crc >> 4) & 0x0fff) ^ crc_tbl[((crc ^ c) & 15)];
459 case Qt::ChecksumIso3309:
462 case Qt::ChecksumItuV41:
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
489
490
491
492
493
494
495
496
498#ifndef QT_NO_COMPRESS
499using CompressSizeHint_t = quint32;
510 Q_UNREACHABLE_RETURN(
nullptr);
514static QByteArray zlibError(ZLibOp op,
const char *what)
516 qWarning(
"%s: %s", zlibOpAsString(op), what);
521static QByteArray dataIsNull(ZLibOp op)
523 return zlibError(op,
"Data is null");
527static QByteArray lengthIsNegative(ZLibOp op)
529 return zlibError(op,
"Input length is negative");
533static QByteArray tooMuchData(ZLibOp op)
535 return zlibError(op,
"Not enough memory");
539static QByteArray invalidCompressedData()
541 return zlibError(ZLibOp::Decompression,
"Input data is corrupted");
545static QByteArray unexpectedZlibError(ZLibOp op,
int err,
const char *msg)
547 qWarning(
"%s unexpected zlib error: %s (%d)",
559 if (out.data() ==
nullptr)
560 return tooMuchData(op);
561 qsizetype capacity = out.allocatedCapacity();
563 const auto initalSize = out.size;
566 zs.next_in =
reinterpret_cast<uchar *>(
const_cast<
char *>(input.data()));
567 if (
const int err = init(&zs); err != Z_OK)
568 return unexpectedZlibError(op, err, zs.msg);
569 const auto sg = qScopeGuard([&] { deinit(&zs); });
571 using ZlibChunkSize_t =
decltype(zs.avail_in);
572 static_assert(!std::is_signed_v<ZlibChunkSize_t>);
573 static_assert(std::is_same_v<ZlibChunkSize_t,
decltype(zs.avail_out)>);
574 constexpr auto MaxChunkSize =
std::numeric_limits<ZlibChunkSize_t>::max();
576 constexpr auto MaxStatisticsSize =
std::numeric_limits<
decltype(zs.total_out)>::max();
578 size_t inputLeft = size_t(input.size());
582 Q_ASSERT(out.freeSpaceAtBegin() == 0);
583 Q_ASSERT(capacity == out.allocatedCapacity());
585 if (zs.avail_out == 0) {
586 Q_ASSERT(size_t(out.size) - initalSize > MaxStatisticsSize ||
587 size_t(out.size) - initalSize == zs.total_out);
588 Q_ASSERT(out.size <= capacity);
590 qsizetype avail_out = capacity - out.size;
591 if (avail_out == 0) {
592 out.reallocateAndGrow(QArrayData::GrowsAtEnd, 1);
593 if (out.data() ==
nullptr)
594 return tooMuchData(op);
595 capacity = out.allocatedCapacity();
596 avail_out = capacity - out.size;
598 zs.next_out =
reinterpret_cast<uchar *>(out.data()) + out.size;
599 zs.avail_out = size_t(avail_out) > size_t(MaxChunkSize) ? MaxChunkSize
600 : ZlibChunkSize_t(avail_out);
601 out.size += zs.avail_out;
603 Q_ASSERT(zs.avail_out > 0);
606 if (zs.avail_in == 0) {
608 zs.avail_in = inputLeft > MaxChunkSize ? MaxChunkSize : ZlibChunkSize_t(inputLeft);
609 inputLeft -= zs.avail_in;
612 res = processChunk(&zs, inputLeft);
613 }
while (res == Z_OK);
617 out.size -= zs.avail_out;
618 Q_ASSERT(size_t(out.size) - initalSize > MaxStatisticsSize ||
619 size_t(out.size) - initalSize == zs.total_out);
620 Q_ASSERT(out.size <= out.allocatedCapacity());
621 out.data()[out.size] =
'\0';
622 return QByteArray(
std::move(out));
625 return tooMuchData(op);
633 return invalidCompressedData();
636 return unexpectedZlibError(op, res, zs.msg);
642 constexpr qsizetype HeaderSize =
sizeof(CompressSizeHint_t);
644 return QByteArray(HeaderSize,
'\0');
652 if (compressionLevel < -1 || compressionLevel > 9)
653 compressionLevel = -1;
655 QArrayDataPointer out = [&] {
656 constexpr qsizetype SingleAllocLimit = 256 * 1024;
660 qsizetype capacity = HeaderSize;
661 if (nbytes < SingleAllocLimit) {
663 capacity += compressBound(uLong(nbytes));
664 return QArrayDataPointer<
char>(capacity);
669 constexpr qsizetype MaxCompressionFactor = 1024;
672 capacity +=
std::max(qsizetype(compressBound(uLong(SingleAllocLimit))),
673 nbytes / MaxCompressionFactor);
674 return QArrayDataPointer<
char>(capacity, 0, QArrayData::Grow);
677 if (out.data() ==
nullptr)
680 qToBigEndian(q26::saturating_cast<CompressSizeHint_t>(nbytes), out.data());
681 out.size = HeaderSize;
683 return xxflate(ZLibOp::Compression, std::move(out), {data, nbytes},
684 [=] (z_stream *zs) {
return deflateInit(zs, compressionLevel); },
685 [] (z_stream *zs, size_t inputLeft) {
686 return deflate(zs, inputLeft ? Z_NO_FLUSH : Z_FINISH);
688 [] (z_stream *zs) { deflateEnd(zs); });
693
694
695
696
697
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
724#ifndef QT_NO_COMPRESS
726
727
728
729
730
731
740 constexpr qsizetype HeaderSize =
sizeof(CompressSizeHint_t);
741 if (nbytes < HeaderSize)
742 return invalidCompressedData();
744 const auto expectedSize = qFromBigEndian<CompressSizeHint_t>(data);
745 if (nbytes == HeaderSize) {
746 if (expectedSize != 0)
747 return invalidCompressedData();
751 constexpr auto MaxDecompressedSize = size_t(QByteArray::maxSize());
752 if constexpr (MaxDecompressedSize < std::numeric_limits<CompressSizeHint_t>::max()) {
753 if (expectedSize > MaxDecompressedSize)
759 qsizetype capacity =
std::max(qsizetype(expectedSize),
762 QArrayDataPointer<
char> d(capacity);
763 return xxflate(ZLibOp::Decompression, std::move(d), {data + HeaderSize, nbytes - HeaderSize},
764 [] (z_stream *zs) {
return inflateInit(zs); },
765 [] (z_stream *zs, size_t) {
return inflate(zs, Z_NO_FLUSH); },
766 [] (z_stream *zs) { inflateEnd(zs); });
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
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
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1067
1068
1069
1072
1073
1074
1075
1076
1077
1078
1079
1080
1083
1084
1085
1086
1087
1088
1089
1090
1093
1094
1095
1096
1097
1098
1099
1100
1103
1104
1105
1108
1109
1110
1111
1112
1113
1114
1115
1116
1119
1120
1121
1122
1123
1124
1125
1126
1129
1130
1131
1132
1133
1134
1135
1136
1137
1140
1141
1142
1145
1146
1147
1148
1149
1150
1151
1152
1153
1156
1157
1158
1159
1160
1161
1162
1163
1164
1167
1168
1169
1172
1173
1174
1175
1176
1177
1178
1179
1180
1183
1184
1185
1186
1189
1190
1191
1192
1193
1196
1197
1198
1199
1200
1203
1204
1205
1206
1207
1210
1211
1212
1213
1216
1217
1218
1219
1220
1223
1224
1225
1226
1227
1230
1231
1232
1233
1234
1237
1238
1239
1240
1241
1244
1245
1246
1247
1248
1249
1250QByteArray::iterator
QByteArray::erase(QByteArray::const_iterator first, QByteArray::const_iterator last)
1252 const auto start =
std::distance(cbegin(), first);
1253 const auto len =
std::distance(first, last);
1255 return begin() + start;
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1287
1288
1289
1290
1291
1292
1293
1296
1297
1298
1299
1300
1303
1304
1307
1308
1309
1310
1313
1314
1315
1316
1317
1318
1319
1326 d = DataPointer::fromRawData(&_empty, 0);
1334
1335
1336
1337
1338
1339
1342
1343
1344
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1365
1366
1367
1368
1369
1370
1371
1374
1375
1376
1377
1378
1379
1380
1381
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1431
1432
1433
1434
1435
1436
1437
1438
1439
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1469
1470
1471
1472
1473
1474
1475
1476
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1514
1515
1516
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1537
1538
1539
1542
1543
1544
1547
1548
1549
1552
1553
1554
1555
1556
1557
1558
1559
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1575
1576
1577
1578
1579
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1642
1643
1644
1645
1646
1647
1648
1651
1652
1653
1654
1655
1656
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1720
1721
1722
1723
1724
1725
1728
1729
1730
1731
1732
1733
1736
1737
1738
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1756
1757
1758
1759
1760
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1782 size = qstrlen(data);
1784 d = DataPointer::fromRawData(&_empty, 0);
1786 d = DataPointer(size, size);
1787 Q_CHECK_PTR(d.data());
1788 memcpy(d.data(), data, size);
1789 d.data()[size] =
'\0';
1795
1796
1797
1798
1803 d = DataPointer::fromRawData(&_empty, 0);
1805 d = DataPointer(size, size);
1806 Q_CHECK_PTR(d.data());
1807 memset(d.data(), ch, size);
1808 d.data()[size] =
'\0';
1813
1814
1815
1816
1817
1818
1819
1824 d = DataPointer::fromRawData(&_empty, 0);
1826 d = DataPointer(size, size);
1827 Q_CHECK_PTR(d.data());
1828 d.data()[size] =
'\0';
1833
1834
1835
1836
1837
1838
1839
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1861 const auto capacityAtEnd = capacity() - d.freeSpaceAtBegin();
1862 if (d.needsDetach() || size > capacityAtEnd)
1863 reallocData(size, QArrayData::Grow);
1865 if (d.allocatedCapacity())
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1888 const auto old = d.size;
1891 memset(d.data() + old, c, d.size - old);
1895
1896
1897
1898
1899
1900
1901
1902
1903
1910
1911
1912
1913
1914
1915
1916
1917
1921 resize(size < 0 ?
this->size() : size);
1923 memset(d.data(), ch,
this->size());
1927void QByteArray::reallocData(qsizetype alloc, QArrayData::AllocationOption option)
1930 d = DataPointer::fromRawData(&_empty, 0);
1936 const bool cannotUseReallocate = d.freeSpaceAtBegin() > 0;
1938 if (d.needsDetach() || cannotUseReallocate) {
1939 DataPointer dd(alloc, qMin(alloc, d.size), option);
1940 Q_CHECK_PTR(dd.data());
1942 ::memcpy(dd.data(), d.data(), dd.size);
1943 dd.data()[dd.size] = 0;
1946 d->reallocate(alloc, option);
1955 if (d.needsDetach()) {
1956 DataPointer dd(DataPointer::allocateGrow(d, n, QArrayData::GrowsAtEnd));
1957 Q_CHECK_PTR(dd.data());
1958 dd->copyAppend(d.data(), d.data() + d.size);
1959 dd.data()[dd.size] = 0;
1962 d->reallocate(d.constAllocatedCapacity() + n, QArrayData::Grow);
1968 resize(qMax(i + 1, size()));
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1993
1994
1995
1996
1997
1998
1999
2000
2001
2013 return std::move(*
this);
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2035
2036
2037
2038
2039
2042 if (size() == 0 && ba.size() > d.constAllocatedCapacity() && ba.d.isMutable())
2043 return (*
this = ba);
2044 return prepend(QByteArrayView(ba));
2048
2049
2050
2051
2052
2055
2056
2057
2058
2059
2060
2061
2064
2065
2066
2067
2068
2069
2072
2073
2074
2075
2076
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2106 if (Q_UNLIKELY(!ba.d.isMutable()))
2110 }
else if (ba.size()) {
2111 append(QByteArrayView(ba));
2118
2119
2120
2121
2122
2125
2126
2127
2128
2129
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2148
2149
2150
2151
2152
2153
2154
2155
2156
2159
2160
2161
2162
2166 d.detachAndGrow(QArrayData::GrowsAtEnd, 1,
nullptr,
nullptr);
2167 d->copyAppend(1, ch);
2168 d.data()[d.size] =
'\0';
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2223 const auto len = v.size();
2225 if (len <= capacity() && isDetached()) {
2226 const auto offset = d.freeSpaceAtBegin();
2228 d.setBegin(d.begin() - offset);
2230 std::memcpy(d.begin(), v.data(), len);
2232 d.data()[d.size] =
'\0';
2234 *
this = v.toByteArray();
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2261 const char *str = data.data();
2262 qsizetype size = data.size();
2263 if (i < 0 || size <= 0)
2272 DataPointer detached{};
2273 d.detachAndGrow(Data::GrowsAtEnd, (i - d.size) + size, &str, &detached);
2274 Q_CHECK_PTR(d.data());
2275 d->copyAppend(i - d.size,
' ');
2276 d->copyAppend(str, str + size);
2277 d.data()[d.size] =
'\0';
2281 if (!d.needsDetach() && QtPrivate::q_points_into_range(str, d)) {
2282 QVarLengthArray a(str, str + size);
2283 return insert(i, a);
2286 d->insert(i, str, size);
2287 d.data()[d.size] =
'\0';
2292
2293
2294
2295
2296
2297
2298
2299
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2314
2315
2316
2317
2318
2319
2320
2321
2322
2325
2326
2327
2328
2329
2330
2331
2334
2335
2336
2337
2338
2339
2340
2341
2342
2346 if (i < 0 || count <= 0)
2351 d.detachAndGrow(Data::GrowsAtEnd, (i - d.size) + count,
nullptr,
nullptr);
2352 Q_CHECK_PTR(d.data());
2353 d->copyAppend(i - d.size,
' ');
2354 d->copyAppend(count, ch);
2355 d.data()[d.size] =
'\0';
2359 d->insert(i, count, ch);
2360 d.data()[d.size] =
'\0';
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2384 if (len <= 0 || pos < 0 || size_t(pos) >= size_t(size()))
2386 if (pos + len > d.size)
2389 const auto toRemove_start = d.begin() + pos;
2390 if (!d.isShared()) {
2391 d->erase(toRemove_start, len);
2392 d.data()[d.size] =
'\0';
2394 QByteArray copy{size() - len, Qt::Uninitialized};
2395 copy.d->copyRanges({{d.begin(), toRemove_start},
2396 {toRemove_start + len, d.end()}});
2403
2404
2405
2406
2407
2408
2409
2410
2411
2414
2415
2416
2417
2418
2419
2420
2421
2422
2424
2425
2426
2427
2428
2429
2430
2431
2432
2435
2436
2437
2438
2439
2440
2441
2442
2445
2446
2447
2448
2449
2450
2451
2452
2456 if (size_t(pos) > size_t(
this->size()))
2458 if (len >
this->size() - pos)
2459 len =
this->size() - pos;
2464 return insert(pos, after);
2466 if (after.isEmpty())
2467 return remove(pos, len);
2469 using A = QStringAlgorithms<QByteArray>;
2470 const qsizetype newlen = A::newSize(*
this, len, after, {pos});
2471 if (data_ptr().needsDetach() || A::needsReallocate(*
this, newlen)) {
2472 A::replace_into_copy(*
this, len, after, {pos}, newlen);
2477 char *
const begin = data_ptr().data();
2478 char *
const before = begin + pos;
2479 const char *beforeEnd = before + len;
2480 if (len >= after.size()) {
2481 memmove(before , after.cbegin(), after.size());
2483 if (len > after.size()) {
2484 memmove(before + after.size(), beforeEnd, d.size - (beforeEnd - begin));
2485 A::setSize(*
this, newlen);
2488 char *oldEnd = begin + d.size;
2489 const qsizetype adjust = newlen - d.size;
2490 A::setSize(*
this, newlen);
2492 QByteArrayView tail{beforeEnd, oldEnd};
2493 QByteArrayView prefix = after;
2494 QByteArrayView suffix;
2495 if (QtPrivate::q_points_into_range(after.cend() - 1, tail)) {
2496 if (QtPrivate::q_points_into_range(after.cbegin(), tail)) {
2499 suffix = QByteArrayView{after.cbegin(), after.cend()};
2501 prefix = QByteArrayView{after.cbegin(), beforeEnd};
2502 suffix = QByteArrayView{beforeEnd, after.cend()};
2505 memmove(before + after.size(), tail.cbegin(), tail.size());
2506 if (!prefix.isEmpty())
2507 memmove(before, prefix.cbegin(), prefix.size());
2508 if (!suffix.isEmpty())
2509 memcpy(before + prefix.size(), suffix.cbegin() + adjust, suffix.size());
2515
2516
2517
2518
2519
2520
2521
2522
2525
2526
2527
2528
2529
2530
2531
2532
2535
2536
2537
2538
2539
2540
2541
2542
2543
2547 const char *b = before.data();
2548 qsizetype bsize = before.size();
2549 const char *a = after.data();
2550 qsizetype asize = after.size();
2556 if (b == a && bsize == asize)
2559 if (asize == 0 && bsize == 0)
2562 if (bsize == 1 && asize == 1)
2563 return replace(*b, *a);
2566 std::string pinnedReplacement;
2567 if (QtPrivate::q_points_into_range(a, d)) {
2568 pinnedReplacement.assign(a, a + asize);
2569 after = pinnedReplacement;
2577 QVarLengthArray<qsizetype> indices;
2578 qsizetype index = 0;
2579 while ((index = matcher.indexIn(*
this, index)) != -1) {
2580 indices.push_back(index);
2587 QStringAlgorithms<QByteArray>::replace_helper(*
this, bsize, after, indices);
2592
2593
2594
2595
2596
2597
2600
2601
2602
2603
2607 if (before != after) {
2608 if (
const auto pos = indexOf(before); pos >= 0) {
2609 if (d.needsDetach()) {
2611 auto dst = tmp.d.data();
2612 dst =
std::copy(d.data(), d.data() + pos, dst);
2614 std::replace_copy(d.data() + pos + 1, d.end(), dst, before, after);
2618 d.data()[pos] = after;
2619 std::replace(d.data() + pos + 1, d.end(), before, after);
2627
2628
2629
2630
2631
2636 qsizetype start = 0;
2638 while ((end = indexOf(sep, start)) != -1) {
2639 list.append(mid(start, end - start));
2642 list.append(mid(start));
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2668 const qsizetype resultSize = times * size();
2671 result.reserve(resultSize);
2672 if (result.capacity() != resultSize)
2675 memcpy(result.d.data(), data(), size());
2677 qsizetype sizeSoFar = size();
2678 char *end = result.d.data() + sizeSoFar;
2680 const qsizetype halfResultSize = resultSize >> 1;
2681 while (sizeSoFar <= halfResultSize) {
2682 memcpy(end, result.d.data(), sizeSoFar);
2686 memcpy(end, result.d.data(), resultSize - sizeSoFar);
2687 result.d.data()[resultSize] =
'\0';
2688 result.d.size = resultSize;
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2720 qsizetype ol, qsizetype from)
2722 auto delta = l - ol;
2725 if (from < 0 || from > delta)
2730 const char *end = haystack;
2732 const qregisteruint ol_minus_1 = ol - 1;
2733 const char *n = needle + ol_minus_1;
2734 const char *h = haystack + ol_minus_1;
2735 qregisteruint hashNeedle = 0, hashHaystack = 0;
2737 for (idx = 0; idx < ol; ++idx) {
2738 hashNeedle = ((hashNeedle<<1) + *(n-idx));
2739 hashHaystack = ((hashHaystack<<1) + *(h-idx));
2741 hashHaystack -= *haystack;
2742 while (haystack >= end) {
2743 hashHaystack += *haystack;
2744 if (hashHaystack == hashNeedle && memcmp(needle, haystack, ol) == 0)
2745 return haystack - end;
2747 if (ol_minus_1 <
sizeof(ol_minus_1) * CHAR_BIT)
2748 hashHaystack -= qregisteruint(*(haystack + ol)) << ol_minus_1;
2754qsizetype
QtPrivate::lastIndexOf(QByteArrayView haystack, qsizetype from, QByteArrayView needle)
noexcept
2756 if (haystack.isEmpty()) {
2757 if (needle.isEmpty() && from == 0)
2761 const auto ol = needle.size();
2763 return QtPrivate::lastIndexOf(haystack, from, needle.front());
2765 return lastIndexOfHelper(haystack.data(), haystack.size(), needle.data(), ol, from);
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2824 for (
char ch : haystack) {
2831qsizetype
QtPrivate::count(QByteArrayView haystack, QByteArrayView needle)
noexcept
2833 if (needle.size() == 0)
2834 return haystack.size() + 1;
2836 if (needle.size() == 1)
2837 return countCharHelper(haystack, needle[0]);
2841 if (haystack.size() > 500 && needle.size() > 5) {
2843 while ((i = matcher.indexIn(haystack, i + 1)) != -1)
2846 while ((i = haystack.indexOf(needle, i + 1)) != -1)
2853
2854
2855
2856
2857
2858
2859
2862
2863
2864
2865
2866
2867
2871 return countCharHelper(*
this, ch);
2874#if QT_DEPRECATED_SINCE(6
, 4
)
2876
2877
2878
2879
2880
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2895bool QtPrivate::startsWith(QByteArrayView haystack, QByteArrayView needle)
noexcept
2897 if (haystack.size() < needle.size())
2899 if (haystack.data() == needle.data() || needle.size() == 0)
2901 return memcmp(haystack.data(), needle.data(), needle.size()) == 0;
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2917
2918
2919
2920
2921
2922
2924bool QtPrivate::endsWith(QByteArrayView haystack, QByteArrayView needle)
noexcept
2926 if (haystack.size() < needle.size())
2928 if (haystack.end() == needle.end() || needle.size() == 0)
2930 return memcmp(haystack.end() - needle.size(), needle.data(), needle.size()) == 0;
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2947
2948
2949
2950
2951
2952
2955
2956
2959 return c >=
'A' && c <=
'Z';
2963
2964
2967 return c >=
'a' && c <=
'z';
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
3003
3004
3005
3006
3007
3008
3009
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3068 switch (QContainerImplHelper::mid(size(), &p, &l)) {
3069 case QContainerImplHelper::Null:
3071 case QContainerImplHelper::Empty:
3073 return QByteArray(DataPointer::fromRawData(&_empty, 0));
3075 case QContainerImplHelper::Full:
3077 case QContainerImplHelper::Subset:
3078 return sliced(p, l);
3088 switch (QContainerImplHelper::mid(size(), &p, &l)) {
3089 case QContainerImplHelper::Null:
3091 case QContainerImplHelper::Empty:
3094 case QContainerImplHelper::Full:
3095 return std::move(*
this);
3096 case QContainerImplHelper::Subset:
3097 return std::move(*
this).sliced(p, l);
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3151 return fromRawData(&_empty, 0);
3152 DataPointer d =
std::move(a.d).sliced(pos, n);
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3228 const char *orig_begin = input.constBegin();
3229 const char *firstBad = orig_begin;
3230 const char *e = input.constEnd();
3231 for ( ; firstBad != e ; ++firstBad) {
3232 uchar ch = uchar(*firstBad);
3233 uchar converted = lookup(ch);
3234 if (ch != converted)
3239 return q_choose_copy_move(input, rvalue);
3242 QByteArray s = q_choose_copy_move(input, rvalue);
3243 char *b = s.begin();
3244 char *p = b + (firstBad - orig_begin);
3246 for ( ; p != e; ++p)
3247 *p =
char(lookup(uchar(*p)));
3253 return toCase(a,
nullptr, asciiLower);
3258 return toCase(a, &a, asciiLower);
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3275 return toCase(a,
nullptr, asciiUpper);
3280 return toCase(a, &a, asciiUpper);
3284
3285
3286
3287
3288
3295#if !defined(QT_NO_DATASTREAM)
3298
3299
3300
3301
3302
3303
3307 if (ba.isNull() && out.version() >= 6) {
3308 QDataStream::writeQSizeType(out, -1);
3311 return out.writeBytes(ba.constData(), ba.size());
3315
3316
3317
3318
3319
3320
3326 qint64 size = QDataStream::readQSizeType(in);
3327 qsizetype len = size;
3328 if (size != len || size < -1) {
3330 in.setStatus(QDataStream::SizeLimitExceeded);
3338 constexpr qsizetype Step = 1024 * 1024;
3339 qsizetype allocated = 0;
3342 qsizetype blockSize = qMin(Step, len - allocated);
3343 ba.resize(allocated + blockSize);
3344 if (in.readRawData(ba.data() + allocated, blockSize) != blockSize) {
3346 in.setStatus(QDataStream::ReadPastEnd);
3349 allocated += blockSize;
3350 }
while (allocated < len);
3357
3358
3359
3360
3361
3362
3363
3366
3367
3368
3369
3370
3371
3372
3375
3376
3377
3378
3379
3380
3381
3384
3385
3386
3387
3388
3389
3390
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
3571
3572
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3593 return QStringAlgorithms<
const QByteArray>::simplified_helper(a);
3598 return QStringAlgorithms<QByteArray>::simplified_helper(a);
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3622 return QStringAlgorithms<
const QByteArray>::trimmed_helper(a);
3627 return QStringAlgorithms<QByteArray>::trimmed_helper(a);
3630QByteArrayView
QtPrivate::trimmed(QByteArrayView view)
noexcept
3632 const auto [start, stop] = QStringAlgorithms<QByteArrayView>::trimmed_helper_positions(view);
3633 return QByteArrayView(start, stop);
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3657 qsizetype len = size();
3658 qsizetype padlen = width - len;
3660 result.resize(len+padlen);
3662 memcpy(result.d.data(), data(), len);
3663 memset(result.d.data()+len, fill, padlen);
3666 result = left(width);
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3694 qsizetype len = size();
3695 qsizetype padlen = width - len;
3697 result.resize(len+padlen);
3699 memcpy(result.d.data()+padlen, data(), len);
3700 memset(result.d.data(), fill, padlen);
3703 result = left(width);
3710auto QtPrivate::toSignedInteger(QByteArrayView data,
int base) -> ParsedNumber<qlonglong>
3712#if defined(QT_CHECK_RANGE)
3713 if (base != 0 && (base < 2 || base > 36)) {
3714 qWarning(
"QByteArray::toIntegral: Invalid base %d", base);
3721 const QSimpleParsedNumber r = QLocaleData::bytearrayToLongLong(data, base);
3723 return ParsedNumber(r.result);
3727auto QtPrivate::toUnsignedInteger(QByteArrayView data,
int base) -> ParsedNumber<qulonglong>
3729#if defined(QT_CHECK_RANGE)
3730 if (base != 0 && (base < 2 || base > 36)) {
3731 qWarning(
"QByteArray::toIntegral: Invalid base %d", base);
3738 const QSimpleParsedNumber r = QLocaleData::bytearrayToUnsLongLong(data, base);
3740 return ParsedNumber(r.result);
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3771 return QtPrivate::toIntegral<qlonglong>(qToByteArrayViewIgnoringNull(*
this), ok, base);
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3801 return QtPrivate::toIntegral<qulonglong>(qToByteArrayViewIgnoringNull(*
this), ok, base);
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3833 return QtPrivate::toIntegral<
int>(qToByteArrayViewIgnoringNull(*
this), ok, base);
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3863 return QtPrivate::toIntegral<uint>(qToByteArrayViewIgnoringNull(*
this), ok, base);
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3896 return QtPrivate::toIntegral<
long>(qToByteArrayViewIgnoringNull(*
this), ok, base);
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3927 return QtPrivate::toIntegral<ulong>(qToByteArrayViewIgnoringNull(*
this), ok, base);
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3957 return QtPrivate::toIntegral<
short>(qToByteArrayViewIgnoringNull(*
this), ok, base);
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3987 return QtPrivate::toIntegral<ushort>(qToByteArrayViewIgnoringNull(*
this), ok, base);
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4017 return QByteArrayView(*
this).toDouble(ok);
4020auto QtPrivate::toDouble(QByteArrayView a)
noexcept -> ParsedNumber<
double>
4023 auto r = qt_asciiToDouble(a.data(), a.size());
4025 return ParsedNumber{r.result};
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4060auto QtPrivate::toFloat(QByteArrayView a)
noexcept -> ParsedNumber<
float>
4062 if (
const auto r = toDouble(a)) {
4066 return ParsedNumber(f);
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4084 constexpr char alphabet_base64[] =
"ABCDEFGH" "IJKLMNOP" "QRSTUVWX" "YZabcdef"
4085 "ghijklmn" "opqrstuv" "wxyz0123" "456789+/";
4086 constexpr char alphabet_base64url[] =
"ABCDEFGH" "IJKLMNOP" "QRSTUVWX" "YZabcdef"
4087 "ghijklmn" "opqrstuv" "wxyz0123" "456789-_";
4088 const char *
const alphabet = options & Base64UrlEncoding ? alphabet_base64url : alphabet_base64;
4089 constexpr char padchar =
'=';
4090 qsizetype padlen = 0;
4092 const qsizetype sz = size();
4094 QByteArray tmp((sz + 2) / 3 * 4, Qt::Uninitialized);
4097 char *out = tmp.data();
4101 chunk |=
int(uchar(data()[i++])) << 16;
4105 chunk |=
int(uchar(data()[i++])) << 8;
4109 chunk |=
int(uchar(data()[i++]));
4112 int j = (chunk & 0x00fc0000) >> 18;
4113 int k = (chunk & 0x0003f000) >> 12;
4114 int l = (chunk & 0x00000fc0) >> 6;
4115 int m = (chunk & 0x0000003f);
4116 *out++ = alphabet[j];
4117 *out++ = alphabet[k];
4120 if ((options & OmitTrailingEquals) == 0)
4123 *out++ = alphabet[l];
4126 if ((options & OmitTrailingEquals) == 0)
4129 *out++ = alphabet[m];
4132 Q_ASSERT((options & OmitTrailingEquals) || (out == tmp.size() + tmp.data()));
4133 if (options & OmitTrailingEquals)
4134 tmp.truncate(out - tmp.data());
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4159
4160
4161
4162
4163
4166
4167
4168
4169
4170
4173
4174
4175
4176
4177
4180
4181
4182
4183
4184
4187
4188
4189
4190
4191
4194
4195
4196
4197
4200 constexpr int buffsize = 66;
4201 char buff[buffsize];
4206 p = qulltoa2(buff + buffsize, qulonglong(-(1 + n)) + 1, base);
4209 p = qulltoa2(buff + buffsize, qulonglong(n), base);
4212 return assign(QByteArrayView{p, buff + buffsize});
4216
4217
4218
4219
4223 constexpr int buffsize = 66;
4224 char buff[buffsize];
4225 char *p = qulltoa2(buff + buffsize, n, base);
4227 return assign(QByteArrayView{p, buff + buffsize});
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4244 return *
this = QByteArray::number(n, format, precision);
4248
4249
4250
4251
4252
4253
4254
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4280
4281
4282
4283
4292
4293
4294
4295
4304
4305
4306
4307
4316
4317
4318
4319
4328
4329
4330
4331
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4355 switch (QtMiscUtils::toAsciiLower(format)) {
4366#if defined(QT_CHECK_RANGE)
4367 qWarning(
"QByteArray::setNum: Invalid format char '%c'", format);
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4430 *
this = fromRawData(data, size);
4435struct fromBase64_helper_result {
4436 qsizetype decodedLength;
4437 QByteArray::Base64DecodingStatus status;
4440fromBase64_helper_result fromBase64_helper(
const char *input, qsizetype inputSize,
4442 QByteArray::Base64Options options)
4444 fromBase64_helper_result result{ 0, QByteArray::Base64DecodingStatus::Ok };
4446 unsigned int buf = 0;
4449 qsizetype offset = 0;
4450 for (qsizetype i = 0; i < inputSize; ++i) {
4454 if (ch >=
'A' && ch <=
'Z') {
4456 }
else if (ch >=
'a' && ch <=
'z') {
4458 }
else if (ch >=
'0' && ch <=
'9') {
4460 }
else if (ch ==
'+' && (options & QByteArray::Base64UrlEncoding) == 0) {
4462 }
else if (ch ==
'-' && (options & QByteArray::Base64UrlEncoding) != 0) {
4464 }
else if (ch ==
'/' && (options & QByteArray::Base64UrlEncoding) == 0) {
4466 }
else if (ch ==
'_' && (options & QByteArray::Base64UrlEncoding) != 0) {
4469 if (options & QByteArray::AbortOnBase64DecodingErrors) {
4473 if ((inputSize % 4) != 0) {
4474 result.status = QByteArray::Base64DecodingStatus::IllegalInputLength;
4476 }
else if ((i == inputSize - 1) ||
4477 (i == inputSize - 2 && input[++i] ==
'=')) {
4480 result.status = QByteArray::Base64DecodingStatus::IllegalPadding;
4484 result.status = QByteArray::Base64DecodingStatus::IllegalCharacter;
4493 buf = (buf << 6) | d;
4497 Q_ASSERT(offset < i);
4498 output[offset++] = buf >> nbits;
4499 buf &= (1 << nbits) - 1;
4504 result.decodedLength = offset;
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4540 if (base64.isDetached()) {
4541 const auto base64result = fromBase64_helper(base64.data(),
4545 base64.truncate(base64result.decodedLength);
4546 return {
std::move(base64), base64result.status };
4549 return fromBase64Encoding(base64, options);
4555 const auto base64Size = base64.size();
4556 QByteArray result((base64Size * 3) / 4, Qt::Uninitialized);
4557 const auto base64result = fromBase64_helper(base64.data(),
4559 const_cast<
char *>(result.constData()),
4561 result.truncate(base64result.decodedLength);
4562 return {
std::move(result), base64result.status };
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4591 if (
auto result = fromBase64Encoding(base64, options))
4592 return std::move(result.decoded);
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4609 QByteArray res((hexEncoded.size() + 1)/ 2, Qt::Uninitialized);
4610 uchar *result = (uchar *)res.data() + res.size();
4612 bool odd_digit =
true;
4613 for (qsizetype i = hexEncoded.size() - 1; i >= 0; --i) {
4614 uchar ch = uchar(hexEncoded.at(i));
4615 int tmp = QtMiscUtils::fromHex(ch);
4623 *result |= tmp << 4;
4628 res.remove(0, result - (
const uchar *)res.constData());
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4651 const qsizetype length = separator ? (size() * 3 - 1) : (size() * 2);
4653 char *hexData = hex.data();
4654 const uchar *data = (
const uchar *)
this->data();
4655 for (qsizetype i = 0, o = 0; i < size(); ++i) {
4656 hexData[o++] = QtMiscUtils::toHexLower(data[i] >> 4);
4657 hexData[o++] = QtMiscUtils::toHexLower(data[i] & 0xf);
4659 if ((separator) && (o < length))
4660 hexData[o++] = separator;
4667 char *data = buffer.begin();
4668 const char *inputPtr = src.begin();
4671 const qsizetype len = src.size();
4673 char c = inputPtr[i];
4674 if (c == percent && i + 2 < len) {
4675 if (
int a = QtMiscUtils::fromHex(uchar(inputPtr[++i])); a != -1)
4677 if (
int b = QtMiscUtils::fromHex(uchar(inputPtr[++i])); b != -1)
4686 return data - buffer.begin();
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4710
4711
4712
4713
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4731 if (input.isEmpty())
4734 QByteArray out{input.size(), Qt::Uninitialized};
4735 qsizetype len = q_fromPercentEncoding(input, percent, out);
4741
4742
4743
4746 if (input.d.needsDetach())
4747 return fromPercentEncoding(input, percent);
4749 if (input.isEmpty())
4750 return std::move(input);
4752 qsizetype len = q_fromPercentEncoding(input, percent, input);
4753 input.truncate(len);
4754 return std::move(input);
4758
4759
4760
4761
4762
4763
4766 return QByteArray(s.data(), qsizetype(s.size()));
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4783 return std::string(data(), size_t(size()));
4787
4788
4789
4790
4791
4792
4793
4794
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4828 const auto contains = [](
const QByteArray &view,
char c) {
4830 return view.size() > 0 && memchr(view.data(), c, view.size()) !=
nullptr;
4834 char *output =
nullptr;
4835 qsizetype length = 0;
4837 for (
unsigned char c : *
this) {
4838 if (
char(c) != percent
4839 && ((c >= 0x61 && c <= 0x7A)
4840 || (c >= 0x41 && c <= 0x5A)
4841 || (c >= 0x30 && c <= 0x39)
4846 || contains(exclude, c))
4847 && !contains(include, c)) {
4854 result.resize(size() * 3);
4855 output = result.data();
4857 output[length++] = percent;
4858 output[length++] = QtMiscUtils::toHexUpper((c & 0xf0) >> 4);
4859 output[length++] = QtMiscUtils::toHexUpper(c & 0xf);
4863 result.truncate(length);
4868#if defined(Q_OS_WASM) || defined(Q_QDOC)
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4893QByteArray QByteArray::fromEcmaUint8Array(emscripten::val uint8array)
4895 return qstdweb::Uint8Array(uint8array).copyToQByteArray();
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915emscripten::val QByteArray::toEcmaUint8Array()
4917 return qstdweb::Uint8Array::copyFrom(*
this).val();
4923
4924
4927
4928
4931
4932
4933
4934
4935
4938
4939
4940
4941
4942
4945
4946
4947
4948
4949
4950
4953
4954
4955
4956
4957
4958
4961
4962
4965
4966
4969
4970
4973
4974
4977
4978
4981
4982
4985
4986
4989
4990
4991
4994
4995
4996
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5018#if QT_DEPRECATED_SINCE(6
, 8
)
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5094
5095
5096
5097
5100
5101
5102
5103
5104
5107
5108
5109
5110
5111
5112
5115
5116
5117
5118
5119
5120
5121
5124
5125
5126
5127
5128
5129
5130
5131
5134
5135
5136
5137
5138
5141
5142
5143size_t
qHash(
const QByteArray::FromBase64Result &key, size_t seed)
noexcept
5145 return qHashMulti(seed, key.decoded,
static_cast<
int>(key.decodingStatus));
5149
5150
5151
5152
5153
5154
5155
5156
5159
5160
5161
5162
5163
5164
5165
5166
5167
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 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)