/src/fwupd/libfwupdplugin/fu-composite-input-stream.c
Line | Count | Source |
1 | | /* |
2 | | * Copyright 2023 Richard Hughes <richard@hughsie.com> |
3 | | * |
4 | | * SPDX-License-Identifier: LGPL-2.1-or-later |
5 | | */ |
6 | | |
7 | 0 | #define G_LOG_DOMAIN "FuCompositeInputStream" |
8 | | |
9 | | #include "config.h" |
10 | | |
11 | | #include "fwupd-codec.h" |
12 | | #include "fwupd-memory-input-stream.h" |
13 | | #include "fwupd-rust-composite-input-stream.h" |
14 | | |
15 | | #include "fu-common.h" |
16 | | #include "fu-composite-input-stream.h" |
17 | | #include "fu-input-stream.h" |
18 | | #include "fu-partial-input-stream-private.h" |
19 | | |
20 | | /** |
21 | | * FuCompositeInputStream: |
22 | | * |
23 | | * A input stream that is made up of other partial streams, e.g. |
24 | | * |
25 | | * off sz off sz |
26 | | * [xxxxxxxxxxxx] [yyyyyyyy] |
27 | | * | 0x6 | |0x4| |
28 | | * \ \ / / |
29 | | * \ \ / / |
30 | | * \ | | |
31 | | * | | | |
32 | | * [xxxxxxyyyyyy] |
33 | | * |
34 | | * xxx offset: 2, sz: 6 |
35 | | * yyy offset: 0, sz: 4 |
36 | | */ |
37 | | |
38 | | struct _FuCompositeInputStream { |
39 | | FuInputStream parent_instance; |
40 | | GPtrArray *partial_streams; /* of FuPartialInputStream (GObject refs) */ |
41 | | FuRsCompositeInputStream *rust; |
42 | | }; |
43 | | |
44 | | static void |
45 | | fu_composite_input_stream_codec_iface_init(FwupdCodecInterface *iface); |
46 | | |
47 | 30.6k | G_DEFINE_TYPE_WITH_CODE(FuCompositeInputStream, |
48 | 30.6k | fu_composite_input_stream, |
49 | 30.6k | FWUPD_TYPE_INPUT_STREAM, |
50 | 30.6k | G_IMPLEMENT_INTERFACE(FWUPD_TYPE_CODEC, |
51 | 30.6k | fu_composite_input_stream_codec_iface_init)) |
52 | 30.6k | |
53 | 30.6k | static void |
54 | 30.6k | fu_composite_input_stream_add_string(FwupdCodec *codec, guint idt, GString *str) |
55 | 30.6k | { |
56 | 0 | FuCompositeInputStream *self = FU_COMPOSITE_INPUT_STREAM(codec); |
57 | 0 | fwupd_codec_string_append_hex(str, |
58 | 0 | idt, |
59 | 0 | "Pos", |
60 | 0 | fu_rs_composite_input_stream_tell(self->rust)); |
61 | 0 | fwupd_codec_string_append_hex(str, |
62 | 0 | idt, |
63 | 0 | "TotalSize", |
64 | 0 | fu_rs_composite_input_stream_size(self->rust)); |
65 | 0 | for (guint i = 0; i < self->partial_streams->len; i++) { |
66 | 0 | FuPartialInputStream *pstream = g_ptr_array_index(self->partial_streams, i); |
67 | 0 | fwupd_codec_add_string(FWUPD_CODEC(pstream), idt, str); |
68 | 0 | } |
69 | 0 | } |
70 | | |
71 | | static void |
72 | | fu_composite_input_stream_codec_iface_init(FwupdCodecInterface *iface) |
73 | 2 | { |
74 | 2 | iface->add_string = fu_composite_input_stream_add_string; |
75 | 2 | } |
76 | | |
77 | | /** |
78 | | * fu_composite_input_stream_add_bytes: |
79 | | * @self: a #FuCompositeInputStream |
80 | | * @bytes: a #GBytes |
81 | | * @error: (nullable): optional return location for an error |
82 | | * |
83 | | * Adds a bytes object. |
84 | | * |
85 | | * Returns: %TRUE for success |
86 | | **/ |
87 | | gboolean |
88 | | fu_composite_input_stream_add_bytes(FuCompositeInputStream *self, GBytes *bytes, GError **error) |
89 | 584 | { |
90 | 584 | g_autoptr(FwupdMemoryInputStream) stream = NULL; |
91 | 584 | g_autoptr(FuInputStream) partial_stream = NULL; |
92 | | |
93 | 584 | g_return_val_if_fail(FU_IS_COMPOSITE_INPUT_STREAM(self), FALSE); |
94 | 584 | g_return_val_if_fail(bytes != NULL, FALSE); |
95 | 584 | g_return_val_if_fail(error == NULL || *error == NULL, FALSE); |
96 | | |
97 | 584 | stream = fwupd_memory_input_stream_new_from_bytes(bytes); |
98 | 584 | partial_stream = fu_partial_input_stream_new(FWUPD_INPUT_STREAM(stream), |
99 | 584 | 0x0, |
100 | 584 | g_bytes_get_size(bytes), |
101 | 584 | error); |
102 | 584 | if (partial_stream == NULL) |
103 | 0 | return FALSE; |
104 | 584 | return fu_composite_input_stream_add_partial_stream(self, |
105 | 584 | FU_PARTIAL_INPUT_STREAM(partial_stream), |
106 | 584 | error); |
107 | 584 | } |
108 | | |
109 | | /** |
110 | | * fu_composite_input_stream_add_partial_stream: |
111 | | * @self: a #FuCompositeInputStream |
112 | | * @partial_stream: a #FuPartialInputStream |
113 | | * @error: (nullable): optional return location for an error |
114 | | * |
115 | | * Adds a partial stream object. |
116 | | * |
117 | | * Returns: %TRUE for success |
118 | | **/ |
119 | | gboolean |
120 | | fu_composite_input_stream_add_partial_stream(FuCompositeInputStream *self, |
121 | | FuPartialInputStream *partial_stream, |
122 | | GError **error) |
123 | 7.08k | { |
124 | 7.08k | gsize psize; |
125 | 7.08k | FuRsStreamImpl *impl; |
126 | | |
127 | 7.08k | g_return_val_if_fail(FU_IS_COMPOSITE_INPUT_STREAM(self), FALSE); |
128 | 7.08k | g_return_val_if_fail(FU_IS_PARTIAL_INPUT_STREAM(partial_stream), FALSE); |
129 | 7.08k | g_return_val_if_fail(FWUPD_INPUT_STREAM(self) != FWUPD_INPUT_STREAM(partial_stream), FALSE); |
130 | 7.08k | g_return_val_if_fail(error == NULL || *error == NULL, FALSE); |
131 | | |
132 | 7.08k | psize = fu_partial_input_stream_get_size(partial_stream); |
133 | | |
134 | | /* tell the Rust side about this sub-stream */ |
135 | 7.08k | impl = fwupd_input_stream_get_impl(FWUPD_INPUT_STREAM(partial_stream)); |
136 | 7.08k | if (impl == NULL) { |
137 | 0 | g_set_error_literal(error, |
138 | 0 | FWUPD_ERROR, |
139 | 0 | FWUPD_ERROR_INTERNAL, |
140 | 0 | "Composite streams only support Rust-based streams"); |
141 | 0 | return FALSE; |
142 | 0 | } |
143 | | |
144 | | /* keep a GObject ref for lifecycle management */ |
145 | 7.08k | g_ptr_array_add(self->partial_streams, g_object_ref(partial_stream)); |
146 | | |
147 | 7.08k | fu_rs_composite_input_stream_add_stream(self->rust, impl, psize); |
148 | 7.08k | return TRUE; |
149 | 7.08k | } |
150 | | |
151 | | /** |
152 | | * fu_composite_input_stream_add_stream: |
153 | | * @self: a #FuCompositeInputStream |
154 | | * @stream: a #FuInputStream |
155 | | * @error: (nullable): optional return location for an error |
156 | | * |
157 | | * Adds a input stream object, which has to be seekable. |
158 | | * |
159 | | * Returns: %TRUE for success |
160 | | **/ |
161 | | gboolean |
162 | | fu_composite_input_stream_add_stream(FuCompositeInputStream *self, |
163 | | FuInputStream *stream, |
164 | | GError **error) |
165 | 584 | { |
166 | 584 | g_autoptr(FuInputStream) partial_stream = NULL; |
167 | | |
168 | 584 | g_return_val_if_fail(FU_IS_COMPOSITE_INPUT_STREAM(self), FALSE); |
169 | 584 | g_return_val_if_fail(FWUPD_IS_INPUT_STREAM(stream), FALSE); |
170 | 584 | g_return_val_if_fail(FWUPD_INPUT_STREAM(self) != stream, FALSE); |
171 | 584 | g_return_val_if_fail(error == NULL || *error == NULL, FALSE); |
172 | | |
173 | | /* create a partial stream that is actually the size of the entire FuInputStream */ |
174 | 584 | partial_stream = fu_partial_input_stream_new(stream, 0x0, G_MAXSIZE, error); |
175 | 584 | if (partial_stream == NULL) { |
176 | 0 | g_prefix_error_literal(error, "failed to add input stream: "); |
177 | 0 | return FALSE; |
178 | 0 | } |
179 | | |
180 | | /* success */ |
181 | 584 | return fu_composite_input_stream_add_partial_stream(self, |
182 | 584 | FU_PARTIAL_INPUT_STREAM(partial_stream), |
183 | 584 | error); |
184 | 584 | } |
185 | | |
186 | | static goffset |
187 | | fu_composite_input_stream_tell(FuInputStream *stream) |
188 | 4.05k | { |
189 | 4.05k | FuCompositeInputStream *self = FU_COMPOSITE_INPUT_STREAM(stream); |
190 | 4.05k | g_return_val_if_fail(FU_IS_COMPOSITE_INPUT_STREAM(self), -1); |
191 | 4.05k | return fu_rs_composite_input_stream_tell(self->rust); |
192 | 4.05k | } |
193 | | |
194 | | static gboolean |
195 | | fu_composite_input_stream_can_seek(FuInputStream *stream) |
196 | 3.62k | { |
197 | 3.62k | FuCompositeInputStream *self = FU_COMPOSITE_INPUT_STREAM(stream); |
198 | 3.62k | return fu_rs_composite_input_stream_can_seek(self->rust); |
199 | 3.62k | } |
200 | | |
201 | | static gboolean |
202 | | fu_composite_input_stream_seek(FuInputStream *stream, |
203 | | goffset offset, |
204 | | GSeekType type, |
205 | | GCancellable *cancellable, |
206 | | GError **error) |
207 | 6.20k | { |
208 | 6.20k | FuCompositeInputStream *self = FU_COMPOSITE_INPUT_STREAM(stream); |
209 | | |
210 | 6.20k | g_return_val_if_fail(FU_IS_COMPOSITE_INPUT_STREAM(self), FALSE); |
211 | 6.20k | g_return_val_if_fail(error == NULL || *error == NULL, FALSE); |
212 | | |
213 | 6.20k | if (!fu_rs_composite_input_stream_seek(self->rust, offset, (gint32)type)) { |
214 | 0 | if (offset >= 0) { |
215 | 0 | g_set_error(error, |
216 | 0 | G_IO_ERROR, |
217 | 0 | G_IO_ERROR_INVALID_ARGUMENT, |
218 | 0 | "seek to 0x%" G_GINT64_MODIFIER "x failed", |
219 | 0 | (guint64)offset); /* nocheck:error */ |
220 | 0 | fwupd_error_convert(error); |
221 | 0 | return FALSE; |
222 | 0 | } |
223 | 0 | g_set_error(error, |
224 | 0 | G_IO_ERROR, |
225 | 0 | G_IO_ERROR_INVALID_ARGUMENT, |
226 | 0 | "seek to %" G_GINT64_MODIFIER "d failed", |
227 | 0 | offset); /* nocheck:error */ |
228 | 0 | fwupd_error_convert(error); |
229 | 0 | return FALSE; |
230 | 0 | } |
231 | 6.20k | return TRUE; |
232 | 6.20k | } |
233 | | |
234 | | /** |
235 | | * fu_composite_input_stream_new: |
236 | | * |
237 | | * Creates a composite input stream. |
238 | | * |
239 | | * Returns: (transfer full): a #FuCompositeInputStream |
240 | | **/ |
241 | | FuInputStream * |
242 | | fu_composite_input_stream_new(void) |
243 | 5.27k | { |
244 | 5.27k | return FWUPD_INPUT_STREAM(g_object_new(FU_TYPE_COMPOSITE_INPUT_STREAM, NULL)); |
245 | 5.27k | } |
246 | | |
247 | | static gssize |
248 | | fu_composite_input_stream_read_fn(FuInputStream *stream, |
249 | | void *buffer, |
250 | | gsize count, |
251 | | GCancellable *cancellable, |
252 | | GError **error) |
253 | 6.90k | { |
254 | 6.90k | FuCompositeInputStream *self = FU_COMPOSITE_INPUT_STREAM(stream); |
255 | 6.90k | gssize rc; |
256 | | |
257 | 6.90k | g_return_val_if_fail(FU_IS_COMPOSITE_INPUT_STREAM(self), -1); |
258 | 6.90k | g_return_val_if_fail(error == NULL || *error == NULL, -1); |
259 | | |
260 | 6.90k | if (g_cancellable_set_error_if_cancelled(cancellable, error)) |
261 | 0 | return -1; |
262 | | |
263 | 6.90k | rc = fu_rs_composite_input_stream_read(self->rust, buffer, count); |
264 | 6.90k | if (rc < 0) { |
265 | 0 | g_set_error(error, |
266 | 0 | G_IO_ERROR, /* nocheck:error */ |
267 | | #ifdef HAVE_ERRNO_H |
268 | | g_io_error_from_errno(-rc), |
269 | | #else |
270 | 0 | G_IO_ERROR_FAILED, /* nocheck:blocked */ |
271 | 0 | #endif |
272 | 0 | "failed to read %zu bytes: %s", |
273 | 0 | count, |
274 | 0 | fwupd_strerror(-rc)); |
275 | 0 | fwupd_error_convert(error); |
276 | 0 | return -1; |
277 | 0 | } |
278 | | |
279 | 6.90k | return rc; |
280 | 6.90k | } |
281 | | |
282 | | static void |
283 | | fu_composite_input_stream_finalize(GObject *object) |
284 | 5.27k | { |
285 | 5.27k | FuCompositeInputStream *self = FU_COMPOSITE_INPUT_STREAM(object); |
286 | | /* must free the Rust stream first */ |
287 | 5.27k | fu_rs_composite_input_stream_free(self->rust); |
288 | 5.27k | g_ptr_array_unref(self->partial_streams); |
289 | 5.27k | G_OBJECT_CLASS(fu_composite_input_stream_parent_class)->finalize(object); |
290 | 5.27k | } |
291 | | |
292 | | static FwupdInputStreamImpl |
293 | | fu_composite_input_stream_get_impl(FuInputStream *stream) |
294 | 1.32k | { |
295 | 1.32k | FuCompositeInputStream *self = FU_COMPOSITE_INPUT_STREAM(stream); |
296 | 1.32k | return fu_rs_composite_input_stream_get_impl(self->rust); |
297 | 1.32k | } |
298 | | |
299 | | static void |
300 | | fu_composite_input_stream_class_init(FuCompositeInputStreamClass *klass) |
301 | 2 | { |
302 | 2 | GObjectClass *object_class = G_OBJECT_CLASS(klass); |
303 | 2 | FwupdInputStreamClass *istream_class = FWUPD_INPUT_STREAM_CLASS(klass); |
304 | 2 | istream_class->read_fn = fu_composite_input_stream_read_fn; |
305 | 2 | istream_class->tell = fu_composite_input_stream_tell; |
306 | 2 | istream_class->can_seek = fu_composite_input_stream_can_seek; |
307 | 2 | istream_class->seek = fu_composite_input_stream_seek; |
308 | 2 | istream_class->get_impl = fu_composite_input_stream_get_impl; |
309 | 2 | object_class->finalize = fu_composite_input_stream_finalize; |
310 | 2 | } |
311 | | |
312 | | static void |
313 | | fu_composite_input_stream_init(FuCompositeInputStream *self) |
314 | 5.27k | { |
315 | 5.27k | self->rust = fu_rs_composite_input_stream_new(); |
316 | 5.27k | self->partial_streams = g_ptr_array_new_with_free_func(g_object_unref); |
317 | 5.27k | } |