aboutsummaryrefslogtreecommitdiff
path: root/lib/sqfs/src/io/stream_api.c
blob: 60e8472cffad7eb844c71b0a3d13677826ed036d (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
98
99
100
/* SPDX-License-Identifier: LGPL-3.0-or-later */
/*
 * stream_api.c
 *
 * Copyright (C) 2019 David Oberhollenzer <goliath@infraroot.at>
 */
#define SQFS_BUILDING_DLL
#include "config.h"

#include "sqfs/io.h"

#include <string.h>

sqfs_s32 sqfs_istream_read(sqfs_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 = strm->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);
		strm->advance_buffer(strm, diff);
		data = (char *)data + diff;
		size -= diff;
		total += diff;
	}

	return total;
}

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

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

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

		size -= diff;
		strm->advance_buffer(strm, diff);
	}

	return 0;
}

sqfs_s32 sqfs_istream_splice(sqfs_istream_t *in, sqfs_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 = in->get_buffered_data(in, &ptr, &diff, size);
		if (ret < 0)
			return ret;
		if (ret > 0)
			break;

		if (diff > size)
			diff = size;

		ret = out->append(out, ptr, diff);
		if (ret)
			return ret;

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

	return total;
}