datapiece.cc 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  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 <google/protobuf/struct.pb.h>
  32. #include <google/protobuf/type.pb.h>
  33. #include <google/protobuf/descriptor.h>
  34. #include <google/protobuf/util/internal/utility.h>
  35. #include <google/protobuf/stubs/strutil.h>
  36. #include <google/protobuf/stubs/mathlimits.h>
  37. #include <google/protobuf/stubs/mathutil.h>
  38. namespace google {
  39. namespace protobuf {
  40. namespace util {
  41. namespace converter {
  42. using google::protobuf::EnumDescriptor;
  43. using google::protobuf::EnumValueDescriptor;
  44. ;
  45. ;
  46. ;
  47. using util::error::Code;
  48. using util::Status;
  49. using util::StatusOr;
  50. namespace {
  51. inline Status InvalidArgument(StringPiece value_str) {
  52. return Status(util::error::INVALID_ARGUMENT, value_str);
  53. }
  54. template <typename To, typename From>
  55. StatusOr<To> ValidateNumberConversion(To after, From before) {
  56. if (after == before &&
  57. MathUtil::Sign<From>(before) == MathUtil::Sign<To>(after)) {
  58. return after;
  59. } else {
  60. return InvalidArgument(std::is_integral<From>::value
  61. ? ValueAsString(before)
  62. : std::is_same<From, double>::value
  63. ? DoubleAsString(before)
  64. : FloatAsString(before));
  65. }
  66. }
  67. // For general conversion between
  68. // int32, int64, uint32, uint64, double and float
  69. // except conversion between double and float.
  70. template <typename To, typename From>
  71. StatusOr<To> NumberConvertAndCheck(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 to integer types (int32, int64, uint32, uint64) from floating
  77. // point types (double, float) only.
  78. template <typename To, typename From>
  79. StatusOr<To> FloatingPointToIntConvertAndCheck(From before) {
  80. if (std::is_same<From, To>::value) return before;
  81. To after = static_cast<To>(before);
  82. return ValidateNumberConversion(after, before);
  83. }
  84. // For conversion between double and float only.
  85. StatusOr<double> FloatToDouble(float before) {
  86. // Casting float to double should just work as double has more precision
  87. // than float.
  88. return static_cast<double>(before);
  89. }
  90. StatusOr<float> DoubleToFloat(double before) {
  91. if (MathLimits<double>::IsNaN(before)) {
  92. return std::numeric_limits<float>::quiet_NaN();
  93. } else if (!MathLimits<double>::IsFinite(before)) {
  94. // Converting a double +inf/-inf to float should just work.
  95. return static_cast<float>(before);
  96. } else if (before > std::numeric_limits<float>::max() ||
  97. before < -std::numeric_limits<float>::max()) {
  98. // Double value outside of the range of float.
  99. return InvalidArgument(DoubleAsString(before));
  100. } else {
  101. return static_cast<float>(before);
  102. }
  103. }
  104. } // namespace
  105. StatusOr<int32> DataPiece::ToInt32() const {
  106. if (type_ == TYPE_STRING) return StringToNumber<int32>(safe_strto32);
  107. if (type_ == TYPE_DOUBLE)
  108. return FloatingPointToIntConvertAndCheck<int32, double>(double_);
  109. if (type_ == TYPE_FLOAT)
  110. return FloatingPointToIntConvertAndCheck<int32, float>(float_);
  111. return GenericConvert<int32>();
  112. }
  113. StatusOr<uint32> DataPiece::ToUint32() const {
  114. if (type_ == TYPE_STRING) return StringToNumber<uint32>(safe_strtou32);
  115. if (type_ == TYPE_DOUBLE)
  116. return FloatingPointToIntConvertAndCheck<uint32, double>(double_);
  117. if (type_ == TYPE_FLOAT)
  118. return FloatingPointToIntConvertAndCheck<uint32, float>(float_);
  119. return GenericConvert<uint32>();
  120. }
  121. StatusOr<int64> DataPiece::ToInt64() const {
  122. if (type_ == TYPE_STRING) return StringToNumber<int64>(safe_strto64);
  123. if (type_ == TYPE_DOUBLE)
  124. return FloatingPointToIntConvertAndCheck<int64, double>(double_);
  125. if (type_ == TYPE_FLOAT)
  126. return FloatingPointToIntConvertAndCheck<int64, float>(float_);
  127. return GenericConvert<int64>();
  128. }
  129. StatusOr<uint64> DataPiece::ToUint64() const {
  130. if (type_ == TYPE_STRING) return StringToNumber<uint64>(safe_strtou64);
  131. if (type_ == TYPE_DOUBLE)
  132. return FloatingPointToIntConvertAndCheck<uint64, double>(double_);
  133. if (type_ == TYPE_FLOAT)
  134. return FloatingPointToIntConvertAndCheck<uint64, float>(float_);
  135. return GenericConvert<uint64>();
  136. }
  137. StatusOr<double> DataPiece::ToDouble() const {
  138. if (type_ == TYPE_FLOAT) {
  139. return FloatToDouble(float_);
  140. }
  141. if (type_ == TYPE_STRING) {
  142. if (str_ == "Infinity") return std::numeric_limits<double>::infinity();
  143. if (str_ == "-Infinity") return -std::numeric_limits<double>::infinity();
  144. if (str_ == "NaN") return std::numeric_limits<double>::quiet_NaN();
  145. StatusOr<double> value = StringToNumber<double>(safe_strtod);
  146. if (value.ok() && !MathLimits<double>::IsFinite(value.ValueOrDie())) {
  147. // safe_strtod converts out-of-range values to +inf/-inf, but we want
  148. // to report them as errors.
  149. return InvalidArgument(StrCat("\"", str_, "\""));
  150. } else {
  151. return value;
  152. }
  153. }
  154. return GenericConvert<double>();
  155. }
  156. StatusOr<float> DataPiece::ToFloat() const {
  157. if (type_ == TYPE_DOUBLE) {
  158. return DoubleToFloat(double_);
  159. }
  160. if (type_ == TYPE_STRING) {
  161. if (str_ == "Infinity") return std::numeric_limits<float>::infinity();
  162. if (str_ == "-Infinity") return -std::numeric_limits<float>::infinity();
  163. if (str_ == "NaN") return std::numeric_limits<float>::quiet_NaN();
  164. // SafeStrToFloat() is used instead of safe_strtof() because the later
  165. // does not fail on inputs like SimpleDtoa(DBL_MAX).
  166. return StringToNumber<float>(SafeStrToFloat);
  167. }
  168. return GenericConvert<float>();
  169. }
  170. StatusOr<bool> DataPiece::ToBool() const {
  171. switch (type_) {
  172. case TYPE_BOOL:
  173. return bool_;
  174. case TYPE_STRING:
  175. return StringToNumber<bool>(safe_strtob);
  176. default:
  177. return InvalidArgument(
  178. ValueAsStringOrDefault("Wrong type. Cannot convert to Bool."));
  179. }
  180. }
  181. StatusOr<string> DataPiece::ToString() const {
  182. switch (type_) {
  183. case TYPE_STRING:
  184. return str_.ToString();
  185. case TYPE_BYTES: {
  186. string base64;
  187. Base64Escape(str_, &base64);
  188. return base64;
  189. }
  190. default:
  191. return InvalidArgument(
  192. ValueAsStringOrDefault("Cannot convert to string."));
  193. }
  194. }
  195. string DataPiece::ValueAsStringOrDefault(StringPiece default_string) const {
  196. switch (type_) {
  197. case TYPE_INT32:
  198. return SimpleItoa(i32_);
  199. case TYPE_INT64:
  200. return SimpleItoa(i64_);
  201. case TYPE_UINT32:
  202. return SimpleItoa(u32_);
  203. case TYPE_UINT64:
  204. return SimpleItoa(u64_);
  205. case TYPE_DOUBLE:
  206. return DoubleAsString(double_);
  207. case TYPE_FLOAT:
  208. return FloatAsString(float_);
  209. case TYPE_BOOL:
  210. return SimpleBtoa(bool_);
  211. case TYPE_STRING:
  212. return StrCat("\"", str_.ToString(), "\"");
  213. case TYPE_BYTES: {
  214. string base64;
  215. WebSafeBase64Escape(str_, &base64);
  216. return StrCat("\"", base64, "\"");
  217. }
  218. case TYPE_NULL:
  219. return "null";
  220. default:
  221. return default_string.ToString();
  222. }
  223. }
  224. StatusOr<string> DataPiece::ToBytes() const {
  225. if (type_ == TYPE_BYTES) return str_.ToString();
  226. if (type_ == TYPE_STRING) {
  227. string decoded;
  228. if (!DecodeBase64(str_, &decoded)) {
  229. return InvalidArgument(ValueAsStringOrDefault("Invalid data in input."));
  230. }
  231. return decoded;
  232. } else {
  233. return InvalidArgument(ValueAsStringOrDefault(
  234. "Wrong type. Only String or Bytes can be converted to Bytes."));
  235. }
  236. }
  237. StatusOr<int> DataPiece::ToEnum(const google::protobuf::Enum* enum_type,
  238. bool use_lower_camel_for_enums,
  239. bool ignore_unknown_enum_values) 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. string enum_name = str_.ToString();
  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. StatusOr<int32> int_value = ToInt32();
  249. if (int_value.ok()) {
  250. if (const google::protobuf::EnumValue* enum_value =
  251. FindEnumValueByNumberOrNull(enum_type, int_value.ValueOrDie())) {
  252. return enum_value->number();
  253. }
  254. }
  255. // Next try a normalized name.
  256. for (string::iterator it = enum_name.begin(); it != enum_name.end(); ++it) {
  257. *it = *it == '-' ? '_' : ascii_toupper(*it);
  258. }
  259. value = FindEnumValueByNameOrNull(enum_type, enum_name);
  260. if (value != nullptr) return value->number();
  261. // If use_lower_camel_for_enums is true try with enum name without
  262. // underscore. This will also accept camel case names as the enum_name has
  263. // been normalized before.
  264. if (use_lower_camel_for_enums) {
  265. value = FindEnumValueByNameWithoutUnderscoreOrNull(enum_type, enum_name);
  266. if (value != nullptr) return value->number();
  267. }
  268. // If ignore_unknown_enum_values is true an unknown enum value is treated
  269. // as the default
  270. if (ignore_unknown_enum_values) return enum_type->enumvalue(0).number();
  271. } else {
  272. // We don't need to check whether the value is actually declared in the
  273. // enum because we preserve unknown enum values as well.
  274. return ToInt32();
  275. }
  276. return InvalidArgument(
  277. ValueAsStringOrDefault("Cannot find enum with given value."));
  278. }
  279. template <typename To>
  280. StatusOr<To> DataPiece::GenericConvert() const {
  281. switch (type_) {
  282. case TYPE_INT32:
  283. return NumberConvertAndCheck<To, int32>(i32_);
  284. case TYPE_INT64:
  285. return NumberConvertAndCheck<To, int64>(i64_);
  286. case TYPE_UINT32:
  287. return NumberConvertAndCheck<To, uint32>(u32_);
  288. case TYPE_UINT64:
  289. return NumberConvertAndCheck<To, uint64>(u64_);
  290. case TYPE_DOUBLE:
  291. return NumberConvertAndCheck<To, double>(double_);
  292. case TYPE_FLOAT:
  293. return NumberConvertAndCheck<To, float>(float_);
  294. default: // TYPE_ENUM, TYPE_STRING, TYPE_CORD, TYPE_BOOL
  295. return InvalidArgument(ValueAsStringOrDefault(
  296. "Wrong type. Bool, Enum, String and Cord not supported in "
  297. "GenericConvert."));
  298. }
  299. }
  300. template <typename To>
  301. StatusOr<To> DataPiece::StringToNumber(bool (*func)(StringPiece, To*)) const {
  302. if (str_.size() > 0 && (str_[0] == ' ' || str_[str_.size() - 1] == ' ')) {
  303. return InvalidArgument(StrCat("\"", str_, "\""));
  304. }
  305. To result;
  306. if (func(str_, &result)) return result;
  307. return InvalidArgument(StrCat("\"", str_.ToString(), "\""));
  308. }
  309. bool DataPiece::DecodeBase64(StringPiece src, string* dest) const {
  310. // Try web-safe decode first, if it fails, try the non-web-safe decode.
  311. if (WebSafeBase64Unescape(src, dest)) {
  312. if (use_strict_base64_decoding_) {
  313. // In strict mode, check if the escaped version gives us the same value as
  314. // unescaped.
  315. string encoded;
  316. // WebSafeBase64Escape does no padding by default.
  317. WebSafeBase64Escape(*dest, &encoded);
  318. // Remove trailing padding '=' characters before comparison.
  319. StringPiece src_no_padding = StringPiece(src).substr(
  320. 0, StringEndsWith(src, "=") ? src.find_last_not_of('=') + 1
  321. : src.length());
  322. return encoded == src_no_padding;
  323. }
  324. return true;
  325. }
  326. if (Base64Unescape(src, dest)) {
  327. if (use_strict_base64_decoding_) {
  328. string encoded;
  329. Base64Escape(
  330. reinterpret_cast<const unsigned char*>(dest->data()), dest->length(),
  331. &encoded, false);
  332. StringPiece src_no_padding = StringPiece(src).substr(
  333. 0, StringEndsWith(src, "=") ? src.find_last_not_of('=') + 1
  334. : src.length());
  335. return encoded == src_no_padding;
  336. }
  337. return true;
  338. }
  339. return false;
  340. }
  341. void DataPiece::InternalCopy(const DataPiece& other) {
  342. type_ = other.type_;
  343. use_strict_base64_decoding_ = other.use_strict_base64_decoding_;
  344. switch (type_) {
  345. case TYPE_INT32:
  346. case TYPE_INT64:
  347. case TYPE_UINT32:
  348. case TYPE_UINT64:
  349. case TYPE_DOUBLE:
  350. case TYPE_FLOAT:
  351. case TYPE_BOOL:
  352. case TYPE_ENUM:
  353. case TYPE_NULL:
  354. case TYPE_BYTES:
  355. case TYPE_STRING: {
  356. str_ = other.str_;
  357. break;
  358. }
  359. }
  360. }
  361. } // namespace converter
  362. } // namespace util
  363. } // namespace protobuf
  364. } // namespace google