diff options
Diffstat (limited to 'escape.c')
-rw-r--r-- | escape.c | 35 |
1 files changed, 35 insertions, 0 deletions
diff --git a/escape.c b/escape.c new file mode 100644 index 0000000..be801e2 --- /dev/null +++ b/escape.c @@ -0,0 +1,35 @@ +#include <stdio.h> +#include <string.h> +#include <stdlib.h> +#include <ctype.h> + +#include "request.h" + +char *real_escape_string(const byte *src, int size) +{ + char *escaped; + char *pos; + + if (memchr(src, '\0', size - 1) || memchr(src, '\n', size) || memchr(src, '\r', size)) { + fprintf(stderr, "cannot handle this string\n"); + return NULL; + } + + pos = escaped = malloc(sizeof(char) * (size * 2 + 1)); + + if (escaped == NULL) { + fprintf(stderr, "malloc failed: %m\n"); + return NULL; + } + + for (int i = 0; i < size; ++i) { + if (!isalnum(src[i])) { + *escaped++ = '\\'; + } + *escaped++ = src[i]; + } + + *escaped = '\0'; + + return pos; +} |