2018-11-07 18:20:02 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stddef.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <assert.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <inttypes.h>
|
|
|
|
#include "zlib.h"
|
|
|
|
|
|
|
|
int LLVMFuzzerTestOneInput(const uint8_t *data, size_t dataLen) {
|
2018-11-14 22:00:16 +00:00
|
|
|
uint32_t crc0 = crc32(0L, NULL, 0);
|
|
|
|
uint32_t crc1 = crc0;
|
|
|
|
uint32_t crc2 = crc0;
|
|
|
|
uint32_t adler0 = adler32(0L, NULL, 0);
|
|
|
|
uint32_t adler1 = adler0;
|
|
|
|
uint32_t adler2 = adler0;
|
2018-11-07 18:20:02 +00:00
|
|
|
/* Checksum with a buffer of size equal to the first byte in the input. */
|
|
|
|
uint32_t buffSize = data[0];
|
|
|
|
uint32_t offset = 0;
|
2018-11-14 22:00:16 +00:00
|
|
|
z_crc_t op[32];
|
2018-11-07 18:20:02 +00:00
|
|
|
|
|
|
|
/* Discard inputs larger than 1Mb. */
|
|
|
|
static size_t kMaxSize = 1024 * 1024;
|
|
|
|
if (dataLen < 1 || dataLen > kMaxSize)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
/* Make sure the buffer has at least a byte. */
|
|
|
|
if (buffSize == 0)
|
|
|
|
++buffSize;
|
|
|
|
|
|
|
|
/* CRC32 */
|
2018-11-14 22:00:16 +00:00
|
|
|
crc32_combine_gen(op, buffSize);
|
|
|
|
for (offset = 0; offset + buffSize <= dataLen; offset += buffSize) {
|
|
|
|
uint32_t crc3 = crc32_z(crc0, data + offset, buffSize);
|
|
|
|
uint32_t crc4 = crc32_combine_op(crc1, crc3, op);
|
2018-11-07 18:20:02 +00:00
|
|
|
crc1 = crc32_z(crc1, data + offset, buffSize);
|
2018-11-14 22:00:16 +00:00
|
|
|
assert(crc1 == crc4);
|
|
|
|
}
|
2018-11-07 18:20:02 +00:00
|
|
|
crc1 = crc32_z(crc1, data + offset, dataLen % buffSize);
|
|
|
|
|
2018-11-14 22:00:16 +00:00
|
|
|
crc2 = crc32(crc2, data, (uint32_t) dataLen);
|
2018-11-07 18:20:02 +00:00
|
|
|
|
|
|
|
assert(crc1 == crc2);
|
|
|
|
assert(crc32_combine(crc1, crc2, dataLen) ==
|
|
|
|
crc32_combine(crc1, crc1, dataLen));
|
|
|
|
|
2018-11-14 22:00:16 +00:00
|
|
|
/* Fast CRC32 combine. */
|
|
|
|
crc32_combine_gen(op, dataLen);
|
|
|
|
assert(crc32_combine_op(crc1, crc2, op) ==
|
|
|
|
crc32_combine_op(crc2, crc1, op));
|
|
|
|
assert(crc32_combine(crc1, crc2, dataLen) ==
|
|
|
|
crc32_combine_op(crc2, crc1, op));
|
|
|
|
|
2018-11-07 18:20:02 +00:00
|
|
|
/* Adler32 */
|
|
|
|
for (offset = 0; offset + buffSize <= dataLen; offset += buffSize)
|
|
|
|
adler1 = adler32_z(adler1, data + offset, buffSize);
|
|
|
|
adler1 = adler32_z(adler1, data + offset, dataLen % buffSize);
|
|
|
|
|
2018-11-14 22:00:16 +00:00
|
|
|
adler2 = adler32(adler2, data, (uint32_t) dataLen);
|
2018-11-07 18:20:02 +00:00
|
|
|
|
|
|
|
assert(adler1 == adler2);
|
|
|
|
assert(adler32_combine(adler1, adler2, dataLen) ==
|
|
|
|
adler32_combine(adler1, adler1, dataLen));
|
|
|
|
|
|
|
|
/* This function must return 0. */
|
|
|
|
return 0;
|
|
|
|
}
|