CapsuleDecoder.h (1280B)
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 2 /* vim: set sw=2 ts=8 et tw=80 : */ 3 /* This Source Code Form is subject to the terms of the Mozilla Public 4 * License, v. 2.0. If a copy of the MPL was not distributed with this 5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 6 7 #ifndef mozilla_net_capsule_decoder_h 8 #define mozilla_net_capsule_decoder_h 9 10 #include "mozilla/RefPtr.h" 11 #include "mozilla/Maybe.h" 12 #include "mozilla/Span.h" 13 14 namespace mozilla::net { 15 16 class NeqoDecoder; 17 18 // CapsuleDecoder provides methods to decode capsule data from a given buffer. 19 // It does not own the underlying data, so the caller must ensure that the 20 // buffer remains valid for the lifetime of the CapsuleDecoder instance. 21 class MOZ_STACK_CLASS CapsuleDecoder final { 22 public: 23 explicit CapsuleDecoder(const uint8_t* aData, size_t aLength); 24 ~CapsuleDecoder(); 25 26 // Return Nothing() when there is not enough data to decode. 27 template <typename T> 28 Maybe<T> DecodeUint(); 29 30 Maybe<uint64_t> DecodeVarint(); 31 Maybe<mozilla::Span<const uint8_t>> Decode(size_t n); 32 mozilla::Span<const uint8_t> GetRemaining(); 33 size_t BytesRemaining(); 34 35 size_t CurrentPos(); 36 37 private: 38 RefPtr<NeqoDecoder> mDecoder; 39 }; 40 41 } // namespace mozilla::net 42 43 #endif