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