Qt 4.8
Functions
qsvgtinydocument.cpp File Reference
#include "qsvgtinydocument_p.h"
#include "qsvghandler_p.h"
#include "qsvgfont_p.h"
#include "qpainter.h"
#include "qfile.h"
#include "qbuffer.h"
#include "qbytearray.h"
#include "qqueue.h"
#include "qstack.h"
#include "qdebug.h"
#include <zlib.h>

Go to the source code of this file.

Functions

static QByteArray qt_inflateGZipDataFrom (QIODevice *device)
 

Function Documentation

◆ qt_inflateGZipDataFrom()

QByteArray qt_inflateGZipDataFrom ( QIODevice device)
static

Definition at line 84 of file qsvgtinydocument.cpp.

Referenced by QSvgTinyDocument::load(), and QSvgTinyDocument::~QSvgTinyDocument().

85 {
86  if (!device)
87  return QByteArray();
88 
89  if (!device->isOpen())
90  device->open(QIODevice::ReadOnly);
91 
92  Q_ASSERT(device->isOpen() && device->isReadable());
93 
94  static const int CHUNK_SIZE = 4096;
95  int zlibResult = Z_OK;
96 
97  QByteArray source;
98  QByteArray destination;
99 
100  // Initialize zlib stream struct
101  z_stream zlibStream;
102  zlibStream.next_in = Z_NULL;
103  zlibStream.avail_in = 0;
104  zlibStream.avail_out = 0;
105  zlibStream.zalloc = Z_NULL;
106  zlibStream.zfree = Z_NULL;
107  zlibStream.opaque = Z_NULL;
108 
109  // Adding 16 to the window size gives us gzip decoding
110  if (inflateInit2(&zlibStream, MAX_WBITS + 16) != Z_OK) {
111  qWarning("Cannot initialize zlib, because: %s",
112  (zlibStream.msg != NULL ? zlibStream.msg : "Unknown error"));
113  return QByteArray();
114  }
115 
116  bool stillMoreWorkToDo = true;
117  while (stillMoreWorkToDo) {
118 
119  if (!zlibStream.avail_in) {
120  source = device->read(CHUNK_SIZE);
121 
122  if (source.isEmpty())
123  break;
124 
125  zlibStream.avail_in = source.size();
126  zlibStream.next_in = reinterpret_cast<Bytef*>(source.data());
127  }
128 
129  do {
130  // Prepare the destination buffer
131  int oldSize = destination.size();
132  destination.resize(oldSize + CHUNK_SIZE);
133  zlibStream.next_out = reinterpret_cast<Bytef*>(
134  destination.data() + oldSize - zlibStream.avail_out);
135  zlibStream.avail_out += CHUNK_SIZE;
136 
137  zlibResult = inflate(&zlibStream, Z_NO_FLUSH);
138  switch (zlibResult) {
139  case Z_NEED_DICT:
140  case Z_DATA_ERROR:
141  case Z_STREAM_ERROR:
142  case Z_MEM_ERROR: {
143  inflateEnd(&zlibStream);
144  qWarning("Error while inflating gzip file: %s",
145  (zlibStream.msg != NULL ? zlibStream.msg : "Unknown error"));
146  destination.chop(zlibStream.avail_out);
147  return destination;
148  }
149  }
150 
151  // If the output buffer still has more room after calling inflate
152  // it means we have to provide more data, so exit the loop here
153  } while (!zlibStream.avail_out);
154 
155  if (zlibResult == Z_STREAM_END) {
156  // Make sure there are no more members to process before exiting
157  if (!(zlibStream.avail_in && inflateReset(&zlibStream) == Z_OK))
158  stillMoreWorkToDo = false;
159  }
160  }
161 
162  // Chop off trailing space in the buffer
163  destination.chop(zlibStream.avail_out);
164 
165  inflateEnd(&zlibStream);
166  return destination;
167 }
void chop(int n)
Removes n bytes from the end of the byte array.
char * data()
Returns a pointer to the data stored in the byte array.
Definition: qbytearray.h:429
bool isReadable() const
Returns true if data can be read from the device; otherwise returns false.
Definition: qiodevice.cpp:544
The QByteArray class provides an array of bytes.
Definition: qbytearray.h:135
#define Q_ASSERT(cond)
Definition: qglobal.h:1823
qint64 read(char *data, qint64 maxlen)
Reads at most maxSize bytes from the device into data, and returns the number of bytes read...
Definition: qiodevice.cpp:791
bool isOpen() const
Returns true if the device is open; otherwise returns false.
Definition: qiodevice.cpp:530
#define CHUNK_SIZE
Q_CORE_EXPORT void qWarning(const char *,...)
static int inflate(Bytef *dest, ulong *destLen, const Bytef *source, ulong sourceLen)
Definition: qzip.cpp:189
void resize(int size)
Sets the size of the byte array to size bytes.
int size() const
Returns the number of bytes in this byte array.
Definition: qbytearray.h:402
virtual bool open(OpenMode mode)
Opens the device and sets its OpenMode to mode.
Definition: qiodevice.cpp:570
bool isEmpty() const
Returns true if the byte array has size 0; otherwise returns false.
Definition: qbytearray.h:421