handle zero size Base64 conversions
[strongswan.git] / src / libcharon / plugins / tnccs_11 / messages / tnccs_reason_strings_msg.c
1 /*
2 * Copyright (C) 2006 Mike McCauley (mikem@open.com.au)
3 * Copyright (C) 2010 Andreas Steffen, HSR Hochschule fuer Technik Rapperswil
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * for more details.
14 */
15
16 #include "tnccs_reason_strings_msg.h"
17
18 #include <debug.h>
19
20 typedef struct private_tnccs_reason_strings_msg_t private_tnccs_reason_strings_msg_t;
21
22 /**
23 * Private data of a tnccs_reason_strings_msg_t object.
24 *
25 */
26 struct private_tnccs_reason_strings_msg_t {
27 /**
28 * Public tnccs_reason_strings_msg_t interface.
29 */
30 tnccs_reason_strings_msg_t public;
31
32 /**
33 * TNCCS message type
34 */
35 tnccs_msg_type_t type;
36
37 /**
38 * XML-encoded message node
39 */
40 xmlNodePtr node;
41
42 /**
43 * Reason Language
44 */
45 char* language;
46
47 /**
48 * Reason String
49 */
50 char* reason;
51 };
52
53 METHOD(tnccs_msg_t, get_type, tnccs_msg_type_t,
54 private_tnccs_reason_strings_msg_t *this)
55 {
56 return this->type;
57 }
58
59 METHOD(tnccs_msg_t, get_node, xmlNodePtr,
60 private_tnccs_reason_strings_msg_t *this)
61 {
62 return this->node;
63 }
64
65 METHOD(tnccs_msg_t, destroy, void,
66 private_tnccs_reason_strings_msg_t *this)
67 {
68 free(this->language);
69 free(this->reason);
70 free(this);
71 }
72
73 METHOD(tnccs_reason_strings_msg_t, get_reason, char*,
74 private_tnccs_reason_strings_msg_t *this, char **language)
75 {
76 *language = this->language;
77
78 return this->reason;
79 }
80
81 /**
82 * See header
83 */
84 tnccs_msg_t *tnccs_reason_strings_msg_create_from_node(xmlNodePtr node,
85 linked_list_t *errors)
86 {
87 private_tnccs_reason_strings_msg_t *this;
88
89 INIT(this,
90 .public = {
91 .tnccs_msg_interface = {
92 .get_type = _get_type,
93 .get_node = _get_node,
94 .destroy = _destroy,
95 },
96 .get_reason = _get_reason,
97 },
98 .type = TNCCS_MSG_REASON_STRINGS,
99 .node = node,
100 );
101
102 return &this->public.tnccs_msg_interface;
103 }
104
105 /**
106 * See header
107 */
108 tnccs_msg_t *tnccs_reason_strings_msg_create(char *language, char *reason)
109 {
110 private_tnccs_reason_strings_msg_t *this;
111 xmlNodePtr n, n2, n3;
112
113 INIT(this,
114 .public = {
115 .tnccs_msg_interface = {
116 .get_type = _get_type,
117 .get_node = _get_node,
118 .destroy = _destroy,
119 },
120 .get_reason = _get_reason,
121 },
122 .type = TNCCS_MSG_REASON_STRINGS,
123 .node = xmlNewNode(NULL, BAD_CAST "TNCC-TNCS-Message"),
124 .language = strdup(language),
125 .reason = strdup(reason),
126 );
127
128 /* add the message type number in hex */
129 n = xmlNewNode(NULL, BAD_CAST "Type");
130 xmlNodeSetContent(n, BAD_CAST "00000004");
131 xmlAddChild(this->node, n);
132
133 n = xmlNewNode(NULL, BAD_CAST "XML");
134 xmlAddChild(this->node, n);
135
136 n2 = xmlNewNode(NULL, BAD_CAST enum_to_name(tnccs_msg_type_names, this->type));
137 xmlNodeSetContent(n2, BAD_CAST language);
138 xmlAddChild(n, n2);
139
140 /* could add multiple reasons here, if we had them */
141 n3 = xmlNewNode(NULL, BAD_CAST "ReasonString");
142 xmlNewProp(n3, BAD_CAST "xml:lang", BAD_CAST language);
143 xmlNodeSetContent(n3, BAD_CAST reason);
144 xmlAddChild(n2, n3);
145
146 return &this->public.tnccs_msg_interface;
147 }