/* ************************************************************************** */ /* _____ _ */ /* utils/skip.c |_ _|__ _ _| |__ ___ _ _ */ /* | Project: libg1m | |/ _ \| | | | '_ \ / _ \ | | | */ /* | | (_) | |_| | | | | __/ |_| | */ /* By: thomas |_|\___/ \__,_|_| |_|\___|\__, |.fr */ /* Last updated: 2016/12/01 13:33:41 |___/ */ /* */ /* ************************************************************************** */ #include /** * 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]; #if LOGLEVEL <= ll_error size_t size_ini = size; #endif 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) { log_error("SKIPPING failed, skipped " "%" PRIuSIZE "/%" PRIuSIZE " bytes, " "%" PRIuSIZE " missing", size_ini - size, size_ini, size); return (g1m_error_eof); } /* feed the checksum */ add = g1m_checksum32(buf, curlen, add); } } *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) { log_error("SKIPPING failed, skipped " "%" PRIuSIZE "/%" PRIuSIZE " bytes, " "%" PRIuSIZE " missing", size_ini - size, size_ini, size); return (g1m_error_eof); } } } } /* no error */ return (0); }