FastLED 3.10.6
Loading...
Searching...
No Matches

◆ parseHeaderAndEnvelope()

bool fl::fled::parseHeaderAndEnvelope ( const fl::u8 * data,
fl::size len,
ParsedHeader * outHeader,
fl::json * outEnvelope )

Definition at line 22 of file parser.cpp.hpp.

24 {
25 if (!outHeader || !outEnvelope) {
26 return false;
27 }
28 if (!data) {
29 return false;
30 }
31 if (len < kHeaderBytes) {
32 return false;
33 }
34 if (data[0] != kMagic[0] || data[1] != kMagic[1] ||
35 data[2] != kMagic[2] || data[3] != kMagic[3]) {
36 return false;
37 }
38 const fl::u8 ver = data[4];
39 if (ver != kVersionV1) {
40 return false;
41 }
42 if (data[6] != 0 || data[7] != 0) {
43 return false;
44 }
45 const fl::u8 pixelFormat = data[5];
46 const fl::u32 jsonLen =
47 static_cast<fl::u32>(data[8]) |
48 (static_cast<fl::u32>(data[9]) << 8) |
49 (static_cast<fl::u32>(data[10]) << 16) |
50 (static_cast<fl::u32>(data[11]) << 24);
51 // Bound-check in u32 BEFORE narrowing to fl::size - on 16-bit targets
52 // (AVR) fl::size is 16 bits and naive narrowing would wraparound.
53 if (jsonLen > static_cast<fl::u32>(kMaxJsonBytes)) {
54 return false;
55 }
56 if (jsonLen > static_cast<fl::u32>(len - kHeaderBytes)) {
57 return false;
58 }
59 const fl::size jsonLenSz = static_cast<fl::size>(jsonLen);
60 // Build a string view over the JSON bytes (fl::string copies the bytes
61 // internally - required because json::parse takes const string&).
62 fl::string jsonText(fl::reinterpret_cast_<const char *>(data + kHeaderBytes),
63 jsonLenSz);
64 fl::json parsed = fl::json::parse(jsonText);
65 // parse() returns json(nullptr) on failure - that flags an envelope
66 // that is not a valid JSON document. Strict-reject per design.
67 if (!parsed.has_value()) {
68 return false;
69 }
70 outHeader->version = ver;
71 outHeader->pixelFormat = pixelFormat;
72 outHeader->payloadOffset = kHeaderBytes + jsonLenSz;
73 *outEnvelope = parsed;
74 return true;
75}
bool has_value() const FL_NO_EXCEPT
Definition json.h:819
static json parse(const fl::string &txt) FL_NO_EXCEPT
Definition json.h:842
fl::size payloadOffset
Definition parser.h:22
unsigned char u8
Definition stdint.h:131
To reinterpret_cast_(const From &from) FL_NO_EXCEPT
Definition bit_cast.h:88

References fl::FL_NO_EXCEPT, fl::json::has_value(), fl::json::parse(), and fl::reinterpret_cast_().

Referenced by fl::Fled::loadFromStatic(), and fl::Fled::loadFromVector().

+ Here is the call graph for this function:
+ Here is the caller graph for this function: