Example #1
0
int zlib_deflateSetDictionary(
	z_streamp strm,
	const Byte *dictionary,
	uInt  dictLength
)
{
    deflate_state *s;
    uInt length = dictLength;
    uInt n;
    IPos hash_head = 0;

    if (strm == NULL || strm->state == NULL || dictionary == NULL)
	return Z_STREAM_ERROR;

    s = (deflate_state *) strm->state;
    if (s->status != INIT_STATE) return Z_STREAM_ERROR;

    strm->adler = zlib_adler32(strm->adler, dictionary, dictLength);

    if (length < MIN_MATCH) return Z_OK;
    if (length > MAX_DIST(s)) {
	length = MAX_DIST(s);
#ifndef USE_DICT_HEAD
	dictionary += dictLength - length; /* use the tail of the dictionary */
#endif
    }
    memcpy((char *)s->window, dictionary, length);
    s->strstart = length;
    s->block_start = (long)length;

    /* Insert all strings in the hash table (except for the last two bytes).
     * s->lookahead stays null, so s->ins_h will be recomputed at the next
     * call of fill_window.
     */
    s->ins_h = s->window[0];
    UPDATE_HASH(s, s->ins_h, s->window[1]);
    for (n = 0; n <= length - MIN_MATCH; n++) {
	INSERT_STRING(s, n, hash_head);
    }
    if (hash_head) hash_head = 0;  /* to make compiler happy */
    return Z_OK;
}
Example #2
0
static unsigned long calc_adler32(void *pub_buf, int pub_elems)
{
	unsigned long adler = zlib_adler32(0L, NULL, 0);

	return zlib_adler32(adler, pub_buf, pub_elems * sizeof(uint32_t));
}
static u32 __adler32(u32 cksum, unsigned char const *p, size_t len)
{
	return zlib_adler32(cksum, p, len);
}
Example #4
0
char *
lcg_compute_checksum (const char *file, enum gfal_cksm_type cksmtype, char *errbuf, int errbufsz)
{
	unsigned long cksm = 0;
	EVP_MD_CTX evpctx;
	int fd = -1;
	unsigned int nbread = 0;
	char buffer[GFAL_CKSM_BUFSIZE];
	char checksum[2 * EVP_MAX_MD_SIZE + 1];
	unsigned char cksm_raw[EVP_MAX_MD_SIZE + 1];
	int cksmsize = 0;

	if (file == NULL || cksmtype == GFAL_CKSM_NONE) {
		gfal_errmsg (errbuf, errbufsz, GFAL_ERRLEVEL_ERROR, "[LCG-UTIL][compute_checksum][] Invalid arguments");
		errno = EINVAL;
		return (NULL);
	}

	memset (checksum, 0, (2 * EVP_MAX_MD_SIZE + 1) * sizeof (char));

	if (cksmtype == GFAL_CKSM_CRC32 || cksmtype == GFAL_CKSM_ADLER32) {
		zlib_handle = dlopen ("libz.so", RTLD_LAZY);
		if (zlib_handle == NULL) {
			gfal_errmsg (errbuf, errbufsz, GFAL_ERRLEVEL_ERROR, "[LCG-UTIL][compute_checksum][] CRC32/ADLER32 need zlib!");
			errno = ELIBACC;
			return (NULL);
		}

		zlib_crc32 = (unsigned long (*) (unsigned long, const char *, unsigned int)) dlsym (zlib_handle, "crc32");
		zlib_adler32 = (unsigned long (*) (unsigned long, const char *, unsigned int)) dlsym (zlib_handle, "adler32");
		if (zlib_crc32 == NULL || zlib_adler32 == NULL) {
			gfal_errmsg (errbuf, errbufsz, GFAL_ERRLEVEL_ERROR, "[LCG-UTIL][compute_checksum][] invalid zlib library!");
			errno = ELIBBAD;
			return (NULL);
		}
	}

	fd = gfal_open (file, O_RDONLY, 0);
	if (fd < 0)
		return (NULL);

	if (cksmtype == GFAL_CKSM_ADLER32)
		cksm = zlib_adler32 (0L, 0, 0);
	else if (cksmtype == GFAL_CKSM_CRC32)
		cksm = zlib_crc32 (0L, 0, 0);
	else {
		if (cksmtype == GFAL_CKSM_MD5)
			EVP_DigestInit (&evpctx, EVP_md5());
		else
			EVP_DigestInit (&evpctx, EVP_sha1());
	}

	while ((nbread = (unsigned int) gfal_read (fd, buffer, GFAL_CKSM_BUFSIZE)) > 0) {
		if (cksmtype == GFAL_CKSM_ADLER32)
			cksm = zlib_adler32 (cksm, buffer, nbread);
		else if (cksmtype == GFAL_CKSM_CRC32)
			cksm = zlib_crc32 (cksm, buffer, nbread);
		else
			EVP_DigestUpdate(&evpctx, buffer, (size_t) nbread);
	}

	gfal_close (fd);

	if (nbread < 0)
		return (NULL);

	if (cksmtype == GFAL_CKSM_ADLER32 || cksmtype == GFAL_CKSM_CRC32) {
		sprintf(checksum, "%08x", (unsigned int) cksm);
	} else {
		char *p;
		int i;

		EVP_DigestFinal (&evpctx, cksm_raw, &cksmsize);

		for (i = 0, p = checksum; i < cksmsize; ++i, p = p + 2)
			sprintf(p, "%02x", cksm_raw[i]);
	}

	return (strdup (checksum));
}