Bug 1729469 [wpt PR 30378] - Stretch nested columns to the block-size of the outer...
[gecko.git] / dom / quota / EncryptedBlock.h
blob703fede8fdc092f238a7057d1e6907ff6458aec3
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 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 file,
5 * You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #ifndef mozilla_dom_quota_EncryptedBlock_h
8 #define mozilla_dom_quota_EncryptedBlock_h
10 #include <cstdint>
11 #include <cstring>
12 #include <limits>
13 #include "mozilla/Assertions.h"
14 #include "mozilla/Span.h"
15 #include "nsTArray.h"
17 namespace mozilla::dom::quota {
19 // An encrypted block has the following format:
20 // - one basic block containing a uint16_t stating the actual payload length
21 // - one basic block containing the cipher prefix (tyipically a nonce)
22 // - encrypted payload up to the remainder of the specified overall size
23 // We currently assume the basic block size is the same as the cipher prefix
24 // length.
26 // XXX Actually, we don't need the actual payload length in every block. Only
27 // the last block may be incomplete. The tricky thing is just that it might be
28 // incomplete by just one or two bytes.
29 template <size_t CipherPrefixLength, size_t BasicBlockSize>
30 class EncryptedBlock {
31 public:
32 explicit EncryptedBlock(const size_t aOverallSize) {
33 MOZ_RELEASE_ASSERT(aOverallSize >
34 CipherPrefixOffset() + CipherPrefixLength);
35 MOZ_RELEASE_ASSERT(aOverallSize <= std::numeric_limits<uint16_t>::max());
36 // XXX Do we need this to be fallible? Then we need a factory/init function.
37 // But maybe that's not necessary as the block size is not user-provided and
38 // small.
39 mData.SetLength(aOverallSize);
40 SetActualPayloadLength(MaxPayloadLength());
43 size_t MaxPayloadLength() const {
44 return mData.Length() - CipherPrefixLength - CipherPrefixOffset();
47 void SetActualPayloadLength(uint16_t aActualPayloadLength) {
48 memcpy(mData.Elements(), &aActualPayloadLength, sizeof(uint16_t));
50 size_t ActualPayloadLength() const {
51 return *reinterpret_cast<const uint16_t*>(mData.Elements());
54 using ConstSpan = Span<const uint8_t>;
55 using MutableSpan = Span<uint8_t>;
57 ConstSpan CipherPrefix() const {
58 return WholeBlock().Subspan(CipherPrefixOffset(), CipherPrefixLength);
60 MutableSpan MutableCipherPrefix() {
61 return MutableWholeBlock().Subspan(CipherPrefixOffset(),
62 CipherPrefixLength);
65 ConstSpan Payload() const {
66 return WholeBlock()
67 .SplitAt(CipherPrefixOffset() + CipherPrefixLength)
68 .second.First(RoundedUpToBasicBlockSize(ActualPayloadLength()));
70 MutableSpan MutablePayload() {
71 return MutableWholeBlock()
72 .SplitAt(CipherPrefixOffset() + CipherPrefixLength)
73 .second.First(RoundedUpToBasicBlockSize(ActualPayloadLength()));
76 ConstSpan WholeBlock() const { return mData; }
77 MutableSpan MutableWholeBlock() { return mData; }
79 private:
80 static constexpr size_t CipherPrefixOffset() {
81 return RoundedUpToBasicBlockSize(sizeof(uint16_t));
84 static constexpr size_t RoundedUpToBasicBlockSize(const size_t aValue) {
85 return (aValue + BasicBlockSize - 1) / BasicBlockSize * BasicBlockSize;
88 nsTArray<uint8_t> mData; ///< XXX use some "safe memory" here?
91 } // namespace mozilla::dom::quota
93 #endif