/* Generated by Bux Compiler (LIR backend) */ #include #include #include #include #include typedef struct TaskHandle TaskHandle; typedef struct Mutex Mutex; typedef struct RwLock RwLock; typedef struct JsonValue JsonValue; typedef struct JsonParser JsonParser; typedef struct StringBuilder StringBuilder; typedef struct Channel_int Channel_int; typedef struct Channel_float64 Channel_float64; typedef struct Drop_FatPtr Drop_FatPtr; /* Extern function declarations */ extern int bux_dir_exists(const char* path); extern int bux_mkdir_if_needed(const char* path); extern const char** bux_list_dir(const char* dir, const char* ext, int* out_count); extern void PrintLine(const char* s); extern void Print(const char* s); extern void PrintInt(int n); extern void PrintInt64(int64_t n); extern void PrintFloat(double f); extern void PrintBool(bool b); extern const char* ReadLine(void); extern const char* bux_read_file(const char* path); extern int bux_write_file(const char* path, const char* content); extern int bux_file_exists(const char* path); extern double bux_sqrt(double x); extern double bux_pow(double x, double y); extern int64_t bux_abs_i64(int64_t x); extern double bux_abs_f64(double x); extern int64_t bux_min_i64(int64_t a, int64_t b); extern int64_t bux_max_i64(int64_t a, int64_t b); extern double bux_min_f64(double a, double b); extern double bux_max_f64(double a, double b); extern void* bux_alloc(unsigned int size); extern void* bux_realloc(void* ptr, unsigned int size); extern void bux_free(void* ptr); extern int bux_mem_eq(void* a, void* b, unsigned int size); extern const char* bux_path_join(const char* a, const char* b); extern const char* bux_path_parent(const char* path); extern const char* bux_path_ext(const char* path); extern void bux_task_init(int num_workers); extern void* bux_task_spawn(void* fn, void* arg); extern void bux_task_join(void* handle); extern void bux_task_sleep(int64_t ms); extern void bux_task_yield(void); extern int bux_task_current_id(void); extern void bux_task_shutdown(void); extern int bux_argc(void); extern const char* bux_argv(int index); extern const char* bux_getenv(const char* name); extern int bux_setenv(const char* name, const char* value); extern const char* bux_getcwd(void); extern int bux_chdir(const char* path); extern int64_t bux_time_ms(void); extern int64_t bux_time_us(void); extern void bux_sleep_ms(int64_t ms); extern int bux_process_run(const char* cmd); extern const char* bux_process_output(const char* cmd); extern int bux_socket_create(void); extern int bux_socket_reuse(int fd); extern int bux_socket_bind(int fd, const char* addr, int port); extern int bux_socket_listen(int fd, int backlog); extern int bux_socket_accept(int fd); extern int bux_socket_connect(int fd, const char* addr, int port); extern int bux_socket_send(int fd, const char* data, int len); extern const char* bux_socket_recv(int fd, int maxLen); extern int bux_socket_close(int fd); extern const char* bux_socket_error(void); extern unsigned int bux_strlen(const char* s); extern int64_t bux_str_to_int(const char* s); extern void* bux_alloc(unsigned int size); extern void bux_sha256(const char* data, int len, void* out); extern void bux_hmac_sha256(const char* key, int keylen, const char* msg, int msglen, void* out); extern int bux_random_bytes(void* buf, int len); extern const char* bux_base64_encode(const char* data, int len); extern const char* bux_base64_decode(const char* data, int len, int* outlen); extern const char* bux_bytes_to_hex(void* data, int len); extern void* bux_mutex_new(void); extern void bux_mutex_lock(void* handle); extern void bux_mutex_unlock(void* handle); extern void bux_mutex_free(void* handle); extern void* bux_rwlock_new(void); extern void bux_rwlock_rdlock(void* handle); extern void bux_rwlock_wrlock(void* handle); extern void bux_rwlock_unlock(void* handle); extern void bux_rwlock_free(void* handle); extern void bux_exit(int code); extern void bux_assert(int cond, const char* file, int line, const char* expr); extern JsonValue JsonParser_ParseValue(JsonParser* p); extern unsigned int bux_strlen(const char* s); extern int bux_strcmp(const char* a, const char* b); extern int bux_strncmp(const char* a, const char* b, unsigned int n); extern char* bux_strcpy(char* dest, const char* src); extern char* bux_strcat(char* dest, const char* src); extern char* bux_strncpy(char* dest, const char* src, unsigned int n); extern const char* bux_strstr(const char* haystack, const char* needle); extern int bux_str_contains(const char* haystack, const char* needle); extern unsigned int bux_str_offset(const char* pos, const char* base); extern int bux_str_is_null(const char* s); extern const char* bux_str_slice(const char* s, unsigned int start, unsigned int len); extern const char* bux_str_trim_left(const char* s); extern const char* bux_str_trim_right(const char* s); extern const char* bux_str_trim(const char* s); extern const char* bux_int_to_str(int64_t n); extern int64_t bux_str_to_int(const char* s); extern void* bux_sb_new(unsigned int initial_cap); extern void bux_sb_append(void* sb, const char* s); extern void bux_sb_append_int(void* sb, int64_t n); extern void bux_sb_append_float(void* sb, double f); extern void bux_sb_append_char(void* sb, char c); extern const char* bux_sb_build(void* sb); extern void bux_sb_free(void* sb); extern unsigned int bux_str_split_count(const char* s, const char* delim); extern const char* bux_str_split_part(const char* s, const char* delim, unsigned int index); extern const char* bux_str_join2(const char* a, const char* b, const char* sep); extern const char* bux_float_to_string(double f); extern const char* bux_str_format(const char* pattern, const char* a0, const char* a1, const char* a2, const char* a3, const char* a4, const char* a5, const char* a6, const char* a7); extern double bux_str_to_float(const char* s); extern void bux_bounds_check(unsigned int index, unsigned int len); extern void* bux_alloc(unsigned int size); extern void* bux_realloc(void* ptr, unsigned int size); extern void bux_free(void* ptr); extern void bux_bounds_check(unsigned int index, unsigned int len); extern void* bux_channel_new(int64_t capacity, int64_t elem_size); extern void bux_channel_send(void* handle, void* elem); extern int bux_channel_recv(void* handle, void* out); extern void bux_channel_close(void* handle); extern void bux_channel_free(void* handle); extern unsigned int bux_hash_bytes(void* ptr, unsigned int size); extern unsigned int bux_hash_string(const char* s); extern unsigned int bux_hash_bytes(void* ptr, unsigned int size); extern int bux_mem_eq(void* a, void* b, unsigned int size); extern void* bux_alloc(unsigned int size); extern void bux_free(void* ptr); extern void bux_sha1(const char* data, int len, void* out); extern void bux_sha256(const char* data, int len, void* out); extern void bux_sha384(const char* data, int len, void* out); extern void bux_sha512(const char* data, int len, void* out); extern const char* bux_bytes_to_hex(void* data, int len); extern const char* bux_base64_encode(const char* data, int len); extern const char* bux_base64_decode(const char* data, int len, int* outlen); extern const char* bux_base64url_encode(const char* data, int len); extern const char* bux_base64url_decode(const char* data, int len, int* outlen); extern void bux_hmac_sha256(const char* key, int keylen, const char* msg, int msglen, void* out); extern void bux_hmac_sha384(const char* key, int keylen, const char* msg, int msglen, void* out); extern void bux_hmac_sha512(const char* key, int keylen, const char* msg, int msglen, void* out); extern const char* bux_bytes_to_hex(void* data, int len); extern const char* bux_base64_encode(const char* data, int len); extern int bux_random_bytes(void* buf, int len); extern const char* bux_base64_encode(const char* data, int len); extern const char* bux_bytes_to_hex(void* data, int len); extern int bux_random_bytes(void* buf, int len); extern const char* bux_aes_256_cbc_encrypt(const char* plain, int plainlen, const char* key, const char* iv, int* outlen); extern const char* bux_aes_256_cbc_decrypt(const char* cipher, int cipherlen, const char* key, const char* iv, int* outlen); extern const char* bux_aes_256_gcm_encrypt(const char* plain, int plainlen, const char* key, const char* iv, void* tag, int* outlen); extern const char* bux_aes_256_gcm_decrypt(const char* cipher, int cipherlen, const char* key, const char* iv, const char* tag, int* outlen); extern const char* bux_base64_encode(const char* data, int len); extern unsigned int bux_str_split_count(const char* s, const char* delim); extern const char* bux_str_split_part(const char* s, const char* delim, unsigned int index); extern const char* bux_ecdsa_sign_p256(const char* key, int keylen, const char* data, int datalen, int* outlen); extern int bux_ecdsa_verify_p256(const char* key, int keylen, const char* data, int datalen, const char* sig, int siglen); extern const char* bux_ecdsa_sign_p384(const char* key, int keylen, const char* data, int datalen, int* outlen); extern int bux_ecdsa_verify_p384(const char* key, int keylen, const char* data, int datalen, const char* sig, int siglen); extern const char* bux_base64_encode(const char* data, int len); extern const char* bux_base64_decode(const char* data, int len, int* outlen); extern int bux_ed25519_keypair(void* pubKey, void* privKey); extern int bux_ed25519_sign(const char* privKey, const char* data, int datalen, void* sig); extern int bux_ed25519_verify(const char* pubKey, const char* sig, const char* data, int datalen); extern const char* bux_base64_encode(const char* data, int len); extern const char* bux_base64_decode(const char* data, int len, int* outlen); extern const char* bux_rsa_sign_sha256(const char* key, int keylen, const char* data, int datalen, int* outlen); extern const char* bux_rsa_sign_sha384(const char* key, int keylen, const char* data, int datalen, int* outlen); extern const char* bux_rsa_sign_sha512(const char* key, int keylen, const char* data, int datalen, int* outlen); extern int bux_rsa_verify_sha256(const char* key, int keylen, const char* data, int datalen, const char* sig, int siglen); extern int bux_rsa_verify_sha384(const char* key, int keylen, const char* data, int datalen, const char* sig, int siglen); extern int bux_rsa_verify_sha512(const char* key, int keylen, const char* data, int datalen, const char* sig, int siglen); extern const char* bux_base64_encode(const char* data, int len); extern const char* bux_base64_decode(const char* data, int len, int* outlen); /* Constants */ #define JsonTagNull 0 #define JsonTagBool 1 #define JsonTagNumber 2 #define JsonTagString 3 #define JsonTagArray 4 #define JsonTagObject 5 #define AES_KEY_SIZE 32 #define AES_IV_SIZE 16 #define AES_GCM_TAG_SIZE 16 #define ED25519_PUBKEY_SIZE 32 #define ED25519_PRIVKEY_SIZE 32 #define ED25519_SIG_SIZE 64 typedef struct Channel_float64 { void* handle; } Channel_float64; typedef struct TaskHandle { void* handle; } TaskHandle; typedef struct RwLock { void* handle; } RwLock; typedef struct Channel_int { void* handle; } Channel_int; typedef enum { Result_Ok, Result_Err } Result_Tag; typedef union { int Ok_0; const char* Err_0; } Result_Data; typedef struct { Result_Tag tag; Result_Data data; } Result; typedef enum { JwtAlg_HS256, JwtAlg_HS384, JwtAlg_HS512, JwtAlg_RS256, JwtAlg_RS384, JwtAlg_RS512, JwtAlg_ES256, JwtAlg_ES384, JwtAlg_EdDSA } JwtAlg; typedef enum { Option_Some, Option_None } Option_Tag; typedef union { int Some_0; } Option_Data; typedef struct { Option_Tag tag; Option_Data data; } Option; typedef struct JsonValue { int tag; bool boolVal; double numVal; const char* strVal; JsonValue* arrData; unsigned int arrLen; unsigned int arrCap; const char** objKeys; JsonValue* objValues; unsigned int objLen; unsigned int objCap; } JsonValue; typedef struct Mutex { void* handle; } Mutex; typedef struct StringBuilder { void* handle; } StringBuilder; typedef struct JsonParser { const char* src; unsigned int pos; unsigned int len; const char* error; } JsonParser; bool DirExists(const char* path); bool Mkdir(const char* path); const char** ListDir(const char* dir, const char* ext, int* count); const char* ReadFile(const char* path); bool WriteFile(const char* path, const char* content); bool FileExists(const char* path); double Sqrt(double x); double Pow(double x, double y); int64_t Abs(int64_t n); double AbsF(double f); int64_t Min(int64_t a, int64_t b); int64_t Max(int64_t a, int64_t b); double MinF(double a, double b); double MaxF(double a, double b); void* Alloc(unsigned int size); void* Realloc(void* ptr, unsigned int size); void Free(void* ptr); bool MemEq(void* a, void* b, unsigned int size); const char* Path_Join(const char* a, const char* b); const char* Path_Parent(const char* path); const char* Path_Ext(const char* path); void Task_Init(int num_workers); TaskHandle Task_Spawn(void* fn, void* arg); void Task_Wait(TaskHandle t); void Task_Sleep(int64_t ms); void Task_Yield(void); int Task_CurrentId(void); void Task_Shutdown(void); int Os_ArgsCount(void); const char* Os_Args(int index); const char* Os_GetEnv(const char* name); bool Os_SetEnv(const char* name, const char* value); const char* Os_GetCwd(void); bool Os_Chdir(const char* path); int64_t Time_NowMs(void); int64_t Time_NowUs(void); void Time_SleepMs(int64_t ms); int Process_Run(const char* cmd); const char* Process_Output(const char* cmd); int Net_Create(void); bool Net_SetReuse(int fd); bool Net_Bind(int fd, const char* addr, int port); bool Net_Listen(int fd, int backlog); int Net_Accept(int fd); bool Net_Connect(int fd, const char* addr, int port); int Net_Send(int fd, const char* data); const char* Net_Recv(int fd, int maxLen); bool Net_Close(int fd); const char* Net_LastError(void); const char* Fmt_Format(const char* tmpl, const char** argStrs, int argCount); const char* Fmt_Fmt1(const char* tmpl, const char* a1); const char* Fmt_FmtInt(const char* tmpl, int64_t val); const char* Fmt_FmtBool(const char* tmpl, bool val); const char* Fmt_FmtFloat(const char* tmpl, double val); const char* Fmt_Fmt2(const char* tmpl, const char* a1, const char* a2); const char* Fmt_Fmt3(const char* tmpl, const char* a1, const char* a2, const char* a3); const char* Crypto_Sha256(const char* data); const char* Crypto_HmacSha256(const char* key, const char* message); const char* Crypto_RandomBytes(int n); const char* Crypto_Base64Encode(const char* s); const char* Crypto_HmacSha256Raw(const char* key, const char* message); const char* Crypto_Base64Decode(const char* s); Mutex Mutex_New(void); void Mutex_Lock(Mutex* m); void Mutex_Unlock(Mutex* m); void Mutex_Free(Mutex* m); RwLock RwLock_New(void); void RwLock_ReadLock(RwLock* rw); void RwLock_WriteLock(RwLock* rw); void RwLock_Unlock(RwLock* rw); void RwLock_Free(RwLock* rw); void Test_Exit(int code); void Test_Assert(bool cond); void Test_AssertEqInt(int a, int b); void Test_AssertTrue(bool cond); void Test_AssertFalse(bool cond); void Test_Fail(const char* msg); Result Result_NewOk(int value); Result Result_NewErr(const char* msg); bool Result_IsOk(Result r); bool Result_IsErr(Result r); int Result_Unwrap(Result r); int Result_UnwrapOr(Result r, int fallback); Option Option_NewSome(int value); Option Option_NewNone(void); bool Option_IsSome(Option o); bool Option_IsNone(Option o); int Option_Unwrap(Option o); int Option_UnwrapOr(Option o, int fallback); JsonValue Json_Null(void); JsonValue Json_Bool(bool b); JsonValue Json_Number(double n); JsonValue Json_String(const char* s); JsonValue Json_Array(void); JsonValue Json_Object(void); unsigned int Json_ArrayLen(JsonValue v); JsonValue Json_ArrayGet(JsonValue v, unsigned int index); void Json_ArrayPush(JsonValue* self, JsonValue val); unsigned int Json_ObjectLen(JsonValue v); JsonValue Json_ObjectGet(JsonValue v, const char* key); bool Json_ObjectHas(JsonValue v, const char* key); void Json_ObjectSet(JsonValue* self, const char* key, JsonValue val); bool Json_IsNull(JsonValue v); bool Json_AsBool(JsonValue v); double Json_AsNumber(JsonValue v); const char* Json_AsString(JsonValue v); int JsonParser_Peek(JsonParser* p); void JsonParser_Advance(JsonParser* p); void JsonParser_SkipWhitespace(JsonParser* p); bool JsonParser_Match(JsonParser* p, const char* expected); const char* JsonParser_ParseString(JsonParser* p); JsonValue JsonParser_ParseNumber(JsonParser* p); JsonValue JsonParser_ParseArray(JsonParser* p); JsonValue JsonParser_ParseObject(JsonParser* p); JsonValue JsonParser_ParseValue(JsonParser* p); JsonValue Json_Parse(const char* s); void Json_StringifyImpl(StringBuilder* sb, JsonValue v); const char* Json_Stringify(JsonValue v); unsigned int String_Len(const char* s); bool String_IsNull(const char* s); bool String_Eq(const char* a, const char* b); const char* String_Concat(const char* a, const char* b); const char* String_Copy(const char* s); bool String_StartsWith(const char* s, const char* prefix); bool String_EndsWith(const char* s, const char* suffix); bool String_Contains(const char* s, const char* substr); const char* String_Slice(const char* s, unsigned int start, unsigned int len); const char* String_Trim(const char* s); const char* String_TrimLeft(const char* s); const char* String_TrimRight(const char* s); const char* String_FromInt(int64_t n); int64_t String_ToInt(const char* s); StringBuilder StringBuilder_New(void); StringBuilder StringBuilder_NewCap(unsigned int cap); void StringBuilder_Append(StringBuilder* sb, const char* s); void StringBuilder_AppendInt(StringBuilder* sb, int64_t n); void StringBuilder_AppendFloat(StringBuilder* sb, double f); void StringBuilder_AppendChar(StringBuilder* sb, char c); const char* StringBuilder_Build(StringBuilder* sb); void StringBuilder_Free(StringBuilder* sb); unsigned int String_SplitCount(const char* s, const char* delim); const char* String_SplitPart(const char* s, const char* delim, unsigned int index); const char* String_Join2(const char* a, const char* b, const char* sep); const char* String_Chars(const char* s, unsigned int index); const char* String_Find(const char* haystack, const char* needle); unsigned int String_Offset(const char* pos, const char* base); const char* String_Replace(const char* s, const char* old, const char* new); double String_ToFloat(const char* s); const char* String_FromBool(bool b); const char* String_FromFloat(double f); const char* String_Format1(const char* pattern, const char* a0); const char* String_Format2(const char* pattern, const char* a0, const char* a1); const char* String_Format3(const char* pattern, const char* a0, const char* a1, const char* a2); void Channel_SendInt(Channel_int* ch, int value); int Channel_RecvInt(Channel_int* ch); void Channel_SendFloat64(Channel_float64* ch, double value); double Channel_RecvFloat64(Channel_float64* ch); const char* Hash_Sha1(const char* data); const char* Hash_Sha256(const char* data); const char* Hash_Sha384(const char* data); const char* Hash_Sha512(const char* data); void Hash_Sha256Raw(const char* data, void* out); void Hash_Sha384Raw(const char* data, void* out); void Hash_Sha512Raw(const char* data, void* out); int Hash_Sha1Size(void); int Hash_Sha256Size(void); int Hash_Sha384Size(void); int Hash_Sha512Size(void); const char* Base64_Encode(const char* s); const char* Base64_Decode(const char* s); const char* Base64URL_Encode(const char* s); const char* Base64URL_Decode(const char* s); const char* Hmac_Sha256(const char* key, const char* message); void Hmac_Sha256Raw(const char* key, const char* message, void* out); const char* Hmac_Sha256Base64(const char* key, const char* message); const char* Hmac_Sha384(const char* key, const char* message); void Hmac_Sha384Raw(const char* key, const char* message, void* out); const char* Hmac_Sha384Base64(const char* key, const char* message); const char* Hmac_Sha512(const char* key, const char* message); void Hmac_Sha512Raw(const char* key, const char* message, void* out); const char* Hmac_Sha512Base64(const char* key, const char* message); const char* Random_Bytes(int n); const char* Random_Hex(int n); const char* Random_Base64(int n); unsigned int Random_Uint32(void); const char* Aes_GenerateKey(void); const char* Aes_GenerateIV(void); const char* Aes_CbcEncrypt(const char* plain, const char* key, const char* iv); const char* Aes_CbcDecrypt(const char* cipher, const char* key, const char* iv); const char* Aes_GcmEncrypt(const char* plain, const char* key, const char* iv, void* tag); const char* Aes_GcmDecrypt(const char* cipher, const char* key, const char* iv, const char* tag); const char* Jwt_MakeHeader(JwtAlg alg); const char* Jwt_Sign(JwtAlg alg, const char* signingInput, const char* key); bool Jwt_Verify(JwtAlg alg, const char* signingInput, const char* signatureB64, const char* key); const char* Jwt_Encode(const char* headerJson, const char* payloadJson, JwtAlg alg, const char* key); bool Jwt_Decode(const char* token, JwtAlg alg, const char* key, const char** headerOut, const char** payloadOut); const char* Jwt_EncodeHS256(const char* payloadJson, const char* secret); const char* Jwt_EncodeHS384(const char* payloadJson, const char* secret); const char* Jwt_EncodeHS512(const char* payloadJson, const char* secret); const char* Jwt_EncodeRS256(const char* payloadJson, const char* pemPrivateKey); const char* Jwt_EncodeES256(const char* payloadJson, const char* pemPrivateKey); const char* Jwt_EncodeEdDSA(const char* payloadJson, const char* rawPrivKey); const char* Ecdsa_SignP256(const char* pemPrivateKey, const char* data); const char* Ecdsa_SignP256Base64(const char* pemPrivateKey, const char* data); bool Ecdsa_VerifyP256(const char* pemPublicKey, const char* data, const char* signature); bool Ecdsa_VerifyP256Base64(const char* pemPublicKey, const char* data, const char* signatureB64); const char* Ecdsa_SignP384(const char* pemPrivateKey, const char* data); const char* Ecdsa_SignP384Base64(const char* pemPrivateKey, const char* data); bool Ecdsa_VerifyP384(const char* pemPublicKey, const char* data, const char* signature); bool Ecdsa_VerifyP384Base64(const char* pemPublicKey, const char* data, const char* signatureB64); bool Ed25519_Keypair(void* pubKey, void* privKey); const char* Ed25519_KeypairBase64(void); const char* Ed25519_Sign(const char* privKey, const char* data); const char* Ed25519_SignBase64(const char* privKey, const char* data); bool Ed25519_Verify(const char* pubKey, const char* signature, const char* data); bool Ed25519_VerifyBase64(const char* pubKey, const char* signatureB64, const char* data); const char* Rsa_SignSha256(const char* pemPrivateKey, const char* data); const char* Rsa_SignSha384(const char* pemPrivateKey, const char* data); const char* Rsa_SignSha512(const char* pemPrivateKey, const char* data); const char* Rsa_SignSha256Base64(const char* pemPrivateKey, const char* data); const char* Rsa_SignSha384Base64(const char* pemPrivateKey, const char* data); const char* Rsa_SignSha512Base64(const char* pemPrivateKey, const char* data); bool Rsa_VerifySha256(const char* pemPublicKey, const char* data, const char* signature); bool Rsa_VerifySha384(const char* pemPublicKey, const char* data, const char* signature); bool Rsa_VerifySha512(const char* pemPublicKey, const char* data, const char* signature); bool Rsa_VerifySha256Base64(const char* pemPublicKey, const char* data, const char* signatureB64); bool Rsa_VerifySha384Base64(const char* pemPublicKey, const char* data, const char* signatureB64); bool Rsa_VerifySha512Base64(const char* pemPublicKey, const char* data, const char* signatureB64); int Main(void); typedef struct Drop_VTable { void (*Drop)(void* self); } Drop_VTable; typedef struct Drop_FatPtr { void* data; Drop_VTable* vtable; } Drop_FatPtr; bool DirExists(const char* path) { int _t2; int _t1; _t1 = bux_dir_exists(path); _t2 = (_t1 != 0); return _t2; } bool Mkdir(const char* path) { int _t4; int _t3; _t3 = bux_mkdir_if_needed(path); _t4 = (_t3 != 0); return _t4; } const char** ListDir(const char* dir, const char* ext, int* count) { const char** _t5; _t5 = bux_list_dir(dir, ext, count); return _t5; } const char* ReadFile(const char* path) { const char* _t6; _t6 = bux_read_file(path); return _t6; } bool WriteFile(const char* path, const char* content) { int _t8; int r; int _t7; _t7 = bux_write_file(path, content); r = _t7; _t8 = (r != 0); return _t8; } bool FileExists(const char* path) { int _t10; int r; int _t9; _t9 = bux_file_exists(path); r = _t9; _t10 = (r != 0); return _t10; } double Sqrt(double x) { double _t11; _t11 = bux_sqrt(x); return _t11; } double Pow(double x, double y) { double _t12; _t12 = bux_pow(x, y); return _t12; } int64_t Abs(int64_t n) { int64_t _t13; _t13 = bux_abs_i64(n); return _t13; } double AbsF(double f) { double _t14; _t14 = bux_abs_f64(f); return _t14; } int64_t Min(int64_t a, int64_t b) { int64_t _t15; _t15 = bux_min_i64(a, b); return _t15; } int64_t Max(int64_t a, int64_t b) { int64_t _t16; _t16 = bux_max_i64(a, b); return _t16; } double MinF(double a, double b) { double _t17; _t17 = bux_min_f64(a, b); return _t17; } double MaxF(double a, double b) { double _t18; _t18 = bux_max_f64(a, b); return _t18; } void* Alloc(unsigned int size) { void* _t19; _t19 = bux_alloc(size); return _t19; } void* Realloc(void* ptr, unsigned int size) { void* _t20; _t20 = bux_realloc(ptr, size); return _t20; } void Free(void* ptr) { bux_free(ptr); } bool MemEq(void* a, void* b, unsigned int size) { int _t22; int _t21; _t21 = bux_mem_eq(a, b, size); _t22 = (_t21 != 0); return _t22; } const char* Path_Join(const char* a, const char* b) { const char* _t23; _t23 = bux_path_join(a, b); return _t23; } const char* Path_Parent(const char* path) { const char* _t24; _t24 = bux_path_parent(path); return _t24; } const char* Path_Ext(const char* path) { const char* _t25; _t25 = bux_path_ext(path); return _t25; } void Task_Init(int num_workers) { bux_task_init(num_workers); } TaskHandle Task_Spawn(void* fn, void* arg) { TaskHandle _t27; void* _t26; _t26 = bux_task_spawn(fn, arg); _t27 = (TaskHandle){.handle = _t26}; return _t27; } void Task_Wait(TaskHandle t) { void* _t28; _t28 = t.handle; bux_task_join(_t28); } void Task_Sleep(int64_t ms) { bux_task_sleep(ms); } void Task_Yield(void) { bux_task_yield(); } int Task_CurrentId(void) { int _t29; _t29 = bux_task_current_id(); return _t29; } void Task_Shutdown(void) { bux_task_shutdown(); } int Os_ArgsCount(void) { int _t30; _t30 = bux_argc(); return _t30; } const char* Os_Args(int index) { const char* _t31; _t31 = bux_argv(index); return _t31; } const char* Os_GetEnv(const char* name) { const char* _t32; _t32 = bux_getenv(name); return _t32; } bool Os_SetEnv(const char* name, const char* value) { int _t34; int _t33; _t33 = bux_setenv(name, value); _t34 = (_t33 == 0); return _t34; } const char* Os_GetCwd(void) { const char* _t35; _t35 = bux_getcwd(); return _t35; } bool Os_Chdir(const char* path) { int _t37; int _t36; _t36 = bux_chdir(path); _t37 = (_t36 == 0); return _t37; } int64_t Time_NowMs(void) { int64_t _t38; _t38 = bux_time_ms(); return _t38; } int64_t Time_NowUs(void) { int64_t _t39; _t39 = bux_time_us(); return _t39; } void Time_SleepMs(int64_t ms) { bux_sleep_ms(ms); } int Process_Run(const char* cmd) { int _t40; _t40 = bux_process_run(cmd); return _t40; } const char* Process_Output(const char* cmd) { const char* _t41; _t41 = bux_process_output(cmd); return _t41; } int Net_Create(void) { int _t42; _t42 = bux_socket_create(); return _t42; } bool Net_SetReuse(int fd) { int _t44; int _t43; _t43 = bux_socket_reuse(fd); _t44 = (_t43 == 0); return _t44; } bool Net_Bind(int fd, const char* addr, int port) { int _t46; int _t45; _t45 = bux_socket_bind(fd, addr, port); _t46 = (_t45 == 0); return _t46; } bool Net_Listen(int fd, int backlog) { int _t48; int _t47; _t47 = bux_socket_listen(fd, backlog); _t48 = (_t47 == 0); return _t48; } int Net_Accept(int fd) { int _t49; _t49 = bux_socket_accept(fd); return _t49; } bool Net_Connect(int fd, const char* addr, int port) { int _t51; int _t50; _t50 = bux_socket_connect(fd, addr, port); _t51 = (_t50 == 0); return _t51; } int Net_Send(int fd, const char* data) { int _t53; unsigned int _t52; _t52 = bux_strlen(data); _t53 = (int)_t52; int _t54; _t54 = bux_socket_send(fd, data, _t53); return _t54; } const char* Net_Recv(int fd, int maxLen) { const char* _t55; _t55 = bux_socket_recv(fd, maxLen); return _t55; } bool Net_Close(int fd) { int _t57; int _t56; _t56 = bux_socket_close(fd); _t57 = (_t56 == 0); return _t57; } const char* Net_LastError(void) { const char* _t58; _t58 = bux_socket_error(); return _t58; } const char* Fmt_Format(const char* tmpl, const char** argStrs, int argCount) { int _t61; int _t64; int _t65; int _t68; int64_t _t69; int _t70; int _t72; int _t73; int _t76; unsigned int _t77; void* _t79; void* _t80; int _t81; void* _t82; StringBuilder sb; StringBuilder _t59; _t59 = StringBuilder_New(); sb = _t59; unsigned int i; i = 0; unsigned int tmplLen; unsigned int _t60; _t60 = bux_strlen(tmpl); tmplLen = _t60; while1:; _t61 = (i < tmplLen); if (!_t61) goto wend2; { const char* ch; const char* _t62; _t62 = String_Chars(tmpl, i); ch = _t62; bool _t63; _t63 = String_Eq(ch, "{"); if (!_t63) goto endif4; { unsigned int digitIdx; _t64 = i + 1; digitIdx = _t64; _t65 = (digitIdx < tmplLen); if (!_t65) goto endif6; { const char* digitCh; const char* _t66; _t66 = String_Chars(tmpl, digitIdx); digitCh = _t66; int64_t d; int64_t _t67; _t67 = bux_str_to_int(digitCh); d = _t67; bool __and_tmp_0; _t68 = (d >= 0); if (!_t68) goto else7; _t69 = (int64_t)argCount; _t70 = (d < _t69); *(bool*)&__and_tmp_0 = _t70; goto endif8; else7:; *(bool*)&__and_tmp_0 = 0; endif8:; bool _t71; _t71 = *(bool*)&__and_tmp_0; if (!_t71) goto endif10; { _t72 = i + 2; i = _t72; _t73 = (i < tmplLen); if (!_t73) goto endif12; { const char* closeCh; const char* _t74; _t74 = String_Chars(tmpl, i); closeCh = _t74; bool _t75; _t75 = String_Eq(closeCh, "}"); if (!_t75) goto endif14; { _t76 = i + 1; i = _t76; const char* argStr; _t77 = (unsigned int)d; const char* _t78; _t78 = argStrs[_t77]; argStr = _t78; _t79 = &sb; StringBuilder_Append(_t79, argStr); goto while1; } endif14:; } endif12:; } endif10:; } endif6:; } endif4:; _t80 = &sb; StringBuilder_Append(_t80, ch); _t81 = i + 1; i = _t81; } goto while1; wend2:; _t82 = &sb; const char* _t83; _t83 = StringBuilder_Build(_t82); return _t83; } const char* Fmt_Fmt1(const char* tmpl, const char* a1) { const char** _t85; const char** args; /* sizeof(const char*) */ void* _t84; _t84 = bux_alloc(sizeof(const char*)); _t85 = (const char**)_t84; args = _t85; args[0] = a1; const char* _t86; _t86 = Fmt_Format(tmpl, args, 1); return _t86; } const char* Fmt_FmtInt(const char* tmpl, int64_t val) { const char* s; const char* _t87; _t87 = String_FromInt(val); s = _t87; const char* _t88; _t88 = Fmt_Fmt1(tmpl, s); return _t88; } const char* Fmt_FmtBool(const char* tmpl, bool val) { const char* s; const char* _t89; _t89 = String_FromBool(val); s = _t89; const char* _t90; _t90 = Fmt_Fmt1(tmpl, s); return _t90; } const char* Fmt_FmtFloat(const char* tmpl, double val) { const char* s; const char* _t91; _t91 = String_FromFloat(val); s = _t91; const char* _t92; _t92 = Fmt_Fmt1(tmpl, s); return _t92; } const char* Fmt_Fmt2(const char* tmpl, const char* a1, const char* a2) { int _t93; const char** _t95; const char** args; /* sizeof(const char*) */ _t93 = 2 * sizeof(const char*); void* _t94; _t94 = bux_alloc(_t93); _t95 = (const char**)_t94; args = _t95; args[0] = a1; args[1] = a2; const char* _t96; _t96 = Fmt_Format(tmpl, args, 2); return _t96; } const char* Fmt_Fmt3(const char* tmpl, const char* a1, const char* a2, const char* a3) { int _t97; const char** _t99; const char** args; /* sizeof(const char*) */ _t97 = 3 * sizeof(const char*); void* _t98; _t98 = bux_alloc(_t97); _t99 = (const char**)_t98; args = _t99; args[0] = a1; args[1] = a2; args[2] = a3; const char* _t100; _t100 = Fmt_Format(tmpl, args, 3); return _t100; } const char* Crypto_Sha256(const char* data) { int _t102; void* _t104; int len; unsigned int _t101; _t101 = String_Len(data); _t102 = (int)_t101; len = _t102; void* hashBuf; void* _t103; _t103 = Alloc(32); hashBuf = _t103; bux_sha256(data, len, hashBuf); const char* result; _t104 = (void*)hashBuf; const char* _t105; _t105 = bux_bytes_to_hex(_t104, 32); result = _t105; Free(hashBuf); return result; } const char* Crypto_HmacSha256(const char* key, const char* message) { int _t107; int _t109; void* _t111; int keylen; unsigned int _t106; _t106 = String_Len(key); _t107 = (int)_t106; keylen = _t107; int msglen; unsigned int _t108; _t108 = String_Len(message); _t109 = (int)_t108; msglen = _t109; void* hmacBuf; void* _t110; _t110 = Alloc(32); hmacBuf = _t110; bux_hmac_sha256(key, keylen, message, msglen, hmacBuf); const char* result; _t111 = (void*)hmacBuf; const char* _t112; _t112 = bux_bytes_to_hex(_t111, 32); result = _t112; Free(hmacBuf); return result; } const char* Crypto_RandomBytes(int n) { int _t113; unsigned int _t114; int _t117; const char* _t118; _t113 = (n <= 0); if (!_t113) goto endif16; { return ""; } endif16:; void* buf; _t114 = (unsigned int)n; void* _t115; _t115 = Alloc(_t114); buf = _t115; int _t116; _t116 = bux_random_bytes(buf, n); _t117 = (_t116 != 1); if (!_t117) goto endif18; { Free(buf); return ""; } endif18:; const char* result; _t118 = (const char*)buf; const char* _t119; _t119 = bux_base64_encode(_t118, n); result = _t119; Free(buf); return result; } const char* Crypto_Base64Encode(const char* s) { int _t121; unsigned int _t120; _t120 = String_Len(s); _t121 = (int)_t120; const char* _t122; _t122 = bux_base64_encode(s, _t121); return _t122; } const char* Crypto_HmacSha256Raw(const char* key, const char* message) { int _t124; int _t126; const char* _t128; int keylen; unsigned int _t123; _t123 = String_Len(key); _t124 = (int)_t123; keylen = _t124; int msglen; unsigned int _t125; _t125 = String_Len(message); _t126 = (int)_t125; msglen = _t126; void* hmacBuf; void* _t127; _t127 = Alloc(32); hmacBuf = _t127; bux_hmac_sha256(key, keylen, message, msglen, hmacBuf); const char* result; _t128 = (const char*)hmacBuf; const char* _t129; _t129 = bux_base64_encode(_t128, 32); result = _t129; Free(hmacBuf); return result; } const char* Crypto_Base64Decode(const char* s) { int _t131; void* _t132; int outlen; outlen = 0; unsigned int _t130; _t130 = String_Len(s); _t131 = (int)_t130; _t132 = &outlen; const char* _t133; _t133 = bux_base64_decode(s, _t131, _t132); return _t133; } Mutex Mutex_New(void) { Mutex _t135; void* _t134; _t134 = bux_mutex_new(); _t135 = (Mutex){.handle = _t134}; return _t135; } void Mutex_Lock(Mutex* m) { void* _t136; _t136 = m->handle; bux_mutex_lock(_t136); } void Mutex_Unlock(Mutex* m) { void* _t137; _t137 = m->handle; bux_mutex_unlock(_t137); } void Mutex_Free(Mutex* m) { void* _t138; _t138 = m->handle; bux_mutex_free(_t138); } RwLock RwLock_New(void) { RwLock _t140; void* _t139; _t139 = bux_rwlock_new(); _t140 = (RwLock){.handle = _t139}; return _t140; } void RwLock_ReadLock(RwLock* rw) { void* _t141; _t141 = rw->handle; bux_rwlock_rdlock(_t141); } void RwLock_WriteLock(RwLock* rw) { void* _t142; _t142 = rw->handle; bux_rwlock_wrlock(_t142); } void RwLock_Unlock(RwLock* rw) { void* _t143; _t143 = rw->handle; bux_rwlock_unlock(_t143); } void RwLock_Free(RwLock* rw) { void* _t144; _t144 = rw->handle; bux_rwlock_free(_t144); } void Test_Exit(int code) { bux_exit(code); } void Test_Assert(bool cond) { int _t145; _t145 = (int)cond; bux_assert(_t145, "", 0, ""); } void Test_AssertEqInt(int a, int b) { int _t146; _t146 = (a != b); if (!_t146) goto endif20; { PrintLine("ASSERT_EQ FAILED:"); PrintInt(a); PrintLine(" != "); PrintInt(b); bux_exit(1); } endif20:; } void Test_AssertTrue(bool cond) { int _t147; _t147 = !cond; if (!_t147) goto endif22; { PrintLine("ASSERT_TRUE FAILED"); bux_exit(1); } endif22:; } void Test_AssertFalse(bool cond) { if (!cond) goto endif24; { PrintLine("ASSERT_FALSE FAILED"); bux_exit(1); } endif24:; } void Test_Fail(const char* msg) { PrintLine("FAIL:"); PrintLine(msg); bux_exit(1); } Result Result_NewOk(int value) { Result _t148; Result r; _t148 = (Result){.tag = Result_Ok}; r = _t148; r.data.Ok_0 = value; return r; } Result Result_NewErr(const char* msg) { Result _t149; Result r; _t149 = (Result){.tag = Result_Err}; r = _t149; r.data.Err_0 = msg; return r; } bool Result_IsOk(Result r) { int _t151; Result_Tag _t150; _t150 = r.tag; _t151 = (_t150 == Result_Ok); return _t151; } bool Result_IsErr(Result r) { int _t153; Result_Tag _t152; _t152 = r.tag; _t153 = (_t152 == Result_Err); return _t153; } int Result_Unwrap(Result r) { int _t155; Result_Tag _t154; _t154 = r.tag; _t155 = (_t154 != Result_Ok); if (!_t155) goto endif26; { PrintLine("panic: unwrap on Err"); return 0; } endif26:; Result_Data _t156; _t156 = r.data; int _t157; _t157 = _t156.Ok_0; return _t157; } int Result_UnwrapOr(Result r, int fallback) { int _t159; Result_Tag _t158; _t158 = r.tag; _t159 = (_t158 == Result_Ok); if (!_t159) goto endif28; { Result_Data _t160; _t160 = r.data; int _t161; _t161 = _t160.Ok_0; return _t161; } endif28:; return fallback; } Option Option_NewSome(int value) { Option _t162; Option o; _t162 = (Option){.tag = Option_Some}; o = _t162; o.data.Some_0 = value; return o; } Option Option_NewNone(void) { Option _t163; _t163 = (Option){.tag = Option_None}; return _t163; } bool Option_IsSome(Option o) { int _t165; Option_Tag _t164; _t164 = o.tag; _t165 = (_t164 == Option_Some); return _t165; } bool Option_IsNone(Option o) { int _t167; Option_Tag _t166; _t166 = o.tag; _t167 = (_t166 == Option_None); return _t167; } int Option_Unwrap(Option o) { int _t169; Option_Tag _t168; _t168 = o.tag; _t169 = (_t168 != Option_Some); if (!_t169) goto endif30; { PrintLine("panic: unwrap on None"); return 0; } endif30:; Option_Data _t170; _t170 = o.data; int _t171; _t171 = _t170.Some_0; return _t171; } int Option_UnwrapOr(Option o, int fallback) { int _t173; Option_Tag _t172; _t172 = o.tag; _t173 = (_t172 == Option_Some); if (!_t173) goto endif32; { Option_Data _t174; _t174 = o.data; int _t175; _t175 = _t174.Some_0; return _t175; } endif32:; return fallback; } JsonValue Json_Null(void) { JsonValue _t176; _t176 = (JsonValue){.tag = JsonTagNull, .boolVal = 0, .numVal = 0.0, .strVal = "", .arrData = 0, .arrLen = 0, .arrCap = 0, .objKeys = 0, .objValues = 0, .objLen = 0, .objCap = 0}; return _t176; } JsonValue Json_Bool(bool b) { JsonValue _t177; _t177 = (JsonValue){.tag = JsonTagBool, .boolVal = b, .numVal = 0.0, .strVal = "", .arrData = 0, .arrLen = 0, .arrCap = 0, .objKeys = 0, .objValues = 0, .objLen = 0, .objCap = 0}; return _t177; } JsonValue Json_Number(double n) { JsonValue _t178; _t178 = (JsonValue){.tag = JsonTagNumber, .boolVal = 0, .numVal = n, .strVal = "", .arrData = 0, .arrLen = 0, .arrCap = 0, .objKeys = 0, .objValues = 0, .objLen = 0, .objCap = 0}; return _t178; } JsonValue Json_String(const char* s) { JsonValue _t179; _t179 = (JsonValue){.tag = JsonTagString, .boolVal = 0, .numVal = 0.0, .strVal = s, .arrData = 0, .arrLen = 0, .arrCap = 0, .objKeys = 0, .objValues = 0, .objLen = 0, .objCap = 0}; return _t179; } JsonValue Json_Array(void) { JsonValue _t180; _t180 = (JsonValue){.tag = JsonTagArray, .boolVal = 0, .numVal = 0.0, .strVal = "", .arrData = 0, .arrLen = 0, .arrCap = 0, .objKeys = 0, .objValues = 0, .objLen = 0, .objCap = 0}; return _t180; } JsonValue Json_Object(void) { JsonValue _t181; _t181 = (JsonValue){.tag = JsonTagObject, .boolVal = 0, .numVal = 0.0, .strVal = "", .arrData = 0, .arrLen = 0, .arrCap = 0, .objKeys = 0, .objValues = 0, .objLen = 0, .objCap = 0}; return _t181; } unsigned int Json_ArrayLen(JsonValue v) { int _t183; int _t182; _t182 = v.tag; _t183 = (_t182 != JsonTagArray); if (!_t183) goto endif34; { return 0; } endif34:; unsigned int _t184; _t184 = v.arrLen; return _t184; } JsonValue Json_ArrayGet(JsonValue v, unsigned int index) { int _t186; int _t189; int _t185; _t185 = v.tag; _t186 = (_t185 != JsonTagArray); if (!_t186) goto endif36; { JsonValue _t187; _t187 = Json_Null(); return _t187; } endif36:; unsigned int _t188; _t188 = v.arrLen; _t189 = (index >= _t188); if (!_t189) goto endif38; { JsonValue _t190; _t190 = Json_Null(); return _t190; } endif38:; JsonValue* _t191; _t191 = v.arrData; JsonValue _t192; _t192 = _t191[index]; return _t192; } void Json_ArrayPush(JsonValue* self, JsonValue val) { int _t194; int _t197; int _t199; int _t200; JsonValue* _t202; int _t203; void* _t205; int _t206; JsonValue* _t208; int _t212; int _t193; _t193 = self->tag; _t194 = (_t193 != JsonTagArray); if (!_t194) goto endif40; { return; } endif40:; unsigned int _t195; _t195 = self->arrLen; unsigned int _t196; _t196 = self->arrCap; _t197 = (_t195 >= _t196); if (!_t197) goto endif42; { unsigned int arrNewCap; unsigned int _t198; _t198 = self->arrCap; arrNewCap = _t198; _t199 = (arrNewCap == 0); if (!_t199) goto else43; { self->arrCap = 4; /* sizeof(JsonValue) */ _t200 = 4 * sizeof(JsonValue); void* _t201; _t201 = Alloc(_t200); _t202 = (JsonValue*)_t201; self->arrData = _t202; } goto endif44; else43:; { unsigned int doubleCap; _t203 = arrNewCap * 2; doubleCap = _t203; self->arrCap = doubleCap; JsonValue* _t204; _t204 = self->arrData; _t205 = (void*)_t204; /* sizeof(JsonValue) */ _t206 = doubleCap * sizeof(JsonValue); void* _t207; _t207 = Realloc(_t205, _t206); _t208 = (JsonValue*)_t207; self->arrData = _t208; } endif44:; } endif42:; JsonValue* _t209; _t209 = self->arrData; unsigned int _t210; _t210 = self->arrLen; _t209[_t210] = val; unsigned int _t211; _t211 = self->arrLen; _t212 = _t211 + 1; self->arrLen = _t212; } unsigned int Json_ObjectLen(JsonValue v) { int _t214; int _t213; _t213 = v.tag; _t214 = (_t213 != JsonTagObject); if (!_t214) goto endif46; { return 0; } endif46:; unsigned int _t215; _t215 = v.objLen; return _t215; } JsonValue Json_ObjectGet(JsonValue v, const char* key) { int _t217; int _t220; int _t226; int _t216; _t216 = v.tag; _t217 = (_t216 != JsonTagObject); if (!_t217) goto endif48; { JsonValue _t218; _t218 = Json_Null(); return _t218; } endif48:; unsigned int i; i = 0; while49:; unsigned int _t219; _t219 = v.objLen; _t220 = (i < _t219); if (!_t220) goto wend50; { const char** _t221; _t221 = v.objKeys; const char* _t222; _t222 = _t221[i]; bool _t223; _t223 = String_Eq(_t222, key); if (!_t223) goto endif52; { JsonValue* _t224; _t224 = v.objValues; JsonValue _t225; _t225 = _t224[i]; return _t225; } endif52:; _t226 = i + 1; i = _t226; } goto while49; wend50:; JsonValue _t227; _t227 = Json_Null(); return _t227; } bool Json_ObjectHas(JsonValue v, const char* key) { int _t229; int _t231; int _t235; int _t228; _t228 = v.tag; _t229 = (_t228 != JsonTagObject); if (!_t229) goto endif54; { return 0; } endif54:; unsigned int i; i = 0; while55:; unsigned int _t230; _t230 = v.objLen; _t231 = (i < _t230); if (!_t231) goto wend56; { const char** _t232; _t232 = v.objKeys; const char* _t233; _t233 = _t232[i]; bool _t234; _t234 = String_Eq(_t233, key); if (!_t234) goto endif58; { return 1; } endif58:; _t235 = i + 1; i = _t235; } goto while55; wend56:; return 0; } void Json_ObjectSet(JsonValue* self, const char* key, JsonValue val) { int _t237; int _t239; int _t244; int _t247; int _t249; int _t250; const char** _t252; int _t253; JsonValue* _t255; int _t256; void* _t258; int _t259; const char** _t261; void* _t263; int _t264; JsonValue* _t266; int _t272; int _t236; _t236 = self->tag; _t237 = (_t236 != JsonTagObject); if (!_t237) goto endif60; { return; } endif60:; unsigned int i; i = 0; while61:; unsigned int _t238; _t238 = self->objLen; _t239 = (i < _t238); if (!_t239) goto wend62; { const char** _t240; _t240 = self->objKeys; const char* _t241; _t241 = _t240[i]; bool _t242; _t242 = String_Eq(_t241, key); if (!_t242) goto endif64; { JsonValue* _t243; _t243 = self->objValues; _t243[i] = val; return; } endif64:; _t244 = i + 1; i = _t244; } goto while61; wend62:; unsigned int _t245; _t245 = self->objLen; unsigned int _t246; _t246 = self->objCap; _t247 = (_t245 >= _t246); if (!_t247) goto endif66; { unsigned int objNewCap; unsigned int _t248; _t248 = self->objCap; objNewCap = _t248; _t249 = (objNewCap == 0); if (!_t249) goto else67; { self->objCap = 4; /* sizeof(const char*) */ _t250 = 4 * sizeof(const char*); void* _t251; _t251 = Alloc(_t250); _t252 = (const char**)_t251; self->objKeys = _t252; /* sizeof(JsonValue) */ _t253 = 4 * sizeof(JsonValue); void* _t254; _t254 = Alloc(_t253); _t255 = (JsonValue*)_t254; self->objValues = _t255; } goto endif68; else67:; { unsigned int doubleCap; _t256 = objNewCap * 2; doubleCap = _t256; self->objCap = doubleCap; const char** _t257; _t257 = self->objKeys; _t258 = (void*)_t257; /* sizeof(const char*) */ _t259 = doubleCap * sizeof(const char*); void* _t260; _t260 = Realloc(_t258, _t259); _t261 = (const char**)_t260; self->objKeys = _t261; JsonValue* _t262; _t262 = self->objValues; _t263 = (void*)_t262; /* sizeof(JsonValue) */ _t264 = doubleCap * sizeof(JsonValue); void* _t265; _t265 = Realloc(_t263, _t264); _t266 = (JsonValue*)_t265; self->objValues = _t266; } endif68:; } endif66:; const char** _t267; _t267 = self->objKeys; unsigned int _t268; _t268 = self->objLen; _t267[_t268] = key; JsonValue* _t269; _t269 = self->objValues; unsigned int _t270; _t270 = self->objLen; _t269[_t270] = val; unsigned int _t271; _t271 = self->objLen; _t272 = _t271 + 1; self->objLen = _t272; } bool Json_IsNull(JsonValue v) { int _t274; int _t273; _t273 = v.tag; _t274 = (_t273 == JsonTagNull); return _t274; } bool Json_AsBool(JsonValue v) { int _t276; int _t275; _t275 = v.tag; _t276 = (_t275 == JsonTagBool); if (!_t276) goto endif70; { bool _t277; _t277 = v.boolVal; return _t277; } endif70:; return 0; } double Json_AsNumber(JsonValue v) { int _t279; int _t278; _t278 = v.tag; _t279 = (_t278 == JsonTagNumber); if (!_t279) goto endif72; { double _t280; _t280 = v.numVal; return _t280; } endif72:; return 0.0; } const char* Json_AsString(JsonValue v) { int _t282; int _t281; _t281 = v.tag; _t282 = (_t281 == JsonTagString); if (!_t282) goto endif74; { const char* _t283; _t283 = v.strVal; return _t283; } endif74:; return ""; } int JsonParser_Peek(JsonParser* p) { int _t286; int _t290; unsigned int _t284; _t284 = p->pos; unsigned int _t285; _t285 = p->len; _t286 = (_t284 >= _t285); if (!_t286) goto endif76; { return 0; } endif76:; const char* _t287; _t287 = p->src; unsigned int _t288; _t288 = p->pos; int _t289; _t289 = _t287[_t288]; _t290 = (int)_t289; return _t290; } void JsonParser_Advance(JsonParser* p) { int _t293; int _t295; unsigned int _t291; _t291 = p->pos; unsigned int _t292; _t292 = p->len; _t293 = (_t291 < _t292); if (!_t293) goto endif78; { unsigned int _t294; _t294 = p->pos; _t295 = _t294 + 1; p->pos = _t295; } endif78:; } void JsonParser_SkipWhitespace(JsonParser* p) { int _t297; int _t298; int _t300; int _t302; while79:; if (!1) goto wend80; { int c; int _t296; _t296 = JsonParser_Peek(p); c = _t296; bool __or_tmp_1; bool __or_tmp_2; bool __or_tmp_3; _t297 = (c == 32); if (!_t297) goto else81; *(bool*)&__or_tmp_3 = 1; goto endif82; else81:; _t298 = (c == 9); *(bool*)&__or_tmp_3 = _t298; endif82:; bool _t299; _t299 = *(bool*)&__or_tmp_3; if (!_t299) goto else83; *(bool*)&__or_tmp_2 = 1; goto endif84; else83:; _t300 = (c == 10); *(bool*)&__or_tmp_2 = _t300; endif84:; bool _t301; _t301 = *(bool*)&__or_tmp_2; if (!_t301) goto else85; *(bool*)&__or_tmp_1 = 1; goto endif86; else85:; _t302 = (c == 13); *(bool*)&__or_tmp_1 = _t302; endif86:; bool _t303; _t303 = *(bool*)&__or_tmp_1; if (!_t303) goto else87; { JsonParser_Advance(p); } goto endif88; else87:; { return; } endif88:; } goto while79; wend80:; } bool JsonParser_Match(JsonParser* p, const char* expected) { int _t306; int _t308; int _t309; int _t312; int _t315; int _t316; int _t318; unsigned int elen; unsigned int _t304; _t304 = String_Len(expected); elen = _t304; unsigned int _t305; _t305 = p->pos; _t306 = _t305 + elen; unsigned int _t307; _t307 = p->len; _t308 = (_t306 > _t307); if (!_t308) goto endif90; { return 0; } endif90:; unsigned int i; i = 0; while91:; _t309 = (i < elen); if (!_t309) goto wend92; { const char* _t310; _t310 = p->src; unsigned int _t311; _t311 = p->pos; _t312 = _t311 + i; int _t313; _t313 = _t310[_t312]; int _t314; _t314 = expected[i]; _t315 = (_t313 != _t314); if (!_t315) goto endif94; { return 0; } endif94:; _t316 = i + 1; i = _t316; } goto while91; wend92:; unsigned int _t317; _t317 = p->pos; _t318 = _t317 + elen; p->pos = _t318; return 1; } const char* JsonParser_ParseString(JsonParser* p) { int _t320; int _t323; int _t324; int _t326; int _t328; void* _t329; int _t330; void* _t331; char _t332; int _t333; void* _t334; char _t335; int _t336; void* _t337; char _t338; int _t339; void* _t340; char _t341; int _t342; void* _t343; char _t344; int _t345; void* _t346; char _t347; int _t348; void* _t349; char _t350; void* _t351; char _t352; void* _t353; char _t354; void* _t355; char _t356; int _t358; void* _t359; void* _t360; void* _t362; int _t319; _t319 = JsonParser_Peek(p); _t320 = (_t319 != 34); if (!_t320) goto endif96; { p->error = "Expected string"; return ""; } endif96:; JsonParser_Advance(p); StringBuilder sb; StringBuilder _t321; _t321 = StringBuilder_New(); sb = _t321; while97:; if (!1) goto wend98; { int c; int _t322; _t322 = JsonParser_Peek(p); c = _t322; bool __or_tmp_4; _t323 = (c == 0); if (!_t323) goto else99; *(bool*)&__or_tmp_4 = 1; goto endif100; else99:; _t324 = (c == 34); *(bool*)&__or_tmp_4 = _t324; endif100:; bool _t325; _t325 = *(bool*)&__or_tmp_4; if (!_t325) goto endif102; { goto wend98; } endif102:; _t326 = (c == 92); if (!_t326) goto else103; { JsonParser_Advance(p); int esc; int _t327; _t327 = JsonParser_Peek(p); esc = _t327; _t328 = (esc == 0); if (!_t328) goto endif106; { p->error = "Unterminated string escape"; _t329 = &sb; StringBuilder_Free(_t329); return ""; } endif106:; _t330 = (esc == 110); if (!_t330) goto else107; { _t331 = &sb; _t332 = (char)10; StringBuilder_AppendChar(_t331, _t332); } goto endif108; else107:; _t333 = (esc == 116); if (!_t333) goto else109; { _t334 = &sb; _t335 = (char)9; StringBuilder_AppendChar(_t334, _t335); } goto endif110; else109:; _t336 = (esc == 114); if (!_t336) goto else111; { _t337 = &sb; _t338 = (char)13; StringBuilder_AppendChar(_t337, _t338); } goto endif112; else111:; _t339 = (esc == 98); if (!_t339) goto else113; { _t340 = &sb; _t341 = (char)8; StringBuilder_AppendChar(_t340, _t341); } goto endif114; else113:; _t342 = (esc == 102); if (!_t342) goto else115; { _t343 = &sb; _t344 = (char)12; StringBuilder_AppendChar(_t343, _t344); } goto endif116; else115:; _t345 = (esc == 34); if (!_t345) goto else117; { _t346 = &sb; _t347 = (char)34; StringBuilder_AppendChar(_t346, _t347); } goto endif118; else117:; _t348 = (esc == 92); if (!_t348) goto else119; { _t349 = &sb; _t350 = (char)92; StringBuilder_AppendChar(_t349, _t350); } goto endif120; else119:; { _t351 = &sb; _t352 = (char)92; StringBuilder_AppendChar(_t351, _t352); _t353 = &sb; _t354 = (char)esc; StringBuilder_AppendChar(_t353, _t354); } endif120:; endif118:; endif116:; endif114:; endif112:; endif110:; endif108:; JsonParser_Advance(p); } goto endif104; else103:; { _t355 = &sb; _t356 = (char)c; StringBuilder_AppendChar(_t355, _t356); JsonParser_Advance(p); } endif104:; } goto while97; wend98:; int _t357; _t357 = JsonParser_Peek(p); _t358 = (_t357 != 34); if (!_t358) goto endif122; { p->error = "Unterminated string"; _t359 = &sb; StringBuilder_Free(_t359); return ""; } endif122:; JsonParser_Advance(p); const char* result; _t360 = &sb; const char* _t361; _t361 = StringBuilder_Build(_t360); result = _t361; _t362 = &sb; StringBuilder_Free(_t362); return result; } JsonValue JsonParser_ParseNumber(JsonParser* p) { int _t365; int _t367; int _t368; int _t371; int _t373; int _t374; int _t378; unsigned int start; unsigned int _t363; _t363 = p->pos; start = _t363; int c0; int _t364; _t364 = JsonParser_Peek(p); c0 = _t364; _t365 = (c0 == 45); if (!_t365) goto endif124; { JsonParser_Advance(p); } endif124:; while125:; if (!1) goto wend126; { int c; int _t366; _t366 = JsonParser_Peek(p); c = _t366; bool __and_tmp_5; _t367 = (c >= 48); if (!_t367) goto else127; _t368 = (c <= 57); *(bool*)&__and_tmp_5 = _t368; goto endif128; else127:; *(bool*)&__and_tmp_5 = 0; endif128:; bool _t369; _t369 = *(bool*)&__and_tmp_5; if (!_t369) goto else129; { JsonParser_Advance(p); } goto endif130; else129:; { goto wend126; } endif130:; } goto while125; wend126:; int _t370; _t370 = JsonParser_Peek(p); _t371 = (_t370 == 46); if (!_t371) goto endif132; { JsonParser_Advance(p); while133:; if (!1) goto wend134; { int c; int _t372; _t372 = JsonParser_Peek(p); c = _t372; bool __and_tmp_6; _t373 = (c >= 48); if (!_t373) goto else135; _t374 = (c <= 57); *(bool*)&__and_tmp_6 = _t374; goto endif136; else135:; *(bool*)&__and_tmp_6 = 0; endif136:; bool _t375; _t375 = *(bool*)&__and_tmp_6; if (!_t375) goto else137; { JsonParser_Advance(p); } goto endif138; else137:; { goto wend134; } endif138:; } goto while133; wend134:; } endif132:; const char* numStr; const char* _t376; _t376 = p->src; unsigned int _t377; _t377 = p->pos; _t378 = _t377 - start; const char* _t379; _t379 = String_Slice(_t376, start, _t378); numStr = _t379; double n; double _t380; _t380 = String_ToFloat(numStr); n = _t380; JsonValue _t381; _t381 = Json_Number(n); return _t381; } JsonValue JsonParser_ParseArray(JsonParser* p) { int _t384; int _t387; void* _t389; int _t391; int _t392; JsonParser_Advance(p); JsonValue arr; JsonValue _t382; _t382 = Json_Array(); arr = _t382; JsonParser_SkipWhitespace(p); int _t383; _t383 = JsonParser_Peek(p); _t384 = (_t383 == 93); if (!_t384) goto endif140; { JsonParser_Advance(p); return arr; } endif140:; while141:; if (!1) goto wend142; { JsonParser_SkipWhitespace(p); JsonValue val; JsonValue _t385; _t385 = JsonParser_ParseValue(p); val = _t385; const char* _t386; _t386 = p->error; _t387 = (_t386 != ""); if (!_t387) goto endif144; { JsonValue _t388; _t388 = Json_Null(); return _t388; } endif144:; _t389 = &arr; Json_ArrayPush(_t389, val); JsonParser_SkipWhitespace(p); int c; int _t390; _t390 = JsonParser_Peek(p); c = _t390; _t391 = (c == 93); if (!_t391) goto endif146; { JsonParser_Advance(p); return arr; } endif146:; _t392 = (c == 44); if (!_t392) goto else147; { JsonParser_Advance(p); } goto endif148; else147:; { p->error = "Expected ',' or ']' in array"; JsonValue _t393; _t393 = Json_Null(); return _t393; } endif148:; } goto while141; wend142:; } JsonValue JsonParser_ParseObject(JsonParser* p) { int _t396; int _t399; int _t402; int _t406; void* _t408; int _t410; int _t411; JsonParser_Advance(p); JsonValue obj; JsonValue _t394; _t394 = Json_Object(); obj = _t394; JsonParser_SkipWhitespace(p); int _t395; _t395 = JsonParser_Peek(p); _t396 = (_t395 == 125); if (!_t396) goto endif150; { JsonParser_Advance(p); return obj; } endif150:; while151:; if (!1) goto wend152; { JsonParser_SkipWhitespace(p); const char* key; const char* _t397; _t397 = JsonParser_ParseString(p); key = _t397; const char* _t398; _t398 = p->error; _t399 = (_t398 != ""); if (!_t399) goto endif154; { JsonValue _t400; _t400 = Json_Null(); return _t400; } endif154:; JsonParser_SkipWhitespace(p); int _t401; _t401 = JsonParser_Peek(p); _t402 = (_t401 != 58); if (!_t402) goto endif156; { p->error = "Expected ':' after object key"; JsonValue _t403; _t403 = Json_Null(); return _t403; } endif156:; JsonParser_Advance(p); JsonParser_SkipWhitespace(p); JsonValue val; JsonValue _t404; _t404 = JsonParser_ParseValue(p); val = _t404; const char* _t405; _t405 = p->error; _t406 = (_t405 != ""); if (!_t406) goto endif158; { JsonValue _t407; _t407 = Json_Null(); return _t407; } endif158:; _t408 = &obj; Json_ObjectSet(_t408, key, val); JsonParser_SkipWhitespace(p); int c; int _t409; _t409 = JsonParser_Peek(p); c = _t409; _t410 = (c == 125); if (!_t410) goto endif160; { JsonParser_Advance(p); return obj; } endif160:; _t411 = (c == 44); if (!_t411) goto else161; { JsonParser_Advance(p); } goto endif162; else161:; { p->error = "Expected ',' or '}' in object"; JsonValue _t412; _t412 = Json_Null(); return _t412; } endif162:; } goto while151; wend152:; } JsonValue JsonParser_ParseValue(JsonParser* p) { int _t414; int _t416; int _t419; int _t421; int _t423; int _t427; int _t431; int _t435; int _t436; int _t438; JsonParser_SkipWhitespace(p); int c; int _t413; _t413 = JsonParser_Peek(p); c = _t413; _t414 = (c == 0); if (!_t414) goto endif164; { p->error = "Unexpected end of input"; JsonValue _t415; _t415 = Json_Null(); return _t415; } endif164:; _t416 = (c == 34); if (!_t416) goto endif166; { const char* _t417; _t417 = JsonParser_ParseString(p); JsonValue _t418; _t418 = Json_String(_t417); return _t418; } endif166:; _t419 = (c == 123); if (!_t419) goto endif168; { JsonValue _t420; _t420 = JsonParser_ParseObject(p); return _t420; } endif168:; _t421 = (c == 91); if (!_t421) goto endif170; { JsonValue _t422; _t422 = JsonParser_ParseArray(p); return _t422; } endif170:; _t423 = (c == 116); if (!_t423) goto endif172; { bool _t424; _t424 = JsonParser_Match(p, "true"); if (!_t424) goto endif174; { JsonValue _t425; _t425 = Json_Bool(1); return _t425; } endif174:; p->error = "Expected 'true'"; JsonValue _t426; _t426 = Json_Null(); return _t426; } endif172:; _t427 = (c == 102); if (!_t427) goto endif176; { bool _t428; _t428 = JsonParser_Match(p, "false"); if (!_t428) goto endif178; { JsonValue _t429; _t429 = Json_Bool(0); return _t429; } endif178:; p->error = "Expected 'false'"; JsonValue _t430; _t430 = Json_Null(); return _t430; } endif176:; _t431 = (c == 110); if (!_t431) goto endif180; { bool _t432; _t432 = JsonParser_Match(p, "null"); if (!_t432) goto endif182; { JsonValue _t433; _t433 = Json_Null(); return _t433; } endif182:; p->error = "Expected 'null'"; JsonValue _t434; _t434 = Json_Null(); return _t434; } endif180:; bool __or_tmp_7; bool __and_tmp_8; _t435 = (c >= 48); if (!_t435) goto else183; _t436 = (c <= 57); *(bool*)&__and_tmp_8 = _t436; goto endif184; else183:; *(bool*)&__and_tmp_8 = 0; endif184:; bool _t437; _t437 = *(bool*)&__and_tmp_8; if (!_t437) goto else185; *(bool*)&__or_tmp_7 = 1; goto endif186; else185:; _t438 = (c == 45); *(bool*)&__or_tmp_7 = _t438; endif186:; bool _t439; _t439 = *(bool*)&__or_tmp_7; if (!_t439) goto endif188; { JsonValue _t440; _t440 = JsonParser_ParseNumber(p); return _t440; } endif188:; p->error = "Unexpected character"; JsonValue _t441; _t441 = Json_Null(); return _t441; } JsonValue Json_Parse(const char* s) { JsonParser _t443; void* _t444; void* _t446; int _t448; int _t451; JsonParser p; unsigned int _t442; _t442 = String_Len(s); _t443 = (JsonParser){.src = s, .pos = 0, .len = _t442, .error = ""}; p = _t443; JsonValue result; _t444 = &p; JsonValue _t445; _t445 = JsonParser_ParseValue(_t444); result = _t445; _t446 = &p; JsonParser_SkipWhitespace(_t446); bool __and_tmp_9; const char* _t447; _t447 = p.error; _t448 = (_t447 == ""); if (!_t448) goto else189; unsigned int _t449; _t449 = p.pos; unsigned int _t450; _t450 = p.len; _t451 = (_t449 != _t450); *(bool*)&__and_tmp_9 = _t451; goto endif190; else189:; *(bool*)&__and_tmp_9 = 0; endif190:; bool _t452; _t452 = *(bool*)&__and_tmp_9; if (!_t452) goto endif192; { p.error = "Trailing data after JSON value"; JsonValue _t453; _t453 = Json_Null(); return _t453; } endif192:; return result; } void Json_StringifyImpl(StringBuilder* sb, JsonValue v) { int _t455; int _t457; int _t460; int _t463; char _t464; char _t466; int _t468; char _t469; int _t471; int _t472; char _t473; int _t476; char _t477; int _t479; char _t480; int _t482; int _t483; char _t484; char _t485; char _t488; char _t489; int _t492; char _t493; int _t454; _t454 = v.tag; _t455 = (_t454 == JsonTagNull); if (!_t455) goto endif194; { StringBuilder_Append(sb, "null"); return; } endif194:; int _t456; _t456 = v.tag; _t457 = (_t456 == JsonTagBool); if (!_t457) goto endif196; { bool _t458; _t458 = v.boolVal; if (!_t458) goto else197; { StringBuilder_Append(sb, "true"); } goto endif198; else197:; { StringBuilder_Append(sb, "false"); } endif198:; return; } endif196:; int _t459; _t459 = v.tag; _t460 = (_t459 == JsonTagNumber); if (!_t460) goto endif200; { double _t461; _t461 = v.numVal; StringBuilder_AppendFloat(sb, _t461); return; } endif200:; int _t462; _t462 = v.tag; _t463 = (_t462 == JsonTagString); if (!_t463) goto endif202; { _t464 = (char)34; StringBuilder_AppendChar(sb, _t464); const char* _t465; _t465 = v.strVal; StringBuilder_Append(sb, _t465); _t466 = (char)34; StringBuilder_AppendChar(sb, _t466); return; } endif202:; int _t467; _t467 = v.tag; _t468 = (_t467 == JsonTagArray); if (!_t468) goto endif204; { _t469 = (char)91; StringBuilder_AppendChar(sb, _t469); unsigned int i; i = 0; while205:; unsigned int _t470; _t470 = v.arrLen; _t471 = (i < _t470); if (!_t471) goto wend206; { _t472 = (i > 0); if (!_t472) goto endif208; { _t473 = (char)44; StringBuilder_AppendChar(sb, _t473); } endif208:; JsonValue* _t474; _t474 = v.arrData; JsonValue _t475; _t475 = _t474[i]; Json_StringifyImpl(sb, _t475); _t476 = i + 1; i = _t476; } goto while205; wend206:; _t477 = (char)93; StringBuilder_AppendChar(sb, _t477); return; } endif204:; int _t478; _t478 = v.tag; _t479 = (_t478 == JsonTagObject); if (!_t479) goto endif210; { _t480 = (char)123; StringBuilder_AppendChar(sb, _t480); unsigned int i; i = 0; while211:; unsigned int _t481; _t481 = v.objLen; _t482 = (i < _t481); if (!_t482) goto wend212; { _t483 = (i > 0); if (!_t483) goto endif214; { _t484 = (char)44; StringBuilder_AppendChar(sb, _t484); } endif214:; _t485 = (char)34; StringBuilder_AppendChar(sb, _t485); const char** _t486; _t486 = v.objKeys; const char* _t487; _t487 = _t486[i]; StringBuilder_Append(sb, _t487); _t488 = (char)34; StringBuilder_AppendChar(sb, _t488); _t489 = (char)58; StringBuilder_AppendChar(sb, _t489); JsonValue* _t490; _t490 = v.objValues; JsonValue _t491; _t491 = _t490[i]; Json_StringifyImpl(sb, _t491); _t492 = i + 1; i = _t492; } goto while211; wend212:; _t493 = (char)125; StringBuilder_AppendChar(sb, _t493); return; } endif210:; } const char* Json_Stringify(JsonValue v) { void* _t495; void* _t496; StringBuilder sb; StringBuilder _t494; _t494 = StringBuilder_New(); sb = _t494; _t495 = &sb; Json_StringifyImpl(_t495, v); _t496 = &sb; const char* _t497; _t497 = StringBuilder_Build(_t496); return _t497; } unsigned int String_Len(const char* s) { unsigned int _t498; _t498 = bux_strlen(s); return _t498; } bool String_IsNull(const char* s) { int _t500; int _t499; _t499 = bux_str_is_null(s); _t500 = (_t499 != 0); return _t500; } bool String_Eq(const char* a, const char* b) { int _t502; int _t501; _t501 = bux_strcmp(a, b); _t502 = (_t501 == 0); return _t502; } const char* String_Concat(const char* a, const char* b) { int _t505; int _t506; char* _t508; unsigned int len_a; unsigned int _t503; _t503 = bux_strlen(a); len_a = _t503; unsigned int len_b; unsigned int _t504; _t504 = bux_strlen(b); len_b = _t504; unsigned int total; _t505 = len_a + len_b; _t506 = _t505 + 1; total = _t506; char* buf; void* _t507; _t507 = bux_alloc(total); _t508 = (char*)_t507; buf = _t508; bux_strcpy(buf, a); bux_strcat(buf, b); return buf; } const char* String_Copy(const char* s) { int _t510; char* _t512; unsigned int len; unsigned int _t509; _t509 = bux_strlen(s); len = _t509; char* buf; _t510 = len + 1; void* _t511; _t511 = bux_alloc(_t510); _t512 = (char*)_t511; buf = _t512; bux_strcpy(buf, s); return buf; } bool String_StartsWith(const char* s, const char* prefix) { int _t515; int _t517; unsigned int s_len; unsigned int _t513; _t513 = bux_strlen(s); s_len = _t513; unsigned int p_len; unsigned int _t514; _t514 = bux_strlen(prefix); p_len = _t514; _t515 = (p_len > s_len); if (!_t515) goto endif216; { return 0; } endif216:; int r; int _t516; _t516 = bux_strncmp(s, prefix, p_len); r = _t516; _t517 = (r == 0); return _t517; } bool String_EndsWith(const char* s, const char* suffix) { int _t520; int _t521; int _t524; unsigned int s_len; unsigned int _t518; _t518 = bux_strlen(s); s_len = _t518; unsigned int suf_len; unsigned int _t519; _t519 = bux_strlen(suffix); suf_len = _t519; _t520 = (suf_len > s_len); if (!_t520) goto endif218; { return 0; } endif218:; unsigned int start; _t521 = s_len - suf_len; start = _t521; const char* tail; const char* _t522; _t522 = bux_str_slice(s, start, suf_len); tail = _t522; bool eq; int _t523; _t523 = bux_strcmp(tail, suffix); _t524 = (_t523 == 0); eq = _t524; return eq; } bool String_Contains(const char* s, const char* substr) { int _t526; int r; int _t525; _t525 = bux_str_contains(s, substr); r = _t525; _t526 = (r != 0); return _t526; } const char* String_Slice(const char* s, unsigned int start, unsigned int len) { const char* _t527; _t527 = bux_str_slice(s, start, len); return _t527; } const char* String_Trim(const char* s) { const char* _t528; _t528 = bux_str_trim(s); return _t528; } const char* String_TrimLeft(const char* s) { const char* _t529; _t529 = bux_str_trim_left(s); return _t529; } const char* String_TrimRight(const char* s) { const char* _t530; _t530 = bux_str_trim_right(s); return _t530; } const char* String_FromInt(int64_t n) { const char* _t531; _t531 = bux_int_to_str(n); return _t531; } int64_t String_ToInt(const char* s) { int64_t _t532; _t532 = bux_str_to_int(s); return _t532; } StringBuilder StringBuilder_New(void) { StringBuilder _t534; void* _t533; _t533 = bux_sb_new(64); _t534 = (StringBuilder){.handle = _t533}; return _t534; } StringBuilder StringBuilder_NewCap(unsigned int cap) { StringBuilder _t536; void* _t535; _t535 = bux_sb_new(cap); _t536 = (StringBuilder){.handle = _t535}; return _t536; } void StringBuilder_Append(StringBuilder* sb, const char* s) { void* _t537; _t537 = sb->handle; bux_sb_append(_t537, s); } void StringBuilder_AppendInt(StringBuilder* sb, int64_t n) { void* _t538; _t538 = sb->handle; bux_sb_append_int(_t538, n); } void StringBuilder_AppendFloat(StringBuilder* sb, double f) { void* _t539; _t539 = sb->handle; bux_sb_append_float(_t539, f); } void StringBuilder_AppendChar(StringBuilder* sb, char c) { void* _t540; _t540 = sb->handle; bux_sb_append_char(_t540, c); } const char* StringBuilder_Build(StringBuilder* sb) { void* _t541; _t541 = sb->handle; const char* _t542; _t542 = bux_sb_build(_t541); return _t542; } void StringBuilder_Free(StringBuilder* sb) { void* _t543; _t543 = sb->handle; bux_sb_free(_t543); } unsigned int String_SplitCount(const char* s, const char* delim) { unsigned int _t544; _t544 = bux_str_split_count(s, delim); return _t544; } const char* String_SplitPart(const char* s, const char* delim, unsigned int index) { const char* _t545; _t545 = bux_str_split_part(s, delim, index); return _t545; } const char* String_Join2(const char* a, const char* b, const char* sep) { const char* _t546; _t546 = bux_str_join2(a, b, sep); return _t546; } const char* String_Chars(const char* s, unsigned int index) { const char* _t547; _t547 = bux_str_slice(s, index, 1); return _t547; } const char* String_Find(const char* haystack, const char* needle) { const char* _t548; _t548 = bux_strstr(haystack, needle); return _t548; } unsigned int String_Offset(const char* pos, const char* base) { unsigned int _t549; _t549 = bux_str_offset(pos, base); return _t549; } const char* String_Replace(const char* s, const char* old, const char* new) { int _t555; int _t557; int _t558; const char* pos; const char* _t550; _t550 = bux_strstr(s, old); pos = _t550; bool _t551; _t551 = String_IsNull(pos); if (!_t551) goto endif220; { return s; } endif220:; unsigned int oldLen; unsigned int _t552; _t552 = bux_strlen(old); oldLen = _t552; unsigned int prefixLen; unsigned int _t553; _t553 = String_Offset(pos, s); prefixLen = _t553; const char* prefix; const char* _t554; _t554 = bux_str_slice(s, 0, prefixLen); prefix = _t554; const char* suffix; _t555 = prefixLen + oldLen; unsigned int _t556; _t556 = bux_strlen(s); _t557 = _t556 - prefixLen; _t558 = _t557 - oldLen; const char* _t559; _t559 = bux_str_slice(s, _t555, _t558); suffix = _t559; const char* temp; const char* _t560; _t560 = String_Concat(prefix, new); temp = _t560; const char* result; const char* _t561; _t561 = String_Concat(temp, suffix); result = _t561; return result; } double String_ToFloat(const char* s) { double _t562; _t562 = bux_str_to_float(s); return _t562; } const char* String_FromBool(bool b) { if (!b) goto endif222; { return "true"; } endif222:; return "false"; } const char* String_FromFloat(double f) { const char* _t563; _t563 = bux_float_to_string(f); return _t563; } const char* String_Format1(const char* pattern, const char* a0) { const char* _t564; _t564 = bux_str_format(pattern, a0, "", "", "", "", "", "", ""); return _t564; } const char* String_Format2(const char* pattern, const char* a0, const char* a1) { const char* _t565; _t565 = bux_str_format(pattern, a0, a1, "", "", "", "", "", ""); return _t565; } const char* String_Format3(const char* pattern, const char* a0, const char* a1, const char* a2) { const char* _t566; _t566 = bux_str_format(pattern, a0, a1, a2, "", "", "", "", ""); return _t566; } void Channel_SendInt(Channel_int* ch, int value) { void* _t568; void* _t569; void* _t567; _t567 = ch->handle; _t568 = &value; _t569 = (void*)_t568; bux_channel_send(_t567, _t569); } int Channel_RecvInt(Channel_int* ch) { void* _t571; void* _t572; int result; result = 0; void* _t570; _t570 = ch->handle; _t571 = &result; _t572 = (void*)_t571; bux_channel_recv(_t570, _t572); return result; } void Channel_SendFloat64(Channel_float64* ch, double value) { void* _t574; void* _t575; void* _t573; _t573 = ch->handle; _t574 = &value; _t575 = (void*)_t574; bux_channel_send(_t573, _t575); } double Channel_RecvFloat64(Channel_float64* ch) { void* _t577; void* _t578; double result; result = 0.0; void* _t576; _t576 = ch->handle; _t577 = &result; _t578 = (void*)_t577; bux_channel_recv(_t576, _t578); return result; } const char* Hash_Sha1(const char* data) { int _t580; int len; unsigned int _t579; _t579 = String_Len(data); _t580 = (int)_t579; len = _t580; void* buf; void* _t581; _t581 = Alloc(20); buf = _t581; bux_sha1(data, len, buf); const char* result; const char* _t582; _t582 = bux_bytes_to_hex(buf, 20); result = _t582; Free(buf); return result; } const char* Hash_Sha256(const char* data) { int _t584; int len; unsigned int _t583; _t583 = String_Len(data); _t584 = (int)_t583; len = _t584; void* buf; void* _t585; _t585 = Alloc(32); buf = _t585; bux_sha256(data, len, buf); const char* result; const char* _t586; _t586 = bux_bytes_to_hex(buf, 32); result = _t586; Free(buf); return result; } const char* Hash_Sha384(const char* data) { int _t588; int len; unsigned int _t587; _t587 = String_Len(data); _t588 = (int)_t587; len = _t588; void* buf; void* _t589; _t589 = Alloc(48); buf = _t589; bux_sha384(data, len, buf); const char* result; const char* _t590; _t590 = bux_bytes_to_hex(buf, 48); result = _t590; Free(buf); return result; } const char* Hash_Sha512(const char* data) { int _t592; int len; unsigned int _t591; _t591 = String_Len(data); _t592 = (int)_t591; len = _t592; void* buf; void* _t593; _t593 = Alloc(64); buf = _t593; bux_sha512(data, len, buf); const char* result; const char* _t594; _t594 = bux_bytes_to_hex(buf, 64); result = _t594; Free(buf); return result; } void Hash_Sha256Raw(const char* data, void* out) { int _t596; unsigned int _t595; _t595 = String_Len(data); _t596 = (int)_t595; bux_sha256(data, _t596, out); } void Hash_Sha384Raw(const char* data, void* out) { int _t598; unsigned int _t597; _t597 = String_Len(data); _t598 = (int)_t597; bux_sha384(data, _t598, out); } void Hash_Sha512Raw(const char* data, void* out) { int _t600; unsigned int _t599; _t599 = String_Len(data); _t600 = (int)_t599; bux_sha512(data, _t600, out); } int Hash_Sha1Size(void) { return 20; } int Hash_Sha256Size(void) { return 32; } int Hash_Sha384Size(void) { return 48; } int Hash_Sha512Size(void) { return 64; } const char* Base64_Encode(const char* s) { int _t602; unsigned int _t601; _t601 = String_Len(s); _t602 = (int)_t601; const char* _t603; _t603 = bux_base64_encode(s, _t602); return _t603; } const char* Base64_Decode(const char* s) { int _t605; void* _t606; int outlen; outlen = 0; unsigned int _t604; _t604 = String_Len(s); _t605 = (int)_t604; _t606 = &outlen; const char* _t607; _t607 = bux_base64_decode(s, _t605, _t606); return _t607; } const char* Base64URL_Encode(const char* s) { int _t609; unsigned int _t608; _t608 = String_Len(s); _t609 = (int)_t608; const char* _t610; _t610 = bux_base64url_encode(s, _t609); return _t610; } const char* Base64URL_Decode(const char* s) { int _t612; void* _t613; int outlen; outlen = 0; unsigned int _t611; _t611 = String_Len(s); _t612 = (int)_t611; _t613 = &outlen; const char* _t614; _t614 = bux_base64url_decode(s, _t612, _t613); return _t614; } const char* Hmac_Sha256(const char* key, const char* message) { int _t616; int _t618; int kl; unsigned int _t615; _t615 = String_Len(key); _t616 = (int)_t615; kl = _t616; int ml; unsigned int _t617; _t617 = String_Len(message); _t618 = (int)_t617; ml = _t618; void* buf; void* _t619; _t619 = Alloc(32); buf = _t619; bux_hmac_sha256(key, kl, message, ml, buf); const char* result; const char* _t620; _t620 = bux_bytes_to_hex(buf, 32); result = _t620; Free(buf); return result; } void Hmac_Sha256Raw(const char* key, const char* message, void* out) { int _t622; int _t624; unsigned int _t621; _t621 = String_Len(key); _t622 = (int)_t621; unsigned int _t623; _t623 = String_Len(message); _t624 = (int)_t623; bux_hmac_sha256(key, _t622, message, _t624, out); } const char* Hmac_Sha256Base64(const char* key, const char* message) { int _t626; int _t628; const char* _t630; int kl; unsigned int _t625; _t625 = String_Len(key); _t626 = (int)_t625; kl = _t626; int ml; unsigned int _t627; _t627 = String_Len(message); _t628 = (int)_t627; ml = _t628; void* buf; void* _t629; _t629 = Alloc(32); buf = _t629; bux_hmac_sha256(key, kl, message, ml, buf); const char* result; _t630 = (const char*)buf; const char* _t631; _t631 = bux_base64_encode(_t630, 32); result = _t631; Free(buf); return result; } const char* Hmac_Sha384(const char* key, const char* message) { int _t633; int _t635; int kl; unsigned int _t632; _t632 = String_Len(key); _t633 = (int)_t632; kl = _t633; int ml; unsigned int _t634; _t634 = String_Len(message); _t635 = (int)_t634; ml = _t635; void* buf; void* _t636; _t636 = Alloc(48); buf = _t636; bux_hmac_sha384(key, kl, message, ml, buf); const char* result; const char* _t637; _t637 = bux_bytes_to_hex(buf, 48); result = _t637; Free(buf); return result; } void Hmac_Sha384Raw(const char* key, const char* message, void* out) { int _t639; int _t641; unsigned int _t638; _t638 = String_Len(key); _t639 = (int)_t638; unsigned int _t640; _t640 = String_Len(message); _t641 = (int)_t640; bux_hmac_sha384(key, _t639, message, _t641, out); } const char* Hmac_Sha384Base64(const char* key, const char* message) { int _t643; int _t645; const char* _t647; int kl; unsigned int _t642; _t642 = String_Len(key); _t643 = (int)_t642; kl = _t643; int ml; unsigned int _t644; _t644 = String_Len(message); _t645 = (int)_t644; ml = _t645; void* buf; void* _t646; _t646 = Alloc(48); buf = _t646; bux_hmac_sha384(key, kl, message, ml, buf); const char* result; _t647 = (const char*)buf; const char* _t648; _t648 = bux_base64_encode(_t647, 48); result = _t648; Free(buf); return result; } const char* Hmac_Sha512(const char* key, const char* message) { int _t650; int _t652; int kl; unsigned int _t649; _t649 = String_Len(key); _t650 = (int)_t649; kl = _t650; int ml; unsigned int _t651; _t651 = String_Len(message); _t652 = (int)_t651; ml = _t652; void* buf; void* _t653; _t653 = Alloc(64); buf = _t653; bux_hmac_sha512(key, kl, message, ml, buf); const char* result; const char* _t654; _t654 = bux_bytes_to_hex(buf, 64); result = _t654; Free(buf); return result; } void Hmac_Sha512Raw(const char* key, const char* message, void* out) { int _t656; int _t658; unsigned int _t655; _t655 = String_Len(key); _t656 = (int)_t655; unsigned int _t657; _t657 = String_Len(message); _t658 = (int)_t657; bux_hmac_sha512(key, _t656, message, _t658, out); } const char* Hmac_Sha512Base64(const char* key, const char* message) { int _t660; int _t662; const char* _t664; int kl; unsigned int _t659; _t659 = String_Len(key); _t660 = (int)_t659; kl = _t660; int ml; unsigned int _t661; _t661 = String_Len(message); _t662 = (int)_t661; ml = _t662; void* buf; void* _t663; _t663 = Alloc(64); buf = _t663; bux_hmac_sha512(key, kl, message, ml, buf); const char* result; _t664 = (const char*)buf; const char* _t665; _t665 = bux_base64_encode(_t664, 64); result = _t665; Free(buf); return result; } const char* Random_Bytes(int n) { int _t666; unsigned int _t667; int _t670; const char* _t671; _t666 = (n <= 0); if (!_t666) goto endif224; { return ""; } endif224:; void* buf; _t667 = (unsigned int)n; void* _t668; _t668 = Alloc(_t667); buf = _t668; int _t669; _t669 = bux_random_bytes(buf, n); _t670 = (_t669 != 1); if (!_t670) goto endif226; { Free(buf); return ""; } endif226:; _t671 = (const char*)buf; return _t671; } const char* Random_Hex(int n) { int _t672; unsigned int _t673; int _t676; _t672 = (n <= 0); if (!_t672) goto endif228; { return ""; } endif228:; void* buf; _t673 = (unsigned int)n; void* _t674; _t674 = Alloc(_t673); buf = _t674; int _t675; _t675 = bux_random_bytes(buf, n); _t676 = (_t675 != 1); if (!_t676) goto endif230; { Free(buf); return ""; } endif230:; const char* result; const char* _t677; _t677 = bux_bytes_to_hex(buf, n); result = _t677; Free(buf); return result; } const char* Random_Base64(int n) { int _t678; unsigned int _t679; int _t682; const char* _t683; _t678 = (n <= 0); if (!_t678) goto endif232; { return ""; } endif232:; void* buf; _t679 = (unsigned int)n; void* _t680; _t680 = Alloc(_t679); buf = _t680; int _t681; _t681 = bux_random_bytes(buf, n); _t682 = (_t681 != 1); if (!_t682) goto endif234; { Free(buf); return ""; } endif234:; const char* result; _t683 = (const char*)buf; const char* _t684; _t684 = bux_base64_encode(_t683, n); result = _t684; Free(buf); return result; } unsigned int Random_Uint32(void) { int _t687; unsigned int* _t688; unsigned int _t689; void* buf; void* _t685; _t685 = Alloc(4); buf = _t685; int _t686; _t686 = bux_random_bytes(buf, 4); _t687 = (_t686 != 1); if (!_t687) goto endif236; { Free(buf); return 0; } endif236:; unsigned int* ptr; _t688 = (unsigned int*)buf; ptr = _t688; unsigned int val; _t689 = *ptr; val = _t689; Free(buf); return val; } const char* Aes_GenerateKey(void) { unsigned int _t690; int _t693; const char* _t694; void* buf; _t690 = (unsigned int)AES_KEY_SIZE; void* _t691; _t691 = Alloc(_t690); buf = _t691; int _t692; _t692 = bux_random_bytes(buf, AES_KEY_SIZE); _t693 = (_t692 != 1); if (!_t693) goto endif238; { Free(buf); return ""; } endif238:; _t694 = (const char*)buf; return _t694; } const char* Aes_GenerateIV(void) { unsigned int _t695; int _t698; const char* _t699; void* buf; _t695 = (unsigned int)AES_IV_SIZE; void* _t696; _t696 = Alloc(_t695); buf = _t696; int _t697; _t697 = bux_random_bytes(buf, AES_IV_SIZE); _t698 = (_t697 != 1); if (!_t698) goto endif240; { Free(buf); return ""; } endif240:; _t699 = (const char*)buf; return _t699; } const char* Aes_CbcEncrypt(const char* plain, const char* key, const char* iv) { int _t701; void* _t702; int outlen; outlen = 0; unsigned int _t700; _t700 = String_Len(plain); _t701 = (int)_t700; _t702 = &outlen; const char* _t703; _t703 = bux_aes_256_cbc_encrypt(plain, _t701, key, iv, _t702); return _t703; } const char* Aes_CbcDecrypt(const char* cipher, const char* key, const char* iv) { int _t705; void* _t706; int outlen; outlen = 0; unsigned int _t704; _t704 = String_Len(cipher); _t705 = (int)_t704; _t706 = &outlen; const char* _t707; _t707 = bux_aes_256_cbc_decrypt(cipher, _t705, key, iv, _t706); return _t707; } const char* Aes_GcmEncrypt(const char* plain, const char* key, const char* iv, void* tag) { int _t709; void* _t710; int outlen; outlen = 0; unsigned int _t708; _t708 = String_Len(plain); _t709 = (int)_t708; _t710 = &outlen; const char* _t711; _t711 = bux_aes_256_gcm_encrypt(plain, _t709, key, iv, tag, _t710); return _t711; } const char* Aes_GcmDecrypt(const char* cipher, const char* key, const char* iv, const char* tag) { int _t713; void* _t714; int outlen; outlen = 0; unsigned int _t712; _t712 = String_Len(cipher); _t713 = (int)_t712; _t714 = &outlen; const char* _t715; _t715 = bux_aes_256_gcm_decrypt(cipher, _t713, key, iv, tag, _t714); return _t715; } const char* Jwt_MakeHeader(JwtAlg alg) { int _t716; int _t717; int _t718; int _t719; int _t720; int _t721; int _t722; int _t723; int _t724; _t716 = (alg == JwtAlg_HS256); if (!_t716) goto endif242; { return "{\"alg\":\"HS256\",\"typ\":\"JWT\"}"; } endif242:; _t717 = (alg == JwtAlg_HS384); if (!_t717) goto endif244; { return "{\"alg\":\"HS384\",\"typ\":\"JWT\"}"; } endif244:; _t718 = (alg == JwtAlg_HS512); if (!_t718) goto endif246; { return "{\"alg\":\"HS512\",\"typ\":\"JWT\"}"; } endif246:; _t719 = (alg == JwtAlg_RS256); if (!_t719) goto endif248; { return "{\"alg\":\"RS256\",\"typ\":\"JWT\"}"; } endif248:; _t720 = (alg == JwtAlg_RS384); if (!_t720) goto endif250; { return "{\"alg\":\"RS384\",\"typ\":\"JWT\"}"; } endif250:; _t721 = (alg == JwtAlg_RS512); if (!_t721) goto endif252; { return "{\"alg\":\"RS512\",\"typ\":\"JWT\"}"; } endif252:; _t722 = (alg == JwtAlg_ES256); if (!_t722) goto endif254; { return "{\"alg\":\"ES256\",\"typ\":\"JWT\"}"; } endif254:; _t723 = (alg == JwtAlg_ES384); if (!_t723) goto endif256; { return "{\"alg\":\"ES384\",\"typ\":\"JWT\"}"; } endif256:; _t724 = (alg == JwtAlg_EdDSA); if (!_t724) goto endif258; { return "{\"alg\":\"EdDSA\",\"typ\":\"JWT\"}"; } endif258:; return "{\"alg\":\"none\",\"typ\":\"JWT\"}"; } const char* Jwt_Sign(JwtAlg alg, const char* signingInput, const char* key) { int _t725; const char* _t727; int _t729; const char* _t731; int _t733; const char* _t735; int _t737; int _t740; int _t742; int _t745; int _t747; int _t750; int _t752; int _t755; int _t757; int _t760; int _t762; _t725 = (alg == JwtAlg_HS256); if (!_t725) goto endif260; { void* buf; void* _t726; _t726 = Alloc(32); buf = _t726; Hmac_Sha256Raw(key, signingInput, buf); const char* result; _t727 = (const char*)buf; const char* _t728; _t728 = bux_base64_encode(_t727, 32); result = _t728; Free(buf); return result; } endif260:; _t729 = (alg == JwtAlg_HS384); if (!_t729) goto endif262; { void* buf; void* _t730; _t730 = Alloc(48); buf = _t730; Hmac_Sha384Raw(key, signingInput, buf); const char* result; _t731 = (const char*)buf; const char* _t732; _t732 = bux_base64_encode(_t731, 48); result = _t732; Free(buf); return result; } endif262:; _t733 = (alg == JwtAlg_HS512); if (!_t733) goto endif264; { void* buf; void* _t734; _t734 = Alloc(64); buf = _t734; Hmac_Sha512Raw(key, signingInput, buf); const char* result; _t735 = (const char*)buf; const char* _t736; _t736 = bux_base64_encode(_t735, 64); result = _t736; Free(buf); return result; } endif264:; _t737 = (alg == JwtAlg_RS256); if (!_t737) goto endif266; { const char* raw; const char* _t738; _t738 = Rsa_SignSha256(key, signingInput); raw = _t738; unsigned int _t739; _t739 = String_Len(raw); _t740 = (int)_t739; const char* _t741; _t741 = bux_base64_encode(raw, _t740); return _t741; } endif266:; _t742 = (alg == JwtAlg_RS384); if (!_t742) goto endif268; { const char* raw; const char* _t743; _t743 = Rsa_SignSha384(key, signingInput); raw = _t743; unsigned int _t744; _t744 = String_Len(raw); _t745 = (int)_t744; const char* _t746; _t746 = bux_base64_encode(raw, _t745); return _t746; } endif268:; _t747 = (alg == JwtAlg_RS512); if (!_t747) goto endif270; { const char* raw; const char* _t748; _t748 = Rsa_SignSha512(key, signingInput); raw = _t748; unsigned int _t749; _t749 = String_Len(raw); _t750 = (int)_t749; const char* _t751; _t751 = bux_base64_encode(raw, _t750); return _t751; } endif270:; _t752 = (alg == JwtAlg_ES256); if (!_t752) goto endif272; { const char* raw; const char* _t753; _t753 = Ecdsa_SignP256(key, signingInput); raw = _t753; unsigned int _t754; _t754 = String_Len(raw); _t755 = (int)_t754; const char* _t756; _t756 = bux_base64_encode(raw, _t755); return _t756; } endif272:; _t757 = (alg == JwtAlg_ES384); if (!_t757) goto endif274; { const char* raw; const char* _t758; _t758 = Ecdsa_SignP384(key, signingInput); raw = _t758; unsigned int _t759; _t759 = String_Len(raw); _t760 = (int)_t759; const char* _t761; _t761 = bux_base64_encode(raw, _t760); return _t761; } endif274:; _t762 = (alg == JwtAlg_EdDSA); if (!_t762) goto endif276; { const char* _t763; _t763 = Ed25519_Sign(key, signingInput); return _t763; } endif276:; return ""; } bool Jwt_Verify(JwtAlg alg, const char* signingInput, const char* signatureB64, const char* key) { int _t764; const char* _t766; int _t769; const char* _t771; int _t774; const char* _t776; int _t779; int _t781; int _t783; int _t785; int _t787; int _t789; _t764 = (alg == JwtAlg_HS256); if (!_t764) goto endif278; { void* expectBuf; void* _t765; _t765 = Alloc(32); expectBuf = _t765; Hmac_Sha256Raw(key, signingInput, expectBuf); const char* expectB64; _t766 = (const char*)expectBuf; const char* _t767; _t767 = bux_base64_encode(_t766, 32); expectB64 = _t767; Free(expectBuf); bool _t768; _t768 = String_Eq(expectB64, signatureB64); return _t768; } endif278:; _t769 = (alg == JwtAlg_HS384); if (!_t769) goto endif280; { void* expectBuf; void* _t770; _t770 = Alloc(48); expectBuf = _t770; Hmac_Sha384Raw(key, signingInput, expectBuf); const char* expectB64; _t771 = (const char*)expectBuf; const char* _t772; _t772 = bux_base64_encode(_t771, 48); expectB64 = _t772; Free(expectBuf); bool _t773; _t773 = String_Eq(expectB64, signatureB64); return _t773; } endif280:; _t774 = (alg == JwtAlg_HS512); if (!_t774) goto endif282; { void* expectBuf; void* _t775; _t775 = Alloc(64); expectBuf = _t775; Hmac_Sha512Raw(key, signingInput, expectBuf); const char* expectB64; _t776 = (const char*)expectBuf; const char* _t777; _t777 = bux_base64_encode(_t776, 64); expectB64 = _t777; Free(expectBuf); bool _t778; _t778 = String_Eq(expectB64, signatureB64); return _t778; } endif282:; _t779 = (alg == JwtAlg_RS256); if (!_t779) goto endif284; { bool _t780; _t780 = Rsa_VerifySha256(key, signingInput, signatureB64); return _t780; } endif284:; _t781 = (alg == JwtAlg_RS384); if (!_t781) goto endif286; { bool _t782; _t782 = Rsa_VerifySha384(key, signingInput, signatureB64); return _t782; } endif286:; _t783 = (alg == JwtAlg_RS512); if (!_t783) goto endif288; { bool _t784; _t784 = Rsa_VerifySha512(key, signingInput, signatureB64); return _t784; } endif288:; _t785 = (alg == JwtAlg_ES256); if (!_t785) goto endif290; { bool _t786; _t786 = Ecdsa_VerifyP256(key, signingInput, signatureB64); return _t786; } endif290:; _t787 = (alg == JwtAlg_ES384); if (!_t787) goto endif292; { bool _t788; _t788 = Ecdsa_VerifyP384(key, signingInput, signatureB64); return _t788; } endif292:; _t789 = (alg == JwtAlg_EdDSA); if (!_t789) goto endif294; { bool _t790; _t790 = Ed25519_Verify(key, signatureB64, signingInput); return _t790; } endif294:; return 0; } const char* Jwt_Encode(const char* headerJson, const char* payloadJson, JwtAlg alg, const char* key) { const char* headerB64; const char* _t791; _t791 = Base64URL_Encode(headerJson); headerB64 = _t791; const char* payloadB64; const char* _t792; _t792 = Base64URL_Encode(payloadJson); payloadB64 = _t792; const char* signingInput; const char* _t793; _t793 = String_Concat(headerB64, "."); signingInput = _t793; const char* signingInputFull; const char* _t794; _t794 = String_Concat(signingInput, payloadB64); signingInputFull = _t794; const char* sigB64; const char* _t795; _t795 = Jwt_Sign(alg, signingInputFull, key); sigB64 = _t795; const char* part1; const char* _t796; _t796 = String_Concat(signingInputFull, "."); part1 = _t796; const char* _t797; _t797 = String_Concat(part1, sigB64); return _t797; } bool Jwt_Decode(const char* token, JwtAlg alg, const char* key, const char** headerOut, const char** payloadOut) { int _t799; int _t806; unsigned int partCount; unsigned int _t798; _t798 = bux_str_split_count(token, "."); partCount = _t798; _t799 = (partCount != 3); if (!_t799) goto endif296; { return 0; } endif296:; const char* headerB64; const char* _t800; _t800 = bux_str_split_part(token, ".", 0); headerB64 = _t800; const char* payloadB64; const char* _t801; _t801 = bux_str_split_part(token, ".", 1); payloadB64 = _t801; const char* sigB64; const char* _t802; _t802 = bux_str_split_part(token, ".", 2); sigB64 = _t802; const char* input; const char* _t803; _t803 = String_Concat(headerB64, "."); input = _t803; const char* signingInput; const char* _t804; _t804 = String_Concat(input, payloadB64); signingInput = _t804; bool _t805; _t805 = Jwt_Verify(alg, signingInput, sigB64, key); _t806 = !_t805; if (!_t806) goto endif298; { return 0; } endif298:; const char* _t807; _t807 = Base64URL_Decode(headerB64); headerOut[0] = _t807; const char* _t808; _t808 = Base64URL_Decode(payloadB64); payloadOut[0] = _t808; return 1; } const char* Jwt_EncodeHS256(const char* payloadJson, const char* secret) { const char* header; const char* _t809; _t809 = Jwt_MakeHeader(JwtAlg_HS256); header = _t809; const char* _t810; _t810 = Jwt_Encode(header, payloadJson, JwtAlg_HS256, secret); return _t810; } const char* Jwt_EncodeHS384(const char* payloadJson, const char* secret) { const char* header; const char* _t811; _t811 = Jwt_MakeHeader(JwtAlg_HS384); header = _t811; const char* _t812; _t812 = Jwt_Encode(header, payloadJson, JwtAlg_HS384, secret); return _t812; } const char* Jwt_EncodeHS512(const char* payloadJson, const char* secret) { const char* header; const char* _t813; _t813 = Jwt_MakeHeader(JwtAlg_HS512); header = _t813; const char* _t814; _t814 = Jwt_Encode(header, payloadJson, JwtAlg_HS512, secret); return _t814; } const char* Jwt_EncodeRS256(const char* payloadJson, const char* pemPrivateKey) { const char* header; const char* _t815; _t815 = Jwt_MakeHeader(JwtAlg_RS256); header = _t815; const char* _t816; _t816 = Jwt_Encode(header, payloadJson, JwtAlg_RS256, pemPrivateKey); return _t816; } const char* Jwt_EncodeES256(const char* payloadJson, const char* pemPrivateKey) { const char* header; const char* _t817; _t817 = Jwt_MakeHeader(JwtAlg_ES256); header = _t817; const char* _t818; _t818 = Jwt_Encode(header, payloadJson, JwtAlg_ES256, pemPrivateKey); return _t818; } const char* Jwt_EncodeEdDSA(const char* payloadJson, const char* rawPrivKey) { const char* header; const char* _t819; _t819 = Jwt_MakeHeader(JwtAlg_EdDSA); header = _t819; const char* _t820; _t820 = Jwt_Encode(header, payloadJson, JwtAlg_EdDSA, rawPrivKey); return _t820; } const char* Ecdsa_SignP256(const char* pemPrivateKey, const char* data) { int _t822; int _t824; void* _t825; int siglen; siglen = 0; unsigned int _t821; _t821 = String_Len(pemPrivateKey); _t822 = (int)_t821; unsigned int _t823; _t823 = String_Len(data); _t824 = (int)_t823; _t825 = &siglen; const char* _t826; _t826 = bux_ecdsa_sign_p256(pemPrivateKey, _t822, data, _t824, _t825); return _t826; } const char* Ecdsa_SignP256Base64(const char* pemPrivateKey, const char* data) { int _t829; const char* raw; const char* _t827; _t827 = Ecdsa_SignP256(pemPrivateKey, data); raw = _t827; unsigned int _t828; _t828 = String_Len(raw); _t829 = (int)_t828; const char* _t830; _t830 = bux_base64_encode(raw, _t829); return _t830; } bool Ecdsa_VerifyP256(const char* pemPublicKey, const char* data, const char* signature) { int _t832; int _t834; int _t836; int _t838; int r; unsigned int _t831; _t831 = String_Len(pemPublicKey); _t832 = (int)_t831; unsigned int _t833; _t833 = String_Len(data); _t834 = (int)_t833; unsigned int _t835; _t835 = String_Len(signature); _t836 = (int)_t835; int _t837; _t837 = bux_ecdsa_verify_p256(pemPublicKey, _t832, data, _t834, signature, _t836); r = _t837; _t838 = (r == 1); return _t838; } bool Ecdsa_VerifyP256Base64(const char* pemPublicKey, const char* data, const char* signatureB64) { int _t840; void* _t841; int outlen; outlen = 0; const char* sig; unsigned int _t839; _t839 = String_Len(signatureB64); _t840 = (int)_t839; _t841 = &outlen; const char* _t842; _t842 = bux_base64_decode(signatureB64, _t840, _t841); sig = _t842; bool _t843; _t843 = Ecdsa_VerifyP256(pemPublicKey, data, sig); return _t843; } const char* Ecdsa_SignP384(const char* pemPrivateKey, const char* data) { int _t845; int _t847; void* _t848; int siglen; siglen = 0; unsigned int _t844; _t844 = String_Len(pemPrivateKey); _t845 = (int)_t844; unsigned int _t846; _t846 = String_Len(data); _t847 = (int)_t846; _t848 = &siglen; const char* _t849; _t849 = bux_ecdsa_sign_p384(pemPrivateKey, _t845, data, _t847, _t848); return _t849; } const char* Ecdsa_SignP384Base64(const char* pemPrivateKey, const char* data) { int _t852; const char* raw; const char* _t850; _t850 = Ecdsa_SignP384(pemPrivateKey, data); raw = _t850; unsigned int _t851; _t851 = String_Len(raw); _t852 = (int)_t851; const char* _t853; _t853 = bux_base64_encode(raw, _t852); return _t853; } bool Ecdsa_VerifyP384(const char* pemPublicKey, const char* data, const char* signature) { int _t855; int _t857; int _t859; int _t861; int r; unsigned int _t854; _t854 = String_Len(pemPublicKey); _t855 = (int)_t854; unsigned int _t856; _t856 = String_Len(data); _t857 = (int)_t856; unsigned int _t858; _t858 = String_Len(signature); _t859 = (int)_t858; int _t860; _t860 = bux_ecdsa_verify_p384(pemPublicKey, _t855, data, _t857, signature, _t859); r = _t860; _t861 = (r == 1); return _t861; } bool Ecdsa_VerifyP384Base64(const char* pemPublicKey, const char* data, const char* signatureB64) { int _t863; void* _t864; int outlen; outlen = 0; const char* sig; unsigned int _t862; _t862 = String_Len(signatureB64); _t863 = (int)_t862; _t864 = &outlen; const char* _t865; _t865 = bux_base64_decode(signatureB64, _t863, _t864); sig = _t865; bool _t866; _t866 = Ecdsa_VerifyP384(pemPublicKey, data, sig); return _t866; } bool Ed25519_Keypair(void* pubKey, void* privKey) { int _t868; int r; int _t867; _t867 = bux_ed25519_keypair(pubKey, privKey); r = _t867; _t868 = (r == 1); return _t868; } const char* Ed25519_KeypairBase64(void) { unsigned int _t869; unsigned int _t871; int _t874; const char* _t875; const char* _t877; void* pubBuf; _t869 = (unsigned int)ED25519_PUBKEY_SIZE; void* _t870; _t870 = Alloc(_t869); pubBuf = _t870; void* priv; _t871 = (unsigned int)ED25519_PRIVKEY_SIZE; void* _t872; _t872 = Alloc(_t871); priv = _t872; int _t873; _t873 = bux_ed25519_keypair(pubBuf, priv); _t874 = (_t873 != 1); if (!_t874) goto endif300; { Free(pubBuf); Free(priv); return ""; } endif300:; const char* pubB64; _t875 = (const char*)pubBuf; const char* _t876; _t876 = bux_base64_encode(_t875, ED25519_PUBKEY_SIZE); pubB64 = _t876; const char* privB64; _t877 = (const char*)priv; const char* _t878; _t878 = bux_base64_encode(_t877, ED25519_PRIVKEY_SIZE); privB64 = _t878; Free(pubBuf); Free(priv); const char* pair; const char* _t879; _t879 = String_Concat(pubB64, ":"); pair = _t879; const char* _t880; _t880 = String_Concat(pair, privB64); return _t880; } const char* Ed25519_Sign(const char* privKey, const char* data) { unsigned int _t881; int _t884; int _t886; const char* _t887; void* sig; _t881 = (unsigned int)ED25519_SIG_SIZE; void* _t882; _t882 = Alloc(_t881); sig = _t882; unsigned int _t883; _t883 = String_Len(data); _t884 = (int)_t883; int _t885; _t885 = bux_ed25519_sign(privKey, data, _t884, sig); _t886 = (_t885 != 1); if (!_t886) goto endif302; { Free(sig); return ""; } endif302:; _t887 = (const char*)sig; return _t887; } const char* Ed25519_SignBase64(const char* privKey, const char* data) { int _t890; const char* sig; const char* _t888; _t888 = Ed25519_Sign(privKey, data); sig = _t888; unsigned int _t889; _t889 = String_Len(sig); _t890 = (_t889 == 0); if (!_t890) goto endif304; { return ""; } endif304:; const char* _t891; _t891 = bux_base64_encode(sig, ED25519_SIG_SIZE); return _t891; } bool Ed25519_Verify(const char* pubKey, const char* signature, const char* data) { int _t893; int _t895; int r; unsigned int _t892; _t892 = String_Len(data); _t893 = (int)_t892; int _t894; _t894 = bux_ed25519_verify(pubKey, signature, data, _t893); r = _t894; _t895 = (r == 1); return _t895; } bool Ed25519_VerifyBase64(const char* pubKey, const char* signatureB64, const char* data) { int _t897; void* _t898; int _t900; int outlen; outlen = 0; const char* sig; unsigned int _t896; _t896 = String_Len(signatureB64); _t897 = (int)_t896; _t898 = &outlen; const char* _t899; _t899 = bux_base64_decode(signatureB64, _t897, _t898); sig = _t899; _t900 = (outlen != ED25519_SIG_SIZE); if (!_t900) goto endif306; { return 0; } endif306:; bool _t901; _t901 = Ed25519_Verify(pubKey, sig, data); return _t901; } const char* Rsa_SignSha256(const char* pemPrivateKey, const char* data) { int _t903; int _t905; void* _t906; int siglen; siglen = 0; unsigned int _t902; _t902 = String_Len(pemPrivateKey); _t903 = (int)_t902; unsigned int _t904; _t904 = String_Len(data); _t905 = (int)_t904; _t906 = &siglen; const char* _t907; _t907 = bux_rsa_sign_sha256(pemPrivateKey, _t903, data, _t905, _t906); return _t907; } const char* Rsa_SignSha384(const char* pemPrivateKey, const char* data) { int _t909; int _t911; void* _t912; int siglen; siglen = 0; unsigned int _t908; _t908 = String_Len(pemPrivateKey); _t909 = (int)_t908; unsigned int _t910; _t910 = String_Len(data); _t911 = (int)_t910; _t912 = &siglen; const char* _t913; _t913 = bux_rsa_sign_sha384(pemPrivateKey, _t909, data, _t911, _t912); return _t913; } const char* Rsa_SignSha512(const char* pemPrivateKey, const char* data) { int _t915; int _t917; void* _t918; int siglen; siglen = 0; unsigned int _t914; _t914 = String_Len(pemPrivateKey); _t915 = (int)_t914; unsigned int _t916; _t916 = String_Len(data); _t917 = (int)_t916; _t918 = &siglen; const char* _t919; _t919 = bux_rsa_sign_sha512(pemPrivateKey, _t915, data, _t917, _t918); return _t919; } const char* Rsa_SignSha256Base64(const char* pemPrivateKey, const char* data) { int _t922; const char* raw; const char* _t920; _t920 = Rsa_SignSha256(pemPrivateKey, data); raw = _t920; unsigned int _t921; _t921 = String_Len(raw); _t922 = (int)_t921; const char* _t923; _t923 = bux_base64_encode(raw, _t922); return _t923; } const char* Rsa_SignSha384Base64(const char* pemPrivateKey, const char* data) { int _t926; const char* raw; const char* _t924; _t924 = Rsa_SignSha384(pemPrivateKey, data); raw = _t924; unsigned int _t925; _t925 = String_Len(raw); _t926 = (int)_t925; const char* _t927; _t927 = bux_base64_encode(raw, _t926); return _t927; } const char* Rsa_SignSha512Base64(const char* pemPrivateKey, const char* data) { int _t930; const char* raw; const char* _t928; _t928 = Rsa_SignSha512(pemPrivateKey, data); raw = _t928; unsigned int _t929; _t929 = String_Len(raw); _t930 = (int)_t929; const char* _t931; _t931 = bux_base64_encode(raw, _t930); return _t931; } bool Rsa_VerifySha256(const char* pemPublicKey, const char* data, const char* signature) { int _t933; int _t935; int _t937; int _t939; int r; unsigned int _t932; _t932 = String_Len(pemPublicKey); _t933 = (int)_t932; unsigned int _t934; _t934 = String_Len(data); _t935 = (int)_t934; unsigned int _t936; _t936 = String_Len(signature); _t937 = (int)_t936; int _t938; _t938 = bux_rsa_verify_sha256(pemPublicKey, _t933, data, _t935, signature, _t937); r = _t938; _t939 = (r == 1); return _t939; } bool Rsa_VerifySha384(const char* pemPublicKey, const char* data, const char* signature) { int _t941; int _t943; int _t945; int _t947; int r; unsigned int _t940; _t940 = String_Len(pemPublicKey); _t941 = (int)_t940; unsigned int _t942; _t942 = String_Len(data); _t943 = (int)_t942; unsigned int _t944; _t944 = String_Len(signature); _t945 = (int)_t944; int _t946; _t946 = bux_rsa_verify_sha384(pemPublicKey, _t941, data, _t943, signature, _t945); r = _t946; _t947 = (r == 1); return _t947; } bool Rsa_VerifySha512(const char* pemPublicKey, const char* data, const char* signature) { int _t949; int _t951; int _t953; int _t955; int r; unsigned int _t948; _t948 = String_Len(pemPublicKey); _t949 = (int)_t948; unsigned int _t950; _t950 = String_Len(data); _t951 = (int)_t950; unsigned int _t952; _t952 = String_Len(signature); _t953 = (int)_t952; int _t954; _t954 = bux_rsa_verify_sha512(pemPublicKey, _t949, data, _t951, signature, _t953); r = _t954; _t955 = (r == 1); return _t955; } bool Rsa_VerifySha256Base64(const char* pemPublicKey, const char* data, const char* signatureB64) { int _t957; void* _t958; int outlen; outlen = 0; const char* sig; unsigned int _t956; _t956 = String_Len(signatureB64); _t957 = (int)_t956; _t958 = &outlen; const char* _t959; _t959 = bux_base64_decode(signatureB64, _t957, _t958); sig = _t959; bool _t960; _t960 = Rsa_VerifySha256(pemPublicKey, data, sig); return _t960; } bool Rsa_VerifySha384Base64(const char* pemPublicKey, const char* data, const char* signatureB64) { int _t962; void* _t963; int outlen; outlen = 0; const char* sig; unsigned int _t961; _t961 = String_Len(signatureB64); _t962 = (int)_t961; _t963 = &outlen; const char* _t964; _t964 = bux_base64_decode(signatureB64, _t962, _t963); sig = _t964; bool _t965; _t965 = Rsa_VerifySha384(pemPublicKey, data, sig); return _t965; } bool Rsa_VerifySha512Base64(const char* pemPublicKey, const char* data, const char* signatureB64) { int _t967; void* _t968; int outlen; outlen = 0; const char* sig; unsigned int _t966; _t966 = String_Len(signatureB64); _t967 = (int)_t966; _t968 = &outlen; const char* _t969; _t969 = bux_base64_decode(signatureB64, _t967, _t968); sig = _t969; bool _t970; _t970 = Rsa_VerifySha512(pemPublicKey, data, sig); return _t970; } int Main(void) { Result _t971; Result _t972; int _t974; int _t978; Result r1; _t971 = (Result){.tag = Result_Ok}; r1 = _t971; r1.data.Ok_0 = 42; Result r2; _t972 = (Result){.tag = Result_Err}; r2 = _t972; r2.data.Err_0 = "error message"; Result_Tag _t973; _t973 = r1.tag; _t974 = (_t973 == Result_Ok); if (!_t974) goto endif308; { PrintLine("r1 is Ok:"); Result_Data _t975; _t975 = r1.data; int _t976; _t976 = _t975.Ok_0; PrintInt(_t976); PrintLine(""); } endif308:; Result_Tag _t977; _t977 = r2.tag; _t978 = (_t977 == Result_Err); if (!_t978) goto endif310; { PrintLine("r2 is Err:"); Result_Data _t979; _t979 = r2.data; const char* _t980; _t980 = _t979.Err_0; PrintLine(_t980); } endif310:; return 0; } /* C entry point wrapper */ extern int g_argc; extern char** g_argv; int main(int argc, char** argv) { g_argc = argc; g_argv = argv; return Main(); }