Add RSS CGI, also quite a lot of refactoring
[blerg.git] / common / escapery.c
1 #include <string.h>
2 #include <stdlib.h>
3 #include "escapery.h"
4
5 char *xml_escape(const char *str) {
6         return xml_escape_data(str, strlen(str));
7 }
8
9 char *xml_escape_data(const char *str, int len) {
10         char *r = malloc(len * 5 + 1);  /* Up to 5x the space if they're all &'s */
11         int i, j;
12
13         for (i = 0, j = 0; i < len; i++) {
14                 switch(str[i]) {
15                 case '<':
16                         memcpy(r + j, "&lt;", 4);
17                         j += 4;
18                         break;
19                 case '>':
20                         memcpy(r + j, "&gt;", 4);
21                         j += 4;
22                         break;
23                 case '&':
24                         memcpy(r + j, "&amp;", 5);
25                         j += 5;
26                         break;
27                 default:
28                         r[j] = str[i];
29                         j += 1;
30                 }
31         }
32         r[j] = 0;
33
34         return r;
35 }