diff options
author | Lizzy Fleckenstein <lizzy@vlhl.dev> | 2024-06-19 21:11:50 +0200 |
---|---|---|
committer | Lizzy Fleckenstein <lizzy@vlhl.dev> | 2024-06-19 21:11:50 +0200 |
commit | dfe8ddb9ff8b3f90036a0e47220b6c180cc95ea7 (patch) | |
tree | 7b8ca0aa82b923d384f234b7d3364d1f752692c0 /src | |
parent | 47984162974a8f7d9903d352567005ac569c5a87 (diff) |
str.c: fix str_cmp and add str_eq
Signed-off-by: Lizzy Fleckenstein <lizzy@vlhl.dev>
Diffstat (limited to 'src')
-rw-r--r-- | src/client.c | 2 | ||||
-rw-r--r-- | src/server.c | 4 | ||||
-rw-r--r-- | src/str.c | 18 |
3 files changed, 18 insertions, 6 deletions
diff --git a/src/client.c b/src/client.c index 08eae9d..b15a3df 100644 --- a/src/client.c +++ b/src/client.c @@ -98,7 +98,7 @@ bool handle_players(str *w, client *c) return false; if (!deser_u64(w, &p->id)) return false; - if (str_cmp(p->name, c->name) == 0) + if (str_eq(p->name, c->name)) c->self_id = p->id; p->name = str_clone(p->name); } diff --git a/src/server.c b/src/server.c index bcca3a9..adbbe80 100644 --- a/src/server.c +++ b/src/server.c @@ -263,7 +263,7 @@ bool handle_hi(str pkt, player *p, game *g) if (! (deser_str(&pkt, &name) && deser_str(&pkt, &pass))) return false; - if (str_cmp(g->passphrase, pass) != 0) { + if (!str_eq(g->passphrase, pass)) { printf("wrong passphrase from %.*s\n", PSTR(name)); // TODO: log ip ? SEND_PKT(p->conn, CPKT_FAIL, ser_fail_reason(&pkt, FAIL_WRONG_PASS);) return true; // valid pkt, but invalid passphrase @@ -271,7 +271,7 @@ bool handle_hi(str pkt, player *p, game *g) for (size_t i = 0; i < g->players.len; i++) { player *p2 = &g->players.data[i]; - if (p2->auth && str_cmp(p2->name, name) == 0) { + if (p2->auth && str_eq(p2->name, name)) { SEND_PKT(p->conn, CPKT_FAIL, ser_fail_reason(&pkt, FAIL_ALREADY_ONLINE);) return true; } @@ -6,12 +6,24 @@ #include <string.h> #include "str.h" -int str_cmp(str s1, str s2) +bool str_eq(str s1, str s2) { if (s1.len != s2.len) - return (int) s1.len - (int) s2.len; + return false; + + return memcmp(s1.data, s2.data, s1.len) == 0; +} + - return memcmp(s1.data, s2.data, s1.len); +int str_cmp(str s1, str s2) +{ + size_t min_len = s1.len < s2.len ? s1.len : s2.len; + int cmp = memcmp(s1.data, s2.data, min_len); + + if (cmp == 0) + return (int) s1.len - (int) s2.len; + else + return cmp; } static bool match_char(char c, str tokens) |