Qt 4.8
Classes | Macros | Functions
qzip.cpp File Reference
#include <qglobal.h>
#include "qzipreader_p.h"
#include "qzipwriter_p.h"
#include <qdatetime.h>
#include <qplatformdefs.h>
#include <qendian.h>
#include <qdebug.h>
#include <qdir.h>
#include <zlib.h>

Go to the source code of this file.

Classes

struct  CentralFileHeader
 
struct  DataDescriptor
 
struct  EndOfDirectory
 
struct  FileHeader
 
struct  LocalFileHeader
 
class  QZipPrivate
 
class  QZipReaderPrivate
 
class  QZipWriterPrivate
 

Macros

#define S_IFDIR   0040000
 
#define S_IFLNK   020000
 
#define S_IFREG   0100000
 
#define S_IRGRP   0040
 
#define S_IROTH   0004
 
#define S_IRUSR   0400
 
#define S_ISDIR(x)   ((x) & S_IFDIR) > 0
 
#define S_ISLNK(x)   ((x) & S_IFLNK) > 0
 
#define S_ISREG(x)   ((x) & 0170000) == S_IFREG
 
#define S_IWGRP   0020
 
#define S_IWOTH   0002
 
#define S_IWUSR   0200
 
#define S_IXGRP   0010
 
#define S_IXOTH   0001
 
#define S_IXUSR   0100
 
#define ZDEBUG   if (0) qDebug
 

Functions

static void copyUInt (uchar *dest, const uchar *src)
 
static void copyUShort (uchar *dest, const uchar *src)
 
static int deflate (Bytef *dest, ulong *destLen, const Bytef *source, ulong sourceLen)
 
static int inflate (Bytef *dest, ulong *destLen, const Bytef *source, ulong sourceLen)
 
static QFile::Permissions modeToPermissions (quint32 mode)
 
static quint32 permissionsToMode (QFile::Permissions perms)
 
static QDateTime readMSDosDate (const uchar *src)
 
static uint readUInt (const uchar *data)
 
static ushort readUShort (const uchar *data)
 
static void writeMSDosDate (uchar *dest, const QDateTime &dt)
 
static void writeUInt (uchar *data, uint i)
 
static void writeUShort (uchar *data, ushort i)
 

Macro Definition Documentation

◆ S_IFDIR

#define S_IFDIR   0040000

Definition at line 60 of file qzip.cpp.

Referenced by QZipWriterPrivate::addEntry().

◆ S_IFLNK

#define S_IFLNK   020000

Definition at line 68 of file qzip.cpp.

Referenced by QZipWriterPrivate::addEntry().

◆ S_IFREG

#define S_IFREG   0100000

Definition at line 58 of file qzip.cpp.

Referenced by QZipWriterPrivate::addEntry().

◆ S_IRGRP

#define S_IRGRP   0040

◆ S_IROTH

#define S_IROTH   0004

◆ S_IRUSR

#define S_IRUSR   0400

◆ S_ISDIR

#define S_ISDIR (   x)    ((x) & S_IFDIR) > 0

◆ S_ISLNK

#define S_ISLNK (   x)    ((x) & S_IFLNK) > 0

◆ S_ISREG

#define S_ISREG (   x)    ((x) & 0170000) == S_IFREG

Definition at line 66 of file qzip.cpp.

Referenced by QZipPrivate::fillFileInfo().

◆ S_IWGRP

#define S_IWGRP   0020

◆ S_IWOTH

#define S_IWOTH   0002

◆ S_IWUSR

#define S_IWUSR   0200

◆ S_IXGRP

#define S_IXGRP   0010

◆ S_IXOTH

#define S_IXOTH   0001

◆ S_IXUSR

#define S_IXUSR   0100

◆ ZDEBUG

#define ZDEBUG   if (0) qDebug

Definition at line 90 of file qzip.cpp.

Referenced by QZipWriterPrivate::addEntry(), and QZipReaderPrivate::scanFiles().

Function Documentation

◆ copyUInt()

static void copyUInt ( uchar dest,
const uchar src 
)
inlinestatic

Definition at line 119 of file qzip.cpp.

Referenced by CentralFileHeader::toLocalHeader().

120 {
121  dest[0] = src[0];
122  dest[1] = src[1];
123  dest[2] = src[2];
124  dest[3] = src[3];
125 }

◆ copyUShort()

static void copyUShort ( uchar dest,
const uchar src 
)
inlinestatic

Definition at line 127 of file qzip.cpp.

Referenced by CentralFileHeader::toLocalHeader().

128 {
129  dest[0] = src[0];
130  dest[1] = src[1];
131 }

◆ deflate()

static int deflate ( Bytef *  dest,
ulong destLen,
const Bytef *  source,
ulong  sourceLen 
)
static

Definition at line 224 of file qzip.cpp.

Referenced by QZipWriterPrivate::addEntry(), and QPdfEnginePrivate::writeCompressed().

225 {
226  z_stream stream;
227  int err;
228 
229  stream.next_in = (Bytef*)source;
230  stream.avail_in = (uInt)sourceLen;
231  stream.next_out = dest;
232  stream.avail_out = (uInt)*destLen;
233  if ((uLong)stream.avail_out != *destLen) return Z_BUF_ERROR;
234 
235  stream.zalloc = (alloc_func)0;
236  stream.zfree = (free_func)0;
237  stream.opaque = (voidpf)0;
238 
239  err = deflateInit2(&stream, Z_DEFAULT_COMPRESSION, Z_DEFLATED, -MAX_WBITS, 8, Z_DEFAULT_STRATEGY);
240  if (err != Z_OK) return err;
241 
242  err = deflate(&stream, Z_FINISH);
243  if (err != Z_STREAM_END) {
244  deflateEnd(&stream);
245  return err == Z_OK ? Z_BUF_ERROR : err;
246  }
247  *destLen = stream.total_out;
248 
249  err = deflateEnd(&stream);
250  return err;
251 }
static int deflate(Bytef *dest, ulong *destLen, const Bytef *source, ulong sourceLen)
Definition: qzip.cpp:224
static FILE * stream

◆ inflate()

static int inflate ( Bytef *  dest,
ulong destLen,
const Bytef *  source,
ulong  sourceLen 
)
static

Definition at line 189 of file qzip.cpp.

Referenced by QZipReader::fileData(), QHttpNetworkReplyPrivate::gunzipBodyPartially(), and qt_inflateGZipDataFrom().

190 {
191  z_stream stream;
192  int err;
193 
194  stream.next_in = (Bytef*)source;
195  stream.avail_in = (uInt)sourceLen;
196  if ((uLong)stream.avail_in != sourceLen)
197  return Z_BUF_ERROR;
198 
199  stream.next_out = dest;
200  stream.avail_out = (uInt)*destLen;
201  if ((uLong)stream.avail_out != *destLen)
202  return Z_BUF_ERROR;
203 
204  stream.zalloc = (alloc_func)0;
205  stream.zfree = (free_func)0;
206 
207  err = inflateInit2(&stream, -MAX_WBITS);
208  if (err != Z_OK)
209  return err;
210 
211  err = inflate(&stream, Z_FINISH);
212  if (err != Z_STREAM_END) {
213  inflateEnd(&stream);
214  if (err == Z_NEED_DICT || (err == Z_BUF_ERROR && stream.avail_in == 0))
215  return Z_DATA_ERROR;
216  return err;
217  }
218  *destLen = stream.total_out;
219 
220  err = inflateEnd(&stream);
221  return err;
222 }
static FILE * stream
static int inflate(Bytef *dest, ulong *destLen, const Bytef *source, ulong sourceLen)
Definition: qzip.cpp:189

◆ modeToPermissions()

static QFile::Permissions modeToPermissions ( quint32  mode)
static

Definition at line 253 of file qzip.cpp.

Referenced by QZipPrivate::fillFileInfo().

254 {
255  QFile::Permissions ret;
256  if (mode & S_IRUSR)
257  ret |= QFile::ReadOwner;
258  if (mode & S_IWUSR)
259  ret |= QFile::WriteOwner;
260  if (mode & S_IXUSR)
261  ret |= QFile::ExeOwner;
262  if (mode & S_IRUSR)
263  ret |= QFile::ReadUser;
264  if (mode & S_IWUSR)
265  ret |= QFile::WriteUser;
266  if (mode & S_IXUSR)
267  ret |= QFile::ExeUser;
268  if (mode & S_IRGRP)
269  ret |= QFile::ReadGroup;
270  if (mode & S_IWGRP)
271  ret |= QFile::WriteGroup;
272  if (mode & S_IXGRP)
273  ret |= QFile::ExeGroup;
274  if (mode & S_IROTH)
275  ret |= QFile::ReadOther;
276  if (mode & S_IWOTH)
277  ret |= QFile::WriteOther;
278  if (mode & S_IXOTH)
279  ret |= QFile::ExeOther;
280  return ret;
281 }
#define S_IWOTH
Definition: qzip.cpp:83
#define S_IRGRP
Definition: qzip.cpp:79
#define S_IXGRP
Definition: qzip.cpp:81
#define S_IRUSR
Definition: qzip.cpp:71
#define S_IXUSR
Definition: qzip.cpp:77
#define S_IXOTH
Definition: qzip.cpp:84
#define S_IROTH
Definition: qzip.cpp:82
#define S_IWGRP
Definition: qzip.cpp:80
#define S_IWUSR
Definition: qzip.cpp:74

◆ permissionsToMode()

static quint32 permissionsToMode ( QFile::Permissions  perms)
static

Definition at line 159 of file qzip.cpp.

Referenced by QZipWriterPrivate::addEntry().

160 {
161  quint32 mode = 0;
162  if (perms & QFile::ReadOwner)
163  mode |= S_IRUSR;
164  if (perms & QFile::WriteOwner)
165  mode |= S_IWUSR;
166  if (perms & QFile::ExeOwner)
167  mode |= S_IXUSR;
168  if (perms & QFile::ReadUser)
169  mode |= S_IRUSR;
170  if (perms & QFile::WriteUser)
171  mode |= S_IWUSR;
172  if (perms & QFile::ExeUser)
173  mode |= S_IXUSR;
174  if (perms & QFile::ReadGroup)
175  mode |= S_IRGRP;
176  if (perms & QFile::WriteGroup)
177  mode |= S_IWGRP;
178  if (perms & QFile::ExeGroup)
179  mode |= S_IXGRP;
180  if (perms & QFile::ReadOther)
181  mode |= S_IROTH;
182  if (perms & QFile::WriteOther)
183  mode |= S_IWOTH;
184  if (perms & QFile::ExeOther)
185  mode |= S_IXOTH;
186  return mode;
187 }
#define S_IWOTH
Definition: qzip.cpp:83
#define S_IRGRP
Definition: qzip.cpp:79
#define S_IXGRP
Definition: qzip.cpp:81
#define S_IRUSR
Definition: qzip.cpp:71
#define S_IXUSR
Definition: qzip.cpp:77
#define S_IXOTH
Definition: qzip.cpp:84
#define S_IROTH
Definition: qzip.cpp:82
unsigned int quint32
Definition: qglobal.h:938
#define S_IWGRP
Definition: qzip.cpp:80
#define S_IWUSR
Definition: qzip.cpp:74

◆ readMSDosDate()

static QDateTime readMSDosDate ( const uchar src)
static

Definition at line 283 of file qzip.cpp.

Referenced by QZipPrivate::fillFileInfo().

284 {
285  uint dosDate = readUInt(src);
286  quint64 uDate;
287  uDate = (quint64)(dosDate >> 16);
288  uint tm_mday = (uDate & 0x1f);
289  uint tm_mon = ((uDate & 0x1E0) >> 5);
290  uint tm_year = (((uDate & 0x0FE00) >> 9) + 1980);
291  uint tm_hour = ((dosDate & 0xF800) >> 11);
292  uint tm_min = ((dosDate & 0x7E0) >> 5);
293  uint tm_sec = ((dosDate & 0x1f) << 1);
294 
295  return QDateTime(QDate(tm_year, tm_mon, tm_mday), QTime(tm_hour, tm_min, tm_sec));
296 }
The QDate class provides date functions.
Definition: qdatetime.h:55
static uint readUInt(const uchar *data)
Definition: qzip.cpp:95
The QTime class provides clock time functions.
Definition: qdatetime.h:148
unsigned __int64 quint64
Definition: qglobal.h:943
unsigned int uint
Definition: qglobal.h:996
The QDateTime class provides date and time functions.
Definition: qdatetime.h:216

◆ readUInt()

static uint readUInt ( const uchar data)
inlinestatic

Definition at line 95 of file qzip.cpp.

Referenced by QZipReader::fileData(), QZipPrivate::fillFileInfo(), readMSDosDate(), and QZipReaderPrivate::scanFiles().

96 {
97  return (data[0]) + (data[1]<<8) + (data[2]<<16) + (data[3]<<24);
98 }
static const char * data(const QByteArray &arr)

◆ readUShort()

static ushort readUShort ( const uchar data)
inlinestatic

Definition at line 100 of file qzip.cpp.

Referenced by QZipReader::fileData(), and QZipReaderPrivate::scanFiles().

101 {
102  return (data[0]) + (data[1]<<8);
103 }
static const char * data(const QByteArray &arr)

◆ writeMSDosDate()

static void writeMSDosDate ( uchar dest,
const QDateTime dt 
)
static

Definition at line 133 of file qzip.cpp.

Referenced by QZipWriterPrivate::addEntry().

134 {
135  if (dt.isValid()) {
136  quint16 time =
137  (dt.time().hour() << 11) // 5 bit hour
138  | (dt.time().minute() << 5) // 6 bit minute
139  | (dt.time().second() >> 1); // 5 bit double seconds
140 
141  dest[0] = time & 0xff;
142  dest[1] = time >> 8;
143 
144  quint16 date =
145  ((dt.date().year() - 1980) << 9) // 7 bit year 1980-based
146  | (dt.date().month() << 5) // 4 bit month
147  | (dt.date().day()); // 5 bit day
148 
149  dest[2] = char(date);
150  dest[3] = char(date >> 8);
151  } else {
152  dest[0] = 0;
153  dest[1] = 0;
154  dest[2] = 0;
155  dest[3] = 0;
156  }
157 }
int month() const
Returns the number corresponding to the month of this date, using the following convention: ...
Definition: qdatetime.cpp:382
int day() const
Returns the day of the month (1 to 31) of this date.
Definition: qdatetime.cpp:395
bool isValid() const
Returns true if both the date and the time are valid; otherwise returns false.
Definition: qdatetime.cpp:2346
unsigned short quint16
Definition: qglobal.h:936
int second() const
Returns the second part (0 to 59) of the time.
Definition: qdatetime.cpp:1600
int minute() const
Returns the minute part (0 to 59) of the time.
Definition: qdatetime.cpp:1589
QDate date() const
Returns the date part of the datetime.
Definition: qdatetime.cpp:2357
int year() const
Returns the year of this date.
Definition: qdatetime.cpp:353
QTime time() const
Returns the time part of the datetime.
Definition: qdatetime.cpp:2368
int hour() const
Returns the hour part (0 to 23) of the time.
Definition: qdatetime.cpp:1578

◆ writeUInt()

static void writeUInt ( uchar data,
uint  i 
)
inlinestatic

Definition at line 105 of file qzip.cpp.

Referenced by QZipWriterPrivate::addEntry(), QZipWriter::close(), and CentralFileHeader::toLocalHeader().

106 {
107  data[0] = i & 0xff;
108  data[1] = (i>>8) & 0xff;
109  data[2] = (i>>16) & 0xff;
110  data[3] = (i>>24) & 0xff;
111 }
static const char * data(const QByteArray &arr)

◆ writeUShort()

static void writeUShort ( uchar data,
ushort  i 
)
inlinestatic

Definition at line 113 of file qzip.cpp.

Referenced by QZipWriterPrivate::addEntry(), and QZipWriter::close().

114 {
115  data[0] = i & 0xff;
116  data[1] = (i>>8) & 0xff;
117 }
static const char * data(const QByteArray &arr)