misc/quazip/qioapi.cpp
author nemo
Sat, 17 Nov 2012 09:03:47 -0500
changeset 8051 f26422ef0333
parent 7889 57b117d441b9
permissions -rw-r--r--
oft-requested, should make the shoppa guys happy, probably, but, knowing them, I'm sure someone will complain
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
7889
57b117d441b9 consistent line endings
nemo
parents: 5752
diff changeset
     1
/* ioapi.c -- IO base function header for compress/uncompress .zip
57b117d441b9 consistent line endings
nemo
parents: 5752
diff changeset
     2
   files using zlib + zip or unzip API
57b117d441b9 consistent line endings
nemo
parents: 5752
diff changeset
     3
57b117d441b9 consistent line endings
nemo
parents: 5752
diff changeset
     4
   Version 1.01e, February 12th, 2005
57b117d441b9 consistent line endings
nemo
parents: 5752
diff changeset
     5
57b117d441b9 consistent line endings
nemo
parents: 5752
diff changeset
     6
   Copyright (C) 1998-2005 Gilles Vollant
57b117d441b9 consistent line endings
nemo
parents: 5752
diff changeset
     7
57b117d441b9 consistent line endings
nemo
parents: 5752
diff changeset
     8
   Modified by Sergey A. Tachenov to integrate with Qt.
57b117d441b9 consistent line endings
nemo
parents: 5752
diff changeset
     9
*/
57b117d441b9 consistent line endings
nemo
parents: 5752
diff changeset
    10
57b117d441b9 consistent line endings
nemo
parents: 5752
diff changeset
    11
#include <stdio.h>
57b117d441b9 consistent line endings
nemo
parents: 5752
diff changeset
    12
#include <stdlib.h>
57b117d441b9 consistent line endings
nemo
parents: 5752
diff changeset
    13
#include <string.h>
57b117d441b9 consistent line endings
nemo
parents: 5752
diff changeset
    14
57b117d441b9 consistent line endings
nemo
parents: 5752
diff changeset
    15
#include "zlib.h"
57b117d441b9 consistent line endings
nemo
parents: 5752
diff changeset
    16
#include "ioapi.h"
57b117d441b9 consistent line endings
nemo
parents: 5752
diff changeset
    17
#include "quazip_global.h"
57b117d441b9 consistent line endings
nemo
parents: 5752
diff changeset
    18
#include <QIODevice>
57b117d441b9 consistent line endings
nemo
parents: 5752
diff changeset
    19
57b117d441b9 consistent line endings
nemo
parents: 5752
diff changeset
    20
57b117d441b9 consistent line endings
nemo
parents: 5752
diff changeset
    21
/* I've found an old Unix (a SunOS 4.1.3_U1) without all SEEK_* defined.... */
57b117d441b9 consistent line endings
nemo
parents: 5752
diff changeset
    22
57b117d441b9 consistent line endings
nemo
parents: 5752
diff changeset
    23
#ifndef SEEK_CUR
57b117d441b9 consistent line endings
nemo
parents: 5752
diff changeset
    24
#define SEEK_CUR    1
57b117d441b9 consistent line endings
nemo
parents: 5752
diff changeset
    25
#endif
57b117d441b9 consistent line endings
nemo
parents: 5752
diff changeset
    26
57b117d441b9 consistent line endings
nemo
parents: 5752
diff changeset
    27
#ifndef SEEK_END
57b117d441b9 consistent line endings
nemo
parents: 5752
diff changeset
    28
#define SEEK_END    2
57b117d441b9 consistent line endings
nemo
parents: 5752
diff changeset
    29
#endif
57b117d441b9 consistent line endings
nemo
parents: 5752
diff changeset
    30
57b117d441b9 consistent line endings
nemo
parents: 5752
diff changeset
    31
#ifndef SEEK_SET
57b117d441b9 consistent line endings
nemo
parents: 5752
diff changeset
    32
#define SEEK_SET    0
57b117d441b9 consistent line endings
nemo
parents: 5752
diff changeset
    33
#endif
57b117d441b9 consistent line endings
nemo
parents: 5752
diff changeset
    34
57b117d441b9 consistent line endings
nemo
parents: 5752
diff changeset
    35
voidpf ZCALLBACK qiodevice_open_file_func (
57b117d441b9 consistent line endings
nemo
parents: 5752
diff changeset
    36
   voidpf opaque UNUSED,
57b117d441b9 consistent line endings
nemo
parents: 5752
diff changeset
    37
   voidpf file,
57b117d441b9 consistent line endings
nemo
parents: 5752
diff changeset
    38
   int mode)
57b117d441b9 consistent line endings
nemo
parents: 5752
diff changeset
    39
{
57b117d441b9 consistent line endings
nemo
parents: 5752
diff changeset
    40
    QIODevice *iodevice = reinterpret_cast<QIODevice*>(file);
57b117d441b9 consistent line endings
nemo
parents: 5752
diff changeset
    41
    if(iodevice->isSequential())
57b117d441b9 consistent line endings
nemo
parents: 5752
diff changeset
    42
        return NULL;
57b117d441b9 consistent line endings
nemo
parents: 5752
diff changeset
    43
    if ((mode & ZLIB_FILEFUNC_MODE_READWRITEFILTER)==ZLIB_FILEFUNC_MODE_READ)
57b117d441b9 consistent line endings
nemo
parents: 5752
diff changeset
    44
        iodevice->open(QIODevice::ReadOnly);
57b117d441b9 consistent line endings
nemo
parents: 5752
diff changeset
    45
    else
57b117d441b9 consistent line endings
nemo
parents: 5752
diff changeset
    46
    if (mode & ZLIB_FILEFUNC_MODE_EXISTING)
57b117d441b9 consistent line endings
nemo
parents: 5752
diff changeset
    47
        iodevice->open(QIODevice::ReadWrite);
57b117d441b9 consistent line endings
nemo
parents: 5752
diff changeset
    48
    else
57b117d441b9 consistent line endings
nemo
parents: 5752
diff changeset
    49
    if (mode & ZLIB_FILEFUNC_MODE_CREATE)
57b117d441b9 consistent line endings
nemo
parents: 5752
diff changeset
    50
        iodevice->open(QIODevice::WriteOnly);
57b117d441b9 consistent line endings
nemo
parents: 5752
diff changeset
    51
57b117d441b9 consistent line endings
nemo
parents: 5752
diff changeset
    52
    if(iodevice->isOpen())
57b117d441b9 consistent line endings
nemo
parents: 5752
diff changeset
    53
        return iodevice;
57b117d441b9 consistent line endings
nemo
parents: 5752
diff changeset
    54
    else
57b117d441b9 consistent line endings
nemo
parents: 5752
diff changeset
    55
        return NULL;
57b117d441b9 consistent line endings
nemo
parents: 5752
diff changeset
    56
}
57b117d441b9 consistent line endings
nemo
parents: 5752
diff changeset
    57
57b117d441b9 consistent line endings
nemo
parents: 5752
diff changeset
    58
57b117d441b9 consistent line endings
nemo
parents: 5752
diff changeset
    59
uLong ZCALLBACK qiodevice_read_file_func (
57b117d441b9 consistent line endings
nemo
parents: 5752
diff changeset
    60
   voidpf opaque UNUSED,
57b117d441b9 consistent line endings
nemo
parents: 5752
diff changeset
    61
   voidpf stream,
57b117d441b9 consistent line endings
nemo
parents: 5752
diff changeset
    62
   void* buf,
57b117d441b9 consistent line endings
nemo
parents: 5752
diff changeset
    63
   uLong size)
57b117d441b9 consistent line endings
nemo
parents: 5752
diff changeset
    64
{
57b117d441b9 consistent line endings
nemo
parents: 5752
diff changeset
    65
    uLong ret;
57b117d441b9 consistent line endings
nemo
parents: 5752
diff changeset
    66
    ret = (uLong)((QIODevice*)stream)->read((char*)buf,size);
57b117d441b9 consistent line endings
nemo
parents: 5752
diff changeset
    67
    return ret;
57b117d441b9 consistent line endings
nemo
parents: 5752
diff changeset
    68
}
57b117d441b9 consistent line endings
nemo
parents: 5752
diff changeset
    69
57b117d441b9 consistent line endings
nemo
parents: 5752
diff changeset
    70
57b117d441b9 consistent line endings
nemo
parents: 5752
diff changeset
    71
uLong ZCALLBACK qiodevice_write_file_func (
57b117d441b9 consistent line endings
nemo
parents: 5752
diff changeset
    72
   voidpf opaque UNUSED,
57b117d441b9 consistent line endings
nemo
parents: 5752
diff changeset
    73
   voidpf stream,
57b117d441b9 consistent line endings
nemo
parents: 5752
diff changeset
    74
   const void* buf,
57b117d441b9 consistent line endings
nemo
parents: 5752
diff changeset
    75
   uLong size)
57b117d441b9 consistent line endings
nemo
parents: 5752
diff changeset
    76
{
57b117d441b9 consistent line endings
nemo
parents: 5752
diff changeset
    77
    uLong ret;
57b117d441b9 consistent line endings
nemo
parents: 5752
diff changeset
    78
    ret = (uLong)((QIODevice*)stream)->write((char*)buf,size);
57b117d441b9 consistent line endings
nemo
parents: 5752
diff changeset
    79
    return ret;
57b117d441b9 consistent line endings
nemo
parents: 5752
diff changeset
    80
}
57b117d441b9 consistent line endings
nemo
parents: 5752
diff changeset
    81
57b117d441b9 consistent line endings
nemo
parents: 5752
diff changeset
    82
uLong ZCALLBACK qiodevice_tell_file_func (
57b117d441b9 consistent line endings
nemo
parents: 5752
diff changeset
    83
   voidpf opaque UNUSED,
57b117d441b9 consistent line endings
nemo
parents: 5752
diff changeset
    84
   voidpf stream)
57b117d441b9 consistent line endings
nemo
parents: 5752
diff changeset
    85
{
57b117d441b9 consistent line endings
nemo
parents: 5752
diff changeset
    86
    uLong ret;
57b117d441b9 consistent line endings
nemo
parents: 5752
diff changeset
    87
    ret = ((QIODevice*)stream)->pos();
57b117d441b9 consistent line endings
nemo
parents: 5752
diff changeset
    88
    return ret;
57b117d441b9 consistent line endings
nemo
parents: 5752
diff changeset
    89
}
57b117d441b9 consistent line endings
nemo
parents: 5752
diff changeset
    90
57b117d441b9 consistent line endings
nemo
parents: 5752
diff changeset
    91
int ZCALLBACK qiodevice_seek_file_func (
57b117d441b9 consistent line endings
nemo
parents: 5752
diff changeset
    92
   voidpf opaque UNUSED,
57b117d441b9 consistent line endings
nemo
parents: 5752
diff changeset
    93
   voidpf stream,
57b117d441b9 consistent line endings
nemo
parents: 5752
diff changeset
    94
   uLong offset,
57b117d441b9 consistent line endings
nemo
parents: 5752
diff changeset
    95
   int origin)
57b117d441b9 consistent line endings
nemo
parents: 5752
diff changeset
    96
{
57b117d441b9 consistent line endings
nemo
parents: 5752
diff changeset
    97
    uLong qiodevice_seek_result=0;
57b117d441b9 consistent line endings
nemo
parents: 5752
diff changeset
    98
    int ret;
57b117d441b9 consistent line endings
nemo
parents: 5752
diff changeset
    99
    switch (origin)
57b117d441b9 consistent line endings
nemo
parents: 5752
diff changeset
   100
    {
57b117d441b9 consistent line endings
nemo
parents: 5752
diff changeset
   101
    case ZLIB_FILEFUNC_SEEK_CUR :
57b117d441b9 consistent line endings
nemo
parents: 5752
diff changeset
   102
        qiodevice_seek_result = ((QIODevice*)stream)->pos() + offset;
57b117d441b9 consistent line endings
nemo
parents: 5752
diff changeset
   103
        break;
57b117d441b9 consistent line endings
nemo
parents: 5752
diff changeset
   104
    case ZLIB_FILEFUNC_SEEK_END :
57b117d441b9 consistent line endings
nemo
parents: 5752
diff changeset
   105
        qiodevice_seek_result = ((QIODevice*)stream)->size() - offset;
57b117d441b9 consistent line endings
nemo
parents: 5752
diff changeset
   106
        break;
57b117d441b9 consistent line endings
nemo
parents: 5752
diff changeset
   107
    case ZLIB_FILEFUNC_SEEK_SET :
57b117d441b9 consistent line endings
nemo
parents: 5752
diff changeset
   108
        qiodevice_seek_result = offset;
57b117d441b9 consistent line endings
nemo
parents: 5752
diff changeset
   109
        break;
57b117d441b9 consistent line endings
nemo
parents: 5752
diff changeset
   110
    default: return -1;
57b117d441b9 consistent line endings
nemo
parents: 5752
diff changeset
   111
    }
57b117d441b9 consistent line endings
nemo
parents: 5752
diff changeset
   112
    ret = !((QIODevice*)stream)->seek(qiodevice_seek_result);
57b117d441b9 consistent line endings
nemo
parents: 5752
diff changeset
   113
    return ret;
57b117d441b9 consistent line endings
nemo
parents: 5752
diff changeset
   114
}
57b117d441b9 consistent line endings
nemo
parents: 5752
diff changeset
   115
57b117d441b9 consistent line endings
nemo
parents: 5752
diff changeset
   116
int ZCALLBACK qiodevice_close_file_func (
57b117d441b9 consistent line endings
nemo
parents: 5752
diff changeset
   117
   voidpf opaque UNUSED,
57b117d441b9 consistent line endings
nemo
parents: 5752
diff changeset
   118
   voidpf stream)
57b117d441b9 consistent line endings
nemo
parents: 5752
diff changeset
   119
{
57b117d441b9 consistent line endings
nemo
parents: 5752
diff changeset
   120
    ((QIODevice*)stream)->close();
57b117d441b9 consistent line endings
nemo
parents: 5752
diff changeset
   121
    return 0;
57b117d441b9 consistent line endings
nemo
parents: 5752
diff changeset
   122
}
57b117d441b9 consistent line endings
nemo
parents: 5752
diff changeset
   123
57b117d441b9 consistent line endings
nemo
parents: 5752
diff changeset
   124
int ZCALLBACK qiodevice_error_file_func (
57b117d441b9 consistent line endings
nemo
parents: 5752
diff changeset
   125
   voidpf opaque UNUSED,
57b117d441b9 consistent line endings
nemo
parents: 5752
diff changeset
   126
   voidpf stream)
57b117d441b9 consistent line endings
nemo
parents: 5752
diff changeset
   127
{
57b117d441b9 consistent line endings
nemo
parents: 5752
diff changeset
   128
    return !((QIODevice*)stream)->errorString().isEmpty();
57b117d441b9 consistent line endings
nemo
parents: 5752
diff changeset
   129
}
57b117d441b9 consistent line endings
nemo
parents: 5752
diff changeset
   130
57b117d441b9 consistent line endings
nemo
parents: 5752
diff changeset
   131
void fill_qiodevice_filefunc (
57b117d441b9 consistent line endings
nemo
parents: 5752
diff changeset
   132
  zlib_filefunc_def* pzlib_filefunc_def)
57b117d441b9 consistent line endings
nemo
parents: 5752
diff changeset
   133
{
57b117d441b9 consistent line endings
nemo
parents: 5752
diff changeset
   134
    pzlib_filefunc_def->zopen_file = qiodevice_open_file_func;
57b117d441b9 consistent line endings
nemo
parents: 5752
diff changeset
   135
    pzlib_filefunc_def->zread_file = qiodevice_read_file_func;
57b117d441b9 consistent line endings
nemo
parents: 5752
diff changeset
   136
    pzlib_filefunc_def->zwrite_file = qiodevice_write_file_func;
57b117d441b9 consistent line endings
nemo
parents: 5752
diff changeset
   137
    pzlib_filefunc_def->ztell_file = qiodevice_tell_file_func;
57b117d441b9 consistent line endings
nemo
parents: 5752
diff changeset
   138
    pzlib_filefunc_def->zseek_file = qiodevice_seek_file_func;
57b117d441b9 consistent line endings
nemo
parents: 5752
diff changeset
   139
    pzlib_filefunc_def->zclose_file = qiodevice_close_file_func;
57b117d441b9 consistent line endings
nemo
parents: 5752
diff changeset
   140
    pzlib_filefunc_def->zerror_file = qiodevice_error_file_func;
57b117d441b9 consistent line endings
nemo
parents: 5752
diff changeset
   141
    pzlib_filefunc_def->opaque = NULL;
57b117d441b9 consistent line endings
nemo
parents: 5752
diff changeset
   142
}