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