File: mod_encode_xml.c

package info (click to toggle)
spl 1.0~pre6-4
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 2,364 kB
  • ctags: 2,070
  • sloc: ansic: 16,614; yacc: 3,182; sh: 299; makefile: 167; xml: 156
file content (137 lines) | stat: -rw-r--r-- 3,574 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/*
 *  SPL - The SPL Programming Language
 *  Copyright (C) 2004, 2005  Clifford Wolf <clifford@clifford.at>
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 *  mod_encode_xml.c: A simple XML encoding function
 */

/**
 * A simple XML encoding function
 */

#include <stdlib.h>
#include "spl.h"
#include "compat.h"

extern void SPL_ABI(spl_mod_encode_xml_init)(struct spl_vm *vm, struct spl_module *mod, int restore);
extern void SPL_ABI(spl_mod_encode_xml_done)(struct spl_vm *vm, struct spl_module *mod);

static char *xml_encode(const char *source)
{
	int source_i, target_i;

	for (source_i = target_i = 0; source[source_i]; source_i++)
		switch (source[source_i]) {
			case '&':
				/* &amp; */
				target_i += 5;
				break;
			case '<':
				/* &lt; */
				target_i += 4;
				break;
			case '>':
				/* &gt; */
				target_i += 4;
				break;
			case '"':
				/* &quot; */
				target_i += 6;
				break;
			case '\'':
				/* &apos; */
				target_i += 6;
				break;
			default:
				target_i++;
		}

	char *target = malloc(target_i+1);

	for (source_i = target_i = 0; source[source_i]; source_i++)
		switch (source[source_i]) {
			case '&':
				/* &amp; */
				target[target_i++] = '&';
				target[target_i++] = 'a';
				target[target_i++] = 'm';
				target[target_i++] = 'p';
				target[target_i++] = ';';
				break;
			case '<':
				/* &lt; */
				target[target_i++] = '&';
				target[target_i++] = 'l';
				target[target_i++] = 't';
				target[target_i++] = ';';
				break;
			case '>':
				/* &gt; */
				target[target_i++] = '&';
				target[target_i++] = 'g';
				target[target_i++] = 't';
				target[target_i++] = ';';
				break;
			case '"':
				/* &quot; */
				target[target_i++] = '&';
				target[target_i++] = 'q';
				target[target_i++] = 'u';
				target[target_i++] = 'o';
				target[target_i++] = 't';
				target[target_i++] = ';';
				break;
			case '\'':
				/* &apos; */
				target[target_i++] = '&';
				target[target_i++] = 'a';
				target[target_i++] = 'p';
				target[target_i++] = 'o';
				target[target_i++] = 's';
				target[target_i++] = ';';
				break;
			default:
				target[target_i++] = source[source_i];
		}

	target[target_i] = 0;
	return target;
}

/**
 * This function ecodes a text in XML. I.e. the characters &, <, >, " and '
 * are translated to &amp;, &lt;, &gt;, &quot;, and &apos;.
 *
 * This function is designed to be used with the encoding/quoting operator (::).
 */
// builtin encode_xml(text)
static struct spl_node *handler_encode_xml(struct spl_task *task, void *data UNUSED)
{
	char *source = spl_clib_get_string(task);
	return SPL_NEW_STRING(xml_encode(source));
}

void SPL_ABI(spl_mod_encode_xml_init)(struct spl_vm *vm, struct spl_module *mod UNUSED, int restore UNUSED)
{
	spl_clib_reg(vm, "encode_xml", handler_encode_xml, 0);
}

void SPL_ABI(spl_mod_encode_xml_done)(struct spl_vm *vm UNUSED, struct spl_module *mod UNUSED)
{
	return;
}