Class: Digest::XXH32

Inherits:
XXHash show all
Defined in:
ext/digest/xxhash/ext.c,
ext/digest/xxhash/ext.c

Overview

This class implements XXH32.

Constant Summary

Constants inherited from XXHash

Digest::XXHash::VERSION, Digest::XXHash::XXH3_SECRET_SIZE_MIN

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from XXHash

#digest, digest, #hexdigest, hexdigest, idigest, #idigest, #idigest!, #initialize, #inspect

Constructor Details

This class inherits a constructor from Digest::XXHash

Class Method Details

.block_lengthInteger

Returns 4

Returns:

  • (Integer)


555
556
557
558
# File 'ext/digest/xxhash/ext.c', line 555

static VALUE _Digest_XXH32_singleton_block_length(VALUE self)
{
	return INT2FIX(_XXH32_BLOCK_SIZE);
}

.digest_lengthInteger

Returns 4

Returns:

  • (Integer)


545
546
547
548
# File 'ext/digest/xxhash/ext.c', line 545

static VALUE _Digest_XXH32_singleton_digest_length(VALUE self)
{
	return INT2FIX(_XXH32_DIGEST_SIZE);
}

Instance Method Details

#block_lengthInteger

Returns 4

Returns:

  • (Integer)


535
536
537
538
# File 'ext/digest/xxhash/ext.c', line 535

static VALUE _Digest_XXH32_block_length(VALUE self)
{
	return INT2FIX(_XXH32_BLOCK_SIZE);
}

#digest_lengthInteger

Returns 4

Returns:

  • (Integer)


525
526
527
528
# File 'ext/digest/xxhash/ext.c', line 525

static VALUE _Digest_XXH32_digest_length(VALUE self)
{
	return INT2FIX(_XXH32_DIGEST_SIZE);
}

#initialize_copy(orig) ⇒ self

This method is called when instances are cloned. It is responsible for replicating internal data.

Returns:

  • (self)


514
515
516
517
518
# File 'ext/digest/xxhash/ext.c', line 514

static VALUE _Digest_XXH32_initialize_copy(VALUE self, VALUE orig)
{
	XXH32_copyState(_get_state_xxh32(self), _get_state_xxh32(orig));
	return self;
}

#reset(seed = 0) ⇒ self

Resets state to initial form with seed.

This discards previous calculations with #update.

seed can be in the form of a string, a hex string, or a number. Its virtual length should be 32-bits.

If seed is not provided, the default value would be 0.

Returns:

  • (self)


461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
# File 'ext/digest/xxhash/ext.c', line 461

static VALUE _Digest_XXH32_reset(int argc, VALUE* argv, VALUE self)
{
	VALUE seed;

	if (argc > 0 && rb_scan_args(argc, argv, "01", &seed) > 0) {
		switch (TYPE(seed)) {
		case T_STRING:
			{
				int len = RSTRING_LEN(seed);
				XXH32_hash_t decoded_seed;

				if (len == _TWICE(sizeof(XXH32_hash_t))) {
					unsigned char hex_decoded_seed[sizeof(XXH32_hash_t)];

					if (! hex_decode_str_implied(_RSTRING_PTR_U(seed), len, hex_decoded_seed))
						rb_raise(rb_eArgError, "Invalid hex string seed: %s\n",
								StringValueCStr(seed));

					decoded_seed = XXH_readBE32(hex_decoded_seed);
				} else if (len == sizeof(XXH32_hash_t)) {
					decoded_seed = XXH_readBE32(RSTRING_PTR(seed));
				} else {
					rb_raise(rb_eArgError, "Invalid seed length.  "
							"Expecting an 8-character hex string or a 4-byte string.");
				}

				_xxh32_reset(_get_state_xxh32(self), decoded_seed);
			}

			break;
		case T_FIXNUM:
			_xxh32_reset(_get_state_xxh32(self), FIX2UINT(seed));
			break;
		case T_BIGNUM:
			_xxh32_reset(_get_state_xxh32(self), NUM2UINT(seed));
			break;
		default:
			rb_raise(rb_eArgError, "Invalid argument type for 'seed'.  "
					"Expecting a string or a number.");
		}
	} else {
		_xxh32_reset(_get_state_xxh32(self), _XXH32_DEFAULT_SEED);
	}

	return self;
}

#update(str) ⇒ self

Updates current digest value with string.

Returns:

  • (self)


425
426
427
428
429
430
431
# File 'ext/digest/xxhash/ext.c', line 425

static VALUE _Digest_XXH32_update(VALUE self, VALUE str)
{
	if (XXH32_update(_get_state_xxh32(self), RSTRING_PTR(str), RSTRING_LEN(str)) != XXH_OK)
		rb_raise(rb_eRuntimeError, "Failed to update state.");

	return self;
}