simple-discuss/src/utils.c

77 lines
1.5 KiB
C

#include "string_buffer.h"
#include "utils.h"
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#define VALID_FOR_USERNAME(c) (isalpha(c) || c == '_')
int
contain_special(const char *s)
{
return strchr(s, '&') || strchr(s, '"') || strchr(s, '\'') || strchr(s, '<') || strchr(s, '>');
}
int
is_valid_username(const char *s)
{
char c;
while ((c = *s++)) {
if (!VALID_FOR_USERNAME(c)) {
return 0;
}
}
return 1;
}
int
is_valid_password(const char *s)
{
char c;
unsigned char b;
while ((c = *s++)) {
b = (unsigned char) c;
if (b < 32) {
return 0;
}
}
return 1;
}
char *
mk_specialchars(const char *input)
{
StringBuffer _buffer, *buffer;
char c;
char *retval;
buffer = &_buffer;
sb_init_empty(buffer);
while ((c = *(input++))) {
switch (c) {
case '&':
sb_add_string(buffer, "&amp;");
break;
case '"':
sb_add_string(buffer, "&quot;");
break;
case '\'':
sb_add_string(buffer, "&#039;");
break;
case '<':
sb_add_string(buffer, "&lt;");
break;
case '>':
sb_add_string(buffer, "&gt;");
break;
default:
sb_add_char(buffer, c);
}
}
SB_MOVE(buffer, retval);
return retval;
}