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
|
|
|
|
|
|
|
class ByteWriter
|
|
|
|
{
|
|
|
|
public:
|
2017-06-07 05:21:09 +03:00
|
|
|
explicit ByteWriter(nsTArray<uint8_t>& aData)
|
2014-12-23 06:36:09 +03:00
|
|
|
: mPtr(aData)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
~ByteWriter()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2017-02-01 11:17:29 +03:00
|
|
|
MOZ_MUST_USE bool WriteU8(uint8_t aByte)
|
2014-12-23 06:36:09 +03:00
|
|
|
{
|
2017-02-01 11:17:29 +03:00
|
|
|
return Write(&aByte, 1);
|
2014-12-23 06:36:09 +03:00
|
|
|
}
|
|
|
|
|
2017-02-01 11:17:29 +03:00
|
|
|
MOZ_MUST_USE bool WriteU16(uint16_t aShort)
|
2014-12-23 06:36:09 +03:00
|
|
|
{
|
|
|
|
uint8_t c[2];
|
|
|
|
mozilla::BigEndian::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
|
|
|
}
|
|
|
|
|
2017-02-01 11:17:29 +03:00
|
|
|
MOZ_MUST_USE bool WriteU32(uint32_t aLong)
|
2014-12-23 06:36:09 +03:00
|
|
|
{
|
|
|
|
uint8_t c[4];
|
|
|
|
mozilla::BigEndian::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
|
|
|
}
|
|
|
|
|
2017-02-01 11:17:29 +03:00
|
|
|
MOZ_MUST_USE bool Write32(int32_t aLong)
|
2014-12-23 06:36:09 +03:00
|
|
|
{
|
|
|
|
uint8_t c[4];
|
|
|
|
mozilla::BigEndian::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
|
|
|
}
|
|
|
|
|
2017-02-01 11:17:29 +03:00
|
|
|
MOZ_MUST_USE bool WriteU64(uint64_t aLongLong)
|
2014-12-23 06:36:09 +03:00
|
|
|
{
|
|
|
|
uint8_t c[8];
|
|
|
|
mozilla::BigEndian::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
|
|
|
}
|
|
|
|
|
2017-02-01 11:17:29 +03:00
|
|
|
MOZ_MUST_USE bool Write64(int64_t aLongLong)
|
2014-12-23 06:36:09 +03:00
|
|
|
{
|
|
|
|
uint8_t c[8];
|
|
|
|
mozilla::BigEndian::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
|
|
|
}
|
|
|
|
|
2017-02-01 11:17:29 +03:00
|
|
|
MOZ_MUST_USE bool Write(const uint8_t* aSrc, size_t aCount)
|
2014-12-23 06:36:09 +03:00
|
|
|
{
|
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
|