GCC Code Coverage Report


Directory: ./
File: common.cpp
Date: 2026-12-01 12:27:28
Exec Total Coverage
Lines: 572 694 82.4%
Functions: 47 56 83.9%
Branches: 409 819 49.9%

Line Branch Exec Source
1 /*
2 * This file is part of the Falcon Player (FPP) and is Copyright (C)
3 * 2013-2022 by the Falcon Player Developers.
4 *
5 * The Falcon Player (FPP) is free software, and is covered under
6 * multiple Open Source licenses. Please see the included 'LICENSES'
7 * file for descriptions of what files are covered by each license.
8 *
9 * This source file is covered under the LGPL v2.1 as described in the
10 * included LICENSE.LGPL file.
11 */
12
13 #include "fpp-pch.h"
14
15 #include "fpp-json.h"
16
17 #ifndef _GNU_SOURCE
18 #define _GNU_SOURCE
19 #endif
20
21 #if defined(__APPLE__)
22 #include <copyfile.h>
23 #else
24 #include <sys/sendfile.h>
25 #endif
26
27 #include <arpa/inet.h>
28 #include <curl/curl.h>
29 #include <net/if.h>
30 #include <netinet/in.h>
31 #include <sys/file.h>
32 #include <sys/ioctl.h>
33 #include <sys/socket.h>
34 #include <sys/stat.h>
35 #include <sys/time.h>
36 #include <atomic>
37 #include <cstdint>
38 #include <ctime>
39 #include <ctype.h>
40 #include <cxxabi.h>
41 #include <dirent.h>
42 #include <errno.h>
43 #include <fcntl.h>
44 #include <fstream>
45 #include <ifaddrs.h>
46 #include <iomanip>
47 #include <iterator>
48 #include <list>
49 #include <map>
50 #include <mutex>
51 #include <netdb.h>
52 #include <pwd.h>
53 #include <sstream>
54 #include <stdio.h>
55 #include <stdlib.h>
56 #include <string.h>
57 #include <string>
58 #include <unistd.h>
59 #include <utility>
60 #include <vector>
61
62 #include "common.h"
63 #include "CurlSocket.h"
64 #include "fppversion.h"
65 #include "log.h"
66
67 /*
68 * Dump memory block in hex and human-readable formats
69 */
70 3066 void HexDump(const char* title, const void* data, int len, FPPLoggerInstance& facility, int perLine) {
71 3066 int l = 0;
72 3066 int i = 0;
73 3066 int x = 0;
74 3066 int y = 0;
75
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3066 times.
3066 if (perLine <= 0) {
76 ✗ perLine = 16;
77 }
78 3066 unsigned char* ch = (unsigned char*)data;
79 3066 unsigned char* str = new unsigned char[perLine + 1];
80
81 3066 int maxLen = perLine * 7 + 20;
82 3066 char* tmpStr = new char[maxLen];
83 3066 tmpStr[0] = 0;
84
85 // The title is data, not a format string.
86
2/4
✓ Branch 0 taken 3066 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3066 times.
✗ Branch 3 not taken.
3066 if (title && strlen(title)) {
87 3066 snprintf(tmpStr, maxLen, "%s: (%d bytes)\n", title, len);
88 3066 LogInfo(facility, "%s", tmpStr);
89 }
90 // Nothing to dump. The tail below appends to tmpStr, which was never
91 // started with an offset (left uninitialized with no title) and would
92 // be logged again.
93
2/4
✓ Branch 0 taken 3066 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 3066 times.
3066 if (len <= 0 || !data) {
94 ✗ delete[] tmpStr;
95 ✗ delete[] str;
96 ✗ return;
97 }
98
99
2/2
✓ Branch 0 taken 26452 times.
✓ Branch 1 taken 3066 times.
29518 while (l < len) {
100
2/2
✓ Branch 0 taken 3066 times.
✓ Branch 1 taken 23386 times.
26452 if (x == 0) {
101 3066 snprintf(tmpStr, maxLen, "%06x: ", i);
102 }
103
104
2/2
✓ Branch 0 taken 26341 times.
✓ Branch 1 taken 111 times.
26452 if (x < perLine) {
105 26341 snprintf(tmpStr + strlen(tmpStr), maxLen - strlen(tmpStr), "%02x ", *ch & 0xFF);
106 26341 str[x] = *ch;
107 26341 x++;
108 26341 i++;
109 } else {
110 111 snprintf(tmpStr + strlen(tmpStr), maxLen - strlen(tmpStr), " ");
111
2/2
✓ Branch 0 taken 1776 times.
✓ Branch 1 taken 111 times.
1887 for (; x > 0; x--) {
112
2/4
✓ Branch 0 taken 1776 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 1776 times.
1776 if (str[perLine - x] == '%' || str[perLine - x] == '\\') {
113 // these are escapes for the Log call, so don't display them
114 ✗ snprintf(tmpStr + strlen(tmpStr), maxLen - strlen(tmpStr), ".");
115
4/4
✓ Branch 0 taken 1657 times.
✓ Branch 1 taken 119 times.
✓ Branch 2 taken 9 times.
✓ Branch 3 taken 1648 times.
1776 } else if (isgraph(str[perLine - x]) || str[perLine - x] == ' ') {
116 128 snprintf(tmpStr + strlen(tmpStr), maxLen - strlen(tmpStr), "%c", str[perLine - x]);
117 } else {
118 1648 snprintf(tmpStr + strlen(tmpStr), maxLen - strlen(tmpStr), ".");
119 }
120 }
121
122 111 snprintf(tmpStr + strlen(tmpStr), maxLen - strlen(tmpStr), "\n");
123 111 LogInfo(facility, "%s", tmpStr);
124 111 x = 0;
125
126 111 snprintf(tmpStr, maxLen, "%06x: ", i);
127 111 snprintf(tmpStr + strlen(tmpStr), maxLen - strlen(tmpStr), "%02x ", *ch & 0xFF);
128 111 str[x] = *ch;
129 111 x++;
130 111 i++;
131 }
132
133 26452 l++;
134 26452 ch++;
135 }
136
2/2
✓ Branch 0 taken 24380 times.
✓ Branch 1 taken 3066 times.
27446 for (y = x; y < perLine; y++) {
137 24380 snprintf(tmpStr + strlen(tmpStr), maxLen - strlen(tmpStr), " ");
138 }
139 3066 snprintf(tmpStr + strlen(tmpStr), maxLen - strlen(tmpStr), " ");
140
2/2
✓ Branch 0 taken 24676 times.
✓ Branch 1 taken 3066 times.
27742 for (y = 0; y < x; y++) {
141
4/4
✓ Branch 0 taken 24603 times.
✓ Branch 1 taken 73 times.
✓ Branch 2 taken 75 times.
✓ Branch 3 taken 24528 times.
24676 if (str[y] == '%' || str[y] == '\\') {
142 // these are escapes for the Log call, so don't display them
143 148 snprintf(tmpStr + strlen(tmpStr), maxLen - strlen(tmpStr), ".");
144
4/4
✓ Branch 0 taken 17727 times.
✓ Branch 1 taken 6801 times.
✓ Branch 2 taken 72 times.
✓ Branch 3 taken 17655 times.
24528 } else if (isgraph(str[y]) || str[y] == ' ') {
145 6873 snprintf(tmpStr + strlen(tmpStr), maxLen - strlen(tmpStr), "%c", str[y]);
146 } else {
147 17655 snprintf(tmpStr + strlen(tmpStr), maxLen - strlen(tmpStr), ".");
148 }
149 }
150
151 3066 snprintf(tmpStr + strlen(tmpStr), maxLen - strlen(tmpStr), "\n");
152 3066 LogInfo(facility, "%s", tmpStr);
153
1/2
✓ Branch 0 taken 3066 times.
✗ Branch 1 not taken.
3066 delete[] tmpStr;
154
1/2
✓ Branch 0 taken 3066 times.
✗ Branch 1 not taken.
3066 delete[] str;
155 }
156
157 104 int CheckForHostSpecificFile(const std::string& hostname, std::string& filename) {
158
1/2
✓ Branch 0 taken 104 times.
✗ Branch 1 not taken.
104 std::string localFilename = filename;
159
160 104 int len = (int)localFilename.length();
161
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 104 times.
104 if (len < 4)
162 ✗ return 0;
163 104 int extIdx = 0;
164
165 // Check for 3 or 4-digit extension
166
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 104 times.
104 if (localFilename[len - 4] == '.') {
167 ✗ extIdx = len - 4;
168
5/6
✓ Branch 0 taken 104 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 97 times.
✓ Branch 3 taken 7 times.
✓ Branch 4 taken 97 times.
✓ Branch 5 taken 7 times.
104 } else if (len >= 5 && localFilename[len - 5] == '.') {
169 97 extIdx = len - 5;
170 }
171
172
2/2
✓ Branch 0 taken 97 times.
✓ Branch 1 taken 7 times.
104 if (extIdx) {
173 // Preserve the extension including the dot
174
1/2
✓ Branch 0 taken 97 times.
✗ Branch 1 not taken.
97 std::string ext = localFilename.substr(extIdx);
175
1/2
✓ Branch 0 taken 97 times.
✗ Branch 1 not taken.
97 localFilename = localFilename.substr(0, extIdx);
176
1/2
✓ Branch 0 taken 97 times.
✗ Branch 1 not taken.
97 localFilename += "-";
177
1/2
✓ Branch 0 taken 97 times.
✗ Branch 1 not taken.
97 localFilename += hostname;
178
1/2
✓ Branch 0 taken 97 times.
✗ Branch 1 not taken.
97 localFilename += ext;
179
180
2/4
✓ Branch 0 taken 97 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 97 times.
97 if (FileExists(localFilename)) {
181 ✗ LogDebug(VB_SEQUENCE, "Found %s to use instead of %s\n",
182 localFilename.c_str(), filename.c_str());
183 ✗ filename = localFilename;
184 ✗ return 1;
185 } else {
186 // Replace hyphen with an underscore and recheck
187 97 localFilename[extIdx] = '_';
188
2/4
✓ Branch 0 taken 97 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 97 times.
97 if (FileExists(localFilename)) {
189 ✗ LogDebug(VB_SEQUENCE, "Found %s to use instead of %s\n",
190 localFilename.c_str(), filename.c_str());
191 ✗ filename = localFilename;
192 ✗ return 1;
193 }
194 }
195
1/2
✓ Branch 0 taken 97 times.
✗ Branch 1 not taken.
97 }
196
197 104 return 0;
198 104 }
199 43 int CheckForHostSpecificFile(const char* hostname, char* filename) {
200
1/2
✓ Branch 0 taken 43 times.
✗ Branch 1 not taken.
86 std::string f = filename;
201
3/6
✓ Branch 0 taken 43 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 43 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 43 times.
86 if (CheckForHostSpecificFile(hostname, f)) {
202 // In-tree callers pass a 2048-byte tmpFilename (Sequence.cpp). Clamp to
203 // that size to avoid overflowing a smaller plugin-provided buffer while
204 // preserving the original strcpy semantics for fitting names. Truncation
205 // is treated as "no host file" so the caller keeps the original name.
206 ✗ constexpr size_t kSafeLimit = 2048;
207 ✗ if (f.size() >= kSafeLimit) {
208 ✗ LogErr(VB_SEQUENCE, "Host-specific filename too long (%zu >= %zu), keeping %s\n",
209 f.size(), kSafeLimit, filename);
210 ✗ return 0;
211 }
212 ✗ snprintf(filename, kSafeLimit, "%s", f.c_str());
213 ✗ return 1;
214 }
215 43 return 0;
216 43 }
217 ✗ char* FindInterfaceForIP(char* ip) {
218 struct ifaddrs *ifaddr, *ifa;
219 int family, s, n;
220 char host[NI_MAXHOST];
221 char interfaceIP[16];
222 // Per thread, and cleared on every call: a lookup that found nothing
223 // returned the previous call's interface.
224 static thread_local char interface[IFNAMSIZ] = "";
225 ✗ interface[0] = 0;
226
227 ✗ if (getifaddrs(&ifaddr) == -1) {
228 ✗ LogErr(VB_SETTING, "Error getting interfaces list: %s\n",
229 FPPstrerror(errno));
230 ✗ return interface;
231 }
232
233 ✗ for (ifa = ifaddr, n = 0; ifa != NULL; ifa = ifa->ifa_next, n++) {
234 ✗ if (ifa->ifa_addr == NULL)
235 ✗ continue;
236
237 ✗ if (ifa->ifa_addr->sa_family == AF_INET) {
238 ✗ s = getnameinfo(ifa->ifa_addr, sizeof(struct sockaddr_in),
239 host, NI_MAXHOST, NULL, 0, NI_NUMERICHOST);
240 ✗ if (s != 0) {
241 ✗ LogErr(VB_SETTING, "getnameinfo failed");
242 ✗ freeifaddrs(ifaddr);
243 ✗ return interface;
244 }
245
246 ✗ if (!strcmp(host, ip)) {
247 ✗ snprintf(interface, sizeof(interface), "%s", ifa->ifa_name);
248 ✗ freeifaddrs(ifaddr);
249 ✗ return interface;
250 }
251 }
252 }
253
254 ✗ freeifaddrs(ifaddr);
255 ✗ return interface;
256 }
257
258 4406 static void ReplaceString(std::string& str, std::string pattern, std::string replacement) {
259 4406 std::size_t found = str.find(pattern);
260
2/2
✓ Branch 0 taken 11 times.
✓ Branch 1 taken 4406 times.
4417 while (found != std::string::npos) {
261 11 str.replace(found, pattern.length(), replacement);
262
263 // Resume after what was inserted, not from the start: a value that
264 // contains its own keyword (a playlist named "%PLAYLIST_NAME%") was
265 // found again inside the replacement, forever, hanging whichever
266 // thread fired the preset.
267 11 found = str.find(pattern, found + replacement.length());
268 }
269 4406 }
270 781 std::string ReplaceKeywords(std::string str, std::map<std::string, std::string>& keywords) {
271 781 std::string key;
272
273
2/2
✓ Branch 0 taken 501 times.
✓ Branch 1 taken 781 times.
1282 for (auto& k : keywords) {
274
1/2
✓ Branch 0 taken 501 times.
✗ Branch 1 not taken.
501 key = std::string("%");
275
1/2
✓ Branch 0 taken 501 times.
✗ Branch 1 not taken.
501 key += k.first;
276
2/4
✓ Branch 0 taken 501 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 501 times.
✗ Branch 3 not taken.
501 key += std::string("%");
277
278
3/6
✓ Branch 0 taken 501 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 501 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 501 times.
✗ Branch 5 not taken.
501 ReplaceString(str, key, k.second);
279 }
280
281 781 std::time_t currTime = std::time(NULL);
282 struct tm now;
283 781 localtime_r(&currTime, &now);
284 char tmpStr[20];
285
286 781 snprintf(tmpStr, sizeof(tmpStr), "%02d:%02d:%02d", now.tm_hour, now.tm_min, now.tm_sec);
287
3/6
✓ Branch 0 taken 781 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 781 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 781 times.
✗ Branch 5 not taken.
2343 ReplaceString(str, "%TIME%", tmpStr);
288
289 781 snprintf(tmpStr, sizeof(tmpStr), "%04d-%02d-%02d", now.tm_year + 1900, now.tm_mon + 1, now.tm_mday);
290
3/6
✓ Branch 0 taken 781 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 781 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 781 times.
✗ Branch 5 not taken.
3124 ReplaceString(str, "%DATE%", tmpStr);
291
292
4/8
✓ Branch 0 taken 781 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 781 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 781 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 781 times.
✗ Branch 7 not taken.
3124 ReplaceString(str, "%FPP_VERSION%", getFPPVersionTriplet());
293
4/8
✓ Branch 0 taken 781 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 781 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 781 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 781 times.
✗ Branch 7 not taken.
3124 ReplaceString(str, "%FPP_SOURCE_VERSION%", getFPPVersion());
294
4/8
✓ Branch 0 taken 781 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 781 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 781 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 781 times.
✗ Branch 7 not taken.
2343 ReplaceString(str, "%FPP_BRANCH%", getFPPBranch());
295
296 1562 return str;
297 781 }
298
299 /////////////////////////////////////////////////////////////////////////////
300 /*
301 * Merge the contens of Json::Value b into Json::Value a
302 */
303 ✗ void MergeJsonValues(Json::Value& a, Json::Value& b) {
304 ✗ if (!a.isObject() || !b.isObject())
305 ✗ return;
306
307 ✗ Json::Value::Members memberNames = b.getMemberNames();
308
309 ✗ for (unsigned int i = 0; i < memberNames.size(); i++) {
310 ✗ std::string key = memberNames[i];
311
312 ✗ if ((a[key].type() == Json::objectValue) &&
313 ✗ (b[key].type() == Json::objectValue)) {
314 ✗ MergeJsonValues(a[key], b[key]);
315 } else {
316 ✗ a[key] = b[key];
317 }
318 ✗ }
319 ✗ }
320
321 /*
322 *
323 */
324 ✗ Json::Value LoadJsonFromFile(const std::string& filename) {
325 ✗ Json::Value root;
326
327 ✗ LoadJsonFromFile(filename, root);
328
329 ✗ return root;
330 ✗ }
331
332 /*
333 *
334 */
335 174109 Json::Value LoadJsonFromString(const std::string& str) {
336 174109 Json::Value root;
337
2/2
✓ Branch 0 taken 174107 times.
✓ Branch 1 taken 2 times.
174109 bool result = LoadJsonFromString(str, root);
338
339 174107 return root;
340 2 }
341
342 /*
343 *
344 */
345 419698 bool LoadJsonFromString(const std::string& str, Json::Value& root) {
346
1/2
✓ Branch 0 taken 419698 times.
✗ Branch 1 not taken.
419698 Json::CharReaderBuilder builder;
347
1/2
✓ Branch 0 taken 419698 times.
✗ Branch 1 not taken.
419698 Json::CharReader* reader = builder.newCharReader();
348 419698 std::string errors;
349
350
3/6
✓ Branch 0 taken 419698 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 419698 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 419698 times.
✗ Branch 5 not taken.
1259094 builder["collectComments"] = false;
351
352
2/2
✓ Branch 0 taken 419597 times.
✓ Branch 1 taken 101 times.
419698 bool success = reader->parse(str.c_str(), str.c_str() + str.size(), &root, &errors);
353
1/2
✓ Branch 0 taken 419597 times.
✗ Branch 1 not taken.
419597 delete reader;
354
355
2/2
✓ Branch 0 taken 466 times.
✓ Branch 1 taken 419131 times.
419597 if (!success) {
356 // Bodies parsed here include HTTP requests and config files that carry
357 // credentials; log only the start, which is enough to see what it was.
358
2/4
✓ Branch 0 taken 466 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 466 times.
✗ Branch 3 not taken.
466 LogErr(VB_GENERAL, "Error parsing JSON string in LoadJsonFromString(): '%s'\n", TruncateForLog(str).c_str());
359
1/2
✓ Branch 0 taken 466 times.
✗ Branch 1 not taken.
466 Json::Value empty;
360
1/2
✓ Branch 0 taken 466 times.
✗ Branch 1 not taken.
466 root = empty;
361 466 return false;
362 466 }
363
364
2/4
✓ Branch 0 taken 419131 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 419131 times.
✗ Branch 3 not taken.
419131 LogDebug(VB_GENERAL, "LoadJsonFromString() loaded: '%s'\n", TruncateForLog(str).c_str());
365
366 419131 return true;
367 419799 }
368
369 24679 bool LoadJsonFromFile(const std::string& filename, Json::Value& root) {
370
3/4
✓ Branch 0 taken 24679 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 11506 times.
✓ Branch 3 taken 13173 times.
24679 if (!FileExists(filename)) {
371
1/2
✓ Branch 0 taken 11506 times.
✗ Branch 1 not taken.
11506 LogErr(VB_GENERAL, "JSON File %s does not exist\n", filename.c_str());
372 11506 return false;
373 }
374
375
1/2
✓ Branch 0 taken 13173 times.
✗ Branch 1 not taken.
13173 std::string jsonStr = GetFileContents(filename);
376
377 // An empty file holds nothing, not bad JSON: some config files exist
378 // empty (an old install's pipewire-audio-groups.json, for one), and their
379 // readers run at every song, each logging a parse error. The result is
380 // the same as a parse failure; a caller that needs the file says so.
381
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 13165 times.
13173 if (jsonStr.find_first_not_of(" \t\r\n") == std::string::npos) {
382
1/2
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
8 LogDebug(VB_GENERAL, "JSON File %s is empty\n", filename.c_str());
383
1/2
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
8 root = Json::Value();
384 8 return false;
385 }
386
387
1/2
✓ Branch 0 taken 13165 times.
✗ Branch 1 not taken.
13165 return LoadJsonFromString(jsonStr, root);
388 13173 }
389
390 ✗ bool LoadJsonFromFile(const char* filename, Json::Value& root) {
391 ✗ std::string filenameStr = filename;
392
393 ✗ return LoadJsonFromFile(filenameStr, root);
394 ✗ }
395
396 184 static const char* JsonTypeName(Json::ValueType t) {
397
4/9
✗ Branch 0 not taken.
✓ Branch 1 taken 17 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 16 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 59 times.
✓ Branch 7 taken 92 times.
✗ Branch 8 not taken.
184 switch (t) {
398 ✗ case Json::nullValue:
399 ✗ return "null";
400 17 case Json::intValue:
401 17 return "int";
402 ✗ case Json::uintValue:
403 ✗ return "uint";
404 ✗ case Json::realValue:
405 ✗ return "real";
406 16 case Json::stringValue:
407 16 return "string";
408 ✗ case Json::booleanValue:
409 ✗ return "boolean";
410 59 case Json::arrayValue:
411 59 return "array";
412 92 case Json::objectValue:
413 92 return "object";
414 }
415 ✗ return "unknown";
416 }
417
418 24653 bool LoadJsonFromFile(const std::string& filename, Json::Value& root, JsonRoot expected) {
419
2/2
✓ Branch 0 taken 308 times.
✓ Branch 1 taken 24345 times.
24653 Json::ValueType expectedType = (expected == JsonRoot::Array) ? Json::arrayValue : Json::objectValue;
420
421
2/2
✓ Branch 0 taken 11596 times.
✓ Branch 1 taken 13057 times.
24653 if (!LoadJsonFromFile(filename, root)) {
422
1/2
✓ Branch 0 taken 11596 times.
✗ Branch 1 not taken.
11596 root = Json::Value(expectedType);
423 11596 return false;
424 }
425
426 // A null root is not a mismatch. jsoncpp treats null as an empty value of
427 // whatever type it is first used as and never throws on it, and FPP writes
428 // it itself - Variables::save() stores a default-constructed Json::Value,
429 // so a stock variables.json with nothing persisted is literally "null".
430 // Normalize it and report success, exactly as before this check existed.
431
2/2
✓ Branch 0 taken 33 times.
✓ Branch 1 taken 13024 times.
13057 if (root.isNull()) {
432
1/2
✓ Branch 0 taken 33 times.
✗ Branch 1 not taken.
33 root = Json::Value(expectedType);
433 33 return true;
434 }
435
436
2/2
✓ Branch 0 taken 25 times.
✓ Branch 1 taken 12999 times.
13024 if (root.type() != expectedType) {
437 25 LogErr(VB_GENERAL, "JSON File %s has a %s at its root, expected %s - treating as empty\n",
438 filename.c_str(), JsonTypeName(root.type()), JsonTypeName(expectedType));
439
1/2
✓ Branch 0 taken 25 times.
✗ Branch 1 not taken.
25 root = Json::Value(expectedType);
440 25 return false;
441 }
442
443 12999 return true;
444 }
445
446 5200 Json::Value LoadJsonFromFile(const std::string& filename, JsonRoot expected) {
447 5200 Json::Value root;
448
449
1/2
✓ Branch 0 taken 5200 times.
✗ Branch 1 not taken.
5200 LoadJsonFromFile(filename, root, expected);
450
451 5200 return root;
452 ✗ }
453
454 3122 bool LoadJsonFromString(const std::string& str, Json::Value& root, JsonRoot expected) {
455
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3122 times.
3122 Json::ValueType expectedType = (expected == JsonRoot::Array) ? Json::arrayValue : Json::objectValue;
456
457
2/2
✓ Branch 0 taken 94 times.
✓ Branch 1 taken 3012 times.
3122 if (!LoadJsonFromString(str, root)) {
458
1/2
✓ Branch 0 taken 94 times.
✗ Branch 1 not taken.
94 root = Json::Value(expectedType);
459 94 return false;
460 }
461
462 // See LoadJsonFromFile() above - a null root is not a mismatch, it's
463 // normalized to an empty value of the expected shape and reported as
464 // success.
465
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 2996 times.
3012 if (root.isNull()) {
466
1/2
✓ Branch 0 taken 16 times.
✗ Branch 1 not taken.
16 root = Json::Value(expectedType);
467 16 return true;
468 }
469
470
2/2
✓ Branch 0 taken 67 times.
✓ Branch 1 taken 2929 times.
2996 if (root.type() != expectedType) {
471 67 LogErr(VB_GENERAL, "JSON string has a %s at its root, expected %s - treating as empty\n",
472 JsonTypeName(root.type()), JsonTypeName(expectedType));
473
1/2
✓ Branch 0 taken 67 times.
✗ Branch 1 not taken.
67 root = Json::Value(expectedType);
474 67 return false;
475 }
476
477 2929 return true;
478 }
479
480 ✗ Json::Value LoadJsonFromString(const std::string& str, JsonRoot expected) {
481 ✗ Json::Value root;
482
483 ✗ LoadJsonFromString(str, root, expected);
484
485 ✗ return root;
486 ✗ }
487
488 1591109 std::string SaveJsonToString(const Json::Value& root, const std::string& indentation) {
489
1/2
✓ Branch 0 taken 1591109 times.
✗ Branch 1 not taken.
1591109 Json::StreamWriterBuilder wbuilder;
490
3/6
✓ Branch 0 taken 1591109 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1591109 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1591109 times.
✗ Branch 5 not taken.
4773327 wbuilder["indentation"] = indentation;
491
492
1/2
✓ Branch 0 taken 1591109 times.
✗ Branch 1 not taken.
1591109 std::string result = Json::writeString(wbuilder, root);
493
494 1591109 return result;
495 1591109 }
496
497 22042 bool SaveJsonToString(const Json::Value& root, std::string& str, const std::string& indentation) {
498
1/2
✓ Branch 0 taken 22042 times.
✗ Branch 1 not taken.
22042 str = SaveJsonToString(root, indentation);
499
500
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 22042 times.
22042 if (str.empty())
501 ✗ return false;
502
503 22042 return true;
504 }
505
506 22042 bool SaveJsonToFile(const Json::Value& root, const std::string& filename, const std::string& indentation) {
507 22042 std::string str;
508
509
1/2
✓ Branch 0 taken 22042 times.
✗ Branch 1 not taken.
22042 bool result = SaveJsonToString(root, str, indentation);
510
511
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 22042 times.
22042 if (!result) {
512 ✗ LogErr(VB_GENERAL, "Error converting Json::Value to std::string()\n");
513 ✗ return false;
514 }
515
516
1/2
✓ Branch 0 taken 22042 times.
✗ Branch 1 not taken.
22042 result = PutFileContents(filename, str);
517
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 22041 times.
22042 if (!result) {
518 1 return false;
519 }
520
521 22041 return true;
522 22042 }
523
524 ✗ bool SaveJsonToFile(const Json::Value& root, const char* filename, const char* indentation) {
525 ✗ std::string filenameStr = filename;
526 ✗ std::string indentationStr = indentation;
527
528 ✗ return SaveJsonToFile(root, filenameStr, indentationStr);
529 ✗ }
530
531 6 std::string getSimpleHTMLTTag(const std::string& html, const std::string& searchStr, const std::string& skipStr, const std::string& endStr) {
532 6 std::string value;
533 6 std::string tStr;
534 6 std::size_t fStart = html.find(searchStr);
535 std::size_t fEnd;
536
537
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
6 if (fStart != std::string::npos) {
538
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
6 tStr = html.substr(fStart + searchStr.size());
539 6 fStart = tStr.find(skipStr);
540
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
6 if (fStart != std::string::npos) {
541 6 fStart += skipStr.size();
542
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
6 fEnd = tStr.substr(fStart).find(endStr);
543 6 fEnd += fStart;
544
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
6 if (fEnd > fStart) {
545
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
6 value = tStr.substr(fStart, fEnd - fStart);
546
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
6 TrimWhiteSpace(value);
547
3/6
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 6 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 6 times.
✗ Branch 5 not taken.
24 replaceAll(value, std::string(" "), std::string(" "));
548 }
549 }
550 }
551
552 6 return value;
553 6 }
554
555 41 std::string getSimpleXMLTag(const std::string& xml, const std::string& tag) {
556 41 std::string value;
557
1/2
✓ Branch 0 taken 41 times.
✗ Branch 1 not taken.
41 std::string sSearch = "<";
558
2/4
✓ Branch 0 taken 41 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 41 times.
✗ Branch 3 not taken.
41 sSearch += tag + ">";
559
560
1/2
✓ Branch 0 taken 41 times.
✗ Branch 1 not taken.
41 std::string eSearch = "</";
561
2/4
✓ Branch 0 taken 41 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 41 times.
✗ Branch 3 not taken.
41 eSearch += tag + ">";
562
563 41 std::size_t fStart = xml.find(sSearch);
564 41 std::size_t fEnd = xml.find(eSearch);
565
566
2/4
✓ Branch 0 taken 41 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 41 times.
✗ Branch 3 not taken.
41 if ((fStart != std::string::npos) &&
567
1/2
✓ Branch 0 taken 41 times.
✗ Branch 1 not taken.
41 (fEnd != std::string::npos) &&
568 (fEnd > fStart)) {
569
1/2
✓ Branch 0 taken 41 times.
✗ Branch 1 not taken.
41 value = xml.substr(fStart + sSearch.length(), fEnd - fStart - sSearch.length());
570
1/2
✓ Branch 0 taken 41 times.
✗ Branch 1 not taken.
41 TrimWhiteSpace(value);
571 }
572
573 41 return value;
574 41 }
575
576 // URL Helpers
577 26 size_t urlWriteData(void* buffer, size_t size, size_t nmemb, void* userp) {
578 26 std::string* str = (std::string*)userp;
579
580 26 str->append(static_cast<const char*>(buffer), size * nmemb);
581
582 26 return size * nmemb;
583 }
584
585 238 std::string NormalizeMacAddress(const std::string& mac) {
586 238 std::string out;
587
2/2
✓ Branch 0 taken 3883 times.
✓ Branch 1 taken 238 times.
4121 for (char c : mac) {
588
2/2
✓ Branch 0 taken 2773 times.
✓ Branch 1 taken 1110 times.
3883 if (isxdigit((unsigned char)c)) {
589
1/2
✓ Branch 0 taken 2773 times.
✗ Branch 1 not taken.
2773 out += toupper((unsigned char)c);
590
3/8
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 1105 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 5 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
1110 } else if (c != ':' && c != '-' && c != '.' && c != ' ') {
591 // Anything else means this was never a MAC.
592 ✗ return "";
593 }
594 }
595
6/8
✓ Branch 0 taken 231 times.
✓ Branch 1 taken 7 times.
✓ Branch 2 taken 231 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 231 times.
✓ Branch 6 taken 7 times.
✓ Branch 7 taken 231 times.
238 if (out.size() != 12 || out == "000000000000") {
596
1/2
✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
14 return "";
597 }
598 231 return out;
599 238 }
600
601 4641 std::string GetMacForAddress(const std::string& address) {
602
4/8
✓ Branch 0 taken 4641 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4641 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 4641 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 4641 times.
4641 if (address.empty() || IsLoopbackAddress(address)) {
603 ✗ return "";
604 }
605 // IPv6 neighbours are not exposed through procfs, so a v6-only peer gets no
606 // MAC here. That is harmless: a dual-stacked device is still identified
607 // through its IPv4 entry, and the UI merges a device's addresses by the
608 // UUID any one of them carries.
609
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4641 times.
4641 if (address.find(':') != std::string::npos) {
610 ✗ return "";
611 }
612
613
1/2
✓ Branch 0 taken 4641 times.
✗ Branch 1 not taken.
4641 std::ifstream arp("/proc/net/arp");
614
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4641 times.
4641 if (!arp.is_open()) {
615 ✗ return "";
616 }
617 4641 std::string line;
618
1/2
✓ Branch 0 taken 4641 times.
✗ Branch 1 not taken.
4641 std::getline(arp, line); // header
619
4/6
✓ Branch 0 taken 14017 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 14017 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 9579 times.
✓ Branch 5 taken 4438 times.
14017 while (std::getline(arp, line)) {
620
1/2
✓ Branch 0 taken 9579 times.
✗ Branch 1 not taken.
9579 std::stringstream ss(line);
621 9579 std::string ip, hwType, flags, hwAddr;
622
6/12
✓ Branch 0 taken 9579 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 9579 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 9579 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 9579 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 9579 times.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✓ Branch 11 taken 9579 times.
9579 if (!(ss >> ip >> hwType >> flags >> hwAddr)) {
623 ✗ continue;
624 }
625
2/2
✓ Branch 0 taken 9376 times.
✓ Branch 1 taken 203 times.
9579 if (ip != address) {
626 9376 continue;
627 }
628 // Flag bit 0x2 (ATF_COM) means the entry is complete. An incomplete
629 // entry carries a zeroed address that would otherwise look like a MAC.
630
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 203 times.
203 if ((strtol(flags.c_str(), nullptr, 0) & 0x2) == 0) {
631 ✗ return "";
632 }
633
1/2
✓ Branch 0 taken 203 times.
✗ Branch 1 not taken.
203 return NormalizeMacAddress(hwAddr);
634
10/10
✓ Branch 0 taken 9376 times.
✓ Branch 1 taken 203 times.
✓ Branch 2 taken 9376 times.
✓ Branch 3 taken 203 times.
✓ Branch 4 taken 9376 times.
✓ Branch 5 taken 203 times.
✓ Branch 6 taken 9376 times.
✓ Branch 7 taken 203 times.
✓ Branch 8 taken 9376 times.
✓ Branch 9 taken 203 times.
47895 }
635
1/2
✓ Branch 0 taken 4438 times.
✗ Branch 1 not taken.
8876 return "";
636 4641 }
637
638 26294 bool IsLoopbackAddress(const std::string& address) {
639
4/6
✓ Branch 0 taken 26294 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 26294 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 9 times.
✓ Branch 5 taken 26285 times.
52588 if (startsWith(address, "127.")) {
640 9 return true;
641 }
642 // "::1" is the usual spelling; the fully expanded form is legal too and
643 // shows up from some resolvers.
644
2/4
✓ Branch 0 taken 26285 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 26285 times.
26285 return address == "::1" || address == "0:0:0:0:0:0:0:1";
645 }
646
647 // See the comment on the declarations in common.h.
648 namespace {
649 struct ResolveCacheEntry {
650 uint32_t addr = 0;
651 bool valid = false;
652 uint64_t expiresMS = 0;
653 };
654 std::mutex resolveCacheLock;
655 std::map<std::string, ResolveCacheEntry> resolveCache;
656
657 // Short enough that a peer coming online is noticed on the next poll of
658 // whatever is watching it, long enough to collapse a startup burst.
659 constexpr uint64_t RESOLVE_TTL_OK_MS = 60000;
660 constexpr uint64_t RESOLVE_TTL_FAIL_MS = 30000;
661 // Nothing should be resolving thousands of distinct names; if it is, drop the
662 // expired entries rather than growing without bound.
663 constexpr size_t RESOLVE_CACHE_MAX = 256;
664 } // namespace
665
666 13152 bool ResolveHostToIPv4(const std::string& host, uint32_t& addr) {
667
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 13152 times.
13152 if (host.empty()) {
668 ✗ return false;
669 }
670 // A literal needs no resolver and must not take a cache slot -- the HTTP
671 // discovery scan walks whole /24s of them.
672 struct in_addr numeric;
673
2/2
✓ Branch 0 taken 13094 times.
✓ Branch 1 taken 58 times.
13152 if (inet_pton(AF_INET, host.c_str(), &numeric) == 1) {
674 13094 addr = numeric.s_addr;
675 13094 return true;
676 }
677
678
1/2
✓ Branch 0 taken 58 times.
✗ Branch 1 not taken.
58 uint64_t now = (uint64_t)GetMonotonicTimeMS();
679 {
680
1/2
✓ Branch 0 taken 58 times.
✗ Branch 1 not taken.
58 std::unique_lock<std::mutex> lock(resolveCacheLock);
681
1/2
✓ Branch 0 taken 58 times.
✗ Branch 1 not taken.
58 auto it = resolveCache.find(host);
682
5/6
✓ Branch 0 taken 35 times.
✓ Branch 1 taken 23 times.
✓ Branch 2 taken 35 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 35 times.
✓ Branch 5 taken 23 times.
58 if (it != resolveCache.end() && it->second.expiresMS > now) {
683 35 addr = it->second.addr;
684 35 return it->second.valid;
685 }
686
2/2
✓ Branch 0 taken 23 times.
✓ Branch 1 taken 35 times.
58 }
687
688 // Resolve with the lock released: this blocks for seconds on a name that
689 // does not exist, and holding the lock across it would hand that stall to
690 // every other thread resolving anything at all. Two threads racing on the
691 // same new name can both pay for it; that is cheaper than serialising all
692 // lookups behind one.
693 23 struct addrinfo hints{};
694 23 hints.ai_family = AF_INET; // IPv4 only -- callers want a dotted-quad
695 23 hints.ai_socktype = SOCK_DGRAM; // one result per address, not per socktype
696 23 struct addrinfo* res = nullptr;
697 23 bool valid = false;
698 23 uint32_t resolved = 0;
699
6/8
✓ Branch 0 taken 23 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 5 times.
✓ Branch 3 taken 18 times.
✓ Branch 4 taken 5 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 5 times.
✓ Branch 7 taken 18 times.
23 if (getaddrinfo(host.c_str(), nullptr, &hints, &res) == 0 && res != nullptr) {
700 5 resolved = ((struct sockaddr_in*)res->ai_addr)->sin_addr.s_addr;
701 5 valid = true;
702 }
703
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 18 times.
23 if (res) {
704 5 freeaddrinfo(res);
705 }
706
707 {
708
1/2
✓ Branch 0 taken 23 times.
✗ Branch 1 not taken.
23 std::unique_lock<std::mutex> lock(resolveCacheLock);
709
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 23 times.
23 if (resolveCache.size() >= RESOLVE_CACHE_MAX) {
710 ✗ uint64_t cutoff = (uint64_t)GetMonotonicTimeMS();
711 ✗ for (auto it = resolveCache.begin(); it != resolveCache.end();) {
712 ✗ it = (it->second.expiresMS <= cutoff) ? resolveCache.erase(it) : std::next(it);
713 }
714 }
715
1/2
✓ Branch 0 taken 23 times.
✗ Branch 1 not taken.
23 ResolveCacheEntry& e = resolveCache[host];
716 23 e.addr = resolved;
717 23 e.valid = valid;
718
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 18 times.
23 e.expiresMS = now + (valid ? RESOLVE_TTL_OK_MS : RESOLVE_TTL_FAIL_MS);
719 23 }
720
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 18 times.
23 if (valid) {
721 5 addr = resolved;
722 }
723 23 return valid;
724 }
725
726 13137 std::string ResolveHostToIPv4(const std::string& host) {
727 13137 uint32_t addr = 0;
728
3/4
✓ Branch 0 taken 13137 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 31 times.
✓ Branch 3 taken 13106 times.
13137 if (!ResolveHostToIPv4(host, addr)) {
729
1/2
✓ Branch 0 taken 31 times.
✗ Branch 1 not taken.
62 return "";
730 }
731 struct in_addr ia;
732 13106 ia.s_addr = addr;
733 13106 char buf[INET_ADDRSTRLEN] = { 0 };
734
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 13106 times.
13106 if (!inet_ntop(AF_INET, &ia, buf, sizeof(buf))) {
735 ✗ return "";
736 }
737
1/2
✓ Branch 0 taken 13106 times.
✗ Branch 1 not taken.
26212 return buf;
738 }
739
740 1947 void FlushHostResolveCache() {
741
1/2
✓ Branch 0 taken 1947 times.
✗ Branch 1 not taken.
1947 std::unique_lock<std::mutex> lock(resolveCacheLock);
742 1947 resolveCache.clear();
743 1947 }
744
745 739 std::string buildHttpURL(const std::string& address, const std::string& path) {
746
1/2
✓ Branch 0 taken 739 times.
✗ Branch 1 not taken.
739 std::string host = address;
747 // An IPv6 literal contains ':' (hostnames and IPv4 never do). curl requires
748 // it bracketed, and a link-local zone id ("fe80::1%eth0") must have its '%'
749 // percent-encoded as "%25" inside a URL.
750
2/2
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 729 times.
739 if (host.find(':') != std::string::npos) {
751 10 std::string::size_type pct = host.find('%');
752
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 2 times.
10 if (pct != std::string::npos) {
753
4/8
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 8 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 8 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 8 times.
✗ Branch 7 not taken.
8 host = host.substr(0, pct) + "%25" + host.substr(pct + 1);
754 }
755
2/4
✓ Branch 0 taken 10 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 10 times.
✗ Branch 3 not taken.
10 host = "[" + host + "]";
756 }
757
2/4
✓ Branch 0 taken 739 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 739 times.
✗ Branch 3 not taken.
1478 return "http://" + host + path;
758 739 }
759
760 // DEPRECATED -- see the comment on the declarations in common.h. Kept for
761 // external plugins and for the callers that have no main loop to complete a
762 // CurlManager request against.
763 47 bool urlHelper(const std::string method, const std::string& url, const std::string& data, std::string& resp, const unsigned int timeout) {
764
2/4
✓ Branch 0 taken 47 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 47 times.
✗ Branch 3 not taken.
47 return urlHelper(method, url, data, resp, std::list<std::string>(), timeout);
765 }
766 47 bool urlHelper(const std::string method, const std::string& url, const std::string& data, std::string& resp, const std::list<std::string>& extraHeaders, const unsigned int timeout) {
767
1/2
✓ Branch 0 taken 47 times.
✗ Branch 1 not taken.
47 CURL* curl = curl_easy_init();
768
1/2
✓ Branch 0 taken 47 times.
✗ Branch 1 not taken.
47 std::string userAgent = "FPP/";
769
2/4
✓ Branch 0 taken 47 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 47 times.
✗ Branch 3 not taken.
47 userAgent += getFPPVersionTriplet();
770
771
1/2
✓ Branch 0 taken 47 times.
✗ Branch 1 not taken.
47 resp = "";
772
773
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 47 times.
47 if (!curl) {
774 ✗ LogDebug(VB_GENERAL, "Unable to create curl instance in urlHelper()\n");
775 ✗ return false;
776 }
777
1/2
✓ Branch 0 taken 47 times.
✗ Branch 1 not taken.
47 SetCurlCloseOnExec(curl);
778
779 CURLcode status;
780
1/2
✓ Branch 0 taken 47 times.
✗ Branch 1 not taken.
47 status = curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, urlWriteData);
781
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 47 times.
47 if (status != CURLE_OK) {
782 ✗ LogErr(VB_GENERAL, "curl_easy_setopt() Error setting write callback function: %s\n", curl_easy_strerror(status));
783 ✗ curl_easy_cleanup(curl);
784 ✗ return false;
785 }
786
787
1/2
✓ Branch 0 taken 47 times.
✗ Branch 1 not taken.
47 status = curl_easy_setopt(curl, CURLOPT_WRITEDATA, &resp);
788
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 47 times.
47 if (status != CURLE_OK) {
789 ✗ LogErr(VB_GENERAL, "curl_easy_setopt() Error setting class pointer: %s\n", curl_easy_strerror(status));
790 ✗ curl_easy_cleanup(curl);
791 ✗ return false;
792 }
793
794
1/2
✓ Branch 0 taken 47 times.
✗ Branch 1 not taken.
47 status = curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
795
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 47 times.
47 if (status != CURLE_OK) {
796 ✗ LogErr(VB_GENERAL, "curl_easy_setopt() Error setting URL: %s\n", curl_easy_strerror(status));
797 ✗ curl_easy_cleanup(curl);
798 ✗ return false;
799 }
800
801 47 struct curl_slist* headers = NULL;
802
15/30
✓ Branch 0 taken 47 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 47 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 6 times.
✓ Branch 5 taken 41 times.
✓ Branch 6 taken 6 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 6 times.
✗ Branch 9 not taken.
✓ Branch 10 taken 6 times.
✗ Branch 11 not taken.
✓ Branch 12 taken 6 times.
✓ Branch 13 taken 41 times.
✓ Branch 14 taken 6 times.
✓ Branch 15 taken 41 times.
✓ Branch 16 taken 47 times.
✗ Branch 17 not taken.
✓ Branch 18 taken 47 times.
✗ Branch 19 not taken.
✓ Branch 20 taken 6 times.
✓ Branch 21 taken 41 times.
✗ Branch 22 not taken.
✗ Branch 23 not taken.
✗ Branch 24 not taken.
✗ Branch 25 not taken.
✗ Branch 26 not taken.
✗ Branch 27 not taken.
✗ Branch 28 not taken.
✗ Branch 29 not taken.
153 if (startsWith(data, "{") && endsWith(data, "}")) {
803
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
6 headers = curl_slist_append(headers, "Accept: application/json");
804
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
6 headers = curl_slist_append(headers, "Content-Type: application/json");
805 }
806
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 47 times.
47 for (auto& h : extraHeaders) {
807 ✗ headers = curl_slist_append(headers, h.c_str());
808 }
809
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 41 times.
47 if (headers) {
810
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
6 curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
811 }
812
813
8/10
✓ Branch 0 taken 47 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 39 times.
✓ Branch 3 taken 8 times.
✓ Branch 4 taken 39 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 31 times.
✓ Branch 7 taken 8 times.
✓ Branch 8 taken 39 times.
✓ Branch 9 taken 8 times.
47 if ((method == "POST") || (method == "PUT")) {
814 // The size first: without it curl takes strlen() of the body, which
815 // cuts a binary body short at its first zero byte.
816
1/2
✓ Branch 0 taken 39 times.
✗ Branch 1 not taken.
39 curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE_LARGE, (curl_off_t)data.size());
817
1/2
✓ Branch 0 taken 39 times.
✗ Branch 1 not taken.
39 status = curl_easy_setopt(curl, CURLOPT_POSTFIELDS, data.c_str());
818
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 39 times.
39 if (status != CURLE_OK) {
819 ✗ LogErr(VB_GENERAL, "curl_easy_setopt() Error setting postfields data: %s\n", curl_easy_strerror(status));
820 ✗ curl_slist_free_all(headers);
821 ✗ curl_easy_cleanup(curl);
822 ✗ return false;
823 }
824 }
825
1/2
✓ Branch 0 taken 47 times.
✗ Branch 1 not taken.
47 curl_easy_setopt(curl, CURLOPT_TIMEOUT, (long)timeout);
826
1/2
✓ Branch 0 taken 47 times.
✗ Branch 1 not taken.
47 curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 2);
827
1/2
✓ Branch 0 taken 47 times.
✗ Branch 1 not taken.
47 curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);
828 // Accept (and transparently decode) whatever encodings this libcurl was built
829 // with. Some controllers gzip a response whether or not the client asked for
830 // it, and without this the caller is handed the compressed bytes and every
831 // find()/JSON parse on them quietly fails. CurlManager has always done this;
832 // this path had not, which is what made such a device undetectable.
833
1/2
✓ Branch 0 taken 47 times.
✗ Branch 1 not taken.
47 curl_easy_setopt(curl, CURLOPT_ACCEPT_ENCODING, "");
834
835
3/4
✓ Branch 0 taken 47 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 8 times.
✓ Branch 3 taken 39 times.
47 if (method == "POST")
836
1/2
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
8 curl_easy_setopt(curl, CURLOPT_POST, 1);
837
3/4
✓ Branch 0 taken 39 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 31 times.
✓ Branch 3 taken 8 times.
39 else if (method == "PUT")
838
1/2
✓ Branch 0 taken 31 times.
✗ Branch 1 not taken.
31 curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "PUT");
839
2/4
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 8 times.
8 else if (method == "DELETE")
840 ✗ curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "DELETE");
841
842
1/2
✓ Branch 0 taken 47 times.
✗ Branch 1 not taken.
47 curl_easy_setopt(curl, CURLOPT_USERAGENT, userAgent.c_str());
843
844
1/2
✓ Branch 0 taken 47 times.
✗ Branch 1 not taken.
47 LogDebug(VB_GENERAL, "Calling %s %s\n", method.c_str(), url.c_str());
845
846
1/2
✓ Branch 0 taken 47 times.
✗ Branch 1 not taken.
47 status = curl_easy_perform(curl);
847
2/2
✓ Branch 0 taken 21 times.
✓ Branch 1 taken 26 times.
47 if (status != CURLE_OK) {
848
2/4
✓ Branch 0 taken 21 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 21 times.
✗ Branch 3 not taken.
21 LogErr(VB_GENERAL, "curl_easy_perform() failed (%s): %s\n", url.c_str(), curl_easy_strerror(status));
849
1/2
✓ Branch 0 taken 21 times.
✗ Branch 1 not taken.
21 curl_slist_free_all(headers);
850
1/2
✓ Branch 0 taken 21 times.
✗ Branch 1 not taken.
21 curl_easy_cleanup(curl);
851 21 return false;
852 }
853
854
1/2
✓ Branch 0 taken 26 times.
✗ Branch 1 not taken.
26 LogDebug(VB_GENERAL, "%s %s resp: %s\n", method.c_str(), url.c_str(), resp.c_str());
855
856
1/2
✓ Branch 0 taken 26 times.
✗ Branch 1 not taken.
26 curl_slist_free_all(headers);
857
1/2
✓ Branch 0 taken 26 times.
✗ Branch 1 not taken.
26 curl_easy_cleanup(curl);
858 26 return true;
859 47 }
860
861 8 bool urlHelper(const std::string method, const std::string& url, std::string& resp, const unsigned int timeout) {
862 8 std::string data;
863
2/4
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 8 times.
✗ Branch 3 not taken.
16 return urlHelper(method, url, data, resp, timeout);
864 8 }
865
866 8 bool urlGet(const std::string url, std::string& resp) {
867 8 std::string data;
868
2/4
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 8 times.
✗ Branch 3 not taken.
16 return urlHelper("GET", url, resp);
869 8 }
870
871 6 bool urlPost(const std::string url, const std::string data, std::string& resp) {
872
2/4
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 6 times.
✗ Branch 3 not taken.
12 return urlHelper("POST", url, data, resp);
873 }
874
875 31 bool urlPut(const std::string url, const std::string data, std::string& resp) {
876
2/4
✓ Branch 0 taken 31 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 31 times.
✗ Branch 3 not taken.
62 return urlHelper("PUT", url, data, resp);
877 }
878
879 ✗ bool urlDelete(const std::string url, const std::string data, std::string& resp) {
880 ✗ return urlHelper("DELETE", url, data, resp);
881 }
882
883 ✗ bool urlDelete(const std::string url, std::string& resp) {
884 ✗ std::string data;
885 ✗ return urlHelper("DELETE", url, data, resp);
886 ✗ }
887
888 static const std::string BASE64_CHARS =
889 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
890 "abcdefghijklmnopqrstuvwxyz"
891 "0123456789+/";
892
893 334 static inline bool isBase64(uint8_t c) {
894
4/6
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 330 times.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 4 times.
✗ Branch 5 not taken.
334 return (isalnum(c) || (c == '+') || (c == '/'));
895 }
896
897 14 std::string base64Encode(uint8_t const* buf, unsigned int bufLen) {
898 14 std::string ret;
899 14 int i = 0;
900 14 int j = 0;
901 uint8_t char_array_3[3];
902 uint8_t char_array_4[4];
903
904
2/2
✓ Branch 0 taken 280 times.
✓ Branch 1 taken 14 times.
294 while (bufLen--) {
905 280 char_array_3[i++] = *(buf++);
906
2/2
✓ Branch 0 taken 84 times.
✓ Branch 1 taken 196 times.
280 if (i == 3) {
907 84 char_array_4[0] = (char_array_3[0] & 0xfc) >> 2;
908 84 char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4);
909 84 char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6);
910 84 char_array_4[3] = char_array_3[2] & 0x3f;
911
912
2/2
✓ Branch 0 taken 336 times.
✓ Branch 1 taken 84 times.
420 for (i = 0; (i < 4); i++) {
913
1/2
✓ Branch 0 taken 336 times.
✗ Branch 1 not taken.
336 ret += BASE64_CHARS[char_array_4[i]];
914 }
915 84 i = 0;
916 }
917 }
918
919
1/2
✓ Branch 0 taken 14 times.
✗ Branch 1 not taken.
14 if (i) {
920
2/2
✓ Branch 0 taken 14 times.
✓ Branch 1 taken 14 times.
28 for (j = i; j < 3; j++) {
921 14 char_array_3[j] = '\0';
922 }
923
924 14 char_array_4[0] = (char_array_3[0] & 0xfc) >> 2;
925 14 char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4);
926 14 char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6);
927 14 char_array_4[3] = char_array_3[2] & 0x3f;
928
929
2/2
✓ Branch 0 taken 42 times.
✓ Branch 1 taken 14 times.
56 for (j = 0; (j < i + 1); j++) {
930
1/2
✓ Branch 0 taken 42 times.
✗ Branch 1 not taken.
42 ret += BASE64_CHARS[char_array_4[j]];
931 }
932
933
2/2
✓ Branch 0 taken 14 times.
✓ Branch 1 taken 14 times.
28 while ((i++ < 3)) {
934
1/2
✓ Branch 0 taken 14 times.
✗ Branch 1 not taken.
14 ret += '=';
935 }
936 }
937 14 return ret;
938 ✗ }
939 31 std::vector<uint8_t> base64Decode(std::string const& encodedString) {
940 31 int in_len = encodedString.size();
941 31 int i = 0;
942 31 int j = 0;
943 31 int in_ = 0;
944 uint8_t char_array_4[4], char_array_3[3];
945 31 std::vector<uint8_t> ret;
946
947
7/8
✓ Branch 0 taken 364 times.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 334 times.
✓ Branch 3 taken 30 times.
✓ Branch 4 taken 334 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 334 times.
✓ Branch 7 taken 31 times.
365 while (in_len-- && (encodedString[in_] != '=') && isBase64(encodedString[in_])) {
948 334 char_array_4[i++] = encodedString[in_];
949 334 in_++;
950
2/2
✓ Branch 0 taken 61 times.
✓ Branch 1 taken 273 times.
334 if (i == 4) {
951
2/2
✓ Branch 0 taken 244 times.
✓ Branch 1 taken 61 times.
305 for (i = 0; i < 4; i++) {
952 244 char_array_4[i] = BASE64_CHARS.find(char_array_4[i]);
953 }
954
955 61 char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4);
956 61 char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2);
957 61 char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3];
958
959
2/2
✓ Branch 0 taken 183 times.
✓ Branch 1 taken 61 times.
244 for (i = 0; (i < 3); i++) {
960
1/2
✓ Branch 0 taken 183 times.
✗ Branch 1 not taken.
183 ret.push_back(char_array_3[i]);
961 }
962 61 i = 0;
963 }
964 }
965
966
2/2
✓ Branch 0 taken 30 times.
✓ Branch 1 taken 1 times.
31 if (i) {
967
2/2
✓ Branch 0 taken 30 times.
✓ Branch 1 taken 30 times.
60 for (j = i; j < 4; j++) {
968 30 char_array_4[j] = 0;
969 }
970
2/2
✓ Branch 0 taken 120 times.
✓ Branch 1 taken 30 times.
150 for (j = 0; j < 4; j++) {
971 120 char_array_4[j] = BASE64_CHARS.find(char_array_4[j]);
972 }
973
974 30 char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4);
975 30 char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2);
976 30 char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3];
977
978
2/2
✓ Branch 0 taken 60 times.
✓ Branch 1 taken 30 times.
90 for (j = 0; (j < i - 1); j++) {
979
1/2
✓ Branch 0 taken 60 times.
✗ Branch 1 not taken.
60 ret.push_back(char_array_3[j]);
980 }
981 }
982 31 return ret;
983 ✗ }
984
985 static std::function<void(bool)> SHUTDOWN_HOOK;
986 static std::atomic<bool> RESTART_RESUME_PLAYLIST{ false };
987 21 void ShutdownFPPD(bool restart) {
988
1/2
✓ Branch 0 taken 21 times.
✗ Branch 1 not taken.
21 if (SHUTDOWN_HOOK) {
989 21 SHUTDOWN_HOOK(restart);
990 }
991 21 }
992 4 void RestartFPPDResumingPlaylist() {
993 4 RESTART_RESUME_PLAYLIST = true;
994 4 ShutdownFPPD(true);
995 4 }
996 19 bool RestartShouldResumePlaylist() {
997 19 return RESTART_RESUME_PLAYLIST;
998 }
999 2651 void RegisterShutdownHandler(const std::function<void(bool)> hook) {
1000 2651 SHUTDOWN_HOOK = hook;
1001 2651 }
1002
1003 static std::function<std::string(std::string&)> MANUAL_CRASH_REPORT_HOOK;
1004 206 std::string BuildManualCrashReport(std::string& error) {
1005
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 206 times.
206 if (!MANUAL_CRASH_REPORT_HOOK) {
1006 ✗ error = "unavailable";
1007 ✗ return "";
1008 }
1009 206 return MANUAL_CRASH_REPORT_HOOK(error);
1010 }
1011 2651 void RegisterManualCrashReportHandler(const std::function<std::string(std::string&)> hook) {
1012 2651 MANUAL_CRASH_REPORT_HOOK = hook;
1013 2651 }
1014
1015 static std::atomic<const char*> MAIN_LOOP_PHASE_STR{ "startup" };
1016 4713751 void SetMainLoopPhase(const char* phase) {
1017 4713751 MAIN_LOOP_PHASE_STR.store(phase, std::memory_order_relaxed);
1018 4713751 }
1019 36 const char* GetMainLoopPhase() {
1020 36 const char* p = MAIN_LOOP_PHASE_STR.load(std::memory_order_relaxed);
1021
1/2
✓ Branch 0 taken 36 times.
✗ Branch 1 not taken.
36 return p ? p : "?";
1022 }
1023
1024 8504 std::string GetFileExtension(const std::string& filename) {
1025
2/2
✓ Branch 0 taken 4040 times.
✓ Branch 1 taken 4464 times.
8504 if (filename.find_last_of(".") != std::string::npos)
1026 4040 return filename.substr(filename.find_last_of(".") + 1);
1027
1/2
✓ Branch 0 taken 4464 times.
✗ Branch 1 not taken.
8928 return "";
1028 }
1029
1030 293639 void SetThreadName(const std::string& name) {
1031 #ifdef PLATFORM_OSX
1032 pthread_setname_np(name.c_str());
1033 #else
1034 293639 pthread_setname_np(pthread_self(), name.c_str());
1035 #endif
1036 293639 }
1037
1038 3790 bool SetThreadRealtimePriority(int priority) {
1039 #ifndef PLATFORM_OSX
1040 // Kernel RT throttling (sched_rt_runtime_us, default 95%) still guarantees
1041 // normal-priority threads a CPU share even if an RT thread runs hot.
1042 // Returns false (typically EPERM in containers/non-root) so callers can
1043 // log which mode they're running in.
1044 struct sched_param param;
1045 3790 param.sched_priority = priority;
1046 3790 return pthread_setschedparam(pthread_self(), SCHED_FIFO, &param) == 0;
1047 #else
1048 return false;
1049 #endif
1050 }
1051
1052 2645 std::string getPlatform() {
1053
2/4
✓ Branch 0 taken 2645 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2645 times.
✗ Branch 3 not taken.
2645 std::string platform = GetFileContents("/etc/fpp/platform");
1054
1/2
✓ Branch 0 taken 2645 times.
✗ Branch 1 not taken.
2645 TrimWhiteSpace(platform);
1055 2645 return platform;
1056 ✗ }
1057
1058
1059 /////////////////////////////////////////////////////////////////////////////
1060 // Unrolled 32x32 bitwise transform from Hacker's Delight
1061 //
1062 // Bit flips an array along the diagonal from MSbit on LS uint32_t to LS bit on MS uint32_t
1063 //
1064 // src[00] = 0b00000000000000000000000000000001
1065 // src[01] = 0b00000000000000000000000000000010
1066 // ...
1067 // src[30] = 0b00000000000000000000000000000000
1068 // src[31] = 0b00000000000000000000000000000000
1069 // becomes
1070 // dst[00] = 0b00000000000000000000000000000000
1071 // dst[01] = 0b00000000000000000000000000000000
1072 // ...
1073 // dst[30] = 0b01000000000000000000000000000000
1074 // dst[31] = 0b10000000000000000000000000000000
1075
1076 #define tr32swap(a0, a1, j, m) t = (a0 ^ (a1 >> j)) & m; \
1077 a0 = a0 ^ t; \
1078 a1 = a1 ^ (t << j);
1079
1080 41160 void TransposeBits32x32(uint32_t *dst, uint32_t *src) {
1081 uint32_t m, t;
1082 uint32_t a0, a1, a2, a3, a4, a5, a6, a7,
1083 a8, a9, a10, a11, a12, a13, a14, a15,
1084 a16, a17, a18, a19, a20, a21, a22, a23,
1085 a24, a25, a26, a27, a28, a29, a30, a31;
1086
1087 41160 a0 = src[ 0]; a1 = src[ 1]; a2 = src[ 2]; a3 = src[ 3];
1088 41160 a4 = src[ 4]; a5 = src[ 5]; a6 = src[ 6]; a7 = src[ 7];
1089 41160 a8 = src[ 8]; a9 = src[ 9]; a10 = src[10]; a11 = src[11];
1090 41160 a12 = src[12]; a13 = src[13]; a14 = src[14]; a15 = src[15];
1091 41160 a16 = src[16]; a17 = src[17]; a18 = src[18]; a19 = src[19];
1092 41160 a20 = src[20]; a21 = src[21]; a22 = src[22]; a23 = src[23];
1093 41160 a24 = src[24]; a25 = src[25]; a26 = src[26]; a27 = src[27];
1094 41160 a28 = src[28]; a29 = src[29]; a30 = src[30]; a31 = src[31];
1095
1096 41160 m = 0x0000FFFF;
1097 41160 tr32swap(a0, a16, 16, m)
1098 41160 tr32swap(a1, a17, 16, m)
1099 41160 tr32swap(a2, a18, 16, m)
1100 41160 tr32swap(a3, a19, 16, m)
1101 41160 tr32swap(a4, a20, 16, m)
1102 41160 tr32swap(a5, a21, 16, m)
1103 41160 tr32swap(a6, a22, 16, m)
1104 41160 tr32swap(a7, a23, 16, m)
1105 41160 tr32swap(a8, a24, 16, m)
1106 41160 tr32swap(a9, a25, 16, m)
1107 41160 tr32swap(a10, a26, 16, m)
1108 41160 tr32swap(a11, a27, 16, m)
1109 41160 tr32swap(a12, a28, 16, m)
1110 41160 tr32swap(a13, a29, 16, m)
1111 41160 tr32swap(a14, a30, 16, m)
1112 41160 tr32swap(a15, a31, 16, m)
1113
1114 41160 m = 0x00FF00FF;
1115 41160 tr32swap(a0, a8, 8, m)
1116 41160 tr32swap(a1, a9, 8, m)
1117 41160 tr32swap(a2, a10, 8, m)
1118 41160 tr32swap(a3, a11, 8, m)
1119 41160 tr32swap(a4, a12, 8, m)
1120 41160 tr32swap(a5, a13, 8, m)
1121 41160 tr32swap(a6, a14, 8, m)
1122 41160 tr32swap(a7, a15, 8, m)
1123 41160 tr32swap(a16, a24, 8, m)
1124 41160 tr32swap(a17, a25, 8, m)
1125 41160 tr32swap(a18, a26, 8, m)
1126 41160 tr32swap(a19, a27, 8, m)
1127 41160 tr32swap(a20, a28, 8, m)
1128 41160 tr32swap(a21, a29, 8, m)
1129 41160 tr32swap(a22, a30, 8, m)
1130 41160 tr32swap(a23, a31, 8, m)
1131
1132 41160 m = 0x0F0F0F0F;
1133 41160 tr32swap(a0, a4, 4, m)
1134 41160 tr32swap(a1, a5, 4, m)
1135 41160 tr32swap(a2, a6, 4, m)
1136 41160 tr32swap(a3, a7, 4, m)
1137 41160 tr32swap(a8, a12, 4, m)
1138 41160 tr32swap(a9, a13, 4, m)
1139 41160 tr32swap(a10, a14, 4, m)
1140 41160 tr32swap(a11, a15, 4, m)
1141 41160 tr32swap(a16, a20, 4, m)
1142 41160 tr32swap(a17, a21, 4, m)
1143 41160 tr32swap(a18, a22, 4, m)
1144 41160 tr32swap(a19, a23, 4, m)
1145 41160 tr32swap(a24, a28, 4, m)
1146 41160 tr32swap(a25, a29, 4, m)
1147 41160 tr32swap(a26, a30, 4, m)
1148 41160 tr32swap(a27, a31, 4, m)
1149
1150 41160 m = 0x33333333;
1151 41160 tr32swap(a0, a2, 2, m)
1152 41160 tr32swap(a1, a3, 2, m)
1153 41160 tr32swap(a4, a6, 2, m)
1154 41160 tr32swap(a5, a7, 2, m)
1155 41160 tr32swap(a8, a10, 2, m)
1156 41160 tr32swap(a9, a11, 2, m)
1157 41160 tr32swap(a12, a14, 2, m)
1158 41160 tr32swap(a13, a15, 2, m)
1159 41160 tr32swap(a16, a18, 2, m)
1160 41160 tr32swap(a17, a19, 2, m)
1161 41160 tr32swap(a20, a22, 2, m)
1162 41160 tr32swap(a21, a23, 2, m)
1163 41160 tr32swap(a24, a26, 2, m)
1164 41160 tr32swap(a25, a27, 2, m)
1165 41160 tr32swap(a28, a30, 2, m)
1166 41160 tr32swap(a29, a31, 2, m)
1167
1168 41160 m = 0x55555555;
1169 41160 tr32swap(a0, a1, 1, m)
1170 41160 tr32swap(a2, a3, 1, m)
1171 41160 tr32swap(a4, a5, 1, m)
1172 41160 tr32swap(a6, a7, 1, m)
1173 41160 tr32swap(a8, a9, 1, m)
1174 41160 tr32swap(a10, a11, 1, m)
1175 41160 tr32swap(a12, a13, 1, m)
1176 41160 tr32swap(a14, a15, 1, m)
1177 41160 tr32swap(a16, a17, 1, m)
1178 41160 tr32swap(a18, a19, 1, m)
1179 41160 tr32swap(a20, a21, 1, m)
1180 41160 tr32swap(a22, a23, 1, m)
1181 41160 tr32swap(a24, a25, 1, m)
1182 41160 tr32swap(a26, a27, 1, m)
1183 41160 tr32swap(a28, a29, 1, m)
1184 41160 tr32swap(a30, a31, 1, m)
1185
1186 41160 dst[ 0] = a0; dst[ 1] = a1; dst[ 2] = a2; dst[ 3] = a3;
1187 41160 dst[ 4] = a4; dst[ 5] = a5; dst[ 6] = a6; dst[ 7] = a7;
1188 41160 dst[ 8] = a8; dst[ 9] = a9; dst[10] = a10; dst[11] = a11;
1189 41160 dst[12] = a12; dst[13] = a13; dst[14] = a14; dst[15] = a15;
1190 41160 dst[16] = a16; dst[17] = a17; dst[18] = a18; dst[19] = a19;
1191 41160 dst[20] = a20; dst[21] = a21; dst[22] = a22; dst[23] = a23;
1192 41160 dst[24] = a24; dst[25] = a25; dst[26] = a26; dst[27] = a27;
1193 41160 dst[28] = a28; dst[29] = a29; dst[30] = a30; dst[31] = a31;
1194 41160 }
1195 /////////////////////////////////////////////////////////////////////////////
1196
1197