crc8.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. var isStr = require('./isStr');
  2. var strToBytes = require('./strToBytes');
  3. var TABLE = [
  4. 0x00,
  5. 0x07,
  6. 0x0e,
  7. 0x09,
  8. 0x1c,
  9. 0x1b,
  10. 0x12,
  11. 0x15,
  12. 0x38,
  13. 0x3f,
  14. 0x36,
  15. 0x31,
  16. 0x24,
  17. 0x23,
  18. 0x2a,
  19. 0x2d,
  20. 0x70,
  21. 0x77,
  22. 0x7e,
  23. 0x79,
  24. 0x6c,
  25. 0x6b,
  26. 0x62,
  27. 0x65,
  28. 0x48,
  29. 0x4f,
  30. 0x46,
  31. 0x41,
  32. 0x54,
  33. 0x53,
  34. 0x5a,
  35. 0x5d,
  36. 0xe0,
  37. 0xe7,
  38. 0xee,
  39. 0xe9,
  40. 0xfc,
  41. 0xfb,
  42. 0xf2,
  43. 0xf5,
  44. 0xd8,
  45. 0xdf,
  46. 0xd6,
  47. 0xd1,
  48. 0xc4,
  49. 0xc3,
  50. 0xca,
  51. 0xcd,
  52. 0x90,
  53. 0x97,
  54. 0x9e,
  55. 0x99,
  56. 0x8c,
  57. 0x8b,
  58. 0x82,
  59. 0x85,
  60. 0xa8,
  61. 0xaf,
  62. 0xa6,
  63. 0xa1,
  64. 0xb4,
  65. 0xb3,
  66. 0xba,
  67. 0xbd,
  68. 0xc7,
  69. 0xc0,
  70. 0xc9,
  71. 0xce,
  72. 0xdb,
  73. 0xdc,
  74. 0xd5,
  75. 0xd2,
  76. 0xff,
  77. 0xf8,
  78. 0xf1,
  79. 0xf6,
  80. 0xe3,
  81. 0xe4,
  82. 0xed,
  83. 0xea,
  84. 0xb7,
  85. 0xb0,
  86. 0xb9,
  87. 0xbe,
  88. 0xab,
  89. 0xac,
  90. 0xa5,
  91. 0xa2,
  92. 0x8f,
  93. 0x88,
  94. 0x81,
  95. 0x86,
  96. 0x93,
  97. 0x94,
  98. 0x9d,
  99. 0x9a,
  100. 0x27,
  101. 0x20,
  102. 0x29,
  103. 0x2e,
  104. 0x3b,
  105. 0x3c,
  106. 0x35,
  107. 0x32,
  108. 0x1f,
  109. 0x18,
  110. 0x11,
  111. 0x16,
  112. 0x03,
  113. 0x04,
  114. 0x0d,
  115. 0x0a,
  116. 0x57,
  117. 0x50,
  118. 0x59,
  119. 0x5e,
  120. 0x4b,
  121. 0x4c,
  122. 0x45,
  123. 0x42,
  124. 0x6f,
  125. 0x68,
  126. 0x61,
  127. 0x66,
  128. 0x73,
  129. 0x74,
  130. 0x7d,
  131. 0x7a,
  132. 0x89,
  133. 0x8e,
  134. 0x87,
  135. 0x80,
  136. 0x95,
  137. 0x92,
  138. 0x9b,
  139. 0x9c,
  140. 0xb1,
  141. 0xb6,
  142. 0xbf,
  143. 0xb8,
  144. 0xad,
  145. 0xaa,
  146. 0xa3,
  147. 0xa4,
  148. 0xf9,
  149. 0xfe,
  150. 0xf7,
  151. 0xf0,
  152. 0xe5,
  153. 0xe2,
  154. 0xeb,
  155. 0xec,
  156. 0xc1,
  157. 0xc6,
  158. 0xcf,
  159. 0xc8,
  160. 0xdd,
  161. 0xda,
  162. 0xd3,
  163. 0xd4,
  164. 0x69,
  165. 0x6e,
  166. 0x67,
  167. 0x60,
  168. 0x75,
  169. 0x72,
  170. 0x7b,
  171. 0x7c,
  172. 0x51,
  173. 0x56,
  174. 0x5f,
  175. 0x58,
  176. 0x4d,
  177. 0x4a,
  178. 0x43,
  179. 0x44,
  180. 0x19,
  181. 0x1e,
  182. 0x17,
  183. 0x10,
  184. 0x05,
  185. 0x02,
  186. 0x0b,
  187. 0x0c,
  188. 0x21,
  189. 0x26,
  190. 0x2f,
  191. 0x28,
  192. 0x3d,
  193. 0x3a,
  194. 0x33,
  195. 0x34,
  196. 0x4e,
  197. 0x49,
  198. 0x40,
  199. 0x47,
  200. 0x52,
  201. 0x55,
  202. 0x5c,
  203. 0x5b,
  204. 0x76,
  205. 0x71,
  206. 0x78,
  207. 0x7f,
  208. 0x6a,
  209. 0x6d,
  210. 0x64,
  211. 0x63,
  212. 0x3e,
  213. 0x39,
  214. 0x30,
  215. 0x37,
  216. 0x22,
  217. 0x25,
  218. 0x2c,
  219. 0x2b,
  220. 0x06,
  221. 0x01,
  222. 0x08,
  223. 0x0f,
  224. 0x1a,
  225. 0x1d,
  226. 0x14,
  227. 0x13,
  228. 0xae,
  229. 0xa9,
  230. 0xa0,
  231. 0xa7,
  232. 0xb2,
  233. 0xb5,
  234. 0xbc,
  235. 0xbb,
  236. 0x96,
  237. 0x91,
  238. 0x98,
  239. 0x9f,
  240. 0x8a,
  241. 0x8d,
  242. 0x84,
  243. 0x83,
  244. 0xde,
  245. 0xd9,
  246. 0xd0,
  247. 0xd7,
  248. 0xc2,
  249. 0xc5,
  250. 0xcc,
  251. 0xcb,
  252. 0xe6,
  253. 0xe1,
  254. 0xe8,
  255. 0xef,
  256. 0xfa,
  257. 0xfd,
  258. 0xf4,
  259. 0xf3
  260. ];
  261. if (typeof Int32Array !== 'undefined') TABLE = new Int32Array(TABLE);
  262. exports = function(input, previous) {
  263. return exports.signed(input, previous) >>> 0;
  264. };
  265. exports.signed = function(input, previous) {
  266. if (isStr(input)) input = strToBytes(input);
  267. var crc = ~~previous;
  268. for (var i = 0, len = input.length; i < len; i++) {
  269. var byte = input[i];
  270. crc = TABLE[(crc ^ byte) & 0xff] & 0xff;
  271. }
  272. return crc;
  273. };
  274. module.exports = exports;