-
Notifications
You must be signed in to change notification settings - Fork 411
/
fu-mem.c
880 lines (827 loc) · 21.8 KB
/
fu-mem.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
/*
* Copyright (C) 2017 Richard Hughes <richard@hughsie.com>
*
* SPDX-License-Identifier: LGPL-2.1+
*/
#define G_LOG_DOMAIN "FuCommon"
#include "config.h"
#include "fwupd-error.h"
#include "fu-mem.h"
/**
* fu_memwrite_uint16:
* @buf: a writable buffer
* @val_native: a value in host byte-order
* @endian: an endian type, e.g. %G_LITTLE_ENDIAN
*
* Writes a value to a buffer using a specified endian.
*
* Since: 1.8.2
**/
void
fu_memwrite_uint16(guint8 *buf, guint16 val_native, FuEndianType endian)
{
guint16 val_hw;
switch (endian) {
case G_BIG_ENDIAN:
val_hw = GUINT16_TO_BE(val_native);
break;
case G_LITTLE_ENDIAN:
val_hw = GUINT16_TO_LE(val_native);
break;
default:
g_assert_not_reached();
}
memcpy(buf, &val_hw, sizeof(val_hw));
}
/**
* fu_memwrite_uint24:
* @buf: a writable buffer
* @val_native: a value in host byte-order
* @endian: an endian type, e.g. %G_LITTLE_ENDIAN
*
* Writes a value to a buffer using a specified endian.
*
* Since: 1.8.2
**/
void
fu_memwrite_uint24(guint8 *buf, guint32 val_native, FuEndianType endian)
{
guint32 val_hw;
switch (endian) {
case G_BIG_ENDIAN:
val_hw = GUINT32_TO_BE(val_native);
memcpy(buf, ((const guint8 *)&val_hw) + 0x1, 0x3);
break;
case G_LITTLE_ENDIAN:
val_hw = GUINT32_TO_LE(val_native);
memcpy(buf, &val_hw, 0x3);
break;
default:
g_assert_not_reached();
}
}
/**
* fu_memwrite_uint32:
* @buf: a writable buffer
* @val_native: a value in host byte-order
* @endian: an endian type, e.g. %G_LITTLE_ENDIAN
*
* Writes a value to a buffer using a specified endian.
*
* Since: 1.8.2
**/
void
fu_memwrite_uint32(guint8 *buf, guint32 val_native, FuEndianType endian)
{
guint32 val_hw;
switch (endian) {
case G_BIG_ENDIAN:
val_hw = GUINT32_TO_BE(val_native);
break;
case G_LITTLE_ENDIAN:
val_hw = GUINT32_TO_LE(val_native);
break;
default:
g_assert_not_reached();
}
memcpy(buf, &val_hw, sizeof(val_hw));
}
/**
* fu_memwrite_uint64:
* @buf: a writable buffer
* @val_native: a value in host byte-order
* @endian: an endian type, e.g. %G_LITTLE_ENDIAN
*
* Writes a value to a buffer using a specified endian.
*
* Since: 1.8.2
**/
void
fu_memwrite_uint64(guint8 *buf, guint64 val_native, FuEndianType endian)
{
guint64 val_hw;
switch (endian) {
case G_BIG_ENDIAN:
val_hw = GUINT64_TO_BE(val_native);
break;
case G_LITTLE_ENDIAN:
val_hw = GUINT64_TO_LE(val_native);
break;
default:
g_assert_not_reached();
}
memcpy(buf, &val_hw, sizeof(val_hw));
}
/**
* fu_memread_uint16:
* @buf: a readable buffer
* @endian: an endian type, e.g. %G_LITTLE_ENDIAN
*
* Read a value from a buffer using a specified endian.
*
* Returns: a value in host byte-order
*
* Since: 1.8.2
**/
guint16
fu_memread_uint16(const guint8 *buf, FuEndianType endian)
{
guint16 val_hw, val_native;
memcpy(&val_hw, buf, sizeof(val_hw));
switch (endian) {
case G_BIG_ENDIAN:
val_native = GUINT16_FROM_BE(val_hw);
break;
case G_LITTLE_ENDIAN:
val_native = GUINT16_FROM_LE(val_hw);
break;
default:
g_assert_not_reached();
}
return val_native;
}
/**
* fu_memread_uint24:
* @buf: a readable buffer
* @endian: an endian type, e.g. %G_LITTLE_ENDIAN
*
* Read a value from a buffer using a specified endian.
*
* Returns: a value in host byte-order
*
* Since: 1.8.2
**/
guint32
fu_memread_uint24(const guint8 *buf, FuEndianType endian)
{
guint32 val_hw = 0;
guint32 val_native;
switch (endian) {
case G_BIG_ENDIAN:
memcpy(((guint8 *)&val_hw) + 0x1, buf, 0x3);
val_native = GUINT32_FROM_BE(val_hw);
break;
case G_LITTLE_ENDIAN:
memcpy(&val_hw, buf, 0x3);
val_native = GUINT32_FROM_LE(val_hw);
break;
default:
g_assert_not_reached();
}
return val_native;
}
/**
* fu_memread_uint32:
* @buf: a readable buffer
* @endian: an endian type, e.g. %G_LITTLE_ENDIAN
*
* Read a value from a buffer using a specified endian.
*
* Returns: a value in host byte-order
*
* Since: 1.8.2
**/
guint32
fu_memread_uint32(const guint8 *buf, FuEndianType endian)
{
guint32 val_hw, val_native;
memcpy(&val_hw, buf, sizeof(val_hw));
switch (endian) {
case G_BIG_ENDIAN:
val_native = GUINT32_FROM_BE(val_hw);
break;
case G_LITTLE_ENDIAN:
val_native = GUINT32_FROM_LE(val_hw);
break;
default:
g_assert_not_reached();
}
return val_native;
}
/**
* fu_memread_uint64:
* @buf: a readable buffer
* @endian: an endian type, e.g. %G_LITTLE_ENDIAN
*
* Read a value from a buffer using a specified endian.
*
* Returns: a value in host byte-order
*
* Since: 1.8.2
**/
guint64
fu_memread_uint64(const guint8 *buf, FuEndianType endian)
{
guint64 val_hw, val_native;
memcpy(&val_hw, buf, sizeof(val_hw));
switch (endian) {
case G_BIG_ENDIAN:
val_native = GUINT64_FROM_BE(val_hw);
break;
case G_LITTLE_ENDIAN:
val_native = GUINT64_FROM_LE(val_hw);
break;
default:
g_assert_not_reached();
}
return val_native;
}
/**
* fu_memcmp_safe:
* @buf1: a buffer
* @bufsz1: sizeof @buf1
* @buf2: another buffer
* @bufsz2: sizeof @buf2
* @error: (nullable): optional return location for an error
*
* Compares the buffers for equality.
*
* Returns: %TRUE if @buf1 and @buf2 are identical
*
* Since: 1.8.2
**/
gboolean
fu_memcmp_safe(const guint8 *buf1, gsize bufsz1, const guint8 *buf2, gsize bufsz2, GError **error)
{
g_return_val_if_fail(buf1 != NULL, FALSE);
g_return_val_if_fail(buf2 != NULL, FALSE);
g_return_val_if_fail(error == NULL || *error == NULL, FALSE);
/* not the same length */
if (bufsz1 != bufsz2) {
g_set_error(error,
G_IO_ERROR,
G_IO_ERROR_INVALID_DATA,
"got %" G_GSIZE_FORMAT " bytes, expected "
"%" G_GSIZE_FORMAT,
bufsz1,
bufsz2);
return FALSE;
}
/* check matches */
for (guint i = 0x0; i < bufsz1; i++) {
if (buf1[i] != buf2[i]) {
g_set_error(error,
G_IO_ERROR,
G_IO_ERROR_INVALID_DATA,
"got 0x%02x, expected 0x%02x @ 0x%04x",
buf1[i],
buf2[i],
i);
return FALSE;
}
}
/* success */
return TRUE;
}
/**
* fu_memcpy_safe:
* @dst: destination buffer
* @dst_sz: maximum size of @dst, typically `sizeof(dst)`
* @dst_offset: offset in bytes into @dst to copy to
* @src: source buffer
* @src_sz: maximum size of @dst, typically `sizeof(src)`
* @src_offset: offset in bytes into @src to copy from
* @n: number of bytes to copy from @src+@offset from
* @error: (nullable): optional return location for an error
*
* Copies some memory using memcpy in a safe way. Providing the buffer sizes
* of both the destination and the source allows us to check for buffer overflow.
*
* Providing the buffer offsets also allows us to check reading past the end of
* the source buffer. For this reason the caller should NEVER add an offset to
* @src or @dst.
*
* You don't need to use this function in "obviously correct" cases, nor should
* you use it when performance is a concern. Only us it when you're not sure if
* malicious data from a device or firmware could cause memory corruption.
*
* Returns: %TRUE if the bytes were copied, %FALSE otherwise
*
* Since: 1.8.2
**/
gboolean
fu_memcpy_safe(guint8 *dst,
gsize dst_sz,
gsize dst_offset,
const guint8 *src,
gsize src_sz,
gsize src_offset,
gsize n,
GError **error)
{
g_return_val_if_fail(dst != NULL, FALSE);
g_return_val_if_fail(src != NULL, FALSE);
g_return_val_if_fail(error == NULL || *error == NULL, FALSE);
if (n == 0)
return TRUE;
if (n > src_sz) {
g_set_error(error,
FWUPD_ERROR,
FWUPD_ERROR_READ,
"attempted to read 0x%02x bytes from buffer of 0x%02x",
(guint)n,
(guint)src_sz);
return FALSE;
}
if (src_offset > src_sz || n + src_offset > src_sz) {
g_set_error(error,
FWUPD_ERROR,
FWUPD_ERROR_READ,
"attempted to read 0x%02x bytes at offset 0x%02x from buffer of 0x%02x",
(guint)n,
(guint)src_offset,
(guint)src_sz);
return FALSE;
}
if (n > dst_sz) {
g_set_error(error,
FWUPD_ERROR,
FWUPD_ERROR_WRITE,
"attempted to write 0x%02x bytes to buffer of 0x%02x",
(guint)n,
(guint)dst_sz);
return FALSE;
}
if (dst_offset > dst_sz || n + dst_offset > dst_sz) {
g_set_error(error,
FWUPD_ERROR,
FWUPD_ERROR_WRITE,
"attempted to write 0x%02x bytes at offset 0x%02x to buffer of 0x%02x",
(guint)n,
(guint)dst_offset,
(guint)dst_sz);
return FALSE;
}
/* phew! */
memcpy(dst + dst_offset, src + src_offset, n);
return TRUE;
}
/**
* fu_memmem_safe:
* @haystack: destination buffer
* @haystack_sz: maximum size of @haystack, typically `sizeof(haystack)`
* @needle: source buffer
* @needle_sz: maximum size of @haystack, typically `sizeof(needle)`
* @offset: (out) (nullable): offset in bytes @needle has been found in @haystack
* @error: (nullable): optional return location for an error
*
* Finds a block of memory in another block of memory in a safe way.
*
* Returns: %TRUE if the needle was found in the haystack, %FALSE otherwise
*
* Since: 1.8.2
**/
gboolean
fu_memmem_safe(const guint8 *haystack,
gsize haystack_sz,
const guint8 *needle,
gsize needle_sz,
gsize *offset,
GError **error)
{
#ifdef HAVE_MEMMEM
const guint8 *tmp;
#endif
g_return_val_if_fail(haystack != NULL, FALSE);
g_return_val_if_fail(needle != NULL, FALSE);
g_return_val_if_fail(error == NULL || *error == NULL, FALSE);
/* nothing to find */
if (needle_sz == 0) {
if (offset != NULL)
*offset = 0;
return TRUE;
}
/* impossible */
if (needle_sz > haystack_sz) {
g_set_error(error,
FWUPD_ERROR,
FWUPD_ERROR_NOT_FOUND,
"needle of 0x%02x bytes is larger than haystack of 0x%02x bytes",
(guint)needle_sz,
(guint)haystack_sz);
return FALSE;
}
#ifdef HAVE_MEMMEM
/* trust glibc to do a binary or linear search as appropriate */
tmp = memmem(haystack, haystack_sz, needle, needle_sz);
if (tmp != NULL) {
if (offset != NULL)
*offset = tmp - haystack;
return TRUE;
}
#else
for (gsize i = 0; i < haystack_sz - needle_sz; i++) {
if (memcmp(haystack + i, needle, needle_sz) == 0) {
if (offset != NULL)
*offset = i;
return TRUE;
}
}
#endif
/* not found */
g_set_error(error,
FWUPD_ERROR,
FWUPD_ERROR_NOT_FOUND,
"needle of 0x%02x bytes was not found in haystack of 0x%02x bytes",
(guint)needle_sz,
(guint)haystack_sz);
return FALSE;
}
/**
* fu_memdup_safe:
* @src: source buffer
* @n: number of bytes to copy from @src
* @error: (nullable): optional return location for an error
*
* Duplicates some memory using memdup in a safe way.
*
* You don't need to use this function in "obviously correct" cases, nor should
* you use it when performance is a concern. Only us it when you're not sure if
* malicious data from a device or firmware could cause memory corruption.
*
* NOTE: This function intentionally limits allocation size to 1GB.
*
* Returns: (transfer full): block of allocated memory, or %NULL for an error.
*
* Since: 1.8.2
**/
guint8 *
fu_memdup_safe(const guint8 *src, gsize n, GError **error)
{
/* sanity check */
if (n > 0x40000000) {
g_set_error(error,
G_IO_ERROR,
G_IO_ERROR_NOT_SUPPORTED,
"cannot allocate %uGB of memory",
(guint)(n / 0x40000000));
return NULL;
}
#if GLIB_CHECK_VERSION(2, 67, 3)
/* linear block of memory */
return g_memdup2(src, n);
#else
return g_memdup(src, (guint)n);
#endif
}
/**
* fu_memread_uint8_safe:
* @buf: source buffer
* @bufsz: maximum size of @buf, typically `sizeof(buf)`
* @offset: offset in bytes into @buf to copy from
* @value: (out) (nullable): the parsed value
* @error: (nullable): optional return location for an error
*
* Read a value from a buffer in a safe way.
*
* You don't need to use this function in "obviously correct" cases, nor should
* you use it when performance is a concern. Only us it when you're not sure if
* malicious data from a device or firmware could cause memory corruption.
*
* Returns: %TRUE if @value was set, %FALSE otherwise
*
* Since: 1.8.2
**/
gboolean
fu_memread_uint8_safe(const guint8 *buf, gsize bufsz, gsize offset, guint8 *value, GError **error)
{
guint8 tmp;
g_return_val_if_fail(buf != NULL, FALSE);
g_return_val_if_fail(error == NULL || *error == NULL, FALSE);
if (!fu_memcpy_safe(&tmp,
sizeof(tmp),
0x0, /* dst */
buf,
bufsz,
offset, /* src */
sizeof(tmp),
error))
return FALSE;
if (value != NULL)
*value = tmp;
return TRUE;
}
/**
* fu_memread_uint16_safe:
* @buf: source buffer
* @bufsz: maximum size of @buf, typically `sizeof(buf)`
* @offset: offset in bytes into @buf to copy from
* @value: (out) (nullable): the parsed value
* @endian: an endian type, e.g. %G_LITTLE_ENDIAN
* @error: (nullable): optional return location for an error
*
* Read a value from a buffer using a specified endian in a safe way.
*
* You don't need to use this function in "obviously correct" cases, nor should
* you use it when performance is a concern. Only us it when you're not sure if
* malicious data from a device or firmware could cause memory corruption.
*
* Returns: %TRUE if @value was set, %FALSE otherwise
*
* Since: 1.8.2
**/
gboolean
fu_memread_uint16_safe(const guint8 *buf,
gsize bufsz,
gsize offset,
guint16 *value,
FuEndianType endian,
GError **error)
{
guint8 dst[2] = {0x0};
g_return_val_if_fail(buf != NULL, FALSE);
g_return_val_if_fail(error == NULL || *error == NULL, FALSE);
if (!fu_memcpy_safe(dst,
sizeof(dst),
0x0, /* dst */
buf,
bufsz,
offset, /* src */
sizeof(dst),
error))
return FALSE;
if (value != NULL)
*value = fu_memread_uint16(dst, endian);
return TRUE;
}
/**
* fu_memread_uint24_safe:
* @buf: source buffer
* @bufsz: maximum size of @buf, typically `sizeof(buf)`
* @offset: offset in bytes into @buf to copy from
* @value: (out) (nullable): the parsed value
* @endian: an endian type, e.g. %G_LITTLE_ENDIAN
* @error: (nullable): optional return location for an error
*
* Read a value from a buffer using a specified endian in a safe way.
*
* You don't need to use this function in "obviously correct" cases, nor should
* you use it when performance is a concern. Only us it when you're not sure if
* malicious data from a device or firmware could cause memory corruption.
*
* Returns: %TRUE if @value was set, %FALSE otherwise
*
* Since: 1.8.3
**/
gboolean
fu_memread_uint24_safe(const guint8 *buf,
gsize bufsz,
gsize offset,
guint32 *value,
FuEndianType endian,
GError **error)
{
guint8 dst[3] = {0x0};
g_return_val_if_fail(buf != NULL, FALSE);
g_return_val_if_fail(error == NULL || *error == NULL, FALSE);
if (!fu_memcpy_safe(dst,
sizeof(dst),
0x0, /* dst */
buf,
bufsz,
offset, /* src */
sizeof(dst),
error))
return FALSE;
if (value != NULL)
*value = fu_memread_uint24(dst, endian);
return TRUE;
}
/**
* fu_memread_uint32_safe:
* @buf: source buffer
* @bufsz: maximum size of @buf, typically `sizeof(buf)`
* @offset: offset in bytes into @buf to copy from
* @value: (out) (nullable): the parsed value
* @endian: an endian type, e.g. %G_LITTLE_ENDIAN
* @error: (nullable): optional return location for an error
*
* Read a value from a buffer using a specified endian in a safe way.
*
* You don't need to use this function in "obviously correct" cases, nor should
* you use it when performance is a concern. Only us it when you're not sure if
* malicious data from a device or firmware could cause memory corruption.
*
* Returns: %TRUE if @value was set, %FALSE otherwise
*
* Since: 1.8.2
**/
gboolean
fu_memread_uint32_safe(const guint8 *buf,
gsize bufsz,
gsize offset,
guint32 *value,
FuEndianType endian,
GError **error)
{
guint8 dst[4] = {0x0};
g_return_val_if_fail(buf != NULL, FALSE);
g_return_val_if_fail(error == NULL || *error == NULL, FALSE);
if (!fu_memcpy_safe(dst,
sizeof(dst),
0x0, /* dst */
buf,
bufsz,
offset, /* src */
sizeof(dst),
error))
return FALSE;
if (value != NULL)
*value = fu_memread_uint32(dst, endian);
return TRUE;
}
/**
* fu_memread_uint64_safe:
* @buf: source buffer
* @bufsz: maximum size of @buf, typically `sizeof(buf)`
* @offset: offset in bytes into @buf to copy from
* @value: (out) (nullable): the parsed value
* @endian: an endian type, e.g. %G_LITTLE_ENDIAN
* @error: (nullable): optional return location for an error
*
* Read a value from a buffer using a specified endian in a safe way.
*
* You don't need to use this function in "obviously correct" cases, nor should
* you use it when performance is a concern. Only us it when you're not sure if
* malicious data from a device or firmware could cause memory corruption.
*
* Returns: %TRUE if @value was set, %FALSE otherwise
*
* Since: 1.8.2
**/
gboolean
fu_memread_uint64_safe(const guint8 *buf,
gsize bufsz,
gsize offset,
guint64 *value,
FuEndianType endian,
GError **error)
{
guint8 dst[8] = {0x0};
g_return_val_if_fail(buf != NULL, FALSE);
g_return_val_if_fail(error == NULL || *error == NULL, FALSE);
if (!fu_memcpy_safe(dst,
sizeof(dst),
0x0, /* dst */
buf,
bufsz,
offset, /* src */
sizeof(dst),
error))
return FALSE;
if (value != NULL)
*value = fu_memread_uint64(dst, endian);
return TRUE;
}
/**
* fu_memwrite_uint8_safe:
* @buf: source buffer
* @bufsz: maximum size of @buf, typically `sizeof(buf)`
* @offset: offset in bytes into @buf to write to
* @value: the value to write
* @error: (nullable): optional return location for an error
*
* Write a value to a buffer in a safe way.
*
* You don't need to use this function in "obviously correct" cases, nor should
* you use it when performance is a concern. Only us it when you're not sure if
* malicious data from a device or firmware could cause memory corruption.
*
* Returns: %TRUE if @value was written, %FALSE otherwise
*
* Since: 1.8.2
**/
gboolean
fu_memwrite_uint8_safe(guint8 *buf, gsize bufsz, gsize offset, guint8 value, GError **error)
{
g_return_val_if_fail(buf != NULL, FALSE);
g_return_val_if_fail(error == NULL || *error == NULL, FALSE);
return fu_memcpy_safe(buf,
bufsz,
offset, /* dst */
&value,
sizeof(value),
0x0, /* src */
sizeof(value),
error);
}
/**
* fu_memwrite_uint16_safe:
* @buf: source buffer
* @bufsz: maximum size of @buf, typically `sizeof(buf)`
* @offset: offset in bytes into @buf to write to
* @value: the value to write
* @endian: an endian type, e.g. %G_LITTLE_ENDIAN
* @error: (nullable): optional return location for an error
*
* Write a value to a buffer using a specified endian in a safe way.
*
* You don't need to use this function in "obviously correct" cases, nor should
* you use it when performance is a concern. Only us it when you're not sure if
* malicious data from a device or firmware could cause memory corruption.
*
* Returns: %TRUE if @value was written, %FALSE otherwise
*
* Since: 1.8.2
**/
gboolean
fu_memwrite_uint16_safe(guint8 *buf,
gsize bufsz,
gsize offset,
guint16 value,
FuEndianType endian,
GError **error)
{
guint8 tmp[2] = {0x0};
g_return_val_if_fail(buf != NULL, FALSE);
g_return_val_if_fail(error == NULL || *error == NULL, FALSE);
fu_memwrite_uint16(tmp, value, endian);
return fu_memcpy_safe(buf,
bufsz,
offset, /* dst */
tmp,
sizeof(tmp),
0x0, /* src */
sizeof(tmp),
error);
}
/**
* fu_memwrite_uint32_safe:
* @buf: source buffer
* @bufsz: maximum size of @buf, typically `sizeof(buf)`
* @offset: offset in bytes into @buf to write to
* @value: the value to write
* @endian: an endian type, e.g. %G_LITTLE_ENDIAN
* @error: (nullable): optional return location for an error
*
* Write a value to a buffer using a specified endian in a safe way.
*
* You don't need to use this function in "obviously correct" cases, nor should
* you use it when performance is a concern. Only us it when you're not sure if
* malicious data from a device or firmware could cause memory corruption.
*
* Returns: %TRUE if @value was written, %FALSE otherwise
*
* Since: 1.8.2
**/
gboolean
fu_memwrite_uint32_safe(guint8 *buf,
gsize bufsz,
gsize offset,
guint32 value,
FuEndianType endian,
GError **error)
{
guint8 tmp[4] = {0x0};
g_return_val_if_fail(buf != NULL, FALSE);
g_return_val_if_fail(error == NULL || *error == NULL, FALSE);
fu_memwrite_uint32(tmp, value, endian);
return fu_memcpy_safe(buf,
bufsz,
offset, /* dst */
tmp,
sizeof(tmp),
0x0, /* src */
sizeof(tmp),
error);
}
/**
* fu_memwrite_uint64_safe:
* @buf: source buffer
* @bufsz: maximum size of @buf, typically `sizeof(buf)`
* @offset: offset in bytes into @buf to write to
* @value: the value to write
* @endian: an endian type, e.g. %G_LITTLE_ENDIAN
* @error: (nullable): optional return location for an error
*
* Write a value to a buffer using a specified endian in a safe way.
*
* You don't need to use this function in "obviously correct" cases, nor should
* you use it when performance is a concern. Only us it when you're not sure if
* malicious data from a device or firmware could cause memory corruption.
*
* Returns: %TRUE if @value was written, %FALSE otherwise
*
* Since: 1.8.2
**/
gboolean
fu_memwrite_uint64_safe(guint8 *buf,
gsize bufsz,
gsize offset,
guint64 value,
FuEndianType endian,
GError **error)
{
guint8 tmp[8] = {0x0};
g_return_val_if_fail(buf != NULL, FALSE);
g_return_val_if_fail(error == NULL || *error == NULL, FALSE);
fu_memwrite_uint64(tmp, value, endian);
return fu_memcpy_safe(buf,
bufsz,
offset, /* dst */
tmp,
sizeof(tmp),
0x0, /* src */
sizeof(tmp),
error);
}