1 /*
2  * Private includes and definitions for userspace use of XZ Embedded
3  *
4  * Author: Lasse Collin <lasse.collin@tukaani.org>
5  *
6  * This file has been put into the public domain.
7  * You can do whatever you want with this file.
8  */
9 
10 #ifndef XZ_CONFIG_H
11 #define XZ_CONFIG_H
12 
13 /* Uncomment to enable CRC64 support. */
14 /* #define XZ_USE_CRC64 */
15 
16 /* Uncomment as needed to enable BCJ filter decoders. */
17 /* #define XZ_DEC_X86 */
18 /* #define XZ_DEC_POWERPC */
19 /* #define XZ_DEC_IA64 */
20 /* #define XZ_DEC_ARM */
21 /* #define XZ_DEC_ARMTHUMB */
22 /* #define XZ_DEC_SPARC */
23 
24 /*
25  * MSVC doesn't support modern C but XZ Embedded is mostly C89
26  * so these are enough.
27  */
28 #ifdef _MSC_VER
29 typedef unsigned char bool;
30 #define true 1
31 #define false 0
32 #define inline __inline
33 #else
34 #include <stdbool.h>
35 #endif
36 
37 #include "xz.h"
38 
39 #define kmalloc(size, flags) ota_malloc(size)
40 #define kfree(ptr) ota_free(ptr)
41 #define vmalloc(size) ota_malloc(size)
42 #define vfree(ptr) ota_free(ptr)
43 
44 #define memeq(a, b, size) (memcmp(a, b, size) == 0)
45 #define memzero(buf, size) memset(buf, 0, size)
46 
47 #ifndef min
48 #define min(x, y) ((x) < (y) ? (x) : (y))
49 #endif
50 #define min_t(type, x, y) min(x, y)
51 
52 /*
53  * Some functions have been marked with __always_inline to keep the
54  * performance reasonable even when the compiler is optimizing for
55  * small code size. You may be able to save a few bytes by #defining
56  * __always_inline to plain inline, but don't complain if the code
57  * becomes slow.
58  *
59  * NOTE: System headers on GNU/Linux may #define this macro already,
60  * so if you want to change it, you need to #undef it first.
61  */
62 #ifndef __always_inline
63 #ifdef __GNUC__
64 #define __always_inline \
65 		inline __attribute__((__always_inline__))
66 #else
67 #define __always_inline inline
68 #endif
69 #endif
70 
71 /* Inline functions to access unaligned unsigned 32-bit integers */
72 #ifndef get_unaligned_le32
get_unaligned_le32(const uint8_t * buf)73 static inline uint32_t get_unaligned_le32(const uint8_t *buf)
74 {
75 	return (uint32_t)buf[0]
76 			| ((uint32_t)buf[1] << 8)
77 			| ((uint32_t)buf[2] << 16)
78 			| ((uint32_t)buf[3] << 24);
79 }
80 #endif
81 
82 #ifndef get_unaligned_be32
get_unaligned_be32(const uint8_t * buf)83 static inline uint32_t get_unaligned_be32(const uint8_t *buf)
84 {
85 	return (uint32_t)(buf[0] << 24)
86 			| ((uint32_t)buf[1] << 16)
87 			| ((uint32_t)buf[2] << 8)
88 			| (uint32_t)buf[3];
89 }
90 #endif
91 
92 #ifndef put_unaligned_le32
put_unaligned_le32(uint32_t val,uint8_t * buf)93 static inline void put_unaligned_le32(uint32_t val, uint8_t *buf)
94 {
95 	buf[0] = (uint8_t)val;
96 	buf[1] = (uint8_t)(val >> 8);
97 	buf[2] = (uint8_t)(val >> 16);
98 	buf[3] = (uint8_t)(val >> 24);
99 }
100 #endif
101 
102 #ifndef put_unaligned_be32
put_unaligned_be32(uint32_t val,uint8_t * buf)103 static inline void put_unaligned_be32(uint32_t val, uint8_t *buf)
104 {
105 	buf[0] = (uint8_t)(val >> 24);
106 	buf[1] = (uint8_t)(val >> 16);
107 	buf[2] = (uint8_t)(val >> 8);
108 	buf[3] = (uint8_t)val;
109 }
110 #endif
111 
112 /*
113  * Use get_unaligned_le32() also for aligned access for simplicity. On
114  * little endian systems, #define get_le32(ptr) (*(const uint32_t *)(ptr))
115  * could save a few bytes in code size.
116  */
117 #ifndef get_le32
118 #define get_le32 get_unaligned_le32
119 #endif
120 
121 #endif
122