| 61 | } |
| 62 | |
| 63 | size_t Decode(const uint8_t* buffer, size_t buffer_size) |
| 64 | { |
| 65 | size_t bytes_read = DecodeAttributes(buffer, buffer_size); |
| 66 | |
| 67 | // We should only be decoding individual strings. |
| 68 | assert((GetAttributeMask() & DecodeAttrib) == DecodeAttrib); |
| 69 | assert((GetAttributeMask() & format::PointerAttributes::kIsArray) != format::PointerAttributes::kIsArray); |
| 70 | |
| 71 | if (!IsNull() && HasData()) |
| 72 | { |
| 73 | size_t string_len = GetLength(); |
| 74 | size_t alloc_len = string_len + 1; |
| 75 | |
| 76 | if (!is_memory_external_) |
| 77 | { |
| 78 | assert(data_ == nullptr); |
| 79 | |
| 80 | data_ = DecodeAllocator::Allocate<CharT>(alloc_len, false); |
| 81 | capacity_ = alloc_len; |
| 82 | bytes_read += ValueDecoder::DecodeArrayFrom<EncodeT>( |
| 83 | (buffer + bytes_read), (buffer_size - bytes_read), data_, string_len); |
| 84 | data_[string_len] = '\0'; |
| 85 | } |
| 86 | else |
| 87 | { |
| 88 | assert(data_ != nullptr); |
| 89 | |
| 90 | if (alloc_len <= capacity_) |
| 91 | { |
| 92 | ValueDecoder::DecodeArrayFrom<EncodeT>( |
| 93 | (buffer + bytes_read), (buffer_size - bytes_read), data_, string_len); |
| 94 | data_[string_len] = '\0'; |
| 95 | } |
| 96 | else |
| 97 | { |
| 98 | size_t truncate_len = capacity_ - 1; |
| 99 | ValueDecoder::DecodeArrayFrom<EncodeT>( |
| 100 | (buffer + bytes_read), (buffer_size - bytes_read), data_, truncate_len); |
| 101 | data_[truncate_len] = '\0'; |
| 102 | |
| 103 | GFXRECON_LOG_WARNING("String decoder's external memory capacity (%" PRIuPTR |
| 104 | ") is smaller than the decoded string size (%" PRIuPTR |
| 105 | "); data will be truncated", |
| 106 | capacity_, |
| 107 | alloc_len); |
| 108 | } |
| 109 | |
| 110 | // We always need to advance the position within the buffer by the amount of data that was expected to |
| 111 | // be decoded, not the actual amount of data decoded if capacity is too small to hold all of the data. |
| 112 | bytes_read += string_len * sizeof(EncodeT); |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | return bytes_read; |
| 117 | } |
| 118 | |
| 119 | private: |
| 120 | CharT* data_; |
no outgoing calls
no test coverage detected