2016-11-02 16:32:30 +01:00
|
|
|
/* ************************************************************************** */
|
|
|
|
/* _____ _ */
|
2016-12-01 13:34:10 +01:00
|
|
|
/* utils/skip.c |_ _|__ _ _| |__ ___ _ _ */
|
2016-11-02 16:32:30 +01:00
|
|
|
/* | Project: libg1m | |/ _ \| | | | '_ \ / _ \ | | | */
|
|
|
|
/* | | (_) | |_| | | | | __/ |_| | */
|
|
|
|
/* By: thomas <thomas@touhey.fr> |_|\___/ \__,_|_| |_|\___|\__, |.fr */
|
2016-12-01 13:34:10 +01:00
|
|
|
/* Last updated: 2016/12/01 13:33:41 |___/ */
|
2016-11-02 16:32:30 +01:00
|
|
|
/* */
|
|
|
|
/* ************************************************************************** */
|
|
|
|
#include <libg1m/internals.h>
|
|
|
|
|
|
|
|
/**
|
|
|
|
* g1m_parse_skip:
|
|
|
|
* Skip some bytes.
|
|
|
|
*
|
|
|
|
* I've made two loops to avoid conditions in each loop iteration
|
|
|
|
* when checksum is not calculated.
|
|
|
|
*
|
|
|
|
* @arg stream the stream where to skip bytes.
|
|
|
|
* @arg size the size to skip.
|
|
|
|
* @arg checksum pointer to the checksum variable if there is one.
|
|
|
|
*/
|
|
|
|
|
|
|
|
int g1m_parse_skip(FILE *stream, size_t size, uint_fast32_t *checksum)
|
|
|
|
{
|
|
|
|
uint8_t buf[1024];
|
2016-12-13 12:31:53 +01:00
|
|
|
#if LOGLEVEL <= ll_error
|
2016-11-02 16:32:30 +01:00
|
|
|
size_t size_ini = size;
|
2016-12-13 12:31:53 +01:00
|
|
|
#endif
|
2016-11-02 16:32:30 +01:00
|
|
|
|
|
|
|
if (checksum) {
|
|
|
|
uint_fast32_t add = 0;
|
|
|
|
while (1) {
|
|
|
|
/* if no size left, exit */
|
|
|
|
if (!size) break;
|
|
|
|
|
|
|
|
/* read that much */
|
|
|
|
{
|
|
|
|
size_t curlen = min(size, 1024);
|
|
|
|
size_t read_len = fread(buf, 1, curlen, stream);
|
|
|
|
size -= read_len;
|
|
|
|
if (read_len < curlen) {
|
2017-01-27 21:49:04 +01:00
|
|
|
log_error("SKIPPING failed, skipped "
|
|
|
|
"%" PRIuSIZE "/%" PRIuSIZE " bytes, "
|
|
|
|
"%" PRIuSIZE " missing",
|
|
|
|
size_ini - size, size_ini, size);
|
2016-11-02 16:32:30 +01:00
|
|
|
return (g1m_error_eof);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* feed the checksum */
|
2016-11-26 03:00:47 +01:00
|
|
|
add = g1m_checksum32(buf, curlen, add);
|
2016-11-02 16:32:30 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
*checksum += add;
|
|
|
|
} else {
|
|
|
|
while (1) {
|
|
|
|
/* if no size left, exit */
|
|
|
|
if (!size) break;
|
|
|
|
|
|
|
|
/* read that much */
|
|
|
|
{
|
|
|
|
size_t curlen = min(size, 1024);
|
|
|
|
size_t read_len = fread(buf, 1, curlen, stream);
|
|
|
|
size -= read_len;
|
|
|
|
if (read_len < curlen) {
|
2017-01-27 21:49:04 +01:00
|
|
|
log_error("SKIPPING failed, skipped "
|
|
|
|
"%" PRIuSIZE "/%" PRIuSIZE " bytes, "
|
|
|
|
"%" PRIuSIZE " missing",
|
|
|
|
size_ini - size, size_ini, size);
|
2016-11-02 16:32:30 +01:00
|
|
|
return (g1m_error_eof);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* no error */
|
|
|
|
return (0);
|
|
|
|
}
|