Fix ordering in perl subscription_list
[blerg.git] / common / escapery.c
1 /* Blerg is (C) 2011 The Dominion of Awesome, and is distributed under a
2  * BSD-style license.  Please see the COPYING file for details.
3  */
4 #include <string.h>
5 #include <stdlib.h>
6 #include "escapery.h"
7
8 char *xml_escape(const char *str) {
9         return xml_escape_data(str, strlen(str));
10 }
11
12 char *xml_escape_data(const char *str, int len) {
13         char *r = malloc(len * 5 + 1);  /* Up to 5x the space if they're all &'s */
14         int i, j;
15
16         for (i = 0, j = 0; i < len; i++) {
17                 switch(str[i]) {
18                 case '<':
19                         memcpy(r + j, "&lt;", 4);
20                         j += 4;
21                         break;
22                 case '>':
23                         memcpy(r + j, "&gt;", 4);
24                         j += 4;
25                         break;
26                 case '&':
27                         memcpy(r + j, "&amp;", 5);
28                         j += 5;
29                         break;
30                 default:
31                         r[j] = str[i];
32                         j += 1;
33                 }
34         }
35         r[j] = 0;
36
37         return r;
38 }