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> |
| 22 | #include <stdarg.h> |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 23 | #include <stdio.h> |
Radek Krejci | 4546aa6 | 2019-07-15 16:53:32 +0200 | [diff] [blame] | 24 | #include <stdlib.h> |
Michal Vasko | 841d1a9 | 2018-09-07 15:40:31 +0200 | [diff] [blame] | 25 | #include <string.h> |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 26 | #include <sys/mman.h> |
| 27 | #include <sys/stat.h> |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 28 | #include <unistd.h> |
Michal Vasko | 841d1a9 | 2018-09-07 15:40:31 +0200 | [diff] [blame] | 29 | |
Radek Krejci | aa45bda | 2020-07-20 07:43:38 +0200 | [diff] [blame] | 30 | #include "compat.h" |
Radek Krejci | b4a4a27 | 2019-06-10 12:44:52 +0200 | [diff] [blame] | 31 | #include "tree_schema_internal.h" |
Michal Vasko | 1324b6c | 2018-09-07 11:16:23 +0200 | [diff] [blame] | 32 | |
| 33 | void * |
| 34 | ly_realloc(void *ptr, size_t size) |
| 35 | { |
| 36 | void *new_mem; |
| 37 | |
| 38 | new_mem = realloc(ptr, size); |
| 39 | if (!new_mem) { |
| 40 | free(ptr); |
| 41 | } |
| 42 | |
| 43 | return new_mem; |
| 44 | } |
Michal Vasko | 841d1a9 | 2018-09-07 15:40:31 +0200 | [diff] [blame] | 45 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 46 | char * |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 47 | ly_strnchr(const char *s, int c, size_t len) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 48 | { |
Michal Vasko | d989ba0 | 2020-08-24 10:59:24 +0200 | [diff] [blame] | 49 | for ( ; *s != (char)c; ++s, --len) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 50 | if ((*s == '\0') || (!len)) { |
| 51 | return NULL; |
| 52 | } |
| 53 | } |
| 54 | return (char *)s; |
| 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 | |
Radek Krejci | b416be6 | 2018-10-01 14:51:45 +0200 | [diff] [blame] | 69 | LY_ERR |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 70 | 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] | 71 | { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 72 | uint32_t c, aux; |
| 73 | size_t len; |
Radek Krejci | b416be6 | 2018-10-01 14:51:45 +0200 | [diff] [blame] | 74 | |
Radek Krejci | cc6a45c | 2019-05-13 10:16:14 +0200 | [diff] [blame] | 75 | if (bytes_read) { |
| 76 | (*bytes_read) = 0; |
| 77 | } |
| 78 | |
Radek Krejci | b416be6 | 2018-10-01 14:51:45 +0200 | [diff] [blame] | 79 | c = (*input)[0]; |
| 80 | LY_CHECK_RET(!c, LY_EINVAL); |
| 81 | |
| 82 | if (!(c & 0x80)) { |
| 83 | /* one byte character */ |
| 84 | len = 1; |
| 85 | |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 86 | if ((c < 0x20) && (c != 0x9) && (c != 0xa) && (c != 0xd)) { |
Radek Krejci | b416be6 | 2018-10-01 14:51:45 +0200 | [diff] [blame] | 87 | return LY_EINVAL; |
| 88 | } |
| 89 | } else if ((c & 0xe0) == 0xc0) { |
| 90 | /* two bytes character */ |
| 91 | len = 2; |
| 92 | |
| 93 | aux = (*input)[1]; |
| 94 | if ((aux & 0xc0) != 0x80) { |
| 95 | return LY_EINVAL; |
| 96 | } |
| 97 | c = ((c & 0x1f) << 6) | (aux & 0x3f); |
| 98 | |
| 99 | if (c < 0x80) { |
| 100 | return LY_EINVAL; |
| 101 | } |
| 102 | } else if ((c & 0xf0) == 0xe0) { |
| 103 | /* three bytes character */ |
| 104 | len = 3; |
| 105 | |
| 106 | c &= 0x0f; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 107 | for (uint64_t i = 1; i <= 2; i++) { |
Radek Krejci | b416be6 | 2018-10-01 14:51:45 +0200 | [diff] [blame] | 108 | aux = (*input)[i]; |
| 109 | if ((aux & 0xc0) != 0x80) { |
| 110 | return LY_EINVAL; |
| 111 | } |
| 112 | |
| 113 | c = (c << 6) | (aux & 0x3f); |
| 114 | } |
| 115 | |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 116 | if ((c < 0x800) || ((c > 0xd7ff) && (c < 0xe000)) || (c > 0xfffd)) { |
Radek Krejci | b416be6 | 2018-10-01 14:51:45 +0200 | [diff] [blame] | 117 | return LY_EINVAL; |
| 118 | } |
| 119 | } else if ((c & 0xf8) == 0xf0) { |
| 120 | /* four bytes character */ |
| 121 | len = 4; |
| 122 | |
| 123 | c &= 0x07; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 124 | for (uint64_t i = 1; i <= 3; i++) { |
Radek Krejci | b416be6 | 2018-10-01 14:51:45 +0200 | [diff] [blame] | 125 | aux = (*input)[i]; |
| 126 | if ((aux & 0xc0) != 0x80) { |
| 127 | return LY_EINVAL; |
| 128 | } |
| 129 | |
| 130 | c = (c << 6) | (aux & 0x3f); |
| 131 | } |
| 132 | |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 133 | if ((c < 0x1000) || (c > 0x10ffff)) { |
Radek Krejci | b416be6 | 2018-10-01 14:51:45 +0200 | [diff] [blame] | 134 | return LY_EINVAL; |
| 135 | } |
| 136 | } else { |
| 137 | return LY_EINVAL; |
| 138 | } |
| 139 | |
| 140 | (*utf8_char) = c; |
| 141 | (*input) += len; |
| 142 | if (bytes_read) { |
| 143 | (*bytes_read) = len; |
| 144 | } |
| 145 | return LY_SUCCESS; |
| 146 | } |
| 147 | |
Radek Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 148 | LY_ERR |
| 149 | ly_pututf8(char *dst, uint32_t value, size_t *bytes_written) |
| 150 | { |
| 151 | if (value < 0x80) { |
| 152 | /* one byte character */ |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 153 | if ((value < 0x20) && |
| 154 | (value != 0x09) && |
| 155 | (value != 0x0a) && |
| 156 | (value != 0x0d)) { |
Radek Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 157 | return LY_EINVAL; |
| 158 | } |
| 159 | |
| 160 | dst[0] = value; |
| 161 | (*bytes_written) = 1; |
| 162 | } else if (value < 0x800) { |
| 163 | /* two bytes character */ |
| 164 | dst[0] = 0xc0 | (value >> 6); |
| 165 | dst[1] = 0x80 | (value & 0x3f); |
| 166 | (*bytes_written) = 2; |
| 167 | } else if (value < 0xfffe) { |
| 168 | /* three bytes character */ |
| 169 | if (((value & 0xf800) == 0xd800) || |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 170 | ((value >= 0xfdd0) && (value <= 0xfdef))) { |
Radek Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 171 | /* exclude surrogate blocks %xD800-DFFF */ |
| 172 | /* exclude noncharacters %xFDD0-FDEF */ |
| 173 | return LY_EINVAL; |
| 174 | } |
| 175 | |
| 176 | dst[0] = 0xe0 | (value >> 12); |
| 177 | dst[1] = 0x80 | ((value >> 6) & 0x3f); |
| 178 | dst[2] = 0x80 | (value & 0x3f); |
| 179 | |
| 180 | (*bytes_written) = 3; |
| 181 | } else if (value < 0x10fffe) { |
| 182 | if ((value & 0xffe) == 0xffe) { |
| 183 | /* exclude noncharacters %xFFFE-FFFF, %x1FFFE-1FFFF, %x2FFFE-2FFFF, %x3FFFE-3FFFF, %x4FFFE-4FFFF, |
| 184 | * %x5FFFE-5FFFF, %x6FFFE-6FFFF, %x7FFFE-7FFFF, %x8FFFE-8FFFF, %x9FFFE-9FFFF, %xAFFFE-AFFFF, |
| 185 | * %xBFFFE-BFFFF, %xCFFFE-CFFFF, %xDFFFE-DFFFF, %xEFFFE-EFFFF, %xFFFFE-FFFFF, %x10FFFE-10FFFF */ |
| 186 | return LY_EINVAL; |
| 187 | } |
| 188 | /* four bytes character */ |
| 189 | dst[0] = 0xf0 | (value >> 18); |
| 190 | dst[1] = 0x80 | ((value >> 12) & 0x3f); |
| 191 | dst[2] = 0x80 | ((value >> 6) & 0x3f); |
| 192 | dst[3] = 0x80 | (value & 0x3f); |
| 193 | |
| 194 | (*bytes_written) = 4; |
| 195 | } else { |
| 196 | return LY_EINVAL; |
| 197 | } |
| 198 | return LY_SUCCESS; |
| 199 | } |
| 200 | |
Radek Krejci | 76c9801 | 2019-08-14 11:23:24 +0200 | [diff] [blame] | 201 | /** |
| 202 | * @brief Static table of the UTF8 characters lengths according to their first byte. |
| 203 | */ |
Radek Krejci | f6a1100 | 2020-08-21 13:29:07 +0200 | [diff] [blame] | 204 | static const unsigned char utf8_char_length_table[] = { |
Radek Krejci | 76c9801 | 2019-08-14 11:23:24 +0200 | [diff] [blame] | 205 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 206 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 207 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 208 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 209 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 210 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 211 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 212 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 213 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 214 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 215 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 216 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 217 | 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, |
| 218 | 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, |
| 219 | 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, |
| 220 | 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 1, 1 |
| 221 | }; |
| 222 | |
| 223 | size_t |
| 224 | ly_utf8len(const char *str, size_t bytes) |
| 225 | { |
Radek Krejci | 1e008d2 | 2020-08-17 11:37:37 +0200 | [diff] [blame] | 226 | size_t len = 0; |
| 227 | const char *ptr = str; |
Radek Krejci | 76c9801 | 2019-08-14 11:23:24 +0200 | [diff] [blame] | 228 | |
Michal Vasko | d989ba0 | 2020-08-24 10:59:24 +0200 | [diff] [blame] | 229 | while (*ptr && (size_t)(ptr - str) < bytes) { |
Radek Krejci | 1e008d2 | 2020-08-17 11:37:37 +0200 | [diff] [blame] | 230 | ++len; |
| 231 | ptr += utf8_char_length_table[((unsigned char)(*ptr))]; |
| 232 | } |
Radek Krejci | 76c9801 | 2019-08-14 11:23:24 +0200 | [diff] [blame] | 233 | return len; |
| 234 | } |
| 235 | |
Radek Krejci | d972c25 | 2018-09-25 13:23:39 +0200 | [diff] [blame] | 236 | size_t |
| 237 | LY_VCODE_INSTREXP_len(const char *str) |
| 238 | { |
| 239 | size_t len = 0; |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 240 | |
Radek Krejci | d972c25 | 2018-09-25 13:23:39 +0200 | [diff] [blame] | 241 | if (!str) { |
| 242 | return len; |
| 243 | } else if (!str[0]) { |
| 244 | return 1; |
| 245 | } |
Radek Krejci | 1e008d2 | 2020-08-17 11:37:37 +0200 | [diff] [blame] | 246 | for (len = 1; len < LY_VCODE_INSTREXP_MAXLEN && str[len]; ++len) {} |
Radek Krejci | d972c25 | 2018-09-25 13:23:39 +0200 | [diff] [blame] | 247 | return len; |
| 248 | } |
| 249 | |
Radek Krejci | f345c01 | 2018-09-19 11:12:59 +0200 | [diff] [blame] | 250 | LY_ERR |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 251 | 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] | 252 | { |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 253 | struct stat sb; |
| 254 | long pagesize; |
| 255 | size_t m; |
Michal Vasko | 841d1a9 | 2018-09-07 15:40:31 +0200 | [diff] [blame] | 256 | |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 257 | assert(length); |
| 258 | assert(addr); |
| 259 | assert(fd >= 0); |
Michal Vasko | 841d1a9 | 2018-09-07 15:40:31 +0200 | [diff] [blame] | 260 | |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 261 | if (fstat(fd, &sb) == -1) { |
| 262 | LOGERR(ctx, LY_ESYS, "Failed to stat the file descriptor (%s) for the mmap().", strerror(errno)); |
| 263 | return LY_ESYS; |
Michal Vasko | 841d1a9 | 2018-09-07 15:40:31 +0200 | [diff] [blame] | 264 | } |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 265 | if (!S_ISREG(sb.st_mode)) { |
| 266 | LOGERR(ctx, LY_EINVAL, "File to mmap() is not a regular file."); |
| 267 | return LY_ESYS; |
Michal Vasko | 841d1a9 | 2018-09-07 15:40:31 +0200 | [diff] [blame] | 268 | } |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 269 | if (!sb.st_size) { |
| 270 | *addr = NULL; |
| 271 | return LY_SUCCESS; |
| 272 | } |
| 273 | pagesize = sysconf(_SC_PAGESIZE); |
| 274 | |
| 275 | m = sb.st_size % pagesize; |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 276 | if (m && (pagesize - m >= 1)) { |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 277 | /* there will be enough space (at least 1 byte) after the file content mapping to provide zeroed NULL-termination byte */ |
| 278 | *length = sb.st_size + 1; |
| 279 | *addr = mmap(NULL, *length, PROT_READ, MAP_PRIVATE, fd, 0); |
| 280 | } else { |
| 281 | /* there will not be enough bytes after the file content mapping for the additional bytes and some of them |
| 282 | * would overflow into another page that would not be zerroed and any access into it would generate SIGBUS. |
| 283 | * Therefore we have to do the following hack with double mapping. First, the required number of bytes |
| 284 | * (including the additinal bytes) is required as anonymous and thus they will be really provided (actually more |
| 285 | * because of using whole pages) and also initialized by zeros. Then, the file is mapped to the same address |
| 286 | * where the anonymous mapping starts. */ |
| 287 | *length = sb.st_size + pagesize; |
| 288 | *addr = mmap(NULL, *length, PROT_READ, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); |
| 289 | *addr = mmap(*addr, sb.st_size, PROT_READ, MAP_PRIVATE | MAP_FIXED, fd, 0); |
| 290 | } |
| 291 | if (*addr == MAP_FAILED) { |
| 292 | LOGERR(ctx, LY_ESYS, "mmap() failed (%s).", strerror(errno)); |
| 293 | return LY_ESYS; |
Michal Vasko | 841d1a9 | 2018-09-07 15:40:31 +0200 | [diff] [blame] | 294 | } |
| 295 | |
Radek Krejci | f345c01 | 2018-09-19 11:12:59 +0200 | [diff] [blame] | 296 | return LY_SUCCESS; |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 297 | } |
Michal Vasko | 841d1a9 | 2018-09-07 15:40:31 +0200 | [diff] [blame] | 298 | |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 299 | LY_ERR |
| 300 | ly_munmap(void *addr, size_t length) |
| 301 | { |
| 302 | if (munmap(addr, length)) { |
| 303 | return LY_ESYS; |
| 304 | } |
| 305 | return LY_SUCCESS; |
Michal Vasko | 841d1a9 | 2018-09-07 15:40:31 +0200 | [diff] [blame] | 306 | } |
Radek Krejci | 4f28eda | 2018-11-12 11:46:16 +0100 | [diff] [blame] | 307 | |
| 308 | LY_ERR |
Radek Krejci | 4546aa6 | 2019-07-15 16:53:32 +0200 | [diff] [blame] | 309 | ly_strcat(char **dest, const char *format, ...) |
| 310 | { |
| 311 | va_list fp; |
| 312 | char *addition = NULL; |
| 313 | size_t len; |
| 314 | |
| 315 | va_start(fp, format); |
| 316 | len = vasprintf(&addition, format, fp); |
| 317 | len += (*dest ? strlen(*dest) : 0) + 1; |
| 318 | |
| 319 | if (*dest) { |
| 320 | *dest = ly_realloc(*dest, len); |
| 321 | if (!*dest) { |
Radek Krejci | 1cd812f | 2020-12-01 12:17:53 +0100 | [diff] [blame] | 322 | va_end(fp); |
Radek Krejci | 4546aa6 | 2019-07-15 16:53:32 +0200 | [diff] [blame] | 323 | return LY_EMEM; |
| 324 | } |
| 325 | *dest = strcat(*dest, addition); |
| 326 | free(addition); |
| 327 | } else { |
| 328 | *dest = addition; |
| 329 | } |
| 330 | |
| 331 | va_end(fp); |
| 332 | return LY_SUCCESS; |
| 333 | } |
| 334 | |
| 335 | LY_ERR |
Radek Krejci | 249973a | 2019-06-10 10:50:54 +0200 | [diff] [blame] | 336 | 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] | 337 | { |
| 338 | char *strptr; |
Radek Krejci | 9ea8ca1 | 2019-06-10 13:11:55 +0200 | [diff] [blame] | 339 | int64_t i; |
Radek Krejci | 4f28eda | 2018-11-12 11:46:16 +0100 | [diff] [blame] | 340 | |
Radek Krejci | 249973a | 2019-06-10 10:50:54 +0200 | [diff] [blame] | 341 | 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] | 342 | |
| 343 | /* convert to 64-bit integer, all the redundant characters are handled */ |
| 344 | errno = 0; |
| 345 | strptr = NULL; |
| 346 | |
| 347 | /* parse the value */ |
Radek Krejci | 9ea8ca1 | 2019-06-10 13:11:55 +0200 | [diff] [blame] | 348 | i = strtoll(val_str, &strptr, base); |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 349 | if (errno || (strptr == val_str)) { |
Radek Krejci | 4f28eda | 2018-11-12 11:46:16 +0100 | [diff] [blame] | 350 | return LY_EVALID; |
Radek Krejci | 9ea8ca1 | 2019-06-10 13:11:55 +0200 | [diff] [blame] | 351 | } else if ((i < min) || (i > max)) { |
Radek Krejci | 4f28eda | 2018-11-12 11:46:16 +0100 | [diff] [blame] | 352 | return LY_EDENIED; |
| 353 | } else if (strptr && *strptr) { |
| 354 | while (isspace(*strptr)) { |
| 355 | ++strptr; |
| 356 | } |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 357 | if (*strptr && (strptr < val_str + val_len)) { |
Radek Krejci | 4f28eda | 2018-11-12 11:46:16 +0100 | [diff] [blame] | 358 | return LY_EVALID; |
| 359 | } |
| 360 | } |
Radek Krejci | 9ea8ca1 | 2019-06-10 13:11:55 +0200 | [diff] [blame] | 361 | |
| 362 | *ret = i; |
Radek Krejci | 4f28eda | 2018-11-12 11:46:16 +0100 | [diff] [blame] | 363 | return LY_SUCCESS; |
| 364 | } |
| 365 | |
| 366 | LY_ERR |
Radek Krejci | 249973a | 2019-06-10 10:50:54 +0200 | [diff] [blame] | 367 | 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] | 368 | { |
| 369 | char *strptr; |
| 370 | uint64_t u; |
| 371 | |
| 372 | LY_CHECK_ARG_RET(NULL, val_str, val_str[0], LY_EINVAL); |
| 373 | |
| 374 | errno = 0; |
| 375 | strptr = NULL; |
| 376 | u = strtoull(val_str, &strptr, base); |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 377 | if (errno || (strptr == val_str)) { |
Radek Krejci | 4f28eda | 2018-11-12 11:46:16 +0100 | [diff] [blame] | 378 | return LY_EVALID; |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 379 | } else if ((u > max) || (u && (val_str[0] == '-'))) { |
Radek Krejci | 4f28eda | 2018-11-12 11:46:16 +0100 | [diff] [blame] | 380 | return LY_EDENIED; |
| 381 | } else if (strptr && *strptr) { |
| 382 | while (isspace(*strptr)) { |
| 383 | ++strptr; |
| 384 | } |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 385 | if (*strptr && (strptr < val_str + val_len)) { |
Radek Krejci | 4f28eda | 2018-11-12 11:46:16 +0100 | [diff] [blame] | 386 | return LY_EVALID; |
| 387 | } |
| 388 | } |
| 389 | |
| 390 | *ret = u; |
| 391 | return LY_SUCCESS; |
| 392 | } |
Radek Krejci | b4a4a27 | 2019-06-10 12:44:52 +0200 | [diff] [blame] | 393 | |
| 394 | /** |
| 395 | * @brief Parse an identifier. |
| 396 | * |
| 397 | * ;; An identifier MUST NOT start with (('X'|'x') ('M'|'m') ('L'|'l')) |
| 398 | * identifier = (ALPHA / "_") |
| 399 | * *(ALPHA / DIGIT / "_" / "-" / ".") |
| 400 | * |
| 401 | * @param[in,out] id Identifier to parse. When returned, it points to the first character which is not part of the identifier. |
| 402 | * @return LY_ERR value: LY_SUCCESS or LY_EINVAL in case of invalid starting character. |
| 403 | */ |
| 404 | static LY_ERR |
| 405 | lys_parse_id(const char **id) |
| 406 | { |
| 407 | assert(id && *id); |
| 408 | |
| 409 | if (!is_yangidentstartchar(**id)) { |
| 410 | return LY_EINVAL; |
| 411 | } |
| 412 | ++(*id); |
| 413 | |
| 414 | while (is_yangidentchar(**id)) { |
| 415 | ++(*id); |
| 416 | } |
| 417 | return LY_SUCCESS; |
| 418 | } |
| 419 | |
| 420 | LY_ERR |
| 421 | ly_parse_nodeid(const char **id, const char **prefix, size_t *prefix_len, const char **name, size_t *name_len) |
| 422 | { |
| 423 | assert(id && *id); |
| 424 | assert(prefix && prefix_len); |
| 425 | assert(name && name_len); |
| 426 | |
| 427 | *prefix = *id; |
| 428 | *prefix_len = 0; |
| 429 | *name = NULL; |
| 430 | *name_len = 0; |
| 431 | |
| 432 | LY_CHECK_RET(lys_parse_id(id)); |
| 433 | if (**id == ':') { |
| 434 | /* there is prefix */ |
| 435 | *prefix_len = *id - *prefix; |
| 436 | ++(*id); |
| 437 | *name = *id; |
| 438 | |
| 439 | LY_CHECK_RET(lys_parse_id(id)); |
| 440 | *name_len = *id - *name; |
| 441 | } else { |
| 442 | /* there is no prefix, so what we have as prefix now is actually the name */ |
| 443 | *name = *prefix; |
| 444 | *name_len = *id - *name; |
| 445 | *prefix = NULL; |
| 446 | } |
| 447 | |
| 448 | return LY_SUCCESS; |
| 449 | } |
| 450 | |
| 451 | LY_ERR |
Radek Krejci | 084289f | 2019-07-09 17:35:30 +0200 | [diff] [blame] | 452 | 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] | 453 | const char **prefix, size_t *prefix_len, const char **id, size_t *id_len, const char **value, size_t *value_len, |
| 454 | const char **errmsg) |
Radek Krejci | b4a4a27 | 2019-06-10 12:44:52 +0200 | [diff] [blame] | 455 | { |
| 456 | LY_ERR ret = LY_EVALID; |
| 457 | const char *in = *pred; |
| 458 | size_t offset = 1; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 459 | 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] | 460 | char quot; |
| 461 | |
Radek Krejci | 4607f54 | 2020-12-01 12:18:49 +0100 | [diff] [blame] | 462 | assert(in[0] == '['); |
Radek Krejci | b4a4a27 | 2019-06-10 12:44:52 +0200 | [diff] [blame] | 463 | |
| 464 | *prefix = *id = *value = NULL; |
| 465 | *prefix_len = *id_len = *value_len = 0; |
| 466 | |
| 467 | /* leading *WSP */ |
Michal Vasko | d989ba0 | 2020-08-24 10:59:24 +0200 | [diff] [blame] | 468 | for ( ; isspace(in[offset]); offset++) {} |
Radek Krejci | b4a4a27 | 2019-06-10 12:44:52 +0200 | [diff] [blame] | 469 | |
| 470 | if (isdigit(in[offset])) { |
| 471 | /* pos: "[" *WSP positive-integer-value *WSP "]" */ |
| 472 | if (in[offset] == '0') { |
| 473 | /* zero */ |
| 474 | *errmsg = "The position predicate cannot be zero."; |
| 475 | goto error; |
| 476 | } |
| 477 | |
| 478 | /* positive-integer-value */ |
Radek Krejci | 10bfdf8 | 2019-06-10 14:08:13 +0200 | [diff] [blame] | 479 | *value = &in[offset++]; |
Michal Vasko | d989ba0 | 2020-08-24 10:59:24 +0200 | [diff] [blame] | 480 | for ( ; isdigit(in[offset]); offset++) {} |
Radek Krejci | 10bfdf8 | 2019-06-10 14:08:13 +0200 | [diff] [blame] | 481 | *value_len = &in[offset] - *value; |
Radek Krejci | b4a4a27 | 2019-06-10 12:44:52 +0200 | [diff] [blame] | 482 | |
| 483 | } else if (in[offset] == '.') { |
| 484 | /* leaf-list-predicate: "[" *WSP "." *WSP "=" *WSP quoted-string *WSP "]" */ |
| 485 | *id = &in[offset]; |
| 486 | *id_len = 1; |
| 487 | offset++; |
| 488 | expr = 1; |
Radek Krejci | 10bfdf8 | 2019-06-10 14:08:13 +0200 | [diff] [blame] | 489 | } else if (in[offset] == '-') { |
| 490 | /* typically negative value */ |
| 491 | *errmsg = "Invalid instance predicate format (negative position or invalid node-identifier)."; |
| 492 | goto error; |
Radek Krejci | b4a4a27 | 2019-06-10 12:44:52 +0200 | [diff] [blame] | 493 | } else { |
| 494 | /* key-predicate: "[" *WSP node-identifier *WSP "=" *WSP quoted-string *WSP "]" */ |
| 495 | in = &in[offset]; |
| 496 | if (ly_parse_nodeid(&in, prefix, prefix_len, id, id_len)) { |
| 497 | *errmsg = "Invalid node-identifier."; |
| 498 | goto error; |
| 499 | } |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 500 | if ((format == LYD_XML) && !(*prefix)) { |
Radek Krejci | 084289f | 2019-07-09 17:35:30 +0200 | [diff] [blame] | 501 | /* all node names MUST be qualified with explicit namespace prefix */ |
| 502 | *errmsg = "Missing prefix of a node name."; |
| 503 | goto error; |
| 504 | } |
Radek Krejci | b4a4a27 | 2019-06-10 12:44:52 +0200 | [diff] [blame] | 505 | offset = in - *pred; |
| 506 | in = *pred; |
Radek Krejci | 10bfdf8 | 2019-06-10 14:08:13 +0200 | [diff] [blame] | 507 | expr = 2; |
Radek Krejci | b4a4a27 | 2019-06-10 12:44:52 +0200 | [diff] [blame] | 508 | } |
| 509 | |
| 510 | if (expr) { |
| 511 | /* *WSP "=" *WSP quoted-string *WSP "]" */ |
Michal Vasko | d989ba0 | 2020-08-24 10:59:24 +0200 | [diff] [blame] | 512 | for ( ; isspace(in[offset]); offset++) {} |
Radek Krejci | b4a4a27 | 2019-06-10 12:44:52 +0200 | [diff] [blame] | 513 | |
| 514 | if (in[offset] != '=') { |
Radek Krejci | 10bfdf8 | 2019-06-10 14:08:13 +0200 | [diff] [blame] | 515 | if (expr == 1) { |
| 516 | *errmsg = "Unexpected character instead of \'=\' in leaf-list-predicate."; |
| 517 | } else { /* 2 */ |
| 518 | *errmsg = "Unexpected character instead of \'=\' in key-predicate."; |
| 519 | } |
Radek Krejci | b4a4a27 | 2019-06-10 12:44:52 +0200 | [diff] [blame] | 520 | goto error; |
| 521 | } |
| 522 | offset++; |
Michal Vasko | d989ba0 | 2020-08-24 10:59:24 +0200 | [diff] [blame] | 523 | for ( ; isspace(in[offset]); offset++) {} |
Radek Krejci | b4a4a27 | 2019-06-10 12:44:52 +0200 | [diff] [blame] | 524 | |
| 525 | /* quoted-string */ |
| 526 | quot = in[offset++]; |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 527 | if ((quot != '\'') && (quot != '\"')) { |
Radek Krejci | b4a4a27 | 2019-06-10 12:44:52 +0200 | [diff] [blame] | 528 | *errmsg = "String value is not quoted."; |
| 529 | goto error; |
| 530 | } |
| 531 | *value = &in[offset]; |
Michal Vasko | d989ba0 | 2020-08-24 10:59:24 +0200 | [diff] [blame] | 532 | for ( ; offset < limit && (in[offset] != quot || (offset && in[offset - 1] == '\\')); offset++) {} |
Radek Krejci | 10bfdf8 | 2019-06-10 14:08:13 +0200 | [diff] [blame] | 533 | if (in[offset] == quot) { |
| 534 | *value_len = &in[offset] - *value; |
| 535 | offset++; |
| 536 | } else { |
| 537 | *errmsg = "Value is not terminated quoted-string."; |
| 538 | goto error; |
| 539 | } |
Radek Krejci | b4a4a27 | 2019-06-10 12:44:52 +0200 | [diff] [blame] | 540 | } |
| 541 | |
| 542 | /* *WSP "]" */ |
Michal Vasko | d989ba0 | 2020-08-24 10:59:24 +0200 | [diff] [blame] | 543 | for ( ; isspace(in[offset]); offset++) {} |
Radek Krejci | b4a4a27 | 2019-06-10 12:44:52 +0200 | [diff] [blame] | 544 | if (in[offset] != ']') { |
Radek Krejci | 10bfdf8 | 2019-06-10 14:08:13 +0200 | [diff] [blame] | 545 | if (expr == 0) { |
| 546 | *errmsg = "Predicate (pos) is not terminated by \']\' character."; |
| 547 | } else if (expr == 1) { |
| 548 | *errmsg = "Predicate (leaf-list-predicate) is not terminated by \']\' character."; |
| 549 | } else { /* 2 */ |
| 550 | *errmsg = "Predicate (key-predicate) is not terminated by \']\' character."; |
| 551 | } |
Radek Krejci | b4a4a27 | 2019-06-10 12:44:52 +0200 | [diff] [blame] | 552 | goto error; |
| 553 | } |
Radek Krejci | 10bfdf8 | 2019-06-10 14:08:13 +0200 | [diff] [blame] | 554 | offset++; |
Radek Krejci | b4a4a27 | 2019-06-10 12:44:52 +0200 | [diff] [blame] | 555 | |
Radek Krejci | 10bfdf8 | 2019-06-10 14:08:13 +0200 | [diff] [blame] | 556 | if (offset <= limit) { |
| 557 | *pred = &in[offset]; |
Radek Krejci | b4a4a27 | 2019-06-10 12:44:52 +0200 | [diff] [blame] | 558 | return LY_SUCCESS; |
| 559 | } |
| 560 | |
| 561 | /* we read after the limit */ |
| 562 | *errmsg = "Predicate is incomplete."; |
| 563 | *prefix = *id = *value = NULL; |
| 564 | *prefix_len = *id_len = *value_len = 0; |
| 565 | offset = limit; |
| 566 | ret = LY_EINVAL; |
| 567 | |
| 568 | error: |
| 569 | *pred = &in[offset]; |
| 570 | return ret; |
| 571 | } |