1/************************************************************** 2 * 3 * Licensed to the Apache Software Foundation (ASF) under one 4 * or more contributor license agreements. See the NOTICE file 5 * distributed with this work for additional information 6 * regarding copyright ownership. The ASF licenses this file 7 * to you under the Apache License, Version 2.0 (the 8 * "License"); you may not use this file except in compliance 9 * with the License. You may obtain a copy of the License at 10 * 11 * http://www.apache.org/licenses/LICENSE-2.0 12 * 13 * Unless required by applicable law or agreed to in writing, 14 * software distributed under the License is distributed on an 15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16 * KIND, either express or implied. See the License for the 17 * specific language governing permissions and limitations 18 * under the License. 19 * 20 *************************************************************/ 21 22 23 24/* ioapi.c -- IO base function header for compress/uncompress .zip 25 files using zlib + zip or unzip API 26 27 Version 1.01e, February 12th, 2005 28 29 Copyright (C) 1998-2005 Gilles Vollant 30*/ 31 32#include <stdio.h> 33#include <stdlib.h> 34#include <string.h> 35 36#include <zlib.h> 37#include "ioapi.h" 38 39 40 41/* I've found an old Unix (a SunOS 4.1.3_U1) without all SEEK_* defined.... */ 42 43#ifndef SEEK_CUR 44#define SEEK_CUR 1 45#endif 46 47#ifndef SEEK_END 48#define SEEK_END 2 49#endif 50 51#ifndef SEEK_SET 52#define SEEK_SET 0 53#endif 54 55voidpf ZCALLBACK fopen_file_func OF(( 56 voidpf opaque, 57 const char* filename, 58 int mode)); 59 60uLong ZCALLBACK fread_file_func OF(( 61 voidpf opaque, 62 voidpf stream, 63 void* buf, 64 uLong size)); 65 66uLong ZCALLBACK fwrite_file_func OF(( 67 voidpf opaque, 68 voidpf stream, 69 const void* buf, 70 uLong size)); 71 72long ZCALLBACK ftell_file_func OF(( 73 voidpf opaque, 74 voidpf stream)); 75 76long ZCALLBACK fseek_file_func OF(( 77 voidpf opaque, 78 voidpf stream, 79 uLong offset, 80 int origin)); 81 82int ZCALLBACK fclose_file_func OF(( 83 voidpf opaque, 84 voidpf stream)); 85 86int ZCALLBACK ferror_file_func OF(( 87 voidpf opaque, 88 voidpf stream)); 89 90 91voidpf ZCALLBACK fopen_file_func (opaque, filename, mode) 92 voidpf opaque; 93 const char* filename; 94 int mode; 95{ 96 FILE* file = NULL; 97 const char* mode_fopen = NULL; 98 if ((mode & ZLIB_FILEFUNC_MODE_READWRITEFILTER)==ZLIB_FILEFUNC_MODE_READ) 99 mode_fopen = "rb"; 100 else 101 if (mode & ZLIB_FILEFUNC_MODE_EXISTING) 102 mode_fopen = "r+b"; 103 else 104 if (mode & ZLIB_FILEFUNC_MODE_CREATE) 105 mode_fopen = "wb"; 106 107 if ((filename!=NULL) && (mode_fopen != NULL)) 108 file = fopen(filename, mode_fopen); 109 return file; 110} 111 112 113uLong ZCALLBACK fread_file_func (opaque, stream, buf, size) 114 voidpf opaque; 115 voidpf stream; 116 void* buf; 117 uLong size; 118{ 119 uLong ret; 120 ret = (uLong)fread(buf, 1, (size_t)size, (FILE *)stream); 121 return ret; 122} 123 124 125uLong ZCALLBACK fwrite_file_func (opaque, stream, buf, size) 126 voidpf opaque; 127 voidpf stream; 128 const void* buf; 129 uLong size; 130{ 131 uLong ret; 132 ret = (uLong)fwrite(buf, 1, (size_t)size, (FILE *)stream); 133 return ret; 134} 135 136long ZCALLBACK ftell_file_func (opaque, stream) 137 voidpf opaque; 138 voidpf stream; 139{ 140 long ret; 141 ret = ftell((FILE *)stream); 142 return ret; 143} 144 145long ZCALLBACK fseek_file_func (opaque, stream, offset, origin) 146 voidpf opaque; 147 voidpf stream; 148 uLong offset; 149 int origin; 150{ 151 int fseek_origin=0; 152 long ret; 153 switch (origin) 154 { 155 case ZLIB_FILEFUNC_SEEK_CUR : 156 fseek_origin = SEEK_CUR; 157 break; 158 case ZLIB_FILEFUNC_SEEK_END : 159 fseek_origin = SEEK_END; 160 break; 161 case ZLIB_FILEFUNC_SEEK_SET : 162 fseek_origin = SEEK_SET; 163 break; 164 default: return -1; 165 } 166 ret = 0; 167 fseek((FILE *)stream, offset, fseek_origin); 168 return ret; 169} 170 171int ZCALLBACK fclose_file_func (opaque, stream) 172 voidpf opaque; 173 voidpf stream; 174{ 175 int ret; 176 ret = fclose((FILE *)stream); 177 return ret; 178} 179 180int ZCALLBACK ferror_file_func (opaque, stream) 181 voidpf opaque; 182 voidpf stream; 183{ 184 int ret; 185 ret = ferror((FILE *)stream); 186 return ret; 187} 188 189void fill_fopen_filefunc (pzlib_filefunc_def) 190 zlib_filefunc_def* pzlib_filefunc_def; 191{ 192 pzlib_filefunc_def->zopen_file = fopen_file_func; 193 pzlib_filefunc_def->zread_file = fread_file_func; 194 pzlib_filefunc_def->zwrite_file = fwrite_file_func; 195 pzlib_filefunc_def->ztell_file = ftell_file_func; 196 pzlib_filefunc_def->zseek_file = fseek_file_func; 197 pzlib_filefunc_def->zclose_file = fclose_file_func; 198 pzlib_filefunc_def->zerror_file = ferror_file_func; 199 pzlib_filefunc_def->opaque = NULL; 200} 201