Change types for validators and use assertions

This commit is contained in:
Alex D. 2021-01-04 17:21:18 +00:00
parent b4c807b37c
commit c952340638
Signed by: caskd
GPG Key ID: F92BA85F61F4C173
11 changed files with 75 additions and 48 deletions

View File

@ -26,6 +26,7 @@
#include "public/tags.h"
#include "public/types.h"
#include <assert.h>
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
@ -34,7 +35,8 @@
signed long
Assm_user(char* buf, IRC_User* in, size_t len, bool useorig)
{
if (buf == NULL || in == NULL) return ERR_UIRC_NULL_ARGS;
assert(buf != NULL);
assert(in != NULL);
char* pos = buf;
if (in->nick == NULL && in->host != NULL) {
if (!safe_strcpy(&pos, in->host, len - (unsigned long) (pos - buf))) return ERR_UIRC_BUFFER_ERR;
@ -53,7 +55,7 @@ Assm_user(char* buf, IRC_User* in, size_t len, bool useorig)
if (!safe_strcpy(&pos, in->host, len - (unsigned long) (pos - buf))) return ERR_UIRC_BUFFER_ERR;
}
} else
return ERR_UIRC_NULL_ARGS;
return ERR_UIRC_INVALID_FORMAT;
return pos - buf;
}
@ -61,7 +63,8 @@ Assm_user(char* buf, IRC_User* in, size_t len, bool useorig)
signed long
Assm_tags(char* buf, IRC_Tags* in, size_t len)
{
if (buf == NULL || in == NULL) return ERR_UIRC_NULL_ARGS;
assert(buf != NULL);
assert(in != NULL);
char* pos = buf;
struct tagmapping tagmps[] = { { .name = "time", .assg = &in->time }, { .name = "account", .assg = &in->account },
{ .name = "batch", .assg = &in->batch }, { .name = "label", .assg = &in->label },
@ -92,7 +95,8 @@ Assm_tags(char* buf, IRC_Tags* in, size_t len)
signed long
Assm_mesg(char* buf, IRC_Message* in, size_t len)
{
if (buf == NULL || in == NULL) return ERR_UIRC_BUFFER_ERR;
assert(buf != NULL);
assert(in != NULL);
char* pos = buf;
signed long cnt, ret;
#ifdef UIRC_IRCV3

View File

@ -18,6 +18,9 @@
#include "public/capabilities.h"
#include "public/errors.h"
#include <assert.h>
#include <stdlib.h>
#include <string.h>
@ -45,9 +48,10 @@ const char* const IRC_v3_Caps[] = {
};
#ifdef UIRC_HELPERS
int
signed int
Tok_CAPS(char* caps)
{
assert(caps != NULL);
int temp = 0;
char* cur = NULL;
if ((cur = strtok(caps, " ")) != NULL) {

View File

@ -21,12 +21,13 @@
#include "public/commands.h"
#include "public/errors.h"
#include <assert.h>
#include <string.h>
signed short
Ircmd_stoi(const char* str)
{
if (str == NULL) return ERR_UIRC_NULL_ARGS;
assert(str != NULL);
for (signed short i = UIRC_FCMD; i <= (signed short) UIRC_LCMD; i++) {
if (IRC_Cmds[i] != NULL && strcmp(IRC_Cmds[i], str) == 0) return i;
}

View File

@ -64,7 +64,7 @@ enum caps {
* \sa caps
* \sa https://ircv3.net/registry#capabilities
*/
int Tok_CAPS(char* caps);
signed int Tok_CAPS(char* caps);
#endif /* UIRC_HELPERS */
/*!

View File

@ -20,12 +20,11 @@
#define UIRC_GUARD_ERRORS
enum uirc_errors {
ERR_UIRC_GENERIC = -1,
ERR_UIRC_NULL_ARGS = -2,
ERR_UIRC_INVALID_FORMAT = -3,
ERR_UIRC_BUFFER_ERR = -4,
ERR_UIRC_UNKNOWN_TOKEN = -5,
ERR_UIRC_VAL_FAILED = -10,
ERR_UIRC_GENERIC = -1,
ERR_UIRC_INVALID_FORMAT = -2,
ERR_UIRC_BUFFER_ERR = -3,
ERR_UIRC_UNKNOWN_TOKEN = -4,
ERR_UIRC_VAL_FAILED = -5,
};
#endif /* UIRC_GUARD_ERRORS */

View File

@ -22,7 +22,7 @@
#define UIRC_GUARD_TAGS
#ifdef UIRC_HELPERS
size_t Assm_tag_timestamp(char* buf, size_t len, time_t time);
ssize_t Assm_tag_timestamp(char* buf, size_t len, time_t time);
#endif /* UIRC_HELPERS */
#endif /* UIRC_GUARD_TAGS */

View File

@ -63,16 +63,16 @@ signed int Tok_mesg(char* str, IRC_Message* out);
* \param[out] source Source of the PING (server)
* \param[out] target Target of the PING (server or client)
*/
void Tok_cmd_PING(IRC_Message* mesg, char** source, char** target);
void Tok_cmd_PING(const IRC_Message* mesg, char** source, char** target);
/*!
* \brief Tokenizer for cases where the first argument is optional
*
* This function is a private helper for cases where the first argument might be missing but the second isn't
* \param[in] mesg IRC_Message struct
* \param[out] optarg Optional argument if given or NULL
* \param[out] optarg Optional argument if found or NULL
* \param[out] target Required argument
*/
void Tok_FArgOpt(IRC_Message* mesg, char** optarg, char** reqarg);
void Tok_FArgOpt(const IRC_Message* mesg, char** optarg, char** reqarg);
#endif /* UIRC_HELPERS */
#endif /* UIRC_GUARD_TOKENIZERS */

View File

@ -18,14 +18,16 @@
#include "types.h"
#include <sys/types.h>
#ifndef UIRC_GUARD_VALIDATORS
#define UIRC_GUARD_VALIDATORS
signed int Val_type_nocrlf(char* str);
signed int Val_type_nospcl(char* str);
signed int Val_type_noblcm(char* str);
signed int Val_channame(char* chan);
signed int Val_mesg(IRC_Message* mesg);
size_t Val_type_nocrlf(const char* str);
size_t Val_type_nospcl(const char* str);
size_t Val_type_noblcm(const char* str);
size_t Val_channame(char* chan);
signed short Val_mesg(IRC_Message* mesg);
#endif /* UIRC_GUARD_VALIDATORS */

View File

@ -18,12 +18,14 @@
#include "public/tags.h"
#include <assert.h>
#include <time.h>
#ifdef UIRC_HELPERS
size_t
ssize_t
Assm_tag_timestamp(char* buf, size_t len, time_t time)
{
assert(buf != NULL);
return strftime(buf, len, "%Y-%m-%dT%H:%M:%S.000Z", gmtime(&time));
}
#endif /* UIRC_HELPERS */

View File

@ -26,6 +26,7 @@
#include "public/tags.h"
#include "public/types.h"
#include <assert.h>
#include <ctype.h>
#include <stdbool.h>
#include <stdio.h>
@ -36,7 +37,8 @@
signed int
Tok_tags(char* str, IRC_Tags* out)
{
if (str == NULL || out == NULL) return ERR_UIRC_NULL_ARGS;
assert(str != NULL);
assert(out != NULL);
char * cval, *cpos = str, *ctag = NULL;
bool clientbound;
const struct tagmapping tagmps[] = {
@ -72,6 +74,8 @@ Tok_tags(char* str, IRC_Tags* out)
signed int
Tok_user(char* str, IRC_User* out, bool useorig)
{
assert(str != NULL);
assert(out != NULL);
char* pos = (*str == ':') ? str + 1 : str;
if ((out->host = strchr(pos, '@')) != NULL) *(out->host++) = '\0';
if (useorig && (out->orig = strchr(pos, '%')) != NULL) *(out->orig++) = '\0';
@ -94,7 +98,8 @@ Tok_user(char* str, IRC_User* out, bool useorig)
signed int
Tok_mesg(char* str, IRC_Message* out)
{
if (str == NULL || out == NULL) return ERR_UIRC_NULL_ARGS;
assert(str != NULL);
assert(out != NULL);
int ret;
char *progr = str, *command;
#ifdef UIRC_IRCV3
@ -113,10 +118,9 @@ Tok_mesg(char* str, IRC_Message* out)
if ((ret = Tok_user(prefix, &out->name, false)) < 0) return ret;
} else
return ERR_UIRC_INVALID_FORMAT;
skip_spaces(&progr);
}
skip_spaces(&progr);
if (isalnum(*progr) && (command = strtok_mr(&progr, " ")) != NULL) {
signed short temp;
if (isalpha(*command)) {
@ -130,7 +134,6 @@ Tok_mesg(char* str, IRC_Message* out)
}
} else
return ERR_UIRC_INVALID_FORMAT;
skip_spaces(&progr);
short i;
@ -150,8 +153,11 @@ Tok_mesg(char* str, IRC_Message* out)
#ifdef UIRC_HELPERS
void
Tok_cmd_PING(IRC_Message* mesg, char** source, char** target)
Tok_cmd_PING(const IRC_Message* mesg, char** source, char** target)
{
assert(mesg != NULL);
assert(source != NULL);
assert(target != NULL);
*source = (mesg->args[0] == NULL && mesg->trailing) ? mesg->args[2] : (mesg->args[1] != NULL) ? mesg->args[0] : NULL;
*target = (mesg->args[1] == NULL) ? mesg->args[0] : mesg->args[1];
}
@ -161,8 +167,11 @@ Tok_cmd_PING(IRC_Message* mesg, char** source, char** target)
*/
void
Tok_FArgOpt(IRC_Message* mesg, char** optarg, char** reqarg)
Tok_FArgOpt(const IRC_Message* mesg, char** optarg, char** reqarg)
{
assert(mesg != NULL);
assert(optarg != NULL);
assert(reqarg != NULL);
*optarg = (mesg->args[1] != NULL) ? mesg->args[0] : NULL;
*reqarg = (mesg->args[1] != NULL) ? mesg->args[1] : mesg->args[0];
}

View File

@ -21,45 +21,50 @@
#include "public/errors.h"
#include "public/types.h"
#include <assert.h>
#include <stdio.h>
#include <string.h>
signed int
Val_type_nocrlf(char* str)
size_t
Val_type_nocrlf(const char* str)
{
if (str == NULL) return ERR_UIRC_NULL_ARGS;
assert(str != NULL);
const char* save = str;
for (; *str; str++) {
if (*str == '\r' || *str == '\n') return 0;
}
return 1;
return str - save;
}
signed int
Val_type_nospcl(char* str)
size_t
Val_type_nospcl(const char* str)
{
if (str == NULL) return ERR_UIRC_NULL_ARGS;
assert(str != NULL);
const char* save = str;
for (; *str; str++) {
if (*str == ' ' || *str == ':') return 0;
}
return 1;
return str - save;
}
signed int
Val_type_noblcm(char* str)
size_t
Val_type_noblcm(const char* str)
{
if (str == NULL) return ERR_UIRC_NULL_ARGS;
assert(str != NULL);
const char* save = str;
for (; *str; str++) {
if (*str == '\a' || *str == ',') return 0;
}
return 1;
return str - save;
}
signed int
size_t
Val_channame(char* chan)
{
if (chan == NULL) return ERR_UIRC_NULL_ARGS;
assert(chan != NULL);
if (*chan != '#' && *chan != '+' && *chan != '!' && *chan != '&') return 0;
char* clps = ++chan;
const char* save = chan;
char* clps = ++chan;
if ((clps = strchr(chan, ':')) != NULL) {
*clps = '\0';
if (Val_type_nospcl(chan) != 1 || Val_type_nocrlf(chan) != 1 || Val_type_noblcm(chan) != 1) {
@ -70,13 +75,14 @@ Val_channame(char* chan)
chan = ++clps;
}
if (Val_type_nospcl(chan) != 1 || Val_type_nocrlf(chan) != 1 || Val_type_noblcm(chan) != 1) return 0;
return 1;
return save - chan;
}
signed int
// TODO: Work on the logic for this
signed short
Val_mesg(IRC_Message* mesg)
{
if (mesg == NULL) return ERR_UIRC_NULL_ARGS;
assert(mesg != NULL);
for (unsigned int i = 0; mesg->args[i] != NULL; i++) {
if (Val_type_nocrlf(mesg->args[i]) != 1) return ERR_UIRC_VAL_FAILED;
if (!(mesg->args[i + 1] == NULL && mesg->trailing)) {