Class: OpenC3::Crc64

Inherits:
Crc show all
Defined in:
lib/openc3/utilities/crc.rb,
ext/openc3/ext/crc/crc.c

Overview

Calculates 64-bit CRCs over a buffer of data.

Constant Summary collapse

DEFAULT_POLY =

CRC-64-ECMA default polynomial

0x42F0E1EBA9EA3693
DEFAULT_SEED =

Default Seed for 64-bit CRC

0xFFFFFFFFFFFFFFFF

Constants inherited from Crc

OpenC3::Crc::BIT_REVERSE_TABLE

Instance Attribute Summary

Attributes inherited from Crc

#poly, #reflect, #seed, #table, #xor

Instance Method Summary collapse

Methods inherited from Crc

#bit_reverse_16, #bit_reverse_32, #bit_reverse_64, #bit_reverse_8

Constructor Details

#initialize(poly = DEFAULT_POLY, seed = DEFAULT_SEED, xor = true, reflect = true) ⇒ Crc64

Creates a 64 bit CRC algorithm instance. By default it is initialized to use the algorithm.

Parameters:

  • poly (Integer) (defaults to: DEFAULT_POLY)

    Polynomial to use when calculating the CRC

  • seed (Integer) (defaults to: DEFAULT_SEED)

    Seed value to start the calculation

  • xor (Boolean) (defaults to: true)

    Whether to XOR the CRC result with 0xFFFF

  • reflect (Boolean) (defaults to: true)

    Whether to bit reverse each byte of data before calculating the CRC



307
308
309
310
311
312
# File 'lib/openc3/utilities/crc.rb', line 307

def initialize(poly = DEFAULT_POLY,
               seed = DEFAULT_SEED,
               xor = true,
               reflect = true)
  super(poly, seed, xor, reflect)
end

Instance Method Details

#calc(*args) ⇒ Object Also known as: calculate_crc64

Calculate a 64-bit CRC



336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
# File 'ext/openc3/ext/crc/crc.c', line 336

static VALUE crc64_calculate(int argc, VALUE *argv, VALUE self)
{
  volatile VALUE param_data = Qnil;
  volatile VALUE param_seed = Qnil;
  unsigned char *data = NULL;
  unsigned long long *table = NULL;
  int i = 0;
  long length = 0;
  unsigned long long crc = 0;

  switch (argc)
  {
  case 1:
    Check_Type(argv[0], T_STRING);
    param_data = argv[0];
    param_seed = rb_ivar_get(self, id_ivar_seed);
    break;
  case 2:
    Check_Type(argv[0], T_STRING);
    param_data = argv[0];
    if (argv[1] == Qnil)
    {
      param_seed = rb_ivar_get(self, id_ivar_seed);
    }
    else
    {
      param_seed = argv[1];
    }
    break;
  default:
    /* Invalid number of arguments given */
    rb_raise(rb_eArgError, "wrong number of arguments (%d for 1..2)", argc);
    break;
  };

  crc = NUM2ULL(param_seed);
  data = (unsigned char *)RSTRING_PTR(param_data);
  length = RSTRING_LEN(param_data);
  table = (unsigned long long *)RSTRING_PTR(rb_ivar_get(self, id_ivar_table));

  if (RTEST(rb_ivar_get(self, id_ivar_reflect)))
  {
    for (i = 0; i < length; i++)
    {
      crc = (crc << 8) ^ table[((crc >> 56) ^ bit_reverse_8(data[i])) & 0x00000000000000FFULL];
    }

    if (RTEST(rb_ivar_get(self, id_ivar_xor)))
    {
      return ULL2NUM(bit_reverse_64(crc ^ 0xFFFFFFFFFFFFFFFFULL));
    }
    else
    {
      return ULL2NUM(bit_reverse_64(crc));
    }
  }
  else
  {
    for (i = 0; i < length; i++)
    {
      crc = (crc << 8) ^ table[((crc >> 56) ^ data[i]) & 0x00000000000000FFULL];
    }

    if (RTEST(rb_ivar_get(self, id_ivar_xor)))
    {
      return ULL2NUM(crc ^ 0xFFFFFFFFFFFFFFFFULL);
    }
    else
    {
      return ULL2NUM(crc);
    }
  }
}