Michal Vasko | 1324b6c | 2018-09-07 11:16:23 +0200 | [diff] [blame] | 1 | /** |
| 2 | * @file common.c |
| 3 | * @author Michal Vasko <mvasko@cesnet.cz> |
| 4 | * @brief common internal definitions for libyang |
| 5 | * |
| 6 | * Copyright (c) 2018 CESNET, z.s.p.o. |
| 7 | * |
| 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 |
| 11 | * |
| 12 | * https://opensource.org/licenses/BSD-3-Clause |
| 13 | */ |
Radek Krejci | b7db73a | 2018-10-24 14:18:40 +0200 | [diff] [blame] | 14 | |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 15 | #define _GNU_SOURCE |
| 16 | |
Radek Krejci | b7db73a | 2018-10-24 14:18:40 +0200 | [diff] [blame] | 17 | #include "common.h" |
Michal Vasko | 1324b6c | 2018-09-07 11:16:23 +0200 | [diff] [blame] | 18 | |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 19 | #include <assert.h> |
Michal Vasko | 841d1a9 | 2018-09-07 15:40:31 +0200 | [diff] [blame] | 20 | #include <ctype.h> |
Radek Krejci | 4546aa6 | 2019-07-15 16:53:32 +0200 | [diff] [blame] | 21 | #include <errno.h> |
Michal Vasko | 15dc9fa | 2021-05-03 14:33:05 +0200 | [diff] [blame] | 22 | #include <inttypes.h> |
Radek Krejci | 4546aa6 | 2019-07-15 16:53:32 +0200 | [diff] [blame] | 23 | #include <stdarg.h> |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 24 | #include <stdio.h> |
Radek Krejci | 4546aa6 | 2019-07-15 16:53:32 +0200 | [diff] [blame] | 25 | #include <stdlib.h> |
Michal Vasko | 841d1a9 | 2018-09-07 15:40:31 +0200 | [diff] [blame] | 26 | #include <string.h> |
Jan Kundrát | 111ce78 | 2021-12-12 03:12:23 +0100 | [diff] [blame^] | 27 | #ifndef _WIN32 |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 28 | #include <sys/mman.h> |
Jan Kundrát | 111ce78 | 2021-12-12 03:12:23 +0100 | [diff] [blame^] | 29 | #endif |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 30 | #include <sys/stat.h> |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 31 | #include <unistd.h> |
Michal Vasko | 841d1a9 | 2018-09-07 15:40:31 +0200 | [diff] [blame] | 32 | |
Radek Krejci | aa45bda | 2020-07-20 07:43:38 +0200 | [diff] [blame] | 33 | #include "compat.h" |
Radek Krejci | b4a4a27 | 2019-06-10 12:44:52 +0200 | [diff] [blame] | 34 | #include "tree_schema_internal.h" |
aPiecek | 704f8e9 | 2021-08-25 13:35:05 +0200 | [diff] [blame] | 35 | #include "xml.h" |
Michal Vasko | 1324b6c | 2018-09-07 11:16:23 +0200 | [diff] [blame] | 36 | |
| 37 | void * |
| 38 | ly_realloc(void *ptr, size_t size) |
| 39 | { |
| 40 | void *new_mem; |
| 41 | |
| 42 | new_mem = realloc(ptr, size); |
| 43 | if (!new_mem) { |
| 44 | free(ptr); |
| 45 | } |
| 46 | |
| 47 | return new_mem; |
| 48 | } |
Michal Vasko | 841d1a9 | 2018-09-07 15:40:31 +0200 | [diff] [blame] | 49 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 50 | char * |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 51 | ly_strnchr(const char *s, int c, size_t len) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 52 | { |
Michal Vasko | b4d40d6 | 2021-05-04 11:42:44 +0200 | [diff] [blame] | 53 | for ( ; len && (*s != (char)c); ++s, --len) {} |
| 54 | return len ? (char *)s : NULL; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 55 | } |
| 56 | |
Radek Krejci | 7f9b651 | 2019-09-18 13:11:09 +0200 | [diff] [blame] | 57 | int |
| 58 | ly_strncmp(const char *refstr, const char *str, size_t str_len) |
| 59 | { |
| 60 | int rc = strncmp(refstr, str, str_len); |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 61 | |
| 62 | if (!rc && (refstr[str_len] == '\0')) { |
Radek Krejci | 7f9b651 | 2019-09-18 13:11:09 +0200 | [diff] [blame] | 63 | return 0; |
| 64 | } else { |
| 65 | return rc ? rc : 1; |
| 66 | } |
| 67 | } |
| 68 | |
Michal Vasko | 15dc9fa | 2021-05-03 14:33:05 +0200 | [diff] [blame] | 69 | LY_ERR |
| 70 | ly_strntou8(const char *nptr, size_t len, uint8_t *ret) |
| 71 | { |
| 72 | uint8_t num = 0, dig, dec_pow; |
| 73 | |
| 74 | if (len > 3) { |
| 75 | /* overflow for sure */ |
| 76 | return LY_EDENIED; |
| 77 | } |
| 78 | |
| 79 | dec_pow = 1; |
| 80 | for ( ; len && isdigit(nptr[len - 1]); --len) { |
| 81 | dig = nptr[len - 1] - 48; |
| 82 | |
| 83 | if (LY_OVERFLOW_MUL(UINT8_MAX, dig, dec_pow)) { |
| 84 | return LY_EDENIED; |
| 85 | } |
| 86 | dig *= dec_pow; |
| 87 | |
| 88 | if (LY_OVERFLOW_ADD(UINT8_MAX, num, dig)) { |
| 89 | return LY_EDENIED; |
| 90 | } |
| 91 | num += dig; |
| 92 | |
| 93 | dec_pow *= 10; |
| 94 | } |
| 95 | |
| 96 | if (len) { |
| 97 | return LY_EVALID; |
| 98 | } |
| 99 | *ret = num; |
| 100 | return LY_SUCCESS; |
| 101 | } |
| 102 | |
aPiecek | e3f828d | 2021-05-10 15:34:41 +0200 | [diff] [blame] | 103 | LY_ERR |
| 104 | ly_value_prefix_next(const char *str_begin, const char *str_end, uint32_t *len, ly_bool *is_prefix, const char **str_next) |
aPiecek | f102d4d | 2021-03-30 12:18:38 +0200 | [diff] [blame] | 105 | { |
| 106 | const char *stop, *prefix; |
aPiecek | e3f828d | 2021-05-10 15:34:41 +0200 | [diff] [blame] | 107 | size_t bytes_read; |
aPiecek | f102d4d | 2021-03-30 12:18:38 +0200 | [diff] [blame] | 108 | uint32_t c; |
| 109 | ly_bool prefix_found; |
aPiecek | e3f828d | 2021-05-10 15:34:41 +0200 | [diff] [blame] | 110 | LY_ERR ret = LY_SUCCESS; |
aPiecek | f102d4d | 2021-03-30 12:18:38 +0200 | [diff] [blame] | 111 | |
aPiecek | e3f828d | 2021-05-10 15:34:41 +0200 | [diff] [blame] | 112 | assert(len && is_prefix && str_next); |
aPiecek | f102d4d | 2021-03-30 12:18:38 +0200 | [diff] [blame] | 113 | |
| 114 | #define IS_AT_END(PTR, STR_END) (STR_END ? PTR == STR_END : !(*PTR)) |
| 115 | |
| 116 | *str_next = NULL; |
| 117 | *is_prefix = 0; |
aPiecek | e3f828d | 2021-05-10 15:34:41 +0200 | [diff] [blame] | 118 | *len = 0; |
aPiecek | f102d4d | 2021-03-30 12:18:38 +0200 | [diff] [blame] | 119 | |
| 120 | if (!str_begin || !(*str_begin) || (str_begin == str_end)) { |
| 121 | return ret; |
| 122 | } |
| 123 | |
| 124 | stop = str_begin; |
| 125 | prefix = NULL; |
| 126 | prefix_found = 0; |
| 127 | |
| 128 | do { |
| 129 | /* look for the beginning of the YANG value */ |
aPiecek | e3f828d | 2021-05-10 15:34:41 +0200 | [diff] [blame] | 130 | do { |
| 131 | LY_CHECK_RET(ly_getutf8(&stop, &c, &bytes_read)); |
| 132 | } while (!is_xmlqnamestartchar(c) && !IS_AT_END(stop, str_end)); |
aPiecek | f102d4d | 2021-03-30 12:18:38 +0200 | [diff] [blame] | 133 | |
| 134 | if (IS_AT_END(stop, str_end)) { |
| 135 | break; |
| 136 | } |
| 137 | |
| 138 | /* maybe the prefix was found */ |
aPiecek | e3f828d | 2021-05-10 15:34:41 +0200 | [diff] [blame] | 139 | prefix = stop - bytes_read; |
aPiecek | f102d4d | 2021-03-30 12:18:38 +0200 | [diff] [blame] | 140 | |
| 141 | /* look for the the end of the prefix */ |
aPiecek | e3f828d | 2021-05-10 15:34:41 +0200 | [diff] [blame] | 142 | do { |
| 143 | LY_CHECK_RET(ly_getutf8(&stop, &c, &bytes_read)); |
| 144 | } while (is_xmlqnamechar(c) && !IS_AT_END(stop, str_end)); |
aPiecek | f102d4d | 2021-03-30 12:18:38 +0200 | [diff] [blame] | 145 | |
| 146 | prefix_found = c == ':' ? 1 : 0; |
| 147 | |
| 148 | /* if it wasn't the prefix, keep looking */ |
| 149 | } while (!IS_AT_END(stop, str_end) && !prefix_found); |
| 150 | |
| 151 | if ((str_begin == prefix) && prefix_found) { |
| 152 | /* prefix found at the beginning of the input string */ |
| 153 | *is_prefix = 1; |
| 154 | *str_next = IS_AT_END(stop, str_end) ? NULL : stop; |
aPiecek | e3f828d | 2021-05-10 15:34:41 +0200 | [diff] [blame] | 155 | *len = (stop - bytes_read) - str_begin; |
aPiecek | f102d4d | 2021-03-30 12:18:38 +0200 | [diff] [blame] | 156 | } else if ((str_begin != prefix) && (prefix_found)) { |
| 157 | /* there is a some string before prefix */ |
| 158 | *str_next = prefix; |
aPiecek | e3f828d | 2021-05-10 15:34:41 +0200 | [diff] [blame] | 159 | *len = prefix - str_begin; |
aPiecek | f102d4d | 2021-03-30 12:18:38 +0200 | [diff] [blame] | 160 | } else { |
| 161 | /* no prefix found */ |
aPiecek | e3f828d | 2021-05-10 15:34:41 +0200 | [diff] [blame] | 162 | *len = stop - str_begin; |
aPiecek | f102d4d | 2021-03-30 12:18:38 +0200 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | #undef IS_AT_END |
| 166 | |
| 167 | return ret; |
| 168 | } |
| 169 | |
Radek Krejci | b416be6 | 2018-10-01 14:51:45 +0200 | [diff] [blame] | 170 | LY_ERR |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 171 | ly_getutf8(const char **input, uint32_t *utf8_char, size_t *bytes_read) |
Radek Krejci | b416be6 | 2018-10-01 14:51:45 +0200 | [diff] [blame] | 172 | { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 173 | uint32_t c, aux; |
| 174 | size_t len; |
Radek Krejci | b416be6 | 2018-10-01 14:51:45 +0200 | [diff] [blame] | 175 | |
Radek Krejci | cc6a45c | 2019-05-13 10:16:14 +0200 | [diff] [blame] | 176 | if (bytes_read) { |
| 177 | (*bytes_read) = 0; |
| 178 | } |
| 179 | |
Radek Krejci | b416be6 | 2018-10-01 14:51:45 +0200 | [diff] [blame] | 180 | c = (*input)[0]; |
| 181 | LY_CHECK_RET(!c, LY_EINVAL); |
| 182 | |
| 183 | if (!(c & 0x80)) { |
| 184 | /* one byte character */ |
| 185 | len = 1; |
| 186 | |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 187 | if ((c < 0x20) && (c != 0x9) && (c != 0xa) && (c != 0xd)) { |
Radek Krejci | b416be6 | 2018-10-01 14:51:45 +0200 | [diff] [blame] | 188 | return LY_EINVAL; |
| 189 | } |
| 190 | } else if ((c & 0xe0) == 0xc0) { |
| 191 | /* two bytes character */ |
| 192 | len = 2; |
| 193 | |
| 194 | aux = (*input)[1]; |
| 195 | if ((aux & 0xc0) != 0x80) { |
| 196 | return LY_EINVAL; |
| 197 | } |
| 198 | c = ((c & 0x1f) << 6) | (aux & 0x3f); |
| 199 | |
| 200 | if (c < 0x80) { |
| 201 | return LY_EINVAL; |
| 202 | } |
| 203 | } else if ((c & 0xf0) == 0xe0) { |
| 204 | /* three bytes character */ |
| 205 | len = 3; |
| 206 | |
| 207 | c &= 0x0f; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 208 | for (uint64_t i = 1; i <= 2; i++) { |
Radek Krejci | b416be6 | 2018-10-01 14:51:45 +0200 | [diff] [blame] | 209 | aux = (*input)[i]; |
| 210 | if ((aux & 0xc0) != 0x80) { |
| 211 | return LY_EINVAL; |
| 212 | } |
| 213 | |
| 214 | c = (c << 6) | (aux & 0x3f); |
| 215 | } |
| 216 | |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 217 | if ((c < 0x800) || ((c > 0xd7ff) && (c < 0xe000)) || (c > 0xfffd)) { |
Radek Krejci | b416be6 | 2018-10-01 14:51:45 +0200 | [diff] [blame] | 218 | return LY_EINVAL; |
| 219 | } |
| 220 | } else if ((c & 0xf8) == 0xf0) { |
| 221 | /* four bytes character */ |
| 222 | len = 4; |
| 223 | |
| 224 | c &= 0x07; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 225 | for (uint64_t i = 1; i <= 3; i++) { |
Radek Krejci | b416be6 | 2018-10-01 14:51:45 +0200 | [diff] [blame] | 226 | aux = (*input)[i]; |
| 227 | if ((aux & 0xc0) != 0x80) { |
| 228 | return LY_EINVAL; |
| 229 | } |
| 230 | |
| 231 | c = (c << 6) | (aux & 0x3f); |
| 232 | } |
| 233 | |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 234 | if ((c < 0x1000) || (c > 0x10ffff)) { |
Radek Krejci | b416be6 | 2018-10-01 14:51:45 +0200 | [diff] [blame] | 235 | return LY_EINVAL; |
| 236 | } |
| 237 | } else { |
| 238 | return LY_EINVAL; |
| 239 | } |
| 240 | |
| 241 | (*utf8_char) = c; |
| 242 | (*input) += len; |
| 243 | if (bytes_read) { |
| 244 | (*bytes_read) = len; |
| 245 | } |
| 246 | return LY_SUCCESS; |
| 247 | } |
| 248 | |
Radek Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 249 | LY_ERR |
| 250 | ly_pututf8(char *dst, uint32_t value, size_t *bytes_written) |
| 251 | { |
| 252 | if (value < 0x80) { |
| 253 | /* one byte character */ |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 254 | if ((value < 0x20) && |
| 255 | (value != 0x09) && |
| 256 | (value != 0x0a) && |
| 257 | (value != 0x0d)) { |
Radek Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 258 | return LY_EINVAL; |
| 259 | } |
| 260 | |
| 261 | dst[0] = value; |
| 262 | (*bytes_written) = 1; |
| 263 | } else if (value < 0x800) { |
| 264 | /* two bytes character */ |
| 265 | dst[0] = 0xc0 | (value >> 6); |
| 266 | dst[1] = 0x80 | (value & 0x3f); |
| 267 | (*bytes_written) = 2; |
| 268 | } else if (value < 0xfffe) { |
| 269 | /* three bytes character */ |
| 270 | if (((value & 0xf800) == 0xd800) || |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 271 | ((value >= 0xfdd0) && (value <= 0xfdef))) { |
Radek Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 272 | /* exclude surrogate blocks %xD800-DFFF */ |
| 273 | /* exclude noncharacters %xFDD0-FDEF */ |
| 274 | return LY_EINVAL; |
| 275 | } |
| 276 | |
| 277 | dst[0] = 0xe0 | (value >> 12); |
| 278 | dst[1] = 0x80 | ((value >> 6) & 0x3f); |
| 279 | dst[2] = 0x80 | (value & 0x3f); |
| 280 | |
| 281 | (*bytes_written) = 3; |
| 282 | } else if (value < 0x10fffe) { |
| 283 | if ((value & 0xffe) == 0xffe) { |
| 284 | /* exclude noncharacters %xFFFE-FFFF, %x1FFFE-1FFFF, %x2FFFE-2FFFF, %x3FFFE-3FFFF, %x4FFFE-4FFFF, |
| 285 | * %x5FFFE-5FFFF, %x6FFFE-6FFFF, %x7FFFE-7FFFF, %x8FFFE-8FFFF, %x9FFFE-9FFFF, %xAFFFE-AFFFF, |
| 286 | * %xBFFFE-BFFFF, %xCFFFE-CFFFF, %xDFFFE-DFFFF, %xEFFFE-EFFFF, %xFFFFE-FFFFF, %x10FFFE-10FFFF */ |
| 287 | return LY_EINVAL; |
| 288 | } |
| 289 | /* four bytes character */ |
| 290 | dst[0] = 0xf0 | (value >> 18); |
| 291 | dst[1] = 0x80 | ((value >> 12) & 0x3f); |
| 292 | dst[2] = 0x80 | ((value >> 6) & 0x3f); |
| 293 | dst[3] = 0x80 | (value & 0x3f); |
| 294 | |
| 295 | (*bytes_written) = 4; |
| 296 | } else { |
| 297 | return LY_EINVAL; |
| 298 | } |
| 299 | return LY_SUCCESS; |
| 300 | } |
| 301 | |
Radek Krejci | 76c9801 | 2019-08-14 11:23:24 +0200 | [diff] [blame] | 302 | /** |
| 303 | * @brief Static table of the UTF8 characters lengths according to their first byte. |
| 304 | */ |
Radek Krejci | f6a1100 | 2020-08-21 13:29:07 +0200 | [diff] [blame] | 305 | static const unsigned char utf8_char_length_table[] = { |
Radek Krejci | 76c9801 | 2019-08-14 11:23:24 +0200 | [diff] [blame] | 306 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 307 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 308 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 309 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 310 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 311 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 312 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 313 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 314 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 315 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 316 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 317 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 318 | 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, |
| 319 | 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, |
| 320 | 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, |
| 321 | 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 1, 1 |
| 322 | }; |
| 323 | |
| 324 | size_t |
| 325 | ly_utf8len(const char *str, size_t bytes) |
| 326 | { |
Radek Krejci | 1e008d2 | 2020-08-17 11:37:37 +0200 | [diff] [blame] | 327 | size_t len = 0; |
| 328 | const char *ptr = str; |
Radek Krejci | 76c9801 | 2019-08-14 11:23:24 +0200 | [diff] [blame] | 329 | |
Michal Vasko | b4d40d6 | 2021-05-04 11:42:44 +0200 | [diff] [blame] | 330 | while (((size_t)(ptr - str) < bytes) && *ptr) { |
Radek Krejci | 1e008d2 | 2020-08-17 11:37:37 +0200 | [diff] [blame] | 331 | ++len; |
| 332 | ptr += utf8_char_length_table[((unsigned char)(*ptr))]; |
| 333 | } |
Radek Krejci | 76c9801 | 2019-08-14 11:23:24 +0200 | [diff] [blame] | 334 | return len; |
| 335 | } |
| 336 | |
Radek Krejci | d972c25 | 2018-09-25 13:23:39 +0200 | [diff] [blame] | 337 | size_t |
| 338 | LY_VCODE_INSTREXP_len(const char *str) |
| 339 | { |
| 340 | size_t len = 0; |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 341 | |
Radek Krejci | d972c25 | 2018-09-25 13:23:39 +0200 | [diff] [blame] | 342 | if (!str) { |
| 343 | return len; |
| 344 | } else if (!str[0]) { |
| 345 | return 1; |
| 346 | } |
Radek Krejci | 1e008d2 | 2020-08-17 11:37:37 +0200 | [diff] [blame] | 347 | for (len = 1; len < LY_VCODE_INSTREXP_MAXLEN && str[len]; ++len) {} |
Radek Krejci | d972c25 | 2018-09-25 13:23:39 +0200 | [diff] [blame] | 348 | return len; |
| 349 | } |
| 350 | |
Jan Kundrát | 111ce78 | 2021-12-12 03:12:23 +0100 | [diff] [blame^] | 351 | #ifdef HAVE_MMAP |
Radek Krejci | f345c01 | 2018-09-19 11:12:59 +0200 | [diff] [blame] | 352 | LY_ERR |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 353 | ly_mmap(struct ly_ctx *ctx, int fd, size_t *length, void **addr) |
Michal Vasko | 841d1a9 | 2018-09-07 15:40:31 +0200 | [diff] [blame] | 354 | { |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 355 | struct stat sb; |
| 356 | long pagesize; |
| 357 | size_t m; |
Michal Vasko | 841d1a9 | 2018-09-07 15:40:31 +0200 | [diff] [blame] | 358 | |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 359 | assert(length); |
| 360 | assert(addr); |
| 361 | assert(fd >= 0); |
Michal Vasko | 841d1a9 | 2018-09-07 15:40:31 +0200 | [diff] [blame] | 362 | |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 363 | if (fstat(fd, &sb) == -1) { |
| 364 | LOGERR(ctx, LY_ESYS, "Failed to stat the file descriptor (%s) for the mmap().", strerror(errno)); |
| 365 | return LY_ESYS; |
Michal Vasko | 841d1a9 | 2018-09-07 15:40:31 +0200 | [diff] [blame] | 366 | } |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 367 | if (!S_ISREG(sb.st_mode)) { |
| 368 | LOGERR(ctx, LY_EINVAL, "File to mmap() is not a regular file."); |
| 369 | return LY_ESYS; |
Michal Vasko | 841d1a9 | 2018-09-07 15:40:31 +0200 | [diff] [blame] | 370 | } |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 371 | if (!sb.st_size) { |
| 372 | *addr = NULL; |
| 373 | return LY_SUCCESS; |
| 374 | } |
| 375 | pagesize = sysconf(_SC_PAGESIZE); |
| 376 | |
| 377 | m = sb.st_size % pagesize; |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 378 | if (m && (pagesize - m >= 1)) { |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 379 | /* there will be enough space (at least 1 byte) after the file content mapping to provide zeroed NULL-termination byte */ |
| 380 | *length = sb.st_size + 1; |
| 381 | *addr = mmap(NULL, *length, PROT_READ, MAP_PRIVATE, fd, 0); |
| 382 | } else { |
| 383 | /* there will not be enough bytes after the file content mapping for the additional bytes and some of them |
| 384 | * would overflow into another page that would not be zerroed and any access into it would generate SIGBUS. |
| 385 | * Therefore we have to do the following hack with double mapping. First, the required number of bytes |
| 386 | * (including the additinal bytes) is required as anonymous and thus they will be really provided (actually more |
| 387 | * because of using whole pages) and also initialized by zeros. Then, the file is mapped to the same address |
| 388 | * where the anonymous mapping starts. */ |
| 389 | *length = sb.st_size + pagesize; |
| 390 | *addr = mmap(NULL, *length, PROT_READ, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); |
| 391 | *addr = mmap(*addr, sb.st_size, PROT_READ, MAP_PRIVATE | MAP_FIXED, fd, 0); |
| 392 | } |
| 393 | if (*addr == MAP_FAILED) { |
| 394 | LOGERR(ctx, LY_ESYS, "mmap() failed (%s).", strerror(errno)); |
| 395 | return LY_ESYS; |
Michal Vasko | 841d1a9 | 2018-09-07 15:40:31 +0200 | [diff] [blame] | 396 | } |
| 397 | |
Radek Krejci | f345c01 | 2018-09-19 11:12:59 +0200 | [diff] [blame] | 398 | return LY_SUCCESS; |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 399 | } |
Michal Vasko | 841d1a9 | 2018-09-07 15:40:31 +0200 | [diff] [blame] | 400 | |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 401 | LY_ERR |
| 402 | ly_munmap(void *addr, size_t length) |
| 403 | { |
| 404 | if (munmap(addr, length)) { |
| 405 | return LY_ESYS; |
| 406 | } |
| 407 | return LY_SUCCESS; |
Michal Vasko | 841d1a9 | 2018-09-07 15:40:31 +0200 | [diff] [blame] | 408 | } |
Radek Krejci | 4f28eda | 2018-11-12 11:46:16 +0100 | [diff] [blame] | 409 | |
Jan Kundrát | 111ce78 | 2021-12-12 03:12:23 +0100 | [diff] [blame^] | 410 | #else |
| 411 | |
| 412 | LY_ERR |
| 413 | ly_mmap(struct ly_ctx *ctx, int fd, size_t *length, void **addr) |
| 414 | { |
| 415 | struct stat sb; |
| 416 | size_t m; |
| 417 | |
| 418 | assert(length); |
| 419 | assert(addr); |
| 420 | assert(fd >= 0); |
| 421 | |
| 422 | if (fstat(fd, &sb) == -1) { |
| 423 | LOGERR(ctx, LY_ESYS, "Failed to stat the file descriptor (%s) for the mmap().", strerror(errno)); |
| 424 | return LY_ESYS; |
| 425 | } |
| 426 | if (!S_ISREG(sb.st_mode)) { |
| 427 | LOGERR(ctx, LY_EINVAL, "File to mmap() is not a regular file."); |
| 428 | return LY_ESYS; |
| 429 | } |
| 430 | if (!sb.st_size) { |
| 431 | *addr = NULL; |
| 432 | return LY_SUCCESS; |
| 433 | } |
| 434 | /* On Windows, the mman-win32 mmap() emulation uses CreateFileMapping and MapViewOfFile, and these functions |
| 435 | * do not allow mapping more than "length of file" bytes for PROT_READ. Remapping existing mappings is not allowed, either. |
| 436 | * At that point the path of least resistance is just reading the file in as-is. */ |
| 437 | m = sb.st_size + 1; |
| 438 | char *buf = calloc(m, 1); |
| 439 | |
| 440 | if (!buf) { |
| 441 | LOGERR(ctx, LY_ESYS, "ly_mmap: malloc() failed (%s).", strerror(errno)); |
| 442 | } |
| 443 | *addr = buf; |
| 444 | *length = m; |
| 445 | |
| 446 | lseek(fd, 0, SEEK_SET); |
| 447 | ssize_t to_read = m - 1; |
| 448 | |
| 449 | while (to_read > 0) { |
| 450 | ssize_t n = read(fd, buf, to_read); |
| 451 | if (n == 0) { |
| 452 | return LY_SUCCESS; |
| 453 | } else if (n < 0) { |
| 454 | if (errno == EINTR) { |
| 455 | continue; // can I get this on Windows? |
| 456 | } |
| 457 | LOGERR(ctx, LY_ESYS, "ly_mmap: read() failed (%s).", strerror(errno)); |
| 458 | } |
| 459 | to_read -= n; |
| 460 | buf += n; |
| 461 | } |
| 462 | return LY_SUCCESS; |
| 463 | } |
| 464 | |
| 465 | LY_ERR |
| 466 | ly_munmap(void *addr, size_t length) |
| 467 | { |
| 468 | (void)length; |
| 469 | free(addr); |
| 470 | return LY_SUCCESS; |
| 471 | } |
| 472 | |
| 473 | #endif |
| 474 | |
Radek Krejci | 4f28eda | 2018-11-12 11:46:16 +0100 | [diff] [blame] | 475 | LY_ERR |
Radek Krejci | 4546aa6 | 2019-07-15 16:53:32 +0200 | [diff] [blame] | 476 | ly_strcat(char **dest, const char *format, ...) |
| 477 | { |
| 478 | va_list fp; |
| 479 | char *addition = NULL; |
| 480 | size_t len; |
| 481 | |
| 482 | va_start(fp, format); |
| 483 | len = vasprintf(&addition, format, fp); |
| 484 | len += (*dest ? strlen(*dest) : 0) + 1; |
| 485 | |
| 486 | if (*dest) { |
| 487 | *dest = ly_realloc(*dest, len); |
| 488 | if (!*dest) { |
Radek Krejci | 1cd812f | 2020-12-01 12:17:53 +0100 | [diff] [blame] | 489 | va_end(fp); |
Radek Krejci | 4546aa6 | 2019-07-15 16:53:32 +0200 | [diff] [blame] | 490 | return LY_EMEM; |
| 491 | } |
| 492 | *dest = strcat(*dest, addition); |
| 493 | free(addition); |
| 494 | } else { |
| 495 | *dest = addition; |
| 496 | } |
| 497 | |
| 498 | va_end(fp); |
| 499 | return LY_SUCCESS; |
| 500 | } |
| 501 | |
| 502 | LY_ERR |
Radek Krejci | 249973a | 2019-06-10 10:50:54 +0200 | [diff] [blame] | 503 | ly_parse_int(const char *val_str, size_t val_len, int64_t min, int64_t max, int base, int64_t *ret) |
Radek Krejci | 4f28eda | 2018-11-12 11:46:16 +0100 | [diff] [blame] | 504 | { |
Michal Vasko | b4d40d6 | 2021-05-04 11:42:44 +0200 | [diff] [blame] | 505 | LY_ERR rc = LY_SUCCESS; |
| 506 | char *ptr, *str; |
Radek Krejci | 9ea8ca1 | 2019-06-10 13:11:55 +0200 | [diff] [blame] | 507 | int64_t i; |
Radek Krejci | 4f28eda | 2018-11-12 11:46:16 +0100 | [diff] [blame] | 508 | |
Radek Krejci | 249973a | 2019-06-10 10:50:54 +0200 | [diff] [blame] | 509 | LY_CHECK_ARG_RET(NULL, val_str, val_str[0], val_len, LY_EINVAL); |
Radek Krejci | 4f28eda | 2018-11-12 11:46:16 +0100 | [diff] [blame] | 510 | |
Michal Vasko | b4d40d6 | 2021-05-04 11:42:44 +0200 | [diff] [blame] | 511 | /* duplicate the value */ |
| 512 | str = strndup(val_str, val_len); |
| 513 | LY_CHECK_RET(!str, LY_EMEM); |
Radek Krejci | 4f28eda | 2018-11-12 11:46:16 +0100 | [diff] [blame] | 514 | |
Michal Vasko | b4d40d6 | 2021-05-04 11:42:44 +0200 | [diff] [blame] | 515 | /* parse the value to avoid accessing following bytes */ |
| 516 | errno = 0; |
| 517 | i = strtoll(str, &ptr, base); |
| 518 | if (errno || (ptr == str)) { |
| 519 | /* invalid string */ |
| 520 | rc = LY_EVALID; |
Radek Krejci | 9ea8ca1 | 2019-06-10 13:11:55 +0200 | [diff] [blame] | 521 | } else if ((i < min) || (i > max)) { |
Michal Vasko | b4d40d6 | 2021-05-04 11:42:44 +0200 | [diff] [blame] | 522 | /* invalid number */ |
| 523 | rc = LY_EDENIED; |
| 524 | } else if (*ptr) { |
| 525 | while (isspace(*ptr)) { |
| 526 | ++ptr; |
Radek Krejci | 4f28eda | 2018-11-12 11:46:16 +0100 | [diff] [blame] | 527 | } |
Michal Vasko | b4d40d6 | 2021-05-04 11:42:44 +0200 | [diff] [blame] | 528 | if (*ptr) { |
| 529 | /* invalid characters after some number */ |
| 530 | rc = LY_EVALID; |
Radek Krejci | 4f28eda | 2018-11-12 11:46:16 +0100 | [diff] [blame] | 531 | } |
| 532 | } |
Radek Krejci | 9ea8ca1 | 2019-06-10 13:11:55 +0200 | [diff] [blame] | 533 | |
Michal Vasko | b4d40d6 | 2021-05-04 11:42:44 +0200 | [diff] [blame] | 534 | /* cleanup */ |
| 535 | free(str); |
| 536 | if (!rc) { |
| 537 | *ret = i; |
| 538 | } |
| 539 | return rc; |
Radek Krejci | 4f28eda | 2018-11-12 11:46:16 +0100 | [diff] [blame] | 540 | } |
| 541 | |
| 542 | LY_ERR |
Radek Krejci | 249973a | 2019-06-10 10:50:54 +0200 | [diff] [blame] | 543 | ly_parse_uint(const char *val_str, size_t val_len, uint64_t max, int base, uint64_t *ret) |
Radek Krejci | 4f28eda | 2018-11-12 11:46:16 +0100 | [diff] [blame] | 544 | { |
Michal Vasko | b4d40d6 | 2021-05-04 11:42:44 +0200 | [diff] [blame] | 545 | LY_ERR rc = LY_SUCCESS; |
| 546 | char *ptr, *str; |
Radek Krejci | 4f28eda | 2018-11-12 11:46:16 +0100 | [diff] [blame] | 547 | uint64_t u; |
| 548 | |
Michal Vasko | b4d40d6 | 2021-05-04 11:42:44 +0200 | [diff] [blame] | 549 | LY_CHECK_ARG_RET(NULL, val_str, val_str[0], val_len, LY_EINVAL); |
Radek Krejci | 4f28eda | 2018-11-12 11:46:16 +0100 | [diff] [blame] | 550 | |
Michal Vasko | b4d40d6 | 2021-05-04 11:42:44 +0200 | [diff] [blame] | 551 | /* duplicate the value to avoid accessing following bytes */ |
| 552 | str = strndup(val_str, val_len); |
| 553 | LY_CHECK_RET(!str, LY_EMEM); |
| 554 | |
| 555 | /* parse the value */ |
Radek Krejci | 4f28eda | 2018-11-12 11:46:16 +0100 | [diff] [blame] | 556 | errno = 0; |
Michal Vasko | b4d40d6 | 2021-05-04 11:42:44 +0200 | [diff] [blame] | 557 | u = strtoull(str, &ptr, base); |
| 558 | if (errno || (ptr == str)) { |
| 559 | /* invalid string */ |
| 560 | rc = LY_EVALID; |
| 561 | } else if ((u > max) || (u && (str[0] == '-'))) { |
| 562 | /* invalid number */ |
| 563 | rc = LY_EDENIED; |
| 564 | } else if (*ptr) { |
| 565 | while (isspace(*ptr)) { |
| 566 | ++ptr; |
Radek Krejci | 4f28eda | 2018-11-12 11:46:16 +0100 | [diff] [blame] | 567 | } |
Michal Vasko | b4d40d6 | 2021-05-04 11:42:44 +0200 | [diff] [blame] | 568 | if (*ptr) { |
| 569 | /* invalid characters after some number */ |
| 570 | rc = LY_EVALID; |
Radek Krejci | 4f28eda | 2018-11-12 11:46:16 +0100 | [diff] [blame] | 571 | } |
| 572 | } |
| 573 | |
Michal Vasko | b4d40d6 | 2021-05-04 11:42:44 +0200 | [diff] [blame] | 574 | /* cleanup */ |
| 575 | free(str); |
| 576 | if (!rc) { |
| 577 | *ret = u; |
| 578 | } |
| 579 | return rc; |
Radek Krejci | 4f28eda | 2018-11-12 11:46:16 +0100 | [diff] [blame] | 580 | } |
Radek Krejci | b4a4a27 | 2019-06-10 12:44:52 +0200 | [diff] [blame] | 581 | |
| 582 | /** |
| 583 | * @brief Parse an identifier. |
| 584 | * |
| 585 | * ;; An identifier MUST NOT start with (('X'|'x') ('M'|'m') ('L'|'l')) |
| 586 | * identifier = (ALPHA / "_") |
| 587 | * *(ALPHA / DIGIT / "_" / "-" / ".") |
| 588 | * |
| 589 | * @param[in,out] id Identifier to parse. When returned, it points to the first character which is not part of the identifier. |
| 590 | * @return LY_ERR value: LY_SUCCESS or LY_EINVAL in case of invalid starting character. |
| 591 | */ |
| 592 | static LY_ERR |
| 593 | lys_parse_id(const char **id) |
| 594 | { |
| 595 | assert(id && *id); |
| 596 | |
| 597 | if (!is_yangidentstartchar(**id)) { |
| 598 | return LY_EINVAL; |
| 599 | } |
| 600 | ++(*id); |
| 601 | |
| 602 | while (is_yangidentchar(**id)) { |
| 603 | ++(*id); |
| 604 | } |
| 605 | return LY_SUCCESS; |
| 606 | } |
| 607 | |
| 608 | LY_ERR |
| 609 | ly_parse_nodeid(const char **id, const char **prefix, size_t *prefix_len, const char **name, size_t *name_len) |
| 610 | { |
| 611 | assert(id && *id); |
| 612 | assert(prefix && prefix_len); |
| 613 | assert(name && name_len); |
| 614 | |
| 615 | *prefix = *id; |
| 616 | *prefix_len = 0; |
| 617 | *name = NULL; |
| 618 | *name_len = 0; |
| 619 | |
| 620 | LY_CHECK_RET(lys_parse_id(id)); |
| 621 | if (**id == ':') { |
| 622 | /* there is prefix */ |
| 623 | *prefix_len = *id - *prefix; |
| 624 | ++(*id); |
| 625 | *name = *id; |
| 626 | |
| 627 | LY_CHECK_RET(lys_parse_id(id)); |
| 628 | *name_len = *id - *name; |
| 629 | } else { |
| 630 | /* there is no prefix, so what we have as prefix now is actually the name */ |
| 631 | *name = *prefix; |
| 632 | *name_len = *id - *name; |
| 633 | *prefix = NULL; |
| 634 | } |
| 635 | |
| 636 | return LY_SUCCESS; |
| 637 | } |
| 638 | |
| 639 | LY_ERR |
Radek Krejci | 084289f | 2019-07-09 17:35:30 +0200 | [diff] [blame] | 640 | ly_parse_instance_predicate(const char **pred, size_t limit, LYD_FORMAT format, |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 641 | const char **prefix, size_t *prefix_len, const char **id, size_t *id_len, const char **value, size_t *value_len, |
| 642 | const char **errmsg) |
Radek Krejci | b4a4a27 | 2019-06-10 12:44:52 +0200 | [diff] [blame] | 643 | { |
| 644 | LY_ERR ret = LY_EVALID; |
| 645 | const char *in = *pred; |
| 646 | size_t offset = 1; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 647 | uint8_t expr = 0; /* 0 - position predicate; 1 - leaf-list-predicate; 2 - key-predicate */ |
Radek Krejci | b4a4a27 | 2019-06-10 12:44:52 +0200 | [diff] [blame] | 648 | char quot; |
| 649 | |
Radek Krejci | 4607f54 | 2020-12-01 12:18:49 +0100 | [diff] [blame] | 650 | assert(in[0] == '['); |
Radek Krejci | b4a4a27 | 2019-06-10 12:44:52 +0200 | [diff] [blame] | 651 | |
| 652 | *prefix = *id = *value = NULL; |
| 653 | *prefix_len = *id_len = *value_len = 0; |
| 654 | |
| 655 | /* leading *WSP */ |
Michal Vasko | d989ba0 | 2020-08-24 10:59:24 +0200 | [diff] [blame] | 656 | for ( ; isspace(in[offset]); offset++) {} |
Radek Krejci | b4a4a27 | 2019-06-10 12:44:52 +0200 | [diff] [blame] | 657 | |
| 658 | if (isdigit(in[offset])) { |
| 659 | /* pos: "[" *WSP positive-integer-value *WSP "]" */ |
| 660 | if (in[offset] == '0') { |
| 661 | /* zero */ |
| 662 | *errmsg = "The position predicate cannot be zero."; |
| 663 | goto error; |
| 664 | } |
| 665 | |
| 666 | /* positive-integer-value */ |
Radek Krejci | 10bfdf8 | 2019-06-10 14:08:13 +0200 | [diff] [blame] | 667 | *value = &in[offset++]; |
Michal Vasko | d989ba0 | 2020-08-24 10:59:24 +0200 | [diff] [blame] | 668 | for ( ; isdigit(in[offset]); offset++) {} |
Radek Krejci | 10bfdf8 | 2019-06-10 14:08:13 +0200 | [diff] [blame] | 669 | *value_len = &in[offset] - *value; |
Radek Krejci | b4a4a27 | 2019-06-10 12:44:52 +0200 | [diff] [blame] | 670 | |
| 671 | } else if (in[offset] == '.') { |
| 672 | /* leaf-list-predicate: "[" *WSP "." *WSP "=" *WSP quoted-string *WSP "]" */ |
| 673 | *id = &in[offset]; |
| 674 | *id_len = 1; |
| 675 | offset++; |
| 676 | expr = 1; |
Radek Krejci | 10bfdf8 | 2019-06-10 14:08:13 +0200 | [diff] [blame] | 677 | } else if (in[offset] == '-') { |
| 678 | /* typically negative value */ |
| 679 | *errmsg = "Invalid instance predicate format (negative position or invalid node-identifier)."; |
| 680 | goto error; |
Radek Krejci | b4a4a27 | 2019-06-10 12:44:52 +0200 | [diff] [blame] | 681 | } else { |
| 682 | /* key-predicate: "[" *WSP node-identifier *WSP "=" *WSP quoted-string *WSP "]" */ |
| 683 | in = &in[offset]; |
| 684 | if (ly_parse_nodeid(&in, prefix, prefix_len, id, id_len)) { |
| 685 | *errmsg = "Invalid node-identifier."; |
| 686 | goto error; |
| 687 | } |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 688 | if ((format == LYD_XML) && !(*prefix)) { |
Radek Krejci | 084289f | 2019-07-09 17:35:30 +0200 | [diff] [blame] | 689 | /* all node names MUST be qualified with explicit namespace prefix */ |
| 690 | *errmsg = "Missing prefix of a node name."; |
| 691 | goto error; |
| 692 | } |
Radek Krejci | b4a4a27 | 2019-06-10 12:44:52 +0200 | [diff] [blame] | 693 | offset = in - *pred; |
| 694 | in = *pred; |
Radek Krejci | 10bfdf8 | 2019-06-10 14:08:13 +0200 | [diff] [blame] | 695 | expr = 2; |
Radek Krejci | b4a4a27 | 2019-06-10 12:44:52 +0200 | [diff] [blame] | 696 | } |
| 697 | |
| 698 | if (expr) { |
| 699 | /* *WSP "=" *WSP quoted-string *WSP "]" */ |
Michal Vasko | d989ba0 | 2020-08-24 10:59:24 +0200 | [diff] [blame] | 700 | for ( ; isspace(in[offset]); offset++) {} |
Radek Krejci | b4a4a27 | 2019-06-10 12:44:52 +0200 | [diff] [blame] | 701 | |
| 702 | if (in[offset] != '=') { |
Radek Krejci | 10bfdf8 | 2019-06-10 14:08:13 +0200 | [diff] [blame] | 703 | if (expr == 1) { |
| 704 | *errmsg = "Unexpected character instead of \'=\' in leaf-list-predicate."; |
| 705 | } else { /* 2 */ |
| 706 | *errmsg = "Unexpected character instead of \'=\' in key-predicate."; |
| 707 | } |
Radek Krejci | b4a4a27 | 2019-06-10 12:44:52 +0200 | [diff] [blame] | 708 | goto error; |
| 709 | } |
| 710 | offset++; |
Michal Vasko | d989ba0 | 2020-08-24 10:59:24 +0200 | [diff] [blame] | 711 | for ( ; isspace(in[offset]); offset++) {} |
Radek Krejci | b4a4a27 | 2019-06-10 12:44:52 +0200 | [diff] [blame] | 712 | |
| 713 | /* quoted-string */ |
| 714 | quot = in[offset++]; |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 715 | if ((quot != '\'') && (quot != '\"')) { |
Radek Krejci | b4a4a27 | 2019-06-10 12:44:52 +0200 | [diff] [blame] | 716 | *errmsg = "String value is not quoted."; |
| 717 | goto error; |
| 718 | } |
| 719 | *value = &in[offset]; |
Michal Vasko | d989ba0 | 2020-08-24 10:59:24 +0200 | [diff] [blame] | 720 | for ( ; offset < limit && (in[offset] != quot || (offset && in[offset - 1] == '\\')); offset++) {} |
Radek Krejci | 10bfdf8 | 2019-06-10 14:08:13 +0200 | [diff] [blame] | 721 | if (in[offset] == quot) { |
| 722 | *value_len = &in[offset] - *value; |
| 723 | offset++; |
| 724 | } else { |
| 725 | *errmsg = "Value is not terminated quoted-string."; |
| 726 | goto error; |
| 727 | } |
Radek Krejci | b4a4a27 | 2019-06-10 12:44:52 +0200 | [diff] [blame] | 728 | } |
| 729 | |
| 730 | /* *WSP "]" */ |
Michal Vasko | d989ba0 | 2020-08-24 10:59:24 +0200 | [diff] [blame] | 731 | for ( ; isspace(in[offset]); offset++) {} |
Radek Krejci | b4a4a27 | 2019-06-10 12:44:52 +0200 | [diff] [blame] | 732 | if (in[offset] != ']') { |
Radek Krejci | 10bfdf8 | 2019-06-10 14:08:13 +0200 | [diff] [blame] | 733 | if (expr == 0) { |
| 734 | *errmsg = "Predicate (pos) is not terminated by \']\' character."; |
| 735 | } else if (expr == 1) { |
| 736 | *errmsg = "Predicate (leaf-list-predicate) is not terminated by \']\' character."; |
| 737 | } else { /* 2 */ |
| 738 | *errmsg = "Predicate (key-predicate) is not terminated by \']\' character."; |
| 739 | } |
Radek Krejci | b4a4a27 | 2019-06-10 12:44:52 +0200 | [diff] [blame] | 740 | goto error; |
| 741 | } |
Radek Krejci | 10bfdf8 | 2019-06-10 14:08:13 +0200 | [diff] [blame] | 742 | offset++; |
Radek Krejci | b4a4a27 | 2019-06-10 12:44:52 +0200 | [diff] [blame] | 743 | |
Radek Krejci | 10bfdf8 | 2019-06-10 14:08:13 +0200 | [diff] [blame] | 744 | if (offset <= limit) { |
| 745 | *pred = &in[offset]; |
Radek Krejci | b4a4a27 | 2019-06-10 12:44:52 +0200 | [diff] [blame] | 746 | return LY_SUCCESS; |
| 747 | } |
| 748 | |
| 749 | /* we read after the limit */ |
| 750 | *errmsg = "Predicate is incomplete."; |
| 751 | *prefix = *id = *value = NULL; |
| 752 | *prefix_len = *id_len = *value_len = 0; |
| 753 | offset = limit; |
| 754 | ret = LY_EINVAL; |
| 755 | |
| 756 | error: |
| 757 | *pred = &in[offset]; |
| 758 | return ret; |
| 759 | } |