123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109 |
- #include <stdbool.h>
- #include <stdint.h>
- #include <stddef.h>
- #include <stdint.h>
- #include "libbase64.h"
- #include "codecs.h"
- #if (__x86_64__ || __i386__ || _M_X86 || _M_X64)
- #define BASE64_X86
- #endif
- #ifdef BASE64_X86
- #ifdef _MSC_VER
- #include <intrin.h>
- #define __cpuid_count(__level, __count, __eax, __ebx, __ecx, __edx) \
- { \
- int info[4]; \
- __cpuidex(info, __level, __count); \
- __eax = info[0]; \
- __ebx = info[1]; \
- __ecx = info[2]; \
- __edx = info[3]; \
- }
- #define __cpuid(__level, __eax, __ebx, __ecx, __edx) \
- __cpuid_count(__level, 0, __eax, __ebx, __ecx, __edx)
- #else
- #include <cpuid.h>
- #endif
- #ifndef bit_AVX2
- #define bit_AVX2 (1 << 5)
- #endif
- #ifndef bit_SSSE3
- #define bit_SSSE3 (1 << 9)
- #endif
- #ifndef bit_SSE41
- #define bit_SSE41 (1 << 19)
- #endif
- #ifndef bit_SSE42
- #define bit_SSE42 (1 << 20)
- #endif
- #ifndef bit_AVX
- #define bit_AVX (1 << 28)
- #endif
- #define bit_XSAVE_XRSTORE (1 << 27)
- #ifndef _XCR_XFEATURE_ENABLED_MASK
- #define _XCR_XFEATURE_ENABLED_MASK 0
- #endif
- #define _XCR_XMM_AND_YMM_STATE_ENABLED_BY_OS 0x6
- #endif
- // Function declarations:
- #define BASE64_CODEC_FUNCS(arch) \
- BASE64_ENC_FUNCTION(arch); \
- BASE64_DEC_FUNCTION(arch); \
- BASE64_CODEC_FUNCS(plain)
- static bool
- codec_choose_forced (struct codec *codec, int flags)
- {
- // If the user wants to use a certain codec,
- // always allow it, even if the codec is a no-op.
- // For testing purposes.
- if (!(flags & 0xFF)) {
- return false;
- }
- codec->enc = base64_stream_encode_plain;
- codec->dec = base64_stream_decode_plain;
- return true;
- }
- static bool
- codec_choose_arm (struct codec *codec)
- {
- (void)codec;
- return false;
- }
- static bool
- codec_choose_x86 (struct codec *codec)
- {
- (void)codec;
- return false;
- }
- void
- codec_choose (struct codec *codec, int flags)
- {
- // User forced a codec:
- if (codec_choose_forced(codec, flags)) {
- return;
- }
- // Runtime feature detection:
- if (codec_choose_arm(codec)) {
- return;
- }
- if (codec_choose_x86(codec)) {
- return;
- }
- codec->enc = base64_stream_encode_plain;
- codec->dec = base64_stream_decode_plain;
- }
|