aboutsummaryrefslogtreecommitdiff
path: root/lib/stream/csum32.c
blob: 39dc2458fe8a5a83fd884431aa68cbe16d20c033 (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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/* ****************************************************************************
 * stream/csum32.c -- built-in stream for making a 32-bit checksum.
 * Copyright (C) 2017 Thomas "Cakeisalie5" Touhey <thomas@touhey.fr>
 *
 * This file is part of libcasio.
 * libcasio is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 3.0 of the License,
 * or (at your option) any later version.
 *
 * libcasio is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with libcasio; if not, see <http://www.gnu.org/licenses/>.
 * ************************************************************************* */
#include "stream.h"

/* ---
 * Cookie structure.
 * --- */

struct thecookie {
	tio_stream_t *_stream;
	casio_uint32_t *_checksum;
};

/* ---
 * Callbacks.
 * --- */

/**
 *	csum32_read:
 *	Read from this stream.
 *
 *	@arg	cookie		the cookie.
 *	@arg	data		the data pointer.
 *	@arg	size		the data size.
 *	@arg	timeout		the timeout.
 *	@return				the error code (0 if ok).
 */

CASIO_LOCAL int csum32_read(struct thecookie *cookie,
	unsigned char *dest, size_t count)
{
	int err;

	/* Make the call. */

	err = tio_read(cookie->_stream, dest, count);
	if (err) return (err);

	/* Make the checksum. */

	*cookie->_checksum = casio_checksum32(dest, count, *cookie->_checksum);
	return (0);
}

/**
 *	csum32_close:
 *	Close the stream.
 *
 *	@arg	cookie		the cookie.
 *	@return				the error code (0 if ok).
 */

CASIO_LOCAL void csum32_close(struct thecookie *cookie)
{
	casio_free(cookie);
}

/* Callbacks. */

CASIO_LOCAL tio_functions_t const csum32_callbacks = {
	(tio_close_t *)&csum32_close,
	(tio_read_t *)&csum32_read,
	NULL, NULL
};

/* ---
 * Main functions.
 * --- */

/**
 *	casio_open_csum32:
 *	Open a 32-bit checksum stream.
 *
 *	@arg	stream		the stream to make.
 *	@arg	original	the original stream.
 *	@arg	csum		the checksum pointer.
 *	@return				the error code (0 if ok).
 */

int CASIO_EXPORT casio_open_csum32(tio_stream_t **stream,
	tio_stream_t *original, casio_uint32_t *csum)
{
	struct thecookie *cookie = NULL;
	unsigned int openmode;

	if (!tio_isreadable(original))
		return (casio_error_op);

	/* Allocate the cookie. */

	cookie = casio_alloc(1, sizeof(struct thecookie));
	if (!cookie)
		return (casio_error_alloc);

	/* Fill the cookie. */

	cookie->_stream   = original;
	cookie->_checksum = csum;

	/* Initialize and return da stream. */

	return (casio_tio_error(tio_open(stream, original, cookie,
		TIO_OPENFLAG_READ, &csum32_callbacks, 0)));
}