Line | Count | Source |
1 | | /* |
2 | | * This file is auto-generated, do not modify! |
3 | | * |
4 | | * SPDX-License-Identifier: LGPL-2.1-or-later |
5 | | */ |
6 | | |
7 | | #include "config.h" |
8 | | |
9 | | #include <glib.h> |
10 | | |
11 | | #include "fu-uswid-struct.h" |
12 | | #include "fu-byte-array.h" |
13 | | #include "fu-mem-private.h" |
14 | | #include "fu-string.h" |
15 | | |
16 | | #ifdef G_LOG_DOMAIN |
17 | | #undef G_LOG_DOMAIN |
18 | | #endif |
19 | 0 | #define G_LOG_DOMAIN "FuStruct" |
20 | | |
21 | | |
22 | | /** |
23 | | * fu_uswid_payload_compression_to_string: |
24 | | * @val: value, e.g. %FU_USWID_PAYLOAD_COMPRESSION_ZLIB |
25 | | * |
26 | | * Converts an enumerated value to a string. |
27 | | * |
28 | | * Returns: identifier string |
29 | | **/ |
30 | | const gchar * |
31 | | fu_uswid_payload_compression_to_string(FuUswidPayloadCompression val) |
32 | 0 | { |
33 | 0 | if (val == FU_USWID_PAYLOAD_COMPRESSION_NONE) |
34 | 0 | return "none"; |
35 | 0 | if (val == FU_USWID_PAYLOAD_COMPRESSION_ZLIB) |
36 | 0 | return "zlib"; |
37 | 0 | if (val == FU_USWID_PAYLOAD_COMPRESSION_LZMA) |
38 | 0 | return "lzma"; |
39 | 0 | return NULL; |
40 | 0 | } |
41 | | /** |
42 | | * fu_uswid_payload_compression_from_string: |
43 | | * @val: (nullable): a string, e.g. `zlib` |
44 | | * |
45 | | * Converts a string to an enumerated value. |
46 | | * |
47 | | * Returns: enumerated value |
48 | | **/ |
49 | | FuUswidPayloadCompression |
50 | | fu_uswid_payload_compression_from_string(const gchar *val) |
51 | 0 | { |
52 | 0 | if (g_strcmp0(val, "none") == 0) |
53 | 0 | return FU_USWID_PAYLOAD_COMPRESSION_NONE; |
54 | 0 | if (g_strcmp0(val, "zlib") == 0) |
55 | 0 | return FU_USWID_PAYLOAD_COMPRESSION_ZLIB; |
56 | 0 | if (g_strcmp0(val, "lzma") == 0) |
57 | 0 | return FU_USWID_PAYLOAD_COMPRESSION_LZMA; |
58 | 0 | return FU_USWID_PAYLOAD_COMPRESSION_NONE; |
59 | 0 | } |
60 | | /** |
61 | | * fu_struct_uswid_ref: (skip): |
62 | | **/ |
63 | | FuStructUswid * |
64 | | fu_struct_uswid_ref(FuStructUswid *st) |
65 | 0 | { |
66 | 0 | g_return_val_if_fail(st != NULL, NULL); |
67 | 0 | st->refcount++; |
68 | 0 | return st; |
69 | 0 | } |
70 | | /** |
71 | | * fu_struct_uswid_unref: (skip): |
72 | | **/ |
73 | | void |
74 | | fu_struct_uswid_unref(FuStructUswid *st) |
75 | 245k | { |
76 | 245k | g_return_if_fail(st != NULL); |
77 | 245k | if (st->refcount == 0) { |
78 | 0 | g_critical("FuStructUswid refcount already zero"); |
79 | 0 | return; |
80 | 0 | } |
81 | 245k | if (--st->refcount > 0) |
82 | 0 | return; |
83 | 245k | if (st->buf != NULL) |
84 | 240k | g_byte_array_unref(st->buf); |
85 | 245k | g_free(st); |
86 | 245k | } |
87 | | /** |
88 | | * fu_struct_uswid_new_internal: (skip): |
89 | | **/ |
90 | | static FuStructUswid * |
91 | | fu_struct_uswid_new_internal(void) |
92 | 245k | { |
93 | 245k | FuStructUswid *st = g_new0(FuStructUswid, 1); |
94 | 245k | st->refcount = 1; |
95 | 245k | return st; |
96 | 245k | } |
97 | | |
98 | | /* getters */ |
99 | | /** |
100 | | * fu_struct_uswid_get_magic: (skip): |
101 | | **/ |
102 | | static const fwupd_guid_t * |
103 | | fu_struct_uswid_get_magic(const FuStructUswid *st) |
104 | 239k | { |
105 | 239k | g_return_val_if_fail(st != NULL, NULL); |
106 | 239k | return (const fwupd_guid_t *) (st->buf->data + 0); |
107 | 239k | } |
108 | | /** |
109 | | * fu_struct_uswid_get_hdrver: (skip): |
110 | | **/ |
111 | | guint8 |
112 | | fu_struct_uswid_get_hdrver(const FuStructUswid *st) |
113 | 119k | { |
114 | 119k | g_return_val_if_fail(st != NULL, 0x0); |
115 | 119k | return st->buf->data[16]; |
116 | 119k | } |
117 | | /** |
118 | | * fu_struct_uswid_get_hdrsz: (skip): |
119 | | **/ |
120 | | guint16 |
121 | | fu_struct_uswid_get_hdrsz(const FuStructUswid *st) |
122 | 119k | { |
123 | 119k | g_return_val_if_fail(st != NULL, 0x0); |
124 | 119k | return fu_memread_uint16(st->buf->data + 17, G_LITTLE_ENDIAN); |
125 | 119k | } |
126 | | /** |
127 | | * fu_struct_uswid_get_payloadsz: (skip): |
128 | | **/ |
129 | | guint32 |
130 | | fu_struct_uswid_get_payloadsz(const FuStructUswid *st) |
131 | 119k | { |
132 | 119k | g_return_val_if_fail(st != NULL, 0x0); |
133 | 119k | return fu_memread_uint32(st->buf->data + 19, G_LITTLE_ENDIAN); |
134 | 119k | } |
135 | | /** |
136 | | * fu_struct_uswid_get_flags: (skip): |
137 | | **/ |
138 | | guint8 |
139 | | fu_struct_uswid_get_flags(const FuStructUswid *st) |
140 | 65.4k | { |
141 | 65.4k | g_return_val_if_fail(st != NULL, 0x0); |
142 | 65.4k | return st->buf->data[23]; |
143 | 65.4k | } |
144 | | /** |
145 | | * fu_struct_uswid_get_compression: (skip): |
146 | | **/ |
147 | | guint8 |
148 | | fu_struct_uswid_get_compression(const FuStructUswid *st) |
149 | 14.3k | { |
150 | 14.3k | g_return_val_if_fail(st != NULL, 0x0); |
151 | 14.3k | return st->buf->data[24]; |
152 | 14.3k | } |
153 | | |
154 | | /* setters */ |
155 | | /** |
156 | | * fu_struct_uswid_set_magic: (skip): |
157 | | **/ |
158 | | static void |
159 | | fu_struct_uswid_set_magic(FuStructUswid *st, const fwupd_guid_t *value) |
160 | 4.77k | { |
161 | 4.77k | g_return_if_fail(st != NULL); |
162 | 4.77k | g_return_if_fail(value != NULL); |
163 | 4.77k | memcpy(st->buf->data + 0, value, sizeof(*value)); /* nocheck:blocked */ |
164 | 4.77k | } |
165 | | /** |
166 | | * fu_struct_uswid_set_hdrver: (skip): |
167 | | **/ |
168 | | void |
169 | | fu_struct_uswid_set_hdrver(FuStructUswid *st, guint8 value) |
170 | 4.77k | { |
171 | 4.77k | g_return_if_fail(st != NULL); |
172 | 4.77k | st->buf->data[16] = value; |
173 | 4.77k | } |
174 | | /** |
175 | | * fu_struct_uswid_set_hdrsz: (skip): |
176 | | **/ |
177 | | void |
178 | | fu_struct_uswid_set_hdrsz(FuStructUswid *st, guint16 value) |
179 | 8.46k | { |
180 | 8.46k | g_return_if_fail(st != NULL); |
181 | 8.46k | fu_memwrite_uint16(st->buf->data + 17, value, G_LITTLE_ENDIAN); |
182 | 8.46k | } |
183 | | /** |
184 | | * fu_struct_uswid_set_payloadsz: (skip): |
185 | | **/ |
186 | | void |
187 | | fu_struct_uswid_set_payloadsz(FuStructUswid *st, guint32 value) |
188 | 4.77k | { |
189 | 4.77k | g_return_if_fail(st != NULL); |
190 | 4.77k | fu_memwrite_uint32(st->buf->data + 19, value, G_LITTLE_ENDIAN); |
191 | 4.77k | } |
192 | | /** |
193 | | * fu_struct_uswid_set_flags: (skip): |
194 | | **/ |
195 | | void |
196 | | fu_struct_uswid_set_flags(FuStructUswid *st, guint8 value) |
197 | 1.13k | { |
198 | 1.13k | g_return_if_fail(st != NULL); |
199 | 1.13k | st->buf->data[23] = value; |
200 | 1.13k | } |
201 | | /** |
202 | | * fu_struct_uswid_set_compression: (skip): |
203 | | **/ |
204 | | void |
205 | | fu_struct_uswid_set_compression(FuStructUswid *st, guint8 value) |
206 | 1.08k | { |
207 | 1.08k | g_return_if_fail(st != NULL); |
208 | 1.08k | st->buf->data[24] = value; |
209 | 1.08k | } |
210 | | /** |
211 | | * fu_struct_uswid_new: (skip): |
212 | | **/ |
213 | | FuStructUswid * |
214 | | fu_struct_uswid_new(void) |
215 | 4.77k | { |
216 | 4.77k | FuStructUswid *st = fu_struct_uswid_new_internal(); |
217 | 4.77k | st->buf = g_byte_array_sized_new(25); |
218 | 4.77k | fu_byte_array_set_size(st->buf, 25, 0x0); |
219 | 4.77k | fu_struct_uswid_set_magic(st, (fwupd_guid_t *) "\x53\x42\x4f\x4d\xd6\xba\x2e\xac\xa3\xe6\x7a\x52\xaa\xee\x3b\xaf"); |
220 | 4.77k | fu_struct_uswid_set_hdrsz(st, 25); |
221 | 4.77k | return st; |
222 | 4.77k | } |
223 | | /** |
224 | | * fu_struct_uswid_to_string: (skip): |
225 | | **/ |
226 | | static gchar * |
227 | | fu_struct_uswid_to_string(const FuStructUswid *st) |
228 | 0 | { |
229 | 0 | g_autoptr(GString) str = g_string_new("FuStructUswid:\n"); |
230 | 0 | g_return_val_if_fail(st != NULL, NULL); |
231 | 0 | g_string_append_printf(str, " hdrver: 0x%x\n", |
232 | 0 | (guint) fu_struct_uswid_get_hdrver(st)); |
233 | 0 | g_string_append_printf(str, " hdrsz: 0x%x\n", |
234 | 0 | (guint) fu_struct_uswid_get_hdrsz(st)); |
235 | 0 | g_string_append_printf(str, " payloadsz: 0x%x\n", |
236 | 0 | (guint) fu_struct_uswid_get_payloadsz(st)); |
237 | 0 | g_string_append_printf(str, " flags: 0x%x\n", |
238 | 0 | (guint) fu_struct_uswid_get_flags(st)); |
239 | 0 | g_string_append_printf(str, " compression: 0x%x\n", |
240 | 0 | (guint) fu_struct_uswid_get_compression(st)); |
241 | 0 | if (str->len > 0) |
242 | 0 | g_string_set_size(str, str->len - 1); |
243 | 0 | return g_string_free(g_steal_pointer(&str), FALSE); |
244 | 0 | } |
245 | | static gboolean |
246 | | fu_struct_uswid_validate_internal(FuStructUswid *st, GError **error) |
247 | 239k | { |
248 | 239k | g_return_val_if_fail(st != NULL, FALSE); |
249 | 239k | if (memcmp(fu_struct_uswid_get_magic(st), "\x53\x42\x4f\x4d\xd6\xba\x2e\xac\xa3\xe6\x7a\x52\xaa\xee\x3b\xaf", 16) != 0) { |
250 | 0 | g_set_error_literal(error, |
251 | 0 | FWUPD_ERROR, |
252 | 0 | FWUPD_ERROR_INVALID_DATA, |
253 | 0 | "constant FuStructUswid.magic was not valid"); |
254 | 0 | return FALSE; |
255 | 0 | } |
256 | 239k | return TRUE; |
257 | 239k | } |
258 | | /** |
259 | | * fu_struct_uswid_validate_stream: (skip): |
260 | | **/ |
261 | | gboolean |
262 | | fu_struct_uswid_validate_stream(GInputStream *stream, gsize offset, GError **error) |
263 | 120k | { |
264 | 120k | g_autoptr(FuStructUswid) st = fu_struct_uswid_new_internal(); |
265 | 120k | g_return_val_if_fail(G_IS_INPUT_STREAM(stream), FALSE); |
266 | 120k | g_return_val_if_fail(error == NULL || *error == NULL, FALSE); |
267 | 120k | st->buf = fu_input_stream_read_byte_array(stream, offset, 25, NULL, error); |
268 | 120k | if (st->buf == NULL) { |
269 | 0 | g_prefix_error(error, "FuStructUswid failed read of 0x%x: ", (guint) 25); |
270 | 0 | return FALSE; |
271 | 0 | } |
272 | 120k | if (st->buf->len != 25) { |
273 | 718 | g_set_error(error, |
274 | 718 | FWUPD_ERROR, |
275 | 718 | FWUPD_ERROR_INVALID_DATA, |
276 | 718 | "FuStructUswid requested 0x%x and got 0x%x", |
277 | 718 | (guint) 25, |
278 | 718 | (guint) st->buf->len); |
279 | 718 | return FALSE; |
280 | 718 | } |
281 | 119k | return fu_struct_uswid_validate_internal(st, error); |
282 | 120k | } |
283 | | static gboolean |
284 | | fu_struct_uswid_parse_internal(FuStructUswid *st, GError **error) |
285 | 119k | { |
286 | 119k | if (g_getenv("FWUPD_VERBOSE") != NULL) { |
287 | 0 | g_autofree gchar *str = fu_struct_uswid_to_string(st); |
288 | 0 | g_debug("%s", str); |
289 | 0 | } |
290 | 119k | if (!fu_struct_uswid_validate_internal(st, error)) |
291 | 0 | return FALSE; |
292 | 119k | return TRUE; |
293 | 119k | } |
294 | | /** |
295 | | * fu_struct_uswid_parse_stream: (skip): |
296 | | **/ |
297 | | FuStructUswid * |
298 | | fu_struct_uswid_parse_stream(GInputStream *stream, gsize offset, GError **error) |
299 | 119k | { |
300 | 119k | g_autoptr(FuStructUswid) st = fu_struct_uswid_new_internal(); |
301 | 119k | st->buf = fu_input_stream_read_byte_array(stream, offset, 25, NULL, error); |
302 | 119k | if (st->buf == NULL) { |
303 | 0 | g_prefix_error(error, "FuStructUswid failed read of 0x%x: ", (guint) 25); |
304 | 0 | return NULL; |
305 | 0 | } |
306 | 119k | if (st->buf->len != 25) { |
307 | 0 | g_set_error(error, |
308 | 0 | FWUPD_ERROR, |
309 | 0 | FWUPD_ERROR_INVALID_DATA, |
310 | 0 | "FuStructUswid requested 0x%x and got 0x%x", |
311 | 0 | (guint) 25, |
312 | 0 | (guint) st->buf->len); |
313 | 0 | return NULL; |
314 | 0 | } |
315 | 119k | if (!fu_struct_uswid_parse_internal(st, error)) |
316 | 0 | return NULL; |
317 | 119k | return g_steal_pointer(&st); |
318 | 119k | } |