119 lines
1.8 KiB
C
119 lines
1.8 KiB
C
/******************************************************************************
|
|
*
|
|
* 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>
|
|
#include "log.h"
|
|
#include "validate.h"
|
|
|
|
|
|
int
|
|
ax_validate_email(const char *value)
|
|
{
|
|
const char *at = NULL,
|
|
*dot = NULL,
|
|
*i = value;
|
|
char c;
|
|
|
|
while ( (c = *i) != 0 ) {
|
|
switch(c) {
|
|
case '@':
|
|
if (at || i == value)
|
|
break;
|
|
|
|
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++;
|
|
continue;
|
|
}
|
|
break;
|
|
|
|
case '-':
|
|
/* Allowed characters */
|
|
i++;
|
|
continue;
|
|
|
|
case '.':
|
|
if (dot && i == dot+1)
|
|
break;
|
|
|
|
if (at) {
|
|
if (i == at+1 || *(i+1) == 0)
|
|
break;
|
|
} else {
|
|
if (i == value || *(i+1) == '@')
|
|
break;
|
|
}
|
|
|
|
dot = i++;
|
|
continue;
|
|
|
|
default:
|
|
if (isalnum(c))
|
|
{
|
|
i++;
|
|
continue;
|
|
}
|
|
break;
|
|
}
|
|
return (int)(i-value)+1;
|
|
}
|
|
|
|
if (!at || !dot)
|
|
return -1;
|
|
|
|
return 0;
|
|
}
|