Upx_Doxygen
https://github.com/upx/upx
bele_policy.h
1 /* bele_policy.h -- access memory in BigEndian and LittleEndian byte order
2 
3  This file is part of the UPX executable compressor.
4 
5  Copyright (C) 1996-2016 Markus Franz Xaver Johannes Oberhumer
6  Copyright (C) 1996-2016 Laszlo Molnar
7  All Rights Reserved.
8 
9  UPX and the UCL library are free software; you can redistribute them
10  and/or modify them under the terms of the GNU General Public License as
11  published by the Free Software Foundation; either version 2 of
12  the License, or (at your option) any later version.
13 
14  This program is distributed in the hope that it will be useful,
15  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  GNU General Public License for more details.
18 
19  You should have received a copy of the GNU General Public License
20  along with this program; see the file COPYING.
21  If not, write to the Free Software Foundation, Inc.,
22  59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23 
24  Markus F.X.J. Oberhumer Laszlo Molnar
25  <markus@oberhumer.com> <ezerotven+github@gmail.com>
26  */
27 
28 
29 #ifndef __UPX_BELE_H
30 # error "this is an internal include file"
31 #endif
32 
33 
34 /*************************************************************************
35 //
36 **************************************************************************/
37 
38 #if defined(BELE_CTP)
39  // CTP - Compile-Time Polymorphism (templates)
40 # define V static inline
41 # define S static int __acc_cdecl_qsort
42 # define C /*empty*/
43 #elif defined(BELE_RTP)
44  // RTP - Run-Time Polymorphism (virtual functions)
45 # define V virtual
46 # define S virtual int
47 # define C const
48 #else
49 # error
50 #endif
51 
52 
53 #if defined(BELE_RTP)
54 struct AbstractPolicy
55 {
56  inline AbstractPolicy() { }
57  virtual inline ~AbstractPolicy() { }
58  V bool isBE() C = 0;
59  V bool isLE() C = 0;
60 
61  V unsigned get16(const void *p) C = 0;
62  V unsigned get24(const void *p) C = 0;
63  V unsigned get32(const void *p) C = 0;
64  V upx_uint64_t get64(const void *p) C = 0;
65 
66  V void set16(void *p, unsigned v) C = 0;
67  V void set24(void *p, unsigned v) C = 0;
68  V void set32(void *p, unsigned v) C = 0;
69  V void set64(void *p, upx_uint64_t v) C = 0;
70 
71  V unsigned get16_signed(const void *p) C = 0;
72  V unsigned get24_signed(const void *p) C = 0;
73  V unsigned get32_signed(const void *p) C = 0;
74  V upx_uint64_t get64_signed(const void *p) C = 0;
75 
76  S u16_compare(const void *a, const void *b) C = 0;
77  S u24_compare(const void *a, const void *b) C = 0;
78  S u32_compare(const void *a, const void *b) C = 0;
79  S u64_compare(const void *a, const void *b) C = 0;
80 
81  S u16_compare_signed(const void *a, const void *b) C = 0;
82  S u24_compare_signed(const void *a, const void *b) C = 0;
83  S u32_compare_signed(const void *a, const void *b) C = 0;
84  S u64_compare_signed(const void *a, const void *b) C = 0;
85 
86  // disable dynamic allocation
87  DISABLE_NEW_DELETE
88 };
89 #endif
90 
91 
92 struct BEPolicy
93 #if defined(BELE_RTP)
94  : public AbstractPolicy
95 #endif
96 {
97  inline BEPolicy() { }
98 #if defined(BELE_CTP)
99  typedef N_BELE_RTP::BEPolicy RTP_Policy;
100 #elif defined(BELE_RTP)
101  typedef N_BELE_CTP::BEPolicy CTP_Policy;
102 #endif
103  V bool isBE() C { return true; }
104  V bool isLE() C { return false; }
105 
106  typedef BE16 U16;
107  typedef BE32 U32;
108  typedef BE64 U64;
109 
110  V unsigned get16(const void *p) C
111  { return get_be16(p); }
112  V unsigned get24(const void *p) C
113  { return get_be24(p); }
114  V unsigned get32(const void *p) C
115  { return get_be32(p); }
116  V upx_uint64_t get64(const void *p) C
117  { return get_be64(p); }
118 
119  V void set16(void *p, unsigned v) C
120  { set_be16(p, v); }
121  V void set24(void *p, unsigned v) C
122  { set_be24(p, v); }
123  V void set32(void *p, unsigned v) C
124  { set_be32(p, v); }
125  V void set64(void *p, upx_uint64_t v) C
126  { set_be64(p, v); }
127 
128  V unsigned get16_signed(const void *p) C
129  { return get_be16_signed(p); }
130  V unsigned get24_signed(const void *p) C
131  { return get_be24_signed(p); }
132  V unsigned get32_signed(const void *p) C
133  { return get_be32_signed(p); }
134  V upx_uint64_t get64_signed(const void *p) C
135  { return get_be64_signed(p); }
136 
137  S u16_compare(const void *a, const void *b) C
138  { return be16_compare(a, b); }
139  S u24_compare(const void *a, const void *b) C
140  { return be24_compare(a, b); }
141  S u32_compare(const void *a, const void *b) C
142  { return be32_compare(a, b); }
143  S u64_compare(const void *a, const void *b) C
144  { return be64_compare(a, b); }
145 
146  S u16_compare_signed(const void *a, const void *b) C
147  { return be16_compare_signed(a, b); }
148  S u24_compare_signed(const void *a, const void *b) C
149  { return be24_compare_signed(a, b); }
150  S u32_compare_signed(const void *a, const void *b) C
151  { return be32_compare_signed(a, b); }
152  S u64_compare_signed(const void *a, const void *b) C
153  { return be64_compare_signed(a, b); }
154 
155  static void compileTimeAssertions() {
156  COMPILE_TIME_ASSERT(sizeof(U16) == 2)
157  COMPILE_TIME_ASSERT(sizeof(U32) == 4)
158  COMPILE_TIME_ASSERT(sizeof(U64) == 8)
159  COMPILE_TIME_ASSERT_ALIGNED1(U16)
160  COMPILE_TIME_ASSERT_ALIGNED1(U32)
161  COMPILE_TIME_ASSERT_ALIGNED1(U64)
162  }
163 
164  // disable dynamic allocation
165  DISABLE_NEW_DELETE
166 };
167 
168 
169 struct LEPolicy
170 #if defined(BELE_RTP)
171  : public AbstractPolicy
172 #endif
173 {
174  inline LEPolicy() { }
175 #if defined(BELE_CTP)
176  typedef N_BELE_RTP::LEPolicy RTP_Policy;
177 #elif defined(BELE_RTP)
178  typedef N_BELE_CTP::LEPolicy CTP_Policy;
179 #endif
180  V bool isBE() C { return false; }
181  V bool isLE() C { return true; }
182 
183  typedef LE16 U16;
184  typedef LE32 U32;
185  typedef LE64 U64;
186 
187  V unsigned get16(const void *p) C
188  { return get_le16(p); }
189  V unsigned get24(const void *p) C
190  { return get_le24(p); }
191  V unsigned get32(const void *p) C
192  { return get_le32(p); }
193  V upx_uint64_t get64(const void *p) C
194  { return get_le64(p); }
195 
196  V void set16(void *p, unsigned v) C
197  { set_le16(p, v); }
198  V void set24(void *p, unsigned v) C
199  { set_le24(p, v); }
200  V void set32(void *p, unsigned v) C
201  { set_le32(p, v); }
202  V void set64(void *p, upx_uint64_t v) C
203  { set_le64(p, v); }
204 
205  V unsigned get16_signed(const void *p) C
206  { return get_le16_signed(p); }
207  V unsigned get24_signed(const void *p) C
208  { return get_le24_signed(p); }
209  V unsigned get32_signed(const void *p) C
210  { return get_le32_signed(p); }
211  V upx_uint64_t get64_signed(const void *p) C
212  { return get_le64_signed(p); }
213 
214  S u16_compare(const void *a, const void *b) C
215  { return le16_compare(a, b); }
216  S u24_compare(const void *a, const void *b) C
217  { return le24_compare(a, b); }
218  S u32_compare(const void *a, const void *b) C
219  { return le32_compare(a, b); }
220  S u64_compare(const void *a, const void *b) C
221  { return le64_compare(a, b); }
222 
223  S u16_compare_signed(const void *a, const void *b) C
224  { return le16_compare_signed(a, b); }
225  S u24_compare_signed(const void *a, const void *b) C
226  { return le24_compare_signed(a, b); }
227  S u32_compare_signed(const void *a, const void *b) C
228  { return le32_compare_signed(a, b); }
229  S u64_compare_signed(const void *a, const void *b) C
230  { return le64_compare_signed(a, b); }
231 
232  static void compileTimeAssertions() {
233  COMPILE_TIME_ASSERT(sizeof(U16) == 2)
234  COMPILE_TIME_ASSERT(sizeof(U32) == 4)
235  COMPILE_TIME_ASSERT(sizeof(U64) == 8)
236  COMPILE_TIME_ASSERT_ALIGNED1(U16)
237  COMPILE_TIME_ASSERT_ALIGNED1(U32)
238  COMPILE_TIME_ASSERT_ALIGNED1(U64)
239  }
240 
241  // disable dynamic allocation
242  DISABLE_NEW_DELETE
243 };
244 
245 
246 #if (ACC_ABI_BIG_ENDIAN)
247 typedef BEPolicy HostPolicy;
248 #elif (ACC_ABI_LITTLE_ENDIAN)
249 typedef LEPolicy HostPolicy;
250 #else
251 # error "ACC_ABI_ENDIAN"
252 #endif
253 
254 
255 #if 0 /* UNUSED */
256 struct HostAlignedPolicy
257 {
258 #if defined(BELE_CTP)
259  enum { isBE = HostPolicy::isBE, isLE = HostPolicy::isLE };
260 #endif
261 
262  typedef upx_uint16_t U16;
263  typedef upx_uint32_t U32;
264  typedef upx_uint64_t U64;
265 
266  static void compileTimeAssertions() {
267  COMPILE_TIME_ASSERT(sizeof(U16) == 2)
268  COMPILE_TIME_ASSERT(sizeof(U32) == 4)
269  COMPILE_TIME_ASSERT(sizeof(U64) == 8)
270  }
271 };
272 #endif
273 
274 
275 #undef V
276 #undef S
277 #undef C
278 
279 /* vim:set ts=4 sw=4 et: */
Definition: bele.h:170
Definition: bele.h:170
Definition: bele_policy.h:169
Definition: bele.h:93
Definition: bele.h:93
Definition: bele_policy.h:92