예제 #1
0
파일: coll.c 프로젝트: AhmadTux/freebsd
/*
 * Implements random sort (-R).
 */
static int
randomcoll(struct key_value *kv1, struct key_value *kv2, size_t offset)
{
	struct bwstring *s1, *s2;
	MD5_CTX ctx1, ctx2;
	char *b1, *b2;

	UNUSED_ARG(offset);

	s1 = kv1->k;
	s2 = kv2->k;

	if (debug_sort) {
		bwsprintf(stdout, s1, "; k1=<", ">");
		bwsprintf(stdout, s2, ", k2=<", ">");
	}

	if (s1 == s2)
		return (0);

	memcpy(&ctx1,&md5_ctx,sizeof(MD5_CTX));
	memcpy(&ctx2,&md5_ctx,sizeof(MD5_CTX));

	MD5Update(&ctx1, bwsrawdata(s1), bwsrawlen(s1));
	MD5Update(&ctx2, bwsrawdata(s2), bwsrawlen(s2));
	b1 = MD5End(&ctx1, NULL);
	b2 = MD5End(&ctx2, NULL);
	if (b1 == NULL) {
		if (b2 == NULL)
			return (0);
		else {
			sort_free(b2);
			return (-1);
		}
	} else if (b2 == NULL) {
		sort_free(b1);
		return (+1);
	} else {
		int cmp_res;

		cmp_res = strcmp(b1,b2);
		sort_free(b1);
		sort_free(b2);

		if (!cmp_res)
			cmp_res = bwscoll(s1, s2, 0);

		return (cmp_res);
	}
}
예제 #2
0
/*
 * Implements random sort (-R).
 */
static int
randomcoll(struct key_value *kv1, struct key_value *kv2,
    size_t offset __unused)
{
	struct bwstring *s1, *s2;
	MD5_CTX ctx1, ctx2;
	unsigned char hash1[MD5_DIGEST_LENGTH], hash2[MD5_DIGEST_LENGTH];
	int cmp;

	s1 = kv1->k;
	s2 = kv2->k;

	if (debug_sort) {
		bwsprintf(stdout, s1, "; k1=<", ">");
		bwsprintf(stdout, s2, ", k2=<", ">");
	}

	if (s1 == s2)
		return (0);

	if (kv1->hint->status == HS_INITIALIZED &&
	    kv2->hint->status == HS_INITIALIZED) {
		cmp = memcmp(kv1->hint->v.Rh.cached,
		    kv2->hint->v.Rh.cached, sizeof(kv1->hint->v.Rh.cached));
		if (cmp != 0)
			return (cmp);
	}

	memcpy(&ctx1, &md5_ctx, sizeof(MD5_CTX));
	memcpy(&ctx2, &md5_ctx, sizeof(MD5_CTX));

	MD5Update(&ctx1, bwsrawdata(s1), bwsrawlen(s1));
	MD5Update(&ctx2, bwsrawdata(s2), bwsrawlen(s2));

	MD5Final(hash1, &ctx1);
	MD5Final(hash2, &ctx2);

	if (kv1->hint->status == HS_UNINITIALIZED)
		randomcoll_init_hint(kv1, hash1);
	if (kv2->hint->status == HS_UNINITIALIZED)
		randomcoll_init_hint(kv2, hash2);

	return (memcmp(hash1, hash2, sizeof(hash1)));
}