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