blob: 29f3d29019a106e259fad13adc9fed76af443e3c [file] [log] [blame]
Denys Vlasenko602ce692010-05-30 03:35:18 +02001/*
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 as needed to enable BCJ filter decoders. */
14/* #define XZ_DEC_X86 */
15/* #define XZ_DEC_POWERPC */
16/* #define XZ_DEC_IA64 */
17/* #define XZ_DEC_ARM */
18/* #define XZ_DEC_ARMTHUMB */
19/* #define XZ_DEC_SPARC */
20
21#include <stdbool.h>
22#include <stdlib.h>
23#include <string.h>
24
25#include "xz.h"
26
27#define kmalloc(size, flags) malloc(size)
28#define kfree(ptr) free(ptr)
29#define vmalloc(size) malloc(size)
30#define vfree(ptr) free(ptr)
31
32#define memeq(a, b, size) (memcmp(a, b, size) == 0)
33#define memzero(buf, size) memset(buf, 0, size)
34
Denys Vlasenko134d0eb2010-06-19 20:07:23 +020035#undef min
36#undef min_t
Denys Vlasenko602ce692010-05-30 03:35:18 +020037#define min(x, y) ((x) < (y) ? (x) : (y))
38#define min_t(type, x, y) min(x, y)
39
40/*
41 * Some functions have been marked with __always_inline to keep the
42 * performance reasonable even when the compiler is optimizing for
43 * small code size. You may be able to save a few bytes by #defining
44 * __always_inline to plain inline, but don't complain if the code
45 * becomes slow.
46 *
47 * NOTE: System headers on GNU/Linux may #define this macro already,
Denys Vlasenko716f3f62010-06-01 14:41:39 +020048 * so if you want to change it, you need to #undef it first.
Denys Vlasenko602ce692010-05-30 03:35:18 +020049 */
maxwen27116ba2015-08-14 21:41:28 +020050#ifdef __BIONIC__
51#undef __always_inline
52#endif
53
Denys Vlasenko602ce692010-05-30 03:35:18 +020054#ifndef __always_inline
55# ifdef __GNUC__
56# define __always_inline \
57 inline __attribute__((__always_inline__))
58# else
59# define __always_inline inline
60# endif
61#endif
62
63/*
64 * Some functions are marked to never be inlined to reduce stack usage.
65 * If you don't care about stack usage, you may want to modify this so
66 * that noinline_for_stack is #defined to be empty even when using GCC.
67 * Doing so may save a few bytes in binary size.
68 */
69#ifndef noinline_for_stack
70# ifdef __GNUC__
71# define noinline_for_stack __attribute__((__noinline__))
72# else
73# define noinline_for_stack
74# endif
75#endif
76
77/* Inline functions to access unaligned unsigned 32-bit integers */
78#ifndef get_unaligned_le32
79static inline uint32_t XZ_FUNC 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 XZ_FUNC 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 XZ_FUNC 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 XZ_FUNC 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 */
Denys Vlasenko716f3f62010-06-01 14:41:39 +0200123#ifndef get_le32
124# define get_le32 get_unaligned_le32
125#endif
Denys Vlasenko602ce692010-05-30 03:35:18 +0200126
127#endif