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