Line data Source code
1 : /*
2 : * If not stated otherwise in this file or this component's LICENSE file the
3 : * following copyright and licenses apply:
4 : *
5 : * Copyright 2022 Sky UK
6 : *
7 : * Licensed under the Apache License, Version 2.0 (the "License");
8 : * you may not use this file except in compliance with the License.
9 : * You may obtain a copy of the License at
10 : *
11 : * http://www.apache.org/licenses/LICENSE-2.0
12 : *
13 : * Unless required by applicable law or agreed to in writing, software
14 : * distributed under the License is distributed on an "AS IS" BASIS,
15 : * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 : * See the License for the specific language governing permissions and
17 : * limitations under the License.
18 : */
19 :
20 : #include "ByteWriter.h"
21 : #include <cstring>
22 :
23 : namespace firebolt::rialto::common
24 : {
25 0 : size_t ByteWriter::writeByte(uint8_t *buf, size_t off, uint8_t byte) const
26 : {
27 0 : buf[off++] = byte;
28 0 : return off;
29 : }
30 :
31 39 : size_t ByteWriter::writeBytes(uint8_t *buf, size_t off, const uint8_t *bytes, size_t count) const
32 : {
33 39 : if (count)
34 37 : memcpy(buf + off, bytes, count);
35 39 : return off + count;
36 : }
37 :
38 76 : size_t ByteWriter::fillBytes(uint8_t *buf, size_t off, uint8_t byte, size_t count) const
39 : {
40 76 : if (count)
41 76 : memset(buf + off, byte, count);
42 76 : return off + count;
43 : }
44 :
45 128 : size_t ByteWriter::writeUint32(uint8_t *buf, size_t off, uint32_t val) const
46 : {
47 128 : buf[off++] = (uint8_t)((val & 0x000000ff) >> 0);
48 128 : buf[off++] = (uint8_t)((val & 0x0000ff00) >> 8);
49 128 : buf[off++] = (uint8_t)((val & 0x00ff0000) >> 16);
50 128 : buf[off++] = (uint8_t)((val & 0xff000000) >> 24);
51 128 : return off;
52 : }
53 :
54 26 : size_t ByteWriter::writeInt64(uint8_t *buf, size_t off, int64_t val) const
55 : {
56 26 : buf[off++] = (uint8_t)((val & 0x00000000000000ff) >> 0);
57 26 : buf[off++] = (uint8_t)((val & 0x000000000000ff00) >> 8);
58 26 : buf[off++] = (uint8_t)((val & 0x0000000000ff0000) >> 16);
59 26 : buf[off++] = (uint8_t)((val & 0x00000000ff000000) >> 24);
60 26 : buf[off++] = (uint8_t)((val & 0x000000ff00000000) >> 32);
61 26 : buf[off++] = (uint8_t)((val & 0x0000ff0000000000) >> 40);
62 26 : buf[off++] = (uint8_t)((val & 0x00ff000000000000) >> 48);
63 26 : buf[off++] = (uint8_t)((val & 0xff00000000000000) >> 56);
64 26 : return off;
65 : }
66 :
67 0 : size_t ByteWriter::writeCString(uint8_t *buf, size_t off, const char *str) const
68 : {
69 0 : while (*str)
70 : {
71 0 : buf[off++] = *str++;
72 : }
73 0 : buf[off++] = *str;
74 0 : return off;
75 : }
76 : } // namespace firebolt::rialto::common
|