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