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


static int append_sparse_fallback(ostream_t *strm, size_t size)
{
	char buffer[512];
	size_t diff;

	memset(buffer, 0, sizeof(buffer));

	while (size > 0) {
		diff = size < sizeof(buffer) ? size : sizeof(buffer);

		if (strm->append(strm, buffer, diff))
			return -1;

		size -= diff;
	}

	return 0;
}


int ostream_append(ostream_t *strm, const void *data, size_t size)
{
	return strm->append(strm, data, size);
}

int ostream_append_sparse(ostream_t *strm, size_t size)
{
	if (strm->append_sparse == NULL)
		return append_sparse_fallback(strm, size);

	return strm->append_sparse(strm, size);
}

int ostream_flush(ostream_t *strm)
{
	return strm->flush(strm);
}

const char *ostream_get_filename(ostream_t *strm)
{
	return strm->get_filename(strm);
}

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

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

	while (size > 0) {
		if (in->buffer_offset >= in->buffer_used) {
			if (istream_precache(in))
				return -1;

			if (in->buffer_used == 0)
				break;
		}

		diff = in->buffer_used - in->buffer_offset;
		if (diff > size)
			diff = size;

		if (out->append(out, in->buffer + in->buffer_offset, diff))
			return -1;

		in->buffer_offset += diff;
		size -= diff;
		total += diff;
	}

	return total;
}