aboutsummaryrefslogtreecommitdiff
path: root/lib/stream/builtin/csum32.c
blob: a0715827bfcfec291a05a5a3346503cf2340a5e9 (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
/* ****************************************************************************
 * stream/builtin/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 "../../internals.h"

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

struct thecookie {
	casio_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.
 *	@return				the error code (0 if ok).
 */

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

	/* Make the call. */

	err = casio_read(cookie->_stream, dest, size);
	if (err) return (err);

	/* Make the checksum. */

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

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

CASIO_LOCAL int csum32_close(struct thecookie *cookie)
{
	casio_free(cookie);
	return (0);
}

/* Callbacks. */
CASIO_LOCAL const casio_streamfuncs_t csum32_callbacks =
casio_stream_callbacks_for_virtual(csum32_close,
	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(casio_stream_t **stream,
	casio_stream_t *original, casio_uint32_t *csum)
{
	struct thecookie *cookie = NULL;
	casio_openmode_t openmode;

	if (!casio_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. */

	openmode = casio_get_openmode(original) & CASIO_OPENMODE_READ;
	return (casio_open_stream(stream, openmode, cookie, &csum32_callbacks, 0));
}