店播爬取Python脚本

safe_numerics.h 6.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. #ifndef GOOGLE_PROTOBUF_PYTHON_CPP_SAFE_NUMERICS_H__
  31. #define GOOGLE_PROTOBUF_PYTHON_CPP_SAFE_NUMERICS_H__
  32. // Copied from chromium with only changes to the namespace.
  33. #include <limits>
  34. #include <google/protobuf/stubs/logging.h>
  35. #include <google/protobuf/stubs/common.h>
  36. namespace google {
  37. namespace protobuf {
  38. namespace python {
  39. template <bool SameSize, bool DestLarger,
  40. bool DestIsSigned, bool SourceIsSigned>
  41. struct IsValidNumericCastImpl;
  42. #define BASE_NUMERIC_CAST_CASE_SPECIALIZATION(A, B, C, D, Code) \
  43. template <> struct IsValidNumericCastImpl<A, B, C, D> { \
  44. template <class Source, class DestBounds> static inline bool Test( \
  45. Source source, DestBounds min, DestBounds max) { \
  46. return Code; \
  47. } \
  48. }
  49. #define BASE_NUMERIC_CAST_CASE_SAME_SIZE(DestSigned, SourceSigned, Code) \
  50. BASE_NUMERIC_CAST_CASE_SPECIALIZATION( \
  51. true, true, DestSigned, SourceSigned, Code); \
  52. BASE_NUMERIC_CAST_CASE_SPECIALIZATION( \
  53. true, false, DestSigned, SourceSigned, Code)
  54. #define BASE_NUMERIC_CAST_CASE_SOURCE_LARGER(DestSigned, SourceSigned, Code) \
  55. BASE_NUMERIC_CAST_CASE_SPECIALIZATION( \
  56. false, false, DestSigned, SourceSigned, Code); \
  57. #define BASE_NUMERIC_CAST_CASE_DEST_LARGER(DestSigned, SourceSigned, Code) \
  58. BASE_NUMERIC_CAST_CASE_SPECIALIZATION( \
  59. false, true, DestSigned, SourceSigned, Code); \
  60. // The three top level cases are:
  61. // - Same size
  62. // - Source larger
  63. // - Dest larger
  64. // And for each of those three cases, we handle the 4 different possibilities
  65. // of signed and unsigned. This gives 12 cases to handle, which we enumerate
  66. // below.
  67. //
  68. // The last argument in each of the macros is the actual comparison code. It
  69. // has three arguments available, source (the value), and min/max which are
  70. // the ranges of the destination.
  71. // These are the cases where both types have the same size.
  72. // Both signed.
  73. BASE_NUMERIC_CAST_CASE_SAME_SIZE(true, true, true);
  74. // Both unsigned.
  75. BASE_NUMERIC_CAST_CASE_SAME_SIZE(false, false, true);
  76. // Dest unsigned, Source signed.
  77. BASE_NUMERIC_CAST_CASE_SAME_SIZE(false, true, source >= 0);
  78. // Dest signed, Source unsigned.
  79. // This cast is OK because Dest's max must be less than Source's.
  80. BASE_NUMERIC_CAST_CASE_SAME_SIZE(true, false,
  81. source <= static_cast<Source>(max));
  82. // These are the cases where Source is larger.
  83. // Both unsigned.
  84. BASE_NUMERIC_CAST_CASE_SOURCE_LARGER(false, false, source <= max);
  85. // Both signed.
  86. BASE_NUMERIC_CAST_CASE_SOURCE_LARGER(true, true,
  87. source >= min && source <= max);
  88. // Dest is unsigned, Source is signed.
  89. BASE_NUMERIC_CAST_CASE_SOURCE_LARGER(false, true,
  90. source >= 0 && source <= max);
  91. // Dest is signed, Source is unsigned.
  92. // This cast is OK because Dest's max must be less than Source's.
  93. BASE_NUMERIC_CAST_CASE_SOURCE_LARGER(true, false,
  94. source <= static_cast<Source>(max));
  95. // These are the cases where Dest is larger.
  96. // Both unsigned.
  97. BASE_NUMERIC_CAST_CASE_DEST_LARGER(false, false, true);
  98. // Both signed.
  99. BASE_NUMERIC_CAST_CASE_DEST_LARGER(true, true, true);
  100. // Dest is unsigned, Source is signed.
  101. BASE_NUMERIC_CAST_CASE_DEST_LARGER(false, true, source >= 0);
  102. // Dest is signed, Source is unsigned.
  103. BASE_NUMERIC_CAST_CASE_DEST_LARGER(true, false, true);
  104. #undef BASE_NUMERIC_CAST_CASE_SPECIALIZATION
  105. #undef BASE_NUMERIC_CAST_CASE_SAME_SIZE
  106. #undef BASE_NUMERIC_CAST_CASE_SOURCE_LARGER
  107. #undef BASE_NUMERIC_CAST_CASE_DEST_LARGER
  108. // The main test for whether the conversion will under or overflow.
  109. template <class Dest, class Source>
  110. inline bool IsValidNumericCast(Source source) {
  111. typedef std::numeric_limits<Source> SourceLimits;
  112. typedef std::numeric_limits<Dest> DestLimits;
  113. static_assert(SourceLimits::is_specialized, "argument must be numeric");
  114. static_assert(SourceLimits::is_integer, "argument must be integral");
  115. static_assert(DestLimits::is_specialized, "result must be numeric");
  116. static_assert(DestLimits::is_integer, "result must be integral");
  117. return IsValidNumericCastImpl<
  118. sizeof(Dest) == sizeof(Source),
  119. (sizeof(Dest) > sizeof(Source)),
  120. DestLimits::is_signed,
  121. SourceLimits::is_signed>::Test(
  122. source,
  123. DestLimits::min(),
  124. DestLimits::max());
  125. }
  126. // checked_numeric_cast<> is analogous to static_cast<> for numeric types,
  127. // except that it CHECKs that the specified numeric conversion will not
  128. // overflow or underflow. Floating point arguments are not currently allowed
  129. // (this is static_asserted), though this could be supported if necessary.
  130. template <class Dest, class Source>
  131. inline Dest checked_numeric_cast(Source source) {
  132. GOOGLE_CHECK(IsValidNumericCast<Dest>(source));
  133. return static_cast<Dest>(source);
  134. }
  135. } // namespace python
  136. } // namespace protobuf
  137. } // namespace google
  138. #endif // GOOGLE_PROTOBUF_PYTHON_CPP_SAFE_NUMERICS_H__