From 8dbc90fbafc0b7fd40a70cffc0295a1d2d22cef0 Mon Sep 17 00:00:00 2001 From: Con Kolivas Date: Fri, 1 Aug 2014 11:21:28 +1000 Subject: [PATCH] Add a helper function for safely checking if a buffer matches a command --- src/libckpool.c | 16 ++++++++++++++++ src/libckpool.h | 1 + 2 files changed, 17 insertions(+) diff --git a/src/libckpool.c b/src/libckpool.c index 0a478649..af3633a1 100644 --- a/src/libckpool.c +++ b/src/libckpool.c @@ -1081,6 +1081,22 @@ int safecmp(const char *a, const char *b) return (strcmp(a, b)); } +/* Returns whether there is a case insensitive match of buf to cmd, safely + * handling NULL or zero length strings. */ +bool cmdmatch(const char *buf, const char *cmd) +{ + int cmdlen, buflen; + + if (!buf) + return false; + buflen = strlen(buf); + if (!buflen) + return false; + cmdlen = strlen(cmd); + if (buflen < cmdlen) + return false; + return !strncasecmp(buf, cmd, cmdlen); +} static const char base64[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; diff --git a/src/libckpool.h b/src/libckpool.h index 08d30242..e2af60c9 100644 --- a/src/libckpool.h +++ b/src/libckpool.h @@ -415,6 +415,7 @@ bool _hex2bin(void *p, const void *vhexstr, size_t len, const char *file, const char *http_base64(const char *src); void b58tobin(char *b58bin, const char *b58); int safecmp(const char *a, const char *b); +bool cmdmatch(const char *buf, const char *cmd); void address_to_pubkeytxn(char *pkh, const char *addr); int ser_number(uchar *s, int32_t val);