2014-12-23 06:36:09 +03:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
#ifndef BYTE_WRITER_H_
|
|
|
|
#define BYTE_WRITER_H_
|
|
|
|
|
2016-05-22 23:31:11 +03:00
|
|
|
#include "mozilla/EndianUtils.h"
|
2014-12-23 06:36:09 +03:00
|
|
|
#include "nsTArray.h"
|
|
|
|
|
2017-11-17 03:53:25 +03:00
|
|
|
namespace mozilla {
|
2014-12-23 06:36:09 +03:00
|
|
|
|
2019-05-21 13:10:04 +03:00
|
|
|
template <typename Endianess>
|
2014-12-23 06:36:09 +03:00
|
|
|
class ByteWriter {
|
|
|
|
public:
|
2017-06-07 05:21:09 +03:00
|
|
|
explicit ByteWriter(nsTArray<uint8_t>& aData) : mPtr(aData) {}
|
2020-03-04 18:39:20 +03:00
|
|
|
~ByteWriter() = default;
|
2014-12-23 06:36:09 +03:00
|
|
|
|
2020-04-27 18:18:07 +03:00
|
|
|
[[nodiscard]] bool WriteU8(uint8_t aByte) { return Write(&aByte, 1); }
|
2014-12-23 06:36:09 +03:00
|
|
|
|
2020-04-27 18:18:07 +03:00
|
|
|
[[nodiscard]] bool WriteU16(uint16_t aShort) {
|
2014-12-23 06:36:09 +03:00
|
|
|
uint8_t c[2];
|
2019-05-21 13:10:04 +03:00
|
|
|
Endianess::writeUint16(&c[0], aShort);
|
2017-02-01 11:17:29 +03:00
|
|
|
return Write(&c[0], 2);
|
2014-12-23 06:36:09 +03:00
|
|
|
}
|
|
|
|
|
2020-04-27 18:18:07 +03:00
|
|
|
[[nodiscard]] bool WriteU32(uint32_t aLong) {
|
2014-12-23 06:36:09 +03:00
|
|
|
uint8_t c[4];
|
2019-05-21 13:10:04 +03:00
|
|
|
Endianess::writeUint32(&c[0], aLong);
|
2017-02-01 11:17:29 +03:00
|
|
|
return Write(&c[0], 4);
|
2014-12-23 06:36:09 +03:00
|
|
|
}
|
|
|
|
|
2020-04-27 18:18:07 +03:00
|
|
|
[[nodiscard]] bool Write32(int32_t aLong) {
|
2014-12-23 06:36:09 +03:00
|
|
|
uint8_t c[4];
|
2019-05-21 13:10:04 +03:00
|
|
|
Endianess::writeInt32(&c[0], aLong);
|
2017-02-01 11:17:29 +03:00
|
|
|
return Write(&c[0], 4);
|
2014-12-23 06:36:09 +03:00
|
|
|
}
|
|
|
|
|
2020-04-27 18:18:07 +03:00
|
|
|
[[nodiscard]] bool WriteU64(uint64_t aLongLong) {
|
2014-12-23 06:36:09 +03:00
|
|
|
uint8_t c[8];
|
2019-05-21 13:10:04 +03:00
|
|
|
Endianess::writeUint64(&c[0], aLongLong);
|
2017-02-01 11:17:29 +03:00
|
|
|
return Write(&c[0], 8);
|
2014-12-23 06:36:09 +03:00
|
|
|
}
|
|
|
|
|
2020-04-27 18:18:07 +03:00
|
|
|
[[nodiscard]] bool Write64(int64_t aLongLong) {
|
2014-12-23 06:36:09 +03:00
|
|
|
uint8_t c[8];
|
2019-05-21 13:10:04 +03:00
|
|
|
Endianess::writeInt64(&c[0], aLongLong);
|
2017-02-01 11:17:29 +03:00
|
|
|
return Write(&c[0], 8);
|
2014-12-23 06:36:09 +03:00
|
|
|
}
|
|
|
|
|
2020-04-27 18:18:07 +03:00
|
|
|
[[nodiscard]] bool Write(const uint8_t* aSrc, size_t aCount) {
|
2017-06-07 05:21:09 +03:00
|
|
|
return mPtr.AppendElements(aSrc, aCount, mozilla::fallible);
|
2014-12-23 06:36:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2017-06-07 05:21:09 +03:00
|
|
|
nsTArray<uint8_t>& mPtr;
|
2014-12-23 06:36:09 +03:00
|
|
|
};
|
2016-10-13 07:33:53 +03:00
|
|
|
|
2017-11-17 03:53:25 +03:00
|
|
|
} // namespace mozilla
|
2014-12-23 06:36:09 +03:00
|
|
|
|
|
|
|
#endif
|