]> granicus.if.org Git - zziplib/blob - zzip/fetch.c
indentation
[zziplib] / zzip / fetch.c
1
2 /*
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
6  *
7  * Author: 
8  *      Guido Draheim <guidod@gmx.de>
9  *
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
16  */
17
18 #include <zzip/fetch.h>
19
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)))
29 #endif
30
31 /* ------------------------- fetch helpers --------------------------------- */
32
33 /**
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))
36  */
37 uint32_t
38 __zzip_get32(unsigned char *s)
39 {
40 #if defined __ZZIP_GET32
41     return __ZZIP_GET32(s);
42 #else
43     return ((uint32_t) s[3] << 24) | ((uint32_t) s[2] << 16)
44         | ((uint32_t) s[1] << 8) | ((uint32_t) s[0]);
45 #endif
46 }
47
48 /** => __zzip_get32
49  * This function does the same for a 16 bit value.
50  */
51 uint16_t
52 __zzip_get16(unsigned char *s)
53 {
54 #if defined __ZZIP_GET16
55     return __ZZIP_GET16(s);
56 #else
57     return ((uint16_t) s[1] << 8) | ((uint16_t) s[0]);
58 #endif
59 }
60
61 /** => __zzip_get32
62  * This function does the same for an off64_t value.
63  */
64 uint64_t
65 __zzip_get64(unsigned char *s)
66 {
67 #ifdef __GNUC__
68     /* *INDENT-OFF* */
69     register uint64_t v
70        = s[7]; v <<= 8;
71     v |= s[6]; v <<= 8;
72     v |= s[5]; v <<= 8;
73     v |= s[4]; v <<= 8;
74     v |= s[3]; v <<= 8;
75     v |= s[2]; v <<= 8;
76     v |= s[1]; v <<= 8;
77     v |= s[0]; return v;
78     /* *INDENT-ON* */
79 #else
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]);
84 #endif
85 }
86
87 /** => __zzip_get32
88  * This function pushes a 32bit value at the specified address
89  */
90 void
91 __zzip_set32(unsigned char *s, uint32_t v)
92 {
93 #if defined __ZZIP_SET32
94     return __ZZIP_SET32(s, v);
95 #else
96     /* *INDENT-OFF* */
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);
101     /* *INDENT-ON* */
102 #endif
103 }
104
105 /** => __zzip_get32
106  * This function does the same for a 16 bit value.
107  */
108 void
109 __zzip_set16(unsigned char *s, uint16_t v)
110 {
111 #if defined __ZZIP_SET16
112     return __ZZIP_SET16(s, v);
113 #else
114     /* *INDENT-OFF* */
115     s[0] = (unsigned char) (v); v >>= 8;
116     s[1] = (unsigned char) (v);
117     /* *INDENT-ON* */
118 #endif
119 }
120
121 /** => __zzip_get32
122  * This function pushes a off64_t value at the specified address
123  */
124 void
125 __zzip_set64(unsigned char *s, uint64_t v)
126 {
127     /* *INDENT-OFF* */
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);
136     /* *INDENT-ON* */
137 }