github.com/google/syzkaller@v0.0.0-20251211124644-a066d2bc4b02/executor/_include/flatbuffers/util.h (about) 1 /* 2 * Copyright 2014 Google Inc. All rights reserved. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 #ifndef FLATBUFFERS_UTIL_H_ 18 #define FLATBUFFERS_UTIL_H_ 19 20 #include <ctype.h> 21 #include <errno.h> 22 23 #include "flatbuffers/base.h" 24 #include "flatbuffers/stl_emulation.h" 25 26 #ifndef FLATBUFFERS_PREFER_PRINTF 27 # include <iomanip> 28 # include <sstream> 29 #else // FLATBUFFERS_PREFER_PRINTF 30 # include <float.h> 31 # include <stdio.h> 32 #endif // FLATBUFFERS_PREFER_PRINTF 33 34 #include <cmath> 35 #include <limits> 36 #include <string> 37 38 namespace flatbuffers { 39 40 // @locale-independent functions for ASCII characters set. 41 42 // Fast checking that character lies in closed range: [a <= x <= b] 43 // using one compare (conditional branch) operator. 44 inline bool check_ascii_range(char x, char a, char b) { 45 FLATBUFFERS_ASSERT(a <= b); 46 // (Hacker's Delight): `a <= x <= b` <=> `(x-a) <={u} (b-a)`. 47 // The x, a, b will be promoted to int and subtracted without overflow. 48 return static_cast<unsigned int>(x - a) <= static_cast<unsigned int>(b - a); 49 } 50 51 // Case-insensitive isalpha 52 inline bool is_alpha(char c) { 53 // ASCII only: alpha to upper case => reset bit 0x20 (~0x20 = 0xDF). 54 return check_ascii_range(c & 0xDF, 'a' & 0xDF, 'z' & 0xDF); 55 } 56 57 // Check for uppercase alpha 58 inline bool is_alpha_upper(char c) { return check_ascii_range(c, 'A', 'Z'); } 59 60 // Check (case-insensitive) that `c` is equal to alpha. 61 inline bool is_alpha_char(char c, char alpha) { 62 FLATBUFFERS_ASSERT(is_alpha(alpha)); 63 // ASCII only: alpha to upper case => reset bit 0x20 (~0x20 = 0xDF). 64 return ((c & 0xDF) == (alpha & 0xDF)); 65 } 66 67 // https://en.cppreference.com/w/cpp/string/byte/isxdigit 68 // isdigit and isxdigit are the only standard narrow character classification 69 // functions that are not affected by the currently installed C locale. although 70 // some implementations (e.g. Microsoft in 1252 codepage) may classify 71 // additional single-byte characters as digits. 72 inline bool is_digit(char c) { return check_ascii_range(c, '0', '9'); } 73 74 inline bool is_xdigit(char c) { 75 // Replace by look-up table. 76 return is_digit(c) || check_ascii_range(c & 0xDF, 'a' & 0xDF, 'f' & 0xDF); 77 } 78 79 // Case-insensitive isalnum 80 inline bool is_alnum(char c) { return is_alpha(c) || is_digit(c); } 81 82 inline char CharToUpper(char c) { 83 return static_cast<char>(::toupper(static_cast<unsigned char>(c))); 84 } 85 86 inline char CharToLower(char c) { 87 return static_cast<char>(::tolower(static_cast<unsigned char>(c))); 88 } 89 90 // @end-locale-independent functions for ASCII character set 91 92 #ifdef FLATBUFFERS_PREFER_PRINTF 93 template<typename T> size_t IntToDigitCount(T t) { 94 size_t digit_count = 0; 95 // Count the sign for negative numbers 96 if (t < 0) digit_count++; 97 // Count a single 0 left of the dot for fractional numbers 98 if (-1 < t && t < 1) digit_count++; 99 // Count digits until fractional part 100 T eps = std::numeric_limits<T>::epsilon(); 101 while (t <= (-1 + eps) || (1 - eps) <= t) { 102 t /= 10; 103 digit_count++; 104 } 105 return digit_count; 106 } 107 108 template<typename T> size_t NumToStringWidth(T t, int precision = 0) { 109 size_t string_width = IntToDigitCount(t); 110 // Count the dot for floating point numbers 111 if (precision) string_width += (precision + 1); 112 return string_width; 113 } 114 115 template<typename T> 116 std::string NumToStringImplWrapper(T t, const char *fmt, int precision = 0) { 117 size_t string_width = NumToStringWidth(t, precision); 118 std::string s(string_width, 0x00); 119 // Allow snprintf to use std::string trailing null to detect buffer overflow 120 snprintf(const_cast<char *>(s.data()), (s.size() + 1), fmt, string_width, t); 121 return s; 122 } 123 #endif // FLATBUFFERS_PREFER_PRINTF 124 125 // Convert an integer or floating point value to a string. 126 // In contrast to std::stringstream, "char" values are 127 // converted to a string of digits, and we don't use scientific notation. 128 template<typename T> std::string NumToString(T t) { 129 // clang-format off 130 131 #ifndef FLATBUFFERS_PREFER_PRINTF 132 std::stringstream ss; 133 ss << t; 134 return ss.str(); 135 #else // FLATBUFFERS_PREFER_PRINTF 136 auto v = static_cast<long long>(t); 137 return NumToStringImplWrapper(v, "%.*lld"); 138 #endif // FLATBUFFERS_PREFER_PRINTF 139 // clang-format on 140 } 141 // Avoid char types used as character data. 142 template<> inline std::string NumToString<signed char>(signed char t) { 143 return NumToString(static_cast<int>(t)); 144 } 145 template<> inline std::string NumToString<unsigned char>(unsigned char t) { 146 return NumToString(static_cast<int>(t)); 147 } 148 template<> inline std::string NumToString<char>(char t) { 149 return NumToString(static_cast<int>(t)); 150 } 151 152 // Special versions for floats/doubles. 153 template<typename T> std::string FloatToString(T t, int precision) { 154 // clang-format off 155 156 #ifndef FLATBUFFERS_PREFER_PRINTF 157 // to_string() prints different numbers of digits for floats depending on 158 // platform and isn't available on Android, so we use stringstream 159 std::stringstream ss; 160 // Use std::fixed to suppress scientific notation. 161 ss << std::fixed; 162 // Default precision is 6, we want that to be higher for doubles. 163 ss << std::setprecision(precision); 164 ss << t; 165 auto s = ss.str(); 166 #else // FLATBUFFERS_PREFER_PRINTF 167 auto v = static_cast<double>(t); 168 auto s = NumToStringImplWrapper(v, "%0.*f", precision); 169 #endif // FLATBUFFERS_PREFER_PRINTF 170 // clang-format on 171 // Sadly, std::fixed turns "1" into "1.00000", so here we undo that. 172 auto p = s.find_last_not_of('0'); 173 if (p != std::string::npos) { 174 // Strip trailing zeroes. If it is a whole number, keep one zero. 175 s.resize(p + (s[p] == '.' ? 2 : 1)); 176 } 177 return s; 178 } 179 180 template<> inline std::string NumToString<double>(double t) { 181 return FloatToString(t, 12); 182 } 183 template<> inline std::string NumToString<float>(float t) { 184 return FloatToString(t, 6); 185 } 186 187 // Convert an integer value to a hexadecimal string. 188 // The returned string length is always xdigits long, prefixed by 0 digits. 189 // For example, IntToStringHex(0x23, 8) returns the string "00000023". 190 inline std::string IntToStringHex(int i, int xdigits) { 191 FLATBUFFERS_ASSERT(i >= 0); 192 // clang-format off 193 194 #ifndef FLATBUFFERS_PREFER_PRINTF 195 std::stringstream ss; 196 ss << std::setw(xdigits) << std::setfill('0') << std::hex << std::uppercase 197 << i; 198 return ss.str(); 199 #else // FLATBUFFERS_PREFER_PRINTF 200 return NumToStringImplWrapper(i, "%.*X", xdigits); 201 #endif // FLATBUFFERS_PREFER_PRINTF 202 // clang-format on 203 } 204 205 // clang-format off 206 // Use locale independent functions {strtod_l, strtof_l, strtoll_l, strtoull_l}. 207 #if defined(FLATBUFFERS_LOCALE_INDEPENDENT) && (FLATBUFFERS_LOCALE_INDEPENDENT > 0) 208 class ClassicLocale { 209 #ifdef _MSC_VER 210 typedef _locale_t locale_type; 211 #else 212 typedef locale_t locale_type; // POSIX.1-2008 locale_t type 213 #endif 214 ClassicLocale(); 215 ~ClassicLocale(); 216 locale_type locale_; 217 static ClassicLocale instance_; 218 public: 219 static locale_type Get() { return instance_.locale_; } 220 }; 221 222 #ifdef _MSC_VER 223 #define __strtoull_impl(s, pe, b) _strtoui64_l(s, pe, b, ClassicLocale::Get()) 224 #define __strtoll_impl(s, pe, b) _strtoi64_l(s, pe, b, ClassicLocale::Get()) 225 #define __strtod_impl(s, pe) _strtod_l(s, pe, ClassicLocale::Get()) 226 #define __strtof_impl(s, pe) _strtof_l(s, pe, ClassicLocale::Get()) 227 #else 228 #define __strtoull_impl(s, pe, b) strtoull_l(s, pe, b, ClassicLocale::Get()) 229 #define __strtoll_impl(s, pe, b) strtoll_l(s, pe, b, ClassicLocale::Get()) 230 #define __strtod_impl(s, pe) strtod_l(s, pe, ClassicLocale::Get()) 231 #define __strtof_impl(s, pe) strtof_l(s, pe, ClassicLocale::Get()) 232 #endif 233 #else 234 #define __strtod_impl(s, pe) strtod(s, pe) 235 #define __strtof_impl(s, pe) static_cast<float>(strtod(s, pe)) 236 #ifdef _MSC_VER 237 #define __strtoull_impl(s, pe, b) _strtoui64(s, pe, b) 238 #define __strtoll_impl(s, pe, b) _strtoi64(s, pe, b) 239 #else 240 #define __strtoull_impl(s, pe, b) strtoull(s, pe, b) 241 #define __strtoll_impl(s, pe, b) strtoll(s, pe, b) 242 #endif 243 #endif 244 245 inline void strtoval_impl(int64_t *val, const char *str, char **endptr, 246 int base) { 247 *val = __strtoll_impl(str, endptr, base); 248 } 249 250 inline void strtoval_impl(uint64_t *val, const char *str, char **endptr, 251 int base) { 252 *val = __strtoull_impl(str, endptr, base); 253 } 254 255 inline void strtoval_impl(double *val, const char *str, char **endptr) { 256 *val = __strtod_impl(str, endptr); 257 } 258 259 // UBSAN: double to float is safe if numeric_limits<float>::is_iec559 is true. 260 FLATBUFFERS_SUPPRESS_UBSAN("float-cast-overflow") 261 inline void strtoval_impl(float *val, const char *str, char **endptr) { 262 *val = __strtof_impl(str, endptr); 263 } 264 #undef __strtoull_impl 265 #undef __strtoll_impl 266 #undef __strtod_impl 267 #undef __strtof_impl 268 // clang-format on 269 270 // Adaptor for strtoull()/strtoll(). 271 // Flatbuffers accepts numbers with any count of leading zeros (-009 is -9), 272 // while strtoll with base=0 interprets first leading zero as octal prefix. 273 // In future, it is possible to add prefixed 0b0101. 274 // 1) Checks errno code for overflow condition (out of range). 275 // 2) If base <= 0, function try to detect base of number by prefix. 276 // 277 // Return value (like strtoull and strtoll, but reject partial result): 278 // - If successful, an integer value corresponding to the str is returned. 279 // - If full string conversion can't be performed, 0 is returned. 280 // - If the converted value falls out of range of corresponding return type, a 281 // range error occurs. In this case value MAX(T)/MIN(T) is returned. 282 template<typename T> 283 inline bool StringToIntegerImpl(T *val, const char *const str, 284 const int base = 0, 285 const bool check_errno = true) { 286 // T is int64_t or uint64_T 287 FLATBUFFERS_ASSERT(str); 288 if (base <= 0) { 289 auto s = str; 290 while (*s && !is_digit(*s)) s++; 291 if (s[0] == '0' && is_alpha_char(s[1], 'X')) 292 return StringToIntegerImpl(val, str, 16, check_errno); 293 // if a prefix not match, try base=10 294 return StringToIntegerImpl(val, str, 10, check_errno); 295 } else { 296 if (check_errno) errno = 0; // clear thread-local errno 297 auto endptr = str; 298 strtoval_impl(val, str, const_cast<char **>(&endptr), base); 299 if ((*endptr != '\0') || (endptr == str)) { 300 *val = 0; // erase partial result 301 return false; // invalid string 302 } 303 // errno is out-of-range, return MAX/MIN 304 if (check_errno && errno) return false; 305 return true; 306 } 307 } 308 309 template<typename T> 310 inline bool StringToFloatImpl(T *val, const char *const str) { 311 // Type T must be either float or double. 312 FLATBUFFERS_ASSERT(str && val); 313 auto end = str; 314 strtoval_impl(val, str, const_cast<char **>(&end)); 315 auto done = (end != str) && (*end == '\0'); 316 if (!done) *val = 0; // erase partial result 317 if (done && std::isnan(*val)) { *val = std::numeric_limits<T>::quiet_NaN(); } 318 return done; 319 } 320 321 // Convert a string to an instance of T. 322 // Return value (matched with StringToInteger64Impl and strtod): 323 // - If successful, a numeric value corresponding to the str is returned. 324 // - If full string conversion can't be performed, 0 is returned. 325 // - If the converted value falls out of range of corresponding return type, a 326 // range error occurs. In this case value MAX(T)/MIN(T) is returned. 327 template<typename T> inline bool StringToNumber(const char *s, T *val) { 328 // Assert on `unsigned long` and `signed long` on LP64. 329 // If it is necessary, it could be solved with flatbuffers::enable_if<B,T>. 330 static_assert(sizeof(T) < sizeof(int64_t), "unexpected type T"); 331 FLATBUFFERS_ASSERT(s && val); 332 int64_t i64; 333 // The errno check isn't needed, will return MAX/MIN on overflow. 334 if (StringToIntegerImpl(&i64, s, 0, false)) { 335 const int64_t max = (flatbuffers::numeric_limits<T>::max)(); 336 const int64_t min = flatbuffers::numeric_limits<T>::lowest(); 337 if (i64 > max) { 338 *val = static_cast<T>(max); 339 return false; 340 } 341 if (i64 < min) { 342 // For unsigned types return max to distinguish from 343 // "no conversion can be performed" when 0 is returned. 344 *val = static_cast<T>(flatbuffers::is_unsigned<T>::value ? max : min); 345 return false; 346 } 347 *val = static_cast<T>(i64); 348 return true; 349 } 350 *val = 0; 351 return false; 352 } 353 354 template<> inline bool StringToNumber<int64_t>(const char *str, int64_t *val) { 355 return StringToIntegerImpl(val, str); 356 } 357 358 template<> 359 inline bool StringToNumber<uint64_t>(const char *str, uint64_t *val) { 360 if (!StringToIntegerImpl(val, str)) return false; 361 // The strtoull accepts negative numbers: 362 // If the minus sign was part of the input sequence, the numeric value 363 // calculated from the sequence of digits is negated as if by unary minus 364 // in the result type, which applies unsigned integer wraparound rules. 365 // Fix this behaviour (except -0). 366 if (*val) { 367 auto s = str; 368 while (*s && !is_digit(*s)) s++; 369 s = (s > str) ? (s - 1) : s; // step back to one symbol 370 if (*s == '-') { 371 // For unsigned types return the max to distinguish from 372 // "no conversion can be performed". 373 *val = (flatbuffers::numeric_limits<uint64_t>::max)(); 374 return false; 375 } 376 } 377 return true; 378 } 379 380 template<> inline bool StringToNumber(const char *s, float *val) { 381 return StringToFloatImpl(val, s); 382 } 383 384 template<> inline bool StringToNumber(const char *s, double *val) { 385 return StringToFloatImpl(val, s); 386 } 387 388 inline int64_t StringToInt(const char *s, int base = 10) { 389 int64_t val; 390 return StringToIntegerImpl(&val, s, base) ? val : 0; 391 } 392 393 inline uint64_t StringToUInt(const char *s, int base = 10) { 394 uint64_t val; 395 return StringToIntegerImpl(&val, s, base) ? val : 0; 396 } 397 398 inline bool StringIsFlatbufferNan(const std::string &s) { 399 return s == "nan" || s == "+nan" || s == "-nan"; 400 } 401 402 inline bool StringIsFlatbufferPositiveInfinity(const std::string &s) { 403 return s == "inf" || s == "+inf" || s == "infinity" || s == "+infinity"; 404 } 405 406 inline bool StringIsFlatbufferNegativeInfinity(const std::string &s) { 407 return s == "-inf" || s == "-infinity"; 408 } 409 410 typedef bool (*LoadFileFunction)(const char *filename, bool binary, 411 std::string *dest); 412 typedef bool (*FileExistsFunction)(const char *filename); 413 414 LoadFileFunction SetLoadFileFunction(LoadFileFunction load_file_function); 415 416 FileExistsFunction SetFileExistsFunction( 417 FileExistsFunction file_exists_function); 418 419 // Check if file "name" exists. 420 bool FileExists(const char *name); 421 422 // Check if "name" exists and it is also a directory. 423 bool DirExists(const char *name); 424 425 // Load file "name" into "buf" returning true if successful 426 // false otherwise. If "binary" is false data is read 427 // using ifstream's text mode, otherwise data is read with 428 // no transcoding. 429 bool LoadFile(const char *name, bool binary, std::string *buf); 430 431 // Save data "buf" of length "len" bytes into a file 432 // "name" returning true if successful, false otherwise. 433 // If "binary" is false data is written using ifstream's 434 // text mode, otherwise data is written with no 435 // transcoding. 436 bool SaveFile(const char *name, const char *buf, size_t len, bool binary); 437 438 // Save data "buf" into file "name" returning true if 439 // successful, false otherwise. If "binary" is false 440 // data is written using ifstream's text mode, otherwise 441 // data is written with no transcoding. 442 inline bool SaveFile(const char *name, const std::string &buf, bool binary) { 443 return SaveFile(name, buf.c_str(), buf.size(), binary); 444 } 445 446 // Functionality for minimalistic portable path handling. 447 448 // The functions below behave correctly regardless of whether posix ('/') or 449 // Windows ('/' or '\\') separators are used. 450 451 // Any new separators inserted are always posix. 452 FLATBUFFERS_CONSTEXPR char kPathSeparator = '/'; 453 454 // Returns the path with the extension, if any, removed. 455 std::string StripExtension(const std::string &filepath); 456 457 // Returns the extension, if any. 458 std::string GetExtension(const std::string &filepath); 459 460 // Return the last component of the path, after the last separator. 461 std::string StripPath(const std::string &filepath); 462 463 // Strip the last component of the path + separator. 464 std::string StripFileName(const std::string &filepath); 465 466 std::string StripPrefix(const std::string &filepath, 467 const std::string &prefix_to_remove); 468 469 // Concatenates a path with a filename, regardless of whether the path 470 // ends in a separator or not. 471 std::string ConCatPathFileName(const std::string &path, 472 const std::string &filename); 473 474 // Replaces any '\\' separators with '/' 475 std::string PosixPath(const char *path); 476 std::string PosixPath(const std::string &path); 477 478 // This function ensure a directory exists, by recursively 479 // creating dirs for any parts of the path that don't exist yet. 480 void EnsureDirExists(const std::string &filepath); 481 482 // Obtains the absolute path from any other path. 483 // Returns the input path if the absolute path couldn't be resolved. 484 std::string AbsolutePath(const std::string &filepath); 485 486 // Returns files relative to the --project_root path, prefixed with `//`. 487 std::string RelativeToRootPath(const std::string &project, 488 const std::string &filepath); 489 490 // To and from UTF-8 unicode conversion functions 491 492 // Convert a unicode code point into a UTF-8 representation by appending it 493 // to a string. Returns the number of bytes generated. 494 inline int ToUTF8(uint32_t ucc, std::string *out) { 495 FLATBUFFERS_ASSERT(!(ucc & 0x80000000)); // Top bit can't be set. 496 // 6 possible encodings: http://en.wikipedia.org/wiki/UTF-8 497 for (int i = 0; i < 6; i++) { 498 // Max bits this encoding can represent. 499 uint32_t max_bits = 6 + i * 5 + static_cast<int>(!i); 500 if (ucc < (1u << max_bits)) { // does it fit? 501 // Remaining bits not encoded in the first byte, store 6 bits each 502 uint32_t remain_bits = i * 6; 503 // Store first byte: 504 (*out) += static_cast<char>((0xFE << (max_bits - remain_bits)) | 505 (ucc >> remain_bits)); 506 // Store remaining bytes: 507 for (int j = i - 1; j >= 0; j--) { 508 (*out) += static_cast<char>(((ucc >> (j * 6)) & 0x3F) | 0x80); 509 } 510 return i + 1; // Return the number of bytes added. 511 } 512 } 513 FLATBUFFERS_ASSERT(0); // Impossible to arrive here. 514 return -1; 515 } 516 517 // Converts whatever prefix of the incoming string corresponds to a valid 518 // UTF-8 sequence into a unicode code. The incoming pointer will have been 519 // advanced past all bytes parsed. 520 // returns -1 upon corrupt UTF-8 encoding (ignore the incoming pointer in 521 // this case). 522 inline int FromUTF8(const char **in) { 523 int len = 0; 524 // Count leading 1 bits. 525 for (int mask = 0x80; mask >= 0x04; mask >>= 1) { 526 if (**in & mask) { 527 len++; 528 } else { 529 break; 530 } 531 } 532 if ((static_cast<unsigned char>(**in) << len) & 0x80) 533 return -1; // Bit after leading 1's must be 0. 534 if (!len) return *(*in)++; 535 // UTF-8 encoded values with a length are between 2 and 4 bytes. 536 if (len < 2 || len > 4) { return -1; } 537 // Grab initial bits of the code. 538 int ucc = *(*in)++ & ((1 << (7 - len)) - 1); 539 for (int i = 0; i < len - 1; i++) { 540 if ((**in & 0xC0) != 0x80) return -1; // Upper bits must 1 0. 541 ucc <<= 6; 542 ucc |= *(*in)++ & 0x3F; // Grab 6 more bits of the code. 543 } 544 // UTF-8 cannot encode values between 0xD800 and 0xDFFF (reserved for 545 // UTF-16 surrogate pairs). 546 if (ucc >= 0xD800 && ucc <= 0xDFFF) { return -1; } 547 // UTF-8 must represent code points in their shortest possible encoding. 548 switch (len) { 549 case 2: 550 // Two bytes of UTF-8 can represent code points from U+0080 to U+07FF. 551 if (ucc < 0x0080 || ucc > 0x07FF) { return -1; } 552 break; 553 case 3: 554 // Three bytes of UTF-8 can represent code points from U+0800 to U+FFFF. 555 if (ucc < 0x0800 || ucc > 0xFFFF) { return -1; } 556 break; 557 case 4: 558 // Four bytes of UTF-8 can represent code points from U+10000 to U+10FFFF. 559 if (ucc < 0x10000 || ucc > 0x10FFFF) { return -1; } 560 break; 561 } 562 return ucc; 563 } 564 565 #ifndef FLATBUFFERS_PREFER_PRINTF 566 // Wraps a string to a maximum length, inserting new lines where necessary. Any 567 // existing whitespace will be collapsed down to a single space. A prefix or 568 // suffix can be provided, which will be inserted before or after a wrapped 569 // line, respectively. 570 inline std::string WordWrap(const std::string in, size_t max_length, 571 const std::string wrapped_line_prefix, 572 const std::string wrapped_line_suffix) { 573 std::istringstream in_stream(in); 574 std::string wrapped, line, word; 575 576 in_stream >> word; 577 line = word; 578 579 while (in_stream >> word) { 580 if ((line.length() + 1 + word.length() + wrapped_line_suffix.length()) < 581 max_length) { 582 line += " " + word; 583 } else { 584 wrapped += line + wrapped_line_suffix + "\n"; 585 line = wrapped_line_prefix + word; 586 } 587 } 588 wrapped += line; 589 590 return wrapped; 591 } 592 #endif // !FLATBUFFERS_PREFER_PRINTF 593 594 inline bool EscapeString(const char *s, size_t length, std::string *_text, 595 bool allow_non_utf8, bool natural_utf8) { 596 std::string &text = *_text; 597 text += "\""; 598 for (uoffset_t i = 0; i < length; i++) { 599 char c = s[i]; 600 switch (c) { 601 case '\n': text += "\\n"; break; 602 case '\t': text += "\\t"; break; 603 case '\r': text += "\\r"; break; 604 case '\b': text += "\\b"; break; 605 case '\f': text += "\\f"; break; 606 case '\"': text += "\\\""; break; 607 case '\\': text += "\\\\"; break; 608 default: 609 if (c >= ' ' && c <= '~') { 610 text += c; 611 } else { 612 // Not printable ASCII data. Let's see if it's valid UTF-8 first: 613 const char *utf8 = s + i; 614 int ucc = FromUTF8(&utf8); 615 if (ucc < 0) { 616 if (allow_non_utf8) { 617 text += "\\x"; 618 text += IntToStringHex(static_cast<uint8_t>(c), 2); 619 } else { 620 // There are two cases here: 621 // 622 // 1) We reached here by parsing an IDL file. In that case, 623 // we previously checked for non-UTF-8, so we shouldn't reach 624 // here. 625 // 626 // 2) We reached here by someone calling GenText() 627 // on a previously-serialized flatbuffer. The data might have 628 // non-UTF-8 Strings, or might be corrupt. 629 // 630 // In both cases, we have to give up and inform the caller 631 // they have no JSON. 632 return false; 633 } 634 } else { 635 if (natural_utf8) { 636 // utf8 points to past all utf-8 bytes parsed 637 text.append(s + i, static_cast<size_t>(utf8 - s - i)); 638 } else if (ucc <= 0xFFFF) { 639 // Parses as Unicode within JSON's \uXXXX range, so use that. 640 text += "\\u"; 641 text += IntToStringHex(ucc, 4); 642 } else if (ucc <= 0x10FFFF) { 643 // Encode Unicode SMP values to a surrogate pair using two \u 644 // escapes. 645 uint32_t base = ucc - 0x10000; 646 auto high_surrogate = (base >> 10) + 0xD800; 647 auto low_surrogate = (base & 0x03FF) + 0xDC00; 648 text += "\\u"; 649 text += IntToStringHex(high_surrogate, 4); 650 text += "\\u"; 651 text += IntToStringHex(low_surrogate, 4); 652 } 653 // Skip past characters recognized. 654 i = static_cast<uoffset_t>(utf8 - s - 1); 655 } 656 } 657 break; 658 } 659 } 660 text += "\""; 661 return true; 662 } 663 664 inline std::string BufferToHexText(const void *buffer, size_t buffer_size, 665 size_t max_length, 666 const std::string &wrapped_line_prefix, 667 const std::string &wrapped_line_suffix) { 668 std::string text = wrapped_line_prefix; 669 size_t start_offset = 0; 670 const char *s = reinterpret_cast<const char *>(buffer); 671 for (size_t i = 0; s && i < buffer_size; i++) { 672 // Last iteration or do we have more? 673 bool have_more = i + 1 < buffer_size; 674 text += "0x"; 675 text += IntToStringHex(static_cast<uint8_t>(s[i]), 2); 676 if (have_more) { text += ','; } 677 // If we have more to process and we reached max_length 678 if (have_more && 679 text.size() + wrapped_line_suffix.size() >= start_offset + max_length) { 680 text += wrapped_line_suffix; 681 text += '\n'; 682 start_offset = text.size(); 683 text += wrapped_line_prefix; 684 } 685 } 686 text += wrapped_line_suffix; 687 return text; 688 } 689 690 // Remove paired quotes in a string: "text"|'text' -> text. 691 std::string RemoveStringQuotes(const std::string &s); 692 693 // Change th global C-locale to locale with name <locale_name>. 694 // Returns an actual locale name in <_value>, useful if locale_name is "" or 695 // null. 696 bool SetGlobalTestLocale(const char *locale_name, 697 std::string *_value = nullptr); 698 699 // Read (or test) a value of environment variable. 700 bool ReadEnvironmentVariable(const char *var_name, 701 std::string *_value = nullptr); 702 703 enum class Case { 704 kUnknown = 0, 705 // TheQuickBrownFox 706 kUpperCamel = 1, 707 // theQuickBrownFox 708 kLowerCamel = 2, 709 // the_quick_brown_fox 710 kSnake = 3, 711 // THE_QUICK_BROWN_FOX 712 kScreamingSnake = 4, 713 // THEQUICKBROWNFOX 714 kAllUpper = 5, 715 // thequickbrownfox 716 kAllLower = 6, 717 // the-quick-brown-fox 718 kDasher = 7, 719 // THEQuiCKBr_ownFox (or whatever you want, we won't change it) 720 kKeep = 8, 721 // the_quick_brown_fox123 (as opposed to the_quick_brown_fox_123) 722 kSnake2 = 9, 723 }; 724 725 // Convert the `input` string of case `input_case` to the specified 726 // `output_case`. 727 std::string ConvertCase(const std::string &input, Case output_case, 728 Case input_case = Case::kSnake); 729 730 } // namespace flatbuffers 731 732 #endif // FLATBUFFERS_UTIL_H_