CapsuleDecoder.cpp (1693B)
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 #include "CapsuleDecoder.h" 8 9 #include "ErrorList.h" 10 #include "mozilla/net/NeqoHttp3Conn.h" 11 12 namespace mozilla::net { 13 14 CapsuleDecoder::CapsuleDecoder(const uint8_t* aData, size_t aLength) { 15 NeqoDecoder::Init(aData, aLength, getter_AddRefs(mDecoder)); 16 } 17 18 CapsuleDecoder::~CapsuleDecoder() = default; 19 20 template <> 21 Maybe<uint32_t> CapsuleDecoder::DecodeUint<uint32_t>() { 22 uint32_t res = 0; 23 if (mDecoder->DecodeUint32(&res)) { 24 return Some(res); 25 } 26 return Nothing(); 27 } 28 29 size_t CapsuleDecoder::BytesRemaining() { return mDecoder->Remaining(); } 30 31 size_t CapsuleDecoder::CurrentPos() { return mDecoder->Offset(); } 32 33 Maybe<uint64_t> CapsuleDecoder::DecodeVarint() { 34 uint64_t v = 0; 35 if (mDecoder->DecodeVarint(&v)) { 36 return Some(v); 37 } 38 39 return Nothing(); 40 } 41 42 // Decodes arbitrary data: returns a span over the next n bytes, if available. 43 Maybe<mozilla::Span<const uint8_t>> CapsuleDecoder::Decode(size_t n) { 44 const uint8_t* buffer = nullptr; 45 uint32_t length = 0; 46 if (mDecoder->Decode(n, &buffer, &length)) { 47 return Some(mozilla::Span<const uint8_t>(buffer, length)); 48 } 49 50 return Nothing(); 51 } 52 53 mozilla::Span<const uint8_t> CapsuleDecoder::GetRemaining() { 54 const uint8_t* buffer = nullptr; 55 uint32_t length = 0; 56 mDecoder->DecodeRemainder(&buffer, &length); 57 return mozilla::Span<const uint8_t>(buffer, length); 58 } 59 60 } // namespace mozilla::net