simdjson  0.9.0
Ridiculously Fast JSON
padded_string-inl.h
1 #ifndef SIMDJSON_INLINE_PADDED_STRING_H
2 #define SIMDJSON_INLINE_PADDED_STRING_H
3 
4 #include "simdjson/portability.h"
5 #include "simdjson/common_defs.h" // for SIMDJSON_PADDING
6 
7 #include <climits>
8 #include <cstring>
9 #include <memory>
10 #include <string>
11 
12 namespace simdjson {
13 namespace internal {
14 
15 // The allocate_padded_buffer function is a low-level function to allocate memory
16 // with padding so we can read past the "length" bytes safely. It is used by
17 // the padded_string class automatically. It returns nullptr in case
18 // of error: the caller should check for a null pointer.
19 // The length parameter is the maximum size in bytes of the string.
20 // The caller is responsible to free the memory (e.g., delete[] (...)).
21 inline char *allocate_padded_buffer(size_t length) noexcept {
22  const size_t totalpaddedlength = length + SIMDJSON_PADDING;
23  if(totalpaddedlength<length) {
24  // overflow
25  return nullptr;
26  }
27 #ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
28  // avoid getting out of memory
29  if (totalpaddedlength>(1UL<<20)) {
30  return nullptr;
31  }
32 #endif
33 
34  char *padded_buffer = new (std::nothrow) char[totalpaddedlength];
35  if (padded_buffer == nullptr) {
36  return nullptr;
37  }
38  // We write zeroes in the padded region to avoid having uninitized
39  // garbage. If nothing else, garbage getting read might trigger a
40  // warning in a memory checking.
41  std::memset(padded_buffer + length, 0, totalpaddedlength - length);
42  return padded_buffer;
43 } // allocate_padded_buffer()
44 
45 } // namespace internal
46 
47 
48 inline padded_string::padded_string() noexcept {}
49 inline padded_string::padded_string(size_t length) noexcept
50  : viable_size(length), data_ptr(internal::allocate_padded_buffer(length)) {
51 }
52 inline padded_string::padded_string(const char *data, size_t length) noexcept
53  : viable_size(length), data_ptr(internal::allocate_padded_buffer(length)) {
54  if ((data != nullptr) and (data_ptr != nullptr)) {
55  std::memcpy(data_ptr, data, length);
56  }
57 }
58 // note: do not pass std::string arguments by value
59 inline padded_string::padded_string(const std::string & str_ ) noexcept
60  : viable_size(str_.size()), data_ptr(internal::allocate_padded_buffer(str_.size())) {
61  if (data_ptr != nullptr) {
62  std::memcpy(data_ptr, str_.data(), str_.size());
63  }
64 }
65 // note: do pass std::string_view arguments by value
66 inline padded_string::padded_string(std::string_view sv_) noexcept
67  : viable_size(sv_.size()), data_ptr(internal::allocate_padded_buffer(sv_.size())) {
68  if(simdjson_unlikely(!data_ptr)) {
69  //allocation failed or zero size
70  viable_size=0;
71  return;
72  }
73  if (sv_.size()) {
74  std::memcpy(data_ptr, sv_.data(), sv_.size());
75  }
76 }
78  : viable_size(o.viable_size), data_ptr(o.data_ptr) {
79  o.data_ptr = nullptr; // we take ownership
80 }
81 
83  delete[] data_ptr;
84  data_ptr = o.data_ptr;
85  viable_size = o.viable_size;
86  o.data_ptr = nullptr; // we take ownership
87  o.viable_size = 0;
88  return *this;
89 }
90 
91 inline void padded_string::swap(padded_string &o) noexcept {
92  size_t tmp_viable_size = viable_size;
93  char *tmp_data_ptr = data_ptr;
94  viable_size = o.viable_size;
95  data_ptr = o.data_ptr;
96  o.data_ptr = tmp_data_ptr;
97  o.viable_size = tmp_viable_size;
98 }
99 
100 inline padded_string::~padded_string() noexcept {
101  delete[] data_ptr;
102 }
103 
104 inline size_t padded_string::size() const noexcept { return viable_size; }
105 
106 inline size_t padded_string::length() const noexcept { return viable_size; }
107 
108 inline const char *padded_string::data() const noexcept { return data_ptr; }
109 
110 inline char *padded_string::data() noexcept { return data_ptr; }
111 
112 inline padded_string::operator std::string_view() const { return std::string_view(data(), length()); }
113 
114 inline padded_string::operator padded_string_view() const noexcept {
115  return padded_string_view(data(), length(), length() + SIMDJSON_PADDING);
116 }
117 
118 inline simdjson_result<padded_string> padded_string::load(const std::string &filename) noexcept {
119  // Open the file
120  SIMDJSON_PUSH_DISABLE_WARNINGS
121  SIMDJSON_DISABLE_DEPRECATED_WARNING // Disable CRT_SECURE warning on MSVC: manually verified this is safe
122  std::FILE *fp = std::fopen(filename.c_str(), "rb");
123  SIMDJSON_POP_DISABLE_WARNINGS
124 
125  if (fp == nullptr) {
126  return IO_ERROR;
127  }
128 
129  // Get the file size
130  if(std::fseek(fp, 0, SEEK_END) < 0) {
131  std::fclose(fp);
132  return IO_ERROR;
133  }
134 #if defined(SIMDJSON_VISUAL_STUDIO) && !SIMDJSON_IS_32BITS
135  __int64 llen = _ftelli64(fp);
136  if(llen == -1L) {
137  std::fclose(fp);
138  return IO_ERROR;
139  }
140 #else
141  long llen = std::ftell(fp);
142  if((llen < 0) || (llen == LONG_MAX)) {
143  std::fclose(fp);
144  return IO_ERROR;
145  }
146 #endif
147 
148  // Allocate the padded_string
149  size_t len = static_cast<size_t>(llen);
150  padded_string s(len);
151  if (s.data() == nullptr) {
152  std::fclose(fp);
153  return MEMALLOC;
154  }
155 
156  // Read the padded_string
157  std::rewind(fp);
158  size_t bytes_read = std::fread(s.data(), 1, len, fp);
159  if (std::fclose(fp) != 0 || bytes_read != len) {
160  return IO_ERROR;
161  }
162 
163  return s;
164 }
165 
166 } // namespace simdjson
167 
168 #endif // SIMDJSON_INLINE_PADDED_STRING_H
simdjson::padded_string::length
size_t length() const noexcept
The length of the string.
Definition: padded_string-inl.h:106
simdjson::padded_string::operator=
padded_string & operator=(padded_string &&o) noexcept
Move one padded string into another.
Definition: padded_string-inl.h:82
simdjson::padded_string::data
const char * data() const noexcept
The string data.
Definition: padded_string-inl.h:108
simdjson::padded_string::load
static simdjson_result< padded_string > load(const std::string &path) noexcept
Load this padded string from a file.
Definition: padded_string-inl.h:118
simdjson::simdjson_result
The result of a simdjson operation that could fail.
Definition: error.h:203
simdjson::padded_string::size
size_t size() const noexcept
The length of the string.
Definition: padded_string-inl.h:104
simdjson::padded_string::padded_string
padded_string() noexcept
Create a new, empty padded string.
Definition: padded_string-inl.h:48
simdjson::padded_string
String with extra allocation for ease of use with parser::parse()
Definition: padded_string.h:21
simdjson::padded_string_view
User-provided string that promises it has extra padded bytes at the end for use with parser::parse().
Definition: padded_string_view.h:18