Michal Vasko | 1324b6c | 2018-09-07 11:16:23 +0200 | [diff] [blame] | 1 | /** |
Michal Vasko | 8f702ee | 2024-02-20 15:44:24 +0100 | [diff] [blame] | 2 | * @file ly_common.c |
Michal Vasko | 1324b6c | 2018-09-07 11:16:23 +0200 | [diff] [blame] | 3 | * @author Michal Vasko <mvasko@cesnet.cz> |
| 4 | * @brief common internal definitions for libyang |
| 5 | * |
Michal Vasko | 8f702ee | 2024-02-20 15:44:24 +0100 | [diff] [blame] | 6 | * Copyright (c) 2018 - 2024 CESNET, z.s.p.o. |
Michal Vasko | 1324b6c | 2018-09-07 11:16:23 +0200 | [diff] [blame] | 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 | |
Michal Vasko | 8f702ee | 2024-02-20 15:44:24 +0100 | [diff] [blame] | 17 | #include "ly_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> |
Jan Kundrát | d31adc1 | 2022-07-07 21:36:15 +0200 | [diff] [blame] | 22 | #include <fcntl.h> |
Michal Vasko | 15dc9fa | 2021-05-03 14:33:05 +0200 | [diff] [blame] | 23 | #include <inttypes.h> |
Radek Krejci | 4546aa6 | 2019-07-15 16:53:32 +0200 | [diff] [blame] | 24 | #include <stdarg.h> |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 25 | #include <stdio.h> |
Radek Krejci | 4546aa6 | 2019-07-15 16:53:32 +0200 | [diff] [blame] | 26 | #include <stdlib.h> |
Michal Vasko | 841d1a9 | 2018-09-07 15:40:31 +0200 | [diff] [blame] | 27 | #include <string.h> |
Jan Kundrát | f1960dc | 2021-12-12 03:12:23 +0100 | [diff] [blame] | 28 | #ifndef _WIN32 |
Michal Vasko | 8f702ee | 2024-02-20 15:44:24 +0100 | [diff] [blame] | 29 | # ifdef HAVE_MMAP |
| 30 | # include <sys/mman.h> |
| 31 | # endif |
Jan Kundrát | d31adc1 | 2022-07-07 21:36:15 +0200 | [diff] [blame] | 32 | #else |
Michal Vasko | 8f702ee | 2024-02-20 15:44:24 +0100 | [diff] [blame] | 33 | # include <io.h> |
Jan Kundrát | f1960dc | 2021-12-12 03:12:23 +0100 | [diff] [blame] | 34 | #endif |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 35 | #include <sys/stat.h> |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 36 | #include <unistd.h> |
Michal Vasko | 841d1a9 | 2018-09-07 15:40:31 +0200 | [diff] [blame] | 37 | |
Radek Krejci | aa45bda | 2020-07-20 07:43:38 +0200 | [diff] [blame] | 38 | #include "compat.h" |
Radek Krejci | b4a4a27 | 2019-06-10 12:44:52 +0200 | [diff] [blame] | 39 | #include "tree_schema_internal.h" |
Michal Vasko | 3ab37cc | 2024-06-28 10:40:07 +0200 | [diff] [blame] | 40 | #include "version.h" |
aPiecek | 704f8e9 | 2021-08-25 13:35:05 +0200 | [diff] [blame] | 41 | #include "xml.h" |
Michal Vasko | 1324b6c | 2018-09-07 11:16:23 +0200 | [diff] [blame] | 42 | |
Michal Vasko | 1158163 | 2024-07-15 15:04:58 +0200 | [diff] [blame] | 43 | LIBYANG_API_DEF uint32_t |
| 44 | ly_version_so_major(void) |
| 45 | { |
| 46 | return LY_VERSION_MAJOR; |
| 47 | } |
Michal Vasko | 3ab37cc | 2024-06-28 10:40:07 +0200 | [diff] [blame] | 48 | |
Michal Vasko | 1158163 | 2024-07-15 15:04:58 +0200 | [diff] [blame] | 49 | LIBYANG_API_DEF uint32_t |
| 50 | ly_version_so_minor(void) |
| 51 | { |
| 52 | return LY_VERSION_MINOR; |
| 53 | } |
| 54 | |
| 55 | LIBYANG_API_DEF uint32_t |
| 56 | ly_version_so_micro(void) |
| 57 | { |
| 58 | return LY_VERSION_MICRO; |
| 59 | } |
| 60 | |
| 61 | LIBYANG_API_DEF const char * |
| 62 | ly_version_so_str(void) |
| 63 | { |
| 64 | return LY_VERSION; |
| 65 | } |
| 66 | |
| 67 | LIBYANG_API_DEF uint32_t |
| 68 | ly_version_proj_major(void) |
| 69 | { |
| 70 | return LY_PROJ_VERSION_MAJOR; |
| 71 | } |
| 72 | |
| 73 | LIBYANG_API_DEF uint32_t |
| 74 | ly_version_proj_minor(void) |
| 75 | { |
| 76 | return LY_PROJ_VERSION_MINOR; |
| 77 | } |
| 78 | |
| 79 | LIBYANG_API_DEF uint32_t |
| 80 | ly_version_proj_micro(void) |
| 81 | { |
| 82 | return LY_PROJ_VERSION_MICRO; |
| 83 | } |
| 84 | |
| 85 | LIBYANG_API_DEF const char * |
| 86 | ly_version_proj_str(void) |
| 87 | { |
| 88 | return LY_PROJ_VERSION; |
| 89 | } |
Michal Vasko | 3ab37cc | 2024-06-28 10:40:07 +0200 | [diff] [blame] | 90 | |
Michal Vasko | 1324b6c | 2018-09-07 11:16:23 +0200 | [diff] [blame] | 91 | void * |
| 92 | ly_realloc(void *ptr, size_t size) |
| 93 | { |
| 94 | void *new_mem; |
| 95 | |
| 96 | new_mem = realloc(ptr, size); |
| 97 | if (!new_mem) { |
| 98 | free(ptr); |
| 99 | } |
| 100 | |
| 101 | return new_mem; |
| 102 | } |
Michal Vasko | 841d1a9 | 2018-09-07 15:40:31 +0200 | [diff] [blame] | 103 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 104 | char * |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 105 | ly_strnchr(const char *s, int c, size_t len) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 106 | { |
Michal Vasko | b4d40d6 | 2021-05-04 11:42:44 +0200 | [diff] [blame] | 107 | for ( ; len && (*s != (char)c); ++s, --len) {} |
| 108 | return len ? (char *)s : NULL; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 109 | } |
| 110 | |
Radek Krejci | 7f9b651 | 2019-09-18 13:11:09 +0200 | [diff] [blame] | 111 | int |
| 112 | ly_strncmp(const char *refstr, const char *str, size_t str_len) |
| 113 | { |
| 114 | int rc = strncmp(refstr, str, str_len); |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 115 | |
| 116 | if (!rc && (refstr[str_len] == '\0')) { |
Radek Krejci | 7f9b651 | 2019-09-18 13:11:09 +0200 | [diff] [blame] | 117 | return 0; |
| 118 | } else { |
| 119 | return rc ? rc : 1; |
| 120 | } |
| 121 | } |
| 122 | |
Michal Vasko | 15dc9fa | 2021-05-03 14:33:05 +0200 | [diff] [blame] | 123 | LY_ERR |
| 124 | ly_strntou8(const char *nptr, size_t len, uint8_t *ret) |
| 125 | { |
Michal Vasko | 014507d | 2024-06-26 16:02:44 +0200 | [diff] [blame] | 126 | uint8_t num = 0, dig; |
| 127 | uint16_t dec_pow; |
Michal Vasko | 15dc9fa | 2021-05-03 14:33:05 +0200 | [diff] [blame] | 128 | |
| 129 | if (len > 3) { |
| 130 | /* overflow for sure */ |
| 131 | return LY_EDENIED; |
| 132 | } |
| 133 | |
| 134 | dec_pow = 1; |
| 135 | for ( ; len && isdigit(nptr[len - 1]); --len) { |
| 136 | dig = nptr[len - 1] - 48; |
| 137 | |
| 138 | if (LY_OVERFLOW_MUL(UINT8_MAX, dig, dec_pow)) { |
| 139 | return LY_EDENIED; |
| 140 | } |
| 141 | dig *= dec_pow; |
| 142 | |
| 143 | if (LY_OVERFLOW_ADD(UINT8_MAX, num, dig)) { |
| 144 | return LY_EDENIED; |
| 145 | } |
| 146 | num += dig; |
| 147 | |
| 148 | dec_pow *= 10; |
| 149 | } |
| 150 | |
| 151 | if (len) { |
| 152 | return LY_EVALID; |
| 153 | } |
| 154 | *ret = num; |
| 155 | return LY_SUCCESS; |
| 156 | } |
| 157 | |
aPiecek | e3f828d | 2021-05-10 15:34:41 +0200 | [diff] [blame] | 158 | LY_ERR |
| 159 | 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] | 160 | { |
| 161 | const char *stop, *prefix; |
aPiecek | e3f828d | 2021-05-10 15:34:41 +0200 | [diff] [blame] | 162 | size_t bytes_read; |
aPiecek | f102d4d | 2021-03-30 12:18:38 +0200 | [diff] [blame] | 163 | uint32_t c; |
| 164 | ly_bool prefix_found; |
aPiecek | e3f828d | 2021-05-10 15:34:41 +0200 | [diff] [blame] | 165 | LY_ERR ret = LY_SUCCESS; |
aPiecek | f102d4d | 2021-03-30 12:18:38 +0200 | [diff] [blame] | 166 | |
aPiecek | e3f828d | 2021-05-10 15:34:41 +0200 | [diff] [blame] | 167 | assert(len && is_prefix && str_next); |
aPiecek | f102d4d | 2021-03-30 12:18:38 +0200 | [diff] [blame] | 168 | |
| 169 | #define IS_AT_END(PTR, STR_END) (STR_END ? PTR == STR_END : !(*PTR)) |
| 170 | |
| 171 | *str_next = NULL; |
| 172 | *is_prefix = 0; |
aPiecek | e3f828d | 2021-05-10 15:34:41 +0200 | [diff] [blame] | 173 | *len = 0; |
aPiecek | f102d4d | 2021-03-30 12:18:38 +0200 | [diff] [blame] | 174 | |
| 175 | if (!str_begin || !(*str_begin) || (str_begin == str_end)) { |
| 176 | return ret; |
| 177 | } |
| 178 | |
| 179 | stop = str_begin; |
| 180 | prefix = NULL; |
| 181 | prefix_found = 0; |
| 182 | |
| 183 | do { |
| 184 | /* look for the beginning of the YANG value */ |
aPiecek | e3f828d | 2021-05-10 15:34:41 +0200 | [diff] [blame] | 185 | do { |
| 186 | LY_CHECK_RET(ly_getutf8(&stop, &c, &bytes_read)); |
| 187 | } while (!is_xmlqnamestartchar(c) && !IS_AT_END(stop, str_end)); |
aPiecek | f102d4d | 2021-03-30 12:18:38 +0200 | [diff] [blame] | 188 | |
| 189 | if (IS_AT_END(stop, str_end)) { |
| 190 | break; |
| 191 | } |
| 192 | |
| 193 | /* maybe the prefix was found */ |
aPiecek | e3f828d | 2021-05-10 15:34:41 +0200 | [diff] [blame] | 194 | prefix = stop - bytes_read; |
aPiecek | f102d4d | 2021-03-30 12:18:38 +0200 | [diff] [blame] | 195 | |
| 196 | /* look for the the end of the prefix */ |
aPiecek | e3f828d | 2021-05-10 15:34:41 +0200 | [diff] [blame] | 197 | do { |
| 198 | LY_CHECK_RET(ly_getutf8(&stop, &c, &bytes_read)); |
| 199 | } while (is_xmlqnamechar(c) && !IS_AT_END(stop, str_end)); |
aPiecek | f102d4d | 2021-03-30 12:18:38 +0200 | [diff] [blame] | 200 | |
| 201 | prefix_found = c == ':' ? 1 : 0; |
| 202 | |
| 203 | /* if it wasn't the prefix, keep looking */ |
| 204 | } while (!IS_AT_END(stop, str_end) && !prefix_found); |
| 205 | |
| 206 | if ((str_begin == prefix) && prefix_found) { |
| 207 | /* prefix found at the beginning of the input string */ |
| 208 | *is_prefix = 1; |
| 209 | *str_next = IS_AT_END(stop, str_end) ? NULL : stop; |
aPiecek | e3f828d | 2021-05-10 15:34:41 +0200 | [diff] [blame] | 210 | *len = (stop - bytes_read) - str_begin; |
aPiecek | f102d4d | 2021-03-30 12:18:38 +0200 | [diff] [blame] | 211 | } else if ((str_begin != prefix) && (prefix_found)) { |
| 212 | /* there is a some string before prefix */ |
| 213 | *str_next = prefix; |
aPiecek | e3f828d | 2021-05-10 15:34:41 +0200 | [diff] [blame] | 214 | *len = prefix - str_begin; |
aPiecek | f102d4d | 2021-03-30 12:18:38 +0200 | [diff] [blame] | 215 | } else { |
| 216 | /* no prefix found */ |
aPiecek | e3f828d | 2021-05-10 15:34:41 +0200 | [diff] [blame] | 217 | *len = stop - str_begin; |
aPiecek | f102d4d | 2021-03-30 12:18:38 +0200 | [diff] [blame] | 218 | } |
| 219 | |
| 220 | #undef IS_AT_END |
| 221 | |
| 222 | return ret; |
| 223 | } |
| 224 | |
Radek Krejci | b416be6 | 2018-10-01 14:51:45 +0200 | [diff] [blame] | 225 | LY_ERR |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 226 | 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] | 227 | { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 228 | uint32_t c, aux; |
| 229 | size_t len; |
Radek Krejci | b416be6 | 2018-10-01 14:51:45 +0200 | [diff] [blame] | 230 | |
| 231 | c = (*input)[0]; |
Radek Krejci | b416be6 | 2018-10-01 14:51:45 +0200 | [diff] [blame] | 232 | |
| 233 | if (!(c & 0x80)) { |
| 234 | /* one byte character */ |
| 235 | len = 1; |
| 236 | |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 237 | if ((c < 0x20) && (c != 0x9) && (c != 0xa) && (c != 0xd)) { |
Michal Vasko | da09abf | 2023-10-06 15:53:18 +0200 | [diff] [blame] | 238 | goto error; |
Radek Krejci | b416be6 | 2018-10-01 14:51:45 +0200 | [diff] [blame] | 239 | } |
| 240 | } else if ((c & 0xe0) == 0xc0) { |
| 241 | /* two bytes character */ |
| 242 | len = 2; |
| 243 | |
| 244 | aux = (*input)[1]; |
| 245 | if ((aux & 0xc0) != 0x80) { |
Michal Vasko | da09abf | 2023-10-06 15:53:18 +0200 | [diff] [blame] | 246 | goto error; |
Radek Krejci | b416be6 | 2018-10-01 14:51:45 +0200 | [diff] [blame] | 247 | } |
| 248 | c = ((c & 0x1f) << 6) | (aux & 0x3f); |
| 249 | |
| 250 | if (c < 0x80) { |
Michal Vasko | da09abf | 2023-10-06 15:53:18 +0200 | [diff] [blame] | 251 | goto error; |
Radek Krejci | b416be6 | 2018-10-01 14:51:45 +0200 | [diff] [blame] | 252 | } |
| 253 | } else if ((c & 0xf0) == 0xe0) { |
| 254 | /* three bytes character */ |
| 255 | len = 3; |
| 256 | |
| 257 | c &= 0x0f; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 258 | for (uint64_t i = 1; i <= 2; i++) { |
Radek Krejci | b416be6 | 2018-10-01 14:51:45 +0200 | [diff] [blame] | 259 | aux = (*input)[i]; |
| 260 | if ((aux & 0xc0) != 0x80) { |
Michal Vasko | da09abf | 2023-10-06 15:53:18 +0200 | [diff] [blame] | 261 | goto error; |
Radek Krejci | b416be6 | 2018-10-01 14:51:45 +0200 | [diff] [blame] | 262 | } |
| 263 | |
| 264 | c = (c << 6) | (aux & 0x3f); |
| 265 | } |
| 266 | |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 267 | if ((c < 0x800) || ((c > 0xd7ff) && (c < 0xe000)) || (c > 0xfffd)) { |
Michal Vasko | da09abf | 2023-10-06 15:53:18 +0200 | [diff] [blame] | 268 | goto error; |
Radek Krejci | b416be6 | 2018-10-01 14:51:45 +0200 | [diff] [blame] | 269 | } |
| 270 | } else if ((c & 0xf8) == 0xf0) { |
| 271 | /* four bytes character */ |
| 272 | len = 4; |
| 273 | |
| 274 | c &= 0x07; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 275 | for (uint64_t i = 1; i <= 3; i++) { |
Radek Krejci | b416be6 | 2018-10-01 14:51:45 +0200 | [diff] [blame] | 276 | aux = (*input)[i]; |
| 277 | if ((aux & 0xc0) != 0x80) { |
Michal Vasko | da09abf | 2023-10-06 15:53:18 +0200 | [diff] [blame] | 278 | goto error; |
Radek Krejci | b416be6 | 2018-10-01 14:51:45 +0200 | [diff] [blame] | 279 | } |
| 280 | |
| 281 | c = (c << 6) | (aux & 0x3f); |
| 282 | } |
| 283 | |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 284 | if ((c < 0x1000) || (c > 0x10ffff)) { |
Michal Vasko | da09abf | 2023-10-06 15:53:18 +0200 | [diff] [blame] | 285 | goto error; |
Radek Krejci | b416be6 | 2018-10-01 14:51:45 +0200 | [diff] [blame] | 286 | } |
| 287 | } else { |
Michal Vasko | da09abf | 2023-10-06 15:53:18 +0200 | [diff] [blame] | 288 | goto error; |
Radek Krejci | b416be6 | 2018-10-01 14:51:45 +0200 | [diff] [blame] | 289 | } |
| 290 | |
| 291 | (*utf8_char) = c; |
| 292 | (*input) += len; |
| 293 | if (bytes_read) { |
| 294 | (*bytes_read) = len; |
| 295 | } |
| 296 | return LY_SUCCESS; |
Michal Vasko | da09abf | 2023-10-06 15:53:18 +0200 | [diff] [blame] | 297 | |
| 298 | error: |
| 299 | if (bytes_read) { |
| 300 | (*bytes_read) = 0; |
| 301 | } |
| 302 | return LY_EINVAL; |
Radek Krejci | b416be6 | 2018-10-01 14:51:45 +0200 | [diff] [blame] | 303 | } |
| 304 | |
Michal Vasko | 989cdb4 | 2023-10-06 15:32:37 +0200 | [diff] [blame] | 305 | /** |
| 306 | * @brief Check whether an UTF-8 string is equal to a hex string after a bitwise and. |
| 307 | * |
| 308 | * (input & 0x[arg1][arg3][arg5]...) == 0x[arg2][arg4][arg6]... |
| 309 | * |
| 310 | * @param[in] input UTF-8 string. |
| 311 | * @param[in] bytes Number of bytes to compare. |
| 312 | * @param[in] ... 2x @p bytes number of bytes to perform bitwise and and equality operations. |
| 313 | * @return Result of the operation. |
| 314 | */ |
| 315 | static int |
Michal Vasko | c03945b | 2024-03-04 08:30:30 +0100 | [diff] [blame] | 316 | ly_utf8_and_equal(const char *input, int bytes, ...) |
Michal Vasko | 989cdb4 | 2023-10-06 15:32:37 +0200 | [diff] [blame] | 317 | { |
| 318 | va_list ap; |
| 319 | int i, and, byte; |
| 320 | |
| 321 | va_start(ap, bytes); |
| 322 | for (i = 0; i < bytes; ++i) { |
| 323 | and = va_arg(ap, int); |
| 324 | byte = va_arg(ap, int); |
| 325 | |
| 326 | /* compare each byte */ |
| 327 | if (((uint8_t)input[i] & and) != (uint8_t)byte) { |
| 328 | return 0; |
| 329 | } |
| 330 | } |
| 331 | va_end(ap); |
| 332 | |
| 333 | return 1; |
| 334 | } |
| 335 | |
| 336 | /** |
| 337 | * @brief Check whether an UTF-8 string is smaller than a hex string. |
| 338 | * |
| 339 | * input < 0x[arg1][arg2]... |
| 340 | * |
| 341 | * @param[in] input UTF-8 string. |
| 342 | * @param[in] bytes Number of bytes to compare. |
| 343 | * @param[in] ... @p bytes number of bytes to compare with. |
| 344 | * @return Result of the operation. |
| 345 | */ |
| 346 | static int |
Michal Vasko | c03945b | 2024-03-04 08:30:30 +0100 | [diff] [blame] | 347 | ly_utf8_less(const char *input, int bytes, ...) |
Michal Vasko | 989cdb4 | 2023-10-06 15:32:37 +0200 | [diff] [blame] | 348 | { |
| 349 | va_list ap; |
| 350 | int i, byte; |
| 351 | |
| 352 | va_start(ap, bytes); |
| 353 | for (i = 0; i < bytes; ++i) { |
| 354 | byte = va_arg(ap, int); |
| 355 | |
| 356 | /* compare until bytes differ */ |
| 357 | if ((uint8_t)input[i] > (uint8_t)byte) { |
| 358 | return 0; |
| 359 | } else if ((uint8_t)input[i] < (uint8_t)byte) { |
| 360 | return 1; |
| 361 | } |
| 362 | } |
| 363 | va_end(ap); |
| 364 | |
| 365 | /* equals */ |
| 366 | return 0; |
| 367 | } |
| 368 | |
| 369 | /** |
| 370 | * @brief Check whether an UTF-8 string is greater than a hex string. |
| 371 | * |
| 372 | * input > 0x[arg1][arg2]... |
| 373 | * |
| 374 | * @param[in] input UTF-8 string. |
| 375 | * @param[in] bytes Number of bytes to compare. |
| 376 | * @param[in] ... @p bytes number of bytes to compare with. |
| 377 | * @return Result of the operation. |
| 378 | */ |
| 379 | static int |
Michal Vasko | c03945b | 2024-03-04 08:30:30 +0100 | [diff] [blame] | 380 | ly_utf8_greater(const char *input, int bytes, ...) |
Michal Vasko | 989cdb4 | 2023-10-06 15:32:37 +0200 | [diff] [blame] | 381 | { |
| 382 | va_list ap; |
| 383 | int i, byte; |
| 384 | |
| 385 | va_start(ap, bytes); |
| 386 | for (i = 0; i < bytes; ++i) { |
| 387 | byte = va_arg(ap, int); |
| 388 | |
| 389 | /* compare until bytes differ */ |
| 390 | if ((uint8_t)input[i] > (uint8_t)byte) { |
| 391 | return 1; |
| 392 | } else if ((uint8_t)input[i] < (uint8_t)byte) { |
| 393 | return 0; |
| 394 | } |
| 395 | } |
| 396 | va_end(ap); |
| 397 | |
| 398 | /* equals */ |
| 399 | return 0; |
| 400 | } |
| 401 | |
| 402 | LY_ERR |
| 403 | ly_checkutf8(const char *input, size_t in_len, size_t *utf8_len) |
| 404 | { |
| 405 | size_t len; |
| 406 | |
| 407 | if (!(input[0] & 0x80)) { |
| 408 | /* one byte character */ |
| 409 | len = 1; |
| 410 | |
| 411 | if (ly_utf8_less(input, 1, 0x20) && (input[0] != 0x9) && (input[0] != 0xa) && (input[0] != 0xd)) { |
| 412 | /* invalid control characters */ |
| 413 | return LY_EINVAL; |
| 414 | } |
| 415 | } else if (((input[0] & 0xe0) == 0xc0) && (in_len > 1)) { |
| 416 | /* two bytes character */ |
| 417 | len = 2; |
| 418 | |
| 419 | /* (input < 0xC280) || (input > 0xDFBF) || ((input & 0xE0C0) != 0xC080) */ |
| 420 | if (ly_utf8_less(input, 2, 0xC2, 0x80) || ly_utf8_greater(input, 2, 0xDF, 0xBF) || |
| 421 | !ly_utf8_and_equal(input, 2, 0xE0, 0xC0, 0xC0, 0x80)) { |
| 422 | return LY_EINVAL; |
| 423 | } |
| 424 | } else if (((input[0] & 0xf0) == 0xe0) && (in_len > 2)) { |
| 425 | /* three bytes character */ |
| 426 | len = 3; |
| 427 | |
| 428 | /* (input >= 0xEDA080) && (input <= 0xEDBFBF) */ |
| 429 | if (!ly_utf8_less(input, 3, 0xED, 0xA0, 0x80) && !ly_utf8_greater(input, 3, 0xED, 0xBF, 0xBF)) { |
| 430 | /* reject UTF-16 surrogates */ |
| 431 | return LY_EINVAL; |
| 432 | } |
| 433 | |
| 434 | /* (input < 0xE0A080) || (input > 0xEFBFBF) || ((input & 0xF0C0C0) != 0xE08080) */ |
| 435 | if (ly_utf8_less(input, 3, 0xE0, 0xA0, 0x80) || ly_utf8_greater(input, 3, 0xEF, 0xBF, 0xBF) || |
| 436 | !ly_utf8_and_equal(input, 3, 0xF0, 0xE0, 0xC0, 0x80, 0xC0, 0x80)) { |
| 437 | return LY_EINVAL; |
| 438 | } |
| 439 | } else if (((input[0] & 0xf8) == 0xf0) && (in_len > 3)) { |
| 440 | /* four bytes character */ |
| 441 | len = 4; |
| 442 | |
| 443 | /* (input < 0xF0908080) || (input > 0xF48FBFBF) || ((input & 0xF8C0C0C0) != 0xF0808080) */ |
| 444 | if (ly_utf8_less(input, 4, 0xF0, 0x90, 0x80, 0x80) || ly_utf8_greater(input, 4, 0xF4, 0x8F, 0xBF, 0xBF) || |
| 445 | !ly_utf8_and_equal(input, 4, 0xF8, 0xF0, 0xC0, 0x80, 0xC0, 0x80, 0xC0, 0x80)) { |
| 446 | return LY_EINVAL; |
| 447 | } |
| 448 | } else { |
| 449 | return LY_EINVAL; |
| 450 | } |
| 451 | |
| 452 | *utf8_len = len; |
| 453 | return LY_SUCCESS; |
| 454 | } |
| 455 | |
Radek Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 456 | LY_ERR |
| 457 | ly_pututf8(char *dst, uint32_t value, size_t *bytes_written) |
| 458 | { |
| 459 | if (value < 0x80) { |
| 460 | /* one byte character */ |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 461 | if ((value < 0x20) && |
| 462 | (value != 0x09) && |
| 463 | (value != 0x0a) && |
| 464 | (value != 0x0d)) { |
Michal Vasko | 519097f | 2023-05-25 10:00:44 +0200 | [diff] [blame] | 465 | /* valid UTF8 but not YANG string character */ |
Radek Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 466 | return LY_EINVAL; |
| 467 | } |
| 468 | |
| 469 | dst[0] = value; |
| 470 | (*bytes_written) = 1; |
| 471 | } else if (value < 0x800) { |
| 472 | /* two bytes character */ |
| 473 | dst[0] = 0xc0 | (value >> 6); |
| 474 | dst[1] = 0x80 | (value & 0x3f); |
| 475 | (*bytes_written) = 2; |
| 476 | } else if (value < 0xfffe) { |
| 477 | /* three bytes character */ |
| 478 | if (((value & 0xf800) == 0xd800) || |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 479 | ((value >= 0xfdd0) && (value <= 0xfdef))) { |
Radek Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 480 | /* exclude surrogate blocks %xD800-DFFF */ |
| 481 | /* exclude noncharacters %xFDD0-FDEF */ |
| 482 | return LY_EINVAL; |
| 483 | } |
| 484 | |
| 485 | dst[0] = 0xe0 | (value >> 12); |
| 486 | dst[1] = 0x80 | ((value >> 6) & 0x3f); |
| 487 | dst[2] = 0x80 | (value & 0x3f); |
| 488 | |
| 489 | (*bytes_written) = 3; |
| 490 | } else if (value < 0x10fffe) { |
| 491 | if ((value & 0xffe) == 0xffe) { |
| 492 | /* exclude noncharacters %xFFFE-FFFF, %x1FFFE-1FFFF, %x2FFFE-2FFFF, %x3FFFE-3FFFF, %x4FFFE-4FFFF, |
| 493 | * %x5FFFE-5FFFF, %x6FFFE-6FFFF, %x7FFFE-7FFFF, %x8FFFE-8FFFF, %x9FFFE-9FFFF, %xAFFFE-AFFFF, |
| 494 | * %xBFFFE-BFFFF, %xCFFFE-CFFFF, %xDFFFE-DFFFF, %xEFFFE-EFFFF, %xFFFFE-FFFFF, %x10FFFE-10FFFF */ |
| 495 | return LY_EINVAL; |
| 496 | } |
| 497 | /* four bytes character */ |
| 498 | dst[0] = 0xf0 | (value >> 18); |
| 499 | dst[1] = 0x80 | ((value >> 12) & 0x3f); |
| 500 | dst[2] = 0x80 | ((value >> 6) & 0x3f); |
| 501 | dst[3] = 0x80 | (value & 0x3f); |
| 502 | |
| 503 | (*bytes_written) = 4; |
| 504 | } else { |
| 505 | return LY_EINVAL; |
| 506 | } |
| 507 | return LY_SUCCESS; |
| 508 | } |
| 509 | |
Radek Krejci | 76c9801 | 2019-08-14 11:23:24 +0200 | [diff] [blame] | 510 | /** |
| 511 | * @brief Static table of the UTF8 characters lengths according to their first byte. |
| 512 | */ |
Radek Krejci | f6a1100 | 2020-08-21 13:29:07 +0200 | [diff] [blame] | 513 | static const unsigned char utf8_char_length_table[] = { |
Radek Krejci | 76c9801 | 2019-08-14 11:23:24 +0200 | [diff] [blame] | 514 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 515 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 516 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 517 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 518 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 519 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 520 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 521 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 522 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 523 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 524 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 525 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 526 | 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, |
| 527 | 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, |
| 528 | 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, |
| 529 | 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 1, 1 |
| 530 | }; |
| 531 | |
| 532 | size_t |
| 533 | ly_utf8len(const char *str, size_t bytes) |
| 534 | { |
Radek Krejci | 1e008d2 | 2020-08-17 11:37:37 +0200 | [diff] [blame] | 535 | size_t len = 0; |
| 536 | const char *ptr = str; |
Radek Krejci | 76c9801 | 2019-08-14 11:23:24 +0200 | [diff] [blame] | 537 | |
Michal Vasko | b4d40d6 | 2021-05-04 11:42:44 +0200 | [diff] [blame] | 538 | while (((size_t)(ptr - str) < bytes) && *ptr) { |
Radek Krejci | 1e008d2 | 2020-08-17 11:37:37 +0200 | [diff] [blame] | 539 | ++len; |
| 540 | ptr += utf8_char_length_table[((unsigned char)(*ptr))]; |
| 541 | } |
Radek Krejci | 76c9801 | 2019-08-14 11:23:24 +0200 | [diff] [blame] | 542 | return len; |
| 543 | } |
| 544 | |
Michal Vasko | 7b3a00e | 2023-08-09 11:58:03 +0200 | [diff] [blame] | 545 | int |
Radek Krejci | d972c25 | 2018-09-25 13:23:39 +0200 | [diff] [blame] | 546 | LY_VCODE_INSTREXP_len(const char *str) |
| 547 | { |
Michal Vasko | 7b3a00e | 2023-08-09 11:58:03 +0200 | [diff] [blame] | 548 | int len = 0; |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 549 | |
Radek Krejci | d972c25 | 2018-09-25 13:23:39 +0200 | [diff] [blame] | 550 | if (!str) { |
| 551 | return len; |
| 552 | } else if (!str[0]) { |
| 553 | return 1; |
| 554 | } |
Radek Krejci | 1e008d2 | 2020-08-17 11:37:37 +0200 | [diff] [blame] | 555 | for (len = 1; len < LY_VCODE_INSTREXP_MAXLEN && str[len]; ++len) {} |
Radek Krejci | d972c25 | 2018-09-25 13:23:39 +0200 | [diff] [blame] | 556 | return len; |
| 557 | } |
| 558 | |
Jan Kundrát | f1960dc | 2021-12-12 03:12:23 +0100 | [diff] [blame] | 559 | #ifdef HAVE_MMAP |
Radek Krejci | f345c01 | 2018-09-19 11:12:59 +0200 | [diff] [blame] | 560 | LY_ERR |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 561 | 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] | 562 | { |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 563 | struct stat sb; |
| 564 | long pagesize; |
| 565 | size_t m; |
Michal Vasko | 841d1a9 | 2018-09-07 15:40:31 +0200 | [diff] [blame] | 566 | |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 567 | assert(length); |
| 568 | assert(addr); |
| 569 | assert(fd >= 0); |
Michal Vasko | 841d1a9 | 2018-09-07 15:40:31 +0200 | [diff] [blame] | 570 | |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 571 | if (fstat(fd, &sb) == -1) { |
| 572 | LOGERR(ctx, LY_ESYS, "Failed to stat the file descriptor (%s) for the mmap().", strerror(errno)); |
| 573 | return LY_ESYS; |
Michal Vasko | 841d1a9 | 2018-09-07 15:40:31 +0200 | [diff] [blame] | 574 | } |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 575 | if (!S_ISREG(sb.st_mode)) { |
| 576 | LOGERR(ctx, LY_EINVAL, "File to mmap() is not a regular file."); |
| 577 | return LY_ESYS; |
Michal Vasko | 841d1a9 | 2018-09-07 15:40:31 +0200 | [diff] [blame] | 578 | } |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 579 | if (!sb.st_size) { |
| 580 | *addr = NULL; |
| 581 | return LY_SUCCESS; |
| 582 | } |
| 583 | pagesize = sysconf(_SC_PAGESIZE); |
| 584 | |
| 585 | m = sb.st_size % pagesize; |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 586 | if (m && (pagesize - m >= 1)) { |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 587 | /* there will be enough space (at least 1 byte) after the file content mapping to provide zeroed NULL-termination byte */ |
| 588 | *length = sb.st_size + 1; |
| 589 | *addr = mmap(NULL, *length, PROT_READ, MAP_PRIVATE, fd, 0); |
| 590 | } else { |
| 591 | /* there will not be enough bytes after the file content mapping for the additional bytes and some of them |
| 592 | * would overflow into another page that would not be zerroed and any access into it would generate SIGBUS. |
| 593 | * Therefore we have to do the following hack with double mapping. First, the required number of bytes |
| 594 | * (including the additinal bytes) is required as anonymous and thus they will be really provided (actually more |
| 595 | * because of using whole pages) and also initialized by zeros. Then, the file is mapped to the same address |
| 596 | * where the anonymous mapping starts. */ |
| 597 | *length = sb.st_size + pagesize; |
| 598 | *addr = mmap(NULL, *length, PROT_READ, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); |
| 599 | *addr = mmap(*addr, sb.st_size, PROT_READ, MAP_PRIVATE | MAP_FIXED, fd, 0); |
| 600 | } |
| 601 | if (*addr == MAP_FAILED) { |
| 602 | LOGERR(ctx, LY_ESYS, "mmap() failed (%s).", strerror(errno)); |
| 603 | return LY_ESYS; |
Michal Vasko | 841d1a9 | 2018-09-07 15:40:31 +0200 | [diff] [blame] | 604 | } |
| 605 | |
Radek Krejci | f345c01 | 2018-09-19 11:12:59 +0200 | [diff] [blame] | 606 | return LY_SUCCESS; |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 607 | } |
Michal Vasko | 841d1a9 | 2018-09-07 15:40:31 +0200 | [diff] [blame] | 608 | |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 609 | LY_ERR |
| 610 | ly_munmap(void *addr, size_t length) |
| 611 | { |
| 612 | if (munmap(addr, length)) { |
| 613 | return LY_ESYS; |
| 614 | } |
| 615 | return LY_SUCCESS; |
Michal Vasko | 841d1a9 | 2018-09-07 15:40:31 +0200 | [diff] [blame] | 616 | } |
Radek Krejci | 4f28eda | 2018-11-12 11:46:16 +0100 | [diff] [blame] | 617 | |
Jan Kundrát | f1960dc | 2021-12-12 03:12:23 +0100 | [diff] [blame] | 618 | #else |
| 619 | |
| 620 | LY_ERR |
| 621 | ly_mmap(struct ly_ctx *ctx, int fd, size_t *length, void **addr) |
| 622 | { |
| 623 | struct stat sb; |
| 624 | size_t m; |
| 625 | |
| 626 | assert(length); |
| 627 | assert(addr); |
| 628 | assert(fd >= 0); |
| 629 | |
Jan Kundrát | d31adc1 | 2022-07-07 21:36:15 +0200 | [diff] [blame] | 630 | #if _WIN32 |
| 631 | if (_setmode(fd, _O_BINARY) == -1) { |
| 632 | LOGERR(ctx, LY_ESYS, "Failed to switch the file descriptor to binary mode.", strerror(errno)); |
| 633 | return LY_ESYS; |
| 634 | } |
| 635 | #endif |
| 636 | |
Jan Kundrát | f1960dc | 2021-12-12 03:12:23 +0100 | [diff] [blame] | 637 | if (fstat(fd, &sb) == -1) { |
| 638 | LOGERR(ctx, LY_ESYS, "Failed to stat the file descriptor (%s) for the mmap().", strerror(errno)); |
| 639 | return LY_ESYS; |
| 640 | } |
| 641 | if (!S_ISREG(sb.st_mode)) { |
| 642 | LOGERR(ctx, LY_EINVAL, "File to mmap() is not a regular file."); |
| 643 | return LY_ESYS; |
| 644 | } |
| 645 | if (!sb.st_size) { |
| 646 | *addr = NULL; |
| 647 | return LY_SUCCESS; |
| 648 | } |
| 649 | /* On Windows, the mman-win32 mmap() emulation uses CreateFileMapping and MapViewOfFile, and these functions |
| 650 | * do not allow mapping more than "length of file" bytes for PROT_READ. Remapping existing mappings is not allowed, either. |
| 651 | * At that point the path of least resistance is just reading the file in as-is. */ |
| 652 | m = sb.st_size + 1; |
| 653 | char *buf = calloc(m, 1); |
| 654 | |
| 655 | if (!buf) { |
| 656 | LOGERR(ctx, LY_ESYS, "ly_mmap: malloc() failed (%s).", strerror(errno)); |
| 657 | } |
| 658 | *addr = buf; |
| 659 | *length = m; |
| 660 | |
| 661 | lseek(fd, 0, SEEK_SET); |
| 662 | ssize_t to_read = m - 1; |
| 663 | |
| 664 | while (to_read > 0) { |
| 665 | ssize_t n = read(fd, buf, to_read); |
Michal Vasko | 2bf4af4 | 2023-01-04 12:08:38 +0100 | [diff] [blame] | 666 | |
Jan Kundrát | f1960dc | 2021-12-12 03:12:23 +0100 | [diff] [blame] | 667 | if (n == 0) { |
| 668 | return LY_SUCCESS; |
| 669 | } else if (n < 0) { |
| 670 | if (errno == EINTR) { |
| 671 | continue; // can I get this on Windows? |
| 672 | } |
| 673 | LOGERR(ctx, LY_ESYS, "ly_mmap: read() failed (%s).", strerror(errno)); |
| 674 | } |
| 675 | to_read -= n; |
| 676 | buf += n; |
| 677 | } |
| 678 | return LY_SUCCESS; |
| 679 | } |
| 680 | |
| 681 | LY_ERR |
| 682 | ly_munmap(void *addr, size_t length) |
| 683 | { |
| 684 | (void)length; |
| 685 | free(addr); |
| 686 | return LY_SUCCESS; |
| 687 | } |
| 688 | |
| 689 | #endif |
| 690 | |
Radek Krejci | 4f28eda | 2018-11-12 11:46:16 +0100 | [diff] [blame] | 691 | LY_ERR |
Radek Krejci | 4546aa6 | 2019-07-15 16:53:32 +0200 | [diff] [blame] | 692 | ly_strcat(char **dest, const char *format, ...) |
| 693 | { |
| 694 | va_list fp; |
| 695 | char *addition = NULL; |
| 696 | size_t len; |
| 697 | |
| 698 | va_start(fp, format); |
| 699 | len = vasprintf(&addition, format, fp); |
| 700 | len += (*dest ? strlen(*dest) : 0) + 1; |
| 701 | |
| 702 | if (*dest) { |
| 703 | *dest = ly_realloc(*dest, len); |
| 704 | if (!*dest) { |
Radek Krejci | 1cd812f | 2020-12-01 12:17:53 +0100 | [diff] [blame] | 705 | va_end(fp); |
Radek Krejci | 4546aa6 | 2019-07-15 16:53:32 +0200 | [diff] [blame] | 706 | return LY_EMEM; |
| 707 | } |
| 708 | *dest = strcat(*dest, addition); |
| 709 | free(addition); |
| 710 | } else { |
| 711 | *dest = addition; |
| 712 | } |
| 713 | |
| 714 | va_end(fp); |
| 715 | return LY_SUCCESS; |
| 716 | } |
| 717 | |
| 718 | LY_ERR |
Radek Krejci | 249973a | 2019-06-10 10:50:54 +0200 | [diff] [blame] | 719 | 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] | 720 | { |
Michal Vasko | b4d40d6 | 2021-05-04 11:42:44 +0200 | [diff] [blame] | 721 | LY_ERR rc = LY_SUCCESS; |
| 722 | char *ptr, *str; |
Radek Krejci | 9ea8ca1 | 2019-06-10 13:11:55 +0200 | [diff] [blame] | 723 | int64_t i; |
Radek Krejci | 4f28eda | 2018-11-12 11:46:16 +0100 | [diff] [blame] | 724 | |
Radek Krejci | 249973a | 2019-06-10 10:50:54 +0200 | [diff] [blame] | 725 | 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] | 726 | |
Michal Vasko | b4d40d6 | 2021-05-04 11:42:44 +0200 | [diff] [blame] | 727 | /* duplicate the value */ |
| 728 | str = strndup(val_str, val_len); |
| 729 | LY_CHECK_RET(!str, LY_EMEM); |
Radek Krejci | 4f28eda | 2018-11-12 11:46:16 +0100 | [diff] [blame] | 730 | |
Michal Vasko | b4d40d6 | 2021-05-04 11:42:44 +0200 | [diff] [blame] | 731 | /* parse the value to avoid accessing following bytes */ |
| 732 | errno = 0; |
| 733 | i = strtoll(str, &ptr, base); |
| 734 | if (errno || (ptr == str)) { |
| 735 | /* invalid string */ |
| 736 | rc = LY_EVALID; |
Radek Krejci | 9ea8ca1 | 2019-06-10 13:11:55 +0200 | [diff] [blame] | 737 | } else if ((i < min) || (i > max)) { |
Michal Vasko | b4d40d6 | 2021-05-04 11:42:44 +0200 | [diff] [blame] | 738 | /* invalid number */ |
| 739 | rc = LY_EDENIED; |
| 740 | } else if (*ptr) { |
| 741 | while (isspace(*ptr)) { |
| 742 | ++ptr; |
Radek Krejci | 4f28eda | 2018-11-12 11:46:16 +0100 | [diff] [blame] | 743 | } |
Michal Vasko | b4d40d6 | 2021-05-04 11:42:44 +0200 | [diff] [blame] | 744 | if (*ptr) { |
| 745 | /* invalid characters after some number */ |
| 746 | rc = LY_EVALID; |
Radek Krejci | 4f28eda | 2018-11-12 11:46:16 +0100 | [diff] [blame] | 747 | } |
| 748 | } |
Radek Krejci | 9ea8ca1 | 2019-06-10 13:11:55 +0200 | [diff] [blame] | 749 | |
Michal Vasko | b4d40d6 | 2021-05-04 11:42:44 +0200 | [diff] [blame] | 750 | /* cleanup */ |
| 751 | free(str); |
| 752 | if (!rc) { |
| 753 | *ret = i; |
| 754 | } |
| 755 | return rc; |
Radek Krejci | 4f28eda | 2018-11-12 11:46:16 +0100 | [diff] [blame] | 756 | } |
| 757 | |
| 758 | LY_ERR |
Radek Krejci | 249973a | 2019-06-10 10:50:54 +0200 | [diff] [blame] | 759 | 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] | 760 | { |
Michal Vasko | b4d40d6 | 2021-05-04 11:42:44 +0200 | [diff] [blame] | 761 | LY_ERR rc = LY_SUCCESS; |
| 762 | char *ptr, *str; |
Radek Krejci | 4f28eda | 2018-11-12 11:46:16 +0100 | [diff] [blame] | 763 | uint64_t u; |
| 764 | |
Michal Vasko | b4d40d6 | 2021-05-04 11:42:44 +0200 | [diff] [blame] | 765 | 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] | 766 | |
Michal Vasko | b4d40d6 | 2021-05-04 11:42:44 +0200 | [diff] [blame] | 767 | /* duplicate the value to avoid accessing following bytes */ |
| 768 | str = strndup(val_str, val_len); |
| 769 | LY_CHECK_RET(!str, LY_EMEM); |
| 770 | |
| 771 | /* parse the value */ |
Radek Krejci | 4f28eda | 2018-11-12 11:46:16 +0100 | [diff] [blame] | 772 | errno = 0; |
Michal Vasko | b4d40d6 | 2021-05-04 11:42:44 +0200 | [diff] [blame] | 773 | u = strtoull(str, &ptr, base); |
| 774 | if (errno || (ptr == str)) { |
| 775 | /* invalid string */ |
| 776 | rc = LY_EVALID; |
| 777 | } else if ((u > max) || (u && (str[0] == '-'))) { |
| 778 | /* invalid number */ |
| 779 | rc = LY_EDENIED; |
| 780 | } else if (*ptr) { |
| 781 | while (isspace(*ptr)) { |
| 782 | ++ptr; |
Radek Krejci | 4f28eda | 2018-11-12 11:46:16 +0100 | [diff] [blame] | 783 | } |
Michal Vasko | b4d40d6 | 2021-05-04 11:42:44 +0200 | [diff] [blame] | 784 | if (*ptr) { |
| 785 | /* invalid characters after some number */ |
| 786 | rc = LY_EVALID; |
Radek Krejci | 4f28eda | 2018-11-12 11:46:16 +0100 | [diff] [blame] | 787 | } |
| 788 | } |
| 789 | |
Michal Vasko | b4d40d6 | 2021-05-04 11:42:44 +0200 | [diff] [blame] | 790 | /* cleanup */ |
| 791 | free(str); |
| 792 | if (!rc) { |
| 793 | *ret = u; |
| 794 | } |
| 795 | return rc; |
Radek Krejci | 4f28eda | 2018-11-12 11:46:16 +0100 | [diff] [blame] | 796 | } |
Radek Krejci | b4a4a27 | 2019-06-10 12:44:52 +0200 | [diff] [blame] | 797 | |
| 798 | /** |
| 799 | * @brief Parse an identifier. |
| 800 | * |
| 801 | * ;; An identifier MUST NOT start with (('X'|'x') ('M'|'m') ('L'|'l')) |
| 802 | * identifier = (ALPHA / "_") |
| 803 | * *(ALPHA / DIGIT / "_" / "-" / ".") |
| 804 | * |
| 805 | * @param[in,out] id Identifier to parse. When returned, it points to the first character which is not part of the identifier. |
| 806 | * @return LY_ERR value: LY_SUCCESS or LY_EINVAL in case of invalid starting character. |
| 807 | */ |
| 808 | static LY_ERR |
| 809 | lys_parse_id(const char **id) |
| 810 | { |
| 811 | assert(id && *id); |
| 812 | |
| 813 | if (!is_yangidentstartchar(**id)) { |
| 814 | return LY_EINVAL; |
| 815 | } |
| 816 | ++(*id); |
| 817 | |
| 818 | while (is_yangidentchar(**id)) { |
| 819 | ++(*id); |
| 820 | } |
| 821 | return LY_SUCCESS; |
| 822 | } |
| 823 | |
| 824 | LY_ERR |
| 825 | ly_parse_nodeid(const char **id, const char **prefix, size_t *prefix_len, const char **name, size_t *name_len) |
| 826 | { |
| 827 | assert(id && *id); |
| 828 | assert(prefix && prefix_len); |
| 829 | assert(name && name_len); |
| 830 | |
| 831 | *prefix = *id; |
| 832 | *prefix_len = 0; |
| 833 | *name = NULL; |
| 834 | *name_len = 0; |
| 835 | |
| 836 | LY_CHECK_RET(lys_parse_id(id)); |
| 837 | if (**id == ':') { |
| 838 | /* there is prefix */ |
| 839 | *prefix_len = *id - *prefix; |
| 840 | ++(*id); |
| 841 | *name = *id; |
| 842 | |
| 843 | LY_CHECK_RET(lys_parse_id(id)); |
| 844 | *name_len = *id - *name; |
| 845 | } else { |
| 846 | /* there is no prefix, so what we have as prefix now is actually the name */ |
| 847 | *name = *prefix; |
| 848 | *name_len = *id - *name; |
| 849 | *prefix = NULL; |
| 850 | } |
| 851 | |
| 852 | return LY_SUCCESS; |
| 853 | } |
| 854 | |
| 855 | LY_ERR |
Radek Krejci | 084289f | 2019-07-09 17:35:30 +0200 | [diff] [blame] | 856 | 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] | 857 | const char **prefix, size_t *prefix_len, const char **id, size_t *id_len, const char **value, size_t *value_len, |
| 858 | const char **errmsg) |
Radek Krejci | b4a4a27 | 2019-06-10 12:44:52 +0200 | [diff] [blame] | 859 | { |
| 860 | LY_ERR ret = LY_EVALID; |
| 861 | const char *in = *pred; |
| 862 | size_t offset = 1; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 863 | 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] | 864 | char quot; |
| 865 | |
Radek Krejci | 4607f54 | 2020-12-01 12:18:49 +0100 | [diff] [blame] | 866 | assert(in[0] == '['); |
Radek Krejci | b4a4a27 | 2019-06-10 12:44:52 +0200 | [diff] [blame] | 867 | |
| 868 | *prefix = *id = *value = NULL; |
| 869 | *prefix_len = *id_len = *value_len = 0; |
| 870 | |
| 871 | /* leading *WSP */ |
Michal Vasko | d989ba0 | 2020-08-24 10:59:24 +0200 | [diff] [blame] | 872 | for ( ; isspace(in[offset]); offset++) {} |
Radek Krejci | b4a4a27 | 2019-06-10 12:44:52 +0200 | [diff] [blame] | 873 | |
| 874 | if (isdigit(in[offset])) { |
| 875 | /* pos: "[" *WSP positive-integer-value *WSP "]" */ |
| 876 | if (in[offset] == '0') { |
| 877 | /* zero */ |
| 878 | *errmsg = "The position predicate cannot be zero."; |
| 879 | goto error; |
| 880 | } |
| 881 | |
| 882 | /* positive-integer-value */ |
Radek Krejci | 10bfdf8 | 2019-06-10 14:08:13 +0200 | [diff] [blame] | 883 | *value = &in[offset++]; |
Michal Vasko | d989ba0 | 2020-08-24 10:59:24 +0200 | [diff] [blame] | 884 | for ( ; isdigit(in[offset]); offset++) {} |
Radek Krejci | 10bfdf8 | 2019-06-10 14:08:13 +0200 | [diff] [blame] | 885 | *value_len = &in[offset] - *value; |
Radek Krejci | b4a4a27 | 2019-06-10 12:44:52 +0200 | [diff] [blame] | 886 | |
| 887 | } else if (in[offset] == '.') { |
| 888 | /* leaf-list-predicate: "[" *WSP "." *WSP "=" *WSP quoted-string *WSP "]" */ |
| 889 | *id = &in[offset]; |
| 890 | *id_len = 1; |
| 891 | offset++; |
| 892 | expr = 1; |
Radek Krejci | 10bfdf8 | 2019-06-10 14:08:13 +0200 | [diff] [blame] | 893 | } else if (in[offset] == '-') { |
| 894 | /* typically negative value */ |
| 895 | *errmsg = "Invalid instance predicate format (negative position or invalid node-identifier)."; |
| 896 | goto error; |
Radek Krejci | b4a4a27 | 2019-06-10 12:44:52 +0200 | [diff] [blame] | 897 | } else { |
| 898 | /* key-predicate: "[" *WSP node-identifier *WSP "=" *WSP quoted-string *WSP "]" */ |
| 899 | in = &in[offset]; |
| 900 | if (ly_parse_nodeid(&in, prefix, prefix_len, id, id_len)) { |
| 901 | *errmsg = "Invalid node-identifier."; |
| 902 | goto error; |
| 903 | } |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 904 | if ((format == LYD_XML) && !(*prefix)) { |
Radek Krejci | 084289f | 2019-07-09 17:35:30 +0200 | [diff] [blame] | 905 | /* all node names MUST be qualified with explicit namespace prefix */ |
| 906 | *errmsg = "Missing prefix of a node name."; |
| 907 | goto error; |
| 908 | } |
Radek Krejci | b4a4a27 | 2019-06-10 12:44:52 +0200 | [diff] [blame] | 909 | offset = in - *pred; |
| 910 | in = *pred; |
Radek Krejci | 10bfdf8 | 2019-06-10 14:08:13 +0200 | [diff] [blame] | 911 | expr = 2; |
Radek Krejci | b4a4a27 | 2019-06-10 12:44:52 +0200 | [diff] [blame] | 912 | } |
| 913 | |
| 914 | if (expr) { |
| 915 | /* *WSP "=" *WSP quoted-string *WSP "]" */ |
Michal Vasko | d989ba0 | 2020-08-24 10:59:24 +0200 | [diff] [blame] | 916 | for ( ; isspace(in[offset]); offset++) {} |
Radek Krejci | b4a4a27 | 2019-06-10 12:44:52 +0200 | [diff] [blame] | 917 | |
| 918 | if (in[offset] != '=') { |
Radek Krejci | 10bfdf8 | 2019-06-10 14:08:13 +0200 | [diff] [blame] | 919 | if (expr == 1) { |
| 920 | *errmsg = "Unexpected character instead of \'=\' in leaf-list-predicate."; |
| 921 | } else { /* 2 */ |
| 922 | *errmsg = "Unexpected character instead of \'=\' in key-predicate."; |
| 923 | } |
Radek Krejci | b4a4a27 | 2019-06-10 12:44:52 +0200 | [diff] [blame] | 924 | goto error; |
| 925 | } |
| 926 | offset++; |
Michal Vasko | d989ba0 | 2020-08-24 10:59:24 +0200 | [diff] [blame] | 927 | for ( ; isspace(in[offset]); offset++) {} |
Radek Krejci | b4a4a27 | 2019-06-10 12:44:52 +0200 | [diff] [blame] | 928 | |
| 929 | /* quoted-string */ |
| 930 | quot = in[offset++]; |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 931 | if ((quot != '\'') && (quot != '\"')) { |
Radek Krejci | b4a4a27 | 2019-06-10 12:44:52 +0200 | [diff] [blame] | 932 | *errmsg = "String value is not quoted."; |
| 933 | goto error; |
| 934 | } |
| 935 | *value = &in[offset]; |
Michal Vasko | d989ba0 | 2020-08-24 10:59:24 +0200 | [diff] [blame] | 936 | for ( ; offset < limit && (in[offset] != quot || (offset && in[offset - 1] == '\\')); offset++) {} |
Radek Krejci | 10bfdf8 | 2019-06-10 14:08:13 +0200 | [diff] [blame] | 937 | if (in[offset] == quot) { |
| 938 | *value_len = &in[offset] - *value; |
| 939 | offset++; |
| 940 | } else { |
| 941 | *errmsg = "Value is not terminated quoted-string."; |
| 942 | goto error; |
| 943 | } |
Radek Krejci | b4a4a27 | 2019-06-10 12:44:52 +0200 | [diff] [blame] | 944 | } |
| 945 | |
| 946 | /* *WSP "]" */ |
Michal Vasko | d989ba0 | 2020-08-24 10:59:24 +0200 | [diff] [blame] | 947 | for ( ; isspace(in[offset]); offset++) {} |
Radek Krejci | b4a4a27 | 2019-06-10 12:44:52 +0200 | [diff] [blame] | 948 | if (in[offset] != ']') { |
Radek Krejci | 10bfdf8 | 2019-06-10 14:08:13 +0200 | [diff] [blame] | 949 | if (expr == 0) { |
| 950 | *errmsg = "Predicate (pos) is not terminated by \']\' character."; |
| 951 | } else if (expr == 1) { |
| 952 | *errmsg = "Predicate (leaf-list-predicate) is not terminated by \']\' character."; |
| 953 | } else { /* 2 */ |
| 954 | *errmsg = "Predicate (key-predicate) is not terminated by \']\' character."; |
| 955 | } |
Radek Krejci | b4a4a27 | 2019-06-10 12:44:52 +0200 | [diff] [blame] | 956 | goto error; |
| 957 | } |
Radek Krejci | 10bfdf8 | 2019-06-10 14:08:13 +0200 | [diff] [blame] | 958 | offset++; |
Radek Krejci | b4a4a27 | 2019-06-10 12:44:52 +0200 | [diff] [blame] | 959 | |
Radek Krejci | 10bfdf8 | 2019-06-10 14:08:13 +0200 | [diff] [blame] | 960 | if (offset <= limit) { |
| 961 | *pred = &in[offset]; |
Radek Krejci | b4a4a27 | 2019-06-10 12:44:52 +0200 | [diff] [blame] | 962 | return LY_SUCCESS; |
| 963 | } |
| 964 | |
| 965 | /* we read after the limit */ |
| 966 | *errmsg = "Predicate is incomplete."; |
| 967 | *prefix = *id = *value = NULL; |
| 968 | *prefix_len = *id_len = *value_len = 0; |
| 969 | offset = limit; |
| 970 | ret = LY_EINVAL; |
| 971 | |
| 972 | error: |
| 973 | *pred = &in[offset]; |
| 974 | return ret; |
| 975 | } |