Class: Cosmos::Crc16

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

Overview

Calculates 16-bit CRCs over a buffer of data.

Constant Summary collapse

DEFAULT_POLY =

CRC-16-CCITT default polynomial

0x1021
DEFAULT_SEED =

Seed for 16-bit CRC

0xFFFF

Instance Attribute Summary

Attributes inherited from Crc

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

Instance Method Summary collapse

Constructor Details

#initialize(poly = DEFAULT_POLY, seed = DEFAULT_SEED, xor = false, reflect = false) ⇒ Crc16

Creates a 16 bit CRC algorithm instance. By default it is initialzed to use the CRC-16-CCITT 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: false)

    Whether to XOR the CRC result with 0xFFFF

  • reflect (Boolean) (defaults to: false)

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



106
107
108
109
110
111
# File 'lib/cosmos/utilities/crc.rb', line 106

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

Instance Method Details

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

Calculate a 16-bit CRC



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'ext/cosmos/ext/crc/crc.c', line 91

static VALUE crc16_calculate(int argc, VALUE* argv, VALUE self)
{
  volatile VALUE param_data = Qnil;
  volatile VALUE param_seed = Qnil;
  unsigned char* data = NULL;
  unsigned short* table = NULL;
  int i = 0;
  long length = 0;
  unsigned short 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 short*) 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 >> 8) ^ bit_reverse_8(data[i])];
    }

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

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