1 /* SPDX-License-Identifier: BSD-3-Clause */
4 * This is a copy from upstream:
5 * https://chromium.googlesource.com/chromiumos/platform/vpd/+/master/lib/vpd_decode.c
7 #include "vpd_decode.h"
9 static int vpd_decode_len(
10 const u32 max_len
, const u8
*in
, u32
*length
, u32
*decoded_len
)
15 if (!length
|| !decoded_len
)
16 return VPD_DECODE_FAIL
;
21 return VPD_DECODE_FAIL
;
25 *length
|= in
[i
] & 0x7f;
33 static int vpd_decode_entry(
34 const u32 max_len
, const u8
*input_buf
, u32
*consumed
,
35 const u8
**entry
, u32
*entry_len
)
39 if (vpd_decode_len(max_len
- *consumed
, &input_buf
[*consumed
],
40 entry_len
, &decoded_len
) != VPD_DECODE_OK
)
41 return VPD_DECODE_FAIL
;
42 if (max_len
- *consumed
< decoded_len
)
43 return VPD_DECODE_FAIL
;
45 *consumed
+= decoded_len
;
46 *entry
= input_buf
+ *consumed
;
48 /* entry_len is untrusted data and must be checked again. */
49 if (max_len
- *consumed
< *entry_len
)
50 return VPD_DECODE_FAIL
;
52 *consumed
+= *entry_len
;
56 int vpd_decode_string(
57 const u32 max_len
, const u8
*input_buf
, u32
*consumed
,
58 vpd_decode_callback callback
, void *callback_arg
)
67 if (*consumed
>= max_len
)
68 return VPD_DECODE_FAIL
;
70 type
= input_buf
[*consumed
];
77 if (vpd_decode_entry(max_len
, input_buf
, consumed
, &key
,
78 &key_len
) != VPD_DECODE_OK
)
79 return VPD_DECODE_FAIL
;
81 if (vpd_decode_entry(max_len
, input_buf
, consumed
, &value
,
82 &value_len
) != VPD_DECODE_OK
)
83 return VPD_DECODE_FAIL
;
85 if (type
== VPD_TYPE_STRING
)
86 return callback(key
, key_len
, value
, value_len
,
91 return VPD_DECODE_FAIL
;