| Flutter Linux Embedder |

fl_standard_message_codec.cc
Go to the documentation of this file.
1 // Copyright 2013 The Flutter Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "flutter/shell/platform/linux/public/flutter_linux/fl_standard_message_codec.h"
6
7 #include <gmodule.h>
8
9 #include <cstring>
10
11 // See lib/src/services/message_codecs.dart in Flutter source for description of
12 // encoding.
13
14 // Type values.
15 static constexpr int kValueNull = 0;
16 static constexpr int kValueTrue = 1;
17 static constexpr int kValueFalse = 2;
18 static constexpr int kValueInt32 = 3;
19 static constexpr int kValueInt64 = 4;
20 static constexpr int kValueFloat64 = 6;
21 static constexpr int kValueString = 7;
22 static constexpr int kValueUint8List = 8;
23 static constexpr int kValueInt32List = 9;
24 static constexpr int kValueInt64List = 10;
25 static constexpr int kValueFloat64List = 11;
26 static constexpr int kValueList = 12;
27 static constexpr int kValueMap = 13;
28 static constexpr int kValueFloat32List = 14;
29
30 G_DEFINE_TYPE(FlStandardMessageCodec,
31 fl_standard_message_codec,
32 fl_message_codec_get_type())
33
34 // Functions to write standard C number types.
35
36 static void write_uint8(GByteArray* buffer, uint8_t value) {
37 g_byte_array_append(buffer, &value, sizeof(uint8_t));
38 }
39
40 static void write_uint16(GByteArray* buffer, uint16_t value) {
41 g_byte_array_append(buffer, reinterpret_cast<uint8_t*>(&value),
42 sizeof(uint16_t));
43 }
44
45 static void write_uint32(GByteArray* buffer, uint32_t value) {
46 g_byte_array_append(buffer, reinterpret_cast<uint8_t*>(&value),
47 sizeof(uint32_t));
48 }
49
50 static void write_int32(GByteArray* buffer, int32_t value) {
51 g_byte_array_append(buffer, reinterpret_cast<uint8_t*>(&value),
52 sizeof(int32_t));
53 }
54
55 static void write_int64(GByteArray* buffer, int64_t value) {
56 g_byte_array_append(buffer, reinterpret_cast<uint8_t*>(&value),
57 sizeof(int64_t));
58 }
59
60 static void write_float64(GByteArray* buffer, double value) {
61 g_byte_array_append(buffer, reinterpret_cast<uint8_t*>(&value),
62 sizeof(double));
63 }
64
65 // Write padding bytes to align to @align multiple of bytes.
66 static void write_align(GByteArray* buffer, guint align) {
67 while (buffer->len % align != 0) {
68 write_uint8(buffer, 0);
69 }
70 }
71
72 // Checks there is enough data in @buffer to be read.
73 static gboolean check_size(GBytes* buffer,
74 size_t offset,
75 size_t required,
76 GError** error) {
77 if (offset + required > g_bytes_get_size(buffer)) {
78 g_set_error(error, FL_MESSAGE_CODEC_ERROR,
79 FL_MESSAGE_CODEC_ERROR_OUT_OF_DATA, "Unexpected end of data");
80 return FALSE;
81 }
82 return TRUE;
83 }
84
85 // Skip bytes to align next read on @align multiple of bytes.
86 static gboolean read_align(GBytes* buffer,
87 size_t* offset,
88 size_t align,
89 GError** error) {
90 if ((*offset) % align == 0) {
91 return TRUE;
92 }
93
94 size_t required = align - (*offset) % align;
95 if (!check_size(buffer, *offset, required, error)) {
96 return FALSE;
97 }
98
99 (*offset) += required;
100 return TRUE;
101 }
102
103 // Gets a pointer to the given offset in @buffer.
104 static const uint8_t* get_data(GBytes* buffer, size_t* offset) {
105 return static_cast<const uint8_t*>(g_bytes_get_data(buffer, nullptr)) +
106 *offset;
107 }
108
109 // Reads an unsigned 8 bit number from @buffer and writes it to @value.
110 // Returns TRUE if successful, otherwise sets an error.
111 static gboolean read_uint8(GBytes* buffer,
112 size_t* offset,
113 uint8_t* value,
114 GError** error) {
115 if (!check_size(buffer, *offset, sizeof(uint8_t), error)) {
116 return FALSE;
117 }
118
119 *value = get_data(buffer, offset)[0];
120 (*offset)++;
121 return TRUE;
122 }
123
124 // Reads an unsigned 16 bit integer from @buffer and writes it to @value.
125 // Returns TRUE if successful, otherwise sets an error.
126 static gboolean read_uint16(GBytes* buffer,
127 size_t* offset,
128 uint16_t* value,
129 GError** error) {
130 if (!check_size(buffer, *offset, sizeof(uint16_t), error)) {
131 return FALSE;
132 }
133
134 *value = reinterpret_cast<const uint16_t*>(get_data(buffer, offset))[0];
135 *offset += sizeof(uint16_t);
136 return TRUE;
137 }
138
139 // Reads an unsigned 32 bit integer from @buffer and writes it to @value.
140 // Returns TRUE if successful, otherwise sets an error.
141 static gboolean read_uint32(GBytes* buffer,
142 size_t* offset,
143 uint32_t* value,
144 GError** error) {
145 if (!check_size(buffer, *offset, sizeof(uint32_t), error)) {
146 return FALSE;
147 }
148
149 *value = reinterpret_cast<const uint32_t*>(get_data(buffer, offset))[0];
150 *offset += sizeof(uint32_t);
151 return TRUE;
152 }
153
154 // Reads a #FL_VALUE_TYPE_INT stored as a signed 32 bit integer from @buffer.
155 // Returns a new #FlValue of type #FL_VALUE_TYPE_INT if successful or %NULL on
156 // error.
157 static FlValue* read_int32_value(GBytes* buffer,
158 size_t* offset,
159 GError** error) {
160 if (!check_size(buffer, *offset, sizeof(int32_t), error)) {
161 return nullptr;
162 }
163
164 FlValue* value = fl_value_new_int(
165 reinterpret_cast<const int32_t*>(get_data(buffer, offset))[0]);
166 *offset += sizeof(int32_t);
167 return value;
168 }
169
170 // Reads a #FL_VALUE_TYPE_INT stored as a signed 64 bit integer from @buffer.
171 // Returns a new #FlValue of type #FL_VALUE_TYPE_INT if successful or %NULL on
172 // error.
173 static FlValue* read_int64_value(GBytes* buffer,
174 size_t* offset,
175 GError** error) {
176 if (!check_size(buffer, *offset, sizeof(int64_t), error)) {
177 return nullptr;
178 }
179
180 FlValue* value = fl_value_new_int(
181 reinterpret_cast<const int64_t*>(get_data(buffer, offset))[0]);
182 *offset += sizeof(int64_t);
183 return value;
184 }
185
186 // Reads a 64 bit floating point number from @buffer and writes it to @value.
187 // Returns a new #FlValue of type #FL_VALUE_TYPE_FLOAT if successful or %NULL on
188 // error.
189 static FlValue* read_float64_value(GBytes* buffer,
190 size_t* offset,
191 GError** error) {
192 if (!read_align(buffer, offset, 8, error)) {
193 return nullptr;
194 }
195 if (!check_size(buffer, *offset, sizeof(double), error)) {
196 return nullptr;
197 }
198
199 FlValue* value = fl_value_new_float(
200 reinterpret_cast<const double*>(get_data(buffer, offset))[0]);
201 *offset += sizeof(double);
202 return value;
203 }
204
205 // Reads an UTF-8 text string from @buffer in standard codec format.
206 // Returns a new #FlValue of type #FL_VALUE_TYPE_STRING if successful or %NULL
207 // on error.
208 static FlValue* read_string_value(FlStandardMessageCodec* self,
209 GBytes* buffer,
210 size_t* offset,
211 GError** error) {
212 uint32_t length;
213 if (!fl_standard_message_codec_read_size(self, buffer, offset, &length,
214 error)) {
215 return nullptr;
216 }
217 if (!check_size(buffer, *offset, length, error)) {
218 return nullptr;
219 }
220 FlValue* value = fl_value_new_string_sized(
221 reinterpret_cast<const gchar*>(get_data(buffer, offset)), length);
222 *offset += length;
223 return value;
224 }
225
226 // Reads an unsigned 8 bit list from @buffer in standard codec format.
227 // Returns a new #FlValue of type #FL_VALUE_TYPE_UINT8_LIST if successful or
228 // %NULL on error.
229 static FlValue* read_uint8_list_value(FlStandardMessageCodec* self,
230 GBytes* buffer,
231 size_t* offset,
232 GError** error) {
233 uint32_t length;
234 if (!fl_standard_message_codec_read_size(self, buffer, offset, &length,
235 error)) {
236 return nullptr;
237 }
238 if (!check_size(buffer, *offset, sizeof(uint8_t) * length, error)) {
239 return nullptr;
240 }
241 FlValue* value = fl_value_new_uint8_list(get_data(buffer, offset), length);
242 *offset += length;
243 return value;
244 }
245
246 // Reads a signed 32 bit list from @buffer in standard codec format.
247 // Returns a new #FlValue of type #FL_VALUE_TYPE_INT32_LIST if successful or
248 // %NULL on error.
249 static FlValue* read_int32_list_value(FlStandardMessageCodec* self,
250 GBytes* buffer,
251 size_t* offset,
252 GError** error) {
253 uint32_t length;
254 if (!fl_standard_message_codec_read_size(self, buffer, offset, &length,
255 error)) {
256 return nullptr;
257 }
258 if (!read_align(buffer, offset, 4, error)) {
259 return nullptr;
260 }
261 if (!check_size(buffer, *offset, sizeof(int32_t) * length, error)) {
262 return nullptr;
263 }
264 FlValue* value = fl_value_new_int32_list(
265 reinterpret_cast<const int32_t*>(get_data(buffer, offset)), length);
266 *offset += sizeof(int32_t) * length;
267 return value;
268 }
269
270 // Reads a signed 64 bit list from @buffer in standard codec format.
271 // Returns a new #FlValue of type #FL_VALUE_TYPE_INT64_LIST if successful or
272 // %NULL on error.
273 static FlValue* read_int64_list_value(FlStandardMessageCodec* self,
274 GBytes* buffer,
275 size_t* offset,
276 GError** error) {
277 uint32_t length;
278 if (!fl_standard_message_codec_read_size(self, buffer, offset, &length,
279 error)) {
280 return nullptr;
281 }
282 if (!read_align(buffer, offset, 8, error)) {
283 return nullptr;
284 }
285 if (!check_size(buffer, *offset, sizeof(int64_t) * length, error)) {
286 return nullptr;
287 }
288 FlValue* value = fl_value_new_int64_list(
289 reinterpret_cast<const int64_t*>(get_data(buffer, offset)), length);
290 *offset += sizeof(int64_t) * length;
291 return value;
292 }
293
294 // Reads a 32 bit floating point number list from @buffer in standard codec
295 // format. Returns a new #FlValue of type #FL_VALUE_TYPE_FLOAT32_LIST if
296 // successful or %NULL on error.
297 static FlValue* read_float32_list_value(FlStandardMessageCodec* self,
298 GBytes* buffer,
299 size_t* offset,
300 GError** error) {
301 uint32_t length;
302 if (!fl_standard_message_codec_read_size(self, buffer, offset, &length,
303 error)) {
304 return nullptr;
305 }
306 if (!read_align(buffer, offset, 4, error)) {
307 return nullptr;
308 }
309 if (!check_size(buffer, *offset, sizeof(float) * length, error)) {
310 return nullptr;
311 }
312 FlValue* value = fl_value_new_float32_list(
313 reinterpret_cast<const float*>(get_data(buffer, offset)), length);
314 *offset += sizeof(float) * length;
315 return value;
316 }
317
318 // Reads a floating point number list from @buffer in standard codec format.
319 // Returns a new #FlValue of type #FL_VALUE_TYPE_FLOAT_LIST if successful or
320 // %NULL on error.
321 static FlValue* read_float64_list_value(FlStandardMessageCodec* self,
322 GBytes* buffer,
323 size_t* offset,
324 GError** error) {
325 uint32_t length;
326 if (!fl_standard_message_codec_read_size(self, buffer, offset, &length,
327 error)) {
328 return nullptr;
329 }
330 if (!read_align(buffer, offset, 8, error)) {
331 return nullptr;
332 }
333 if (!check_size(buffer, *offset, sizeof(double) * length, error)) {
334 return nullptr;
335 }
336 FlValue* value = fl_value_new_float_list(
337 reinterpret_cast<const double*>(get_data(buffer, offset)), length);
338 *offset += sizeof(double) * length;
339 return value;
340 }
341
342 // Reads a list from @buffer in standard codec format.
343 // Returns a new #FlValue of type #FL_VALUE_TYPE_LIST if successful or %NULL on
344 // error.
345 static FlValue* read_list_value(FlStandardMessageCodec* self,
346 GBytes* buffer,
347 size_t* offset,
348 GError** error) {
349 uint32_t length;
350 if (!fl_standard_message_codec_read_size(self, buffer, offset, &length,
351 error)) {
352 return nullptr;
353 }
354
355 g_autoptr(FlValue) list = fl_value_new_list();
356 for (size_t i = 0; i < length; i++) {
357 g_autoptr(FlValue) child =
358 fl_standard_message_codec_read_value(self, buffer, offset, error);
359 if (child == nullptr) {
360 return nullptr;
361 }
362 fl_value_append(list, child);
363 }
364
365 return fl_value_ref(list);
366 }
367
368 // Reads a map from @buffer in standard codec format.
369 // Returns a new #FlValue of type #FL_VALUE_TYPE_MAP if successful or %NULL on
370 // error.
371 static FlValue* read_map_value(FlStandardMessageCodec* self,
372 GBytes* buffer,
373 size_t* offset,
374 GError** error) {
375 uint32_t length;
376 if (!fl_standard_message_codec_read_size(self, buffer, offset, &length,
377 error)) {
378 return nullptr;
379 }
380
381 g_autoptr(FlValue) map = fl_value_new_map();
382 for (size_t i = 0; i < length; i++) {
383 g_autoptr(FlValue) key =
384 fl_standard_message_codec_read_value(self, buffer, offset, error);
385 if (key == nullptr) {
386 return nullptr;
387 }
388 g_autoptr(FlValue) value =
389 fl_standard_message_codec_read_value(self, buffer, offset, error);
390 if (value == nullptr) {
391 return nullptr;
392 }
393 fl_value_set(map, key, value);
394 }
395
396 return fl_value_ref(map);
397 }
398
399 // Implements FlMessageCodec::encode_message.
400 static GBytes* fl_standard_message_codec_encode_message(FlMessageCodec* codec,
401 FlValue* message,
402 GError** error) {
403 FlStandardMessageCodec* self =
404 reinterpret_cast<FlStandardMessageCodec*>(codec);
405
406 g_autoptr(GByteArray) buffer = g_byte_array_new();
407 if (!fl_standard_message_codec_write_value(self, buffer, message, error)) {
408 return nullptr;
409 }
410 return g_byte_array_free_to_bytes(
411 static_cast<GByteArray*>(g_steal_pointer(&buffer)));
412 }
413
414 // Implements FlMessageCodec::decode_message.
415 static FlValue* fl_standard_message_codec_decode_message(FlMessageCodec* codec,
416 GBytes* message,
417 GError** error) {
418 if (g_bytes_get_size(message) == 0) {
419 return fl_value_new_null();
420 }
421
422 FlStandardMessageCodec* self =
423 reinterpret_cast<FlStandardMessageCodec*>(codec);
424
425 size_t offset = 0;
426 g_autoptr(FlValue) value =
427 fl_standard_message_codec_read_value(self, message, &offset, error);
428 if (value == nullptr) {
429 return nullptr;
430 }
431
432 if (offset != g_bytes_get_size(message)) {
433 g_set_error(error, FL_MESSAGE_CODEC_ERROR,
434 FL_MESSAGE_CODEC_ERROR_ADDITIONAL_DATA,
435 "Unused %zi bytes after standard message",
436 g_bytes_get_size(message) - offset);
437 return nullptr;
438 }
439
440 return fl_value_ref(value);
441 }
442
443 // Implements FlStandardMessageCodec::write_value.
444 static gboolean fl_standard_message_codec_real_write_value(
445 FlStandardMessageCodec* self,
446 GByteArray* buffer,
447 FlValue* value,
448 GError** error) {
449 if (value == nullptr) {
450 write_uint8(buffer, kValueNull);
451 return TRUE;
452 }
453
454 switch (fl_value_get_type(value)) {
455 case FL_VALUE_TYPE_NULL:
456 write_uint8(buffer, kValueNull);
457 return TRUE;
458 case FL_VALUE_TYPE_BOOL:
459 if (fl_value_get_bool(value)) {
460 write_uint8(buffer, kValueTrue);
461 } else {
462 write_uint8(buffer, kValueFalse);
463 }
464 return TRUE;
465 case FL_VALUE_TYPE_INT: {
466 int64_t v = fl_value_get_int(value);
467 if (v >= INT32_MIN && v <= INT32_MAX) {
468 write_uint8(buffer, kValueInt32);
469 write_int32(buffer, v);
470 } else {
471 write_uint8(buffer, kValueInt64);
472 write_int64(buffer, v);
473 }
474 return TRUE;
475 }
476 case FL_VALUE_TYPE_FLOAT:
477 write_uint8(buffer, kValueFloat64);
478 write_align(buffer, 8);
479 write_float64(buffer, fl_value_get_float(value));
480 return TRUE;
481 case FL_VALUE_TYPE_STRING: {
482 write_uint8(buffer, kValueString);
483 const char* text = fl_value_get_string(value);
484 size_t length = strlen(text);
485 fl_standard_message_codec_write_size(self, buffer, length);
486 g_byte_array_append(buffer, reinterpret_cast<const uint8_t*>(text),
487 length);
488 return TRUE;
489 }
490 case FL_VALUE_TYPE_UINT8_LIST: {
491 write_uint8(buffer, kValueUint8List);
492 size_t length = fl_value_get_length(value);
493 fl_standard_message_codec_write_size(self, buffer, length);
494 g_byte_array_append(buffer, fl_value_get_uint8_list(value),
495 sizeof(uint8_t) * length);
496 return TRUE;
497 }
498 case FL_VALUE_TYPE_INT32_LIST: {
499 write_uint8(buffer, kValueInt32List);
500 size_t length = fl_value_get_length(value);
501 fl_standard_message_codec_write_size(self, buffer, length);
502 write_align(buffer, 4);
503 g_byte_array_append(
504 buffer,
505 reinterpret_cast<const uint8_t*>(fl_value_get_int32_list(value)),
506 sizeof(int32_t) * length);
507 return TRUE;
508 }
509 case FL_VALUE_TYPE_INT64_LIST: {
510 write_uint8(buffer, kValueInt64List);
511 size_t length = fl_value_get_length(value);
512 fl_standard_message_codec_write_size(self, buffer, length);
513 write_align(buffer, 8);
514 g_byte_array_append(
515 buffer,
516 reinterpret_cast<const uint8_t*>(fl_value_get_int64_list(value)),
517 sizeof(int64_t) * length);
518 return TRUE;
519 }
520 case FL_VALUE_TYPE_FLOAT32_LIST: {
521 write_uint8(buffer, kValueFloat32List);
522 size_t length = fl_value_get_length(value);
523 fl_standard_message_codec_write_size(self, buffer, length);
524 write_align(buffer, 4);
525 g_byte_array_append(
526 buffer,
527 reinterpret_cast<const uint8_t*>(fl_value_get_float32_list(value)),
528 sizeof(float) * length);
529 return TRUE;
530 }
531 case FL_VALUE_TYPE_FLOAT_LIST: {
532 write_uint8(buffer, kValueFloat64List);
533 size_t length = fl_value_get_length(value);
534 fl_standard_message_codec_write_size(self, buffer, length);
535 write_align(buffer, 8);
536 g_byte_array_append(
537 buffer,
538 reinterpret_cast<const uint8_t*>(fl_value_get_float_list(value)),
539 sizeof(double) * length);
540 return TRUE;
541 }
542 case FL_VALUE_TYPE_LIST:
543 write_uint8(buffer, kValueList);
544 fl_standard_message_codec_write_size(self, buffer,
545 fl_value_get_length(value));
546 for (size_t i = 0; i < fl_value_get_length(value); i++) {
547 if (!fl_standard_message_codec_write_value(
548 self, buffer, fl_value_get_list_value(value, i), error)) {
549 return FALSE;
550 }
551 }
552 return TRUE;
553 case FL_VALUE_TYPE_MAP:
554 write_uint8(buffer, kValueMap);
555 fl_standard_message_codec_write_size(self, buffer,
556 fl_value_get_length(value));
557 for (size_t i = 0; i < fl_value_get_length(value); i++) {
558 if (!fl_standard_message_codec_write_value(
559 self, buffer, fl_value_get_map_key(value, i), error) ||
560 !fl_standard_message_codec_write_value(
561 self, buffer, fl_value_get_map_value(value, i), error)) {
562 return FALSE;
563 }
564 }
565 return TRUE;
566 case FL_VALUE_TYPE_CUSTOM:
567 g_set_error(error, FL_MESSAGE_CODEC_ERROR,
568 FL_MESSAGE_CODEC_ERROR_UNSUPPORTED_TYPE,
569 "Custom value not implemented");
570 return FALSE;
571 }
572
573 g_set_error(error, FL_MESSAGE_CODEC_ERROR,
574 FL_MESSAGE_CODEC_ERROR_UNSUPPORTED_TYPE,
575 "Unexpected FlValue type %d", fl_value_get_type(value));
576 return FALSE;
577 }
578
579 // Implements FlStandardMessageCodec::read_value_of_type.
580 static FlValue* fl_standard_message_codec_read_value_of_type(
581 FlStandardMessageCodec* self,
582 GBytes* buffer,
583 size_t* offset,
584 int type,
585 GError** error) {
586 g_autoptr(FlValue) value = nullptr;
587 if (type == kValueNull) {
588 return fl_value_new_null();
589 } else if (type == kValueTrue) {
590 return fl_value_new_bool(TRUE);
591 } else if (type == kValueFalse) {
592 return fl_value_new_bool(FALSE);
593 } else if (type == kValueInt32) {
594 value = read_int32_value(buffer, offset, error);
595 } else if (type == kValueInt64) {
596 value = read_int64_value(buffer, offset, error);
597 } else if (type == kValueFloat64) {
598 value = read_float64_value(buffer, offset, error);
599 } else if (type == kValueString) {
600 value = read_string_value(self, buffer, offset, error);
601 } else if (type == kValueUint8List) {
602 value = read_uint8_list_value(self, buffer, offset, error);
603 } else if (type == kValueInt32List) {
604 value = read_int32_list_value(self, buffer, offset, error);
605 } else if (type == kValueInt64List) {
606 value = read_int64_list_value(self, buffer, offset, error);
607 } else if (type == kValueFloat32List) {
608 value = read_float32_list_value(self, buffer, offset, error);
609 } else if (type == kValueFloat64List) {
610 value = read_float64_list_value(self, buffer, offset, error);
611 } else if (type == kValueList) {
612 value = read_list_value(self, buffer, offset, error);
613 } else if (type == kValueMap) {
614 value = read_map_value(self, buffer, offset, error);
615 } else {
616 g_set_error(error, FL_MESSAGE_CODEC_ERROR,
617 FL_MESSAGE_CODEC_ERROR_UNSUPPORTED_TYPE,
618 "Unexpected standard codec type %02x", type);
619 return nullptr;
620 }
621
622 return value == nullptr ? nullptr : fl_value_ref(value);
623 }
624
625 static void fl_standard_message_codec_class_init(
626 FlStandardMessageCodecClass* klass) {
627 FL_MESSAGE_CODEC_CLASS(klass)->encode_message =
628 fl_standard_message_codec_encode_message;
629 FL_MESSAGE_CODEC_CLASS(klass)->decode_message =
630 fl_standard_message_codec_decode_message;
631 klass->write_value = fl_standard_message_codec_real_write_value;
632 klass->read_value_of_type = fl_standard_message_codec_read_value_of_type;
633 }
634
635 static void fl_standard_message_codec_init(FlStandardMessageCodec* self) {}
636
637 G_MODULE_EXPORT FlStandardMessageCodec* fl_standard_message_codec_new() {
638 return static_cast<FlStandardMessageCodec*>(
639 g_object_new(fl_standard_message_codec_get_type(), nullptr));
640 }
641
642 G_MODULE_EXPORT void fl_standard_message_codec_write_size(
643 FlStandardMessageCodec* codec,
644 GByteArray* buffer,
645 uint32_t size) {
646 if (size < 254) {
647 write_uint8(buffer, size);
648 } else if (size <= 0xffff) {
649 write_uint8(buffer, 254);
650 write_uint16(buffer, size);
651 } else {
652 write_uint8(buffer, 255);
653 write_uint32(buffer, size);
654 }
655 }
656
657 G_MODULE_EXPORT gboolean
658 fl_standard_message_codec_read_size(FlStandardMessageCodec* codec,
659 GBytes* buffer,
660 size_t* offset,
661 uint32_t* value,
662 GError** error) {
663 uint8_t value8;
664 if (!read_uint8(buffer, offset, &value8, error)) {
665 return FALSE;
666 }
667
668 if (value8 == 255) {
669 if (!read_uint32(buffer, offset, value, error)) {
670 return FALSE;
671 }
672 } else if (value8 == 254) {
673 uint16_t value16;
674 if (!read_uint16(buffer, offset, &value16, error)) {
675 return FALSE;
676 }
677 *value = value16;
678 } else {
679 *value = value8;
680 }
681
682 return TRUE;
683 }
684
685 G_MODULE_EXPORT gboolean
686 fl_standard_message_codec_write_value(FlStandardMessageCodec* self,
687 GByteArray* buffer,
688 FlValue* value,
689 GError** error) {
690 return FL_STANDARD_MESSAGE_CODEC_GET_CLASS(self)->write_value(self, buffer,
691 value, error);
692 }
693
694 G_MODULE_EXPORT FlValue* fl_standard_message_codec_read_value(
695 FlStandardMessageCodec* self,
696 GBytes* buffer,
697 size_t* offset,
698 GError** error) {
699 uint8_t type;
700 if (!read_uint8(buffer, offset, &type, error)) {
701 return nullptr;
702 }
703
704 return FL_STANDARD_MESSAGE_CODEC_GET_CLASS(self)->read_value_of_type(
705 self, buffer, offset, type, error);
706 }
g_autoptr
g_autoptr(GMutexLocker) locker
TRUE
return TRUE
Definition: fl_compositor_software.cc:68
FL_MESSAGE_CODEC_ERROR_OUT_OF_DATA
@ FL_MESSAGE_CODEC_ERROR_OUT_OF_DATA
Definition: fl_message_codec.h:34
FL_MESSAGE_CODEC_ERROR_UNSUPPORTED_TYPE
@ FL_MESSAGE_CODEC_ERROR_UNSUPPORTED_TYPE
Definition: fl_message_codec.h:36
FL_MESSAGE_CODEC_ERROR_ADDITIONAL_DATA
@ FL_MESSAGE_CODEC_ERROR_ADDITIONAL_DATA
Definition: fl_message_codec.h:35
FL_MESSAGE_CODEC_ERROR
#define FL_MESSAGE_CODEC_ERROR
Definition: fl_message_codec.h:30
buffer
static const uint8_t buffer[]
Definition: fl_pixel_buffer_texture_test.cc:44
error
const uint8_t uint32_t uint32_t GError ** error
Definition: fl_pixel_buffer_texture_test.cc:40
i
int i
Definition: fl_socket_accessible.cc:18
fl_standard_message_codec_read_size
G_MODULE_EXPORT gboolean fl_standard_message_codec_read_size(FlStandardMessageCodec *codec, GBytes *buffer, size_t *offset, uint32_t *value, GError **error)
Definition: fl_standard_message_codec.cc:658
fl_standard_message_codec_write_value
G_MODULE_EXPORT gboolean fl_standard_message_codec_write_value(FlStandardMessageCodec *self, GByteArray *buffer, FlValue *value, GError **error)
Definition: fl_standard_message_codec.cc:686
kValueNull
static constexpr int kValueNull
Definition: fl_standard_message_codec.cc:15
kValueString
static constexpr int kValueString
Definition: fl_standard_message_codec.cc:21
read_int32_value
static FlValue * read_int32_value(GBytes *buffer, size_t *offset, GError **error)
Definition: fl_standard_message_codec.cc:157
read_uint32
static gboolean read_uint32(GBytes *buffer, size_t *offset, uint32_t *value, GError **error)
Definition: fl_standard_message_codec.cc:141
read_float32_list_value
static FlValue * read_float32_list_value(FlStandardMessageCodec *self, GBytes *buffer, size_t *offset, GError **error)
Definition: fl_standard_message_codec.cc:297
fl_standard_message_codec_write_size
G_MODULE_EXPORT void fl_standard_message_codec_write_size(FlStandardMessageCodec *codec, GByteArray *buffer, uint32_t size)
Definition: fl_standard_message_codec.cc:642
read_float64_value
static FlValue * read_float64_value(GBytes *buffer, size_t *offset, GError **error)
Definition: fl_standard_message_codec.cc:189
check_size
static gboolean check_size(GBytes *buffer, size_t offset, size_t required, GError **error)
Definition: fl_standard_message_codec.cc:73
kValueTrue
static constexpr int kValueTrue
Definition: fl_standard_message_codec.cc:16
fl_standard_message_codec_read_value
G_MODULE_EXPORT FlValue * fl_standard_message_codec_read_value(FlStandardMessageCodec *self, GBytes *buffer, size_t *offset, GError **error)
Definition: fl_standard_message_codec.cc:694
read_string_value
static FlValue * read_string_value(FlStandardMessageCodec *self, GBytes *buffer, size_t *offset, GError **error)
Definition: fl_standard_message_codec.cc:208
fl_standard_message_codec_read_value_of_type
static FlValue * fl_standard_message_codec_read_value_of_type(FlStandardMessageCodec *self, GBytes *buffer, size_t *offset, int type, GError **error)
Definition: fl_standard_message_codec.cc:580
fl_standard_message_codec_real_write_value
static gboolean fl_standard_message_codec_real_write_value(FlStandardMessageCodec *self, GByteArray *buffer, FlValue *value, GError **error)
Definition: fl_standard_message_codec.cc:444
read_align
static gboolean read_align(GBytes *buffer, size_t *offset, size_t align, GError **error)
Definition: fl_standard_message_codec.cc:86
write_align
static void write_align(GByteArray *buffer, guint align)
Definition: fl_standard_message_codec.cc:66
kValueInt64List
static constexpr int kValueInt64List
Definition: fl_standard_message_codec.cc:24
kValueFloat64List
static constexpr int kValueFloat64List
Definition: fl_standard_message_codec.cc:25
value
uint8_t value
Definition: fl_standard_message_codec.cc:36
kValueFloat32List
static constexpr int kValueFloat32List
Definition: fl_standard_message_codec.cc:28
read_list_value
static FlValue * read_list_value(FlStandardMessageCodec *self, GBytes *buffer, size_t *offset, GError **error)
Definition: fl_standard_message_codec.cc:345
read_uint8_list_value
static FlValue * read_uint8_list_value(FlStandardMessageCodec *self, GBytes *buffer, size_t *offset, GError **error)
Definition: fl_standard_message_codec.cc:229
kValueUint8List
static constexpr int kValueUint8List
Definition: fl_standard_message_codec.cc:22
read_int32_list_value
static FlValue * read_int32_list_value(FlStandardMessageCodec *self, GBytes *buffer, size_t *offset, GError **error)
Definition: fl_standard_message_codec.cc:249
fl_standard_message_codec_init
static void fl_standard_message_codec_init(FlStandardMessageCodec *self)
Definition: fl_standard_message_codec.cc:635
kValueInt32
static constexpr int kValueInt32
Definition: fl_standard_message_codec.cc:18
read_map_value
static FlValue * read_map_value(FlStandardMessageCodec *self, GBytes *buffer, size_t *offset, GError **error)
Definition: fl_standard_message_codec.cc:371
kValueList
static constexpr int kValueList
Definition: fl_standard_message_codec.cc:26
read_uint16
static gboolean read_uint16(GBytes *buffer, size_t *offset, uint16_t *value, GError **error)
Definition: fl_standard_message_codec.cc:126
kValueFloat64
static constexpr int kValueFloat64
Definition: fl_standard_message_codec.cc:20
kValueMap
static constexpr int kValueMap
Definition: fl_standard_message_codec.cc:27
fl_standard_message_codec_new
G_MODULE_EXPORT FlStandardMessageCodec * fl_standard_message_codec_new()
Definition: fl_standard_message_codec.cc:637
kValueInt32List
static constexpr int kValueInt32List
Definition: fl_standard_message_codec.cc:23
write_uint32
static void write_uint32(GByteArray *buffer, uint32_t value)
Definition: fl_standard_message_codec.cc:45
read_int64_value
static FlValue * read_int64_value(GBytes *buffer, size_t *offset, GError **error)
Definition: fl_standard_message_codec.cc:173
write_int64
static void write_int64(GByteArray *buffer, int64_t value)
Definition: fl_standard_message_codec.cc:55
fl_standard_message_codec_encode_message
static GBytes * fl_standard_message_codec_encode_message(FlMessageCodec *codec, FlValue *message, GError **error)
Definition: fl_standard_message_codec.cc:400
fl_standard_message_codec_decode_message
static FlValue * fl_standard_message_codec_decode_message(FlMessageCodec *codec, GBytes *message, GError **error)
Definition: fl_standard_message_codec.cc:415
write_uint16
static void write_uint16(GByteArray *buffer, uint16_t value)
Definition: fl_standard_message_codec.cc:40
read_uint8
static gboolean read_uint8(GBytes *buffer, size_t *offset, uint8_t *value, GError **error)
Definition: fl_standard_message_codec.cc:111
write_float64
static void write_float64(GByteArray *buffer, double value)
Definition: fl_standard_message_codec.cc:60
kValueInt64
static constexpr int kValueInt64
Definition: fl_standard_message_codec.cc:19
write_int32
static void write_int32(GByteArray *buffer, int32_t value)
Definition: fl_standard_message_codec.cc:50
read_int64_list_value
static FlValue * read_int64_list_value(FlStandardMessageCodec *self, GBytes *buffer, size_t *offset, GError **error)
Definition: fl_standard_message_codec.cc:273
fl_standard_message_codec_class_init
static void fl_standard_message_codec_class_init(FlStandardMessageCodecClass *klass)
Definition: fl_standard_message_codec.cc:625
get_data
static const uint8_t * get_data(GBytes *buffer, size_t *offset)
Definition: fl_standard_message_codec.cc:104
read_float64_list_value
static FlValue * read_float64_list_value(FlStandardMessageCodec *self, GBytes *buffer, size_t *offset, GError **error)
Definition: fl_standard_message_codec.cc:321
kValueFalse
static constexpr int kValueFalse
Definition: fl_standard_message_codec.cc:17
G_DEFINE_TYPE
G_DEFINE_TYPE(FlStandardMessageCodec, fl_standard_message_codec, fl_message_codec_get_type()) static void write_uint8(GByteArray *buffer
fl_standard_message_codec.h
type
uint8_t type
Definition: fl_standard_message_codec_test.cc:1117
g_byte_array_append
g_byte_array_append(buffer, &type, sizeof(uint8_t))
length
size_t length
Definition: fl_standard_message_codec_test.cc:1115
fl_value_new_int32_list
G_MODULE_EXPORT FlValue * fl_value_new_int32_list(const int32_t *data, size_t data_length)
Definition: fl_value.cc:309
fl_value_get_map_value
G_MODULE_EXPORT FlValue * fl_value_get_map_value(FlValue *self, size_t index)
Definition: fl_value.cc:792
fl_value_new_float
G_MODULE_EXPORT FlValue * fl_value_new_float(double value)
Definition: fl_value.cc:269
fl_value_get_float_list
G_MODULE_EXPORT const double * fl_value_get_float_list(FlValue *self)
Definition: fl_value.cc:717
fl_value_get_list_value
G_MODULE_EXPORT FlValue * fl_value_get_list_value(FlValue *self, size_t index)
Definition: fl_value.cc:776
fl_value_new_int64_list
G_MODULE_EXPORT FlValue * fl_value_new_int64_list(const int64_t *data, size_t data_length)
Definition: fl_value.cc:319
fl_value_get_uint8_list
G_MODULE_EXPORT const uint8_t * fl_value_get_uint8_list(FlValue *self)
Definition: fl_value.cc:689
fl_value_get_int
G_MODULE_EXPORT int64_t fl_value_get_int(FlValue *self)
Definition: fl_value.cc:668
fl_value_get_type
G_MODULE_EXPORT FlValueType fl_value_get_type(FlValue *self)
Definition: fl_value.cc:466
fl_value_new_bool
G_MODULE_EXPORT FlValue * fl_value_new_bool(bool value)
Definition: fl_value.cc:255
fl_value_ref
G_MODULE_EXPORT FlValue * fl_value_ref(FlValue *self)
Definition: fl_value.cc:394
fl_value_get_float32_list
G_MODULE_EXPORT const float * fl_value_get_float32_list(FlValue *self)
Definition: fl_value.cc:710
fl_value_append
G_MODULE_EXPORT void fl_value_append(FlValue *self, FlValue *value)
Definition: fl_value.cc:592
fl_value_new_list
G_MODULE_EXPORT FlValue * fl_value_new_list()
Definition: fl_value.cc:349
fl_value_new_string_sized
G_MODULE_EXPORT FlValue * fl_value_new_string_sized(const gchar *value, size_t value_length)
Definition: fl_value.cc:283
fl_value_get_bool
G_MODULE_EXPORT bool fl_value_get_bool(FlValue *self)
Definition: fl_value.cc:661
fl_value_new_float_list
G_MODULE_EXPORT FlValue * fl_value_new_float_list(const double *data, size_t data_length)
Definition: fl_value.cc:339
fl_value_new_null
G_MODULE_EXPORT FlValue * fl_value_new_null()
Definition: fl_value.cc:251
fl_value_get_map_key
G_MODULE_EXPORT FlValue * fl_value_get_map_key(FlValue *self, size_t index)
Definition: fl_value.cc:784
fl_value_get_string
G_MODULE_EXPORT const gchar * fl_value_get_string(FlValue *self)
Definition: fl_value.cc:682
fl_value_get_int64_list
G_MODULE_EXPORT const int64_t * fl_value_get_int64_list(FlValue *self)
Definition: fl_value.cc:703
fl_value_new_int
G_MODULE_EXPORT FlValue * fl_value_new_int(int64_t value)
Definition: fl_value.cc:262
fl_value_new_map
G_MODULE_EXPORT FlValue * fl_value_new_map()
Definition: fl_value.cc:366
fl_value_new_float32_list
G_MODULE_EXPORT FlValue * fl_value_new_float32_list(const float *data, size_t data_length)
Definition: fl_value.cc:329
fl_value_new_uint8_list
G_MODULE_EXPORT FlValue * fl_value_new_uint8_list(const uint8_t *data, size_t data_length)
Definition: fl_value.cc:292
fl_value_get_float
G_MODULE_EXPORT double fl_value_get_float(FlValue *self)
Definition: fl_value.cc:675
fl_value_get_int32_list
G_MODULE_EXPORT const int32_t * fl_value_get_int32_list(FlValue *self)
Definition: fl_value.cc:696
fl_value_get_length
G_MODULE_EXPORT size_t fl_value_get_length(FlValue *self)
Definition: fl_value.cc:724
fl_value_set
G_MODULE_EXPORT void fl_value_set(FlValue *self, FlValue *key, FlValue *value)
Definition: fl_value.cc:609
FlValue
typedefG_BEGIN_DECLS struct _FlValue FlValue
Definition: fl_value.h:42
FL_VALUE_TYPE_STRING
@ FL_VALUE_TYPE_STRING
Definition: fl_value.h:68
FL_VALUE_TYPE_NULL
@ FL_VALUE_TYPE_NULL
Definition: fl_value.h:64
FL_VALUE_TYPE_INT
@ FL_VALUE_TYPE_INT
Definition: fl_value.h:66
FL_VALUE_TYPE_BOOL
@ FL_VALUE_TYPE_BOOL
Definition: fl_value.h:65
FL_VALUE_TYPE_FLOAT32_LIST
@ FL_VALUE_TYPE_FLOAT32_LIST
Definition: fl_value.h:75
FL_VALUE_TYPE_INT32_LIST
@ FL_VALUE_TYPE_INT32_LIST
Definition: fl_value.h:70
FL_VALUE_TYPE_UINT8_LIST
@ FL_VALUE_TYPE_UINT8_LIST
Definition: fl_value.h:69
FL_VALUE_TYPE_LIST
@ FL_VALUE_TYPE_LIST
Definition: fl_value.h:73
FL_VALUE_TYPE_MAP
@ FL_VALUE_TYPE_MAP
Definition: fl_value.h:74
FL_VALUE_TYPE_INT64_LIST
@ FL_VALUE_TYPE_INT64_LIST
Definition: fl_value.h:71
FL_VALUE_TYPE_FLOAT_LIST
@ FL_VALUE_TYPE_FLOAT_LIST
Definition: fl_value.h:72
FL_VALUE_TYPE_CUSTOM
@ FL_VALUE_TYPE_CUSTOM
Definition: fl_value.h:76
FL_VALUE_TYPE_FLOAT
@ FL_VALUE_TYPE_FLOAT
Definition: fl_value.h:67