aboutsummaryrefslogtreecommitdiff
path: root/lib/io/src/istream.c
blob: 9340dd663c6bfd9f4ac01a3c3885895b09f08b36 (plain)
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
/* SPDX-License-Identifier: GPL-3.0-or-later */
/*
 * istream.c
 *
 * Copyright (C) 2019 David Oberhollenzer <goliath@infraroot.at>
 */
#include "internal.h"


sqfs_s32 istream_read(istream_t *strm, void *data, size_t size)
{
	sqfs_s32 total = 0;

	if (size > 0x7FFFFFFF)
		size = 0x7FFFFFFF;

	while (size > 0) {
		const sqfs_u8 *ptr;
		size_t diff;
		int ret;

		ret = istream_get_buffered_data(strm, &ptr, &diff, size);
		if (ret > 0)
			break;
		if (ret < 0)
			return ret;

		if (diff > size)
			diff = size;

		memcpy(data, ptr, diff);
		istream_advance_buffer(strm, diff);
		data = (char *)data + diff;
		size -= diff;
		total += diff;
	}

	return total;
}

int istream_skip(istream_t *strm, sqfs_u64 size)
{
	while (size > 0) {
		const sqfs_u8 *ptr;
		size_t diff;
		int ret;

		ret = istream_get_buffered_data(strm, &ptr, &diff, size);
		if (ret < 0)
			return ret;
		if (ret > 0) {
			fprintf(stderr, "%s: unexpected end-of-file\n",
				strm->get_filename(strm));
			return -1;
		}

		if ((sqfs_u64)diff > size)
			diff = size;

		size -= diff;
		istream_advance_buffer(strm, diff);
	}

	return 0;
}

sqfs_s32 istream_splice(istream_t *in, ostream_t *out, sqfs_u32 size)
{
	sqfs_s32 total = 0;

	if (size > 0x7FFFFFFF)
		size = 0x7FFFFFFF;

	while (size > 0) {
		const sqfs_u8 *ptr;
		size_t diff;
		int ret;

		ret = istream_get_buffered_data(in, &ptr, &diff, size);
		if (ret < 0)
			return ret;
		if (ret > 0)
			break;

		if (diff > size)
			diff = size;

		if (ostream_append(out, ptr, diff))
			return -1;

		total += diff;
		size -= diff;
		istream_advance_buffer(in, diff);
	}

	return total;
}