Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Added buffer variants for crc 8/16/32

  • Loading branch information...
commit 830159d36ebe20c12bf3ed05b8afdae61f24f778 1 parent 9c78fae
TJ Holowaychuk authored

Showing 2 changed files with 42 additions and 1 deletion. Show diff stats Hide diff stats

  1. +30 1 lib/crc.js
  2. +12 0 test/crc.js
31 lib/crc.js
@@ -253,6 +253,14 @@
253 253 return crc;
254 254 };
255 255
  256 + function crc8Buffer(buf) {
  257 + var crc = 0;
  258 + for (var i = 0, len = buf.length; i < len; ++i) {
  259 + crc = crc8Add(crc, buf[i]);
  260 + }
  261 + return crc;
  262 + }
  263 +
256 264 function crcArc(str)
257 265 {
258 266 var i,
@@ -279,6 +287,14 @@
279 287 return crc;
280 288 };
281 289
  290 + function crc16Buffer(buf) {
  291 + var crc = 0;
  292 + for (var i = 0, len = buf.length; i < len; ++i) {
  293 + crc = crc16Add(crc, buf[i]);
  294 + }
  295 + return crc;
  296 + }
  297 +
282 298 function fcs16(str)
283 299 {
284 300 var i,
@@ -305,6 +321,14 @@
305 321 return crc^0xFFFFFFFF;
306 322 };
307 323
  324 + function crc32Buffer(buf) {
  325 + var crc = 0xFFFFFFFF;
  326 + for (var i = 0, len = buf.length; i < len; ++i) {
  327 + crc = crc32Add(crc, buf[i]);
  328 + }
  329 + return crc ^ 0xFFFFFFFF;
  330 + }
  331 +
308 332 /**
309 333 * Convert value as 8-bit unsigned integer to 2 digit hexadecimal number.
310 334 */
@@ -357,6 +381,11 @@
357 381 'crc32' : crc32,
358 382 'hex8' : hex8,
359 383 'hex16' : hex16,
360   - 'hex32' : hex32
  384 + 'hex32' : hex32,
  385 + 'buffer' : {
  386 + crc8: crc8Buffer,
  387 + crc16: crc16Buffer,
  388 + crc32: crc32Buffer
  389 + }
361 390 };
362 391 })();
12 test/crc.js
@@ -6,18 +6,30 @@ describe('crc8()', function(){
6 6 it('should work with strings', function(){
7 7 crc.crc8('hello world').should.equal(64);
8 8 })
  9 +
  10 + it('should work with Buffers', function(){
  11 + crc.buffer.crc8(new Buffer('hello world')).should.equal(64);
  12 + })
9 13 })
10 14
11 15 describe('crc16()', function(){
12 16 it('should work with strings', function(){
13 17 crc.crc16('hello world').should.equal(15332);
14 18 })
  19 +
  20 + it('should work with Buffers', function(){
  21 + crc.buffer.crc16(new Buffer('hello world')).should.equal(15332);
  22 + })
15 23 })
16 24
17 25 describe('crc32()', function(){
18 26 it('should work with strings', function(){
19 27 crc.crc32('hello world').should.equal(222957957);
20 28 })
  29 +
  30 + it('should work with Buffers', function(){
  31 + crc.buffer.crc32(new Buffer('hello world')).should.equal(222957957);
  32 + })
21 33 })
22 34
23 35 describe('crcArc()', function(){

0 comments on commit 830159d

Please sign in to comment.
Something went wrong with that request. Please try again.