File: fu-partial-input-stream.c

package info (click to toggle)
fwupd 2.0.19-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 32,340 kB
  • sloc: ansic: 274,440; python: 11,468; xml: 9,432; sh: 1,625; makefile: 167; cpp: 19; asm: 11; javascript: 9
file content (279 lines) | stat: -rw-r--r-- 7,406 bytes parent folder | download
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
/*
 * Copyright 2023 Richard Hughes <richard@hughsie.com>
 *
 * SPDX-License-Identifier: LGPL-2.1-or-later
 */

#define G_LOG_DOMAIN "FuPartialInputStream"

#include "config.h"

#include "fwupd-codec.h"

#include "fu-common.h"
#include "fu-input-stream.h"
#include "fu-partial-input-stream-private.h"

/**
 * FuPartialInputStream:
 *
 * A input stream that is a slice of another input stream.
 *
 *       off    sz
 *    [xxxxxxxxxxxx]
 *       |  0x6  |
 *        \      \
 *         \      \
 *          \      |
 *           |     |
 *          [xxxxxx]
 *
 * xxx offset: 2, sz: 6
 */

struct _FuPartialInputStream {
	GInputStream parent_instance;
	GInputStream *base_stream;
	gsize offset;
	gsize size;
};

static void
fu_partial_input_stream_seekable_iface_init(GSeekableIface *iface);
static void
fu_partial_input_stream_codec_iface_init(FwupdCodecInterface *iface);

G_DEFINE_TYPE_WITH_CODE(FuPartialInputStream,
			fu_partial_input_stream,
			G_TYPE_INPUT_STREAM,
			G_IMPLEMENT_INTERFACE(G_TYPE_SEEKABLE,
					      fu_partial_input_stream_seekable_iface_init)
			    G_IMPLEMENT_INTERFACE(FWUPD_TYPE_CODEC,
						  fu_partial_input_stream_codec_iface_init))

static void
fu_partial_input_stream_add_string(FwupdCodec *codec, guint idt, GString *str)
{
	FuPartialInputStream *self = FU_PARTIAL_INPUT_STREAM(codec);
	fwupd_codec_string_append_hex(str, idt, "Offset", self->offset);
	fwupd_codec_string_append_hex(str, idt, "Size", self->size);
}

static void
fu_partial_input_stream_codec_iface_init(FwupdCodecInterface *iface)
{
	iface->add_string = fu_partial_input_stream_add_string;
}

static goffset
fu_partial_input_stream_tell(GSeekable *seekable)
{
	FuPartialInputStream *self = FU_PARTIAL_INPUT_STREAM(seekable);
	return g_seekable_tell(G_SEEKABLE(self->base_stream)) - self->offset;
}

static gboolean
fu_partial_input_stream_can_seek(GSeekable *seekable)
{
	FuPartialInputStream *self = FU_PARTIAL_INPUT_STREAM(seekable);
	return g_seekable_can_seek(G_SEEKABLE(self->base_stream));
}

static gboolean
fu_partial_input_stream_seek(GSeekable *seekable,
			     goffset offset,
			     GSeekType type,
			     GCancellable *cancellable,
			     GError **error)
{
	FuPartialInputStream *self = FU_PARTIAL_INPUT_STREAM(seekable);

	g_return_val_if_fail(FU_IS_PARTIAL_INPUT_STREAM(self), FALSE);
	g_return_val_if_fail(error == NULL || *error == NULL, FALSE);

	if (type == G_SEEK_CUR) {
		goffset pos = g_seekable_tell(G_SEEKABLE(self->base_stream));
		return g_seekable_seek(G_SEEKABLE(self->base_stream),
				       self->offset + pos + offset,
				       G_SEEK_SET,
				       cancellable,
				       error);
	}
	if (type == G_SEEK_END) {
		return g_seekable_seek(G_SEEKABLE(self->base_stream),
				       self->offset + self->size + offset,
				       G_SEEK_SET,
				       cancellable,
				       error);
	}
	return g_seekable_seek(G_SEEKABLE(self->base_stream),
			       self->offset + offset,
			       G_SEEK_SET,
			       cancellable,
			       error);
}

static gboolean
fu_partial_input_stream_can_truncate(GSeekable *seekable)
{
	return FALSE;
}

static gboolean
fu_partial_input_stream_truncate(GSeekable *seekable,
				 goffset offset,
				 GCancellable *cancellable,
				 GError **error)
{
	g_set_error_literal(error,
			    FWUPD_ERROR,
			    FWUPD_ERROR_NOT_SUPPORTED,
			    "cannot truncate FuPartialInputStream");
	return FALSE;
}

static void
fu_partial_input_stream_seekable_iface_init(GSeekableIface *iface)
{
	iface->tell = fu_partial_input_stream_tell;
	iface->can_seek = fu_partial_input_stream_can_seek;
	iface->seek = fu_partial_input_stream_seek;
	iface->can_truncate = fu_partial_input_stream_can_truncate;
	iface->truncate_fn = fu_partial_input_stream_truncate;
}

/**
 * fu_partial_input_stream_new:
 * @stream: a base #GInputStream
 * @offset: offset into @stream
 * @size: size of @stream in bytes, or %G_MAXSIZE for the "rest" of the stream
 * @error: (nullable): optional return location for an error
 *
 * Creates a partial input stream where content is read from the donor stream.
 *
 * Returns: (transfer full): a #FuPartialInputStream, or %NULL on error
 *
 * Since: 2.0.0
 **/
GInputStream *
fu_partial_input_stream_new(GInputStream *stream, gsize offset, gsize size, GError **error)
{
	gsize base_sz = 0;
	g_autoptr(FuPartialInputStream) self = g_object_new(FU_TYPE_PARTIAL_INPUT_STREAM, NULL);

	g_return_val_if_fail(G_IS_INPUT_STREAM(stream), NULL);
	g_return_val_if_fail(error == NULL || *error == NULL, NULL);

	self->base_stream = g_object_ref(stream);
	self->offset = offset;

	/* sanity check */
	if (!fu_input_stream_size(stream, &base_sz, error)) {
		g_prefix_error_literal(error, "failed to get size: ");
		return NULL;
	}
	if (size == G_MAXSIZE) {
		if (offset > base_sz) {
			g_set_error(error,
				    FWUPD_ERROR,
				    FWUPD_ERROR_INVALID_DATA,
				    "base stream was 0x%x bytes in size "
				    "and tried to create partial stream @0x%x",
				    (guint)base_sz,
				    (guint)offset);
			return NULL;
		}
		self->size = base_sz - offset;
	} else {
		if (fu_size_checked_add(offset, size) > base_sz) {
			g_set_error(error,
				    FWUPD_ERROR,
				    FWUPD_ERROR_INVALID_DATA,
				    "base stream was 0x%x bytes in size, and tried to create "
				    "partial stream @0x%x of 0x%x bytes",
				    (guint)base_sz,
				    (guint)offset,
				    (guint)size);
			return NULL;
		}
		self->size = size;
	}

	/* success */
	return G_INPUT_STREAM(g_steal_pointer(&self));
}

/**
 * fu_partial_input_stream_get_offset:
 * @self: a #FuPartialInputStream
 *
 * Gets the offset of the stream.
 *
 * Returns: integer
 *
 * Since: 2.0.0
 **/
gsize
fu_partial_input_stream_get_offset(FuPartialInputStream *self)
{
	g_return_val_if_fail(FU_IS_PARTIAL_INPUT_STREAM(self), G_MAXSIZE);
	return self->offset;
}

/**
 * fu_partial_input_stream_get_size:
 * @self: a #FuPartialInputStream
 *
 * Gets the offset of the stream.
 *
 * Returns: integer
 *
 * Since: 2.0.0
 **/
gsize
fu_partial_input_stream_get_size(FuPartialInputStream *self)
{
	g_return_val_if_fail(FU_IS_PARTIAL_INPUT_STREAM(self), G_MAXSIZE);
	return self->size;
}

static gssize
fu_partial_input_stream_read(GInputStream *stream,
			     void *buffer,
			     gsize count,
			     GCancellable *cancellable,
			     GError **error)
{
	FuPartialInputStream *self = FU_PARTIAL_INPUT_STREAM(stream);
	g_return_val_if_fail(FU_IS_PARTIAL_INPUT_STREAM(self), -1);
	g_return_val_if_fail(error == NULL || *error == NULL, -1);
	if (self->size < (gsize)g_seekable_tell(G_SEEKABLE(stream))) {
		g_warning("base stream is outside seekable range");
		return 0;
	}
	count = MIN(count, self->size - g_seekable_tell(G_SEEKABLE(stream)));
	return g_input_stream_read(self->base_stream, buffer, count, cancellable, error);
}

static void
fu_partial_input_stream_finalize(GObject *object)
{
	FuPartialInputStream *self = FU_PARTIAL_INPUT_STREAM(object);
	if (self->base_stream != NULL)
		g_object_unref(self->base_stream);
	G_OBJECT_CLASS(fu_partial_input_stream_parent_class)->finalize(object);
}

static void
fu_partial_input_stream_class_init(FuPartialInputStreamClass *klass)
{
	GObjectClass *object_class = G_OBJECT_CLASS(klass);
	GInputStreamClass *istream_class = G_INPUT_STREAM_CLASS(klass);
	istream_class->read_fn = fu_partial_input_stream_read;
	object_class->finalize = fu_partial_input_stream_finalize;
}

static void
fu_partial_input_stream_init(FuPartialInputStream *self)
{
}