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