aisl-sdk/components/validate.c

119 lines
1.8 KiB
C
Raw Normal View History

2019-04-16 14:26:37 +02:00
/******************************************************************************
*
* Copyright (c) 2017-2019 by Löwenware Ltd
* Please, refer LICENSE file for legal information
*
******************************************************************************/
/**
* @file validate.c
* @author Ilja Kartašov <ik@lowenware.com>
* @brief Validation component source file
*
* @see https://lowenware.com/aisl/
*/
#include <ctype.h>
#include <stddef.h>
#include <stdlib.h>
2019-04-25 15:04:45 +02:00
#include "log.h"
2019-04-16 14:26:37 +02:00
#include "validate.h"
int
ax_validate_email(const char *value)
2019-04-16 14:26:37 +02:00
{
const char *at = NULL,
*dot = NULL,
*i = value;
2019-04-16 14:26:37 +02:00
char c;
while ( (c = *i) != 0 ) {
switch(c) {
case '@':
if (at || i == value)
break;
2019-04-16 14:26:37 +02:00
at = i++;
dot = NULL;
continue;
case '"':
case '(':
case ')':
case ',':
case ';':
case ':':
case '<':
case '>':
case '[':
case '\\':
case ']':
case ' ':
/* These characters are allowed in double quotes only, but it is not
* implemented in this validator, so we count them as forbidden
*/
break;
case '!':
case '#':
case '$':
case '%':
case '&':
case '\'':
case '*':
case '+':
case '/':
case '=':
case '?':
case '^':
case '_':
case '`':
case '{':
case '|':
case '}':
case '~':
/* Allowed special local-part characters */
if (!at) {
i++;
2019-04-16 14:26:37 +02:00
continue;
}
break;
2019-04-16 14:26:37 +02:00
case '-':
/* Allowed characters */
i++;
continue;
case '.':
if (dot && i == dot+1)
2019-04-16 14:26:37 +02:00
break;
if (at) {
if (i == at+1 || *(i+1) == 0)
2019-04-16 14:26:37 +02:00
break;
} else {
if (i == value || *(i+1) == '@')
2019-04-16 14:26:37 +02:00
break;
}
2019-04-16 14:26:37 +02:00
dot = i++;
continue;
2019-04-16 14:26:37 +02:00
default:
2019-04-25 15:04:45 +02:00
if (isalnum(c))
{
i++;
2019-04-16 14:26:37 +02:00
continue;
2019-04-25 15:04:45 +02:00
}
break;
2019-04-16 14:26:37 +02:00
}
return (int)(i-value)+1;
}
if (!at || !dot)
return -1;
return 0;
}