1/*************************************************************************
2*
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2000, 2010 Oracle and/or its affiliates.
6 *
7 * OpenOffice.org - a multi-platform office productivity suite
8 *
9 * This file is part of OpenOffice.org.
10 *
11 * OpenOffice.org is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License version 3
13 * only, as published by the Free Software Foundation.
14 *
15 * OpenOffice.org is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 * GNU Lesser General Public License version 3 for more details
19 * (a copy is included in the LICENSE file that accompanied this code).
20 *
21 * You should have received a copy of the GNU Lesser General Public License
22 * version 3 along with OpenOffice.org.  If not, see
23 * <http://www.openoffice.org/license.html>
24 * for a copy of the LGPLv3 License.
25 *
26*************************************************************************/
27
28/* ioapi.c -- IO base function header for compress/uncompress .zip
29   files using zlib + zip or unzip API
30
31   Version 1.01e, February 12th, 2005
32
33   Copyright (C) 1998-2005 Gilles Vollant
34*/
35
36#include <stdio.h>
37#include <stdlib.h>
38#include <string.h>
39
40#include <zlib.h>
41#include "ioapi.h"
42
43
44
45/* I've found an old Unix (a SunOS 4.1.3_U1) without all SEEK_* defined.... */
46
47#ifndef SEEK_CUR
48#define SEEK_CUR    1
49#endif
50
51#ifndef SEEK_END
52#define SEEK_END    2
53#endif
54
55#ifndef SEEK_SET
56#define SEEK_SET    0
57#endif
58
59voidpf ZCALLBACK fopen_file_func OF((
60   voidpf opaque,
61   const char* filename,
62   int mode));
63
64uLong ZCALLBACK fread_file_func OF((
65   voidpf opaque,
66   voidpf stream,
67   void* buf,
68   uLong size));
69
70uLong ZCALLBACK fwrite_file_func OF((
71   voidpf opaque,
72   voidpf stream,
73   const void* buf,
74   uLong size));
75
76long ZCALLBACK ftell_file_func OF((
77   voidpf opaque,
78   voidpf stream));
79
80long ZCALLBACK fseek_file_func OF((
81   voidpf opaque,
82   voidpf stream,
83   uLong offset,
84   int origin));
85
86int ZCALLBACK fclose_file_func OF((
87   voidpf opaque,
88   voidpf stream));
89
90int ZCALLBACK ferror_file_func OF((
91   voidpf opaque,
92   voidpf stream));
93
94
95voidpf ZCALLBACK fopen_file_func (opaque, filename, mode)
96   voidpf opaque;
97   const char* filename;
98   int mode;
99{
100    FILE* file = NULL;
101    const char* mode_fopen = NULL;
102    if ((mode & ZLIB_FILEFUNC_MODE_READWRITEFILTER)==ZLIB_FILEFUNC_MODE_READ)
103        mode_fopen = "rb";
104    else
105    if (mode & ZLIB_FILEFUNC_MODE_EXISTING)
106        mode_fopen = "r+b";
107    else
108    if (mode & ZLIB_FILEFUNC_MODE_CREATE)
109        mode_fopen = "wb";
110
111    if ((filename!=NULL) && (mode_fopen != NULL))
112        file = fopen(filename, mode_fopen);
113    return file;
114}
115
116
117uLong ZCALLBACK fread_file_func (opaque, stream, buf, size)
118   voidpf opaque;
119   voidpf stream;
120   void* buf;
121   uLong size;
122{
123    uLong ret;
124    ret = (uLong)fread(buf, 1, (size_t)size, (FILE *)stream);
125    return ret;
126}
127
128
129uLong ZCALLBACK fwrite_file_func (opaque, stream, buf, size)
130   voidpf opaque;
131   voidpf stream;
132   const void* buf;
133   uLong size;
134{
135    uLong ret;
136    ret = (uLong)fwrite(buf, 1, (size_t)size, (FILE *)stream);
137    return ret;
138}
139
140long ZCALLBACK ftell_file_func (opaque, stream)
141   voidpf opaque;
142   voidpf stream;
143{
144    long ret;
145    ret = ftell((FILE *)stream);
146    return ret;
147}
148
149long ZCALLBACK fseek_file_func (opaque, stream, offset, origin)
150   voidpf opaque;
151   voidpf stream;
152   uLong offset;
153   int origin;
154{
155    int fseek_origin=0;
156    long ret;
157    switch (origin)
158    {
159    case ZLIB_FILEFUNC_SEEK_CUR :
160        fseek_origin = SEEK_CUR;
161        break;
162    case ZLIB_FILEFUNC_SEEK_END :
163        fseek_origin = SEEK_END;
164        break;
165    case ZLIB_FILEFUNC_SEEK_SET :
166        fseek_origin = SEEK_SET;
167        break;
168    default: return -1;
169    }
170    ret = 0;
171    fseek((FILE *)stream, offset, fseek_origin);
172    return ret;
173}
174
175int ZCALLBACK fclose_file_func (opaque, stream)
176   voidpf opaque;
177   voidpf stream;
178{
179    int ret;
180    ret = fclose((FILE *)stream);
181    return ret;
182}
183
184int ZCALLBACK ferror_file_func (opaque, stream)
185   voidpf opaque;
186   voidpf stream;
187{
188    int ret;
189    ret = ferror((FILE *)stream);
190    return ret;
191}
192
193void fill_fopen_filefunc (pzlib_filefunc_def)
194  zlib_filefunc_def* pzlib_filefunc_def;
195{
196    pzlib_filefunc_def->zopen_file = fopen_file_func;
197    pzlib_filefunc_def->zread_file = fread_file_func;
198    pzlib_filefunc_def->zwrite_file = fwrite_file_func;
199    pzlib_filefunc_def->ztell_file = ftell_file_func;
200    pzlib_filefunc_def->zseek_file = fseek_file_func;
201    pzlib_filefunc_def->zclose_file = fclose_file_func;
202    pzlib_filefunc_def->zerror_file = ferror_file_func;
203    pzlib_filefunc_def->opaque = NULL;
204}
205