-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathjunzip.h
141 lines (117 loc) · 4.19 KB
/
junzip.h
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
/**
* JUnzip library by Joonas Pihlajamaa (firstname.lastname@iki.fi).
* Released into public domain. https://github.com/jokkebk/JUnzip
*/
#ifndef __JUNZIP_H
#define __JUNZIP_H
#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */
#include <stdint.h>
// Enable compiling without Zlib as well
// (no compression support, only "store")
#ifdef HAVE_ZLIB
#include <zlib.h>
#else
#define Z_OK 0
#define Z_ERRNO -1
#endif
#ifdef HAVE_PUFF
#include "puff.h"
#endif
// If you don't have stdint.h, the following two lines should work for most 32/64 bit systems
// typedef unsigned int uint32_t;
// typedef unsigned short uint16_t;
#define JZHOUR(t) ((t)>>11)
#define JZMINUTE(t) (((t)>>5) & 63)
#define JZSECOND(t) (((t) & 31) * 2)
#define JZTIME(h,m,s) (((h)<<11) + ((m)<<5) + (s)/2)
#define JZYEAR(t) (((t)>>9) + 1980)
#define JZMONTH(t) (((t)>>5) & 15)
#define JZDAY(t) ((t) & 31)
#define JZDATE(y,m,d) ((((y)-1980)<<9) + ((m)<<5) + (d))
typedef struct JZFile JZFile;
struct JZFile {
size_t (*read)(JZFile *file, void *buf, size_t size);
size_t (*tell)(JZFile *file);
int (*seek)(JZFile *file, size_t offset, int whence);
int (*error)(JZFile *file);
void (*close)(JZFile *file);
};
JZFile *
jzfile_from_stdio_file(FILE *fp);
typedef struct __attribute__ ((__packed__)) {
uint32_t signature; // 0x04034B50
uint16_t versionNeededToExtract; // unsupported
uint16_t generalPurposeBitFlag; // unsupported
uint16_t compressionMethod;
uint16_t lastModFileTime;
uint16_t lastModFileDate;
uint32_t crc32;
uint32_t compressedSize;
uint32_t uncompressedSize;
uint16_t fileNameLength;
uint16_t extraFieldLength; // unsupported
} JZLocalFileHeader;
typedef struct __attribute__ ((__packed__)) {
uint32_t signature; // 0x02014B50
uint16_t versionMadeBy; // unsupported
uint16_t versionNeededToExtract; // unsupported
uint16_t generalPurposeBitFlag; // unsupported
uint16_t compressionMethod;
uint16_t lastModFileTime;
uint16_t lastModFileDate;
uint32_t crc32;
uint32_t compressedSize;
uint32_t uncompressedSize;
uint16_t fileNameLength;
uint16_t extraFieldLength; // unsupported
uint16_t fileCommentLength; // unsupported
uint16_t diskNumberStart; // unsupported
uint16_t internalFileAttributes; // unsupported
uint32_t externalFileAttributes; // unsupported
uint32_t relativeOffsetOflocalHeader;
} JZGlobalFileHeader;
typedef struct __attribute__ ((__packed__)) {
uint16_t compressionMethod;
uint16_t lastModFileTime;
uint16_t lastModFileDate;
uint32_t crc32;
uint32_t compressedSize;
uint32_t uncompressedSize;
uint32_t offset;
} JZFileHeader;
typedef struct __attribute__ ((__packed__)) {
uint32_t signature; // 0x06054b50
uint16_t diskNumber; // unsupported
uint16_t centralDirectoryDiskNumber; // unsupported
uint16_t numEntriesThisDisk; // unsupported
uint16_t numEntries;
uint32_t centralDirectorySize;
uint32_t centralDirectoryOffset;
uint16_t zipCommentLength;
// Followed by .ZIP file comment (variable size)
} JZEndRecord;
// Callback prototype for central and local file record reading functions
typedef int (*JZRecordCallback)(JZFile *zip, int index, JZFileHeader *header,
char *filename, void *user_data);
#define JZ_BUFFER_SIZE 65536
// Read ZIP file end record. Will move within file.
int jzReadEndRecord(JZFile *zip, JZEndRecord *endRecord);
// Read ZIP file global directory. Will move within file.
// Callback is called for each record, until callback returns zero
int jzReadCentralDirectory(JZFile *zip, JZEndRecord *endRecord,
JZRecordCallback callback, void *user_data);
// Read local ZIP file header. Silent on errors so optimistic reading possible.
int jzReadLocalFileHeader(JZFile *zip, JZFileHeader *header,
char *filename, int len);
// Same as above but returns the full raw header
int jzReadLocalFileHeaderRaw(JZFile *zip, JZLocalFileHeader *header,
char *filename, int len);
// Read data from file stream, described by header, to preallocated buffer
// Return value is zlib coded, e.g. Z_OK, or error code
int jzReadData(JZFile *zip, JZFileHeader *header, void *buffer);
#ifdef __cplusplus
};
#endif /* __cplusplus */
#endif