1 /**************************************************************************** |
1 /**************************************************************************** |
2 ** |
2 ** |
3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). |
3 ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). |
4 ** All rights reserved. |
4 ** All rights reserved. |
5 ** Contact: Nokia Corporation (qt-info@nokia.com) |
5 ** Contact: Nokia Corporation (qt-info@nokia.com) |
6 ** |
6 ** |
7 ** This file is part of the QtCore module of the Qt Toolkit. |
7 ** This file is part of the QtCore module of the Qt Toolkit. |
8 ** |
8 ** |
536 ulong len = qMax(expectedSize, 1ul); |
536 ulong len = qMax(expectedSize, 1ul); |
537 QScopedPointer<QByteArray::Data, QScopedPointerPodDeleter> d; |
537 QScopedPointer<QByteArray::Data, QScopedPointerPodDeleter> d; |
538 |
538 |
539 forever { |
539 forever { |
540 ulong alloc = len; |
540 ulong alloc = len; |
541 d.reset(q_check_ptr(static_cast<QByteArray::Data *>(qRealloc(d.data(), sizeof(QByteArray::Data) + alloc)))); |
541 d.reset(q_check_ptr(static_cast<QByteArray::Data *>(qRealloc(d.take(), sizeof(QByteArray::Data) + alloc)))); |
542 if (!d) { |
542 if (!d) { |
543 // we are not allowed to crash here when compiling with QT_NO_EXCEPTIONS |
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"); |
544 qWarning("qUncompress: could not allocate enough memory to uncompress data"); |
545 return QByteArray(); |
545 return QByteArray(); |
546 } |
546 } |
549 (uchar*)data+4, nbytes-4); |
549 (uchar*)data+4, nbytes-4); |
550 |
550 |
551 switch (res) { |
551 switch (res) { |
552 case Z_OK: |
552 case Z_OK: |
553 if (len != alloc) { |
553 if (len != alloc) { |
554 d.reset(q_check_ptr(static_cast<QByteArray::Data *>(qRealloc(d.data(), sizeof(QByteArray::Data) + len)))); |
554 d.reset(q_check_ptr(static_cast<QByteArray::Data *>(qRealloc(d.take(), sizeof(QByteArray::Data) + len)))); |
555 if (!d) { |
555 if (!d) { |
556 // we are not allowed to crash here when compiling with QT_NO_EXCEPTIONS |
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"); |
557 qWarning("qUncompress: could not allocate enough memory to uncompress data"); |
558 return QByteArray(); |
558 return QByteArray(); |
559 } |
559 } |
560 } |
560 } |
561 d->ref = 1; |
561 d->ref = 1; |
562 d->alloc = d->size = len; |
562 d->alloc = d->size = len; |
563 d->data = d->array; |
563 d->data = d->array; |
|
564 d->array[len] = 0; |
564 |
565 |
565 return QByteArray(d.take(), 0, 0); |
566 return QByteArray(d.take(), 0, 0); |
566 |
567 |
567 case Z_MEM_ERROR: |
568 case Z_MEM_ERROR: |
568 qWarning("qUncompress: Z_MEM_ERROR: Not enough memory"); |
569 qWarning("qUncompress: Z_MEM_ERROR: Not enough memory"); |
901 Returns the number of bytes in this byte array. |
902 Returns the number of bytes in this byte array. |
902 |
903 |
903 The last byte in the byte array is at position size() - 1. In |
904 The last byte in the byte array is at position size() - 1. In |
904 addition, QByteArray ensures that the byte at position size() is |
905 addition, QByteArray ensures that the byte at position size() is |
905 always '\\0', so that you can use the return value of data() and |
906 always '\\0', so that you can use the return value of data() and |
906 constData() as arguments to functions that expect |
907 constData() as arguments to functions that expect '\\0'-terminated |
907 '\\0'-terminated strings. |
908 strings. |
908 |
909 |
909 Example: |
910 Example: |
910 \snippet doc/src/snippets/code/src_corelib_tools_qbytearray.cpp 6 |
911 \snippet doc/src/snippets/code/src_corelib_tools_qbytearray.cpp 6 |
911 |
912 |
912 \sa isEmpty(), resize() |
913 \sa isEmpty(), resize() |
995 |
996 |
996 /*! \fn char *QByteArray::data() |
997 /*! \fn char *QByteArray::data() |
997 |
998 |
998 Returns a pointer to the data stored in the byte array. The |
999 Returns a pointer to the data stored in the byte array. The |
999 pointer can be used to access and modify the bytes that compose |
1000 pointer can be used to access and modify the bytes that compose |
1000 the array. The data is '\\0'-terminated. |
1001 the array. The data is '\\0'-terminated, i.e. the number of |
|
1002 bytes in the returned character string is size() + 1 for the |
|
1003 '\\0' terminator. |
1001 |
1004 |
1002 Example: |
1005 Example: |
1003 \snippet doc/src/snippets/code/src_corelib_tools_qbytearray.cpp 8 |
1006 \snippet doc/src/snippets/code/src_corelib_tools_qbytearray.cpp 8 |
1004 |
1007 |
1005 The pointer remains valid as long as the byte array isn't |
1008 The pointer remains valid as long as the byte array isn't |
1006 reallocated or destroyed. For read-only access, constData() is |
1009 reallocated or destroyed. For read-only access, constData() is |
1007 faster because it never causes a \l{deep copy} to occur. |
1010 faster because it never causes a \l{deep copy} to occur. |
1008 |
1011 |
1009 This function is mostly useful to pass a byte array to a function |
1012 This function is mostly useful to pass a byte array to a function |
1010 that accepts a \c{const char *}. |
1013 that accepts a \c{const char *}. |
|
1014 |
|
1015 The following example makes a copy of the char* returned by |
|
1016 data(), but it will corrupt the heap and cause a crash because it |
|
1017 does not allocate a byte for the '\\0' at the end: |
|
1018 |
|
1019 \snippet doc/src/snippets/code/src_corelib_tools_qbytearray.cpp 46 |
|
1020 |
|
1021 This one allocates the correct amount of space: |
|
1022 |
|
1023 \snippet doc/src/snippets/code/src_corelib_tools_qbytearray.cpp 47 |
1011 |
1024 |
1012 Note: A QByteArray can store any byte values including '\\0's, |
1025 Note: A QByteArray can store any byte values including '\\0's, |
1013 but most functions that take \c{char *} arguments assume that the |
1026 but most functions that take \c{char *} arguments assume that the |
1014 data ends at the first '\\0' they encounter. |
1027 data ends at the first '\\0' they encounter. |
1015 |
1028 |
1589 |
1602 |
1590 Appends the first \a len characters of the string \a str to this byte |
1603 Appends the first \a len characters of the string \a str to this byte |
1591 array and returns a reference to this byte array. |
1604 array and returns a reference to this byte array. |
1592 |
1605 |
1593 If \a len is negative, the length of the string will be determined |
1606 If \a len is negative, the length of the string will be determined |
1594 automatically using qstrlen(). If \a len is zero or the length of the |
1607 automatically using qstrlen(). If \a len is zero or \a str is |
1595 string is zero, nothing will be appended to the byte array. |
1608 null, nothing is appended to the byte array. Ensure that \a len is |
|
1609 \e not longer than \a str. |
1596 */ |
1610 */ |
1597 |
1611 |
1598 QByteArray &QByteArray::append(const char *str, int len) |
1612 QByteArray &QByteArray::append(const char *str, int len) |
1599 { |
1613 { |
1600 if (len < 0) |
1614 if (len < 0) |