Ignore:
Timestamp:
Feb 11, 2010, 11:19:06 PM (15 years ago)
Author:
Dmitry A. Kuminov
Message:

trunk: Merged in qt 4.6.1 sources.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk

  • trunk/src/corelib/tools/qbytearray.cpp

    r2 r561  
    22**
    33** 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])
    56**
    67** This file is part of the QtCore module of the Qt Toolkit.
     
    2122** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
    2223**
    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.
    2727**
    2828** GNU General Public License Usage
     
    3434** met: http://www.gnu.org/copyleft/gpl.html.
    3535**
    36 ** If you are unsure which license is appropriate for your use, please
    37 ** contact the sales department at qt-sales@nokia.com.
     36** If you
     37** @nokia.com.
    3838** $QT_END_LICENSE$
    3939**
     
    4848#include "qlocale_p.h"
    4949#include "qunicodetables_p.h"
    50 #ifndef QT_NO_DATASTREAM
     50#i
    5151#include <qdatastream.h>
    52 #endif
    5352
    5453#ifndef QT_NO_COMPRESS
     
    536535                       (data[2] <<  8) | (data[3]      );
    537536    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);
    544550
    545551        switch (res) {
    546552        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
    550567        case Z_MEM_ERROR:
    551568            qWarning("qUncompress: Z_MEM_ERROR: Not enough memory");
    552             break;
     569            return QByteArray();
     570
    553571        case Z_BUF_ERROR:
    554572            len *= 2;
    555             break;
     573            continue;
     574
    556575        case Z_DATA_ERROR:
    557576            qWarning("qUncompress: Z_DATA_ERROR: Input data is corrupted");
    558             break;
     577            ;
    559578        }
    560     } while (res == Z_BUF_ERROR);
    561 
    562     if (res != Z_OK)
    563         baunzip = QByteArray();
    564 
    565     return baunzip;
     579    }
    566580}
    567581#endif
     
    580594}
    581595
    582 Q_CORE_EXPORT QByteArray::Data QByteArray::shared_null = {Q_BASIC_ATOMIC_INITIALIZER(1),
     596QByteArray::Data QByteArray::shared_null = {Q_BASIC_ATOMIC_INITIALIZER(1),
    583597                                                          0, 0, shared_null.array, {0} };
    584598QByteArray::Data QByteArray::shared_empty = { Q_BASIC_ATOMIC_INITIALIZER(1),
     
    591605    \ingroup tools
    592606    \ingroup shared
    593     \ingroup text
    594     \mainclass
     607    \ingroup
     608
    595609    \reentrant
    596610
     
    890904    addition, QByteArray ensures that the byte at position size() is
    891905    always '\\0', so that you can use the return value of data() and
    892     constData() as arguments to functions that expect
    893     '\\0'-terminated strings.
     906    constData() as arguments to functions that expect
     907    strings.
    894908
    895909    Example:
     
    984998    Returns a pointer to the data stored in the byte array. The
    985999    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.
    9871003
    9881004    Example:
     
    9951011    This function is mostly useful to pass a byte array to a function
    9961012    that accepts a \c{const char *}.
     1013
     1014
     1015
     1016
     1017
     1018
     1019
     1020
     1021
     1022
    9971023
    9981024    Note: A QByteArray can store any byte values including '\\0's,
     
    12341260        int len = qstrlen(str);
    12351261        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
    12441267    }
    12451268    d->ref.ref();
     
    12651288    } else {
    12661289        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';
    12761296    }
    12771297    d->ref.ref();
     
    12911311    } else {
    12921312        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);
    13021319    }
    13031320    d->ref.ref();
     1321
     1322
     1323
     1324
     1325
     1326
     1327
     1328
     1329
     1330
     1331
     1332
     1333
     1334
     1335
     1336
    13041337}
    13051338
     
    13351368        //
    13361369        Data *x = static_cast<Data *>(qMalloc(sizeof(Data)+size));
    1337         if (!x)
    1338             return;
     1370        Q_CHECK_PTR(x);
    13391371        x->ref = 1;
    13401372        x->alloc = x->size = size;
     
    13781410    if (d->ref != 1 || d->data != d->array) {
    13791411        Data *x = static_cast<Data *>(qMalloc(sizeof(Data) + alloc));
    1380         if (!x)
    1381             return;
     1412        Q_CHECK_PTR(x);
    13821413        x->size = qMin(alloc, d->size);
    13831414        ::memcpy(x->array, d->data, x->size);
     
    13911422    } else {
    13921423        Data *x = static_cast<Data *>(qRealloc(d, sizeof(Data) + alloc));
    1393         if (!x)
    1394             return;
     1424        Q_CHECK_PTR(x);
    13951425        x->alloc = alloc;
    13961426        x->data = x->array;
     
    14601490QByteArray &QByteArray::prepend(const char *str)
    14611491{
     1492
     1493
     1494
     1495
     1496
     1497
     1498
     1499
     1500
     1501
     1502
     1503
    14621504    if (str) {
    1463         int len = qstrlen(str);
    14641505        if (d->ref != 1 || d->size + len > d->alloc)
    14651506            realloc(qAllocMore(d->size + len, sizeof(Data)));
     
    15631604
    15641605    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.
    15671609*/
    15681610
     
    16721714/*!
    16731715    \overload
     1716
     1717
     1718
     1719
     1720
     1721
     1722
     1723
     1724
     1725
     1726
     1727
     1728
     1729
     1730
     1731
    16741732
    16751733    Inserts character \a ch at index position \a i in the byte array.
     
    18131871    if (after >= d->data && after < d->data + d->size) {
    18141872        char *copy = (char *)malloc(asize);
     1873
    18151874        memcpy(copy, after, asize);
    18161875        a = copy;
     
    18181877    if (before >= d->data && before < d->data + d->size) {
    18191878        char *copy = (char *)malloc(bsize);
     1879
    18201880        memcpy(copy, before, bsize);
    18211881        b = copy;
     
    25922652}
    25932653
     2654
     2655
    25942656/*! \relates QByteArray
    25952657
     
    25992661    \sa {Format of the QDataStream operators}
    26002662*/
    2601 #ifndef QT_NO_DATASTREAM
    26022663
    26032664QDataStream &operator<<(QDataStream &out, const QByteArray &ba)
     
    26422703    return in;
    26432704}
    2644 #endif //QT_NO_DATASTREAM
     2705#endif //QT_NO_DATASTREAM
    26452706
    26462707/*! \fn bool QByteArray::operator==(const QString &str) const
     
    29773038    if (d->size == 0)
    29783039        return *this;
    2979     QByteArray result;
    2980     result.resize(d->size);
     3040    QByteArray result(d->size, Qt::Uninitialized);
    29813041    const char *from = d->data;
    29823042    const char *fromend = from + d->size;
     
    34293489    int padlen = 0;
    34303490
    3431     QByteArray tmp;
    3432     tmp.resize(((d->size * 4) / 3) + 3);
     3491    QByteArray tmp((d->size * 4) / 3 + 3, Qt::Uninitialized);
    34333492
    34343493    int i = 0;
     
    37393798{
    37403799    Data *x = static_cast<Data *>(qMalloc(sizeof(Data)));
     3800
    37413801    if (data) {
    37423802        x->data = const_cast<char *>(data);
     
    37683828    unsigned int buf = 0;
    37693829    int nbits = 0;
    3770     QByteArray tmp;
    3771     tmp.resize((base64.size() * 3) / 4);
     3830    QByteArray tmp((base64.size() * 3) / 4, Qt::Uninitialized);
    37723831
    37733832    int offset = 0;
     
    38173876QByteArray QByteArray::fromHex(const QByteArray &hexEncoded)
    38183877{
    3819     QByteArray res;
    3820     res.resize((hexEncoded.size() + 1)/ 2);
     3878    QByteArray res((hexEncoded.size() + 1)/ 2, Qt::Uninitialized);
    38213879    uchar *result = (uchar *)res.data() + res.size();
    38223880
     
    38553913QByteArray QByteArray::toHex() const
    38563914{
    3857     QByteArray hex;
    3858     hex.resize(d->size*2);
     3915    QByteArray hex(d->size * 2, Qt::Uninitialized);
    38593916    char *hexData = hex.data();
    38603917    const uchar *data = (const uchar *)d->data;
     
    40934150*/
    40944151
     4152
     4153
     4154
     4155
    40954156/*!
    40964157    \fn QByteArray::QByteArray(int size)
Note: See TracChangeset for help on using the changeset viewer.