xref: /aosp_15_r20/external/webrtc/net/dcsctp/packet/bounded_byte_writer.h (revision d9f758449e529ab9291ac668be2861e7a55c2422)
1 /*
2  *  Copyright (c) 2021 The WebRTC project authors. All Rights Reserved.
3  *
4  *  Use of this source code is governed by a BSD-style license
5  *  that can be found in the LICENSE file in the root of the source
6  *  tree. An additional intellectual property rights grant can be found
7  *  in the file PATENTS.  All contributing project authors may
8  *  be found in the AUTHORS file in the root of the source tree.
9  */
10 
11 #ifndef NET_DCSCTP_PACKET_BOUNDED_BYTE_WRITER_H_
12 #define NET_DCSCTP_PACKET_BOUNDED_BYTE_WRITER_H_
13 
14 #include <algorithm>
15 
16 #include "api/array_view.h"
17 
18 namespace dcsctp {
19 
20 // TODO(boivie): These generic functions - and possibly this entire class -
21 // could be a candidate to have added to rtc_base/. They should use compiler
22 // intrinsics as well.
23 namespace internal {
24 // Stores a 8-bit unsigned word at `data`.
StoreBigEndian8(uint8_t * data,uint8_t val)25 inline void StoreBigEndian8(uint8_t* data, uint8_t val) {
26   data[0] = val;
27 }
28 
29 // Stores a 16-bit unsigned word at `data`.
StoreBigEndian16(uint8_t * data,uint16_t val)30 inline void StoreBigEndian16(uint8_t* data, uint16_t val) {
31   data[0] = val >> 8;
32   data[1] = val;
33 }
34 
35 // Stores a 32-bit unsigned word at `data`.
StoreBigEndian32(uint8_t * data,uint32_t val)36 inline void StoreBigEndian32(uint8_t* data, uint32_t val) {
37   data[0] = val >> 24;
38   data[1] = val >> 16;
39   data[2] = val >> 8;
40   data[3] = val;
41 }
42 }  // namespace internal
43 
44 // BoundedByteWriter wraps an ArrayView and divides it into two parts; A fixed
45 // size - which is the template parameter - and a variable size, which is what
46 // remains in `data` after the `FixedSize`.
47 //
48 // The BoundedByteWriter provides methods to write big endian numbers to the
49 // FixedSize portion of the buffer, and these are written with static bounds
50 // checking, to avoid out-of-bounds accesses without a run-time penalty.
51 //
52 // The variable sized portion can either be used to create sub-writers, which
53 // themselves would provide compile-time bounds-checking, or data can be copied
54 // to it.
55 template <int FixedSize>
56 class BoundedByteWriter {
57  public:
BoundedByteWriter(rtc::ArrayView<uint8_t> data)58   explicit BoundedByteWriter(rtc::ArrayView<uint8_t> data) : data_(data) {
59     RTC_CHECK(data.size() >= FixedSize);
60   }
61 
62   template <size_t offset>
Store8(uint8_t value)63   void Store8(uint8_t value) {
64     static_assert(offset + sizeof(uint8_t) <= FixedSize, "Out-of-bounds");
65     internal::StoreBigEndian8(&data_[offset], value);
66   }
67 
68   template <size_t offset>
Store16(uint16_t value)69   void Store16(uint16_t value) {
70     static_assert(offset + sizeof(uint16_t) <= FixedSize, "Out-of-bounds");
71     static_assert((offset % sizeof(uint16_t)) == 0, "Unaligned access");
72     internal::StoreBigEndian16(&data_[offset], value);
73   }
74 
75   template <size_t offset>
Store32(uint32_t value)76   void Store32(uint32_t value) {
77     static_assert(offset + sizeof(uint32_t) <= FixedSize, "Out-of-bounds");
78     static_assert((offset % sizeof(uint32_t)) == 0, "Unaligned access");
79     internal::StoreBigEndian32(&data_[offset], value);
80   }
81 
82   template <size_t SubSize>
sub_writer(size_t variable_offset)83   BoundedByteWriter<SubSize> sub_writer(size_t variable_offset) {
84     RTC_CHECK(FixedSize + variable_offset + SubSize <= data_.size());
85 
86     return BoundedByteWriter<SubSize>(
87         data_.subview(FixedSize + variable_offset, SubSize));
88   }
89 
CopyToVariableData(rtc::ArrayView<const uint8_t> source)90   void CopyToVariableData(rtc::ArrayView<const uint8_t> source) {
91     size_t copy_size = std::min(source.size(), data_.size() - FixedSize);
92     if (source.data() == nullptr || copy_size == 0) {
93       return;
94     }
95     memcpy(data_.data() + FixedSize, source.data(), copy_size);
96   }
97 
98  private:
99   rtc::ArrayView<uint8_t> data_;
100 };
101 }  // namespace dcsctp
102 
103 #endif  // NET_DCSCTP_PACKET_BOUNDED_BYTE_WRITER_H_
104