datapiece.cc 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  1. // Protocol Buffers - Google's data interchange format
  2. // Copyright 2008 Google Inc. All rights reserved.
  3. // https://developers.google.com/protocol-buffers/
  4. //
  5. // Redistribution and use in source and binary forms, with or without
  6. // modification, are permitted provided that the following conditions are
  7. // met:
  8. //
  9. // * Redistributions of source code must retain the above copyright
  10. // notice, this list of conditions and the following disclaimer.
  11. // * Redistributions in binary form must reproduce the above
  12. // copyright notice, this list of conditions and the following disclaimer
  13. // in the documentation and/or other materials provided with the
  14. // distribution.
  15. // * Neither the name of Google Inc. nor the names of its
  16. // contributors may be used to endorse or promote products derived from
  17. // this software without specific prior written permission.
  18. //
  19. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  22. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  23. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  24. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  25. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  26. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  27. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  28. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  29. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. #include <google/protobuf/util/internal/datapiece.h>
  31. #include <cmath>
  32. #include <cstdint>
  33. #include <limits>
  34. #include <google/protobuf/struct.pb.h>
  35. #include <google/protobuf/type.pb.h>
  36. #include <google/protobuf/descriptor.h>
  37. #include <google/protobuf/util/internal/utility.h>
  38. #include <google/protobuf/stubs/status.h>
  39. #include <google/protobuf/stubs/strutil.h>
  40. #include <google/protobuf/stubs/mathutil.h>
  41. namespace google {
  42. namespace protobuf {
  43. namespace util {
  44. namespace converter {
  45. using util::Status;
  46. namespace {
  47. template <typename To, typename From>
  48. util::StatusOr<To> ValidateNumberConversion(To after, From before) {
  49. if (after == before &&
  50. MathUtil::Sign<From>(before) == MathUtil::Sign<To>(after)) {
  51. return after;
  52. } else {
  53. return util::InvalidArgumentError(
  54. std::is_integral<From>::value ? ValueAsString(before)
  55. : std::is_same<From, double>::value ? DoubleAsString(before)
  56. : FloatAsString(before));
  57. }
  58. }
  59. // For general conversion between
  60. // int32, int64, uint32, uint64, double and float
  61. // except conversion between double and float.
  62. template <typename To, typename From>
  63. util::StatusOr<To> NumberConvertAndCheck(From before) {
  64. if (std::is_same<From, To>::value) return before;
  65. To after = static_cast<To>(before);
  66. return ValidateNumberConversion(after, before);
  67. }
  68. // For conversion to integer types (int32, int64, uint32, uint64) from floating
  69. // point types (double, float) only.
  70. template <typename To, typename From>
  71. util::StatusOr<To> FloatingPointToIntConvertAndCheck(From before) {
  72. if (std::is_same<From, To>::value) return before;
  73. To after = static_cast<To>(before);
  74. return ValidateNumberConversion(after, before);
  75. }
  76. // For conversion between double and float only.
  77. util::StatusOr<double> FloatToDouble(float before) {
  78. // Casting float to double should just work as double has more precision
  79. // than float.
  80. return static_cast<double>(before);
  81. }
  82. util::StatusOr<float> DoubleToFloat(double before) {
  83. if (std::isnan(before)) {
  84. return std::numeric_limits<float>::quiet_NaN();
  85. } else if (!std::isfinite(before)) {
  86. // Converting a double +inf/-inf to float should just work.
  87. return static_cast<float>(before);
  88. } else if (before > std::numeric_limits<float>::max() ||
  89. before < -std::numeric_limits<float>::max()) {
  90. // Double value outside of the range of float.
  91. return util::InvalidArgumentError(DoubleAsString(before));
  92. } else {
  93. return static_cast<float>(before);
  94. }
  95. }
  96. } // namespace
  97. util::StatusOr<int32_t> DataPiece::ToInt32() const {
  98. if (type_ == TYPE_STRING)
  99. return StringToNumber<int32_t>(safe_strto32);
  100. if (type_ == TYPE_DOUBLE)
  101. return FloatingPointToIntConvertAndCheck<int32_t, double>(double_);
  102. if (type_ == TYPE_FLOAT)
  103. return FloatingPointToIntConvertAndCheck<int32_t, float>(float_);
  104. return GenericConvert<int32_t>();
  105. }
  106. util::StatusOr<uint32_t> DataPiece::ToUint32() const {
  107. if (type_ == TYPE_STRING)
  108. return StringToNumber<uint32_t>(safe_strtou32);
  109. if (type_ == TYPE_DOUBLE)
  110. return FloatingPointToIntConvertAndCheck<uint32_t, double>(double_);
  111. if (type_ == TYPE_FLOAT)
  112. return FloatingPointToIntConvertAndCheck<uint32_t, float>(float_);
  113. return GenericConvert<uint32_t>();
  114. }
  115. util::StatusOr<int64_t> DataPiece::ToInt64() const {
  116. if (type_ == TYPE_STRING)
  117. return StringToNumber<int64_t>(safe_strto64);
  118. if (type_ == TYPE_DOUBLE)
  119. return FloatingPointToIntConvertAndCheck<int64_t, double>(double_);
  120. if (type_ == TYPE_FLOAT)
  121. return FloatingPointToIntConvertAndCheck<int64_t, float>(float_);
  122. return GenericConvert<int64_t>();
  123. }
  124. util::StatusOr<uint64_t> DataPiece::ToUint64() const {
  125. if (type_ == TYPE_STRING)
  126. return StringToNumber<uint64_t>(safe_strtou64);
  127. if (type_ == TYPE_DOUBLE)
  128. return FloatingPointToIntConvertAndCheck<uint64_t, double>(double_);
  129. if (type_ == TYPE_FLOAT)
  130. return FloatingPointToIntConvertAndCheck<uint64_t, float>(float_);
  131. return GenericConvert<uint64_t>();
  132. }
  133. util::StatusOr<double> DataPiece::ToDouble() const {
  134. if (type_ == TYPE_FLOAT) {
  135. return FloatToDouble(float_);
  136. }
  137. if (type_ == TYPE_STRING) {
  138. if (str_ == "Infinity") return std::numeric_limits<double>::infinity();
  139. if (str_ == "-Infinity") return -std::numeric_limits<double>::infinity();
  140. if (str_ == "NaN") return std::numeric_limits<double>::quiet_NaN();
  141. util::StatusOr<double> value = StringToNumber<double>(safe_strtod);
  142. if (value.ok() && !std::isfinite(value.value())) {
  143. // safe_strtod converts out-of-range values to +inf/-inf, but we want
  144. // to report them as errors.
  145. return util::InvalidArgumentError(StrCat("\"", str_, "\""));
  146. } else {
  147. return value;
  148. }
  149. }
  150. return GenericConvert<double>();
  151. }
  152. util::StatusOr<float> DataPiece::ToFloat() const {
  153. if (type_ == TYPE_DOUBLE) {
  154. return DoubleToFloat(double_);
  155. }
  156. if (type_ == TYPE_STRING) {
  157. if (str_ == "Infinity") return std::numeric_limits<float>::infinity();
  158. if (str_ == "-Infinity") return -std::numeric_limits<float>::infinity();
  159. if (str_ == "NaN") return std::numeric_limits<float>::quiet_NaN();
  160. // SafeStrToFloat() is used instead of safe_strtof() because the later
  161. // does not fail on inputs like SimpleDtoa(DBL_MAX).
  162. return StringToNumber<float>(SafeStrToFloat);
  163. }
  164. return GenericConvert<float>();
  165. }
  166. util::StatusOr<bool> DataPiece::ToBool() const {
  167. switch (type_) {
  168. case TYPE_BOOL:
  169. return bool_;
  170. case TYPE_STRING:
  171. return StringToNumber<bool>(safe_strtob);
  172. default:
  173. return util::InvalidArgumentError(
  174. ValueAsStringOrDefault("Wrong type. Cannot convert to Bool."));
  175. }
  176. }
  177. util::StatusOr<std::string> DataPiece::ToString() const {
  178. switch (type_) {
  179. case TYPE_STRING:
  180. return std::string(str_);
  181. case TYPE_BYTES: {
  182. std::string base64;
  183. Base64Escape(str_, &base64);
  184. return base64;
  185. }
  186. default:
  187. return util::InvalidArgumentError(
  188. ValueAsStringOrDefault("Cannot convert to string."));
  189. }
  190. }
  191. std::string DataPiece::ValueAsStringOrDefault(
  192. StringPiece default_string) const {
  193. switch (type_) {
  194. case TYPE_INT32:
  195. return StrCat(i32_);
  196. case TYPE_INT64:
  197. return StrCat(i64_);
  198. case TYPE_UINT32:
  199. return StrCat(u32_);
  200. case TYPE_UINT64:
  201. return StrCat(u64_);
  202. case TYPE_DOUBLE:
  203. return DoubleAsString(double_);
  204. case TYPE_FLOAT:
  205. return FloatAsString(float_);
  206. case TYPE_BOOL:
  207. return SimpleBtoa(bool_);
  208. case TYPE_STRING:
  209. return StrCat("\"", str_.ToString(), "\"");
  210. case TYPE_BYTES: {
  211. std::string base64;
  212. WebSafeBase64Escape(str_, &base64);
  213. return StrCat("\"", base64, "\"");
  214. }
  215. case TYPE_NULL:
  216. return "null";
  217. default:
  218. return std::string(default_string);
  219. }
  220. }
  221. util::StatusOr<std::string> DataPiece::ToBytes() const {
  222. if (type_ == TYPE_BYTES) return str_.ToString();
  223. if (type_ == TYPE_STRING) {
  224. std::string decoded;
  225. if (!DecodeBase64(str_, &decoded)) {
  226. return util::InvalidArgumentError(
  227. ValueAsStringOrDefault("Invalid data in input."));
  228. }
  229. return decoded;
  230. } else {
  231. return util::InvalidArgumentError(ValueAsStringOrDefault(
  232. "Wrong type. Only String or Bytes can be converted to Bytes."));
  233. }
  234. }
  235. util::StatusOr<int> DataPiece::ToEnum(const google::protobuf::Enum* enum_type,
  236. bool use_lower_camel_for_enums,
  237. bool case_insensitive_enum_parsing,
  238. bool ignore_unknown_enum_values,
  239. bool* is_unknown_enum_value) const {
  240. if (type_ == TYPE_NULL) return google::protobuf::NULL_VALUE;
  241. if (type_ == TYPE_STRING) {
  242. // First try the given value as a name.
  243. std::string enum_name = std::string(str_);
  244. const google::protobuf::EnumValue* value =
  245. FindEnumValueByNameOrNull(enum_type, enum_name);
  246. if (value != nullptr) return value->number();
  247. // Check if int version of enum is sent as string.
  248. util::StatusOr<int32_t> int_value = ToInt32();
  249. if (int_value.ok()) {
  250. if (const google::protobuf::EnumValue* enum_value =
  251. FindEnumValueByNumberOrNull(enum_type, int_value.value())) {
  252. return enum_value->number();
  253. }
  254. }
  255. // Next try a normalized name.
  256. bool should_normalize_enum =
  257. case_insensitive_enum_parsing || use_lower_camel_for_enums;
  258. if (should_normalize_enum) {
  259. for (std::string::iterator it = enum_name.begin(); it != enum_name.end();
  260. ++it) {
  261. *it = *it == '-' ? '_' : ascii_toupper(*it);
  262. }
  263. value = FindEnumValueByNameOrNull(enum_type, enum_name);
  264. if (value != nullptr) return value->number();
  265. }
  266. // If use_lower_camel_for_enums is true try with enum name without
  267. // underscore. This will also accept camel case names as the enum_name has
  268. // been normalized before.
  269. if (use_lower_camel_for_enums) {
  270. value = FindEnumValueByNameWithoutUnderscoreOrNull(enum_type, enum_name);
  271. if (value != nullptr) return value->number();
  272. }
  273. // If ignore_unknown_enum_values is true an unknown enum value is ignored.
  274. if (ignore_unknown_enum_values) {
  275. *is_unknown_enum_value = true;
  276. if (enum_type->enumvalue_size() > 0) {
  277. return enum_type->enumvalue(0).number();
  278. }
  279. }
  280. } else {
  281. // We don't need to check whether the value is actually declared in the
  282. // enum because we preserve unknown enum values as well.
  283. return ToInt32();
  284. }
  285. return util::InvalidArgumentError(
  286. ValueAsStringOrDefault("Cannot find enum with given value."));
  287. }
  288. template <typename To>
  289. util::StatusOr<To> DataPiece::GenericConvert() const {
  290. switch (type_) {
  291. case TYPE_INT32:
  292. return NumberConvertAndCheck<To, int32_t>(i32_);
  293. case TYPE_INT64:
  294. return NumberConvertAndCheck<To, int64_t>(i64_);
  295. case TYPE_UINT32:
  296. return NumberConvertAndCheck<To, uint32_t>(u32_);
  297. case TYPE_UINT64:
  298. return NumberConvertAndCheck<To, uint64_t>(u64_);
  299. case TYPE_DOUBLE:
  300. return NumberConvertAndCheck<To, double>(double_);
  301. case TYPE_FLOAT:
  302. return NumberConvertAndCheck<To, float>(float_);
  303. default: // TYPE_ENUM, TYPE_STRING, TYPE_CORD, TYPE_BOOL
  304. return util::InvalidArgumentError(ValueAsStringOrDefault(
  305. "Wrong type. Bool, Enum, String and Cord not supported in "
  306. "GenericConvert."));
  307. }
  308. }
  309. template <typename To>
  310. util::StatusOr<To> DataPiece::StringToNumber(bool (*func)(StringPiece,
  311. To*)) const {
  312. if (str_.size() > 0 && (str_[0] == ' ' || str_[str_.size() - 1] == ' ')) {
  313. return util::InvalidArgumentError(StrCat("\"", str_, "\""));
  314. }
  315. To result;
  316. if (func(str_, &result)) return result;
  317. return util::InvalidArgumentError(
  318. StrCat("\"", std::string(str_), "\""));
  319. }
  320. bool DataPiece::DecodeBase64(StringPiece src, std::string* dest) const {
  321. // Try web-safe decode first, if it fails, try the non-web-safe decode.
  322. if (WebSafeBase64Unescape(src, dest)) {
  323. if (use_strict_base64_decoding_) {
  324. // In strict mode, check if the escaped version gives us the same value as
  325. // unescaped.
  326. std::string encoded;
  327. // WebSafeBase64Escape does no padding by default.
  328. WebSafeBase64Escape(*dest, &encoded);
  329. // Remove trailing padding '=' characters before comparison.
  330. StringPiece src_no_padding = StringPiece(src).substr(
  331. 0, HasSuffixString(src, "=") ? src.find_last_not_of('=') + 1
  332. : src.length());
  333. return encoded == src_no_padding;
  334. }
  335. return true;
  336. }
  337. if (Base64Unescape(src, dest)) {
  338. if (use_strict_base64_decoding_) {
  339. std::string encoded;
  340. Base64Escape(reinterpret_cast<const unsigned char*>(dest->data()),
  341. dest->length(), &encoded, false);
  342. StringPiece src_no_padding = StringPiece(src).substr(
  343. 0, HasSuffixString(src, "=") ? src.find_last_not_of('=') + 1
  344. : src.length());
  345. return encoded == src_no_padding;
  346. }
  347. return true;
  348. }
  349. return false;
  350. }
  351. void DataPiece::InternalCopy(const DataPiece& other) {
  352. type_ = other.type_;
  353. use_strict_base64_decoding_ = other.use_strict_base64_decoding_;
  354. switch (type_) {
  355. case TYPE_INT32:
  356. case TYPE_INT64:
  357. case TYPE_UINT32:
  358. case TYPE_UINT64:
  359. case TYPE_DOUBLE:
  360. case TYPE_FLOAT:
  361. case TYPE_BOOL:
  362. case TYPE_ENUM:
  363. case TYPE_NULL:
  364. case TYPE_BYTES:
  365. case TYPE_STRING: {
  366. str_ = other.str_;
  367. break;
  368. }
  369. }
  370. }
  371. } // namespace converter
  372. } // namespace util
  373. } // namespace protobuf
  374. } // namespace google