Robert Bieber | 7c52284 | 2010-07-21 07:45:29 +0000 | [diff] [blame] | 1 | /* ioapi.c -- IO base function header for compress/uncompress .zip |
| 2 | files using zlib + zip or unzip API |
| 3 | |
| 4 | Version 1.01e, February 12th, 2005 |
| 5 | |
| 6 | Copyright (C) 1998-2005 Gilles Vollant |
| 7 | */ |
| 8 | |
| 9 | #include <stdio.h> |
| 10 | #include <stdlib.h> |
| 11 | #include <string.h> |
| 12 | |
| 13 | #include "zlib.h" |
| 14 | #include "ioapi.h" |
| 15 | |
| 16 | |
| 17 | |
| 18 | /* I've found an old Unix (a SunOS 4.1.3_U1) without all SEEK_* defined.... */ |
| 19 | |
| 20 | #ifndef SEEK_CUR |
| 21 | #define SEEK_CUR 1 |
| 22 | #endif |
| 23 | |
| 24 | #ifndef SEEK_END |
| 25 | #define SEEK_END 2 |
| 26 | #endif |
| 27 | |
| 28 | #ifndef SEEK_SET |
| 29 | #define SEEK_SET 0 |
| 30 | #endif |
| 31 | |
| 32 | voidpf ZCALLBACK fopen_file_func OF(( |
| 33 | voidpf opaque, |
| 34 | const char* filename, |
| 35 | int mode)); |
| 36 | |
| 37 | uLong ZCALLBACK fread_file_func OF(( |
| 38 | voidpf opaque, |
| 39 | voidpf stream, |
| 40 | void* buf, |
| 41 | uLong size)); |
| 42 | |
| 43 | uLong ZCALLBACK fwrite_file_func OF(( |
| 44 | voidpf opaque, |
| 45 | voidpf stream, |
| 46 | const void* buf, |
| 47 | uLong size)); |
| 48 | |
| 49 | long ZCALLBACK ftell_file_func OF(( |
| 50 | voidpf opaque, |
| 51 | voidpf stream)); |
| 52 | |
| 53 | long ZCALLBACK fseek_file_func OF(( |
| 54 | voidpf opaque, |
| 55 | voidpf stream, |
| 56 | uLong offset, |
| 57 | int origin)); |
| 58 | |
| 59 | int ZCALLBACK fclose_file_func OF(( |
| 60 | voidpf opaque, |
| 61 | voidpf stream)); |
| 62 | |
| 63 | int ZCALLBACK ferror_file_func OF(( |
| 64 | voidpf opaque, |
| 65 | voidpf stream)); |
| 66 | |
| 67 | |
| 68 | voidpf ZCALLBACK fopen_file_func (opaque, filename, mode) |
| 69 | voidpf opaque; |
| 70 | const char* filename; |
| 71 | int mode; |
| 72 | { |
| 73 | (void) opaque; /* avoid "unused parameter" warning */ |
| 74 | FILE* file = NULL; |
| 75 | const char* mode_fopen = NULL; |
| 76 | if ((mode & ZLIB_FILEFUNC_MODE_READWRITEFILTER)==ZLIB_FILEFUNC_MODE_READ) |
| 77 | mode_fopen = "rb"; |
| 78 | else |
| 79 | if (mode & ZLIB_FILEFUNC_MODE_EXISTING) |
| 80 | mode_fopen = "r+b"; |
| 81 | else |
| 82 | if (mode & ZLIB_FILEFUNC_MODE_CREATE) |
| 83 | mode_fopen = "wb"; |
| 84 | |
| 85 | if ((filename!=NULL) && (mode_fopen != NULL)) |
| 86 | file = fopen(filename, mode_fopen); |
| 87 | return file; |
| 88 | } |
| 89 | |
| 90 | |
| 91 | uLong ZCALLBACK fread_file_func (opaque, stream, buf, size) |
| 92 | voidpf opaque; |
| 93 | voidpf stream; |
| 94 | void* buf; |
| 95 | uLong size; |
| 96 | { |
| 97 | (void) opaque; /* avoid "unused parameter" warning */ |
| 98 | uLong ret; |
| 99 | ret = (uLong)fread(buf, 1, (size_t)size, (FILE *)stream); |
| 100 | return ret; |
| 101 | } |
| 102 | |
| 103 | |
| 104 | uLong ZCALLBACK fwrite_file_func (opaque, stream, buf, size) |
| 105 | voidpf opaque; |
| 106 | voidpf stream; |
| 107 | const void* buf; |
| 108 | uLong size; |
| 109 | { |
| 110 | (void) opaque; /* avoid "unused parameter" warning */ |
| 111 | uLong ret; |
| 112 | ret = (uLong)fwrite(buf, 1, (size_t)size, (FILE *)stream); |
| 113 | return ret; |
| 114 | } |
| 115 | |
| 116 | long ZCALLBACK ftell_file_func (opaque, stream) |
| 117 | voidpf opaque; |
| 118 | voidpf stream; |
| 119 | { |
| 120 | (void) opaque; /* avoid "unused parameter" warning */ |
| 121 | long ret; |
| 122 | ret = ftell((FILE *)stream); |
| 123 | return ret; |
| 124 | } |
| 125 | |
| 126 | long ZCALLBACK fseek_file_func (opaque, stream, offset, origin) |
| 127 | voidpf opaque; |
| 128 | voidpf stream; |
| 129 | uLong offset; |
| 130 | int origin; |
| 131 | { |
| 132 | (void) opaque; /* avoid "unused parameter" warning */ |
| 133 | int fseek_origin=0; |
| 134 | long ret; |
| 135 | switch (origin) |
| 136 | { |
| 137 | case ZLIB_FILEFUNC_SEEK_CUR : |
| 138 | fseek_origin = SEEK_CUR; |
| 139 | break; |
| 140 | case ZLIB_FILEFUNC_SEEK_END : |
| 141 | fseek_origin = SEEK_END; |
| 142 | break; |
| 143 | case ZLIB_FILEFUNC_SEEK_SET : |
| 144 | fseek_origin = SEEK_SET; |
| 145 | break; |
| 146 | default: return -1; |
| 147 | } |
| 148 | ret = 0; |
| 149 | fseek((FILE *)stream, offset, fseek_origin); |
| 150 | return ret; |
| 151 | } |
| 152 | |
| 153 | int ZCALLBACK fclose_file_func (opaque, stream) |
| 154 | voidpf opaque; |
| 155 | voidpf stream; |
| 156 | { |
| 157 | (void) opaque; /* avoid "unused parameter" warning */ |
| 158 | int ret; |
| 159 | ret = fclose((FILE *)stream); |
| 160 | return ret; |
| 161 | } |
| 162 | |
| 163 | int ZCALLBACK ferror_file_func (opaque, stream) |
| 164 | voidpf opaque; |
| 165 | voidpf stream; |
| 166 | { |
| 167 | (void) opaque; /* avoid "unused parameter" warning */ |
| 168 | int ret; |
| 169 | ret = ferror((FILE *)stream); |
| 170 | return ret; |
| 171 | } |
| 172 | |
| 173 | void fill_fopen_filefunc (pzlib_filefunc_def) |
| 174 | zlib_filefunc_def* pzlib_filefunc_def; |
| 175 | { |
| 176 | pzlib_filefunc_def->zopen_file = fopen_file_func; |
| 177 | pzlib_filefunc_def->zread_file = fread_file_func; |
| 178 | pzlib_filefunc_def->zwrite_file = fwrite_file_func; |
| 179 | pzlib_filefunc_def->ztell_file = ftell_file_func; |
| 180 | pzlib_filefunc_def->zseek_file = fseek_file_func; |
| 181 | pzlib_filefunc_def->zclose_file = fclose_file_func; |
| 182 | pzlib_filefunc_def->zerror_file = ferror_file_func; |
| 183 | pzlib_filefunc_def->opaque = NULL; |
| 184 | } |