Debian Codegraph @

src/RequestPacket.cpp

raw
1 /*
2 * Copyright (C) 2010-2021 Team Kodi (https://kodi.tv)
3 * Copyright (C) 2010 Alwin Esch (Team Kodi)
4 *
5 * SPDX-License-Identifier: GPL-2.0-or-later
6 * See LICENSE.md for more information.
7 */
8
9 #include "RequestPacket.h"
10
11 #include "TCPSocket.h"
12 #include "Tools.h"
13 #include "vnsicommand.h"
14
15 #include <assert.h>
16 #include <stdlib.h>
17 #include <string.h>
18
19 uint32_t cRequestPacket::m_serialNumberCounter = 1;
20
21 cRequestPacket::~cRequestPacket()
22 {
23 free(m_buffer);
24 }
25
26 void cRequestPacket::init(uint32_t topcode,
27 bool stream,
28 bool setUserDataLength,
29 size_t userDataLength)
30 {
31 assert(m_buffer == nullptr);
32
33 if (setUserDataLength)
34 {
35 m_bufSize = m_headerLength + userDataLength;
36 lengthSet = true;
37 }
38 else
39 {
40 m_bufSize = 512;
41 userDataLength = 0; // so the below will write a zero
42 }
43
44 m_buffer = static_cast<uint8_t*>(malloc(m_bufSize));
45 if (!m_buffer)
46 throw std::bad_alloc();
47
48 if (!stream)
49 m_channel = VNSI_CHANNEL_REQUEST_RESPONSE;
50 else
51 m_channel = VNSI_CHANNEL_STREAM;
52 m_serialNumber = m_serialNumberCounter++;
53 m_opcode = topcode;
54
55 uint32_t ul;
56 ul = htonl(m_channel);
57 memcpy(&m_buffer[0], &ul, sizeof(uint32_t));
58 ul = htonl(m_serialNumber);
59 memcpy(&m_buffer[4], &ul, sizeof(uint32_t));
60 ul = htonl(m_opcode);
61 memcpy(&m_buffer[8], &ul, sizeof(uint32_t));
62 ul = htonl(userDataLength);
63 memcpy(&m_buffer[m_userDataLenPos], &ul, sizeof(uint32_t));
64 m_bufUsed = m_headerLength;
65 }
66
67 void cRequestPacket::add_String(const char* string)
68 {
69 size_t len = strlen(string) + 1;
70 checkExtend(len);
71 memcpy(m_buffer + m_bufUsed, string, len);
72 m_bufUsed += len;
73 if (!lengthSet)
74 {
75 uint32_t tmp = htonl(m_bufUsed - m_headerLength);
76 memcpy(&m_buffer[m_userDataLenPos], &tmp, sizeof(uint32_t));
77 }
78 }
79
80 void cRequestPacket::add_U8(uint8_t c)
81 {
82 checkExtend(sizeof(uint8_t));
83 m_buffer[m_bufUsed] = c;
84 m_bufUsed += sizeof(uint8_t);
85 if (!lengthSet)
86 {
87 uint32_t tmp = htonl(m_bufUsed - m_headerLength);
88 memcpy(&m_buffer[m_userDataLenPos], &tmp, sizeof(uint32_t));
89 }
90 }
91
92 void cRequestPacket::add_S32(int32_t l)
93 {
94 checkExtend(sizeof(int32_t));
95 int32_t tmp = htonl(l);
96 memcpy(&m_buffer[m_bufUsed], &tmp, sizeof(int32_t));
97 m_bufUsed += sizeof(int32_t);
98 if (!lengthSet)
99 {
100 uint32_t tmp = htonl(m_bufUsed - m_headerLength);
101 memcpy(&m_buffer[m_userDataLenPos], &tmp, sizeof(uint32_t));
102 }
103 }
104
105 void cRequestPacket::add_U32(uint32_t ul)
106 {
107 checkExtend(sizeof(uint32_t));
108 uint32_t tmp = htonl(ul);
109 memcpy(&m_buffer[m_bufUsed], &tmp, sizeof(uint32_t));
110 m_bufUsed += sizeof(uint32_t);
111 if (!lengthSet)
112 {
113 uint32_t tmp = htonl(m_bufUsed - m_headerLength);
114 memcpy(&m_buffer[m_userDataLenPos], &tmp, sizeof(uint32_t));
115 }
116 }
117
118 void cRequestPacket::add_U64(uint64_t ull)
119 {
120 checkExtend(sizeof(uint64_t));
121 uint64_t tmp = htonll(ull);
122 memcpy(&m_buffer[m_bufUsed], &tmp, sizeof(uint64_t));
123 m_bufUsed += sizeof(uint64_t);
124 if (!lengthSet)
125 {
126 uint32_t tmp = htonl(m_bufUsed - m_headerLength);
127 memcpy(&m_buffer[m_userDataLenPos], &tmp, sizeof(uint32_t));
128 }
129 }
130
131 void cRequestPacket::add_S64(int64_t ll)
132 {
133 checkExtend(sizeof(int64_t));
134 int64_t tmp = htonll(ll);
135 memcpy(&m_buffer[m_bufUsed], &tmp, sizeof(int64_t));
136 m_bufUsed += sizeof(int64_t);
137 if (!lengthSet)
138 {
139 uint32_t tmp = htonl(m_bufUsed - m_headerLength);
140 memcpy(&m_buffer[m_userDataLenPos], &tmp, sizeof(uint32_t));
141 }
142 }
143
144 void cRequestPacket::checkExtend(size_t by)
145 {
146 if (lengthSet)
147 return;
148 if ((m_bufUsed + by) <= m_bufSize)
149 return;
150 uint8_t* newBuf = static_cast<uint8_t*>(realloc(m_buffer, m_bufUsed + by));
151 if (!newBuf)
152 {
153 newBuf = static_cast<uint8_t*>(malloc(m_bufUsed + by));
154 if (!newBuf)
155 {
156 throw std::bad_alloc();
157 }
158 memcpy(newBuf, m_buffer, m_bufUsed);
159 free(m_buffer);
160 }
161 m_buffer = newBuf;
162 m_bufSize = m_bufUsed + by;
163 }