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