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