Class: TDriver::Checksum
- Defined in:
- lib/tdriver/util/common/crc16.rb,
ext/native_extensions.c
Class Method Summary collapse
-
.crc16(*args) ⇒ Object
static VALUE crc16_ibm( VALUE self, VALUE string ) {.
-
.crc16_ibm(*args) ⇒ Object
checksum methods.
-
.native_extension ⇒ Object
determines that native extension is in use.
Instance Method Summary collapse
-
#crc16(buf, crc = 0) ⇒ Object
CRC-16-CCITT.
-
#crc16_ibm(buf, crc = 0xffff) ⇒ Object
IBM-CRC-16: fallback when native extensions are not supported (e.g. jruby).
-
#native_extension ⇒ Object
determines that native extension is not in use.
Class Method Details
.crc16(*args) ⇒ Object
static VALUE crc16_ibm( VALUE self, VALUE string )
// verify argument type
Check_Type( string, T_STRING );
const char* data = RSTRING_PTR( string );
int len = RSTRING_LEN( string );
int crc = 0xffff;
int c = 0;
while( len-- != 0 ){
c = *data++;
crc = ( ( crc >> 4 ) & 0x0fff ) ^ CRC_16[ ( ( crc ^ c ) & 15 ) ];
crc = ( ( crc >> 4 ) & 0x0fff ) ^ CRC_16[ ( ( crc ^ ( c >> 4 )) & 15 ) ];
return INT2FIX( ~crc & 0xffff );
}
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 |
# File 'ext/native_extensions.c', line 100
static VALUE crc16( int argc, VALUE* argv, VALUE self ) {
/*
# CRC-16-CCITT
def crc16( buf, crc = 0 )
# calculate the checksum
buf.each_byte{ | x | crc = ( ( crc << 8 ) ^ @CCITT_16[ ( crc >> 8 ) ^ x ] ) & 0xffff }
# result
crc
end # crc16
*/
// variables for arguments
VALUE string, initial_crc;
// retrieve arguments
rb_scan_args(argc, argv, "11", &string, &initial_crc);
// verify argument type
Check_Type( string, T_STRING );
// initialize crc
unsigned int crc = 0;
if (!NIL_P(initial_crc)){
// verify initial crc value
Check_Type( initial_crc, T_FIXNUM );
crc = FIX2INT( initial_crc );
}
const unsigned char* data = RSTRING_PTR( string );
unsigned int len = RSTRING_LEN( string );
unsigned char c = 0;
while( len-- ){
c = *data++;
crc = ( ( crc << 8 ) ^ CCITT_16[ ( crc >> 8 ) ^ c ] ) & 0xffff;
}
return INT2FIX( crc );
}
|
.crc16_ibm(*args) ⇒ Object
checksum methods
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 |
# File 'ext/native_extensions.c', line 157
static VALUE crc16_ibm( int argc, VALUE* argv, VALUE self ) {
/*
# IBM-CRC-16: fallback when native extensions are not supported (e.g. jruby)
def crc16_ibm( buf, crc = 0xffff )
buf.each_byte do | c |
crc = ( ( crc >> 4 ) & 0x0fff ) ^ @IBM_16[ ( ( crc ^ c ) & 15 ) ]
crc = ( ( crc >> 4 ) & 0x0fff ) ^ @IBM_16[ ( ( crc ^ ( c >> 4 ) ) & 15 ) ]
end
# result
~crc & 0xffff
end # crc16_ibm
*/
// variables for arguments
VALUE string, initial_crc;
// retrieve arguments
rb_scan_args(argc, argv, "11", &string, &initial_crc);
unsigned int crc = 0xffff;
if (!NIL_P(initial_crc)){
// verify initial crc value
Check_Type( initial_crc, T_FIXNUM );
crc = FIX2INT( initial_crc );
}
// verify argument type
Check_Type( string, T_STRING );
const unsigned char* data = RSTRING_PTR( string );
unsigned int len = RSTRING_LEN( string );
unsigned char c = 0;
while( len-- ){
c = *data++;
crc = ( crc >> 4 ) ^ CRC_16[ ( crc ^ c ) & 15 ];
crc = ( crc >> 4 ) ^ CRC_16[ ( crc ^ ( c >> 4 ) ) & 15 ];
}
return INT2FIX( ~crc & 0xffff );
}
|
.native_extension ⇒ Object
determines that native extension is in use
221 222 223 224 225 |
# File 'ext/native_extensions.c', line 221 static VALUE native_extension( VALUE self ) { return Qtrue; } |
Instance Method Details
#crc16(buf, crc = 0) ⇒ Object
CRC-16-CCITT
80 81 82 83 84 85 86 87 88 |
# File 'lib/tdriver/util/common/crc16.rb', line 80 def crc16( buf, crc = 0 ) # calculate the checksum buf.each_byte{ | x | crc = ( ( crc << 8 ) ^ @CCITT_16[ ( crc >> 8 ) ^ x ] ) & 0xffff } # result crc end |
#crc16_ibm(buf, crc = 0xffff) ⇒ Object
IBM-CRC-16: fallback when native extensions are not supported (e.g. jruby)
98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/tdriver/util/common/crc16.rb', line 98 def crc16_ibm( buf, crc = 0xffff ) buf.each_byte do | c | crc = ( ( crc >> 4 ) & 0x0fff ) ^ @IBM_16[ ( ( crc ^ c ) & 15 ) ] crc = ( ( crc >> 4 ) & 0x0fff ) ^ @IBM_16[ ( ( crc ^ ( c >> 4 ) ) & 15 ) ] end # result ~crc & 0xffff end |
#native_extension ⇒ Object
determines that native extension is not in use
91 92 93 94 95 |
# File 'lib/tdriver/util/common/crc16.rb', line 91 def native_extension false end |