Barracuda Application Server C/C++ Reference
Native APIs, integration guides, and platform interfaces
ZipFileIterator.h
Go to the documentation of this file.
1/*
2 * ____ _________ __ _
3 * / __ \___ ____ _/ /_ __(_)___ ___ ___ / / ____ ____ _(_)____
4 * / /_/ / _ \/ __ `/ / / / / / __ `__ \/ _ \/ / / __ \/ __ `/ / ___/
5 * / _, _/ __/ /_/ / / / / / / / / / / / __/ /___/ /_/ / /_/ / / /__
6 * /_/ |_|\___/\__,_/_/ /_/ /_/_/ /_/ /_/\___/_____/\____/\__, /_/\___/
7 * /____/
8 *
9 * Barracuda Embedded Web-Server
10 *
11 ****************************************************************************
12 * HEADER
13 *
14 * $Id: ZipFileIterator.h 5978 2026-09-11 16:13:48Z wini $
15 *
16 * COPYRIGHT: Real Time Logic LLC, 2003-2019
17 *
18 * This software is copyrighted by and is the sole property of Real
19 * Time Logic LLC. All rights, title, ownership, or other interests in
20 * the software remain the property of Real Time Logic LLC. This
21 * software may only be used in accordance with the terms and
22 * conditions stipulated in the corresponding license agreement under
23 * which the software has been supplied. Any unauthorized use,
24 * duplication, transmission, distribution, or disclosure of this
25 * software is expressly forbidden.
26 *
27 * This Copyright notice may not be removed or modified without prior
28 * written consent of Real Time Logic LLC.
29 *
30 * Real Time Logic LLC. reserves the right to modify this software
31 * without notice.
32 *
33 * http://www.realtimelogic.com
34 ****************************************************************************
35 *
36 *
37 */
40#ifndef __ZipFileIterator_h
41#define __ZipFileIterator_h
42
43#include <HttpServer.h>
44#include <CspRunTm.h>
45
46#ifndef __DOXYGEN__
47struct ZipContainer;
48struct ZipFileHeaderData;
49#endif
50
51#ifdef _SHARK_DOXY
59#else
63#endif
64
71typedef struct ZipReader
72#ifdef __cplusplus
73: public CspReader
74{
84 ZipReader(CspReader_Read r, U32 zipFileSize);
85#else
86{
87 CspReader super; /* As if inherited. */
88#endif
89 U32 size;
91
92#ifdef __cplusplus
93extern "C" {
94#endif
99 ZipReader* o, CspReader_Read r, U32 zipFileSize);
100#ifdef __cplusplus
101}
103 ZipReader_constructor(this, r, zipFileSize); }
104#endif
105
106
108typedef enum {
109 ZipErr_Buf = -2000,
114 ZipErr_NoError = 0
115} ZipErr;
116
117
119typedef enum {
120 ZipComprMethod_Stored=0,
121 ZipComprMethod_Deflated=8,
122 ZipComprMethod_AES=99 /* Gianluca 9/22 */
124
125
126#ifndef __DOXYGEN__
127
128typedef struct ZipFileHeader
129{
130#ifdef __cplusplus
131 const char* getFn();
132 const char* getEf();
133 const char* getAESef();
134 bool isDirectory();
135 U16 getFnLen();
136 U16 getEfLen();
137 U16 getFcLen();
138 ZipComprMethod getComprMethod();
139 U32 getUncompressedSizeLittleEndian();
140 U32 getCrc32LittleEndian();
141 U32 getCompressedSize();
142 U32 getUncompressedSize();
143 U32 getCrc32();
144 static const char* e2str(ZipErr e);
145 U16 getVersionMade();
146 U16 getFlag();
147 U32 getDataOffset();
148 U32 getTime();
149 CspReader* getReader();
150 private:
151#endif
152 struct ZipFileHeaderData* data;
153 char* fn; /* File name. Not null terminated. */
154 U8* ef; /* Extra field - GV 9/28 */
155 U8* AESef; /* AES extra field - GV 11/30/2010 */
156 ZipReader* reader;
157 U8* buf;
158 U32 bufSize;
159 U32 fileHeaderOffs;
160 ZipComprMethod comprMethod;
161 U16 fnLen;
162 U16 efLen;
163 U16 fcLen;
164} ZipFileHeader;
165
166#ifdef __cplusplus
167extern "C" {
168#endif
169#define ZipFileHeader_getFn(o) (o)->fn
170#define ZipFileHeader_getEf(o) (o)->ef
171#define ZipFileHeader_getAESef(o) (o)->AESef
172#define ZipFileHeader_isDirectory(o) ((o)->fn[(o)->fnLen-1] == '/')
173#define ZipFileHeader_getFnLen(o) (o)->fnLen
174#define ZipFileHeader_getEfLen(o) (o)->efLen
175#define ZipFileHeader_getFcLen(o) (o)->fcLen
176#define ZipFileHeader_getComprMethod(o) (o)->comprMethod
177#define ZipFileHeader_getReader(o) (CspReader*)(o)->reader
178BA_API U32 ZipFileHeader_getUncompressedSizeLittleEndian(ZipFileHeader* o);
179BA_API U32 ZipFileHeader_getCrc32LittleEndian(ZipFileHeader* o);
180BA_API U32 ZipFileHeader_getCompressedSize(ZipFileHeader* o);
181BA_API U32 ZipFileHeader_getUncompressedSize(ZipFileHeader* o);
182BA_API U32 ZipFileHeader_getCrc32(ZipFileHeader* o);
183BA_API const char* ZipFileHeader_e2str(ZipErr e);
184
185/* Return: The operating system that compressed the file. */
186BA_API U16 ZipFileHeader_getVersionMade(ZipFileHeader* o);
187
188/* ZipFileHeader_getFlag
189 Bit 0: If set, indicates that the file is encrypted.
190 Bit 2 Bit 1
191 0 0 Normal (-en) compression option was used.
192 0 1 Maximum (-exx/-ex) compression option was used.
193 1 0 Fast (-ef) compression option was used.
194 1 1 Super Fast (-es) compression option was used.
195*/
196BA_API U16 ZipFileHeader_getFlag(ZipFileHeader* o);
197
198BA_API U32 ZipFileHeader_getDataOffset(ZipFileHeader* o);
199BA_API U32 ZipFileHeader_getTime(ZipFileHeader* o);
200
201#ifdef __cplusplus
202}
203inline const char* ZipFileHeader::getFn() {
204 return ZipFileHeader_getFn(this);
205}
206inline const char* ZipFileHeader::getEf() {
207 return (char*)ZipFileHeader_getEf(this);
208}
209inline const char* ZipFileHeader::getAESef() {
210 return (char*)ZipFileHeader_getAESef(this);
211}
212inline bool ZipFileHeader::isDirectory() {
213 return ZipFileHeader_isDirectory(this) ? true : false;
214}
215inline U16 ZipFileHeader::getFnLen() {
216 return ZipFileHeader_getFnLen(this);
217}
218inline U16 ZipFileHeader::getEfLen() {
219 return ZipFileHeader_getEfLen(this);
220}
221inline U16 ZipFileHeader::getFcLen() {
222 return ZipFileHeader_getFcLen(this);
223}
224inline ZipComprMethod ZipFileHeader::getComprMethod() {
225 return ZipFileHeader_getComprMethod(this);
226}
227inline CspReader* ZipFileHeader::getReader() {
228 return ZipFileHeader_getReader(this);
229}
230inline U32 ZipFileHeader::getUncompressedSizeLittleEndian() {
231 return ZipFileHeader_getUncompressedSizeLittleEndian(this);
232}
233inline U32 ZipFileHeader::getCrc32LittleEndian() {
234 return ZipFileHeader_getCrc32LittleEndian(this);
235}
236inline U32 ZipFileHeader::getCompressedSize() {
237 return ZipFileHeader_getCompressedSize(this);
238}
239inline U32 ZipFileHeader::getUncompressedSize() {
240 return ZipFileHeader_getUncompressedSize(this);
241}
242inline U32 ZipFileHeader::getCrc32() {
243 return ZipFileHeader_getCrc32(this);
244}
245inline const char* ZipFileHeader::e2str(ZipErr e) {
246 return ZipFileHeader_e2str(e);
247}
248inline U16 ZipFileHeader::getVersionMade() {
249 return ZipFileHeader_getVersionMade(this);
250}
251inline U16 ZipFileHeader::getFlag() {
252 return ZipFileHeader_getFlag(this);
253}
254inline U32 ZipFileHeader::getDataOffset() {
255 return ZipFileHeader_getDataOffset(this);
256}
257inline U32 ZipFileHeader::getTime() {
258 return ZipFileHeader_getTime(this);
259}
260#endif
261
262#endif /* __DOXYGEN__ */
263
264
267typedef struct ZipFileInfo
268{
269 U32 crc32;
270 U32 dataOffset;
271 U32 uncompressedSize;
272 U32 compressedSize;
273 U32 time;
274 ZipComprMethod comprMethod;
275 U16 flag;
276 U8 *ef; /* AES encrypted ZIP file info */
277 U8 *AESef; /* AES encrypted ZIP file info */
279
280#if defined(B_LITTLE_ENDIAN)
281#define ZipFileInfo_makeLittleEndianU32(x) x
282#elif defined(B_BIG_ENDIAN)
283#define ZipFileInfo_makeLittleEndianU32(x) \
284 ((U32)((((U32)(x) & 0x000000ffU) << 24) | \
285 (((U32)(x) & 0x0000ff00U) << 8) | \
286 (((U32)(x) & 0x00ff0000U) >> 8) | \
287 (((U32)(x) & 0xff000000U) >> 24)))
288#else
289#error in config/make file, you must define one of B_LITTLE_ENDIAN or B_BIG_ENDIAN
290#endif
291
292#define ZipFileInfo_getCrc32LittleEndian(o) \
293 ZipFileInfo_makeLittleEndianU32((o)->crc32);
294#define ZipFileInfo_getUncompressedSizeLittleEndian(o) \
295 ZipFileInfo_makeLittleEndianU32((o)->uncompressedSize);
296
297
298#define ZipFileInfo_constructor(o, fileHeader, EfPtr) \
299do { \
300 (o)->crc32 = ZipFileHeader_getCrc32(fileHeader); \
301 (o)->dataOffset = ZipFileHeader_getDataOffset(fileHeader); \
302 (o)->uncompressedSize = ZipFileHeader_getUncompressedSize(fileHeader); \
303 (o)->compressedSize = ZipFileHeader_getCompressedSize(fileHeader); \
304 (o)->time = ZipFileHeader_getTime(fileHeader); \
305 (o)->comprMethod = ZipFileHeader_getComprMethod(fileHeader); \
306 (o)->flag = ZipFileHeader_getFlag(fileHeader); \
307 (o)->ef = EfPtr; \
308 (o)->AESef = (EfPtr + (ZipFileHeader_getAESef(fileHeader) - ZipFileHeader_getEf(fileHeader))); \
309}while(0)
310
311
312#ifndef __DOXYGEN__
313typedef struct
314{
315 U32 dataOffset;
316 U32 uncompressedSize;
317 U32 compressedSize;
318 ZipComprMethod comprMethod;
319} MiniZipFileInfo;
320#endif
321
322#define MiniZipFileInfo_constructor(o, fileHeader) \
323do { \
324 (o)->dataOffset = ZipFileHeader_getDataOffset(fileHeader); \
325 (o)->uncompressedSize = ZipFileHeader_getUncompressedSize(fileHeader); \
326 (o)->compressedSize = ZipFileHeader_getCompressedSize(fileHeader); \
327 (o)->comprMethod = ZipFileHeader_getComprMethod(fileHeader); \
328}while(0)
329
330
335typedef struct CentralDirIterator
336{
337#ifdef __cplusplus
350 CentralDirIterator(ZipContainer* container, U8* buf, U32 bufSize);
355 ZipErr getECode();
362 ZipFileHeader* getElement();
367 bool nextElement();
368 private:
369#endif
370 ZipFileHeader fileHeader;
371 U32 curFileHeaderOffs;
372 ZipErr err;
373 U16 entriesInCd;
374 U16 curEntry;
376
377#ifdef __cplusplus
378extern "C" {
379#endif
383#define CentralDirIterator_getECode(o) (o)->err
388 struct ZipContainer* container);
389/* Re-entrant version,
390 * minimum size for buffer is 256
391 */
396 struct ZipContainer* container,
397 U8* buf, U32 bufSize);
406#ifdef __cplusplus
407}
409 CentralDirIterator_constructor(this, container);
410}
412 ZipContainer* container, U8* buf, U32 bufSize) {
413 CentralDirIterator_constructorR(this, container, buf, bufSize);
414}
416 return CentralDirIterator_getECode(this);
417}
418inline ZipFileHeader* CentralDirIterator::getElement() {
420}
422 return CentralDirIterator_nextElement(this) ? true : false;
423}
424#endif
425
426
427
433typedef struct ZipContainer
434{
435#ifdef __cplusplus
438 void *operator new(size_t s) { return ::baMalloc(s); }
439 void operator delete(void* d) { if(d) ::baFree(d); }
440 void *operator new(size_t, void *place) { return place; }
441 void operator delete(void*, void *) { }
442
455 ZipContainer(ZipReader* reader, U8* buf, U32 bufSize);
458 ZipErr getECode();
459 private:
460#endif
461 ZipReader* reader;
462 U8* buf;
463 U32 bufSize;
464 U32 cdOffset;
465 ZipErr errCode;
466 U16 entriesInCd;
468
469#ifdef __cplusplus
470extern "C" {
471#endif
476 ZipReader* reader,
477 U8* buf,
478 U32 bufSize);
482#define ZipContainer_getECode(o) (o)->errCode
483#ifdef __cplusplus
484}
485inline ZipContainer::ZipContainer(ZipReader* reader, U8* buf, U32 bufSize) {
486 ZipContainer_constructor(this,reader,buf,bufSize); }
488 return ZipContainer_getECode(this); }
489#endif
490
491int initGZipHeader(struct ZipFileInfo* zfi, GzipHeader* gzipH);
492
493 /* end of IO */
495
496
497#endif
int(* CspReader_Read)(struct CspReader *o, void *data, U32 offset, U32 size, int blockStart)
Prototype for the CspReader callback function.
Definition: CspRunTm.h:115
void * baMalloc(size_t size)
Allocate uninitialized storage using the target's configured allocator.
void baFree(void *p)
Release storage using the target's configured allocator.
BA_API void ZipContainer_constructor(ZipContainer *o, ZipReader *reader, U8 *buf, U32 bufSize)
Create a ZipContainer instance.
ZipErr getECode()
Definition: ZipFileIterator.h:487
ZipFileHeader * getElement()
Read the current central-directory entry.
Definition: ZipFileIterator.h:418
struct ZipContainer ZipContainer
A ZipContainer is a buffer used by a ZipIo when reading data from a ZipReader.
ZipErr
ZIP metadata parsing result; zero is success, negative values are failures.
Definition: ZipFileIterator.h:108
#define CentralDirIterator_getECode(o)
Query the iterator's last parsing result.
Definition: ZipFileIterator.h:383
BA_API BaBool CentralDirIterator_nextElement(CentralDirIterator *o)
Advance after a successful getElement().
struct CentralDirIterator CentralDirIterator
Low level ZIP file central directory iterator.
struct ZipFileInfo ZipFileInfo
Low level ZIP file information used internally by the Zip File System.
CentralDirIterator(ZipContainer *container)
Initialize an iterator using the container's shared working buffer.
Definition: ZipFileIterator.h:408
bool nextElement()
Advance after a successful getElement().
Definition: ZipFileIterator.h:421
#define ZipContainer_getECode(o)
Definition: ZipFileIterator.h:482
ZipErr getECode()
Query the iterator's last parsing result.
Definition: ZipFileIterator.h:415
BA_API void CentralDirIterator_constructorR(CentralDirIterator *o, struct ZipContainer *container, U8 *buf, U32 bufSize)
Initialize an iterator with separate working storage.
BA_API void ZipReader_constructor(ZipReader *o, CspReader_Read r, U32 zipFileSize)
Initialize a reader interface; no ZIP data is read yet.
BA_API void CentralDirIterator_constructor(CentralDirIterator *o, struct ZipContainer *container)
Initialize an iterator using the container's shared working buffer.
BA_API ZipFileHeader * CentralDirIterator_getElement(CentralDirIterator *o)
Read the current central-directory entry.
ZipComprMethod
ZIP compression-method identifiers used in entry metadata.
Definition: ZipFileIterator.h:119
@ ZipErr_Compression
Unsupported compr.
Definition: ZipFileIterator.h:112
@ ZipErr_Spanned
Spanned/Split archives not supported.
Definition: ZipFileIterator.h:111
@ ZipErr_Reading
Reading failed.
Definition: ZipFileIterator.h:110
@ ZipErr_Buf
The buffer is too small.
Definition: ZipFileIterator.h:109
@ ZipErr_Incompatible
Unknown ZIP Central Directory Structure.
Definition: ZipFileIterator.h:113
uint16_t U16
Unsigned 16-bit integer.
Definition: GenPrimT.h:91
uint32_t U32
Unsigned 32-bit integer.
Definition: GenPrimT.h:93
uint8_t U8
Unsigned 8-bit integer.
Definition: GenPrimT.h:89
U8 BaBool
Boolean stored in an unsigned byte; FALSE is zero and TRUE is one.
Definition: GenPrimT.h:118
Low level ZIP file central directory iterator.
Definition: ZipFileIterator.h:336
Abstract interface class for reading the "dat" file generated by HttpLink.
Definition: CspRunTm.h:126
A ZipContainer is a buffer used by a ZipIo when reading data from a ZipReader.
Definition: ZipFileIterator.h:434
ZipContainer()
Uninitialized storage; call ZipContainer_constructor before use.
Definition: ZipFileIterator.h:437
Low level ZIP file information used internally by the Zip File System.
Definition: ZipFileIterator.h:268
Abstract interface class for reading a ZipFile.
Definition: ZipFileIterator.h:74
ZipReader()
Uninitialized storage; call ZipReader_constructor before use.
Definition: ZipFileIterator.h:76