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

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.



533
534
535
536
# File 'ext/digest/xxhash/ext.c', line 533

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

.digest_lengthInteger

Returns 4.



523
524
525
526
# File 'ext/digest/xxhash/ext.c', line 523

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

Instance Method Details

#block_lengthInteger

Returns 4.



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

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

#digest_lengthInteger

Returns 4.



503
504
505
506
# File 'ext/digest/xxhash/ext.c', line 503

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.



492
493
494
495
496
# File 'ext/digest/xxhash/ext.c', line 492

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

#reset(seed = 0) ⇒ self

Resets state to initial form with seed.

This would discard previous calculations with #update.

seed can be in the form of a string, a hex string, or a number.

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



442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
# File 'ext/digest/xxhash/ext.c', line 442

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);
        uint32_t decoded_seed;

        if (len == (sizeof(uint32_t) * 2)) {
          unsigned char hex_decoded_seed[sizeof(uint32_t)];

          if (! hex_decode_str_implied(RSTRING_PTR(seed), sizeof(uint32_t) * 2, hex_decoded_seed))
            rb_raise(rb_eArgError, "Invalid hex string seed: %s\n", StringValueCStr(seed));

          decoded_seed = _decode_big_endian_32_cstr(hex_decoded_seed);
        } else if (len == sizeof(uint32_t)) {
          decoded_seed = _decode_big_endian_32_cstr(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_32(self), decoded_seed);
      }

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

  return self;
}

#update(str) ⇒ self

Updates current digest value with string.



409
410
411
412
413
414
415
# File 'ext/digest/xxhash/ext.c', line 409

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

  return self;
}