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 | 263k | { |
76 | 263k | g_return_if_fail(st != NULL); |
77 | 263k | if (st->refcount == 0) { |
78 | 0 | g_critical("FuStructUswid refcount already zero"); |
79 | 0 | return; |
80 | 0 | } |
81 | 263k | if (--st->refcount > 0) |
82 | 0 | return; |
83 | 263k | if (st->buf != NULL) |
84 | 255k | g_byte_array_unref(st->buf); |
85 | 263k | g_free(st); |
86 | 263k | } |
87 | | /** |
88 | | * fu_struct_uswid_new_internal: (skip): |
89 | | **/ |
90 | | static FuStructUswid * |
91 | | fu_struct_uswid_new_internal(void) |
92 | 263k | { |
93 | 263k | FuStructUswid *st = g_new0(FuStructUswid, 1); |
94 | 263k | st->refcount = 1; |
95 | 263k | return st; |
96 | 263k | } |
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 | 254k | { |
105 | 254k | g_return_val_if_fail(st != NULL, NULL); |
106 | 254k | return (const fwupd_guid_t *) (st->buf->data + 0); |
107 | 254k | } |
108 | | /** |
109 | | * fu_struct_uswid_get_hdrver: (skip): |
110 | | **/ |
111 | | guint8 |
112 | | fu_struct_uswid_get_hdrver(const FuStructUswid *st) |
113 | 127k | { |
114 | 127k | g_return_val_if_fail(st != NULL, 0x0); |
115 | 127k | return st->buf->data[16]; |
116 | 127k | } |
117 | | /** |
118 | | * fu_struct_uswid_get_hdrsz: (skip): |
119 | | **/ |
120 | | guint16 |
121 | | fu_struct_uswid_get_hdrsz(const FuStructUswid *st) |
122 | 125k | { |
123 | 125k | g_return_val_if_fail(st != NULL, 0x0); |
124 | 125k | return fu_memread_uint16(st->buf->data + 17, G_LITTLE_ENDIAN); |
125 | 125k | } |
126 | | /** |
127 | | * fu_struct_uswid_get_payloadsz: (skip): |
128 | | **/ |
129 | | guint32 |
130 | | fu_struct_uswid_get_payloadsz(const FuStructUswid *st) |
131 | 125k | { |
132 | 125k | g_return_val_if_fail(st != NULL, 0x0); |
133 | 125k | return fu_memread_uint32(st->buf->data + 19, G_LITTLE_ENDIAN); |
134 | 125k | } |
135 | | /** |
136 | | * fu_struct_uswid_get_flags: (skip): |
137 | | **/ |
138 | | guint8 |
139 | | fu_struct_uswid_get_flags(const FuStructUswid *st) |
140 | 70.0k | { |
141 | 70.0k | g_return_val_if_fail(st != NULL, 0x0); |
142 | 70.0k | return st->buf->data[23]; |
143 | 70.0k | } |
144 | | /** |
145 | | * fu_struct_uswid_get_compression: (skip): |
146 | | **/ |
147 | | guint8 |
148 | | fu_struct_uswid_get_compression(const FuStructUswid *st) |
149 | 16.8k | { |
150 | 16.8k | g_return_val_if_fail(st != NULL, 0x0); |
151 | 16.8k | return st->buf->data[24]; |
152 | 16.8k | } |
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 | 8.48k | { |
161 | 8.48k | g_return_if_fail(st != NULL); |
162 | 8.48k | g_return_if_fail(value != NULL); |
163 | 8.48k | memcpy(st->buf->data + 0, value, sizeof(*value)); /* nocheck:blocked */ |
164 | 8.48k | } |
165 | | /** |
166 | | * fu_struct_uswid_set_hdrver: (skip): |
167 | | **/ |
168 | | void |
169 | | fu_struct_uswid_set_hdrver(FuStructUswid *st, guint8 value) |
170 | 8.47k | { |
171 | 8.47k | g_return_if_fail(st != NULL); |
172 | 8.47k | st->buf->data[16] = value; |
173 | 8.47k | } |
174 | | /** |
175 | | * fu_struct_uswid_set_hdrsz: (skip): |
176 | | **/ |
177 | | void |
178 | | fu_struct_uswid_set_hdrsz(FuStructUswid *st, guint16 value) |
179 | 13.3k | { |
180 | 13.3k | g_return_if_fail(st != NULL); |
181 | 13.3k | fu_memwrite_uint16(st->buf->data + 17, value, G_LITTLE_ENDIAN); |
182 | 13.3k | } |
183 | | /** |
184 | | * fu_struct_uswid_set_payloadsz: (skip): |
185 | | **/ |
186 | | void |
187 | | fu_struct_uswid_set_payloadsz(FuStructUswid *st, guint32 value) |
188 | 8.47k | { |
189 | 8.47k | g_return_if_fail(st != NULL); |
190 | 8.47k | fu_memwrite_uint32(st->buf->data + 19, value, G_LITTLE_ENDIAN); |
191 | 8.47k | } |
192 | | /** |
193 | | * fu_struct_uswid_set_flags: (skip): |
194 | | **/ |
195 | | void |
196 | | fu_struct_uswid_set_flags(FuStructUswid *st, guint8 value) |
197 | 3.63k | { |
198 | 3.63k | g_return_if_fail(st != NULL); |
199 | 3.63k | st->buf->data[23] = value; |
200 | 3.63k | } |
201 | | /** |
202 | | * fu_struct_uswid_set_compression: (skip): |
203 | | **/ |
204 | | void |
205 | | fu_struct_uswid_set_compression(FuStructUswid *st, guint8 value) |
206 | 3.58k | { |
207 | 3.58k | g_return_if_fail(st != NULL); |
208 | 3.58k | st->buf->data[24] = value; |
209 | 3.58k | } |
210 | | /** |
211 | | * fu_struct_uswid_new: (skip): |
212 | | **/ |
213 | | FuStructUswid * |
214 | | fu_struct_uswid_new(void) |
215 | 8.48k | { |
216 | 8.48k | FuStructUswid *st = fu_struct_uswid_new_internal(); |
217 | 8.48k | st->buf = g_byte_array_sized_new(25); |
218 | 8.48k | fu_byte_array_set_size(st->buf, 25, 0x0); |
219 | 8.48k | 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 | 8.48k | fu_struct_uswid_set_hdrsz(st, 25); |
221 | 8.48k | return st; |
222 | 8.48k | } |
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 | { |
232 | 0 | g_autofree gchar *tmp = fwupd_guid_to_string(fu_struct_uswid_get_magic(st), FWUPD_GUID_FLAG_MIXED_ENDIAN); |
233 | 0 | g_string_append_printf(str, " magic: %s\n", tmp); |
234 | 0 | } |
235 | 0 | g_string_append_printf(str, " hdrver: 0x%x\n", |
236 | 0 | (guint) fu_struct_uswid_get_hdrver(st)); |
237 | 0 | g_string_append_printf(str, " hdrsz: 0x%x\n", |
238 | 0 | (guint) fu_struct_uswid_get_hdrsz(st)); |
239 | 0 | g_string_append_printf(str, " payloadsz: 0x%x\n", |
240 | 0 | (guint) fu_struct_uswid_get_payloadsz(st)); |
241 | 0 | g_string_append_printf(str, " flags: 0x%x\n", |
242 | 0 | (guint) fu_struct_uswid_get_flags(st)); |
243 | 0 | g_string_append_printf(str, " compression: 0x%x\n", |
244 | 0 | (guint) fu_struct_uswid_get_compression(st)); |
245 | 0 | if (str->len > 0) |
246 | 0 | g_string_set_size(str, str->len - 1); |
247 | 0 | return g_string_free(g_steal_pointer(&str), FALSE); |
248 | 0 | } |
249 | | static gboolean |
250 | | fu_struct_uswid_validate_internal(FuStructUswid *st, GError **error) |
251 | 254k | { |
252 | 254k | g_return_val_if_fail(st != NULL, FALSE); |
253 | 254k | 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) { |
254 | 0 | g_set_error_literal(error, |
255 | 0 | FWUPD_ERROR, |
256 | 0 | FWUPD_ERROR_INVALID_DATA, |
257 | 0 | "constant FuStructUswid.magic was not valid"); |
258 | 0 | return FALSE; |
259 | 0 | } |
260 | 254k | return TRUE; |
261 | 254k | } |
262 | | /** |
263 | | * fu_struct_uswid_validate_stream: (skip): |
264 | | **/ |
265 | | gboolean |
266 | | fu_struct_uswid_validate_stream(GInputStream *stream, gsize offset, GError **error) |
267 | 128k | { |
268 | 128k | g_autoptr(FuStructUswid) st = fu_struct_uswid_new_internal(); |
269 | 128k | g_return_val_if_fail(G_IS_INPUT_STREAM(stream), FALSE); |
270 | 128k | g_return_val_if_fail(error == NULL || *error == NULL, FALSE); |
271 | 128k | st->buf = fu_input_stream_read_byte_array(stream, offset, 25, NULL, error); |
272 | 128k | if (st->buf == NULL) { |
273 | 0 | g_prefix_error(error, "FuStructUswid failed read of 0x%x: ", (guint) 25); |
274 | 0 | return FALSE; |
275 | 0 | } |
276 | 128k | if (st->buf->len != 25) { |
277 | 826 | g_set_error(error, |
278 | 826 | FWUPD_ERROR, |
279 | 826 | FWUPD_ERROR_INVALID_DATA, |
280 | 826 | "FuStructUswid requested 0x%x and got 0x%x", |
281 | 826 | (guint) 25, |
282 | 826 | (guint) st->buf->len); |
283 | 826 | return FALSE; |
284 | 826 | } |
285 | 127k | return fu_struct_uswid_validate_internal(st, error); |
286 | 128k | } |
287 | | static gboolean |
288 | | fu_struct_uswid_parse_internal(FuStructUswid *st, GError **error) |
289 | 127k | { |
290 | 127k | if (g_getenv("FWUPD_VERBOSE") != NULL) { |
291 | 0 | g_autofree gchar *str = fu_struct_uswid_to_string(st); |
292 | 0 | g_debug("%s", str); |
293 | 0 | } |
294 | 127k | if (!fu_struct_uswid_validate_internal(st, error)) |
295 | 0 | return FALSE; |
296 | 127k | return TRUE; |
297 | 127k | } |
298 | | /** |
299 | | * fu_struct_uswid_parse_stream: (skip): |
300 | | **/ |
301 | | FuStructUswid * |
302 | | fu_struct_uswid_parse_stream(GInputStream *stream, gsize offset, GError **error) |
303 | 127k | { |
304 | 127k | g_autoptr(FuStructUswid) st = fu_struct_uswid_new_internal(); |
305 | 127k | st->buf = fu_input_stream_read_byte_array(stream, offset, 25, NULL, error); |
306 | 127k | if (st->buf == NULL) { |
307 | 0 | g_prefix_error(error, "FuStructUswid failed read of 0x%x: ", (guint) 25); |
308 | 0 | return NULL; |
309 | 0 | } |
310 | 127k | if (st->buf->len != 25) { |
311 | 0 | g_set_error(error, |
312 | 0 | FWUPD_ERROR, |
313 | 0 | FWUPD_ERROR_INVALID_DATA, |
314 | 0 | "FuStructUswid requested 0x%x and got 0x%x", |
315 | 0 | (guint) 25, |
316 | 0 | (guint) st->buf->len); |
317 | 0 | return NULL; |
318 | 0 | } |
319 | 127k | if (!fu_struct_uswid_parse_internal(st, error)) |
320 | 0 | return NULL; |
321 | 127k | return g_steal_pointer(&st); |
322 | 127k | } |