Class: OpenC3::Crc32

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

Overview

Calculates 32-bit CRCs over a buffer of data.

Constant Summary collapse

DEFAULT_POLY =

CRC-32 default polynomial

0x04C11DB7
DEFAULT_SEED =

Default Seed for 32-bit CRC

0xFFFFFFFF

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) ⇒ Crc32

Creates a 32 bit CRC algorithm instance. By default it is initialzed to use the CRC-32 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



282
283
284
285
286
287
# File 'lib/openc3/utilities/crc.rb', line 282

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_crc32

Calculate a 32-bit CRC



259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
# File 'ext/openc3/ext/crc/crc.c', line 259

static VALUE crc32_calculate(int argc, VALUE *argv, VALUE self)
{
  volatile VALUE param_data = Qnil;
  volatile VALUE param_seed = Qnil;
  unsigned char *data = NULL;
  unsigned int *table = NULL;
  int i = 0;
  long length = 0;
  unsigned int 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 = NUM2UINT(param_seed);
  data = (unsigned char *)RSTRING_PTR(param_data);
  length = RSTRING_LEN(param_data);
  table = (unsigned int *)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 >> 24) ^ bit_reverse_8(data[i])) & 0x000000FF];
    }

    if (RTEST(rb_ivar_get(self, id_ivar_xor)))
    {
      return UINT2NUM(bit_reverse_32(crc ^ 0xFFFFFFFF));
    }
    else
    {
      return UINT2NUM(bit_reverse_32(crc));
    }
  }
  else
  {
    for (i = 0; i < length; i++)
    {
      crc = (crc << 8) ^ table[((crc >> 24) ^ data[i]) & 0x000000FF];
    }

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