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 { |
Radek Krejci | deee60e | 2016-09-23 15:21:14 +0200 | [diff] [blame] | 697 | r = copyutf8(&buf[o], &data[*len]); |
| 698 | if (!r) { |
| 699 | goto error; |
| 700 | } |
| 701 | |
| 702 | o += r - 1; /* o is ++ in for loop */ |
| 703 | (*len) = (*len) + r; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 704 | } |
| 705 | } |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 706 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 707 | if (delim == '<' && !memcmp(&data[*len], "<![CDATA[", 9)) { |
| 708 | /* ignore loop's end condition on beginning of CDSect */ |
| 709 | goto loop; |
| 710 | } |
Radek Krejci | 709fee6 | 2015-04-15 13:56:19 +0200 | [diff] [blame] | 711 | #undef BUFSIZE |
| 712 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 713 | if (o) { |
| 714 | if (result) { |
| 715 | size = size + o; |
| 716 | aux = realloc(result, size + 1); |
| 717 | result = aux; |
| 718 | } else { |
| 719 | size = o; |
| 720 | result = malloc((size + 1) * sizeof *result); |
| 721 | } |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 722 | if (!result) { |
| 723 | LOGMEM; |
| 724 | return NULL; |
| 725 | } |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 726 | memcpy(&result[size - o], buf, o); |
| 727 | } |
| 728 | if (result) { |
| 729 | result[size] = '\0'; |
Radek Krejci | a526964 | 2015-07-20 19:04:11 +0200 | [diff] [blame] | 730 | } else { |
| 731 | size = 0; |
| 732 | result = strdup(""); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 733 | } |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 734 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 735 | return result; |
Radek Krejci | 709fee6 | 2015-04-15 13:56:19 +0200 | [diff] [blame] | 736 | |
| 737 | error: |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 738 | free(result); |
| 739 | return NULL; |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 740 | } |
| 741 | |
Michal Vasko | 0d343d1 | 2015-08-24 14:57:36 +0200 | [diff] [blame] | 742 | /* logs directly */ |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 743 | static struct lyxml_attr * |
Radek Krejci | 00249f2 | 2015-07-07 13:43:28 +0200 | [diff] [blame] | 744 | 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] | 745 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 746 | const char *c = data, *start, *delim; |
| 747 | char prefix[32]; |
| 748 | int uc; |
Radek Krejci | 00249f2 | 2015-07-07 13:43:28 +0200 | [diff] [blame] | 749 | struct lyxml_attr *attr = NULL, *a; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 750 | unsigned int size; |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 751 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 752 | /* check if it is attribute or namespace */ |
| 753 | if (!memcmp(c, "xmlns", 5)) { |
| 754 | /* namespace */ |
| 755 | attr = calloc(1, sizeof (struct lyxml_ns)); |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 756 | if (!attr) { |
| 757 | LOGMEM; |
| 758 | return NULL; |
| 759 | } |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 760 | attr->type = LYXML_ATTR_NS; |
Radek Krejci | 00249f2 | 2015-07-07 13:43:28 +0200 | [diff] [blame] | 761 | ((struct lyxml_ns *)attr)->parent = parent; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 762 | c += 5; |
| 763 | if (*c != ':') { |
| 764 | /* default namespace, prefix will be empty */ |
| 765 | goto equal; |
| 766 | } |
| 767 | c++; /* go after ':' to the prefix value */ |
| 768 | } else { |
| 769 | /* attribute */ |
| 770 | attr = calloc(1, sizeof *attr); |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 771 | if (!attr) { |
| 772 | LOGMEM; |
| 773 | return NULL; |
| 774 | } |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 775 | attr->type = LYXML_ATTR_STD; |
| 776 | } |
Radek Krejci | 4ea0838 | 2015-04-21 09:41:40 +0200 | [diff] [blame] | 777 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 778 | /* process name part of the attribute */ |
| 779 | start = c; |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 780 | uc = lyxml_getutf8(c, &size); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 781 | if (!is_xmlnamestartchar(uc)) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 782 | LOGVAL(LYE_XML_INVAL, LY_VLOG_NONE, NULL, "NameStartChar of the attribute"); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 783 | free(attr); |
| 784 | return NULL; |
| 785 | } |
| 786 | c += size; |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 787 | uc = lyxml_getutf8(c, &size); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 788 | while (is_xmlnamechar(uc)) { |
| 789 | if (attr->type == LYXML_ATTR_STD && *c == ':') { |
| 790 | /* attribute in a namespace */ |
| 791 | start = c + 1; |
Radek Krejci | 4ea0838 | 2015-04-21 09:41:40 +0200 | [diff] [blame] | 792 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 793 | /* look for the prefix in namespaces */ |
| 794 | memcpy(prefix, data, c - data); |
| 795 | prefix[c - data] = '\0'; |
Radek Krejci | 4476d41 | 2015-07-10 15:35:01 +0200 | [diff] [blame] | 796 | attr->ns = lyxml_get_ns(parent, prefix); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 797 | } |
| 798 | c += size; |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 799 | uc = lyxml_getutf8(c, &size); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 800 | } |
Radek Krejci | 674e1f8 | 2015-04-21 14:12:19 +0200 | [diff] [blame] | 801 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 802 | /* store the name */ |
| 803 | size = c - start; |
| 804 | attr->name = lydict_insert(ctx, start, size); |
Radek Krejci | 674e1f8 | 2015-04-21 14:12:19 +0200 | [diff] [blame] | 805 | |
| 806 | equal: |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 807 | /* check Eq mark that can be surrounded by whitespaces */ |
| 808 | ign_xmlws(c); |
| 809 | if (*c != '=') { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 810 | LOGVAL(LYE_XML_INVAL, LY_VLOG_NONE, NULL, "attribute definition, \"=\" expected"); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 811 | goto error; |
| 812 | } |
| 813 | c++; |
| 814 | ign_xmlws(c); |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 815 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 816 | /* process value part of the attribute */ |
| 817 | if (!*c || (*c != '"' && *c != '\'')) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 818 | LOGVAL(LYE_XML_INVAL, LY_VLOG_NONE, NULL, "attribute value, \" or \' expected"); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 819 | goto error; |
| 820 | } |
| 821 | delim = c; |
| 822 | attr->value = lydict_insert_zc(ctx, parse_text(++c, *delim, &size)); |
| 823 | if (ly_errno) { |
| 824 | goto error; |
| 825 | } |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 826 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 827 | *len = c + size + 1 - data; /* +1 is delimiter size */ |
Radek Krejci | 00249f2 | 2015-07-07 13:43:28 +0200 | [diff] [blame] | 828 | |
| 829 | /* put attribute into the parent's attributes list */ |
| 830 | if (parent->attr) { |
| 831 | /* go to the end of the list */ |
| 832 | for (a = parent->attr; a->next; a = a->next); |
| 833 | /* and append new attribute */ |
| 834 | a->next = attr; |
| 835 | } else { |
| 836 | /* add the first attribute in the list */ |
| 837 | parent->attr = attr; |
| 838 | } |
| 839 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 840 | return attr; |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 841 | |
| 842 | error: |
Radek Krejci | 00249f2 | 2015-07-07 13:43:28 +0200 | [diff] [blame] | 843 | lyxml_free_attr(ctx, NULL, attr); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 844 | return NULL; |
Radek Krejci | 54ea8de | 2015-04-09 18:02:56 +0200 | [diff] [blame] | 845 | } |
| 846 | |
Michal Vasko | 0d343d1 | 2015-08-24 14:57:36 +0200 | [diff] [blame] | 847 | /* logs directly */ |
Radek Krejci | 9a5daea | 2016-03-02 16:49:40 +0100 | [diff] [blame] | 848 | struct lyxml_elem * |
| 849 | 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] | 850 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 851 | const char *c = data, *start, *e; |
| 852 | const char *lws; /* leading white space for handling mixed content */ |
| 853 | int uc; |
| 854 | char *str; |
| 855 | char prefix[32] = { 0 }; |
| 856 | unsigned int prefix_len = 0; |
| 857 | struct lyxml_elem *elem = NULL, *child; |
| 858 | struct lyxml_attr *attr; |
| 859 | unsigned int size; |
| 860 | int nons_flag = 0, closed_flag = 0; |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 861 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 862 | *len = 0; |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 863 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 864 | if (*c != '<') { |
| 865 | return NULL; |
| 866 | } |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 867 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 868 | /* locate element name */ |
| 869 | c++; |
| 870 | e = c; |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 871 | |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 872 | uc = lyxml_getutf8(e, &size); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 873 | if (!is_xmlnamestartchar(uc)) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 874 | LOGVAL(LYE_XML_INVAL, LY_VLOG_NONE, NULL, "NameStartChar of the element"); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 875 | return NULL; |
| 876 | } |
| 877 | e += size; |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 878 | uc = lyxml_getutf8(e, &size); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 879 | while (is_xmlnamechar(uc)) { |
| 880 | if (*e == ':') { |
| 881 | if (prefix_len) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 882 | 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] | 883 | goto error; |
| 884 | } |
| 885 | /* element in a namespace */ |
| 886 | start = e + 1; |
Radek Krejci | 674e1f8 | 2015-04-21 14:12:19 +0200 | [diff] [blame] | 887 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 888 | /* look for the prefix in namespaces */ |
| 889 | memcpy(prefix, c, prefix_len = e - c); |
| 890 | prefix[prefix_len] = '\0'; |
| 891 | c = start; |
| 892 | } |
| 893 | e += size; |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 894 | uc = lyxml_getutf8(e, &size); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 895 | } |
| 896 | if (!*e) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 897 | LOGVAL(LYE_EOF, LY_VLOG_NONE, NULL); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 898 | return NULL; |
| 899 | } |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 900 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 901 | /* allocate element structure */ |
| 902 | elem = calloc(1, sizeof *elem); |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 903 | if (!elem) { |
| 904 | LOGMEM; |
| 905 | return NULL; |
| 906 | } |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 907 | elem->next = NULL; |
| 908 | elem->prev = elem; |
| 909 | if (parent) { |
Michal Vasko | f8879c2 | 2015-08-21 09:07:36 +0200 | [diff] [blame] | 910 | lyxml_add_child(ctx, parent, elem); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 911 | } |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 912 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 913 | /* store the name into the element structure */ |
| 914 | elem->name = lydict_insert(ctx, c, e - c); |
| 915 | c = e; |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 916 | |
| 917 | process: |
Radek Krejci | db4f078 | 2016-05-03 12:07:16 +0200 | [diff] [blame] | 918 | ly_errno = LY_SUCCESS; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 919 | ign_xmlws(c); |
| 920 | if (!memcmp("/>", c, 2)) { |
| 921 | /* we are done, it was EmptyElemTag */ |
| 922 | c += 2; |
Michal Vasko | 4491384 | 2016-04-13 14:20:41 +0200 | [diff] [blame] | 923 | elem->content = lydict_insert(ctx, "", 0); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 924 | closed_flag = 1; |
| 925 | } else if (*c == '>') { |
| 926 | /* process element content */ |
| 927 | c++; |
| 928 | lws = NULL; |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 929 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 930 | while (*c) { |
| 931 | if (!memcmp(c, "</", 2)) { |
| 932 | if (lws && !elem->child) { |
| 933 | /* leading white spaces were actually content */ |
| 934 | goto store_content; |
| 935 | } |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 936 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 937 | /* Etag */ |
| 938 | c += 2; |
| 939 | /* get name and check it */ |
| 940 | e = c; |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 941 | uc = lyxml_getutf8(e, &size); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 942 | if (!is_xmlnamestartchar(uc)) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 943 | LOGVAL(LYE_XML_INVAL, LY_VLOG_XML, elem, "NameStartChar of the element"); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 944 | goto error; |
| 945 | } |
| 946 | e += size; |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 947 | uc = lyxml_getutf8(e, &size); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 948 | while (is_xmlnamechar(uc)) { |
| 949 | if (*e == ':') { |
| 950 | /* element in a namespace */ |
| 951 | start = e + 1; |
Radek Krejci | 674e1f8 | 2015-04-21 14:12:19 +0200 | [diff] [blame] | 952 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 953 | /* look for the prefix in namespaces */ |
| 954 | if (memcmp(prefix, c, e - c)) { |
Michal Vasko | ff9336a | 2016-05-10 10:48:48 +0200 | [diff] [blame] | 955 | LOGVAL(LYE_SPEC, LY_VLOG_XML, elem, |
| 956 | "Invalid (different namespaces) opening (%s) and closing element tags.", elem->name); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 957 | goto error; |
| 958 | } |
| 959 | c = start; |
| 960 | } |
| 961 | e += size; |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 962 | uc = lyxml_getutf8(e, &size); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 963 | } |
| 964 | if (!*e) { |
Radek Krejci | 3cc1096 | 2016-04-13 15:03:27 +0200 | [diff] [blame] | 965 | LOGVAL(LYE_EOF, LY_VLOG_NONE, NULL); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 966 | goto error; |
| 967 | } |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 968 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 969 | /* check that it corresponds to opening tag */ |
| 970 | size = e - c; |
| 971 | str = malloc((size + 1) * sizeof *str); |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 972 | if (!str) { |
| 973 | LOGMEM; |
| 974 | goto error; |
| 975 | } |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 976 | memcpy(str, c, e - c); |
| 977 | str[e - c] = '\0'; |
| 978 | if (size != strlen(elem->name) || memcmp(str, elem->name, size)) { |
Michal Vasko | ff9336a | 2016-05-10 10:48:48 +0200 | [diff] [blame] | 979 | LOGVAL(LYE_SPEC, LY_VLOG_XML, elem, |
| 980 | "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] | 981 | free(str); |
| 982 | goto error; |
| 983 | } |
| 984 | free(str); |
| 985 | c = e; |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 986 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 987 | ign_xmlws(c); |
| 988 | if (*c != '>') { |
Michal Vasko | ff9336a | 2016-05-10 10:48:48 +0200 | [diff] [blame] | 989 | 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] | 990 | goto error; |
| 991 | } |
| 992 | c++; |
Michal Vasko | e00b789 | 2016-04-14 10:12:18 +0200 | [diff] [blame] | 993 | if (!(elem->flags & LYXML_ELEM_MIXED) && !elem->content) { |
| 994 | /* there was no content, but we don't want NULL (only if mixed content) */ |
| 995 | elem->content = lydict_insert(ctx, "", 0); |
| 996 | } |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 997 | closed_flag = 1; |
| 998 | break; |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 999 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1000 | } else if (!memcmp(c, "<?", 2)) { |
| 1001 | if (lws) { |
| 1002 | /* leading white spaces were only formatting */ |
| 1003 | lws = NULL; |
| 1004 | } |
| 1005 | /* PI - ignore it */ |
| 1006 | c += 2; |
| 1007 | if (parse_ignore(c, "?>", &size)) { |
| 1008 | goto error; |
| 1009 | } |
| 1010 | c += size; |
| 1011 | } else if (!memcmp(c, "<!--", 4)) { |
| 1012 | if (lws) { |
| 1013 | /* leading white spaces were only formatting */ |
| 1014 | lws = NULL; |
| 1015 | } |
| 1016 | /* Comment - ignore it */ |
| 1017 | c += 4; |
| 1018 | if (parse_ignore(c, "-->", &size)) { |
| 1019 | goto error; |
| 1020 | } |
| 1021 | c += size; |
| 1022 | } else if (!memcmp(c, "<![CDATA[", 9)) { |
| 1023 | /* CDSect */ |
| 1024 | goto store_content; |
| 1025 | } else if (*c == '<') { |
| 1026 | if (lws) { |
| 1027 | if (elem->flags & LYXML_ELEM_MIXED) { |
| 1028 | /* we have a mixed content */ |
| 1029 | goto store_content; |
| 1030 | } else { |
| 1031 | /* leading white spaces were only formatting */ |
| 1032 | lws = NULL; |
| 1033 | } |
| 1034 | } |
| 1035 | if (elem->content) { |
| 1036 | /* we have a mixed content */ |
| 1037 | child = calloc(1, sizeof *child); |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 1038 | if (!child) { |
| 1039 | LOGMEM; |
| 1040 | goto error; |
| 1041 | } |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1042 | child->content = elem->content; |
| 1043 | elem->content = NULL; |
Michal Vasko | f8879c2 | 2015-08-21 09:07:36 +0200 | [diff] [blame] | 1044 | lyxml_add_child(ctx, elem, child); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1045 | elem->flags |= LYXML_ELEM_MIXED; |
| 1046 | } |
Radek Krejci | 9a5daea | 2016-03-02 16:49:40 +0100 | [diff] [blame] | 1047 | child = lyxml_parse_elem(ctx, c, &size, elem); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1048 | if (!child) { |
| 1049 | goto error; |
| 1050 | } |
| 1051 | c += size; /* move after processed child element */ |
| 1052 | } else if (is_xmlws(*c)) { |
| 1053 | lws = c; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1054 | ign_xmlws(c); |
| 1055 | } else { |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 1056 | store_content: |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1057 | /* store text content */ |
| 1058 | if (lws) { |
| 1059 | /* process content including the leading white spaces */ |
| 1060 | c = lws; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1061 | lws = NULL; |
| 1062 | } |
| 1063 | elem->content = lydict_insert_zc(ctx, parse_text(c, '<', &size)); |
| 1064 | if (ly_errno) { |
| 1065 | goto error; |
| 1066 | } |
| 1067 | c += size; /* move after processed text content */ |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 1068 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1069 | if (elem->child) { |
| 1070 | /* we have a mixed content */ |
| 1071 | child = calloc(1, sizeof *child); |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 1072 | if (!child) { |
| 1073 | LOGMEM; |
| 1074 | goto error; |
| 1075 | } |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1076 | child->content = elem->content; |
| 1077 | elem->content = NULL; |
Michal Vasko | f8879c2 | 2015-08-21 09:07:36 +0200 | [diff] [blame] | 1078 | lyxml_add_child(ctx, elem, child); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1079 | elem->flags |= LYXML_ELEM_MIXED; |
| 1080 | } |
| 1081 | } |
| 1082 | } |
| 1083 | } else { |
| 1084 | /* process attribute */ |
| 1085 | attr = parse_attr(ctx, c, &size, elem); |
| 1086 | if (!attr) { |
| 1087 | goto error; |
| 1088 | } |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1089 | c += size; /* move after processed attribute */ |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 1090 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1091 | /* check namespace */ |
| 1092 | if (attr->type == LYXML_ATTR_NS) { |
| 1093 | if (!prefix[0] && !attr->name) { |
| 1094 | if (attr->value) { |
| 1095 | /* default prefix */ |
| 1096 | elem->ns = (struct lyxml_ns *)attr; |
| 1097 | } else { |
| 1098 | /* xmlns="" -> no namespace */ |
| 1099 | nons_flag = 1; |
| 1100 | } |
| 1101 | } else if (prefix[0] && attr->name && !memcmp(attr->name, prefix, prefix_len + 1)) { |
| 1102 | /* matching namespace with prefix */ |
| 1103 | elem->ns = (struct lyxml_ns *)attr; |
| 1104 | } |
| 1105 | } |
Radek Krejci | 674e1f8 | 2015-04-21 14:12:19 +0200 | [diff] [blame] | 1106 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1107 | /* go back to finish element processing */ |
| 1108 | goto process; |
| 1109 | } |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 1110 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1111 | *len = c - data; |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 1112 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1113 | if (!closed_flag) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 1114 | 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] | 1115 | goto error; |
| 1116 | } |
Radek Krejci | 674e1f8 | 2015-04-21 14:12:19 +0200 | [diff] [blame] | 1117 | |
Radek Krejci | 78a230a | 2015-07-07 17:04:40 +0200 | [diff] [blame] | 1118 | if (!elem->ns && !nons_flag && parent) { |
Radek Krejci | 4476d41 | 2015-07-10 15:35:01 +0200 | [diff] [blame] | 1119 | elem->ns = lyxml_get_ns(parent, prefix_len ? prefix : NULL); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1120 | } |
Radek Krejci | 674e1f8 | 2015-04-21 14:12:19 +0200 | [diff] [blame] | 1121 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1122 | return elem; |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 1123 | |
| 1124 | error: |
Michal Vasko | 345da0a | 2015-12-02 10:35:55 +0100 | [diff] [blame] | 1125 | lyxml_free(ctx, elem); |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 1126 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1127 | return NULL; |
Radek Krejci | 54ea8de | 2015-04-09 18:02:56 +0200 | [diff] [blame] | 1128 | } |
| 1129 | |
Michal Vasko | 0d343d1 | 2015-08-24 14:57:36 +0200 | [diff] [blame] | 1130 | /* logs directly */ |
Radek Krejci | c6704c8 | 2015-10-06 11:12:45 +0200 | [diff] [blame] | 1131 | API struct lyxml_elem * |
Radek Krejci | 722b007 | 2016-02-01 17:09:45 +0100 | [diff] [blame] | 1132 | lyxml_parse_mem(struct ly_ctx *ctx, const char *data, int options) |
Radek Krejci | 54ea8de | 2015-04-09 18:02:56 +0200 | [diff] [blame] | 1133 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1134 | const char *c = data; |
| 1135 | unsigned int len; |
Radek Krejci | 851ea66 | 2016-01-08 09:30:53 +0100 | [diff] [blame] | 1136 | struct lyxml_elem *root, *first = NULL, *next; |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 1137 | |
Radek Krejci | 2342cf6 | 2016-01-29 16:48:23 +0100 | [diff] [blame] | 1138 | ly_errno = LY_SUCCESS; |
| 1139 | |
Radek Krejci | 120f624 | 2015-12-17 12:32:56 +0100 | [diff] [blame] | 1140 | repeat: |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1141 | /* process document */ |
Radek Krejci | f8ae23e | 2016-07-26 17:11:17 +0200 | [diff] [blame] | 1142 | while (1) { |
| 1143 | if (!*c) { |
| 1144 | /* eof */ |
| 1145 | return first; |
| 1146 | } else if (is_xmlws(*c)) { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1147 | /* skip whitespaces */ |
| 1148 | ign_xmlws(c); |
| 1149 | } else if (!memcmp(c, "<?", 2)) { |
| 1150 | /* XMLDecl or PI - ignore it */ |
| 1151 | c += 2; |
| 1152 | if (parse_ignore(c, "?>", &len)) { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1153 | return NULL; |
| 1154 | } |
| 1155 | c += len; |
| 1156 | } else if (!memcmp(c, "<!--", 4)) { |
| 1157 | /* Comment - ignore it */ |
| 1158 | c += 2; |
| 1159 | if (parse_ignore(c, "-->", &len)) { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1160 | return NULL; |
| 1161 | } |
| 1162 | c += len; |
| 1163 | } else if (!memcmp(c, "<!", 2)) { |
| 1164 | /* DOCTYPE */ |
| 1165 | /* TODO - standalone ignore counting < and > */ |
| 1166 | LOGERR(LY_EINVAL, "DOCTYPE not supported in XML documents."); |
| 1167 | return NULL; |
| 1168 | } else if (*c == '<') { |
| 1169 | /* element - process it in next loop to strictly follow XML |
| 1170 | * format |
| 1171 | */ |
| 1172 | break; |
Michal Vasko | c2e8056 | 2015-07-27 11:31:41 +0200 | [diff] [blame] | 1173 | } else { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 1174 | LOGVAL(LYE_XML_INCHAR, LY_VLOG_NONE, NULL, c); |
Michal Vasko | c2e8056 | 2015-07-27 11:31:41 +0200 | [diff] [blame] | 1175 | return NULL; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1176 | } |
| 1177 | } |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 1178 | |
Radek Krejci | 9a5daea | 2016-03-02 16:49:40 +0100 | [diff] [blame] | 1179 | root = lyxml_parse_elem(ctx, c, &len, NULL); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1180 | if (!root) { |
Michal Vasko | bc58b4a | 2016-01-07 14:42:31 +0100 | [diff] [blame] | 1181 | if (first) { |
Radek Krejci | 851ea66 | 2016-01-08 09:30:53 +0100 | [diff] [blame] | 1182 | LY_TREE_FOR_SAFE(first, next, root) { |
Michal Vasko | bc58b4a | 2016-01-07 14:42:31 +0100 | [diff] [blame] | 1183 | lyxml_free(ctx, root); |
| 1184 | } |
Radek Krejci | 120f624 | 2015-12-17 12:32:56 +0100 | [diff] [blame] | 1185 | } |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1186 | return NULL; |
Radek Krejci | 120f624 | 2015-12-17 12:32:56 +0100 | [diff] [blame] | 1187 | } else if (!first) { |
| 1188 | first = root; |
| 1189 | } else { |
| 1190 | first->prev->next = root; |
| 1191 | root->prev = first->prev; |
| 1192 | first->prev = root; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1193 | } |
| 1194 | c += len; |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 1195 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1196 | /* ignore the rest of document where can be comments, PIs and whitespaces, |
| 1197 | * note that we are not detecting syntax errors in these parts |
| 1198 | */ |
| 1199 | ign_xmlws(c); |
| 1200 | if (*c) { |
Radek Krejci | 722b007 | 2016-02-01 17:09:45 +0100 | [diff] [blame] | 1201 | if (options & LYXML_PARSE_MULTIROOT) { |
Radek Krejci | 120f624 | 2015-12-17 12:32:56 +0100 | [diff] [blame] | 1202 | goto repeat; |
| 1203 | } else { |
| 1204 | LOGWRN("There are some not parsed data:\n%s", c); |
| 1205 | } |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1206 | } |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 1207 | |
Radek Krejci | 120f624 | 2015-12-17 12:32:56 +0100 | [diff] [blame] | 1208 | return first; |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 1209 | } |
| 1210 | |
Radek Krejci | c6704c8 | 2015-10-06 11:12:45 +0200 | [diff] [blame] | 1211 | API struct lyxml_elem * |
Radek Krejci | 722b007 | 2016-02-01 17:09:45 +0100 | [diff] [blame] | 1212 | lyxml_parse_path(struct ly_ctx *ctx, const char *filename, int options) |
Radek Krejci | 54ea8de | 2015-04-09 18:02:56 +0200 | [diff] [blame] | 1213 | { |
Radek Krejci | 6b3d926 | 2015-12-03 13:45:27 +0100 | [diff] [blame] | 1214 | struct lyxml_elem *elem = NULL; |
Pavol Vican | b2570c1 | 2015-11-12 13:50:20 +0100 | [diff] [blame] | 1215 | struct stat sb; |
| 1216 | int fd; |
| 1217 | char *addr; |
| 1218 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1219 | if (!filename || !ctx) { |
| 1220 | LOGERR(LY_EINVAL, "%s: Invalid parameter.", __func__); |
| 1221 | return NULL; |
| 1222 | } |
Radek Krejci | 54ea8de | 2015-04-09 18:02:56 +0200 | [diff] [blame] | 1223 | |
Pavol Vican | b2570c1 | 2015-11-12 13:50:20 +0100 | [diff] [blame] | 1224 | fd = open(filename, O_RDONLY); |
| 1225 | if (fd == -1) { |
Radek Krejci | 6b3d926 | 2015-12-03 13:45:27 +0100 | [diff] [blame] | 1226 | LOGERR(LY_EINVAL,"Opening file \"%s\" failed.", filename); |
Pavol Vican | b2570c1 | 2015-11-12 13:50:20 +0100 | [diff] [blame] | 1227 | return NULL; |
| 1228 | } |
| 1229 | if (fstat(fd, &sb) == -1) { |
| 1230 | LOGERR(LY_EINVAL, "Unable to get file \"%s\" information.\n", filename); |
| 1231 | goto error; |
| 1232 | } |
| 1233 | if (!S_ISREG(sb.st_mode)) { |
Radek Krejci | b051f72 | 2016-02-25 15:12:21 +0100 | [diff] [blame] | 1234 | 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] | 1235 | goto error; |
| 1236 | } |
Pavol Vican | f7cc285 | 2016-03-22 23:27:35 +0100 | [diff] [blame] | 1237 | 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] | 1238 | if (addr == MAP_FAILED) { |
Radek Krejci | 6b3d926 | 2015-12-03 13:45:27 +0100 | [diff] [blame] | 1239 | LOGERR(LY_EMEM,"Map file into memory failed (%s()).", __func__); |
Pavol Vican | b2570c1 | 2015-11-12 13:50:20 +0100 | [diff] [blame] | 1240 | goto error; |
| 1241 | } |
Radek Krejci | 6b3d926 | 2015-12-03 13:45:27 +0100 | [diff] [blame] | 1242 | |
Radek Krejci | 722b007 | 2016-02-01 17:09:45 +0100 | [diff] [blame] | 1243 | elem = lyxml_parse_mem(ctx, addr, options); |
Pavol Vican | f7cc285 | 2016-03-22 23:27:35 +0100 | [diff] [blame] | 1244 | munmap(addr, sb.st_size +2); |
Radek Krejci | 30793ab | 2015-12-03 13:45:45 +0100 | [diff] [blame] | 1245 | close(fd); |
Radek Krejci | 6b3d926 | 2015-12-03 13:45:27 +0100 | [diff] [blame] | 1246 | |
Pavol Vican | b2570c1 | 2015-11-12 13:50:20 +0100 | [diff] [blame] | 1247 | return elem; |
| 1248 | |
| 1249 | error: |
Radek Krejci | 6b3d926 | 2015-12-03 13:45:27 +0100 | [diff] [blame] | 1250 | if (fd != -1) { |
| 1251 | close(fd); |
| 1252 | } |
| 1253 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1254 | return NULL; |
Radek Krejci | 54ea8de | 2015-04-09 18:02:56 +0200 | [diff] [blame] | 1255 | } |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 1256 | |
Michal Vasko | 5db027d | 2015-10-09 14:38:50 +0200 | [diff] [blame] | 1257 | int |
| 1258 | lyxml_dump_text(struct lyout *out, const char *text) |
Radek Krejci | f0023a9 | 2015-04-20 20:51:39 +0200 | [diff] [blame] | 1259 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1260 | unsigned int i, n; |
Radek Krejci | f0023a9 | 2015-04-20 20:51:39 +0200 | [diff] [blame] | 1261 | |
Michal Vasko | 5db027d | 2015-10-09 14:38:50 +0200 | [diff] [blame] | 1262 | if (!text) { |
| 1263 | return 0; |
| 1264 | } |
| 1265 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1266 | for (i = n = 0; text[i]; i++) { |
| 1267 | switch (text[i]) { |
| 1268 | case '&': |
Radek Krejci | 5248f13 | 2015-10-09 10:34:25 +0200 | [diff] [blame] | 1269 | n += ly_print(out, "&"); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1270 | break; |
| 1271 | case '<': |
Radek Krejci | 5248f13 | 2015-10-09 10:34:25 +0200 | [diff] [blame] | 1272 | n += ly_print(out, "<"); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1273 | break; |
| 1274 | case '>': |
| 1275 | /* not needed, just for readability */ |
Radek Krejci | 5248f13 | 2015-10-09 10:34:25 +0200 | [diff] [blame] | 1276 | n += ly_print(out, ">"); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1277 | break; |
Radek Krejci | 952a725 | 2016-07-16 20:52:43 +0200 | [diff] [blame] | 1278 | case '"': |
| 1279 | n += ly_print(out, """); |
| 1280 | break; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1281 | default: |
Radek Krejci | 5248f13 | 2015-10-09 10:34:25 +0200 | [diff] [blame] | 1282 | ly_write(out, &text[i], 1); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1283 | n++; |
| 1284 | } |
| 1285 | } |
Radek Krejci | f0023a9 | 2015-04-20 20:51:39 +0200 | [diff] [blame] | 1286 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1287 | return n; |
Radek Krejci | f0023a9 | 2015-04-20 20:51:39 +0200 | [diff] [blame] | 1288 | } |
| 1289 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1290 | static int |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 1291 | 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] | 1292 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1293 | int size = 0; |
| 1294 | struct lyxml_attr *a; |
| 1295 | struct lyxml_elem *child; |
| 1296 | const char *delim, *delim_outer; |
| 1297 | int indent; |
Radek Krejci | f0023a9 | 2015-04-20 20:51:39 +0200 | [diff] [blame] | 1298 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1299 | if (!e->name) { |
| 1300 | /* mixed content */ |
| 1301 | if (e->content) { |
Michal Vasko | 5db027d | 2015-10-09 14:38:50 +0200 | [diff] [blame] | 1302 | return lyxml_dump_text(out, e->content); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1303 | } else { |
| 1304 | return 0; |
| 1305 | } |
| 1306 | } |
Radek Krejci | f0023a9 | 2015-04-20 20:51:39 +0200 | [diff] [blame] | 1307 | |
Radek Krejci | 722b007 | 2016-02-01 17:09:45 +0100 | [diff] [blame] | 1308 | delim = delim_outer = (options & LYXML_PRINT_FORMAT) ? "\n" : ""; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1309 | indent = 2 * level; |
| 1310 | if ((e->flags & LYXML_ELEM_MIXED) || (e->parent && (e->parent->flags & LYXML_ELEM_MIXED))) { |
| 1311 | delim = ""; |
| 1312 | } |
| 1313 | if (e->parent && (e->parent->flags & LYXML_ELEM_MIXED)) { |
| 1314 | delim_outer = ""; |
| 1315 | indent = 0; |
| 1316 | } |
Radek Krejci | f0023a9 | 2015-04-20 20:51:39 +0200 | [diff] [blame] | 1317 | |
Radek Krejci | 722b007 | 2016-02-01 17:09:45 +0100 | [diff] [blame] | 1318 | 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] | 1319 | /* opening tag */ |
| 1320 | if (e->ns && e->ns->prefix) { |
Radek Krejci | 5248f13 | 2015-10-09 10:34:25 +0200 | [diff] [blame] | 1321 | 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] | 1322 | } else { |
Radek Krejci | 5248f13 | 2015-10-09 10:34:25 +0200 | [diff] [blame] | 1323 | size += ly_print(out, "%*s<%s", indent, "", e->name); |
Radek Krejci | c6704c8 | 2015-10-06 11:12:45 +0200 | [diff] [blame] | 1324 | } |
Radek Krejci | 722b007 | 2016-02-01 17:09:45 +0100 | [diff] [blame] | 1325 | } else if (options & LYXML_PRINT_CLOSE) { |
Radek Krejci | c6704c8 | 2015-10-06 11:12:45 +0200 | [diff] [blame] | 1326 | indent = 0; |
| 1327 | goto close; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1328 | } |
Radek Krejci | 674e1f8 | 2015-04-21 14:12:19 +0200 | [diff] [blame] | 1329 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1330 | /* attributes */ |
| 1331 | for (a = e->attr; a; a = a->next) { |
| 1332 | if (a->type == LYXML_ATTR_NS) { |
| 1333 | if (a->name) { |
Radek Krejci | 5248f13 | 2015-10-09 10:34:25 +0200 | [diff] [blame] | 1334 | 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] | 1335 | } else { |
Radek Krejci | 5248f13 | 2015-10-09 10:34:25 +0200 | [diff] [blame] | 1336 | size += ly_print(out, " xmlns=\"%s\"", a->value ? a->value : ""); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1337 | } |
| 1338 | } else if (a->ns && a->ns->prefix) { |
Radek Krejci | 5248f13 | 2015-10-09 10:34:25 +0200 | [diff] [blame] | 1339 | 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] | 1340 | } else { |
Radek Krejci | 5248f13 | 2015-10-09 10:34:25 +0200 | [diff] [blame] | 1341 | size += ly_print(out, " %s=\"%s\"", a->name, a->value); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1342 | } |
| 1343 | } |
Radek Krejci | 674e1f8 | 2015-04-21 14:12:19 +0200 | [diff] [blame] | 1344 | |
Radek Krejci | c6704c8 | 2015-10-06 11:12:45 +0200 | [diff] [blame] | 1345 | /* apply options */ |
Radek Krejci | 722b007 | 2016-02-01 17:09:45 +0100 | [diff] [blame] | 1346 | if ((options & LYXML_PRINT_CLOSE) && (options & LYXML_PRINT_OPEN)) { |
Radek Krejci | 5248f13 | 2015-10-09 10:34:25 +0200 | [diff] [blame] | 1347 | size += ly_print(out, "/>%s", delim); |
Radek Krejci | c6704c8 | 2015-10-06 11:12:45 +0200 | [diff] [blame] | 1348 | return size; |
Radek Krejci | 722b007 | 2016-02-01 17:09:45 +0100 | [diff] [blame] | 1349 | } else if (options & LYXML_PRINT_OPEN) { |
Radek Krejci | 5248f13 | 2015-10-09 10:34:25 +0200 | [diff] [blame] | 1350 | ly_print(out, ">"); |
Radek Krejci | c6704c8 | 2015-10-06 11:12:45 +0200 | [diff] [blame] | 1351 | return ++size; |
Radek Krejci | 722b007 | 2016-02-01 17:09:45 +0100 | [diff] [blame] | 1352 | } else if (options & LYXML_PRINT_ATTRS) { |
Radek Krejci | c6704c8 | 2015-10-06 11:12:45 +0200 | [diff] [blame] | 1353 | return size; |
| 1354 | } |
| 1355 | |
Michal Vasko | 3a61161 | 2016-04-14 10:12:56 +0200 | [diff] [blame] | 1356 | if (!e->child && (!e->content || !e->content[0])) { |
Radek Krejci | 5248f13 | 2015-10-09 10:34:25 +0200 | [diff] [blame] | 1357 | size += ly_print(out, "/>%s", delim); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1358 | return size; |
Michal Vasko | 3a61161 | 2016-04-14 10:12:56 +0200 | [diff] [blame] | 1359 | } else if (e->content && e->content[0]) { |
Radek Krejci | 5248f13 | 2015-10-09 10:34:25 +0200 | [diff] [blame] | 1360 | ly_print(out, ">"); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1361 | size++; |
Radek Krejci | f0023a9 | 2015-04-20 20:51:39 +0200 | [diff] [blame] | 1362 | |
Michal Vasko | 5db027d | 2015-10-09 14:38:50 +0200 | [diff] [blame] | 1363 | size += lyxml_dump_text(out, e->content); |
Radek Krejci | f0023a9 | 2015-04-20 20:51:39 +0200 | [diff] [blame] | 1364 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1365 | if (e->ns && e->ns->prefix) { |
Radek Krejci | 5248f13 | 2015-10-09 10:34:25 +0200 | [diff] [blame] | 1366 | 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] | 1367 | } else { |
Radek Krejci | 5248f13 | 2015-10-09 10:34:25 +0200 | [diff] [blame] | 1368 | size += ly_print(out, "</%s>%s", e->name, delim); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1369 | } |
| 1370 | return size; |
| 1371 | } else { |
Radek Krejci | 5248f13 | 2015-10-09 10:34:25 +0200 | [diff] [blame] | 1372 | size += ly_print(out, ">%s", delim); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1373 | } |
Radek Krejci | 674e1f8 | 2015-04-21 14:12:19 +0200 | [diff] [blame] | 1374 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1375 | /* go recursively */ |
| 1376 | LY_TREE_FOR(e->child, child) { |
Radek Krejci | 722b007 | 2016-02-01 17:09:45 +0100 | [diff] [blame] | 1377 | if (options & LYXML_PRINT_FORMAT) { |
| 1378 | size += dump_elem(out, child, level + 1, LYXML_PRINT_FORMAT); |
Pavol Vican | be7eef5 | 2015-10-22 14:07:48 +0200 | [diff] [blame] | 1379 | } else { |
| 1380 | size += dump_elem(out, child, level, 0); |
| 1381 | } |
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 | c6704c8 | 2015-10-06 11:12:45 +0200 | [diff] [blame] | 1384 | close: |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1385 | /* closing tag */ |
| 1386 | if (e->ns && e->ns->prefix) { |
Radek Krejci | 5248f13 | 2015-10-09 10:34:25 +0200 | [diff] [blame] | 1387 | 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] | 1388 | } else { |
Radek Krejci | 5248f13 | 2015-10-09 10:34:25 +0200 | [diff] [blame] | 1389 | size += ly_print(out, "%*s</%s>%s", indent, "", e->name, delim_outer); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1390 | } |
Radek Krejci | f0023a9 | 2015-04-20 20:51:39 +0200 | [diff] [blame] | 1391 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1392 | return size; |
Radek Krejci | f0023a9 | 2015-04-20 20:51:39 +0200 | [diff] [blame] | 1393 | } |
| 1394 | |
Radek Krejci | 8c56a5a | 2015-12-16 15:10:28 +0100 | [diff] [blame] | 1395 | static int |
| 1396 | dump_siblings(struct lyout *out, const struct lyxml_elem *e, int options) |
| 1397 | { |
| 1398 | const struct lyxml_elem *start, *iter; |
| 1399 | int ret = 0; |
| 1400 | |
| 1401 | if (e->parent) { |
| 1402 | start = e->parent->child; |
| 1403 | } else { |
| 1404 | start = e; |
| 1405 | while(start->prev && start->prev->next) { |
| 1406 | start = start->prev; |
| 1407 | } |
| 1408 | } |
| 1409 | |
| 1410 | LY_TREE_FOR(start, iter) { |
| 1411 | ret += dump_elem(out, iter, 0, options); |
| 1412 | } |
| 1413 | |
| 1414 | return ret; |
| 1415 | } |
| 1416 | |
Radek Krejci | c6704c8 | 2015-10-06 11:12:45 +0200 | [diff] [blame] | 1417 | API int |
Radek Krejci | 722b007 | 2016-02-01 17:09:45 +0100 | [diff] [blame] | 1418 | lyxml_print_file(FILE *stream, const struct lyxml_elem *elem, int options) |
Radek Krejci | f0023a9 | 2015-04-20 20:51:39 +0200 | [diff] [blame] | 1419 | { |
Radek Krejci | 5248f13 | 2015-10-09 10:34:25 +0200 | [diff] [blame] | 1420 | struct lyout out; |
| 1421 | |
| 1422 | if (!stream || !elem) { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1423 | return 0; |
| 1424 | } |
Radek Krejci | f0023a9 | 2015-04-20 20:51:39 +0200 | [diff] [blame] | 1425 | |
Radek Krejci | 5248f13 | 2015-10-09 10:34:25 +0200 | [diff] [blame] | 1426 | out.type = LYOUT_STREAM; |
| 1427 | out.method.f = stream; |
| 1428 | |
Radek Krejci | 722b007 | 2016-02-01 17:09:45 +0100 | [diff] [blame] | 1429 | if (options & LYXML_PRINT_SIBLINGS) { |
Radek Krejci | 8c56a5a | 2015-12-16 15:10:28 +0100 | [diff] [blame] | 1430 | return dump_siblings(&out, elem, options); |
| 1431 | } else { |
| 1432 | return dump_elem(&out, elem, 0, options); |
| 1433 | } |
Radek Krejci | 5248f13 | 2015-10-09 10:34:25 +0200 | [diff] [blame] | 1434 | } |
| 1435 | |
| 1436 | API int |
Radek Krejci | 722b007 | 2016-02-01 17:09:45 +0100 | [diff] [blame] | 1437 | lyxml_print_fd(int fd, const struct lyxml_elem *elem, int options) |
Radek Krejci | 5248f13 | 2015-10-09 10:34:25 +0200 | [diff] [blame] | 1438 | { |
| 1439 | struct lyout out; |
| 1440 | |
| 1441 | if (fd < 0 || !elem) { |
| 1442 | return 0; |
| 1443 | } |
| 1444 | |
| 1445 | out.type = LYOUT_FD; |
| 1446 | out.method.fd = fd; |
| 1447 | |
Radek Krejci | 722b007 | 2016-02-01 17:09:45 +0100 | [diff] [blame] | 1448 | if (options & LYXML_PRINT_SIBLINGS) { |
Radek Krejci | 8c56a5a | 2015-12-16 15:10:28 +0100 | [diff] [blame] | 1449 | return dump_siblings(&out, elem, options); |
| 1450 | } else { |
| 1451 | return dump_elem(&out, elem, 0, options); |
| 1452 | } |
Radek Krejci | f0023a9 | 2015-04-20 20:51:39 +0200 | [diff] [blame] | 1453 | } |
Radek Krejci | 6140e4e | 2015-10-09 15:50:55 +0200 | [diff] [blame] | 1454 | |
| 1455 | API int |
Radek Krejci | 722b007 | 2016-02-01 17:09:45 +0100 | [diff] [blame] | 1456 | lyxml_print_mem(char **strp, const struct lyxml_elem *elem, int options) |
Radek Krejci | 2fa0fc1 | 2015-10-14 18:14:29 +0200 | [diff] [blame] | 1457 | { |
| 1458 | struct lyout out; |
| 1459 | int r; |
| 1460 | |
| 1461 | if (!strp || !elem) { |
| 1462 | return 0; |
| 1463 | } |
| 1464 | |
| 1465 | out.type = LYOUT_MEMORY; |
| 1466 | out.method.mem.buf = NULL; |
| 1467 | out.method.mem.len = 0; |
| 1468 | out.method.mem.size = 0; |
| 1469 | |
Radek Krejci | 722b007 | 2016-02-01 17:09:45 +0100 | [diff] [blame] | 1470 | if (options & LYXML_PRINT_SIBLINGS) { |
Radek Krejci | 8c56a5a | 2015-12-16 15:10:28 +0100 | [diff] [blame] | 1471 | r = dump_siblings(&out, elem, options); |
| 1472 | } else { |
| 1473 | r = dump_elem(&out, elem, 0, options); |
| 1474 | } |
Radek Krejci | 2fa0fc1 | 2015-10-14 18:14:29 +0200 | [diff] [blame] | 1475 | |
| 1476 | *strp = out.method.mem.buf; |
| 1477 | return r; |
| 1478 | } |
| 1479 | |
| 1480 | API int |
Radek Krejci | 722b007 | 2016-02-01 17:09:45 +0100 | [diff] [blame] | 1481 | 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] | 1482 | { |
| 1483 | struct lyout out; |
| 1484 | |
| 1485 | if (!writeclb || !elem) { |
| 1486 | return 0; |
| 1487 | } |
| 1488 | |
| 1489 | out.type = LYOUT_CALLBACK; |
Radek Krejci | 50929eb | 2015-10-09 18:14:15 +0200 | [diff] [blame] | 1490 | out.method.clb.f = writeclb; |
| 1491 | out.method.clb.arg = arg; |
Radek Krejci | 6140e4e | 2015-10-09 15:50:55 +0200 | [diff] [blame] | 1492 | |
Radek Krejci | 722b007 | 2016-02-01 17:09:45 +0100 | [diff] [blame] | 1493 | if (options & LYXML_PRINT_SIBLINGS) { |
Radek Krejci | 8c56a5a | 2015-12-16 15:10:28 +0100 | [diff] [blame] | 1494 | return dump_siblings(&out, elem, options); |
| 1495 | } else { |
| 1496 | return dump_elem(&out, elem, 0, options); |
| 1497 | } |
Radek Krejci | 6140e4e | 2015-10-09 15:50:55 +0200 | [diff] [blame] | 1498 | } |