dqn: Rewrite crappy arena and string implementations

This commit is contained in:
doyle 2022-08-06 16:43:20 +10:00
parent 1d31d7f270
commit 4056e19727
4 changed files with 1052 additions and 1240 deletions

View File

@ -4,7 +4,6 @@ set code_dir=%~dp0
if not exist Build mkdir Build if not exist Build mkdir Build
pushd Build pushd Build
REM ------------------------------------------------------------------------
REM Flags REM Flags
REM ------------------------------------------------------------------------ REM ------------------------------------------------------------------------
REM MT Static CRT REM MT Static CRT
@ -20,7 +19,6 @@ pushd Build
set msvc_flags=-fsanitize=address -D STBSP__ASAN=__declspec(no_sanitize_address) set msvc_flags=-fsanitize=address -D STBSP__ASAN=__declspec(no_sanitize_address)
set clang_flags=-fsanitize=address -fsanitize=undefined set clang_flags=-fsanitize=address -fsanitize=undefined
REM ------------------------------------------------------------------------
REM Compiler: MSVC cl REM Compiler: MSVC cl
REM ------------------------------------------------------------------------ REM ------------------------------------------------------------------------
where /q cl || ( where /q cl || (
@ -33,7 +31,6 @@ pushd Build
cl %compile_flags% %msvc_flags% %code_dir%Dqn_Tests.cpp %link_flags% cl %compile_flags% %msvc_flags% %code_dir%Dqn_Tests.cpp %link_flags%
popd popd
REM ------------------------------------------------------------------------
REM Compiler: clang-cl REM Compiler: clang-cl
REM ------------------------------------------------------------------------ REM ------------------------------------------------------------------------
where /q clang-cl || ( where /q clang-cl || (

2120
dqn.h

File diff suppressed because it is too large Load Diff

View File

@ -641,7 +641,8 @@ Dqn_Jsmn Dqn_JsmnInitWithArenaJSON(Dqn_JsmnString json, Dqn_Arena *arena)
Dqn_Jsmn Dqn_JsmnInitWithArenaJSONFile(Dqn_JsmnString file, Dqn_Arena *arena) Dqn_Jsmn Dqn_JsmnInitWithArenaJSONFile(Dqn_JsmnString file, Dqn_Arena *arena)
{ {
Dqn_String json = Dqn_FileArenaReadToString(file.data, file.size, arena); Dqn_String json = Dqn_FileArenaReadToString(file.data, file.size, arena);
Dqn_Jsmn result = Dqn_JsmnInitWithArenaJSON({{json.str}, (int)json.size}, arena); Dqn_JsmnString string = {json.str, (int)json.size};
Dqn_Jsmn result = Dqn_JsmnInitWithArenaJSON(string, arena);
return result; return result;
} }
#endif // DQN_IMPLEMENTATION #endif // DQN_IMPLEMENTATION
@ -654,7 +655,7 @@ int Dqn_JsmnTokenArraySize(jsmntok_t token)
Dqn_JsmnString Dqn_JsmnTokenString(jsmntok_t token, Dqn_JsmnString json) Dqn_JsmnString Dqn_JsmnTokenString(jsmntok_t token, Dqn_JsmnString json)
{ {
Dqn_JsmnString result = {{json.data + token.start}, token.end - token.start}; Dqn_JsmnString result = DQN_JSMN_CLITERAL(Dqn_JsmnString){json.data + token.start, token.end - token.start};
return result; return result;
} }
@ -670,7 +671,7 @@ bool Dqn_JsmnTokenBool(jsmntok_t token, Dqn_JsmnString json)
Dqn_JsmnU64 Dqn_JsmnTokenU64(jsmntok_t token, Dqn_JsmnString json) Dqn_JsmnU64 Dqn_JsmnTokenU64(jsmntok_t token, Dqn_JsmnString json)
{ {
DQN_JSMN_ASSERT(token.start < json.size); DQN_JSMN_ASSERT(token.start < json.size);
Dqn_JsmnString string = {{json.data + token.start}, token.end - token.start}; Dqn_JsmnString string = DQN_JSMN_CLITERAL(Dqn_JsmnString){json.data + token.start, token.end - token.start};
Dqn_JsmnU64 result = Dqn_JsmnStringToU64(string); Dqn_JsmnU64 result = Dqn_JsmnStringToU64(string);
return result; return result;
} }
@ -725,7 +726,7 @@ Dqn_JsmnString Dqn_JsmnIteratorKey(Dqn_JsmnIterator *it)
{ {
Dqn_JsmnString result = DQN_JSMN_ZERO_INIT; Dqn_JsmnString result = DQN_JSMN_ZERO_INIT;
if (it && it->key) if (it && it->key)
result = DQN_JSMN_CLITERAL(Dqn_JsmnString){{it->json.data + it->key->start}, it->key->end - it->key->start}; result = DQN_JSMN_CLITERAL(Dqn_JsmnString){it->json.data + it->key->start, it->key->end - it->key->start};
return result; return result;
} }
@ -813,7 +814,7 @@ Dqn_JsmnString Dqn_JsmnIteratorString(Dqn_JsmnIterator const *it)
{ {
Dqn_JsmnString result = DQN_JSMN_ZERO_INIT; Dqn_JsmnString result = DQN_JSMN_ZERO_INIT;
if (it->value && it->json.data) if (it->value && it->json.data)
result = DQN_JSMN_CLITERAL(Dqn_JsmnString){{it->json.data + it->value->start}, it->value->end - it->value->start}; result = DQN_JSMN_CLITERAL(Dqn_JsmnString){it->json.data + it->value->start, it->value->end - it->value->start};
return result; return result;
} }

View File

@ -101,10 +101,10 @@ typedef struct Dqn_KeccakBytes32 { char data[32]; } Dqn_KeccakBytes32; // 256 b
typedef struct Dqn_KeccakBytes48 { char data[48]; } Dqn_KeccakBytes48; // 384 bit typedef struct Dqn_KeccakBytes48 { char data[48]; } Dqn_KeccakBytes48; // 384 bit
typedef struct Dqn_KeccakBytes64 { char data[64]; } Dqn_KeccakBytes64; // 512 bit typedef struct Dqn_KeccakBytes64 { char data[64]; } Dqn_KeccakBytes64; // 512 bit
typedef struct Dqn_KeccakString56 { char str[(sizeof(Dqn_KeccakBytes28) * 2) + 1]; } Dqn_KeccakString56; typedef struct Dqn_KeccakString56 { char data[(sizeof(Dqn_KeccakBytes28) * 2) + 1]; } Dqn_KeccakString56;
typedef struct Dqn_KeccakString64 { char str[(sizeof(Dqn_KeccakBytes32) * 2) + 1]; } Dqn_KeccakString64; typedef struct Dqn_KeccakString64 { char data[(sizeof(Dqn_KeccakBytes32) * 2) + 1]; } Dqn_KeccakString64;
typedef struct Dqn_KeccakString96 { char str[(sizeof(Dqn_KeccakBytes48) * 2) + 1]; } Dqn_KeccakString96; typedef struct Dqn_KeccakString96 { char data[(sizeof(Dqn_KeccakBytes48) * 2) + 1]; } Dqn_KeccakString96;
typedef struct Dqn_KeccakString128 { char str[(sizeof(Dqn_KeccakBytes64) * 2) + 1]; } Dqn_KeccakString128; typedef struct Dqn_KeccakString128 { char data[(sizeof(Dqn_KeccakBytes64) * 2) + 1]; } Dqn_KeccakString128;
#define DQN_KECCAK_LANE_SIZE_U64 5 #define DQN_KECCAK_LANE_SIZE_U64 5
typedef struct Dqn_KeccakState typedef struct Dqn_KeccakState
@ -350,7 +350,11 @@ Dqn_KeccakState Dqn_KeccakSHA3Init(bool sha3, int hash_size_bits)
char const KECCAK_DELIMITED_SUFFIX = 0x01; char const KECCAK_DELIMITED_SUFFIX = 0x01;
int const bitrate = 1600 - (hash_size_bits * 2); int const bitrate = 1600 - (hash_size_bits * 2);
#if defined(__cplusplus)
Dqn_KeccakState result = {}; Dqn_KeccakState result = {};
#else
Dqn_KeccakState result = {0};
#endif
result.hash_size_bits = hash_size_bits; result.hash_size_bits = hash_size_bits;
result.absorb_size = bitrate / 8; result.absorb_size = bitrate / 8;
result.delimited_suffix = sha3 ? SHA3_DELIMITED_SUFFIX : KECCAK_DELIMITED_SUFFIX; result.delimited_suffix = sha3 ? SHA3_DELIMITED_SUFFIX : KECCAK_DELIMITED_SUFFIX;
@ -511,7 +515,7 @@ Dqn_KeccakBytes64 Dqn_Keccak512ToBytes64(void *bytes, Dqn_KeccakU64 bytes_size)
Dqn_KeccakBytes28 Dqn_SHA3_224StringToBytes28(Dqn_String string) Dqn_KeccakBytes28 Dqn_SHA3_224StringToBytes28(Dqn_String string)
{ {
Dqn_KeccakBytes28 result; Dqn_KeccakBytes28 result;
Dqn_SHA3_224(string.str, string.size, result.data, sizeof(result)); Dqn_SHA3_224(string.data, string.size, result.data, sizeof(result));
return result; return result;
} }
@ -525,7 +529,7 @@ Dqn_KeccakBytes28 Dqn_SHA3_224_U8ArrayToBytes28(Dqn_Array<Dqn_KeccakU8> array)
Dqn_KeccakBytes32 Dqn_SHA3_256StringToBytes32(Dqn_String string) Dqn_KeccakBytes32 Dqn_SHA3_256StringToBytes32(Dqn_String string)
{ {
Dqn_KeccakBytes32 result; Dqn_KeccakBytes32 result;
Dqn_SHA3_256(string.str, string.size, result.data, sizeof(result)); Dqn_SHA3_256(string.data, string.size, result.data, sizeof(result));
return result; return result;
} }
@ -539,7 +543,7 @@ Dqn_KeccakBytes32 Dqn_SHA3_256_U8ArrayToBytes32(Dqn_Array<Dqn_KeccakU8> array)
Dqn_KeccakBytes48 Dqn_SHA3_384StringToBytes48(Dqn_String string) Dqn_KeccakBytes48 Dqn_SHA3_384StringToBytes48(Dqn_String string)
{ {
Dqn_KeccakBytes48 result; Dqn_KeccakBytes48 result;
Dqn_SHA3_384(string.str, string.size, result.data, sizeof(result)); Dqn_SHA3_384(string.data, string.size, result.data, sizeof(result));
return result; return result;
} }
@ -553,7 +557,7 @@ Dqn_KeccakBytes48 Dqn_SHA3_384_U8ArrayToBytes48(Dqn_Array<Dqn_KeccakU8> array)
Dqn_KeccakBytes64 Dqn_SHA3_512StringToBytes64(Dqn_String string) Dqn_KeccakBytes64 Dqn_SHA3_512StringToBytes64(Dqn_String string)
{ {
Dqn_KeccakBytes64 result; Dqn_KeccakBytes64 result;
Dqn_SHA3_512(string.str, string.size, result.data, sizeof(result)); Dqn_SHA3_512(string.data, string.size, result.data, sizeof(result));
return result; return result;
} }
@ -572,7 +576,7 @@ Dqn_KeccakBytes64 Dqn_SHA3_512_U8ArrayToBytes64(Dqn_Array<Dqn_KeccakU8> array)
Dqn_KeccakBytes28 Dqn_Keccak224StringToBytes28(Dqn_String string) Dqn_KeccakBytes28 Dqn_Keccak224StringToBytes28(Dqn_String string)
{ {
Dqn_KeccakBytes28 result; Dqn_KeccakBytes28 result;
Dqn_Keccak224(string.str, string.size, result.data, sizeof(result)); Dqn_Keccak224(string.data, string.size, result.data, sizeof(result));
return result; return result;
} }
@ -586,7 +590,7 @@ Dqn_KeccakBytes28 Dqn_Keccak224_U8ArrayToBytes28(Dqn_Array<Dqn_KeccakU8> array)
Dqn_KeccakBytes32 Dqn_Keccak256StringToBytes32(Dqn_String string) Dqn_KeccakBytes32 Dqn_Keccak256StringToBytes32(Dqn_String string)
{ {
Dqn_KeccakBytes32 result; Dqn_KeccakBytes32 result;
Dqn_Keccak256(string.str, string.size, result.data, sizeof(result)); Dqn_Keccak256(string.data, string.size, result.data, sizeof(result));
return result; return result;
} }
@ -601,7 +605,7 @@ Dqn_KeccakBytes32 Dqn_Keccak256_U8ArrayToBytes32(Dqn_Array<Dqn_KeccakU8> array)
Dqn_KeccakBytes48 Dqn_Keccak384StringToBytes48(Dqn_String string) Dqn_KeccakBytes48 Dqn_Keccak384StringToBytes48(Dqn_String string)
{ {
Dqn_KeccakBytes48 result; Dqn_KeccakBytes48 result;
Dqn_Keccak384(string.str, string.size, result.data, sizeof(result)); Dqn_Keccak384(string.data, string.size, result.data, sizeof(result));
return result; return result;
} }
@ -615,7 +619,7 @@ Dqn_KeccakBytes48 Dqn_Keccak384_U8ArrayToBytes48(Dqn_Array<Dqn_KeccakU8> array)
Dqn_KeccakBytes64 Dqn_Keccak512StringToBytes64(Dqn_String string) Dqn_KeccakBytes64 Dqn_Keccak512StringToBytes64(Dqn_String string)
{ {
Dqn_KeccakBytes64 result; Dqn_KeccakBytes64 result;
Dqn_Keccak512(string.str, string.size, result.data, sizeof(result)); Dqn_Keccak512(string.data, string.size, result.data, sizeof(result));
return result; return result;
} }
@ -651,32 +655,32 @@ void Dqn_KeccakBytesToHex(void const *src, Dqn_KeccakU64 src_size, char *dest, D
Dqn_KeccakString56 Dqn_KeccakBytes28ToHex(Dqn_KeccakBytes28 const *bytes) Dqn_KeccakString56 Dqn_KeccakBytes28ToHex(Dqn_KeccakBytes28 const *bytes)
{ {
Dqn_KeccakString56 result; Dqn_KeccakString56 result;
Dqn_KeccakBytesToHex(bytes->data, sizeof(bytes->data), result.str, sizeof(result.str)); Dqn_KeccakBytesToHex(bytes->data, sizeof(bytes->data), result.data, sizeof(result.data));
result.str[sizeof(result.str) - 1] = 0; result.data[sizeof(result.data) - 1] = 0;
return result; return result;
} }
Dqn_KeccakString64 Dqn_KeccakBytes32ToHex(Dqn_KeccakBytes32 const *bytes) Dqn_KeccakString64 Dqn_KeccakBytes32ToHex(Dqn_KeccakBytes32 const *bytes)
{ {
Dqn_KeccakString64 result; Dqn_KeccakString64 result;
Dqn_KeccakBytesToHex(bytes->data, sizeof(bytes->data), result.str, sizeof(result.str)); Dqn_KeccakBytesToHex(bytes->data, sizeof(bytes->data), result.data, sizeof(result.data));
result.str[sizeof(result.str) - 1] = 0; result.data[sizeof(result.data) - 1] = 0;
return result; return result;
} }
Dqn_KeccakString96 Dqn_KeccakBytes48ToHex(Dqn_KeccakBytes48 const *bytes) Dqn_KeccakString96 Dqn_KeccakBytes48ToHex(Dqn_KeccakBytes48 const *bytes)
{ {
Dqn_KeccakString96 result; Dqn_KeccakString96 result;
Dqn_KeccakBytesToHex(bytes->data, sizeof(bytes->data), result.str, sizeof(result.str)); Dqn_KeccakBytesToHex(bytes->data, sizeof(bytes->data), result.data, sizeof(result.data));
result.str[sizeof(result.str) - 1] = 0; result.data[sizeof(result.data) - 1] = 0;
return result; return result;
} }
Dqn_KeccakString128 Dqn_KeccakBytes64ToHex(Dqn_KeccakBytes64 const *bytes) Dqn_KeccakString128 Dqn_KeccakBytes64ToHex(Dqn_KeccakBytes64 const *bytes)
{ {
Dqn_KeccakString128 result; Dqn_KeccakString128 result;
Dqn_KeccakBytesToHex(bytes->data, sizeof(bytes->data), result.str, sizeof(result.str)); Dqn_KeccakBytesToHex(bytes->data, sizeof(bytes->data), result.data, sizeof(result.data));
result.str[sizeof(result.str) - 1] = 0; result.data[sizeof(result.data) - 1] = 0;
return result; return result;
} }
@ -712,7 +716,7 @@ Dqn_KeccakBytes32 Dqn_KeccakHex64StringToBytes(Dqn_String hex)
{ {
DQN_KECCAK_ASSERT(hex.size == 64); DQN_KECCAK_ASSERT(hex.size == 64);
Dqn_KeccakBytes32 result; Dqn_KeccakBytes32 result;
Dqn_HexToBytes(hex.str, hex.size, result.data, sizeof(result)); Dqn_HexToBytes(hex.data, hex.size, result.data, sizeof(result));
return result; return result;
} }
#endif // DQN_H && DQN_WITH_HEX #endif // DQN_H && DQN_WITH_HEX