Debian Codegraph @

xmpp-vala/src/core/stanza_reader.vala

raw
1 using Gee;
2
3 namespace Xmpp {
4
5 public const string XMLNS_URI = "http://www.w3.org/2000/xmlns/";
6 public const string XML_URI = "http://www.w3.org/XML/1998/namespace";
7 public const string JABBER_URI = "jabber:client";
8
9 public class StanzaReader {
10 private static int BUFFER_MAX = 4096;
11
12 private InputStream? input;
13 private uint8[] buffer;
14 private int buffer_fill = 0;
15 private int buffer_pos = 0;
16 private Cancellable? cancellable;
17
18 private NamespaceState ns_state = new NamespaceState();
19
20 public StanzaReader.for_buffer(uint8[] buffer) {
21 this.buffer = buffer;
22 this.buffer_fill = buffer.length;
23 }
24
25 public StanzaReader.for_string(string s) {
26 this.for_buffer(s.data);
27 }
28
29 public StanzaReader.for_stream(InputStream input, Cancellable? cancellable = null) {
30 this.input = input;
31 this.cancellable = cancellable;
32 buffer = new uint8[BUFFER_MAX];
33 }
34
35 private async void update_buffer() throws IOError {
36 InputStream? input = this.input;
37 if (input == null) throw new IOError.CLOSED("No input stream specified and end of buffer reached.");
38 if (cancellable != null && cancellable.is_cancelled()) throw new IOError.CANCELLED("Input stream is canceled.");
39 try {
40 buffer_fill = (int) yield ((!)input).read_async(buffer, GLib.Priority.DEFAULT, cancellable);
41 } catch (TlsError.EOF e) {
42 throw new IOError.CLOSED("End of TLS stream reached");
43 }
44 if (buffer_fill == 0) throw new IOError.CLOSED("End of input stream reached.");
45 buffer_pos = 0;
46 }
47
48 private async char read_single() throws IOError {
49 if (buffer_pos >= buffer_fill) {
50 yield update_buffer();
51 }
52 return (char) buffer[buffer_pos++];
53 }
54
55 private async char peek_single() throws IOError {
56 if (buffer_pos >= buffer_fill) {
57 yield update_buffer();
58 }
59 return (char) buffer[buffer_pos];
60 }
61
62 private bool is_ws(uint8 what) {
63 return what == ' ' || what == '\t' || what == '\r' || what == '\n';
64 }
65
66 private void skip_single() {
67 buffer_pos++;
68 }
69
70 private async void skip_until_non_ws() throws IOError {
71 if (buffer_pos >= buffer_fill) {
72 yield update_buffer();
73 }
74 while (is_ws(buffer[buffer_pos])) {
75 buffer_pos++;
76 if (buffer_pos >= buffer_fill) {
77 yield update_buffer();
78 }
79 }
80 }
81
82 private async string read_until_ws() throws IOError {
83 var res = new StringBuilder();
84 if (buffer_pos >= buffer_fill) {
85 yield update_buffer();
86 }
87 while (!is_ws(buffer[buffer_pos])) {
88 res.append_c((char) buffer[buffer_pos++]);
89 if (buffer_pos >= buffer_fill) {
90 yield update_buffer();
91 }
92 }
93 return res.str;
94 }
95
96 private async string read_until_char_or_ws(char x, char y = 0) throws IOError {
97 var res = new StringBuilder();
98 if (buffer_pos >= buffer_fill) {
99 yield update_buffer();
100 }
101 while (buffer[buffer_pos] != x && buffer[buffer_pos] != y && !is_ws(buffer[buffer_pos])) {
102 res.append_c((char) buffer[buffer_pos++]);
103 if (buffer_pos >= buffer_fill) {
104 yield update_buffer();
105 }
106 }
107 return res.str;
108 }
109
110 private async string read_until_char(char x) throws IOError {
111 var res = new StringBuilder();
112 if (buffer_pos >= buffer_fill) {
113 yield update_buffer();
114 }
115 while (buffer[buffer_pos] != x) {
116 res.append_c((char) buffer[buffer_pos++]);
117 if (buffer_pos >= buffer_fill) {
118 yield update_buffer();
119 }
120 }
121 return res.str;
122 }
123
124 private async StanzaAttribute read_attribute() throws IOError {
125 var res = new StanzaAttribute();
126 res.name = yield read_until_char_or_ws('=');
127 if ((yield read_single()) == '=') {
128 var quot = yield peek_single();
129 if (quot == '\'' || quot == '"') {
130 skip_single();
131 res.encoded_val = yield read_until_char(quot);
132 skip_single();
133 } else {
134 res.encoded_val = yield read_until_ws();
135 }
136 }
137 return res;
138 }
139
140 private void handle_entry_ns(StanzaEntry entry, string default_uri = ns_state.current_ns_uri) throws IOError {
141 if (entry.ns_uri != null) return;
142 if (entry.name.contains(":")) {
143 var split = entry.name.split(":");
144 entry.ns_uri = ns_state.find_uri(split[0]);
145 entry.name = split[1];
146 } else {
147 entry.ns_uri = default_uri;
148 }
149 }
150
151 private void handle_stanza_ns(StanzaNode res) throws IOError {
152 foreach (StanzaAttribute attr in res.attributes) {
153 if (attr.name == "xmlns" && attr.val != null) {
154 attr.ns_uri = XMLNS_URI;
155 ns_state.set_current((!)attr.val);
156 } else if (attr.name.contains(":") && attr.val != null) {
157 var split = attr.name.split(":");
158 if (split[0] == "xmlns") {
159 attr.ns_uri = XMLNS_URI;
160 attr.name = split[1];
161 ns_state.add_assoc((!)attr.val, attr.name);
162 }
163 }
164 }
165 handle_entry_ns(res);
166 foreach (StanzaAttribute attr in res.attributes) {
167 handle_entry_ns(attr, res.ns_uri ?? ns_state.current_ns_uri);
168 }
169 }
170
171 public async StanzaNode read_node_start() throws IOError {
172 var res = new StanzaNode();
173 res.attributes = new ArrayList<StanzaAttribute>();
174 var eof = false;
175 if ((yield peek_single()) == '<') skip_single();
176 if ((yield peek_single()) == '?') res.pseudo = true;
177 if ((yield peek_single()) == '/') {
178 eof = true;
179 skip_single();
180 res.name = yield read_until_char_or_ws('>');
181 while ((yield peek_single()) != '>') {
182 skip_single();
183 }
184 skip_single();
185 res.has_nodes = false;
186 res.pseudo = false;
187 handle_stanza_ns(res);
188 return res;
189 }
190 res.name = yield read_until_char_or_ws('>', '/');
191 yield skip_until_non_ws();
192 char next_char = yield peek_single();
193 while (next_char != '/' && next_char != '>' && next_char != '?') {
194 res.attributes.add(yield read_attribute());
195 yield skip_until_non_ws();
196 next_char = yield peek_single();
197 }
198 if ((yield read_single()) == '/' || res.pseudo) {
199 res.has_nodes = false;
200 skip_single();
201 } else {
202 res.has_nodes = true;
203 }
204 handle_stanza_ns(res);
205 return res;
206 }
207
208 public async StanzaNode read_text_node() throws IOError {
209 var res = new StanzaNode();
210 res.name = "#text";
211 res.ns_uri = ns_state.current_ns_uri;
212 res.encoded_val = (yield read_until_char('<'));
213 return res;
214 }
215
216 public async StanzaNode read_root_node() throws IOError {
217 yield skip_until_non_ws();
218 if ((yield peek_single()) == '<') {
219 var res = yield read_node_start();
220 if (res.pseudo) {
221 return yield read_root_node();
222 }
223 return res;
224 } else {
225 throw new IOError.INVALID_DATA("XML: Content before root node");
226 }
227 }
228
229 public async StanzaNode read_stanza_node() throws IOError {
230 try {
231 ns_state = ns_state.push();
232 var res = yield read_node_start();
233 if (res.has_nodes) {
234 bool finish_node_seen = false;
235 StanzaNode? text_node = null;
236 do {
237 text_node = yield read_text_node();
238 if ((yield peek_single()) == '<') {
239 skip_single();
240 if ((yield peek_single()) == '/') {
241 skip_single();
242 string desc = yield read_until_char('>');
243 skip_single();
244 if (desc.contains(":")) {
245 var split = desc.split(":");
246 if (split[0] != ns_state.find_name((!)res.ns_uri)) throw new IOError.INVALID_DATA("XML: Closing namespace prefix mismatch");
247 if (split[1] != res.name) throw new IOError.INVALID_DATA("XML: Closing element name mismatch");
248 } else {
249 if (ns_state.current_ns_uri != res.ns_uri) throw new IOError.INVALID_DATA("XML: Closing element namespace mismatch");
250 if (desc != res.name) throw new IOError.INVALID_DATA("XML: Closing element name mismatch");
251 }
252 finish_node_seen = true;
253 } else {
254 res.sub_nodes.add(yield read_stanza_node());
255 }
256 }
257 } while (!finish_node_seen);
258 if (res.sub_nodes.size == 0) {
259 if (text_node == null || text_node.val.length == 0) {
260 res.has_nodes = false;
261 } else {
262 res.sub_nodes.add(text_node);
263 }
264 }
265 }
266 ns_state = ns_state.pop();
267 return res;
268 } catch (IOError.INVALID_DATA e) {
269 uint8[] buffer_cpy = new uint8[buffer.length + 1];
270 Memory.copy(buffer_cpy, buffer, buffer.length);
271 warning("invalid data at: %s".printf((string)buffer_cpy) + "\n");
272 throw e;
273 }
274 }
275
276 public async StanzaNode read_node() throws IOError {
277 yield skip_until_non_ws();
278 if ((yield peek_single()) == '<') {
279 return yield read_stanza_node();
280 } else {
281 return yield read_text_node();
282 }
283 }
284 }
285
286 }