Back to home page

LXR

 
 

    


File indexing completed on 2025-05-11 08:24:10

0001 /*
0002  * Private includes and definitions for userspace use of XZ Embedded
0003  *
0004  * Author: Lasse Collin <lasse.collin@tukaani.org>
0005  *
0006  * This file has been put into the public domain.
0007  * You can do whatever you want with this file.
0008  */
0009 
0010 #ifndef XZ_CONFIG_H
0011 #define XZ_CONFIG_H
0012 
0013 /* Uncomment to enable CRC64 support. */
0014 /* #define XZ_USE_CRC64 */
0015 
0016 /* Uncomment as needed to enable BCJ filter decoders. */
0017 /* #define XZ_DEC_X86 */
0018 /* #define XZ_DEC_POWERPC */
0019 /* #define XZ_DEC_IA64 */
0020 /* #define XZ_DEC_ARM */
0021 /* #define XZ_DEC_ARMTHUMB */
0022 /* #define XZ_DEC_SPARC */
0023 
0024 /*
0025  * MSVC doesn't support modern C but XZ Embedded is mostly C89
0026  * so these are enough.
0027  */
0028 #ifdef _MSC_VER
0029 typedef unsigned char bool;
0030 #   define true 1
0031 #   define false 0
0032 #   define inline __inline
0033 #else
0034 #   include <stdbool.h>
0035 #endif
0036 
0037 #include <stdlib.h>
0038 #include <string.h>
0039 
0040 #include "xz.h"
0041 
0042 #define kmalloc(size, flags) malloc(size)
0043 #define kfree(ptr) free(ptr)
0044 #define vmalloc(size) malloc(size)
0045 #define vfree(ptr) free(ptr)
0046 
0047 #define memeq(a, b, size) (memcmp(a, b, size) == 0)
0048 #define memzero(buf, size) memset(buf, 0, size)
0049 
0050 #ifndef min
0051 #   define min(x, y) ((x) < (y) ? (x) : (y))
0052 #endif
0053 #define min_t(type, x, y) min(x, y)
0054 
0055 /*
0056  * Some functions have been marked with __always_inline to keep the
0057  * performance reasonable even when the compiler is optimizing for
0058  * small code size. You may be able to save a few bytes by #defining
0059  * __always_inline to plain inline, but don't complain if the code
0060  * becomes slow.
0061  *
0062  * NOTE: System headers on GNU/Linux may #define this macro already,
0063  * so if you want to change it, you need to #undef it first.
0064  */
0065 #ifndef __always_inline
0066 #   ifdef __GNUC__
0067 #       define __always_inline \
0068             inline __attribute__((__always_inline__))
0069 #   else
0070 #       define __always_inline inline
0071 #   endif
0072 #endif
0073 
0074 /* Inline functions to access unaligned unsigned 32-bit integers */
0075 #ifndef get_unaligned_le32
0076 static inline uint32_t get_unaligned_le32(const uint8_t *buf)
0077 {
0078     return (uint32_t)buf[0]
0079             | ((uint32_t)buf[1] << 8)
0080             | ((uint32_t)buf[2] << 16)
0081             | ((uint32_t)buf[3] << 24);
0082 }
0083 #endif
0084 
0085 #ifndef get_unaligned_be32
0086 static inline uint32_t get_unaligned_be32(const uint8_t *buf)
0087 {
0088     return (uint32_t)(buf[0] << 24)
0089             | ((uint32_t)buf[1] << 16)
0090             | ((uint32_t)buf[2] << 8)
0091             | (uint32_t)buf[3];
0092 }
0093 #endif
0094 
0095 #ifndef put_unaligned_le32
0096 static inline void put_unaligned_le32(uint32_t val, uint8_t *buf)
0097 {
0098     buf[0] = (uint8_t)val;
0099     buf[1] = (uint8_t)(val >> 8);
0100     buf[2] = (uint8_t)(val >> 16);
0101     buf[3] = (uint8_t)(val >> 24);
0102 }
0103 #endif
0104 
0105 #ifndef put_unaligned_be32
0106 static inline void put_unaligned_be32(uint32_t val, uint8_t *buf)
0107 {
0108     buf[0] = (uint8_t)(val >> 24);
0109     buf[1] = (uint8_t)(val >> 16);
0110     buf[2] = (uint8_t)(val >> 8);
0111     buf[3] = (uint8_t)val;
0112 }
0113 #endif
0114 
0115 /*
0116  * Use get_unaligned_le32() also for aligned access for simplicity. On
0117  * little endian systems, #define get_le32(ptr) (*(const uint32_t *)(ptr))
0118  * could save a few bytes in code size.
0119  */
0120 #ifndef get_le32
0121 #   define get_le32 get_unaligned_le32
0122 #endif
0123 
0124 #endif