Browse code

various platform-specific cleanups

git-svn: trunk@3452

Tomasz Kojm authored on 2007/12/22 08:47:52
Showing 4 changed files
... ...
@@ -1,3 +1,7 @@
1
+Sat Dec 22 00:10:53 CET 2007 (tk)
2
+---------------------------------
3
+  * libclamav: various platform-specific cleanups
4
+
1 5
 Fri Dec 21 10:39:22 GMT 2007 (njh)
2 6
 ----------------------------------
3 7
   * libclamav/vba_extract.c:	More tidies
... ...
@@ -25,42 +25,43 @@
25 25
 #include "clamav-config.h"
26 26
 #endif
27 27
 
28
-#ifdef HAVE_STDINT_H
29
-#include <stdint.h>
30
-#elif defined HAVE_SYS_INT_TYPES_H /*solaris*/
28
+#ifdef HAVE_SYS_INT_TYPES_H
29
+/* First to give it higher priority on Solaris */
31 30
 #include <sys/int_types.h>
32
-#elif defined HAVE_INTTYPES_H /*freebsd*/
31
+#elif defined(HAVE_INTTYPES_H)
32
+/* C99: inttypes.h should include stdint.h; more universal because some
33
+ * older platforms don't provide stdint.h
34
+ */
33 35
 #include <inttypes.h>
36
+#elif defined(HAVE_STDINT_H)
37
+#include <stdint.h>
34 38
 #else
35
-    typedef unsigned char uint8_t;      typedef signed char int8_t;
36
-
37
-# if SIZEOF_INT == 2
38
-    typedef unsigned int uint16_t;      typedef signed int int16_t;
39
-# elif SIZEOF_SHORT == 2
40
-    typedef unsigned short uint16_t;    typedef signed short int16_t;
41
-# else
42
-#   error unable to typedef int16_t from either int or short
43
-    typedef unsigned short uint16_t;    typedef signed short int16_t;
44
-# endif
39
+    typedef signed char int8_t;
40
+    typedef unsigned char uint8_t;
45 41
 
46
-# if SIZEOF_INT == 4
47
-    typedef unsigned int uint32_t;      typedef signed int int32_t;
48
-# elif SIZEOF_LONG == 4
49
-    typedef unsigned long uint32_t;     typedef signed long int32_t;
50
-# else
51
-#   error unable to typedef int32_t from either int or long
52
-    typedef unsigned long uint32_t;     typedef signed long int32_t;
53
-# endif
42
+#if SIZEOF_INT == 2
43
+    typedef signed int int16_t;
44
+    typedef unsigned int uint16_t;
45
+#elif SIZEOF_SHORT == 2
46
+    typedef signed short int16_t;
47
+    typedef unsigned short uint16_t;
48
+#endif
54 49
 
55
-# if SIZEOF_LONG == 8
56
-    typedef unsigned long uint64_t;      typedef signed long int64_t;
57
-# elif SIZEOF_LONG_LONG == 8
58
-    typedef unsigned long long uint64_t;     typedef signed long long int64_t;
59
-# else
60
-#   error unable to typedef int64_t from either long or long long
61
-    typedef unsigned long long uint64_t;     typedef signed long long int64_t;
62
-# endif
50
+#if SIZEOF_INT == 4
51
+    typedef signed int int32_t;
52
+    typedef unsigned int uint32_t;
53
+#elif SIZEOF_LONG == 4
54
+    typedef signed long int32_t;
55
+    typedef unsigned long uint32_t;
56
+#endif
63 57
 
58
+#if SIZEOF_LONG == 8
59
+    typedef signed long int64_t;
60
+    typedef unsigned long uint64_t;
61
+#elif SIZEOF_LONG_LONG == 8
62
+    typedef signed long long int64_t;
63
+    typedef unsigned long long uint64_t;
64
+#endif
64 65
 #endif
65 66
 
66 67
 #endif
... ...
@@ -99,35 +99,65 @@ typedef struct {
99 99
 #define BLOCKMAX	    (ctx->options & CL_SCAN_BLOCKMAX)
100 100
 #define DETECT_BROKEN	    (ctx->options & CL_SCAN_BLOCKBROKEN)
101 101
 
102
+/* based on macros from A. Melnikoff */
103
+#define cbswap16(v) (((v & 0xff) << 8) | (((v) >> 8) & 0xff))
104
+#define cbswap32(v) ((((v) & 0x000000ff) << 24) | (((v) & 0x0000ff00) << 8) | \
105
+		    (((v) & 0x00ff0000) >> 8)  | (((v) & 0xff000000) >> 24))
106
+#define cbswap64(v) ((((x) & 0x00000000000000ff) << 56) | \
107
+		     (((x) & 0x000000000000ff00) << 40) | \
108
+		     (((v) & 0x0000000000ff0000) << 24) | \
109
+		     (((v) & 0x00000000ff000000) <<  8) | \
110
+		     (((v) & 0x000000ff00000000) >>  8) | \
111
+		     (((v) & 0x0000ff0000000000) >> 24) | \
112
+		     (((v) & 0x00ff000000000000) >> 40) | \
113
+		     (((v) & 0xff00000000000000) >> 56))
114
+
115
+
102 116
 #if WORDS_BIGENDIAN == 0
103
-/* new macros from A. Melnikoff */
117
+/* Little endian */
104 118
 #define le16_to_host(v)	(v)
105 119
 #define le32_to_host(v)	(v)
106 120
 #define le64_to_host(v)	(v)
107
-#define	be16_to_host(v)	((v >> 8) | ((v & 0xFF) << 8))
108
-#define	be32_to_host(v)	((v >> 24) | ((v & 0x00FF0000) >> 8) | \
109
-				((v & 0x0000FF00) << 8) | (v << 24))
110
-#define be64_to_host(v)	((v >> 56) | ((v & 0x00FF000000000000LL) >> 40) | \
111
-				((v & 0x0000FF0000000000LL) >> 24) | \
112
-				((v & 0x000000FF00000000LL) >> 8) |  \
113
-				((v & 0x00000000FF000000LL) << 8) |  \
114
-				((v & 0x0000000000FF0000LL) << 24) | \
115
-				((v & 0x000000000000FF00LL) << 40) | \
116
-				(v << 56))
121
+#define	be16_to_host(v)	cbswap16(v)
122
+#define	be32_to_host(v)	cbswap32(v)
123
+#define be64_to_host(v) cbswap64(v)
124
+#define cli_readint32(buff) (*(const int32_t *)(buff))
125
+#define cli_readint16(buff) (*(const int16_t *)(buff))
126
+#define cli_writeint32(offset, value) (*(uint32_t *)(offset)=(uint32_t)(value))
117 127
 #else
118
-#define	le16_to_host(v)	((v >> 8) | ((v & 0xFF) << 8))
119
-#define	le32_to_host(v)	((v >> 24) | ((v & 0x00FF0000) >> 8) | \
120
-				((v & 0x0000FF00) << 8) | (v << 24))
121
-#define le64_to_host(v)	((v >> 56) | ((v & 0x00FF000000000000LL) >> 40) | \
122
-				((v & 0x0000FF0000000000LL) >> 24) | \
123
-				((v & 0x000000FF00000000LL) >> 8) |  \
124
-				((v & 0x00000000FF000000LL) << 8) |  \
125
-				((v & 0x0000000000FF0000LL) << 24) | \
126
-				((v & 0x000000000000FF00LL) << 40) | \
127
-				(v << 56))
128
+/* Big endian */
129
+#define	le16_to_host(v)	cbswap16(v)
130
+#define	le32_to_host(v)	cbswap32(v)
131
+#define le64_to_host(v) cbswap64(v)
128 132
 #define be16_to_host(v)	(v)
129 133
 #define be32_to_host(v)	(v)
130 134
 #define be64_to_host(v)	(v)
135
+
136
+static inline int32_t cli_readint32(const char *buff)
137
+{
138
+	int32_t ret;
139
+    ret = buff[0] & 0xff;
140
+    ret |= (buff[1] & 0xff) << 8;
141
+    ret |= (buff[2] & 0xff) << 16;
142
+    ret |= (buff[3] & 0xff) << 24;
143
+    return ret;
144
+}
145
+
146
+static inline int16_t cli_readint32(const char *buff)
147
+{
148
+	int16_t ret;
149
+    ret = buff[0] & 0xff;
150
+    ret |= (buff[1] & 0xff) << 8;
151
+    return ret;
152
+}
153
+
154
+static inline void cli_writeint32(char *offset, uint32_t value)
155
+{
156
+    offset[0] = value & 0xff;
157
+    offset[1] = (value & 0xff00) >> 8;
158
+    offset[2] = (value & 0xff0000) >> 16;
159
+    offset[3] = (value & 0xff000000) >> 24;
160
+}
131 161
 #endif
132 162
 
133 163
 /* used by: spin, yc (C) aCaB */
... ...
@@ -201,36 +231,4 @@ void cli_bitset_free(bitset_t *bs);
201 201
 int cli_bitset_set(bitset_t *bs, unsigned long bit_offset);
202 202
 int cli_bitset_test(bitset_t *bs, unsigned long bit_offset);
203 203
 
204
-#if WORDS_BIGENDIAN == 0
205
-#define cli_readint32(buff) (*(const int32_t *)(buff))
206
-#define cli_readint16(buff) (*(const int16_t *)(buff))
207
-#define cli_writeint32(offset, value) (*(uint32_t *)(offset)=(uint32_t)(value))
208
-#else
209
-static inline int32_t cli_readint32(const char *buff)
210
-{
211
-	int32_t ret;
212
-    ret = buff[0] & 0xff;
213
-    ret |= (buff[1] & 0xff) << 8;
214
-    ret |= (buff[2] & 0xff) << 16;
215
-    ret |= (buff[3] & 0xff) << 24;
216
-    return ret;
217
-}
218
-
219
-static inline int16_t cli_readint32(const char *buff)
220
-{
221
-	int16_t ret;
222
-    ret = buff[0] & 0xff;
223
-    ret |= (buff[1] & 0xff) << 8;
224
-    return ret;
225
-}
226
-
227
-static inline void cli_writeint32(char *offset, uint32_t value)
228
-{
229
-    offset[0] = value & 0xff;
230
-    offset[1] = (value & 0xff00) >> 8;
231
-    offset[2] = (value & 0xff0000) >> 16;
232
-    offset[3] = (value & 0xff000000) >> 24;
233
-}
234
-#endif /* WORDS_BIGENDIAN == 0 */
235
-
236 204
 #endif
... ...
@@ -399,8 +399,8 @@ int unrar_extract_next_prepare(unrar_state_t *state, const char *dirname)
399 399
     if(!new_metadata)
400 400
 	return UNRAR_EMEM;
401 401
 
402
-    new_metadata->pack_size = state->file_header->high_pack_size * 0x100000000 + state->file_header->pack_size;
403
-    new_metadata->unpack_size = state->file_header->high_unpack_size * 0x100000000 + state->file_header->unpack_size;
402
+    new_metadata->pack_size = state->file_header->high_pack_size * 0x100000000ULL + state->file_header->pack_size;
403
+    new_metadata->unpack_size = state->file_header->high_unpack_size * 0x100000000ULL + state->file_header->unpack_size;
404 404
     new_metadata->crc = state->file_header->file_crc;
405 405
     new_metadata->method = state->file_header->method;
406 406
     new_metadata->filename = strdup(state->file_header->filename);