|
| 1 | +#ifndef COMPRESSION_API_H |
| 2 | +#define COMPRESSION_API_H |
| 3 | + |
| 4 | +#ifdef COMPRESSION_API_EXPORT |
| 5 | + |
| 6 | +// For MSC |
| 7 | +#define _CRT_SECURE_NO_WARNINGS |
| 8 | +#define _CRT_NON_CONFORMING_SWPRINTFS |
| 9 | + |
| 10 | +// For GCC |
| 11 | +#define __STDC_LIMIT_MACROS |
| 12 | + |
| 13 | +#include <stdlib.h> |
| 14 | +#include <string.h> |
| 15 | +#include <stdint.h> |
| 16 | +#include <assert.h> |
| 17 | +#include <errno.h> |
| 18 | + |
| 19 | +// Check that it is 8 bits to the byte |
| 20 | +#if CHAR_BIT != 8 |
| 21 | + #error Unsupported char size |
| 22 | +#endif |
| 23 | + |
| 24 | +// Determine the endianness of the compilation, however this isn't very accurate |
| 25 | +// It would be much better to define LITTLE_ENDIAN, BIG_ENDIAN, or PDP_ENDIAN yourself |
| 26 | +// LITTLE_ENDIAN is what the program is developed for and tested with |
| 27 | +// BIG_ENDIAN and PDP_ENDIAN are untested |
| 28 | +#if !defined(LITTLE_ENDIAN) && !defined(BIG_ENDIAN) && !defined(PDP_ENDIAN) && !defined(_MSC_VER) && !defined(_WIN32) && !defined(__LITTLE_ENDIAN__) && !defined(__IEEE_LITTLE_ENDIAN) |
| 29 | + #if defined(WORDS_BIGENDIAN) || defined(__BIG_ENDIAN__) || defined(__IEEE_BIG_ENDIAN) |
| 30 | + #define BIG_ENDIAN |
| 31 | + #else |
| 32 | + #include <endian.h> |
| 33 | + #if (defined(__PDP_ENDIAN) && !defined(__LITTLE_ENDIAN)) || __BYTE_ORDER == __PDP_ENDIAN |
| 34 | + #define PDP_ENDIAN |
| 35 | + #elif (defined(__BIG_ENDIAN) && !defined(__LITTLE_ENDIAN)) || __BYTE_ORDER == __BIG_ENDIAN |
| 36 | + #define BIG_ENDIAN |
| 37 | + #endif |
| 38 | + #endif |
| 39 | +#endif |
| 40 | + |
| 41 | +#if defined(BIG_ENDIAN) |
| 42 | + #define GET_UINT16(x) ((x)[0]|((x)[1]<<8)) |
| 43 | + #define GET_UINT32(x) ((x)[0]|((x)[1]<<8)|((x)[2]<<16)|((x)[3]<<24)) |
| 44 | + #define SET_UINT16(x,val) (((byte*)(x))[0]=(byte)(val), ((byte*)(x))[1]=(byte)((val) >> 8)) |
| 45 | + #define SET_UINT32(x,val) (((byte*)(x))[0]=(byte)(val), ((byte*)(x))[1]=(byte)((val) >> 8), ((byte*)(x))[2]=(byte)((val) >> 16), ((byte*)(x))[3]=(byte)((val) >> 24)) |
| 46 | +#elif defined(PDP_ENDIAN) // for 16-bit ints its the same as little-endian |
| 47 | + #define GET_UINT16(x) (*(const uint16_t*)(x)) |
| 48 | + #define GET_UINT32(x) (*(const uint16_t*)(x)|(*(const uint16_t*)((x)+2)<<16)) |
| 49 | + #define SET_UINT16(x,val) (*(uint16_t*)(x) = (uint16_t)(val)) |
| 50 | + #define SET_UINT32(x,val) (*(uint16_t*)(x) = (uint16_t)(val), *(((uint16_t*)(x))+1) = (uint16_t)((val) >> 16)) |
| 51 | +#else |
| 52 | + #ifndef LITTLE_ENDIAN |
| 53 | + #define LITTLE_ENDIAN |
| 54 | + #endif |
| 55 | + #define GET_UINT16(x) (*(const uint16_t*)(x)) |
| 56 | + #define GET_UINT32(x) (*(const uint32_t*)(x)) |
| 57 | + #define SET_UINT16(x,val) (*(uint16_t*)(x) = (uint16_t)(val)) |
| 58 | + #define SET_UINT32(x,val) (*(uint32_t*)(x) = (uint32_t)(val)) |
| 59 | +#endif |
| 60 | + |
| 61 | +// Determine the number of bits used by pointers |
| 62 | +#ifndef PNTR_BITS |
| 63 | + #if SIZE_MAX == UINT64_MAX |
| 64 | + #define PNTR_BITS 64 |
| 65 | + #elif SIZE_MAX == UINT32_MAX |
| 66 | + #define PNTR_BITS 32 |
| 67 | + #elif SIZE_MAX == UINT16_MAX |
| 68 | + #define PNTR_BITS 16 |
| 69 | + #else |
| 70 | + #error You must define PNTR_BITS to be the number of bits used for pointers |
| 71 | + #endif |
| 72 | +#endif |
| 73 | + |
| 74 | +// Get ROTL function |
| 75 | +#if defined(_MSC_VER) && (defined(_M_IX86) || defined(_M_AMD64) || defined(_M_X64)) |
| 76 | + #include <stdlib.h> |
| 77 | + #pragma intrinsic(_rotl) |
| 78 | + #define ROTL(x, bits) _rotl(x, bits) |
| 79 | +#else |
| 80 | + #define ROTL(x, bits) (((x) << (bits)) | ((x) >> (sizeof(x) - (bits)))) |
| 81 | +#endif |
| 82 | + |
| 83 | + |
| 84 | +// Error message system (mainly for debugging) |
| 85 | +#ifdef _DEBUG |
| 86 | + #define PRINT_WARNINGS |
| 87 | + #define PRINT_ERRORS |
| 88 | +#endif |
| 89 | + |
| 90 | +#if defined(PRINT_WARNINGS) || defined(PRINT_ERRORS) |
| 91 | + #include <stdio.h> |
| 92 | +#endif |
| 93 | + |
| 94 | +#ifdef PRINT_WARNINGS |
| 95 | + #define PRINT_WARNING(...) fprintf(stderr, __VA_ARGS__) |
| 96 | +#else |
| 97 | + #define PRINT_WARNING(...) |
| 98 | +#endif |
| 99 | + |
| 100 | +#ifdef PRINT_ERRORS |
| 101 | + #define PRINT_ERROR(...) fprintf(stderr, __VA_ARGS__) |
| 102 | +#else |
| 103 | + #define PRINT_ERROR(...) |
| 104 | +#endif |
| 105 | + |
| 106 | +// If compiling as C than make sure we have access to some C++ keywords |
| 107 | +#ifndef __cplusplus |
| 108 | + typedef uint_fast8_t bool; |
| 109 | + #define true 1 |
| 110 | + #define false 0 |
| 111 | + #define inline __inline |
| 112 | +#endif |
| 113 | + |
| 114 | +// DLL linkage |
| 115 | +#ifdef COMPRESSION_API_DLL |
| 116 | + #define COMPAPI __declspec(dllexport) |
| 117 | +#else |
| 118 | + #define COMPAPI |
| 119 | +#endif |
| 120 | + |
| 121 | + |
| 122 | +#else // Importing from DLL or LIB |
| 123 | + |
| 124 | +#include <stdint.h> |
| 125 | +#ifdef COMPRESSION_API_DLL |
| 126 | + #define COMPAPI __declspec(dllimport) |
| 127 | +#else |
| 128 | + #define COMPAPI |
| 129 | +#endif |
| 130 | + |
| 131 | +#endif |
| 132 | + |
| 133 | +#ifndef __cplusplus |
| 134 | + #define EXTERN_C |
| 135 | +#else |
| 136 | + #define EXTERN_C extern "C" |
| 137 | +#endif |
| 138 | + |
| 139 | +// Custom errno values |
| 140 | +#define E_ILLEGAL_FORMAT 0x101 |
| 141 | +#define E_INSUFFICIENT_BUFFER 0x102 |
| 142 | +#define E_INVALID_DATA 0x103 |
| 143 | + |
| 144 | +// Define types used |
| 145 | +typedef uint8_t byte; |
| 146 | +typedef byte* bytes; |
| 147 | +typedef const byte const_byte; |
| 148 | +typedef const_byte* const_bytes; |
| 149 | + |
| 150 | +#endif |
0 commit comments