summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--include/str.h3
-rw-r--r--src/client.c2
-rw-r--r--src/server.c4
-rw-r--r--src/str.c18
4 files changed, 21 insertions, 6 deletions
diff --git a/include/str.h b/include/str.h
index 277a6f3..5b3e04c 100644
--- a/include/str.h
+++ b/include/str.h
@@ -20,6 +20,9 @@ typedef array(char) str;
typedef arraybuf(char) strbuf;
#define NILSBUF ((strbuf) { 0, 0, NULL })
+// return if two strings are equal
+bool str_eq(str s1, str s2);
+
// compares two strings by length and ASCII values. return value:
// < 0 if s1 < s2
// = 0 if s1 = s2
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;
}
diff --git a/src/str.c b/src/str.c
index 4f6d5c7..e14591d 100644
--- a/src/str.c
+++ b/src/str.c
@@ -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)