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