Browse Source

CRC-8-CCITT implementation

Luke Dashjr 12 years ago
parent
commit
5eeaaf069d
3 changed files with 28 additions and 0 deletions
  1. 1 0
      miner.c
  2. 23 0
      util.c
  3. 4 0
      util.h

+ 1 - 0
miner.c

@@ -11001,6 +11001,7 @@ int main(int argc, char *argv[])
 #ifndef HAVE_PTHREAD_CANCEL
 #ifndef HAVE_PTHREAD_CANCEL
 	setup_pthread_cancel_workaround();
 	setup_pthread_cancel_workaround();
 #endif
 #endif
+	bfg_init_checksums();
 
 
 #ifdef WIN32
 #ifdef WIN32
 	{
 	{

+ 23 - 0
util.c

@@ -2911,3 +2911,26 @@ void run_cmd(const char *cmd)
 	pthread_t pth;
 	pthread_t pth;
 	pthread_create(&pth, NULL, cmd_thread, (void*)cmd);
 	pthread_create(&pth, NULL, cmd_thread, (void*)cmd);
 }
 }
+
+
+static uint8_t _crc8ccitt_table[0x100];
+
+void bfg_init_checksums(void)
+{
+	for (int i = 0; i < 0x100; ++i)
+	{
+		uint8_t crc = i;
+		for (int j = 0; j < 8; ++j)
+			crc = (crc << 1) ^ ((crc & 0x80) ? 7 : 0);
+		_crc8ccitt_table[i] = crc & 0xff;
+	}
+}
+
+uint8_t crc8ccitt(const void * const buf, const size_t buflen)
+{
+	const uint8_t *p = buf;
+	uint8_t crc = 0xff;
+	for (int i = 0; i < buflen; ++i)
+		crc = _crc8ccitt_table[crc ^ *p++];
+	return crc;
+}

+ 4 - 0
util.h

@@ -500,4 +500,8 @@ void maybe_strdup_if_null(const char **p, const char *s)
 extern void run_cmd(const char *cmd);
 extern void run_cmd(const char *cmd);
 
 
 
 
+extern void bfg_init_checksums(void);
+extern uint8_t crc8ccitt(const void *, size_t);
+
+
 #endif /* __UTIL_H__ */
 #endif /* __UTIL_H__ */