3 * These routines are helpers - note that this file gets linked into all the
4 * zip-access variants that refer to <zzip/format.h>. On the x86 platform the
5 * actual definitions will be empty - fetching is done on native machine-level
8 * Guido Draheim <guidod@gmx.de>
10 * Copyright (c) 2004,2005,2006 Guido Draheim
11 * All rights reserved,
12 * use under the restrictions of the
13 * Lesser GNU General Public License
14 * or alternatively the restrictions
15 * of the Mozilla Public License 1.1
18 #include <zzip/fetch.h>
20 #if defined ZZIP_WORDS_BIGENDIAN && \
21 defined bswap_16 && defined bswap_32 && defined bswap_64
22 # define __ZZIP_GET16(__p) bswap_16(*(uint16_t*)(__p))
23 # define __ZZIP_GET32(__p) bswap_32(*(uint32_t*)(__p))
24 # define __ZZIP_SET16(__p,__x) (*(uint16_t*)(__p) = bswap_16((uint16_t)(__x)))
25 # define __ZZIP_SET32(__p,__x) (*(uint32_t*)(__p) = bswap_32((uint32_t)(__x)))
26 # define __ZZIP_GET64(__p) bswap_64(*(zzip_off64_t*)(__p))
27 # define __ZZIP_SET64(__p,__x) \
28 (*(zzip_off64_t*)(__p) = bswap_64((zzip_off64_t)(__x)))
31 /* ------------------------- fetch helpers --------------------------------- */
34 * Make 32 bit value in host byteorder from little-endian mapped octet-data
35 * (works also on machines which SIGBUS on misaligned data access (eg. 68000))
38 __zzip_get32(unsigned char *s)
40 #if defined __ZZIP_GET32
41 return __ZZIP_GET32(s);
43 return ((uint32_t) s[3] << 24) | ((uint32_t) s[2] << 16)
44 | ((uint32_t) s[1] << 8) | ((uint32_t) s[0]);
49 * This function does the same for a 16 bit value.
52 __zzip_get16(unsigned char *s)
54 #if defined __ZZIP_GET16
55 return __ZZIP_GET16(s);
57 return ((uint16_t) s[1] << 8) | ((uint16_t) s[0]);
62 * This function does the same for an off64_t value.
65 __zzip_get64(unsigned char *s)
80 return ((uint64_t) s[7] << 56) | ((uint64_t) s[6] << 48)
81 | ((uint64_t) s[5] << 40) | ((uint64_t) s[4] << 32)
82 | ((uint64_t) s[3] << 24) | ((uint64_t) s[2] << 16)
83 | ((uint64_t) s[1] << 8) | ((uint64_t) s[0]);
88 * This function pushes a 32bit value at the specified address
91 __zzip_set32(unsigned char *s, uint32_t v)
93 #if defined __ZZIP_SET32
94 return __ZZIP_SET32(s, v);
97 s[0] = (unsigned char) (v); v >>= 8;
98 s[1] = (unsigned char) (v); v >>= 8;
99 s[2] = (unsigned char) (v); v >>= 8;
100 s[3] = (unsigned char) (v);
106 * This function does the same for a 16 bit value.
109 __zzip_set16(unsigned char *s, uint16_t v)
111 #if defined __ZZIP_SET16
112 return __ZZIP_SET16(s, v);
115 s[0] = (unsigned char) (v); v >>= 8;
116 s[1] = (unsigned char) (v);
122 * This function pushes a off64_t value at the specified address
125 __zzip_set64(unsigned char *s, uint64_t v)
128 s[0] = (unsigned char) (v); v >>= 8;
129 s[1] = (unsigned char) (v); v >>= 8;
130 s[2] = (unsigned char) (v); v >>= 8;
131 s[3] = (unsigned char) (v); v >>= 8;
132 s[4] = (unsigned char) (v); v >>= 8;
133 s[5] = (unsigned char) (v); v >>= 8;
134 s[6] = (unsigned char) (v); v >>= 8;
135 s[7] = (unsigned char) (v);