tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

ByteWriter.h (1447B)


      1 /* This Source Code Form is subject to the terms of the Mozilla Public
      2 * License, v. 2.0. If a copy of the MPL was not distributed with this
      3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
      4 
      5 #ifndef BYTE_WRITER_H_
      6 #define BYTE_WRITER_H_
      7 
      8 #include "nsTArray.h"
      9 
     10 namespace mozilla {
     11 
     12 template <typename Endianess>
     13 class ByteWriter {
     14 public:
     15  explicit ByteWriter(nsTArray<uint8_t>& aData) : mPtr(aData) {}
     16  ~ByteWriter() = default;
     17 
     18  [[nodiscard]] bool WriteU8(uint8_t aByte) { return Write(&aByte, 1); }
     19 
     20  [[nodiscard]] bool WriteU16(uint16_t aShort) {
     21    uint8_t c[2];
     22    Endianess::writeUint16(&c[0], aShort);
     23    return Write(&c[0], 2);
     24  }
     25 
     26  [[nodiscard]] bool WriteU32(uint32_t aLong) {
     27    uint8_t c[4];
     28    Endianess::writeUint32(&c[0], aLong);
     29    return Write(&c[0], 4);
     30  }
     31 
     32  [[nodiscard]] bool Write32(int32_t aLong) {
     33    uint8_t c[4];
     34    Endianess::writeInt32(&c[0], aLong);
     35    return Write(&c[0], 4);
     36  }
     37 
     38  [[nodiscard]] bool WriteU64(uint64_t aLongLong) {
     39    uint8_t c[8];
     40    Endianess::writeUint64(&c[0], aLongLong);
     41    return Write(&c[0], 8);
     42  }
     43 
     44  [[nodiscard]] bool Write64(int64_t aLongLong) {
     45    uint8_t c[8];
     46    Endianess::writeInt64(&c[0], aLongLong);
     47    return Write(&c[0], 8);
     48  }
     49 
     50  [[nodiscard]] bool Write(const uint8_t* aSrc, size_t aCount) {
     51    return mPtr.AppendElements(aSrc, aCount, mozilla::fallible);
     52  }
     53 
     54 private:
     55  nsTArray<uint8_t>& mPtr;
     56 };
     57 
     58 }  // namespace mozilla
     59 
     60 #endif