handle zero size Base64 conversions
[strongswan.git] / src / libcharon / plugins / tnccs_11 / messages / tnccs_preferred_language_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_preferred_language_msg.h"
17
18 #include <debug.h>
19
20 typedef struct private_tnccs_preferred_language_msg_t private_tnccs_preferred_language_msg_t;
21
22 /**
23 * Private data of a tnccs_preferred_language_msg_t object.
24 *
25 */
26 struct private_tnccs_preferred_language_msg_t {
27 /**
28 * Public tnccs_preferred_language_msg_t interface.
29 */
30 tnccs_preferred_language_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 * Preferred language
44 */
45 char *preferred_language;
46 };
47
48 METHOD(tnccs_msg_t, get_type, tnccs_msg_type_t,
49 private_tnccs_preferred_language_msg_t *this)
50 {
51 return this->type;
52 }
53
54 METHOD(tnccs_msg_t, get_node, xmlNodePtr,
55 private_tnccs_preferred_language_msg_t *this)
56 {
57 return this->node;
58 }
59
60 METHOD(tnccs_msg_t, destroy, void,
61 private_tnccs_preferred_language_msg_t *this)
62 {
63 free(this->preferred_language);
64 free(this);
65 }
66
67 METHOD(tnccs_preferred_language_msg_t, get_preferred_language, char*,
68 private_tnccs_preferred_language_msg_t *this)
69 {
70 return this->preferred_language;
71 }
72
73 /**
74 * See header
75 */
76 tnccs_msg_t *tnccs_preferred_language_msg_create_from_node(xmlNodePtr node,
77 linked_list_t *errors)
78 {
79 private_tnccs_preferred_language_msg_t *this;
80 xmlChar *language;
81
82 INIT(this,
83 .public = {
84 .tnccs_msg_interface = {
85 .get_type = _get_type,
86 .get_node = _get_node,
87 .destroy = _destroy,
88 },
89 .get_preferred_language = _get_preferred_language,
90 },
91 .type = TNCCS_MSG_PREFERRED_LANGUAGE,
92 .node = node,
93 );
94
95 language = xmlNodeGetContent(node);
96 this->preferred_language = strdup((char*)language);
97 xmlFree(language);
98
99 return &this->public.tnccs_msg_interface;
100 }
101
102 /**
103 * See header
104 */
105 tnccs_msg_t *tnccs_preferred_language_msg_create(char *language)
106 {
107 private_tnccs_preferred_language_msg_t *this;
108 xmlNodePtr n, n2;
109
110 INIT(this,
111 .public = {
112 .tnccs_msg_interface = {
113 .get_type = _get_type,
114 .get_node = _get_node,
115 .destroy = _destroy,
116 },
117 .get_preferred_language = _get_preferred_language,
118 },
119 .type = TNCCS_MSG_PREFERRED_LANGUAGE,
120 .node = xmlNewNode(NULL, BAD_CAST "TNCC-TNCS-Message"),
121 .preferred_language = strdup(language),
122 );
123
124 /* add the message type number in hex */
125 n = xmlNewNode(NULL, BAD_CAST "Type");
126 xmlNodeSetContent(n, BAD_CAST "00000003");
127 xmlAddChild(this->node, n);
128
129 n = xmlNewNode(NULL, BAD_CAST "XML");
130 xmlAddChild(this->node, n);
131
132 n2 = xmlNewNode(NULL, BAD_CAST enum_to_name(tnccs_msg_type_names, this->type));
133 xmlNodeSetContent(n2, BAD_CAST language);
134 xmlAddChild(n, n2);
135
136 return &this->public.tnccs_msg_interface;
137 }