/work/obj-fuzz/dist/include/ByteWriter.h
Line | Count | Source (jump to first uncovered line) |
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 "mozilla/EndianUtils.h" |
9 | | #include "nsTArray.h" |
10 | | |
11 | | namespace mozilla { |
12 | | |
13 | | class ByteWriter |
14 | | { |
15 | | public: |
16 | | explicit ByteWriter(nsTArray<uint8_t>& aData) |
17 | | : mPtr(aData) |
18 | 0 | { |
19 | 0 | } |
20 | | ~ByteWriter() |
21 | 0 | { |
22 | 0 | } |
23 | | |
24 | | MOZ_MUST_USE bool WriteU8(uint8_t aByte) |
25 | 0 | { |
26 | 0 | return Write(&aByte, 1); |
27 | 0 | } |
28 | | |
29 | | MOZ_MUST_USE bool WriteU16(uint16_t aShort) |
30 | 0 | { |
31 | 0 | uint8_t c[2]; |
32 | 0 | mozilla::BigEndian::writeUint16(&c[0], aShort); |
33 | 0 | return Write(&c[0], 2); |
34 | 0 | } |
35 | | |
36 | | MOZ_MUST_USE bool WriteU32(uint32_t aLong) |
37 | 0 | { |
38 | 0 | uint8_t c[4]; |
39 | 0 | mozilla::BigEndian::writeUint32(&c[0], aLong); |
40 | 0 | return Write(&c[0], 4); |
41 | 0 | } |
42 | | |
43 | | MOZ_MUST_USE bool Write32(int32_t aLong) |
44 | 0 | { |
45 | 0 | uint8_t c[4]; |
46 | 0 | mozilla::BigEndian::writeInt32(&c[0], aLong); |
47 | 0 | return Write(&c[0], 4); |
48 | 0 | } |
49 | | |
50 | | MOZ_MUST_USE bool WriteU64(uint64_t aLongLong) |
51 | 0 | { |
52 | 0 | uint8_t c[8]; |
53 | 0 | mozilla::BigEndian::writeUint64(&c[0], aLongLong); |
54 | 0 | return Write(&c[0], 8); |
55 | 0 | } |
56 | | |
57 | | MOZ_MUST_USE bool Write64(int64_t aLongLong) |
58 | 0 | { |
59 | 0 | uint8_t c[8]; |
60 | 0 | mozilla::BigEndian::writeInt64(&c[0], aLongLong); |
61 | 0 | return Write(&c[0], 8); |
62 | 0 | } |
63 | | |
64 | | MOZ_MUST_USE bool Write(const uint8_t* aSrc, size_t aCount) |
65 | 0 | { |
66 | 0 | return mPtr.AppendElements(aSrc, aCount, mozilla::fallible); |
67 | 0 | } |
68 | | |
69 | | private: |
70 | | nsTArray<uint8_t>& mPtr; |
71 | | }; |
72 | | |
73 | | } // namespace mozilla |
74 | | |
75 | | #endif |