Debian Codegraph @

libs/zziplib/zziplib-src/zzip/fetch.c

raw
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 <[email protected]>
9 *
10 * Copyright (c) Guido Draheim, use under copyleft (LGPL,MPL)
11 */
12
13 #include <zzip/fetch.h>
14
15 /* ------------------------- fetch helpers --------------------------------- */
16
17 /** internal.
18 * Make 32 bit value in host byteorder from little-endian mapped octet-data
19 * (works also on machines which SIGBUS on misaligned data access (eg. 68000))
20 */
21 uint32_t
22 __zzip_get32(unsigned char *s)
23 {
24 #if defined __ZZIP_GET32
25 return __ZZIP_GET32(s);
26 #else
27 return ((uint32_t) s[3] << 24) | ((uint32_t) s[2] << 16)
28 | ((uint32_t) s[1] << 8) | ((uint32_t) s[0]);
29 #endif
30 }
31
32 /** => __zzip_get32
33 * This function does the same for a 16 bit value.
34 */
35 uint16_t
36 __zzip_get16(unsigned char *s)
37 {
38 #if defined __ZZIP_GET16
39 return __ZZIP_GET16(s);
40 #else
41 return ((uint16_t) s[1] << 8) | ((uint16_t) s[0]);
42 #endif
43 }
44
45 /** => __zzip_get32
46 * This function does the same for an off64_t value.
47 */
48 uint64_t
49 __zzip_get64(unsigned char *s)
50 {
51 #ifdef __GNUC__
52 /* *INDENT-OFF* */
53 register uint64_t v
54 = s[7]; v <<= 8;
55 v |= s[6]; v <<= 8;
56 v |= s[5]; v <<= 8;
57 v |= s[4]; v <<= 8;
58 v |= s[3]; v <<= 8;
59 v |= s[2]; v <<= 8;
60 v |= s[1]; v <<= 8;
61 v |= s[0]; return v;
62 /* *INDENT-ON* */
63 #else
64 return ((uint64_t) s[7] << 56) | ((uint64_t) s[6] << 48)
65 | ((uint64_t) s[5] << 40) | ((uint64_t) s[4] << 32)
66 | ((uint64_t) s[3] << 24) | ((uint64_t) s[2] << 16)
67 | ((uint64_t) s[1] << 8) | ((uint64_t) s[0]);
68 #endif
69 }
70
71 /** => __zzip_get32
72 * This function pushes a 32bit value at the specified address
73 */
74 void
75 __zzip_set32(unsigned char *s, uint32_t v)
76 {
77 #if defined __ZZIP_SET32
78 return __ZZIP_SET32(s, v);
79 #else
80 /* *INDENT-OFF* */
81 s[0] = (unsigned char) (v); v >>= 8;
82 s[1] = (unsigned char) (v); v >>= 8;
83 s[2] = (unsigned char) (v); v >>= 8;
84 s[3] = (unsigned char) (v);
85 /* *INDENT-ON* */
86 #endif
87 }
88
89 /** => __zzip_get32
90 * This function does the same for a 16 bit value.
91 */
92 void
93 __zzip_set16(unsigned char *s, uint16_t v)
94 {
95 #if defined __ZZIP_SET16
96 return __ZZIP_SET16(s, v);
97 #else
98 /* *INDENT-OFF* */
99 s[0] = (unsigned char) (v); v >>= 8;
100 s[1] = (unsigned char) (v);
101 /* *INDENT-ON* */
102 #endif
103 }
104
105 /** => __zzip_get32
106 * This function pushes a off64_t value at the specified address
107 */
108 void
109 __zzip_set64(unsigned char *s, uint64_t v)
110 {
111 /* *INDENT-OFF* */
112 s[0] = (unsigned char) (v); v >>= 8;
113 s[1] = (unsigned char) (v); v >>= 8;
114 s[2] = (unsigned char) (v); v >>= 8;
115 s[3] = (unsigned char) (v); v >>= 8;
116 s[4] = (unsigned char) (v); v >>= 8;
117 s[5] = (unsigned char) (v); v >>= 8;
118 s[6] = (unsigned char) (v); v >>= 8;
119 s[7] = (unsigned char) (v);
120 /* *INDENT-ON* */
121 }