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