1
0
mirror of https://github.com/fumiama/simple-crypto.git synced 2026-06-05 01:50:24 +08:00

适配MSVC和其它非GNUC编译器

This commit is contained in:
源文雨
2023-07-29 15:41:04 +08:00
parent c9595dc90f
commit 8a9df3041d
3 changed files with 62 additions and 10 deletions

49
binary.h Normal file
View File

@@ -0,0 +1,49 @@
#ifndef _SCRYPTO_BINARY_H
#define _SCRYPTO_BINARY_H
// https://github.com/TheSnowfield
#include <stdint.h>
inline uint32_t _swap_16(uint16_t value) {
#ifdef _MSC_VER
return _byteswap_ushort(value);
#elif __GNUC__
return __builtin_bswap16(value);
#else
return ((value & 0xff00) >> 8) |
((value & 0x00ff) << 8);
#endif
}
inline uint32_t _swap_32(uint32_t value) {
#ifdef _MSC_VER
return _byteswap_ulong(value);
#elif __GNUC__
return __builtin_bswap32(value);
#else
return ((value & 0xff000000) >> 24) |
((value & 0x00ff0000) >> 8) |
((value & 0x0000ff00) << 8) |
((value & 0x000000ff) << 24);
#endif
}
inline uint64_t _swap_64(uint64_t value) {
#ifdef _MSC_VER
return _byteswap_uint64(value);
#elif __GNUC__
return __builtin_bswap64(value);
#else
return ((value & 0xff00000000000000) >> 56) |
((value & 0x00ff000000000000) >> 40) |
((value & 0x0000ff0000000000) >> 24) |
((value & 0x000000ff00000000) >> 8) |
((value & 0x00000000ff000000) << 8) |
((value & 0x0000000000ff0000) << 24) |
((value & 0x000000000000ff00) << 40) |
((value & 0x00000000000000ff) << 56);
#endif
}
#endif /* _SCRYPTO_BINARY_H */

6
md5.c
View File

@@ -4,6 +4,8 @@
#include <string.h>
#include <stdint.h>
#include "binary.h"
// Constants are the integer part of the sines of integers (in radians) * 2^32.
const static uint32_t k[64] = {
0xd76aa478, 0xe8c7b756, 0x242070db, 0xc1bdceee ,
@@ -34,7 +36,7 @@ const static uint32_t r[] = {7, 12, 17, 22, 7, 12, 17, 22, 7, 12, 17, 22, 7, 12,
static void to_bytes(uint32_t val, uint8_t *bytes) {
#ifdef WORDS_BIGENDIAN
*(uint32_t*)bytes = __builtin_bswap32(val);
*(uint32_t*)bytes = _swap_32(val);
#else
*(uint32_t*)bytes = val;
#endif
@@ -42,7 +44,7 @@ static void to_bytes(uint32_t val, uint8_t *bytes) {
static uint32_t to_uint32(const uint8_t *bytes) {
#ifdef WORDS_BIGENDIAN
return __builtin_bswap32(*(uint32_t*)bytes);
return _swap_32(*(uint32_t*)bytes);
#else
return *(uint32_t*)bytes;
#endif

17
tea.c
View File

@@ -8,6 +8,7 @@
#include <machine/endian.h>
#endif
#include "simplecrypto.h"
#include "binary.h"
const static uint32_t qqsumtable[0x10] = {
0x9e3779b9,
@@ -43,7 +44,7 @@ int64_t tea_encrypt_qq(const TEA t, const uint8_t* src, int64_t srclen, uint8_t*
#ifdef WORDS_BIGENDIAN
uint64_t block = ((uint64_t*)dst)[i];
#else
uint64_t block = __builtin_bswap64(((uint64_t*)dst)[i]);
uint64_t block = _swap_64(((uint64_t*)dst)[i]);
#endif
holder = block ^ iv1;
iv1 = holder;
@@ -60,7 +61,7 @@ int64_t tea_encrypt_qq(const TEA t, const uint8_t* src, int64_t srclen, uint8_t*
#ifdef WORDS_BIGENDIAN
((uint64_t*)dst)[i] = iv1;
#else
((uint64_t*)dst)[i] = __builtin_bswap64(iv1);
((uint64_t*)dst)[i] = _swap_64(iv1);
#endif
}
return dstlen;
@@ -81,7 +82,7 @@ int64_t tea_encrypt(const TEA t, const uint32_t sumtable[0x10], const uint8_t* s
#ifdef WORDS_BIGENDIAN
uint64_t block = ((uint64_t*)dst)[i];
#else
uint64_t block = __builtin_bswap64(((uint64_t*)dst)[i]);
uint64_t block = _swap_64(((uint64_t*)dst)[i]);
#endif
holder = block ^ iv1;
iv1 = holder;
@@ -98,7 +99,7 @@ int64_t tea_encrypt(const TEA t, const uint32_t sumtable[0x10], const uint8_t* s
#ifdef WORDS_BIGENDIAN
((uint64_t*)dst)[i] = iv1;
#else
((uint64_t*)dst)[i] = __builtin_bswap64(iv1);
((uint64_t*)dst)[i] = _swap_64(iv1);
#endif
}
return dstlen;
@@ -171,7 +172,7 @@ uint8_t* tea_decrypt_qq(const TEA t, const uint8_t* src, int64_t srclen, uint8_t
#ifdef WORDS_BIGENDIAN
iv1 = ((uint64_t*)(src))[i];
#else
iv1 = __builtin_bswap64(((uint64_t*)(src))[i]);
iv1 = _swap_64(((uint64_t*)(src))[i]);
#endif
iv2 ^= iv1;
uint32_t v1 = iv2;
@@ -185,7 +186,7 @@ uint8_t* tea_decrypt_qq(const TEA t, const uint8_t* src, int64_t srclen, uint8_t
#ifdef WORDS_BIGENDIAN
((uint64_t*)dst)[i] = iv2^holder;
#else
((uint64_t*)dst)[i] = __builtin_bswap64(iv2^holder);
((uint64_t*)dst)[i] = _swap_64(iv2^holder);
#endif
holder = iv1;
}
@@ -202,7 +203,7 @@ uint8_t* tea_decrypt(const TEA t, const uint32_t sumtable[0x10], const uint8_t*
#ifdef WORDS_BIGENDIAN
iv1 = ((uint64_t*)(src))[i];
#else
iv1 = __builtin_bswap64(((uint64_t*)(src))[i]);
iv1 = _swap_64(((uint64_t*)(src))[i]);
#endif
iv2 ^= iv1;
uint32_t v1 = iv2;
@@ -216,7 +217,7 @@ uint8_t* tea_decrypt(const TEA t, const uint32_t sumtable[0x10], const uint8_t*
#ifdef WORDS_BIGENDIAN
((uint64_t*)dst)[i] = iv2^holder;
#else
((uint64_t*)dst)[i] = __builtin_bswap64(iv2^holder);
((uint64_t*)dst)[i] = _swap_64(iv2^holder);
#endif
holder = iv1;
}