Changeset View
Changeset View
Standalone View
Standalone View
extern/draco/draco/src/draco/core/varint_decoding.h
- This file was moved from extern/draco/dracoenc/src/draco/core/varint_decoding.h.
| Show All 16 Lines | |||||
| #include <type_traits> | #include <type_traits> | ||||
| #include "draco/core/bit_utils.h" | #include "draco/core/bit_utils.h" | ||||
| #include "draco/core/decoder_buffer.h" | #include "draco/core/decoder_buffer.h" | ||||
| namespace draco { | namespace draco { | ||||
| // Decodes a specified integer as varint. Note that the IntTypeT must be the | namespace { | ||||
| // same as the one used in the corresponding EncodeVarint() call. | |||||
| // Decodes a specified unsigned integer as varint. |depth| is the current | |||||
| // recursion call depth. The first call to the function must be 1. | |||||
| template <typename IntTypeT> | template <typename IntTypeT> | ||||
| bool DecodeVarint(IntTypeT *out_val, DecoderBuffer *buffer) { | bool DecodeVarintUnsigned(int depth, IntTypeT *out_val, DecoderBuffer *buffer) { | ||||
| if (std::is_unsigned<IntTypeT>::value) { | constexpr IntTypeT max_depth = sizeof(IntTypeT) + 1 + (sizeof(IntTypeT) >> 3); | ||||
| if (depth > max_depth) { | |||||
| return false; | |||||
| } | |||||
| // Coding of unsigned values. | // Coding of unsigned values. | ||||
| // 0-6 bit - data | // 0-6 bit - data | ||||
| // 7 bit - next byte? | // 7 bit - next byte? | ||||
| uint8_t in; | uint8_t in; | ||||
| if (!buffer->Decode(&in)) | if (!buffer->Decode(&in)) { | ||||
| return false; | return false; | ||||
| } | |||||
| if (in & (1 << 7)) { | if (in & (1 << 7)) { | ||||
| // Next byte is available, decode it first. | // Next byte is available, decode it first. | ||||
| if (!DecodeVarint<IntTypeT>(out_val, buffer)) | if (!DecodeVarintUnsigned<IntTypeT>(depth + 1, out_val, buffer)) { | ||||
| return false; | return false; | ||||
| } | |||||
| // Append decoded info from this byte. | // Append decoded info from this byte. | ||||
| *out_val <<= 7; | *out_val <<= 7; | ||||
| *out_val |= in & ((1 << 7) - 1); | *out_val |= in & ((1 << 7) - 1); | ||||
| } else { | } else { | ||||
| // Last byte reached | // Last byte reached | ||||
| *out_val = in; | *out_val = in; | ||||
| } | } | ||||
| return true; | |||||
| } | |||||
| } // namespace | |||||
| // Decodes a specified integer as varint. Note that the IntTypeT must be the | |||||
| // same as the one used in the corresponding EncodeVarint() call. | |||||
| template <typename IntTypeT> | |||||
| bool DecodeVarint(IntTypeT *out_val, DecoderBuffer *buffer) { | |||||
| if (std::is_unsigned<IntTypeT>::value) { | |||||
| if (!DecodeVarintUnsigned<IntTypeT>(1, out_val, buffer)) { | |||||
| return false; | |||||
| } | |||||
| } else { | } else { | ||||
| // IntTypeT is a signed value. Decode the symbol and convert to signed. | // IntTypeT is a signed value. Decode the symbol and convert to signed. | ||||
| typename std::make_unsigned<IntTypeT>::type symbol; | typename std::make_unsigned<IntTypeT>::type symbol; | ||||
| if (!DecodeVarint(&symbol, buffer)) | if (!DecodeVarintUnsigned(1, &symbol, buffer)) { | ||||
| return false; | return false; | ||||
| } | |||||
| *out_val = ConvertSymbolToSignedInt(symbol); | *out_val = ConvertSymbolToSignedInt(symbol); | ||||
| } | } | ||||
| return true; | return true; | ||||
| } | } | ||||
| } // namespace draco | } // namespace draco | ||||
| #endif // DRACO_CORE_VARINT_DECODING_H_ | #endif // DRACO_CORE_VARINT_DECODING_H_ | ||||