| 3867 | |
| 3868 | template <typename T> |
| 3869 | FORY_ALWAYS_INLINE T read_varint_at_checked(Buffer &buffer, uint32_t &offset, |
| 3870 | Error &error) { |
| 3871 | uint32_t bytes_read = 0; |
| 3872 | if constexpr (std::is_same_v<T, int32_t> || std::is_same_v<T, int>) { |
| 3873 | uint32_t raw = buffer.get_var_uint32(offset, &bytes_read); |
| 3874 | if (FORY_PREDICT_FALSE(bytes_read == 0)) { |
| 3875 | error.set_buffer_out_of_bound(offset, 1, buffer.size()); |
| 3876 | return T{}; |
| 3877 | } |
| 3878 | offset += bytes_read; |
| 3879 | return static_cast<T>((raw >> 1) ^ (~(raw & 1) + 1)); |
| 3880 | } else if constexpr (std::is_same_v<T, int64_t> || |
| 3881 | std::is_same_v<T, long long>) { |
| 3882 | uint64_t raw = buffer.get_var_uint64(offset, &bytes_read); |
| 3883 | if (FORY_PREDICT_FALSE(bytes_read == 0)) { |
| 3884 | error.set_buffer_out_of_bound(offset, 1, buffer.size()); |
| 3885 | return T{}; |
| 3886 | } |
| 3887 | offset += bytes_read; |
| 3888 | return static_cast<T>((raw >> 1) ^ (~(raw & 1) + 1)); |
| 3889 | } else if constexpr (std::is_same_v<T, uint32_t> || |
| 3890 | std::is_same_v<T, unsigned int>) { |
| 3891 | uint32_t raw = buffer.get_var_uint32(offset, &bytes_read); |
| 3892 | if (FORY_PREDICT_FALSE(bytes_read == 0)) { |
| 3893 | error.set_buffer_out_of_bound(offset, 1, buffer.size()); |
| 3894 | return T{}; |
| 3895 | } |
| 3896 | offset += bytes_read; |
| 3897 | return static_cast<T>(raw); |
| 3898 | } else if constexpr (std::is_same_v<T, uint64_t> || |
| 3899 | std::is_same_v<T, unsigned long long>) { |
| 3900 | uint64_t raw = buffer.get_var_uint64(offset, &bytes_read); |
| 3901 | if (FORY_PREDICT_FALSE(bytes_read == 0)) { |
| 3902 | error.set_buffer_out_of_bound(offset, 1, buffer.size()); |
| 3903 | return T{}; |
| 3904 | } |
| 3905 | offset += bytes_read; |
| 3906 | return static_cast<T>(raw); |
| 3907 | } else { |
| 3908 | static_assert(sizeof(T) == 0, "Unsupported checked varint type"); |
| 3909 | return T{}; |
| 3910 | } |
| 3911 | } |
| 3912 | |
| 3913 | template <size_t Bytes> |
| 3914 | FORY_ALWAYS_INLINE bool ensure_offset_readable(Buffer &buffer, uint32_t offset, |
nothing calls this directly
no test coverage detected