Skip to content

Commit

Permalink
Removed Require.JS stub in favor of basic window.crc export.
Browse files Browse the repository at this point in the history
  • Loading branch information
alexgorbatchev committed Dec 19, 2010
1 parent d2bdb13 commit 64ef353
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 22 deletions.
13 changes: 9 additions & 4 deletions README.md
@@ -1,12 +1,12 @@
# CRC for node.js
# JavaScript CRC 8, 16 and 32.

This is a basic port/copy of the JavaScript CRC implementation. The module is compatible with Node.js and Require.js.
This is a basic port/copy of the JavaScript CRC implementation. The module works with any CommonJS system supporting `module.exports` notation as well as in the browser. When loaded in the browser, all functions end up under the `window.crc` "namespace".

This is an almost direct copy from http://www.digsys.se/JavaScript/CRC.aspx
Original code is taken from http://www.digsys.se/JavaScript/CRC.aspx

## Functions

The following functions are exported:
The following functions are implemented:

crc8(String) #=> Number
crcArc(String) #=> Number
Expand All @@ -19,4 +19,9 @@ The following functions are exported:

## Installation

git clone git://github.com/alexgorbatchev/node-crc.git

or

npm install crc

46 changes: 28 additions & 18 deletions lib/crc.js
@@ -1,7 +1,4 @@
// make this module compatible with RequireJS
typeof(define) == "undefined" && (define = function(deps, factory) { module.exports = factory.apply(this, deps.map(require)); });

define([], function()
(function()
{
// CRC-8 in table form
//
Expand Down Expand Up @@ -32,7 +29,7 @@ define([], function()
// 'crc' should be initialized to 0x00.
{
return CRC8_TAB[(crc^c)&0xFF];
}
};
// C/C++ language:
//
// inline unsigned char crc8Add(unsigned char crc, unsigned char c)
Expand Down Expand Up @@ -75,7 +72,7 @@ define([], function()
// 'crc' should be initialized to 0x0000.
{
return CRC_ARC_TAB[(crc^c)&0xFF]^((crc>>8)&0xFF);
}
};
// C/C++ language:
//
// inline unsigned short crcArcAdd(unsigned short crc, unsigned char c)
Expand Down Expand Up @@ -118,7 +115,7 @@ define([], function()
// 'crc' should be initialized to 0x0000.
{
return CRC16_TAB[((crc>>8)^c)&0xFF]^((crc<<8)&0xFFFF);
}
};
// C/C++ language:
//
// inline unsigned short crc16Add(unsigned short crc, unsigned char c)
Expand Down Expand Up @@ -165,7 +162,7 @@ define([], function()
// result will always be 0xF0B8 (without the complementation).
{
return FCS_16_TAB[(fcs^c)&0xFF]^((fcs>>8)&0xFF);
}
};

// C/C++ language:
//
Expand Down Expand Up @@ -233,7 +230,7 @@ define([], function()
// result will always be 0xDEBB20E3 (without the complementation).
{
return CRC32_TAB[(crc^c)&0xFF]^((crc>>8)&0xFFFFFF);
}
};
//
// C/C++ language:
//
Expand All @@ -254,7 +251,7 @@ define([], function()
crc = crc8Add(crc, str.charCodeAt(i));

return crc;
}
};

function crcArc(str)
{
Expand All @@ -267,7 +264,7 @@ define([], function()
crc = crcArcAdd(crc, str.charCodeAt(i));

return crc;
}
};

function crc16(str)
{
Expand All @@ -280,7 +277,7 @@ define([], function()
crc = crc16Add(crc, str.charCodeAt(i));

return crc;
}
};

function fcs16(str)
{
Expand All @@ -293,7 +290,7 @@ define([], function()
fcs = fcs16Add(fcs,str.charCodeAt(i));

return fcs^0xFFFF;
}
};

function crc32(str)
{
Expand All @@ -306,7 +303,7 @@ define([], function()
crc = crc32Add(crc, str.charCodeAt(i));

return crc^0xFFFFFFFF;
}
};

/**
* Convert value as 8-bit unsigned integer to 2 digit hexadecimal number.
Expand All @@ -321,25 +318,38 @@ define([], function()
str = "0" + str;

return str;
}
};

/**
* Convert value as 16-bit unsigned integer to 4 digit hexadecimal number.
*/
function hex16(val)
{
return hex8(val >> 8) + hex8(val);
}
};

/**
* Convert value as 32-bit unsigned integer to 8 digit hexadecimal number.
*/
function hex32(val)
{
return hex16(val >> 16) + hex16(val);
};

var target, property;

if(typeof(window) == 'undefined')
{
target = module;
property = 'exports';
}
else
{
target = window;
property = 'crc';
}

return {
target[property] = {
'crc8' : crc8,
'crcArc' : crcArc,
'crc16' : crc16,
Expand All @@ -349,4 +359,4 @@ define([], function()
'hex16' : hex16,
'hex32' : hex32
};
});
})();

0 comments on commit 64ef353

Please sign in to comment.