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/
|
|
|
|
*/
|
|
|
|
|
2019-04-17 10:17:27 +02:00
|
|
|
#include <ctype.h>
|
|
|
|
#include <stddef.h>
|
|
|
|
#include <stdlib.h>
|
2019-04-16 14:26:37 +02:00
|
|
|
#include "validate.h"
|
|
|
|
|
|
|
|
|
|
|
|
int
|
2019-04-17 10:17:27 +02:00
|
|
|
aislx_validate_email(const char *value)
|
2019-04-16 14:26:37 +02:00
|
|
|
{
|
2019-04-17 10:17:27 +02:00
|
|
|
const char *at = NULL,
|
|
|
|
*dot = NULL,
|
|
|
|
*i = value;
|
2019-04-16 14:26:37 +02:00
|
|
|
char c;
|
|
|
|
|
2019-04-17 10:17:27 +02:00
|
|
|
while ( (c = *i) != 0 ) {
|
|
|
|
switch(c) {
|
|
|
|
case '@':
|
|
|
|
if (at || i == value)
|
|
|
|
break;
|
2019-04-16 14:26:37 +02:00
|
|
|
|
2019-04-17 10:17:27 +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;
|
2019-04-17 10:17:27 +02:00
|
|
|
}
|
|
|
|
break;
|
2019-04-16 14:26:37 +02:00
|
|
|
|
2019-04-17 10:17:27 +02:00
|
|
|
case '-':
|
|
|
|
/* Allowed characters */
|
|
|
|
i++;
|
|
|
|
continue;
|
|
|
|
|
|
|
|
case '.':
|
|
|
|
if (dot && i == dot+1)
|
2019-04-16 14:26:37 +02:00
|
|
|
break;
|
|
|
|
|
2019-04-17 10:17:27 +02:00
|
|
|
if (at) {
|
|
|
|
if (i == at+1 || *(i+1) == 0)
|
2019-04-16 14:26:37 +02:00
|
|
|
break;
|
2019-04-17 10:17:27 +02:00
|
|
|
} else {
|
|
|
|
if (i == value || *(i+1) == '@')
|
2019-04-16 14:26:37 +02:00
|
|
|
break;
|
2019-04-17 10:17:27 +02:00
|
|
|
}
|
2019-04-16 14:26:37 +02:00
|
|
|
|
2019-04-17 10:17:27 +02:00
|
|
|
dot = i++;
|
|
|
|
continue;
|
2019-04-16 14:26:37 +02:00
|
|
|
|
2019-04-17 10:17:27 +02:00
|
|
|
default:
|
|
|
|
if ( isalnum(c) == 0 )
|
2019-04-16 14:26:37 +02:00
|
|
|
continue;
|
2019-04-17 10:17:27 +02:00
|
|
|
break;
|
2019-04-16 14:26:37 +02:00
|
|
|
}
|
|
|
|
return (int)(i-value)+1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!at || !dot)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|