/* **************************************************************************** * stream/csum32.c -- built-in stream for making a 32-bit checksum. * Copyright (C) 2017 Thomas "Cakeisalie5" Touhey * * 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 . * ************************************************************************* */ #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))); }