Code

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