Changeset 561 for trunk/src/corelib/tools/qbytearray.cpp
- Timestamp:
- Feb 11, 2010, 11:19:06 PM (15 years ago)
- Location:
- trunk
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk
-
Property svn:mergeinfo
set to (toggle deleted branches)
/branches/vendor/nokia/qt/4.6.1 merged eligible /branches/vendor/nokia/qt/current merged eligible /branches/vendor/trolltech/qt/current 3-149
-
Property svn:mergeinfo
set to (toggle deleted branches)
-
trunk/src/corelib/tools/qbytearray.cpp
r2 r561 2 2 ** 3 3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). 4 ** Contact: Qt Software Information ([email protected]) 4 ** All rights reserved. 5 ** Contact: Nokia Corporation ([email protected]) 5 6 ** 6 7 ** This file is part of the QtCore module of the Qt Toolkit. … … 21 22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. 22 23 ** 23 ** In addition, as a special exception, Nokia gives you certain 24 ** additional rights. These rights are described in the Nokia Qt LGPL 25 ** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this 26 ** package. 24 ** In addition, as a special exception, Nokia gives you certain additional 25 ** rights. These rights are described in the Nokia Qt LGPL Exception 26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. 27 27 ** 28 28 ** GNU General Public License Usage … … 34 34 ** met: http://www.gnu.org/copyleft/gpl.html. 35 35 ** 36 ** If you are unsure which license is appropriate for your use, please37 ** contact the sales department at qt-sales@nokia.com.36 ** If you 37 ** @nokia.com. 38 38 ** $QT_END_LICENSE$ 39 39 ** … … 48 48 #include "qlocale_p.h" 49 49 #include "qunicodetables_p.h" 50 #i fndef QT_NO_DATASTREAM50 #i 51 51 #include <qdatastream.h> 52 #endif53 52 54 53 #ifndef QT_NO_COMPRESS … … 536 535 (data[2] << 8) | (data[3] ); 537 536 ulong len = qMax(expectedSize, 1ul); 538 QByteArray baunzip; 539 int res; 540 do { 541 baunzip.resize(len); 542 res = ::uncompress((uchar*)baunzip.data(), &len, 543 (uchar*)data+4, nbytes-4); 537 QScopedPointer<QByteArray::Data, QScopedPointerPodDeleter> d; 538 539 forever { 540 ulong alloc = len; 541 d.reset(q_check_ptr(static_cast<QByteArray::Data *>(qRealloc(d.data(), sizeof(QByteArray::Data) + alloc)))); 542 if (!d) { 543 // we are not allowed to crash here when compiling with QT_NO_EXCEPTIONS 544 qWarning("qUncompress: could not allocate enough memory to uncompress data"); 545 return QByteArray(); 546 } 547 548 int res = ::uncompress((uchar*)d->array, &len, 549 (uchar*)data+4, nbytes-4); 544 550 545 551 switch (res) { 546 552 case Z_OK: 547 if ((int)len != baunzip.size()) 548 baunzip.resize(len); 549 break; 553 if (len != alloc) { 554 d.reset(q_check_ptr(static_cast<QByteArray::Data *>(qRealloc(d.data(), sizeof(QByteArray::Data) + len)))); 555 if (!d) { 556 // we are not allowed to crash here when compiling with QT_NO_EXCEPTIONS 557 qWarning("qUncompress: could not allocate enough memory to uncompress data"); 558 return QByteArray(); 559 } 560 } 561 d->ref = 1; 562 d->alloc = d->size = len; 563 d->data = d->array; 564 565 return QByteArray(d.take(), 0, 0); 566 550 567 case Z_MEM_ERROR: 551 568 qWarning("qUncompress: Z_MEM_ERROR: Not enough memory"); 552 break; 569 return QByteArray(); 570 553 571 case Z_BUF_ERROR: 554 572 len *= 2; 555 break; 573 continue; 574 556 575 case Z_DATA_ERROR: 557 576 qWarning("qUncompress: Z_DATA_ERROR: Input data is corrupted"); 558 break;577 ; 559 578 } 560 } while (res == Z_BUF_ERROR); 561 562 if (res != Z_OK) 563 baunzip = QByteArray(); 564 565 return baunzip; 579 } 566 580 } 567 581 #endif … … 580 594 } 581 595 582 Q _CORE_EXPORT QByteArray::Data QByteArray::shared_null = {Q_BASIC_ATOMIC_INITIALIZER(1),596 QByteArray::Data QByteArray::shared_null = {Q_BASIC_ATOMIC_INITIALIZER(1), 583 597 0, 0, shared_null.array, {0} }; 584 598 QByteArray::Data QByteArray::shared_empty = { Q_BASIC_ATOMIC_INITIALIZER(1), … … 591 605 \ingroup tools 592 606 \ingroup shared 593 \ingroup text594 \mainclass 607 \ingroup 608 595 609 \reentrant 596 610 … … 890 904 addition, QByteArray ensures that the byte at position size() is 891 905 always '\\0', so that you can use the return value of data() and 892 constData() as arguments to functions that expect 893 '\\0'-terminatedstrings.906 constData() as arguments to functions that expect 907 strings. 894 908 895 909 Example: … … 984 998 Returns a pointer to the data stored in the byte array. The 985 999 pointer can be used to access and modify the bytes that compose 986 the array. The data is '\\0'-terminated. 1000 the array. The data is '\\0'-terminated, i.e. the number of 1001 bytes in the returned character string is size() + 1 for the 1002 '\\0' terminator. 987 1003 988 1004 Example: … … 995 1011 This function is mostly useful to pass a byte array to a function 996 1012 that accepts a \c{const char *}. 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 997 1023 998 1024 Note: A QByteArray can store any byte values including '\\0's, … … 1234 1260 int len = qstrlen(str); 1235 1261 d = static_cast<Data *>(qMalloc(sizeof(Data)+len)); 1236 if (!d) { 1237 d = &shared_null; 1238 } else { 1239 d->ref = 0;; 1240 d->alloc = d->size = len; 1241 d->data = d->array; 1242 memcpy(d->array, str, len+1); // include null terminator 1243 } 1262 Q_CHECK_PTR(d); 1263 d->ref = 0;; 1264 d->alloc = d->size = len; 1265 d->data = d->array; 1266 memcpy(d->array, str, len+1); // include null terminator 1244 1267 } 1245 1268 d->ref.ref(); … … 1265 1288 } else { 1266 1289 d = static_cast<Data *>(qMalloc(sizeof(Data) + size)); 1267 if (!d) { 1268 d = &shared_null; 1269 } else { 1270 d->ref = 0; 1271 d->alloc = d->size = size; 1272 d->data = d->array; 1273 memcpy(d->array, data, size); 1274 d->array[size] = '\0'; 1275 } 1290 Q_CHECK_PTR(d); 1291 d->ref = 0; 1292 d->alloc = d->size = size; 1293 d->data = d->array; 1294 memcpy(d->array, data, size); 1295 d->array[size] = '\0'; 1276 1296 } 1277 1297 d->ref.ref(); … … 1291 1311 } else { 1292 1312 d = static_cast<Data *>(qMalloc(sizeof(Data)+size)); 1293 if (!d) { 1294 d = &shared_null; 1295 } else { 1296 d->ref = 0; 1297 d->alloc = d->size = size; 1298 d->data = d->array; 1299 d->array[size] = '\0'; 1300 memset(d->array, ch, size); 1301 } 1313 Q_CHECK_PTR(d); 1314 d->ref = 0; 1315 d->alloc = d->size = size; 1316 d->data = d->array; 1317 d->array[size] = '\0'; 1318 memset(d->array, ch, size); 1302 1319 } 1303 1320 d->ref.ref(); 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1304 1337 } 1305 1338 … … 1335 1368 // 1336 1369 Data *x = static_cast<Data *>(qMalloc(sizeof(Data)+size)); 1337 if (!x) 1338 return; 1370 Q_CHECK_PTR(x); 1339 1371 x->ref = 1; 1340 1372 x->alloc = x->size = size; … … 1378 1410 if (d->ref != 1 || d->data != d->array) { 1379 1411 Data *x = static_cast<Data *>(qMalloc(sizeof(Data) + alloc)); 1380 if (!x) 1381 return; 1412 Q_CHECK_PTR(x); 1382 1413 x->size = qMin(alloc, d->size); 1383 1414 ::memcpy(x->array, d->data, x->size); … … 1391 1422 } else { 1392 1423 Data *x = static_cast<Data *>(qRealloc(d, sizeof(Data) + alloc)); 1393 if (!x) 1394 return; 1424 Q_CHECK_PTR(x); 1395 1425 x->alloc = alloc; 1396 1426 x->data = x->array; … … 1460 1490 QByteArray &QByteArray::prepend(const char *str) 1461 1491 { 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1462 1504 if (str) { 1463 int len = qstrlen(str);1464 1505 if (d->ref != 1 || d->size + len > d->alloc) 1465 1506 realloc(qAllocMore(d->size + len, sizeof(Data))); … … 1563 1604 1564 1605 If \a len is negative, the length of the string will be determined 1565 automatically using qstrlen(). If \a len is zero or the length of the 1566 string is zero, nothing will be appended to the byte array. 1606 automatically using qstrlen(). If \a len is zero or \a str is 1607 null, nothing is appended to the byte array. Ensure that \a len is 1608 \e not longer than \a str. 1567 1609 */ 1568 1610 … … 1672 1714 /*! 1673 1715 \overload 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1674 1732 1675 1733 Inserts character \a ch at index position \a i in the byte array. … … 1813 1871 if (after >= d->data && after < d->data + d->size) { 1814 1872 char *copy = (char *)malloc(asize); 1873 1815 1874 memcpy(copy, after, asize); 1816 1875 a = copy; … … 1818 1877 if (before >= d->data && before < d->data + d->size) { 1819 1878 char *copy = (char *)malloc(bsize); 1879 1820 1880 memcpy(copy, before, bsize); 1821 1881 b = copy; … … 2592 2652 } 2593 2653 2654 2655 2594 2656 /*! \relates QByteArray 2595 2657 … … 2599 2661 \sa {Format of the QDataStream operators} 2600 2662 */ 2601 #ifndef QT_NO_DATASTREAM2602 2663 2603 2664 QDataStream &operator<<(QDataStream &out, const QByteArray &ba) … … 2642 2703 return in; 2643 2704 } 2644 #endif // QT_NO_DATASTREAM2705 #endif //QT_NO_DATASTREAM 2645 2706 2646 2707 /*! \fn bool QByteArray::operator==(const QString &str) const … … 2977 3038 if (d->size == 0) 2978 3039 return *this; 2979 QByteArray result; 2980 result.resize(d->size); 3040 QByteArray result(d->size, Qt::Uninitialized); 2981 3041 const char *from = d->data; 2982 3042 const char *fromend = from + d->size; … … 3429 3489 int padlen = 0; 3430 3490 3431 QByteArray tmp; 3432 tmp.resize(((d->size * 4) / 3) + 3); 3491 QByteArray tmp((d->size * 4) / 3 + 3, Qt::Uninitialized); 3433 3492 3434 3493 int i = 0; … … 3739 3798 { 3740 3799 Data *x = static_cast<Data *>(qMalloc(sizeof(Data))); 3800 3741 3801 if (data) { 3742 3802 x->data = const_cast<char *>(data); … … 3768 3828 unsigned int buf = 0; 3769 3829 int nbits = 0; 3770 QByteArray tmp; 3771 tmp.resize((base64.size() * 3) / 4); 3830 QByteArray tmp((base64.size() * 3) / 4, Qt::Uninitialized); 3772 3831 3773 3832 int offset = 0; … … 3817 3876 QByteArray QByteArray::fromHex(const QByteArray &hexEncoded) 3818 3877 { 3819 QByteArray res; 3820 res.resize((hexEncoded.size() + 1)/ 2); 3878 QByteArray res((hexEncoded.size() + 1)/ 2, Qt::Uninitialized); 3821 3879 uchar *result = (uchar *)res.data() + res.size(); 3822 3880 … … 3855 3913 QByteArray QByteArray::toHex() const 3856 3914 { 3857 QByteArray hex; 3858 hex.resize(d->size*2); 3915 QByteArray hex(d->size * 2, Qt::Uninitialized); 3859 3916 char *hexData = hex.data(); 3860 3917 const uchar *data = (const uchar *)d->data; … … 4093 4150 */ 4094 4151 4152 4153 4154 4155 4095 4156 /*! 4096 4157 \fn QByteArray::QByteArray(int size)
Note:
See TracChangeset
for help on using the changeset viewer.