diff options
author | ꦌꦫꦶꦏ꧀ꦦꦿꦧꦮꦑꦩꦭ꧀ <erik@darapsa.co.id> | 2022-09-15 08:45:40 +0800 |
---|---|---|
committer | ꦌꦫꦶꦏ꧀ꦦꦿꦧꦮꦑꦩꦭ꧀ <erik@darapsa.co.id> | 2022-09-15 08:45:40 +0800 |
commit | dacf25e962238c679c69b77a915689cdf72416d5 (patch) | |
tree | c22495931a068aa08743218d6deb8baec3e1e969 /shopify.c | |
parent | a631d884fcccdcdf826f1a6e5a4db22237b398c1 (diff) |
Generalise the struct comparison
Both structs have char * as their first elements.
Pointers to the structs are pointers to each of their first elements.
(https://beej.us/guide/bgc/html/split/structs-ii-more-fun-with-structs.html#fake-oop)
So, they would be char ** (not char *, as they're pointers to char *).
We then take the objects they point to, to get char *, so that we can
compare them with strcmp.
Diffstat (limited to 'shopify.c')
-rw-r--r-- | shopify.c | 35 |
1 files changed, 14 insertions, 21 deletions
@@ -89,16 +89,9 @@ static inline void clear(struct shopify_param params[]) } } -static int paramcmp(const void *param1, const void *param2) +static int keycmp(const void *struct1, const void *struct2) { - return strcmp(((struct shopify_param *)param1)->key, - ((struct shopify_param *)param2)->key); -} - -static int sessioncmp(const void *session1, const void *session2) -{ - return strcmp(((struct session *)session1)->shop, - ((struct session *)session2)->shop); + return strcmp(*(char **)struct1, *(char **)struct2); } bool shopify_valid(struct MHD_Connection *conn, const char *url, @@ -113,11 +106,11 @@ bool shopify_valid(struct MHD_Connection *conn, const char *url, nparams++; if (!nparams) return false; - qsort(*params, nparams, sizeof(struct shopify_param), paramcmp); + qsort(*params, nparams, sizeof(struct shopify_param), keycmp); char *shop = NULL; struct shopify_param *param = bsearch(&(struct shopify_param) { "shop" }, *params, nparams, - sizeof(struct shopify_param), paramcmp); + sizeof(struct shopify_param), keycmp); if (param) shop = param->val; if (!shop || !regex_match(shop)) { @@ -142,7 +135,7 @@ bool shopify_valid(struct MHD_Connection *conn, const char *url, } char *hmac = NULL; param = bsearch(&(struct shopify_param){ "hmac" }, *params, nparams, - sizeof(struct shopify_param), paramcmp); + sizeof(struct shopify_param), keycmp); if (param) hmac = param->val; if (!hmac || !crypt_maccmp(secret_key, query, hmac)) { @@ -155,13 +148,13 @@ bool shopify_valid(struct MHD_Connection *conn, const char *url, return true; char *state = ((struct shopify_param *)bsearch(&(struct shopify_param) { "state" }, *params, nparams, - sizeof(struct shopify_param), paramcmp))->val; + sizeof(struct shopify_param), keycmp))->val; int nsessions = 0; while (sessions[nsessions].shop) nsessions++; - qsort(sessions, nsessions, sizeof(struct session), sessioncmp); + qsort(sessions, nsessions, sizeof(struct session), keycmp); struct session *session = bsearch(&(struct session){ shop }, sessions, - nsessions, sizeof(struct session), sessioncmp); + nsessions, sizeof(struct session), keycmp); if (strcmp(state, session->nonce)) { clear(*params); return false; @@ -190,23 +183,23 @@ enum MHD_Result shopify_respond(struct shopify_param params[], const char *url, nparams++; char *shop = ((struct shopify_param *)bsearch(&(struct shopify_param) { "shop" }, params, nparams, - sizeof(struct shopify_param), paramcmp))->val; + sizeof(struct shopify_param), keycmp))->val; const size_t shop_len = strlen(shop); char *host = ((struct shopify_param *)bsearch(&(struct shopify_param) { "host" }, params, nparams, - sizeof(struct shopify_param), paramcmp))->val; + sizeof(struct shopify_param), keycmp))->val; struct shopify_param *param = bsearch(&(struct shopify_param) { "embedded" }, params, nparams, - sizeof(struct shopify_param), paramcmp); + sizeof(struct shopify_param), keycmp); bool embedded = param && !strcmp(param->val, "1"); char *decoded_host; base64_decode((unsigned char *)host, &decoded_host); int nsessions = 0; while (sessions[nsessions].shop) nsessions++; - qsort(sessions, nsessions, sizeof(struct session), sessioncmp); + qsort(sessions, nsessions, sizeof(struct session), keycmp); struct session *session = bsearch(&(struct session){ shop }, sessions, - nsessions, sizeof(struct session), sessioncmp); + nsessions, sizeof(struct session), keycmp); const size_t key_len = strlen(key); char frame[FRAME_LEN + shop_len + 1]; sprintf(frame, FRAME, shop); @@ -216,7 +209,7 @@ enum MHD_Result shopify_respond(struct shopify_param params[], const char *url, &(struct shopify_param){ "code" }, params, nparams, sizeof(struct shopify_param), - paramcmp))->val; + keycmp))->val; char *token = NULL; request_token(decoded_host, key, secret_key, code, &token); token_parse(token, session); |