aboutsummaryrefslogtreecommitdiff
path: root/src/common/unzip.h
blob: 55e7bb6eb90abb3659052dbb0c9314f60ebb0043 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
/*
     This file is part of libextractor.
     Copyright (C) 2008, 2012 Christian Grothoff (and other contributing authors)

     libextractor is free software; you can redistribute it and/or modify
     it under the terms of the GNU General Public License as published
     by the Free Software Foundation; either version 3, or (at your
     option) any later version.

     libextractor is distributed in the hope that it will be useful, but
     WITHOUT ANY WARRANTY; without even the implied warranty of
     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     General Public License for more details.

     You should have received a copy of the GNU General Public License
     along with libextractor; see the file COPYING.  If not, write to the
     Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
     Boston, MA 02110-1301, USA.
*/
/**
 * @file common/unzip.h
 * @brief API to access ZIP archives
 * @author Christian Grothoff
 * 
 * This code is based in part on
 * unzip 1.00 Copyright 1998-2003 Gilles Vollant
 * http://www.winimage.com/zLibDll"
 */
#ifndef LE_COMMON_UNZIP_H
#define LE_COMMON_UNZIP_H

#include <zlib.h>

/**
 * Operation was successful.
 */
#define EXTRACTOR_UNZIP_OK                          (0)

/**
 * Cannot move to next file, we are at the end
 */
#define EXTRACTOR_UNZIP_END_OF_LIST_OF_FILE         (-100)

/**
 * IO error, see errno.
 */
#define EXTRACTOR_UNZIP_ERRNO                       (Z_ERRNO)

/**
 * Reached end of the file (NOTE: same as OK!)
 */
#define EXTRACTOR_UNZIP_EOF                         (0)

/**
 * Invalid arguments to call.
 */
#define EXTRACTOR_UNZIP_PARAMERROR                  (-102)

/**
 * Not a zip file (or malformed)
 */
#define EXTRACTOR_UNZIP_BADZIPFILE                  (-103)

/**
 * Internal error.
 */
#define EXTRACTOR_UNZIP_INTERNALERROR               (-104)

/**
 * Checksum failure.
 */
#define EXTRACTOR_UNZIP_CRCERROR                    (-105)

/**
 * Handle for a ZIP archive.
 */
struct EXTRACTOR_UnzipFile;


/**
 * date/time information
 */
struct EXTRACTOR_UnzipDateTimeInfo
{
  /**
   * seconds after the minute - [0,59] 
   */
  uInt tm_sec;            

  /**
   * minutes after the hour - [0,59] 
   */
  uInt tm_min;            
  
  /**
   * hours since midnight - [0,23] 
   */
  uInt tm_hour;           

  /**
   * day of the month - [1,31] 
   */
  uInt tm_mday;           
 
  /**
   * months since January - [0,11] 
   */
  uInt tm_mon;            

  /**
   * years - [1980..2044] 
   */
  uInt tm_year;           
};


/**
 * Information about a file in the zipfile 
 */
struct EXTRACTOR_UnzipFileInfo
{
  /**
   * version made by                 2 bytes 
   */
  uLong version;              

  /**
   * version needed to extract       2 bytes 
   */
  uLong version_needed;       

  /**
   * general purpose bit flag        2 bytes 
   */
  uLong flag;                 

  /**
   * compression method              2 bytes 
   */
  uLong compression_method;   

  /**
   * last mod file date in Dos fmt   4 bytes 
   */
  uLong dosDate;              

  /**
   * crc-32                          4 bytes 
   */
  uLong crc;                  

  /**
   * compressed size                 4 bytes
   */
  uLong compressed_size;      

  /**
   * uncompressed size               4 bytes 
   */
  uLong uncompressed_size;    

  /**
   * filename length                 2 bytes 
   */
  uLong size_filename;        

  /**
   * extra field length              2 bytes 
   */
  uLong size_file_extra;      

  /**
   * file comment length             2 bytes 
   */
  uLong size_file_comment;    
  
  /**
   * disk number start               2 bytes 
   */
  uLong disk_num_start;       

  /**
   * internal file attributes        2 bytes 
   */
  uLong internal_fa;          

  /**
   * external file attributes        4 bytes 
   */
  uLong external_fa;          
  
  /**
   * Time and date of last modification.
   */
  struct EXTRACTOR_UnzipDateTimeInfo tmu_date;
};


/**
 * Open a zip file for processing using the data access
 * functions from the extract context.
 *
 * @param ec extract context to use
 * @return handle to zip data, NULL on error
 */
struct EXTRACTOR_UnzipFile * 
EXTRACTOR_common_unzip_open (struct EXTRACTOR_ExtractContext *ec);


/**
 * Obtain the global comment from a ZIP file.
 *
 * @param file unzip file to inspect
 * @param comment where to copy the comment
 * @param comment_len maximum number of bytes available in comment
 * @return EXTRACTOR_UNZIP_OK on success
 */
int
EXTRACTOR_common_unzip_get_global_comment (struct EXTRACTOR_UnzipFile *file,
					   char *comment,
					   size_t comment_len);


/**
 * Close a ZipFile.
 *
 * @param file zip file to close
 * @return EXTRACTOR_UNZIP_OK if there is no problem. 
 */
int
EXTRACTOR_common_unzip_close (struct EXTRACTOR_UnzipFile *file);


/**
 * Set the current file of the zipfile to the first file.
 *
 * @param file zipfile to manipulate
 * @return UNZ_OK if there is no problem
 */
int 
EXTRACTOR_common_unzip_go_to_first_file (struct EXTRACTOR_UnzipFile *file);


/**
 * Set the current file of the zipfile to the next file.
 *
 * @param file zipfile to manipulate
 * @return EXTRACTOR_UNZIP_OK if there is no problem,
 *         EXTRACTOR_UNZIP_END_OF_LIST_OF_FILE if the actual file was the latest.
 */
int
EXTRACTOR_common_unzip_go_to_next_file (struct EXTRACTOR_UnzipFile *file);


/**
 * Try locate the file szFileName in the zipfile.
 * 
 * @param file zipfile to manipulate
 * @param szFileName name to find
 * @param iCaseSensitivity, use 1 for case sensitivity (like strcmp);
 *        2 for no case sensitivity (like strcmpi or strcasecmp); or
 *        0 for defaut of your operating system (like 1 on Unix, 2 on Windows)
 * @return EXTRACTOR_UNZIP_OK if the file is found. It becomes the current file.
 *         EXTRACTOR_UNZIP_END_OF_LIST_OF_FILE if the file is not found
 */
int
EXTRACTOR_common_unzip_go_find_local_file (struct EXTRACTOR_UnzipFile *file, 
					   const char *szFileName,
					   int iCaseSensitivity);


/**
 * Write info about the ZipFile in the *pglobal_info structure.
 * No preparation of the structure is needed.
 *
 * @param file zipfile to manipulate
 * @param pfile_info file information to initialize
 * @param szFileName where to write the name of the current file
 * @param fileNameBufferSize number of bytes available in szFileName
 * @param extraField where to write extra data
 * @param extraFieldBufferSize number of bytes available in extraField 
 * @param szComment where to write the comment on the current file
 * @param commentBufferSize number of bytes available in szComment
 * @return EXTRACTOR_UNZIP_OK if there is no problem.
 */
int 
EXTRACTOR_common_unzip_get_current_file_info (struct EXTRACTOR_UnzipFile *file,
					      struct EXTRACTOR_UnzipFileInfo *pfile_info, 
					      char *szFileName,
					      uLong fileNameBufferSize,
					      void *extraField, 
					      uLong extraFieldBufferSize, 
					      char *szComment,
					      uLong commentBufferSize);


/**
 * Open for reading data the current file in the zipfile.
 *
 * @param file zipfile to manipulate
 * @return EXTRACTOR_UNZIP_OK on success
 */
int
EXTRACTOR_common_unzip_open_current_file (struct EXTRACTOR_UnzipFile *file);


/**
 * Read bytes from the current file (must have been opened).
 *
 * @param buf contain buffer where data must be copied
 * @param len the size of buf.
 * @return the number of byte copied if somes bytes are copied
 *         0 if the end of file was reached
 *         <0 with error code if there is an error
 *        (EXTRACTOR_UNZIP_ERRNO for IO error, or zLib error for uncompress error)
 */
ssize_t 
EXTRACTOR_common_unzip_read_current_file (struct EXTRACTOR_UnzipFile *file,
					  void *buf,
					  size_t len);


/**
 * Close the file in zip opened with EXTRACTOR_common_unzip_open_current_file.
 *
 * @return EXTRACTOR_UNZIP_CRCERROR if all the file was read but the CRC is not good
 */
int 
EXTRACTOR_common_unzip_close_current_file (struct EXTRACTOR_UnzipFile *file);


#endif 
/* LE_COMMON_UNZIP_H */