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 | * |
Radek Krejci | 54f6fb3 | 2016-02-24 12:56:39 +0100 | [diff] [blame] | 8 | * This source code is licensed under BSD 3-Clause License (the "License"). |
| 9 | * You may not use this file except in compliance with the License. |
| 10 | * You may obtain a copy of the License at |
Michal Vasko | 8de098c | 2016-02-26 10:00:25 +0100 | [diff] [blame] | 11 | * |
Radek Krejci | 54f6fb3 | 2016-02-24 12:56:39 +0100 | [diff] [blame] | 12 | * https://opensource.org/licenses/BSD-3-Clause |
Radek Krejci | 54ea8de | 2015-04-09 18:02:56 +0200 | [diff] [blame] | 13 | */ |
| 14 | |
Radek Krejci | 812b10a | 2015-05-28 16:48:25 +0200 | [diff] [blame] | 15 | #include <assert.h> |
Radek Krejci | 563427e | 2016-02-08 16:26:34 +0100 | [diff] [blame] | 16 | #include <errno.h> |
Radek Krejci | 709fee6 | 2015-04-15 13:56:19 +0200 | [diff] [blame] | 17 | #include <ctype.h> |
| 18 | #include <stdint.h> |
Radek Krejci | f0023a9 | 2015-04-20 20:51:39 +0200 | [diff] [blame] | 19 | #include <stdio.h> |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 20 | #include <stdlib.h> |
| 21 | #include <string.h> |
Radek Krejci | 54ea8de | 2015-04-09 18:02:56 +0200 | [diff] [blame] | 22 | #include <unistd.h> |
Radek Krejci | 563427e | 2016-02-08 16:26:34 +0100 | [diff] [blame] | 23 | #include <pthread.h> |
Pavol Vican | b2570c1 | 2015-11-12 13:50:20 +0100 | [diff] [blame] | 24 | #include <sys/stat.h> |
| 25 | #include <sys/mman.h> |
Radek Krejci | 563427e | 2016-02-08 16:26:34 +0100 | [diff] [blame] | 26 | #include <sys/syscall.h> |
Pavol Vican | b2570c1 | 2015-11-12 13:50:20 +0100 | [diff] [blame] | 27 | #include <fcntl.h> |
Radek Krejci | 54ea8de | 2015-04-09 18:02:56 +0200 | [diff] [blame] | 28 | |
Radek Krejci | 06a704e | 2015-04-22 14:50:49 +0200 | [diff] [blame] | 29 | #include "common.h" |
Radek Krejci | 41912fe | 2015-10-22 10:22:12 +0200 | [diff] [blame] | 30 | #include "dict_private.h" |
Radek Krejci | 5248f13 | 2015-10-09 10:34:25 +0200 | [diff] [blame] | 31 | #include "printer.h" |
Radek Krejci | 5449d47 | 2015-10-26 14:35:56 +0100 | [diff] [blame] | 32 | #include "parser.h" |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 33 | #include "tree_schema.h" |
Michal Vasko | fc5744d | 2015-10-22 12:09:34 +0200 | [diff] [blame] | 34 | #include "xml_internal.h" |
Radek Krejci | 54ea8de | 2015-04-09 18:02:56 +0200 | [diff] [blame] | 35 | |
Radek Krejci | 3045cf3 | 2015-05-28 10:58:52 +0200 | [diff] [blame] | 36 | #define ign_xmlws(p) \ |
Radek Krejci | 563427e | 2016-02-08 16:26:34 +0100 | [diff] [blame] | 37 | while (is_xmlws(*p)) { \ |
Radek Krejci | 563427e | 2016-02-08 16:26:34 +0100 | [diff] [blame] | 38 | p++; \ |
| 39 | } |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 40 | |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 41 | static struct lyxml_attr *lyxml_dup_attr(struct ly_ctx *ctx, struct lyxml_elem *parent, struct lyxml_attr *attr); |
| 42 | |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 43 | API const struct lyxml_ns * |
| 44 | lyxml_get_ns(const struct lyxml_elem *elem, const char *prefix) |
Michal Vasko | f8879c2 | 2015-08-21 09:07:36 +0200 | [diff] [blame] | 45 | { |
| 46 | struct lyxml_attr *attr; |
| 47 | int len; |
| 48 | |
| 49 | if (!elem) { |
| 50 | return NULL; |
| 51 | } |
| 52 | |
| 53 | if (!prefix) { |
| 54 | len = 0; |
| 55 | } else { |
| 56 | len = strlen(prefix) + 1; |
| 57 | } |
| 58 | |
| 59 | for (attr = elem->attr; attr; attr = attr->next) { |
| 60 | if (attr->type != LYXML_ATTR_NS) { |
| 61 | continue; |
| 62 | } |
| 63 | if (!attr->name) { |
| 64 | if (!len) { |
| 65 | /* default namespace found */ |
| 66 | if (!attr->value) { |
| 67 | /* empty default namespace -> no default namespace */ |
| 68 | return NULL; |
| 69 | } |
| 70 | return (struct lyxml_ns *)attr; |
| 71 | } |
| 72 | } else if (len && !memcmp(attr->name, prefix, len)) { |
| 73 | /* prefix found */ |
| 74 | return (struct lyxml_ns *)attr; |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | /* go recursively */ |
| 79 | return lyxml_get_ns(elem->parent, prefix); |
| 80 | } |
| 81 | |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 82 | static void |
| 83 | lyxml_correct_attr_ns(struct ly_ctx *ctx, struct lyxml_attr *attr, struct lyxml_elem *attr_parent, int copy_ns) |
| 84 | { |
| 85 | const struct lyxml_ns *tmp_ns; |
Michal Vasko | f610911 | 2015-12-03 14:00:42 +0100 | [diff] [blame] | 86 | struct lyxml_elem *ns_root, *attr_root; |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 87 | |
| 88 | if ((attr->type != LYXML_ATTR_NS) && attr->ns) { |
Michal Vasko | f610911 | 2015-12-03 14:00:42 +0100 | [diff] [blame] | 89 | /* find the root of attr */ |
| 90 | for (attr_root = attr_parent; attr_root->parent; attr_root = attr_root->parent); |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 91 | |
| 92 | /* find the root of attr NS */ |
| 93 | for (ns_root = attr->ns->parent; ns_root->parent; ns_root = ns_root->parent); |
| 94 | |
Michal Vasko | f610911 | 2015-12-03 14:00:42 +0100 | [diff] [blame] | 95 | /* attr NS is defined outside attr parent subtree */ |
| 96 | if (ns_root != attr_root) { |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 97 | if (copy_ns) { |
| 98 | tmp_ns = attr->ns; |
| 99 | /* we may have already copied the NS over? */ |
Radek Krejci | 66aca40 | 2016-05-24 15:23:02 +0200 | [diff] [blame] | 100 | attr->ns = lyxml_get_ns(attr_parent, tmp_ns->prefix); |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 101 | |
| 102 | /* we haven't copied it over, copy it now */ |
| 103 | if (!attr->ns) { |
Michal Vasko | f610911 | 2015-12-03 14:00:42 +0100 | [diff] [blame] | 104 | attr->ns = (struct lyxml_ns *)lyxml_dup_attr(ctx, attr_parent, (struct lyxml_attr *)tmp_ns); |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 105 | } |
| 106 | } else { |
| 107 | attr->ns = NULL; |
| 108 | } |
| 109 | } |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | static struct lyxml_attr * |
Michal Vasko | f8879c2 | 2015-08-21 09:07:36 +0200 | [diff] [blame] | 114 | lyxml_dup_attr(struct ly_ctx *ctx, struct lyxml_elem *parent, struct lyxml_attr *attr) |
| 115 | { |
| 116 | struct lyxml_attr *result, *a; |
| 117 | |
| 118 | if (!attr || !parent) { |
| 119 | return NULL; |
| 120 | } |
| 121 | |
| 122 | if (attr->type == LYXML_ATTR_NS) { |
| 123 | /* this is correct, despite that all attributes seems like a standard |
| 124 | * attributes (struct lyxml_attr), some of them can be namespace |
| 125 | * definitions (and in that case they are struct lyxml_ns). |
| 126 | */ |
| 127 | result = (struct lyxml_attr *)calloc(1, sizeof (struct lyxml_ns)); |
| 128 | } else { |
| 129 | result = calloc(1, sizeof (struct lyxml_attr)); |
| 130 | } |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 131 | if (!result) { |
| 132 | LOGMEM; |
| 133 | return NULL; |
| 134 | } |
Michal Vasko | f8879c2 | 2015-08-21 09:07:36 +0200 | [diff] [blame] | 135 | result->value = lydict_insert(ctx, attr->value, 0); |
| 136 | result->name = lydict_insert(ctx, attr->name, 0); |
| 137 | result->type = attr->type; |
| 138 | |
| 139 | /* set namespace in case of standard attributes */ |
| 140 | if (result->type == LYXML_ATTR_STD && attr->ns) { |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 141 | result->ns = attr->ns; |
| 142 | lyxml_correct_attr_ns(ctx, result, parent, 1); |
Michal Vasko | f8879c2 | 2015-08-21 09:07:36 +0200 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | /* set parent pointer in case of namespace attribute */ |
| 146 | if (result->type == LYXML_ATTR_NS) { |
| 147 | ((struct lyxml_ns *)result)->parent = parent; |
| 148 | } |
| 149 | |
| 150 | /* put attribute into the parent's attributes list */ |
| 151 | if (parent->attr) { |
| 152 | /* go to the end of the list */ |
| 153 | for (a = parent->attr; a->next; a = a->next); |
| 154 | /* and append new attribute */ |
| 155 | a->next = result; |
| 156 | } else { |
| 157 | /* add the first attribute in the list */ |
| 158 | parent->attr = result; |
| 159 | } |
| 160 | |
| 161 | return result; |
| 162 | } |
| 163 | |
Michal Vasko | f748dbc | 2016-04-05 11:27:47 +0200 | [diff] [blame] | 164 | void |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 165 | lyxml_correct_elem_ns(struct ly_ctx *ctx, struct lyxml_elem *elem, int copy_ns, int correct_attrs) |
| 166 | { |
| 167 | const struct lyxml_ns *tmp_ns; |
Radek Krejci | d5be568 | 2016-01-14 16:23:22 +0100 | [diff] [blame] | 168 | struct lyxml_elem *elem_root, *ns_root, *tmp, *iter; |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 169 | struct lyxml_attr *attr; |
| 170 | |
| 171 | /* find the root of elem */ |
| 172 | for (elem_root = elem; elem_root->parent; elem_root = elem_root->parent); |
| 173 | |
Radek Krejci | d5be568 | 2016-01-14 16:23:22 +0100 | [diff] [blame] | 174 | LY_TREE_DFS_BEGIN(elem, tmp, iter) { |
| 175 | if (iter->ns) { |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 176 | /* find the root of elem NS */ |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 177 | for (ns_root = iter->ns->parent; ns_root; ns_root = ns_root->parent); |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 178 | |
| 179 | /* elem NS is defined outside elem subtree */ |
| 180 | if (ns_root != elem_root) { |
| 181 | if (copy_ns) { |
Radek Krejci | d5be568 | 2016-01-14 16:23:22 +0100 | [diff] [blame] | 182 | tmp_ns = iter->ns; |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 183 | /* we may have already copied the NS over? */ |
Radek Krejci | d5be568 | 2016-01-14 16:23:22 +0100 | [diff] [blame] | 184 | iter->ns = lyxml_get_ns(iter, tmp_ns->prefix); |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 185 | |
| 186 | /* we haven't copied it over, copy it now */ |
Radek Krejci | d5be568 | 2016-01-14 16:23:22 +0100 | [diff] [blame] | 187 | if (!iter->ns) { |
| 188 | iter->ns = (struct lyxml_ns *)lyxml_dup_attr(ctx, iter, (struct lyxml_attr *)tmp_ns); |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 189 | } |
| 190 | } else { |
Radek Krejci | d5be568 | 2016-01-14 16:23:22 +0100 | [diff] [blame] | 191 | iter->ns = NULL; |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 192 | } |
| 193 | } |
| 194 | } |
| 195 | if (correct_attrs) { |
Radek Krejci | d5be568 | 2016-01-14 16:23:22 +0100 | [diff] [blame] | 196 | LY_TREE_FOR(iter->attr, attr) { |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 197 | lyxml_correct_attr_ns(ctx, attr, elem_root, copy_ns); |
| 198 | } |
| 199 | } |
Radek Krejci | d5be568 | 2016-01-14 16:23:22 +0100 | [diff] [blame] | 200 | LY_TREE_DFS_END(elem, tmp, iter); |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 201 | } |
| 202 | } |
| 203 | |
Michal Vasko | f8879c2 | 2015-08-21 09:07:36 +0200 | [diff] [blame] | 204 | struct lyxml_elem * |
| 205 | lyxml_dup_elem(struct ly_ctx *ctx, struct lyxml_elem *elem, struct lyxml_elem *parent, int recursive) |
| 206 | { |
| 207 | struct lyxml_elem *result, *child; |
| 208 | struct lyxml_attr *attr; |
| 209 | |
| 210 | if (!elem) { |
| 211 | return NULL; |
| 212 | } |
| 213 | |
| 214 | result = calloc(1, sizeof *result); |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 215 | if (!result) { |
| 216 | LOGMEM; |
| 217 | return NULL; |
| 218 | } |
Michal Vasko | f8879c2 | 2015-08-21 09:07:36 +0200 | [diff] [blame] | 219 | result->content = lydict_insert(ctx, elem->content, 0); |
| 220 | result->name = lydict_insert(ctx, elem->name, 0); |
| 221 | result->flags = elem->flags; |
Michal Vasko | f8879c2 | 2015-08-21 09:07:36 +0200 | [diff] [blame] | 222 | result->prev = result; |
| 223 | |
| 224 | if (parent) { |
| 225 | lyxml_add_child(ctx, parent, result); |
| 226 | } |
| 227 | |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 228 | /* keep old namespace for now */ |
| 229 | result->ns = elem->ns; |
| 230 | |
| 231 | /* correct namespaces */ |
| 232 | lyxml_correct_elem_ns(ctx, result, 1, 0); |
Michal Vasko | f8879c2 | 2015-08-21 09:07:36 +0200 | [diff] [blame] | 233 | |
| 234 | /* duplicate attributes */ |
| 235 | for (attr = elem->attr; attr; attr = attr->next) { |
| 236 | lyxml_dup_attr(ctx, result, attr); |
| 237 | } |
| 238 | |
| 239 | if (!recursive) { |
| 240 | return result; |
| 241 | } |
| 242 | |
| 243 | /* duplicate children */ |
| 244 | LY_TREE_FOR(elem->child, child) { |
| 245 | lyxml_dup_elem(ctx, child, result, 1); |
| 246 | } |
| 247 | |
| 248 | return result; |
| 249 | } |
| 250 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 251 | void |
Michal Vasko | f8879c2 | 2015-08-21 09:07:36 +0200 | [diff] [blame] | 252 | lyxml_unlink_elem(struct ly_ctx *ctx, struct lyxml_elem *elem, int copy_ns) |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 253 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 254 | struct lyxml_elem *parent, *first; |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 255 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 256 | if (!elem) { |
| 257 | return; |
| 258 | } |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 259 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 260 | /* store pointers to important nodes */ |
| 261 | parent = elem->parent; |
Radek Krejci | e1f1391 | 2015-05-26 15:17:38 +0200 | [diff] [blame] | 262 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 263 | /* unlink from parent */ |
| 264 | if (parent) { |
| 265 | if (parent->child == elem) { |
| 266 | /* we unlink the first child */ |
| 267 | /* update the parent's link */ |
| 268 | parent->child = elem->next; |
| 269 | } |
| 270 | /* forget about the parent */ |
| 271 | elem->parent = NULL; |
| 272 | } |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 273 | |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 274 | if (copy_ns < 2) { |
| 275 | lyxml_correct_elem_ns(ctx, elem, copy_ns, 1); |
| 276 | } |
| 277 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 278 | /* unlink from siblings */ |
| 279 | if (elem->prev == elem) { |
| 280 | /* there are no more siblings */ |
| 281 | return; |
| 282 | } |
| 283 | if (elem->next) { |
| 284 | elem->next->prev = elem->prev; |
| 285 | } else { |
| 286 | /* unlinking the last element */ |
| 287 | if (parent) { |
| 288 | first = parent->child; |
| 289 | } else { |
| 290 | first = elem; |
Radek Krejci | e4fffcf | 2016-02-23 16:06:25 +0100 | [diff] [blame] | 291 | while (first->prev->next) { |
| 292 | first = first->prev; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 293 | } |
| 294 | } |
| 295 | first->prev = elem->prev; |
| 296 | } |
| 297 | if (elem->prev->next) { |
| 298 | elem->prev->next = elem->next; |
| 299 | } |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 300 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 301 | /* clean up the unlinked element */ |
| 302 | elem->next = NULL; |
| 303 | elem->prev = elem; |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 304 | } |
| 305 | |
Michal Vasko | 345da0a | 2015-12-02 10:35:55 +0100 | [diff] [blame] | 306 | API void |
| 307 | lyxml_unlink(struct ly_ctx *ctx, struct lyxml_elem *elem) |
| 308 | { |
| 309 | if (!elem) { |
| 310 | return; |
| 311 | } |
| 312 | |
| 313 | lyxml_unlink_elem(ctx, elem, 1); |
| 314 | } |
| 315 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 316 | void |
Radek Krejci | 00249f2 | 2015-07-07 13:43:28 +0200 | [diff] [blame] | 317 | lyxml_free_attr(struct ly_ctx *ctx, struct lyxml_elem *parent, struct lyxml_attr *attr) |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 318 | { |
Radek Krejci | 00249f2 | 2015-07-07 13:43:28 +0200 | [diff] [blame] | 319 | struct lyxml_attr *aiter, *aprev; |
| 320 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 321 | if (!attr) { |
| 322 | return; |
| 323 | } |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 324 | |
Radek Krejci | 00249f2 | 2015-07-07 13:43:28 +0200 | [diff] [blame] | 325 | if (parent) { |
| 326 | /* unlink attribute from the parent's list of attributes */ |
| 327 | aprev = NULL; |
| 328 | for (aiter = parent->attr; aiter; aiter = aiter->next) { |
| 329 | if (aiter == attr) { |
| 330 | break; |
| 331 | } |
| 332 | aprev = aiter; |
| 333 | } |
| 334 | if (!aiter) { |
| 335 | /* attribute to remove not found */ |
| 336 | return; |
| 337 | } |
| 338 | |
| 339 | if (!aprev) { |
| 340 | /* attribute is first in parent's list of attributes */ |
| 341 | parent->attr = attr->next; |
| 342 | } else { |
| 343 | /* reconnect previous attribute to the next */ |
| 344 | aprev->next = attr->next; |
| 345 | } |
| 346 | } |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 347 | lydict_remove(ctx, attr->name); |
| 348 | lydict_remove(ctx, attr->value); |
| 349 | free(attr); |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 350 | } |
| 351 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 352 | void |
| 353 | lyxml_free_attrs(struct ly_ctx *ctx, struct lyxml_elem *elem) |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 354 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 355 | struct lyxml_attr *a, *next; |
| 356 | if (!elem || !elem->attr) { |
| 357 | return; |
| 358 | } |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 359 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 360 | a = elem->attr; |
| 361 | do { |
| 362 | next = a->next; |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 363 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 364 | lydict_remove(ctx, a->name); |
| 365 | lydict_remove(ctx, a->value); |
| 366 | free(a); |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 367 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 368 | a = next; |
| 369 | } while (a); |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 370 | } |
| 371 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 372 | static void |
Michal Vasko | 272e42f | 2015-12-02 12:20:37 +0100 | [diff] [blame] | 373 | lyxml_free_elem(struct ly_ctx *ctx, struct lyxml_elem *elem) |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 374 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 375 | struct lyxml_elem *e, *next; |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 376 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 377 | if (!elem) { |
| 378 | return; |
| 379 | } |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 380 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 381 | lyxml_free_attrs(ctx, elem); |
| 382 | LY_TREE_FOR_SAFE(elem->child, next, e) { |
Michal Vasko | 272e42f | 2015-12-02 12:20:37 +0100 | [diff] [blame] | 383 | lyxml_free_elem(ctx, e); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 384 | } |
| 385 | lydict_remove(ctx, elem->name); |
| 386 | lydict_remove(ctx, elem->content); |
| 387 | free(elem); |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 388 | } |
| 389 | |
Radek Krejci | c6704c8 | 2015-10-06 11:12:45 +0200 | [diff] [blame] | 390 | API void |
Michal Vasko | 345da0a | 2015-12-02 10:35:55 +0100 | [diff] [blame] | 391 | lyxml_free(struct ly_ctx *ctx, struct lyxml_elem *elem) |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 392 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 393 | if (!elem) { |
| 394 | return; |
| 395 | } |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 396 | |
Michal Vasko | 61f7ccb | 2015-10-23 10:15:08 +0200 | [diff] [blame] | 397 | lyxml_unlink_elem(ctx, elem, 2); |
Michal Vasko | 272e42f | 2015-12-02 12:20:37 +0100 | [diff] [blame] | 398 | lyxml_free_elem(ctx, elem); |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 399 | } |
| 400 | |
Radek Krejci | 8f8db23 | 2016-05-23 16:48:21 +0200 | [diff] [blame] | 401 | API void |
| 402 | lyxml_free_withsiblings(struct ly_ctx *ctx, struct lyxml_elem *elem) |
| 403 | { |
| 404 | struct lyxml_elem *iter, *aux; |
| 405 | |
| 406 | if (!elem) { |
| 407 | return; |
| 408 | } |
| 409 | |
| 410 | /* optimization - avoid freeing (unlinking) the last node of the siblings list */ |
| 411 | /* so, first, free the node's predecessors to the beginning of the list ... */ |
| 412 | for(iter = elem->prev; iter->next; iter = aux) { |
| 413 | aux = iter->prev; |
| 414 | lyxml_free(ctx, iter); |
| 415 | } |
| 416 | /* ... then, the node is the first in the siblings list, so free them all */ |
| 417 | LY_TREE_FOR_SAFE(elem, aux, iter) { |
| 418 | lyxml_free(ctx, iter); |
| 419 | } |
| 420 | } |
| 421 | |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 422 | API const char * |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 423 | lyxml_get_attr(const struct lyxml_elem *elem, const char *name, const char *ns) |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 424 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 425 | struct lyxml_attr *a; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 426 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 427 | assert(elem); |
| 428 | assert(name); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 429 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 430 | for (a = elem->attr; a; a = a->next) { |
| 431 | if (a->type != LYXML_ATTR_STD) { |
| 432 | continue; |
| 433 | } |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 434 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 435 | if (!strcmp(name, a->name)) { |
| 436 | if ((!ns && !a->ns) || (ns && a->ns && !strcmp(ns, a->ns->value))) { |
| 437 | return a->value; |
| 438 | } |
| 439 | } |
| 440 | } |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 441 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 442 | return NULL; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 443 | } |
| 444 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 445 | int |
Michal Vasko | f8879c2 | 2015-08-21 09:07:36 +0200 | [diff] [blame] | 446 | lyxml_add_child(struct ly_ctx *ctx, struct lyxml_elem *parent, struct lyxml_elem *elem) |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 447 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 448 | struct lyxml_elem *e; |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 449 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 450 | assert(parent); |
| 451 | assert(elem); |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 452 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 453 | /* (re)link element to parent */ |
| 454 | if (elem->parent) { |
Michal Vasko | f8879c2 | 2015-08-21 09:07:36 +0200 | [diff] [blame] | 455 | lyxml_unlink_elem(ctx, elem, 1); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 456 | } |
| 457 | elem->parent = parent; |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 458 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 459 | /* link parent to element */ |
| 460 | if (parent->child) { |
| 461 | e = parent->child; |
| 462 | elem->prev = e->prev; |
| 463 | elem->next = NULL; |
| 464 | elem->prev->next = elem; |
| 465 | e->prev = elem; |
| 466 | } else { |
| 467 | parent->child = elem; |
| 468 | elem->prev = elem; |
| 469 | elem->next = NULL; |
| 470 | } |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 471 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 472 | return EXIT_SUCCESS; |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 473 | } |
| 474 | |
Michal Vasko | 3b85572 | 2015-08-28 16:01:18 +0200 | [diff] [blame] | 475 | int |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 476 | lyxml_getutf8(const char *buf, unsigned int *read) |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 477 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 478 | int c, aux; |
| 479 | int i; |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 480 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 481 | c = buf[0]; |
| 482 | *read = 0; |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 483 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 484 | /* buf is NULL terminated string, so 0 means EOF */ |
| 485 | if (!c) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 486 | LOGVAL(LYE_EOF, LY_VLOG_NONE, NULL); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 487 | return 0; |
| 488 | } |
| 489 | *read = 1; |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 490 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 491 | /* process character byte(s) */ |
| 492 | if ((c & 0xf8) == 0xf0) { |
| 493 | /* four bytes character */ |
| 494 | *read = 4; |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 495 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 496 | c &= 0x07; |
| 497 | for (i = 1; i <= 3; i++) { |
| 498 | aux = buf[i]; |
| 499 | if ((aux & 0xc0) != 0x80) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 500 | LOGVAL(LYE_XML_INVAL, LY_VLOG_NONE, NULL, "input character"); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 501 | return 0; |
| 502 | } |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 503 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 504 | c = (c << 6) | (aux & 0x3f); |
| 505 | } |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 506 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 507 | if (c < 0x1000 || c > 0x10ffff) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 508 | LOGVAL(LYE_XML_INVAL, LY_VLOG_NONE, NULL, "input character"); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 509 | return 0; |
| 510 | } |
| 511 | } else if ((c & 0xf0) == 0xe0) { |
| 512 | /* three bytes character */ |
| 513 | *read = 3; |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 514 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 515 | c &= 0x0f; |
| 516 | for (i = 1; i <= 2; i++) { |
| 517 | aux = buf[i]; |
| 518 | if ((aux & 0xc0) != 0x80) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 519 | LOGVAL(LYE_XML_INVAL, LY_VLOG_NONE, NULL, "input character"); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 520 | return 0; |
| 521 | } |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 522 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 523 | c = (c << 6) | (aux & 0x3f); |
| 524 | } |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 525 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 526 | if (c < 0x800 || (c > 0xd7ff && c < 0xe000) || c > 0xfffd) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 527 | LOGVAL(LYE_XML_INVAL, LY_VLOG_NONE, NULL, "input character"); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 528 | return 0; |
| 529 | } |
| 530 | } else if ((c & 0xe0) == 0xc0) { |
| 531 | /* two bytes character */ |
| 532 | *read = 2; |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 533 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 534 | aux = buf[1]; |
| 535 | if ((aux & 0xc0) != 0x80) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 536 | LOGVAL(LYE_XML_INVAL, LY_VLOG_NONE, NULL, "input character"); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 537 | return 0; |
| 538 | } |
| 539 | c = ((c & 0x1f) << 6) | (aux & 0x3f); |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 540 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 541 | if (c < 0x80) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 542 | LOGVAL(LYE_XML_INVAL, LY_VLOG_NONE, NULL, "input character"); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 543 | return 0; |
| 544 | } |
| 545 | } else if (!(c & 0x80)) { |
| 546 | /* one byte character */ |
| 547 | if (c < 0x20 && c != 0x9 && c != 0xa && c != 0xd) { |
| 548 | /* invalid character */ |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 549 | LOGVAL(LYE_XML_INVAL, LY_VLOG_NONE, NULL, "input character"); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 550 | return 0; |
| 551 | } |
| 552 | } else { |
| 553 | /* invalid character */ |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 554 | LOGVAL(LYE_XML_INVAL, LY_VLOG_NONE, NULL, "input character"); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 555 | return 0; |
| 556 | } |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 557 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 558 | return c; |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 559 | } |
| 560 | |
Michal Vasko | 0d343d1 | 2015-08-24 14:57:36 +0200 | [diff] [blame] | 561 | /* logs directly */ |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 562 | static int |
| 563 | parse_ignore(const char *data, const char *endstr, unsigned int *len) |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 564 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 565 | unsigned int slen; |
| 566 | const char *c = data; |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 567 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 568 | slen = strlen(endstr); |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 569 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 570 | while (*c && memcmp(c, endstr, slen)) { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 571 | c++; |
| 572 | } |
| 573 | if (!*c) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 574 | LOGVAL(LYE_XML_MISS, LY_VLOG_NONE, NULL, "closing sequence", endstr); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 575 | return EXIT_FAILURE; |
| 576 | } |
| 577 | c += slen; |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 578 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 579 | *len = c - data; |
| 580 | return EXIT_SUCCESS; |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 581 | } |
| 582 | |
Michal Vasko | 0d343d1 | 2015-08-24 14:57:36 +0200 | [diff] [blame] | 583 | /* logs directly */ |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 584 | static char * |
| 585 | parse_text(const char *data, char delim, unsigned int *len) |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 586 | { |
Radek Krejci | 709fee6 | 2015-04-15 13:56:19 +0200 | [diff] [blame] | 587 | #define BUFSIZE 1024 |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 588 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 589 | char buf[BUFSIZE]; |
| 590 | char *result = NULL, *aux; |
| 591 | unsigned int r; |
| 592 | int o, size = 0; |
| 593 | int cdsect = 0; |
| 594 | int32_t n; |
Radek Krejci | 709fee6 | 2015-04-15 13:56:19 +0200 | [diff] [blame] | 595 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 596 | for (*len = o = 0; cdsect || data[*len] != delim; o++) { |
Radek Krejci | 62ea46b | 2016-06-16 11:18:52 +0200 | [diff] [blame] | 597 | if (!data[*len] || (!cdsect && !memcmp(&data[*len], "]]>", 3))) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 598 | LOGVAL(LYE_XML_INVAL, LY_VLOG_NONE, NULL, "element content, \"]]>\" found"); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 599 | goto error; |
| 600 | } |
Radek Krejci | 709fee6 | 2015-04-15 13:56:19 +0200 | [diff] [blame] | 601 | |
Radek Krejci | a4a8406 | 2015-04-16 13:00:10 +0200 | [diff] [blame] | 602 | loop: |
| 603 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 604 | if (o > BUFSIZE - 3) { |
| 605 | /* add buffer into the result */ |
| 606 | if (result) { |
| 607 | size = size + o; |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 608 | aux = ly_realloc(result, size + 1); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 609 | result = aux; |
| 610 | } else { |
| 611 | size = o; |
| 612 | result = malloc((size + 1) * sizeof *result); |
| 613 | } |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 614 | if (!result) { |
| 615 | LOGMEM; |
| 616 | return NULL; |
| 617 | } |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 618 | memcpy(&result[size - o], buf, o); |
Radek Krejci | 709fee6 | 2015-04-15 13:56:19 +0200 | [diff] [blame] | 619 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 620 | /* write again into the beginning of the buffer */ |
| 621 | o = 0; |
| 622 | } |
Radek Krejci | 709fee6 | 2015-04-15 13:56:19 +0200 | [diff] [blame] | 623 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 624 | if (cdsect || !memcmp(&data[*len], "<![CDATA[", 9)) { |
| 625 | /* CDSect */ |
| 626 | if (!cdsect) { |
| 627 | cdsect = 1; |
| 628 | *len += 9; |
| 629 | } |
| 630 | if (data[*len] && !memcmp(&data[*len], "]]>", 3)) { |
| 631 | *len += 3; |
| 632 | cdsect = 0; |
| 633 | o--; /* we don't write any data in this iteration */ |
| 634 | } else { |
| 635 | buf[o] = data[*len]; |
| 636 | (*len)++; |
| 637 | } |
| 638 | } else if (data[*len] == '&') { |
| 639 | (*len)++; |
| 640 | if (data[*len] != '#') { |
| 641 | /* entity reference - only predefined refs are supported */ |
| 642 | if (!memcmp(&data[*len], "lt;", 3)) { |
| 643 | buf[o] = '<'; |
| 644 | *len += 3; |
| 645 | } else if (!memcmp(&data[*len], "gt;", 3)) { |
| 646 | buf[o] = '>'; |
| 647 | *len += 3; |
| 648 | } else if (!memcmp(&data[*len], "amp;", 4)) { |
| 649 | buf[o] = '&'; |
| 650 | *len += 4; |
| 651 | } else if (!memcmp(&data[*len], "apos;", 5)) { |
| 652 | buf[o] = '\''; |
| 653 | *len += 5; |
| 654 | } else if (!memcmp(&data[*len], "quot;", 5)) { |
| 655 | buf[o] = '\"'; |
| 656 | *len += 5; |
| 657 | } else { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 658 | LOGVAL(LYE_XML_INVAL, LY_VLOG_NONE, NULL, "entity reference (only predefined references are supported)"); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 659 | goto error; |
| 660 | } |
| 661 | } else { |
| 662 | /* character reference */ |
| 663 | (*len)++; |
| 664 | if (isdigit(data[*len])) { |
| 665 | for (n = 0; isdigit(data[*len]); (*len)++) { |
| 666 | n = (10 * n) + (data[*len] - '0'); |
| 667 | } |
| 668 | if (data[*len] != ';') { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 669 | LOGVAL(LYE_XML_INVAL, LY_VLOG_NONE, NULL, "character reference, missing semicolon"); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 670 | goto error; |
| 671 | } |
| 672 | } else if (data[(*len)++] == 'x' && isxdigit(data[*len])) { |
| 673 | for (n = 0; isxdigit(data[*len]); (*len)++) { |
| 674 | if (isdigit(data[*len])) { |
| 675 | r = (data[*len] - '0'); |
| 676 | } else if (data[*len] > 'F') { |
| 677 | r = 10 + (data[*len] - 'a'); |
| 678 | } else { |
| 679 | r = 10 + (data[*len] - 'A'); |
| 680 | } |
| 681 | n = (16 * n) + r; |
| 682 | } |
| 683 | } else { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 684 | LOGVAL(LYE_XML_INVAL, LY_VLOG_NONE, NULL, "character reference"); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 685 | goto error; |
Radek Krejci | 709fee6 | 2015-04-15 13:56:19 +0200 | [diff] [blame] | 686 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 687 | } |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 688 | r = pututf8(&buf[o], n); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 689 | if (!r) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 690 | LOGVAL(LYE_XML_INVAL, LY_VLOG_NONE, NULL, "character reference value"); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 691 | goto error; |
| 692 | } |
| 693 | o += r - 1; /* o is ++ in for loop */ |
| 694 | (*len)++; |
| 695 | } |
| 696 | } else { |
| 697 | buf[o] = data[*len]; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 698 | (*len)++; |
| 699 | } |
| 700 | } |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 701 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 702 | if (delim == '<' && !memcmp(&data[*len], "<![CDATA[", 9)) { |
| 703 | /* ignore loop's end condition on beginning of CDSect */ |
| 704 | goto loop; |
| 705 | } |
Radek Krejci | 709fee6 | 2015-04-15 13:56:19 +0200 | [diff] [blame] | 706 | #undef BUFSIZE |
| 707 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 708 | if (o) { |
| 709 | if (result) { |
| 710 | size = size + o; |
| 711 | aux = realloc(result, size + 1); |
| 712 | result = aux; |
| 713 | } else { |
| 714 | size = o; |
| 715 | result = malloc((size + 1) * sizeof *result); |
| 716 | } |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 717 | if (!result) { |
| 718 | LOGMEM; |
| 719 | return NULL; |
| 720 | } |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 721 | memcpy(&result[size - o], buf, o); |
| 722 | } |
| 723 | if (result) { |
| 724 | result[size] = '\0'; |
Radek Krejci | a526964 | 2015-07-20 19:04:11 +0200 | [diff] [blame] | 725 | } else { |
| 726 | size = 0; |
| 727 | result = strdup(""); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 728 | } |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 729 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 730 | return result; |
Radek Krejci | 709fee6 | 2015-04-15 13:56:19 +0200 | [diff] [blame] | 731 | |
| 732 | error: |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 733 | free(result); |
| 734 | return NULL; |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 735 | } |
| 736 | |
Michal Vasko | 0d343d1 | 2015-08-24 14:57:36 +0200 | [diff] [blame] | 737 | /* logs directly */ |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 738 | static struct lyxml_attr * |
Radek Krejci | 00249f2 | 2015-07-07 13:43:28 +0200 | [diff] [blame] | 739 | parse_attr(struct ly_ctx *ctx, const char *data, unsigned int *len, struct lyxml_elem *parent) |
Radek Krejci | 674e1f8 | 2015-04-21 14:12:19 +0200 | [diff] [blame] | 740 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 741 | const char *c = data, *start, *delim; |
| 742 | char prefix[32]; |
| 743 | int uc; |
Radek Krejci | 00249f2 | 2015-07-07 13:43:28 +0200 | [diff] [blame] | 744 | struct lyxml_attr *attr = NULL, *a; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 745 | unsigned int size; |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 746 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 747 | /* check if it is attribute or namespace */ |
| 748 | if (!memcmp(c, "xmlns", 5)) { |
| 749 | /* namespace */ |
| 750 | attr = calloc(1, sizeof (struct lyxml_ns)); |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 751 | if (!attr) { |
| 752 | LOGMEM; |
| 753 | return NULL; |
| 754 | } |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 755 | attr->type = LYXML_ATTR_NS; |
Radek Krejci | 00249f2 | 2015-07-07 13:43:28 +0200 | [diff] [blame] | 756 | ((struct lyxml_ns *)attr)->parent = parent; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 757 | c += 5; |
| 758 | if (*c != ':') { |
| 759 | /* default namespace, prefix will be empty */ |
| 760 | goto equal; |
| 761 | } |
| 762 | c++; /* go after ':' to the prefix value */ |
| 763 | } else { |
| 764 | /* attribute */ |
| 765 | attr = calloc(1, sizeof *attr); |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 766 | if (!attr) { |
| 767 | LOGMEM; |
| 768 | return NULL; |
| 769 | } |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 770 | attr->type = LYXML_ATTR_STD; |
| 771 | } |
Radek Krejci | 4ea0838 | 2015-04-21 09:41:40 +0200 | [diff] [blame] | 772 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 773 | /* process name part of the attribute */ |
| 774 | start = c; |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 775 | uc = lyxml_getutf8(c, &size); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 776 | if (!is_xmlnamestartchar(uc)) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 777 | LOGVAL(LYE_XML_INVAL, LY_VLOG_NONE, NULL, "NameStartChar of the attribute"); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 778 | free(attr); |
| 779 | return NULL; |
| 780 | } |
| 781 | c += size; |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 782 | uc = lyxml_getutf8(c, &size); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 783 | while (is_xmlnamechar(uc)) { |
| 784 | if (attr->type == LYXML_ATTR_STD && *c == ':') { |
| 785 | /* attribute in a namespace */ |
| 786 | start = c + 1; |
Radek Krejci | 4ea0838 | 2015-04-21 09:41:40 +0200 | [diff] [blame] | 787 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 788 | /* look for the prefix in namespaces */ |
| 789 | memcpy(prefix, data, c - data); |
| 790 | prefix[c - data] = '\0'; |
Radek Krejci | 4476d41 | 2015-07-10 15:35:01 +0200 | [diff] [blame] | 791 | attr->ns = lyxml_get_ns(parent, prefix); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 792 | } |
| 793 | c += size; |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 794 | uc = lyxml_getutf8(c, &size); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 795 | } |
Radek Krejci | 674e1f8 | 2015-04-21 14:12:19 +0200 | [diff] [blame] | 796 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 797 | /* store the name */ |
| 798 | size = c - start; |
| 799 | attr->name = lydict_insert(ctx, start, size); |
Radek Krejci | 674e1f8 | 2015-04-21 14:12:19 +0200 | [diff] [blame] | 800 | |
| 801 | equal: |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 802 | /* check Eq mark that can be surrounded by whitespaces */ |
| 803 | ign_xmlws(c); |
| 804 | if (*c != '=') { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 805 | LOGVAL(LYE_XML_INVAL, LY_VLOG_NONE, NULL, "attribute definition, \"=\" expected"); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 806 | goto error; |
| 807 | } |
| 808 | c++; |
| 809 | ign_xmlws(c); |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 810 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 811 | /* process value part of the attribute */ |
| 812 | if (!*c || (*c != '"' && *c != '\'')) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 813 | LOGVAL(LYE_XML_INVAL, LY_VLOG_NONE, NULL, "attribute value, \" or \' expected"); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 814 | goto error; |
| 815 | } |
| 816 | delim = c; |
| 817 | attr->value = lydict_insert_zc(ctx, parse_text(++c, *delim, &size)); |
| 818 | if (ly_errno) { |
| 819 | goto error; |
| 820 | } |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 821 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 822 | *len = c + size + 1 - data; /* +1 is delimiter size */ |
Radek Krejci | 00249f2 | 2015-07-07 13:43:28 +0200 | [diff] [blame] | 823 | |
| 824 | /* put attribute into the parent's attributes list */ |
| 825 | if (parent->attr) { |
| 826 | /* go to the end of the list */ |
| 827 | for (a = parent->attr; a->next; a = a->next); |
| 828 | /* and append new attribute */ |
| 829 | a->next = attr; |
| 830 | } else { |
| 831 | /* add the first attribute in the list */ |
| 832 | parent->attr = attr; |
| 833 | } |
| 834 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 835 | return attr; |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 836 | |
| 837 | error: |
Radek Krejci | 00249f2 | 2015-07-07 13:43:28 +0200 | [diff] [blame] | 838 | lyxml_free_attr(ctx, NULL, attr); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 839 | return NULL; |
Radek Krejci | 54ea8de | 2015-04-09 18:02:56 +0200 | [diff] [blame] | 840 | } |
| 841 | |
Michal Vasko | 0d343d1 | 2015-08-24 14:57:36 +0200 | [diff] [blame] | 842 | /* logs directly */ |
Radek Krejci | 9a5daea | 2016-03-02 16:49:40 +0100 | [diff] [blame] | 843 | struct lyxml_elem * |
| 844 | lyxml_parse_elem(struct ly_ctx *ctx, const char *data, unsigned int *len, struct lyxml_elem *parent) |
Radek Krejci | 54ea8de | 2015-04-09 18:02:56 +0200 | [diff] [blame] | 845 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 846 | const char *c = data, *start, *e; |
| 847 | const char *lws; /* leading white space for handling mixed content */ |
| 848 | int uc; |
| 849 | char *str; |
| 850 | char prefix[32] = { 0 }; |
| 851 | unsigned int prefix_len = 0; |
| 852 | struct lyxml_elem *elem = NULL, *child; |
| 853 | struct lyxml_attr *attr; |
| 854 | unsigned int size; |
| 855 | int nons_flag = 0, closed_flag = 0; |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 856 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 857 | *len = 0; |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 858 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 859 | if (*c != '<') { |
| 860 | return NULL; |
| 861 | } |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 862 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 863 | /* locate element name */ |
| 864 | c++; |
| 865 | e = c; |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 866 | |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 867 | uc = lyxml_getutf8(e, &size); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 868 | if (!is_xmlnamestartchar(uc)) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 869 | LOGVAL(LYE_XML_INVAL, LY_VLOG_NONE, NULL, "NameStartChar of the element"); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 870 | return NULL; |
| 871 | } |
| 872 | e += size; |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 873 | uc = lyxml_getutf8(e, &size); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 874 | while (is_xmlnamechar(uc)) { |
| 875 | if (*e == ':') { |
| 876 | if (prefix_len) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 877 | LOGVAL(LYE_XML_INVAL, LY_VLOG_NONE, NULL, "element name, multiple colons found"); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 878 | goto error; |
| 879 | } |
| 880 | /* element in a namespace */ |
| 881 | start = e + 1; |
Radek Krejci | 674e1f8 | 2015-04-21 14:12:19 +0200 | [diff] [blame] | 882 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 883 | /* look for the prefix in namespaces */ |
| 884 | memcpy(prefix, c, prefix_len = e - c); |
| 885 | prefix[prefix_len] = '\0'; |
| 886 | c = start; |
| 887 | } |
| 888 | e += size; |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 889 | uc = lyxml_getutf8(e, &size); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 890 | } |
| 891 | if (!*e) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 892 | LOGVAL(LYE_EOF, LY_VLOG_NONE, NULL); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 893 | return NULL; |
| 894 | } |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 895 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 896 | /* allocate element structure */ |
| 897 | elem = calloc(1, sizeof *elem); |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 898 | if (!elem) { |
| 899 | LOGMEM; |
| 900 | return NULL; |
| 901 | } |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 902 | elem->next = NULL; |
| 903 | elem->prev = elem; |
| 904 | if (parent) { |
Michal Vasko | f8879c2 | 2015-08-21 09:07:36 +0200 | [diff] [blame] | 905 | lyxml_add_child(ctx, parent, elem); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 906 | } |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 907 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 908 | /* store the name into the element structure */ |
| 909 | elem->name = lydict_insert(ctx, c, e - c); |
| 910 | c = e; |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 911 | |
| 912 | process: |
Radek Krejci | db4f078 | 2016-05-03 12:07:16 +0200 | [diff] [blame] | 913 | ly_errno = LY_SUCCESS; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 914 | ign_xmlws(c); |
| 915 | if (!memcmp("/>", c, 2)) { |
| 916 | /* we are done, it was EmptyElemTag */ |
| 917 | c += 2; |
Michal Vasko | 4491384 | 2016-04-13 14:20:41 +0200 | [diff] [blame] | 918 | elem->content = lydict_insert(ctx, "", 0); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 919 | closed_flag = 1; |
| 920 | } else if (*c == '>') { |
| 921 | /* process element content */ |
| 922 | c++; |
| 923 | lws = NULL; |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 924 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 925 | while (*c) { |
| 926 | if (!memcmp(c, "</", 2)) { |
| 927 | if (lws && !elem->child) { |
| 928 | /* leading white spaces were actually content */ |
| 929 | goto store_content; |
| 930 | } |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 931 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 932 | /* Etag */ |
| 933 | c += 2; |
| 934 | /* get name and check it */ |
| 935 | e = c; |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 936 | uc = lyxml_getutf8(e, &size); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 937 | if (!is_xmlnamestartchar(uc)) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 938 | LOGVAL(LYE_XML_INVAL, LY_VLOG_XML, elem, "NameStartChar of the element"); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 939 | goto error; |
| 940 | } |
| 941 | e += size; |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 942 | uc = lyxml_getutf8(e, &size); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 943 | while (is_xmlnamechar(uc)) { |
| 944 | if (*e == ':') { |
| 945 | /* element in a namespace */ |
| 946 | start = e + 1; |
Radek Krejci | 674e1f8 | 2015-04-21 14:12:19 +0200 | [diff] [blame] | 947 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 948 | /* look for the prefix in namespaces */ |
| 949 | if (memcmp(prefix, c, e - c)) { |
Michal Vasko | ff9336a | 2016-05-10 10:48:48 +0200 | [diff] [blame] | 950 | LOGVAL(LYE_SPEC, LY_VLOG_XML, elem, |
| 951 | "Invalid (different namespaces) opening (%s) and closing element tags.", elem->name); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 952 | goto error; |
| 953 | } |
| 954 | c = start; |
| 955 | } |
| 956 | e += size; |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 957 | uc = lyxml_getutf8(e, &size); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 958 | } |
| 959 | if (!*e) { |
Radek Krejci | 3cc1096 | 2016-04-13 15:03:27 +0200 | [diff] [blame] | 960 | LOGVAL(LYE_EOF, LY_VLOG_NONE, NULL); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 961 | goto error; |
| 962 | } |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 963 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 964 | /* check that it corresponds to opening tag */ |
| 965 | size = e - c; |
| 966 | str = malloc((size + 1) * sizeof *str); |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 967 | if (!str) { |
| 968 | LOGMEM; |
| 969 | goto error; |
| 970 | } |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 971 | memcpy(str, c, e - c); |
| 972 | str[e - c] = '\0'; |
| 973 | if (size != strlen(elem->name) || memcmp(str, elem->name, size)) { |
Michal Vasko | ff9336a | 2016-05-10 10:48:48 +0200 | [diff] [blame] | 974 | LOGVAL(LYE_SPEC, LY_VLOG_XML, elem, |
| 975 | "Invalid (mixed names) opening (%s) and closing (%s) element tags.", elem->name, str); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 976 | free(str); |
| 977 | goto error; |
| 978 | } |
| 979 | free(str); |
| 980 | c = e; |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 981 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 982 | ign_xmlws(c); |
| 983 | if (*c != '>') { |
Michal Vasko | ff9336a | 2016-05-10 10:48:48 +0200 | [diff] [blame] | 984 | LOGVAL(LYE_SPEC, LY_VLOG_XML, elem, "Data after closing element tag \"%s\".", elem->name); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 985 | goto error; |
| 986 | } |
| 987 | c++; |
Michal Vasko | e00b789 | 2016-04-14 10:12:18 +0200 | [diff] [blame] | 988 | if (!(elem->flags & LYXML_ELEM_MIXED) && !elem->content) { |
| 989 | /* there was no content, but we don't want NULL (only if mixed content) */ |
| 990 | elem->content = lydict_insert(ctx, "", 0); |
| 991 | } |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 992 | closed_flag = 1; |
| 993 | break; |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 994 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 995 | } else if (!memcmp(c, "<?", 2)) { |
| 996 | if (lws) { |
| 997 | /* leading white spaces were only formatting */ |
| 998 | lws = NULL; |
| 999 | } |
| 1000 | /* PI - ignore it */ |
| 1001 | c += 2; |
| 1002 | if (parse_ignore(c, "?>", &size)) { |
| 1003 | goto error; |
| 1004 | } |
| 1005 | c += size; |
| 1006 | } else if (!memcmp(c, "<!--", 4)) { |
| 1007 | if (lws) { |
| 1008 | /* leading white spaces were only formatting */ |
| 1009 | lws = NULL; |
| 1010 | } |
| 1011 | /* Comment - ignore it */ |
| 1012 | c += 4; |
| 1013 | if (parse_ignore(c, "-->", &size)) { |
| 1014 | goto error; |
| 1015 | } |
| 1016 | c += size; |
| 1017 | } else if (!memcmp(c, "<![CDATA[", 9)) { |
| 1018 | /* CDSect */ |
| 1019 | goto store_content; |
| 1020 | } else if (*c == '<') { |
| 1021 | if (lws) { |
| 1022 | if (elem->flags & LYXML_ELEM_MIXED) { |
| 1023 | /* we have a mixed content */ |
| 1024 | goto store_content; |
| 1025 | } else { |
| 1026 | /* leading white spaces were only formatting */ |
| 1027 | lws = NULL; |
| 1028 | } |
| 1029 | } |
| 1030 | if (elem->content) { |
| 1031 | /* we have a mixed content */ |
| 1032 | child = calloc(1, sizeof *child); |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 1033 | if (!child) { |
| 1034 | LOGMEM; |
| 1035 | goto error; |
| 1036 | } |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1037 | child->content = elem->content; |
| 1038 | elem->content = NULL; |
Michal Vasko | f8879c2 | 2015-08-21 09:07:36 +0200 | [diff] [blame] | 1039 | lyxml_add_child(ctx, elem, child); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1040 | elem->flags |= LYXML_ELEM_MIXED; |
| 1041 | } |
Radek Krejci | 9a5daea | 2016-03-02 16:49:40 +0100 | [diff] [blame] | 1042 | child = lyxml_parse_elem(ctx, c, &size, elem); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1043 | if (!child) { |
| 1044 | goto error; |
| 1045 | } |
| 1046 | c += size; /* move after processed child element */ |
| 1047 | } else if (is_xmlws(*c)) { |
| 1048 | lws = c; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1049 | ign_xmlws(c); |
| 1050 | } else { |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 1051 | store_content: |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1052 | /* store text content */ |
| 1053 | if (lws) { |
| 1054 | /* process content including the leading white spaces */ |
| 1055 | c = lws; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1056 | lws = NULL; |
| 1057 | } |
| 1058 | elem->content = lydict_insert_zc(ctx, parse_text(c, '<', &size)); |
| 1059 | if (ly_errno) { |
| 1060 | goto error; |
| 1061 | } |
| 1062 | c += size; /* move after processed text content */ |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 1063 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1064 | if (elem->child) { |
| 1065 | /* we have a mixed content */ |
| 1066 | child = calloc(1, sizeof *child); |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 1067 | if (!child) { |
| 1068 | LOGMEM; |
| 1069 | goto error; |
| 1070 | } |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1071 | child->content = elem->content; |
| 1072 | elem->content = NULL; |
Michal Vasko | f8879c2 | 2015-08-21 09:07:36 +0200 | [diff] [blame] | 1073 | lyxml_add_child(ctx, elem, child); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1074 | elem->flags |= LYXML_ELEM_MIXED; |
| 1075 | } |
| 1076 | } |
| 1077 | } |
| 1078 | } else { |
| 1079 | /* process attribute */ |
| 1080 | attr = parse_attr(ctx, c, &size, elem); |
| 1081 | if (!attr) { |
| 1082 | goto error; |
| 1083 | } |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1084 | c += size; /* move after processed attribute */ |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 1085 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1086 | /* check namespace */ |
| 1087 | if (attr->type == LYXML_ATTR_NS) { |
| 1088 | if (!prefix[0] && !attr->name) { |
| 1089 | if (attr->value) { |
| 1090 | /* default prefix */ |
| 1091 | elem->ns = (struct lyxml_ns *)attr; |
| 1092 | } else { |
| 1093 | /* xmlns="" -> no namespace */ |
| 1094 | nons_flag = 1; |
| 1095 | } |
| 1096 | } else if (prefix[0] && attr->name && !memcmp(attr->name, prefix, prefix_len + 1)) { |
| 1097 | /* matching namespace with prefix */ |
| 1098 | elem->ns = (struct lyxml_ns *)attr; |
| 1099 | } |
| 1100 | } |
Radek Krejci | 674e1f8 | 2015-04-21 14:12:19 +0200 | [diff] [blame] | 1101 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1102 | /* go back to finish element processing */ |
| 1103 | goto process; |
| 1104 | } |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 1105 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1106 | *len = c - data; |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 1107 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1108 | if (!closed_flag) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 1109 | LOGVAL(LYE_XML_MISS, LY_VLOG_XML, elem, "closing element tag", elem->name); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1110 | goto error; |
| 1111 | } |
Radek Krejci | 674e1f8 | 2015-04-21 14:12:19 +0200 | [diff] [blame] | 1112 | |
Radek Krejci | 78a230a | 2015-07-07 17:04:40 +0200 | [diff] [blame] | 1113 | if (!elem->ns && !nons_flag && parent) { |
Radek Krejci | 4476d41 | 2015-07-10 15:35:01 +0200 | [diff] [blame] | 1114 | elem->ns = lyxml_get_ns(parent, prefix_len ? prefix : NULL); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1115 | } |
Radek Krejci | 674e1f8 | 2015-04-21 14:12:19 +0200 | [diff] [blame] | 1116 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1117 | return elem; |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 1118 | |
| 1119 | error: |
Michal Vasko | 345da0a | 2015-12-02 10:35:55 +0100 | [diff] [blame] | 1120 | lyxml_free(ctx, elem); |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 1121 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1122 | return NULL; |
Radek Krejci | 54ea8de | 2015-04-09 18:02:56 +0200 | [diff] [blame] | 1123 | } |
| 1124 | |
Michal Vasko | 0d343d1 | 2015-08-24 14:57:36 +0200 | [diff] [blame] | 1125 | /* logs directly */ |
Radek Krejci | c6704c8 | 2015-10-06 11:12:45 +0200 | [diff] [blame] | 1126 | API struct lyxml_elem * |
Radek Krejci | 722b007 | 2016-02-01 17:09:45 +0100 | [diff] [blame] | 1127 | lyxml_parse_mem(struct ly_ctx *ctx, const char *data, int options) |
Radek Krejci | 54ea8de | 2015-04-09 18:02:56 +0200 | [diff] [blame] | 1128 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1129 | const char *c = data; |
| 1130 | unsigned int len; |
Radek Krejci | 851ea66 | 2016-01-08 09:30:53 +0100 | [diff] [blame] | 1131 | struct lyxml_elem *root, *first = NULL, *next; |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 1132 | |
Radek Krejci | 2342cf6 | 2016-01-29 16:48:23 +0100 | [diff] [blame] | 1133 | ly_errno = LY_SUCCESS; |
| 1134 | |
Radek Krejci | 120f624 | 2015-12-17 12:32:56 +0100 | [diff] [blame] | 1135 | repeat: |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1136 | /* process document */ |
| 1137 | while (*c) { |
| 1138 | if (is_xmlws(*c)) { |
| 1139 | /* skip whitespaces */ |
| 1140 | ign_xmlws(c); |
| 1141 | } else if (!memcmp(c, "<?", 2)) { |
| 1142 | /* XMLDecl or PI - ignore it */ |
| 1143 | c += 2; |
| 1144 | if (parse_ignore(c, "?>", &len)) { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1145 | return NULL; |
| 1146 | } |
| 1147 | c += len; |
| 1148 | } else if (!memcmp(c, "<!--", 4)) { |
| 1149 | /* Comment - ignore it */ |
| 1150 | c += 2; |
| 1151 | if (parse_ignore(c, "-->", &len)) { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1152 | return NULL; |
| 1153 | } |
| 1154 | c += len; |
| 1155 | } else if (!memcmp(c, "<!", 2)) { |
| 1156 | /* DOCTYPE */ |
| 1157 | /* TODO - standalone ignore counting < and > */ |
| 1158 | LOGERR(LY_EINVAL, "DOCTYPE not supported in XML documents."); |
| 1159 | return NULL; |
| 1160 | } else if (*c == '<') { |
| 1161 | /* element - process it in next loop to strictly follow XML |
| 1162 | * format |
| 1163 | */ |
| 1164 | break; |
Michal Vasko | c2e8056 | 2015-07-27 11:31:41 +0200 | [diff] [blame] | 1165 | } else { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 1166 | LOGVAL(LYE_XML_INCHAR, LY_VLOG_NONE, NULL, c); |
Michal Vasko | c2e8056 | 2015-07-27 11:31:41 +0200 | [diff] [blame] | 1167 | return NULL; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1168 | } |
| 1169 | } |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 1170 | |
Radek Krejci | 9a5daea | 2016-03-02 16:49:40 +0100 | [diff] [blame] | 1171 | root = lyxml_parse_elem(ctx, c, &len, NULL); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1172 | if (!root) { |
Michal Vasko | bc58b4a | 2016-01-07 14:42:31 +0100 | [diff] [blame] | 1173 | if (first) { |
Radek Krejci | 851ea66 | 2016-01-08 09:30:53 +0100 | [diff] [blame] | 1174 | LY_TREE_FOR_SAFE(first, next, root) { |
Michal Vasko | bc58b4a | 2016-01-07 14:42:31 +0100 | [diff] [blame] | 1175 | lyxml_free(ctx, root); |
| 1176 | } |
Radek Krejci | 120f624 | 2015-12-17 12:32:56 +0100 | [diff] [blame] | 1177 | } |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1178 | return NULL; |
Radek Krejci | 120f624 | 2015-12-17 12:32:56 +0100 | [diff] [blame] | 1179 | } else if (!first) { |
| 1180 | first = root; |
| 1181 | } else { |
| 1182 | first->prev->next = root; |
| 1183 | root->prev = first->prev; |
| 1184 | first->prev = root; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1185 | } |
| 1186 | c += len; |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 1187 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1188 | /* ignore the rest of document where can be comments, PIs and whitespaces, |
| 1189 | * note that we are not detecting syntax errors in these parts |
| 1190 | */ |
| 1191 | ign_xmlws(c); |
| 1192 | if (*c) { |
Radek Krejci | 722b007 | 2016-02-01 17:09:45 +0100 | [diff] [blame] | 1193 | if (options & LYXML_PARSE_MULTIROOT) { |
Radek Krejci | 120f624 | 2015-12-17 12:32:56 +0100 | [diff] [blame] | 1194 | goto repeat; |
| 1195 | } else { |
| 1196 | LOGWRN("There are some not parsed data:\n%s", c); |
| 1197 | } |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1198 | } |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 1199 | |
Radek Krejci | 120f624 | 2015-12-17 12:32:56 +0100 | [diff] [blame] | 1200 | return first; |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 1201 | } |
| 1202 | |
Radek Krejci | c6704c8 | 2015-10-06 11:12:45 +0200 | [diff] [blame] | 1203 | API struct lyxml_elem * |
Radek Krejci | 722b007 | 2016-02-01 17:09:45 +0100 | [diff] [blame] | 1204 | lyxml_parse_path(struct ly_ctx *ctx, const char *filename, int options) |
Radek Krejci | 54ea8de | 2015-04-09 18:02:56 +0200 | [diff] [blame] | 1205 | { |
Radek Krejci | 6b3d926 | 2015-12-03 13:45:27 +0100 | [diff] [blame] | 1206 | struct lyxml_elem *elem = NULL; |
Pavol Vican | b2570c1 | 2015-11-12 13:50:20 +0100 | [diff] [blame] | 1207 | struct stat sb; |
| 1208 | int fd; |
| 1209 | char *addr; |
| 1210 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1211 | if (!filename || !ctx) { |
| 1212 | LOGERR(LY_EINVAL, "%s: Invalid parameter.", __func__); |
| 1213 | return NULL; |
| 1214 | } |
Radek Krejci | 54ea8de | 2015-04-09 18:02:56 +0200 | [diff] [blame] | 1215 | |
Pavol Vican | b2570c1 | 2015-11-12 13:50:20 +0100 | [diff] [blame] | 1216 | fd = open(filename, O_RDONLY); |
| 1217 | if (fd == -1) { |
Radek Krejci | 6b3d926 | 2015-12-03 13:45:27 +0100 | [diff] [blame] | 1218 | LOGERR(LY_EINVAL,"Opening file \"%s\" failed.", filename); |
Pavol Vican | b2570c1 | 2015-11-12 13:50:20 +0100 | [diff] [blame] | 1219 | return NULL; |
| 1220 | } |
| 1221 | if (fstat(fd, &sb) == -1) { |
| 1222 | LOGERR(LY_EINVAL, "Unable to get file \"%s\" information.\n", filename); |
| 1223 | goto error; |
| 1224 | } |
| 1225 | if (!S_ISREG(sb.st_mode)) { |
Radek Krejci | b051f72 | 2016-02-25 15:12:21 +0100 | [diff] [blame] | 1226 | LOGERR(LY_EINVAL, "%s: Invalid parameter, input file is not a regular file", __func__); |
Pavol Vican | b2570c1 | 2015-11-12 13:50:20 +0100 | [diff] [blame] | 1227 | goto error; |
| 1228 | } |
Pavol Vican | f7cc285 | 2016-03-22 23:27:35 +0100 | [diff] [blame] | 1229 | addr = mmap(NULL, sb.st_size + 2, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0); |
Pavol Vican | b2570c1 | 2015-11-12 13:50:20 +0100 | [diff] [blame] | 1230 | if (addr == MAP_FAILED) { |
Radek Krejci | 6b3d926 | 2015-12-03 13:45:27 +0100 | [diff] [blame] | 1231 | LOGERR(LY_EMEM,"Map file into memory failed (%s()).", __func__); |
Pavol Vican | b2570c1 | 2015-11-12 13:50:20 +0100 | [diff] [blame] | 1232 | goto error; |
| 1233 | } |
Radek Krejci | 6b3d926 | 2015-12-03 13:45:27 +0100 | [diff] [blame] | 1234 | |
Radek Krejci | 722b007 | 2016-02-01 17:09:45 +0100 | [diff] [blame] | 1235 | elem = lyxml_parse_mem(ctx, addr, options); |
Pavol Vican | f7cc285 | 2016-03-22 23:27:35 +0100 | [diff] [blame] | 1236 | munmap(addr, sb.st_size +2); |
Radek Krejci | 30793ab | 2015-12-03 13:45:45 +0100 | [diff] [blame] | 1237 | close(fd); |
Radek Krejci | 6b3d926 | 2015-12-03 13:45:27 +0100 | [diff] [blame] | 1238 | |
Pavol Vican | b2570c1 | 2015-11-12 13:50:20 +0100 | [diff] [blame] | 1239 | return elem; |
| 1240 | |
| 1241 | error: |
Radek Krejci | 6b3d926 | 2015-12-03 13:45:27 +0100 | [diff] [blame] | 1242 | if (fd != -1) { |
| 1243 | close(fd); |
| 1244 | } |
| 1245 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1246 | return NULL; |
Radek Krejci | 54ea8de | 2015-04-09 18:02:56 +0200 | [diff] [blame] | 1247 | } |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 1248 | |
Michal Vasko | 5db027d | 2015-10-09 14:38:50 +0200 | [diff] [blame] | 1249 | int |
| 1250 | lyxml_dump_text(struct lyout *out, const char *text) |
Radek Krejci | f0023a9 | 2015-04-20 20:51:39 +0200 | [diff] [blame] | 1251 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1252 | unsigned int i, n; |
Radek Krejci | f0023a9 | 2015-04-20 20:51:39 +0200 | [diff] [blame] | 1253 | |
Michal Vasko | 5db027d | 2015-10-09 14:38:50 +0200 | [diff] [blame] | 1254 | if (!text) { |
| 1255 | return 0; |
| 1256 | } |
| 1257 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1258 | for (i = n = 0; text[i]; i++) { |
| 1259 | switch (text[i]) { |
| 1260 | case '&': |
Radek Krejci | 5248f13 | 2015-10-09 10:34:25 +0200 | [diff] [blame] | 1261 | n += ly_print(out, "&"); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1262 | break; |
| 1263 | case '<': |
Radek Krejci | 5248f13 | 2015-10-09 10:34:25 +0200 | [diff] [blame] | 1264 | n += ly_print(out, "<"); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1265 | break; |
| 1266 | case '>': |
| 1267 | /* not needed, just for readability */ |
Radek Krejci | 5248f13 | 2015-10-09 10:34:25 +0200 | [diff] [blame] | 1268 | n += ly_print(out, ">"); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1269 | break; |
Radek Krejci | 952a725 | 2016-07-16 20:52:43 +0200 | [diff] [blame^] | 1270 | case '"': |
| 1271 | n += ly_print(out, """); |
| 1272 | break; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1273 | default: |
Radek Krejci | 5248f13 | 2015-10-09 10:34:25 +0200 | [diff] [blame] | 1274 | ly_write(out, &text[i], 1); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1275 | n++; |
| 1276 | } |
| 1277 | } |
Radek Krejci | f0023a9 | 2015-04-20 20:51:39 +0200 | [diff] [blame] | 1278 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1279 | return n; |
Radek Krejci | f0023a9 | 2015-04-20 20:51:39 +0200 | [diff] [blame] | 1280 | } |
| 1281 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1282 | static int |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 1283 | dump_elem(struct lyout *out, const struct lyxml_elem *e, int level, int options) |
Radek Krejci | f0023a9 | 2015-04-20 20:51:39 +0200 | [diff] [blame] | 1284 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1285 | int size = 0; |
| 1286 | struct lyxml_attr *a; |
| 1287 | struct lyxml_elem *child; |
| 1288 | const char *delim, *delim_outer; |
| 1289 | int indent; |
Radek Krejci | f0023a9 | 2015-04-20 20:51:39 +0200 | [diff] [blame] | 1290 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1291 | if (!e->name) { |
| 1292 | /* mixed content */ |
| 1293 | if (e->content) { |
Michal Vasko | 5db027d | 2015-10-09 14:38:50 +0200 | [diff] [blame] | 1294 | return lyxml_dump_text(out, e->content); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1295 | } else { |
| 1296 | return 0; |
| 1297 | } |
| 1298 | } |
Radek Krejci | f0023a9 | 2015-04-20 20:51:39 +0200 | [diff] [blame] | 1299 | |
Radek Krejci | 722b007 | 2016-02-01 17:09:45 +0100 | [diff] [blame] | 1300 | delim = delim_outer = (options & LYXML_PRINT_FORMAT) ? "\n" : ""; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1301 | indent = 2 * level; |
| 1302 | if ((e->flags & LYXML_ELEM_MIXED) || (e->parent && (e->parent->flags & LYXML_ELEM_MIXED))) { |
| 1303 | delim = ""; |
| 1304 | } |
| 1305 | if (e->parent && (e->parent->flags & LYXML_ELEM_MIXED)) { |
| 1306 | delim_outer = ""; |
| 1307 | indent = 0; |
| 1308 | } |
Radek Krejci | f0023a9 | 2015-04-20 20:51:39 +0200 | [diff] [blame] | 1309 | |
Radek Krejci | 722b007 | 2016-02-01 17:09:45 +0100 | [diff] [blame] | 1310 | if (!(options & (LYXML_PRINT_OPEN | LYXML_PRINT_CLOSE | LYXML_PRINT_ATTRS)) || (options & LYXML_PRINT_OPEN)) { |
Radek Krejci | c6704c8 | 2015-10-06 11:12:45 +0200 | [diff] [blame] | 1311 | /* opening tag */ |
| 1312 | if (e->ns && e->ns->prefix) { |
Radek Krejci | 5248f13 | 2015-10-09 10:34:25 +0200 | [diff] [blame] | 1313 | size += ly_print(out, "%*s<%s:%s", indent, "", e->ns->prefix, e->name); |
Radek Krejci | c6704c8 | 2015-10-06 11:12:45 +0200 | [diff] [blame] | 1314 | } else { |
Radek Krejci | 5248f13 | 2015-10-09 10:34:25 +0200 | [diff] [blame] | 1315 | size += ly_print(out, "%*s<%s", indent, "", e->name); |
Radek Krejci | c6704c8 | 2015-10-06 11:12:45 +0200 | [diff] [blame] | 1316 | } |
Radek Krejci | 722b007 | 2016-02-01 17:09:45 +0100 | [diff] [blame] | 1317 | } else if (options & LYXML_PRINT_CLOSE) { |
Radek Krejci | c6704c8 | 2015-10-06 11:12:45 +0200 | [diff] [blame] | 1318 | indent = 0; |
| 1319 | goto close; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1320 | } |
Radek Krejci | 674e1f8 | 2015-04-21 14:12:19 +0200 | [diff] [blame] | 1321 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1322 | /* attributes */ |
| 1323 | for (a = e->attr; a; a = a->next) { |
| 1324 | if (a->type == LYXML_ATTR_NS) { |
| 1325 | if (a->name) { |
Radek Krejci | 5248f13 | 2015-10-09 10:34:25 +0200 | [diff] [blame] | 1326 | size += ly_print(out, " xmlns:%s=\"%s\"", a->name, a->value ? a->value : ""); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1327 | } else { |
Radek Krejci | 5248f13 | 2015-10-09 10:34:25 +0200 | [diff] [blame] | 1328 | size += ly_print(out, " xmlns=\"%s\"", a->value ? a->value : ""); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1329 | } |
| 1330 | } else if (a->ns && a->ns->prefix) { |
Radek Krejci | 5248f13 | 2015-10-09 10:34:25 +0200 | [diff] [blame] | 1331 | size += ly_print(out, " %s:%s=\"%s\"", a->ns->prefix, a->name, a->value); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1332 | } else { |
Radek Krejci | 5248f13 | 2015-10-09 10:34:25 +0200 | [diff] [blame] | 1333 | size += ly_print(out, " %s=\"%s\"", a->name, a->value); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1334 | } |
| 1335 | } |
Radek Krejci | 674e1f8 | 2015-04-21 14:12:19 +0200 | [diff] [blame] | 1336 | |
Radek Krejci | c6704c8 | 2015-10-06 11:12:45 +0200 | [diff] [blame] | 1337 | /* apply options */ |
Radek Krejci | 722b007 | 2016-02-01 17:09:45 +0100 | [diff] [blame] | 1338 | if ((options & LYXML_PRINT_CLOSE) && (options & LYXML_PRINT_OPEN)) { |
Radek Krejci | 5248f13 | 2015-10-09 10:34:25 +0200 | [diff] [blame] | 1339 | size += ly_print(out, "/>%s", delim); |
Radek Krejci | c6704c8 | 2015-10-06 11:12:45 +0200 | [diff] [blame] | 1340 | return size; |
Radek Krejci | 722b007 | 2016-02-01 17:09:45 +0100 | [diff] [blame] | 1341 | } else if (options & LYXML_PRINT_OPEN) { |
Radek Krejci | 5248f13 | 2015-10-09 10:34:25 +0200 | [diff] [blame] | 1342 | ly_print(out, ">"); |
Radek Krejci | c6704c8 | 2015-10-06 11:12:45 +0200 | [diff] [blame] | 1343 | return ++size; |
Radek Krejci | 722b007 | 2016-02-01 17:09:45 +0100 | [diff] [blame] | 1344 | } else if (options & LYXML_PRINT_ATTRS) { |
Radek Krejci | c6704c8 | 2015-10-06 11:12:45 +0200 | [diff] [blame] | 1345 | return size; |
| 1346 | } |
| 1347 | |
Michal Vasko | 3a61161 | 2016-04-14 10:12:56 +0200 | [diff] [blame] | 1348 | if (!e->child && (!e->content || !e->content[0])) { |
Radek Krejci | 5248f13 | 2015-10-09 10:34:25 +0200 | [diff] [blame] | 1349 | size += ly_print(out, "/>%s", delim); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1350 | return size; |
Michal Vasko | 3a61161 | 2016-04-14 10:12:56 +0200 | [diff] [blame] | 1351 | } else if (e->content && e->content[0]) { |
Radek Krejci | 5248f13 | 2015-10-09 10:34:25 +0200 | [diff] [blame] | 1352 | ly_print(out, ">"); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1353 | size++; |
Radek Krejci | f0023a9 | 2015-04-20 20:51:39 +0200 | [diff] [blame] | 1354 | |
Michal Vasko | 5db027d | 2015-10-09 14:38:50 +0200 | [diff] [blame] | 1355 | size += lyxml_dump_text(out, e->content); |
Radek Krejci | f0023a9 | 2015-04-20 20:51:39 +0200 | [diff] [blame] | 1356 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1357 | if (e->ns && e->ns->prefix) { |
Radek Krejci | 5248f13 | 2015-10-09 10:34:25 +0200 | [diff] [blame] | 1358 | size += ly_print(out, "</%s:%s>%s", e->ns->prefix, e->name, delim); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1359 | } else { |
Radek Krejci | 5248f13 | 2015-10-09 10:34:25 +0200 | [diff] [blame] | 1360 | size += ly_print(out, "</%s>%s", e->name, delim); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1361 | } |
| 1362 | return size; |
| 1363 | } else { |
Radek Krejci | 5248f13 | 2015-10-09 10:34:25 +0200 | [diff] [blame] | 1364 | size += ly_print(out, ">%s", delim); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1365 | } |
Radek Krejci | 674e1f8 | 2015-04-21 14:12:19 +0200 | [diff] [blame] | 1366 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1367 | /* go recursively */ |
| 1368 | LY_TREE_FOR(e->child, child) { |
Radek Krejci | 722b007 | 2016-02-01 17:09:45 +0100 | [diff] [blame] | 1369 | if (options & LYXML_PRINT_FORMAT) { |
| 1370 | size += dump_elem(out, child, level + 1, LYXML_PRINT_FORMAT); |
Pavol Vican | be7eef5 | 2015-10-22 14:07:48 +0200 | [diff] [blame] | 1371 | } else { |
| 1372 | size += dump_elem(out, child, level, 0); |
| 1373 | } |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1374 | } |
Radek Krejci | f0023a9 | 2015-04-20 20:51:39 +0200 | [diff] [blame] | 1375 | |
Radek Krejci | c6704c8 | 2015-10-06 11:12:45 +0200 | [diff] [blame] | 1376 | close: |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1377 | /* closing tag */ |
| 1378 | if (e->ns && e->ns->prefix) { |
Radek Krejci | 5248f13 | 2015-10-09 10:34:25 +0200 | [diff] [blame] | 1379 | size += ly_print(out, "%*s</%s:%s>%s", indent, "", e->ns->prefix, e->name, delim_outer); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1380 | } else { |
Radek Krejci | 5248f13 | 2015-10-09 10:34:25 +0200 | [diff] [blame] | 1381 | size += ly_print(out, "%*s</%s>%s", indent, "", e->name, delim_outer); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1382 | } |
Radek Krejci | f0023a9 | 2015-04-20 20:51:39 +0200 | [diff] [blame] | 1383 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1384 | return size; |
Radek Krejci | f0023a9 | 2015-04-20 20:51:39 +0200 | [diff] [blame] | 1385 | } |
| 1386 | |
Radek Krejci | 8c56a5a | 2015-12-16 15:10:28 +0100 | [diff] [blame] | 1387 | static int |
| 1388 | dump_siblings(struct lyout *out, const struct lyxml_elem *e, int options) |
| 1389 | { |
| 1390 | const struct lyxml_elem *start, *iter; |
| 1391 | int ret = 0; |
| 1392 | |
| 1393 | if (e->parent) { |
| 1394 | start = e->parent->child; |
| 1395 | } else { |
| 1396 | start = e; |
| 1397 | while(start->prev && start->prev->next) { |
| 1398 | start = start->prev; |
| 1399 | } |
| 1400 | } |
| 1401 | |
| 1402 | LY_TREE_FOR(start, iter) { |
| 1403 | ret += dump_elem(out, iter, 0, options); |
| 1404 | } |
| 1405 | |
| 1406 | return ret; |
| 1407 | } |
| 1408 | |
Radek Krejci | c6704c8 | 2015-10-06 11:12:45 +0200 | [diff] [blame] | 1409 | API int |
Radek Krejci | 722b007 | 2016-02-01 17:09:45 +0100 | [diff] [blame] | 1410 | lyxml_print_file(FILE *stream, const struct lyxml_elem *elem, int options) |
Radek Krejci | f0023a9 | 2015-04-20 20:51:39 +0200 | [diff] [blame] | 1411 | { |
Radek Krejci | 5248f13 | 2015-10-09 10:34:25 +0200 | [diff] [blame] | 1412 | struct lyout out; |
| 1413 | |
| 1414 | if (!stream || !elem) { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1415 | return 0; |
| 1416 | } |
Radek Krejci | f0023a9 | 2015-04-20 20:51:39 +0200 | [diff] [blame] | 1417 | |
Radek Krejci | 5248f13 | 2015-10-09 10:34:25 +0200 | [diff] [blame] | 1418 | out.type = LYOUT_STREAM; |
| 1419 | out.method.f = stream; |
| 1420 | |
Radek Krejci | 722b007 | 2016-02-01 17:09:45 +0100 | [diff] [blame] | 1421 | if (options & LYXML_PRINT_SIBLINGS) { |
Radek Krejci | 8c56a5a | 2015-12-16 15:10:28 +0100 | [diff] [blame] | 1422 | return dump_siblings(&out, elem, options); |
| 1423 | } else { |
| 1424 | return dump_elem(&out, elem, 0, options); |
| 1425 | } |
Radek Krejci | 5248f13 | 2015-10-09 10:34:25 +0200 | [diff] [blame] | 1426 | } |
| 1427 | |
| 1428 | API int |
Radek Krejci | 722b007 | 2016-02-01 17:09:45 +0100 | [diff] [blame] | 1429 | lyxml_print_fd(int fd, const struct lyxml_elem *elem, int options) |
Radek Krejci | 5248f13 | 2015-10-09 10:34:25 +0200 | [diff] [blame] | 1430 | { |
| 1431 | struct lyout out; |
| 1432 | |
| 1433 | if (fd < 0 || !elem) { |
| 1434 | return 0; |
| 1435 | } |
| 1436 | |
| 1437 | out.type = LYOUT_FD; |
| 1438 | out.method.fd = fd; |
| 1439 | |
Radek Krejci | 722b007 | 2016-02-01 17:09:45 +0100 | [diff] [blame] | 1440 | if (options & LYXML_PRINT_SIBLINGS) { |
Radek Krejci | 8c56a5a | 2015-12-16 15:10:28 +0100 | [diff] [blame] | 1441 | return dump_siblings(&out, elem, options); |
| 1442 | } else { |
| 1443 | return dump_elem(&out, elem, 0, options); |
| 1444 | } |
Radek Krejci | f0023a9 | 2015-04-20 20:51:39 +0200 | [diff] [blame] | 1445 | } |
Radek Krejci | 6140e4e | 2015-10-09 15:50:55 +0200 | [diff] [blame] | 1446 | |
| 1447 | API int |
Radek Krejci | 722b007 | 2016-02-01 17:09:45 +0100 | [diff] [blame] | 1448 | lyxml_print_mem(char **strp, const struct lyxml_elem *elem, int options) |
Radek Krejci | 2fa0fc1 | 2015-10-14 18:14:29 +0200 | [diff] [blame] | 1449 | { |
| 1450 | struct lyout out; |
| 1451 | int r; |
| 1452 | |
| 1453 | if (!strp || !elem) { |
| 1454 | return 0; |
| 1455 | } |
| 1456 | |
| 1457 | out.type = LYOUT_MEMORY; |
| 1458 | out.method.mem.buf = NULL; |
| 1459 | out.method.mem.len = 0; |
| 1460 | out.method.mem.size = 0; |
| 1461 | |
Radek Krejci | 722b007 | 2016-02-01 17:09:45 +0100 | [diff] [blame] | 1462 | if (options & LYXML_PRINT_SIBLINGS) { |
Radek Krejci | 8c56a5a | 2015-12-16 15:10:28 +0100 | [diff] [blame] | 1463 | r = dump_siblings(&out, elem, options); |
| 1464 | } else { |
| 1465 | r = dump_elem(&out, elem, 0, options); |
| 1466 | } |
Radek Krejci | 2fa0fc1 | 2015-10-14 18:14:29 +0200 | [diff] [blame] | 1467 | |
| 1468 | *strp = out.method.mem.buf; |
| 1469 | return r; |
| 1470 | } |
| 1471 | |
| 1472 | API int |
Radek Krejci | 722b007 | 2016-02-01 17:09:45 +0100 | [diff] [blame] | 1473 | lyxml_print_clb(ssize_t (*writeclb)(void *arg, const void *buf, size_t count), void *arg, const struct lyxml_elem *elem, int options) |
Radek Krejci | 6140e4e | 2015-10-09 15:50:55 +0200 | [diff] [blame] | 1474 | { |
| 1475 | struct lyout out; |
| 1476 | |
| 1477 | if (!writeclb || !elem) { |
| 1478 | return 0; |
| 1479 | } |
| 1480 | |
| 1481 | out.type = LYOUT_CALLBACK; |
Radek Krejci | 50929eb | 2015-10-09 18:14:15 +0200 | [diff] [blame] | 1482 | out.method.clb.f = writeclb; |
| 1483 | out.method.clb.arg = arg; |
Radek Krejci | 6140e4e | 2015-10-09 15:50:55 +0200 | [diff] [blame] | 1484 | |
Radek Krejci | 722b007 | 2016-02-01 17:09:45 +0100 | [diff] [blame] | 1485 | if (options & LYXML_PRINT_SIBLINGS) { |
Radek Krejci | 8c56a5a | 2015-12-16 15:10:28 +0100 | [diff] [blame] | 1486 | return dump_siblings(&out, elem, options); |
| 1487 | } else { |
| 1488 | return dump_elem(&out, elem, 0, options); |
| 1489 | } |
Radek Krejci | 6140e4e | 2015-10-09 15:50:55 +0200 | [diff] [blame] | 1490 | } |