1 #ifndef __INKJAR_JAR_H_
2 #define __INKJAR_JAR_H_
3 /*
4 * Copyright (C) 1999 Bryan Burns
5 * Copyright (C) 2004 Johan Ceuppens
6 * Released under GNU GPL, read the file 'COPYING' for more information
7 */
9 #ifdef HAVE_CONFIG_H
10 # include "config.h"
11 #endif
13 #if defined(WIN32) || defined(__WIN32__)
14 # include <zlib.h>
15 #endif
17 #ifdef HAVE_ZLIB_H
18 # include <zlib.h>
19 #endif
21 #ifdef HAVE_INTTYPES_H
22 # include <inttypes.h>
23 #else
24 # ifdef HAVE_STDINT_H
25 # include <stdint.h>
26 # endif
27 #endif
29 #include <glib/garray.h>
30 #include <glib/gtypes.h>
32 namespace Inkjar {
34 unsigned const RDSZ = 4096;
36 //#define DEBUG 1 //uncommment for debug messages
38 enum JarFileReaderState {CLOSED, OPEN};
40 //fixme: The following will be removed
41 typedef uint8_t ub1;
42 typedef uint16_t ub2;
43 typedef uint32_t ub4;
45 #define LOC_EXTRA 6 /* extra bytes */
46 #define LOC_COMP 8 /* compression method */
47 #define LOC_MODTIME 10 /* last modification time */
48 #define LOC_MODDATE 12 /* last modification date */
49 #define LOC_CRC 14 /* CRC */
50 #define LOC_CSIZE 18 /* compressed size */
51 #define LOC_USIZE 22 /* uncompressed size */
52 #define LOC_FNLEN 26 /* filename length */
53 #define LOC_EFLEN 28 /* extra-field length */
55 #define CEN_COMP 10 /* compression method */
56 #define CEN_MODTIME 12
57 #define CEN_MODDATE 14
58 #define CEN_CRC 16
59 #define CEN_CSIZE 20
60 #define CEN_USIZE 24
61 #define CEN_FNLEN 28
62 #define CEN_EFLEN 30
63 #define CEN_COMLEN 32
64 #define CEN_OFFSET 42
67 /* macros */
68 #define PACK_UB4(d, o, v) d[o] = (ub1)((v) & 0x000000ff); \
69 d[o + 1] = (ub1)(((v) & 0x0000ff00) >> 8); \
70 d[o + 2] = (ub1)(((v) & 0x00ff0000) >> 16); \
71 d[o + 3] = (ub1)(((v) & 0xff000000) >> 24)
73 #define PACK_UB2(d, o, v) d[o] = (ub1)((v) & 0x00ff); \
74 d[o + 1] = (ub1)(((v) & 0xff00) >> 8)
76 #define UNPACK_UB4(s, o) (ub4)s[o] + (((ub4)s[o + 1]) << 8) +\
77 (((ub4)s[o + 2]) << 16) + (((ub4)s[o + 3]) << 24)
79 #define UNPACK_UB2(s, o) (ub2)s[o] + (((ub2)s[o + 1]) << 8)
83 /*
84 * JarFile:
85 *
86 * This is a wrapper class for canonical jarfile functions like reading,
87 * writing, seeking etc. JarFile is a dumb class with no state information.
88 *
89 * All memory allocations are done with g_malloc.
90 */
92 class JarFile {
93 public:
95 JarFile() : fd(-1), _filename(NULL), _last_filename(NULL) {}
96 virtual ~JarFile();
97 JarFile(gchar const *new_filename);
99 GByteArray *get_next_file_contents();
100 gchar *get_last_filename() const;
101 bool open();
102 bool close();
103 int read(guint8 *buf, int count);
105 JarFile(JarFile const &rhs);
106 JarFile &operator=(JarFile const &rhs);
108 private:
110 int fd;
111 gchar *_filename;
112 z_stream _zs;
113 gchar *_last_filename;
115 bool init_inflation();
116 bool read_signature();
117 guint32 get_crc(guint8 *bytes, guint16 flags);
118 guint8 *read_filename(guint16 filename_length);
119 bool check_compression_method(guint16 method, guint16 flags);
120 bool check_crc(guint32 oldcrc, guint32 crc, guint16 flags);
121 guint8 *get_compressed_file(guint32 compressed_size,
122 unsigned int &file_length,
123 guint32 oldcrc, guint16 flags);
124 guint8 *get_uncompressed_file(guint32 compressed_szie, guint32 crc,
125 guint16 eflen, guint16 flags);
126 }; // class JarFile
129 /*
130 * JarFileReader:
131 *
132 * This provides some smarter functions for operating on a jarfile object
133 * It should be able to grep for files or return the contents of a specific
134 * file.
135 */
137 class JarFileReader {
138 public:
140 JarFileReader(gchar const *new_filename)
141 : _state(CLOSED), _jarfile(new_filename) {}
142 JarFileReader() : _state(CLOSED) {}
143 virtual ~JarFileReader() { if (_state == OPEN) _jarfile.close(); }
144 //fixme return types are incorrect
145 GByteArray *get_next_file();//fixme clean up return type
146 void set_filename(gchar const *new_filename);
147 void set_jarfile(JarFile const &new_jarfile);
148 gchar *get_last_filename() const { return _jarfile.get_last_filename(); };
149 JarFileReader(JarFileReader const &rhs);
150 JarFileReader &operator=(JarFileReader const &rhs);
151 private:
152 JarFileReaderState _state;
153 JarFile _jarfile;
155 }; // class JarFileReader
157 } // namespace Inkjar
158 #endif // header guard
160 /*
161 Local Variables:
162 mode:c++
163 c-file-style:"stroustrup"
164 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
165 indent-tabs-mode:nil
166 fill-column:99
167 End:
168 */
169 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :