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