Radek Krejci | 54ea8de | 2015-04-09 18:02:56 +0200 | [diff] [blame] | 1 | /** |
| 2 | * @file xml.c |
| 3 | * @author Radek Krejci <rkrejci@cesnet.cz> |
| 4 | * @brief XML parser implementation for libyang |
| 5 | * |
| 6 | * Copyright (c) 2015 CESNET, z.s.p.o. |
| 7 | * |
| 8 | * Redistribution and use in source and binary forms, with or without |
| 9 | * modification, are permitted provided that the following conditions |
| 10 | * are met: |
| 11 | * 1. Redistributions of source code must retain the above copyright |
| 12 | * notice, this list of conditions and the following disclaimer. |
| 13 | * 2. Redistributions in binary form must reproduce the above copyright |
| 14 | * notice, this list of conditions and the following disclaimer in |
| 15 | * the documentation and/or other materials provided with the |
| 16 | * distribution. |
| 17 | * 3. Neither the name of the Company nor the names of its contributors |
| 18 | * may be used to endorse or promote products derived from this |
| 19 | * software without specific prior written permission. |
| 20 | */ |
| 21 | |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 22 | |
Radek Krejci | 709fee6 | 2015-04-15 13:56:19 +0200 | [diff] [blame] | 23 | #include <ctype.h> |
| 24 | #include <stdint.h> |
Radek Krejci | f0023a9 | 2015-04-20 20:51:39 +0200 | [diff] [blame] | 25 | #include <stdio.h> |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 26 | #include <stdlib.h> |
| 27 | #include <string.h> |
Radek Krejci | 54ea8de | 2015-04-09 18:02:56 +0200 | [diff] [blame] | 28 | #include <unistd.h> |
| 29 | |
Radek Krejci | 06a704e | 2015-04-22 14:50:49 +0200 | [diff] [blame] | 30 | #include "common.h" |
| 31 | #include "dict.h" |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 32 | #include "tree.h" |
Radek Krejci | 54ea8de | 2015-04-09 18:02:56 +0200 | [diff] [blame] | 33 | #include "xml.h" |
| 34 | |
Radek Krejci | 3045cf3 | 2015-05-28 10:58:52 +0200 | [diff] [blame^] | 35 | #ifndef NDEBUG |
| 36 | unsigned int lineno, lws_lineno; |
| 37 | #define COUNTLINE(c) if ((c) == 0xa) {lineno++;} |
| 38 | #else |
| 39 | #define COUNTLINE(C) |
| 40 | #endif |
| 41 | |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 42 | /* |
| 43 | * Macro to test if character is #x20 | #x9 | #xA | #xD (whitespace) |
| 44 | */ |
| 45 | #define is_xmlws(c) (c == 0x20 || c == 0x9 || c == 0xa || c == 0xd) |
Radek Krejci | 54ea8de | 2015-04-09 18:02:56 +0200 | [diff] [blame] | 46 | |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 47 | #define is_xmlnamestartchar(c) ((c >= 'a' && c <= 'z') || c == '_' || \ |
| 48 | (c >= 'A' && c <= 'Z') || c == ':' || \ |
| 49 | (c >= 0x370 && c <= 0x1fff && c != 0x37e ) || \ |
| 50 | (c >= 0xc0 && c <= 0x2ff && c != 0xd7 && c != 0xf7) || c == 0x200c || \ |
| 51 | c == 0x200d || (c >= 0x2070 && c <= 0x218f) || \ |
| 52 | (c >= 0x2c00 && c <= 0x2fef) || (c >= 0x3001 && c <= 0xd7ff) || \ |
| 53 | (c >= 0xf900 && c <= 0xfdcf) || (c >= 0xfdf0 && c <= 0xfffd) || \ |
| 54 | (c >= 0x10000 && c <= 0xeffff)) |
| 55 | |
| 56 | #define is_xmlnamechar(c) ((c >= 'a' && c <= 'z') || c == '_' || c == '-' || \ |
| 57 | (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9') || c == ':' || \ |
| 58 | c == '.' || c == 0xb7 || (c >= 0x370 && c <= 0x1fff && c != 0x37e ) ||\ |
| 59 | (c >= 0xc0 && c <= 0x2ff && c != 0xd7 && c != 0xf7) || c == 0x200c || \ |
| 60 | c == 0x200d || (c >= 0x300 && c <= 0x36f) || \ |
| 61 | (c >= 0x2070 && c <= 0x218f) || (c >= 0x2030f && c <= 0x2040) || \ |
| 62 | (c >= 0x2c00 && c <= 0x2fef) || (c >= 0x3001 && c <= 0xd7ff) || \ |
| 63 | (c >= 0xf900 && c <= 0xfdcf) || (c >= 0xfdf0 && c <= 0xfffd) || \ |
| 64 | (c >= 0x10000 && c <= 0xeffff)) |
| 65 | |
Radek Krejci | 3045cf3 | 2015-05-28 10:58:52 +0200 | [diff] [blame^] | 66 | #define ign_xmlws(p) \ |
| 67 | while (is_xmlws(*p)) { \ |
| 68 | COUNTLINE(*p); \ |
| 69 | p++; \ |
| 70 | } |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 71 | |
Radek Krejci | e1f1391 | 2015-05-26 15:17:38 +0200 | [diff] [blame] | 72 | void lyxml_unlink_attr(struct lyxml_attr *attr) |
Radek Krejci | 54ea8de | 2015-04-09 18:02:56 +0200 | [diff] [blame] | 73 | { |
Radek Krejci | e1f1391 | 2015-05-26 15:17:38 +0200 | [diff] [blame] | 74 | struct lyxml_attr *prev; |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 75 | |
| 76 | if (!attr) { |
Radek Krejci | e1f1391 | 2015-05-26 15:17:38 +0200 | [diff] [blame] | 77 | return; |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | if (!attr->parent) { |
Radek Krejci | e1f1391 | 2015-05-26 15:17:38 +0200 | [diff] [blame] | 81 | /* hmm, something is probably wrong */ |
| 82 | attr->next = NULL; |
| 83 | return; |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 84 | } |
| 85 | |
Radek Krejci | e1f1391 | 2015-05-26 15:17:38 +0200 | [diff] [blame] | 86 | prev = attr->parent->attr; |
| 87 | if (prev == attr) { |
| 88 | /* unlinking the first attribute -> update the element's pointer */ |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 89 | attr->parent->attr = attr->next; |
| 90 | } else { |
Radek Krejci | e1f1391 | 2015-05-26 15:17:38 +0200 | [diff] [blame] | 91 | while(prev && prev->next != attr) { |
| 92 | prev = prev->next; |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 93 | } |
| 94 | |
Radek Krejci | e1f1391 | 2015-05-26 15:17:38 +0200 | [diff] [blame] | 95 | if (!prev) { |
| 96 | /* something is probably broken */ |
| 97 | attr->parent = NULL; |
| 98 | attr->next = NULL; |
| 99 | return; |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 100 | } |
| 101 | |
Radek Krejci | e1f1391 | 2015-05-26 15:17:38 +0200 | [diff] [blame] | 102 | /* fix the previous's attribute pointer to next in the list */ |
| 103 | prev->next = attr->next; |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | attr->parent = NULL; |
| 107 | attr->next = NULL; |
| 108 | |
Radek Krejci | e1f1391 | 2015-05-26 15:17:38 +0200 | [diff] [blame] | 109 | return; |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 110 | } |
| 111 | |
Radek Krejci | e1f1391 | 2015-05-26 15:17:38 +0200 | [diff] [blame] | 112 | void lyxml_unlink_elem(struct lyxml_elem *elem) |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 113 | { |
Radek Krejci | e1f1391 | 2015-05-26 15:17:38 +0200 | [diff] [blame] | 114 | struct lyxml_elem *parent, *first; |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 115 | |
| 116 | if (!elem) { |
Radek Krejci | e1f1391 | 2015-05-26 15:17:38 +0200 | [diff] [blame] | 117 | return; |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 118 | } |
| 119 | |
Radek Krejci | e1f1391 | 2015-05-26 15:17:38 +0200 | [diff] [blame] | 120 | /* store pointers to important nodes */ |
| 121 | parent = elem->parent; |
| 122 | |
| 123 | /* unlink from parent */ |
| 124 | if (parent) { |
| 125 | if (parent->child == elem) { |
| 126 | /* we unlink the first child */ |
| 127 | /* update the parent's link */ |
| 128 | parent->child = elem->next; |
| 129 | } |
| 130 | /* forget about the parent */ |
| 131 | elem->parent = NULL; |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 132 | } |
| 133 | |
Radek Krejci | e1f1391 | 2015-05-26 15:17:38 +0200 | [diff] [blame] | 134 | /* unlink from siblings */ |
| 135 | if (elem->prev == elem) { |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 136 | /* there are no more siblings */ |
Radek Krejci | e1f1391 | 2015-05-26 15:17:38 +0200 | [diff] [blame] | 137 | return; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 138 | } |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 139 | if (elem->next) { |
| 140 | elem->next->prev = elem->prev; |
| 141 | } else { |
Radek Krejci | e1f1391 | 2015-05-26 15:17:38 +0200 | [diff] [blame] | 142 | /* unlinking the last element */ |
| 143 | if (parent) { |
| 144 | first = parent->child; |
| 145 | } else { |
| 146 | first = elem; |
| 147 | while (elem->prev->next) { |
| 148 | first = elem->prev; |
| 149 | } |
| 150 | } |
| 151 | first->prev = elem->prev; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 152 | } |
Radek Krejci | e1f1391 | 2015-05-26 15:17:38 +0200 | [diff] [blame] | 153 | if (elem->prev->next) { |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 154 | elem->prev->next = elem->next; |
| 155 | } |
| 156 | |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 157 | /* clean up the unlinked element */ |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 158 | elem->next = NULL; |
| 159 | elem->prev = elem; |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 160 | } |
| 161 | |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 162 | void lyxml_free_attr(struct ly_ctx *ctx, struct lyxml_attr *attr) |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 163 | { |
| 164 | if (!attr) { |
| 165 | return; |
| 166 | } |
| 167 | |
| 168 | lyxml_unlink_attr(attr); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 169 | lydict_remove(ctx, attr->name); |
| 170 | lydict_remove(ctx, attr->value); |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 171 | free(attr); |
| 172 | } |
| 173 | |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 174 | void lyxml_free_attrs(struct ly_ctx *ctx, struct lyxml_elem *elem) |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 175 | { |
| 176 | struct lyxml_attr *a, *next; |
| 177 | if (!elem || !elem->attr) { |
| 178 | return; |
| 179 | } |
| 180 | |
| 181 | a = elem->attr; |
| 182 | do { |
| 183 | next = a->next; |
| 184 | |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 185 | lydict_remove(ctx, a->name); |
| 186 | lydict_remove(ctx, a->value); |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 187 | free(a); |
| 188 | |
| 189 | a = next; |
| 190 | } while (a); |
| 191 | } |
| 192 | |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 193 | static void lyxml_free_elem_(struct ly_ctx *ctx, struct lyxml_elem *elem) |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 194 | { |
| 195 | struct lyxml_elem *e, *next; |
| 196 | |
| 197 | if (!elem) { |
| 198 | return; |
| 199 | } |
| 200 | |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 201 | lyxml_free_attrs(ctx, elem); |
| 202 | LY_TREE_FOR_SAFE(elem->child, next, e) { |
| 203 | lyxml_free_elem_(ctx, e); |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 204 | } |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 205 | lydict_remove(ctx, elem->name); |
| 206 | lydict_remove(ctx, elem->content); |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 207 | free(elem); |
| 208 | } |
| 209 | |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 210 | void lyxml_free_elem(struct ly_ctx *ctx, struct lyxml_elem *elem) |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 211 | { |
| 212 | if (!elem) { |
| 213 | return; |
| 214 | } |
| 215 | |
| 216 | lyxml_unlink_elem(elem); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 217 | lyxml_free_elem_(ctx, elem); |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 218 | } |
| 219 | |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 220 | int lyxml_add_attr(struct lyxml_elem *parent, struct lyxml_attr *attr) |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 221 | { |
| 222 | struct lyxml_attr *a; |
| 223 | |
| 224 | if (!parent || !attr) { |
Radek Krejci | 3045cf3 | 2015-05-28 10:58:52 +0200 | [diff] [blame^] | 225 | LOGERR(LY_EINVAL, NULL); |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 226 | return EXIT_FAILURE; |
| 227 | } |
| 228 | |
| 229 | /* (re)link attribute to parent */ |
| 230 | if (attr->parent) { |
| 231 | lyxml_unlink_attr(attr); |
| 232 | } |
| 233 | attr->parent = parent; |
| 234 | |
| 235 | /* link parent to attribute */ |
| 236 | if (parent->attr) { |
| 237 | for (a = parent->attr; a->next; a = a->next); |
| 238 | a->next = attr; |
| 239 | } else { |
| 240 | parent->attr = attr; |
| 241 | } |
| 242 | |
| 243 | return EXIT_SUCCESS; |
| 244 | } |
| 245 | |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 246 | const char *lyxml_get_attr(struct lyxml_elem *elem, const char *name, |
| 247 | const char *ns) |
| 248 | { |
| 249 | struct lyxml_attr *a; |
| 250 | |
| 251 | if (!elem || !name) { |
Radek Krejci | 3045cf3 | 2015-05-28 10:58:52 +0200 | [diff] [blame^] | 252 | LOGERR(LY_EINVAL, NULL); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 253 | return NULL; |
| 254 | } |
| 255 | |
| 256 | for (a = elem->attr; a; a = a->next) { |
| 257 | if (a->type != LYXML_ATTR_STD) { |
| 258 | continue; |
| 259 | } |
| 260 | |
| 261 | if (!strcmp(name, a->name)) { |
| 262 | if ((!ns && !a->ns) |
| 263 | || (ns && a->ns && !strcmp(ns, a->ns->value))) { |
| 264 | return a->value; |
| 265 | } |
| 266 | } |
| 267 | } |
| 268 | |
| 269 | return NULL; |
| 270 | } |
| 271 | |
| 272 | int lyxml_add_child(struct lyxml_elem *parent, struct lyxml_elem *elem) |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 273 | { |
| 274 | struct lyxml_elem *e; |
| 275 | |
| 276 | if (!parent || !elem) { |
Radek Krejci | 3045cf3 | 2015-05-28 10:58:52 +0200 | [diff] [blame^] | 277 | LOGERR(LY_EINVAL, NULL); |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 278 | return EXIT_FAILURE; |
| 279 | } |
| 280 | |
| 281 | /* (re)link element to parent */ |
| 282 | if (elem->parent) { |
| 283 | lyxml_unlink_elem(elem); |
| 284 | } |
| 285 | elem->parent = parent; |
| 286 | |
| 287 | /* link parent to element */ |
| 288 | if (parent->child) { |
| 289 | e = parent->child; |
| 290 | elem->prev = e->prev; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 291 | elem->next = NULL; |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 292 | elem->prev->next = elem; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 293 | e->prev = elem; |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 294 | } else { |
| 295 | parent->child = elem; |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 296 | elem->prev = elem; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 297 | elem->next = NULL; |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 298 | } |
| 299 | |
| 300 | return EXIT_SUCCESS; |
| 301 | } |
| 302 | |
| 303 | /** |
| 304 | * @brief Get the first UTF-8 character value (4bytes) from buffer |
| 305 | * @param[in] buf pointr to the current position in input buffer |
| 306 | * @param[out] read Number of processed bytes in buf (length of UTF-8 |
| 307 | * character). |
| 308 | * @return UTF-8 value as 4 byte number. 0 means error, only UTF-8 characters |
| 309 | * valid for XML are returned, so: |
| 310 | * #x9 | #xA | #xD | [#x20-#xD7FF] | [#xE000-#xFFFD] | [#x10000-#x10FFFF] |
| 311 | * = any Unicode character, excluding the surrogate blocks, FFFE, and FFFF. |
| 312 | * |
| 313 | * UTF-8 mapping: |
| 314 | * 00000000 -- 0000007F: 0xxxxxxx |
| 315 | * 00000080 -- 000007FF: 110xxxxx 10xxxxxx |
| 316 | * 00000800 -- 0000FFFF: 1110xxxx 10xxxxxx 10xxxxxx |
| 317 | * 00010000 -- 001FFFFF: 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx |
| 318 | * |
| 319 | */ |
| 320 | static int getutf8(const char *buf, unsigned int *read) |
| 321 | { |
| 322 | int c, aux; |
| 323 | int i; |
| 324 | |
| 325 | /* check input variable */ |
| 326 | if (!buf || !read) { |
Radek Krejci | 3045cf3 | 2015-05-28 10:58:52 +0200 | [diff] [blame^] | 327 | LOGERR(LY_EINVAL, NULL); |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 328 | return 0; |
| 329 | } |
| 330 | c = buf[0]; |
| 331 | *read = 0; |
| 332 | |
| 333 | /* buf is NULL terminated string, so 0 means EOF */ |
| 334 | if (!c) { |
Radek Krejci | 3045cf3 | 2015-05-28 10:58:52 +0200 | [diff] [blame^] | 335 | LOGERR(LY_EEOF, NULL); |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 336 | return 0; |
| 337 | } |
| 338 | *read = 1; |
| 339 | |
| 340 | /* process character byte(s) */ |
Radek Krejci | c1265a6 | 2015-05-26 15:46:28 +0200 | [diff] [blame] | 341 | if ((c & 0xf8) == 0xf0) { |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 342 | /* four bytes character */ |
| 343 | *read = 4; |
| 344 | |
| 345 | c &= 0x07; |
| 346 | for (i = 1; i <= 3; i++) { |
| 347 | aux = buf[i]; |
| 348 | if ((aux & 0xc0) != 0x80) { |
Radek Krejci | 3045cf3 | 2015-05-28 10:58:52 +0200 | [diff] [blame^] | 349 | LOGERR(LY_EINVAL, NULL); |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 350 | return 0; |
| 351 | } |
| 352 | |
| 353 | c = (c << 6) | (aux & 0x3f); |
| 354 | } |
| 355 | |
| 356 | |
| 357 | if (c < 0x1000 || c > 0x10ffff) { |
Radek Krejci | 3045cf3 | 2015-05-28 10:58:52 +0200 | [diff] [blame^] | 358 | LOGERR(LY_EINVAL, NULL); |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 359 | return 0; |
| 360 | } |
| 361 | } else if ((c & 0xf0) == 0xe0) { |
| 362 | /* three bytes character */ |
| 363 | *read = 3; |
| 364 | |
| 365 | c &= 0x0f; |
| 366 | for (i = 1; i <= 2; i++) { |
| 367 | aux = buf[i]; |
| 368 | if ((aux & 0xc0) != 0x80) { |
Radek Krejci | 3045cf3 | 2015-05-28 10:58:52 +0200 | [diff] [blame^] | 369 | LOGERR(LY_EINVAL, NULL); |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 370 | return 0; |
| 371 | } |
| 372 | |
| 373 | c = (c << 6) | (aux & 0x3f); |
| 374 | } |
| 375 | |
| 376 | |
| 377 | if (c < 0x800 || (c > 0xd7ff && c < 0xe000) || c > 0xfffd ) { |
Radek Krejci | 3045cf3 | 2015-05-28 10:58:52 +0200 | [diff] [blame^] | 378 | LOGERR(LY_EINVAL, NULL); |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 379 | return 0; |
| 380 | } |
| 381 | } else if ((c & 0xe0) == 0xc0) { |
| 382 | /* two bytes character */ |
| 383 | *read = 2; |
| 384 | |
| 385 | aux = buf[1]; |
| 386 | if ((aux & 0xc0) != 0x80) { |
Radek Krejci | 3045cf3 | 2015-05-28 10:58:52 +0200 | [diff] [blame^] | 387 | LOGERR(LY_EINVAL, NULL); |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 388 | return 0; |
| 389 | } |
| 390 | c = ((c & 0x1f) << 6) | (aux & 0x3f); |
| 391 | |
| 392 | if (c < 0x80) { |
Radek Krejci | 3045cf3 | 2015-05-28 10:58:52 +0200 | [diff] [blame^] | 393 | LOGERR(LY_EINVAL, NULL); |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 394 | return 0; |
| 395 | } |
| 396 | } else if (!(c & 0x80)) { |
| 397 | /* one byte character */ |
| 398 | if (c < 0x20 && c != 0x9 && c != 0xa && c != 0xd) { |
| 399 | /* invalid character */ |
Radek Krejci | 3045cf3 | 2015-05-28 10:58:52 +0200 | [diff] [blame^] | 400 | LOGERR(LY_EINVAL, NULL); |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 401 | return 0; |
| 402 | } |
| 403 | } else { |
| 404 | /* invalid character */ |
Radek Krejci | 3045cf3 | 2015-05-28 10:58:52 +0200 | [diff] [blame^] | 405 | LOGERR(LY_EINVAL, NULL); |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 406 | return 0; |
| 407 | } |
| 408 | |
| 409 | return c; |
| 410 | } |
| 411 | |
Radek Krejci | 709fee6 | 2015-04-15 13:56:19 +0200 | [diff] [blame] | 412 | /** |
| 413 | * Store UTF-8 character specified as 4byte integer into the dst buffer. |
| 414 | * Returns number of written bytes (4 max), expects that dst has enough space. |
| 415 | * |
| 416 | * UTF-8 mapping: |
| 417 | * 00000000 -- 0000007F: 0xxxxxxx |
| 418 | * 00000080 -- 000007FF: 110xxxxx 10xxxxxx |
| 419 | * 00000800 -- 0000FFFF: 1110xxxx 10xxxxxx 10xxxxxx |
| 420 | * 00010000 -- 001FFFFF: 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx |
| 421 | * |
| 422 | */ |
| 423 | static unsigned int pututf8(char *dst, int32_t value) |
| 424 | { |
| 425 | if (value < 0x80) { |
| 426 | /* one byte character */ |
| 427 | dst[0] = value; |
| 428 | |
| 429 | return 1; |
| 430 | } else if (value < 0x800) { |
| 431 | /* two bytes character */ |
| 432 | dst[0] = 0xc0 | (value >> 6); |
| 433 | dst[1] = 0x80 | (value & 0x3f); |
| 434 | |
| 435 | return 2; |
| 436 | } else if (value < 0x10000) { |
| 437 | /* three bytes character */ |
| 438 | dst[0] = 0xe0 | (value >> 12); |
| 439 | dst[1] = 0x80 | ((value >> 6) & 0x3f); |
| 440 | dst[2] = 0x80 | (value & 0x3f); |
| 441 | |
| 442 | return 3; |
| 443 | } else if (value < 0x200000) { |
| 444 | /* four bytes character */ |
| 445 | dst[0] = 0xf0 | (value >> 18); |
| 446 | dst[1] = 0x80 | ((value >> 12) & 0x3f); |
| 447 | dst[2] = 0x80 | ((value >> 6) & 0x3f); |
| 448 | dst[3] = 0x80 | (value & 0x3f); |
| 449 | |
| 450 | return 4; |
| 451 | } else { |
| 452 | /* out of range */ |
Radek Krejci | 3045cf3 | 2015-05-28 10:58:52 +0200 | [diff] [blame^] | 453 | LOGERR(LY_EINVAL, NULL); |
Radek Krejci | 709fee6 | 2015-04-15 13:56:19 +0200 | [diff] [blame] | 454 | return 0; |
| 455 | } |
| 456 | } |
| 457 | |
Radek Krejci | 05e37a3 | 2015-04-15 14:40:34 +0200 | [diff] [blame] | 458 | static int parse_ignore(const char *data, const char *endstr, |
| 459 | unsigned int *len) |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 460 | { |
Radek Krejci | 05e37a3 | 2015-04-15 14:40:34 +0200 | [diff] [blame] | 461 | unsigned int slen; |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 462 | const char *c = data; |
| 463 | |
Radek Krejci | 05e37a3 | 2015-04-15 14:40:34 +0200 | [diff] [blame] | 464 | slen = strlen(endstr); |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 465 | |
Radek Krejci | 05e37a3 | 2015-04-15 14:40:34 +0200 | [diff] [blame] | 466 | while (*c && memcmp(c, endstr, slen)) { |
Radek Krejci | 3045cf3 | 2015-05-28 10:58:52 +0200 | [diff] [blame^] | 467 | COUNTLINE(*c); |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 468 | c++; |
| 469 | } |
| 470 | if (!*c) { |
Radek Krejci | 3045cf3 | 2015-05-28 10:58:52 +0200 | [diff] [blame^] | 471 | LOGERR(LY_EWELLFORM, "Missing close sequence \"%s\".", endstr); |
Radek Krejci | 05e37a3 | 2015-04-15 14:40:34 +0200 | [diff] [blame] | 472 | return EXIT_FAILURE; |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 473 | } |
Radek Krejci | 05e37a3 | 2015-04-15 14:40:34 +0200 | [diff] [blame] | 474 | c += slen; |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 475 | |
Radek Krejci | 05e37a3 | 2015-04-15 14:40:34 +0200 | [diff] [blame] | 476 | *len = c - data; |
| 477 | return EXIT_SUCCESS; |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 478 | } |
| 479 | |
Radek Krejci | 521008e | 2015-04-15 14:41:07 +0200 | [diff] [blame] | 480 | static char *parse_text(const char *data, char delim, unsigned int *len) |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 481 | { |
Radek Krejci | 709fee6 | 2015-04-15 13:56:19 +0200 | [diff] [blame] | 482 | #define BUFSIZE 1024 |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 483 | |
Radek Krejci | 709fee6 | 2015-04-15 13:56:19 +0200 | [diff] [blame] | 484 | char buf[BUFSIZE]; |
| 485 | char *result = NULL, *aux; |
| 486 | unsigned int r; |
| 487 | int o, size = 0; |
Radek Krejci | a4a8406 | 2015-04-16 13:00:10 +0200 | [diff] [blame] | 488 | int cdsect = 0; |
Radek Krejci | 709fee6 | 2015-04-15 13:56:19 +0200 | [diff] [blame] | 489 | int32_t n; |
| 490 | |
Radek Krejci | a4a8406 | 2015-04-16 13:00:10 +0200 | [diff] [blame] | 491 | for (*len = o = 0; cdsect || data[*len] != delim; o++) { |
| 492 | if (!data[*len] || (!cdsect && !memcmp(&data[*len], "]]>", 2))) { |
Radek Krejci | 3045cf3 | 2015-05-28 10:58:52 +0200 | [diff] [blame^] | 493 | LOGERR(LY_EWELLFORM, "Invalid element content, \"]]>\" found."); |
Radek Krejci | 709fee6 | 2015-04-15 13:56:19 +0200 | [diff] [blame] | 494 | goto error; |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 495 | } |
Radek Krejci | 709fee6 | 2015-04-15 13:56:19 +0200 | [diff] [blame] | 496 | |
Radek Krejci | a4a8406 | 2015-04-16 13:00:10 +0200 | [diff] [blame] | 497 | loop: |
| 498 | |
Radek Krejci | 709fee6 | 2015-04-15 13:56:19 +0200 | [diff] [blame] | 499 | if (o > BUFSIZE - 3) { |
| 500 | /* add buffer into the result */ |
| 501 | if (result) { |
| 502 | size = size + o; |
| 503 | aux = realloc(result, size + 1); |
| 504 | result = aux; |
| 505 | } else { |
| 506 | size = o; |
| 507 | result = malloc((size + 1) * sizeof *result); |
| 508 | } |
| 509 | memcpy(&result[size - o], buf, o); |
| 510 | |
| 511 | /* write again into the beginning of the buffer */ |
| 512 | o = 0; |
| 513 | } |
| 514 | |
Radek Krejci | a4a8406 | 2015-04-16 13:00:10 +0200 | [diff] [blame] | 515 | if (cdsect || !memcmp(&data[*len], "<![CDATA[", 9)) { |
| 516 | /* CDSect */ |
| 517 | if (!cdsect) { |
| 518 | cdsect = 1; |
| 519 | *len += 9; |
| 520 | } |
| 521 | if (data[*len] && !memcmp(&data[*len], "]]>", 3)) { |
| 522 | *len += 3; |
| 523 | cdsect = 0; |
| 524 | o--; /* we don't write any data in this iteration */ |
| 525 | } else { |
| 526 | buf[o] = data[*len]; |
| 527 | (*len)++; |
| 528 | } |
| 529 | } else if (data[*len] == '&') { |
Radek Krejci | 709fee6 | 2015-04-15 13:56:19 +0200 | [diff] [blame] | 530 | (*len)++; |
| 531 | if (data[*len] != '#') { |
| 532 | /* entity reference - only predefined refs are supported */ |
| 533 | if (!memcmp(&data[*len], "lt;", 3)) { |
| 534 | buf[o] = '<'; |
| 535 | *len += 3; |
| 536 | } else if (!memcmp(&data[*len], "gt;", 3)) { |
| 537 | buf[o] = '>'; |
| 538 | *len += 3; |
| 539 | } else if (!memcmp(&data[*len], "amp;", 4)) { |
| 540 | buf[o] = '&'; |
| 541 | *len += 4; |
| 542 | } else if (!memcmp(&data[*len], "apos;", 5)) { |
| 543 | buf[o] = '\''; |
| 544 | *len += 5; |
| 545 | } else if (!memcmp(&data[*len], "quot;", 5)) { |
| 546 | buf[o] = '\"'; |
| 547 | *len += 5; |
| 548 | } else { |
Radek Krejci | 3045cf3 | 2015-05-28 10:58:52 +0200 | [diff] [blame^] | 549 | LOGERR(LY_EWELLFORM, |
Radek Krejci | 709fee6 | 2015-04-15 13:56:19 +0200 | [diff] [blame] | 550 | "Invalid entity reference, only predefined entity references are supported."); |
| 551 | goto error; |
| 552 | } |
| 553 | } else { |
| 554 | /* character reference */ |
| 555 | (*len)++; |
| 556 | if (isdigit(data[*len])) { |
| 557 | for (n = 0; isdigit(data[*len]); (*len)++) { |
| 558 | n = (10 * n) + (data[*len] - '0'); |
| 559 | } |
| 560 | if (data[*len] != ';') { |
Radek Krejci | 3045cf3 | 2015-05-28 10:58:52 +0200 | [diff] [blame^] | 561 | LOGERR(LY_EWELLFORM, |
Radek Krejci | 709fee6 | 2015-04-15 13:56:19 +0200 | [diff] [blame] | 562 | "Invalid character reference, missing semicolon."); |
| 563 | goto error; |
| 564 | } |
| 565 | } else if (data[(*len)++] == 'x' && isxdigit(data[*len])) { |
| 566 | for (n = 0; isxdigit(data[*len]); (*len)++) { |
| 567 | if (isdigit(data[*len])) { |
| 568 | r = (data[*len] - '0'); |
| 569 | } else if (data[*len] > 'F') { |
| 570 | r = 10 + (data[*len] - 'a'); |
| 571 | } else { |
| 572 | r = 10 + (data[*len] - 'A'); |
| 573 | } |
| 574 | n = (16 * n) + r; |
| 575 | } |
| 576 | } else { |
Radek Krejci | 3045cf3 | 2015-05-28 10:58:52 +0200 | [diff] [blame^] | 577 | LOGERR(LY_EWELLFORM, "Invalid character reference."); |
Radek Krejci | 709fee6 | 2015-04-15 13:56:19 +0200 | [diff] [blame] | 578 | goto error; |
| 579 | |
| 580 | } |
| 581 | r = pututf8(&buf[o], n); |
| 582 | if (!r) { |
Radek Krejci | 3045cf3 | 2015-05-28 10:58:52 +0200 | [diff] [blame^] | 583 | LOGERR(LY_EWELLFORM, "Invalid character reference value."); |
Radek Krejci | 709fee6 | 2015-04-15 13:56:19 +0200 | [diff] [blame] | 584 | goto error; |
| 585 | } |
| 586 | o += r - 1; /* o is ++ in for loop */ |
| 587 | (*len)++; |
| 588 | } |
| 589 | } else { |
| 590 | buf[o] = data[*len]; |
Radek Krejci | 3045cf3 | 2015-05-28 10:58:52 +0200 | [diff] [blame^] | 591 | COUNTLINE(buf[o]); |
Radek Krejci | 709fee6 | 2015-04-15 13:56:19 +0200 | [diff] [blame] | 592 | (*len)++; |
| 593 | } |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 594 | } |
| 595 | |
Radek Krejci | a4a8406 | 2015-04-16 13:00:10 +0200 | [diff] [blame] | 596 | if (delim == '<' && !memcmp(&data[*len], "<![CDATA[", 9)) { |
| 597 | /* ignore loop's end condition on beginning of CDSect */ |
| 598 | goto loop; |
| 599 | } |
| 600 | |
Radek Krejci | 709fee6 | 2015-04-15 13:56:19 +0200 | [diff] [blame] | 601 | #undef BUFSIZE |
| 602 | |
| 603 | if (o) { |
| 604 | if (result) { |
| 605 | size = size + o; |
Radek Krejci | 9c16b33 | 2015-04-20 17:37:54 +0200 | [diff] [blame] | 606 | aux = realloc(result, size + 1); |
Radek Krejci | 709fee6 | 2015-04-15 13:56:19 +0200 | [diff] [blame] | 607 | result = aux; |
| 608 | } else { |
| 609 | size = o; |
Radek Krejci | 9c16b33 | 2015-04-20 17:37:54 +0200 | [diff] [blame] | 610 | result = malloc((size + 1) * sizeof *result); |
Radek Krejci | 709fee6 | 2015-04-15 13:56:19 +0200 | [diff] [blame] | 611 | } |
| 612 | memcpy(&result[size - o], buf, o); |
| 613 | } |
Radek Krejci | 674e1f8 | 2015-04-21 14:12:19 +0200 | [diff] [blame] | 614 | if (result) { |
| 615 | result[size] = '\0'; |
| 616 | } |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 617 | |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 618 | return result; |
Radek Krejci | 709fee6 | 2015-04-15 13:56:19 +0200 | [diff] [blame] | 619 | |
| 620 | error: |
| 621 | free(result); |
| 622 | return NULL; |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 623 | } |
| 624 | |
Radek Krejci | 674e1f8 | 2015-04-21 14:12:19 +0200 | [diff] [blame] | 625 | static struct lyxml_ns *get_ns(struct lyxml_elem *elem, const char *prefix) |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 626 | { |
Radek Krejci | 674e1f8 | 2015-04-21 14:12:19 +0200 | [diff] [blame] | 627 | struct lyxml_attr *attr; |
| 628 | int len; |
| 629 | |
| 630 | if (!elem) { |
| 631 | return NULL; |
| 632 | } |
| 633 | |
| 634 | if (!prefix) { |
| 635 | len = 0; |
| 636 | } else { |
| 637 | len = strlen(prefix); |
| 638 | } |
| 639 | |
| 640 | for (attr = elem->attr; attr; attr = attr->next) { |
| 641 | if (attr->type != LYXML_ATTR_NS) { |
| 642 | continue; |
| 643 | } |
| 644 | if (!attr->name) { |
| 645 | if (!len) { |
| 646 | /* default namespace found */ |
| 647 | if (!attr->value) { |
| 648 | /* empty default namespace -> no default namespace */ |
| 649 | return NULL; |
| 650 | } |
| 651 | return (struct lyxml_ns *)attr; |
| 652 | } |
| 653 | } else if (len && !memcmp(attr->name, prefix, len)) { |
| 654 | /* prefix found */ |
| 655 | return (struct lyxml_ns *)attr; |
| 656 | } |
| 657 | } |
| 658 | |
| 659 | /* go recursively */ |
| 660 | return get_ns(elem->parent, prefix); |
| 661 | } |
| 662 | |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 663 | static struct lyxml_attr *parse_attr(struct ly_ctx *ctx, const char *data, |
| 664 | unsigned int *len, struct lyxml_elem *elem) |
Radek Krejci | 674e1f8 | 2015-04-21 14:12:19 +0200 | [diff] [blame] | 665 | { |
| 666 | const char *c = data, *start, *delim; |
| 667 | char prefix[32]; |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 668 | int uc; |
| 669 | struct lyxml_attr *attr = NULL; |
| 670 | unsigned int size; |
| 671 | |
Radek Krejci | 4ea0838 | 2015-04-21 09:41:40 +0200 | [diff] [blame] | 672 | |
Radek Krejci | 674e1f8 | 2015-04-21 14:12:19 +0200 | [diff] [blame] | 673 | /* check if it is attribute or namespace */ |
| 674 | if (!memcmp(c, "xmlns", 5)) { |
| 675 | /* namespace */ |
| 676 | attr = calloc(1, sizeof(struct lyxml_ns)); |
| 677 | attr->type = LYXML_ATTR_NS; |
| 678 | c += 5; |
| 679 | if (*c != ':') { |
| 680 | /* default namespace, prefix will be empty */ |
| 681 | goto equal; |
| 682 | } |
| 683 | c++; /* go after ':' to the prefix value */ |
| 684 | } else { |
| 685 | /* attribute */ |
Radek Krejci | bcf46db | 2015-05-26 15:40:56 +0200 | [diff] [blame] | 686 | attr = calloc(1, sizeof *attr); |
Radek Krejci | 674e1f8 | 2015-04-21 14:12:19 +0200 | [diff] [blame] | 687 | attr->type = LYXML_ATTR_STD; |
| 688 | } |
Radek Krejci | 4ea0838 | 2015-04-21 09:41:40 +0200 | [diff] [blame] | 689 | |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 690 | /* process name part of the attribute */ |
Radek Krejci | 674e1f8 | 2015-04-21 14:12:19 +0200 | [diff] [blame] | 691 | start = c; |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 692 | uc = getutf8(c, &size); |
| 693 | if (!is_xmlnamestartchar(uc)) { |
Radek Krejci | 3045cf3 | 2015-05-28 10:58:52 +0200 | [diff] [blame^] | 694 | LOGERR(LY_EWELLFORM, "Invalid NameStartChar of the attribute"); |
Radek Krejci | 674e1f8 | 2015-04-21 14:12:19 +0200 | [diff] [blame] | 695 | free(attr); |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 696 | return NULL; |
| 697 | } |
| 698 | c += size; |
| 699 | uc = getutf8(c, &size); |
| 700 | while (is_xmlnamechar(uc)) { |
Radek Krejci | 674e1f8 | 2015-04-21 14:12:19 +0200 | [diff] [blame] | 701 | if (attr->type == LYXML_ATTR_STD && *c == ':') { |
| 702 | /* attribute in a namespace */ |
| 703 | start = c + 1; |
| 704 | |
| 705 | /* look for the prefix in namespaces */ |
| 706 | memcpy(prefix, data, c - data); |
| 707 | prefix[c - data] = '\0'; |
| 708 | attr->ns = get_ns(elem, prefix); |
| 709 | } |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 710 | c += size; |
| 711 | uc = getutf8(c, &size); |
| 712 | } |
| 713 | |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 714 | /* store the name */ |
Radek Krejci | 674e1f8 | 2015-04-21 14:12:19 +0200 | [diff] [blame] | 715 | size = c - start; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 716 | attr->name = lydict_insert(ctx, start, size); |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 717 | |
Radek Krejci | 674e1f8 | 2015-04-21 14:12:19 +0200 | [diff] [blame] | 718 | |
| 719 | equal: |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 720 | /* check Eq mark that can be surrounded by whitespaces */ |
| 721 | ign_xmlws(c); |
| 722 | if (*c != '=') { |
Radek Krejci | 3045cf3 | 2015-05-28 10:58:52 +0200 | [diff] [blame^] | 723 | LOGERR(LY_EWELLFORM, "Invalid attribute definition, \"=\" expected."); |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 724 | goto error; |
| 725 | } |
| 726 | c++; |
| 727 | ign_xmlws(c); |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 728 | |
| 729 | /* process value part of the attribute */ |
| 730 | if (!*c || (*c != '"' && *c != '\'')) { |
Radek Krejci | 3045cf3 | 2015-05-28 10:58:52 +0200 | [diff] [blame^] | 731 | LOGERR(LY_EWELLFORM, "Invalid attribute value, \" or \' expected."); |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 732 | goto error; |
| 733 | } |
| 734 | delim = c; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 735 | attr->value = lydict_insert_zc(ctx, parse_text(++c, *delim, &size)); |
Radek Krejci | 521008e | 2015-04-15 14:41:07 +0200 | [diff] [blame] | 736 | if (ly_errno) { |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 737 | goto error; |
| 738 | } |
| 739 | |
Radek Krejci | 521008e | 2015-04-15 14:41:07 +0200 | [diff] [blame] | 740 | *len = c + size + 1 - data; /* +1 is delimiter size */ |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 741 | return attr; |
| 742 | |
| 743 | error: |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 744 | lyxml_free_attr(ctx, attr); |
Radek Krejci | 54ea8de | 2015-04-09 18:02:56 +0200 | [diff] [blame] | 745 | return NULL; |
| 746 | } |
| 747 | |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 748 | static struct lyxml_elem *parse_elem(struct ly_ctx *ctx, const char *data, |
| 749 | unsigned int *len, |
| 750 | struct lyxml_elem *parent) |
Radek Krejci | 54ea8de | 2015-04-09 18:02:56 +0200 | [diff] [blame] | 751 | { |
Radek Krejci | 674e1f8 | 2015-04-21 14:12:19 +0200 | [diff] [blame] | 752 | const char *c = data, *start, *e; |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 753 | const char *lws; /* leading white space for handling mixed content */ |
| 754 | int uc; |
| 755 | char *str; |
Radek Krejci | 674e1f8 | 2015-04-21 14:12:19 +0200 | [diff] [blame] | 756 | char prefix[32] = {0}; |
| 757 | unsigned int prefix_len = 0; |
Radek Krejci | 39ebd8e | 2015-05-26 15:49:54 +0200 | [diff] [blame] | 758 | struct lyxml_elem *elem = NULL, *child; |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 759 | struct lyxml_attr *attr; |
Radek Krejci | 05e37a3 | 2015-04-15 14:40:34 +0200 | [diff] [blame] | 760 | unsigned int size; |
Radek Krejci | 674e1f8 | 2015-04-21 14:12:19 +0200 | [diff] [blame] | 761 | int nons_flag = 0, closed_flag = 0; |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 762 | |
| 763 | *len = 0; |
| 764 | |
| 765 | if (*c != '<') { |
| 766 | return NULL; |
| 767 | } |
| 768 | |
| 769 | /* locate element name */ |
| 770 | c++; |
| 771 | e = c; |
| 772 | |
Radek Krejci | 05e37a3 | 2015-04-15 14:40:34 +0200 | [diff] [blame] | 773 | uc = getutf8(e, &size); |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 774 | if (!is_xmlnamestartchar(uc)) { |
Radek Krejci | 3045cf3 | 2015-05-28 10:58:52 +0200 | [diff] [blame^] | 775 | LOGERR(LY_EWELLFORM, "Invalid NameStartChar of the attribute"); |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 776 | return NULL; |
| 777 | } |
Radek Krejci | 05e37a3 | 2015-04-15 14:40:34 +0200 | [diff] [blame] | 778 | e += size; |
| 779 | uc = getutf8(e, &size); |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 780 | while (is_xmlnamechar(uc)) { |
Radek Krejci | 674e1f8 | 2015-04-21 14:12:19 +0200 | [diff] [blame] | 781 | if (*e == ':') { |
| 782 | if (prefix_len) { |
Radek Krejci | 3045cf3 | 2015-05-28 10:58:52 +0200 | [diff] [blame^] | 783 | LOGERR(LY_EWELLFORM, "Multiple colons in element name."); |
Radek Krejci | 674e1f8 | 2015-04-21 14:12:19 +0200 | [diff] [blame] | 784 | goto error; |
| 785 | } |
| 786 | /* element in a namespace */ |
| 787 | start = e + 1; |
| 788 | |
| 789 | /* look for the prefix in namespaces */ |
| 790 | memcpy(prefix, c, prefix_len = e - c); |
| 791 | prefix[prefix_len] = '\0'; |
| 792 | c = start; |
| 793 | } |
Radek Krejci | 05e37a3 | 2015-04-15 14:40:34 +0200 | [diff] [blame] | 794 | e += size; |
| 795 | uc = getutf8(e, &size); |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 796 | } |
| 797 | if (!*e) { |
Radek Krejci | 3045cf3 | 2015-05-28 10:58:52 +0200 | [diff] [blame^] | 798 | LOGERR(LY_EWELLFORM, "Unexpected end of input data."); |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 799 | return NULL; |
| 800 | } |
| 801 | |
| 802 | /* allocate element structure */ |
| 803 | elem = calloc(1, sizeof *elem); |
Radek Krejci | 3045cf3 | 2015-05-28 10:58:52 +0200 | [diff] [blame^] | 804 | #ifndef NDEBUG |
| 805 | elem->line = lineno; |
| 806 | #endif |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 807 | elem->next = NULL; |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 808 | elem->prev = elem; |
Radek Krejci | 674e1f8 | 2015-04-21 14:12:19 +0200 | [diff] [blame] | 809 | if (parent) { |
| 810 | lyxml_add_child(parent, elem); |
| 811 | } |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 812 | |
| 813 | /* store the name into the element structure */ |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 814 | elem->name = lydict_insert(ctx, c, e - c); |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 815 | c = e; |
| 816 | |
| 817 | process: |
Radek Krejci | 709fee6 | 2015-04-15 13:56:19 +0200 | [diff] [blame] | 818 | ly_errno = 0; |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 819 | ign_xmlws(c); |
| 820 | if (!memcmp("/>", c, 2)) { |
| 821 | /* we are done, it was EmptyElemTag */ |
| 822 | c += 2; |
Radek Krejci | 674e1f8 | 2015-04-21 14:12:19 +0200 | [diff] [blame] | 823 | closed_flag = 1; |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 824 | } else if (*c == '>') { |
| 825 | /* process element content */ |
| 826 | c++; |
| 827 | lws = NULL; |
| 828 | |
| 829 | while (*c) { |
| 830 | if (!memcmp(c, "</", 2)) { |
Radek Krejci | 674e1f8 | 2015-04-21 14:12:19 +0200 | [diff] [blame] | 831 | if (lws && !elem->child) { |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 832 | /* leading white spaces were actually content */ |
| 833 | goto store_content; |
| 834 | } |
| 835 | |
| 836 | /* Etag */ |
| 837 | c += 2; |
| 838 | /* get name and check it */ |
| 839 | e = c; |
Radek Krejci | 05e37a3 | 2015-04-15 14:40:34 +0200 | [diff] [blame] | 840 | uc = getutf8(e, &size); |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 841 | if (!is_xmlnamestartchar(uc)) { |
Radek Krejci | 3045cf3 | 2015-05-28 10:58:52 +0200 | [diff] [blame^] | 842 | LOGERR(LY_EWELLFORM, |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 843 | "Invalid NameStartChar of the attribute"); |
| 844 | goto error; |
| 845 | } |
Radek Krejci | 05e37a3 | 2015-04-15 14:40:34 +0200 | [diff] [blame] | 846 | e += size; |
| 847 | uc = getutf8(e, &size); |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 848 | while (is_xmlnamechar(uc)) { |
Radek Krejci | 674e1f8 | 2015-04-21 14:12:19 +0200 | [diff] [blame] | 849 | if (*e == ':') { |
| 850 | /* element in a namespace */ |
| 851 | start = e + 1; |
| 852 | |
| 853 | /* look for the prefix in namespaces */ |
| 854 | if (memcmp(prefix, c, e - c)) { |
Radek Krejci | 3045cf3 | 2015-05-28 10:58:52 +0200 | [diff] [blame^] | 855 | LOGERR(LY_EWELLFORM, |
Radek Krejci | 674e1f8 | 2015-04-21 14:12:19 +0200 | [diff] [blame] | 856 | "Mixed opening (%s) and closing element tag - different namespaces", |
| 857 | elem->name); |
| 858 | } |
| 859 | c = start; |
| 860 | } |
Radek Krejci | 05e37a3 | 2015-04-15 14:40:34 +0200 | [diff] [blame] | 861 | e += size; |
| 862 | uc = getutf8(e, &size); |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 863 | } |
| 864 | if (!*e) { |
Radek Krejci | 3045cf3 | 2015-05-28 10:58:52 +0200 | [diff] [blame^] | 865 | LOGERR(LY_EWELLFORM, "Unexpected end of input data."); |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 866 | goto error; |
| 867 | } |
| 868 | |
| 869 | /* check that it corresponds to opening tag */ |
Radek Krejci | 05e37a3 | 2015-04-15 14:40:34 +0200 | [diff] [blame] | 870 | size = e - c; |
| 871 | str = malloc((size + 1) * sizeof *str); |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 872 | memcpy(str, c, e - c); |
| 873 | str[e - c] = '\0'; |
Radek Krejci | 05e37a3 | 2015-04-15 14:40:34 +0200 | [diff] [blame] | 874 | if (size != strlen(elem->name) || |
| 875 | memcmp(str, elem->name, size)) { |
Radek Krejci | 3045cf3 | 2015-05-28 10:58:52 +0200 | [diff] [blame^] | 876 | LOGERR(LY_EWELLFORM, |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 877 | "Mixed opening (%s) and closing (%s) element tag", |
Radek Krejci | 674e1f8 | 2015-04-21 14:12:19 +0200 | [diff] [blame] | 878 | elem->name, str); |
Radek Krejci | 6f0c6f9 | 2015-05-25 15:01:15 +0200 | [diff] [blame] | 879 | free(str); |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 880 | goto error; |
| 881 | } |
| 882 | free(str); |
| 883 | c = e; |
| 884 | |
| 885 | ign_xmlws(c); |
| 886 | if (*c != '>') { |
Radek Krejci | 3045cf3 | 2015-05-28 10:58:52 +0200 | [diff] [blame^] | 887 | LOGERR(LY_EWELLFORM, |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 888 | "Close element tag \"%s\" contain additional data.", |
| 889 | elem->name); |
| 890 | goto error; |
| 891 | } |
| 892 | c++; |
Radek Krejci | 674e1f8 | 2015-04-21 14:12:19 +0200 | [diff] [blame] | 893 | closed_flag = 1; |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 894 | break; |
| 895 | |
| 896 | } else if (!memcmp(c, "<?", 2)) { |
| 897 | if (lws) { |
| 898 | /* leading white spaces were only formatting */ |
| 899 | lws = NULL; |
| 900 | } |
| 901 | /* PI - ignore it */ |
| 902 | c += 2; |
Radek Krejci | 05e37a3 | 2015-04-15 14:40:34 +0200 | [diff] [blame] | 903 | if (parse_ignore(c, "?>", &size)) { |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 904 | goto error; |
| 905 | } |
| 906 | c += size; |
| 907 | } else if (!memcmp(c, "<!--", 4)) { |
| 908 | if (lws) { |
| 909 | /* leading white spaces were only formatting */ |
| 910 | lws = NULL; |
| 911 | } |
| 912 | /* Comment - ignore it */ |
| 913 | c += 4; |
Radek Krejci | 05e37a3 | 2015-04-15 14:40:34 +0200 | [diff] [blame] | 914 | if (parse_ignore(c, "-->", &size)) { |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 915 | goto error; |
| 916 | } |
| 917 | c += size; |
| 918 | } else if (!memcmp(c, "<![CDATA[", 9)) { |
| 919 | /* CDSect */ |
Radek Krejci | a4a8406 | 2015-04-16 13:00:10 +0200 | [diff] [blame] | 920 | goto store_content; |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 921 | } else if (*c == '<') { |
| 922 | if (lws) { |
Radek Krejci | f0023a9 | 2015-04-20 20:51:39 +0200 | [diff] [blame] | 923 | if (elem->flags & LYXML_ELEM_MIXED) { |
| 924 | /* we have a mixed content */ |
| 925 | goto store_content; |
| 926 | } else { |
| 927 | /* leading white spaces were only formatting */ |
| 928 | lws = NULL; |
| 929 | } |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 930 | } |
| 931 | if (elem->content) { |
| 932 | /* we have a mixed content */ |
| 933 | child = calloc(1, sizeof *child); |
| 934 | child->content = elem->content; |
| 935 | elem->content = NULL; |
| 936 | lyxml_add_child(elem, child); |
Radek Krejci | f0023a9 | 2015-04-20 20:51:39 +0200 | [diff] [blame] | 937 | elem->flags |= LYXML_ELEM_MIXED; |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 938 | } |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 939 | child = parse_elem(ctx, c, &size, elem); |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 940 | if (!child) { |
Radek Krejci | 3045cf3 | 2015-05-28 10:58:52 +0200 | [diff] [blame^] | 941 | LOGERR(LY_EWELLFORM, "Unexpected end of input data."); |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 942 | goto error; |
| 943 | } |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 944 | c += size; /* move after processed child element */ |
| 945 | } else if (is_xmlws(*c)) { |
| 946 | lws = c; |
Radek Krejci | 3045cf3 | 2015-05-28 10:58:52 +0200 | [diff] [blame^] | 947 | #ifndef NDEBUG |
| 948 | lws_lineno = lineno; |
| 949 | #endif |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 950 | ign_xmlws(c); |
| 951 | } else { |
| 952 | store_content: |
| 953 | /* store text content */ |
| 954 | if (lws) { |
| 955 | /* process content including the leading white spaces */ |
| 956 | c = lws; |
Radek Krejci | 3045cf3 | 2015-05-28 10:58:52 +0200 | [diff] [blame^] | 957 | #ifndef NDEBUG |
| 958 | lineno = lws_lineno; |
| 959 | #endif |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 960 | lws = NULL; |
| 961 | } |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 962 | elem->content = lydict_insert_zc(ctx, parse_text(c, '<', &size)); |
Radek Krejci | 521008e | 2015-04-15 14:41:07 +0200 | [diff] [blame] | 963 | if (ly_errno) { |
Radek Krejci | 709fee6 | 2015-04-15 13:56:19 +0200 | [diff] [blame] | 964 | goto error; |
| 965 | } |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 966 | c += size; /* move after processed text content */ |
| 967 | |
| 968 | if (elem->child) { |
| 969 | /* we have a mixed content */ |
| 970 | child = calloc(1, sizeof *child); |
| 971 | child->content = elem->content; |
| 972 | elem->content = NULL; |
| 973 | lyxml_add_child(elem, child); |
Radek Krejci | f0023a9 | 2015-04-20 20:51:39 +0200 | [diff] [blame] | 974 | elem->flags |= LYXML_ELEM_MIXED; |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 975 | } |
| 976 | } |
| 977 | } |
| 978 | } else { |
| 979 | /* process attribute */ |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 980 | attr = parse_attr(ctx, c, &size, elem); |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 981 | if (!attr) { |
Radek Krejci | 3045cf3 | 2015-05-28 10:58:52 +0200 | [diff] [blame^] | 982 | LOGERR(LY_EWELLFORM, "Unexpected end of input data."); |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 983 | goto error; |
| 984 | } |
| 985 | lyxml_add_attr(elem, attr); |
| 986 | c += size; /* move after processed attribute */ |
| 987 | |
Radek Krejci | 674e1f8 | 2015-04-21 14:12:19 +0200 | [diff] [blame] | 988 | /* check namespace */ |
| 989 | if (attr->type == LYXML_ATTR_NS) { |
| 990 | if (!prefix[0] && !attr->name) { |
| 991 | if (attr->value) { |
| 992 | /* default prefix */ |
| 993 | elem->ns = (struct lyxml_ns *)attr; |
| 994 | } else { |
| 995 | /* xmlns="" -> no namespace */ |
| 996 | nons_flag = 1; |
| 997 | } |
| 998 | } else if (prefix[0] && attr->name && |
| 999 | !memcmp(attr->name, prefix, prefix_len + 1)) { |
| 1000 | /* matching namespace with prefix */ |
| 1001 | elem->ns = (struct lyxml_ns *)attr; |
| 1002 | } |
| 1003 | } |
| 1004 | |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 1005 | /* go back to finish element processing */ |
| 1006 | goto process; |
| 1007 | } |
| 1008 | |
| 1009 | *len = c - data; |
| 1010 | |
Radek Krejci | 674e1f8 | 2015-04-21 14:12:19 +0200 | [diff] [blame] | 1011 | if (!closed_flag) { |
Radek Krejci | 3045cf3 | 2015-05-28 10:58:52 +0200 | [diff] [blame^] | 1012 | LOGERR(LY_EWELLFORM, "Missing closing element tag (%s).", elem->name); |
Radek Krejci | 674e1f8 | 2015-04-21 14:12:19 +0200 | [diff] [blame] | 1013 | goto error; |
| 1014 | } |
| 1015 | |
| 1016 | if (!nons_flag && parent) { |
| 1017 | elem->ns = get_ns(parent, prefix_len ? prefix : NULL); |
| 1018 | } |
| 1019 | |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 1020 | return elem; |
| 1021 | |
| 1022 | error: |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1023 | lyxml_free_elem(ctx, elem); |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 1024 | |
Radek Krejci | 54ea8de | 2015-04-09 18:02:56 +0200 | [diff] [blame] | 1025 | return NULL; |
| 1026 | } |
| 1027 | |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1028 | struct lyxml_elem *lyxml_read(struct ly_ctx *ctx, const char *data, |
| 1029 | int UNUSED(options)) |
Radek Krejci | 54ea8de | 2015-04-09 18:02:56 +0200 | [diff] [blame] | 1030 | { |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 1031 | const char *c = data; |
Radek Krejci | 05e37a3 | 2015-04-15 14:40:34 +0200 | [diff] [blame] | 1032 | unsigned int len; |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 1033 | struct lyxml_elem *root = NULL; |
| 1034 | |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1035 | if (!data || !ctx) { |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 1036 | ly_errno = LY_EINVAL; |
| 1037 | return NULL; |
| 1038 | } |
| 1039 | |
Radek Krejci | 3045cf3 | 2015-05-28 10:58:52 +0200 | [diff] [blame^] | 1040 | #ifndef NDEBUG |
| 1041 | /* TODO: threads support */ |
| 1042 | lineno = 1; |
| 1043 | #endif |
| 1044 | |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 1045 | /* process document */ |
| 1046 | while (*c) { |
| 1047 | if (is_xmlws(*c)) { |
| 1048 | /* skip whitespaces */ |
Radek Krejci | 3045cf3 | 2015-05-28 10:58:52 +0200 | [diff] [blame^] | 1049 | ign_xmlws(c); |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 1050 | } else if (!memcmp(c, "<?", 2)) { |
| 1051 | /* XMLDecl or PI - ignore it */ |
| 1052 | c += 2; |
Radek Krejci | 05e37a3 | 2015-04-15 14:40:34 +0200 | [diff] [blame] | 1053 | if (parse_ignore(c, "?>", &len)) { |
Radek Krejci | 3045cf3 | 2015-05-28 10:58:52 +0200 | [diff] [blame^] | 1054 | LOGERR(LY_EWELLFORM, "Missing close sequence \"?>\"."); |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 1055 | return NULL; |
| 1056 | } |
| 1057 | c += len; |
| 1058 | } else if (!memcmp(c, "<!--", 4)) { |
| 1059 | /* Comment - ignore it */ |
| 1060 | c += 2; |
Radek Krejci | 05e37a3 | 2015-04-15 14:40:34 +0200 | [diff] [blame] | 1061 | if (parse_ignore(c, "-->", &len)) { |
Radek Krejci | 3045cf3 | 2015-05-28 10:58:52 +0200 | [diff] [blame^] | 1062 | LOGERR(LY_EWELLFORM, "Missing close sequence \"-->\"."); |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 1063 | return NULL; |
| 1064 | } |
| 1065 | c += len; |
| 1066 | } else if (!memcmp(c, "<!", 2)) { |
| 1067 | /* DOCTYPE */ |
| 1068 | /* TODO - standalone ignore counting < and > */ |
Radek Krejci | 3045cf3 | 2015-05-28 10:58:52 +0200 | [diff] [blame^] | 1069 | LOGERR(LY_EINVAL, "DOCTYPE not implemented."); |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 1070 | return NULL; |
| 1071 | } else if (*c == '<') { |
| 1072 | /* element - process it in next loop to strictly follow XML |
| 1073 | * format |
| 1074 | */ |
| 1075 | break; |
| 1076 | } |
| 1077 | } |
| 1078 | |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1079 | root = parse_elem(ctx, c, &len, NULL); |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 1080 | if (!root) { |
| 1081 | return NULL; |
| 1082 | } |
| 1083 | c += len; |
| 1084 | |
| 1085 | /* ignore the rest of document where can be comments, PIs and whitespaces, |
| 1086 | * note that we are not detecting syntax errors in these parts |
| 1087 | */ |
| 1088 | ign_xmlws(c); |
| 1089 | if (*c) { |
Radek Krejci | 3045cf3 | 2015-05-28 10:58:52 +0200 | [diff] [blame^] | 1090 | LOGWRN("There are some not parsed data:\n%s", c); |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 1091 | } |
| 1092 | |
| 1093 | return root; |
| 1094 | } |
| 1095 | |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1096 | struct lyxml_elem *lyxml_read_fd(struct ly_ctx *ctx, int fd, |
| 1097 | int UNUSED(options)) |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 1098 | { |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1099 | if (fd == -1 || !ctx) { |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 1100 | ly_errno = LY_EINVAL; |
| 1101 | return NULL; |
| 1102 | } |
| 1103 | |
Radek Krejci | 54ea8de | 2015-04-09 18:02:56 +0200 | [diff] [blame] | 1104 | return NULL; |
| 1105 | } |
| 1106 | |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1107 | struct lyxml_elem *lyxml_read_file(struct ly_ctx *ctx, const char *filename, |
| 1108 | int UNUSED(options)) |
Radek Krejci | 54ea8de | 2015-04-09 18:02:56 +0200 | [diff] [blame] | 1109 | { |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1110 | if (!filename || !ctx) { |
Radek Krejci | 3045cf3 | 2015-05-28 10:58:52 +0200 | [diff] [blame^] | 1111 | LOGERR(LY_EINVAL, NULL); |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 1112 | return NULL; |
| 1113 | } |
Radek Krejci | 54ea8de | 2015-04-09 18:02:56 +0200 | [diff] [blame] | 1114 | |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 1115 | return NULL; |
Radek Krejci | 54ea8de | 2015-04-09 18:02:56 +0200 | [diff] [blame] | 1116 | } |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 1117 | |
Radek Krejci | f0023a9 | 2015-04-20 20:51:39 +0200 | [diff] [blame] | 1118 | static int dump_text(FILE *f, char* text) |
| 1119 | { |
| 1120 | unsigned int i, n; |
| 1121 | |
| 1122 | for (i = n = 0; text[i]; i++) { |
| 1123 | switch (text[i]) { |
| 1124 | case '&': |
| 1125 | n += fprintf(f, "&"); |
| 1126 | break; |
| 1127 | case '<': |
| 1128 | n += fprintf(f, "<"); |
| 1129 | break; |
Radek Krejci | 674e1f8 | 2015-04-21 14:12:19 +0200 | [diff] [blame] | 1130 | case '>': |
| 1131 | /* not needed, just for readability */ |
| 1132 | n += fprintf(f, ">"); |
| 1133 | break; |
Radek Krejci | f0023a9 | 2015-04-20 20:51:39 +0200 | [diff] [blame] | 1134 | default: |
| 1135 | fputc(text[i], f); |
| 1136 | n++; |
| 1137 | } |
| 1138 | } |
| 1139 | |
| 1140 | return n; |
| 1141 | } |
| 1142 | |
| 1143 | static int dump_elem(FILE *f, struct lyxml_elem *e, int level) |
| 1144 | { |
| 1145 | int size = 0; |
| 1146 | struct lyxml_attr *a; |
| 1147 | struct lyxml_elem *child; |
Radek Krejci | 674e1f8 | 2015-04-21 14:12:19 +0200 | [diff] [blame] | 1148 | const char *delim, *delim_outer; |
| 1149 | int indent; |
Radek Krejci | f0023a9 | 2015-04-20 20:51:39 +0200 | [diff] [blame] | 1150 | |
| 1151 | if (!e->name) { |
| 1152 | /* mixed content */ |
| 1153 | if (e->content) { |
| 1154 | return dump_text(f, e->content); |
| 1155 | } else { |
| 1156 | return 0; |
| 1157 | } |
| 1158 | } |
| 1159 | |
Radek Krejci | 674e1f8 | 2015-04-21 14:12:19 +0200 | [diff] [blame] | 1160 | delim = delim_outer = "\n"; |
| 1161 | indent = 2 * level; |
| 1162 | if ((e->flags & LYXML_ELEM_MIXED) || (e->parent && (e->parent->flags & LYXML_ELEM_MIXED))) { |
| 1163 | delim = ""; |
| 1164 | } |
| 1165 | if (e->parent && (e->parent->flags & LYXML_ELEM_MIXED)) { |
| 1166 | delim_outer = ""; |
| 1167 | indent = 0; |
Radek Krejci | f0023a9 | 2015-04-20 20:51:39 +0200 | [diff] [blame] | 1168 | } |
| 1169 | |
Radek Krejci | 674e1f8 | 2015-04-21 14:12:19 +0200 | [diff] [blame] | 1170 | /* opening tag */ |
| 1171 | if (e->ns && e->ns->prefix) { |
| 1172 | size += fprintf(f, "%*s<%s:%s", indent, "", e->ns->prefix, e->name); |
| 1173 | } else { |
| 1174 | size += fprintf(f, "%*s<%s", indent, "", e->name); |
Radek Krejci | f0023a9 | 2015-04-20 20:51:39 +0200 | [diff] [blame] | 1175 | } |
Radek Krejci | 674e1f8 | 2015-04-21 14:12:19 +0200 | [diff] [blame] | 1176 | |
| 1177 | /* attributes */ |
| 1178 | for (a = e->attr; a; a = a->next) { |
| 1179 | if (a->type == LYXML_ATTR_NS) { |
| 1180 | if (a->name) { |
| 1181 | size += fprintf(f, " xmlns:%s=\"%s\"", a->name, |
| 1182 | a->value ? a->value : ""); |
| 1183 | } else { |
| 1184 | size += fprintf(f, " xmlns=\"%s\"", a->value ? a->value : ""); |
| 1185 | } |
| 1186 | } else if (a->ns && a->ns->prefix) { |
| 1187 | size += fprintf(f, " %s:%s=\"%s\"", a->ns->prefix, a->name, |
| 1188 | a->value); |
| 1189 | } else { |
| 1190 | size += fprintf(f, " %s=\"%s\"", a->name, a->value); |
| 1191 | } |
| 1192 | } |
| 1193 | |
Radek Krejci | f0023a9 | 2015-04-20 20:51:39 +0200 | [diff] [blame] | 1194 | if (!e->child && !e->content) { |
Radek Krejci | 674e1f8 | 2015-04-21 14:12:19 +0200 | [diff] [blame] | 1195 | size += fprintf(f, "/>%s", delim); |
Radek Krejci | f0023a9 | 2015-04-20 20:51:39 +0200 | [diff] [blame] | 1196 | return size; |
| 1197 | } else if (e->content) { |
| 1198 | fputc('>', f); |
| 1199 | size++; |
| 1200 | |
| 1201 | size += dump_text(f, e->content); |
| 1202 | |
Radek Krejci | 674e1f8 | 2015-04-21 14:12:19 +0200 | [diff] [blame] | 1203 | |
| 1204 | if (e->ns && e->ns->prefix) { |
| 1205 | size += fprintf(f, "</%s:%s>%s", e->ns->prefix, e->name, delim); |
| 1206 | } else { |
| 1207 | size += fprintf(f, "</%s>%s", e->name, delim); |
| 1208 | } |
Radek Krejci | f0023a9 | 2015-04-20 20:51:39 +0200 | [diff] [blame] | 1209 | return size; |
| 1210 | } else { |
Radek Krejci | 674e1f8 | 2015-04-21 14:12:19 +0200 | [diff] [blame] | 1211 | size += fprintf(f, ">%s", delim); |
Radek Krejci | f0023a9 | 2015-04-20 20:51:39 +0200 | [diff] [blame] | 1212 | } |
| 1213 | |
| 1214 | /* go recursively */ |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1215 | LY_TREE_FOR(e->child, child) { |
Radek Krejci | f0023a9 | 2015-04-20 20:51:39 +0200 | [diff] [blame] | 1216 | size += dump_elem(f, child, level + 1); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1217 | } |
Radek Krejci | f0023a9 | 2015-04-20 20:51:39 +0200 | [diff] [blame] | 1218 | |
Radek Krejci | 674e1f8 | 2015-04-21 14:12:19 +0200 | [diff] [blame] | 1219 | /* closing tag */ |
| 1220 | if (e->ns && e->ns->prefix) { |
| 1221 | size += fprintf(f, "%*s</%s:%s>%s", indent, "", e->ns->prefix, e->name, |
| 1222 | delim_outer); |
Radek Krejci | f0023a9 | 2015-04-20 20:51:39 +0200 | [diff] [blame] | 1223 | } else { |
Radek Krejci | 674e1f8 | 2015-04-21 14:12:19 +0200 | [diff] [blame] | 1224 | size += fprintf(f, "%*s</%s>%s", indent, "", e->name, delim_outer); |
Radek Krejci | f0023a9 | 2015-04-20 20:51:39 +0200 | [diff] [blame] | 1225 | } |
| 1226 | |
| 1227 | return size; |
| 1228 | } |
| 1229 | |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1230 | int lyxml_dump(FILE *stream, struct lyxml_elem *elem, int UNUSED(options)) |
Radek Krejci | f0023a9 | 2015-04-20 20:51:39 +0200 | [diff] [blame] | 1231 | { |
| 1232 | if (!elem) { |
| 1233 | return 0; |
| 1234 | } |
| 1235 | |
| 1236 | return dump_elem(stream, elem, 0); |
| 1237 | } |