Advertisement
stream13

CRC16 miner

Jan 4th, 2018
250
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.60 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdio>
  3.  
  4. #include <pthread.h>
  5. #include <vector>
  6. #include <unistd.h>
  7. #include <arpa/inet.h>
  8. #include <algorithm>
  9.  
  10. using namespace std;
  11.  
  12. #define MAXINT 0xFFFFFFFF
  13. #define CALC_STEP 1000000
  14. #define N_THREADS 8
  15.  
  16. #define CRC16 0x8005
  17.  
  18. pthread_mutex_t mtx_count = PTHREAD_MUTEX_INITIALIZER;
  19. pthread_mutex_t mtx_found = PTHREAD_MUTEX_INITIALIZER;
  20. vector<uint32_t> found;
  21. uint32_t my_count = 0;
  22.  
  23. void hd(uint8_t* data, size_t size){ //HEX DUMP
  24.     if(nullptr == data || size == 0)
  25.         return;
  26.  
  27.     printf("Dumping \t%u bytes:\t", size);
  28.     for(size_t i = 0; i < size; ++i){
  29.         printf("%02X ", data[i]);
  30.     }
  31.     printf("\n");
  32. }
  33.  
  34.  
  35. uint16_t crc16_ARC(const uint8_t *data, size_t size) {
  36.     uint16_t out = 0;
  37.     int bits_read = 0, bit_flag;
  38.  
  39.     /* Sanity check: */
  40.     if(data == NULL)
  41.         return 0;
  42.  
  43.     while(size > 0) {
  44.         bit_flag = out >> 15;
  45.  
  46.         /* Get next bit: */
  47.         out <<= 1;
  48.         out |= (*data >> bits_read) & 1; // item a) work from the least significant bits
  49.  
  50.         /* Increment bit counter: */
  51.         bits_read++;
  52.         if(bits_read > 7) {
  53.             bits_read = 0;
  54.             data++;
  55.             size--;
  56.         }
  57.  
  58.         /* Cycle check: */
  59.         if(bit_flag)
  60.             out ^= CRC16;
  61.     }
  62.  
  63.     // item b) "push out" the last 16 bits
  64.     int i;
  65.     for (i = 0; i < 16; ++i) {
  66.         bit_flag = out >> 15;
  67.         out <<= 1;
  68.         if(bit_flag)
  69.             out ^= CRC16;
  70.     }
  71.  
  72.     // item c) reverse the bits
  73.     uint16_t crc = 0;
  74.     i = 0x8000;
  75.     int j = 0x0001;
  76.     for (; i != 0; i >>=1, j <<= 1) {
  77.         if (i & out) crc |= j;
  78.     }
  79.  
  80.     return crc;
  81. }
  82.  
  83. uint16_t crc16_CCITT_FALSE(const uint8_t* data_p, size_t length) {
  84.     uint8_t x;
  85.     uint16_t crc = 0xFFFF;
  86.  
  87.     while (length--){
  88.         x = crc >> 8 ^ *data_p++;
  89.         x ^= x>>4;
  90.         crc = (crc << 8) ^ ((uint16_t)(x << 12)) ^ ((uint16_t)(x <<5)) ^ ((uint16_t)x);
  91.     }
  92.     return crc;
  93. }
  94.  
  95.  
  96. struct u32_tuple{
  97.     uint32_t a;
  98.     uint32_t b;
  99. };
  100.  
  101. void chk_rng(u32_tuple&rng, vector<uint32_t>&local_found){
  102. //  vector<uint32_t> local_found;
  103.     uint8_t* d8;
  104.     uint8_t* c8;
  105.     uint32_t d32;
  106.     uint32_t dn32;
  107.     uint16_t crc;
  108.  
  109.     if(!(rng.b > rng.a)) {
  110.         cerr << "Invalid range!" << endl;
  111.     }
  112.  
  113.     d8 = (uint8_t*)(&dn32);
  114.     c8 = (uint8_t*)(&crc);
  115.  
  116.     for(d32 = rng.a; d32 < rng.b; ++d32){
  117.         dn32 = htonl(d32);
  118.         crc = crc16_ARC(d8, 4);
  119.         if(c8[0] == d8[3] && c8[1] == d8[2]){
  120.             local_found.push_back(d32);
  121.         }
  122.     }
  123. }
  124.  
  125. void *th_fun(void*){
  126.     u32_tuple t;
  127.     vector<uint32_t> v;
  128.     bool roll = true;
  129.  
  130.     while (roll) {
  131.         pthread_mutex_lock(&mtx_count);
  132.         if(MAXINT == my_count){
  133.             roll = false;
  134.         }else if((MAXINT - my_count) < CALC_STEP){
  135.             t.a = my_count;
  136.             my_count = MAXINT;
  137.             t.b = my_count;
  138.         }else{
  139.             t.a = my_count;
  140.             my_count += 100000;
  141.             t.b = my_count;
  142.         }
  143.         pthread_mutex_unlock(&mtx_count);
  144.         if(false == roll){
  145.             continue;
  146.         }
  147.         v.clear();
  148.         chk_rng(t, v);
  149.         pthread_mutex_lock(&mtx_found);
  150.         found.insert( found.end(), v.begin(), v.end() );
  151.         pthread_mutex_unlock(&mtx_found);
  152.     }
  153.     return nullptr;
  154. }
  155.  
  156. inline float percent_of(float small, float big){
  157.     return (100*small)/big;
  158. }
  159.  
  160.  
  161. int main() {
  162. //  char s1[10] = "123456789";
  163.  
  164. //  uint32_t s = htonl(0x8962FAFF); // data FFFA6289 -- 8962FAFF
  165. //  size_t size = sizeof(s);
  166. //  uint16_t crc16 = crc16_ARC((uint8_t*)&s, size);
  167. ////    uint16_t crc16_cf = crc16_CCITT_FALSE((uint8_t*)&s, size);
  168.  
  169. //  hd((uint8_t*)&s1, 9);
  170. //  hd((uint8_t*)&s,size);
  171. //  hd((uint8_t*)&crc16,2);
  172.  
  173. //  cout << "CRC16/ARC = 0x" << std::hex << crc16 << endl;
  174. ////    cout << "CRC16/CCITT-FALSE = 0x" << std::hex << crc16_cf << endl;
  175.  
  176. ///////////////////////////////////////////////////////////////////////////////
  177.  
  178.     FILE * pFile;
  179.  
  180.     pthread_t th[N_THREADS];
  181.     uint32_t cnt_local;
  182.     uint32_t fnd_local;
  183.  
  184.     for(int i = 0; i < N_THREADS; ++i){
  185.         if(pthread_create(&th[i], NULL, th_fun, NULL)) {
  186.         fprintf(stderr, "Error creating thread %d;\n", i);
  187.         return 1;
  188.         }
  189.     }
  190.     printf("  \%  \tCOUNT\t\tFOUND\n");
  191.     while (my_count < MAXINT) {
  192.         pthread_mutex_lock(&mtx_count);
  193.         cnt_local = my_count;
  194.         pthread_mutex_unlock(&mtx_count);
  195.         pthread_mutex_lock(&mtx_found);
  196.         fnd_local = found.size();
  197.         pthread_mutex_unlock(&mtx_found);
  198.         printf("\r[%.1f]\t%08X\t%d", percent_of((float)cnt_local, (float)MAXINT), cnt_local, fnd_local);
  199.         fflush(stdout);
  200.         sleep(1);
  201.     }
  202.  
  203.     for(int i = 0; i < N_THREADS; ++i){
  204.         if(pthread_join(th[i], NULL)) {
  205.  
  206.         fprintf(stderr, "Error joining thread %d;\n", i);
  207.         return 2;
  208.  
  209.         }
  210.     }
  211.  
  212.     printf("\nSorting data...\n");
  213.     std::sort(found.begin(), found.end());
  214.     pFile = fopen ("crc16_ARC.txt","w");
  215.  
  216.     printf("Saving to file...\n");
  217.     for(size_t i = 0; i < found.size(); ++i){
  218. //      printf("0x%08X\n", found[i]);
  219.         fprintf(pFile, "0x%08X\n", found[i]);
  220.     }
  221.     fclose(pFile);
  222.  
  223.     return 0;
  224. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement